Welcome to Doom9's Forum, THE in-place to be for everyone interested in DVD conversion.

Before you start posting please read the forum rules. By posting to this forum you agree to abide by the rules.

 

Go Back   Doom9's Forum > Capturing and Editing Video > Avisynth Development

Reply
 
Thread Tools Search this Thread Display Modes
Old 18th March 2017, 00:56   #1  |  Link
jconklin
Registered User
 
Join Date: Mar 2006
Posts: 35
amDCT "adaptive multifunction DCT", deblock, smooth, sharpen, range expansion filter.

amDCT() is an adaptive video filter providing deblocking, sharpening, local range expansion, smoothing, and bright noise removal in a single filter that can be used on video of any level of quality. These four operations work synergistically to drastically reduce block artifacts while maintaining detail and increasing local contrast.

A key advantage of amDCT--and one way in which it is a major advance over previous approaches--is that it determines how much smoothing, local range expansion and sharpening should be done at three different levels: the frame as a whole, block-by-block, and pixel-by-pixel.

Adaptivity was built into the core algorithm in order to provide a filter that could automatically adjust its strength to varying conditions in badly-encoded material so that good frames or good parts of bad frames would not be oversmoothed and bad frames or bad parts of good frames would be smoothed and deblocked more.


amDCT is built around a processing core modifed from my SmoothD2 filter that runs each block of a frame thru a DCT quant dequant inverse DCT process. Each frame is run through this process up to 64 times. Each time through the loop the frame is shifted up down left or right, and the resultant frame is then shifted back so that it is aligned with the original image. The values of each frame generated by this loop are added to each other and then averaged to produce a new frame.


The basic problem I found when using SmoothD2 which led me to start work on what became amDCT is that some frames needed more smoothing than others did.

So I decided I would find a way for the program to measure the amount of smoothing a frame needed and adjust the strength of smoothing accordingly.
After all, how hard could that be?
The gods laugh; the wife cries.
Well it turns out doing a first swipe at it is fairly easy. Just take an existing deblocker and measure the amount of change it makes in the frame and use that as a measure for the amount of smoothing you need.
The first rush of success. I've been through this before and I know I am hooked.
The problem is that you then start seeing that there are cases that the simple method of determining the global amount of smoothing doesn't work all of the time.
For example a blocky but almost black frame with a small area of light with a lot of detail. The deblocker tells you the frame is very blocky so you should strongly smooth the frame.
Unfortunately if you smooth it at a high level the area of detail gets smoothed out.

When you start getting a number of special cases like this you figure that a measure of the internal detail of each block might be useful.
It is useful. I am not an expert in this, so think of a hamster running in a wheel as I try all kinds of different measures and try to figure out which ones work best.

During that exercise I realize that I might have a measure of the amount of smoothing that I want for each block of the frame. So I figure out how to rebuild the DCT processing loop so that the amount of smoothing can be controlled on a block by block basis.
And it works. It took awhile to get it to work.
Hamster running really fast, wheel falls out of bearings, wheel and hamster fly out of cage and down the hall.
Acidentally discover that if you screw up the quant dequant code you can make the DCT processing loop do local range expansion instead of smoothing.

A NEW TOY!!! Spend a bunch of time messing around with the quant dequant code and figure out how to make it sharpen the image.
Two new problems. Range expansion intoduces halos on very strong black white borders. Sharppening introduces artifacts.
Sharpening artifacts can be reduced by presmoothing with a hand crafted matrix using shift=2.

Notice the most visible noise is in the brightest parts of the image and that the most oversmoothed parts are in dark areas.
Add controls that allow the user to specify at what brightness and at what strength dark areas should start being protected from being oversmoothed.
Add controls that allow the user to specify at what brightness and at what strength bright areas should start being more smoothed.
To add the extra smoothing at bright areas required building a very smoothed frame, shift=2, quant=6, qtype=2, which it turns out also makes an excellent smoothed frame for mvtools2() to use.
The very smoothed frame is also used to build a boundary mask to reduce bright halos that would be introduced by range expansion.

Hamster finds note taped on monitor
todo list not getting done
todo list will be glued to monitor.
Note understood.
amDCT version 1.0 is out the door.
Let me know what works, what doesn't, and what can be improved. My own improved list will easily take a year.
I think I just heard laughter.
Download code and manual https://sites.google.com/site/amdctfilter/
or just the manual https://docs.google.com/viewer?a=v&p...JiYWViNjEyMmIz
jconklin is offline   Reply With Quote
Old 18th March 2017, 09:51   #2  |  Link
ChaosKing
Registered User
 
Join Date: Dec 2005
Location: Germany
Posts: 494
Nice filter!

I get about 7.5fps on a 480p source i5 3570K@4.4Ghz with
Code:
amDCT(adapt=28, quant=9, expand=16, sharpWAmt=8, quality=4)
3fps more with quality=3.

I had to set expand to 30 to see a diffenrence at first (it's an anime source)

Can you also provide a 64Bit build?
__________________
Search and denoise
ChaosKing is offline   Reply With Quote
Old 18th March 2017, 10:36   #3  |  Link
Boulder
Pig on the wing
 
Boulder's Avatar
 
Join Date: Mar 2002
Location: Hollola, Finland
Posts: 4,455
Thanks a huge bunch! I've missed a good deblocker since I switched to VapourSynth, thus, I second ChaosKing's request for a 64-bit build SmoothD2 was the item in my toolbox for all DVD sources, and many Blu-rays would also benefit.
__________________
And if the band you're in starts playing different tunes
I'll see you on the dark side of the Moon...
Boulder is offline   Reply With Quote
Old 18th March 2017, 13:10   #4  |  Link
hydra3333
Registered User
 
Join Date: Oct 2009
Location: crow-land
Posts: 517
+1
thank you.

Quote:
Originally Posted by Boulder View Post
I've missed a good deblocker since I switched to VapourSynth
So have I.
DG has an NV based gpu accelerated deblock filter on on his to-do list ...
hydra3333 is offline   Reply With Quote
Old 18th March 2017, 23:38   #5  |  Link
jconklin
Registered User
 
Join Date: Mar 2006
Posts: 35
Thanks for trying amDCT so quickly.
3fps on 480p sounds about right. I have just started trying to get some gpu code "arrayfire.com" to work. There are 3 parts.
1. Get it to compile and link with my code.
2. replace the frame analysis code which should make it a bit faster.
3. Rebuild the whole multi threaded dct quant-dequant idct back end to take advantage of the gpu.

I have no idea how long #3 will take. I have never done gpu code.

Getting a 64 bit version out sounds easy. ... I think I just heard laughter.

What else do I need to do to build a 64-bit filter besides
Download and install 64bit avisynth. Do I need to uninstall the current avisynth?
Compile amDCT with visual studio 2015 with Configuration manager active solution platform set to CPU_x64
It can't be that easy.

I have a couple of priority items first and then
I will give it a try and see what happens.
jconklin is offline   Reply With Quote
Old 19th March 2017, 04:41   #6  |  Link
real.finder
Registered User
 
Join Date: Jan 2012
Location: Mesopotamia
Posts: 1,046
Quote:
Originally Posted by jconklin View Post
Thanks for trying amDCT so quickly.
3fps on 480p sounds about right. I have just started trying to get some gpu code "arrayfire.com" to work. There are 3 parts.
1. Get it to compile and link with my code.
2. replace the frame analysis code which should make it a bit faster.
3. Rebuild the whole multi threaded dct quant-dequant idct back end to take advantage of the gpu.

I have no idea how long #3 will take. I have never done gpu code.

Getting a 64 bit version out sounds easy. ... I think I just heard laughter.

What else do I need to do to build a 64-bit filter besides
Download and install 64bit avisynth. Do I need to uninstall the current avisynth?
Compile amDCT with visual studio 2015 with Configuration manager active solution platform set to CPU_x64
It can't be that easy.

I have a couple of priority items first and then
I will give it a try and see what happens.
you don't need avs64 nor avs+ 64 for that, you just need avisynth.h

if you still use avs25 I put the avisynth.h that work with it in Attachments

but it's better to use the one from avs+ and support new avs+ colour format

and you don't need install avs 64 for test too, you can use mp_pipeline to load avisynth .dll inside it block

Code:
LoadPlugin("E:\mpp\MP_Pipeline.dll") #32 one
MP_Pipeline("""
### platform: win64
### dll: E:\avs64\AviSynth.dll
LoadPlugin("E:\amDCT.dll") #64one
ColorBars.converttoyv12
your filter here
### ###
""")
run that in any normal 32 avs editor/player
Attached Files
File Type: rar avisynth.rar (8.2 KB, 36 views)
__________________
My Avisynth Stuff

Last edited by real.finder; 19th March 2017 at 04:45.
real.finder is offline   Reply With Quote
Old 19th March 2017, 11:14   #7  |  Link
Boulder
Pig on the wing
 
Boulder's Avatar
 
Join Date: Mar 2002
Location: Hollola, Finland
Posts: 4,455
For the 64-bit VapourSynth to be able to use Avisynth plugins, you need to compile with Avisynth 2.6 headers. I think that's recommended anyway because Avisynth has been officially at v2.6 for some time now.

I requested a port last year in this thread: https://forum.doom9.org/showthread.php?t=173866 . As you can see, Myrsloik was not very optimistic
__________________
And if the band you're in starts playing different tunes
I'll see you on the dark side of the Moon...
Boulder is offline   Reply With Quote
Old 19th March 2017, 11:47   #8  |  Link
Groucho2004
 
Join Date: Mar 2006
Posts: 3,929
Quote:
Originally Posted by jconklin View Post
Getting a 64 bit version out sounds easy. ... I think I just heard laughter.

What else do I need to do to build a 64-bit filter besides
Download and install 64bit avisynth. Do I need to uninstall the current avisynth?
Compile amDCT with visual studio 2015 with Configuration manager active solution platform set to CPU_x64
It can't be that easy.
It's not easy. I had a quick glance at your code, there's a lot of inline asm which would have to be converted to SIMD intrinsics to maintain reasonable performance. Compared to that, fixing other (potential) x64 porting issues is simple.
Installing AVS+ x64 alongside AVS(+) x86 is also no problem.

Last edited by Groucho2004; 19th March 2017 at 12:04.
Groucho2004 is offline   Reply With Quote
Old 4th April 2017, 18:44   #9  |  Link
jconklin
Registered User
 
Join Date: Mar 2006
Posts: 35
Thanks Groucho2004 for looking at my code. It never crossed my mind that the Microsoft linker could not link a 32 bit dll to a 64 bit program.

I will end up building a 64 bit version of amDCT so that I can use one of the open source image processing libraries which tend to be 64 bit. This should also improve the speed of the DCT loop. To get a real speedup the DCT loop will need to run on the GPU, something I have no experience with.

The image processing libraries will allow me to quickly test some ideas I have for improving the block level adaptivity.
jconklin is offline   Reply With Quote
Old 4th April 2017, 19:30   #10  |  Link
TheFluff
Excessively jovial fellow
 
Join Date: Jun 2004
Location: rude
Posts: 1,050
To clarify: inline asm doesn't compile in x64 mode. At all. You must either port all of it to intrinsics (do this) or switch to a separate assembler like YASM (don't do this). Good luck.
TheFluff is offline   Reply With Quote
Old 4th April 2017, 20:29   #11  |  Link
Myrsloik
Professional Code Monkey
 
Myrsloik's Avatar
 
Join Date: Jun 2003
Location: Ikea Chair
Posts: 1,876
Quote:
Originally Posted by TheFluff View Post
To clarify: inline asm doesn't compile in x64 mode. At all. You must either port all of it to intrinsics (do this) or switch to a separate assembler like YASM (don't do this). Good luck.
You forgot option #3. GO FULL DOOM9 STYLE!

Intel compiler is best compiler (assuming you manage to replace all register names)
__________________
VapourSynth - proving that scripting languages and video processing isn't dead yet
Myrsloik is offline   Reply With Quote
Old 5th April 2017, 08:58   #12  |  Link
jpsdr
Registered User
 
Join Date: Oct 2002
Location: France
Posts: 1,651
It's not necessary to switch to another assembler, you can still put all your inline asm in external asm file, and still continue to use the assembler provided by Visual Studio. This will avoid the need of other who want to compile your code to have to install third party things, you'll just only need Visual Studio, nothing more.
jpsdr is offline   Reply With Quote
Old 13th April 2017, 01:39   #13  |  Link
jconklin
Registered User
 
Join Date: Mar 2006
Posts: 35
It looks like it should be fairly easy to take all of the inline asm and put it in a separate .asm file and have nasm process it. This is how the XVID dct idct routines are done. They %include "nasm.inc" which contains the following lines.

%ifdef ARCH_IS_X86_64
BITS 64
DEFAULT REL
%define SECTION_ALIGN 32

...

%else
%define SECTION_ALIGN 16
BITS 32

I hope this means that the .asm routines are aready setup to build and link to both 32 and 64 bit code depending on the visual studio settings.

I have several projects that I have to get done before I move the inline assembler to its own file.

In the meantime I uploaded a VERY basic architecture doc "amDCT Software Architecture Picture.pdf" at https://sites.google.com/site/amdctfilter/
jconklin is offline   Reply With Quote
Old 13th April 2017, 18:35   #14  |  Link
CkJ
Registered User
 
Join Date: Jul 2014
Posts: 29
Original


amdct(quant=16, matrix=8)


Is this filter require mod8? Any chance it support for mod4 too? Thank you.

Last edited by CkJ; 13th April 2017 at 18:38.
CkJ is offline   Reply With Quote
Old 14th April 2017, 01:09   #15  |  Link
jconklin
Registered User
 
Join Date: Mar 2006
Posts: 35
CKJ Both the height and width of the input video needs to be evenly divisible by 8. Anytime I have delt with non mod8 clips I did a AddBorders(val, val, 0, 0).amDCT().Crop(val, val, 0, 0)

I really like the images. amDCT() will sometimes enhance the detail if expand is used. In this case it might bring out some of the details of the patterns in the water. If I think expand will help I usually start with expand=15.
jconklin is offline   Reply With Quote
Old 14th April 2017, 13:30   #16  |  Link
CkJ
Registered User
 
Join Date: Jul 2014
Posts: 29
Look like the expand parameter darkens black areas. I don't think it can restores the details lost. I played with some amdct parameters. This is what i can deal with a heavy blocky image by using amdct.

Code:
mt_merge(last, last.amdct(shift=0, quant=4, qtype=2), last.histogram("luma").mt_deflate.greyscale.mt_invert)
CkJ is offline   Reply With Quote
Old 15th April 2017, 02:34   #17  |  Link
jconklin
Registered User
 
Join Date: Mar 2006
Posts: 35
CKJ using shift=0 does almost no deblocking and in some cases will make blocking worse. Try shift=3. It will be slower but the deblocking should be better.
jconklin is offline   Reply With Quote
Old 15th April 2017, 10:56   #18  |  Link
CkJ
Registered User
 
Join Date: Jul 2014
Posts: 29
I have found out that amdct work better with supersampling. My final result with the max value of shift:
Code:
filtered = spline36resize(last.width*4, last.height*4).amdct(adapt=31, shift=5, quant=8, matrix=8, qtype=2).spline36resize(last.width, last.height)
mt_merge(last, filtered, last.histogram("luma").mt_deflate.blur(.4).mt_inpand.greyscale.mt_invert)
CkJ is offline   Reply With Quote
Old 18th April 2017, 04:48   #19  |  Link
jconklin
Registered User
 
Join Date: Mar 2006
Posts: 35
CKJ I took a copy of your image marked original, and ran it thru

amDCT(quality=4, adapt=31, quant=9, matrix=8, shift=5, qtype=1, expand=16, sharpWPos=6, sharpWAmt=24, sharpTPos=7, sharpTAmt=31, darkStart=64, darkAmt=10, brightStart=205, brightAmt=30)


The top left is the original. Top right is output from amDCT
The bottom left is the makediff of the original and the output of amDCT()
The bottom right is the makediff output expanded so the diff is more obvious.

Note that doing a lot of sharpening might cause flickering of the highlights between frames. One of the things that I would like to do is to pass a number of motion compensated frames to amDCT and use them for higher quality processing including flicker reduction. At one point I built a prototype out of smoothD2. It showed some promise but its speed was several seconds per frame. It is on my upgrade list after getting the main DCT processing loop to run on a gpu.

What the arguments are doing to the image.
Note that the image I am starting with will have gone thru a compression to .png and a decompression back to a frame for AviSynth. This process will have lost some detail in the image and might have introduced some more and different blocking than the image you are working with. Also the image you are seeing has gone thru another encoding decoding process.

My process in setting the values for amDCT for your image.
adapt=31
Note: Improvements needed frame size HD vs 480. interlaced converted to progressive often has smoothing done along the top and bottom edges of the blocks in a vertical column. This is not accounted for in the adaptation calculation. Among other things that the adaptation calculation is used for is to determine the strength of the block edge to block edge deblocking. If you really want to see the problems at the pixel and block edge level then in VirtualDub display your output setting zoom to 200%, 1to1 pixel, and point resize, and calibrate you monitors gray scale. Thats what I use. I don't recommend it. You will never be satisfied with any of your filters!!!

quant=9 Make as large as necessary to remove noise and blockiness.

and now I don't have any detail left.

darkStart=64, darkAmt=10 Find dark details that have been smoothed out. Use colors.exe available at http://www.den4b.com/ to find the values of the dark details that have disappeared. Set darkStart larger then that value and darkAmt large enough to bring back some of the blacks.

expand=16 get most of the details back. Possibly go back and increase quant.

brightStart=205, brightAmt=30 Notice that expand has made some of the white highlights too bright. Use colors.exe program to see what the original values of the over brightened highlights are. Set brightStart lower than this value and the values above brightStart will be compressed toward brightStart by an amount controlled by brightAmt. This also reduces the blocking artifacts that show up as short thin bright lines.

sharpWPos=6, sharpWAmt=24, sharpTPos=7, sharpTAmt=31 start dialing in fine detail. Go back to quant and check the values.

Last edited by jconklin; 22nd April 2017 at 16:14.
jconklin is offline   Reply With Quote
Old 19th April 2017, 20:02   #20  |  Link
CkJ
Registered User
 
Join Date: Jul 2014
Posts: 29
Never heard about colors.exe program. Where do I can download it?
CkJ is offline   Reply With Quote
Reply

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 15:21.


Powered by vBulletin® Version 3.8.11
Copyright ©2000 - 2018, vBulletin Solutions Inc.