AOMediaCodec / libavif

libavif - Library for encoding and decoding .avif files
Other
1.57k stars 202 forks source link

Sample HDR AVIF decoded to JPG shows incorrect colors #2039

Open o-l-a-v opened 8 months ago

o-l-a-v commented 8 months ago

libavif v1.0.4.

Following image:

Decodes to JPG but colors end up looking dull/washed out.

I used following command:

avifdec.exe input.avif output.jpg

Output always looks like this, in GIMP, Paint.NET, Windows Photos, XnView MP.

AVIF - PNG - JPG

240224 - avifdec - HDR AVIF to JPG

y-guyon commented 8 months ago

Thank you for the bug report. See https://github.com/AOMediaCodec/libavif/issues/1421 for an open entry about the issue: avifdec now writes CICP metadata (here 9/16/9) to PNG as a cHRM chunk, but JPEG does not support that.

The AVIF file has no ICC color profile by the way but I tried stuffing ITUR_2100_PQ_FULL.ICC into the output JPG using avifdec input.avif --icc colorprofile.icc output.jpg (requires https://github.com/AOMediaCodec/libavif/pull/2042):

20140606_102418_IMGP0297_hdr_rec2020_pq_yuv444_full_cq10

It does not seem to be the correct ICC but it could be a working approach.

o-l-a-v commented 8 months ago

Even if --icc is not specified I still expect avifdec.exe input.avif output.jpg to create an SDR JPG image looking close to the original.

One more alternative approach/idea: If input is HDR => Use JPEG XL jpegli to encode to JPEG with XYB colorspace, which supports HDR.

y-guyon commented 8 months ago

Even if --icc is not specified I still expect avifdec.exe input.avif output.jpg to create an SDR JPG image looking close to the original.

Unfortunately libavif cannot handle all format and color space combinations, with workarounds for every situation. Detecting special cases such as common CICP combinations and outputting a matching ICC profile in JPG seems doable though.
If this is impossible for various reasons, or in the meantime, would an error or warning in avifdec console output be better?

Have you tried other conversion tools than avifdec for going from AVIF to JPG? Maybe there is some acceptable behavior we could match.

One more alternative approach/idea: If input is HDR => Use JPEG XL jpegli to encode to JPEG with XYB colorspace, which supports HDR.

So the input AVIF file would be in XYB colorspace?
If so, why not directly embed an HDR PQ ICC profile in the input AVIF file? It would be forwarded to the output JPG by avifdec.

o-l-a-v commented 8 months ago

Error if input has CICP and output is set to JPG until this might get fixed in the future makes sense.

I'm no image expert, I might've gotten the XYB part of jpegli wrong.

I don't know of other tools that handle this scenario, no.

kmilos commented 8 months ago

If so, why not directly embed an HDR PQ ICC profile in the input AVIF file? It would be forwarded to the output JPG by avifdec.

This not a good idea for 8-bit encoding like JPEG (I guess until, and if avifdec decides to support 12-bit JPEG as well).

IMHO, I think it should be enough detect anything other than sRGB/Rec709 (as either CICP or ICC), and throw a warning in the console that the image cannot really be preserved in a JPEG (or any other 8-bit container), unless the input was 8-bit to begin with of course.

I still expect avifdec.exe input.avif output.jpg to create an SDR JPG image looking close to the original.

Gamut and tone mapping should be left to a different abstraction level/library, as there are a few compromises and creative choices to be made there, it's not really a job of the codec library IMHO.

kmilos commented 8 months ago

Btw, if -d is not specified, will avifdec correctly choose PNG bit depth depending on input?

y-guyon commented 8 months ago

Btw, if -d is not specified, will avifdec correctly choose PNG bit depth depending on input?

Yes.

leo-barnes commented 8 months ago

Gamut and tone mapping should be left to a different abstraction level/library, as there are a few compromises and creative choices to be made there, it's not really a job of the codec library IMHO.

Completely agree. libavif could add the ITU-R recommended way of tone mapping from HDR to SDR as a convenience, but it doesn't necessarily produce great results.