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.

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