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.

238 lines
7.0KB

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