Layer

From Avisynth wiki
(Difference between revisions)
Jump to: navigation, search
(ColorKeyMask)
(add link to avs+ documentation)
 
(19 intermediate revisions by 3 users not shown)
Line 1: Line 1:
 +
<div style="max-width:82em; min-width:42em;" >
 +
{{AvsStarFilter}}
 +
__TOC__
 
== Layer ==
 
== Layer ==
 +
<div style="max-width:62em" >
 +
Layer (aka ''overlay'', ''blend'', ''merge'') two clips of possibly different sizes, but with the same color format.
  
This filter can overlay two clips of different sizes (but with the same color format) using different operation modes.
+
For pixel-wise transparency information, the alpha channel of an RGBA ''overlay_clip'' is used as a mask.<br>
For pixel-wise transparency information the 4th color channel of [[RGB32]] (A- or alpha-channel) is used as a mask.
+
  
{{Template:FuncDef|Layer(clip ''base_clip'', clip ''overlay_clip'', string ''op'', int ''level'', int ''x'', int ''y'', int ''threshold'', bool ''use_chroma'')}}
+
{{AvsPluscon}} Before Avisynth+ 3.4.0 Layer was working only for RGB32 and YUY2. Filter "Overlay" was used primarily for YUV. Now Layer accepts practically all formats (also RGB24/48 since v3.5).<br>
 +
Note that some modes can be similar to Overlay, but the two filters are still different.<br>
 +
Overlay accepts mask clip, Layer would use existing A plane.<br>
 +
Overlay "blend" is Layer "add", Overlay "add" is different.<br>
 +
Lighten and darken is a bit different in Overlay.<br>
 +
Layer has "placement" parameter for proper mask positioning over chroma.<br>
 +
 
 +
{{FuncDef
 +
|Layer( clip ''base_clip'', clip ''overlay_clip'' [, string ''op'', int ''level'', int ''x'', int ''y'', int ''threshold'', bool ''use_chroma'' ] )
 +
}}
 +
 
 +
{{AvsPluscon}}<br>
 +
{{FuncDef
 +
|Layer( clip ''base_clip'', clip ''overlay_clip'' [, string ''op'', int ''level'', int ''x'', int ''y'', int ''threshold'', bool ''use_chroma'', float ''opacity'', string ''placement'' ] )
 +
}}
  
 
:{{Par2|base_clip|clip|}}
 
:{{Par2|base_clip|clip|}}
:: the underlying clip which determines the size and all other video and audio properties of the result.
+
:: The underlying clip, which determines the size and all other video and audio properties of the result.
 +
::Color format must be [[YUY2]] or [[RGB32]].
 +
::{{AvsPluscon}} supports all color spaces, except YV411.
  
 
:{{Par2|overlay_clip|clip|}}
 
:{{Par2|overlay_clip|clip|}}
:: the clip which is merged onto clip.
+
:: The clip which is merged onto {{FuncArg|clip}}. If [[RGB32]] or other alpha-aware color space, the alpha channel is used as a mask.
 +
::Color format must match {{FuncArg|base_clip}}.
 +
::{{AvsPluscon}}Non-alpha plane YUV/planar RGB color spaces act as having a fully transparent alpha channel (like the former YUY2 only working mode)
  
 
:{{Par2|op|string|"add"}}
 
:{{Par2|op|string|"add"}}
::Name of the merge operation to be performed, which can be one of the following:
+
::The merge operation to be performed, which can be one of the following:
::{| class="wikitable"
+
<div style="max-width:56em;margin:0 0 0 3.5em" >
 +
{| class="wikitable"
 
|-  
 
|-  
| '''Op'''
+
!style="min-width:4em"| Op
| '''Description'''
+
! Example
 +
! Description
 
|-  
 
|-  
| add
+
|style="vertical-align:top"| ''add''
| This is the default mode. Equivalent to [[Overlay]](mode="blend"). The ''overlay_clip'' will be copied on top of the original, in proportion to ''level'', and subject to the alpha channel.
+
|[[File:Layer-base-Lena.png]] [[File:Layer-over-grad.png]]
 +
 
 +
[[File:Layer-example-add.png]]
 +
|style="vertical-align:top"| This is the default mode. Equivalent to [[Overlay]](mode="blend").<br>{{FuncArg|overlay_clip}} will be copied on top of the original, in proportion to {{FuncArg|level}}, and subject to the alpha channel. <br><br>The difference between {{FuncArg|base_clip}} and {{FuncArg|overlay_clip}} is multiplied with alpha and added to {{FuncArg|base_clip}}.
 +
*alpha=0{{D}} &nbsp;&nbsp;&nbsp;&rarr; only {{FuncArg|base_clip}} visible
 +
*alpha=128{{D}} &rarr; {{FuncArg|base_clip}} and {{FuncArg|overlay_clip}} equally blended
 +
*alpha=255{{D}} &rarr; only {{FuncArg|overlay_clip}} visible
 +
Formula used :
 +
*RGB: &nbsp;[[File:Layer-Eq1.png]]
 +
*YUY2: [[File:Layer-Eq2.png]]
 +
*In general, see {{FuncArg|opacity}} parameter
 +
*Alpha-aware: base = base + (overlay - base) * opacity * alpha / max_range{{D}}
 +
*No alpha: base = base + (overlay - base) * opacity
 
|-  
 
|-  
| subtract
+
|style="vertical-align:top"| ''subtract''
| ''base_clip'' minus ''overlay_clip'' - or to say it in another way: ''base_clip'' plus negative ''overlay_clip''. If both clips are equal and ''level''=128, a flat gray field is returned; compare to [[Subtract]].
+
|style="vertical-align:top"| [[File:Layer-example-sub.png]]
 +
|style="vertical-align:top"| {{FuncArg|base_clip}} minus {{FuncArg|overlay_clip}}. The same as ''add'', but {{FuncArg|overlay_clip}} is inverted before adding.<br><br>If both clips are equal and {{FuncArg|level}}=128, a flat gray field is returned; compare to [[Subtract]].
 
|-  
 
|-  
| lighten
+
|style="vertical-align:top"| ''lighten''
| This will copy the ''overlay_clip'' to the ''base_clip'' in areas where the ''overlay_clip'' is lighter than the ''base_clip''.
+
|style="vertical-align:top"| [[File:Layer-example-lite.png]]
 +
|style="vertical-align:top"| Copy {{FuncArg|overlay_clip}} over {{FuncArg|base_clip}} in areas where {{FuncArg|overlay_clip}} is lighter by {{FuncArg|threshold}}. <br><br>Performs the same operation as ''add'', but only when {{FuncArg|overlay_clip}} is BRIGHTER than {{FuncArg|base_clip}}. {{FuncArg|use_chroma}} must be ''true''.
 +
<br> Also known as [http://photoblogstop.com/photoshop/photoshop-blend-modes-explained ''lighter color.'']
 
|-  
 
|-  
| darken
+
|style="vertical-align:top"| ''darken''
| This will copy the ''overlay_clip'' to the ''base_clip'' in areas where ''overlay_clip'' is darker than the ''base_clip''.
+
| [[File:Layer-example-dark.png]]
 +
|style="vertical-align:top"| Copy {{FuncArg|overlay_clip}} over {{FuncArg|base_clip}} in areas where {{FuncArg|overlay_clip}} is darker by {{FuncArg|threshold}}. <br><br>The same as "lighten", but it is performed only when {{FuncArg|overlay_clip}} is DARKER than {{FuncArg|base_clip}}. {{FuncArg|use_chroma}} must be ''true''.  
 +
<br>Also known as [http://photoblogstop.com/photoshop/photoshop-blend-modes-explained ''darker color.'']
 
|-  
 
|-  
| fast
+
|style="vertical-align:top"| ''mul''
| Like ''add'', but without masking.
+
|style="vertical-align:top"| [[File:Layer-example-mul-rgb.png]]
 +
|style="vertical-align:top"| {{FuncArg|base_clip}} multiplied by {{FuncArg|overlay_clip}}. This will generally make the output darker.
 +
*alpha=0{{D}} &nbsp;&nbsp;&nbsp;&rarr; only {{FuncArg|base_clip}} visible.
 +
*alpha=255{{D}} &rarr; approx. the same luminance as {{FuncArg|base_clip}} but with the colors of {{FuncArg|overlay_clip}}.
 +
See [http://docs.gimp.org/2.8/en/gimp-concepts-layer-modes.html#layer-mode-multiply GIMP: Multiply]
 
|-  
 
|-  
| mul
+
|style="vertical-align:top"| ''fast''
| Base clip multiplied by overlay clip. This will generally make the output darker (see [http://docs.gimp.org/2.8/en/gimp-concepts-layer-modes.html#layer-mode-multiply GIMP: Multiply]).
+
|  
 +
|style="vertical-align:top"| Like ''add'', but without masking. {{FuncArg|use_chroma}} must be ''true''; {{FuncArg|level}} and {{FuncArg|threshold}} are not used. The result is simply the average of {{FuncArg|base_clip}} and {{FuncArg|overlay_clip}}.
 
|}
 
|}
 +
  
 
:{{Par2|level|int|(maximum)}}
 
:{{Par2|level|int|(maximum)}}
 
::The strength of the performed operation:
 
::The strength of the performed operation:
::* 0 - no effect: the base_clip is returned unchanged
+
::* 0 &ndash; no effect: {{FuncArg|base_clip}} is returned unchanged
::* 257 (256 for YUY2) - maximum strength
+
::* 257 (256 for YUY2) &ndash; maximum strength
 +
::{{AvsPluscon}} deprecated &ndash; use {{FuncArg|opacity}} instead.
  
 
:{{Par2|x|int|0}}
 
:{{Par2|x|int|0}}
 
:{{Par2|y|int|0}}
 
:{{Par2|y|int|0}}
::offset position of ''overlay_clip''
+
::offset position of {{FuncArg|overlay_clip}}
  
 
:{{Par2|threshold|int|0}}
 
:{{Par2|threshold|int|0}}
::Higher values make the ''op="darken"'' and ''op="lighten"'' effect more sensitive. See [[#Examples|example images]] below.
+
::Changes the transition point of {{FuncArg|op}} = "darken", "lighten."
 +
::{{AvsPluscon}} automatically scaled for bit depths over 8, keep it between 0 and 255
  
 
:{{Par2|use_chroma|bool|true}}
 
:{{Par2|use_chroma|bool|true}}
::use chroma of the '''overlay_clip''', default=true. When false only luma is used.
+
::Use chroma of the {{FuncArg|overlay_clip}}. Default=true. When false, only luma is used.  
 +
::Must be true for {{FuncArg|op}} = "darken", "lighten", "add."
  
 +
:{{Par2|opacity|float|1.0}}
 +
::{{AvsPluscon}} usable for all bit depths, replaces the previous {{FuncArg|level}} parameter. Similar to "opacity" in "Overlay"
 +
::valid values are 0.0 to 1.0. Default value is 1.0 if {{FuncArg|level}} does not exist (full transparency)
 +
::If {{FuncArg|level}} parameter is given then opacity is calculated as:
 +
:::for color spaces having alpha: opacity = level / ((1 << bits_per_pixel) + 1) which gives 1.0 for level=257 (@8bit) and 65537 (@16 bits)
 +
:::for color spaces not having alpha: opacity = level / ((1 << bits_per_pixel)) e.g. for YUY2 or other non-Alpha, gives 1.0 for level=256 (@8bit)
 +
:::Note: originally level was used in formula: (alpha*level + 1) / range_size, now level is calculated from opacity as: level = opacity * ((1 << bits_per_pixel) + 1)
  
There are some differences in the behaviour and the allowed parameter depending on the color format and the operation and here are the details:
+
:{{Par2|placement|string|"mpeg2"}}
 +
::{{AvsPluscon}} Possible values: "mpeg2" (default), "mpeg1".
 +
::Used in "mul", "darken" and "lighten", "add" and "subtract" modes with planar YUV 4:2:0 or 4:2:2 color spaces (not available for YUY2) in order to properly apply luma/overlay mask on U and V chroma channels.
  
*There is no mask (alpha-channel) in YUY2, so the alpha-channel is assumed to be fully opaque everywhere.
 
*In RGB32 the alpha-channel of the '''overlay_clip''' is multiplied with '''level''', so the resulting alpha = (alpha_mask * level + 1) / 256. <br>This means for full strength of operation alpha has to be 255 and '''level''' has to be 257.
 
  
 +
Audio, [[Clip_properties|FrameRate]] and [[Clip_properties|FrameCount]] are taken from the first clip.
  
These operators behave equally for [[RGB32]] or [[YUY2]]:
 
  
''fast'': use_chroma must be TRUE, level and threshold is not used. The result is simply the average of base_clip and overlay_clip.
+
There are some differences in the behaviour and the allowed parameter depending on the color format and the operation; here are the details:
  
''add'': threshold is not used. The difference between '''base_clip''' and '''overlay_clip''' is multiplied with alpha and added to the '''base_clip'''.
+
*There is no mask (alpha channel) in [[YUY2]], so the alpha channel is assumed to be fully opaque (255{{D}}) everywhere.
:*alpha=0 &nbsp;&nbsp;&nbsp;&rarr; only base_clip visible
+
:*alpha=128 &rarr; base_clip and overlay_clip equally blended
+
:*alpha=255 &rarr; only overlay_clip visible
+
Formula used :-
+
:*RGB: &nbsp;[[File:Layer-Eq1.png]]
+
:*YUY2: [[File:Layer-Eq2.png]]
+
  
''subtract'': the same as add, but the '''overlay_clip''' is inverted before.
+
*In [[RGB32]] the {{FuncArg|overlay_clip}} alpha channel is multiplied with {{FuncArg|level}}, so the resulting alpha is
 +
:: (alpha * {{FuncArg|level}} + 1) / 256.
 +
:This means for full strength of operation, alpha has to be 255{{D}} and {{FuncArg|level}} has to be 257.
  
 +
*{{AvsPluscon}} in alpha-aware color spaces alpha channel is multiplied with {{FuncArg|opacity}}, so the resulting alpha is
 +
:: alpha * opacity 
 +
:This means for full strength of operation, alpha has to be 255{{D}} and {{FuncArg|opacity}} has to be 1.0.
  
These operators seem to work correctly only in YUY2: [todo: check and update documentation]
+
====Examples====
 +
*'''Layer''' can be used to combine two captures of different broadcasts for reducing noise. A discussion of this idea can be found {{doom9|28438}}. Of course you have to ensure that the frames of the two clips match exactly -- use [[Trim]] or [[DeleteFrame]] as necessary. A sample script:
 +
<div {{BoxWidthIndent|46|1.5}} >
 +
clip1 = [[AviSource]]("capture-pass-1.avi").[[ConvertToYUY2]]
 +
clip2 = [[AviSource]]("capture-pass-2.avi").[[ConvertToYUY2]].[[Trim]](101, 0)
 +
return Layer(clip1, clip2, "fast")
 +
</div>
 +
</div>
  
''mul'': threshold is not used. The base_clip is colored as overlay_clip, so use_chroma should be TRUE.
+
== Mask ==
:*alpha=0 &nbsp;&nbsp;&nbsp;&rarr; only base_clip visible.
+
<div style="max-width:62em" >
:*alpha=255 &rarr; approx. the same Luminance as Base but with the colors of [[Overlay]].
+
  
''lighten'': '''use_chroma''' must be TRUE. Performs the same operation as ''add'', but only when the result is BRIGHTER than the base the new values is used. With a higher threshold the operation is more likely, so with <code>threshold=255</code> it's the same as "add", with <code>threshold=0</code> the base_clip is more likely passed unchanged, depending on the difference between base_clip and overlay_clip.
+
<div {{BlueBox2|40|0|3px solid purple}} >
 +
{{AvsPlusFullname}}<br>
 +
Up-to-date documentation: [https://avisynthplus.readthedocs.io/en/latest/avisynthdoc/corefilters/mask.html#mask https://avisynthplus.readthedocs.io]
 +
</div>
  
''darken'': the same as "lighten", but it is performed only when the result is DARKER than the base.
 
  
The audio and framerate/framecount are taken from the first clip.  
+
Applies a defined alpha-mask to a clip, by converting [[RGB32]]-only {{FuncArg|mask_clip}} to greyscale and copying that greyscale information to the alpha channel of RGB32-only {{FuncArg|clip}}. In the alpha channel, "black" means transparent and "white" means opaque. For those of you who familiar with Photoshop masks, the concept is the same. In fact you can create a black and white photo in Photoshop, load it in your script and use it as a mask.
  
====Examples====
+
{{FuncDef|Mask(clip ''clip'', clip ''mask_clip'')}}
*Demonstration of ''lighten'', ''darken'' and ''threshold'':
+
[[ImageSource]]("[[:File:Lenna.png|Lenna.png]]")
+
[[ConvertToYUY2]]
+
Layer([[BlankClip]](Last, color=[[Color_presets|color_blue]]), op="lighten"''', threshold=40''')
+
Layer([[BlankClip]](Last, color=[[Color_presets|color_yellow]]), op="darken"''', threshold=40''')
+
{|
+
|[[File:Lenna-Layer-lighten-blue%2Bdarken-yellow,threshold%3D0(default).jpg|256px]]
+
|[[File:Lenna-Layer-lighten-blue%2Bdarken-yellow,threshold%3D40.jpg|256px|]]
+
|-
+
|style="text-align:center"|''threshold'' = 0 (default)
+
|style="text-align:center"|''threshold'' = 40
+
|}
+
  
*''Layer'' can be used to combine two captures of different broadcasts for reducing noise. A discussion of this idea can be found {{doom9|28438}}. Of course you have to ensure that the frames of the two clips match exactly -- use [[Trim]] or [[DeleteFrame]] as necessary. A sample script:
+
:{{Par2|clip|clip|}}
 +
:: The base clip, which determines the size and all other video and audio properties of the result.
 +
:: Color format must be [[RGB32]].
 +
::{{AvsPluscon}} also supports [[RGB64]] and [[PlanarRGBA]].
  
clip1 = [[AviSource]]("capture-pass-1.avi").[[ConvertToYUY2]]
+
:{{Par2|mask_clip|clip|}}
clip2 = [[AviSource]]("capture-pass-2.avi").[[ConvertToYUY2]].[[Trim]](101, 0)
+
:: Alpha-mask source, as described above. Size and color format must match {{FuncArg|clip}}.  
return Layer(clip1, clip2, "fast")
+
  
== Mask ==
+
====Examples====
 +
Suppose "ss.jpg" is a snapshot from a video clip, which served as a guideline to create the mask using Paint. We use [[ImageSource]] to load the image in the script and '''Mask''' to apply it.
 +
<div {{BoxWidthIndent|68|1}} >
 +
bg  = [[AviSource]]("01gray.avi").[[ConvertToRGB32]]      # here is the background clip
 +
mk  = [[ImageSource]]("ss.jpg").ConvertToRGB32        # load the image
 +
top = AviSource("k3.avi").ConvertToRGB32.Mask(mk) # load the top layer clip and apply the mask to it
 +
Layer(bg, top)                                    # layer the background and the top layer with mask
 +
</div>
 +
</div>
  
Applies a defined alpha-mask to clip, for use with Layer, by converting ''mask_clip ''to greyscale and using that for the mask (the alpha-channel) of RGB32. In this channel "black" means completely transparent and "white" means completely opaque. For those of you who familiar with Photoshop masks, the concept is the same. In fact you can create a black and white photo in Photoshop, load it in your script and use it as a mask.
 
  
{{Template:FuncDef|Mask(clip ''clip'', clip ''mask_clip'')}}
+
== ResetMask ==
 +
<div style="max-width:62em" >
  
====Examples====
+
<div {{BlueBox2|40|0|3px solid purple}} >
ss.jpg is derived from a snapshot from a video clip, which served as a guideline to create the mask just using Paint. We use [[ImageSource]] to load the image in the script and Mask to apply it.
+
{{AvsPlusFullname}}<br>
 +
Up-to-date documentation: [https://avisynthplus.readthedocs.io/en/latest/avisynthdoc/corefilters/mask.html#resetmask https://avisynthplus.readthedocs.io]
 +
</div>
  
bg = AviSource("01gray.avi").ConvertToRGB32()    # here is the background clip
 
mk = Imagesource("ss.jpg").ConvertToRGB32()      # load the image
 
top = AviSource("k3.avi").ConvertToRGB32().Mask(mk) # load the top layer clip and apply the mask to it
 
Layer(bg, top)                                  # layer the background and the top layer clip with the mask
 
  
 +
Applies an opaque (white) alpha channel to a clip. The alpha channel of an [[RGB32]] clip is not always well-defined, depending on the source (it may contain random data); this filter is a fast way to apply an all-white mask.
  
== ResetMask ==
+
{{FuncDef|ResetMask(clip ''clip'')}}
  
Applies an "all-opaque" (that is white) alpha-mask to clip, for use with Layer. The alpha-channel of a RGB32-clip is not always well-defined (depending on the source), this filter is the faster way to apply an all white mask:
+
{{FuncDef|ResetMask(clip ''clip'', float ''mask'')}} {{AvsPluscon}}
  
{{Template:FuncDef|ResetMask(clip ''clip'')}}
+
:{{Par2|clip|clip|}}
 +
:: Source clip. Alpha channel will be set to opaque. Color format must be [[RGB32]].
 +
::{{AvsPluscon}} also supports [[RGB64]], [[PlanarRGBA]] and [[YUVA]].
  
 +
:{{Par2|float|mask|}}
 +
::{{AvsPluscon}} Optional mask value to set. No bit-depth scaling occurs, but value is clipped to be between 0 and maximum_pixel_value.
 +
::Maximum opacity is 1.0 for 32 bit float formats, and (1^bit_depth) - 1 for 8-16 bit formats
 +
</div>
  
 
== ColorKeyMask ==
 
== ColorKeyMask ==
 +
<div style="max-width:62em" >
  
Clears pixels in the alpha-channel by comparing to a ''Transparent'' color (default black).  
+
<div {{BlueBox2|40|0|3px solid purple}} >
 +
{{AvsPlusFullname}}<br>
 +
Up-to-date documentation: [https://avisynthplus.readthedocs.io/en/latest/avisynthdoc/corefilters/mask.html#colorkeymask https://avisynthplus.readthedocs.io]
 +
</div>
  
Each pixel with a color differing less than (''tolB'',''tolR'',''tolG'') (default 10) is set to transparent (that is black), otherwise it is left unchanged i.e. It is NOT set to opaque - that's why you might need [[ResetMask]] before applying this filter. This allows a aggregate mask to be constructed with multiple calls.
 
  
When ''tolR'' or ''tolG'' are not set, they get the value of ''tolB'' by default. Normally you start with a ResetMask, then chain a few calls to ColorKeyMask to cause transparent holes where each color of interest occurs. See [[Overlay]] for examples.
+
Clears pixels in the alpha channel by comparing to a transparent {{FuncArg|color}} (default black).  
  
For AviSynth versions older than v2.58, there were no separate tolerance levels for blue, green and red. There was only one tolerance level called "tolerance" and this was used for blue, green and red simultaneously.
+
Each pixel with a color differing less than ({{FuncArg|tolB}},{{FuncArg|tolR}},{{FuncArg|tolG}}) (default 10{{D}}) is set to transparent (black); otherwise it is left unchanged &ndash; note, it is NOT set to opaque (white). That's why you might need '''ResetMask''' before applying this filter. This behaviour allows an aggregate mask to be constructed with multiple calls to '''ColorKeyMask'''.  
  
{{Template:FuncDef|ColorKeyMask(clip ''clip'', int ''color'' [, int ''tolB'', int ''tolG'', int ''tolR''])}}
+
When {{FuncArg|tolR}} or {{FuncArg|tolG}} are not set, they get the value of {{FuncArg|tolB}} by default. Normally you start with a '''ResetMask''', then chain a few calls to '''ColorKeyMask''' to cause transparent holes where each color of interest occurs. See [[Overlay]] for examples.
 +
 
 +
{{FuncDef|ColorKeyMask(clip ''clip'', int ''color'' [, int ''tolB'', int ''tolG'', int ''tolR''])}}
  
 
:{{Par2|clip|clip|}}
 
:{{Par2|clip|clip|}}
::Source clip.
+
::Source clip. Color format must be [[RGB32]].
 +
::{{AvsPluscon}} also supports [[RGB64]] and [[PlanarRGBA]].
  
 
:{{Par2|color|int|black}}
 
:{{Par2|color|int|black}}
 
::Transparent color. Default black.
 
::Transparent color. Default black.
  
:{{Par2|tolB|int|10}}
+
:{{Par2|tolB|int|10{{Template:D}}}}
:{{Par2|tolG|int|tolB}}
+
:{{Par2|tolG|int|(tolB)}}
:{{Par2|tolR|int|tolB}}
+
:{{Par2|tolR|int|(tolB)}}
 
::Color tolerance. See description above.
 
::Color tolerance. See description above.
 +
</div>
 +
</div>
 +
 +
== Changelog ==
  
 +
{|border=1 cellspacing=1 cellpadding=4
 +
| v2.58
 +
| ColorKeyMask: Added separate tolerance levels.
 +
|}
  
 
[[Category:Internal filters]]
 
[[Category:Internal filters]]
 
[[Category:Layering]]
 
[[Category:Layering]]
 
[[Category:Masking]]
 
[[Category:Masking]]

Latest revision as of 19:46, 17 September 2022

Contents

[edit] Layer

Layer (aka overlay, blend, merge) two clips of possibly different sizes, but with the same color format.

For pixel-wise transparency information, the alpha channel of an RGBA overlay_clip is used as a mask.

AVS+ Before Avisynth+ 3.4.0 Layer was working only for RGB32 and YUY2. Filter "Overlay" was used primarily for YUV. Now Layer accepts practically all formats (also RGB24/48 since v3.5).
Note that some modes can be similar to Overlay, but the two filters are still different.
Overlay accepts mask clip, Layer would use existing A plane.
Overlay "blend" is Layer "add", Overlay "add" is different.
Lighten and darken is a bit different in Overlay.
Layer has "placement" parameter for proper mask positioning over chroma.

Layer( clip base_clip, clip overlay_clip [, string op, int level, int x, int y, int threshold, bool use_chroma ] )

AVS+
Layer( clip base_clip, clip overlay_clip [, string op, int level, int x, int y, int threshold, bool use_chroma, float opacity, string placement ] )

clip  base_clip =
The underlying clip, which determines the size and all other video and audio properties of the result.
Color format must be YUY2 or RGB32.
AVS+ supports all color spaces, except YV411.
clip  overlay_clip =
The clip which is merged onto clip. If RGB32 or other alpha-aware color space, the alpha channel is used as a mask.
Color format must match base_clip.
AVS+Non-alpha plane YUV/planar RGB color spaces act as having a fully transparent alpha channel (like the former YUY2 only working mode)
string  op = "add"
The merge operation to be performed, which can be one of the following:
Op Example Description
add Layer-base-Lena.png Layer-over-grad.png

Layer-example-add.png

This is the default mode. Equivalent to Overlay(mode="blend").
overlay_clip will be copied on top of the original, in proportion to level, and subject to the alpha channel.

The difference between base_clip and overlay_clip is multiplied with alpha and added to base_clip.
  • alpha=0d    → only base_clip visible
  • alpha=128dbase_clip and overlay_clip equally blended
  • alpha=255d → only overlay_clip visible

Formula used :

  • RGB:  Layer-Eq1.png
  • YUY2: Layer-Eq2.png
  • In general, see opacity parameter
  • Alpha-aware: base = base + (overlay - base) * opacity * alpha / max_ranged
  • No alpha: base = base + (overlay - base) * opacity
subtract Layer-example-sub.png base_clip minus overlay_clip. The same as add, but overlay_clip is inverted before adding.

If both clips are equal and level=128, a flat gray field is returned; compare to Subtract.
lighten Layer-example-lite.png Copy overlay_clip over base_clip in areas where overlay_clip is lighter by threshold.

Performs the same operation as add, but only when overlay_clip is BRIGHTER than base_clip. use_chroma must be true.


Also known as lighter color.

darken Layer-example-dark.png Copy overlay_clip over base_clip in areas where overlay_clip is darker by threshold.

The same as "lighten", but it is performed only when overlay_clip is DARKER than base_clip. use_chroma must be true.


Also known as darker color.

mul Layer-example-mul-rgb.png base_clip multiplied by overlay_clip. This will generally make the output darker.
  • alpha=0d    → only base_clip visible.
  • alpha=255d → approx. the same luminance as base_clip but with the colors of overlay_clip.

See GIMP: Multiply

fast Like add, but without masking. use_chroma must be true; level and threshold are not used. The result is simply the average of base_clip and overlay_clip.


int  level = (maximum)
The strength of the performed operation:
  • 0 – no effect: base_clip is returned unchanged
  • 257 (256 for YUY2) – maximum strength
AVS+ deprecated – use opacity instead.
int  x = 0
int  y = 0
offset position of overlay_clip
int  threshold = 0
Changes the transition point of op = "darken", "lighten."
AVS+ automatically scaled for bit depths over 8, keep it between 0 and 255
bool  use_chroma = true
Use chroma of the overlay_clip. Default=true. When false, only luma is used.
Must be true for op = "darken", "lighten", "add."
float  opacity = 1.0
AVS+ usable for all bit depths, replaces the previous level parameter. Similar to "opacity" in "Overlay"
valid values are 0.0 to 1.0. Default value is 1.0 if level does not exist (full transparency)
If level parameter is given then opacity is calculated as:
for color spaces having alpha: opacity = level / ((1 << bits_per_pixel) + 1) which gives 1.0 for level=257 (@8bit) and 65537 (@16 bits)
for color spaces not having alpha: opacity = level / ((1 << bits_per_pixel)) e.g. for YUY2 or other non-Alpha, gives 1.0 for level=256 (@8bit)
Note: originally level was used in formula: (alpha*level + 1) / range_size, now level is calculated from opacity as: level = opacity * ((1 << bits_per_pixel) + 1)
string  placement = "mpeg2"
AVS+ Possible values: "mpeg2" (default), "mpeg1".
Used in "mul", "darken" and "lighten", "add" and "subtract" modes with planar YUV 4:2:0 or 4:2:2 color spaces (not available for YUY2) in order to properly apply luma/overlay mask on U and V chroma channels.


Audio, FrameRate and FrameCount are taken from the first clip.


There are some differences in the behaviour and the allowed parameter depending on the color format and the operation; here are the details:

  • There is no mask (alpha channel) in YUY2, so the alpha channel is assumed to be fully opaque (255d) everywhere.
  • In RGB32 the overlay_clip alpha channel is multiplied with level, so the resulting alpha is
(alpha * level + 1) / 256.
This means for full strength of operation, alpha has to be 255d and level has to be 257.
  • AVS+ in alpha-aware color spaces alpha channel is multiplied with opacity, so the resulting alpha is
alpha * opacity
This means for full strength of operation, alpha has to be 255d and opacity has to be 1.0.

[edit] Examples

  • Layer can be used to combine two captures of different broadcasts for reducing noise. A discussion of this idea can be found here. Of course you have to ensure that the frames of the two clips match exactly -- use Trim or DeleteFrame as necessary. A sample script:
clip1 = AviSource("capture-pass-1.avi").ConvertToYUY2
clip2 = AviSource("capture-pass-2.avi").ConvertToYUY2.Trim(101, 0)
return Layer(clip1, clip2, "fast")

[edit] Mask

AviSynth+
Up-to-date documentation: https://avisynthplus.readthedocs.io


Applies a defined alpha-mask to a clip, by converting RGB32-only mask_clip to greyscale and copying that greyscale information to the alpha channel of RGB32-only clip. In the alpha channel, "black" means transparent and "white" means opaque. For those of you who familiar with Photoshop masks, the concept is the same. In fact you can create a black and white photo in Photoshop, load it in your script and use it as a mask.

Mask(clip clip, clip mask_clip)

clip  clip =
The base clip, which determines the size and all other video and audio properties of the result.
Color format must be RGB32.
AVS+ also supports RGB64 and PlanarRGBA.
clip  mask_clip =
Alpha-mask source, as described above. Size and color format must match clip.

[edit] Examples

Suppose "ss.jpg" is a snapshot from a video clip, which served as a guideline to create the mask using Paint. We use ImageSource to load the image in the script and Mask to apply it.

bg  = AviSource("01gray.avi").ConvertToRGB32      # here is the background clip
mk  = ImageSource("ss.jpg").ConvertToRGB32        # load the image
top = AviSource("k3.avi").ConvertToRGB32.Mask(mk) # load the top layer clip and apply the mask to it
Layer(bg, top)                                    # layer the background and the top layer with mask


[edit] ResetMask

AviSynth+
Up-to-date documentation: https://avisynthplus.readthedocs.io


Applies an opaque (white) alpha channel to a clip. The alpha channel of an RGB32 clip is not always well-defined, depending on the source (it may contain random data); this filter is a fast way to apply an all-white mask.

ResetMask(clip clip)

ResetMask(clip clip, float mask) AVS+

clip  clip =
Source clip. Alpha channel will be set to opaque. Color format must be RGB32.
AVS+ also supports RGB64, PlanarRGBA and YUVA.
mask  float =
AVS+ Optional mask value to set. No bit-depth scaling occurs, but value is clipped to be between 0 and maximum_pixel_value.
Maximum opacity is 1.0 for 32 bit float formats, and (1^bit_depth) - 1 for 8-16 bit formats

[edit] ColorKeyMask

AviSynth+
Up-to-date documentation: https://avisynthplus.readthedocs.io


Clears pixels in the alpha channel by comparing to a transparent color (default black).

Each pixel with a color differing less than (tolB,tolR,tolG) (default 10d) is set to transparent (black); otherwise it is left unchanged – note, it is NOT set to opaque (white). That's why you might need ResetMask before applying this filter. This behaviour allows an aggregate mask to be constructed with multiple calls to ColorKeyMask.

When tolR or tolG are not set, they get the value of tolB by default. Normally you start with a ResetMask, then chain a few calls to ColorKeyMask to cause transparent holes where each color of interest occurs. See Overlay for examples.

ColorKeyMask(clip clip, int color [, int tolB, int tolG, int tolR])

clip  clip =
Source clip. Color format must be RGB32.
AVS+ also supports RGB64 and PlanarRGBA.
int  color = black
Transparent color. Default black.
int  tolB = 10d
int  tolG = (tolB)
int  tolR = (tolB)
Color tolerance. See description above.

[edit] Changelog

v2.58 ColorKeyMask: Added separate tolerance levels.
Personal tools