Q:

AVFrame: How to get/replace plane data buffer(s) and size?

I'm working on gstreamer1.0-libav (1.6.3), trying to port custom FPGA based H264 video acceleration from gstreamer 0.10. 
The data planes (YUV) used to be allocated by a simple malloc back in gstreamer 0.10, so we simply replaced the AVFrame.data[i] pointers by pointers to memory in our video acceleration core. It seems to be MUCH more complicated in gstreamer 1.12. 
For starters, I tried copying the YUV planes from AVFrame.data[i] to a separate buffer - which worked fine! Since I haven't seen an immediate way to obtain the size of AVFrame.data[i] and I recognized that data[0], data[1], data[2] seem to be in a single continuous buffer, I simply used (data[1] - data [0]) for the size of the Y plane and (data[2] - data[1]) for the sizes of the U/V planes respectively. This works fine, expect for one scenario:

Input H264 stream with resolution of 800x600 or greater
The camera is covered (jacket, hand, ...)

This causes a SEGFAULT in the memcpy of the V plane (data[2]) using the sizes determined as described above. Before covering the camera, the stream is displayed completely fine ... so for some reason the dark screen changes the plane sizes?
My ultimate goal is replacing the data[i] pointers allocated by gstreamer by my custom memory allocation (for futher processing) ... where exactly are these buffers assigned, can I change them and how can I obtain the size of each plane (data[0], data[1], data[2])?

A:

Do not use hacks like (data[1] - data [0]) planes may have padding.
You can obtain buffer sizes like this:
Y = data[0]  
U = data[1]  
V = data[2]

YUV444/422 Format:
Size of data[0] is linesize[0] * AVFrame::height
Size of data[1] is linesize[1] * AVFrame::height
Size of data[2] is linesize[2] * AVFrame::height

YUV420 Format:
Size of data[0] is linesize[0] * AVFrame::height
Size of data[1] is linesize[1] * (AVFrame::height / 2)
Size of data[2] is linesize[2] * (AVFrame::height / 2)

If there is an alpha channel use same formula as Y.
Hope that helps.

