HDR -> SDR conversion

These CUDA filters are packaged into DGDecodeNV, which is part of DGDecNV.
User avatar
admin
Posts: 4551
Joined: Thu Sep 09, 2010 3:08 pm

Re: HDR -> SDR tonemapping

Post by admin »

Yes, probably a limit. I'll do some tests and see if I can mitgate it.

Thanks for the test images. Great selection of light and dark scenes. I'll digest these before asking for more. I really appreciate the work y'all put in on testing.

Things I am doing:

1. Disengage equalization in flat areas. I'll calculate the variance in the window and if it is below a certain amount then don't do equalization. Or fade it out based on variance. This should avoid banding and noise amplification with equal > 0.

2. Look at DJ's issue with the flowers. I'm still not quite sure what the problem is but DJ is sending me samples. There could be an issue with the 2020->709 gamut mapping for some colors but it looks good to me. We'll see.

3. Port equalization to CUDA. I'm looking at a sliding window version of equalization because it is easier to port to CUDA. The current approach has fixed LUTs per window and interpolates neighboring LUTs to avoid blocking.

4. The Reinhard mode can be improved. Also Hable can be added.

5. Vapoursynth native version.

Here's a little secret. In the DLL is a filter called WinHisto(strength, xwindow, ywindow) that is a port of my WinHisto VirtualDub filter. Look here for usage (maximum intensity option is removed):

http://rationalqm.us/winhistogram.html

Be careful, though, it currently does not like to receive a clip cropped by Avisynth+ (have to code pitches properly; not an issue in VirtualDub filters :facepalm: ) The avisynth equalization filter from vcmohan shows terrible blocking on the window boundaries. WinHisto avoids it by interpolating LUTs as described above.
DAE avatar
Guest

Re: HDR -> SDR tonemapping

Post by Guest »

Can we access the WinHisto settings directly in HDRtoSDR
User avatar
admin
Posts: 4551
Joined: Thu Sep 09, 2010 3:08 pm

Re: HDR -> SDR tonemapping

Post by admin »

WinHisto() is included so you can run it standalone. If you are talking about the equalizer in the tonemapping, you can already set the strength; that is called equal. But the window sizes are fixed at xwindow=128 and ywindow=135. These have to evenly divide into 3840x2160 and the size seems to be a sweet spot for tonemapping.
DAE avatar
Guest

Re: HDR -> SDR tonemapping

Post by Guest »

So no cropping allowed in DGIndexNV unless the result is divisible, evenly, by 128 x 135
User avatar
admin
Posts: 4551
Joined: Thu Sep 09, 2010 3:08 pm

Re: HDR -> SDR tonemapping

Post by admin »

No, only Avisynth+ cropping is relevant.

But anyway, it's totally unrealistic that you would crop down to 3840x2160. Those dimensions are required by DGHDRtoSDR.

Don't worry too much about this because I will code it for proper pitch handling.
User avatar
admin
Posts: 4551
Joined: Thu Sep 09, 2010 3:08 pm

Re: HDR -> SDR tonemapping

Post by admin »

I completed some experiments with tempering the equalization based on the window variance of the histogram. Also tried using the interquartile range. Neither was useful for identifying flat areas, and in retrospect it's not surprising because what is needed is a measure of the spatial detail, and that is not encoded in the histogram. So my next attempt will be to develop a measure based on the spatial fourier transform of the window. If y'all have any ideas about this please post.

Another thing I will try is to use the anti-banding from my global histogram equalization virtualdub filter. If we can't find a solution we'll have to abandon the equalization idea, which would be a shame because it is really good at bringing out the detail in the crushed areas produced by the 2020->709 gamma transform and tonemapping.
User avatar
admin
Posts: 4551
Joined: Thu Sep 09, 2010 3:08 pm

Re: HDR -> SDR tonemapping

Post by admin »

Looks like the spatial fourier transform is what we need. Here's the transform of a flat area taken from the banded example submitted earlier in the thread:

test2.raw.mag.bmp
And here is a detailed area (sorry didn't get the same window size but it doesn't matter):

test4.raw.mag.bmp
So I can just check the brightness of a doughnut area around the origin. The hole in the doughnut excludes the effect of the overall brightness of the area. The outside margin of the donut excludes high-frequency noise. This will give me a good measure of the window detail that I can use to grade the amount of equalization: less in undetailed windows.

Fortunately, we have this:

https://developer.nvidia.com/cufft
DAE avatar
Guest

Re: HDR -> SDR tonemapping

Post by Guest »

As a Canadian I must say, reading your post about the doughnuts made me hungry :lol:
Just kidding
Looks like you are making significant progress :salute:
User avatar
admin
Posts: 4551
Joined: Thu Sep 09, 2010 3:08 pm

Re: HDR -> SDR tonemapping

Post by admin »

Canadians eat a lot of doughnuts? :?

Doughnuts in real life are no good, unless you want to be fat and sick. Low carb/sugar, high fat is the way to go.

I think the fourier transform idea is going to really make histogram equalization a much more useful thing, and not just for tonemapping.
DAE avatar
Guest

Re: HDR -> SDR tonemapping

Post by Guest »

Sounds like you have ideas for the Fast Fourier Transform
User avatar
admin
Posts: 4551
Joined: Thu Sep 09, 2010 3:08 pm

Re: HDR -> SDR tonemapping

Post by admin »

@dmcs

I appreciate the humor of the post you made, but in the current PC climate it could get us in trouble, so forgive me for deleting it.
User avatar
admin
Posts: 4551
Joined: Thu Sep 09, 2010 3:08 pm

Re: HDR -> SDR tonemapping

Post by admin »

gonca wrote:
Tue May 15, 2018 4:04 pm
Sounds like you have ideas for the Fast Fourier Transform
This is CUDA land. Slow does not exist here. ;)
User avatar
admin
Posts: 4551
Joined: Thu Sep 09, 2010 3:08 pm

Re: HDR -> SDR tonemapping

Post by admin »

I've got the cuFFT working. Here are pictures for flat and detailed windows:

test2.mag.bmp
test4.mag.bmp
While it is certainly usable to distinguish flat areas, the FFT magnitude plot differs from the one I get with a brute force fourier transform (C code, not FFT). The CUDA one seems compressed along the X axis. I want to look at that before trying to use this for the mitigation of equalization.
User avatar
admin
Posts: 4551
Joined: Thu Sep 09, 2010 3:08 pm

Re: HDR -> SDR tonemapping

Post by admin »

I found and fixed the pitch bug I had that was making skinny FFT plots. Now I will develop the metric based on the doughnut idea. After that will integrate it into the equalization code to make the strength of the equalization a function of the amount of detail in the area. Looking good so far.
DAE avatar
Guest

Re: HDR -> SDR tonemapping

Post by Guest »

Man, you are fast and good at coding
Well done :salute:
User avatar
admin
Posts: 4551
Joined: Thu Sep 09, 2010 3:08 pm

Re: HDR -> SDR tonemapping

Post by admin »

Thanks, gonca. It's not so much about the coding as it is the debugging. I have always been a kick-ass debugger, all modesty aside. It's easy to crank out code; making it actually work right is the challenge. But I am a good coder too. ;)

I completed the metric (but my doughnut is a square one, hehe). Here it is in action. First the flat window (the first image is the input image and the second is the fourier transform image):

test2.bmp
test2.mag.bmp

The detail metric for the flat window is 0.055790. And now the detailed window:

test4.bmp
test4.mag.bmp

The detail metric for the detailed window is 0.903815. This gives a ratio of about 16:1 for the two metrics.

The metric is normalized for overall brightness of the window. It's clear that this metric effectively distinguishes flat areas from detailed areas.

Next step: integrate this metric into DGHDRtoSDR and use it to modulate the equalization.
User avatar
admin
Posts: 4551
Joined: Thu Sep 09, 2010 3:08 pm

Re: HDR -> SDR tonemapping

Post by admin »

I implemented batched FFT for all the windows in the frame with one execute call. This improved performance as expected. But more importantly, I experimented with the FFTW library and discovered that for my transform size (128 x 135 real-to-complex), multithreaded FFTW is about twice as fast as cuFFT! The reason of course is (yet again) the overhead of transferring data to/from the GPU. Using FFTW will also allow me to overlap the GPU activities and the CPU activities (kernel execution is asynchronous), whereas if I used cuFFT, everything would be running serially on the GPU.

So finally now I am ready to start integrating this metric into DGHDRtoSDR and to see if the banding due to equalization can be avoided. I'm really learning a lot!
DAE avatar
Guest

Re: HDR -> SDR tonemapping

Post by Guest »

It is, after all, the Fastest Fourier Transform in the West
Good to know you are still learning, good to keep the brain cells active and alive
User avatar
admin
Posts: 4551
Joined: Thu Sep 09, 2010 3:08 pm

Re: HDR -> SDR tonemapping

Post by admin »

Unfortunately, the FFT-based idea to salvage equalization is not working out, for two main reasons. First, the metric does not reliably distinguish flat areas in real video full frames. There is simply two much variance for it to make a clean cut. Second, equalization itself changes the overall brightness of the window, which produces a horrid effect in the full frame when not all windows are equalized. So for now I am removing this functionality. I doubt that it can be salvaged.

Here is DGHDRtoSDR 1.1:

* Equalization mechanism removed.

* Reinhard replaced with gamma. I find it works better.

* You can now crop and resize before calling DGHDRtoSDR().

* See the user manual for details and examples.

http://rationalqm.us/misc/DGHDRtoSDR_1.1.rar
DAE avatar
Narkyy
Posts: 51
Joined: Thu May 25, 2017 11:51 pm

Re: HDR -> SDR tonemapping

Post by Narkyy »

Thanks for the update :hat:

I've tried the new gamma tonemap and noticed that the midtones are lacking in brightness.
However default is much better than Hable for the highlights.
Some comparisons and respective settings:

SDR Image

dghdrtosdr(impl="255",light=47,gamma=0.38) Image

DGHable(exposure=1.0, w=2.5) Image

Odd values for Hable but they're what I find work well when the highlights are not bright enough at default.
Reinhard (other than bad highlights) behaved similarly as Gamma does, but Hable has that extra 'w' parameter that makes the difference.
User avatar
admin
Posts: 4551
Joined: Thu Sep 09, 2010 3:08 pm

Re: HDR -> SDR tonemapping

Post by admin »

Try gamma of 0.40 to 0.42 and report back. Thank you.

gamma= 0.42 is the theoretically correct value. I set a lower one to bring out some black detail but probably set too low.
DAE avatar
dmcs
Posts: 36
Joined: Sat Oct 21, 2017 9:40 pm

Re: HDR -> SDR tonemapping

Post by dmcs »

admin wrote:
Mon May 21, 2018 4:53 pm
Try gamma of 0.40 to 0.42 and report back. Thank you.

gamma= 4.2 is the theoretically correct value. I set a lower one to bring out some black detail but probably set too low.
Typo, I guess?
User avatar
admin
Posts: 4551
Joined: Thu Sep 09, 2010 3:08 pm

Re: HDR -> SDR tonemapping

Post by admin »

For sure. Corrected, thank you. :salute:
DAE avatar
Narkyy
Posts: 51
Joined: Thu May 25, 2017 11:51 pm

Re: HDR -> SDR tonemapping

Post by Narkyy »

Here's the same frame, gamma at 0.40 and 0.42
It does improve the contrast, though still dim in terms of brightness where light shines.
However the Hable method often results in some scenes brighter than SDR, so it might work better in places.

If I increase light, it only makes the paper overblown and loses detail there (which is already happening at 0.47)

dghdrtosdr(impl="255",light=47,gamma=0.40) Image
dghdrtosdr(impl="255",light=47,gamma=0.42) Image
User avatar
admin
Posts: 4551
Joined: Thu Sep 09, 2010 3:08 pm

Re: HDR -> SDR tonemapping

Post by admin »

Honestly, I cannot see the things you claim to see and they seem highly subjective to me in any case. So I'll just add Hable as an option and declare mission accomplished.
Post Reply