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.

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