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.

230 lines
6.9KB

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