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

Thilo Borgmann thilo.borgmann at googlemail.com
Mon Aug 5 17:34:30 CEST 2013


3/3 rev 2

-Thilo
-------------- next part --------------
>From ecf8ec26f11565e4e71a90799072c7d95447ae90 Mon Sep 17 00:00:00 2001
From: Thilo Borgmann <thilo.borgmann at googlemail.com>
Date: Mon, 5 Aug 2013 17:25:04 +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 48f93c7..8d29154 100644
--- a/Changelog
+++ b/Changelog
@@ -8,6 +8,7 @@ version <next>
 - ffprobe -show_programs option
 - compand filter
 - RTMP seek support
+- read EXIF metadata from JPEG
 
 
 version 2.0:
diff --git a/libavcodec/Makefile b/libavcodec/Makefile
index c23d0c8..d873431 100644
--- a/libavcodec/Makefile
+++ b/libavcodec/Makefile
@@ -263,7 +263,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..5f641bc 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 = ff_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