00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023 #include "libavutil/intreadwrite.h"
00024 #include "libavutil/dict.h"
00025 #include "avformat.h"
00026 #include "apetag.h"
00027 #include "internal.h"
00028
00029 #define APE_TAG_FLAG_CONTAINS_HEADER (1 << 31)
00030 #define APE_TAG_FLAG_IS_HEADER (1 << 29)
00031 #define APE_TAG_FLAG_IS_BINARY (1 << 1)
00032
00033 static int ape_tag_read_field(AVFormatContext *s)
00034 {
00035 AVIOContext *pb = s->pb;
00036 uint8_t key[1024], *value;
00037 uint32_t size, flags;
00038 int i, c;
00039
00040 size = avio_rl32(pb);
00041 flags = avio_rl32(pb);
00042 for (i = 0; i < sizeof(key) - 1; i++) {
00043 c = avio_r8(pb);
00044 if (c < 0x20 || c > 0x7E)
00045 break;
00046 else
00047 key[i] = c;
00048 }
00049 key[i] = 0;
00050 if (c != 0) {
00051 av_log(s, AV_LOG_WARNING, "Invalid APE tag key '%s'.\n", key);
00052 return -1;
00053 }
00054 if (size >= UINT_MAX)
00055 return -1;
00056 if (flags & APE_TAG_FLAG_IS_BINARY) {
00057 uint8_t filename[1024];
00058 enum AVCodecID id;
00059 AVStream *st = avformat_new_stream(s, NULL);
00060 if (!st)
00061 return AVERROR(ENOMEM);
00062
00063 size -= avio_get_str(pb, size, filename, sizeof(filename));
00064 if (size <= 0) {
00065 av_log(s, AV_LOG_WARNING, "Skipping binary tag '%s'.\n", key);
00066 return 0;
00067 }
00068
00069 av_dict_set(&st->metadata, key, filename, 0);
00070
00071 if ((id = ff_guess_image2_codec(filename)) != AV_CODEC_ID_NONE) {
00072 AVPacket pkt;
00073 int ret;
00074
00075 ret = av_get_packet(s->pb, &pkt, size);
00076 if (ret < 0) {
00077 av_log(s, AV_LOG_ERROR, "Error reading cover art.\n");
00078 return ret;
00079 }
00080
00081 st->disposition |= AV_DISPOSITION_ATTACHED_PIC;
00082 st->codec->codec_type = AVMEDIA_TYPE_VIDEO;
00083 st->codec->codec_id = id;
00084
00085 st->attached_pic = pkt;
00086 st->attached_pic.stream_index = st->index;
00087 st->attached_pic.flags |= AV_PKT_FLAG_KEY;
00088 } else {
00089 st->codec->extradata = av_malloc(size + FF_INPUT_BUFFER_PADDING_SIZE);
00090 if (!st->codec->extradata)
00091 return AVERROR(ENOMEM);
00092 if (avio_read(pb, st->codec->extradata, size) != size) {
00093 av_freep(&st->codec->extradata);
00094 return AVERROR(EIO);
00095 }
00096 st->codec->extradata_size = size;
00097 st->codec->codec_type = AVMEDIA_TYPE_ATTACHMENT;
00098 }
00099 } else {
00100 value = av_malloc(size+1);
00101 if (!value)
00102 return AVERROR(ENOMEM);
00103 c = avio_read(pb, value, size);
00104 if (c < 0) {
00105 av_free(value);
00106 return c;
00107 }
00108 value[c] = 0;
00109 av_dict_set(&s->metadata, key, value, AV_DICT_DONT_STRDUP_VAL);
00110 }
00111 return 0;
00112 }
00113
00114 int64_t ff_ape_parse_tag(AVFormatContext *s)
00115 {
00116 AVIOContext *pb = s->pb;
00117 int file_size = avio_size(pb);
00118 uint32_t val, fields, tag_bytes;
00119 uint8_t buf[8];
00120 int64_t tag_start;
00121 int i;
00122
00123 if (file_size < APE_TAG_FOOTER_BYTES)
00124 return 0;
00125
00126 avio_seek(pb, file_size - APE_TAG_FOOTER_BYTES, SEEK_SET);
00127
00128 avio_read(pb, buf, 8);
00129 if (strncmp(buf, APE_TAG_PREAMBLE, 8)) {
00130 return 0;
00131 }
00132
00133 val = avio_rl32(pb);
00134 if (val > APE_TAG_VERSION) {
00135 av_log(s, AV_LOG_ERROR, "Unsupported tag version. (>=%d)\n", APE_TAG_VERSION);
00136 return 0;
00137 }
00138
00139 tag_bytes = avio_rl32(pb);
00140 if (tag_bytes - APE_TAG_FOOTER_BYTES > (1024 * 1024 * 16)) {
00141 av_log(s, AV_LOG_ERROR, "Tag size is way too big\n");
00142 return 0;
00143 }
00144
00145 tag_start = file_size - tag_bytes - APE_TAG_FOOTER_BYTES;
00146 if (tag_start < 0) {
00147 av_log(s, AV_LOG_ERROR, "Invalid tag size %u.\n", tag_bytes);
00148 return 0;
00149 }
00150
00151 fields = avio_rl32(pb);
00152 if (fields > 65536) {
00153 av_log(s, AV_LOG_ERROR, "Too many tag fields (%d)\n", fields);
00154 return 0;
00155 }
00156
00157 val = avio_rl32(pb);
00158 if (val & APE_TAG_FLAG_IS_HEADER) {
00159 av_log(s, AV_LOG_ERROR, "APE Tag is a header\n");
00160 return 0;
00161 }
00162
00163 avio_seek(pb, file_size - tag_bytes, SEEK_SET);
00164
00165 for (i=0; i<fields; i++)
00166 if (ape_tag_read_field(s) < 0) break;
00167
00168 return tag_start;
00169 }