[FFmpeg-devel] [PATCH 2/3] avcodec/pngdec: parse eXIf chunk

Leo Izen leo.izen at gmail.com
Tue Feb 13 23:24:55 EET 2024


Add support to parse eXIf chunks using the new EXIF framework.

Signed-off-by: Leo Izen <leo.izen at gmail.com>
---
 libavcodec/pngdec.c | 35 +++++++++++++++++++++++++++++++++++
 1 file changed, 35 insertions(+)

diff --git a/libavcodec/pngdec.c b/libavcodec/pngdec.c
index 026da30c25..e7951d1802 100644
--- a/libavcodec/pngdec.c
+++ b/libavcodec/pngdec.c
@@ -38,6 +38,7 @@
 #include "bytestream.h"
 #include "codec_internal.h"
 #include "decode.h"
+#include "exif_internal.h"
 #include "apng.h"
 #include "png.h"
 #include "pngdsp.h"
@@ -123,6 +124,7 @@ typedef struct PNGDecContext {
     int pass_row_size; /* decompress row size of the current pass */
     int y;
     FFZStream zstream;
+    AVBufferRef *exif_data;
 } PNGDecContext;
 
 /* Mask to determine which pixels are valid in a pass */
@@ -652,6 +654,26 @@ static int decode_phys_chunk(AVCodecContext *avctx, PNGDecContext *s,
     return 0;
 }
 
+static int decode_exif_chunk(AVCodecContext *avctx, PNGDecContext *s,
+                             GetByteContext *gb)
+{
+    if (!(s->hdr_state & PNG_IHDR)) {
+        av_log(avctx, AV_LOG_ERROR, "eXIf before IHDR\n");
+        return AVERROR_INVALIDDATA;
+    }
+    if (s->pic_state & PNG_IDAT) {
+        av_log(avctx, AV_LOG_ERROR, "eXIf after IDAT\n");
+        return AVERROR_INVALIDDATA;
+    }
+    av_buffer_unref(&s->exif_data);
+    s->exif_data = av_buffer_alloc(bytestream2_get_bytes_left(gb));
+    if (!s->exif_data)
+        return AVERROR(ENOMEM);
+    bytestream2_get_buffer(gb, s->exif_data->data, s->exif_data->size);
+
+    return 0;
+}
+
 /*
  * This populates AVCodecContext fields so it must be called before
  * ff_thread_finish_setup() to avoid a race condition with respect to the
@@ -890,6 +912,12 @@ static int decode_idat_chunk(AVCodecContext *avctx, PNGDecContext *s,
         p->flags           |= AV_FRAME_FLAG_KEY;
         p->flags |= AV_FRAME_FLAG_INTERLACED * !!s->interlace_type;
 
+        if (s->exif_data) {
+            ret = ff_exif_attach(avctx, p, &s->exif_data);
+            if (ret < 0)
+                return ret;
+        }
+
         if ((ret = populate_avctx_color_fields(avctx, p)) < 0)
             return ret;
         ff_thread_finish_setup(avctx);
@@ -1571,6 +1599,12 @@ static int decode_frame_common(AVCodecContext *avctx, PNGDecContext *s,
             s->mdvc_max_lum = bytestream2_get_be32u(&gb_chunk);
             s->mdvc_min_lum = bytestream2_get_be32u(&gb_chunk);
             break;
+        case MKTAG('e', 'X', 'I', 'f'): {
+            ret = decode_exif_chunk(avctx, s, &gb_chunk);
+            if (ret < 0)
+                goto fail;
+            break;
+        }
         case MKTAG('I', 'E', 'N', 'D'):
             if (!(s->pic_state & PNG_ALLIMAGE))
                 av_log(avctx, AV_LOG_ERROR, "IEND without all image\n");
@@ -1907,6 +1941,7 @@ static av_cold int png_dec_end(AVCodecContext *avctx)
     s->tmp_row_size = 0;
 
     av_freep(&s->iccp_data);
+    av_buffer_unref(&s->exif_data);
     av_dict_free(&s->frame_metadata);
     ff_inflate_end(&s->zstream);
 
-- 
2.43.0



More information about the ffmpeg-devel mailing list