martin53
14th February 2015, 21:18
The attached script is intended as the ultimate wrapper for all ConvertTo...() calls.
With the internal ConvertTo...() functions, I repeatedly struggle with several issues
- Matrix parameter is not allowed when converting from YUV to YUV, and RGB to RGB. So script lines with the parameter throw an error if a clip happens to be in the same color space.
- Matrix parameter depends on clip. Here (http://forum.doom9.org/showthread.php?t=169155) was found that it can in most cases be derived from clip resolution, but the internal functions require the user to enter it.
- Sometimes a number of formats are possible, and it can get tedious wo write down all the conditional commands depending on the format of the current clip
- it is harder than neccessary to set the format of a clip identical to the format of a reference clip. E.g. a user function could convert the format accordingly to the internally neded plugins, and then convert the clip back to the input format, so the function can be inserted everywhere without performing a hidden format change.
- ...
Comments & suggestions welcome.
# Cnv2() is designed to replace all pixel type conversion calls (ConvertTo...()).
# It features all ConvertTo...() function parameters, plus the boolean parameters 'PC', 'stacked' and the int 'tunnel'.
# The name Cnv2() is a phonetic shorthand for 'ConvertTo' for lazy typers.
#
# CCITT color transformation coefficients (601 or 709) are automatically chosen depending on clip size.
#
# Additional features: Instead of a pixel type string, simply a reference clip with the desired pixel type may be used.
# Arbitrary combinations of pixel type names or even pixel type Classes e.g. "RGB", "YUV" etc. may be used as target pixel type.
# Possible pixel type tokens in pixel_type: RGB24,RGB32,Y8,YV12,YV16,YV24,YV411,YUY2
# Possible pixel type class tokens in pixel_type: RGB,YUV,444,PLANAR,INTERLEAVED
#
# Function Parameters
# val pixel_type Either a string with one or more target pixel type names and/or pixel type classes or a reference clip with the target pixel type
# str matrix ("") CCITT matrix name. If omitted: Rec601/Rec709 automatically chosen depending on clip size
# bool interlaced (false) Interlaced layout for YV12 conversions. See AviSynth doc 'ConvertToYV12()'
# str ChromaInPlacement ("") For converting from YV12. See AviSynth doc 'ConvertToYV12()'
# str chromaresample ("") Chroma resampling for conversions with different chroma subsampling. See AviSynth doc 'ConvertToYV12()'
# str ChromaOutPlacement ("") For converting to YV12. See AviSynth doc 'ConvertToYV12()'
# bool PC (false) If true, PC.601/PC.709 are used instead of Rec601/Rec709. The variant 601/709 is still automatically chosen
# bool stacked (false) See 'dither' package. If true, vertical size of stacked 16bit frame is assumed for Rec601/Rec709 decision
# int tunnel (0) if >0, for YUV<->RGB conversion, direct channel transfer Y<->G, U<->B, and V<->R channels
# Tunneling allows to use most RGB filters on YUV, or YUV filters on RGB footage with:
# Source(pixel_type="YVxx").Cnv2("RGB",tunnel=1).YourRGBFilters().Cnv2("YV24",tunnel=2)
# or Source(pixel_type="RGBxx").Cnv2("YV24",tunnel=1).YourYUVFilters().Cnv2("RGB",tunnel=2)
# NO color conversion is done. So it's faster and 100% lossless
# 1 tunnel entrance. If RGB requested and clip is YUV, or vice versa, frame is tunneled and global var gCnv2Tunnel is set.
# But if clip is already RGB (UR YUV, if YUV requested), there is no tunnel.
# 2 tunnel exit. Only if global var gCnv2Tunnel is !="", tunnel exit is performed, otherwise normal conversion.
# Tunneling is tolerant against input/output formats to the maximum possible extent.
#
# Key features
# Cnv2(reference_clip) is a big helper for StackHorizontal() and a lot of other filters. For comparison output,
# StackHorizontal(original,processed.Cnv2(original)) is all you need, *no matter what pixel_type of original is*
# Cnv2() allows more than one output pixel_type. E.g. if no YV24 renderer is available, but YV12 and RGB24
# renderers, Cnv2("YV12 RGB24") at the end of the filter chain will convert all YUV footage to YV24, and all RGB
# footage to RGB24.
# For universal scripting, Cnv2("...") lines with all possible pixel_types reduce conversions, yet prevent crashes
# Cnv2() automatically omits or inserts the 'matrix=' parameter in the resolved base conversions, depending on the
# fact that a YUV<->RGB conversion is performed or not.
# For HD footage, Rec709 coefficients are used automatically.
# Cnv2() does not affect framereate performance, because it performs some string calculations during filter graph
# setup, but resolves to typically one base conversion or no conversion at all.
# No-Conversion situations are detected
# Conversions that yield best output resolution and least color space transformations are preferred
#
# Caveats
# Because of the global variable, multi-instances scripts work only correctly, if only one color space,
# (i.e. RGB *OR* YUV) is used as a tunnel in one script. There will be interference if at different places of
# the script RGB and YUV are both used as tunnels.
#
# Needs GScript()
#
# Version 1.0, 02-14-2015
#
EDIT: 2015-02-16 attached version is 1.02 with again slightly improved tunnel processing and description
With the internal ConvertTo...() functions, I repeatedly struggle with several issues
- Matrix parameter is not allowed when converting from YUV to YUV, and RGB to RGB. So script lines with the parameter throw an error if a clip happens to be in the same color space.
- Matrix parameter depends on clip. Here (http://forum.doom9.org/showthread.php?t=169155) was found that it can in most cases be derived from clip resolution, but the internal functions require the user to enter it.
- Sometimes a number of formats are possible, and it can get tedious wo write down all the conditional commands depending on the format of the current clip
- it is harder than neccessary to set the format of a clip identical to the format of a reference clip. E.g. a user function could convert the format accordingly to the internally neded plugins, and then convert the clip back to the input format, so the function can be inserted everywhere without performing a hidden format change.
- ...
Comments & suggestions welcome.
# Cnv2() is designed to replace all pixel type conversion calls (ConvertTo...()).
# It features all ConvertTo...() function parameters, plus the boolean parameters 'PC', 'stacked' and the int 'tunnel'.
# The name Cnv2() is a phonetic shorthand for 'ConvertTo' for lazy typers.
#
# CCITT color transformation coefficients (601 or 709) are automatically chosen depending on clip size.
#
# Additional features: Instead of a pixel type string, simply a reference clip with the desired pixel type may be used.
# Arbitrary combinations of pixel type names or even pixel type Classes e.g. "RGB", "YUV" etc. may be used as target pixel type.
# Possible pixel type tokens in pixel_type: RGB24,RGB32,Y8,YV12,YV16,YV24,YV411,YUY2
# Possible pixel type class tokens in pixel_type: RGB,YUV,444,PLANAR,INTERLEAVED
#
# Function Parameters
# val pixel_type Either a string with one or more target pixel type names and/or pixel type classes or a reference clip with the target pixel type
# str matrix ("") CCITT matrix name. If omitted: Rec601/Rec709 automatically chosen depending on clip size
# bool interlaced (false) Interlaced layout for YV12 conversions. See AviSynth doc 'ConvertToYV12()'
# str ChromaInPlacement ("") For converting from YV12. See AviSynth doc 'ConvertToYV12()'
# str chromaresample ("") Chroma resampling for conversions with different chroma subsampling. See AviSynth doc 'ConvertToYV12()'
# str ChromaOutPlacement ("") For converting to YV12. See AviSynth doc 'ConvertToYV12()'
# bool PC (false) If true, PC.601/PC.709 are used instead of Rec601/Rec709. The variant 601/709 is still automatically chosen
# bool stacked (false) See 'dither' package. If true, vertical size of stacked 16bit frame is assumed for Rec601/Rec709 decision
# int tunnel (0) if >0, for YUV<->RGB conversion, direct channel transfer Y<->G, U<->B, and V<->R channels
# Tunneling allows to use most RGB filters on YUV, or YUV filters on RGB footage with:
# Source(pixel_type="YVxx").Cnv2("RGB",tunnel=1).YourRGBFilters().Cnv2("YV24",tunnel=2)
# or Source(pixel_type="RGBxx").Cnv2("YV24",tunnel=1).YourYUVFilters().Cnv2("RGB",tunnel=2)
# NO color conversion is done. So it's faster and 100% lossless
# 1 tunnel entrance. If RGB requested and clip is YUV, or vice versa, frame is tunneled and global var gCnv2Tunnel is set.
# But if clip is already RGB (UR YUV, if YUV requested), there is no tunnel.
# 2 tunnel exit. Only if global var gCnv2Tunnel is !="", tunnel exit is performed, otherwise normal conversion.
# Tunneling is tolerant against input/output formats to the maximum possible extent.
#
# Key features
# Cnv2(reference_clip) is a big helper for StackHorizontal() and a lot of other filters. For comparison output,
# StackHorizontal(original,processed.Cnv2(original)) is all you need, *no matter what pixel_type of original is*
# Cnv2() allows more than one output pixel_type. E.g. if no YV24 renderer is available, but YV12 and RGB24
# renderers, Cnv2("YV12 RGB24") at the end of the filter chain will convert all YUV footage to YV24, and all RGB
# footage to RGB24.
# For universal scripting, Cnv2("...") lines with all possible pixel_types reduce conversions, yet prevent crashes
# Cnv2() automatically omits or inserts the 'matrix=' parameter in the resolved base conversions, depending on the
# fact that a YUV<->RGB conversion is performed or not.
# For HD footage, Rec709 coefficients are used automatically.
# Cnv2() does not affect framereate performance, because it performs some string calculations during filter graph
# setup, but resolves to typically one base conversion or no conversion at all.
# No-Conversion situations are detected
# Conversions that yield best output resolution and least color space transformations are preferred
#
# Caveats
# Because of the global variable, multi-instances scripts work only correctly, if only one color space,
# (i.e. RGB *OR* YUV) is used as a tunnel in one script. There will be interference if at different places of
# the script RGB and YUV are both used as tunnels.
#
# Needs GScript()
#
# Version 1.0, 02-14-2015
#
EDIT: 2015-02-16 attached version is 1.02 with again slightly improved tunnel processing and description