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.

241 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. if (ff_alloc_extradata(st->codec, size))
  82. return AVERROR(ENOMEM);
  83. if (avio_read(pb, st->codec->extradata, size) != size) {
  84. av_freep(&st->codec->extradata);
  85. st->codec->extradata_size = 0;
  86. return AVERROR(EIO);
  87. }
  88. st->codec->codec_type = AVMEDIA_TYPE_ATTACHMENT;
  89. }
  90. } else {
  91. value = av_malloc(size+1);
  92. if (!value)
  93. return AVERROR(ENOMEM);
  94. c = avio_read(pb, value, size);
  95. if (c < 0) {
  96. av_free(value);
  97. return c;
  98. }
  99. value[c] = 0;
  100. av_dict_set(&s->metadata, key, value, AV_DICT_DONT_STRDUP_VAL);
  101. }
  102. return 0;
  103. }
  104. int64_t ff_ape_parse_tag(AVFormatContext *s)
  105. {
  106. AVIOContext *pb = s->pb;
  107. int64_t file_size = avio_size(pb);
  108. uint32_t val, fields, tag_bytes;
  109. uint8_t buf[8];
  110. int64_t tag_start;
  111. int i;
  112. if (file_size < APE_TAG_FOOTER_BYTES)
  113. return 0;
  114. avio_seek(pb, file_size - APE_TAG_FOOTER_BYTES, SEEK_SET);
  115. avio_read(pb, buf, 8); /* APETAGEX */
  116. if (strncmp(buf, APE_TAG_PREAMBLE, 8)) {
  117. return 0;
  118. }
  119. val = avio_rl32(pb); /* APE tag version */
  120. if (val > APE_TAG_VERSION) {
  121. av_log(s, AV_LOG_ERROR, "Unsupported tag version. (>=%d)\n", APE_TAG_VERSION);
  122. return 0;
  123. }
  124. tag_bytes = avio_rl32(pb); /* tag size */
  125. if (tag_bytes - APE_TAG_FOOTER_BYTES > (1024 * 1024 * 16)) {
  126. av_log(s, AV_LOG_ERROR, "Tag size is way too big\n");
  127. return 0;
  128. }
  129. if (tag_bytes > file_size - APE_TAG_FOOTER_BYTES) {
  130. av_log(s, AV_LOG_ERROR, "Invalid tag size %u.\n", tag_bytes);
  131. return 0;
  132. }
  133. tag_start = file_size - tag_bytes - APE_TAG_FOOTER_BYTES;
  134. fields = avio_rl32(pb); /* number of fields */
  135. if (fields > 65536) {
  136. av_log(s, AV_LOG_ERROR, "Too many tag fields (%d)\n", fields);
  137. return 0;
  138. }
  139. val = avio_rl32(pb); /* flags */
  140. if (val & APE_TAG_FLAG_IS_HEADER) {
  141. av_log(s, AV_LOG_ERROR, "APE Tag is a header\n");
  142. return 0;
  143. }
  144. avio_seek(pb, file_size - tag_bytes, SEEK_SET);
  145. for (i=0; i<fields; i++)
  146. if (ape_tag_read_field(s) < 0) break;
  147. return tag_start;
  148. }
  149. static int string_is_ascii(const uint8_t *str)
  150. {
  151. while (*str && *str >= 0x20 && *str <= 0x7e ) str++;
  152. return !*str;
  153. }
  154. int ff_ape_write_tag(AVFormatContext *s)
  155. {
  156. AVDictionaryEntry *e = NULL;
  157. int size, ret, count = 0;
  158. AVIOContext *dyn_bc = NULL;
  159. uint8_t *dyn_buf = NULL;
  160. if ((ret = avio_open_dyn_buf(&dyn_bc)) < 0)
  161. goto end;
  162. // flags
  163. avio_wl32(dyn_bc, APE_TAG_FLAG_CONTAINS_HEADER | APE_TAG_FLAG_CONTAINS_FOOTER |
  164. APE_TAG_FLAG_IS_HEADER);
  165. ffio_fill(dyn_bc, 0, 8); // reserved
  166. while ((e = av_dict_get(s->metadata, "", e, AV_DICT_IGNORE_SUFFIX))) {
  167. int val_len;
  168. if (!string_is_ascii(e->key)) {
  169. av_log(s, AV_LOG_WARNING, "Non ASCII keys are not allowed\n");
  170. continue;
  171. }
  172. val_len = strlen(e->value);
  173. avio_wl32(dyn_bc, val_len); // value length
  174. avio_wl32(dyn_bc, 0); // item flags
  175. avio_put_str(dyn_bc, e->key); // key
  176. avio_write(dyn_bc, e->value, val_len); // value
  177. count++;
  178. }
  179. if (!count)
  180. goto end;
  181. size = avio_close_dyn_buf(dyn_bc, &dyn_buf);
  182. if (size <= 0)
  183. goto end;
  184. size += 20;
  185. // header
  186. avio_write(s->pb, "APETAGEX", 8); // id
  187. avio_wl32(s->pb, APE_TAG_VERSION); // version
  188. avio_wl32(s->pb, size);
  189. avio_wl32(s->pb, count);
  190. avio_write(s->pb, dyn_buf, size - 20);
  191. // footer
  192. avio_write(s->pb, "APETAGEX", 8); // id
  193. avio_wl32(s->pb, APE_TAG_VERSION); // version
  194. avio_wl32(s->pb, size); // size
  195. avio_wl32(s->pb, count); // tag count
  196. // flags
  197. avio_wl32(s->pb, APE_TAG_FLAG_CONTAINS_HEADER | APE_TAG_FLAG_CONTAINS_FOOTER);
  198. ffio_fill(s->pb, 0, 8); // reserved
  199. end:
  200. if (dyn_bc && !dyn_buf)
  201. avio_close_dyn_buf(dyn_bc, &dyn_buf);
  202. av_freep(&dyn_buf);
  203. return ret;
  204. }