Convert

From Avisynth wiki
(Difference between revisions)
Jump to: navigation, search
m (added category)
(formatting, links, phrasing)
Line 1: Line 1:
{{Template:Func10Def|ConvertToRGB(clip [, string ''matrix''] [, bool ''interlaced''] [, string ''ChromaInPlacement''] [, string ''chromaresample''])|ConvertToRGB24(clip [, string ''matrix''] [, bool ''interlaced''] [, string ''ChromaInPlacement''] [, string ''chromaresample''])|ConvertToRGB32(clip [, string ''matrix''] [, bool ''interlaced''] [, string ''ChromaInPlacement''] [, string ''chromaresample''])|ConvertToYUY2(clip [, bool ''interlaced''] [, string ''matrix''] [, string ''ChromaInPlacement''] [, string ''chromaresample''])|ConvertBackToYUY2(clip [, string ''matrix'']|ConvertToY8(clip [, string ''matrix''])|ConvertToYV411(clip [, bool ''interlaced''] [, string ''matrix''] [, string ''ChromaInPlacement''] [, string ''chromaresample''])|ConvertToYV12(clip [, bool ''interlaced''] [, string ''matrix''] [, string ''ChromaInPlacement''] [, string ''chromaresample''] [, string ''ChromaOutPlacement''])|ConvertToYV16(clip [, bool ''interlaced''] [, string ''matrix''] [, string ''ChromaInPlacement''] [, string ''chromaresample''])|ConvertToYV24(clip [, bool ''interlaced''] [, string ''matrix''] [, string ''ChromaInPlacement''] [, string ''chromaresample'']))}}
+
Convert between any of the color formats that AviSynth supports:
 
+
:{| class="wikitable"
== Colorformats ==
+
 
+
The following formats can be converted to and from.
+
 
+
{| border="1"
+
 
|-  
 
|-  
! colorformats
+
! Color format
! planar/interleaved
+
! Image format
! chroma resolution
+
! Chroma resolution
 
|-  
 
|-  
! [[RGB]]
+
| '''[[RGB24]]'''
| interleaved
+
| [[Interleaved|interleaved]]
| full chroma - 4:4:4
+
| 4:4:4 (full chroma)
 
|-  
 
|-  
! [[RGB24]]
+
| '''[[RGB32]]'''
 
| interleaved
 
| interleaved
| full chroma - 4:4:4
+
| 4:4:4 (full chroma)
 
|-  
 
|-  
! [[RGB32]]
+
| '''[[YV24]]'''
| interleaved
+
| [[Planar|planar]]
| full chroma - 4:4:4
+
| 4:4:4 (full chroma)
 
|-  
 
|-  
! [[YUY2]]
+
| '''[[YV16]]'''
| interleaved
+
| planar
| chroma shared between 2 pixels - 4:2:2
+
| 4:2:2 (chroma shared between 2 pixels)
 
|-  
 
|-  
! [[Y8]]
+
| '''[[YUY2]]'''
| planar/interleaved
+
| interleaved
| no chroma - 4:0:0
+
| 4:2:2 (chroma shared between 2 pixels)
 
|-  
 
|-  
! [[YV411]]
+
| '''[[YV12]]'''
 
| planar
 
| planar
| chroma shared between 4 pixels - 4:1:1
+
| 4:2:0 (chroma shared between 2x2 pixels) 
 
|-  
 
|-  
! [[YV12]]
+
| '''[[YV411]]'''
 
| planar
 
| planar
| chroma shared between 2x2 pixels - 4:2:0
+
| 4:1:1 (chroma shared between 4 pixels)
 
|-  
 
|-  
! [[YV16]]
+
| '''[[Y8]]'''
| planar
+
| (both)
| chroma shared between 2 pixels - 4:2:2
+
| 4:0:0 (no chroma)
|-
+
! [[YV24]]
+
| planar
+
| full chroma - 4:4:4
+
 
|}
 
|}
 +
When the target format is the same as the source format, the original clip will be returned unchanged, except for the case of '''ConvertToYV12''' where the ''ChromaInPlacement'' and ''ChromaOutPlacement'' parameters (see below) are different.
  
== Options ==
+
There is no unique way of [[Color_conversions|converting]] YUV to RGB or vice-versa. There are different ''conversion matrices'' in use; the two most common ones are available in AviSynth, namely ''Rec.601'' and ''Rec.709'', plus some others. The following should be correct in ''most'' cases (see [[Colorimetry#How_can_I_see_if_the_correct_standard_is_used_upon_playback.3F|here]] for more)
 +
*Rec.601 should be used when your source is standard definition (usually defined as smaller than [[Wikipedia:720p|720p]]):
 +
:<tt>ConvertToRGB(clip) </tt> (using default "Rec601")
 +
*Rec.709 should be used when your source is DVD or HDTV:
 +
:<tt>ConvertToRGB(clip, matrix="Rec709")</tt>
 +
*The special-purpose matrices ''PC.601'' and ''PC.709'' keep the ''range'' unchanged,
 +
:instead of converting between 0-255&nbsp;RGB and 16-235&nbsp;YUV, as is the normal practice.
 +
*The special-purpose matrix ''AVERAGE'' is used for (rarely found) sources with ''unweighted'' luma,
 +
:where ''Y'' = (R + G + B) / 3.
  
;''matrix'':
+
Use '''ConvertBackToYUY2''' to convert ''back'' to YUY2 with minimal color-blurring when you have previously applied a YUY2&rarr;RGB conversion.
:Default "Rec601". Controls the colour coefficients and scaling factors used in RGB - YUV conversions.
+
:*"Rec601" : Uses Rec.601 coefficients; scale full range [0..255] RGB to TV range [16..235] YUV, and vice versa.
+
:*"Rec709" : Uses Rec.709 coefficients; scale full range [0..255] RGB to TV range [16..235] YUV, and vice versa.
+
:*"PC.601" : Uses Rec.601 coefficients, keep full range.
+
:*"PC.709" : Uses Rec.709 coefficients, keep full range.
+
:*"AVERAGE" : Uses averaged coefficients, keep full range. (So the luma becomes the average of the RGB channels.)
+
  
;''interlaced'':
 
:Default false. Use interlaced layout for YV12 conversions.
 
:Since v2.51/v2.52 an optional interlaced parameter is added (interlaced=false is the default operation). When set to false it is assumed that clip is progressive, when set to true it is assumed that clip is interlaced. This option is added because for example (assuming clip is interlaced YV12):
 
::<code>[[SeparateFields]](clip)
 
::ConvertToYUY2()
 
::[[Weave]]()</code>
 
:is upsampled incorrectly. Instead it is better to use:
 
::<code>ConvertToYUY2(clip, interlaced=true)</code>
 
:Note, the ''interlaced''=true setting only does something if the conversion YV12 <-> YUY2/RGB is requested, otherwise it's simply ignored. More about it can be found [[Interlaced fieldbased|here]].
 
  
;''ChromaInPlacement''  
+
== Syntax and Parameters ==
:(added in v2.60): This determines the chroma placement when converting from YV12. It can be "[[MPEG2]]" (default), "[[MPEG1]]" and "DV".
+
{{Template:Func10Def
 +
|ConvertToRGB(clip [, string ''matrix'', bool ''interlaced'', string ''ChromaInPlacement'', string ''chromaresample'' ] )
 +
|ConvertToRGB24(clip [, string ''matrix'', bool ''interlaced'', string ''ChromaInPlacement'', string ''chromaresample'' ] )
 +
|ConvertToRGB32(clip [, string ''matrix'', bool ''interlaced'', string ''ChromaInPlacement'', string ''chromaresample'' ] )
 +
|ConvertToYV24(clip [, bool ''interlaced'', string ''matrix'', string ''ChromaInPlacement'', string ''chromaresample'' ] )
 +
|ConvertToYV16(clip [, bool ''interlaced'', string ''matrix'', string ''ChromaInPlacement'', string ''chromaresample'' ] )
 +
|ConvertToYUY2(clip [, bool ''interlaced'', string ''matrix'', string ''ChromaInPlacement'', string ''chromaresample'' ] )
 +
|ConvertToYV12(clip [, bool ''interlaced'', string ''matrix'', string ''ChromaInPlacement'', string ''chromaresample'', string ''ChromaOutPlacement'' ] )
 +
|ConvertToYV411(clip [, bool ''interlaced'', string ''matrix'', string ''ChromaInPlacement'', string ''chromaresample'' ] )
 +
|ConvertToY8(clip [, string ''matrix'' ] )
 +
|ConvertBackToYUY2(clip [, string ''matrix'' ] )
 +
}}
  
;''chromaresample''
 
:(added in v2.60): This determines which resizer is used in the conversion. It is used when the chroma resolution of the source and target is different. It can be all resamplers ("point", "bilinear", "bicubic", "lanczos", "lanczos4", "blackman", "spline16", "spline36", "spline64", "gauss" and "sinc"), default is "bicubic".
 
  
;''ChromaOutPlacement''
+
{{Par2|matrix|string|"Rec601"}}
:(added in v2.60): This determines the chroma placement when converting to YV12. It can be "[[MPEG2]]" (default), "[[MPEG1]]" and "DV".  
+
:Controls the colour coefficients and scaling factors used in RGB&harr;YUV conversions.
<br>
+
:*"Rec601" : Uses Rec.601 coefficients; scale full range [0..255] RGB to TV range [16..235] YUV, and vice versa.
AviSynth prior to v2.60 can deal internally with three color formats, '''RGB''', '''YUY2''' and '''YV12'''. These six filters convert between them. If the video is already in the specified format, it will be passed through unchanged. (RGB is assumed throughout this doc to mean RGBA.) ConvertToRGB converts to RGB32 unless your clip is RGB24. If you need 24-bit RGB for some reason, use ConvertToRGB24 explicitly and ConvertToRGB32 to do the reverse.
+
:*"Rec709" : Uses Rec.709 coefficients; scale full range [0..255] RGB to TV range [16..235] YUV, and vice versa.
 +
:*"PC.601" : Uses Rec.601 coefficients; keep range unchanged.
 +
:*"PC.709" : Uses Rec.709 coefficients; keep range unchanged.
 +
:*"AVERAGE" : Uses averaged coefficients (the luma becomes the average of the RGB channels); keep range unchanged.
  
In v2.60 the following additional formats are supported: '''Y8''' greyscale (it is both planar and interleaved since it contains no chroma; 4:0:0), '''YV411''' (planar; YUV 4:1:1), '''YV16''' (a planar version of YUY2; 4:2:2) and '''YV24''' (planar; YUV 4:4:4).
 
  
Syntax and operation of ConvertToRGB24 are identical to ConvertToRGB, except that the output format is 24-bit; if the source is RGB32, the alpha channel will be stripped.
+
{{Par2|interlaced|bool|false}}
 +
:If true, it is assumed that {{FuncArg|clip}} is interlaced; by default, it is assumed to be progressive.
 +
:This option is needed because for example, the following (assuming clip is interlaced YV12):
 +
<div {{BoxWidthIndent|28|2}} >
 +
[[SeparateFields]](clip)
 +
ConvertToYUY2
 +
[[Weave]]
 +
</div>
 +
:...is upsampled incorrectly. Instead it is better to use:
 +
<div {{BoxWidthIndent|28|2}} >
 +
ConvertToYUY2(clip, interlaced=true)
 +
</div>
 +
:Note, {{FuncArg|interlaced}}=true has an effect only on YV12&harr;YUY2 or YV12&harr;RGB conversion. More about that can be found [[Interlaced fieldbased|here]].
  
When the target format is the same as the source format, the original clip will be returned. Except for the following situation: When both formats are YV12, the source clip will be processed when ''ChromaInPlacement'' and ''ChromaOutPlacement'' differ. So this can be used to change the chroma placement in a YV12 clip.
 
  
== Examples ==
+
{{Par2|ChromaInPlacement|string|"MPEG2"}}
 
+
:Placement of the chroma samples in the {{FuncArg|clip}}. Can be one of these strings:
Contrary to what one might expect, there is no unique way of converting YUV to RGB. In AviSynth the two most common ones are implemented: Rec.601 and Rec.709 (named after their official specifications). Although it will not be correct in all cases, the following should be correct in most cases:
+
:{|class="wikitable" width="600"
 
+
|-
The first one (Rec.601) should be used when your source is DivX/XviD or some analogue capture:
+
|"MPEG2"&nbsp;
 
+
|Subsampling used in MPEG-2 4:2:''x'' and most other formats. Chroma samples are located on the left pixel column of the group&nbsp;(default).
ConvertToRGB(clip)
+
|-
 
+
|"MPEG1"
The second one (Rec.709) should be used when your source is DVD or HDTV:
+
|Subsampling used in MPEG-1 4:2:0. Chroma samples are located on the center of each group of 4 pixels.
 
+
|-
ConvertToRGB(clip, matrix="rec709")
+
|"DV"
 
+
| Like MPEG-2, but U and V channels are “co-sited” vertically: V on the top row, and U on the bottom row. For 4:1:1, chroma is located on the leftmost column.
In v2.56, the reverse is also available, that is
+
|}
 
+
ConvertToYUY2(clip, matrix="rec709") or ConvertToYV12(clip, matrix="rec709")
+
  
In v2.56, ''matrix''="pc.601" (and ''matrix''="pc.709") enables you to do the RGB <-> YUV conversion while keeping the luma range, thus RGB [0,255] <-> YUV [0,255] (instead of the usual/default RGB [0,255] <-> YUV [16,235]).
 
  
All [[VirtualDub]] filters (loaded with [[Plugins|LoadVirtualdubPlugin]]) support ''only'' [[RGB32]] input.
+
{{Par2|chromaresample|string|"bicubic"}}
 +
:Determines which chroma resampler is used in the conversion. It is only used when the chroma resolutions of the source and target are different. All AviSynth [[Resize|resizers]] are allowed ("point", "bilinear", "bicubic", "lanczos", "lanczos4", "blackman", "spline16", "spline36", "spline64", "gauss" and "sinc"). Default is "bicubic".
  
If you try to use any of these filters with RGB input, you will get an error. Putting ConvertToYUY2 just before the offending filter should resolve the problem. All AviSynth filters support YUY2 input.
 
  
Conversion back and forth is not lossless, so use as few conversions as possible. If multiple conversions are necessary, use ConvertBackToYUY2() to convert back to YUY2, when you applied a YUY2->RGB conversion prior to that in your script. This will reduce color-blurring, but there are still some precision lost.  
+
{{Par2|ChromaOutPlacement|string|"MPEG2"}}
 +
:Determines the output chroma placement in '''ConvertToYV12'''. See {{FuncArg|ChromaInPlacement}}.  
  
In most cases, the ConvertToRGB filter should not be necessary. If AviSynth's output is in YUY2 format and an application expects RGB, the system will use the installed YUY2 codec to make the conversion. However, if there's no installed YUY2 codec, or if (as is the case with ATI's YUY2 codec) the codec converts from YUY2 to RGB incorrectly, you can use Avisynth's built-in filter to convert instead.
 
  
== Conversion paths ==
+
== Notes ==
 +
==== Conversion paths ====
  
* The ''ChromaInPlacement'', ''chromaresample'' and ''ChromaOutPlacement'' options are only used in the 'planar conversion part' of the conversion path, and they process the chroma of the clip.
+
: The chroma options {{FuncArg|ChromaInPlacement}}, {{FuncArg|chromaresample}} and {{FuncArg|ChromaOutPlacement}} options are only used in the 'planar conversion' part of the conversion path, and they process the chroma of the clip.
  
In v2.60 the following conversion paths occur
+
:In v2.60 the following conversion paths occur:
 +
:* YUV planar &rarr; RGB via YV24
 +
:* YUV planar &rarr; YUY2 via YV16
 +
::(except for YV12 and chroma options listed above not explicitly set; in that case there is a direct conversion from YV12 to YUY2)
 +
:* RGB &rarr; YUV planar via YV24
 +
:* YUY2 &rarr; YUV planar via YV16
 +
::(except for YV12 and chroma options listed above not explicitly set; in that case there is a direct conversion from YUY2 to YV12)
  
* YUV planar -> RGB via YV24
+
:Suppose you have a YUY2 clip for example and you convert it to YV24. The YUY2 will be converted to YV16 first without applying the chroma options. Then YV16 will be converted to YV24 while applying {{FuncArg|chromaresample}}. {{FuncArg|ChromaInPlacement}} and ''ChromaOutPlacement'' won't be used since YV12 is not involved.
* YUV planar -> YUY2 via YV16 (except for YV12 and parameters ''ChromaInPlacement''/''chromaresample''/''ChromaOutPlacement'' not explicitly set", in that case there is a direct conversion from YV12 to YUY2)
+
* RGB -> YUV planar via YV24
+
* YUY2 -> YUV planar via YV16 (except for YV12 and parameters ''ChromaInPlacement''/''chromaresample''/''ChromaOutPlacement'' not explicitly set", in that case there is a direct conversion from YUY2 to YV12)
+
  
Suppose you have a YUY2 clip for example and you convert it to YV24. The YUY2 will be converted to YV16 first without applying ''ChromaInPlacement'', ''chromaresample'' and ''ChromaOutPlacement''. Then YV16 will be converted to YV24 while applying ''chromaresample''. ''ChromaInPlacement'' and ''ChromaOutPlacement'' won't be used since YV12 is not involved.
+
==== Sampling ====
  
== Sampling ==
+
:[[Sampling|This page]] covers the sampling methods and color formats in more detail.
  
[[Sampling|This page]] covers the sampling methods and color formats in more detail.
+
==== Color conversions ====
  
== Color conversions ==
+
:[[Color_conversions|This page]] covers the color conversions in more detail.
  
[[Color_conversions|This page]] covers the color conversions, "YUV <-> RGB", in more detail.
 
  
 
== Changelog ==
 
== Changelog ==

Revision as of 15:20, 16 January 2016

Convert between any of the color formats that AviSynth supports:

Color format Image format Chroma resolution
RGB24 interleaved 4:4:4 (full chroma)
RGB32 interleaved 4:4:4 (full chroma)
YV24 planar 4:4:4 (full chroma)
YV16 planar 4:2:2 (chroma shared between 2 pixels)
YUY2 interleaved 4:2:2 (chroma shared between 2 pixels)
YV12 planar 4:2:0 (chroma shared between 2x2 pixels) 
YV411 planar 4:1:1 (chroma shared between 4 pixels)
Y8 (both) 4:0:0 (no chroma)

When the target format is the same as the source format, the original clip will be returned unchanged, except for the case of ConvertToYV12 where the ChromaInPlacement and ChromaOutPlacement parameters (see below) are different.

There is no unique way of converting YUV to RGB or vice-versa. There are different conversion matrices in use; the two most common ones are available in AviSynth, namely Rec.601 and Rec.709, plus some others. The following should be correct in most cases (see here for more)

  • Rec.601 should be used when your source is standard definition (usually defined as smaller than 720p):
ConvertToRGB(clip) (using default "Rec601")
  • Rec.709 should be used when your source is DVD or HDTV:
ConvertToRGB(clip, matrix="Rec709")
  • The special-purpose matrices PC.601 and PC.709 keep the range unchanged,
instead of converting between 0-255 RGB and 16-235 YUV, as is the normal practice.
  • The special-purpose matrix AVERAGE is used for (rarely found) sources with unweighted luma,
where Y = (R + G + B) / 3.

Use ConvertBackToYUY2 to convert back to YUY2 with minimal color-blurring when you have previously applied a YUY2→RGB conversion.


Contents

Syntax and Parameters

ConvertToRGB(clip [, string matrix, bool interlaced, string ChromaInPlacement, string chromaresample ] )
ConvertToRGB24(clip [, string matrix, bool interlaced, string ChromaInPlacement, string chromaresample ] )
ConvertToRGB32(clip [, string matrix, bool interlaced, string ChromaInPlacement, string chromaresample ] )
ConvertToYV24(clip [, bool interlaced, string matrix, string ChromaInPlacement, string chromaresample ] )
ConvertToYV16(clip [, bool interlaced, string matrix, string ChromaInPlacement, string chromaresample ] )
ConvertToYUY2(clip [, bool interlaced, string matrix, string ChromaInPlacement, string chromaresample ] )
ConvertToYV12(clip [, bool interlaced, string matrix, string ChromaInPlacement, string chromaresample, string ChromaOutPlacement ] )
ConvertToYV411(clip [, bool interlaced, string matrix, string ChromaInPlacement, string chromaresample ] )
ConvertToY8(clip [, string matrix ] )
ConvertBackToYUY2(clip [, string matrix ] )


string  matrix = "Rec601"

Controls the colour coefficients and scaling factors used in RGB↔YUV conversions.
  • "Rec601" : Uses Rec.601 coefficients; scale full range [0..255] RGB to TV range [16..235] YUV, and vice versa.
  • "Rec709" : Uses Rec.709 coefficients; scale full range [0..255] RGB to TV range [16..235] YUV, and vice versa.
  • "PC.601" : Uses Rec.601 coefficients; keep range unchanged.
  • "PC.709" : Uses Rec.709 coefficients; keep range unchanged.
  • "AVERAGE" : Uses averaged coefficients (the luma becomes the average of the RGB channels); keep range unchanged.


bool  interlaced = false

If true, it is assumed that clip is interlaced; by default, it is assumed to be progressive.
This option is needed because for example, the following (assuming clip is interlaced YV12):
SeparateFields(clip)
ConvertToYUY2
Weave
...is upsampled incorrectly. Instead it is better to use:
ConvertToYUY2(clip, interlaced=true)
Note, interlaced=true has an effect only on YV12↔YUY2 or YV12↔RGB conversion. More about that can be found here.


string  ChromaInPlacement = "MPEG2"

Placement of the chroma samples in the clip. Can be one of these strings:
"MPEG2"  Subsampling used in MPEG-2 4:2:x and most other formats. Chroma samples are located on the left pixel column of the group (default).
"MPEG1" Subsampling used in MPEG-1 4:2:0. Chroma samples are located on the center of each group of 4 pixels.
"DV" Like MPEG-2, but U and V channels are “co-sited” vertically: V on the top row, and U on the bottom row. For 4:1:1, chroma is located on the leftmost column.


string  chromaresample = "bicubic"

Determines which chroma resampler is used in the conversion. It is only used when the chroma resolutions of the source and target are different. All AviSynth resizers are allowed ("point", "bilinear", "bicubic", "lanczos", "lanczos4", "blackman", "spline16", "spline36", "spline64", "gauss" and "sinc"). Default is "bicubic".


string  ChromaOutPlacement = "MPEG2"

Determines the output chroma placement in ConvertToYV12. See ChromaInPlacement.


Notes

Conversion paths

The chroma options ChromaInPlacement, chromaresample and ChromaOutPlacement options are only used in the 'planar conversion' part of the conversion path, and they process the chroma of the clip.
In v2.60 the following conversion paths occur:
  • YUV planar → RGB via YV24
  • YUV planar → YUY2 via YV16
(except for YV12 and chroma options listed above not explicitly set; in that case there is a direct conversion from YV12 to YUY2)
  • RGB → YUV planar via YV24
  • YUY2 → YUV planar via YV16
(except for YV12 and chroma options listed above not explicitly set; in that case there is a direct conversion from YUY2 to YV12)
Suppose you have a YUY2 clip for example and you convert it to YV24. The YUY2 will be converted to YV16 first without applying the chroma options. Then YV16 will be converted to YV24 while applying chromaresample. ChromaInPlacement and ChromaOutPlacement won't be used since YV12 is not involved.

Sampling

This page covers the sampling methods and color formats in more detail.

Color conversions

This page covers the color conversions in more detail.


Changelog

v2.60 Added ConvertToY8, ConvertToYV411, ConvertToYV16, ConvertToYV24, ChromaInPlacement, ChromaOutPlacement, chromaresample, matrix="AVERAGE".
v2.50 Added ConvertToYV12.
Personal tools