B-frame
YUV
Introduction
The introduction section is fetched from this GitHub page. Copyright Jim-Bar.webarchive
First of all: YUV pixel formats and Recommended 8-Bit YUV Formats for Video Rendering. Chromium’s source code contains good documentation about those formats too: chromium/src/media/base/video_types.h and chromium/src/media/base/video_frame.cc (search for RequiresEvenSizeAllocation()
, NumPlanes()
and those kinds of functions).
YUV?
You can think of an image as a superposition of several planes (or layers in a more natural language). YUV formats have three planes: Y
, U
, and V
.
Y
is the luma plane, and can be seen as the image as grayscale. U
and V
are referred to as the chroma planes, which are basically the colours. All the YUV formats have these three planes, and differ by the different orderings of them.
Sometimes the term YCrCb
is used. It’s essentially the same thing: Y
, Cr
, Cb
respectively refer to Y
, U
, V
although Cr
and Cb
are sometimes used when speaking of the components of U
and V
(that is U
= Cr1Cr2…Crn and V
= Cb1Cb2…Cbn).
420, 422, (…), And Subsampling
The chroma planes (U
and V
) are subsampled. This means there is less information in the chroma planes than in the luma plane. This is because the human eye is less sensitive to colours than luminance; so we can just gain space by keeping less information about colours (chroma planes) than luminance (luma plane, Y
).
The subsampling is expressed as a three part ratio: J:a:b
(e.g. 4:2:0
). This ratio makes possible to get the size of the planes relative to each others. Refer to the Wikipedia article “Chroma subsampling” for more information.
Basically the chroma planes are often shorter than the luma plane. Most commonly, the ratio is length(Y) / n = length(U) = length(V)
where n
is 1, 2, 4, …
Packed, Planar, and Semi-planar
YUV formats are either:
- Packed (or interleaved)
- Planar (the names of those formats often end with “p”)
- Semi-planar (the names of those formats often end with “sp”)
Those terms define how the planes are ordered in the format. In the memory:
- Packed means the components of
Y
,U
, andV
are interleaved. For instance: Y1U1Y2V1Y3U2Y4V2…Yn-1Un/2YnVn/2. - Planar means the components of
Y
,U
, andV
are respectively grouped together. For instance: Y1Y2…YnU1U2…Un/2V1V2…Vn/2. - Semi-planar means the components of
Y
are grouped together, and the components ofU
andV
are interleaved. For instance: Y1Y2…YnU1V1U2V2…Un/2Vn/2
Semi-planar formats are sometimes put in the Planar family.
Some YUV Formats
The following formats are described in the picture:
- YV12
- I420 = IYUV = YUV420p (sometimes YUV420p can refer to YV12)
- NV21
- NV12 = YUV420sp (sometimes YUV420sp can refer to NV21)
NV12
FFMPEG with NVDEC
Code for extracting raw YUV data from AVFrame
:
I420
FFMPEG with CPU decoder
- When using default CPU decoder, the decoded
AVFrame
stores data in I420 format
frame->data[0]
stores Y-planeframe->data[1]
stores U-planeframe->data[2]
stores V-planeframe->linesize[0] == width
,frame->linesize[1] == frame->linesize[2] == width / 2
Code for extracting raw YUV data from AVFrame
:
RGB
Convert YUV to RGB
CUDA implementation to convert buffer from YUV format to RGB format: