haasn / libplacebo

Official mirror of libplacebo
http://libplacebo.org/
GNU Lesser General Public License v2.1
563 stars 71 forks source link

Linear scaling for HDR content should not be disabled #281

Open jan2000 opened 3 months ago

jan2000 commented 3 months ago

For correct results resampling should always be done in linear color space. Otherwise problems will be introduced: a too dark image, stair-stepping along edges, stars start to twinkle in panning shots, etc...

https://x.com/ID_AA_Carmack/status/1654528504724062217 https://petewarden.com/2008/08/02/why-fonts-need/ http://www.ericbrasseur.org/gamma.html

Unfortunately in libplacebo linear resampling is currently always disabled for HDR content. This leads to errors when downscaling.

See this example clip. It was created by taking screenshot of the output of mpv of HDR content rescaled from 2160p to 1080p. On the left with libplacebo as is, on the right libplacebo patched to allow linear scaling with HDR content. (please view images in original size with 100% zoom on monitor with 1:1 pixel mapping)

compare Notice on the left the stair-stepping along the edges of the A and V. Also notice the wobbling between the T and the O and the T and wings of the logo (simular problem as twinkling stars described by Carmack). Compare to the correct linear resampling on the right.

Here another shot:

compare-text Again the stair-stepping / aliasing on the left, the text is also too slim. The linear resampling on the right looks way better.

Please do not disable linear scaling for HDR content so we can get correctly scaled video.

haasn commented 3 months ago

This was repeatedly enabled and disabled again in the past, because of other issues with nasty ringing artifacts around bright edges when scaling HDR sources in linear light.

Maybe you can take a look:

jan2000 commented 3 months ago

Looking at those github issues the nasty ringing artifacts are there because lanczos resampling is used, which is known to introduce ringing/clipping artifacts!

Using the incorrect colorspace for all resampling methods in order to work around an issue caused by a single method is of course not the way to go. What would be is to look at the problematic method lanczos separately.

https://en.wikipedia.org/wiki/Lanczos_resampling#Limitations

In some applications, the low-end clipping artifacts can be ameliorated by transforming the data to a logarithmic domain prior to filtering. In this case the interpolated values will be a weighted geometric mean, rather than an arithmetic mean, of the input samples.

So a solution to mitigate the ringing artifacts caused by the lancoz filter is to do the resampling in log-space just for lancoz. (This could be maybe be controlled by an option, say something like "lancoz-log-scaling", maybe enabled by default.) As this is basically similar to disabling linear scaling a quick fix would be to just in that case disable linear scaling.

Then for all other resampling methods linear scaling does not have to be disabled, and we can have correctly scaled HDR video.

natural-harmonia-gropius commented 3 months ago

something like "lancoz-log-scaling"

What if we do every thing (deband, scale) in Iab (currently using in tone map and gamut map)?

emotion3459 commented 2 months ago

After https://github.com/haasn/libplacebo/commit/75b3947b2c07803456483ec6976c037bad91b5dd there should be no downsides to re-enabling linear light downscaling on HDR content.

norinoriko commented 2 months ago

The original user's issue (https://github.com/mpv-player/mpv/issues/11036) with the dark halos on negative lobe filters is also very easily solvable nowadays since EWA antiringing has been implemented. If they wanted to keep using ewa_lanczos, they would just need to add this to their mpv config:

libplacebo-opts=downscaler=custom,downscaler_preset=ewa_lanczos,downscaler_antiring=1