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.

318 lines
7.8KB

  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. unsigned v = le ? bytestream2_get_le16(gb) : bytestream2_get_be16(gb);
  40. return v;
  41. }
  42. unsigned ff_tget_long(GetByteContext *gb, int le)
  43. {
  44. unsigned v = le ? bytestream2_get_le32(gb) : bytestream2_get_be32(gb);
  45. return v;
  46. }
  47. double ff_tget_double(GetByteContext *gb, int le)
  48. {
  49. av_alias64 i = { .u64 = le ? bytestream2_get_le64(gb) : bytestream2_get_be64(gb)};
  50. return i.f64;
  51. }
  52. unsigned ff_tget(GetByteContext *gb, int type, int le)
  53. {
  54. switch (type) {
  55. case TIFF_BYTE:
  56. return bytestream2_get_byte(gb);
  57. case TIFF_SHORT:
  58. return ff_tget_short(gb, le);
  59. case TIFF_LONG:
  60. return ff_tget_long(gb, le);
  61. default:
  62. return UINT_MAX;
  63. }
  64. }
  65. static const char *auto_sep(int count, const char *sep, int i, int columns)
  66. {
  67. if (sep)
  68. return i ? sep : "";
  69. if (i && i%columns) {
  70. return ", ";
  71. } else
  72. return columns < count ? "\n" : "";
  73. }
  74. int ff_tadd_rational_metadata(int count, const char *name, const char *sep,
  75. GetByteContext *gb, int le, AVDictionary **metadata)
  76. {
  77. AVBPrint bp;
  78. char *ap;
  79. int32_t nom, denom;
  80. int i;
  81. if (count >= INT_MAX / sizeof(int64_t) || count <= 0)
  82. return AVERROR_INVALIDDATA;
  83. if (bytestream2_get_bytes_left(gb) < count * sizeof(int64_t))
  84. return AVERROR_INVALIDDATA;
  85. av_bprint_init(&bp, 10 * count, AV_BPRINT_SIZE_UNLIMITED);
  86. for (i = 0; i < count; i++) {
  87. nom = ff_tget_long(gb, le);
  88. denom = ff_tget_long(gb, le);
  89. av_bprintf(&bp, "%s%7i:%-7i", auto_sep(count, sep, i, 4), nom, denom);
  90. }
  91. if ((i = av_bprint_finalize(&bp, &ap))) {
  92. return i;
  93. }
  94. if (!ap) {
  95. return AVERROR(ENOMEM);
  96. }
  97. av_dict_set(metadata, name, ap, AV_DICT_DONT_STRDUP_VAL);
  98. return 0;
  99. }
  100. int ff_tadd_long_metadata(int count, const char *name, const char *sep,
  101. GetByteContext *gb, int le, AVDictionary **metadata)
  102. {
  103. AVBPrint bp;
  104. char *ap;
  105. int i;
  106. if (count >= INT_MAX / sizeof(int32_t) || count <= 0)
  107. return AVERROR_INVALIDDATA;
  108. if (bytestream2_get_bytes_left(gb) < count * sizeof(int32_t))
  109. return AVERROR_INVALIDDATA;
  110. av_bprint_init(&bp, 10 * count, AV_BPRINT_SIZE_UNLIMITED);
  111. for (i = 0; i < count; i++) {
  112. av_bprintf(&bp, "%s%7i", auto_sep(count, sep, i, 8), ff_tget_long(gb, le));
  113. }
  114. if ((i = av_bprint_finalize(&bp, &ap))) {
  115. return i;
  116. }
  117. if (!ap) {
  118. return AVERROR(ENOMEM);
  119. }
  120. av_dict_set(metadata, name, ap, AV_DICT_DONT_STRDUP_VAL);
  121. return 0;
  122. }
  123. int ff_tadd_doubles_metadata(int count, const char *name, const char *sep,
  124. GetByteContext *gb, int le, AVDictionary **metadata)
  125. {
  126. AVBPrint bp;
  127. char *ap;
  128. int i;
  129. if (count >= INT_MAX / sizeof(int64_t) || count <= 0)
  130. return AVERROR_INVALIDDATA;
  131. if (bytestream2_get_bytes_left(gb) < count * sizeof(int64_t))
  132. return AVERROR_INVALIDDATA;
  133. av_bprint_init(&bp, 10 * count, 100 * count);
  134. for (i = 0; i < count; i++) {
  135. av_bprintf(&bp, "%s%f", auto_sep(count, sep, i, 4), ff_tget_double(gb, le));
  136. }
  137. if ((i = av_bprint_finalize(&bp, &ap))) {
  138. return i;
  139. }
  140. if (!ap) {
  141. return AVERROR(ENOMEM);
  142. }
  143. av_dict_set(metadata, name, ap, AV_DICT_DONT_STRDUP_VAL);
  144. return 0;
  145. }
  146. int ff_tadd_shorts_metadata(int count, const char *name, const char *sep,
  147. GetByteContext *gb, int le, AVDictionary **metadata)
  148. {
  149. AVBPrint bp;
  150. char *ap;
  151. int i;
  152. if (count >= INT_MAX / sizeof(int16_t) || count <= 0)
  153. return AVERROR_INVALIDDATA;
  154. if (bytestream2_get_bytes_left(gb) < count * sizeof(int16_t))
  155. return AVERROR_INVALIDDATA;
  156. av_bprint_init(&bp, 10 * count, AV_BPRINT_SIZE_UNLIMITED);
  157. for (i = 0; i < count; i++) {
  158. av_bprintf(&bp, "%s%5i", auto_sep(count, sep, i, 8), ff_tget_short(gb, le));
  159. }
  160. if ((i = av_bprint_finalize(&bp, &ap))) {
  161. return i;
  162. }
  163. if (!ap) {
  164. return AVERROR(ENOMEM);
  165. }
  166. av_dict_set(metadata, name, ap, AV_DICT_DONT_STRDUP_VAL);
  167. return 0;
  168. }
  169. int ff_tadd_bytes_metadata(int count, const char *name, const char *sep,
  170. GetByteContext *gb, int le, AVDictionary **metadata)
  171. {
  172. AVBPrint bp;
  173. char *ap;
  174. int i;
  175. if (count >= INT_MAX / sizeof(int8_t) || count < 0)
  176. return AVERROR_INVALIDDATA;
  177. if (bytestream2_get_bytes_left(gb) < count * sizeof(int8_t))
  178. return AVERROR_INVALIDDATA;
  179. av_bprint_init(&bp, 10 * count, AV_BPRINT_SIZE_UNLIMITED);
  180. for (i = 0; i < count; i++) {
  181. av_bprintf(&bp, "%s%3i", auto_sep(count, sep, i, 16), bytestream2_get_byte(gb));
  182. }
  183. if ((i = av_bprint_finalize(&bp, &ap))) {
  184. return i;
  185. }
  186. if (!ap) {
  187. return AVERROR(ENOMEM);
  188. }
  189. av_dict_set(metadata, name, ap, AV_DICT_DONT_STRDUP_VAL);
  190. return 0;
  191. }
  192. int ff_tadd_string_metadata(int count, const char *name,
  193. GetByteContext *gb, int le, AVDictionary **metadata)
  194. {
  195. char *value;
  196. if (bytestream2_get_bytes_left(gb) < count || count < 0)
  197. return AVERROR_INVALIDDATA;
  198. value = av_malloc(count + 1);
  199. if (!value)
  200. return AVERROR(ENOMEM);
  201. bytestream2_get_bufferu(gb, value, count);
  202. value[count] = 0;
  203. av_dict_set(metadata, name, value, AV_DICT_DONT_STRDUP_VAL);
  204. return 0;
  205. }
  206. int ff_tdecode_header(GetByteContext *gb, int *le, int *ifd_offset)
  207. {
  208. if (bytestream2_get_bytes_left(gb) < 8) {
  209. return AVERROR_INVALIDDATA;
  210. }
  211. *le = bytestream2_get_le16u(gb);
  212. if (*le == AV_RB16("II")) {
  213. *le = 1;
  214. } else if (*le == AV_RB16("MM")) {
  215. *le = 0;
  216. } else {
  217. return AVERROR_INVALIDDATA;
  218. }
  219. if (ff_tget_short(gb, *le) != 42) {
  220. return AVERROR_INVALIDDATA;
  221. }
  222. *ifd_offset = ff_tget_long(gb, *le);
  223. return 0;
  224. }
  225. int ff_tread_tag(GetByteContext *gb, int le, unsigned *tag, unsigned *type,
  226. unsigned *count, int *next)
  227. {
  228. int ifd_tag;
  229. int valid_type;
  230. *tag = ff_tget_short(gb, le);
  231. *type = ff_tget_short(gb, le);
  232. *count = ff_tget_long (gb, le);
  233. ifd_tag = ff_tis_ifd(*tag);
  234. valid_type = *type != 0 && *type < FF_ARRAY_ELEMS(type_sizes);
  235. *next = bytestream2_tell(gb) + 4;
  236. // check for valid type
  237. if (!valid_type) {
  238. return AVERROR_INVALIDDATA;
  239. }
  240. // seek to offset if this is an IFD-tag or
  241. // if count values do not fit into the offset value
  242. if (ifd_tag || (*count > 4 || !(type_sizes[*type] * (*count) <= 4 || *type == TIFF_STRING))) {
  243. bytestream2_seek(gb, ff_tget_long (gb, le), SEEK_SET);
  244. }
  245. return 0;
  246. }