3 * Copyright (c) 2007 Benjamin Zores <ben@geexbox.org>
4 * based upon libdemac from Dave Chapman.
6 * This file is part of FFmpeg.
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.
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.
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
23 #include "libavutil/intreadwrite.h"
24 #include "libavutil/dict.h"
26 #include "avio_internal.h"
30 #define APE_TAG_FLAG_CONTAINS_HEADER (1 << 31)
31 #define APE_TAG_FLAG_CONTAINS_FOOTER (1 << 30)
32 #define APE_TAG_FLAG_IS_HEADER (1 << 29)
33 #define APE_TAG_FLAG_IS_BINARY (1 << 1)
35 static int ape_tag_read_field(AVFormatContext *s)
37 AVIOContext *pb = s->pb;
38 uint8_t key[1024], *value;
42 size = avio_rl32(pb); /* field size */
43 flags = avio_rl32(pb); /* field flags */
44 for (i = 0; i < sizeof(key) - 1; i++) {
46 if (c < 0x20 || c > 0x7E)
53 av_log(s, AV_LOG_WARNING, "Invalid APE tag key '%s'.\n", key);
58 if (flags & APE_TAG_FLAG_IS_BINARY) {
59 uint8_t filename[1024];
61 AVStream *st = avformat_new_stream(s, NULL);
63 return AVERROR(ENOMEM);
65 size -= avio_get_str(pb, size, filename, sizeof(filename));
67 av_log(s, AV_LOG_WARNING, "Skipping binary tag '%s'.\n", key);
71 av_dict_set(&st->metadata, key, filename, 0);
73 if ((id = ff_guess_image2_codec(filename)) != AV_CODEC_ID_NONE) {
77 ret = av_get_packet(s->pb, &pkt, size);
79 av_log(s, AV_LOG_ERROR, "Error reading cover art.\n");
83 st->disposition |= AV_DISPOSITION_ATTACHED_PIC;
84 st->codec->codec_type = AVMEDIA_TYPE_VIDEO;
85 st->codec->codec_id = id;
87 st->attached_pic = pkt;
88 st->attached_pic.stream_index = st->index;
89 st->attached_pic.flags |= AV_PKT_FLAG_KEY;
91 if (ff_alloc_extradata(st->codec, size))
92 return AVERROR(ENOMEM);
93 if (avio_read(pb, st->codec->extradata, size) != size) {
94 av_freep(&st->codec->extradata);
95 st->codec->extradata_size = 0;
98 st->codec->codec_type = AVMEDIA_TYPE_ATTACHMENT;
101 value = av_malloc(size+1);
103 return AVERROR(ENOMEM);
104 c = avio_read(pb, value, size);
110 av_dict_set(&s->metadata, key, value, AV_DICT_DONT_STRDUP_VAL);
115 int64_t ff_ape_parse_tag(AVFormatContext *s)
117 AVIOContext *pb = s->pb;
118 int64_t file_size = avio_size(pb);
119 uint32_t val, fields, tag_bytes;
124 if (file_size < APE_TAG_FOOTER_BYTES)
127 avio_seek(pb, file_size - APE_TAG_FOOTER_BYTES, SEEK_SET);
129 avio_read(pb, buf, 8); /* APETAGEX */
130 if (strncmp(buf, APE_TAG_PREAMBLE, 8)) {
134 val = avio_rl32(pb); /* APE tag version */
135 if (val > APE_TAG_VERSION) {
136 av_log(s, AV_LOG_ERROR, "Unsupported tag version. (>=%d)\n", APE_TAG_VERSION);
140 tag_bytes = avio_rl32(pb); /* tag size */
141 if (tag_bytes - APE_TAG_FOOTER_BYTES > (1024 * 1024 * 16)) {
142 av_log(s, AV_LOG_ERROR, "Tag size is way too big\n");
146 if (tag_bytes > file_size - APE_TAG_FOOTER_BYTES) {
147 av_log(s, AV_LOG_ERROR, "Invalid tag size %u.\n", tag_bytes);
150 tag_start = file_size - tag_bytes - APE_TAG_FOOTER_BYTES;
152 fields = avio_rl32(pb); /* number of fields */
153 if (fields > 65536) {
154 av_log(s, AV_LOG_ERROR, "Too many tag fields (%d)\n", fields);
158 val = avio_rl32(pb); /* flags */
159 if (val & APE_TAG_FLAG_IS_HEADER) {
160 av_log(s, AV_LOG_ERROR, "APE Tag is a header\n");
164 avio_seek(pb, file_size - tag_bytes, SEEK_SET);
166 for (i=0; i<fields; i++)
167 if (ape_tag_read_field(s) < 0) break;
172 static int string_is_ascii(const uint8_t *str)
174 while (*str && *str >= 0x20 && *str <= 0x7e ) str++;
178 int ff_ape_write_tag(AVFormatContext *s)
180 AVDictionaryEntry *e = NULL;
181 int size, ret, count = 0;
182 AVIOContext *dyn_bc = NULL;
183 uint8_t *dyn_buf = NULL;
185 if ((ret = avio_open_dyn_buf(&dyn_bc)) < 0)
189 avio_wl32(dyn_bc, APE_TAG_FLAG_CONTAINS_HEADER | APE_TAG_FLAG_CONTAINS_FOOTER |
190 APE_TAG_FLAG_IS_HEADER);
191 ffio_fill(dyn_bc, 0, 8); // reserved
193 while ((e = av_dict_get(s->metadata, "", e, AV_DICT_IGNORE_SUFFIX))) {
196 if (!string_is_ascii(e->key)) {
197 av_log(s, AV_LOG_WARNING, "Non ASCII keys are not allowed\n");
201 val_len = strlen(e->value);
202 avio_wl32(dyn_bc, val_len); // value length
203 avio_wl32(dyn_bc, 0); // item flags
204 avio_put_str(dyn_bc, e->key); // key
205 avio_write(dyn_bc, e->value, val_len); // value
211 size = avio_close_dyn_buf(dyn_bc, &dyn_buf);
217 avio_write(s->pb, "APETAGEX", 8); // id
218 avio_wl32(s->pb, APE_TAG_VERSION); // version
219 avio_wl32(s->pb, size);
220 avio_wl32(s->pb, count);
222 avio_write(s->pb, dyn_buf, size - 20);
225 avio_write(s->pb, "APETAGEX", 8); // id
226 avio_wl32(s->pb, APE_TAG_VERSION); // version
227 avio_wl32(s->pb, size); // size
228 avio_wl32(s->pb, count); // tag count
231 avio_wl32(s->pb, APE_TAG_FLAG_CONTAINS_HEADER | APE_TAG_FLAG_CONTAINS_FOOTER);
232 ffio_fill(s->pb, 0, 8); // reserved
235 if (dyn_bc && !dyn_buf)
236 avio_close_dyn_buf(dyn_bc, &dyn_buf);