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.

131 lines
3.8KB

  1. /*
  2. * EXIF metadata parser
  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. * EXIF metadata parser
  24. * @author Thilo Borgmann <thilo.borgmann _at_ mail.de>
  25. */
  26. #include "exif.h"
  27. static const char *exif_get_tag_name(uint16_t id)
  28. {
  29. int i;
  30. for (i = 0; i < FF_ARRAY_ELEMS(tag_list); i++) {
  31. if (tag_list[i].id == id)
  32. return tag_list[i].name;
  33. }
  34. return NULL;
  35. }
  36. static int exif_add_metadata(AVCodecContext *avctx, int count, int type,
  37. const char *name, const char *sep,
  38. GetByteContext *gb, int le,
  39. AVDictionary **metadata)
  40. {
  41. switch(type) {
  42. case TIFF_DOUBLE : return ff_tadd_doubles_metadata(count, name, sep, gb, le, metadata);
  43. case TIFF_SHORT : return ff_tadd_shorts_metadata(count, name, sep, gb, le, metadata);
  44. case TIFF_BYTE :
  45. case TIFF_UNDEFINED: return ff_tadd_bytes_metadata(count, name, sep, gb, le, metadata);
  46. case TIFF_STRING : return ff_tadd_string_metadata(count, name, gb, le, metadata);
  47. case TIFF_SRATIONAL:
  48. case TIFF_RATIONAL : return ff_tadd_rational_metadata(count, name, sep, gb, le, metadata);
  49. case TIFF_SLONG :
  50. case TIFF_LONG : return ff_tadd_long_metadata(count, name, sep, gb, le, metadata);
  51. default:
  52. avpriv_request_sample(avctx, "TIFF tag type (%u)", type);
  53. return 0;
  54. };
  55. }
  56. static int exif_decode_tag(AVCodecContext *avctx, GetByteContext *gbytes, int le,
  57. int depth, AVDictionary **metadata)
  58. {
  59. int ret, cur_pos;
  60. unsigned id, count;
  61. enum TiffTypes type;
  62. if (depth > 2) {
  63. return 0;
  64. }
  65. ff_tread_tag(gbytes, le, &id, &type, &count, &cur_pos);
  66. // read count values and add it metadata
  67. // store metadata or proceed with next IFD
  68. ret = ff_tis_ifd(id);
  69. if (ret) {
  70. ret = avpriv_exif_decode_ifd(avctx, gbytes, le, depth + 1, metadata);
  71. } else {
  72. const char *name = exif_get_tag_name(id);
  73. char *use_name = (char*) name;
  74. if (!use_name) {
  75. use_name = av_malloc(7);
  76. if (!use_name) {
  77. return AVERROR(ENOMEM);
  78. }
  79. snprintf(use_name, 7, "0x%04X", id);
  80. }
  81. ret = exif_add_metadata(avctx, count, type, use_name, NULL,
  82. gbytes, le, metadata);
  83. if (!name) {
  84. av_freep(&use_name);
  85. }
  86. }
  87. bytestream2_seek(gbytes, cur_pos, SEEK_SET);
  88. return ret;
  89. }
  90. int avpriv_exif_decode_ifd(AVCodecContext *avctx, GetByteContext *gbytes, int le,
  91. int depth, AVDictionary **metadata)
  92. {
  93. int i, ret;
  94. int entries;
  95. entries = ff_tget_short(gbytes, le);
  96. if (bytestream2_get_bytes_left(gbytes) < entries * 12) {
  97. return AVERROR_INVALIDDATA;
  98. }
  99. for (i = 0; i < entries; i++) {
  100. if ((ret = exif_decode_tag(avctx, gbytes, le, depth, metadata)) < 0) {
  101. return ret;
  102. }
  103. }
  104. // return next IDF offset or 0x000000000 or a value < 0 for failure
  105. return ff_tget_long(gbytes, le);
  106. }