ColorYUV

From Avisynth wiki
(Difference between revisions)
Jump to: navigation, search
(note AVS+ differences *** WIP ***)
(continue work on this page)
Line 7: Line 7:
  
 
* In AviSynth 2.6, all [[YUV]] color formats are supported ([[Y8]], [[YV12]], [[YV16]], [[YV24]], [[YV411]] and [[YUY2]])
 
* In AviSynth 2.6, all [[YUV]] color formats are supported ([[Y8]], [[YV12]], [[YV16]], [[YV24]], [[YV411]] and [[YUY2]])
* {{AvsPluscon}} all [[YUV]] and [[YUVA]] color formats are supported, except 32-bit float.
+
* {{AvsPluscon}} all [[YUV]] and [[YUVA]] integer color formats are supported.
  
  
Line 38: Line 38:
 
:{{Par2|gain_v|float|0}}
 
:{{Par2|gain_v|float|0}}
 
::A multiplier for the ''Y'', ''U'' or ''V'' value.  
 
::A multiplier for the ''Y'', ''U'' or ''V'' value.  
::If {{FuncArg|gain_y}} = {{Serif|''k''}}, then ''Y'' becomes ''Y'' '''·''' ({{Serif|''k''}}/256 + 1) – for example:
+
::If {{FuncArg|gain_y}} = {{Serif|''k''}}, then ''Y'' becomes ''Y'' '''·''' ({{Serif|''k''}} / 256 + 1) – for example:
::('''TL;DR''' ''see [[#Parameter_Scaling|Parameter Scaling]] below'')
+
::('''TL;DR''' ''see [[#Parameter_Rescaling|Parameter Rescaling]] below'')
 
::{|class="wikitable"
 
::{|class="wikitable"
 
!style="padding:0.3em;width:4em;text-align:left;"| {{FuncArg|gain_}}''x''
 
!style="padding:0.3em;width:4em;text-align:left;"| {{FuncArg|gain_}}''x''
Line 73: Line 73:
 
:{{Par2|off_v|float|0}}
 
:{{Par2|off_v|float|0}}
 
::Adds an ''offset'' to the luma or chroma values.  
 
::Adds an ''offset'' to the luma or chroma values.  
::*An {{FuncArg|off_}}''x'' of 16 will add 16 to all pixel values.   
+
::*An {{FuncArg|off_}}''x'' of 16 will add 16{{D}} to all pixel values.   
::*An {{FuncArg|off_}}''x'' of -32 will subtract 32 from all pixel values.
+
::*An {{FuncArg|off_}}''x'' of -32 will subtract 32{{D}} from all pixel values.
  
 
:{{Par2|gamma_y|float|0}}
 
:{{Par2|gamma_y|float|0}}
Line 80: Line 80:
 
:{{Par2|gamma_v|float|0}}
 
:{{Par2|gamma_v|float|0}}
 
::Adjusts [[Wikipedia:Gamma_correction|gamma]] of the specified channel,  
 
::Adjusts [[Wikipedia:Gamma_correction|gamma]] of the specified channel,  
:: as ''X''<span style="vertical-align:0.2em">(1/{{Serif|''g''}})</span> where {{Serif|''g''}}=({{FuncArg|gamma_}}''x''+256)/256 &ndash; for example:  
+
:: as ''X''{{Sup2|(1/{{Template:Serif|g}})}} where {{Serif|''g''}}=({{FuncArg|gamma_}}''x'' + 256) / 256 &ndash; for example:  
:: ('''TL;DR''' ''see [[#Parameter_Scaling|Parameter Scaling]] below'')
+
:: ('''TL;DR''' ''see [[#Parameter_Rescaling|Parameter Rescaling]] below'')
 
::{|class="wikitable"
 
::{|class="wikitable"
 
!style="padding:0.3em;width:4em;text-align:left;"| {{FuncArg|gamma_}}''x''
 
!style="padding:0.3em;width:4em;text-align:left;"| {{FuncArg|gamma_}}''x''
Line 116: Line 116:
 
::This is most useful for the chroma components, where it translates to saturation.  
 
::This is most useful for the chroma components, where it translates to saturation.  
 
::If {{FuncArg|cont_y}} = {{Serif|''k''}}, then ''Y'' becomes (''Y''-128) '''·''' {{Serif|''k''}}/256 + 128 &ndash; for example:
 
::If {{FuncArg|cont_y}} = {{Serif|''k''}}, then ''Y'' becomes (''Y''-128) '''·''' {{Serif|''k''}}/256 + 128 &ndash; for example:
:: ('''TL;DR''' ''see [[#Parameter_Scaling|Parameter Scaling]] below'')
+
:: ('''TL;DR''' ''see [[#Parameter_Rescaling|Parameter Rescaling]] below'')
 
::{|class="wikitable"
 
::{|class="wikitable"
 
!style="padding:0.3em;width:4em;text-align:left;"| {{FuncArg|cont_}}''x''
 
!style="padding:0.3em;width:4em;text-align:left;"| {{FuncArg|cont_}}''x''
Line 147: Line 147:
 
:{{Par2|levels|string|""}}
 
:{{Par2|levels|string|""}}
 
::Can be "<tt>TV->PC</tt>", "<tt>PC->TV</tt>" or nothing (default)
 
::Can be "<tt>TV->PC</tt>", "<tt>PC->TV</tt>" or nothing (default)
::This option can convert between "TV" range (black=16, white=235) and "Full" or "PC" range (black=0, white=255).
+
::This option can convert between "TV" range (black=16{{D}}, white=235{{D}}) and "Full" or "PC" range (black=0{{D}}, white=255{{D}}).
 
::*If {{FuncArg|levels}}="<tt>TV->PC</tt>", a 16{{D}}-235{{D}} &rarr; 0{{D}}-255{{D}} (contrast increasing) conversion is made
 
::*If {{FuncArg|levels}}="<tt>TV->PC</tt>", a 16{{D}}-235{{D}} &rarr; 0{{D}}-255{{D}} (contrast increasing) conversion is made
 
::*If {{FuncArg|levels}}="<tt>PC->TV</tt>", a 0{{D}}-255{{D}} &rarr; 16{{D}}-235{{D}} (contrast decreasing) conversion is made
 
::*If {{FuncArg|levels}}="<tt>PC->TV</tt>", a 0{{D}}-255{{D}} &rarr; 16{{D}}-235{{D}} (contrast decreasing) conversion is made
Line 158: Line 158:
 
:{{Par2|showyuv|bool|false}}
 
:{{Par2|showyuv|bool|false}}
 
::If ''true, ''this option will overwrite your image with a new 448x448 image showing all chroma values along the two axes. This can be useful if you need to adjust the color of your image, but need to know how the colors are arranged.  At the top left of the image, the chroma values are 16{{D}}. At the right side of the image, U is at maximum.  At the bottom of the screen V is at its maximum.  In the middle both chroma is 128 (or grey). See example image [[#Examples:|below]].
 
::If ''true, ''this option will overwrite your image with a new 448x448 image showing all chroma values along the two axes. This can be useful if you need to adjust the color of your image, but need to know how the colors are arranged.  At the top left of the image, the chroma values are 16{{D}}. At the right side of the image, U is at maximum.  At the bottom of the screen V is at its maximum.  In the middle both chroma is 128 (or grey). See example image [[#Examples:|below]].
::{{AvsPluscon}} Size is now 450x450. See the new {{FuncArg|showyuv}} options {{FuncArg|bits}} and {{FuncArg|showyuv_fullrange}} below.
+
::{{AvsPluscon}} Size changes depending on options selected:
 +
::*By default size is 450x450.  
 +
::*With {{FuncArg|showyuv_fullrange}}=true, size is 512x512.
 +
::*With {{FuncArg|bits}}=10, width and height are both multiplied by 4.  
  
 
:{{Par2|analyze|bool|false}}
 
:{{Par2|analyze|bool|false}}
Line 177: Line 180:
 
:{{Par2|bits|int|8}}
 
:{{Par2|bits|int|8}}
 
::{{AvsPluscon}} Sets the bit depth of {{FuncArg|showyuv}} output. Must be 8, 10, 12, 14 or 16.
 
::{{AvsPluscon}} Sets the bit depth of {{FuncArg|showyuv}} output. Must be 8, 10, 12, 14 or 16.
 +
::Size of the generated clip grows by
  
 
:{{Par2|showyuv_fullrange|bool|false}}
 
:{{Par2|showyuv_fullrange|bool|false}}
Line 183: Line 187:
  
  
==Parameter Scaling==
+
==Parameter Rescaling==
 
<div style="max-width:62em" >
 
<div style="max-width:62em" >
 
The quantities ''saturation'', ''contrast'' and ''brightness'' (as in [[Tweak]] for example) are connected with quantities in this filter by the following equations:
 
The quantities ''saturation'', ''contrast'' and ''brightness'' (as in [[Tweak]] for example) are connected with quantities in this filter by the following equations:
Line 193: Line 197:
 
Note that in [[Tweak]] your YUV values will always be clipped to valid TV-ranges, but here you have to specify {{FuncArg|opt}}="coring".
 
Note that in [[Tweak]] your YUV values will always be clipped to valid TV-ranges, but here you have to specify {{FuncArg|opt}}="coring".
  
You can add a simple [[User_defined_script_functions|user function]] to translate from familiar "Tweak-like" units to ColorYUV units:
+
You can add a simple [[User_defined_script_functions|user function]] to rescale or translate from familiar "Tweak-like" units to ColorYUV units:
 
<div {{BoxWidthIndent|56|2}} >
 
<div {{BoxWidthIndent|56|2}} >
 
  function f2c(float f) {
 
  function f2c(float f) {
Line 208: Line 212:
 
  ColorYUV(gain_y={{BoldColor|blue|100|256}}, cont_u={{BoldColor|blue|100|51.2}}, cont_v={{BoldColor|blue|100|-51.2}})
 
  ColorYUV(gain_y={{BoldColor|blue|100|256}}, cont_u={{BoldColor|blue|100|51.2}}, cont_v={{BoldColor|blue|100|-51.2}})
 
</div>
 
</div>
{{AvsPluscon}} parameter scaling is ''backward compatible'' &ndash; it works without changes at all bit depths.
+
{{AvsPluscon}} Note, '''ColorYUV''' parameters are ''backward compatible'' &ndash; they work without changes at all bit depths.
 
</div>
 
</div>
  
Line 249: Line 253:
 
</div>
 
</div>
  
* ''autogain'' and ''autowhite'': recovers visibility on "very bad" recordings:
+
* {{FuncArg|autogain}} and {{FuncArg|autowhite}}: recovers visibility on "very bad" recordings:
 
<div {{BoxWidthIndent|56|2}} >
 
<div {{BoxWidthIndent|56|2}} >
 
  ColorYUV(autogain=true, autowhite=true)
 
  ColorYUV(autogain=true, autowhite=true)
 
</div>
 
</div>
  
* ''showyuv'': show all colors in the [[YUV]] space. Requires a YUV clip as input. Returns a [[YV12]], 448x448 clip with source frame rate, duration and audio.
+
* {{FuncArg|showyuv}}: show all colors in the [[YUV]] space. Requires a YUV clip as input.  
 +
:Returns a [[YV12]], 448x448 clip with source frame rate, duration and audio.
 
:'''Note''', not all colors are convertible to RGB - see [http://forum.doom9.org/showthread.php?t=154731 this discussion on doom9].
 
:'''Note''', not all colors are convertible to RGB - see [http://forum.doom9.org/showthread.php?t=154731 this discussion on doom9].
:''U ''values run 16{{D}}-239{{D}}, left to right, and ''V'' values run from 16{{D}}-239{{D}}, top to bottom. Frame 0 luma is 16{{D}}, frame 1 luma is 17{{D}} and so on.
+
:''U ''values run 16{{D}}-239{{D}}, left to right, and ''V'' values run from 16{{D}}-239{{D}}, top to bottom.  
 +
:Frame 0 luma is 16{{D}}, frame 1 luma is 17{{D}} and so on.
 +
:{{AvsPluscon}} Size changes depending on options selected:
 +
:*By default size is 450x450.
 +
:*With {{FuncArg|showyuv_fullrange}}=true, size is 512x512.
 +
:*With {{FuncArg|bits}}=10, width and height are both multiplied by 4.  
 
<div {{BoxWidthIndent|56|2}} >
 
<div {{BoxWidthIndent|56|2}} >
 
  ColorYUV(showyuv=true)
 
  ColorYUV(showyuv=true)
Line 263: Line 273:
  
  
* Adjusting ''off_u'' and ''off_v'' to remove color cast (reduce yellow and magenta tints):
+
* Adjusting {{FuncArg|off_u}} and {{FuncArg|off_v}} to remove color cast (reduce yellow and magenta tints):
 
<div {{BoxWidthIndent|56|2}} >
 
<div {{BoxWidthIndent|56|2}} >
 
  [[ImageSource]]("[[:File:Lenna.png|Lenna.png]]")
 
  [[ImageSource]]("[[:File:Lenna.png|Lenna.png]]")
Line 276: Line 286:
  
  
*Demonstrating {{FuncArg|analyze}} output
+
*Demonstrating {{FuncArg|analyze}} output:
 
<div id="analyze" style="display:inline;font-size:50%;height:0;line-height:0;overflow:hidden;"></div>
 
<div id="analyze" style="display:inline;font-size:50%;height:0;line-height:0;overflow:hidden;"></div>
 
<div {{BoxWidthIndent|24|2}} >
 
<div {{BoxWidthIndent|24|2}} >

Revision as of 07:25, 26 April 2017

ColorYUV has many different methods of changing the color and luminance of your images. Gain, offset, gamma and contrast can be set independently on each channel. Also included are two 'auto' modes, an analysis function and a color gamut generator.

All settings for this filter are optional. All values are defaulting to "0" or false.

RGBAdjust works in a similar manner for RGB clips.


Contents

Syntax and Parameters

ColorYUV(clip [,
     float gain_y, float off_y, float gamma_y, float cont_y,
     float gain_u, float off_u, float gamma_u, float cont_u,
     float gain_v, float off_v, float gamma_v, float cont_v,
     string levels, string opt, bool showyuv, bool analyze,
     bool autowhite, bool autogain, bool conditional ] )

ColorYUV(clip [,
     float gain_y, float off_y, float gamma_y, float cont_y,
     float gain_u, float off_u, float gamma_u, float cont_u,
     float gain_v, float off_v, float gamma_v, float cont_v,
     string levels, string opt, bool showyuv, bool analyze,
     bool autowhite, bool autogain, bool conditional,
     int bits, bool showyuv_fullrange ] )
AVS+

clip   =
Input clip
float  gain_y = 0
float  gain_u = 0
float  gain_v = 0
A multiplier for the Y, U or V value.
If gain_y = k, then Y becomes Y · (k / 256 + 1) – for example:
(TL;DR see Parameter Rescaling below)
gain_x multiplier
  -256 (-256 / 256) + 1 0.0 (no output)
-128 (-128 / 256) + 1 0.5 (darker)
0 (0 / 256) + 1 1.0 (no change)
256 (256 / 256) + 1 2.0 (brighter)
512 (512 / 256) + 1 3.0
  • gain_x is valid down to -256, where a gain of 0.0 is applied.
  • Although it is possible, it doesn't make sense to apply this setting to the chroma of the signal.
  • In order to confuse you, in the filter Tweak this setting is called contrast.
float  off_y = 0
float  off_u = 0
float  off_v = 0
Adds an offset to the luma or chroma values.
  • An off_x of 16 will add 16d to all pixel values.
  • An off_x of -32 will subtract 32d from all pixel values.
float  gamma_y = 0
float  gamma_u = 0
float  gamma_v = 0
Adjusts gamma of the specified channel,
as X(1/g) where g=(gamma_x + 256) / 256 – for example:
(TL;DR see Parameter Rescaling below)
gamma_x gamma
  -256 (-256 / 256) + 1 0.01
-128 (-128 / 256) + 1 0.5 (darker)
0 (0 / 256) + 1 1.0 (no change)
256 (256 / 256) + 1 2.0 (lighter)
512 (512 / 256) + 1 3.0
  • gamma_x is valid down to -256, where a minimal gamma of 0.01 is applied (gamma cannot be zero)
  • Note: gamma for chroma is not implemented (gamma_u and gamma_v are dummy parameters).
float  cont_y = 0
float  cont_u = 0
float  cont_v = 0
Like gain_X, a multiplier for the value, but stretches or shrinks the signal from the center (128).
This is most useful for the chroma components, where it translates to saturation.
If cont_y = k, then Y becomes (Y-128) · k/256 + 128 – for example:
(TL;DR see Parameter Rescaling below)
cont_x contrast
  -256 (-256 / 256) + 1 0.0 (zero contrast)
-128 (-128 / 256) + 1 0.5 (less contrast)
0 (0 / 256) + 1 1.0 (no change)
256 (256 / 256) + 1 2.0 (more contrast)
512 (512 / 256) + 1 3.0
  • cont_x is valid down to -256, where the signal is set to 128.
  • Although it is possible, it doesn't make sense to apply this setting to the luma of the signal.
string  levels = ""
Can be "TV->PC", "PC->TV" or nothing (default)
This option can convert between "TV" range (black=16d, white=235d) and "Full" or "PC" range (black=0d, white=255d).
  • If levels="TV->PC", a 16d-235d → 0d-255d (contrast increasing) conversion is made
  • If levels="PC->TV", a 0d-255d → 16d-235d (contrast decreasing) conversion is made
  • By default, no conversion is made
string  opt = ""
Can be either "coring" or "" (nothing, default setting).
Specifying "coring" will clip your YUV values to the "TV" range (16d-235d). Otherwise values outside that range will be accepted.
bool  showyuv = false
If true, this option will overwrite your image with a new 448x448 image showing all chroma values along the two axes. This can be useful if you need to adjust the color of your image, but need to know how the colors are arranged. At the top left of the image, the chroma values are 16d. At the right side of the image, U is at maximum. At the bottom of the screen V is at its maximum. In the middle both chroma is 128 (or grey). See example image below.
AVS+ Size changes depending on options selected:
  • By default size is 450x450.
  • With showyuv_fullrange=true, size is 512x512.
  • With bits=10, width and height are both multiplied by 4.
bool  analyze = false
If true, ColorYUV will print color channel statistics on the screen (see example below)
  • There is an Average for all channels.
  • There are Minimum and Maximum values for all channels.
  • There is Loose Minimum and Loose Maximum which ignore the brightest and darkest 0.4% (1/256) pixels.
bool  autowhite = false
If true, this option will use the information from the analyzer, and attempt to center the color offsets. If you have recorded some material, where the colors are shifted toward one color, this filter may help. But be careful - it isn't very intelligent - if your material is a clear blue sky, autowhite will make it completely grey! This option overrides off_u and off_v.
bool  autogain = false
If true, this option will use the information from the analyzer, and attempt to create as good contrast as possible. That means, it will scale up the Y (luma) values to match the minimum and maximum values. This will make it act like an "autogain" setting on cameras, amplifying dark scenes very much, while leaving scenes with good contrast alone. Some places this is also referred to as "autolevels".
bool  conditional = false
See the Conditional Variables section for an overview of the conditional variables and their use with ColorYUV. If false (the default), ColorYUV will ignore any conditional variables.
int  bits = 8
AVS+ Sets the bit depth of showyuv output. Must be 8, 10, 12, 14 or 16.
Size of the generated clip grows by
bool  showyuv_fullrange = false
AVS+ If true, showyuv output is full range instead of TV range.


Parameter Rescaling

The quantities saturation, contrast and brightness (as in Tweak for example) are connected with quantities in this filter by the following equations:

  • cont_u = cont_v = (sat-1) * 256
  • gain_y = (cont-1) * 256
  • off_y = bright

A saturation of 0.8 gives for example: cont_u = cont_v = -0.2 * 256 = -51.2.

Note that in Tweak your YUV values will always be clipped to valid TV-ranges, but here you have to specify opt="coring".

You can add a simple user function to rescale or translate from familiar "Tweak-like" units to ColorYUV units:

function f2c(float f) {
    return (f - 1.0) * 256.0
}
## usage example: 
ColorYUV(gain_y=f2c(2.0), cont_u=f2c(1.2), cont_v=f2c(0.8) 

## that is, gain is 200%, and contrast (saturation) 
## is +20% in the U channel, -20% in the V channel.  

## ...which specified normally, would look like this: 
ColorYUV(gain_y=256, cont_u=51.2, cont_v=-51.2)

AVS+ Note, ColorYUV parameters are backward compatible – they work without changes at all bit depths.


Conditional Variables

The global variables "coloryuv_xxx" with xxx = gain_y, gain_u, gain_v, bright_y, bright_u, bright_v, gamma_y, contrast_y, contrast_u or contrast_v are read each frame, and applied. It is possible to modify these variables using FrameEvaluate or ConditionalReader.

For example:

coloryuvoffset.txt:

Type float
Default 0.0

I 25 50 0.0 255.0
R 75 225 128.0
I 250 275 255.0 0.0

the script:

Colorbars(512,256).ConvertToYV12.Trim(0,299)
ColorYUV(cont_y=10, conditional=true)
ConditionalReader("coloryuvoffset.txt", "coloryuv_gain_y", false)
ShowFrameNumber()

So up to frame 25 gain_y is equal to the default (which is 0.0), for frame 25 up to 50 the gain_y is increased from 0.0 to 255.0, etc ...

There are more examples of conditional modification at the ConditionalReader page.


Examples

  • Adjust Y (luma) gamma and offset (brightness), while increasing chroma U (yellow-blue) offset (away from yellow and toward blue)
ColorYUV(gamma_y=128, off_y=-16, off_u=5)
  • autogain and autowhite: recovers visibility on "very bad" recordings:
ColorYUV(autogain=true, autowhite=true)
  • showyuv: show all colors in the YUV space. Requires a YUV clip as input.
Returns a YV12, 448x448 clip with source frame rate, duration and audio.
Note, not all colors are convertible to RGB - see this discussion on doom9.
U values run 16d-239d, left to right, and V values run from 16d-239d, top to bottom.
Frame 0 luma is 16d, frame 1 luma is 17d and so on.
AVS+ Size changes depending on options selected:
  • By default size is 450x450.
  • With showyuv_fullrange=true, size is 512x512.
  • With bits=10, width and height are both multiplied by 4.
ColorYUV(showyuv=true)
ColorYUV(showyuv=true) fr175.png


  • Adjusting off_u and off_v to remove color cast (reduce yellow and magenta tints):
ImageSource("Lenna.png")
BicubicResize(Width/2, Height/2)
ColorYUV(off_u=+6, off_v=-23)
Histogram("levels")
Lenna-256x256-HistogramLevels-original.png
(before)
Lenna-256x256-HistogramLevels-ColorYUV-1.png
(after; note the U and V range is moved towards the center)


  • Demonstrating analyze output:
FFmpegSource2("sintel.mp4")
BicubicResize(546, 272)
ColorYUV(analyze=true)

ColorYUV analyze Ex1.jpg

Changelog

v2.6 Added conditional option
Personal tools