3 * Copyright (c) 2003 Fabrice Bellard
5 * This file is part of FFmpeg.
7 * FFmpeg 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.
12 * FFmpeg 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.
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with FFmpeg; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
24 #include "libavutil/avstring.h"
25 #include "libavutil/intreadwrite.h"
27 int ff_id3v2_match(const uint8_t *buf, const char * magic)
29 return buf[0] == magic[0] &&
34 (buf[6] & 0x80) == 0 &&
35 (buf[7] & 0x80) == 0 &&
36 (buf[8] & 0x80) == 0 &&
40 int ff_id3v2_tag_len(const uint8_t * buf)
42 int len = ((buf[6] & 0x7f) << 21) +
43 ((buf[7] & 0x7f) << 14) +
44 ((buf[8] & 0x7f) << 7) +
48 len += ID3v2_HEADER_SIZE;
52 void ff_id3v2_read(AVFormatContext *s, const char *magic)
55 uint8_t buf[ID3v2_HEADER_SIZE];
57 ret = get_buffer(s->pb, buf, ID3v2_HEADER_SIZE);
58 if (ret != ID3v2_HEADER_SIZE)
60 if (ff_id3v2_match(buf, magic)) {
61 /* parse ID3v2 header */
62 len = ((buf[6] & 0x7f) << 21) |
63 ((buf[7] & 0x7f) << 14) |
64 ((buf[8] & 0x7f) << 7) |
66 ff_id3v2_parse(s, len, buf[3], buf[5]);
68 url_fseek(s->pb, 0, SEEK_SET);
72 static unsigned int get_size(ByteIOContext *s, int len)
76 v = (v << 7) + (get_byte(s) & 0x7F);
80 static void read_ttag(AVFormatContext *s, int taglen, const char *key)
83 const char *val = NULL;
84 int len, dstlen = sizeof(dst) - 1;
86 unsigned int (*get)(ByteIOContext*) = get_be16;
92 taglen--; /* account for encoding type byte */
94 switch (get_byte(s->pb)) { /* encoding type */
96 case 0: /* ISO-8859-1 (0 - 255 maps directly into unicode) */
98 while (taglen-- && q - dst < dstlen - 7) {
100 PUT_UTF8(get_byte(s->pb), tmp, *q++ = tmp;)
105 case 1: /* UTF-16 with BOM */
107 switch (get_be16(s->pb)) {
113 av_log(s, AV_LOG_ERROR, "Incorrect BOM value in tag %s.\n", key);
118 case 2: /* UTF-16BE without BOM */
120 while (taglen > 1 && q - dst < dstlen - 7) {
124 GET_UTF16(ch, ((taglen -= 2) >= 0 ? get(s->pb) : 0), break;)
125 PUT_UTF8(ch, tmp, *q++ = tmp;)
131 len = FFMIN(taglen, dstlen);
132 get_buffer(s->pb, dst, len);
136 av_log(s, AV_LOG_WARNING, "Unknown encoding in tag %s\n.", key);
139 if (!(strcmp(key, "TCON") && strcmp(key, "TCO"))
140 && (sscanf(dst, "(%d)", &genre) == 1 || sscanf(dst, "%d", &genre) == 1)
141 && genre <= ID3v1_GENRE_MAX)
142 val = ff_id3v1_genre_str[genre];
143 else if (!(strcmp(key, "TXXX") && strcmp(key, "TXX"))) {
144 /* dst now contains two 0-terminated strings */
148 val = dst + FFMIN(len + 1, dstlen);
154 av_metadata_set2(&s->metadata, key, val, 0);
157 void ff_id3v2_parse(AVFormatContext *s, int len, uint8_t version, uint8_t flags)
168 reason = "compression";
187 reason = "unsynchronization";
191 if (isv34 && flags & 0x40) /* Extended header present, just skip over it */
192 url_fskip(s->pb, get_size(s->pb, 4));
194 while (len >= taghdrlen) {
196 get_buffer(s->pb, tag, 4);
199 tlen = get_be32(s->pb);
201 tlen = get_size(s->pb, 4);
202 get_be16(s->pb); /* flags */
204 get_buffer(s->pb, tag, 3);
206 tlen = get_be24(s->pb);
208 len -= taghdrlen + tlen;
213 next = url_ftell(s->pb) + tlen;
216 read_ttag(s, tlen, tag);
219 av_log(s, AV_LOG_WARNING, "invalid frame id, assuming padding");
220 url_fskip(s->pb, tlen);
223 /* Skip to end of tag */
224 url_fseek(s->pb, next, SEEK_SET);
229 url_fskip(s->pb, len);
231 if (version == 4 && flags & 0x10) /* Footer preset, always 10 bytes, skip over it */
232 url_fskip(s->pb, 10);
236 av_log(s, AV_LOG_INFO, "ID3v2.%d tag skipped, cannot handle %s\n", version, reason);
237 url_fskip(s->pb, len);
240 const AVMetadataConv ff_id3v2_metadata_conv[] = {
243 { "TCOM", "composer"},
246 { "TCOP", "copyright"},
249 { "TENC", "encoded_by"},
250 { "TEN", "encoded_by"},
253 { "TLAN", "language"},
256 { "TPE2", "album_artist"},
257 { "TP2", "album_artist"},
258 { "TPE3", "performer"},
259 { "TP3", "performer"},
261 { "TPUB", "publisher"},
264 { "TSOA", "album-sort"},
265 { "TSOP", "artist-sort"},
266 { "TSOT", "title-sort"},
267 { "TSSE", "encoder"},
271 const char ff_id3v2_tags[][4] = {
272 "TALB", "TBPM", "TCOM", "TCON", "TCOP", "TDEN", "TDLY", "TDOR", "TDRC",
273 "TDRL", "TDTG", "TENC", "TEXT", "TFLT", "TIPL", "TIT1", "TIT2", "TIT3",
274 "TKEY", "TLAN", "TLEN", "TMCL", "TMED", "TMOO", "TOAL", "TOFN", "TOLY",
275 "TOPE", "TOWN", "TPE1", "TPE2", "TPE3", "TPE4", "TPOS", "TPRO", "TPUB",
276 "TRCK", "TRSN", "TRSO", "TSOA", "TSOP", "TSOT", "TSRC", "TSSE", "TSST",