Search code examples
androidyuvlibyuv

Building parameters for libyuv's NV21ToI420


In my Android application, I get Camera output in NV21 format as a byte array. On the native side, I need to convert this array to I420 format using libyuv's NV21ToI420 function. Here is its definition:

LIBYUV_API
int NV21ToI420(const uint8* src_y, int src_stride_y,
           const uint8* src_vu, int src_stride_vu,
           uint8* dst_y, int dst_stride_y,
           uint8* dst_u, int dst_stride_u,
           uint8* dst_v, int dst_stride_v,
           int width, int height);

All I have is a pointer to NV21 data, the width and the height. I am wondering how one can compute the values for src_stride_y, src_stride_vu, etc. Appreciate it if someone can share some sample code on how to properly use this function. Searching the net for NV21ToI420 did not result in any example. Regards.


Solution

  • src_stride_y is usually the width of the frame and src_stride_uv is half the frame width (since there is a downsampling in the horizontal dimension for any YUV42x format). In some cases, the width is a funky number and using it would cause great inefficiency in the memory arrangement, for instance if you're using a width of, say, 135. In that case, the processor/compiler/library/OS would pad with zeros/ones/garbage each row until this size is more computation friendly -- multiple of 4/64 bytes or similar. This reasoning applies to dst_stride_y (~width) and dst_stride_u/dst_stride_v (~width/2). IIRC, if you select the capture sizes offered by the camera in Android, you should not have strides that differ from width and width/2, so you should be good to go.