Changes between Initial Version and Version 1 of colorspace


Ignore:
Timestamp:
Apr 26, 2016, 2:41:33 AM (4 years ago)
Author:
rbultje
Comment:

--

Legend:

Unmodified
Added
Removed
Modified
  • colorspace

    v1 v1  
     1= Colorspace support in FFmpeg =
     2
     3== What is a colorspace and why should you care ==
     4A colorspace describes how you display an array of pixel values on screen. For example, a colorspace tells us what type of color components exist within the array of pixels (e.g. RGB or YUV), and how to translate the pixel values in each color component to photons on the screen.
     5
     6The difference between YUV and RGB should be relatively obvious. RGB distinguishes color pixel values into red, green and blue color component pixel values. YUV is an orthogonal representation that allows representing color pixel values in luminance (brightness) and chroma (color differences) components.
     7
     8The conversion between YUV pixel buffer representation and visual representation depends on the type of YUV represented in the pixel buffers, which are essentially device-dependent. Examples are [https://en.wikipedia.org/wiki/Rec._601 Bt-601] (“Standard-definition” or SD), [https://en.wikipedia.org/wiki/Rec._709 Bt-709] (“High-definition” or HD) or [https://en.wikipedia.org/wiki/Rec._2020 Bt-2020] (“Ultra-high-definition” or UHD). These standards describe not just things like how to convert the YUV signals to RGB, but also how the RGB signal should be represented in terms of photon emission in a device-independent way.
     9
     10
     11== How does FFmpeg identify colorspaces ==
     12
     13In practical terms, the things you care about are 1) whether the pixel buffer contains RGB, YUV or some other type of signal, the bit depth of this signal; 2) whether the signal (assuming YUV) is full-range or restricted range; 3) the transformation matrix between YUV and RGB; 4) the linearization function from RGB to a linear RGB signal; and 5) the conversion matrix between linearized RGB and the device-independent XYZ colorspace.
     14
     15FFmpeg stores all these properties in the [https://ffmpeg.org/doxygen/trunk/structAVFrame.html AVFrame] struct. The RGB/YUV format and bitdepth are stored in AVFrame->[https://ffmpeg.org/doxygen/trunk/structAVFrame.html#aed14fa772ce46881020fd1545c86432c format]; the signal range is stored in AVFrame->[https://ffmpeg.org/doxygen/trunk/structAVFrame.html#a853afbad220bbc58549b4860732a3aa5 color_range]; the YUV/RGB transformation matrix is stored in AVFrame->[https://ffmpeg.org/doxygen/trunk/structAVFrame.html#a9262c231f1f64869439b4fe587fe1710 colorspace]; the linearization function is called a transformation characteristics and is stored in AVFrame->[https://ffmpeg.org/doxygen/trunk/structAVFrame.html#ab09abb126e3922bc1d010cf044087939 color_trc]; lastly, the RGB/XYZ matrix is stored in AVFrame->[https://ffmpeg.org/doxygen/trunk/structAVFrame.html#a59a3f830494f2ed1133103a1bc9481e7 color_primaries].
     16
     17
     18== How do you convert between colorspaces in FFmpeg ==
     19
     20Conversion between RGB/YUV is typically done using swscale. Conversion between color types (matrix, primaries, transfer characteristics) can be done using the [https://ffmpeg.org/ffmpeg-filters.html#colorspace colorspace] or [https://ffmpeg.org/ffmpeg-filters.html#colormatrix colormatrix] video filters. There’s also a filter using the external library [https://ffmpeg.org/ffmpeg-filters.html#zscale zscale]. colorspace/matrix have the following relationship:
     21
     22- they both do only YUV-to-YUV colorspace conversion; YUV-to-RGB or scaling requires swscale.
     23- colormatrix supports only 8bit pixel formats; colorspace supports 10/12bit content also.
     24- colormatrix does not do gamma/primary correction, whereas colorspace does (it has an option to disable this if you want a faster conversion).
     25- colormatrix is C only, whereas colorspace has x86 SIMD (i.e. it’s faster).
     26
     27Read the filters’ respective documentation to read up exactly on how to use them. The easiest way to use these filters is to ensure that the input AVFrame has all relevant struct members set to the correct value. Then, set the target colorspace property on the video filter, and it will output converted frames.
     28
     29To convert RGB/YUV or scale using swscale, use swscale and set the correct color type using [https://ffmpeg.org/doxygen/trunk/group__lsws.html#ga541bdffa8149f5f9203664f955faa040 sws_setColorspaceDetails]().