ppc: Only compile AltiVec FFT assembly when AltiVec is enabled
[libav.git] / libavformat / id3v2.c
CommitLineData
2ea512a6
AC
1/*
2 * ID3v2 header parser
3 * Copyright (c) 2003 Fabrice Bellard
4 *
2912e87a 5 * This file is part of Libav.
2ea512a6 6 *
2912e87a 7 * Libav is free software; you can redistribute it and/or
2ea512a6
AC
8 * modify it under the terms of the GNU Lesser General Public
9 * License as published by the Free Software Foundation; either
10 * version 2.1 of the License, or (at your option) any later version.
11 *
2912e87a 12 * Libav is distributed in the hope that it will be useful,
2ea512a6
AC
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
16 *
17 * You should have received a copy of the GNU Lesser General Public
2912e87a 18 * License along with Libav; if not, write to the Free Software
2ea512a6
AC
19 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
20 */
21
22#include "id3v2.h"
75411182
PD
23#include "id3v1.h"
24#include "libavutil/avstring.h"
3a1350e8 25#include "libavutil/intreadwrite.h"
d2d67e42 26#include "libavutil/dict.h"
e731b8d8 27#include "avio_internal.h"
a93b09cb 28#include "internal.h"
2ea512a6 29
3b78c180
DB
30const AVMetadataConv ff_id3v2_34_metadata_conv[] = {
31 { "TALB", "album"},
32 { "TCOM", "composer"},
33 { "TCON", "genre"},
34 { "TCOP", "copyright"},
35 { "TENC", "encoded_by"},
36 { "TIT2", "title"},
37 { "TLAN", "language"},
38 { "TPE1", "artist"},
39 { "TPE2", "album_artist"},
40 { "TPE3", "performer"},
41 { "TPOS", "disc"},
42 { "TPUB", "publisher"},
43 { "TRCK", "track"},
44 { "TSSE", "encoder"},
45 { 0 }
46};
47
48const AVMetadataConv ff_id3v2_4_metadata_conv[] = {
49 { "TDRL", "date"},
50 { "TDRC", "date"},
51 { "TDEN", "creation_time"},
52 { "TSOA", "album-sort"},
53 { "TSOP", "artist-sort"},
54 { "TSOT", "title-sort"},
55 { 0 }
56};
57
c4a37885 58static const AVMetadataConv id3v2_2_metadata_conv[] = {
3b78c180
DB
59 { "TAL", "album"},
60 { "TCO", "genre"},
61 { "TT2", "title"},
62 { "TEN", "encoded_by"},
63 { "TP1", "artist"},
64 { "TP2", "album_artist"},
65 { "TP3", "performer"},
66 { "TRK", "track"},
67 { 0 }
68};
69
70
71const char ff_id3v2_tags[][4] = {
72 "TALB", "TBPM", "TCOM", "TCON", "TCOP", "TDLY", "TENC", "TEXT",
73 "TFLT", "TIT1", "TIT2", "TIT3", "TKEY", "TLAN", "TLEN", "TMED",
74 "TOAL", "TOFN", "TOLY", "TOPE", "TOWN", "TPE1", "TPE2", "TPE3",
75 "TPE4", "TPOS", "TPUB", "TRCK", "TRSN", "TRSO", "TSRC", "TSSE",
76 { 0 },
77};
78
79const char ff_id3v2_4_tags[][4] = {
80 "TDEN", "TDOR", "TDRC", "TDRL", "TDTG", "TIPL", "TMCL", "TMOO",
81 "TPRO", "TSOA", "TSOP", "TSOT", "TSST",
82 { 0 },
83};
84
85const char ff_id3v2_3_tags[][4] = {
86 "TDAT", "TIME", "TORY", "TRDA", "TSIZ", "TYER",
87 { 0 },
88};
89
a93b09cb
AK
90const char *ff_id3v2_picture_types[21] = {
91 "Other",
92 "32x32 pixels 'file icon'",
93 "Other file icon",
94 "Cover (front)",
95 "Cover (back)",
96 "Leaflet page",
97 "Media (e.g. label side of CD)",
98 "Lead artist/lead performer/soloist",
99 "Artist/performer",
100 "Conductor",
101 "Band/Orchestra",
102 "Composer",
103 "Lyricist/text writer",
104 "Recording Location",
105 "During recording",
106 "During performance",
107 "Movie/video screen capture",
108 "A bright coloured fish",
109 "Illustration",
110 "Band/artist logotype",
111 "Publisher/Studio logotype",
112};
113
114const CodecMime ff_id3v2_mime_tags[] = {
36ef5369
AK
115 {"image/gif" , AV_CODEC_ID_GIF},
116 {"image/jpeg", AV_CODEC_ID_MJPEG},
117 {"image/jpg", AV_CODEC_ID_MJPEG},
118 {"image/png" , AV_CODEC_ID_PNG},
119 {"image/tiff", AV_CODEC_ID_TIFF},
120 {"image/bmp", AV_CODEC_ID_BMP},
385ec296
MA
121 {"JPG", AV_CODEC_ID_MJPEG}, /* ID3v2.2 */
122 {"PNG" , AV_CODEC_ID_PNG}, /* ID3v2.2 */
36ef5369 123 {"", AV_CODEC_ID_NONE},
a93b09cb
AK
124};
125
3a1350e8 126int ff_id3v2_match(const uint8_t *buf, const char * magic)
2ea512a6 127{
3a1350e8
MK
128 return buf[0] == magic[0] &&
129 buf[1] == magic[1] &&
130 buf[2] == magic[2] &&
7d7b8c32
DB
131 buf[3] != 0xff &&
132 buf[4] != 0xff &&
133 (buf[6] & 0x80) == 0 &&
134 (buf[7] & 0x80) == 0 &&
135 (buf[8] & 0x80) == 0 &&
1d4b1bf2 136 (buf[9] & 0x80) == 0;
2ea512a6 137}
ac3ef4a4
AC
138
139int ff_id3v2_tag_len(const uint8_t * buf)
140{
141 int len = ((buf[6] & 0x7f) << 21) +
7d7b8c32
DB
142 ((buf[7] & 0x7f) << 14) +
143 ((buf[8] & 0x7f) << 7) +
144 (buf[9] & 0x7f) +
145 ID3v2_HEADER_SIZE;
ac3ef4a4
AC
146 if (buf[5] & 0x10)
147 len += ID3v2_HEADER_SIZE;
148 return len;
149}
75411182 150
ae628ec1 151static unsigned int get_size(AVIOContext *s, int len)
75411182 152{
7d7b8c32
DB
153 int v = 0;
154 while (len--)
b7effd4e 155 v = (v << 7) + (avio_r8(s) & 0x7F);
75411182
PD
156 return v;
157}
158
7a019dff
DG
159/**
160 * Free GEOB type extra metadata.
161 */
ec22979a 162static void free_geobtag(void *obj)
75411182 163{
ec22979a 164 ID3v2ExtraMetaGEOB *geob = obj;
7a019dff
DG
165 av_free(geob->mime_type);
166 av_free(geob->file_name);
167 av_free(geob->description);
168 av_free(geob->data);
169 av_free(geob);
170}
75411182 171
7a019dff
DG
172/**
173 * Decode characters to UTF-8 according to encoding type. The decoded buffer is
d2961e4e
AK
174 * always null terminated. Stop reading when either *maxread bytes are read from
175 * pb or U+0000 character is found.
7a019dff
DG
176 *
177 * @param dst Pointer where the address of the buffer with the decoded bytes is
178 * stored. Buffer must be freed by caller.
7a019dff
DG
179 * @param maxread Pointer to maximum number of characters to read from the
180 * AVIOContext. After execution the value is decremented by the number of bytes
181 * actually read.
da9cea77 182 * @returns 0 if no error occurred, dst is uninitialized on error
7a019dff
DG
183 */
184static int decode_str(AVFormatContext *s, AVIOContext *pb, int encoding,
d2961e4e 185 uint8_t **dst, int *maxread)
7a019dff 186{
d2961e4e 187 int ret;
7a019dff
DG
188 uint8_t tmp;
189 uint32_t ch = 1;
190 int left = *maxread;
191 unsigned int (*get)(AVIOContext*) = avio_rb16;
192 AVIOContext *dynbuf;
75411182 193
7a019dff
DG
194 if ((ret = avio_open_dyn_buf(&dynbuf)) < 0) {
195 av_log(s, AV_LOG_ERROR, "Error opening memory stream\n");
196 return ret;
197 }
75411182 198
7a019dff 199 switch (encoding) {
75411182 200
d66eff36 201 case ID3v2_ENCODING_ISO8859:
d2961e4e 202 while (left && ch) {
7a019dff
DG
203 ch = avio_r8(pb);
204 PUT_UTF8(ch, tmp, avio_w8(dynbuf, tmp);)
205 left--;
75411182 206 }
75411182
PD
207 break;
208
d66eff36 209 case ID3v2_ENCODING_UTF16BOM:
7a019dff
DG
210 if ((left -= 2) < 0) {
211 av_log(s, AV_LOG_ERROR, "Cannot read BOM value, input too short\n");
60df6b00 212 avio_close_dyn_buf(dynbuf, dst);
7a019dff
DG
213 av_freep(dst);
214 return AVERROR_INVALIDDATA;
215 }
b7effd4e 216 switch (avio_rb16(pb)) {
20c68378 217 case 0xfffe:
b7effd4e 218 get = avio_rl16;
20c68378
AK
219 case 0xfeff:
220 break;
221 default:
7a019dff 222 av_log(s, AV_LOG_ERROR, "Incorrect BOM value\n");
60df6b00 223 avio_close_dyn_buf(dynbuf, dst);
7a019dff
DG
224 av_freep(dst);
225 *maxread = left;
226 return AVERROR_INVALIDDATA;
20c68378
AK
227 }
228 // fall-through
229
d66eff36 230 case ID3v2_ENCODING_UTF16BE:
d2961e4e 231 while ((left > 1) && ch) {
7a019dff
DG
232 GET_UTF16(ch, ((left -= 2) >= 0 ? get(pb) : 0), break;)
233 PUT_UTF8(ch, tmp, avio_w8(dynbuf, tmp);)
20c68378 234 }
7a019dff
DG
235 if (left < 0)
236 left += 2; /* did not read last char from pb */
20c68378
AK
237 break;
238
d66eff36 239 case ID3v2_ENCODING_UTF8:
d2961e4e 240 while (left && ch) {
7a019dff
DG
241 ch = avio_r8(pb);
242 avio_w8(dynbuf, ch);
243 left--;
244 }
75411182 245 break;
20c68378 246 default:
7a019dff
DG
247 av_log(s, AV_LOG_WARNING, "Unknown encoding\n");
248 }
249
250 if (ch)
251 avio_w8(dynbuf, 0);
252
60df6b00 253 avio_close_dyn_buf(dynbuf, dst);
7a019dff
DG
254 *maxread = left;
255
256 return 0;
257}
258
259/**
260 * Parse a text tag.
261 */
262static void read_ttag(AVFormatContext *s, AVIOContext *pb, int taglen, const char *key)
263{
264 uint8_t *dst;
049ce4fa 265 int encoding, dict_flags = AV_DICT_DONT_OVERWRITE | AV_DICT_DONT_STRDUP_VAL;
7a019dff
DG
266 unsigned genre;
267
268 if (taglen < 1)
269 return;
270
d2961e4e 271 encoding = avio_r8(pb);
7a019dff
DG
272 taglen--; /* account for encoding type byte */
273
d2961e4e 274 if (decode_str(s, pb, encoding, &dst, &taglen) < 0) {
7a019dff
DG
275 av_log(s, AV_LOG_ERROR, "Error reading frame %s, skipped\n", key);
276 return;
75411182
PD
277 }
278
41770abf 279 if (!(strcmp(key, "TCON") && strcmp(key, "TCO"))
75411182 280 && (sscanf(dst, "(%d)", &genre) == 1 || sscanf(dst, "%d", &genre) == 1)
d2961e4e
AK
281 && genre <= ID3v1_GENRE_MAX) {
282 av_freep(&dst);
049ce4fa 283 dst = av_strdup(ff_id3v1_genre_str[genre]);
d2961e4e
AK
284 } else if (!(strcmp(key, "TXXX") && strcmp(key, "TXX"))) {
285 /* dst now contains the key, need to get value */
41770abf 286 key = dst;
d2961e4e
AK
287 if (decode_str(s, pb, encoding, &dst, &taglen) < 0) {
288 av_log(s, AV_LOG_ERROR, "Error reading frame %s, skipped\n", key);
289 av_freep(&key);
290 return;
291 }
049ce4fa
CB
292 dict_flags |= AV_DICT_DONT_STRDUP_KEY;
293 } else if (!*dst)
ffdd2e91 294 av_freep(&dst);
7a019dff 295
d2961e4e
AK
296 if (dst)
297 av_dict_set(&s->metadata, key, dst, dict_flags);
7a019dff
DG
298}
299
300/**
301 * Parse GEOB tag into a ID3v2ExtraMetaGEOB struct.
302 */
303static void read_geobtag(AVFormatContext *s, AVIOContext *pb, int taglen, char *tag, ID3v2ExtraMeta **extra_meta)
304{
305 ID3v2ExtraMetaGEOB *geob_data = NULL;
306 ID3v2ExtraMeta *new_extra = NULL;
307 char encoding;
308 unsigned int len;
309
310 if (taglen < 1)
311 return;
312
313 geob_data = av_mallocz(sizeof(ID3v2ExtraMetaGEOB));
314 if (!geob_data) {
315 av_log(s, AV_LOG_ERROR, "Failed to alloc %zu bytes\n", sizeof(ID3v2ExtraMetaGEOB));
316 return;
317 }
318
319 new_extra = av_mallocz(sizeof(ID3v2ExtraMeta));
320 if (!new_extra) {
321 av_log(s, AV_LOG_ERROR, "Failed to alloc %zu bytes\n", sizeof(ID3v2ExtraMeta));
322 goto fail;
323 }
324
325 /* read encoding type byte */
326 encoding = avio_r8(pb);
327 taglen--;
328
329 /* read MIME type (always ISO-8859) */
d2961e4e 330 if (decode_str(s, pb, ID3v2_ENCODING_ISO8859, &geob_data->mime_type, &taglen) < 0
7a019dff
DG
331 || taglen <= 0)
332 goto fail;
333
334 /* read file name */
d2961e4e 335 if (decode_str(s, pb, encoding, &geob_data->file_name, &taglen) < 0
7a019dff
DG
336 || taglen <= 0)
337 goto fail;
338
339 /* read content description */
d2961e4e 340 if (decode_str(s, pb, encoding, &geob_data->description, &taglen) < 0
7a019dff
DG
341 || taglen < 0)
342 goto fail;
343
344 if (taglen) {
345 /* save encapsulated binary data */
346 geob_data->data = av_malloc(taglen);
347 if (!geob_data->data) {
348 av_log(s, AV_LOG_ERROR, "Failed to alloc %d bytes\n", taglen);
349 goto fail;
350 }
351 if ((len = avio_read(pb, geob_data->data, taglen)) < taglen)
352 av_log(s, AV_LOG_WARNING, "Error reading GEOB frame, data truncated.\n");
353 geob_data->datasize = len;
354 } else {
355 geob_data->data = NULL;
356 geob_data->datasize = 0;
357 }
358
359 /* add data to the list */
360 new_extra->tag = "GEOB";
361 new_extra->data = geob_data;
362 new_extra->next = *extra_meta;
363 *extra_meta = new_extra;
364
365 return;
366
367fail:
368 av_log(s, AV_LOG_ERROR, "Error reading frame %s, skipped\n", tag);
369 free_geobtag(geob_data);
370 av_free(new_extra);
371 return;
75411182
PD
372}
373
56e2ac6b
AK
374static int is_number(const char *str)
375{
376 while (*str >= '0' && *str <= '9') str++;
377 return !*str;
378}
379
d2d67e42 380static AVDictionaryEntry* get_date_tag(AVDictionary *m, const char *tag)
56e2ac6b 381{
d2d67e42
AK
382 AVDictionaryEntry *t;
383 if ((t = av_dict_get(m, tag, NULL, AV_DICT_MATCH_CASE)) &&
56e2ac6b
AK
384 strlen(t->value) == 4 && is_number(t->value))
385 return t;
386 return NULL;
387}
388
d2d67e42 389static void merge_date(AVDictionary **m)
56e2ac6b 390{
d2d67e42 391 AVDictionaryEntry *t;
56e2ac6b
AK
392 char date[17] = {0}; // YYYY-MM-DD hh:mm
393
394 if (!(t = get_date_tag(*m, "TYER")) &&
395 !(t = get_date_tag(*m, "TYE")))
396 return;
397 av_strlcpy(date, t->value, 5);
d2d67e42
AK
398 av_dict_set(m, "TYER", NULL, 0);
399 av_dict_set(m, "TYE", NULL, 0);
56e2ac6b
AK
400
401 if (!(t = get_date_tag(*m, "TDAT")) &&
402 !(t = get_date_tag(*m, "TDA")))
403 goto finish;
404 snprintf(date + 4, sizeof(date) - 4, "-%.2s-%.2s", t->value + 2, t->value);
d2d67e42
AK
405 av_dict_set(m, "TDAT", NULL, 0);
406 av_dict_set(m, "TDA", NULL, 0);
56e2ac6b
AK
407
408 if (!(t = get_date_tag(*m, "TIME")) &&
409 !(t = get_date_tag(*m, "TIM")))
410 goto finish;
411 snprintf(date + 10, sizeof(date) - 10, " %.2s:%.2s", t->value, t->value + 2);
d2d67e42
AK
412 av_dict_set(m, "TIME", NULL, 0);
413 av_dict_set(m, "TIM", NULL, 0);
56e2ac6b
AK
414
415finish:
416 if (date[0])
d2d67e42 417 av_dict_set(m, "date", date, 0);
56e2ac6b
AK
418}
419
a93b09cb
AK
420static void free_apic(void *obj)
421{
422 ID3v2ExtraMetaAPIC *apic = obj;
1afddbe5 423 av_buffer_unref(&apic->buf);
a93b09cb
AK
424 av_freep(&apic->description);
425 av_freep(&apic);
426}
427
428static void read_apic(AVFormatContext *s, AVIOContext *pb, int taglen, char *tag, ID3v2ExtraMeta **extra_meta)
429{
430 int enc, pic_type;
431 char mimetype[64];
432 const CodecMime *mime = ff_id3v2_mime_tags;
36ef5369 433 enum AVCodecID id = AV_CODEC_ID_NONE;
a93b09cb
AK
434 ID3v2ExtraMetaAPIC *apic = NULL;
435 ID3v2ExtraMeta *new_extra = NULL;
436 int64_t end = avio_tell(pb) + taglen;
437
438 if (taglen <= 4)
439 goto fail;
440
441 new_extra = av_mallocz(sizeof(*new_extra));
442 apic = av_mallocz(sizeof(*apic));
443 if (!new_extra || !apic)
444 goto fail;
445
446 enc = avio_r8(pb);
447 taglen--;
448
449 /* mimetype */
450 taglen -= avio_get_str(pb, taglen, mimetype, sizeof(mimetype));
36ef5369 451 while (mime->id != AV_CODEC_ID_NONE) {
6e9bbc65 452 if (!av_strncasecmp(mime->str, mimetype, sizeof(mimetype))) {
a93b09cb
AK
453 id = mime->id;
454 break;
455 }
456 mime++;
457 }
36ef5369 458 if (id == AV_CODEC_ID_NONE) {
a93b09cb
AK
459 av_log(s, AV_LOG_WARNING, "Unknown attached picture mimetype: %s, skipping.\n", mimetype);
460 goto fail;
461 }
462 apic->id = id;
463
464 /* picture type */
465 pic_type = avio_r8(pb);
466 taglen--;
467 if (pic_type < 0 || pic_type >= FF_ARRAY_ELEMS(ff_id3v2_picture_types)) {
468 av_log(s, AV_LOG_WARNING, "Unknown attached picture type %d.\n", pic_type);
469 pic_type = 0;
470 }
471 apic->type = ff_id3v2_picture_types[pic_type];
472
473 /* description and picture data */
474 if (decode_str(s, pb, enc, &apic->description, &taglen) < 0) {
475 av_log(s, AV_LOG_ERROR, "Error decoding attached picture description.\n");
476 goto fail;
477 }
478
8d617b11 479 apic->buf = av_buffer_alloc(taglen + FF_INPUT_BUFFER_PADDING_SIZE);
1afddbe5 480 if (!apic->buf || avio_read(pb, apic->buf->data, taglen) != taglen)
a93b09cb 481 goto fail;
8d617b11 482 memset(apic->buf->data + taglen, 0, FF_INPUT_BUFFER_PADDING_SIZE);
a93b09cb
AK
483
484 new_extra->tag = "APIC";
485 new_extra->data = apic;
486 new_extra->next = *extra_meta;
487 *extra_meta = new_extra;
488
489 return;
490
491fail:
492 if (apic)
493 free_apic(apic);
494 av_freep(&new_extra);
495 avio_seek(pb, end, SEEK_SET);
496}
497
c4a37885
DB
498typedef struct ID3v2EMFunc {
499 const char *tag3;
500 const char *tag4;
501 void (*read)(AVFormatContext*, AVIOContext*, int, char*, ID3v2ExtraMeta **);
ec22979a 502 void (*free)(void *obj);
c4a37885
DB
503} ID3v2EMFunc;
504
505static const ID3v2EMFunc id3v2_extra_meta_funcs[] = {
3b78c180 506 { "GEO", "GEOB", read_geobtag, free_geobtag },
a93b09cb 507 { "PIC", "APIC", read_apic, free_apic },
3b78c180
DB
508 { NULL }
509};
510
7a019dff
DG
511/**
512 * Get the corresponding ID3v2EMFunc struct for a tag.
513 * @param isv34 Determines if v2.2 or v2.3/4 strings are used
514 * @return A pointer to the ID3v2EMFunc struct if found, NULL otherwise.
515 */
516static const ID3v2EMFunc *get_extra_meta_func(const char *tag, int isv34)
517{
518 int i = 0;
c4a37885 519 while (id3v2_extra_meta_funcs[i].tag3) {
7a019dff 520 if (!memcmp(tag,
c4a37885
DB
521 (isv34 ? id3v2_extra_meta_funcs[i].tag4 :
522 id3v2_extra_meta_funcs[i].tag3),
7a019dff 523 (isv34 ? 4 : 3)))
c4a37885 524 return &id3v2_extra_meta_funcs[i];
7a019dff
DG
525 i++;
526 }
527 return NULL;
528}
529
e926b5ce
DB
530static void id3v2_parse(AVFormatContext *s, int len, uint8_t version,
531 uint8_t flags, ID3v2ExtraMeta **extra_meta)
75411182 532{
18bbe9df 533 int isv34, tlen, unsync;
41770abf 534 char tag[5];
bca6dee3 535 int64_t next, end = avio_tell(s->pb) + len;
75411182 536 int taghdrlen;
eb1e7f78 537 const char *reason = NULL;
ae628ec1 538 AVIOContext pb;
7a019dff 539 AVIOContext *pbx;
18bbe9df
AK
540 unsigned char *buffer = NULL;
541 int buffer_size = 0;
c780b543 542 const ID3v2EMFunc *extra_func;
75411182 543
7d7b8c32 544 switch (version) {
75411182 545 case 2:
7d7b8c32 546 if (flags & 0x40) {
75411182
PD
547 reason = "compression";
548 goto error;
549 }
550 isv34 = 0;
551 taghdrlen = 6;
552 break;
553
554 case 3:
555 case 4:
556 isv34 = 1;
557 taghdrlen = 10;
558 break;
559
560 default:
561 reason = "version";
562 goto error;
563 }
564
18bbe9df 565 unsync = flags & 0x80;
75411182 566
ddb44312
AK
567 if (isv34 && flags & 0x40) { /* Extended header present, just skip over it */
568 int extlen = get_size(s->pb, 4);
569 if (version == 4)
570 extlen -= 4; // in v2.4 the length includes the length field we just read
571
572 if (extlen < 0) {
573 reason = "invalid extended header length";
574 goto error;
575 }
576 avio_skip(s->pb, extlen);
577 }
75411182 578
7d7b8c32 579 while (len >= taghdrlen) {
40a5dd2f 580 unsigned int tflags = 0;
18bbe9df
AK
581 int tunsync = 0;
582
7d7b8c32 583 if (isv34) {
b7effd4e 584 avio_read(s->pb, tag, 4);
41770abf 585 tag[4] = 0;
3fd5a75b 586 if(version==3){
b7effd4e 587 tlen = avio_rb32(s->pb);
3fd5a75b
MN
588 }else
589 tlen = get_size(s->pb, 4);
b7effd4e 590 tflags = avio_rb16(s->pb);
7a07d158 591 tunsync = tflags & ID3v2_FLAG_UNSYNCH;
75411182 592 } else {
b7effd4e 593 avio_read(s->pb, tag, 3);
41770abf 594 tag[3] = 0;
b7effd4e 595 tlen = avio_rb24(s->pb);
75411182 596 }
1e18d32d 597 if (tlen < 0 || tlen > len - taghdrlen) {
c5f4c0fd 598 av_log(s, AV_LOG_WARNING, "Invalid size in frame %s, skipping the rest of tag.\n", tag);
75411182 599 break;
c5f4c0fd
AK
600 }
601 len -= taghdrlen + tlen;
a2704c97 602 next = avio_tell(s->pb) + tlen;
75411182 603
1e18d32d
AK
604 if (!tlen) {
605 if (tag[0])
606 av_log(s, AV_LOG_DEBUG, "Invalid empty frame %s, skipping.\n", tag);
607 continue;
608 }
609
a152c77f 610 if (tflags & ID3v2_FLAG_DATALEN) {
b7effd4e 611 avio_rb32(s->pb);
a152c77f
AK
612 tlen -= 4;
613 }
614
407d3d5a
AK
615 if (tflags & (ID3v2_FLAG_ENCRYPTION | ID3v2_FLAG_COMPRESSION)) {
616 av_log(s, AV_LOG_WARNING, "Skipping encrypted/compressed ID3v2 frame %s.\n", tag);
45a8a02a 617 avio_skip(s->pb, tlen);
7a019dff 618 /* check for text tag or supported special meta tag */
c780b543 619 } else if (tag[0] == 'T' || (extra_meta && (extra_func = get_extra_meta_func(tag, isv34)))) {
18bbe9df 620 if (unsync || tunsync) {
9ae80e6a
AK
621 int64_t end = avio_tell(s->pb) + tlen;
622 uint8_t *b;
18bbe9df 623 av_fast_malloc(&buffer, &buffer_size, tlen);
86f86877
AC
624 if (!buffer) {
625 av_log(s, AV_LOG_ERROR, "Failed to alloc %d bytes\n", tlen);
626 goto seek;
627 }
9ae80e6a
AK
628 b = buffer;
629 while (avio_tell(s->pb) < end) {
630 *b++ = avio_r8(s->pb);
631 if (*(b - 1) == 0xff && avio_tell(s->pb) < end - 1) {
632 uint8_t val = avio_r8(s->pb);
633 *b++ = val ? val : avio_r8(s->pb);
18bbe9df
AK
634 }
635 }
9ae80e6a
AK
636 ffio_init_context(&pb, buffer, b - buffer, 0, NULL, NULL, NULL, NULL);
637 tlen = b - buffer;
7a019dff 638 pbx = &pb; // read from sync buffer
18bbe9df 639 } else {
7a019dff 640 pbx = s->pb; // read straight from input
18bbe9df 641 }
7a019dff
DG
642 if (tag[0] == 'T')
643 /* parse text tag */
644 read_ttag(s, pbx, tlen, tag);
645 else
646 /* parse special meta tag */
c780b543 647 extra_func->read(s, pbx, tlen, tag, extra_meta);
18bbe9df 648 }
2e3ca1ff
JM
649 else if (!tag[0]) {
650 if (tag[1])
651 av_log(s, AV_LOG_WARNING, "invalid frame id, assuming padding");
45a8a02a 652 avio_skip(s->pb, tlen);
2e3ca1ff
JM
653 break;
654 }
75411182 655 /* Skip to end of tag */
86f86877 656seek:
6b4aa5da 657 avio_seek(s->pb, next, SEEK_SET);
75411182
PD
658 }
659
7d7b8c32 660 if (version == 4 && flags & 0x10) /* Footer preset, always 10 bytes, skip over it */
bca6dee3 661 end += 10;
18bbe9df 662
eb1e7f78
AK
663 error:
664 if (reason)
665 av_log(s, AV_LOG_INFO, "ID3v2.%d tag skipped, cannot handle %s\n", version, reason);
bca6dee3 666 avio_seek(s->pb, end, SEEK_SET);
18bbe9df 667 av_free(buffer);
75411182 668 return;
75411182 669}
6378b062 670
393fd0d8 671void ff_id3v2_read(AVFormatContext *s, const char *magic, ID3v2ExtraMeta **extra_meta)
46a2da76
AK
672{
673 int len, ret;
674 uint8_t buf[ID3v2_HEADER_SIZE];
675 int found_header;
676 int64_t off;
677
678 do {
679 /* save the current offset in case there's nothing to read/skip */
a2704c97 680 off = avio_tell(s->pb);
b7effd4e 681 ret = avio_read(s->pb, buf, ID3v2_HEADER_SIZE);
46a2da76 682 if (ret != ID3v2_HEADER_SIZE)
f7fcd6a2 683 break;
46a2da76
AK
684 found_header = ff_id3v2_match(buf, magic);
685 if (found_header) {
686 /* parse ID3v2 header */
687 len = ((buf[6] & 0x7f) << 21) |
688 ((buf[7] & 0x7f) << 14) |
689 ((buf[8] & 0x7f) << 7) |
690 (buf[9] & 0x7f);
e926b5ce 691 id3v2_parse(s, len, buf[3], buf[5], extra_meta);
46a2da76 692 } else {
6b4aa5da 693 avio_seek(s->pb, off, SEEK_SET);
46a2da76
AK
694 }
695 } while (found_header);
cb6bc576 696 ff_metadata_conv(&s->metadata, NULL, ff_id3v2_34_metadata_conv);
c4a37885 697 ff_metadata_conv(&s->metadata, NULL, id3v2_2_metadata_conv);
cb6bc576 698 ff_metadata_conv(&s->metadata, NULL, ff_id3v2_4_metadata_conv);
56e2ac6b 699 merge_date(&s->metadata);
46a2da76
AK
700}
701
7a019dff
DG
702void ff_id3v2_free_extra_meta(ID3v2ExtraMeta **extra_meta)
703{
704 ID3v2ExtraMeta *current = *extra_meta, *next;
c780b543 705 const ID3v2EMFunc *extra_func;
7a019dff
DG
706
707 while (current) {
c780b543
AK
708 if ((extra_func = get_extra_meta_func(current->tag, 1)))
709 extra_func->free(current->data);
7a019dff
DG
710 next = current->next;
711 av_freep(&current);
712 current = next;
713 }
714}
079ea6ca
AK
715
716int ff_id3v2_parse_apic(AVFormatContext *s, ID3v2ExtraMeta **extra_meta)
717{
718 ID3v2ExtraMeta *cur;
719
720 for (cur = *extra_meta; cur; cur = cur->next) {
721 ID3v2ExtraMetaAPIC *apic;
722 AVStream *st;
723
724 if (strcmp(cur->tag, "APIC"))
725 continue;
726 apic = cur->data;
727
728 if (!(st = avformat_new_stream(s, NULL)))
729 return AVERROR(ENOMEM);
730
731 st->disposition |= AV_DISPOSITION_ATTACHED_PIC;
732 st->codec->codec_type = AVMEDIA_TYPE_VIDEO;
733 st->codec->codec_id = apic->id;
734 av_dict_set(&st->metadata, "title", apic->description, 0);
735 av_dict_set(&st->metadata, "comment", apic->type, 0);
736
737 av_init_packet(&st->attached_pic);
1afddbe5
AK
738 st->attached_pic.buf = apic->buf;
739 st->attached_pic.data = apic->buf->data;
8d617b11 740 st->attached_pic.size = apic->buf->size - FF_INPUT_BUFFER_PADDING_SIZE;
079ea6ca 741 st->attached_pic.stream_index = st->index;
713f3062 742 st->attached_pic.flags |= AV_PKT_FLAG_KEY;
079ea6ca 743
1afddbe5 744 apic->buf = NULL;
079ea6ca
AK
745 }
746
747 return 0;
748}