PDA

View Full Version : New script:Quantizer mask for MPEG sources


redfordxx
2nd December 2007, 00:49
Hi,
I made this script for my purposes so I decided to share it...maybe you find use of it.

The function MPEGQuants produces mask of quantizers. It needs input from dgdecode with enabled quants.

Usage example:
q=MPEG2Source("*.d2v",showQ=true)
q.MPEGQuants(0)
The parameter mode>-2 is described in the script you can play with it and will see..

Needed (best in plugins directory):
DGMask.avsi
mt_masktools
Patterns.avsi
dgdecodequantmask.ebmp

Enjoy...

redfordxx
2nd December 2007, 00:50
####################################
#
# MPEGQuants
#
# 2007 by Redford
#
# Produces quantizer mask for d2v files coming from dgdecode with option showQ=true.
# mode=-1 ...shows quants
# mode=0 ...outputs 16 reduced size clip with quant values --- each pixel for one block
# this output is intended to save to lossless format
# once saved, can be opened as a source to script for processing the original source
# mode>0 ...visualizes quantizers where mode=contrast
#
# needed: mt_masktools,
# Patterns.avsi (by me as well)
# dgdecodequantmask.ebmp (edit the path for it)
#
####################################

Function MPEGQuants(clip q, int mode) {

#Prepare mask
m=ImageSource("C:\Program Files\Multimedia\Video\AviSynth 2.5\plugins\dgdecodequantmask.ebmp",0,q.Framecount)
m= (int((q.height-0.1)/m.height+1) > 1) ? StackVertical(m,m) : m
m= (int((q.width-0.1)/m.width+1) > 1) ? StackHorizontal(m,m) : m
m= (int((q.height-0.1)/m.height+1) > 1) ? StackVertical(m,m) : m
m= (int((q.width-0.1)/m.width+1) > 1) ? StackHorizontal(m,m) : m
m=m.crop(0,0,q.width,q.height)

#Calculate values
xx=mt_lutxy(m,q,"y 130 > x 1 == x 1 - x ? 2 / 0 ?",y=3,u=-128,v=-128)
xx=xx.AddBorders(30,28,int(xx.width/64+1)*64+2-xx.width,int(xx.height/64+1)*64+4-xx.height,$000000)
xx=xx.mt_convolution("1 1 1 1 1", "1 1 1 1 1",total=1,y=3,u=-128,v=-128)
xx=xx.mt_convolution("1", "0 0 1",total=1,y=3,u=-128,v=-128)
xx=xx.pointresize(xx.width/2, xx.height/16)
p=PtrnVertical(xx,-128,255,0,0,255,0,255,0,0)
xx=mt_lutxy(xx,p,"y 255 == x 0 ?",y=3,u=-128,v=-128)
xx=xx.mt_convolution("0 1 1", "1",total=1,y=3,u=-128,v=-128)
xx=xx.pointresize(xx.width/2, xx.height)
xx=xx.mt_lut("x 30 == 0 x 4 == 1 x 23 == 2 x 7 == 3 x 13 == 4 x 11 == 5 x 27 == 6 x 6 == 7 x 31 == 8 x 15 == 9 0 ? ? ? ? ? ? ? ? ? ?",y=3,u=-128,v=-128)
xx=xx.mt_convolution("100 10 1", "1",total=1,y=3,u=-128,v=-128)
xx=xx.mt_convolution("0 0 1", "1",total=1,y=3,u=-128,v=-128)
xx=xx.pointresize(xx.width/4, xx.height)
xx=xx.crop(2,2,int((q.width+63.9)/64)*4,int((q.height+63.9)/64)*4)






####### Save #######
#xx


####### Visualization ###########
#xx=xx.mt_lut("x 18 == 255 0 ?",y=3,u=-128,v=-128)
#xx=xx.mt_lut("x 8 *",y=3,u=-128,v=-128)
#xx=xx.pointresize(xx.width*mode, xx.height*mode).crop(0,0,q.width,q.height)
#StackVertical(q,xx)
#xx
#q


####### Show quants #######
#mt_lutxy(m,q,"x 0 > y 130 - 4 * 0 ?",y=3,u=-128,v=-128)


####### Show mask #######
#m.mt_lut("x 0 > x 5 + 0 ?",y=3,u=-128,v=-128)
#mt_lut("x 16 *",y=3,u=-128,v=-128)


############################
####### All together #######
############################
(mode == -1) ? mt_lutxy(m,q,"x 0 > y 130 - 4 * 0 ?",y=3,u=-128,v=-128) : \
(mode == 0) ? xx : \
xx.mt_lut("x "+string(mode)+" *",y=3,u=-128,v=-128).pointresize(xx.width*16, xx.height*16).crop(0,0,q.width,q.height)

return last
}

redfordxx
2nd December 2007, 00:56
function Matrix (clip clp, int "uv")
{
uv = default(uv,3)

unit=blankclip(clp,width=4,height=4,pixel_type="YV12")
row=stackhorizontal( unit.mt_lut(y=0,u=2,v=2),
\ unit.mt_lut(y=-1,u=2,v=2),
\ unit.mt_lut(y=-2,u=2,v=2),
\ unit.mt_lut(y=-3,u=2,v=2),
\ unit.mt_lut(y=-4,u=2,v=2),
\ unit.mt_lut(y=-5,u=2,v=2),
\ unit.mt_lut(y=-6,u=2,v=2),
\ unit.mt_lut(y=-7,u=2,v=2))
square=stackvertical( row.mt_lut("x 0 +",y=3,u=2,v=2),
\ row.mt_lut("x 8 +",y=3,u=2,v=2),
\ row.mt_lut("x 16 +",y=3,u=2,v=2),
\ row.mt_lut("x 24 +",y=3,u=2,v=2),
\ row.mt_lut("x 32 +",y=3,u=2,v=2),
\ row.mt_lut("x 40 +",y=3,u=2,v=2),
\ row.mt_lut("x 48 +",y=3,u=2,v=2),
\ row.mt_lut("x 56 +",y=3,u=2,v=2))
block=square#.mt_lut("x 4 *",y=3,u=2,v=2)
block4=stackvertical(stackhorizontal(block,block),stackhorizontal(block,block)).pointresize(16,16)
block4 = (uv!=3) ? block4.mt_lut(y=3,u=uv,v=uv)
\ : YtoUV(block4.crop(0,0,8,8),block4.crop(0,0,8,8),block4)
return(block4.trim(1,1))
}




function PtrnFormat(clip clp, clip form)
{
fh=form.height
fw=form.width
clp=StackHorizontal(clp,clp,clp,clp,clp,clp,clp,clp,clp,clp,clp)
clp=StackVertical(clp,clp,clp,clp,clp,clp,clp,clp,clp)
clp=(fw > 160) ? StackHorizontal(clp,clp,clp,clp) : clp
clp=(fh > 128) ? StackVertical(clp,clp,clp,clp) : clp
clp=(fw > 640) ? StackHorizontal(clp,clp,clp) : clp
clp=(fh > 512) ? StackVertical(clp,clp,clp) : clp
clp=clp.crop(0,0,fw,fh)

return(clp.loop(framecount(form)))
}




function PtrnVertical(clip clp, int "uv", int "v0", int "v1", int "v2", int "v3", int "v4", int "v5", int "v6", int "v7")
{
v1 = default(v1,255-v0)
v2 = default(v2,v1)
v3 = default(v3,v2)
v4 = default(v4,v3)
v5 = default(v5,v2)
v6 = default(v6,v1)
v7 = default(v7,v0)

mtrx=Matrix(clp,uv)
ptrn=mt_lutxy(mtrx,mtrx,"x 8 % 0 == "+String(v0)+" "+String(v1)+" ?",y=3,u=uv,v=uv)
ptrn=mt_lutxy(mtrx,ptrn,"x 8 % 2 == "+String(v2)+" y ?",y=3,u=uv,v=uv)
ptrn=mt_lutxy(mtrx,ptrn,"x 8 % 3 == "+String(v3)+" y ?",y=3,u=uv,v=uv)
ptrn=mt_lutxy(mtrx,ptrn,"x 8 % 4 == "+String(v4)+" y ?",y=3,u=uv,v=uv)
ptrn=mt_lutxy(mtrx,ptrn,"x 8 % 5 == "+String(v5)+" y ?",y=3,u=uv,v=uv)
ptrn=mt_lutxy(mtrx,ptrn,"x 8 % 6 == "+String(v6)+" y ?",y=3,u=uv,v=uv)
ptrn=mt_lutxy(mtrx,ptrn,"x 8 % 7 == "+String(v7)+" y ?",y=3,u=uv,v=uv)

return(ptrn.PtrnFormat(clp))
}




function PtrnHorizontal(clip clp, int "uv", int "v0", int "v1", int "v2", int "v3", int "v4", int "v5", int "v6", int "v7")
{
v1 = default(v1,255-v0)
v2 = default(v2,v1)
v3 = default(v3,v2)
v4 = default(v4,v3)
v5 = default(v5,v2)
v6 = default(v6,v1)
v7 = default(v7,v0)

mtrx=Matrix(clp,uv)
ptrn=mt_lutxy(mtrx,mtrx,"x 4 - 8 / 8 % 0 == "+String(v0)+" "+String(v1)+" ?",y=3,u=uv,v=uv)
ptrn=mt_lutxy(mtrx,ptrn,"x 4 - 8 / 8 % 2 == "+String(v2)+" y ?",y=3,u=uv,v=uv)
ptrn=mt_lutxy(mtrx,ptrn,"x 4 - 8 / 8 % 3 == "+String(v3)+" y ?",y=3,u=uv,v=uv)
ptrn=mt_lutxy(mtrx,ptrn,"x 4 - 8 / 8 % 4 == "+String(v4)+" y ?",y=3,u=uv,v=uv)
ptrn=mt_lutxy(mtrx,ptrn,"x 4 - 8 / 8 % 5 == "+String(v5)+" y ?",y=3,u=uv,v=uv)
ptrn=mt_lutxy(mtrx,ptrn,"x 4 - 8 / 8 % 6 == "+String(v6)+" y ?",y=3,u=uv,v=uv)
ptrn=mt_lutxy(mtrx,ptrn,"x 4 - 8 / 8 % 7 == "+String(v7)+" y ?",y=3,u=uv,v=uv)

return(ptrn.PtrnFormat(clp))
}



function PtrnBlock(clip clp, int "uv", int "v0", int "v1", int "v2", int "v3")
{
v1 = default(v1,255-v0)
v2 = default(v2,v1)
v3 = default(v3,v2)

mtrx=Matrix(clp,uv)
ptrn=mt_lutxy(mtrx,mtrx,"x 4 - 8 / 8 % 1 >= x 4 - 8 / 8 % 6 <= & x 8 % 1 >= x 8 % 6 <= & & "+String(v1)+" "+String(v0)+" ?",y=3,u=uv,v=uv)
ptrn=mt_lutxy(mtrx,ptrn,"x 4 - 8 / 8 % 2 >= x 4 - 8 / 8 % 5 <= & x 8 % 2 >= x 8 % 5 <= & & "+String(v2)+" y ?",y=3,u=uv,v=uv)
ptrn=mt_lutxy(mtrx,ptrn,"x 4 - 8 / 8 % 3 >= x 4 - 8 / 8 % 4 <= & x 8 % 3 >= x 8 % 4 <= & & "+String(v3)+" y ?",y=3,u=uv,v=uv)

return(ptrn.PtrnFormat(clp))
}




function PtrnChess(clip clp, int "uv", int "v0", int "v1", int "v2", int "v3")
{
v1 = default(v1,255-v0)
v2 = default(v2,v0)
v3 = default(v3,v1)

mtrx=Matrix(clp,uv)
ptrn=mt_lutxy(mtrx,mtrx,"x 4 - 8 / 2 % 0 == x 2 % 0 == & "+String(v0)+" "+String(v1)+" ?",y=3,u=uv,v=uv)
ptrn=mt_lutxy(mtrx,ptrn,"x 4 - 8 / 2 % 1 == x 2 % 1 == & "+String(v2)+" y ?",y=3,u=uv,v=uv)
ptrn=mt_lutxy(mtrx,ptrn,"x 4 - 8 / 2 % 1 == x 2 % 0 == & "+String(v3)+" y ?",y=3,u=uv,v=uv)

return(ptrn.PtrnFormat(clp))
}




function PtrnSawH(clip clp, int "uv", string "shift", int "v0", int "v1", int "v2", int "v3", int "v4", int "v5", int "v6", int "v7")
{
v1 = default(v1,255-v0)
v2 = default(v2,v1)
v3 = default(v3,v2)
v4 = default(v4,v3)
v5 = default(v5,255-v4)
v6 = default(v6,v5)
v7 = default(v7,v6)

mtrx=Matrix(clp,uv)
ptrn=mt_lutxy(mtrx,mtrx,"x 4 - 8 / 8 % 0 == x 2 % "+shift+" 2 % == & x 4 + 8 / 8 % 0 == x 1 + 2 % "+shift+" 2 % == & | "+String(v0)+" "+String(v1)+" ?",y=3,u=uv,v=uv)
ptrn=mt_lutxy(mtrx,ptrn,"x 4 - 8 / 8 % 2 == x 2 % "+shift+" 2 % == & x 4 + 8 / 8 % 2 == x 1 + 2 % "+shift+" 2 % == & | "+String(v2)+" y ?",y=3,u=uv,v=uv)
ptrn=mt_lutxy(mtrx,ptrn,"x 4 - 8 / 8 % 3 == x 2 % "+shift+" 2 % == & x 4 + 8 / 8 % 3 == x 1 + 2 % "+shift+" 2 % == & | "+String(v3)+" y ?",y=3,u=uv,v=uv)
ptrn=mt_lutxy(mtrx,ptrn,"x 4 - 8 / 8 % 4 == x 2 % "+shift+" 2 % == & x 4 + 8 / 8 % 4 == x 1 + 2 % "+shift+" 2 % == & | "+String(v4)+" y ?",y=3,u=uv,v=uv)
ptrn=mt_lutxy(mtrx,ptrn,"x 4 - 8 / 8 % 5 == x 2 % "+shift+" 2 % == & x 4 + 8 / 8 % 5 == x 1 + 2 % "+shift+" 2 % == & | "+String(v5)+" y ?",y=3,u=uv,v=uv)
ptrn=mt_lutxy(mtrx,ptrn,"x 4 - 8 / 8 % 6 == x 2 % "+shift+" 2 % == & x 4 + 8 / 8 % 6 == x 1 + 2 % "+shift+" 2 % == & | "+String(v6)+" y ?",y=3,u=uv,v=uv)
ptrn=mt_lutxy(mtrx,ptrn,"x 4 - 8 / 8 % 7 == x 2 % "+shift+" 2 % == & x 4 + 8 / 8 % 7 == x 1 + 2 % "+shift+" 2 % == & | "+String(v7)+" y ?",y=3,u=uv,v=uv)

return(ptrn.PtrnFormat(clp))
}



function PtrnSawV(clip clp, int "uv", string "shift", int "v0", int "v1", int "v2", int "v3", int "v4", int "v5", int "v6", int "v7")
{
v1 = default(v1,255-v0)
v2 = default(v2,v1)
v3 = default(v3,v2)
v4 = default(v4,v3)
v5 = default(v5,255-v4)
v6 = default(v6,v5)
v7 = default(v7,v6)

mtrx=Matrix(clp,uv)
ptrn=mt_lutxy(mtrx,mtrx,"x 4 - 8 / 2 % "+shift+" 2 % == x 8 % 0 == & x 4 + 8 / 2 % "+shift+" 2 % == x 1 + 8 % 0 == & | "+String(v0)+" "+String(v1)+" ?",y=3,u=uv,v=uv)
ptrn=mt_lutxy(mtrx,ptrn,"x 4 - 8 / 2 % "+shift+" 2 % == x 8 % 2 == & x 4 + 8 / 2 % "+shift+" 2 % == x 1 + 8 % 2 == & | "+String(v2)+" y ?",y=3,u=uv,v=uv)
ptrn=mt_lutxy(mtrx,ptrn,"x 4 - 8 / 2 % "+shift+" 2 % == x 8 % 3 == & x 4 + 8 / 2 % "+shift+" 2 % == x 1 + 8 % 3 == & | "+String(v3)+" y ?",y=3,u=uv,v=uv)
ptrn=mt_lutxy(mtrx,ptrn,"x 4 - 8 / 2 % "+shift+" 2 % == x 8 % 4 == & x 4 + 8 / 2 % "+shift+" 2 % == x 1 + 8 % 4 == & | "+String(v4)+" y ?",y=3,u=uv,v=uv)
ptrn=mt_lutxy(mtrx,ptrn,"x 4 - 8 / 2 % "+shift+" 2 % == x 8 % 5 == & x 4 + 8 / 2 % "+shift+" 2 % == x 1 + 8 % 5 == & | "+String(v5)+" y ?",y=3,u=uv,v=uv)
ptrn=mt_lutxy(mtrx,ptrn,"x 4 - 8 / 2 % "+shift+" 2 % == x 8 % 6 == & x 4 + 8 / 2 % "+shift+" 2 % == x 1 + 8 % 6 == & | "+String(v6)+" y ?",y=3,u=uv,v=uv)
ptrn=mt_lutxy(mtrx,ptrn,"x 4 - 8 / 2 % "+shift+" 2 % == x 8 % 7 == & x 4 + 8 / 2 % "+shift+" 2 % == x 1 + 8 % 7 == & | "+String(v7)+" y ?",y=3,u=uv,v=uv)

return(ptrn.PtrnFormat(clp))
}


function QuantMask (clip "orig", int "quant", int "lowpass", int "highpass", int "expand",int "output")
{
lowpass= default(lowpass,0)
highpass= default(highpass,255)
expand= default(expand,0)
output= default(output,5)

qm0=Select(quant,orig,orig.dctfilter(1,1,1,1,1,1,1,0),orig.dctfilter(1,1,1,1,1,1,0,0),orig.dctfilter(1,1,1,1,1,0,0,0),orig.dctfilter(1,1,1,1,0,0,0,0),orig.dctfilter(1,1,1,0,0,0,0,0),orig.dctfilter(1,1,0,0,0,0,0,0),orig.dctfilter(1,0,0,0,0,0,0,0))
qm1=qm0.mt_lutxy(orig,"x y - abs "+String(lowpass)+" - "+String(highpass)+" "+String(lowpass)+" - / 256 *",y=3,u=3,v=3)
qm2=qm1.mt_convolution("0 0 0 0 0 0 0 1 1 1 1 1 1 1 1","0 0 0 0 0 0 0 1 1 1 1 1 1 1 1",y=3,u=3,v=3)
qm3=qm2.pointresize(qm2.width/8,qm2.height/8)
qm4=Select(expand,qm3,qm3.mt_expand(y=3,u=3,v=3))
qm5=qm4.pointresize(qm4.width*8,qm4.height*8)

test6=qm0.mt_lutxy(orig,"x y - 128 +",y=3,u=3,v=3)


return(Select(output,qm0,qm1,qm2,qm3,qm4,qm5,test6))
}






function QuantMaskInterval (clip "orig", int "quant1", int "quant2", int "lowpass", int "highpass", int "expand",int "output")
{
lowpass= default(lowpass,0)
highpass= default(highpass,255)
expand= default(expand,0)
output= default(output,5)

qm0a=Select(quant1,orig,orig.dctfilter(1,1,1,1,1,1,1,0),orig.dctfilter(1,1,1,1,1,1,0,0),orig.dctfilter(1,1,1,1,1,0,0,0),orig.dctfilter(1,1,1,1,0,0,0,0),orig.dctfilter(1,1,1,0,0,0,0,0),orig.dctfilter(1,1,0,0,0,0,0,0),orig.dctfilter(1,0,0,0,0,0,0,0))
qm0b=Select(quant2,orig,orig.dctfilter(1,1,1,1,1,1,1,0),orig.dctfilter(1,1,1,1,1,1,0,0),orig.dctfilter(1,1,1,1,1,0,0,0),orig.dctfilter(1,1,1,1,0,0,0,0),orig.dctfilter(1,1,1,0,0,0,0,0),orig.dctfilter(1,1,0,0,0,0,0,0),orig.dctfilter(1,0,0,0,0,0,0,0))
qm1=qm0a.mt_lutxy(qm0b,"x y - abs "+String(lowpass)+" - "+String(highpass)+" "+String(lowpass)+" - / 256 *",y=3,u=3,v=3)
qm2=qm1.mt_convolution("0 0 0 0 0 0 0 1 1 1 1 1 1 1 1","0 0 0 0 0 0 0 1 1 1 1 1 1 1 1",y=3,u=3,v=3)
qm3=qm2.pointresize(qm2.width/8,qm2.height/8)
qm4=Select(expand,qm3,qm3.mt_expand(y=3,u=3,v=3))
qm5=qm4.pointresize(qm4.width*8,qm4.height*8)

test6=qm0a.mt_lutxy(qm0b,"x y - "+String(lowpass)+" > x y - "+String(lowpass)+" - y x - "+String(lowpass)+" > x y - "+String(lowpass)+" + 0 ? ? 128 +",y=3,u=3,v=3)


return(Select(output,qm0a,qm1,qm2,qm3,qm4,qm5,test6))
}