[FFmpeg-devel] [Read EXIF metadata 1/3] Refactor TIFF tag related functions to share the code.
Thilo Borgmann
thilo.borgmann at googlemail.com
Fri Aug 2 22:15:05 CEST 2013
Updated Patch according to latest comments.
-Thilo
-------------- next part --------------
>From d64662e9507a62fd3eef7d46c9dee1b0ea07b9cd Mon Sep 17 00:00:00 2001
From: Thilo Borgmann <thilo.borgmann at googlemail.com>
Date: Fri, 2 Aug 2013 22:10:05 +0200
Subject: [PATCH] Refactor TIFF tag related functions to share the code.
---
libavcodec/Makefile | 2 +-
libavcodec/tiff.c | 235 +++++----------------------------------------
libavcodec/tiff.h | 22 +----
libavcodec/tiff_common.c | 240 ++++++++++++++++++++++++++++++++++++++++++++++
libavcodec/tiff_common.h | 113 ++++++++++++++++++++++
5 files changed, 380 insertions(+), 232 deletions(-)
create mode 100644 libavcodec/tiff_common.c
create mode 100644 libavcodec/tiff_common.h
diff --git a/libavcodec/Makefile b/libavcodec/Makefile
index 31477ac..5e2e894 100644
--- a/libavcodec/Makefile
+++ b/libavcodec/Makefile
@@ -421,7 +421,7 @@ OBJS-$(CONFIG_TARGA_Y216_DECODER) += targa_y216dec.o
OBJS-$(CONFIG_THEORA_DECODER) += xiph.o
OBJS-$(CONFIG_THP_DECODER) += mjpegdec.o mjpeg.o
OBJS-$(CONFIG_TIERTEXSEQVIDEO_DECODER) += tiertexseqv.o
-OBJS-$(CONFIG_TIFF_DECODER) += tiff.o lzw.o faxcompr.o tiff_data.o
+OBJS-$(CONFIG_TIFF_DECODER) += tiff.o lzw.o faxcompr.o tiff_data.o tiff_common.o
OBJS-$(CONFIG_TIFF_ENCODER) += tiffenc.o rle.o lzwenc.o tiff_data.o
OBJS-$(CONFIG_TMV_DECODER) += tmv.o cga_data.o
OBJS-$(CONFIG_TRUEHD_DECODER) += mlpdec.o mlpdsp.o
diff --git a/libavcodec/tiff.c b/libavcodec/tiff.c
index fdfa8f2..fe56754 100644
--- a/libavcodec/tiff.c
+++ b/libavcodec/tiff.c
@@ -70,38 +70,6 @@ typedef struct TiffContext {
TiffGeoTag *geotags;
} TiffContext;
-static 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 v = le ? bytestream2_get_le32(gb) : bytestream2_get_be32(gb);
- return v;
-}
-
-static double tget_double(GetByteContext *gb, int le)
-{
- av_alias64 i = { .u64 = le ? bytestream2_get_le64(gb) : bytestream2_get_be64(gb)};
- return i.f64;
-}
-
-static unsigned tget(GetByteContext *gb, int type, int le)
-{
- switch (type) {
- case TIFF_BYTE:
- return bytestream2_get_byte(gb);
- case TIFF_SHORT:
- return tget_short(gb, le);
- case TIFF_LONG:
- return tget_long(gb, le);
- default:
- return UINT_MAX;
- }
-}
-
static void free_geotags(TiffContext *const s)
{
int i;
@@ -219,137 +187,13 @@ static char *get_geokey_val(int key, int val)
return ap;
}
-static char *doubles2str(double *dp, int count, const char *sep)
-{
- int i;
- char *ap, *ap0;
- uint64_t component_len;
- if (!sep) sep = ", ";
- component_len = 15LL + strlen(sep);
- if (count >= (INT_MAX - 1)/component_len)
- return NULL;
- ap = av_malloc(component_len * count + 1);
- if (!ap)
- return NULL;
- ap0 = ap;
- ap[0] = '\0';
- for (i = 0; i < count; i++) {
- unsigned l = snprintf(ap, component_len, "%f%s", dp[i], sep);
- if(l >= component_len) {
- av_free(ap0);
- return NULL;
- }
- ap += l;
- }
- ap0[strlen(ap0) - strlen(sep)] = '\0';
- return ap0;
-}
-
-static char *shorts2str(int16_t *sp, int count, const char *sep)
-{
- int i;
- char *ap, *ap0;
- uint64_t component_len;
- if (!sep) sep = ", ";
- component_len = 7LL + strlen(sep);
- if (count >= (INT_MAX - 1)/component_len)
- return NULL;
- ap = av_malloc(component_len * count + 1);
- if (!ap)
- return NULL;
- ap0 = ap;
- ap[0] = '\0';
- for (i = 0; i < count; i++) {
- unsigned l = snprintf(ap, component_len, "%d%s", sp[i], sep);
- if (l >= component_len) {
- av_free(ap0);
- return NULL;
- }
- ap += l;
- }
- ap0[strlen(ap0) - strlen(sep)] = '\0';
- return ap0;
-}
-
-static int add_doubles_metadata(int count,
- const char *name, const char *sep,
- TiffContext *s, AVFrame *frame)
-{
- char *ap;
- int i;
- double *dp;
-
- if (count >= INT_MAX / sizeof(int64_t) || count <= 0)
- return AVERROR_INVALIDDATA;
- if (bytestream2_get_bytes_left(&s->gb) < count * sizeof(int64_t))
- return AVERROR_INVALIDDATA;
-
- dp = av_malloc(count * sizeof(double));
- if (!dp)
- return AVERROR(ENOMEM);
-
- for (i = 0; i < count; i++)
- dp[i] = tget_double(&s->gb, s->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);
- return 0;
-}
-
-static int add_shorts_metadata(int count, const char *name,
- const char *sep, TiffContext *s, AVFrame *frame)
-{
- char *ap;
- int i;
- int16_t *sp;
-
- if (count >= INT_MAX / sizeof(int16_t) || count <= 0)
- return AVERROR_INVALIDDATA;
- if (bytestream2_get_bytes_left(&s->gb) < count * sizeof(int16_t))
- return AVERROR_INVALIDDATA;
-
- sp = av_malloc(count * sizeof(int16_t));
- if (!sp)
- return AVERROR(ENOMEM);
-
- for (i = 0; i < count; i++)
- sp[i] = tget_short(&s->gb, s->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);
- return 0;
-}
-
-static int add_string_metadata(int count, const char *name,
- TiffContext *s, AVFrame *frame)
-{
- char *value;
-
- if (bytestream2_get_bytes_left(&s->gb) < count || count < 0)
- return AVERROR_INVALIDDATA;
-
- value = av_malloc(count + 1);
- if (!value)
- return AVERROR(ENOMEM);
-
- bytestream2_get_bufferu(&s->gb, value, count);
- value[count] = 0;
-
- av_dict_set(avpriv_frame_get_metadatap(frame), 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)
{
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 ff_tadd_doubles_metadata(count, name, sep, &s->gb, s->le, avpriv_frame_get_metadatap(frame));
+ case TIFF_SHORT : return ff_tadd_shorts_metadata(count, name, sep, &s->gb, s->le, avpriv_frame_get_metadatap(frame));
+ case TIFF_STRING: return ff_tadd_string_metadata(count, name, &s->gb, s->le, avpriv_frame_get_metadatap(frame));
default : return AVERROR_INVALIDDATA;
};
}
@@ -702,10 +546,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);
+ ff_tread_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 +555,20 @@ static int tiff_decode_tag(TiffContext *s, AVFrame *frame)
return 0;
}
+ ff_tseek_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);
+ value = ff_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) {
@@ -769,7 +600,7 @@ static int tiff_decode_tag(TiffContext *s, AVFrame *frame)
if (bytestream2_get_bytes_left(&s->gb) < type_sizes[type] * count)
return AVERROR_INVALIDDATA;
for (i = 0; i < count; i++)
- s->bpp += tget(&s->gb, type, s->le);
+ s->bpp += ff_tget(&s->gb, type, s->le);
break;
default:
s->bpp = -1;
@@ -909,7 +740,7 @@ static int tiff_decode_tag(TiffContext *s, AVFrame *frame)
for (i = 0; i < count / 3; i++) {
if (k == 2)
pal[i] = 0xFFU << 24;
- j = (tget(&s->gb, type, s->le) >> off) << (k * 8);
+ j = (ff_tget(&s->gb, type, s->le) >> off) << (k * 8);
pal[i] |= j;
}
}
@@ -943,7 +774,7 @@ static int tiff_decode_tag(TiffContext *s, AVFrame *frame)
case TIFF_GEO_KEY_DIRECTORY:
ADD_METADATA(1, "GeoTIFF_Version", NULL);
ADD_METADATA(2, "GeoTIFF_Key_Revision", ".");
- s->geotag_count = tget_short(&s->gb, s->le);
+ s->geotag_count = ff_tget_short(&s->gb, s->le);
if (s->geotag_count > count / 4 - 1) {
s->geotag_count = count / 4 - 1;
av_log(s->avctx, AV_LOG_WARNING, "GeoTIFF key directory buffer shorter than specified\n");
@@ -959,14 +790,14 @@ static int tiff_decode_tag(TiffContext *s, AVFrame *frame)
return AVERROR(ENOMEM);
}
for (i = 0; i < s->geotag_count; i++) {
- s->geotags[i].key = tget_short(&s->gb, s->le);
- s->geotags[i].type = tget_short(&s->gb, s->le);
- s->geotags[i].count = tget_short(&s->gb, s->le);
+ s->geotags[i].key = ff_tget_short(&s->gb, s->le);
+ s->geotags[i].type = ff_tget_short(&s->gb, s->le);
+ s->geotags[i].count = ff_tget_short(&s->gb, s->le);
if (!s->geotags[i].type)
- s->geotags[i].val = get_geokey_val(s->geotags[i].key, tget_short(&s->gb, s->le));
+ s->geotags[i].val = get_geokey_val(s->geotags[i].key, ff_tget_short(&s->gb, s->le));
else
- s->geotags[i].offset = tget_short(&s->gb, s->le);
+ s->geotags[i].offset = ff_tget_short(&s->gb, s->le);
}
break;
case TIFF_GEO_DOUBLE_PARAMS:
@@ -980,14 +811,14 @@ static int tiff_decode_tag(TiffContext *s, AVFrame *frame)
return AVERROR(ENOMEM);
}
for (i = 0; i < count; i++)
- dp[i] = tget_double(&s->gb, s->le);
+ dp[i] = ff_tget_double(&s->gb, s->le);
for (i = 0; i < s->geotag_count; i++) {
if (s->geotags[i].type == TIFF_GEO_DOUBLE_PARAMS) {
if (s->geotags[i].count == 0
|| s->geotags[i].offset + s->geotags[i].count > count) {
av_log(s->avctx, AV_LOG_WARNING, "Invalid GeoTIFF key %d\n", s->geotags[i].key);
} else {
- char *ap = doubles2str(&dp[s->geotags[i].offset], s->geotags[i].count, ", ");
+ char *ap = ff_tdoubles2str(&dp[s->geotags[i].offset], s->geotags[i].count, ", ");
if (!ap) {
av_log(s->avctx, AV_LOG_ERROR, "Error allocating temporary buffer\n");
av_freep(&dp);
@@ -1075,7 +906,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,15 +916,11 @@ 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 = ff_tdecode_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;
@@ -1104,23 +931,11 @@ 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);
+ entries = ff_tget_short(&s->gb, le);
if (bytestream2_get_bytes_left(&s->gb) < entries * 12)
return AVERROR_INVALIDDATA;
for (i = 0; i < entries; i++) {
@@ -1180,12 +995,12 @@ static int decode_frame(AVCodecContext *avctx,
dst = p->data[plane];
for (i = 0; i < s->height; i += s->rps) {
if (s->stripsizesoff)
- ssize = tget(&stripsizes, s->sstype, s->le);
+ ssize = ff_tget(&stripsizes, s->sstype, s->le);
else
ssize = s->stripsize;
if (s->strippos)
- soff = tget(&stripdata, s->sot, s->le);
+ soff = ff_tget(&stripdata, s->sot, s->le);
else
soff = s->stripoff;
diff --git a/libavcodec/tiff.h b/libavcodec/tiff.h
index 3ea2158..ae189b6 100644
--- a/libavcodec/tiff.h
+++ b/libavcodec/tiff.h
@@ -31,6 +31,7 @@
#define AVCODEC_TIFF_H
#include <stdint.h>
+#include "tiff_common.h"
/** abridged list of TIFF tags */
enum TiffTags {
@@ -97,22 +98,6 @@ enum TiffCompr {
TIFF_DEFLATE = 0x80B2
};
-enum TiffTypes {
- TIFF_BYTE = 1,
- TIFF_STRING,
- TIFF_SHORT,
- TIFF_LONG,
- TIFF_RATIONAL,
- TIFF_SBYTE,
- TIFF_UNDEFINED,
- TIFF_SSHORT,
- TIFF_SLONG,
- TIFF_SRATIONAL,
- TIFF_FLOAT,
- TIFF_DOUBLE,
- TIFF_IFD
-};
-
enum TiffGeoTagKey {
TIFF_GT_MODEL_TYPE_GEOKEY = 1024,
TIFF_GT_RASTER_TYPE_GEOKEY = 1025,
@@ -167,11 +152,6 @@ enum TiffGeoTagType {
GEOTIFF_STRING = 34737
};
-/** sizes of various TIFF field types (string size = 100)*/
-static const uint8_t type_sizes[14] = {
- 0, 1, 100, 2, 4, 8, 1, 1, 2, 4, 8, 4, 8, 4
-};
-
typedef struct TiffGeoTag {
enum TiffGeoTagKey key;
enum TiffTags type;
diff --git a/libavcodec/tiff_common.c b/libavcodec/tiff_common.c
new file mode 100644
index 0000000..b09810a
--- /dev/null
+++ b/libavcodec/tiff_common.c
@@ -0,0 +1,240 @@
+/*
+ * TIFF Common Routines
+ * Copyright (c) 2013 Thilo Borgmann <thilo.borgmann _at_ googlemail.com>
+ *
+ * This file is part of FFmpeg.
+ *
+ * FFmpeg is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 2.1 of the License, or (at your option) any later version.
+ *
+ * FFmpeg is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with FFmpeg; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
+ */
+
+/**
+ * @file
+ * TIFF Common Routines
+ * @author Thilo Borgmann <thilo.borgmann _at_ googlemail.com>
+ */
+
+#include "tiff_common.h"
+
+
+unsigned ff_tget_short(GetByteContext *gb, int le)
+{
+ unsigned v = le ? bytestream2_get_le16(gb) : bytestream2_get_be16(gb);
+ return v;
+}
+
+
+unsigned ff_tget_long(GetByteContext *gb, int le)
+{
+ unsigned v = le ? bytestream2_get_le32(gb) : bytestream2_get_be32(gb);
+ return v;
+}
+
+
+double ff_tget_double(GetByteContext *gb, int le)
+{
+ av_alias64 i = { .u64 = le ? bytestream2_get_le64(gb) : bytestream2_get_be64(gb)};
+ return i.f64;
+}
+
+
+unsigned ff_tget(GetByteContext *gb, int type, int le)
+{
+ switch (type) {
+ case TIFF_BYTE:
+ return bytestream2_get_byte(gb);
+ case TIFF_SHORT:
+ return ff_tget_short(gb, le);
+ case TIFF_LONG:
+ return ff_tget_long(gb, le);
+ default:
+ return UINT_MAX;
+ }
+}
+
+
+char *ff_tdoubles2str(double *dp, int count, const char *sep)
+{
+ int i;
+ char *ap, *ap0;
+ uint64_t component_len;
+ if (!sep) sep = ", ";
+ component_len = 15LL + strlen(sep);
+ if (count >= (INT_MAX - 1)/component_len)
+ return NULL;
+ ap = av_malloc(component_len * count + 1);
+ if (!ap)
+ return NULL;
+ ap0 = ap;
+ ap[0] = '\0';
+ for (i = 0; i < count; i++) {
+ unsigned l = snprintf(ap, component_len, "%f%s", dp[i], sep);
+ if(l >= component_len) {
+ av_free(ap0);
+ return NULL;
+ }
+ ap += l;
+ }
+ ap0[strlen(ap0) - strlen(sep)] = '\0';
+ return ap0;
+}
+
+
+char *ff_tshorts2str(int16_t *sp, int count, const char *sep)
+{
+ int i;
+ char *ap, *ap0;
+ uint64_t component_len;
+ if (!sep) sep = ", ";
+ component_len = 7LL + strlen(sep);
+ if (count >= (INT_MAX - 1)/component_len)
+ return NULL;
+ ap = av_malloc(component_len * count + 1);
+ if (!ap)
+ return NULL;
+ ap0 = ap;
+ ap[0] = '\0';
+ for (i = 0; i < count; i++) {
+ unsigned l = snprintf(ap, component_len, "%d%s", sp[i], sep);
+ if (l >= component_len) {
+ av_free(ap0);
+ return NULL;
+ }
+ ap += l;
+ }
+ ap0[strlen(ap0) - strlen(sep)] = '\0';
+ return ap0;
+}
+
+
+int ff_tadd_doubles_metadata(int count, const char *name, const char *sep,
+ GetByteContext *gb, int le, AVDictionary **metadata)
+{
+ char *ap;
+ int i;
+ double *dp;
+
+ if (count >= INT_MAX / sizeof(int64_t) || count <= 0)
+ return AVERROR_INVALIDDATA;
+ if (bytestream2_get_bytes_left(gb) < count * sizeof(int64_t))
+ return AVERROR_INVALIDDATA;
+
+ dp = av_malloc(count * sizeof(double));
+ if (!dp)
+ return AVERROR(ENOMEM);
+
+ for (i = 0; i < count; i++)
+ dp[i] = ff_tget_double(gb, le);
+ ap = ff_tdoubles2str(dp, count, sep);
+ av_freep(&dp);
+ if (!ap)
+ return AVERROR(ENOMEM);
+ av_dict_set(metadata, name, ap, AV_DICT_DONT_STRDUP_VAL);
+ return 0;
+}
+
+
+int ff_tadd_shorts_metadata(int count, const char *name, const char *sep,
+ GetByteContext *gb, int le, AVDictionary **metadata)
+{
+ char *ap;
+ int i;
+ int16_t *sp;
+
+ if (count >= INT_MAX / sizeof(int16_t) || count <= 0)
+ return AVERROR_INVALIDDATA;
+ if (bytestream2_get_bytes_left(gb) < count * sizeof(int16_t))
+ return AVERROR_INVALIDDATA;
+
+ sp = av_malloc(count * sizeof(int16_t));
+ if (!sp)
+ return AVERROR(ENOMEM);
+
+ for (i = 0; i < count; i++)
+ sp[i] = ff_tget_short(gb, le);
+ ap = ff_tshorts2str(sp, count, sep);
+ av_freep(&sp);
+ if (!ap)
+ return AVERROR(ENOMEM);
+ av_dict_set(metadata, name, ap, AV_DICT_DONT_STRDUP_VAL);
+ return 0;
+}
+
+
+int ff_tadd_string_metadata(int count, const char *name,
+ GetByteContext *gb, int le, AVDictionary **metadata)
+{
+ char *value;
+
+ 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(gb, value, count);
+ value[count] = 0;
+
+ av_dict_set(metadata, name, value, AV_DICT_DONT_STRDUP_VAL);
+ return 0;
+}
+
+
+int ff_tdecode_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 (ff_tget_short(gb, *le) != 42) {
+ return AVERROR_INVALIDDATA;
+ }
+
+ *ifd_offset = ff_tget_long(gb, *le);
+
+ return 0;
+}
+
+
+void ff_tread_tag_data(GetByteContext *gb, int le, unsigned *tag,
+ unsigned *type, unsigned *count, unsigned *offset)
+{
+ *tag = ff_tget_short(gb, le);
+ *type = ff_tget_short(gb, le);
+ *count = ff_tget_long (gb, le);
+ *offset = ff_tget_long (gb, le);
+}
+
+
+void ff_tseek_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);
+ }
+}
+
+
diff --git a/libavcodec/tiff_common.h b/libavcodec/tiff_common.h
new file mode 100644
index 0000000..952ebc1
--- /dev/null
+++ b/libavcodec/tiff_common.h
@@ -0,0 +1,113 @@
+/*
+ * TIFF Common Routines
+ * Copyright (c) 2013 Thilo Borgmann <thilo.borgmann _at_ googlemail.com>
+ *
+ * This file is part of FFmpeg.
+ *
+ * FFmpeg is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 2.1 of the License, or (at your option) any later version.
+ *
+ * FFmpeg is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with FFmpeg; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
+ */
+
+/**
+ * @file
+ * TIFF Common Routines
+ * @author Thilo Borgmann <thilo.borgmann _at_ googlemail.com>
+ */
+
+#ifndef AVCODEC_COMMON_H
+#define AVCODEC_COMMON_H
+
+#include "tiff.h"
+#include "bytestream.h"
+
+/** data type identifiers for TIFF tags */
+enum TiffTypes {
+ TIFF_BYTE = 1,
+ TIFF_STRING,
+ TIFF_SHORT,
+ TIFF_LONG,
+ TIFF_RATIONAL,
+ TIFF_SBYTE,
+ TIFF_UNDEFINED,
+ TIFF_SSHORT,
+ TIFF_SLONG,
+ TIFF_SRATIONAL,
+ TIFF_FLOAT,
+ TIFF_DOUBLE,
+ TIFF_IFD
+};
+
+/** sizes of various TIFF field types (string size = 100)*/
+static const uint8_t type_sizes[14] = {
+ 0, 1, 100, 2, 4, 8, 1, 1, 2, 4, 8, 4, 8, 4
+};
+
+/** Read a short from the bytestream using given endianess. */
+unsigned ff_tget_short(GetByteContext *gb, int le);
+
+/** Read a long from the bytestream using given endianess. */
+unsigned ff_tget_long(GetByteContext *gb, int le);
+
+/** Read a double from the bytestream using given endianess. */
+double ff_tget_double(GetByteContext *gb, int le);
+
+/** Read a byte from the bytestream using given endianess. */
+unsigned ff_tget(GetByteContext *gb, int type, int le);
+
+/** Returns an allocated string containing count
+ * double values using the given seperator.
+ */
+char *ff_tdoubles2str(double *dp, int count, const char *sep);
+
+/** Returns an allocated string containing count
+ * short values using the given seperator.
+ */
+char *ff_tshorts2str(int16_t *sp, int count, const char *sep);
+
+/** Adds count doubles converted to a string
+ * into the metadata dictionary.
+ */
+int ff_tadd_doubles_metadata(int count, const char *name, const char *sep,
+ GetByteContext *gb, int le, AVDictionary **metadata);
+
+/** Adds count shorts converted to a string
+ * into the metadata dictionary.
+ */
+int ff_tadd_shorts_metadata(int count, const char *name, const char *sep,
+ GetByteContext *gb, int le, AVDictionary **metadata);
+
+/** Adds a string of count characters
+ * into the metadata dictionary.
+ */
+int ff_tadd_string_metadata(int count, const char *name,
+ GetByteContext *gb, int le, AVDictionary **metadata);
+
+/** Decodes a TIFF header from the input bytestream
+ * and sets the endianess in *le and the offset to
+ * the first IFD in *ifd_offset accordingly.
+ */
+int ff_tdecode_header(GetByteContext *gb, int *le, int *ifd_offset);
+
+/** Reads the fixed 12-byte long TIFF tag data.
+ */
+void ff_tread_tag_data(GetByteContext *gb, int le, unsigned *tag,
+ unsigned *type, unsigned *count, unsigned *offset);
+
+/** Seeks the bytestream to the position of the
+ * array of values for the given tag data.
+ */
+void ff_tseek_tag_value(GetByteContext *gb, unsigned type,
+ unsigned count, unsigned offset);
+
+#endif /* AVCODEC_COMMON_H */
--
1.7.4.3
More information about the ffmpeg-devel
mailing list