]> git.sesse.net Git - ffmpeg/blob - libavformat/id3v2.c
movenc: Add channel layouts for PCM.
[ffmpeg] / libavformat / id3v2.c
1 /*
2  * ID3v2 header parser
3  * Copyright (c) 2003 Fabrice Bellard
4  *
5  * This file is part of Libav.
6  *
7  * Libav is free software; you can redistribute it and/or
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  *
12  * Libav is distributed in the hope that it will be useful,
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
18  * License along with Libav; if not, write to the Free Software
19  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
20  */
21
22 #include "id3v2.h"
23 #include "id3v1.h"
24 #include "libavutil/avstring.h"
25 #include "libavutil/intreadwrite.h"
26 #include "libavutil/dict.h"
27 #include "avio_internal.h"
28 #include "internal.h"
29
30 const 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
48 const 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
58 static const AVMetadataConv id3v2_2_metadata_conv[] = {
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
71 const 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
79 const 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
85 const char ff_id3v2_3_tags[][4] = {
86    "TDAT", "TIME", "TORY", "TRDA", "TSIZ", "TYER",
87    { 0 },
88 };
89
90 const 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
114 const CodecMime ff_id3v2_mime_tags[] = {
115     {"image/gif" , CODEC_ID_GIF},
116     {"image/jpeg", CODEC_ID_MJPEG},
117     {"image/jpg",  CODEC_ID_MJPEG},
118     {"image/png" , CODEC_ID_PNG},
119     {"image/tiff", CODEC_ID_TIFF},
120     {"",           CODEC_ID_NONE},
121 };
122
123 int ff_id3v2_match(const uint8_t *buf, const char * magic)
124 {
125     return  buf[0]         == magic[0] &&
126             buf[1]         == magic[1] &&
127             buf[2]         == magic[2] &&
128             buf[3]         != 0xff &&
129             buf[4]         != 0xff &&
130            (buf[6] & 0x80) ==    0 &&
131            (buf[7] & 0x80) ==    0 &&
132            (buf[8] & 0x80) ==    0 &&
133            (buf[9] & 0x80) ==    0;
134 }
135
136 int ff_id3v2_tag_len(const uint8_t * buf)
137 {
138     int len = ((buf[6] & 0x7f) << 21) +
139               ((buf[7] & 0x7f) << 14) +
140               ((buf[8] & 0x7f) << 7) +
141                (buf[9] & 0x7f) +
142               ID3v2_HEADER_SIZE;
143     if (buf[5] & 0x10)
144         len += ID3v2_HEADER_SIZE;
145     return len;
146 }
147
148 static unsigned int get_size(AVIOContext *s, int len)
149 {
150     int v = 0;
151     while (len--)
152         v = (v << 7) + (avio_r8(s) & 0x7F);
153     return v;
154 }
155
156 /**
157  * Free GEOB type extra metadata.
158  */
159 static void free_geobtag(void *obj)
160 {
161     ID3v2ExtraMetaGEOB *geob = obj;
162     av_free(geob->mime_type);
163     av_free(geob->file_name);
164     av_free(geob->description);
165     av_free(geob->data);
166     av_free(geob);
167 }
168
169 /**
170  * Decode characters to UTF-8 according to encoding type. The decoded buffer is
171  * always null terminated. Stop reading when either *maxread bytes are read from
172  * pb or U+0000 character is found.
173  *
174  * @param dst Pointer where the address of the buffer with the decoded bytes is
175  * stored. Buffer must be freed by caller.
176  * @param maxread Pointer to maximum number of characters to read from the
177  * AVIOContext. After execution the value is decremented by the number of bytes
178  * actually read.
179  * @returns 0 if no error occurred, dst is uninitialized on error
180  */
181 static int decode_str(AVFormatContext *s, AVIOContext *pb, int encoding,
182                       uint8_t **dst, int *maxread)
183 {
184     int ret;
185     uint8_t tmp;
186     uint32_t ch = 1;
187     int left = *maxread;
188     unsigned int (*get)(AVIOContext*) = avio_rb16;
189     AVIOContext *dynbuf;
190
191     if ((ret = avio_open_dyn_buf(&dynbuf)) < 0) {
192         av_log(s, AV_LOG_ERROR, "Error opening memory stream\n");
193         return ret;
194     }
195
196     switch (encoding) {
197
198     case ID3v2_ENCODING_ISO8859:
199         while (left && ch) {
200             ch = avio_r8(pb);
201             PUT_UTF8(ch, tmp, avio_w8(dynbuf, tmp);)
202             left--;
203         }
204         break;
205
206     case ID3v2_ENCODING_UTF16BOM:
207         if ((left -= 2) < 0) {
208             av_log(s, AV_LOG_ERROR, "Cannot read BOM value, input too short\n");
209             avio_close_dyn_buf(dynbuf, dst);
210             av_freep(dst);
211             return AVERROR_INVALIDDATA;
212         }
213         switch (avio_rb16(pb)) {
214         case 0xfffe:
215             get = avio_rl16;
216         case 0xfeff:
217             break;
218         default:
219             av_log(s, AV_LOG_ERROR, "Incorrect BOM value\n");
220             avio_close_dyn_buf(dynbuf, dst);
221             av_freep(dst);
222             *maxread = left;
223             return AVERROR_INVALIDDATA;
224         }
225         // fall-through
226
227     case ID3v2_ENCODING_UTF16BE:
228         while ((left > 1) && ch) {
229             GET_UTF16(ch, ((left -= 2) >= 0 ? get(pb) : 0), break;)
230             PUT_UTF8(ch, tmp, avio_w8(dynbuf, tmp);)
231         }
232         if (left < 0)
233             left += 2; /* did not read last char from pb */
234         break;
235
236     case ID3v2_ENCODING_UTF8:
237         while (left && ch) {
238             ch = avio_r8(pb);
239             avio_w8(dynbuf, ch);
240             left--;
241         }
242         break;
243     default:
244         av_log(s, AV_LOG_WARNING, "Unknown encoding\n");
245     }
246
247     if (ch)
248         avio_w8(dynbuf, 0);
249
250     avio_close_dyn_buf(dynbuf, dst);
251     *maxread = left;
252
253     return 0;
254 }
255
256 /**
257  * Parse a text tag.
258  */
259 static void read_ttag(AVFormatContext *s, AVIOContext *pb, int taglen, const char *key)
260 {
261     uint8_t *dst;
262     int encoding, dict_flags = AV_DICT_DONT_OVERWRITE;
263     unsigned genre;
264
265     if (taglen < 1)
266         return;
267
268     encoding = avio_r8(pb);
269     taglen--; /* account for encoding type byte */
270
271     if (decode_str(s, pb, encoding, &dst, &taglen) < 0) {
272         av_log(s, AV_LOG_ERROR, "Error reading frame %s, skipped\n", key);
273         return;
274     }
275
276     if (!(strcmp(key, "TCON") && strcmp(key, "TCO"))
277         && (sscanf(dst, "(%d)", &genre) == 1 || sscanf(dst, "%d", &genre) == 1)
278         && genre <= ID3v1_GENRE_MAX) {
279         av_freep(&dst);
280         dst = ff_id3v1_genre_str[genre];
281     } else if (!(strcmp(key, "TXXX") && strcmp(key, "TXX"))) {
282         /* dst now contains the key, need to get value */
283         key = dst;
284         if (decode_str(s, pb, encoding, &dst, &taglen) < 0) {
285             av_log(s, AV_LOG_ERROR, "Error reading frame %s, skipped\n", key);
286             av_freep(&key);
287             return;
288         }
289         dict_flags |= AV_DICT_DONT_STRDUP_VAL | AV_DICT_DONT_STRDUP_KEY;
290     }
291     else if (*dst)
292         dict_flags |= AV_DICT_DONT_STRDUP_VAL;
293
294     if (dst)
295         av_dict_set(&s->metadata, key, dst, dict_flags);
296 }
297
298 /**
299  * Parse GEOB tag into a ID3v2ExtraMetaGEOB struct.
300  */
301 static void read_geobtag(AVFormatContext *s, AVIOContext *pb, int taglen, char *tag, ID3v2ExtraMeta **extra_meta)
302 {
303     ID3v2ExtraMetaGEOB *geob_data = NULL;
304     ID3v2ExtraMeta *new_extra = NULL;
305     char encoding;
306     unsigned int len;
307
308     if (taglen < 1)
309         return;
310
311     geob_data = av_mallocz(sizeof(ID3v2ExtraMetaGEOB));
312     if (!geob_data) {
313         av_log(s, AV_LOG_ERROR, "Failed to alloc %zu bytes\n", sizeof(ID3v2ExtraMetaGEOB));
314         return;
315     }
316
317     new_extra = av_mallocz(sizeof(ID3v2ExtraMeta));
318     if (!new_extra) {
319         av_log(s, AV_LOG_ERROR, "Failed to alloc %zu bytes\n", sizeof(ID3v2ExtraMeta));
320         goto fail;
321     }
322
323     /* read encoding type byte */
324     encoding = avio_r8(pb);
325     taglen--;
326
327     /* read MIME type (always ISO-8859) */
328     if (decode_str(s, pb, ID3v2_ENCODING_ISO8859, &geob_data->mime_type, &taglen) < 0
329         || taglen <= 0)
330         goto fail;
331
332     /* read file name */
333     if (decode_str(s, pb, encoding, &geob_data->file_name, &taglen) < 0
334         || taglen <= 0)
335         goto fail;
336
337     /* read content description */
338     if (decode_str(s, pb, encoding, &geob_data->description, &taglen) < 0
339         || taglen < 0)
340         goto fail;
341
342     if (taglen) {
343         /* save encapsulated binary data */
344         geob_data->data = av_malloc(taglen);
345         if (!geob_data->data) {
346             av_log(s, AV_LOG_ERROR, "Failed to alloc %d bytes\n", taglen);
347             goto fail;
348         }
349         if ((len = avio_read(pb, geob_data->data, taglen)) < taglen)
350             av_log(s, AV_LOG_WARNING, "Error reading GEOB frame, data truncated.\n");
351         geob_data->datasize = len;
352     } else {
353         geob_data->data = NULL;
354         geob_data->datasize = 0;
355     }
356
357     /* add data to the list */
358     new_extra->tag = "GEOB";
359     new_extra->data = geob_data;
360     new_extra->next = *extra_meta;
361     *extra_meta = new_extra;
362
363     return;
364
365 fail:
366     av_log(s, AV_LOG_ERROR, "Error reading frame %s, skipped\n", tag);
367     free_geobtag(geob_data);
368     av_free(new_extra);
369     return;
370 }
371
372 static int is_number(const char *str)
373 {
374     while (*str >= '0' && *str <= '9') str++;
375     return !*str;
376 }
377
378 static AVDictionaryEntry* get_date_tag(AVDictionary *m, const char *tag)
379 {
380     AVDictionaryEntry *t;
381     if ((t = av_dict_get(m, tag, NULL, AV_DICT_MATCH_CASE)) &&
382         strlen(t->value) == 4 && is_number(t->value))
383         return t;
384     return NULL;
385 }
386
387 static void merge_date(AVDictionary **m)
388 {
389     AVDictionaryEntry *t;
390     char date[17] = {0};      // YYYY-MM-DD hh:mm
391
392     if (!(t = get_date_tag(*m, "TYER")) &&
393         !(t = get_date_tag(*m, "TYE")))
394         return;
395     av_strlcpy(date, t->value, 5);
396     av_dict_set(m, "TYER", NULL, 0);
397     av_dict_set(m, "TYE",  NULL, 0);
398
399     if (!(t = get_date_tag(*m, "TDAT")) &&
400         !(t = get_date_tag(*m, "TDA")))
401         goto finish;
402     snprintf(date + 4, sizeof(date) - 4, "-%.2s-%.2s", t->value + 2, t->value);
403     av_dict_set(m, "TDAT", NULL, 0);
404     av_dict_set(m, "TDA",  NULL, 0);
405
406     if (!(t = get_date_tag(*m, "TIME")) &&
407         !(t = get_date_tag(*m, "TIM")))
408         goto finish;
409     snprintf(date + 10, sizeof(date) - 10, " %.2s:%.2s", t->value, t->value + 2);
410     av_dict_set(m, "TIME", NULL, 0);
411     av_dict_set(m, "TIM",  NULL, 0);
412
413 finish:
414     if (date[0])
415         av_dict_set(m, "date", date, 0);
416 }
417
418 static void free_apic(void *obj)
419 {
420     ID3v2ExtraMetaAPIC *apic = obj;
421     av_freep(&apic->data);
422     av_freep(&apic->description);
423     av_freep(&apic);
424 }
425
426 static void read_apic(AVFormatContext *s, AVIOContext *pb, int taglen, char *tag, ID3v2ExtraMeta **extra_meta)
427 {
428     int enc, pic_type;
429     char             mimetype[64];
430     const CodecMime     *mime = ff_id3v2_mime_tags;
431     enum CodecID           id = CODEC_ID_NONE;
432     ID3v2ExtraMetaAPIC  *apic = NULL;
433     ID3v2ExtraMeta *new_extra = NULL;
434     int64_t               end = avio_tell(pb) + taglen;
435
436     if (taglen <= 4)
437         goto fail;
438
439     new_extra = av_mallocz(sizeof(*new_extra));
440     apic      = av_mallocz(sizeof(*apic));
441     if (!new_extra || !apic)
442         goto fail;
443
444     enc = avio_r8(pb);
445     taglen--;
446
447     /* mimetype */
448     taglen -= avio_get_str(pb, taglen, mimetype, sizeof(mimetype));
449     while (mime->id != CODEC_ID_NONE) {
450         if (!strncmp(mime->str, mimetype, sizeof(mimetype))) {
451             id = mime->id;
452             break;
453         }
454         mime++;
455     }
456     if (id == CODEC_ID_NONE) {
457         av_log(s, AV_LOG_WARNING, "Unknown attached picture mimetype: %s, skipping.\n", mimetype);
458         goto fail;
459     }
460     apic->id = id;
461
462     /* picture type */
463     pic_type = avio_r8(pb);
464     taglen--;
465     if (pic_type < 0 || pic_type >= FF_ARRAY_ELEMS(ff_id3v2_picture_types)) {
466         av_log(s, AV_LOG_WARNING, "Unknown attached picture type %d.\n", pic_type);
467         pic_type = 0;
468     }
469     apic->type = ff_id3v2_picture_types[pic_type];
470
471     /* description and picture data */
472     if (decode_str(s, pb, enc, &apic->description, &taglen) < 0) {
473         av_log(s, AV_LOG_ERROR, "Error decoding attached picture description.\n");
474         goto fail;
475     }
476
477     apic->len   = taglen;
478     apic->data  = av_malloc(taglen);
479     if (!apic->data || avio_read(pb, apic->data, taglen) != taglen)
480         goto fail;
481
482     new_extra->tag    = "APIC";
483     new_extra->data   = apic;
484     new_extra->next   = *extra_meta;
485     *extra_meta       = new_extra;
486
487     return;
488
489 fail:
490     if (apic)
491         free_apic(apic);
492     av_freep(&new_extra);
493     avio_seek(pb, end, SEEK_SET);
494 }
495
496 typedef struct ID3v2EMFunc {
497     const char *tag3;
498     const char *tag4;
499     void (*read)(AVFormatContext*, AVIOContext*, int, char*, ID3v2ExtraMeta **);
500     void (*free)(void *obj);
501 } ID3v2EMFunc;
502
503 static const ID3v2EMFunc id3v2_extra_meta_funcs[] = {
504     { "GEO", "GEOB", read_geobtag, free_geobtag },
505     { "PIC", "APIC", read_apic,    free_apic },
506     { NULL }
507 };
508
509 /**
510  * Get the corresponding ID3v2EMFunc struct for a tag.
511  * @param isv34 Determines if v2.2 or v2.3/4 strings are used
512  * @return A pointer to the ID3v2EMFunc struct if found, NULL otherwise.
513  */
514 static const ID3v2EMFunc *get_extra_meta_func(const char *tag, int isv34)
515 {
516     int i = 0;
517     while (id3v2_extra_meta_funcs[i].tag3) {
518         if (!memcmp(tag,
519                     (isv34 ? id3v2_extra_meta_funcs[i].tag4 :
520                              id3v2_extra_meta_funcs[i].tag3),
521                     (isv34 ? 4 : 3)))
522             return &id3v2_extra_meta_funcs[i];
523         i++;
524     }
525     return NULL;
526 }
527
528 static void ff_id3v2_parse(AVFormatContext *s, int len, uint8_t version, uint8_t flags, ID3v2ExtraMeta **extra_meta)
529 {
530     int isv34, tlen, unsync;
531     char tag[5];
532     int64_t next, end = avio_tell(s->pb) + len;
533     int taghdrlen;
534     const char *reason = NULL;
535     AVIOContext pb;
536     AVIOContext *pbx;
537     unsigned char *buffer = NULL;
538     int buffer_size = 0;
539     const ID3v2EMFunc *extra_func;
540
541     switch (version) {
542     case 2:
543         if (flags & 0x40) {
544             reason = "compression";
545             goto error;
546         }
547         isv34 = 0;
548         taghdrlen = 6;
549         break;
550
551     case 3:
552     case 4:
553         isv34 = 1;
554         taghdrlen = 10;
555         break;
556
557     default:
558         reason = "version";
559         goto error;
560     }
561
562     unsync = flags & 0x80;
563
564     if (isv34 && flags & 0x40) { /* Extended header present, just skip over it */
565         int extlen = get_size(s->pb, 4);
566         if (version == 4)
567             extlen -= 4;     // in v2.4 the length includes the length field we just read
568
569         if (extlen < 0) {
570             reason = "invalid extended header length";
571             goto error;
572         }
573         avio_skip(s->pb, extlen);
574     }
575
576     while (len >= taghdrlen) {
577         unsigned int tflags = 0;
578         int tunsync = 0;
579
580         if (isv34) {
581             avio_read(s->pb, tag, 4);
582             tag[4] = 0;
583             if(version==3){
584                 tlen = avio_rb32(s->pb);
585             }else
586                 tlen = get_size(s->pb, 4);
587             tflags = avio_rb16(s->pb);
588             tunsync = tflags & ID3v2_FLAG_UNSYNCH;
589         } else {
590             avio_read(s->pb, tag, 3);
591             tag[3] = 0;
592             tlen = avio_rb24(s->pb);
593         }
594         if (tlen < 0 || tlen > len - taghdrlen) {
595             av_log(s, AV_LOG_WARNING, "Invalid size in frame %s, skipping the rest of tag.\n", tag);
596             break;
597         }
598         len -= taghdrlen + tlen;
599         next = avio_tell(s->pb) + tlen;
600
601         if (!tlen) {
602             if (tag[0])
603                 av_log(s, AV_LOG_DEBUG, "Invalid empty frame %s, skipping.\n", tag);
604             continue;
605         }
606
607         if (tflags & ID3v2_FLAG_DATALEN) {
608             avio_rb32(s->pb);
609             tlen -= 4;
610         }
611
612         if (tflags & (ID3v2_FLAG_ENCRYPTION | ID3v2_FLAG_COMPRESSION)) {
613             av_log(s, AV_LOG_WARNING, "Skipping encrypted/compressed ID3v2 frame %s.\n", tag);
614             avio_skip(s->pb, tlen);
615         /* check for text tag or supported special meta tag */
616         } else if (tag[0] == 'T' || (extra_meta && (extra_func = get_extra_meta_func(tag, isv34)))) {
617             if (unsync || tunsync) {
618                 int i, j;
619                 av_fast_malloc(&buffer, &buffer_size, tlen);
620                 if (!buffer) {
621                     av_log(s, AV_LOG_ERROR, "Failed to alloc %d bytes\n", tlen);
622                     goto seek;
623                 }
624                 for (i = 0, j = 0; i < tlen; i++, j++) {
625                     buffer[j] = avio_r8(s->pb);
626                     if (j > 0 && !buffer[j] && buffer[j - 1] == 0xff) {
627                         /* Unsynchronised byte, skip it */
628                         j--;
629                     }
630                 }
631                 ffio_init_context(&pb, buffer, j, 0, NULL, NULL, NULL, NULL);
632                 tlen = j;
633                 pbx = &pb; // read from sync buffer
634             } else {
635                 pbx = s->pb; // read straight from input
636             }
637             if (tag[0] == 'T')
638                 /* parse text tag */
639                 read_ttag(s, pbx, tlen, tag);
640             else
641                 /* parse special meta tag */
642                 extra_func->read(s, pbx, tlen, tag, extra_meta);
643         }
644         else if (!tag[0]) {
645             if (tag[1])
646                 av_log(s, AV_LOG_WARNING, "invalid frame id, assuming padding");
647             avio_skip(s->pb, tlen);
648             break;
649         }
650         /* Skip to end of tag */
651 seek:
652         avio_seek(s->pb, next, SEEK_SET);
653     }
654
655     if (version == 4 && flags & 0x10) /* Footer preset, always 10 bytes, skip over it */
656         end += 10;
657
658   error:
659     if (reason)
660         av_log(s, AV_LOG_INFO, "ID3v2.%d tag skipped, cannot handle %s\n", version, reason);
661     avio_seek(s->pb, end, SEEK_SET);
662     av_free(buffer);
663     return;
664 }
665
666 void ff_id3v2_read(AVFormatContext *s, const char *magic, ID3v2ExtraMeta **extra_meta)
667 {
668     int len, ret;
669     uint8_t buf[ID3v2_HEADER_SIZE];
670     int     found_header;
671     int64_t off;
672
673     do {
674         /* save the current offset in case there's nothing to read/skip */
675         off = avio_tell(s->pb);
676         ret = avio_read(s->pb, buf, ID3v2_HEADER_SIZE);
677         if (ret != ID3v2_HEADER_SIZE)
678             break;
679             found_header = ff_id3v2_match(buf, magic);
680             if (found_header) {
681             /* parse ID3v2 header */
682             len = ((buf[6] & 0x7f) << 21) |
683                   ((buf[7] & 0x7f) << 14) |
684                   ((buf[8] & 0x7f) << 7) |
685                    (buf[9] & 0x7f);
686             ff_id3v2_parse(s, len, buf[3], buf[5], extra_meta);
687         } else {
688             avio_seek(s->pb, off, SEEK_SET);
689         }
690     } while (found_header);
691     ff_metadata_conv(&s->metadata, NULL, ff_id3v2_34_metadata_conv);
692     ff_metadata_conv(&s->metadata, NULL, id3v2_2_metadata_conv);
693     ff_metadata_conv(&s->metadata, NULL, ff_id3v2_4_metadata_conv);
694     merge_date(&s->metadata);
695 }
696
697 void ff_id3v2_free_extra_meta(ID3v2ExtraMeta **extra_meta)
698 {
699     ID3v2ExtraMeta *current = *extra_meta, *next;
700     const ID3v2EMFunc *extra_func;
701
702     while (current) {
703         if ((extra_func = get_extra_meta_func(current->tag, 1)))
704             extra_func->free(current->data);
705         next = current->next;
706         av_freep(&current);
707         current = next;
708     }
709 }
710
711 int ff_id3v2_parse_apic(AVFormatContext *s, ID3v2ExtraMeta **extra_meta)
712 {
713     ID3v2ExtraMeta *cur;
714
715     for (cur = *extra_meta; cur; cur = cur->next) {
716         ID3v2ExtraMetaAPIC *apic;
717         AVStream *st;
718
719         if (strcmp(cur->tag, "APIC"))
720             continue;
721         apic = cur->data;
722
723         if (!(st = avformat_new_stream(s, NULL)))
724             return AVERROR(ENOMEM);
725
726         st->disposition      |= AV_DISPOSITION_ATTACHED_PIC;
727         st->codec->codec_type = AVMEDIA_TYPE_VIDEO;
728         st->codec->codec_id   = apic->id;
729         av_dict_set(&st->metadata, "title",   apic->description, 0);
730         av_dict_set(&st->metadata, "comment", apic->type, 0);
731
732         av_init_packet(&st->attached_pic);
733         st->attached_pic.data         = apic->data;
734         st->attached_pic.size         = apic->len;
735         st->attached_pic.destruct     = av_destruct_packet;
736         st->attached_pic.stream_index = st->index;
737         st->attached_pic.flags       |= AV_PKT_FLAG_KEY;
738
739         apic->data = NULL;
740         apic->len  = 0;
741     }
742
743     return 0;
744 }