Mercurial > libavformat.hg
annotate mp3.c @ 1265:613fdf995af0 libavformat
read and use index (based on a patch by John Donaghy on the 23-03-2006 '[Ffmpeg-devel] dvr-ms seek help request')
author | michael |
---|---|
date | Wed, 23 Aug 2006 17:07:01 +0000 |
parents | d18cc9a1fd02 |
children | 866d43ed0a67 |
rev | line source |
---|---|
885 | 1 /* |
234 | 2 * MP3 encoder and decoder |
3 * Copyright (c) 2003 Fabrice Bellard. | |
4 * | |
5 * This library is free software; you can redistribute it and/or | |
6 * modify it under the terms of the GNU Lesser General Public | |
7 * License as published by the Free Software Foundation; either | |
8 * version 2 of the License, or (at your option) any later version. | |
9 * | |
10 * This library is distributed in the hope that it will be useful, | |
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | |
13 * Lesser General Public License for more details. | |
14 * | |
15 * You should have received a copy of the GNU Lesser General Public | |
16 * License along with this library; if not, write to the Free Software | |
896
edbe5c3717f9
Update licensing information: The FSF changed postal address.
diego
parents:
885
diff
changeset
|
17 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA |
234 | 18 */ |
19 #include "avformat.h" | |
20 | |
21 #define ID3_HEADER_SIZE 10 | |
22 #define ID3_TAG_SIZE 128 | |
23 | |
24 #define ID3_GENRE_MAX 125 | |
25 | |
26 static const char *id3_genre_str[ID3_GENRE_MAX + 1] = { | |
27 [0] = "Blues", | |
28 [1] = "Classic Rock", | |
29 [2] = "Country", | |
30 [3] = "Dance", | |
31 [4] = "Disco", | |
32 [5] = "Funk", | |
33 [6] = "Grunge", | |
34 [7] = "Hip-Hop", | |
35 [8] = "Jazz", | |
36 [9] = "Metal", | |
37 [10] = "New Age", | |
38 [11] = "Oldies", | |
39 [12] = "Other", | |
40 [13] = "Pop", | |
41 [14] = "R&B", | |
42 [15] = "Rap", | |
43 [16] = "Reggae", | |
44 [17] = "Rock", | |
45 [18] = "Techno", | |
46 [19] = "Industrial", | |
47 [20] = "Alternative", | |
48 [21] = "Ska", | |
49 [22] = "Death Metal", | |
50 [23] = "Pranks", | |
51 [24] = "Soundtrack", | |
52 [25] = "Euro-Techno", | |
53 [26] = "Ambient", | |
54 [27] = "Trip-Hop", | |
55 [28] = "Vocal", | |
56 [29] = "Jazz+Funk", | |
57 [30] = "Fusion", | |
58 [31] = "Trance", | |
59 [32] = "Classical", | |
60 [33] = "Instrumental", | |
61 [34] = "Acid", | |
62 [35] = "House", | |
63 [36] = "Game", | |
64 [37] = "Sound Clip", | |
65 [38] = "Gospel", | |
66 [39] = "Noise", | |
67 [40] = "AlternRock", | |
68 [41] = "Bass", | |
69 [42] = "Soul", | |
70 [43] = "Punk", | |
71 [44] = "Space", | |
72 [45] = "Meditative", | |
73 [46] = "Instrumental Pop", | |
74 [47] = "Instrumental Rock", | |
75 [48] = "Ethnic", | |
76 [49] = "Gothic", | |
77 [50] = "Darkwave", | |
78 [51] = "Techno-Industrial", | |
79 [52] = "Electronic", | |
80 [53] = "Pop-Folk", | |
81 [54] = "Eurodance", | |
82 [55] = "Dream", | |
83 [56] = "Southern Rock", | |
84 [57] = "Comedy", | |
85 [58] = "Cult", | |
86 [59] = "Gangsta", | |
87 [60] = "Top 40", | |
88 [61] = "Christian Rap", | |
89 [62] = "Pop/Funk", | |
90 [63] = "Jungle", | |
91 [64] = "Native American", | |
92 [65] = "Cabaret", | |
93 [66] = "New Wave", | |
94 [67] = "Psychadelic", | |
95 [68] = "Rave", | |
96 [69] = "Showtunes", | |
97 [70] = "Trailer", | |
98 [71] = "Lo-Fi", | |
99 [72] = "Tribal", | |
100 [73] = "Acid Punk", | |
101 [74] = "Acid Jazz", | |
102 [75] = "Polka", | |
103 [76] = "Retro", | |
104 [77] = "Musical", | |
105 [78] = "Rock & Roll", | |
106 [79] = "Hard Rock", | |
107 [80] = "Folk", | |
108 [81] = "Folk-Rock", | |
109 [82] = "National Folk", | |
110 [83] = "Swing", | |
111 [84] = "Fast Fusion", | |
112 [85] = "Bebob", | |
113 [86] = "Latin", | |
114 [87] = "Revival", | |
115 [88] = "Celtic", | |
116 [89] = "Bluegrass", | |
117 [90] = "Avantgarde", | |
118 [91] = "Gothic Rock", | |
119 [92] = "Progressive Rock", | |
120 [93] = "Psychedelic Rock", | |
121 [94] = "Symphonic Rock", | |
122 [95] = "Slow Rock", | |
123 [96] = "Big Band", | |
124 [97] = "Chorus", | |
125 [98] = "Easy Listening", | |
126 [99] = "Acoustic", | |
127 [100] = "Humour", | |
128 [101] = "Speech", | |
129 [102] = "Chanson", | |
130 [103] = "Opera", | |
131 [104] = "Chamber Music", | |
132 [105] = "Sonata", | |
133 [106] = "Symphony", | |
134 [107] = "Booty Bass", | |
135 [108] = "Primus", | |
136 [109] = "Porn Groove", | |
137 [110] = "Satire", | |
138 [111] = "Slow Jam", | |
139 [112] = "Club", | |
140 [113] = "Tango", | |
141 [114] = "Samba", | |
142 [115] = "Folklore", | |
143 [116] = "Ballad", | |
144 [117] = "Power Ballad", | |
145 [118] = "Rhythmic Soul", | |
146 [119] = "Freestyle", | |
147 [120] = "Duet", | |
148 [121] = "Punk Rock", | |
149 [122] = "Drum Solo", | |
150 [123] = "A capella", | |
151 [124] = "Euro-House", | |
152 [125] = "Dance Hall", | |
153 }; | |
154 | |
155 /* buf must be ID3_HEADER_SIZE byte long */ | |
156 static int id3_match(const uint8_t *buf) | |
157 { | |
158 return (buf[0] == 'I' && | |
159 buf[1] == 'D' && | |
160 buf[2] == '3' && | |
161 buf[3] != 0xff && | |
162 buf[4] != 0xff && | |
163 (buf[6] & 0x80) == 0 && | |
164 (buf[7] & 0x80) == 0 && | |
165 (buf[8] & 0x80) == 0 && | |
166 (buf[9] & 0x80) == 0); | |
167 } | |
168 | |
885 | 169 static void id3_get_string(char *str, int str_size, |
234 | 170 const uint8_t *buf, int buf_size) |
171 { | |
172 int i, c; | |
173 char *q; | |
174 | |
175 q = str; | |
176 for(i = 0; i < buf_size; i++) { | |
177 c = buf[i]; | |
178 if (c == '\0') | |
179 break; | |
180 if ((q - str) >= str_size - 1) | |
181 break; | |
182 *q++ = c; | |
183 } | |
184 *q = '\0'; | |
185 } | |
186 | |
187 /* 'buf' must be ID3_TAG_SIZE byte long */ | |
188 static int id3_parse_tag(AVFormatContext *s, const uint8_t *buf) | |
189 { | |
190 char str[5]; | |
191 int genre; | |
885 | 192 |
234 | 193 if (!(buf[0] == 'T' && |
194 buf[1] == 'A' && | |
195 buf[2] == 'G')) | |
196 return -1; | |
197 id3_get_string(s->title, sizeof(s->title), buf + 3, 30); | |
198 id3_get_string(s->author, sizeof(s->author), buf + 33, 30); | |
199 id3_get_string(s->album, sizeof(s->album), buf + 63, 30); | |
200 id3_get_string(str, sizeof(str), buf + 93, 4); | |
201 s->year = atoi(str); | |
202 id3_get_string(s->comment, sizeof(s->comment), buf + 97, 30); | |
203 if (buf[125] == 0 && buf[126] != 0) | |
204 s->track = buf[126]; | |
205 genre = buf[127]; | |
206 if (genre <= ID3_GENRE_MAX) | |
207 pstrcpy(s->genre, sizeof(s->genre), id3_genre_str[genre]); | |
208 return 0; | |
209 } | |
210 | |
211 static void id3_create_tag(AVFormatContext *s, uint8_t *buf) | |
212 { | |
213 int v, i; | |
214 | |
215 memset(buf, 0, ID3_TAG_SIZE); /* fail safe */ | |
216 buf[0] = 'T'; | |
217 buf[1] = 'A'; | |
218 buf[2] = 'G'; | |
219 strncpy(buf + 3, s->title, 30); | |
220 strncpy(buf + 33, s->author, 30); | |
221 strncpy(buf + 63, s->album, 30); | |
222 v = s->year; | |
223 if (v > 0) { | |
224 for(i = 0;i < 4; i++) { | |
225 buf[96 - i] = '0' + (v % 10); | |
226 v = v / 10; | |
227 } | |
228 } | |
229 strncpy(buf + 97, s->comment, 30); | |
230 if (s->track != 0) { | |
231 buf[125] = 0; | |
232 buf[126] = s->track; | |
233 } | |
234 for(i = 0; i <= ID3_GENRE_MAX; i++) { | |
235 if (!strcasecmp(s->genre, id3_genre_str[i])) { | |
236 buf[127] = i; | |
237 break; | |
238 } | |
239 } | |
240 } | |
241 | |
242 /* mp3 read */ | |
1107 | 243 |
244 static int mp3_read_probe(AVProbeData *p) | |
245 { | |
246 int d; | |
247 | |
248 if(p->buf_size < 4) | |
249 return 0; | |
250 | |
251 if(p->buf[0] == 'I' && p->buf[1] == 'D' && p->buf[2] == '3' && | |
252 p->buf[3] < 5) | |
253 return AVPROBE_SCORE_MAX; | |
254 | |
255 if(p->buf[0] != 0xff) | |
256 return 0; | |
257 | |
258 d = p->buf[1]; | |
259 if((d & 0xe0) != 0xe0 || ((d & 0x18) == 0x08 || (d & 0x06) == 0)) | |
260 return 0; | |
261 | |
262 d = p->buf[2]; | |
263 if((d & 0xf0) == 0xf0 || (d & 0x0c) == 0x0c) | |
264 return 0; | |
265 | |
266 return AVPROBE_SCORE_MAX; | |
267 } | |
268 | |
234 | 269 static int mp3_read_header(AVFormatContext *s, |
270 AVFormatParameters *ap) | |
271 { | |
272 AVStream *st; | |
273 uint8_t buf[ID3_TAG_SIZE]; | |
274 int len, ret, filesize; | |
275 | |
276 st = av_new_stream(s, 0); | |
277 if (!st) | |
278 return AVERROR_NOMEM; | |
279 | |
820
feca73904e67
changing AVCodecContext codec -> *codec in AVStream so additions to AVCodecContext dont randomize AVStream and break binary compatibility
michael
parents:
814
diff
changeset
|
280 st->codec->codec_type = CODEC_TYPE_AUDIO; |
feca73904e67
changing AVCodecContext codec -> *codec in AVStream so additions to AVCodecContext dont randomize AVStream and break binary compatibility
michael
parents:
814
diff
changeset
|
281 st->codec->codec_id = CODEC_ID_MP3; |
307 | 282 st->need_parsing = 1; |
885 | 283 |
234 | 284 /* try to get the TAG */ |
285 if (!url_is_streamed(&s->pb)) { | |
286 /* XXX: change that */ | |
764
cdb845a57ae4
drop most url_fileno() calls (allows to use ByteIOContext directly in caller apps instead of URLProtocol)
aurel
parents:
482
diff
changeset
|
287 filesize = url_fsize(&s->pb); |
234 | 288 if (filesize > 128) { |
289 url_fseek(&s->pb, filesize - 128, SEEK_SET); | |
290 ret = get_buffer(&s->pb, buf, ID3_TAG_SIZE); | |
291 if (ret == ID3_TAG_SIZE) { | |
292 id3_parse_tag(s, buf); | |
293 } | |
294 url_fseek(&s->pb, 0, SEEK_SET); | |
295 } | |
296 } | |
297 | |
298 /* if ID3 header found, skip it */ | |
299 ret = get_buffer(&s->pb, buf, ID3_HEADER_SIZE); | |
300 if (ret != ID3_HEADER_SIZE) | |
301 return -1; | |
302 if (id3_match(buf)) { | |
303 /* skip ID3 header */ | |
304 len = ((buf[6] & 0x7f) << 21) | | |
305 ((buf[7] & 0x7f) << 14) | | |
306 ((buf[8] & 0x7f) << 7) | | |
307 (buf[9] & 0x7f); | |
308 url_fskip(&s->pb, len); | |
309 } else { | |
310 url_fseek(&s->pb, 0, SEEK_SET); | |
311 } | |
312 | |
313 /* the parameters will be extracted from the compressed bitstream */ | |
314 return 0; | |
315 } | |
316 | |
317 #define MP3_PACKET_SIZE 1024 | |
318 | |
319 static int mp3_read_packet(AVFormatContext *s, AVPacket *pkt) | |
320 { | |
321 int ret, size; | |
322 // AVStream *st = s->streams[0]; | |
885 | 323 |
234 | 324 size= MP3_PACKET_SIZE; |
325 | |
775 | 326 ret= av_get_packet(&s->pb, pkt, size); |
234 | 327 |
328 pkt->stream_index = 0; | |
329 if (ret <= 0) { | |
482 | 330 return AVERROR_IO; |
234 | 331 } |
332 /* note: we need to modify the packet size here to handle the last | |
333 packet */ | |
334 pkt->size = ret; | |
335 return ret; | |
336 } | |
337 | |
338 static int mp3_read_close(AVFormatContext *s) | |
339 { | |
340 return 0; | |
341 } | |
342 | |
858
66cc656ea404
Replace CONFIG_ENCODERS/CONFIG_DECODERS with CONFIG_MUXERS/CONFIG_DEMUXERS
diego
parents:
820
diff
changeset
|
343 #ifdef CONFIG_MUXERS |
234 | 344 /* simple formats */ |
345 static int mp3_write_header(struct AVFormatContext *s) | |
346 { | |
347 return 0; | |
348 } | |
349 | |
468 | 350 static int mp3_write_packet(struct AVFormatContext *s, AVPacket *pkt) |
234 | 351 { |
468 | 352 put_buffer(&s->pb, pkt->data, pkt->size); |
234 | 353 put_flush_packet(&s->pb); |
354 return 0; | |
355 } | |
356 | |
357 static int mp3_write_trailer(struct AVFormatContext *s) | |
358 { | |
359 uint8_t buf[ID3_TAG_SIZE]; | |
360 | |
361 /* write the id3 header */ | |
362 if (s->title[0] != '\0') { | |
363 id3_create_tag(s, buf); | |
364 put_buffer(&s->pb, buf, ID3_TAG_SIZE); | |
365 put_flush_packet(&s->pb); | |
366 } | |
367 return 0; | |
368 } | |
858
66cc656ea404
Replace CONFIG_ENCODERS/CONFIG_DECODERS with CONFIG_MUXERS/CONFIG_DEMUXERS
diego
parents:
820
diff
changeset
|
369 #endif //CONFIG_MUXERS |
234 | 370 |
1169 | 371 #ifdef CONFIG_MP3_DEMUXER |
1167 | 372 AVInputFormat mp3_demuxer = { |
234 | 373 "mp3", |
374 "MPEG audio", | |
375 0, | |
1107 | 376 mp3_read_probe, |
234 | 377 mp3_read_header, |
378 mp3_read_packet, | |
379 mp3_read_close, | |
814 | 380 .extensions = "mp2,mp3,m2a", /* XXX: use probe */ |
234 | 381 }; |
1169 | 382 #endif |
383 #ifdef CONFIG_MP2_MUXER | |
1167 | 384 AVOutputFormat mp2_muxer = { |
234 | 385 "mp2", |
386 "MPEG audio layer 2", | |
387 "audio/x-mpeg", | |
388 #ifdef CONFIG_MP3LAME | |
814 | 389 "mp2,m2a", |
234 | 390 #else |
814 | 391 "mp2,mp3,m2a", |
234 | 392 #endif |
393 0, | |
394 CODEC_ID_MP2, | |
395 0, | |
396 mp3_write_header, | |
397 mp3_write_packet, | |
398 mp3_write_trailer, | |
399 }; | |
1169 | 400 #endif |
401 #ifdef CONFIG_MP3_MUXER | |
1167 | 402 AVOutputFormat mp3_muxer = { |
234 | 403 "mp3", |
404 "MPEG audio layer 3", | |
405 "audio/x-mpeg", | |
406 "mp3", | |
407 0, | |
408 CODEC_ID_MP3, | |
409 0, | |
410 mp3_write_header, | |
411 mp3_write_packet, | |
412 mp3_write_trailer, | |
413 }; | |
414 #endif |