]> git.sesse.net Git - ffmpeg/blob - libavformat/id3v2.c
shorten: use bytestream functions to decode the embedded WAVE header
[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
29 const AVMetadataConv ff_id3v2_34_metadata_conv[] = {
30     { "TALB", "album"},
31     { "TCOM", "composer"},
32     { "TCON", "genre"},
33     { "TCOP", "copyright"},
34     { "TENC", "encoded_by"},
35     { "TIT2", "title"},
36     { "TLAN", "language"},
37     { "TPE1", "artist"},
38     { "TPE2", "album_artist"},
39     { "TPE3", "performer"},
40     { "TPOS", "disc"},
41     { "TPUB", "publisher"},
42     { "TRCK", "track"},
43     { "TSSE", "encoder"},
44     { 0 }
45 };
46
47 const AVMetadataConv ff_id3v2_4_metadata_conv[] = {
48     { "TDRL", "date"},
49     { "TDRC", "date"},
50     { "TDEN", "creation_time"},
51     { "TSOA", "album-sort"},
52     { "TSOP", "artist-sort"},
53     { "TSOT", "title-sort"},
54     { 0 }
55 };
56
57 static const AVMetadataConv id3v2_2_metadata_conv[] = {
58     { "TAL",  "album"},
59     { "TCO",  "genre"},
60     { "TT2",  "title"},
61     { "TEN",  "encoded_by"},
62     { "TP1",  "artist"},
63     { "TP2",  "album_artist"},
64     { "TP3",  "performer"},
65     { "TRK",  "track"},
66     { 0 }
67 };
68
69
70 const char ff_id3v2_tags[][4] = {
71    "TALB", "TBPM", "TCOM", "TCON", "TCOP", "TDLY", "TENC", "TEXT",
72    "TFLT", "TIT1", "TIT2", "TIT3", "TKEY", "TLAN", "TLEN", "TMED",
73    "TOAL", "TOFN", "TOLY", "TOPE", "TOWN", "TPE1", "TPE2", "TPE3",
74    "TPE4", "TPOS", "TPUB", "TRCK", "TRSN", "TRSO", "TSRC", "TSSE",
75    { 0 },
76 };
77
78 const char ff_id3v2_4_tags[][4] = {
79    "TDEN", "TDOR", "TDRC", "TDRL", "TDTG", "TIPL", "TMCL", "TMOO",
80    "TPRO", "TSOA", "TSOP", "TSOT", "TSST",
81    { 0 },
82 };
83
84 const char ff_id3v2_3_tags[][4] = {
85    "TDAT", "TIME", "TORY", "TRDA", "TSIZ", "TYER",
86    { 0 },
87 };
88
89 int ff_id3v2_match(const uint8_t *buf, const char * magic)
90 {
91     return  buf[0]         == magic[0] &&
92             buf[1]         == magic[1] &&
93             buf[2]         == magic[2] &&
94             buf[3]         != 0xff &&
95             buf[4]         != 0xff &&
96            (buf[6] & 0x80) ==    0 &&
97            (buf[7] & 0x80) ==    0 &&
98            (buf[8] & 0x80) ==    0 &&
99            (buf[9] & 0x80) ==    0;
100 }
101
102 int ff_id3v2_tag_len(const uint8_t * buf)
103 {
104     int len = ((buf[6] & 0x7f) << 21) +
105               ((buf[7] & 0x7f) << 14) +
106               ((buf[8] & 0x7f) << 7) +
107                (buf[9] & 0x7f) +
108               ID3v2_HEADER_SIZE;
109     if (buf[5] & 0x10)
110         len += ID3v2_HEADER_SIZE;
111     return len;
112 }
113
114 static unsigned int get_size(AVIOContext *s, int len)
115 {
116     int v = 0;
117     while (len--)
118         v = (v << 7) + (avio_r8(s) & 0x7F);
119     return v;
120 }
121
122 /**
123  * Free GEOB type extra metadata.
124  */
125 static void free_geobtag(ID3v2ExtraMetaGEOB *geob)
126 {
127     av_free(geob->mime_type);
128     av_free(geob->file_name);
129     av_free(geob->description);
130     av_free(geob->data);
131     av_free(geob);
132 }
133
134 /**
135  * Decode characters to UTF-8 according to encoding type. The decoded buffer is
136  * always null terminated. Stop reading when either *maxread bytes are read from
137  * pb or U+0000 character is found.
138  *
139  * @param dst Pointer where the address of the buffer with the decoded bytes is
140  * stored. Buffer must be freed by caller.
141  * @param maxread Pointer to maximum number of characters to read from the
142  * AVIOContext. After execution the value is decremented by the number of bytes
143  * actually read.
144  * @returns 0 if no error occured, dst is uninitialized on error
145  */
146 static int decode_str(AVFormatContext *s, AVIOContext *pb, int encoding,
147                       uint8_t **dst, int *maxread)
148 {
149     int ret;
150     uint8_t tmp;
151     uint32_t ch = 1;
152     int left = *maxread;
153     unsigned int (*get)(AVIOContext*) = avio_rb16;
154     AVIOContext *dynbuf;
155
156     if ((ret = avio_open_dyn_buf(&dynbuf)) < 0) {
157         av_log(s, AV_LOG_ERROR, "Error opening memory stream\n");
158         return ret;
159     }
160
161     switch (encoding) {
162
163     case ID3v2_ENCODING_ISO8859:
164         while (left && ch) {
165             ch = avio_r8(pb);
166             PUT_UTF8(ch, tmp, avio_w8(dynbuf, tmp);)
167             left--;
168         }
169         break;
170
171     case ID3v2_ENCODING_UTF16BOM:
172         if ((left -= 2) < 0) {
173             av_log(s, AV_LOG_ERROR, "Cannot read BOM value, input too short\n");
174             avio_close_dyn_buf(dynbuf, dst);
175             av_freep(dst);
176             return AVERROR_INVALIDDATA;
177         }
178         switch (avio_rb16(pb)) {
179         case 0xfffe:
180             get = avio_rl16;
181         case 0xfeff:
182             break;
183         default:
184             av_log(s, AV_LOG_ERROR, "Incorrect BOM value\n");
185             avio_close_dyn_buf(dynbuf, dst);
186             av_freep(dst);
187             *maxread = left;
188             return AVERROR_INVALIDDATA;
189         }
190         // fall-through
191
192     case ID3v2_ENCODING_UTF16BE:
193         while ((left > 1) && ch) {
194             GET_UTF16(ch, ((left -= 2) >= 0 ? get(pb) : 0), break;)
195             PUT_UTF8(ch, tmp, avio_w8(dynbuf, tmp);)
196         }
197         if (left < 0)
198             left += 2; /* did not read last char from pb */
199         break;
200
201     case ID3v2_ENCODING_UTF8:
202         while (left && ch) {
203             ch = avio_r8(pb);
204             avio_w8(dynbuf, ch);
205             left--;
206         }
207         break;
208     default:
209         av_log(s, AV_LOG_WARNING, "Unknown encoding\n");
210     }
211
212     if (ch)
213         avio_w8(dynbuf, 0);
214
215     avio_close_dyn_buf(dynbuf, dst);
216     *maxread = left;
217
218     return 0;
219 }
220
221 /**
222  * Parse a text tag.
223  */
224 static void read_ttag(AVFormatContext *s, AVIOContext *pb, int taglen, const char *key)
225 {
226     uint8_t *dst;
227     int encoding, dict_flags = AV_DICT_DONT_OVERWRITE;
228     unsigned genre;
229
230     if (taglen < 1)
231         return;
232
233     encoding = avio_r8(pb);
234     taglen--; /* account for encoding type byte */
235
236     if (decode_str(s, pb, encoding, &dst, &taglen) < 0) {
237         av_log(s, AV_LOG_ERROR, "Error reading frame %s, skipped\n", key);
238         return;
239     }
240
241     if (!(strcmp(key, "TCON") && strcmp(key, "TCO"))
242         && (sscanf(dst, "(%d)", &genre) == 1 || sscanf(dst, "%d", &genre) == 1)
243         && genre <= ID3v1_GENRE_MAX) {
244         av_freep(&dst);
245         dst = ff_id3v1_genre_str[genre];
246     } else if (!(strcmp(key, "TXXX") && strcmp(key, "TXX"))) {
247         /* dst now contains the key, need to get value */
248         key = dst;
249         if (decode_str(s, pb, encoding, &dst, &taglen) < 0) {
250             av_log(s, AV_LOG_ERROR, "Error reading frame %s, skipped\n", key);
251             av_freep(&key);
252             return;
253         }
254         dict_flags |= AV_DICT_DONT_STRDUP_VAL | AV_DICT_DONT_STRDUP_KEY;
255     }
256     else if (*dst)
257         dict_flags |= AV_DICT_DONT_STRDUP_VAL;
258
259     if (dst)
260         av_dict_set(&s->metadata, key, dst, dict_flags);
261 }
262
263 /**
264  * Parse GEOB tag into a ID3v2ExtraMetaGEOB struct.
265  */
266 static void read_geobtag(AVFormatContext *s, AVIOContext *pb, int taglen, char *tag, ID3v2ExtraMeta **extra_meta)
267 {
268     ID3v2ExtraMetaGEOB *geob_data = NULL;
269     ID3v2ExtraMeta *new_extra = NULL;
270     char encoding;
271     unsigned int len;
272
273     if (taglen < 1)
274         return;
275
276     geob_data = av_mallocz(sizeof(ID3v2ExtraMetaGEOB));
277     if (!geob_data) {
278         av_log(s, AV_LOG_ERROR, "Failed to alloc %zu bytes\n", sizeof(ID3v2ExtraMetaGEOB));
279         return;
280     }
281
282     new_extra = av_mallocz(sizeof(ID3v2ExtraMeta));
283     if (!new_extra) {
284         av_log(s, AV_LOG_ERROR, "Failed to alloc %zu bytes\n", sizeof(ID3v2ExtraMeta));
285         goto fail;
286     }
287
288     /* read encoding type byte */
289     encoding = avio_r8(pb);
290     taglen--;
291
292     /* read MIME type (always ISO-8859) */
293     if (decode_str(s, pb, ID3v2_ENCODING_ISO8859, &geob_data->mime_type, &taglen) < 0
294         || taglen <= 0)
295         goto fail;
296
297     /* read file name */
298     if (decode_str(s, pb, encoding, &geob_data->file_name, &taglen) < 0
299         || taglen <= 0)
300         goto fail;
301
302     /* read content description */
303     if (decode_str(s, pb, encoding, &geob_data->description, &taglen) < 0
304         || taglen < 0)
305         goto fail;
306
307     if (taglen) {
308         /* save encapsulated binary data */
309         geob_data->data = av_malloc(taglen);
310         if (!geob_data->data) {
311             av_log(s, AV_LOG_ERROR, "Failed to alloc %d bytes\n", taglen);
312             goto fail;
313         }
314         if ((len = avio_read(pb, geob_data->data, taglen)) < taglen)
315             av_log(s, AV_LOG_WARNING, "Error reading GEOB frame, data truncated.\n");
316         geob_data->datasize = len;
317     } else {
318         geob_data->data = NULL;
319         geob_data->datasize = 0;
320     }
321
322     /* add data to the list */
323     new_extra->tag = "GEOB";
324     new_extra->data = geob_data;
325     new_extra->next = *extra_meta;
326     *extra_meta = new_extra;
327
328     return;
329
330 fail:
331     av_log(s, AV_LOG_ERROR, "Error reading frame %s, skipped\n", tag);
332     free_geobtag(geob_data);
333     av_free(new_extra);
334     return;
335 }
336
337 static int is_number(const char *str)
338 {
339     while (*str >= '0' && *str <= '9') str++;
340     return !*str;
341 }
342
343 static AVDictionaryEntry* get_date_tag(AVDictionary *m, const char *tag)
344 {
345     AVDictionaryEntry *t;
346     if ((t = av_dict_get(m, tag, NULL, AV_DICT_MATCH_CASE)) &&
347         strlen(t->value) == 4 && is_number(t->value))
348         return t;
349     return NULL;
350 }
351
352 static void merge_date(AVDictionary **m)
353 {
354     AVDictionaryEntry *t;
355     char date[17] = {0};      // YYYY-MM-DD hh:mm
356
357     if (!(t = get_date_tag(*m, "TYER")) &&
358         !(t = get_date_tag(*m, "TYE")))
359         return;
360     av_strlcpy(date, t->value, 5);
361     av_dict_set(m, "TYER", NULL, 0);
362     av_dict_set(m, "TYE",  NULL, 0);
363
364     if (!(t = get_date_tag(*m, "TDAT")) &&
365         !(t = get_date_tag(*m, "TDA")))
366         goto finish;
367     snprintf(date + 4, sizeof(date) - 4, "-%.2s-%.2s", t->value + 2, t->value);
368     av_dict_set(m, "TDAT", NULL, 0);
369     av_dict_set(m, "TDA",  NULL, 0);
370
371     if (!(t = get_date_tag(*m, "TIME")) &&
372         !(t = get_date_tag(*m, "TIM")))
373         goto finish;
374     snprintf(date + 10, sizeof(date) - 10, " %.2s:%.2s", t->value, t->value + 2);
375     av_dict_set(m, "TIME", NULL, 0);
376     av_dict_set(m, "TIM",  NULL, 0);
377
378 finish:
379     if (date[0])
380         av_dict_set(m, "date", date, 0);
381 }
382
383 typedef struct ID3v2EMFunc {
384     const char *tag3;
385     const char *tag4;
386     void (*read)(AVFormatContext*, AVIOContext*, int, char*, ID3v2ExtraMeta **);
387     void (*free)();
388 } ID3v2EMFunc;
389
390 static const ID3v2EMFunc id3v2_extra_meta_funcs[] = {
391     { "GEO", "GEOB", read_geobtag, free_geobtag },
392     { NULL }
393 };
394
395 /**
396  * Get the corresponding ID3v2EMFunc struct for a tag.
397  * @param isv34 Determines if v2.2 or v2.3/4 strings are used
398  * @return A pointer to the ID3v2EMFunc struct if found, NULL otherwise.
399  */
400 static const ID3v2EMFunc *get_extra_meta_func(const char *tag, int isv34)
401 {
402     int i = 0;
403     while (id3v2_extra_meta_funcs[i].tag3) {
404         if (!memcmp(tag,
405                     (isv34 ? id3v2_extra_meta_funcs[i].tag4 :
406                              id3v2_extra_meta_funcs[i].tag3),
407                     (isv34 ? 4 : 3)))
408             return &id3v2_extra_meta_funcs[i];
409         i++;
410     }
411     return NULL;
412 }
413
414 static void ff_id3v2_parse(AVFormatContext *s, int len, uint8_t version, uint8_t flags, ID3v2ExtraMeta **extra_meta)
415 {
416     int isv34, tlen, unsync;
417     char tag[5];
418     int64_t next, end = avio_tell(s->pb) + len;
419     int taghdrlen;
420     const char *reason = NULL;
421     AVIOContext pb;
422     AVIOContext *pbx;
423     unsigned char *buffer = NULL;
424     int buffer_size = 0;
425     const ID3v2EMFunc *extra_func;
426
427     switch (version) {
428     case 2:
429         if (flags & 0x40) {
430             reason = "compression";
431             goto error;
432         }
433         isv34 = 0;
434         taghdrlen = 6;
435         break;
436
437     case 3:
438     case 4:
439         isv34 = 1;
440         taghdrlen = 10;
441         break;
442
443     default:
444         reason = "version";
445         goto error;
446     }
447
448     unsync = flags & 0x80;
449
450     if (isv34 && flags & 0x40) /* Extended header present, just skip over it */
451         avio_skip(s->pb, get_size(s->pb, 4));
452
453     while (len >= taghdrlen) {
454         unsigned int tflags = 0;
455         int tunsync = 0;
456
457         if (isv34) {
458             avio_read(s->pb, tag, 4);
459             tag[4] = 0;
460             if(version==3){
461                 tlen = avio_rb32(s->pb);
462             }else
463                 tlen = get_size(s->pb, 4);
464             tflags = avio_rb16(s->pb);
465             tunsync = tflags & ID3v2_FLAG_UNSYNCH;
466         } else {
467             avio_read(s->pb, tag, 3);
468             tag[3] = 0;
469             tlen = avio_rb24(s->pb);
470         }
471         if (tlen < 0 || tlen > len - taghdrlen) {
472             av_log(s, AV_LOG_WARNING, "Invalid size in frame %s, skipping the rest of tag.\n", tag);
473             break;
474         }
475         len -= taghdrlen + tlen;
476         next = avio_tell(s->pb) + tlen;
477
478         if (!tlen) {
479             if (tag[0])
480                 av_log(s, AV_LOG_DEBUG, "Invalid empty frame %s, skipping.\n", tag);
481             continue;
482         }
483
484         if (tflags & ID3v2_FLAG_DATALEN) {
485             avio_rb32(s->pb);
486             tlen -= 4;
487         }
488
489         if (tflags & (ID3v2_FLAG_ENCRYPTION | ID3v2_FLAG_COMPRESSION)) {
490             av_log(s, AV_LOG_WARNING, "Skipping encrypted/compressed ID3v2 frame %s.\n", tag);
491             avio_skip(s->pb, tlen);
492         /* check for text tag or supported special meta tag */
493         } else if (tag[0] == 'T' || (extra_meta && (extra_func = get_extra_meta_func(tag, isv34)))) {
494             if (unsync || tunsync) {
495                 int i, j;
496                 av_fast_malloc(&buffer, &buffer_size, tlen);
497                 if (!buffer) {
498                     av_log(s, AV_LOG_ERROR, "Failed to alloc %d bytes\n", tlen);
499                     goto seek;
500                 }
501                 for (i = 0, j = 0; i < tlen; i++, j++) {
502                     buffer[j] = avio_r8(s->pb);
503                     if (j > 0 && !buffer[j] && buffer[j - 1] == 0xff) {
504                         /* Unsynchronised byte, skip it */
505                         j--;
506                     }
507                 }
508                 ffio_init_context(&pb, buffer, j, 0, NULL, NULL, NULL, NULL);
509                 tlen = j;
510                 pbx = &pb; // read from sync buffer
511             } else {
512                 pbx = s->pb; // read straight from input
513             }
514             if (tag[0] == 'T')
515                 /* parse text tag */
516                 read_ttag(s, pbx, tlen, tag);
517             else
518                 /* parse special meta tag */
519                 extra_func->read(s, pbx, tlen, tag, extra_meta);
520         }
521         else if (!tag[0]) {
522             if (tag[1])
523                 av_log(s, AV_LOG_WARNING, "invalid frame id, assuming padding");
524             avio_skip(s->pb, tlen);
525             break;
526         }
527         /* Skip to end of tag */
528 seek:
529         avio_seek(s->pb, next, SEEK_SET);
530     }
531
532     if (version == 4 && flags & 0x10) /* Footer preset, always 10 bytes, skip over it */
533         end += 10;
534
535   error:
536     if (reason)
537         av_log(s, AV_LOG_INFO, "ID3v2.%d tag skipped, cannot handle %s\n", version, reason);
538     avio_seek(s->pb, end, SEEK_SET);
539     av_free(buffer);
540     return;
541 }
542
543 void ff_id3v2_read_all(AVFormatContext *s, const char *magic, ID3v2ExtraMeta **extra_meta)
544 {
545     int len, ret;
546     uint8_t buf[ID3v2_HEADER_SIZE];
547     int     found_header;
548     int64_t off;
549
550     do {
551         /* save the current offset in case there's nothing to read/skip */
552         off = avio_tell(s->pb);
553         ret = avio_read(s->pb, buf, ID3v2_HEADER_SIZE);
554         if (ret != ID3v2_HEADER_SIZE)
555             break;
556             found_header = ff_id3v2_match(buf, magic);
557             if (found_header) {
558             /* parse ID3v2 header */
559             len = ((buf[6] & 0x7f) << 21) |
560                   ((buf[7] & 0x7f) << 14) |
561                   ((buf[8] & 0x7f) << 7) |
562                    (buf[9] & 0x7f);
563             ff_id3v2_parse(s, len, buf[3], buf[5], extra_meta);
564         } else {
565             avio_seek(s->pb, off, SEEK_SET);
566         }
567     } while (found_header);
568     ff_metadata_conv(&s->metadata, NULL, ff_id3v2_34_metadata_conv);
569     ff_metadata_conv(&s->metadata, NULL, id3v2_2_metadata_conv);
570     ff_metadata_conv(&s->metadata, NULL, ff_id3v2_4_metadata_conv);
571     merge_date(&s->metadata);
572 }
573
574 void ff_id3v2_read(AVFormatContext *s, const char *magic)
575 {
576     ff_id3v2_read_all(s, magic, NULL);
577 }
578
579 void ff_id3v2_free_extra_meta(ID3v2ExtraMeta **extra_meta)
580 {
581     ID3v2ExtraMeta *current = *extra_meta, *next;
582     const ID3v2EMFunc *extra_func;
583
584     while (current) {
585         if ((extra_func = get_extra_meta_func(current->tag, 1)))
586             extra_func->free(current->data);
587         next = current->next;
588         av_freep(&current);
589         current = next;
590     }
591 }