You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

314 lines
7.9KB

  1. /*
  2. * TIFF Common Routines
  3. * Copyright (c) 2013 Thilo Borgmann <thilo.borgmann _at_ mail.de>
  4. *
  5. * This file is part of FFmpeg.
  6. *
  7. * FFmpeg is free software; you can redistribute it and/or
  8. * modify it under the terms of the GNU Lesser General Public
  9. * License as published by the Free Software Foundation; either
  10. * version 2.1 of the License, or (at your option) any later version.
  11. *
  12. * FFmpeg is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  15. * Lesser General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU Lesser General Public
  18. * License along with FFmpeg; if not, write to the Free Software
  19. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  20. */
  21. /**
  22. * @file
  23. * TIFF Common Routines
  24. * @author Thilo Borgmann <thilo.borgmann _at_ mail.de>
  25. */
  26. #include "tiff_common.h"
  27. int ff_tis_ifd(unsigned tag)
  28. {
  29. int i;
  30. for (i = 0; i < FF_ARRAY_ELEMS(ifd_tags); i++) {
  31. if (ifd_tags[i] == tag) {
  32. return i + 1;
  33. }
  34. }
  35. return 0;
  36. }
  37. unsigned ff_tget_short(GetByteContext *gb, int le)
  38. {
  39. return le ? bytestream2_get_le16(gb) : bytestream2_get_be16(gb);
  40. }
  41. unsigned ff_tget_long(GetByteContext *gb, int le)
  42. {
  43. return le ? bytestream2_get_le32(gb) : bytestream2_get_be32(gb);
  44. }
  45. double ff_tget_double(GetByteContext *gb, int le)
  46. {
  47. av_alias64 i = { .u64 = le ? bytestream2_get_le64(gb) : bytestream2_get_be64(gb)};
  48. return i.f64;
  49. }
  50. unsigned ff_tget(GetByteContext *gb, int type, int le)
  51. {
  52. switch (type) {
  53. case TIFF_BYTE: return bytestream2_get_byte(gb);
  54. case TIFF_SHORT: return ff_tget_short(gb, le);
  55. case TIFF_LONG: return ff_tget_long(gb, le);
  56. default: return UINT_MAX;
  57. }
  58. }
  59. static const char *auto_sep(int count, const char *sep, int i, int columns)
  60. {
  61. if (sep)
  62. return i ? sep : "";
  63. if (i && i%columns) {
  64. return ", ";
  65. } else
  66. return columns < count ? "\n" : "";
  67. }
  68. int ff_tadd_rational_metadata(int count, const char *name, const char *sep,
  69. GetByteContext *gb, int le, AVDictionary **metadata)
  70. {
  71. AVBPrint bp;
  72. char *ap;
  73. int32_t nom, denom;
  74. int i;
  75. if (count >= INT_MAX / sizeof(int64_t) || count <= 0)
  76. return AVERROR_INVALIDDATA;
  77. if (bytestream2_get_bytes_left(gb) < count * sizeof(int64_t))
  78. return AVERROR_INVALIDDATA;
  79. av_bprint_init(&bp, 10 * count, AV_BPRINT_SIZE_UNLIMITED);
  80. for (i = 0; i < count; i++) {
  81. nom = ff_tget_long(gb, le);
  82. denom = ff_tget_long(gb, le);
  83. av_bprintf(&bp, "%s%7"PRId32":%-7"PRId32, auto_sep(count, sep, i, 4), nom, denom);
  84. }
  85. if ((i = av_bprint_finalize(&bp, &ap))) {
  86. return i;
  87. }
  88. if (!ap) {
  89. return AVERROR(ENOMEM);
  90. }
  91. av_dict_set(metadata, name, ap, AV_DICT_DONT_STRDUP_VAL);
  92. return 0;
  93. }
  94. int ff_tadd_long_metadata(int count, const char *name, const char *sep,
  95. GetByteContext *gb, int le, AVDictionary **metadata)
  96. {
  97. AVBPrint bp;
  98. char *ap;
  99. int i;
  100. if (count >= INT_MAX / sizeof(int32_t) || count <= 0)
  101. return AVERROR_INVALIDDATA;
  102. if (bytestream2_get_bytes_left(gb) < count * sizeof(int32_t))
  103. return AVERROR_INVALIDDATA;
  104. av_bprint_init(&bp, 10 * count, AV_BPRINT_SIZE_UNLIMITED);
  105. for (i = 0; i < count; i++) {
  106. av_bprintf(&bp, "%s%7i", auto_sep(count, sep, i, 8), ff_tget_long(gb, le));
  107. }
  108. if ((i = av_bprint_finalize(&bp, &ap))) {
  109. return i;
  110. }
  111. if (!ap) {
  112. return AVERROR(ENOMEM);
  113. }
  114. av_dict_set(metadata, name, ap, AV_DICT_DONT_STRDUP_VAL);
  115. return 0;
  116. }
  117. int ff_tadd_doubles_metadata(int count, const char *name, const char *sep,
  118. GetByteContext *gb, int le, AVDictionary **metadata)
  119. {
  120. AVBPrint bp;
  121. char *ap;
  122. int i;
  123. if (count >= INT_MAX / sizeof(int64_t) || count <= 0)
  124. return AVERROR_INVALIDDATA;
  125. if (bytestream2_get_bytes_left(gb) < count * sizeof(int64_t))
  126. return AVERROR_INVALIDDATA;
  127. av_bprint_init(&bp, 10 * count, 100 * count);
  128. for (i = 0; i < count; i++) {
  129. av_bprintf(&bp, "%s%.15g", auto_sep(count, sep, i, 4), ff_tget_double(gb, le));
  130. }
  131. if ((i = av_bprint_finalize(&bp, &ap))) {
  132. return i;
  133. }
  134. if (!ap) {
  135. return AVERROR(ENOMEM);
  136. }
  137. av_dict_set(metadata, name, ap, AV_DICT_DONT_STRDUP_VAL);
  138. return 0;
  139. }
  140. int ff_tadd_shorts_metadata(int count, const char *name, const char *sep,
  141. GetByteContext *gb, int le, int is_signed, AVDictionary **metadata)
  142. {
  143. AVBPrint bp;
  144. char *ap;
  145. int i;
  146. if (count >= INT_MAX / sizeof(int16_t) || count <= 0)
  147. return AVERROR_INVALIDDATA;
  148. if (bytestream2_get_bytes_left(gb) < count * sizeof(int16_t))
  149. return AVERROR_INVALIDDATA;
  150. av_bprint_init(&bp, 10 * count, AV_BPRINT_SIZE_UNLIMITED);
  151. for (i = 0; i < count; i++) {
  152. int v = is_signed ? (int16_t)ff_tget_short(gb, le) : ff_tget_short(gb, le);
  153. av_bprintf(&bp, "%s%5i", auto_sep(count, sep, i, 8), v);
  154. }
  155. if ((i = av_bprint_finalize(&bp, &ap))) {
  156. return i;
  157. }
  158. if (!ap) {
  159. return AVERROR(ENOMEM);
  160. }
  161. av_dict_set(metadata, name, ap, AV_DICT_DONT_STRDUP_VAL);
  162. return 0;
  163. }
  164. int ff_tadd_bytes_metadata(int count, const char *name, const char *sep,
  165. GetByteContext *gb, int le, int is_signed, AVDictionary **metadata)
  166. {
  167. AVBPrint bp;
  168. char *ap;
  169. int i;
  170. if (count >= INT_MAX / sizeof(int8_t) || count < 0)
  171. return AVERROR_INVALIDDATA;
  172. if (bytestream2_get_bytes_left(gb) < count * sizeof(int8_t))
  173. return AVERROR_INVALIDDATA;
  174. av_bprint_init(&bp, 10 * count, AV_BPRINT_SIZE_UNLIMITED);
  175. for (i = 0; i < count; i++) {
  176. int v = is_signed ? (int8_t)bytestream2_get_byte(gb) : bytestream2_get_byte(gb);
  177. av_bprintf(&bp, "%s%3i", auto_sep(count, sep, i, 16), v);
  178. }
  179. if ((i = av_bprint_finalize(&bp, &ap))) {
  180. return i;
  181. }
  182. if (!ap) {
  183. return AVERROR(ENOMEM);
  184. }
  185. av_dict_set(metadata, name, ap, AV_DICT_DONT_STRDUP_VAL);
  186. return 0;
  187. }
  188. int ff_tadd_string_metadata(int count, const char *name,
  189. GetByteContext *gb, int le, AVDictionary **metadata)
  190. {
  191. char *value;
  192. if (bytestream2_get_bytes_left(gb) < count || count < 0)
  193. return AVERROR_INVALIDDATA;
  194. value = av_malloc(count + 1);
  195. if (!value)
  196. return AVERROR(ENOMEM);
  197. bytestream2_get_bufferu(gb, value, count);
  198. value[count] = 0;
  199. av_dict_set(metadata, name, value, AV_DICT_DONT_STRDUP_VAL);
  200. return 0;
  201. }
  202. int ff_tdecode_header(GetByteContext *gb, int *le, int *ifd_offset)
  203. {
  204. if (bytestream2_get_bytes_left(gb) < 8) {
  205. return AVERROR_INVALIDDATA;
  206. }
  207. *le = bytestream2_get_le16u(gb);
  208. if (*le == AV_RB16("II")) {
  209. *le = 1;
  210. } else if (*le == AV_RB16("MM")) {
  211. *le = 0;
  212. } else {
  213. return AVERROR_INVALIDDATA;
  214. }
  215. if (ff_tget_short(gb, *le) != 42) {
  216. return AVERROR_INVALIDDATA;
  217. }
  218. *ifd_offset = ff_tget_long(gb, *le);
  219. return 0;
  220. }
  221. int ff_tread_tag(GetByteContext *gb, int le, unsigned *tag, unsigned *type,
  222. unsigned *count, int *next)
  223. {
  224. int ifd_tag;
  225. int valid_type;
  226. *tag = ff_tget_short(gb, le);
  227. *type = ff_tget_short(gb, le);
  228. *count = ff_tget_long (gb, le);
  229. ifd_tag = ff_tis_ifd(*tag);
  230. valid_type = *type != 0 && *type < FF_ARRAY_ELEMS(type_sizes);
  231. *next = bytestream2_tell(gb) + 4;
  232. // check for valid type
  233. if (!valid_type) {
  234. return AVERROR_INVALIDDATA;
  235. }
  236. // seek to offset if this is an IFD-tag or
  237. // if count values do not fit into the offset value
  238. if (ifd_tag || (*count > 4 || !(type_sizes[*type] * (*count) <= 4 || *type == TIFF_STRING))) {
  239. bytestream2_seek(gb, ff_tget_long (gb, le), SEEK_SET);
  240. }
  241. return 0;
  242. }