Mercurial > libavformat.hg
annotate mp3.c @ 1349:04b421915b14 libavformat
rtp.o needs mpegts.o
author | diego |
---|---|
date | Mon, 02 Oct 2006 17:15:00 +0000 |
parents | 9eeb01383e30 |
children | 0899bfe4105c |
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" | |
1308
866d43ed0a67
allow ffmpeg to read mp3s beginning with partial frames
gpoirier
parents:
1169
diff
changeset
|
20 #include "mpegaudio.h" |
234 | 21 |
22 #define ID3_HEADER_SIZE 10 | |
23 #define ID3_TAG_SIZE 128 | |
24 | |
25 #define ID3_GENRE_MAX 125 | |
26 | |
27 static const char *id3_genre_str[ID3_GENRE_MAX + 1] = { | |
28 [0] = "Blues", | |
29 [1] = "Classic Rock", | |
30 [2] = "Country", | |
31 [3] = "Dance", | |
32 [4] = "Disco", | |
33 [5] = "Funk", | |
34 [6] = "Grunge", | |
35 [7] = "Hip-Hop", | |
36 [8] = "Jazz", | |
37 [9] = "Metal", | |
38 [10] = "New Age", | |
39 [11] = "Oldies", | |
40 [12] = "Other", | |
41 [13] = "Pop", | |
42 [14] = "R&B", | |
43 [15] = "Rap", | |
44 [16] = "Reggae", | |
45 [17] = "Rock", | |
46 [18] = "Techno", | |
47 [19] = "Industrial", | |
48 [20] = "Alternative", | |
49 [21] = "Ska", | |
50 [22] = "Death Metal", | |
51 [23] = "Pranks", | |
52 [24] = "Soundtrack", | |
53 [25] = "Euro-Techno", | |
54 [26] = "Ambient", | |
55 [27] = "Trip-Hop", | |
56 [28] = "Vocal", | |
57 [29] = "Jazz+Funk", | |
58 [30] = "Fusion", | |
59 [31] = "Trance", | |
60 [32] = "Classical", | |
61 [33] = "Instrumental", | |
62 [34] = "Acid", | |
63 [35] = "House", | |
64 [36] = "Game", | |
65 [37] = "Sound Clip", | |
66 [38] = "Gospel", | |
67 [39] = "Noise", | |
68 [40] = "AlternRock", | |
69 [41] = "Bass", | |
70 [42] = "Soul", | |
71 [43] = "Punk", | |
72 [44] = "Space", | |
73 [45] = "Meditative", | |
74 [46] = "Instrumental Pop", | |
75 [47] = "Instrumental Rock", | |
76 [48] = "Ethnic", | |
77 [49] = "Gothic", | |
78 [50] = "Darkwave", | |
79 [51] = "Techno-Industrial", | |
80 [52] = "Electronic", | |
81 [53] = "Pop-Folk", | |
82 [54] = "Eurodance", | |
83 [55] = "Dream", | |
84 [56] = "Southern Rock", | |
85 [57] = "Comedy", | |
86 [58] = "Cult", | |
87 [59] = "Gangsta", | |
88 [60] = "Top 40", | |
89 [61] = "Christian Rap", | |
90 [62] = "Pop/Funk", | |
91 [63] = "Jungle", | |
92 [64] = "Native American", | |
93 [65] = "Cabaret", | |
94 [66] = "New Wave", | |
95 [67] = "Psychadelic", | |
96 [68] = "Rave", | |
97 [69] = "Showtunes", | |
98 [70] = "Trailer", | |
99 [71] = "Lo-Fi", | |
100 [72] = "Tribal", | |
101 [73] = "Acid Punk", | |
102 [74] = "Acid Jazz", | |
103 [75] = "Polka", | |
104 [76] = "Retro", | |
105 [77] = "Musical", | |
106 [78] = "Rock & Roll", | |
107 [79] = "Hard Rock", | |
108 [80] = "Folk", | |
109 [81] = "Folk-Rock", | |
110 [82] = "National Folk", | |
111 [83] = "Swing", | |
112 [84] = "Fast Fusion", | |
113 [85] = "Bebob", | |
114 [86] = "Latin", | |
115 [87] = "Revival", | |
116 [88] = "Celtic", | |
117 [89] = "Bluegrass", | |
118 [90] = "Avantgarde", | |
119 [91] = "Gothic Rock", | |
120 [92] = "Progressive Rock", | |
121 [93] = "Psychedelic Rock", | |
122 [94] = "Symphonic Rock", | |
123 [95] = "Slow Rock", | |
124 [96] = "Big Band", | |
125 [97] = "Chorus", | |
126 [98] = "Easy Listening", | |
127 [99] = "Acoustic", | |
128 [100] = "Humour", | |
129 [101] = "Speech", | |
130 [102] = "Chanson", | |
131 [103] = "Opera", | |
132 [104] = "Chamber Music", | |
133 [105] = "Sonata", | |
134 [106] = "Symphony", | |
135 [107] = "Booty Bass", | |
136 [108] = "Primus", | |
137 [109] = "Porn Groove", | |
138 [110] = "Satire", | |
139 [111] = "Slow Jam", | |
140 [112] = "Club", | |
141 [113] = "Tango", | |
142 [114] = "Samba", | |
143 [115] = "Folklore", | |
144 [116] = "Ballad", | |
145 [117] = "Power Ballad", | |
146 [118] = "Rhythmic Soul", | |
147 [119] = "Freestyle", | |
148 [120] = "Duet", | |
149 [121] = "Punk Rock", | |
150 [122] = "Drum Solo", | |
151 [123] = "A capella", | |
152 [124] = "Euro-House", | |
153 [125] = "Dance Hall", | |
154 }; | |
155 | |
156 /* buf must be ID3_HEADER_SIZE byte long */ | |
157 static int id3_match(const uint8_t *buf) | |
158 { | |
159 return (buf[0] == 'I' && | |
160 buf[1] == 'D' && | |
161 buf[2] == '3' && | |
162 buf[3] != 0xff && | |
163 buf[4] != 0xff && | |
164 (buf[6] & 0x80) == 0 && | |
165 (buf[7] & 0x80) == 0 && | |
166 (buf[8] & 0x80) == 0 && | |
167 (buf[9] & 0x80) == 0); | |
168 } | |
169 | |
885 | 170 static void id3_get_string(char *str, int str_size, |
234 | 171 const uint8_t *buf, int buf_size) |
172 { | |
173 int i, c; | |
174 char *q; | |
175 | |
176 q = str; | |
177 for(i = 0; i < buf_size; i++) { | |
178 c = buf[i]; | |
179 if (c == '\0') | |
180 break; | |
181 if ((q - str) >= str_size - 1) | |
182 break; | |
183 *q++ = c; | |
184 } | |
185 *q = '\0'; | |
186 } | |
187 | |
188 /* 'buf' must be ID3_TAG_SIZE byte long */ | |
189 static int id3_parse_tag(AVFormatContext *s, const uint8_t *buf) | |
190 { | |
191 char str[5]; | |
192 int genre; | |
885 | 193 |
234 | 194 if (!(buf[0] == 'T' && |
195 buf[1] == 'A' && | |
196 buf[2] == 'G')) | |
197 return -1; | |
198 id3_get_string(s->title, sizeof(s->title), buf + 3, 30); | |
199 id3_get_string(s->author, sizeof(s->author), buf + 33, 30); | |
200 id3_get_string(s->album, sizeof(s->album), buf + 63, 30); | |
201 id3_get_string(str, sizeof(str), buf + 93, 4); | |
202 s->year = atoi(str); | |
203 id3_get_string(s->comment, sizeof(s->comment), buf + 97, 30); | |
204 if (buf[125] == 0 && buf[126] != 0) | |
205 s->track = buf[126]; | |
206 genre = buf[127]; | |
207 if (genre <= ID3_GENRE_MAX) | |
208 pstrcpy(s->genre, sizeof(s->genre), id3_genre_str[genre]); | |
209 return 0; | |
210 } | |
211 | |
212 static void id3_create_tag(AVFormatContext *s, uint8_t *buf) | |
213 { | |
214 int v, i; | |
215 | |
216 memset(buf, 0, ID3_TAG_SIZE); /* fail safe */ | |
217 buf[0] = 'T'; | |
218 buf[1] = 'A'; | |
219 buf[2] = 'G'; | |
220 strncpy(buf + 3, s->title, 30); | |
221 strncpy(buf + 33, s->author, 30); | |
222 strncpy(buf + 63, s->album, 30); | |
223 v = s->year; | |
224 if (v > 0) { | |
225 for(i = 0;i < 4; i++) { | |
226 buf[96 - i] = '0' + (v % 10); | |
227 v = v / 10; | |
228 } | |
229 } | |
230 strncpy(buf + 97, s->comment, 30); | |
231 if (s->track != 0) { | |
232 buf[125] = 0; | |
233 buf[126] = s->track; | |
234 } | |
235 for(i = 0; i <= ID3_GENRE_MAX; i++) { | |
236 if (!strcasecmp(s->genre, id3_genre_str[i])) { | |
237 buf[127] = i; | |
238 break; | |
239 } | |
240 } | |
241 } | |
242 | |
243 /* mp3 read */ | |
1107 | 244 |
245 static int mp3_read_probe(AVProbeData *p) | |
246 { | |
1321
9eeb01383e30
reduce scores if the mp3 frames dont start from the begin of the file (fixes flv deteted as mp3 issues)
michael
parents:
1312
diff
changeset
|
247 int max_frames, first_frames; |
1308
866d43ed0a67
allow ffmpeg to read mp3s beginning with partial frames
gpoirier
parents:
1169
diff
changeset
|
248 int fsize, frames; |
866d43ed0a67
allow ffmpeg to read mp3s beginning with partial frames
gpoirier
parents:
1169
diff
changeset
|
249 uint32_t header; |
866d43ed0a67
allow ffmpeg to read mp3s beginning with partial frames
gpoirier
parents:
1169
diff
changeset
|
250 uint8_t *buf, *buf2, *end; |
866d43ed0a67
allow ffmpeg to read mp3s beginning with partial frames
gpoirier
parents:
1169
diff
changeset
|
251 AVCodecContext avctx; |
1107 | 252 |
1308
866d43ed0a67
allow ffmpeg to read mp3s beginning with partial frames
gpoirier
parents:
1169
diff
changeset
|
253 if(p->buf_size < ID3_HEADER_SIZE) |
1107 | 254 return 0; |
255 | |
1308
866d43ed0a67
allow ffmpeg to read mp3s beginning with partial frames
gpoirier
parents:
1169
diff
changeset
|
256 if(id3_match(p->buf)) |
1107 | 257 return AVPROBE_SCORE_MAX; |
258 | |
1308
866d43ed0a67
allow ffmpeg to read mp3s beginning with partial frames
gpoirier
parents:
1169
diff
changeset
|
259 max_frames = 0; |
866d43ed0a67
allow ffmpeg to read mp3s beginning with partial frames
gpoirier
parents:
1169
diff
changeset
|
260 buf = p->buf; |
866d43ed0a67
allow ffmpeg to read mp3s beginning with partial frames
gpoirier
parents:
1169
diff
changeset
|
261 end = buf + FFMIN(4096, p->buf_size - sizeof(uint32_t)); |
866d43ed0a67
allow ffmpeg to read mp3s beginning with partial frames
gpoirier
parents:
1169
diff
changeset
|
262 |
866d43ed0a67
allow ffmpeg to read mp3s beginning with partial frames
gpoirier
parents:
1169
diff
changeset
|
263 for(; buf < end; buf++) { |
866d43ed0a67
allow ffmpeg to read mp3s beginning with partial frames
gpoirier
parents:
1169
diff
changeset
|
264 buf2 = buf; |
1107 | 265 |
1312 | 266 for(frames = 0; buf2 < end; frames++) { |
1308
866d43ed0a67
allow ffmpeg to read mp3s beginning with partial frames
gpoirier
parents:
1169
diff
changeset
|
267 header = (buf2[0] << 24) | (buf2[1] << 16) | (buf2[2] << 8) | buf2[3]; |
866d43ed0a67
allow ffmpeg to read mp3s beginning with partial frames
gpoirier
parents:
1169
diff
changeset
|
268 fsize = mpa_decode_header(&avctx, header); |
866d43ed0a67
allow ffmpeg to read mp3s beginning with partial frames
gpoirier
parents:
1169
diff
changeset
|
269 if(fsize < 0) |
866d43ed0a67
allow ffmpeg to read mp3s beginning with partial frames
gpoirier
parents:
1169
diff
changeset
|
270 break; |
866d43ed0a67
allow ffmpeg to read mp3s beginning with partial frames
gpoirier
parents:
1169
diff
changeset
|
271 buf2 += fsize; |
866d43ed0a67
allow ffmpeg to read mp3s beginning with partial frames
gpoirier
parents:
1169
diff
changeset
|
272 } |
866d43ed0a67
allow ffmpeg to read mp3s beginning with partial frames
gpoirier
parents:
1169
diff
changeset
|
273 max_frames = FFMAX(max_frames, frames); |
1321
9eeb01383e30
reduce scores if the mp3 frames dont start from the begin of the file (fixes flv deteted as mp3 issues)
michael
parents:
1312
diff
changeset
|
274 if(buf == p->buf) |
9eeb01383e30
reduce scores if the mp3 frames dont start from the begin of the file (fixes flv deteted as mp3 issues)
michael
parents:
1312
diff
changeset
|
275 first_frames= frames; |
1308
866d43ed0a67
allow ffmpeg to read mp3s beginning with partial frames
gpoirier
parents:
1169
diff
changeset
|
276 } |
1321
9eeb01383e30
reduce scores if the mp3 frames dont start from the begin of the file (fixes flv deteted as mp3 issues)
michael
parents:
1312
diff
changeset
|
277 if (first_frames>=3) return AVPROBE_SCORE_MAX/2+1; |
9eeb01383e30
reduce scores if the mp3 frames dont start from the begin of the file (fixes flv deteted as mp3 issues)
michael
parents:
1312
diff
changeset
|
278 else if(max_frames>=3) return AVPROBE_SCORE_MAX/4; |
9eeb01383e30
reduce scores if the mp3 frames dont start from the begin of the file (fixes flv deteted as mp3 issues)
michael
parents:
1312
diff
changeset
|
279 else if(max_frames>=1) return 1; |
1308
866d43ed0a67
allow ffmpeg to read mp3s beginning with partial frames
gpoirier
parents:
1169
diff
changeset
|
280 else return 0; |
1107 | 281 } |
282 | |
234 | 283 static int mp3_read_header(AVFormatContext *s, |
284 AVFormatParameters *ap) | |
285 { | |
286 AVStream *st; | |
287 uint8_t buf[ID3_TAG_SIZE]; | |
288 int len, ret, filesize; | |
289 | |
290 st = av_new_stream(s, 0); | |
291 if (!st) | |
292 return AVERROR_NOMEM; | |
293 | |
820
feca73904e67
changing AVCodecContext codec -> *codec in AVStream so additions to AVCodecContext dont randomize AVStream and break binary compatibility
michael
parents:
814
diff
changeset
|
294 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
|
295 st->codec->codec_id = CODEC_ID_MP3; |
307 | 296 st->need_parsing = 1; |
885 | 297 |
234 | 298 /* try to get the TAG */ |
299 if (!url_is_streamed(&s->pb)) { | |
300 /* 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
|
301 filesize = url_fsize(&s->pb); |
234 | 302 if (filesize > 128) { |
303 url_fseek(&s->pb, filesize - 128, SEEK_SET); | |
304 ret = get_buffer(&s->pb, buf, ID3_TAG_SIZE); | |
305 if (ret == ID3_TAG_SIZE) { | |
306 id3_parse_tag(s, buf); | |
307 } | |
308 url_fseek(&s->pb, 0, SEEK_SET); | |
309 } | |
310 } | |
311 | |
312 /* if ID3 header found, skip it */ | |
313 ret = get_buffer(&s->pb, buf, ID3_HEADER_SIZE); | |
314 if (ret != ID3_HEADER_SIZE) | |
315 return -1; | |
316 if (id3_match(buf)) { | |
317 /* skip ID3 header */ | |
318 len = ((buf[6] & 0x7f) << 21) | | |
319 ((buf[7] & 0x7f) << 14) | | |
320 ((buf[8] & 0x7f) << 7) | | |
321 (buf[9] & 0x7f); | |
322 url_fskip(&s->pb, len); | |
323 } else { | |
324 url_fseek(&s->pb, 0, SEEK_SET); | |
325 } | |
326 | |
327 /* the parameters will be extracted from the compressed bitstream */ | |
328 return 0; | |
329 } | |
330 | |
331 #define MP3_PACKET_SIZE 1024 | |
332 | |
333 static int mp3_read_packet(AVFormatContext *s, AVPacket *pkt) | |
334 { | |
335 int ret, size; | |
336 // AVStream *st = s->streams[0]; | |
885 | 337 |
234 | 338 size= MP3_PACKET_SIZE; |
339 | |
775 | 340 ret= av_get_packet(&s->pb, pkt, size); |
234 | 341 |
342 pkt->stream_index = 0; | |
343 if (ret <= 0) { | |
482 | 344 return AVERROR_IO; |
234 | 345 } |
346 /* note: we need to modify the packet size here to handle the last | |
347 packet */ | |
348 pkt->size = ret; | |
349 return ret; | |
350 } | |
351 | |
352 static int mp3_read_close(AVFormatContext *s) | |
353 { | |
354 return 0; | |
355 } | |
356 | |
858
66cc656ea404
Replace CONFIG_ENCODERS/CONFIG_DECODERS with CONFIG_MUXERS/CONFIG_DEMUXERS
diego
parents:
820
diff
changeset
|
357 #ifdef CONFIG_MUXERS |
234 | 358 /* simple formats */ |
359 static int mp3_write_header(struct AVFormatContext *s) | |
360 { | |
361 return 0; | |
362 } | |
363 | |
468 | 364 static int mp3_write_packet(struct AVFormatContext *s, AVPacket *pkt) |
234 | 365 { |
468 | 366 put_buffer(&s->pb, pkt->data, pkt->size); |
234 | 367 put_flush_packet(&s->pb); |
368 return 0; | |
369 } | |
370 | |
371 static int mp3_write_trailer(struct AVFormatContext *s) | |
372 { | |
373 uint8_t buf[ID3_TAG_SIZE]; | |
374 | |
375 /* write the id3 header */ | |
376 if (s->title[0] != '\0') { | |
377 id3_create_tag(s, buf); | |
378 put_buffer(&s->pb, buf, ID3_TAG_SIZE); | |
379 put_flush_packet(&s->pb); | |
380 } | |
381 return 0; | |
382 } | |
858
66cc656ea404
Replace CONFIG_ENCODERS/CONFIG_DECODERS with CONFIG_MUXERS/CONFIG_DEMUXERS
diego
parents:
820
diff
changeset
|
383 #endif //CONFIG_MUXERS |
234 | 384 |
1169 | 385 #ifdef CONFIG_MP3_DEMUXER |
1167 | 386 AVInputFormat mp3_demuxer = { |
234 | 387 "mp3", |
388 "MPEG audio", | |
389 0, | |
1107 | 390 mp3_read_probe, |
234 | 391 mp3_read_header, |
392 mp3_read_packet, | |
393 mp3_read_close, | |
814 | 394 .extensions = "mp2,mp3,m2a", /* XXX: use probe */ |
234 | 395 }; |
1169 | 396 #endif |
397 #ifdef CONFIG_MP2_MUXER | |
1167 | 398 AVOutputFormat mp2_muxer = { |
234 | 399 "mp2", |
400 "MPEG audio layer 2", | |
401 "audio/x-mpeg", | |
402 #ifdef CONFIG_MP3LAME | |
814 | 403 "mp2,m2a", |
234 | 404 #else |
814 | 405 "mp2,mp3,m2a", |
234 | 406 #endif |
407 0, | |
408 CODEC_ID_MP2, | |
409 0, | |
410 mp3_write_header, | |
411 mp3_write_packet, | |
412 mp3_write_trailer, | |
413 }; | |
1169 | 414 #endif |
415 #ifdef CONFIG_MP3_MUXER | |
1167 | 416 AVOutputFormat mp3_muxer = { |
234 | 417 "mp3", |
418 "MPEG audio layer 3", | |
419 "audio/x-mpeg", | |
420 "mp3", | |
421 0, | |
422 CODEC_ID_MP3, | |
423 0, | |
424 mp3_write_header, | |
425 mp3_write_packet, | |
426 mp3_write_trailer, | |
427 }; | |
428 #endif |