View Single Post
Old 2nd June 2006, 09:07   #17  |  Link
redfordxx
Registered User
 
Join Date: Jan 2005
Location: Praha (not that one in Texas)
Posts: 863
SmooothDeblock part two

Code:
function SmoothDeblock1(clip orig, int "passes", int "density", int "inclOrig", string "wieghtMX",string "dct_type",int "pregain",int "pregainoffset",int "gain",string "adaption_blursmall",string "adaption_blurbig")
{
passes=3#fixed for now
inclOrig=default(inclOrig,1)
density=default(density,2)
pregainoffset=default(pregainoffset,0)
pregain=default(pregain,10)
gain=default(gain,10)
b1=default(adaption_blursmall,"1")
b2=default(adaption_blurbig,"1 1 2 2 2 1 1")
wieghtMX=default(wieghtMX,"04center255border0smooth_1")
dct_type=default(dct_type,"DCTFilter")

mx2=orig.AddBorders(16,16,0,0).WeightMatrixClip(wieghtMX)
mxs=WeightMatrixSum(wieghtMX)





sdl8=orig.LanczosResize(orig.width/2,orig.height/2)
sdl8=((sdl8.Width % 32)==0) ? sdl8 : StackHorizontal(sdl8,sdl8.crop(sdl8.Width-32+(sdl8.Width % 32),0,-0,-0).FlipHorizontal())
sdl8=((sdl8.height % 32)==0) ? sdl8 : StackVertical(sdl8,sdl8.crop(0,sdl8.height-32+(sdl8.height % 32),-0,-0).FlipVertical())
sdl8=sdl8.SmoothDeblockLayer(5,density,mx2,mxs,"DCTFilter",DCTorig=1)
sdl8=sdl8.GaussResize(sdl8.width*2,sdl8.height*2,p=20).crop(0,0,orig.width,orig.height)
#sdl8=sdl8.mt_convolution("1 2 1","1 2 1",y=3,u=3,v=3)
sdl5=orig.SmoothDeblockLayer(5,density,mx2,mxs,"DCTFilter")
sdl3=orig.SmoothDeblockLayer(3,density,mx2,mxs,"DCTFilter")
qd8=orig.QuantDiff(6)
qd5=orig.QuantDiff(5)
qdo=orig.QuantDiff(inclOrig)
am8=qd8.AdaptionMask(pregain,pregainoffset,gain,b1,b2)
am5=qd5.AdaptionMask(pregain,pregainoffset,gain,b1,b2)
amo=qdo.AdaptionMask(pregain,pregainoffset,gain,b1,b2)

deblocked=mt_merge(sdl8,sdl5,am8,y=3,u=3,v=3) 
deblocked=mt_merge(deblocked,sdl3,am5,y=3,u=3,v=3) 
deblocked=(inclOrig==0) ? deblocked : mt_merge(deblocked,orig,amo,y=3,u=3,v=3) 
return(deblocked)
}



function SmoothDeblock2(clip orig, int "passes", int "density", int "inclOrig", string "wieghtMX",string "dct_type",int "pregain",int "pregainoffset",int "gain", int "thresh", string "adaption_blur")
{
passes=3#fixed for now
inclOrig=default(inclOrig,2)
density=default(density,2)
#pregainoffset=default(pregainoffset,-2)
#pregain=default(pregain,100)
#gain=default(gain,30)
#thresh=default(thresh,1)
#b2=default(adaption_blurbig,"1 1 1")
wieghtMX=default(wieghtMX,(density==4) ? "16center255border0smooth_1" : "04center255border0smooth_1")
dct_type=default(dct_type,"DCTFilter")

mx2=orig.AddBorders(16,16,0,0).WeightMatrixClip(wieghtMX)
mxs=WeightMatrixSum(wieghtMX)

sdl8=orig.LanczosResize(orig.width/2,orig.height/2)
sdl8=((sdl8.Width % 32)==0) ? sdl8 : StackHorizontal(sdl8,sdl8.crop(sdl8.Width-32+(sdl8.Width % 32),0,-0,-0).FlipHorizontal())
sdl8=((sdl8.height % 32)==0) ? sdl8 : StackVertical(sdl8,sdl8.crop(0,sdl8.height-32+(sdl8.height % 32),-0,-0).FlipVertical())
sdl8=sdl8.SmoothDeblockLayer(5,density,mx2,mxs,"DCTFilter",DCTorig=1)
sdl8=sdl8.GaussResize(sdl8.width*2,sdl8.height*2,p=20).crop(0,0,orig.width,orig.height)
#sdl8=sdl8.mt_convolution("1 2 1","1 2 1",y=3,u=3,v=3)
sdl5=orig.SmoothDeblockLayer(5,density,mx2,mxs,"DCTFilter")
sdl3=orig.SmoothDeblockLayer(3,density,mx2,mxs,"DCTFilter")
am8=orig.AdaptionMaskDiff(sdl8,6,pregain,pregainoffset,gain,thresh,adaption_blur,expands=5)
am5=orig.AdaptionMaskDiff(sdl5,5,pregain,pregainoffset,gain,thresh,adaption_blur,expands=2)
amo=orig.AdaptionMaskDiff(sdl3,inclOrig,pregain,pregainoffset,gain,thresh,adaption_blur,expands=1)

deblocked=sdl8
deblocked=mt_merge(deblocked,sdl5,am8,y=3,u=3,v=3) 
deblocked=mt_merge(deblocked,sdl3,am5,y=3,u=3,v=3) 
deblocked=(inclOrig==0) ? deblocked : mt_merge(deblocked,orig,amo,y=3,u=3,v=3) 
return(deblocked)
}



function SmoothDeblock3(clip orig, int "passes", int "density", int "inclOrig", string "wieghtMX",string "dct_type",int "pregain",int "pregainoffset",int "gain", int "thresh", string "adaption_blur", int "bth1", int "bth2", int "bshare", int "eth1", int "eth2", int "eshare", float "buvmod", float "euvmod")
{
passes=3#fixed for now
inclOrig=default(inclOrig,2)
density=default(density,2)
wieghtMX=default(wieghtMX,(density==4) ? "16center255border0smooth_1" : "04center255border0smooth_1")
dct_type=default(dct_type,"DCTFilter")

mx2=orig.AddBorders(16,16,0,0).WeightMatrixClip(wieghtMX)
mxs=WeightMatrixSum(wieghtMX)
bdm=orig.BorderMask(bth1, bth2, bshare, eth1, eth2, eshare)

sdl8=orig.LanczosResize(orig.width/2,orig.height/2)
sdl8=((sdl8.Width % 32)==0) ? sdl8 : StackHorizontal(sdl8,sdl8.crop(sdl8.Width-32+(sdl8.Width % 32),0,-0,-0).FlipHorizontal())
sdl8=((sdl8.height % 32)==0) ? sdl8 : StackVertical(sdl8,sdl8.crop(0,sdl8.height-32+(sdl8.height % 32),-0,-0).FlipVertical())
sdl8=sdl8.SmoothDeblockLayer(5,density,mx2,mxs,"DCTFilter",DCTorig=1)
sdl8=sdl8.GaussResize(sdl8.width*2,sdl8.height*2,p=20).crop(0,0,orig.width,orig.height)
#sdl8=sdl8.mt_convolution("1 2 1","1 2 1",y=3,u=3,v=3)
sdl5=orig.SmoothDeblockLayer(5,density,mx2,mxs,"DCTFilter")
sdl3=orig.SmoothDeblockLayer(3,density,mx2,mxs,"DCTFilter")
am8=orig.AdaptionMaskDiff(sdl8,6,pregain,pregainoffset,gain,thresh,adaption_blur,expands=5).mt_lutxy(bdm,"255 255 x - y * 255 / -",y=3,u=3,v=3)#   255-((255-x)*y)/255 = x+255-y-x*(255-y)/255
am5=orig.AdaptionMaskDiff(sdl5,5,pregain,pregainoffset,gain,thresh,adaption_blur,expands=2).mt_lutxy(bdm,"255 255 x - y * 255 / -",y=3,u=3,v=3)
amo=orig.AdaptionMaskDiff(sdl3,inclOrig,pregain,pregainoffset,gain,thresh,adaption_blur,expands=1).mt_lutxy(bdm,"255 255 x - y * 255 / -",y=3,u=3,v=3)

deblocked=sdl8
deblocked=mt_merge(deblocked,sdl5,am8,y=3,u=3,v=3) 
deblocked=mt_merge(deblocked,sdl3,am5,y=3,u=3,v=3) 
deblocked=(inclOrig==0) ? deblocked : mt_merge(deblocked,orig,amo,y=3,u=3,v=3) 
return(deblocked)
}



function SmoothDeblock4(clip orig, int "passes", int "density", int "inclOrig", string "wieghtMX",string "dct_type",int "pregain",int "pregainoffset",int "gain",string "adaption_blursmall",string "adaption_blurbig", int "bth1", int "bth2", int "bshare", int "eth1", int "eth2", int "eshare", float "buvmod", float "euvmod")
{
passes=3#fixed for now
inclOrig=default(inclOrig,1)
density=default(density,2)
pregainoffset=default(pregainoffset,0)
pregain=default(pregain,10)
gain=default(gain,10)
b1=default(adaption_blursmall,"1")
b2=default(adaption_blurbig,"1 1 2 2 2 1 1")
wieghtMX=default(wieghtMX,"04center255border0smooth_1")
dct_type=default(dct_type,"DCTFilter")

mx2=orig.AddBorders(16,16,0,0).WeightMatrixClip(wieghtMX)
mxs=WeightMatrixSum(wieghtMX)
bdm=orig.BorderMask(bth1, bth2, bshare, eth1, eth2, eshare)



sdl8=orig.LanczosResize(orig.width/2,orig.height/2)
sdl8=((sdl8.Width % 32)==0) ? sdl8 : StackHorizontal(sdl8,sdl8.crop(sdl8.Width-32+(sdl8.Width % 32),0,-0,-0).FlipHorizontal())
sdl8=((sdl8.height % 32)==0) ? sdl8 : StackVertical(sdl8,sdl8.crop(0,sdl8.height-32+(sdl8.height % 32),-0,-0).FlipVertical())
sdl8=sdl8.SmoothDeblockLayer(5,density,mx2,mxs,"DCTFilter",DCTorig=1)
sdl8=sdl8.GaussResize(sdl8.width*2,sdl8.height*2,p=20).crop(0,0,orig.width,orig.height)
#sdl8=sdl8.mt_convolution("1 2 1","1 2 1",y=3,u=3,v=3)
sdl5=orig.SmoothDeblockLayer(5,density,mx2,mxs,"DCTFilter")
sdl3=orig.SmoothDeblockLayer(3,density,mx2,mxs,"DCTFilter")
qd8=orig.QuantDiff(6)
qd5=orig.QuantDiff(5)
qdo=orig.QuantDiff(inclOrig)
am8=qd8.AdaptionMask(pregain,pregainoffset,gain,b1,b2).mt_lutxy(bdm,"255 255 x - y * 255 / -",y=3,u=3,v=3)
am5=qd5.AdaptionMask(pregain,pregainoffset,gain,b1,b2).mt_lutxy(bdm,"255 255 x - y * 255 / -",y=3,u=3,v=3)
amo=qdo.AdaptionMask(pregain,pregainoffset,gain,b1,b2).mt_lutxy(bdm,"255 255 x - y * 255 / -",y=3,u=3,v=3)

deblocked=mt_merge(sdl8,sdl5,am8,y=3,u=3,v=3) 
deblocked=mt_merge(deblocked,sdl3,am5,y=3,u=3,v=3) 
deblocked=(inclOrig==0) ? deblocked : mt_merge(deblocked,orig,amo,y=3,u=3,v=3) 
return(deblocked)
}
SmoothDeblock3 added - there is detail preservation and real border-edge preservation routine added.
How it works: it looks how strong are the edges of the block. If they are small, the deblocking is reduced (not necessary) if they are big it is also reduced (coz the edge should stay there).

Thresholds for blocking:
bth1=0 bth2=3 means that the deblocking will be eliminated, when the border difference is 0 and deblocking will be applied without change when the block border difference is > 3
bshare=overall impact of the correction 0=none 255=full. The (bth1;bth2) interval mapped onto mask scaled (0;255) is squeezed to (255-bshare;255) For example bshare=150 mean that when there is 0 block edge detected, the deblocking is reduced only to ((255-150)/255*100)%.
bshare=0 --> 100% deblocking

Thresholds for real edges are similar:
When the block edge difference is < eth1, no change.
When the block edge difference is > eth2, full deblocking reduction.
eshare=overall correction impact modifier.

The b-part is imo quite acceptable, but the e-part is prete bad. There are many edges missed and some real blocking is misjudged as edge.
And I think the real edges destruction is the worst part of this script.


IMO SmoothDeblock3 at default settings performs best.

Last edited by redfordxx; 3rd June 2006 at 12:02.
redfordxx is offline   Reply With Quote