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