Colorimetry

From Avisynth wiki
(Difference between revisions)
Jump to: navigation, search
m (1 revision)
m (Colorimetry.png smaller (720->540); add color bars note)
 
(2 intermediate revisions by one user not shown)
Line 1: Line 1:
When playing back video content, several issues might go wrong. The levels could be wrong, resulting in washed out colors (black is displayed as dark gray and white is displayed as light gray). This is described in more detail [[Luminance_levels|here]]. The other issue is a slight distortion in color (which often looks like a small change in brightness) and this will be described here.
+
<div style="max-width:62em" >
 +
When playing back video content, several issues might arise. The luminance levels could be wrong, resulting in washed out colors (black displayed as dark gray, and white displayed as light gray). This is described in more detail on the [[Luminance_levels|''luminance levels'']] page. Another issue is a slight distortion in color (which often looks like a small change in brightness) and this will be described here.
  
The problem could arise if your content is created in a different colorspace as it is stored later on. Usually it is created in RGB, but stored as YCbCr. Upon display it will be converted back again to RGB. This means that two color conversions will take place. The problem is that there are different ways of doing this conversion. If the two color conversions are not the same, the displayed content won't be the same as the original content. There are many other colorspaces, but they won't be discussed here. See for example [http://www.poynton.com/notes/colour_and_gamma/ColorFAQ.html here].
+
This distortion arise because of an improper [[Color_spaces|color space]] conversion at some point. Usually content is ''created'' (whether by a camera or a computer) in [[wikipedia:RGB|RGB]], but for efficiency, ''stored'' as [[wikipedia:YCbCr|YCbCr]] (<span style="font-size:90%">typically the terms '''YCbCr''' and [[YUV|'''YUV''']] are used interchangeably, leading to some confusion; when referring to signals in video or digital form, the term "YUV" mostly means "Y′CbCr"</span><sup>[http://en.wikipedia.org/wiki/YCbCr (1)]</sup>). Besides RGB and YCbCr, there are many other colorspaces, which we won't discuss here. For an in-depth discussion, see Charles Poynton's [http://www.poynton.com/notes/colour_and_gamma/ColorFAQ.html ''Color FAQ''].
  
So when converting YCbCr to RGB upon display, the correct color conversion standard (Rec.601 or Rec.709) must be used, namely the one which was used when storing the content. (Rec.709 and Rec.601 are short for ITU-R BT.709 and ITU-R BT.601.)
+
The content must eventually be converted back to RGB for display. This means that two color conversions take place: RGB&rarr;YCbCr and YCbCr&rarr;RGB. The problem is that there are several ways of doing this conversion, involving different mathematical constants. If the two color conversions are not the same, the displayed colors won't be the quite the same as the original. So when converting RGB&rarr;YCbCr or YCbCr&rarr;RGB, the correct color conversion standard must be used, namely the one with which the content was encoded &ndash; [[wikipedia:Rec._601|'''Rec.601''']] or [[wikipedia:Rec._709|'''Rec.709''']] being the most common.  
 +
</div>
  
== What are color conversion standards? ==
+
__TOC__
  
There are multiple ways of doing the YCbCr <-> RGB conversion. The general conversion is given by
 
  
  Y = Kr*R + Kg*G + Kb*B
+
== What are color conversion standards? ==
 +
<div style="max-width:62em" >
 +
There are multiple ways of doing the YCbCr&harr;RGB [[Color_conversions|conversion]]. The general conversion is given by
 +
<div {{BoxWidthIndent|36|1}} >
 +
  Y = Kr*R + Kg*G + Kb*B
 
  Cb = (B-Y)/(1-Kb)
 
  Cb = (B-Y)/(1-Kb)
 
  Cr = (R-Y)/(1-Kr) = R - G * Kg/(1-Kr) - B * Kb/(1-Kr)
 
  Cr = (R-Y)/(1-Kr) = R - G * Kg/(1-Kr) - B * Kb/(1-Kr)
 
+
</div>
 
and the other way around:
 
and the other way around:
 
+
<div {{BoxWidthIndent|36|1}} >
 
  R = Y + Cr*(1-Kr)
 
  R = Y + Cr*(1-Kr)
 
  G = Y - Cb*(1-Kb)*Kb/Kg - Cr*(1-Kr)*Kr/Kg
 
  G = Y - Cb*(1-Kb)*Kb/Kg - Cr*(1-Kr)*Kr/Kg
 
  B = Y + Cb*(1-Kb)
 
  B = Y + Cb*(1-Kb)
 +
</div>
 +
where
 +
<div {{BoxWidthIndent|36|1}} >
 +
( 0.0 <= [Y,R,G,B] <= 1.0),
 +
(-1.0 <= [Cb,Cr]  <= 1.0) and
 +
Kr + Kg + Kb = 1
 +
</div>
  
with (0.0 <= [Y,R,G,B] <= 1.0) ; (-1.0 <= [Cb,Cr] <= 1.0) and Kr + Kg + Kb = 1.  
+
The red, green and blue coefficients (<tt>Kr</tt>,<tt>Kg</tt>,<tt>Kb</tt>) are standardized in specifications.
 +
The most common conversion coefficients are given below:
 +
<div {{BoxWidthIndent|36|1}} >
 +
{| class="wikitable" style="width:100%"
 +
!style="width:20em"| Standard
 +
! Kr
 +
! Kg
 +
! Kb
 +
|-
 +
| Rec.601/BT.601<sup>[http://avisynth.nl/index.php/Color_conversions (2)]</sup>, SMPTE 170M<sup>[http://avisynth.org.ru/docs/english/externalfilters/colormatrix.htm#colorimetry (3)]</sup>
 +
|style="text-align:center;letter-spacing:0.08em"| 0.299
 +
|style="text-align:center;letter-spacing:0.08em"| 0.587
 +
|style="text-align:center;letter-spacing:0.08em"| 0.114
 +
|-
 +
| FCC<sup>[http://avisynth.nl/index.php/Color_conversions (2)]</sup>
 +
|style="text-align:center;letter-spacing:0.08em"| 0.300
 +
|style="text-align:center;letter-spacing:0.08em"| 0.590
 +
|style="text-align:center;letter-spacing:0.08em"| 0.110
 +
|-
 +
| Rec.709/BT.709<sup>[http://avisynth.nl/index.php/Color_conversions (2)]</sup>
 +
|style="text-align:center"| 0.2126
 +
|style="text-align:center"| 0.7152
 +
|style="text-align:center"| 0.0722
 +
|-
 +
| SMPTE 240M<sup>[http://en.wikipedia.org/wiki/Luma_%28video%29 (4)]</sup>
 +
|style="text-align:center;letter-spacing:0.08em"| 0.212
 +
|style="text-align:center;letter-spacing:0.08em"| 0.701
 +
|style="text-align:center;letter-spacing:0.08em"| 0.087
 +
|}</div>
 +
</div>
  
The red, green and blue coefficients (Kr,Kg,Kb) are standardized in specifications and there are several of them. Have a look at [[color conversions]] for examples.
 
  
 
== How can I see if the correct standard is used upon playback? ==
 
== How can I see if the correct standard is used upon playback? ==
 
+
<div style="max-width:62em" >
 
That's a tough question, because it's hardly possible to notice if the wrong standard is used upon playback. In fact, it's hard to see any difference at all between them. Take a look at the following screenshot:
 
That's a tough question, because it's hardly possible to notice if the wrong standard is used upon playback. In fact, it's hard to see any difference at all between them. Take a look at the following screenshot:
  
[[Image:Colorimetry.png]]
+
[[Image:Colorimetry.png|540px]]
  
The upper picture is shown correctly (using Rec.601), and for the lower one the incorrect standard (Rec.709) is used upon playback. What you notice is that the lower picture is more saturated (i.e., more colorful). But also that the red is shifted a bit to yellow. [The other way around results in a less saturated clip, red shifting towards magenta and blue shifting to cyan.]
+
The upper picture is shown correctly (using Rec.601), and the lower one is decoded with the incorrect standard (Rec.709). What you notice is that the lower picture is more saturated (i.e., more colorful), but also that the red is shifted a bit to yellow. The other way around results in a less saturated clip, red shifting towards magenta and blue shifting to cyan.
  
 
So, how do you know then, which standard to choose upon playback? Well that's not always possible, but you have to know the origin of your file. Also, in some cases, that information can be stored in the header of the file. You should stick to the following guideline:
 
So, how do you know then, which standard to choose upon playback? Well that's not always possible, but you have to know the origin of your file. Also, in some cases, that information can be stored in the header of the file. You should stick to the following guideline:
  
'''If your content is Standard Definition (SD) content use Rec.601 and if your content is High Definition (HD) content use Rec.709, unless the header of the content specifies otherwise.''' I will elaborate on this in the following sections.
+
'''If your content is Standard Definition (SD) content use Rec.601; if your content is High Definition (HD) content use Rec.709 &ndash; unless the header of the content specifies otherwise.''' We will elaborate on this in the following sections.
 +
</div>
  
== Should I correct anything when processing my content? ==
 
  
Yes, you should correct for it in some cases. It depends on your end format and how it is played back. In general, it is save to assume that Rec.601 is used for Standard Definition content and Rec.709 for High Definition content upon playback. That means that often any specific header information regarding colorimetry will be ignored. It will be assumed here that you are processing with AviSynth.
+
== Using color bars ==
 +
<div style="max-width:62em" >
 +
As noted in the previous section, it's hard to see the effects of improper decoding or conversion. If the source includes [[ColorBars|color bars]], your task is much easier:
 +
{|border="0"
 +
|rowspan="3"|[[File:ColorBars-compare3B.png]]
 +
|&nbsp;
 +
|When decoding Rec709 color bars as Rec601, the Green bar is bright and over-saturated and the Red bar is dim.
 +
|-
 +
|&nbsp;
 +
|&nbsp;
 +
Properly decoded color bars.<br>&nbsp;
 +
|-
 +
|&nbsp;
 +
|When decoding Rec601 color bars as Rec709, the Red bar is bright and over-saturated and the Green bar is dim.
 +
|}
 +
</div>
 +
Once you have trained your eyes to see this shift, you can usually spot a problem on any video frame with saturated reds and greens.
  
Suppose you want to encode your content as Standard Definition content (that is, height of the encoded video < 720p (720 vertical lines)). If you source is MPEG-2 (then the colorimetry information can be stored in its header), get the ColorMatrix plugin, and create the following script:
 
  
# ColorMatrix does a Rec.709 -> Rec.601 conversion, if your source is Rec.709:
+
== Should I correct anything when processing my content? ==
DGDecode_mpeg2source("D:\source.d2v", info=3)
+
<div style="max-width:62em" >
ColorMatrix(hints=true, dest=2, interlace=false) # use interlace=true for interlaced content
+
Yes, you should correct for it in some cases. It depends on your end format and how it is played back. In general, it is safe to assume that Rec.601 is used for Standard Definition content and Rec.709 for High Definition content upon playback. That means that often, media players will ignore any specific header information regarding colorimetry. It will be assumed here that you are processing with AviSynth.
  
 +
Suppose you want to encode your content as Standard Definition content (that is, height of the encoded video < 720p (720 vertical lines)). If your source is MPEG-2 (therefore the colorimetry information can be stored in its header), get the [[ColorMatrix]] plugin, and create the following script:
 +
<div {{BoxWidthIndent|60|1}} >
 +
# ColorMatrix does a Rec.709 -> Rec.601 conversion, if your source is Rec.709:
 +
[[DGDecode]]_mpeg2source("D:\source.d2v", info=3)
 +
[[ColorMatrix]](hints=true, dest=2, interlace=false) # use interlace=true for interlaced content
 +
</div>
 
Suppose you want to encode your content as High Definition content (that is, height of the encoded video >= 720p). Create the following script:
 
Suppose you want to encode your content as High Definition content (that is, height of the encoded video >= 720p). Create the following script:
 
+
<div {{BoxWidthIndent|60|1}} >
 
  # ColorMatrix does a Rec.601 -> Rec.709 conversion, if your source is Rec.601:
 
  # ColorMatrix does a Rec.601 -> Rec.709 conversion, if your source is Rec.601:
  DGDecode_mpeg2source("D:\source.d2v", info=3)
+
  [[DGDecode]]_mpeg2source("D:\source.d2v", info=3)
  ColorMatrix(hints=true, dest=0, interlace=false) # use interlace=true for interlaced content
+
  [[ColorMatrix]](hints=true, dest=0, interlace=false) # use interlace=true for interlaced content
 +
</div>
 +
If the colorimetry information is ''not'' stored in the header (as with DivX/XviD for example), it is not really possible to tell which colorimetry was used to create it. All that you can do is assume Rec.601 for SD content and Rec.709 for HD content, and hope that the assumption is correct. In this case, you can't use the ''hints'' parameter of ColorMatrix, but you need to give the conversion explicitly using the ''mode'' parameter.
 +
 
 +
 
 +
To correct a source that was badly processed at some point, use the following:
 +
<div {{BoxWidthIndent|44|1}} >
 +
# overbright greens, faded reds: perform a 709->601 conversion
 +
[[ColorMatrix]](source=0, dest=2)
 +
 +
# for RGB sources:
 +
[[ConvertToYV24]](matrix="Rec601").[[ConvertToRGB32]](matrix="Rec709")
 +
</div>
 +
 
 +
<div {{BoxWidthIndent|44|1}} >
 +
# overbright reds, faded greens: perform a 601->709 conversion 
 +
[[ColorMatrix]](source=2, dest=0)
 +
 +
# for RGB sources:
 +
[[ConvertToYV24]](matrix="Rec709").[[ConvertToRGB32]](matrix="Rec601")
 +
</div>
 +
</div>
  
If the colorimetry information is not stored in the header (as with DivX/XviD for example), it is not really possible to tell which colorimetry is used to create it. All that you can do is assume Rec.601 is used for SD content and Rec.709 is used for HD content, and hope that the assumption is correct. In this case, you can't use the ''hints'' parameter of ColorMatrix, but you need to give the conversion explicitly using the ''mode'' parameter.
 
  
 
== How can I use the correct standard upon playback? ==
 
== How can I use the correct standard upon playback? ==
 
+
<div style="max-width:62em" >
Whether this is possible (provided that the wrong standard is used upon play back) depends on how your content is being played back. If you use a software player you need to check whether it has an option to correct it. If you use a directshow based player (such as WMP or MPC) there are different possibilities which are explained below. If the renderer does the YCbCr->RGB conversion, you should keep in mind that (as found are out in this [http://forum.doom9.org/showthread.php?t=82217&page=24 thread]):
+
Whether this is possible (provided that the wrong standard is used upon play back) depends on how your content is being played back. If you use a software player you need to check whether it has an option to correct it. If you use a [[wikipedia:DirectShow|DirectShow]]-based player (such as [[wikipedia:Windows_Media_Player|WMP]] or [[wikipedia:Media_Player_Classic|MPC]]) there are different possibilities which are explained below. If the [[wikipedia:Video_renderer|renderer]] does the YCbCr&rarr;RGB conversion, you should keep in mind that (as found are out in a doom9 thread<sup>[http://forum.doom9.org/showthread.php?t=82217&page=24 (5)]</sup>):
 
* Windowed/renderless VMR7 and VMR9 use BT.601 for video < 720p (720 vertical lines)
 
* Windowed/renderless VMR7 and VMR9 use BT.601 for video < 720p (720 vertical lines)
 
* Windowed/renderless VMR7 and VMR9 use BT.709 for video >= 720p (720 vertical lines)
 
* Windowed/renderless VMR7 and VMR9 use BT.709 for video >= 720p (720 vertical lines)
  
If you are using Haali's Video Renderer (get [http://haali.cs.msu.ru/mkv/ Haali Media Splitter] in order to install it) you can choose the color standard.
+
If you are using [https://haali.su/mkv/ Haali's Video Splitter] and its Renderer, you can choose the color standard.
 
+
Have a look [http://forum.doom9.org/showthread.php?t=134735 here] for information about the different renderers.
+
 
+
=== Method #1: Adjusting graphics driver settings ===
+
  
I'm not sure whether this is possible. I need to ask someone ...
+
Have a look at this doom9 thread<sup>[http://forum.doom9.org/showthread.php?t=134735 (6)]</sup> for information about the different renderers.
  
=== Method #2: Convert to RGB32 with ffdshow ===
+
===== ''Method 1: Adjusting graphics driver settings'' =====
 +
[[TODO]]
  
Forcing ffdshow to output RGB32 can help prevent colorimetry issues. Downside of this method is that doing this conversion in software increases CPU usage.
+
===== ''Method 2: Convert to RGB32 with ffdshow'' =====
 +
Forcing [[wikipedia:Ffdshow|ffdshow]] to output RGB32 can help prevent colorimetry issues. Downside of this method is that doing this conversion in software increases CPU usage.
  
 
To force RGB32 output in ffdshow, you should uncheck all colorspaces except RGB32 on the Output page in ffdshow configuration. It is also recommended to enable "High quality YV12 to RGB conversion". On the RGB conversion page, you can choose which standard should be assumed, BT.601 or BT709. Choose the first for SD material, and the latter for HD video.
 
To force RGB32 output in ffdshow, you should uncheck all colorspaces except RGB32 on the Output page in ffdshow configuration. It is also recommended to enable "High quality YV12 to RGB conversion". On the RGB conversion page, you can choose which standard should be assumed, BT.601 or BT709. Choose the first for SD material, and the latter for HD video.
  
=== Method #3: Pixelshader in Media Player Classic ===
+
===== ''Method 3: Pixel shader in Media Player Classic'' =====
 
+
A pixel shader is a small program that runs on your graphics card and processes some graphic data. In this case each frame of your video.
A pixelshader is a small program that runs on your graphics card and processes some graphic data. In this case each frame of your video.
+
  
 
Media Player Classic has a shader called "BT.601 -> BT.709". A shader which does the conversion the other way around is still not available. Use this when needed.
 
Media Player Classic has a shader called "BT.601 -> BT.709". A shader which does the conversion the other way around is still not available. Use this when needed.
  
Some requirements for the pixelshaders in MPC:
+
Some requirements for the pixel shaders in MPC:
 
+
 
* You need to use a compatible video renderer: VMR-7 (renderless), VMR-9 (renderless), or EVR Custom Presenter.
 
* You need to use a compatible video renderer: VMR-7 (renderless), VMR-9 (renderless), or EVR Custom Presenter.
 
* Surface setting must be set to "3D surfaces".
 
* Surface setting must be set to "3D surfaces".
 
* It requires some DirectX components that are not included with a default Windows installation. Run the DirectX Web Installer to get the required DirectX updates.
 
* It requires some DirectX components that are not included with a default Windows installation. Run the DirectX Web Installer to get the required DirectX updates.
  
=== Method #4: Use AviSynth script in ffdshow video decoder ===
+
===== ''Method 4: Use AviSynth script in ffdshow video decoder'' =====
 
+
There are two ways to do this. The first one is to use the [[ColorMatrix]] plugin, as explained in one of the previous sections, and the other one is to convert to RGB using the appropriate matrix:
There are two ways to do this. The first one is to use the ColorMatrix plugin, as explained in one of the previous sections, and the other one is to convert to RGB using the appropriate matrix:
+
  
 
[[Image:Ffdshow_avisynth2.jpg]]
 
[[Image:Ffdshow_avisynth2.jpg]]
  
Here ffdshow will convert your content to RGB using Rec.601.
+
''With the above setting, ffdshow will use AviSynth to convert your content to RGB using Rec.601.''
 +
</div>
 +
 
  
 
== What do the specifications say about which colorimetry is supported in a particular format? ==
 
== What do the specifications say about which colorimetry is supported in a particular format? ==
 
+
<div style="max-width:62em" >
=== VCD/MPEG-1 ===
+
===== ''VCD/MPEG-1'' =====
 
+
 
Part 2 of the MPEG-1 standard covers video and is defined in ISO/IEC-11172-2. It is heavily based on H.261. The colorimetry information is not written into the header.
 
Part 2 of the MPEG-1 standard covers video and is defined in ISO/IEC-11172-2. It is heavily based on H.261. The colorimetry information is not written into the header.
  
=== DVD/MPEG-2 ===
+
===== ''DVD/MPEG-2'' =====
 
+
The DVD specs are not publicly available for free. The DVD specs ''should'' be a subset of the MPEG-2 specs (yes, that's a big assumption), and the latter ''is'' available for free; it says the following:
The DVD specs are not publicly available for free, and I don't people who have read those specs. The DVD specs should be a subset of the MPEG-2 specs (yes I know, that's a big assumption), and the latter is available for free. It says the following:
+
  
 
''The older 1995 MPEG-2 spec (ISO/IEC 13818-2: 1995 (E)) says''
 
''The older 1995 MPEG-2 spec (ISO/IEC 13818-2: 1995 (E)) says''
 +
<div {{BoxWidthIndent|52|1}} >
 +
{{BoldColor|blue|120|“}}In the case that sequence_display_extension() is not present in the bitstream or colour_description is zero the matrix coefficients are assumed to be those corresponding to matrix_coefficients having the value 1.{{BoldColor|blue|120|”}}
 +
</div>
  
"In the case that sequence_display_extension() is not present in the bitstream or colour_description is zero the matrix coefficients are assumed to be those corresponding to matrix_coefficients having the value 1." The value 1 stands for "1 Recommendation ITU-R BT.709". Note that in 1995 the first DVD titles came out, so when making those specs, the DVD didn't exist yet. In 2000, the MPEG-2 specs have changed with respect to this:
+
The value 1 stands for "1 Recommendation ITU-R BT.709". Note that in 1995 the first DVD titles came out, so when making those specs, the DVD didn't exist yet. In 2000, the MPEG-2 specs have changed with respect to this:
  
''The newer [http://www.itu.int/rec/T-REC-H.262/en 2000 MPEG-2 spec (ITU-T Rec.H262 (2000 E))] says''
+
''The newer 2000 MPEG-2 spec (ITU-T Rec.H262 (2000 E))<sup>[http://www.itu.int/rec/T-REC-H.262/en (7)]</sup> says''
 +
<div {{BoxWidthIndent|52|1}} >
 +
{{BoldColor|blue|120|“}}In the case that sequence_display_extension() is not present in the bitstream or colour_description is zero the matrix coefficients are assumed to be implicitly defined by the application.{{BoldColor|blue|120|”}}
 +
</div>
  
"In the case that sequence_display_extension() is not present in the bitstream or colour_description is zero the matrix coefficients are assumed to be implicitly defined by the application." The matrix coefficients are described in "Table 6-9 – Matrix Coefficients".
+
The matrix coefficients are described in "Table 6-9 – Matrix Coefficients". In other words if sequence_display_extension is not present, the colorimetry can be anything.  
  
In other words if sequence_display_extension is not present, the colorimetry can be anything.  
+
===== ''The ATSC standard'' =====
 +
As noted by hkazemi on doom9<sup>[http://forum.doom9.org/showthread.php?p=1089854#post1089854 (8)]</sup>, the ATSC a_81 standard on page 18<sup>[http://atsc.org/standard/a81-direct-to-home-satellite-broadcast-standard/ (9)]</sup> (section 7.3.4, 'Sequence Display Extension Constraints') talks about colorimetry and the assumptions to make when 'sequence_display_extension' does not tell you what to do:
 +
<div {{BoxWidthIndent|60|1}} >
 +
{{BoldColor|blue|120|“}}The values for ''color_primaries'', ''transfer_characteristics'', and ''matrix_coefficients'' shall be explicitly indicated in the ''sequence_display_extension''. While all values for ''color_primaries'', ''transfer_characteristics'', and ''matrix_coefficients'' defined in Tables 6-7, 6-8, and 6-9 of ISO/IEC 13818-2 [16, 18] are allowed in the transmitted bit stream, it is noted that ITU-R BT.709 [27] and SMPTE 170M are the most likely to be in common use.
  
=== The ATSC standard ===
+
Note: Some previously-encoded legacy material may not have the colorimetry (i.e., ''color_primaries'', ''transfer_characteristics'', and ''matrix_coefficients'') explicitly indicated in the ''sequence_display_extension'', in which case the colorimetry is most likely ITU-R BT.709 for all formats except those formats with ''vertical_size_value''=480, which are most likely to have colorimetry according to SMPTE 170M.{{BoldColor|blue|120|”}}
 
+
</div>
''Just for kicks, ATSC standard on page 18 (http://www.atsc.org/standards/a_81.pdf ) talks about colorimetry and the assumptions to make when 'sequence_display_extension' does not tell you what to do: <br>
+
"The values for color_primaries, transfer_characteristics, and matrix_coefficients shall be explicitly indicated in the sequence_display_extension. While all values for color_primaries, transfer_characteristics, and matrix_coefficients defined in Tables 6-7, 6-8, and 6-9 of ISO/IEC 13818-2 [16, 18] are allowed in the transmitted bit stream, it is noted that ITU-R BT.709 [27] and SMPTE 170M are the most likely to be in common use.
+
 
+
Note: Some previously-encoded legacy material may not have the colorimetry (i.e., color_primaries, transfer_characteristics, and matrix_coefficients) explicitly indicated in the sequence_display_extension, in which case the colorimetry is most likely ITU-R BT.709 for all formats except those formats with vertical_size_value = 480, which are most likely to have colorimetry according to SMPTE 170M."'' As noted by [hkazemi http://forum.doom9.org/showthread.php?p=1089854#post1089854]
+
 
+
=== MPEG-4/ASP ===
+
  
 +
===== ''MPEG-4/ASP'' =====
 
The colorimetry information is not written into the header.
 
The colorimetry information is not written into the header.
  
=== MPEG-4/AVC ===
+
===== ''MPEG-4/AVC'' =====
 +
ITU-T Rec. H.264 (2005)/Amd.1 (06/2006) says
 +
<div {{BoxWidthIndent|52|1}} >
 +
{{BoldColor|blue|120|“}}When the matrix_coefficients syntax element is not present, the value of matrix_coefficients shall be inferred to be equal to 2. That is: Image characteristics are unknown or are determined by the application.{{BoldColor|blue|120|”}}
 +
</div>
 +
The matrix coefficients are described in "Table E-5 – Matrix coefficients".
  
''ITU-T Rec. H.264 (2005)/Amd.1 (06/2006) says''
+
===== ''Application defaults'' =====
 
+
In ''Video Demystified''<sup>[http://www.amazon.com/Video-Demystified-Keith-Jack/dp/187870723X (10)]</sup>, the author Keith Jack, gives the following table <br>
"When the matrix_coefficients syntax element is not present, the value of matrix_coefficients shall be inferred to be equal
+
(as noted by [http://forum.doom9.org/showthread.php?p=1075105#post1075105 '''ariga'''] on doom9.org; see: Table 13.16, ''MPEG-2 matrix_coefficients Codewords'')
to 2. That is: Image characteristics are unknown or are determined by the application." The matrix coefficients are described in "Table E-5 – Matrix coefficients".
+
<div {{BoxWidthIndent|60|1}} >
 
+
  '''Sequence Display Extension:'''
=== Application defaults ===
+
 
+
In [http://www.amazon.com/Video-Demystified-Keith-Jack/dp/187870723X Video Mystified] the author Keith Jack, gives the following table (as noted by [http://forum.doom9.org/showthread.php?p=1075105#post1075105 ariga]; see: Table 13.16. MPEG-2 matrix_coefficients Codewords.)
+
 
+
  Sequence Display Extension:
+
+
 
  Color_primaries
 
  Color_primaries
  This optional 8-bit codeword describes the chromaticity coordinates of the source primaries, as shown in table.
+
  This optional 8-bit codeword describes the chromaticity coordinates of the source primaries,  
If sequence_display_extension is not present, or color_description = 0, the indicated default value must be used.
+
as shown in table. If ''sequence_display_extension'' is not present, or ''color_description'' = 0,  
This information may be used to adjust the color processing after MPEG-2 decoding to compensate for the color
+
the indicated default value must be used. This information may be used to adjust the color  
primaries of the display.
+
processing after MPEG-2 decoding to compensate for the color primaries of the display.
   
+
  -------------------------------------------------------------------------------------
Code:
+
+
-----------------------------------------------------------------------------------
+
 
  |    Color Primaries    |    Code    |          Application Default              |
 
  |    Color Primaries    |    Code    |          Application Default              |
 
  |------------------------|-------------|--------------------------------------------|
 
  |------------------------|-------------|--------------------------------------------|
Line 159: Line 235:
 
  | :                      |      :      |                                            |
 
  | :                      |      :      |                                            |
 
  | reserved              |  1111 1111  |                                            |
 
  | reserved              |  1111 1111  |                                            |
  -----------------------------------------------------------------------------------
+
  -------------------------------------------------------------------------------------
 +
</div>
 +
'''dragongodz''' claims on doom9<sup>[http://forum.doom9.org/showthread.php?p=1089979#post1089979 (11)]</sup> that this table should be interpreted as follows:
 +
<div {{BoxWidthIndent|52|1}} >
 +
{{BoldColor|blue|120|“}}Having finally heard back from a person who is in the professional encoder industry (no I will not say who), I have more reliable information.: "The table is actually the limitation of what you can use. In other words if you set the sequence_display_extension to Rec.709 for dvd it is illegal. That doesn't mean it will not play etc it just means that it is not supported and any player that strictly follows the specs is more likely just to ignore it as if no value was set at all.{{BoldColor|blue|120|”}}
 +
</div>
 +
</div>
  
[http://forum.doom9.org/showthread.php?p=1089979#post1089979 dragongodz] claims this table should be interpreted as follows: <br>
 
''Having finally heard back from a person who is in the professional encoder industry (no I will not say who), I have more reliable information.: "The table is actually the limitation of what you can use. In other words if you set the sequence_display_extension to Rec.709 for dvd it is illegal. That doesn't mean it will not play etc it just means that it is not supported and any player that strictly follows the specs is more likely just to ignore it as if no value was set at all."''
 
  
 
== References ==
 
== References ==
 
+
<div style="max-width:62em" >
* http://forum.doom9.org/showthread.php?t=131169
+
# [http://en.wikipedia.org/wiki/YCbCr ''YCbCr''] (wikipedia.org)
* http://forum.doom9.org/showthread.php?t=133982
+
# [[Color_conversions|''Color conversions'']] (avisynth.nl)
* http://forum.doom9.org/showthread.php?p=1089979
+
# [http://avisynth.org.ru/docs/english/externalfilters/colormatrix.htm#colorimetry ''ColorMatrix documentation: Colorimetry''] (avisynth.org.ru)
* http://forum.doom9.org/showthread.php?t=133982#post1090068
+
# [http://en.wikipedia.org/wiki/Luma_%28video%29 ''Luma (video)''] (wikipedia.org)
 +
# [http://forum.doom9.org/showthread.php?t=82217&page=24 ''ColorMatrix v2.3, page 24''] (doom9.org)
 +
# [http://forum.doom9.org/showthread.php?t=134735 ''Windowless, Renderless, VMR, Overlay - what do they mean?''] (doom9.org)
 +
# [http://www.itu.int/rec/T-REC-H.262/en ''ITU-T Rec.H262 (2000 E)''] (itu.int)
 +
# [http://forum.doom9.org/showthread.php?p=1089854#post1089854 ''HCenc 22 and Colorimetry''] (doom9.org; post #18: hkazemi)
 +
# [http://atsc.org/standard/a81-direct-to-home-satellite-broadcast-standard/ ''A/81: Direct-To-Home Satellite Broadcast Standard''] (atsc.org)
 +
# [http://www.amazon.com/Video-Demystified-Keith-Jack/dp/187870723X ''Video Demystified''] by Keith Jack (amazon.com)
 +
# [http://forum.doom9.org/showthread.php?p=1089979 ''HCenc 22 and Colorimetry''] (doom9.org; post #19: dragongodz)
 +
# [http://www.poynton.com/notes/colour_and_gamma/ColorFAQ.html ''Color FAQ''] by Charles Poynton (poynton.com)
 +
</div>
  
 
[[Category:Advanced topics]]
 
[[Category:Advanced topics]]

Latest revision as of 14:36, 16 March 2016

When playing back video content, several issues might arise. The luminance levels could be wrong, resulting in washed out colors (black displayed as dark gray, and white displayed as light gray). This is described in more detail on the luminance levels page. Another issue is a slight distortion in color (which often looks like a small change in brightness) and this will be described here.

This distortion arise because of an improper color space conversion at some point. Usually content is created (whether by a camera or a computer) in RGB, but for efficiency, stored as YCbCr (typically the terms YCbCr and YUV are used interchangeably, leading to some confusion; when referring to signals in video or digital form, the term "YUV" mostly means "Y′CbCr"(1)). Besides RGB and YCbCr, there are many other colorspaces, which we won't discuss here. For an in-depth discussion, see Charles Poynton's Color FAQ.

The content must eventually be converted back to RGB for display. This means that two color conversions take place: RGB→YCbCr and YCbCr→RGB. The problem is that there are several ways of doing this conversion, involving different mathematical constants. If the two color conversions are not the same, the displayed colors won't be the quite the same as the original. So when converting RGB→YCbCr or YCbCr→RGB, the correct color conversion standard must be used, namely the one with which the content was encoded – Rec.601 or Rec.709 being the most common.

Contents


[edit] What are color conversion standards?

There are multiple ways of doing the YCbCr↔RGB conversion. The general conversion is given by

Y  = Kr*R + Kg*G + Kb*B
Cb = (B-Y)/(1-Kb)
Cr = (R-Y)/(1-Kr) = R - G * Kg/(1-Kr) - B * Kb/(1-Kr)

and the other way around:

R = Y + Cr*(1-Kr)
G = Y - Cb*(1-Kb)*Kb/Kg - Cr*(1-Kr)*Kr/Kg
B = Y + Cb*(1-Kb)

where

( 0.0 <= [Y,R,G,B] <= 1.0),
(-1.0 <= [Cb,Cr]   <= 1.0) and
Kr + Kg + Kb = 1

The red, green and blue coefficients (Kr,Kg,Kb) are standardized in specifications. The most common conversion coefficients are given below:

Standard Kr Kg Kb
Rec.601/BT.601(2), SMPTE 170M(3) 0.299 0.587 0.114
FCC(2) 0.300 0.590 0.110
Rec.709/BT.709(2) 0.2126 0.7152 0.0722
SMPTE 240M(4) 0.212 0.701 0.087


[edit] How can I see if the correct standard is used upon playback?

That's a tough question, because it's hardly possible to notice if the wrong standard is used upon playback. In fact, it's hard to see any difference at all between them. Take a look at the following screenshot:

Colorimetry.png

The upper picture is shown correctly (using Rec.601), and the lower one is decoded with the incorrect standard (Rec.709). What you notice is that the lower picture is more saturated (i.e., more colorful), but also that the red is shifted a bit to yellow. The other way around results in a less saturated clip, red shifting towards magenta and blue shifting to cyan.

So, how do you know then, which standard to choose upon playback? Well that's not always possible, but you have to know the origin of your file. Also, in some cases, that information can be stored in the header of the file. You should stick to the following guideline:

If your content is Standard Definition (SD) content use Rec.601; if your content is High Definition (HD) content use Rec.709 – unless the header of the content specifies otherwise. We will elaborate on this in the following sections.


[edit] Using color bars

As noted in the previous section, it's hard to see the effects of improper decoding or conversion. If the source includes color bars, your task is much easier:

ColorBars-compare3B.png   When decoding Rec709 color bars as Rec601, the Green bar is bright and over-saturated and the Red bar is dim.
   

Properly decoded color bars.
 

  When decoding Rec601 color bars as Rec709, the Red bar is bright and over-saturated and the Green bar is dim.

Once you have trained your eyes to see this shift, you can usually spot a problem on any video frame with saturated reds and greens.


[edit] Should I correct anything when processing my content?

Yes, you should correct for it in some cases. It depends on your end format and how it is played back. In general, it is safe to assume that Rec.601 is used for Standard Definition content and Rec.709 for High Definition content upon playback. That means that often, media players will ignore any specific header information regarding colorimetry. It will be assumed here that you are processing with AviSynth.

Suppose you want to encode your content as Standard Definition content (that is, height of the encoded video < 720p (720 vertical lines)). If your source is MPEG-2 (therefore the colorimetry information can be stored in its header), get the ColorMatrix plugin, and create the following script:

# ColorMatrix does a Rec.709 -> Rec.601 conversion, if your source is Rec.709:
DGDecode_mpeg2source("D:\source.d2v", info=3)
ColorMatrix(hints=true, dest=2, interlace=false) # use interlace=true for interlaced content

Suppose you want to encode your content as High Definition content (that is, height of the encoded video >= 720p). Create the following script:

# ColorMatrix does a Rec.601 -> Rec.709 conversion, if your source is Rec.601:
DGDecode_mpeg2source("D:\source.d2v", info=3)
ColorMatrix(hints=true, dest=0, interlace=false) # use interlace=true for interlaced content

If the colorimetry information is not stored in the header (as with DivX/XviD for example), it is not really possible to tell which colorimetry was used to create it. All that you can do is assume Rec.601 for SD content and Rec.709 for HD content, and hope that the assumption is correct. In this case, you can't use the hints parameter of ColorMatrix, but you need to give the conversion explicitly using the mode parameter.


To correct a source that was badly processed at some point, use the following:

# overbright greens, faded reds: perform a 709->601 conversion 
ColorMatrix(source=0, dest=2)

# for RGB sources:
ConvertToYV24(matrix="Rec601").ConvertToRGB32(matrix="Rec709") 
# overbright reds, faded greens: perform a 601->709 conversion  
ColorMatrix(source=2, dest=0)

# for RGB sources:
ConvertToYV24(matrix="Rec709").ConvertToRGB32(matrix="Rec601") 


[edit] How can I use the correct standard upon playback?

Whether this is possible (provided that the wrong standard is used upon play back) depends on how your content is being played back. If you use a software player you need to check whether it has an option to correct it. If you use a DirectShow-based player (such as WMP or MPC) there are different possibilities which are explained below. If the renderer does the YCbCr→RGB conversion, you should keep in mind that (as found are out in a doom9 thread(5)):

  • Windowed/renderless VMR7 and VMR9 use BT.601 for video < 720p (720 vertical lines)
  • Windowed/renderless VMR7 and VMR9 use BT.709 for video >= 720p (720 vertical lines)

If you are using Haali's Video Splitter and its Renderer, you can choose the color standard.

Have a look at this doom9 thread(6) for information about the different renderers.

[edit] Method 1: Adjusting graphics driver settings

TODO

[edit] Method 2: Convert to RGB32 with ffdshow

Forcing ffdshow to output RGB32 can help prevent colorimetry issues. Downside of this method is that doing this conversion in software increases CPU usage.

To force RGB32 output in ffdshow, you should uncheck all colorspaces except RGB32 on the Output page in ffdshow configuration. It is also recommended to enable "High quality YV12 to RGB conversion". On the RGB conversion page, you can choose which standard should be assumed, BT.601 or BT709. Choose the first for SD material, and the latter for HD video.

[edit] Method 3: Pixel shader in Media Player Classic

A pixel shader is a small program that runs on your graphics card and processes some graphic data. In this case each frame of your video.

Media Player Classic has a shader called "BT.601 -> BT.709". A shader which does the conversion the other way around is still not available. Use this when needed.

Some requirements for the pixel shaders in MPC:

  • You need to use a compatible video renderer: VMR-7 (renderless), VMR-9 (renderless), or EVR Custom Presenter.
  • Surface setting must be set to "3D surfaces".
  • It requires some DirectX components that are not included with a default Windows installation. Run the DirectX Web Installer to get the required DirectX updates.
[edit] Method 4: Use AviSynth script in ffdshow video decoder

There are two ways to do this. The first one is to use the ColorMatrix plugin, as explained in one of the previous sections, and the other one is to convert to RGB using the appropriate matrix:

Ffdshow avisynth2.jpg

With the above setting, ffdshow will use AviSynth to convert your content to RGB using Rec.601.


[edit] What do the specifications say about which colorimetry is supported in a particular format?

[edit] VCD/MPEG-1

Part 2 of the MPEG-1 standard covers video and is defined in ISO/IEC-11172-2. It is heavily based on H.261. The colorimetry information is not written into the header.

[edit] DVD/MPEG-2

The DVD specs are not publicly available for free. The DVD specs should be a subset of the MPEG-2 specs (yes, that's a big assumption), and the latter is available for free; it says the following:

The older 1995 MPEG-2 spec (ISO/IEC 13818-2: 1995 (E)) says

In the case that sequence_display_extension() is not present in the bitstream or colour_description is zero the matrix coefficients are assumed to be those corresponding to matrix_coefficients having the value 1.

The value 1 stands for "1 Recommendation ITU-R BT.709". Note that in 1995 the first DVD titles came out, so when making those specs, the DVD didn't exist yet. In 2000, the MPEG-2 specs have changed with respect to this:

The newer 2000 MPEG-2 spec (ITU-T Rec.H262 (2000 E))(7) says

In the case that sequence_display_extension() is not present in the bitstream or colour_description is zero the matrix coefficients are assumed to be implicitly defined by the application.

The matrix coefficients are described in "Table 6-9 – Matrix Coefficients". In other words if sequence_display_extension is not present, the colorimetry can be anything.

[edit] The ATSC standard

As noted by hkazemi on doom9(8), the ATSC a_81 standard on page 18(9) (section 7.3.4, 'Sequence Display Extension Constraints') talks about colorimetry and the assumptions to make when 'sequence_display_extension' does not tell you what to do:

The values for color_primaries, transfer_characteristics, and matrix_coefficients shall be explicitly indicated in the sequence_display_extension. While all values for color_primaries, transfer_characteristics, and matrix_coefficients defined in Tables 6-7, 6-8, and 6-9 of ISO/IEC 13818-2 [16, 18] are allowed in the transmitted bit stream, it is noted that ITU-R BT.709 [27] and SMPTE 170M are the most likely to be in common use.

Note: Some previously-encoded legacy material may not have the colorimetry (i.e., color_primaries, transfer_characteristics, and matrix_coefficients) explicitly indicated in the sequence_display_extension, in which case the colorimetry is most likely ITU-R BT.709 for all formats except those formats with vertical_size_value=480, which are most likely to have colorimetry according to SMPTE 170M.

[edit] MPEG-4/ASP

The colorimetry information is not written into the header.

[edit] MPEG-4/AVC

ITU-T Rec. H.264 (2005)/Amd.1 (06/2006) says

When the matrix_coefficients syntax element is not present, the value of matrix_coefficients shall be inferred to be equal to 2. That is: Image characteristics are unknown or are determined by the application.

The matrix coefficients are described in "Table E-5 – Matrix coefficients".

[edit] Application defaults

In Video Demystified(10), the author Keith Jack, gives the following table
(as noted by ariga on doom9.org; see: Table 13.16, MPEG-2 matrix_coefficients Codewords)

Sequence Display Extension: 
Color_primaries
This optional 8-bit codeword describes the chromaticity coordinates of the source primaries, 
as shown in table. If sequence_display_extension is not present, or color_description = 0, 
the indicated default value must be used. This information may be used to adjust the color 
processing after MPEG-2 decoding to compensate for the color primaries of the display.
-------------------------------------------------------------------------------------
|    Color Primaries     |    Code     |          Application Default               |
|------------------------|-------------|--------------------------------------------|
| forbidden              |  0000 0000  |                                            |
| BT.709, SMPTE 274M     |  0000 0001  | MPEG-2, ATSC, DVB 25Hz HDTV, DVB 30Hz HDTV |
| unspecified            |  0000 0010  |                                            |
| reserved               |  0000 0011  |                                            |
| BT.470 system M        |  0000 0100  | DVD-Video 30 Hz                            |
| BT.470 system B, G, I  |  0000 0101  | DVD-Video 25 Hz, DVB 25Hz SDTV             |
| SMPTE 170M             |  0000 0110  | DVD-Video 30 Hz, DVB 30Hz SDTV             |
| SMPTE 240M             |  0000 0111  |                                            |
| reserved               |  0000 1000  |                                            |
| :                      |      :      |                                            |
| reserved               |  1111 1111  |                                            |
-------------------------------------------------------------------------------------

dragongodz claims on doom9(11) that this table should be interpreted as follows:

Having finally heard back from a person who is in the professional encoder industry (no I will not say who), I have more reliable information.: "The table is actually the limitation of what you can use. In other words if you set the sequence_display_extension to Rec.709 for dvd it is illegal. That doesn't mean it will not play etc it just means that it is not supported and any player that strictly follows the specs is more likely just to ignore it as if no value was set at all.


[edit] References

  1. YCbCr (wikipedia.org)
  2. Color conversions (avisynth.nl)
  3. ColorMatrix documentation: Colorimetry (avisynth.org.ru)
  4. Luma (video) (wikipedia.org)
  5. ColorMatrix v2.3, page 24 (doom9.org)
  6. Windowless, Renderless, VMR, Overlay - what do they mean? (doom9.org)
  7. ITU-T Rec.H262 (2000 E) (itu.int)
  8. HCenc 22 and Colorimetry (doom9.org; post #18: hkazemi)
  9. A/81: Direct-To-Home Satellite Broadcast Standard (atsc.org)
  10. Video Demystified by Keith Jack (amazon.com)
  11. HCenc 22 and Colorimetry (doom9.org; post #19: dragongodz)
  12. Color FAQ by Charles Poynton (poynton.com)
Personal tools