annotate id3v2.c @ 6391:d05e8fb04c6f libavformat

Add Apple HTTP Live Streaming demuxer
author mstorsjo
date Thu, 19 Aug 2010 14:54:37 +0000
parents dd54b36d1b8c
children 1a62da965733
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
4221
55f448c99135 Factorise id3v2 header parsing from mp3.c to be shared
superdump
parents:
diff changeset
1 /*
55f448c99135 Factorise id3v2 header parsing from mp3.c to be shared
superdump
parents:
diff changeset
2 * ID3v2 header parser
55f448c99135 Factorise id3v2 header parsing from mp3.c to be shared
superdump
parents:
diff changeset
3 * Copyright (c) 2003 Fabrice Bellard
55f448c99135 Factorise id3v2 header parsing from mp3.c to be shared
superdump
parents:
diff changeset
4 *
55f448c99135 Factorise id3v2 header parsing from mp3.c to be shared
superdump
parents:
diff changeset
5 * This file is part of FFmpeg.
55f448c99135 Factorise id3v2 header parsing from mp3.c to be shared
superdump
parents:
diff changeset
6 *
55f448c99135 Factorise id3v2 header parsing from mp3.c to be shared
superdump
parents:
diff changeset
7 * FFmpeg is free software; you can redistribute it and/or
55f448c99135 Factorise id3v2 header parsing from mp3.c to be shared
superdump
parents:
diff changeset
8 * modify it under the terms of the GNU Lesser General Public
55f448c99135 Factorise id3v2 header parsing from mp3.c to be shared
superdump
parents:
diff changeset
9 * License as published by the Free Software Foundation; either
55f448c99135 Factorise id3v2 header parsing from mp3.c to be shared
superdump
parents:
diff changeset
10 * version 2.1 of the License, or (at your option) any later version.
55f448c99135 Factorise id3v2 header parsing from mp3.c to be shared
superdump
parents:
diff changeset
11 *
55f448c99135 Factorise id3v2 header parsing from mp3.c to be shared
superdump
parents:
diff changeset
12 * FFmpeg is distributed in the hope that it will be useful,
55f448c99135 Factorise id3v2 header parsing from mp3.c to be shared
superdump
parents:
diff changeset
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
55f448c99135 Factorise id3v2 header parsing from mp3.c to be shared
superdump
parents:
diff changeset
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
55f448c99135 Factorise id3v2 header parsing from mp3.c to be shared
superdump
parents:
diff changeset
15 * Lesser General Public License for more details.
55f448c99135 Factorise id3v2 header parsing from mp3.c to be shared
superdump
parents:
diff changeset
16 *
55f448c99135 Factorise id3v2 header parsing from mp3.c to be shared
superdump
parents:
diff changeset
17 * You should have received a copy of the GNU Lesser General Public
55f448c99135 Factorise id3v2 header parsing from mp3.c to be shared
superdump
parents:
diff changeset
18 * License along with FFmpeg; if not, write to the Free Software
55f448c99135 Factorise id3v2 header parsing from mp3.c to be shared
superdump
parents:
diff changeset
19 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
55f448c99135 Factorise id3v2 header parsing from mp3.c to be shared
superdump
parents:
diff changeset
20 */
55f448c99135 Factorise id3v2 header parsing from mp3.c to be shared
superdump
parents:
diff changeset
21
55f448c99135 Factorise id3v2 header parsing from mp3.c to be shared
superdump
parents:
diff changeset
22 #include "id3v2.h"
5016
eb6dd7717805 Move id3v2 parsing code from mp3.c to id3v2.h and id3v2.c.
diego
parents: 4254
diff changeset
23 #include "id3v1.h"
eb6dd7717805 Move id3v2 parsing code from mp3.c to id3v2.h and id3v2.c.
diego
parents: 4254
diff changeset
24 #include "libavutil/avstring.h"
6121
81614e9b541b Generalize ID3v2 functions to support ID3v2-like ID headers with a
cehoyos
parents: 5982
diff changeset
25 #include "libavutil/intreadwrite.h"
4221
55f448c99135 Factorise id3v2 header parsing from mp3.c to be shared
superdump
parents:
diff changeset
26
6121
81614e9b541b Generalize ID3v2 functions to support ID3v2-like ID headers with a
cehoyos
parents: 5982
diff changeset
27 int ff_id3v2_match(const uint8_t *buf, const char * magic)
4221
55f448c99135 Factorise id3v2 header parsing from mp3.c to be shared
superdump
parents:
diff changeset
28 {
6121
81614e9b541b Generalize ID3v2 functions to support ID3v2-like ID headers with a
cehoyos
parents: 5982
diff changeset
29 return buf[0] == magic[0] &&
81614e9b541b Generalize ID3v2 functions to support ID3v2-like ID headers with a
cehoyos
parents: 5982
diff changeset
30 buf[1] == magic[1] &&
81614e9b541b Generalize ID3v2 functions to support ID3v2-like ID headers with a
cehoyos
parents: 5982
diff changeset
31 buf[2] == magic[2] &&
5017
1763f7d0d8d0 cosmetics: Reformat to K&R and prettyprint newly created files.
diego
parents: 5016
diff changeset
32 buf[3] != 0xff &&
1763f7d0d8d0 cosmetics: Reformat to K&R and prettyprint newly created files.
diego
parents: 5016
diff changeset
33 buf[4] != 0xff &&
1763f7d0d8d0 cosmetics: Reformat to K&R and prettyprint newly created files.
diego
parents: 5016
diff changeset
34 (buf[6] & 0x80) == 0 &&
1763f7d0d8d0 cosmetics: Reformat to K&R and prettyprint newly created files.
diego
parents: 5016
diff changeset
35 (buf[7] & 0x80) == 0 &&
1763f7d0d8d0 cosmetics: Reformat to K&R and prettyprint newly created files.
diego
parents: 5016
diff changeset
36 (buf[8] & 0x80) == 0 &&
5018
2d29b49bae3e cosmetics: Prettyprint one more line to have columns line up.
diego
parents: 5017
diff changeset
37 (buf[9] & 0x80) == 0;
4221
55f448c99135 Factorise id3v2 header parsing from mp3.c to be shared
superdump
parents:
diff changeset
38 }
4254
d05b13327b07 Fix probing of files with ID3v2 tags. Discussed at
alexc
parents: 4221
diff changeset
39
d05b13327b07 Fix probing of files with ID3v2 tags. Discussed at
alexc
parents: 4221
diff changeset
40 int ff_id3v2_tag_len(const uint8_t * buf)
d05b13327b07 Fix probing of files with ID3v2 tags. Discussed at
alexc
parents: 4221
diff changeset
41 {
d05b13327b07 Fix probing of files with ID3v2 tags. Discussed at
alexc
parents: 4221
diff changeset
42 int len = ((buf[6] & 0x7f) << 21) +
5017
1763f7d0d8d0 cosmetics: Reformat to K&R and prettyprint newly created files.
diego
parents: 5016
diff changeset
43 ((buf[7] & 0x7f) << 14) +
1763f7d0d8d0 cosmetics: Reformat to K&R and prettyprint newly created files.
diego
parents: 5016
diff changeset
44 ((buf[8] & 0x7f) << 7) +
1763f7d0d8d0 cosmetics: Reformat to K&R and prettyprint newly created files.
diego
parents: 5016
diff changeset
45 (buf[9] & 0x7f) +
1763f7d0d8d0 cosmetics: Reformat to K&R and prettyprint newly created files.
diego
parents: 5016
diff changeset
46 ID3v2_HEADER_SIZE;
4254
d05b13327b07 Fix probing of files with ID3v2 tags. Discussed at
alexc
parents: 4221
diff changeset
47 if (buf[5] & 0x10)
d05b13327b07 Fix probing of files with ID3v2 tags. Discussed at
alexc
parents: 4221
diff changeset
48 len += ID3v2_HEADER_SIZE;
d05b13327b07 Fix probing of files with ID3v2 tags. Discussed at
alexc
parents: 4221
diff changeset
49 return len;
d05b13327b07 Fix probing of files with ID3v2 tags. Discussed at
alexc
parents: 4221
diff changeset
50 }
5016
eb6dd7717805 Move id3v2 parsing code from mp3.c to id3v2.h and id3v2.c.
diego
parents: 4254
diff changeset
51
6121
81614e9b541b Generalize ID3v2 functions to support ID3v2-like ID headers with a
cehoyos
parents: 5982
diff changeset
52 void ff_id3v2_read(AVFormatContext *s, const char *magic)
5045
9ed3c88ed9ba Move id3v1/id3v2 handling code from mp3.c to id3v[12].c.
diego
parents: 5018
diff changeset
53 {
9ed3c88ed9ba Move id3v1/id3v2 handling code from mp3.c to id3v[12].c.
diego
parents: 5018
diff changeset
54 int len, ret;
9ed3c88ed9ba Move id3v1/id3v2 handling code from mp3.c to id3v[12].c.
diego
parents: 5018
diff changeset
55 uint8_t buf[ID3v2_HEADER_SIZE];
9ed3c88ed9ba Move id3v1/id3v2 handling code from mp3.c to id3v[12].c.
diego
parents: 5018
diff changeset
56
9ed3c88ed9ba Move id3v1/id3v2 handling code from mp3.c to id3v[12].c.
diego
parents: 5018
diff changeset
57 ret = get_buffer(s->pb, buf, ID3v2_HEADER_SIZE);
9ed3c88ed9ba Move id3v1/id3v2 handling code from mp3.c to id3v[12].c.
diego
parents: 5018
diff changeset
58 if (ret != ID3v2_HEADER_SIZE)
9ed3c88ed9ba Move id3v1/id3v2 handling code from mp3.c to id3v[12].c.
diego
parents: 5018
diff changeset
59 return;
6121
81614e9b541b Generalize ID3v2 functions to support ID3v2-like ID headers with a
cehoyos
parents: 5982
diff changeset
60 if (ff_id3v2_match(buf, magic)) {
5045
9ed3c88ed9ba Move id3v1/id3v2 handling code from mp3.c to id3v[12].c.
diego
parents: 5018
diff changeset
61 /* parse ID3v2 header */
9ed3c88ed9ba Move id3v1/id3v2 handling code from mp3.c to id3v[12].c.
diego
parents: 5018
diff changeset
62 len = ((buf[6] & 0x7f) << 21) |
9ed3c88ed9ba Move id3v1/id3v2 handling code from mp3.c to id3v[12].c.
diego
parents: 5018
diff changeset
63 ((buf[7] & 0x7f) << 14) |
9ed3c88ed9ba Move id3v1/id3v2 handling code from mp3.c to id3v[12].c.
diego
parents: 5018
diff changeset
64 ((buf[8] & 0x7f) << 7) |
9ed3c88ed9ba Move id3v1/id3v2 handling code from mp3.c to id3v[12].c.
diego
parents: 5018
diff changeset
65 (buf[9] & 0x7f);
9ed3c88ed9ba Move id3v1/id3v2 handling code from mp3.c to id3v[12].c.
diego
parents: 5018
diff changeset
66 ff_id3v2_parse(s, len, buf[3], buf[5]);
9ed3c88ed9ba Move id3v1/id3v2 handling code from mp3.c to id3v[12].c.
diego
parents: 5018
diff changeset
67 } else {
9ed3c88ed9ba Move id3v1/id3v2 handling code from mp3.c to id3v[12].c.
diego
parents: 5018
diff changeset
68 url_fseek(s->pb, 0, SEEK_SET);
9ed3c88ed9ba Move id3v1/id3v2 handling code from mp3.c to id3v[12].c.
diego
parents: 5018
diff changeset
69 }
9ed3c88ed9ba Move id3v1/id3v2 handling code from mp3.c to id3v[12].c.
diego
parents: 5018
diff changeset
70 }
9ed3c88ed9ba Move id3v1/id3v2 handling code from mp3.c to id3v[12].c.
diego
parents: 5018
diff changeset
71
5016
eb6dd7717805 Move id3v2 parsing code from mp3.c to id3v2.h and id3v2.c.
diego
parents: 4254
diff changeset
72 static unsigned int get_size(ByteIOContext *s, int len)
eb6dd7717805 Move id3v2 parsing code from mp3.c to id3v2.h and id3v2.c.
diego
parents: 4254
diff changeset
73 {
5017
1763f7d0d8d0 cosmetics: Reformat to K&R and prettyprint newly created files.
diego
parents: 5016
diff changeset
74 int v = 0;
1763f7d0d8d0 cosmetics: Reformat to K&R and prettyprint newly created files.
diego
parents: 5016
diff changeset
75 while (len--)
1763f7d0d8d0 cosmetics: Reformat to K&R and prettyprint newly created files.
diego
parents: 5016
diff changeset
76 v = (v << 7) + (get_byte(s) & 0x7F);
5016
eb6dd7717805 Move id3v2 parsing code from mp3.c to id3v2.h and id3v2.c.
diego
parents: 4254
diff changeset
77 return v;
eb6dd7717805 Move id3v2 parsing code from mp3.c to id3v2.h and id3v2.c.
diego
parents: 4254
diff changeset
78 }
eb6dd7717805 Move id3v2 parsing code from mp3.c to id3v2.h and id3v2.c.
diego
parents: 4254
diff changeset
79
6386
dd54b36d1b8c Support unsynchronisation for id3v2 tags.
cehoyos
parents: 6333
diff changeset
80 static void read_ttag(AVFormatContext *s, ByteIOContext *pb, int taglen, const char *key)
5016
eb6dd7717805 Move id3v2 parsing code from mp3.c to id3v2.h and id3v2.c.
diego
parents: 4254
diff changeset
81 {
eb6dd7717805 Move id3v2 parsing code from mp3.c to id3v2.h and id3v2.c.
diego
parents: 4254
diff changeset
82 char *q, dst[512];
5265
79be5a6fd62f id3v2: Export all text information frames with correct names.
ramiro
parents: 5264
diff changeset
83 const char *val = NULL;
5016
eb6dd7717805 Move id3v2 parsing code from mp3.c to id3v2.h and id3v2.c.
diego
parents: 4254
diff changeset
84 int len, dstlen = sizeof(dst) - 1;
eb6dd7717805 Move id3v2 parsing code from mp3.c to id3v2.h and id3v2.c.
diego
parents: 4254
diff changeset
85 unsigned genre;
5231
d2e3bc991df4 id3v2: Add support for UTF-16 encoding.
diego
parents: 5230
diff changeset
86 unsigned int (*get)(ByteIOContext*) = get_be16;
5016
eb6dd7717805 Move id3v2 parsing code from mp3.c to id3v2.h and id3v2.c.
diego
parents: 4254
diff changeset
87
5017
1763f7d0d8d0 cosmetics: Reformat to K&R and prettyprint newly created files.
diego
parents: 5016
diff changeset
88 dst[0] = 0;
1763f7d0d8d0 cosmetics: Reformat to K&R and prettyprint newly created files.
diego
parents: 5016
diff changeset
89 if (taglen < 1)
5016
eb6dd7717805 Move id3v2 parsing code from mp3.c to id3v2.h and id3v2.c.
diego
parents: 4254
diff changeset
90 return;
eb6dd7717805 Move id3v2 parsing code from mp3.c to id3v2.h and id3v2.c.
diego
parents: 4254
diff changeset
91
eb6dd7717805 Move id3v2 parsing code from mp3.c to id3v2.h and id3v2.c.
diego
parents: 4254
diff changeset
92 taglen--; /* account for encoding type byte */
eb6dd7717805 Move id3v2 parsing code from mp3.c to id3v2.h and id3v2.c.
diego
parents: 4254
diff changeset
93
6386
dd54b36d1b8c Support unsynchronisation for id3v2 tags.
cehoyos
parents: 6333
diff changeset
94 switch (get_byte(pb)) { /* encoding type */
5016
eb6dd7717805 Move id3v2 parsing code from mp3.c to id3v2.h and id3v2.c.
diego
parents: 4254
diff changeset
95
eb6dd7717805 Move id3v2 parsing code from mp3.c to id3v2.h and id3v2.c.
diego
parents: 4254
diff changeset
96 case 0: /* ISO-8859-1 (0 - 255 maps directly into unicode) */
eb6dd7717805 Move id3v2 parsing code from mp3.c to id3v2.h and id3v2.c.
diego
parents: 4254
diff changeset
97 q = dst;
5229
4a01407f678d id3v2: check for enough space to write full UTF-8 characters.
diego
parents: 5077
diff changeset
98 while (taglen-- && q - dst < dstlen - 7) {
5016
eb6dd7717805 Move id3v2 parsing code from mp3.c to id3v2.h and id3v2.c.
diego
parents: 4254
diff changeset
99 uint8_t tmp;
6386
dd54b36d1b8c Support unsynchronisation for id3v2 tags.
cehoyos
parents: 6333
diff changeset
100 PUT_UTF8(get_byte(pb), tmp, *q++ = tmp;)
5016
eb6dd7717805 Move id3v2 parsing code from mp3.c to id3v2.h and id3v2.c.
diego
parents: 4254
diff changeset
101 }
5230
0d26775e9f47 id3v2: Use 0 instead of '\0'.
diego
parents: 5229
diff changeset
102 *q = 0;
5016
eb6dd7717805 Move id3v2 parsing code from mp3.c to id3v2.h and id3v2.c.
diego
parents: 4254
diff changeset
103 break;
eb6dd7717805 Move id3v2 parsing code from mp3.c to id3v2.h and id3v2.c.
diego
parents: 4254
diff changeset
104
5231
d2e3bc991df4 id3v2: Add support for UTF-16 encoding.
diego
parents: 5230
diff changeset
105 case 1: /* UTF-16 with BOM */
d2e3bc991df4 id3v2: Add support for UTF-16 encoding.
diego
parents: 5230
diff changeset
106 taglen -= 2;
6386
dd54b36d1b8c Support unsynchronisation for id3v2 tags.
cehoyos
parents: 6333
diff changeset
107 switch (get_be16(pb)) {
5231
d2e3bc991df4 id3v2: Add support for UTF-16 encoding.
diego
parents: 5230
diff changeset
108 case 0xfffe:
d2e3bc991df4 id3v2: Add support for UTF-16 encoding.
diego
parents: 5230
diff changeset
109 get = get_le16;
d2e3bc991df4 id3v2: Add support for UTF-16 encoding.
diego
parents: 5230
diff changeset
110 case 0xfeff:
d2e3bc991df4 id3v2: Add support for UTF-16 encoding.
diego
parents: 5230
diff changeset
111 break;
d2e3bc991df4 id3v2: Add support for UTF-16 encoding.
diego
parents: 5230
diff changeset
112 default:
d2e3bc991df4 id3v2: Add support for UTF-16 encoding.
diego
parents: 5230
diff changeset
113 av_log(s, AV_LOG_ERROR, "Incorrect BOM value in tag %s.\n", key);
d2e3bc991df4 id3v2: Add support for UTF-16 encoding.
diego
parents: 5230
diff changeset
114 return;
d2e3bc991df4 id3v2: Add support for UTF-16 encoding.
diego
parents: 5230
diff changeset
115 }
d2e3bc991df4 id3v2: Add support for UTF-16 encoding.
diego
parents: 5230
diff changeset
116 // fall-through
d2e3bc991df4 id3v2: Add support for UTF-16 encoding.
diego
parents: 5230
diff changeset
117
d2e3bc991df4 id3v2: Add support for UTF-16 encoding.
diego
parents: 5230
diff changeset
118 case 2: /* UTF-16BE without BOM */
d2e3bc991df4 id3v2: Add support for UTF-16 encoding.
diego
parents: 5230
diff changeset
119 q = dst;
d2e3bc991df4 id3v2: Add support for UTF-16 encoding.
diego
parents: 5230
diff changeset
120 while (taglen > 1 && q - dst < dstlen - 7) {
d2e3bc991df4 id3v2: Add support for UTF-16 encoding.
diego
parents: 5230
diff changeset
121 uint32_t ch;
d2e3bc991df4 id3v2: Add support for UTF-16 encoding.
diego
parents: 5230
diff changeset
122 uint8_t tmp;
d2e3bc991df4 id3v2: Add support for UTF-16 encoding.
diego
parents: 5230
diff changeset
123
6386
dd54b36d1b8c Support unsynchronisation for id3v2 tags.
cehoyos
parents: 6333
diff changeset
124 GET_UTF16(ch, ((taglen -= 2) >= 0 ? get(pb) : 0), break;)
5231
d2e3bc991df4 id3v2: Add support for UTF-16 encoding.
diego
parents: 5230
diff changeset
125 PUT_UTF8(ch, tmp, *q++ = tmp;)
d2e3bc991df4 id3v2: Add support for UTF-16 encoding.
diego
parents: 5230
diff changeset
126 }
d2e3bc991df4 id3v2: Add support for UTF-16 encoding.
diego
parents: 5230
diff changeset
127 *q = 0;
d2e3bc991df4 id3v2: Add support for UTF-16 encoding.
diego
parents: 5230
diff changeset
128 break;
d2e3bc991df4 id3v2: Add support for UTF-16 encoding.
diego
parents: 5230
diff changeset
129
5016
eb6dd7717805 Move id3v2 parsing code from mp3.c to id3v2.h and id3v2.c.
diego
parents: 4254
diff changeset
130 case 3: /* UTF-8 */
5980
b32c7be57669 Fix off-by-1 error in the tag parsing code.
jai_menon
parents: 5618
diff changeset
131 len = FFMIN(taglen, dstlen);
6386
dd54b36d1b8c Support unsynchronisation for id3v2 tags.
cehoyos
parents: 6333
diff changeset
132 get_buffer(pb, dst, len);
5016
eb6dd7717805 Move id3v2 parsing code from mp3.c to id3v2.h and id3v2.c.
diego
parents: 4254
diff changeset
133 dst[len] = 0;
eb6dd7717805 Move id3v2 parsing code from mp3.c to id3v2.h and id3v2.c.
diego
parents: 4254
diff changeset
134 break;
5231
d2e3bc991df4 id3v2: Add support for UTF-16 encoding.
diego
parents: 5230
diff changeset
135 default:
d2e3bc991df4 id3v2: Add support for UTF-16 encoding.
diego
parents: 5230
diff changeset
136 av_log(s, AV_LOG_WARNING, "Unknown encoding in tag %s\n.", key);
5016
eb6dd7717805 Move id3v2 parsing code from mp3.c to id3v2.h and id3v2.c.
diego
parents: 4254
diff changeset
137 }
eb6dd7717805 Move id3v2 parsing code from mp3.c to id3v2.h and id3v2.c.
diego
parents: 4254
diff changeset
138
5265
79be5a6fd62f id3v2: Export all text information frames with correct names.
ramiro
parents: 5264
diff changeset
139 if (!(strcmp(key, "TCON") && strcmp(key, "TCO"))
5016
eb6dd7717805 Move id3v2 parsing code from mp3.c to id3v2.h and id3v2.c.
diego
parents: 4254
diff changeset
140 && (sscanf(dst, "(%d)", &genre) == 1 || sscanf(dst, "%d", &genre) == 1)
eb6dd7717805 Move id3v2 parsing code from mp3.c to id3v2.h and id3v2.c.
diego
parents: 4254
diff changeset
141 && genre <= ID3v1_GENRE_MAX)
5265
79be5a6fd62f id3v2: Export all text information frames with correct names.
ramiro
parents: 5264
diff changeset
142 val = ff_id3v1_genre_str[genre];
79be5a6fd62f id3v2: Export all text information frames with correct names.
ramiro
parents: 5264
diff changeset
143 else if (!(strcmp(key, "TXXX") && strcmp(key, "TXX"))) {
79be5a6fd62f id3v2: Export all text information frames with correct names.
ramiro
parents: 5264
diff changeset
144 /* dst now contains two 0-terminated strings */
79be5a6fd62f id3v2: Export all text information frames with correct names.
ramiro
parents: 5264
diff changeset
145 dst[dstlen] = 0;
79be5a6fd62f id3v2: Export all text information frames with correct names.
ramiro
parents: 5264
diff changeset
146 len = strlen(dst);
79be5a6fd62f id3v2: Export all text information frames with correct names.
ramiro
parents: 5264
diff changeset
147 key = dst;
79be5a6fd62f id3v2: Export all text information frames with correct names.
ramiro
parents: 5264
diff changeset
148 val = dst + FFMIN(len + 1, dstlen);
79be5a6fd62f id3v2: Export all text information frames with correct names.
ramiro
parents: 5264
diff changeset
149 }
79be5a6fd62f id3v2: Export all text information frames with correct names.
ramiro
parents: 5264
diff changeset
150 else if (*dst)
79be5a6fd62f id3v2: Export all text information frames with correct names.
ramiro
parents: 5264
diff changeset
151 val = dst;
5016
eb6dd7717805 Move id3v2 parsing code from mp3.c to id3v2.h and id3v2.c.
diego
parents: 4254
diff changeset
152
5265
79be5a6fd62f id3v2: Export all text information frames with correct names.
ramiro
parents: 5264
diff changeset
153 if (val)
5982
f74198942337 Mark av_metadata_set() as deprecated, and use av_metadata_set2()
stefano
parents: 5980
diff changeset
154 av_metadata_set2(&s->metadata, key, val, 0);
5016
eb6dd7717805 Move id3v2 parsing code from mp3.c to id3v2.h and id3v2.c.
diego
parents: 4254
diff changeset
155 }
eb6dd7717805 Move id3v2 parsing code from mp3.c to id3v2.h and id3v2.c.
diego
parents: 4254
diff changeset
156
eb6dd7717805 Move id3v2 parsing code from mp3.c to id3v2.h and id3v2.c.
diego
parents: 4254
diff changeset
157 void ff_id3v2_parse(AVFormatContext *s, int len, uint8_t version, uint8_t flags)
eb6dd7717805 Move id3v2 parsing code from mp3.c to id3v2.h and id3v2.c.
diego
parents: 4254
diff changeset
158 {
6386
dd54b36d1b8c Support unsynchronisation for id3v2 tags.
cehoyos
parents: 6333
diff changeset
159 int isv34, tlen, unsync;
5265
79be5a6fd62f id3v2: Export all text information frames with correct names.
ramiro
parents: 5264
diff changeset
160 char tag[5];
5016
eb6dd7717805 Move id3v2 parsing code from mp3.c to id3v2.h and id3v2.c.
diego
parents: 4254
diff changeset
161 int64_t next;
eb6dd7717805 Move id3v2 parsing code from mp3.c to id3v2.h and id3v2.c.
diego
parents: 4254
diff changeset
162 int taghdrlen;
eb6dd7717805 Move id3v2 parsing code from mp3.c to id3v2.h and id3v2.c.
diego
parents: 4254
diff changeset
163 const char *reason;
6386
dd54b36d1b8c Support unsynchronisation for id3v2 tags.
cehoyos
parents: 6333
diff changeset
164 ByteIOContext pb;
dd54b36d1b8c Support unsynchronisation for id3v2 tags.
cehoyos
parents: 6333
diff changeset
165 unsigned char *buffer = NULL;
dd54b36d1b8c Support unsynchronisation for id3v2 tags.
cehoyos
parents: 6333
diff changeset
166 int buffer_size = 0;
5016
eb6dd7717805 Move id3v2 parsing code from mp3.c to id3v2.h and id3v2.c.
diego
parents: 4254
diff changeset
167
5017
1763f7d0d8d0 cosmetics: Reformat to K&R and prettyprint newly created files.
diego
parents: 5016
diff changeset
168 switch (version) {
5016
eb6dd7717805 Move id3v2 parsing code from mp3.c to id3v2.h and id3v2.c.
diego
parents: 4254
diff changeset
169 case 2:
5017
1763f7d0d8d0 cosmetics: Reformat to K&R and prettyprint newly created files.
diego
parents: 5016
diff changeset
170 if (flags & 0x40) {
5016
eb6dd7717805 Move id3v2 parsing code from mp3.c to id3v2.h and id3v2.c.
diego
parents: 4254
diff changeset
171 reason = "compression";
eb6dd7717805 Move id3v2 parsing code from mp3.c to id3v2.h and id3v2.c.
diego
parents: 4254
diff changeset
172 goto error;
eb6dd7717805 Move id3v2 parsing code from mp3.c to id3v2.h and id3v2.c.
diego
parents: 4254
diff changeset
173 }
eb6dd7717805 Move id3v2 parsing code from mp3.c to id3v2.h and id3v2.c.
diego
parents: 4254
diff changeset
174 isv34 = 0;
eb6dd7717805 Move id3v2 parsing code from mp3.c to id3v2.h and id3v2.c.
diego
parents: 4254
diff changeset
175 taghdrlen = 6;
eb6dd7717805 Move id3v2 parsing code from mp3.c to id3v2.h and id3v2.c.
diego
parents: 4254
diff changeset
176 break;
eb6dd7717805 Move id3v2 parsing code from mp3.c to id3v2.h and id3v2.c.
diego
parents: 4254
diff changeset
177
eb6dd7717805 Move id3v2 parsing code from mp3.c to id3v2.h and id3v2.c.
diego
parents: 4254
diff changeset
178 case 3:
eb6dd7717805 Move id3v2 parsing code from mp3.c to id3v2.h and id3v2.c.
diego
parents: 4254
diff changeset
179 case 4:
eb6dd7717805 Move id3v2 parsing code from mp3.c to id3v2.h and id3v2.c.
diego
parents: 4254
diff changeset
180 isv34 = 1;
eb6dd7717805 Move id3v2 parsing code from mp3.c to id3v2.h and id3v2.c.
diego
parents: 4254
diff changeset
181 taghdrlen = 10;
eb6dd7717805 Move id3v2 parsing code from mp3.c to id3v2.h and id3v2.c.
diego
parents: 4254
diff changeset
182 break;
eb6dd7717805 Move id3v2 parsing code from mp3.c to id3v2.h and id3v2.c.
diego
parents: 4254
diff changeset
183
eb6dd7717805 Move id3v2 parsing code from mp3.c to id3v2.h and id3v2.c.
diego
parents: 4254
diff changeset
184 default:
eb6dd7717805 Move id3v2 parsing code from mp3.c to id3v2.h and id3v2.c.
diego
parents: 4254
diff changeset
185 reason = "version";
eb6dd7717805 Move id3v2 parsing code from mp3.c to id3v2.h and id3v2.c.
diego
parents: 4254
diff changeset
186 goto error;
eb6dd7717805 Move id3v2 parsing code from mp3.c to id3v2.h and id3v2.c.
diego
parents: 4254
diff changeset
187 }
eb6dd7717805 Move id3v2 parsing code from mp3.c to id3v2.h and id3v2.c.
diego
parents: 4254
diff changeset
188
6386
dd54b36d1b8c Support unsynchronisation for id3v2 tags.
cehoyos
parents: 6333
diff changeset
189 unsync = flags & 0x80;
5016
eb6dd7717805 Move id3v2 parsing code from mp3.c to id3v2.h and id3v2.c.
diego
parents: 4254
diff changeset
190
5017
1763f7d0d8d0 cosmetics: Reformat to K&R and prettyprint newly created files.
diego
parents: 5016
diff changeset
191 if (isv34 && flags & 0x40) /* Extended header present, just skip over it */
5016
eb6dd7717805 Move id3v2 parsing code from mp3.c to id3v2.h and id3v2.c.
diego
parents: 4254
diff changeset
192 url_fskip(s->pb, get_size(s->pb, 4));
eb6dd7717805 Move id3v2 parsing code from mp3.c to id3v2.h and id3v2.c.
diego
parents: 4254
diff changeset
193
5017
1763f7d0d8d0 cosmetics: Reformat to K&R and prettyprint newly created files.
diego
parents: 5016
diff changeset
194 while (len >= taghdrlen) {
6386
dd54b36d1b8c Support unsynchronisation for id3v2 tags.
cehoyos
parents: 6333
diff changeset
195 unsigned int tflags;
dd54b36d1b8c Support unsynchronisation for id3v2 tags.
cehoyos
parents: 6333
diff changeset
196 int tunsync = 0;
dd54b36d1b8c Support unsynchronisation for id3v2 tags.
cehoyos
parents: 6333
diff changeset
197
5017
1763f7d0d8d0 cosmetics: Reformat to K&R and prettyprint newly created files.
diego
parents: 5016
diff changeset
198 if (isv34) {
5265
79be5a6fd62f id3v2: Export all text information frames with correct names.
ramiro
parents: 5264
diff changeset
199 get_buffer(s->pb, tag, 4);
79be5a6fd62f id3v2: Export all text information frames with correct names.
ramiro
parents: 5264
diff changeset
200 tag[4] = 0;
5076
e07fb352a12c id3v2.4.0 uses syncsafe integers for sizes of frames.
michael
parents: 5073
diff changeset
201 if(version==3){
5077
michael
parents: 5076
diff changeset
202 tlen = get_be32(s->pb);
5076
e07fb352a12c id3v2.4.0 uses syncsafe integers for sizes of frames.
michael
parents: 5073
diff changeset
203 }else
e07fb352a12c id3v2.4.0 uses syncsafe integers for sizes of frames.
michael
parents: 5073
diff changeset
204 tlen = get_size(s->pb, 4);
6386
dd54b36d1b8c Support unsynchronisation for id3v2 tags.
cehoyos
parents: 6333
diff changeset
205 tflags = get_be16(s->pb);
dd54b36d1b8c Support unsynchronisation for id3v2 tags.
cehoyos
parents: 6333
diff changeset
206 tunsync = tflags & 0x02;
5016
eb6dd7717805 Move id3v2 parsing code from mp3.c to id3v2.h and id3v2.c.
diego
parents: 4254
diff changeset
207 } else {
5265
79be5a6fd62f id3v2: Export all text information frames with correct names.
ramiro
parents: 5264
diff changeset
208 get_buffer(s->pb, tag, 3);
79be5a6fd62f id3v2: Export all text information frames with correct names.
ramiro
parents: 5264
diff changeset
209 tag[3] = 0;
5073
2216af3795e1 Fix id3v2.2 frame size parsing.
michael
parents: 5072
diff changeset
210 tlen = get_be24(s->pb);
5016
eb6dd7717805 Move id3v2 parsing code from mp3.c to id3v2.h and id3v2.c.
diego
parents: 4254
diff changeset
211 }
eb6dd7717805 Move id3v2 parsing code from mp3.c to id3v2.h and id3v2.c.
diego
parents: 4254
diff changeset
212 len -= taghdrlen + tlen;
eb6dd7717805 Move id3v2 parsing code from mp3.c to id3v2.h and id3v2.c.
diego
parents: 4254
diff changeset
213
5017
1763f7d0d8d0 cosmetics: Reformat to K&R and prettyprint newly created files.
diego
parents: 5016
diff changeset
214 if (len < 0)
5016
eb6dd7717805 Move id3v2 parsing code from mp3.c to id3v2.h and id3v2.c.
diego
parents: 4254
diff changeset
215 break;
eb6dd7717805 Move id3v2 parsing code from mp3.c to id3v2.h and id3v2.c.
diego
parents: 4254
diff changeset
216
eb6dd7717805 Move id3v2 parsing code from mp3.c to id3v2.h and id3v2.c.
diego
parents: 4254
diff changeset
217 next = url_ftell(s->pb) + tlen;
eb6dd7717805 Move id3v2 parsing code from mp3.c to id3v2.h and id3v2.c.
diego
parents: 4254
diff changeset
218
6386
dd54b36d1b8c Support unsynchronisation for id3v2 tags.
cehoyos
parents: 6333
diff changeset
219 if (tag[0] == 'T') {
dd54b36d1b8c Support unsynchronisation for id3v2 tags.
cehoyos
parents: 6333
diff changeset
220 if (unsync || tunsync) {
dd54b36d1b8c Support unsynchronisation for id3v2 tags.
cehoyos
parents: 6333
diff changeset
221 int i, j;
dd54b36d1b8c Support unsynchronisation for id3v2 tags.
cehoyos
parents: 6333
diff changeset
222 av_fast_malloc(&buffer, &buffer_size, tlen);
dd54b36d1b8c Support unsynchronisation for id3v2 tags.
cehoyos
parents: 6333
diff changeset
223 for (i = 0, j = 0; i < tlen; i++, j++) {
dd54b36d1b8c Support unsynchronisation for id3v2 tags.
cehoyos
parents: 6333
diff changeset
224 buffer[j] = get_byte(s->pb);
dd54b36d1b8c Support unsynchronisation for id3v2 tags.
cehoyos
parents: 6333
diff changeset
225 if (j > 0 && !buffer[j] && buffer[j - 1] == 0xff) {
dd54b36d1b8c Support unsynchronisation for id3v2 tags.
cehoyos
parents: 6333
diff changeset
226 /* Unsynchronised byte, skip it */
dd54b36d1b8c Support unsynchronisation for id3v2 tags.
cehoyos
parents: 6333
diff changeset
227 j--;
dd54b36d1b8c Support unsynchronisation for id3v2 tags.
cehoyos
parents: 6333
diff changeset
228 }
dd54b36d1b8c Support unsynchronisation for id3v2 tags.
cehoyos
parents: 6333
diff changeset
229 }
dd54b36d1b8c Support unsynchronisation for id3v2 tags.
cehoyos
parents: 6333
diff changeset
230 init_put_byte(&pb, buffer, j, 0, NULL, NULL, NULL, NULL);
dd54b36d1b8c Support unsynchronisation for id3v2 tags.
cehoyos
parents: 6333
diff changeset
231 read_ttag(s, &pb, j, tag);
dd54b36d1b8c Support unsynchronisation for id3v2 tags.
cehoyos
parents: 6333
diff changeset
232 } else {
dd54b36d1b8c Support unsynchronisation for id3v2 tags.
cehoyos
parents: 6333
diff changeset
233 read_ttag(s, s->pb, tlen, tag);
dd54b36d1b8c Support unsynchronisation for id3v2 tags.
cehoyos
parents: 6333
diff changeset
234 }
dd54b36d1b8c Support unsynchronisation for id3v2 tags.
cehoyos
parents: 6333
diff changeset
235 }
5286
3ce92654e943 Skip padding bytes correctly in ID3 tags.
jai_menon
parents: 5265
diff changeset
236 else if (!tag[0]) {
3ce92654e943 Skip padding bytes correctly in ID3 tags.
jai_menon
parents: 5265
diff changeset
237 if (tag[1])
3ce92654e943 Skip padding bytes correctly in ID3 tags.
jai_menon
parents: 5265
diff changeset
238 av_log(s, AV_LOG_WARNING, "invalid frame id, assuming padding");
6333
f5fec1ab4925 Skip short padding in id3v2.
cehoyos
parents: 6121
diff changeset
239 url_fskip(s->pb, tlen);
5286
3ce92654e943 Skip padding bytes correctly in ID3 tags.
jai_menon
parents: 5265
diff changeset
240 break;
3ce92654e943 Skip padding bytes correctly in ID3 tags.
jai_menon
parents: 5265
diff changeset
241 }
5016
eb6dd7717805 Move id3v2 parsing code from mp3.c to id3v2.h and id3v2.c.
diego
parents: 4254
diff changeset
242 /* Skip to end of tag */
eb6dd7717805 Move id3v2 parsing code from mp3.c to id3v2.h and id3v2.c.
diego
parents: 4254
diff changeset
243 url_fseek(s->pb, next, SEEK_SET);
eb6dd7717805 Move id3v2 parsing code from mp3.c to id3v2.h and id3v2.c.
diego
parents: 4254
diff changeset
244 }
eb6dd7717805 Move id3v2 parsing code from mp3.c to id3v2.h and id3v2.c.
diego
parents: 4254
diff changeset
245
6333
f5fec1ab4925 Skip short padding in id3v2.
cehoyos
parents: 6121
diff changeset
246 if (len > 0) {
f5fec1ab4925 Skip short padding in id3v2.
cehoyos
parents: 6121
diff changeset
247 /* Skip padding */
f5fec1ab4925 Skip short padding in id3v2.
cehoyos
parents: 6121
diff changeset
248 url_fskip(s->pb, len);
f5fec1ab4925 Skip short padding in id3v2.
cehoyos
parents: 6121
diff changeset
249 }
5017
1763f7d0d8d0 cosmetics: Reformat to K&R and prettyprint newly created files.
diego
parents: 5016
diff changeset
250 if (version == 4 && flags & 0x10) /* Footer preset, always 10 bytes, skip over it */
5016
eb6dd7717805 Move id3v2 parsing code from mp3.c to id3v2.h and id3v2.c.
diego
parents: 4254
diff changeset
251 url_fskip(s->pb, 10);
6386
dd54b36d1b8c Support unsynchronisation for id3v2 tags.
cehoyos
parents: 6333
diff changeset
252
dd54b36d1b8c Support unsynchronisation for id3v2 tags.
cehoyos
parents: 6333
diff changeset
253 av_free(buffer);
5016
eb6dd7717805 Move id3v2 parsing code from mp3.c to id3v2.h and id3v2.c.
diego
parents: 4254
diff changeset
254 return;
eb6dd7717805 Move id3v2 parsing code from mp3.c to id3v2.h and id3v2.c.
diego
parents: 4254
diff changeset
255
eb6dd7717805 Move id3v2 parsing code from mp3.c to id3v2.h and id3v2.c.
diego
parents: 4254
diff changeset
256 error:
eb6dd7717805 Move id3v2 parsing code from mp3.c to id3v2.h and id3v2.c.
diego
parents: 4254
diff changeset
257 av_log(s, AV_LOG_INFO, "ID3v2.%d tag skipped, cannot handle %s\n", version, reason);
eb6dd7717805 Move id3v2 parsing code from mp3.c to id3v2.h and id3v2.c.
diego
parents: 4254
diff changeset
258 url_fskip(s->pb, len);
6386
dd54b36d1b8c Support unsynchronisation for id3v2 tags.
cehoyos
parents: 6333
diff changeset
259 av_free(buffer);
5016
eb6dd7717805 Move id3v2 parsing code from mp3.c to id3v2.h and id3v2.c.
diego
parents: 4254
diff changeset
260 }
5236
1403c88b1ce7 Add id3v2 metadata conversion table and use it in mp3 muxer.
cehoyos
parents: 5231
diff changeset
261
1403c88b1ce7 Add id3v2 metadata conversion table and use it in mp3 muxer.
cehoyos
parents: 5231
diff changeset
262 const AVMetadataConv ff_id3v2_metadata_conv[] = {
1403c88b1ce7 Add id3v2 metadata conversion table and use it in mp3 muxer.
cehoyos
parents: 5231
diff changeset
263 { "TALB", "album"},
5449
82a3916eacbd More entries for ff_id3v2_metadata_conv from ffmbc.
michael
parents: 5448
diff changeset
264 { "TAL", "album"},
5236
1403c88b1ce7 Add id3v2 metadata conversion table and use it in mp3 muxer.
cehoyos
parents: 5231
diff changeset
265 { "TCOM", "composer"},
1403c88b1ce7 Add id3v2 metadata conversion table and use it in mp3 muxer.
cehoyos
parents: 5231
diff changeset
266 { "TCON", "genre"},
5449
82a3916eacbd More entries for ff_id3v2_metadata_conv from ffmbc.
michael
parents: 5448
diff changeset
267 { "TCO", "genre"},
5236
1403c88b1ce7 Add id3v2 metadata conversion table and use it in mp3 muxer.
cehoyos
parents: 5231
diff changeset
268 { "TCOP", "copyright"},
1403c88b1ce7 Add id3v2 metadata conversion table and use it in mp3 muxer.
cehoyos
parents: 5231
diff changeset
269 { "TDRL", "date"},
5618
27fd77f20a89 Add a list of generic tags and change demuxers to follow it.
pross
parents: 5449
diff changeset
270 { "TDRC", "date"},
27fd77f20a89 Add a list of generic tags and change demuxers to follow it.
pross
parents: 5449
diff changeset
271 { "TENC", "encoded_by"},
27fd77f20a89 Add a list of generic tags and change demuxers to follow it.
pross
parents: 5449
diff changeset
272 { "TEN", "encoded_by"},
5236
1403c88b1ce7 Add id3v2 metadata conversion table and use it in mp3 muxer.
cehoyos
parents: 5231
diff changeset
273 { "TIT2", "title"},
5449
82a3916eacbd More entries for ff_id3v2_metadata_conv from ffmbc.
michael
parents: 5448
diff changeset
274 { "TT2", "title"},
5236
1403c88b1ce7 Add id3v2 metadata conversion table and use it in mp3 muxer.
cehoyos
parents: 5231
diff changeset
275 { "TLAN", "language"},
5448
97b8e17cfbd4 s/author/artist/ for ID3
michael
parents: 5286
diff changeset
276 { "TPE1", "artist"},
5449
82a3916eacbd More entries for ff_id3v2_metadata_conv from ffmbc.
michael
parents: 5448
diff changeset
277 { "TP1", "artist"},
5618
27fd77f20a89 Add a list of generic tags and change demuxers to follow it.
pross
parents: 5449
diff changeset
278 { "TPE2", "album_artist"},
27fd77f20a89 Add a list of generic tags and change demuxers to follow it.
pross
parents: 5449
diff changeset
279 { "TP2", "album_artist"},
27fd77f20a89 Add a list of generic tags and change demuxers to follow it.
pross
parents: 5449
diff changeset
280 { "TPE3", "performer"},
27fd77f20a89 Add a list of generic tags and change demuxers to follow it.
pross
parents: 5449
diff changeset
281 { "TP3", "performer"},
5236
1403c88b1ce7 Add id3v2 metadata conversion table and use it in mp3 muxer.
cehoyos
parents: 5231
diff changeset
282 { "TPOS", "disc"},
1403c88b1ce7 Add id3v2 metadata conversion table and use it in mp3 muxer.
cehoyos
parents: 5231
diff changeset
283 { "TPUB", "publisher"},
1403c88b1ce7 Add id3v2 metadata conversion table and use it in mp3 muxer.
cehoyos
parents: 5231
diff changeset
284 { "TRCK", "track"},
5449
82a3916eacbd More entries for ff_id3v2_metadata_conv from ffmbc.
michael
parents: 5448
diff changeset
285 { "TRK", "track"},
5618
27fd77f20a89 Add a list of generic tags and change demuxers to follow it.
pross
parents: 5449
diff changeset
286 { "TSOA", "album-sort"},
27fd77f20a89 Add a list of generic tags and change demuxers to follow it.
pross
parents: 5449
diff changeset
287 { "TSOP", "artist-sort"},
27fd77f20a89 Add a list of generic tags and change demuxers to follow it.
pross
parents: 5449
diff changeset
288 { "TSOT", "title-sort"},
27fd77f20a89 Add a list of generic tags and change demuxers to follow it.
pross
parents: 5449
diff changeset
289 { "TSSE", "encoder"},
5236
1403c88b1ce7 Add id3v2 metadata conversion table and use it in mp3 muxer.
cehoyos
parents: 5231
diff changeset
290 { 0 }
1403c88b1ce7 Add id3v2 metadata conversion table and use it in mp3 muxer.
cehoyos
parents: 5231
diff changeset
291 };
5264
323fc801aa57 MP3 muxer: Write all metadata.
ramiro
parents: 5236
diff changeset
292
323fc801aa57 MP3 muxer: Write all metadata.
ramiro
parents: 5236
diff changeset
293 const char ff_id3v2_tags[][4] = {
323fc801aa57 MP3 muxer: Write all metadata.
ramiro
parents: 5236
diff changeset
294 "TALB", "TBPM", "TCOM", "TCON", "TCOP", "TDEN", "TDLY", "TDOR", "TDRC",
323fc801aa57 MP3 muxer: Write all metadata.
ramiro
parents: 5236
diff changeset
295 "TDRL", "TDTG", "TENC", "TEXT", "TFLT", "TIPL", "TIT1", "TIT2", "TIT3",
323fc801aa57 MP3 muxer: Write all metadata.
ramiro
parents: 5236
diff changeset
296 "TKEY", "TLAN", "TLEN", "TMCL", "TMED", "TMOO", "TOAL", "TOFN", "TOLY",
323fc801aa57 MP3 muxer: Write all metadata.
ramiro
parents: 5236
diff changeset
297 "TOPE", "TOWN", "TPE1", "TPE2", "TPE3", "TPE4", "TPOS", "TPRO", "TPUB",
323fc801aa57 MP3 muxer: Write all metadata.
ramiro
parents: 5236
diff changeset
298 "TRCK", "TRSN", "TRSO", "TSOA", "TSOP", "TSOT", "TSRC", "TSSE", "TSST",
323fc801aa57 MP3 muxer: Write all metadata.
ramiro
parents: 5236
diff changeset
299 { 0 },
323fc801aa57 MP3 muxer: Write all metadata.
ramiro
parents: 5236
diff changeset
300 };