IOS libyuv open-source library and ioslibyuv open-source Library
Libyuv is an open-source Google library that can be used to convert image data formats, such as the format conversion during video stream encoding/decoding and YUV data conversion to RGB.
Libyuv static library
For ease of use, the libyuv source code has been packaged into the iOS static library and the libyuv static library.
Use libyuv
The following example shows how to convert nv12 (yuv420sp) to I420 (yuv420p:
Void nv12_to_yuv420p (unsigned char * nv12, unsigned char * yuv420p, int width, int height ){
Int y_size = width * height;
Int u_size = y_size/4;
Int v_size = u_size;
Int uv_size = u_size + v_size;
Unsigned char * src_y = malloc (y_size );
Unsigned char * src_uv = malloc (uv_size );
Memcpy (src_y, nv12, y_size );
Memcpy (src_uv, nv12 + y_size, uv_size );
Unsigned char * dst_y = malloc (y_size );
Unsigned char * dst_u = malloc (u_size );
Unsigned char * dst_v = malloc (v_size );
Int src_stride_y = width;
Int src_stride_uv = width;
Int dst_stride_y = width;
Int dst_stride_u = width> 1;
Int dst_stride_v = dst_stride_u;
NV12ToI420 (src_y, src_stride_y, src_uv, src_stride_uv, dst_y, dst_stride_y, dst_u, distance, dst_v, dst_stride_v, width, height );
Memcpy (yuv420p, dst_y, y_size );
Memcpy (yuv420p + y_size, dst_u, u_size );
Memcpy (yuv420p + y_size + u_size, dst_v, v_size );
Free (src_y );
Free (src_uv );
Free (dst_y );
Free (dst_u );
Free (dst_v );
}
There are multiple implementation methods for converting nv12 to yuv420p, such as using libyuv and ffmpeg. In addition to using a third-party library for conversion, you can also implement the conversion by yourself. The Code is as follows:
Void yuv420sp_to_yuv420p (unsigned char * yuv420sp, unsigned char * yuv420p, int width, int height)
{
Int I, j;
Int y_size = width * height;
Unsigned char * y = yuv420sp;
Unsigned char * uv = yuv420sp + y_size;
Unsigned char * y_tmp = yuv420p;
Unsigned char * u_tmp = yuv420p + y_size;
Unsigned char * v_tmp = yuv420p + y_size * 5/4;
Memcpy (y_tmp, y, y_size );
For (j = 0, I = 0; j <y_size/2; j + = 2, I ++)
{
U_tmp [I] = uv [j];
V_tmp [I] = uv [j + 1];
}
}