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