2 * Copyright (c) 2003 Fabrice Bellard
4 * This file is part of FFmpeg.
6 * FFmpeg is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2.1 of the License, or (at your option) any later version.
11 * FFmpeg is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with FFmpeg; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
25 * Specifications available at:
26 * http://id3.org/Developer_Information
31 #include "libavutil/avstring.h"
32 #include "libavutil/intreadwrite.h"
33 #include "libavutil/dict.h"
34 #include "avio_internal.h"
36 const AVMetadataConv ff_id3v2_34_metadata_conv[] = {
38 { "TCOM", "composer"},
40 { "TCOP", "copyright"},
41 { "TENC", "encoded_by"},
43 { "TLAN", "language"},
45 { "TPE2", "album_artist"},
46 { "TPE3", "performer"},
48 { "TPUB", "publisher"},
54 const AVMetadataConv ff_id3v2_4_metadata_conv[] = {
57 { "TDEN", "creation_time"},
58 { "TSOA", "album-sort"},
59 { "TSOP", "artist-sort"},
60 { "TSOT", "title-sort"},
64 static const AVMetadataConv id3v2_2_metadata_conv[] = {
68 { "TEN", "encoded_by"},
70 { "TP2", "album_artist"},
71 { "TP3", "performer"},
77 const char ff_id3v2_tags[][4] = {
78 "TALB", "TBPM", "TCOM", "TCON", "TCOP", "TDLY", "TENC", "TEXT",
79 "TFLT", "TIT1", "TIT2", "TIT3", "TKEY", "TLAN", "TLEN", "TMED",
80 "TOAL", "TOFN", "TOLY", "TOPE", "TOWN", "TPE1", "TPE2", "TPE3",
81 "TPE4", "TPOS", "TPUB", "TRCK", "TRSN", "TRSO", "TSRC", "TSSE",
85 const char ff_id3v2_4_tags[][4] = {
86 "TDEN", "TDOR", "TDRC", "TDRL", "TDTG", "TIPL", "TMCL", "TMOO",
87 "TPRO", "TSOA", "TSOP", "TSOT", "TSST",
91 const char ff_id3v2_3_tags[][4] = {
92 "TDAT", "TIME", "TORY", "TRDA", "TSIZ", "TYER",
96 int ff_id3v2_match(const uint8_t *buf, const char * magic)
98 return buf[0] == magic[0] &&
100 buf[2] == magic[2] &&
103 (buf[6] & 0x80) == 0 &&
104 (buf[7] & 0x80) == 0 &&
105 (buf[8] & 0x80) == 0 &&
106 (buf[9] & 0x80) == 0;
109 int ff_id3v2_tag_len(const uint8_t * buf)
111 int len = ((buf[6] & 0x7f) << 21) +
112 ((buf[7] & 0x7f) << 14) +
113 ((buf[8] & 0x7f) << 7) +
117 len += ID3v2_HEADER_SIZE;
121 static unsigned int get_size(AVIOContext *s, int len)
125 v = (v << 7) + (avio_r8(s) & 0x7F);
130 * Free GEOB type extra metadata.
132 static void free_geobtag(void *obj)
134 ID3v2ExtraMetaGEOB *geob = obj;
135 av_free(geob->mime_type);
136 av_free(geob->file_name);
137 av_free(geob->description);
143 * Decode characters to UTF-8 according to encoding type. The decoded buffer is
144 * always null terminated. Stop reading when either *maxread bytes are read from
145 * pb or U+0000 character is found.
147 * @param dst Pointer where the address of the buffer with the decoded bytes is
148 * stored. Buffer must be freed by caller.
149 * @param maxread Pointer to maximum number of characters to read from the
150 * AVIOContext. After execution the value is decremented by the number of bytes
152 * @returns 0 if no error occured, dst is uninitialized on error
154 static int decode_str(AVFormatContext *s, AVIOContext *pb, int encoding,
155 uint8_t **dst, int *maxread)
161 unsigned int (*get)(AVIOContext*) = avio_rb16;
164 if ((ret = avio_open_dyn_buf(&dynbuf)) < 0) {
165 av_log(s, AV_LOG_ERROR, "Error opening memory stream\n");
171 case ID3v2_ENCODING_ISO8859:
174 PUT_UTF8(ch, tmp, avio_w8(dynbuf, tmp);)
179 case ID3v2_ENCODING_UTF16BOM:
180 if ((left -= 2) < 0) {
181 av_log(s, AV_LOG_ERROR, "Cannot read BOM value, input too short\n");
182 avio_close_dyn_buf(dynbuf, dst);
184 return AVERROR_INVALIDDATA;
186 switch (avio_rb16(pb)) {
192 av_log(s, AV_LOG_ERROR, "Incorrect BOM value\n");
193 avio_close_dyn_buf(dynbuf, dst);
196 return AVERROR_INVALIDDATA;
200 case ID3v2_ENCODING_UTF16BE:
201 while ((left > 1) && ch) {
202 GET_UTF16(ch, ((left -= 2) >= 0 ? get(pb) : 0), break;)
203 PUT_UTF8(ch, tmp, avio_w8(dynbuf, tmp);)
206 left += 2; /* did not read last char from pb */
209 case ID3v2_ENCODING_UTF8:
217 av_log(s, AV_LOG_WARNING, "Unknown encoding\n");
223 avio_close_dyn_buf(dynbuf, dst);
232 static void read_ttag(AVFormatContext *s, AVIOContext *pb, int taglen, const char *key)
235 int encoding, dict_flags = AV_DICT_DONT_OVERWRITE;
241 encoding = avio_r8(pb);
242 taglen--; /* account for encoding type byte */
244 if (decode_str(s, pb, encoding, &dst, &taglen) < 0) {
245 av_log(s, AV_LOG_ERROR, "Error reading frame %s, skipped\n", key);
249 if (!(strcmp(key, "TCON") && strcmp(key, "TCO"))
250 && (sscanf(dst, "(%d)", &genre) == 1 || sscanf(dst, "%d", &genre) == 1)
251 && genre <= ID3v1_GENRE_MAX) {
253 dst = ff_id3v1_genre_str[genre];
254 } else if (!(strcmp(key, "TXXX") && strcmp(key, "TXX"))) {
255 /* dst now contains the key, need to get value */
257 if (decode_str(s, pb, encoding, &dst, &taglen) < 0) {
258 av_log(s, AV_LOG_ERROR, "Error reading frame %s, skipped\n", key);
262 dict_flags |= AV_DICT_DONT_STRDUP_VAL | AV_DICT_DONT_STRDUP_KEY;
265 dict_flags |= AV_DICT_DONT_STRDUP_VAL;
268 av_dict_set(&s->metadata, key, dst, dict_flags);
272 * Parse GEOB tag into a ID3v2ExtraMetaGEOB struct.
274 static void read_geobtag(AVFormatContext *s, AVIOContext *pb, int taglen, char *tag, ID3v2ExtraMeta **extra_meta)
276 ID3v2ExtraMetaGEOB *geob_data = NULL;
277 ID3v2ExtraMeta *new_extra = NULL;
284 geob_data = av_mallocz(sizeof(ID3v2ExtraMetaGEOB));
286 av_log(s, AV_LOG_ERROR, "Failed to alloc %zu bytes\n", sizeof(ID3v2ExtraMetaGEOB));
290 new_extra = av_mallocz(sizeof(ID3v2ExtraMeta));
292 av_log(s, AV_LOG_ERROR, "Failed to alloc %zu bytes\n", sizeof(ID3v2ExtraMeta));
296 /* read encoding type byte */
297 encoding = avio_r8(pb);
300 /* read MIME type (always ISO-8859) */
301 if (decode_str(s, pb, ID3v2_ENCODING_ISO8859, &geob_data->mime_type, &taglen) < 0
306 if (decode_str(s, pb, encoding, &geob_data->file_name, &taglen) < 0
310 /* read content description */
311 if (decode_str(s, pb, encoding, &geob_data->description, &taglen) < 0
316 /* save encapsulated binary data */
317 geob_data->data = av_malloc(taglen);
318 if (!geob_data->data) {
319 av_log(s, AV_LOG_ERROR, "Failed to alloc %d bytes\n", taglen);
322 if ((len = avio_read(pb, geob_data->data, taglen)) < taglen)
323 av_log(s, AV_LOG_WARNING, "Error reading GEOB frame, data truncated.\n");
324 geob_data->datasize = len;
326 geob_data->data = NULL;
327 geob_data->datasize = 0;
330 /* add data to the list */
331 new_extra->tag = "GEOB";
332 new_extra->data = geob_data;
333 new_extra->next = *extra_meta;
334 *extra_meta = new_extra;
339 av_log(s, AV_LOG_ERROR, "Error reading frame %s, skipped\n", tag);
340 free_geobtag(geob_data);
345 static int is_number(const char *str)
347 while (*str >= '0' && *str <= '9') str++;
351 static AVDictionaryEntry* get_date_tag(AVDictionary *m, const char *tag)
353 AVDictionaryEntry *t;
354 if ((t = av_dict_get(m, tag, NULL, AV_DICT_MATCH_CASE)) &&
355 strlen(t->value) == 4 && is_number(t->value))
360 static void merge_date(AVDictionary **m)
362 AVDictionaryEntry *t;
363 char date[17] = {0}; // YYYY-MM-DD hh:mm
365 if (!(t = get_date_tag(*m, "TYER")) &&
366 !(t = get_date_tag(*m, "TYE")))
368 av_strlcpy(date, t->value, 5);
369 av_dict_set(m, "TYER", NULL, 0);
370 av_dict_set(m, "TYE", NULL, 0);
372 if (!(t = get_date_tag(*m, "TDAT")) &&
373 !(t = get_date_tag(*m, "TDA")))
375 snprintf(date + 4, sizeof(date) - 4, "-%.2s-%.2s", t->value + 2, t->value);
376 av_dict_set(m, "TDAT", NULL, 0);
377 av_dict_set(m, "TDA", NULL, 0);
379 if (!(t = get_date_tag(*m, "TIME")) &&
380 !(t = get_date_tag(*m, "TIM")))
382 snprintf(date + 10, sizeof(date) - 10, " %.2s:%.2s", t->value, t->value + 2);
383 av_dict_set(m, "TIME", NULL, 0);
384 av_dict_set(m, "TIM", NULL, 0);
388 av_dict_set(m, "date", date, 0);
391 typedef struct ID3v2EMFunc {
394 void (*read)(AVFormatContext*, AVIOContext*, int, char*, ID3v2ExtraMeta **);
395 void (*free)(void *obj);
398 static const ID3v2EMFunc id3v2_extra_meta_funcs[] = {
399 { "GEO", "GEOB", read_geobtag, free_geobtag },
404 * Get the corresponding ID3v2EMFunc struct for a tag.
405 * @param isv34 Determines if v2.2 or v2.3/4 strings are used
406 * @return A pointer to the ID3v2EMFunc struct if found, NULL otherwise.
408 static const ID3v2EMFunc *get_extra_meta_func(const char *tag, int isv34)
411 while (id3v2_extra_meta_funcs[i].tag3) {
413 (isv34 ? id3v2_extra_meta_funcs[i].tag4 :
414 id3v2_extra_meta_funcs[i].tag3),
416 return &id3v2_extra_meta_funcs[i];
419 return &id3v2_extra_meta_funcs[i];
422 static void ff_id3v2_parse(AVFormatContext *s, int len, uint8_t version, uint8_t flags, ID3v2ExtraMeta **extra_meta)
427 int64_t next, end = avio_tell(s->pb) + len;
429 const char *reason = NULL;
432 unsigned char *buffer = NULL;
434 const ID3v2EMFunc *extra_func;
439 reason = "compression";
457 unsync = flags & 0x80;
459 if (isv34 && flags & 0x40) /* Extended header present, just skip over it */
460 avio_skip(s->pb, get_size(s->pb, 4));
462 while (len >= taghdrlen) {
463 unsigned int tflags = 0;
467 avio_read(s->pb, tag, 4);
470 tlen = avio_rb32(s->pb);
472 tlen = get_size(s->pb, 4);
473 tflags = avio_rb16(s->pb);
474 tunsync = tflags & ID3v2_FLAG_UNSYNCH;
476 avio_read(s->pb, tag, 3);
478 tlen = avio_rb24(s->pb);
482 len -= taghdrlen + tlen;
487 next = avio_tell(s->pb) + tlen;
491 av_log(s, AV_LOG_DEBUG, "Invalid empty frame %s, skipping.\n", tag);
495 if (tflags & ID3v2_FLAG_DATALEN) {
502 if (tflags & (ID3v2_FLAG_ENCRYPTION | ID3v2_FLAG_COMPRESSION)) {
503 av_log(s, AV_LOG_WARNING, "Skipping encrypted/compressed ID3v2 frame %s.\n", tag);
504 avio_skip(s->pb, tlen);
505 /* check for text tag or supported special meta tag */
506 } else if (tag[0] == 'T' || (extra_meta && (extra_func = get_extra_meta_func(tag, isv34)))) {
507 if (unsync || tunsync) {
509 av_fast_malloc(&buffer, &buffer_size, tlen);
511 av_log(s, AV_LOG_ERROR, "Failed to alloc %d bytes\n", tlen);
514 for (i = 0, j = 0; i < tlen; i++, j++) {
515 buffer[j] = avio_r8(s->pb);
516 if (j > 0 && !buffer[j] && buffer[j - 1] == 0xff) {
517 /* Unsynchronised byte, skip it */
521 ffio_init_context(&pb, buffer, j, 0, NULL, NULL, NULL, NULL);
523 pbx = &pb; // read from sync buffer
525 pbx = s->pb; // read straight from input
529 read_ttag(s, pbx, tlen, tag);
531 /* parse special meta tag */
532 extra_func->read(s, pbx, tlen, tag, extra_meta);
536 av_log(s, AV_LOG_WARNING, "invalid frame id, assuming padding");
537 avio_skip(s->pb, tlen);
540 /* Skip to end of tag */
542 avio_seek(s->pb, next, SEEK_SET);
545 if (version == 4 && flags & 0x10) /* Footer preset, always 10 bytes, skip over it */
550 av_log(s, AV_LOG_INFO, "ID3v2.%d tag skipped, cannot handle %s\n", version, reason);
551 avio_seek(s->pb, end, SEEK_SET);
556 void ff_id3v2_read_all(AVFormatContext *s, const char *magic, ID3v2ExtraMeta **extra_meta)
559 uint8_t buf[ID3v2_HEADER_SIZE];
564 /* save the current offset in case there's nothing to read/skip */
565 off = avio_tell(s->pb);
566 ret = avio_read(s->pb, buf, ID3v2_HEADER_SIZE);
567 if (ret != ID3v2_HEADER_SIZE)
569 found_header = ff_id3v2_match(buf, magic);
571 /* parse ID3v2 header */
572 len = ((buf[6] & 0x7f) << 21) |
573 ((buf[7] & 0x7f) << 14) |
574 ((buf[8] & 0x7f) << 7) |
576 ff_id3v2_parse(s, len, buf[3], buf[5], extra_meta);
578 avio_seek(s->pb, off, SEEK_SET);
580 } while (found_header);
581 ff_metadata_conv(&s->metadata, NULL, ff_id3v2_34_metadata_conv);
582 ff_metadata_conv(&s->metadata, NULL, id3v2_2_metadata_conv);
583 ff_metadata_conv(&s->metadata, NULL, ff_id3v2_4_metadata_conv);
584 merge_date(&s->metadata);
587 void ff_id3v2_read(AVFormatContext *s, const char *magic)
589 ff_id3v2_read_all(s, magic, NULL);
592 void ff_id3v2_free_extra_meta(ID3v2ExtraMeta **extra_meta)
594 ID3v2ExtraMeta *current = *extra_meta, *next;
595 const ID3v2EMFunc *extra_func;
598 if ((extra_func = get_extra_meta_func(current->tag, 1)))
599 extra_func->free(current->data);
600 next = current->next;