apetag.c 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  1. /*
  2. * APE tag handling
  3. * Copyright (c) 2007 Benjamin Zores <ben@geexbox.org>
  4. * based upon libdemac from Dave Chapman.
  5. *
  6. * This file is part of FFmpeg.
  7. *
  8. * FFmpeg is free software; you can redistribute it and/or
  9. * modify it under the terms of the GNU Lesser General Public
  10. * License as published by the Free Software Foundation; either
  11. * version 2.1 of the License, or (at your option) any later version.
  12. *
  13. * FFmpeg is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  16. * Lesser General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU Lesser General Public
  19. * License along with FFmpeg; if not, write to the Free Software
  20. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  21. */
  22. #include "libavutil/intreadwrite.h"
  23. #include "libavutil/dict.h"
  24. #include "avformat.h"
  25. #include "apetag.h"
  26. #include "internal.h"
  27. #define APE_TAG_FLAG_CONTAINS_HEADER (1 << 31)
  28. #define APE_TAG_FLAG_IS_HEADER (1 << 29)
  29. #define APE_TAG_FLAG_IS_BINARY (1 << 1)
  30. static int ape_tag_read_field(AVFormatContext *s)
  31. {
  32. AVIOContext *pb = s->pb;
  33. uint8_t key[1024], *value;
  34. uint32_t size, flags;
  35. int i, c;
  36. size = avio_rl32(pb); /* field size */
  37. flags = avio_rl32(pb); /* field flags */
  38. for (i = 0; i < sizeof(key) - 1; i++) {
  39. c = avio_r8(pb);
  40. if (c < 0x20 || c > 0x7E)
  41. break;
  42. else
  43. key[i] = c;
  44. }
  45. key[i] = 0;
  46. if (c != 0) {
  47. av_log(s, AV_LOG_WARNING, "Invalid APE tag key '%s'.\n", key);
  48. return -1;
  49. }
  50. if (size >= UINT_MAX)
  51. return -1;
  52. if (flags & APE_TAG_FLAG_IS_BINARY) {
  53. uint8_t filename[1024];
  54. enum AVCodecID id;
  55. AVStream *st = avformat_new_stream(s, NULL);
  56. if (!st)
  57. return AVERROR(ENOMEM);
  58. size -= avio_get_str(pb, size, filename, sizeof(filename));
  59. if (size <= 0) {
  60. av_log(s, AV_LOG_WARNING, "Skipping binary tag '%s'.\n", key);
  61. return 0;
  62. }
  63. av_dict_set(&st->metadata, key, filename, 0);
  64. if ((id = ff_guess_image2_codec(filename)) != AV_CODEC_ID_NONE) {
  65. AVPacket pkt;
  66. int ret;
  67. ret = av_get_packet(s->pb, &pkt, size);
  68. if (ret < 0) {
  69. av_log(s, AV_LOG_ERROR, "Error reading cover art.\n");
  70. return ret;
  71. }
  72. st->disposition |= AV_DISPOSITION_ATTACHED_PIC;
  73. st->codec->codec_type = AVMEDIA_TYPE_VIDEO;
  74. st->codec->codec_id = id;
  75. st->attached_pic = pkt;
  76. st->attached_pic.stream_index = st->index;
  77. st->attached_pic.flags |= AV_PKT_FLAG_KEY;
  78. } else {
  79. st->codec->extradata = av_malloc(size + FF_INPUT_BUFFER_PADDING_SIZE);
  80. if (!st->codec->extradata)
  81. return AVERROR(ENOMEM);
  82. if (avio_read(pb, st->codec->extradata, size) != size) {
  83. av_freep(&st->codec->extradata);
  84. return AVERROR(EIO);
  85. }
  86. st->codec->extradata_size = size;
  87. st->codec->codec_type = AVMEDIA_TYPE_ATTACHMENT;
  88. }
  89. } else {
  90. value = av_malloc(size+1);
  91. if (!value)
  92. return AVERROR(ENOMEM);
  93. c = avio_read(pb, value, size);
  94. if (c < 0) {
  95. av_free(value);
  96. return c;
  97. }
  98. value[c] = 0;
  99. av_dict_set(&s->metadata, key, value, AV_DICT_DONT_STRDUP_VAL);
  100. }
  101. return 0;
  102. }
  103. int64_t ff_ape_parse_tag(AVFormatContext *s)
  104. {
  105. AVIOContext *pb = s->pb;
  106. int file_size = avio_size(pb);
  107. uint32_t val, fields, tag_bytes;
  108. uint8_t buf[8];
  109. int64_t tag_start;
  110. int i;
  111. if (file_size < APE_TAG_FOOTER_BYTES)
  112. return 0;
  113. avio_seek(pb, file_size - APE_TAG_FOOTER_BYTES, SEEK_SET);
  114. avio_read(pb, buf, 8); /* APETAGEX */
  115. if (strncmp(buf, APE_TAG_PREAMBLE, 8)) {
  116. return 0;
  117. }
  118. val = avio_rl32(pb); /* APE tag version */
  119. if (val > APE_TAG_VERSION) {
  120. av_log(s, AV_LOG_ERROR, "Unsupported tag version. (>=%d)\n", APE_TAG_VERSION);
  121. return 0;
  122. }
  123. tag_bytes = avio_rl32(pb); /* tag size */
  124. if (tag_bytes - APE_TAG_FOOTER_BYTES > (1024 * 1024 * 16)) {
  125. av_log(s, AV_LOG_ERROR, "Tag size is way too big\n");
  126. return 0;
  127. }
  128. tag_start = file_size - tag_bytes - APE_TAG_FOOTER_BYTES;
  129. if (tag_start < 0) {
  130. av_log(s, AV_LOG_ERROR, "Invalid tag size %u.\n", tag_bytes);
  131. return 0;
  132. }
  133. fields = avio_rl32(pb); /* number of fields */
  134. if (fields > 65536) {
  135. av_log(s, AV_LOG_ERROR, "Too many tag fields (%d)\n", fields);
  136. return 0;
  137. }
  138. val = avio_rl32(pb); /* flags */
  139. if (val & APE_TAG_FLAG_IS_HEADER) {
  140. av_log(s, AV_LOG_ERROR, "APE Tag is a header\n");
  141. return 0;
  142. }
  143. avio_seek(pb, file_size - tag_bytes, SEEK_SET);
  144. for (i=0; i<fields; i++)
  145. if (ape_tag_read_field(s) < 0) break;
  146. return tag_start;
  147. }