Thread: U-V-Swap issue
View Single Post
Old 12th September 2018, 17:42   #12  |  Link
StainlessS
HeartlessS Usurer
 
StainlessS's Avatar
 
Join Date: Dec 2009
Location: Over the rainbow
Posts: 10,980
Modified to detect both bad range and last bad + 1 half bad frame, writing clipclop file to fix both
(assumes that detector works for complete clip, maybe does, maybe does not,
Who can tell the secret of the Black Magic Box.
Is the box of chocolates alive, or is the box of chocolates dead, you can only know when you open the box ).

Code:
#_FIX_UVSWAP_MOD.avs
AVISource("D:\UVSwap.mp4.AVI").ConvertToYV24
### Prep
Crop_L=10 CROP_T=36 CROP_R=-20 CROP_B=-4
x0=701 x1=702 x2=703 x3=704
MID = Crop(Crop_L,CROP_T,CROP_R,CROP_B) # Crop off all border crap
YC  = MID.ConvertToY8 # available if needed
UC  = MID.UToY8
VC  = MID.VToY8
### Config ###
GAP=0
Y0_TH=24.0  # AveLuma of RHS vertical column of pixels @ x0, ie border. [Border creeps in on damage]
U_TH =17.0  # 17.0, Percent population in range 128-GAP -> 128+GAP      [On damage, gets less saturated, more chroma at 128, less outside of 128]
V_TH =23.5  # 23.5, Percent population in range 128-GAP -> 128+GAP      [Ditto]
ClipClopName=  "ClipClopCmd.txt"
SHOW = True # True Show Metrics
FLAGS= True # True Show Frame Number and Flags (Also Shown if SHOW=False)
###########################
PREV_BINGO=False
PREV_START=-1
PREV_FRAME=-1
ClipClopName=(ClipClopName!="") ? RT_GetFullPathName(ClipClopName): ""
DORANGES=ClipClopName!=""
(ClipClopName!="") ? RT_FileDelete(ClipClopName)     : NOP
HILITE=RT_Ord("!")    # RT_Subtitle Hilite control code
NORM  =RT_Ord("-")    # RT_Subtitle Normal white text control code
LOLITE=RT_Ord("L")    # RT_Subtitle LoLite/DIM control code

SSS="""
    n=current_frame
    y0=RT_AverageLuma(x=x0,y=CROP_T,h=CROP_B,w=1)
    y1=RT_AverageLuma(x=x1,y=CROP_T,h=CROP_B,w=1)
    y2=RT_AverageLuma(x=x2,y=CROP_T,h=CROP_B,w=1)
    y3=RT_AverageLuma(x=x3,y=CROP_T,h=CROP_B,w=1)
    #
    Y=YC.RT_AverageLuma
    U=UC.RT_YInRange(lo=128-GAP,hi=128+GAP) * 100.0 # %
    V=VC.RT_YInRange(lo=128-GAP,hi=128+GAP) * 100.0 # %
    T1 = (Y0<Y0_TH)  T2 = (U>U_TH)  T3 = (V>V_TH)
    BINGO=(T1 && T2 && T3)
    T1_C=(T1)?HILITE:NORM   T2_C=(T2)?HILITE:NORM   T3_C=(T3)?HILITE:NORM   # Hilite control codes for Metrics
    F1_C=(T1)?NORM:LOLITE   F2_C=(T2)?NORM:LOLITE   F3_C=(T3)?NORM:LOLITE F_BINGO=(BINGO)?HILITE:LOLITE # Ctrl codes for FLAGS
    (FLAGS)?RT_Subtitle("%d] \a%cB\a%cU\a%cV \a%c*",n,F1_C,F2_C,F3_C,F_BINGO):NOP
    (SHOW)?RT_Subtitle("Y at RHS:  \a%cx0=%.2f\a- x1=%.2f x2=%.2f x3=%.2f",T1_C,y0,y1,y2,y3,align=5,Y=5*20):NOP
    (SHOW)?RT_Subtitle("YAve=%.2f : \a%cU=%.2f%%\a- : \a%cV=%.2f%%\a-",Y,T2_C,U,T3_C,V,align=5,Y=8*20):NOP
    (SHOW&&BINGO)?SUBTITLE("!!!BINGO!!!",Align=5,size=48):NOP
    if(DORANGES) {
        If(PREV_FRAME!=n-1) { DORANGES = False } # User jumped about so all bets are OFF
        Else {
            if(BINGO) {
                 if(!PREV_BINGO) { # New Start
                    PREV_START=n
                }
            } Else {
                 if(PREV_BINGO) {  # New End
                    RT_WriteFile(ClipClopName,"1 %d,%d",PREV_START,n-1,Append=True) # Write ClipClop Range to replace with clip index 1
                    if(T2 && T3) {  # Border Detect Fail
                        RT_WriteFile(ClipClopName,"2 %d",n,Append=True)             # replace End Bad + 1 frame with clip index 2 frame
                    }
                    PREV_START=-1
                 }
            }
            PREV_BINGO=BINGO
        }
        PREV_FRAME=n
    }
    Return last
"""
Scriptclip(SSS)
ClipClop repair script
Code:
# _FIX_UVSwap_MOD_ClipClop.avs
AVISource("D:\UVSwap.mp4.AVI")
ClipClopName=  "D:\ClipClopCmd.txt"
ORG=Last

CONT_U = 0  # ColorYUV U Sat adjust for bad range (0=no adjust)
CONT_V = 0  # ColorYUV V Sat adjust for bad range (0=no adjust)

Nxt=SelectEvery(1,1)              # Shift next frame to current
CN_Chroma = ORG.MergeChroma(Nxt)  # Copy Next Chroma into current, Fix End Bad + 1 half bad frame


V1 = SwapUV().ColorYUV(cont_u=CONT_U,cont_v=CONT_V)
V2 = CN_Chroma

NickNames ="""  # Psuedonyms for clips (clip index number)
    SwapUV    = 1 # Fixed Bad range clip (SwapUV with optional Chroma Sat adjustment)
    CN_Chroma = 2 # Fixed half bad end bad + 1 frame (Copy Chroma from next)
"""

SHOW=True

ClipClop(ORG,V1,V2,Cmd=ClipClopName,nickname=NickNames,show=SHOW)
StackVertical(Last,ORG)
Last bad range frame fixed by ClipClop, (Src on bottom)


Last bad range + 1 frame (half bad frame, copy chroma from next frame) fixed (src on bottom)



EDIT:
ClipClopCmd.txt to fix range Plus End Bad + 1 (Half bad) frame
Code:
1 21,33
2 34
__________________
I sometimes post sober.
StainlessS@MediaFire ::: AND/OR ::: StainlessS@SendSpace

"Some infinities are bigger than other infinities", but how many of them are infinitely bigger ???

Last edited by StainlessS; 12th September 2018 at 18:23.
StainlessS is offline   Reply With Quote