Transferred from: https://my.oschina.net/u/589963/blog/167766
The YUV format typically has two main classes: the packaged (packed) format and the planar format. The former stores YUV components in the same array, usually several adjacent pixels to form a macro pixel (Macro-pixel), and the latter uses three arrays to store the YUV three components separately.
yuv420p (planar format) is stored in ffmpeg in the data[] array of the struct avframe
Data[0]-------y component data[1]------u component Data[2]-------V Component
YUV420P's memory structure:
4 y components correspond to 1 UV components
The YUV420 format is that each pixel retains a Y (luminance) component, and in the horizontal direction, not each row takes the U and V components, but a row only takes the U component, then the next line takes only the V component, repeating (i.e. 4:2:0, 4:0:2, 4:2:0, 4:0:2 ...), So 420 does not mean no V, but a line of sampling takes u only, and the other line samples only v. At the time of taking U and V, take a U or v between each of the two Y. However, from the 4x4 matrix column, there is only one U and V in each of the 4 matrix point y regions, so their ratio is 4:1. So for a pixel, RGB requires 8 * 3 = 24 bits, which is 3 bytes, while yuv420p,8 + 8/4 + 8/4 = 12 bits, which accounts for 2 bytes, where 8 refers to the Y component, and 8/4 refers to the U and V components.
YUV420 Memory Format:
YYYYYYYY UU VV------The most common
YYYYYYYY UU VV------Yes, but I haven't seen it in my development.
We use FFmpeg to store the yuv420p in a file, and then use some YUV player can play YUV raw data, seemingly VLC can not play YUV raw data:
data[] is stored in the YUV raw data (in the struct avframe, located in frame.h----ffmpeg)