Skip to content

Instantly share code, notes, and snippets.

@Jim-Bar
Last active October 30, 2024 13:14
Show Gist options
  • Save Jim-Bar/3cbba684a71d1a9d468a6711a6eddbeb to your computer and use it in GitHub Desktop.
Save Jim-Bar/3cbba684a71d1a9d468a6711a6eddbeb to your computer and use it in GitHub Desktop.
About YUV formats

About YUV formats

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 reffered 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:

  1. Packed (or interleaved)
  2. Planar (the names of those formats often end with "p")
  3. 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:

  1. Packed means the components of Y, U, and V are interleaved. For instance: Y1U1Y2V1Y3U2Y4V2…Yn-1Un/2YnVn/2.
  2. Planar means the components of Y, U, and V are respectively grouped together. For instance: Y1Y2…YnU1U2…Un/2V1V2…Vn/2.
  3. Semi-planar means the components of Y are grouped together, and the components of U and V are interleaved. For instance: Y1Y2…YnU1V1U2V2…Un/2Vn/2

Semi-planar formats are sometimes put in the Planar familly.

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)
<mxfile userAgent="Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:55.0) Gecko/20100101 Firefox/55.0" version="7.3.5" editor="www.draw.io" type="device"><diagram id="33e20851-287e-c678-fda7-a99a3a0c866b" name="Page-1">7Z1Pc9pIEMU/DcetQn8QcIzZJbuH5JKCrT0SUICKQC4sr5399CtiSQb1y8ZbxbyZrmpfgiVAsX56o+mn7p5BMjs8vz+t7ncfyk1eDOLh5nmQ/DqI4ygbx/U/5y3fXrYkyfBlw/a03zRvet3waf9P3mxs3/a43+QPL9uaTVVZFtX+/uHq0+vyeMzX1dW21elUPl2/7UtZXB/1frXNxYZP61Uht/6531S7Zms0HL7u+D3fb3fNoSejZsfn1frr9lQ+HpvjDeLky/efl92HVftdzfsfdqtN+XSxKfltkMxOZVm9vDo8z/LifG7b0/byufkP9nb/71N+rN7ygYbT36visfnT/2q+46H61p6N739Pfv7EcJDcPe32Vf7pfrU+732q+dfbdtWhqH+L6pfdX3R+77ZYPTw0r9flYb9uXherz3lx152rWVmUp3rXsTzm56+oTuXXvN1Yn8Lh959uT4vkfLgv+6K4eOd0OpvN5+ft5bGarw774nwFzsrH0z4/1X/Vx/yp2dlcdFHW/I4Otyr222O9bV2fzfrjyV1ztvJTlT//8IxHHcdaH3l5yKvTt/otzQfi9hpqpDFpfn16vc7SZtPu4gprBbRqruxt98WvdOsXDWAMO5GwY4PtFHbqDXYqYY8NNlPZUcSjPZK0J0abKW0m7YmknWimPZpNRnEWNO2Jt4F8KmGnBtsl7CT2BrsdRC5pT402U9rMgTyKJO728Mabo24qbxCCjTTjntc/02nQuDN/gzmIwTKj7ZJ26s9diUAMFqk208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ckb9ExT3UJLAe++vpkdVzLQM011Cy0FvPv6pvIGPdP0xN+6lg3yMX6315Kt8uarhpeLG3RIU912PmDcPobrsbTPbJk3sp6pvMGyAnrsM5ULA/mMrsdgUQE97plK3P0aXi5v5J6pts/CB+4zuh4D98wWeiMLnAoc2Ge20BvXP+uef1KAozUHVN/Cw3/g2c9Po/JGaw6ojrgVCpzZdqU9ti305s8h5wIHlpot9ObWU6MO4RPkqan2yBUqmgocrdWpOupWoGjqkA1stFh1mB0w4B9Mwqm8kY2mOksl/KxDn0H2BNloqrNUwufdt9G4wNHKncpttNCBew2ygYtmC72RBU4FLm00a8PC1TezaqA9lnVh8RdxU3lLD826sJBNcmaV51RaataFhaxvKm/QRc30zdU3dTwHXdT0TM8B7/Adc6+3b9BETU/0DXEHm7XkZfgGTdNMzlQ5U3GDnmkmZ7dypg7XoEeanhQ0yFedeUblDXqkmXlGNc+Iw3fXrstarPiTN5W3NM/U0g591Ym39FZxtexEPJSumVjp7a9lFNtKb//7WrnBFC4BD7xdLUsQD6WhZpp3NDfnatxW7QypktcdZ7Rqp3H2VvDlDrQtyum1LMAdWFuVM6RkUnecpVuW2mJvvhZ7486ypXF2b8jJq705JF7/eirL6mLf+/rc7j6Um/z8jn8B</diagram></mxfile>
@duclahoang
Copy link

Thank you so much !

@pengguoqing
Copy link

Thank you !

@46cv8
Copy link

46cv8 commented Jan 18, 2023

Awesome explanation! Thanks!

@nniranjhana
Copy link

nniranjhana commented Sep 13, 2023

Thank you, this helped. Can you also explain how single-planar and multi-planar relate to this?

Answering my own question:
Packed is single-planar as it uses 1 plane.
Semi-planar and planar use 2 and 3 planes, hence multi-planar.

@mnyoshie
Copy link

mnyoshie commented Jul 7, 2024

Amazing! Thanks!

@lix19937
Copy link

lix19937 commented Sep 1, 2024

I think can be desc follow:

NV12(NV12: YYYYYYYYUVUV =>YUV420SP)     
NV21(NV21: YYYYYYYYVUVU =>YUV420SP)   

@zelalem61
Copy link

amazing

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment