[FFmpeg-devel] [Read EXIF metadata 3/3] Read EXIF metadata in JPEG input.

Thilo Borgmann thilo.borgmann at googlemail.com
Fri Aug 2 22:15:44 CEST 2013


Updated Patch according to latest comments.

-Thilo
-------------- next part --------------
>From 459ed32c4327daf533223d2d1a99a3b1e689dad3 Mon Sep 17 00:00:00 2001
From: Thilo Borgmann <thilo.borgmann at googlemail.com>
Date: Fri, 2 Aug 2013 21:41:51 +0200
Subject: [PATCH 3/3] Read EXIF metadata in JPEG input.

---
 Changelog             |    1 +
 libavcodec/Makefile   |    2 +-
 libavcodec/mjpegdec.c |   43 +++++++++++++++++++++++++++++++++++++++++++
 3 files changed, 45 insertions(+), 1 deletions(-)

diff --git a/Changelog b/Changelog
index cbee5fa..0778987 100644
--- a/Changelog
+++ b/Changelog
@@ -7,6 +7,7 @@ version <next>
 - perspective filter ported from libmpcodecs
 - ffprobe -show_programs option
 - compand filter
+- read EXIF metadata from JPEG
 
 
 version 2.0:
diff --git a/libavcodec/Makefile b/libavcodec/Makefile
index 5e2e894..6d6fe97 100644
--- a/libavcodec/Makefile
+++ b/libavcodec/Makefile
@@ -262,7 +262,7 @@ OBJS-$(CONFIG_MACE6_DECODER)           += mace.o
 OBJS-$(CONFIG_MDEC_DECODER)            += mdec.o mpeg12.o mpeg12data.o
 OBJS-$(CONFIG_MICRODVD_DECODER)        += microdvddec.o ass.o
 OBJS-$(CONFIG_MIMIC_DECODER)           += mimic.o
-OBJS-$(CONFIG_MJPEG_DECODER)           += mjpegdec.o mjpeg.o
+OBJS-$(CONFIG_MJPEG_DECODER)           += mjpegdec.o mjpeg.o exif.o tiff_common.o
 OBJS-$(CONFIG_MJPEG_ENCODER)           += mjpegenc.o mjpeg.o
 OBJS-$(CONFIG_MJPEGB_DECODER)          += mjpegbdec.o mjpegdec.o mjpeg.o
 OBJS-$(CONFIG_MLP_DECODER)             += mlpdec.o mlpdsp.o
diff --git a/libavcodec/mjpegdec.c b/libavcodec/mjpegdec.c
index c92738f..e5460b0 100644
--- a/libavcodec/mjpegdec.c
+++ b/libavcodec/mjpegdec.c
@@ -39,6 +39,9 @@
 #include "mjpeg.h"
 #include "mjpegdec.h"
 #include "jpeglsdec.h"
+#include "tiff.h"
+#include "exif.h"
+#include "bytestream.h"
 
 
 static int build_vlc(VLC *vlc, const uint8_t *bits_table,
@@ -215,6 +218,8 @@ int ff_mjpeg_decode_sof(MJpegDecodeContext *s)
     int len, nb_components, i, width, height, pix_fmt_id;
     int h_count[MAX_COMPONENTS];
     int v_count[MAX_COMPONENTS];
+    AVDictionary **metadata = avpriv_frame_get_metadatap(s->picture_ptr);
+    AVDictionary *tmp;
 
     s->cur_scan = 0;
     s->upscale_h = s->upscale_v = 0;
@@ -459,6 +464,10 @@ int ff_mjpeg_decode_sof(MJpegDecodeContext *s)
             s->avctx->pix_fmt = AV_PIX_FMT_GRAY16;
     }
 
+    // keep metadata alive by hiding AVDictioniary behind *tmp
+    tmp       = *metadata;
+    *metadata = NULL;
+
     av_frame_unref(s->picture_ptr);
     if (ff_get_buffer(s->avctx, s->picture_ptr, AV_GET_BUFFER_FLAG_REF) < 0)
         return -1;
@@ -466,6 +475,9 @@ int ff_mjpeg_decode_sof(MJpegDecodeContext *s)
     s->picture_ptr->key_frame = 1;
     s->got_picture            = 1;
 
+    // restore metadata in current buffer
+    *avpriv_frame_get_metadatap(s->picture_ptr) = tmp;
+
     for (i = 0; i < 3; i++)
         s->linesize[i] = s->picture_ptr->linesize[i] << s->interlaced;
 
@@ -1493,6 +1505,37 @@ static int mjpeg_decode_app(MJpegDecodeContext *s)
         goto out;
     }
 
+    /* EXIF metadata */
+    if (s->start_code == APP1 && id == AV_RB32("Exif")) {
+        GetByteContext gbytes;
+        int ret, le, ifd_offset, bytes_read;
+
+        skip_bits(&s->gb, 16); // skip padding
+
+        // init byte wise reading
+        bytestream2_init(&gbytes, align_get_bits(&s->gb), get_bits_left(&s->gb) >> 3);
+
+        // read TIFF header
+        ret = ff_tdecode_header(&gbytes, &le, &ifd_offset);
+        if (ret) {
+            av_log(s->avctx, AV_LOG_ERROR, "mjpeg: invalid TIFF header in EXIF data\n");
+            return ret;
+        }
+
+        bytestream2_seek(&gbytes, ifd_offset, SEEK_SET);
+
+        // read all IFDs and store the metadata
+        while ((ifd_offset = exif_decode_ifd(s->avctx, &gbytes, le, avpriv_frame_get_metadatap(s->picture_ptr))) > 0) {
+            bytestream2_seek(&gbytes, ifd_offset, SEEK_SET);
+        }
+
+        bytes_read = bytestream2_tell(&gbytes);
+        skip_bits(&s->gb, bytes_read << 3);
+        len -= bytes_read;
+
+        goto out;
+    }
+
     /* Apple MJPEG-A */
     if ((s->start_code == APP1) && (len > (0x28 - 8))) {
         id   = get_bits_long(&s->gb, 32);
-- 
1.7.4.3



More information about the ffmpeg-devel mailing list