Avisynth ColorMatrix

From wiki.selur.net
Jump to navigation Jump to search

ColorMatrix recalculates the YUV values of a clip for a different set of matrix coefficients (e.g. Rec.709 → Rec.601), directly in YUV without a detour through RGB. Typical use: HD material converted to SD should carry Rec.601 colors, SD upscaled to HD Rec.709 colors. It works on 8-bit clips only; ColorMatrix (AVSR) does the same for any format.

Filtering → Avisynth → Color → Conversion

ColorMatrix controls in Hybrid
ColorMatrix controls in Hybrid

What it does

A YUV picture only shows the right colors if the player decodes it with the same matrix it was encoded with. If the matrix changes along the way (for example when HD Rec.709 material is resized to SD, where players assume Rec.601), the colors shift slightly — mostly visible in reds and greens. ColorMatrix converts the values so that they are correct for the new matrix.

Settings

Control Parameter Values Hybrid default Meaning
From mode (first part) Rec.709, FCC, Rec.601, SMPTE 240M, Rec.2020 from the source Matrix of the input. Hybrid sets it from the analysed source; for an unknown matrix it guesses from the source resolution (Rec.601 up to 720×576, Rec.709 up to 1920×1088, Rec.2020 above).
To mode (second part) Rec.709, FCC, Rec.601, SMPTE 240M, Rec.2020 Rec.601 Matrix of the output; locked while Adjust by resolution is on.
Adjust by resolution – on/off on Picks To from the output resolution: Rec.601 up to 720×576, Rec.709 above, Rec.2020 above 1920×1088.
Clipping clamp no clipping, pre clipping, post clipping, pre and post clipping pre and post clipping Clamps to 16–235 (luma) and 16–240 (chroma) before and/or after the conversion.

Example

Rec.709 to Rec.601 on a 10-bit YUV 4:2:0 source (640×352, so Adjust by resolution picks Rec.601); Hybrid goes to 8 bit for the plugin:

# Converting from 10 to 8bit for ColorMatrix
ConvertBits(8)
# color modifications
ColorMatrix(mode="Rec.709->Rec.601",clamp=3,interlaced=false,hints=false)
# Converting from 8 to 10bit for encoder
ConvertBits(10)

How Hybrid calls it

  • Hybrid loads ColorMatrix64.dll. The plugin needs 8-bit YV12 or YUY2, so Hybrid converts higher bit depths to 8 bit first.
  • The filter is left out when From and To are the same.
  • interlaced is set from the source's scan type; hints=true is only used with the DGDecode source filter, which passes the matrix of MPEG-2 streams to the plugin.
  • Later steps of the script use the new matrix (for example when converting to RGB).
  • ColorMatrix and ColorMatrix (AVSR) exclude each other: enabling one disables the other.
  • ColorMatrix also offers the common options and can be added to the filter queue several times.

Background

ColorMatrix was written for AviSynth by Wilbert Dijkhof and later rewritten by tritical. The clamp values are 0 = none, 1 = before, 2 = after, 3 = both (the behavior of the old 1.x versions). The build Hybrid ships also knows Rec.2020.

AviSynth+ itself can change the matrix only through RGB (the matrix arguments of the Convert functions).

See also: Vapoursynth ColorMatrix

Sources