ConditionalResize

ConditionalResize ist eine von Gebbi geschriebene Funktion (Idee kam aber von mir!), welche automatisch erkennt, ob hier eine überschärfte Form von Bicubic oder eine normale vorliegt. Auch, ob es jeweils Überschärftes Bicubic oder Bilinear ist. Sie basiert mehr oder weniger auf dem Prinzip von Autodeblock. In Anime treten oftmals mehrere Upscales gleichzeitig auf (je nach Szene wird dann eins von beiden gewählt). Dies hier stellt eine Alternative zum Szenenfiltern dar.

Wie funktioniert das ganze?

Per Masking wird bei jedem Frame geschaut, ob Überschärfe auftritt. Tritt diese auf, wird diese per OS-Debicubic gefixt. Klingt simpel, ist es auch.

Wie sieht denn das Skript aus?

##################################################################################################################
#
# ConditionalResize r1
# Credits - Directed by: Gebbi - Wanted to be mentioned as well: eXmendiC <3
#
# What?
# - This function can be used to apply the right downscaling method for each frame
# for sources with mixed upscaling methods (one being "oversharped Bicubic" (Bicubic with b=0, c=1)
# in particular).
# - To be used with cartoons.
#
# Requirements:
# - Debilinear
# - Debicubic
# - xaa & nnedi3 (optional for mode=1)
#
# Parameters:
# - clip_a / clip_b:
# Custom clips to be used for output instead of Debilinear/Debicubic
# - mode:
# 0 Direct luma comparison (Default & recommended)
# 1 xaa(nnedi3)-based luma comparison
# - rmode:
# 0 Bilinear upscale (for non-oversharped-bicubic frames) (Default)
# 1 Bicubic upscale (for non-oversharped-bicubic frames)
# - w / h:
# Width/height of assumed production resolution. Default is 1280x720.
# - thr:
# Increasing/decreasing the offset may result in better/worse detection of oversharped-bicubic frames.
# Default is 0.03.
# - xaa_mthr (only for mode=1):
# mthr value to be passed to xaa.
# Increasing/decreasing this value may result in better/worse detection of oversharped-bicubic frames.
# Default is 16.
# - debug:
# Setting it to true will show detected upscaling method in the top left corner.
# - analysis:
# Setting it to true will write oversharped-bicubic frames into analysis_file.
# You can use this file as a mappings file for ReplaceFramesSimple afterwards.
#
# Usage example:
# bilinear = GradFun3mod(resizer = "Debilinear")
# os_bicubic = GradFun3mod(resizer = "Debicubic", b=0, c=1)
# ConditionalResize(bilinear, os_bicubic)
#
##################################################################################################################

Function ConditionalResize(clip src, clip clip_a, clip clip_b, int "mode", int "rmode", int "w", int "h", float "thr", int "xaa_mthr", bool "debug", bool "analysis", string "analysis_file") {
 mode = Default(mode, 0)
 rmode = Default(rmode, 0)
 global thr = Default(thr, 0.03)
 xaa_mthr = Default(xaa_mthr, 16)
 debug = Default(debug, false)
 analysis = Default(analysis, false)
 analysis_file = Default(analysis_file, "analysis.txt")
 w = Default(w, 1280)
 h = Default(h, 720)
 clip_a = Default(clip_a, (rmode == 0) ? src.Debilinear(w,h) : src.Debicubic(w,h))
 clip_b = Default(clip_b, src.Debicubic(w,h,b=0,c=1))
 
 Assert(Width(src) == 1920 && Height(src) == 1080, "ConditionalResize: Source clip must be 1920x1080. You can add Crop via clip_a/clip_b.")
 
 global src = src
 
 global src_d1 = (mode == 0 && rmode == 0) ? src.Debilinear(w,h).BilinearResize(1920,1080)
\ : (mode == 0 && rmode == 1) ? src.Debicubic(w,h).BicubicResize(1920,1080)
\ : (mode == 1) ? src.Invert("Y")
\ : Assert(false, "ConditionalResize: mode and rmode must be 0 or 1.")

 global src_d2 = (mode == 0) ? src.Debicubic(w,h,b=0,c=1).BicubicResize(1920,1080,b=0,c=1)
\ : (mode == 1) ? src.xaa(mode="nnedi3",mthr=xaa_mthr).Invert("Y")
\ : Assert(false, "ConditionalResize: mode and rmode must be 0 or 1.")

 debug_ol1 = (rmode == 0) ? "Bilinear" : "Bicubic"
 
 clip_a = debug ? clip_a.Subtitle(debug_ol1) : clip_a
 clip_b = debug ? clip_b.Subtitle("OS Bicubic") : clip_b
 
 clip_b = analysis ? clip_b.WriteFile(analysis_file, "current_frame") : clip_b
 
 result = (mode == 0) ? ConditionalFilter(src, clip_b , clip_a, "LumaDifference(src,src_d1) - thr > LumaDifference(src,src_d2)", "==", "True")
\ : (mode == 1) ? ConditionalFilter(src, clip_b , clip_a, "(LumaDifference(src_d1,src_d2) > thr)", "==", "True")
\ : Assert(false, "ConditionalResize: mode must be 0 or 1.")

 return result
}

Für Vapoursynth Port HIER klicken.

 

Beispiele für die Nutzung?

#Beispiel mit 8-bit
bilinear = Debilinear(1280,720)
os_bicubic = Debicubic(1280,720,b=0,c=1)
ConditionalResize(bilinear,os_bicubic)
#Beispiel mit gradfun3mod
bilinear = gradfun3mod(resizer="DebilinearM")
os_bicubic = gradfun3mod(resizer="DebicubicM",b=0,c=1)
ConditionalResize(bilinear,os_bicubic)
#Beispiel mit Luma/Chroma/Chroma Nutzung
lu = utoy().Spline36Resize(640,360)
lv = vtoy().Spline36Resize(640,360)
ly = Debilinear(1280,720)
ly2 = Debicubic(1280,720,b=0,c=1)
luma=ConditionalResize(ly,ly2,rmode=1)
ytouv(lu,lv,luma)
#Beispiel mit 16-bit
Dither_convert_8_to_16()
lu = utoy().dither_resize16(1280,720,kernel="blackmanminlobe",src_left=0.25,u=1,v=1)
lv = vtoy().dither_resize16(1280,720,kernel="blackmanminlobe",src_left=0.25,u=1,v=1)
ly = DebicubicM_precise16(1280,720, chroma=false)
ly2 = DebicubicM_precise16(1280,720,b=0,c=1, chroma=false)
luma=ConditionalResize(source,ly,ly2,rmode=1)
ytouv(lu,lv,luma)
#Beispiel mit 16-bit und SMDegrain
src16 = Dither_convert_8_to_16()
source=LWLibavVideoSource("C:\Users\Misaki\Desktop\Ilya\sp01.m2ts")
src16.utoy().SMDegrain(plane=0,tr=2,blksize=16,pel=1,subpixel=3,prefilter=2,thSAD=50,thSADC=0,lsb=true,lsb_in=true,lsb_out=true)
lu = last.dither_resize16(1600,900,kernel="blackmanminlobe",src_left=0.25,u=1,v=1)
src16.vtoy().SMDegrain(plane=0,tr=2,blksize=16,pel=1,subpixel=3,prefilter=2,thSAD=50,thSADC=0,lsb=true,lsb_in=true,lsb_out=true)
lv = last.dither_resize16(1600,900,kernel="blackmanminlobe",src_left=0.25,u=1,v=1)
src16.SMDegrain(plane=0,tr=2,blksize=16,pel=1,subpixel=3,prefilter=2,thSAD=125,thSADC=0,lsb=true,lsb_in=true,lsb_out=true)
ly = last.DebicubicM_precise16(1600,900, chroma=false)
src16.SMDegrain(plane=0,tr=2,blksize=16,pel=1,subpixel=3,prefilter=2,thSAD=125,thSADC=0,lsb=true,lsb_in=true,lsb_out=true)
ly2 = last.DebicubicM_precise16(1600,900,b=0,c=1, chroma=false)
luma=ConditionalResize(source,ly,ly2,rmode=1)
ytouv(lu,lv,luma)