mirror of https://github.com/FFmpeg/FFmpeg.git
Signed-off-by: Michael Niedermayer <michaelni@gmx.at>pull/37/head
parent
4b101ab02e
commit
b7ba7cbd6e
5 changed files with 457 additions and 203 deletions
@ -0,0 +1,282 @@ |
||||
/*
|
||||
* 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" |
||||
|
||||
|
||||
int ff_tis_ifd(unsigned tag) |
||||
{ |
||||
int i; |
||||
for (i = 0; i < FF_ARRAY_ELEMS(ifd_tags); i++) { |
||||
if (ifd_tags[i] == tag) { |
||||
return i + 1; |
||||
} |
||||
} |
||||
return 0; |
||||
} |
||||
|
||||
|
||||
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; |
||||
} |
||||
} |
||||
|
||||
|
||||
int ff_tadd_rational_metadata(int count, const char *name, const char *sep, |
||||
GetByteContext *gb, int le, AVDictionary **metadata) |
||||
{ |
||||
AVBPrint bp; |
||||
char *ap; |
||||
int32_t nom, denom; |
||||
int i; |
||||
|
||||
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; |
||||
if (!sep) sep = ", "; |
||||
|
||||
av_bprint_init(&bp, 10 * count, AV_BPRINT_SIZE_AUTOMATIC); |
||||
|
||||
for (i = 0; i < count; i++) { |
||||
nom = ff_tget_long(gb, le); |
||||
denom = ff_tget_long(gb, le); |
||||
av_bprintf(&bp, "%s%i:%i", (i ? sep : ""), nom, denom); |
||||
} |
||||
|
||||
if ((i = av_bprint_finalize(&bp, &ap))) { |
||||
return i; |
||||
} |
||||
if (!ap) { |
||||
return AVERROR(ENOMEM); |
||||
} |
||||
|
||||
av_dict_set(metadata, name, ap, AV_DICT_DONT_STRDUP_VAL); |
||||
|
||||
return 0; |
||||
} |
||||
|
||||
|
||||
int ff_tadd_long_metadata(int count, const char *name, const char *sep, |
||||
GetByteContext *gb, int le, AVDictionary **metadata) |
||||
{ |
||||
AVBPrint bp; |
||||
char *ap; |
||||
int i; |
||||
|
||||
if (count >= INT_MAX / sizeof(int32_t) || count <= 0) |
||||
return AVERROR_INVALIDDATA; |
||||
if (bytestream2_get_bytes_left(gb) < count * sizeof(int32_t)) |
||||
return AVERROR_INVALIDDATA; |
||||
if (!sep) sep = ", "; |
||||
|
||||
av_bprint_init(&bp, 10 * count, AV_BPRINT_SIZE_AUTOMATIC); |
||||
|
||||
for (i = 0; i < count; i++) { |
||||
av_bprintf(&bp, "%s%i", (i ? sep : ""), ff_tget_long(gb, le)); |
||||
} |
||||
|
||||
if ((i = av_bprint_finalize(&bp, &ap))) { |
||||
return i; |
||||
} |
||||
if (!ap) { |
||||
return AVERROR(ENOMEM); |
||||
} |
||||
|
||||
av_dict_set(metadata, name, ap, AV_DICT_DONT_STRDUP_VAL); |
||||
|
||||
return 0; |
||||
} |
||||
|
||||
|
||||
int ff_tadd_doubles_metadata(int count, const char *name, const char *sep, |
||||
GetByteContext *gb, int le, AVDictionary **metadata) |
||||
{ |
||||
AVBPrint bp; |
||||
char *ap; |
||||
int i; |
||||
|
||||
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; |
||||
if (!sep) sep = ", "; |
||||
|
||||
av_bprint_init(&bp, 10 * count, AV_BPRINT_SIZE_AUTOMATIC); |
||||
|
||||
for (i = 0; i < count; i++) { |
||||
av_bprintf(&bp, "%s%f", (i ? sep : ""), ff_tget_double(gb, le)); |
||||
} |
||||
|
||||
if ((i = av_bprint_finalize(&bp, &ap))) { |
||||
return i; |
||||
} |
||||
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) |
||||
{ |
||||
AVBPrint bp; |
||||
char *ap; |
||||
int i; |
||||
|
||||
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; |
||||
if (!sep) sep = ", "; |
||||
|
||||
av_bprint_init(&bp, 10 * count, AV_BPRINT_SIZE_AUTOMATIC); |
||||
|
||||
for (i = 0; i < count; i++) { |
||||
av_bprintf(&bp, "%s%i", (i ? sep : ""), ff_tget_short(gb, le)); |
||||
} |
||||
|
||||
if ((i = av_bprint_finalize(&bp, &ap))) { |
||||
return i; |
||||
} |
||||
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; |
||||
} |
||||
|
||||
|
||||
int ff_tread_tag(GetByteContext *gb, int le, unsigned *tag, unsigned *type, |
||||
unsigned *count, int *next) |
||||
{ |
||||
int ifd_tag; |
||||
int valid_type; |
||||
|
||||
*tag = ff_tget_short(gb, le); |
||||
*type = ff_tget_short(gb, le); |
||||
*count = ff_tget_long (gb, le); |
||||
|
||||
ifd_tag = ff_tis_ifd(*tag); |
||||
valid_type = *type != 0 && *type < FF_ARRAY_ELEMS(type_sizes); |
||||
|
||||
*next = bytestream2_tell(gb) + 4; |
||||
|
||||
// check for valid type
|
||||
if (!valid_type) { |
||||
return AVERROR_INVALIDDATA; |
||||
} |
||||
|
||||
// seek to offset if this is an IFD-tag or
|
||||
// if count values do not fit into the offset value
|
||||
if (ifd_tag || (*count > 4 || !(type_sizes[*type] * (*count) <= 4 || *type == TIFF_STRING))) { |
||||
bytestream2_seek(gb, ff_tget_long (gb, le), SEEK_SET); |
||||
} |
||||
|
||||
return 0; |
||||
} |
@ -0,0 +1,146 @@ |
||||
/*
|
||||
* 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_TIFF_COMMON_H |
||||
#define AVCODEC_TIFF_COMMON_H |
||||
|
||||
#include "avcodec.h" |
||||
#include "tiff.h" |
||||
#include "bytestream.h" |
||||
#include "libavutil/bprint.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 |
||||
}; |
||||
|
||||
static const uint16_t ifd_tags[] = { |
||||
0x8769, // EXIF IFD
|
||||
0x8825, // GPS IFD
|
||||
0xA005 // Interoperability IFD
|
||||
}; |
||||
|
||||
|
||||
/** Returns a value > 0 if the tag is a known IFD-tag.
|
||||
* The return value is the array index + 1 within ifd_tags[]. |
||||
*/ |
||||
int ff_tis_ifd(unsigned tag); |
||||
|
||||
/** Reads a short from the bytestream using given endianess. */ |
||||
unsigned ff_tget_short(GetByteContext *gb, int le); |
||||
|
||||
/** Reads a long from the bytestream using given endianess. */ |
||||
unsigned ff_tget_long(GetByteContext *gb, int le); |
||||
|
||||
/** Reads a double from the bytestream using given endianess. */ |
||||
double ff_tget_double(GetByteContext *gb, int le); |
||||
|
||||
/** Reads a byte from the bytestream using given endianess. */ |
||||
unsigned ff_tget(GetByteContext *gb, int type, int le); |
||||
|
||||
/** Returns an allocated string containing count
|
||||
* rational values using the given seperator. |
||||
*/ |
||||
char *ff_trationals2str(int *rp, int count, const char *sep); |
||||
|
||||
/** Returns an allocated string containing count
|
||||
* long values using the given seperator. |
||||
*/ |
||||
char *ff_tlongs2str(int32_t *lp, int count, const char *sep); |
||||
|
||||
/** 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 rationals converted to a string
|
||||
* into the metadata dictionary. |
||||
*/ |
||||
int ff_tadd_rational_metadata(int count, const char *name, const char *sep, |
||||
GetByteContext *gb, int le, AVDictionary **metadata); |
||||
|
||||
/** Adds count longs converted to a string
|
||||
* into the metadata dictionary. |
||||
*/ |
||||
int ff_tadd_long_metadata(int count, const char *name, const char *sep, |
||||
GetByteContext *gb, int le, AVDictionary **metadata); |
||||
|
||||
/** 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 first 3 fields of a TIFF tag, which are
|
||||
* the tag id, the tag type and the count of values for that tag. |
||||
* Afterwards the bytestream is located at the first value to read and |
||||
* *next holds the bytestream offset of the following tag. |
||||
*/ |
||||
int ff_tread_tag(GetByteContext *gb, int le, unsigned *tag, unsigned *type, |
||||
unsigned *count, int *next); |
||||
|
||||
#endif /* AVCODEC_TIFF_COMMON_H */ |
Loading…
Reference in new issue