[FFmpeg-devel] [Read EXIF metadata 1/5] Refactor TIFF tag related functions to share the code.

Thilo Borgmann thilo.borgmann at googlemail.com
Tue Jul 30 20:32:40 CEST 2013


---
 libavcodec/tiff.c |  134 +++++++++++++++++++++++++++++------------------------
 libavcodec/tiff.h |   15 ++++++
 2 files changed, 89 insertions(+), 60 deletions(-)

diff --git a/libavcodec/tiff.c b/libavcodec/tiff.c
index fdfa8f2..0076be3 100644
--- a/libavcodec/tiff.c
+++ b/libavcodec/tiff.c
@@ -70,13 +70,13 @@ typedef struct TiffContext {
     TiffGeoTag *geotags;
 } TiffContext;
 -static unsigned tget_short(GetByteContext *gb, int le)
+unsigned tget_short(GetByteContext *gb, int le)
 {
     unsigned v = le ? bytestream2_get_le16(gb) : bytestream2_get_be16(gb);
     return v;
 }
 -static unsigned tget_long(GetByteContext *gb, int le)
+unsigned tget_long(GetByteContext *gb, int le)
 {
     unsigned v = le ? bytestream2_get_le32(gb) : bytestream2_get_be32(gb);
     return v;
@@ -271,9 +271,9 @@ static char *shorts2str(int16_t *sp, int count, const char *sep)
     return ap0;
 }
 -static int add_doubles_metadata(int count,
+int tiff_add_doubles_metadata(int count,
                                 const char *name, const char *sep,
-                                TiffContext *s, AVFrame *frame)
+                                GetByteContext *gb, int le, AVDictionary
**metadata)
 {
     char *ap;
     int i;
@@ -281,7 +281,7 @@ static int add_doubles_metadata(int count,
      if (count >= INT_MAX / sizeof(int64_t) || count <= 0)
         return AVERROR_INVALIDDATA;
-    if (bytestream2_get_bytes_left(&s->gb) < count * sizeof(int64_t))
+    if (bytestream2_get_bytes_left(gb) < count * sizeof(int64_t))
         return AVERROR_INVALIDDATA;
      dp = av_malloc(count * sizeof(double));
@@ -289,17 +289,18 @@ static int add_doubles_metadata(int count,
         return AVERROR(ENOMEM);
      for (i = 0; i < count; i++)
-        dp[i] = tget_double(&s->gb, s->le);
+        dp[i] = tget_double(gb, le);
     ap = doubles2str(dp, count, sep);
     av_freep(&dp);
     if (!ap)
         return AVERROR(ENOMEM);
-    av_dict_set(avpriv_frame_get_metadatap(frame), name, ap,
AV_DICT_DONT_STRDUP_VAL);
+    av_dict_set(metadata, name, ap, AV_DICT_DONT_STRDUP_VAL);
     return 0;
 }
 -static int add_shorts_metadata(int count, const char *name,
-                               const char *sep, TiffContext *s, AVFrame *frame)
+int tiff_add_shorts_metadata(int count, const char *name,
+                               const char *sep,
+                                GetByteContext *gb, int le, AVDictionary
**metadata)
 {
     char *ap;
     int i;
@@ -307,7 +308,7 @@ static int add_shorts_metadata(int count, const char *name,
      if (count >= INT_MAX / sizeof(int16_t) || count <= 0)
         return AVERROR_INVALIDDATA;
-    if (bytestream2_get_bytes_left(&s->gb) < count * sizeof(int16_t))
+    if (bytestream2_get_bytes_left(gb) < count * sizeof(int16_t))
         return AVERROR_INVALIDDATA;
      sp = av_malloc(count * sizeof(int16_t));
@@ -315,41 +316,41 @@ static int add_shorts_metadata(int count, const char *name,
         return AVERROR(ENOMEM);
      for (i = 0; i < count; i++)
-        sp[i] = tget_short(&s->gb, s->le);
+        sp[i] = tget_short(gb, le);
     ap = shorts2str(sp, count, sep);
     av_freep(&sp);
     if (!ap)
         return AVERROR(ENOMEM);
-    av_dict_set(avpriv_frame_get_metadatap(frame), name, ap,
AV_DICT_DONT_STRDUP_VAL);
+    av_dict_set(metadata, name, ap, AV_DICT_DONT_STRDUP_VAL);
     return 0;
 }
 -static int add_string_metadata(int count, const char *name,
-                               TiffContext *s, AVFrame *frame)
+int tiff_add_string_metadata(int count, const char *name,
+                                GetByteContext *gb, int le, AVDictionary
**metadata)
 {
     char *value;
 -    if (bytestream2_get_bytes_left(&s->gb) < count || count < 0)
+    if (bytestream2_get_bytes_left(gb) < count || count < 0)
         return AVERROR_INVALIDDATA;
      value = av_malloc(count + 1);
     if (!value)
         return AVERROR(ENOMEM);
 -    bytestream2_get_bufferu(&s->gb, value, count);
+    bytestream2_get_bufferu(gb, value, count);
     value[count] = 0;
 -    av_dict_set(avpriv_frame_get_metadatap(frame), name, value,
AV_DICT_DONT_STRDUP_VAL);
+    av_dict_set(metadata, name, value, AV_DICT_DONT_STRDUP_VAL);
     return 0;
 }
 -static int add_metadata(int count, int type,
-                        const char *name, const char *sep, TiffContext *s,
AVFrame *frame)
+static int add_metadata(int count, int type, const char *name, const char *sep,
+                 GetByteContext *gb, int le, AVDictionary **metadata)
 {
     switch(type) {
-    case TIFF_DOUBLE: return add_doubles_metadata(count, name, sep, s, frame);
-    case TIFF_SHORT : return add_shorts_metadata(count, name, sep, s, frame);
-    case TIFF_STRING: return add_string_metadata(count, name, s, frame);
+    case TIFF_DOUBLE: return tiff_add_doubles_metadata(count, name, sep, gb,
le, metadata);
+    case TIFF_SHORT : return tiff_add_shorts_metadata(count, name, sep, gb, le,
metadata);
+    case TIFF_STRING: return tiff_add_string_metadata(count, name, gb, le,
metadata);
     default         : return AVERROR_INVALIDDATA;
     };
 }
@@ -694,6 +695,47 @@ static int init_image(TiffContext *s, AVFrame *frame)
     return 0;
 }
 +int tiff_decode_header(GetByteContext *gb, int *le, int *ifd_offset)
+{
+    if (bytestream2_get_bytes_left(gb) < 8) {
+        return AVERROR_INVALIDDATA;
+    }
+
+    *le = bytestream2_get_le16u(gb);
+    if (*le == AV_RB16("II")) {
+        *le = 1;
+    } else if (*le == AV_RB16("MM")) {
+        *le = 0;
+    } else {
+        return AVERROR_INVALIDDATA;
+    }
+
+    if (tget_short(gb, *le) != 42) {
+        return AVERROR_INVALIDDATA;
+    }
+
+    *ifd_offset = tget_long(gb, *le);
+
+    return 0;
+}
+
+void tiff_read_tag_data(GetByteContext *gb, int le, unsigned *tag, unsigned
*type, unsigned *count, unsigned *offset)
+{
+    *tag    = tget_short(gb, le);
+    *type   = tget_short(gb, le);
+    *count  = tget_long (gb, le);
+    *offset = tget_long (gb, le);
+}
+
+void tiff_seek_tag_value(GetByteContext *gb, unsigned type, unsigned count,
unsigned offset)
+{
+    if (count <= 4 && (type_sizes[type] * count <= 4 || type == TIFF_STRING)) {
+        bytestream2_seek(gb, -4, SEEK_CUR);
+    } else {
+        bytestream2_seek(gb, offset, SEEK_SET);
+    }
+}
+
 static int tiff_decode_tag(TiffContext *s, AVFrame *frame)
 {
     unsigned tag, type, count, off, value = 0;
@@ -702,10 +744,7 @@ static int tiff_decode_tag(TiffContext *s, AVFrame *frame)
     uint32_t *pal;
     double *dp;
 -    tag   = tget_short(&s->gb, s->le);
-    type  = tget_short(&s->gb, s->le);
-    count = tget_long(&s->gb, s->le);
-    off   = tget_long(&s->gb, s->le);
+    tiff_read_tag_data(&s->gb, s->le, &tag, &type, &count, &off);
     start = bytestream2_tell(&s->gb);
      if (type == 0 || type >= FF_ARRAY_ELEMS(type_sizes)) {
@@ -714,30 +753,20 @@ static int tiff_decode_tag(TiffContext *s, AVFrame *frame)
         return 0;
     }
 +    tiff_seek_tag_value(&s->gb, type, count, off);
     if (count == 1) {
         switch (type) {
         case TIFF_BYTE:
         case TIFF_SHORT:
-            bytestream2_seek(&s->gb, -4, SEEK_CUR);
             value = tget(&s->gb, type, s->le);
             break;
         case TIFF_LONG:
             value = off;
             break;
-        case TIFF_STRING:
-            if (count <= 4) {
-                bytestream2_seek(&s->gb, -4, SEEK_CUR);
-                break;
-            }
         default:
             value = UINT_MAX;
             bytestream2_seek(&s->gb, off, SEEK_SET);
         }
-    } else {
-        if (count <= 4 && type_sizes[type] * count <= 4)
-            bytestream2_seek(&s->gb, -4, SEEK_CUR);
-        else
-            bytestream2_seek(&s->gb, off, SEEK_SET);
     }
      switch (tag) {
@@ -927,7 +956,7 @@ static int tiff_decode_tag(TiffContext *s, AVFrame *frame)
             s->fax_opts = value;
         break;
 #define ADD_METADATA(count, name, sep)\
-    if ((ret = add_metadata(count, type, name, sep, s, frame)) < 0) {\
+    if ((ret = add_metadata(count, type, name, sep, &s->gb, s->le,
avpriv_frame_get_metadatap(frame))) < 0) {\
         av_log(s->avctx, AV_LOG_ERROR, "Error allocating temporary buffer\n");\
         return ret;\
     }
@@ -1075,7 +1104,7 @@ static int decode_frame(AVCodecContext *avctx,
     TiffContext *const s = avctx->priv_data;
     AVFrame *const p = data;
     unsigned off;
-    int id, le, ret, plane, planes;
+    int le, ret, plane, planes;
     int i, j, entries, stride;
     unsigned soff, ssize;
     uint8_t *dst;
@@ -1085,17 +1114,14 @@ static int decode_frame(AVCodecContext *avctx,
     bytestream2_init(&s->gb, avpkt->data, avpkt->size);
      // parse image header
-    if (avpkt->size < 8)
-        return AVERROR_INVALIDDATA;
-    id   = bytestream2_get_le16u(&s->gb);
-    if (id == 0x4949)
-        le = 1;
-    else if (id == 0x4D4D)
-        le = 0;
-    else {
-        av_log(avctx, AV_LOG_ERROR, "TIFF header not found\n");
+    if ((ret = tiff_decode_header(&s->gb, &le, &off))) {
+        av_log(avctx, AV_LOG_ERROR, "Invalid TIFF header\n");
+        return ret;
+    } else if (off >= UINT_MAX - 14 || avpkt->size < off + 14) {
+        av_log(avctx, AV_LOG_ERROR, "IFD offset is greater than image size\n");
         return AVERROR_INVALIDDATA;
     }
+
     s->le         = le;
     // TIFF_BPP is not a required tag and defaults to 1
     s->bppcount   = s->bpp = 1;
@@ -1104,21 +1130,9 @@ static int decode_frame(AVCodecContext *avctx,
     s->fill_order = 0;
     free_geotags(s);
 -    // As TIFF 6.0 specification puts it "An arbitrary but carefully chosen number
-    // that further identifies the file as a TIFF file"
-    if (tget_short(&s->gb, le) != 42) {
-        av_log(avctx, AV_LOG_ERROR,
-               "The answer to life, universe and everything is not correct!\n");
-        return AVERROR_INVALIDDATA;
-    }
     // Reset these offsets so we can tell if they were set this frame
     s->stripsizesoff = s->strippos = 0;
     /* parse image file directory */
-    off = tget_long(&s->gb, le);
-    if (off >= UINT_MAX - 14 || avpkt->size < off + 14) {
-        av_log(avctx, AV_LOG_ERROR, "IFD offset is greater than image size\n");
-        return AVERROR_INVALIDDATA;
-    }
     bytestream2_seek(&s->gb, off, SEEK_SET);
     entries = tget_short(&s->gb, le);
     if (bytestream2_get_bytes_left(&s->gb) < entries * 12)
diff --git a/libavcodec/tiff.h b/libavcodec/tiff.h
index 3ea2158..3274807 100644
--- a/libavcodec/tiff.h
+++ b/libavcodec/tiff.h
@@ -31,6 +31,7 @@
 #define AVCODEC_TIFF_H
  #include <stdint.h>
+#include "bytestream.h"
  /** abridged list of TIFF tags */
 enum TiffTags {
@@ -190,4 +191,18 @@ typedef struct TiffGeoTagNameType {
     const enum TiffGeoTagType type;
 } TiffGeoTagNameType;
 +int  tiff_decode_header(GetByteContext *gb, int *le, int *ifd_offset);
+void tiff_read_tag_data(GetByteContext *gb, int le, unsigned *tag, unsigned
*type, unsigned *count, unsigned *offset);
+void tiff_seek_tag_value(GetByteContext *gb, unsigned type, unsigned count,
unsigned offset);
+
+unsigned tget_short(GetByteContext *gb, int le);
+unsigned tget_long(GetByteContext *gb, int le);
+
+int tiff_add_doubles_metadata(int count, const char *name, const char *sep,
+                              GetByteContext *gb, int le, AVDictionary **metadata);
+int tiff_add_shorts_metadata(int count, const char *name, const char *sep,
+                             GetByteContext *gb, int le, AVDictionary **metadata);
+int tiff_add_string_metadata(int count, const char *name,
+                             GetByteContext *gb, int le, AVDictionary **metadata);
+
 #endif /* AVCODEC_TIFF_H */
-- 
1.7.4.3




More information about the ffmpeg-devel mailing list