diff mbox series

[FFmpeg-devel,v3,3/3] avcodec/pngdec: populate enum transfer values from gAMA chunk

Message ID 20230202150938.22297-4-leo.izen@gmail.com
State New
Headers show
Series PNG color tagging improvements | expand

Checks

Context Check Description
yinshiyou/make_loongarch64 success Make finished
yinshiyou/make_fate_loongarch64 success Make fate finished
andriy/make_x86 success Make finished
andriy/make_fate_x86 success Make fate finished

Commit Message

Leo Izen Feb. 2, 2023, 3:09 p.m. UTC
If the gAMA chunk is present, the gamma-transfer described by it often
matches one of our enum values (e.g. gamma = 2.2). If so, we should
populate the corresponding fields in AVCodecContext and AVFrame,
provided that some other chunk isn't already providing that info.
---
 libavcodec/pngdec.c | 27 ++++++++++++++++++++++++---
 1 file changed, 24 insertions(+), 3 deletions(-)
diff mbox series

Patch

diff --git a/libavcodec/pngdec.c b/libavcodec/pngdec.c
index a80e0d15bb..b7751c64d8 100644
--- a/libavcodec/pngdec.c
+++ b/libavcodec/pngdec.c
@@ -75,6 +75,7 @@  typedef struct PNGDecContext {
     int have_chrm;
     uint32_t white_point[2];
     uint32_t display_primaries[3][2];
+    int gamma;
     int have_srgb;
     int have_cicp;
     enum AVColorPrimaries cicp_primaries;
@@ -1259,6 +1260,7 @@  static int decode_frame_common(AVCodecContext *avctx, PNGDecContext *s,
             case MKTAG('s', 'R', 'G', 'B'):
             case MKTAG('c', 'I', 'C', 'P'):
             case MKTAG('c', 'H', 'R', 'M'):
+            case MKTAG('g', 'A', 'M', 'A'):
                 break;
             default:
                 continue;
@@ -1361,10 +1363,10 @@  static int decode_frame_common(AVCodecContext *avctx, PNGDecContext *s,
         case MKTAG('g', 'A', 'M', 'A'): {
             AVBPrint bp;
             char *gamma_str;
-            int num = bytestream2_get_be32(&gb_chunk);
+            s->gamma = bytestream2_get_be32(&gb_chunk);
 
             av_bprint_init(&bp, 0, AV_BPRINT_SIZE_UNLIMITED);
-            av_bprintf(&bp, "%i/%i", num, 100000);
+            av_bprintf(&bp, "%i/%i", s->gamma, 100000);
             ret = av_bprint_finalize(&bp, &gamma_str);
             if (ret < 0)
                 return ret;
@@ -1429,8 +1431,27 @@  exit_loop:
     }
 
     /* these chunks override gAMA */
-    if (s->iccp_data || s->have_srgb || s->have_cicp)
+    if (s->iccp_data || s->have_srgb || s->have_cicp) {
         av_dict_set(&s->frame_metadata, "gamma", NULL, 0);
+    } else if (s->gamma) {
+        /*
+         * These values are 100000/2.2, 100000/2.8, 100000/2.6, and
+         * 100000/1.0 respectively. 45455, 35714, and 38462, and 100000.
+         * There's a 0.001 gamma tolerance here in case of floating
+         * point issues when the PNG was written.
+         *
+         * None of the other enums have a pure gamma curve so it makes
+         * sense to leave those to sRGB and cICP.
+         */
+        if (s->gamma > 45355 && s->gamma < 45555)
+            avctx->color_trc = p->color_trc = AVCOL_TRC_GAMMA22;
+        else if (s->gamma > 35614 && s->gamma < 35814)
+            avctx->color_trc = p->color_trc = AVCOL_TRC_GAMMA28;
+        else if (s->gamma > 38362 && s->gamma < 38562)
+            avctx->color_trc = p->color_trc = AVCOL_TRC_SMPTE428;
+        else if (s->gamma > 99900 && s->gamma < 100100)
+            avctx->color_trc = p->color_trc = AVCOL_TRC_LINEAR;
+    }
 
     avctx->colorspace = p->colorspace = AVCOL_SPC_RGB;
     avctx->color_range = p->color_range = AVCOL_RANGE_JPEG;