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.

163 lines
4.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 Libav.
  7. *
  8. * Libav 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. * Libav 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 Libav; 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 "apetag.h"
  26. #include "internal.h"
  27. #define APE_TAG_VERSION 2000
  28. #define APE_TAG_FOOTER_BYTES 32
  29. #define APE_TAG_FLAG_CONTAINS_HEADER (1 << 31)
  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 CodecID 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)) != 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. void ff_ape_parse_tag(AVFormatContext *s)
  106. {
  107. AVIOContext *pb = s->pb;
  108. int file_size = avio_size(pb);
  109. uint32_t val, fields, tag_bytes;
  110. uint8_t buf[8];
  111. int i;
  112. if (file_size < APE_TAG_FOOTER_BYTES)
  113. return;
  114. avio_seek(pb, file_size - APE_TAG_FOOTER_BYTES, SEEK_SET);
  115. avio_read(pb, buf, 8); /* APETAGEX */
  116. if (strncmp(buf, "APETAGEX", 8)) {
  117. return;
  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;
  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;
  128. }
  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;
  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;
  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. }