3 * Copyright (c) 2007 Benjamin Zores <ben@geexbox.org>
4 * based upon libdemac from Dave Chapman.
6 * This file is part of Libav.
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.
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.
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
25 #include "libavutil/intreadwrite.h"
26 #include "libavutil/dict.h"
28 #include "avio_internal.h"
32 #define APE_TAG_VERSION 2000
33 #define APE_TAG_FOOTER_BYTES 32
34 #define APE_TAG_HEADER_BYTES 32
35 #define APE_TAG_FLAG_CONTAINS_HEADER (1 << 31)
36 #define APE_TAG_FLAG_LACKS_FOOTER (1 << 30)
37 #define APE_TAG_FLAG_IS_HEADER (1 << 29)
38 #define APE_TAG_FLAG_IS_BINARY (1 << 1)
40 static int ape_tag_read_field(AVFormatContext *s)
42 AVIOContext *pb = s->pb;
43 uint8_t key[1024], *value;
47 size = avio_rl32(pb); /* field size */
48 flags = avio_rl32(pb); /* field flags */
49 for (i = 0; i < sizeof(key) - 1; i++) {
51 if (c < 0x20 || c > 0x7E)
58 av_log(s, AV_LOG_WARNING, "Invalid APE tag key '%s'.\n", key);
61 if (size > INT32_MAX - AV_INPUT_BUFFER_PADDING_SIZE) {
62 av_log(s, AV_LOG_ERROR, "APE tag size too large.\n");
63 return AVERROR_INVALIDDATA;
65 if (flags & APE_TAG_FLAG_IS_BINARY) {
66 uint8_t filename[1024];
68 AVStream *st = avformat_new_stream(s, NULL);
70 return AVERROR(ENOMEM);
72 size -= avio_get_str(pb, size, filename, sizeof(filename));
74 av_log(s, AV_LOG_WARNING, "Skipping binary tag '%s'.\n", key);
78 av_dict_set(&st->metadata, key, filename, 0);
80 if ((id = ff_guess_image2_codec(filename)) != AV_CODEC_ID_NONE) {
84 ret = av_get_packet(s->pb, &pkt, size);
86 av_log(s, AV_LOG_ERROR, "Error reading cover art.\n");
90 st->disposition |= AV_DISPOSITION_ATTACHED_PIC;
91 st->codecpar->codec_type = AVMEDIA_TYPE_VIDEO;
92 st->codecpar->codec_id = id;
94 st->attached_pic = pkt;
95 st->attached_pic.stream_index = st->index;
96 st->attached_pic.flags |= AV_PKT_FLAG_KEY;
98 st->codecpar->extradata = av_malloc(size + AV_INPUT_BUFFER_PADDING_SIZE);
99 if (!st->codecpar->extradata)
100 return AVERROR(ENOMEM);
101 if (avio_read(pb, st->codecpar->extradata, size) != size) {
102 av_freep(&st->codecpar->extradata);
105 st->codecpar->extradata_size = size;
106 st->codecpar->codec_type = AVMEDIA_TYPE_ATTACHMENT;
109 value = av_malloc(size+1);
111 return AVERROR(ENOMEM);
112 c = avio_read(pb, value, size);
118 av_dict_set(&s->metadata, key, value, AV_DICT_DONT_STRDUP_VAL);
123 int64_t ff_ape_parse_tag(AVFormatContext *s)
125 AVIOContext *pb = s->pb;
126 int64_t file_size = avio_size(pb);
127 uint32_t val, fields, tag_bytes;
132 if (file_size < APE_TAG_FOOTER_BYTES)
135 avio_seek(pb, file_size - APE_TAG_FOOTER_BYTES, SEEK_SET);
137 avio_read(pb, buf, 8); /* APETAGEX */
138 if (strncmp(buf, "APETAGEX", 8)) {
142 val = avio_rl32(pb); /* APE tag version */
143 if (val > APE_TAG_VERSION) {
144 av_log(s, AV_LOG_ERROR, "Unsupported tag version. (>=%d)\n", APE_TAG_VERSION);
148 tag_bytes = avio_rl32(pb); /* tag size */
149 if (tag_bytes - APE_TAG_FOOTER_BYTES > (1024 * 1024 * 16)) {
150 av_log(s, AV_LOG_ERROR, "Tag size is way too big\n");
154 if (tag_bytes > file_size - APE_TAG_FOOTER_BYTES) {
155 av_log(s, AV_LOG_ERROR, "Invalid tag size %"PRIu32".\n", tag_bytes);
159 fields = avio_rl32(pb); /* number of fields */
160 if (fields > 65536) {
161 av_log(s, AV_LOG_ERROR, "Too many tag fields (%"PRIu32")\n", fields);
165 val = avio_rl32(pb); /* flags */
166 if (val & APE_TAG_FLAG_IS_HEADER) {
167 av_log(s, AV_LOG_ERROR, "APE Tag is a header\n");
171 avio_seek(pb, file_size - tag_bytes, SEEK_SET);
173 if (val & APE_TAG_FLAG_CONTAINS_HEADER)
174 tag_bytes += APE_TAG_HEADER_BYTES;
176 tag_start = file_size - tag_bytes;
178 for (i=0; i<fields; i++)
179 if (ape_tag_read_field(s) < 0) break;
184 int ff_ape_write_tag(AVFormatContext *s)
186 AVDictionaryEntry *e = NULL;
190 if (!(s->pb->seekable & AVIO_SEEKABLE_NORMAL))
193 start = avio_tell(s->pb);
196 avio_write(s->pb, "APETAGEX", 8); // id
197 avio_wl32 (s->pb, APE_TAG_VERSION); // version
198 avio_wl32(s->pb, 0); // reserve space for size
199 avio_wl32(s->pb, 0); // reserve space for tag count
202 avio_wl32(s->pb, APE_TAG_FLAG_CONTAINS_HEADER | APE_TAG_FLAG_IS_HEADER);
203 ffio_fill(s->pb, 0, 8); // reserved
205 while ((e = av_dict_get(s->metadata, "", e, AV_DICT_IGNORE_SUFFIX))) {
206 int val_len = strlen(e->value);
208 avio_wl32(s->pb, val_len); // value length
209 avio_wl32(s->pb, 0); // item flags
210 avio_put_str(s->pb, e->key); // key
211 avio_write(s->pb, e->value, val_len); // value
215 size = avio_tell(s->pb) - start;
218 avio_write(s->pb, "APETAGEX", 8); // id
219 avio_wl32 (s->pb, APE_TAG_VERSION); // version
220 avio_wl32(s->pb, size); // size
221 avio_wl32(s->pb, count); // tag count
224 avio_wl32(s->pb, APE_TAG_FLAG_CONTAINS_HEADER);
225 ffio_fill(s->pb, 0, 8); // reserved
227 // update values in the header
228 end = avio_tell(s->pb);
229 avio_seek(s->pb, start + 12, SEEK_SET);
230 avio_wl32(s->pb, size);
231 avio_wl32(s->pb, count);
232 avio_seek(s->pb, end, SEEK_SET);