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"
26 int ff_id3v2_match(const uint8_t *buf
)
28 return buf
[0] == 'I' &&
33 (buf
[6] & 0x80) == 0 &&
34 (buf
[7] & 0x80) == 0 &&
35 (buf
[8] & 0x80) == 0 &&
39 int ff_id3v2_tag_len(const uint8_t * buf
)
41 int len
= ((buf
[6] & 0x7f) << 21) +
42 ((buf
[7] & 0x7f) << 14) +
43 ((buf
[8] & 0x7f) << 7) +
47 len
+= ID3v2_HEADER_SIZE
;
51 void ff_id3v2_read(AVFormatContext
*s
)
54 uint8_t buf
[ID3v2_HEADER_SIZE
];
56 ret
= get_buffer(s
->pb
, buf
, ID3v2_HEADER_SIZE
);
57 if (ret
!= ID3v2_HEADER_SIZE
)
59 if (ff_id3v2_match(buf
)) {
60 /* parse ID3v2 header */
61 len
= ((buf
[6] & 0x7f) << 21) |
62 ((buf
[7] & 0x7f) << 14) |
63 ((buf
[8] & 0x7f) << 7) |
65 ff_id3v2_parse(s
, len
, buf
[3], buf
[5]);
67 url_fseek(s
->pb
, 0, SEEK_SET
);
71 static unsigned int get_size(ByteIOContext
*s
, int len
)
75 v
= (v
<< 7) + (get_byte(s
) & 0x7F);
79 static void read_ttag(AVFormatContext
*s
, int taglen
, const char *key
)
82 int len
, dstlen
= sizeof(dst
) - 1;
84 unsigned int (*get
)(ByteIOContext
*) = get_be16
;
90 taglen
--; /* account for encoding type byte */
92 switch (get_byte(s
->pb
)) { /* encoding type */
94 case 0: /* ISO-8859-1 (0 - 255 maps directly into unicode) */
96 while (taglen
-- && q
- dst
< dstlen
- 7) {
98 PUT_UTF8(get_byte(s
->pb
), tmp
, *q
++ = tmp
;)
103 case 1: /* UTF-16 with BOM */
105 switch (get_be16(s
->pb
)) {
111 av_log(s
, AV_LOG_ERROR
, "Incorrect BOM value in tag %s.\n", key
);
116 case 2: /* UTF-16BE without BOM */
118 while (taglen
> 1 && q
- dst
< dstlen
- 7) {
122 GET_UTF16(ch
, ((taglen
-= 2) >= 0 ? get(s
->pb
) : 0), break;)
123 PUT_UTF8(ch
, tmp
, *q
++ = tmp
;)
129 len
= FFMIN(taglen
, dstlen
- 1);
130 get_buffer(s
->pb
, dst
, len
);
134 av_log(s
, AV_LOG_WARNING
, "Unknown encoding in tag %s\n.", key
);
137 if (!strcmp(key
, "genre")
138 && (sscanf(dst
, "(%d)", &genre
) == 1 || sscanf(dst
, "%d", &genre
) == 1)
139 && genre
<= ID3v1_GENRE_MAX
)
140 av_strlcpy(dst
, ff_id3v1_genre_str
[genre
], sizeof(dst
));
143 av_metadata_set(&s
->metadata
, key
, dst
);
146 void ff_id3v2_parse(AVFormatContext
*s
, int len
, uint8_t version
, uint8_t flags
)
157 reason
= "compression";
176 reason
= "unsynchronization";
180 if (isv34
&& flags
& 0x40) /* Extended header present, just skip over it */
181 url_fskip(s
->pb
, get_size(s
->pb
, 4));
183 while (len
>= taghdrlen
) {
185 tag
= get_be32(s
->pb
);
187 tlen
= get_be32(s
->pb
);
189 tlen
= get_size(s
->pb
, 4);
190 get_be16(s
->pb
); /* flags */
192 tag
= get_be24(s
->pb
);
193 tlen
= get_be24(s
->pb
);
195 len
-= taghdrlen
+ tlen
;
200 next
= url_ftell(s
->pb
) + tlen
;
203 case MKBETAG('T', 'I', 'T', '2'):
204 case MKBETAG(0, 'T', 'T', '2'):
205 read_ttag(s
, tlen
, "title");
207 case MKBETAG('T', 'P', 'E', '1'):
208 case MKBETAG(0, 'T', 'P', '1'):
209 read_ttag(s
, tlen
, "author");
211 case MKBETAG('T', 'A', 'L', 'B'):
212 case MKBETAG(0, 'T', 'A', 'L'):
213 read_ttag(s
, tlen
, "album");
215 case MKBETAG('T', 'C', 'O', 'N'):
216 case MKBETAG(0, 'T', 'C', 'O'):
217 read_ttag(s
, tlen
, "genre");
219 case MKBETAG('T', 'C', 'O', 'P'):
220 case MKBETAG(0, 'T', 'C', 'R'):
221 read_ttag(s
, tlen
, "copyright");
223 case MKBETAG('T', 'R', 'C', 'K'):
224 case MKBETAG(0, 'T', 'R', 'K'):
225 read_ttag(s
, tlen
, "track");
228 /* padding, skip to end */
229 url_fskip(s
->pb
, len
);
233 /* Skip to end of tag */
234 url_fseek(s
->pb
, next
, SEEK_SET
);
237 if (version
== 4 && flags
& 0x10) /* Footer preset, always 10 bytes, skip over it */
238 url_fskip(s
->pb
, 10);
242 av_log(s
, AV_LOG_INFO
, "ID3v2.%d tag skipped, cannot handle %s\n", version
, reason
);
243 url_fskip(s
->pb
, len
);
246 const AVMetadataConv ff_id3v2_metadata_conv
[] = {
248 { "TCOM", "composer"},
250 { "TCOP", "copyright"},
252 { "TENC", "encoder"},
254 { "TLAN", "language"},
257 { "TPUB", "publisher"},
259 { "TSOA", "albumsort"},
260 { "TSOP", "authorsort"},
261 { "TSOT", "titlesort"},