View Single Post
Old 27th March 2011, 19:43   #13  |  Link
Dogway
Registered User
 
Join Date: Nov 2009
Posts: 2,361
I read that page before but thanks to your guidance I think I got it working.
The only pitfall here is in case the logo is animated, and the sample frame to take the alpha from (Default FadeIn(I)) has alpha set to 100% opacity. Its quite unlikely, but Im done in this regard. Maybe only that ConditionalFilter feels a bit unstable, like in the same boat as layer, or overlay...

The only last part I want to fix is making chroma work in blending modes.
The function where I took the blend mode from is Blend_MT_alpha3, but even in that function, chroma seems broken.

Code:
FUNCTION Logo(clip clip,string path, int "x", int "y",int "start",int "end", float "Opac",int "I",int "O",float "blur",string "mode", bool "anim_gif")
{
path=string(path)
x=default(x, 0)
y=default(y, 0)
start=default(start, 0)
end=default(end, 100)
I=default(I, 20)
O=default(O, I)
Opac=default(Opac, 1.0)
Sblur=default(blur, 0.0)
gif=default(anim_gif,false)
mode=default(mode,"Screen")


logo0 = gif == false ? CoronaSequence(path,start=start,stop=end-1).mmod2.FadeIn0(I).FadeOut0(O)
\  : ImmaRead(path,start=start,end=end-1,animation=true).mmod2.FadeIn0(I).FadeOut0(O)
logo0a=logo0.addborders(x,y,clip.width,clip.height).crop(0,0,-logo0.width-x,-logo0.height-y).blur(Sblur)
logo1 =logo0a.converttoyv12().assumefps(Framerate(Clip))

video1=ConditionalFilter(ShowAlpha(logo0,"YV12").trim(I+1,I),
\   mt_lutxy(clip.trim(start,end-1),logo1,Y=3, U=2, V=2,
\ (mode=="Screen")    ? "236 16 - 236 16 - x 16 - - 236 16 - y 16 - - * 236 16 - / - 16 +  "+String(Opac)+" * x 16 - 1 "+String(Opac)+" - * + "
\:(mode=="Multiply") ? "x 16 - y 16 - * 236 16 - / 16 + "+String(Opac)+" * x 16 - 1 "+String(Opac)+" - * + " : Assert(false, "Unsupported Blending Mode")
\), mt_merge(clip.trim(start,end-1),logo1,mt_lut(ShowAlpha(logo0a,"YV12")," x "+String(Opac)+" * "),luma=true),"AverageLuma()","==","255",false)

return start >0 ? clip.trim(0,start == 1 ? -1 : start-1)+video1+clip.trim(end,0) : video1+clip.trim(end,0)}
Edit: I reworked the function completly, I fixed some things, and added option for Over blending mode. Syntax is awful with my limited knowledge... so Im sure the code can be optimized far far better. I really just would like to fix the chroma blending, I tried everything but my guess is it has to do with the polish notation formula.
Edit2:I read a paper. Im going to try to compute the effect in RGB and add the difference to my YUV clip. Maybe there's less degradation in this way... I might also fix something I saw when I got time.
Edit3: I didn't come to think masktools is only for YV12, so I guess chroma blending stays unfunctional, unless I find a way to compute blend modes in RGB. Also I updated/fixed/optimized the code for the new workaround, now this is a working release. Enjoy it.

(Old)
Code:
FUNCTION Logo(clip clip,string path, int "x", int "y",int "start",int "end", float "Opac",int "I",int "O",float "blur",string "mode",bool "matte",bool "anim_gif")
{
path=string(path)
x=default(x, 0)
y=default(y, 0)
start=default(start, 0)
end=default(end, 100)
I=default(I, 20)
O=default(O, I)
Opac=default(Opac, 1.0)
Sblur=default(blur, 0.0)
gif=default(anim_gif,false)
mode=default(mode,"Over")
matte=default(matte,false)


logo0 = gif == false ? CoronaSequence(path,start=start,stop=end-1).mmod2.FadeIn0(I).FadeOut0(O)
\                    : ImmaRead(path,start=start,end=end-1,animation=true).mmod2.FadeIn0(I).FadeOut0(O)
logo0a=logo0.addborders(x,y,clip.width,clip.height).crop(0,0,-logo0.width-x,-logo0.height-y).blur(Sblur)
logo1 =logo0a.converttoyv12().assumefps(Framerate(Clip))

msk = matte == false ? ShowAlpha(logo0a,"YV12")
\                    : ShowAlpha(logo0a,"YV12").lanczosresize(logo0a.width*3,logo0a.height*3).mt_inpand(mode="both").LanczosResize(logo0a.width,logo0a.height)
org=clip.trim(start,end-1)
screen="236 16 - 236 16 - x 16 - - 236 16 - y 16 - - * 236 16 - / - "+String(Opac)+" * x 16 - 1 "+String(Opac)+" - * + 16 + "
multiply=" x 16 - y 16 - * 236 / "+String(Opac)+" * x 16 - 1 "+String(Opac)+" - * + 16 + "

video1 =
\ (mode=="Screen")   ? mt_merge(org,mt_lutxy(org,logo1,Y=3, U=2, V=2,screen)  ,msk,luma=true)
\:(mode=="Multiply") ? mt_merge(org,mt_lutxy(org,logo1,Y=3, U=2, V=2,multiply),msk,luma=true)
\:(mode=="Over")     ? mt_merge(org,logo1,mt_lut(msk," x "+String(Opac)+" * "),luma=true)
\:Assert(false, "Unsupported Blending Mode")

return start >0 ? clip.trim(0,start == 1 ? -1 : start-1)+video1+clip.trim(end,0) : video1+clip.trim(end,0)}

function mmod2(clip c){
bh = 2 - ((c.Width()-1)%2 + 1)
bv = 2 - ((c.Height()-1)%2 + 1)
l = bh/2
l = c.IsYUV() && l%2 != 0 ? l-1 : l
r = bh - l
t = bv/2
t = c.IsYV12() && t%2 != 0 ? t-1 : t
b = bv - t
c.crop(l,t,-r,-b)}
(Updated)
Logo9.0.avsi
__________________
i7-4790K@Stock::GTX 1070] AviSynth+ filters and mods on GitHub + Discussion thread

Last edited by Dogway; 24th May 2011 at 17:57. Reason: update to 9.0 link
Dogway is offline   Reply With Quote