How to wrap NDK AImage containg YUV420 planar or semi-planar into cv::Mat

Hey folks,

I’m writing an NDK based application and am getting YUV image data fed into a surface which is delivered to my code in an AImage* structure. If the image is coming from the camera, it can be semi-planar in NV21 form so one plane of Y and then one plane of VU of same width but half the height. If the image is coming from the decoder then I get planar YUV420 so 3 planes with the U and V planes with 1/4 the number of pixels.

I want to wrap this in a cv::Mat using one of the constructors that accept an external data pointer. I know how detect if I’m getting semi planar or planar YUVs. The problem is that I’m not sure that the all the planes that comes from the NDK is a contiguous block. Furthermore, the dimensions for the planes are not the same since there are fewer pixels in the UV plains. How would I go about wrapping the AImage. Has anyone done something like this before?