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.

228 lines
6.8KB

  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 Libav.
  7. *
  8. * Libav 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. * Libav 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 Libav; 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 "avio_internal.h"
  26. #include "apetag.h"
  27. #include "internal.h"
  28. #define APE_TAG_VERSION 2000
  29. #define APE_TAG_FOOTER_BYTES 32
  30. #define APE_TAG_FLAG_CONTAINS_HEADER (1 << 31)
  31. #define APE_TAG_FLAG_CONTAINS_FOOTER (1 << 30)
  32. #define APE_TAG_FLAG_IS_HEADER (1 << 29)
  33. #define APE_TAG_FLAG_IS_BINARY (1 << 1)
  34. static int ape_tag_read_field(AVFormatContext *s)
  35. {
  36. AVIOContext *pb = s->pb;
  37. uint8_t key[1024], *value;
  38. uint32_t size, flags;
  39. int i, c;
  40. size = avio_rl32(pb); /* field size */
  41. flags = avio_rl32(pb); /* field flags */
  42. for (i = 0; i < sizeof(key) - 1; i++) {
  43. c = avio_r8(pb);
  44. if (c < 0x20 || c > 0x7E)
  45. break;
  46. else
  47. key[i] = c;
  48. }
  49. key[i] = 0;
  50. if (c != 0) {
  51. av_log(s, AV_LOG_WARNING, "Invalid APE tag key '%s'.\n", key);
  52. return -1;
  53. }
  54. if (size >= UINT_MAX)
  55. return -1;
  56. if (flags & APE_TAG_FLAG_IS_BINARY) {
  57. uint8_t filename[1024];
  58. enum AVCodecID id;
  59. AVStream *st = avformat_new_stream(s, NULL);
  60. if (!st)
  61. return AVERROR(ENOMEM);
  62. size -= avio_get_str(pb, size, filename, sizeof(filename));
  63. if (size <= 0) {
  64. av_log(s, AV_LOG_WARNING, "Skipping binary tag '%s'.\n", key);
  65. return 0;
  66. }
  67. av_dict_set(&st->metadata, key, filename, 0);
  68. if ((id = ff_guess_image2_codec(filename)) != AV_CODEC_ID_NONE) {
  69. AVPacket pkt;
  70. int ret;
  71. ret = av_get_packet(s->pb, &pkt, size);
  72. if (ret < 0) {
  73. av_log(s, AV_LOG_ERROR, "Error reading cover art.\n");
  74. return ret;
  75. }
  76. st->disposition |= AV_DISPOSITION_ATTACHED_PIC;
  77. st->codec->codec_type = AVMEDIA_TYPE_VIDEO;
  78. st->codec->codec_id = id;
  79. st->attached_pic = pkt;
  80. st->attached_pic.stream_index = st->index;
  81. st->attached_pic.flags |= AV_PKT_FLAG_KEY;
  82. } else {
  83. st->codec->extradata = av_malloc(size + FF_INPUT_BUFFER_PADDING_SIZE);
  84. if (!st->codec->extradata)
  85. return AVERROR(ENOMEM);
  86. if (avio_read(pb, st->codec->extradata, size) != size) {
  87. av_freep(&st->codec->extradata);
  88. return AVERROR(EIO);
  89. }
  90. st->codec->extradata_size = size;
  91. st->codec->codec_type = AVMEDIA_TYPE_ATTACHMENT;
  92. }
  93. } else {
  94. value = av_malloc(size+1);
  95. if (!value)
  96. return AVERROR(ENOMEM);
  97. c = avio_read(pb, value, size);
  98. if (c < 0) {
  99. av_free(value);
  100. return c;
  101. }
  102. value[c] = 0;
  103. av_dict_set(&s->metadata, key, value, AV_DICT_DONT_STRDUP_VAL);
  104. }
  105. return 0;
  106. }
  107. int64_t ff_ape_parse_tag(AVFormatContext *s)
  108. {
  109. AVIOContext *pb = s->pb;
  110. int64_t file_size = avio_size(pb);
  111. uint32_t val, fields, tag_bytes;
  112. uint8_t buf[8];
  113. int64_t tag_start;
  114. int i;
  115. if (file_size < APE_TAG_FOOTER_BYTES)
  116. return 0;
  117. avio_seek(pb, file_size - APE_TAG_FOOTER_BYTES, SEEK_SET);
  118. avio_read(pb, buf, 8); /* APETAGEX */
  119. if (strncmp(buf, "APETAGEX", 8)) {
  120. return 0;
  121. }
  122. val = avio_rl32(pb); /* APE tag version */
  123. if (val > APE_TAG_VERSION) {
  124. av_log(s, AV_LOG_ERROR, "Unsupported tag version. (>=%d)\n", APE_TAG_VERSION);
  125. return 0;
  126. }
  127. tag_bytes = avio_rl32(pb); /* tag size */
  128. if (tag_bytes - APE_TAG_FOOTER_BYTES > (1024 * 1024 * 16)) {
  129. av_log(s, AV_LOG_ERROR, "Tag size is way too big\n");
  130. return 0;
  131. }
  132. if (tag_bytes > file_size - APE_TAG_FOOTER_BYTES) {
  133. av_log(s, AV_LOG_ERROR, "Invalid tag size %u.\n", tag_bytes);
  134. return 0;
  135. }
  136. tag_start = file_size - tag_bytes - APE_TAG_FOOTER_BYTES;
  137. fields = avio_rl32(pb); /* number of fields */
  138. if (fields > 65536) {
  139. av_log(s, AV_LOG_ERROR, "Too many tag fields (%d)\n", fields);
  140. return 0;
  141. }
  142. val = avio_rl32(pb); /* flags */
  143. if (val & APE_TAG_FLAG_IS_HEADER) {
  144. av_log(s, AV_LOG_ERROR, "APE Tag is a header\n");
  145. return 0;
  146. }
  147. avio_seek(pb, file_size - tag_bytes, SEEK_SET);
  148. for (i=0; i<fields; i++)
  149. if (ape_tag_read_field(s) < 0) break;
  150. return tag_start;
  151. }
  152. int ff_ape_write_tag(AVFormatContext *s)
  153. {
  154. AVDictionaryEntry *e = NULL;
  155. int64_t start, end;
  156. int size, count = 0;
  157. if (!s->pb->seekable)
  158. return 0;
  159. start = avio_tell(s->pb);
  160. // header
  161. avio_write(s->pb, "APETAGEX", 8); // id
  162. avio_wl32 (s->pb, APE_TAG_VERSION); // version
  163. avio_wl32(s->pb, 0); // reserve space for size
  164. avio_wl32(s->pb, 0); // reserve space for tag count
  165. // flags
  166. avio_wl32(s->pb, APE_TAG_FLAG_CONTAINS_HEADER | APE_TAG_FLAG_CONTAINS_FOOTER |
  167. APE_TAG_FLAG_IS_HEADER);
  168. ffio_fill(s->pb, 0, 8); // reserved
  169. while ((e = av_dict_get(s->metadata, "", e, AV_DICT_IGNORE_SUFFIX))) {
  170. int val_len = strlen(e->value);
  171. avio_wl32(s->pb, val_len); // value length
  172. avio_wl32(s->pb, 0); // item flags
  173. avio_put_str(s->pb, e->key); // key
  174. avio_write(s->pb, e->value, val_len); // value
  175. count++;
  176. }
  177. size = avio_tell(s->pb) - start;
  178. // footer
  179. avio_write(s->pb, "APETAGEX", 8); // id
  180. avio_wl32 (s->pb, APE_TAG_VERSION); // version
  181. avio_wl32(s->pb, size); // size
  182. avio_wl32(s->pb, count); // tag count
  183. // flags
  184. avio_wl32(s->pb, APE_TAG_FLAG_CONTAINS_HEADER | APE_TAG_FLAG_CONTAINS_FOOTER);
  185. ffio_fill(s->pb, 0, 8); // reserved
  186. // update values in the header
  187. end = avio_tell(s->pb);
  188. avio_seek(s->pb, start + 12, SEEK_SET);
  189. avio_wl32(s->pb, size);
  190. avio_wl32(s->pb, count);
  191. avio_seek(s->pb, end, SEEK_SET);
  192. return 0;
  193. }