Mercurial > libavformat.hg
annotate mp3.c @ 2123:ffd1de292ea7 libavformat
skip no more needed on error
author | bcoudurier |
---|---|
date | Sun, 03 Jun 2007 18:04:33 +0000 |
parents | b51b57909b5f |
children | da71207a7cf1 |
rev | line source |
---|---|
885 | 1 /* |
1415
3b00fb8ef8e4
replace coder/decoder file description in libavformat by muxer/demuxer
aurel
parents:
1358
diff
changeset
|
2 * MP3 muxer and demuxer |
234 | 3 * Copyright (c) 2003 Fabrice Bellard. |
4 * | |
1358
0899bfe4105c
Change license headers to say 'FFmpeg' instead of 'this program/this library'
diego
parents:
1321
diff
changeset
|
5 * This file is part of FFmpeg. |
0899bfe4105c
Change license headers to say 'FFmpeg' instead of 'this program/this library'
diego
parents:
1321
diff
changeset
|
6 * |
0899bfe4105c
Change license headers to say 'FFmpeg' instead of 'this program/this library'
diego
parents:
1321
diff
changeset
|
7 * FFmpeg is free software; you can redistribute it and/or |
234 | 8 * modify it under the terms of the GNU Lesser General Public |
9 * License as published by the Free Software Foundation; either | |
1358
0899bfe4105c
Change license headers to say 'FFmpeg' instead of 'this program/this library'
diego
parents:
1321
diff
changeset
|
10 * version 2.1 of the License, or (at your option) any later version. |
234 | 11 * |
1358
0899bfe4105c
Change license headers to say 'FFmpeg' instead of 'this program/this library'
diego
parents:
1321
diff
changeset
|
12 * FFmpeg is distributed in the hope that it will be useful, |
234 | 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. | |
16 * | |
17 * You should have received a copy of the GNU Lesser General Public | |
1358
0899bfe4105c
Change license headers to say 'FFmpeg' instead of 'this program/this library'
diego
parents:
1321
diff
changeset
|
18 * License along with FFmpeg; if not, write to the Free Software |
896
edbe5c3717f9
Update licensing information: The FSF changed postal address.
diego
parents:
885
diff
changeset
|
19 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA |
234 | 20 */ |
21 #include "avformat.h" | |
1308
866d43ed0a67
allow ffmpeg to read mp3s beginning with partial frames
gpoirier
parents:
1169
diff
changeset
|
22 #include "mpegaudio.h" |
234 | 23 |
2096 | 24 #define ID3v2_HEADER_SIZE 10 |
25 #define ID3v1_TAG_SIZE 128 | |
234 | 26 |
2096 | 27 #define ID3v1_GENRE_MAX 125 |
234 | 28 |
2096 | 29 static const char *id3v1_genre_str[ID3v1_GENRE_MAX + 1] = { |
234 | 30 [0] = "Blues", |
31 [1] = "Classic Rock", | |
32 [2] = "Country", | |
33 [3] = "Dance", | |
34 [4] = "Disco", | |
35 [5] = "Funk", | |
36 [6] = "Grunge", | |
37 [7] = "Hip-Hop", | |
38 [8] = "Jazz", | |
39 [9] = "Metal", | |
40 [10] = "New Age", | |
41 [11] = "Oldies", | |
42 [12] = "Other", | |
43 [13] = "Pop", | |
44 [14] = "R&B", | |
45 [15] = "Rap", | |
46 [16] = "Reggae", | |
47 [17] = "Rock", | |
48 [18] = "Techno", | |
49 [19] = "Industrial", | |
50 [20] = "Alternative", | |
51 [21] = "Ska", | |
52 [22] = "Death Metal", | |
53 [23] = "Pranks", | |
54 [24] = "Soundtrack", | |
55 [25] = "Euro-Techno", | |
56 [26] = "Ambient", | |
57 [27] = "Trip-Hop", | |
58 [28] = "Vocal", | |
59 [29] = "Jazz+Funk", | |
60 [30] = "Fusion", | |
61 [31] = "Trance", | |
62 [32] = "Classical", | |
63 [33] = "Instrumental", | |
64 [34] = "Acid", | |
65 [35] = "House", | |
66 [36] = "Game", | |
67 [37] = "Sound Clip", | |
68 [38] = "Gospel", | |
69 [39] = "Noise", | |
70 [40] = "AlternRock", | |
71 [41] = "Bass", | |
72 [42] = "Soul", | |
73 [43] = "Punk", | |
74 [44] = "Space", | |
75 [45] = "Meditative", | |
76 [46] = "Instrumental Pop", | |
77 [47] = "Instrumental Rock", | |
78 [48] = "Ethnic", | |
79 [49] = "Gothic", | |
80 [50] = "Darkwave", | |
81 [51] = "Techno-Industrial", | |
82 [52] = "Electronic", | |
83 [53] = "Pop-Folk", | |
84 [54] = "Eurodance", | |
85 [55] = "Dream", | |
86 [56] = "Southern Rock", | |
87 [57] = "Comedy", | |
88 [58] = "Cult", | |
89 [59] = "Gangsta", | |
90 [60] = "Top 40", | |
91 [61] = "Christian Rap", | |
92 [62] = "Pop/Funk", | |
93 [63] = "Jungle", | |
94 [64] = "Native American", | |
95 [65] = "Cabaret", | |
96 [66] = "New Wave", | |
97 [67] = "Psychadelic", | |
98 [68] = "Rave", | |
99 [69] = "Showtunes", | |
100 [70] = "Trailer", | |
101 [71] = "Lo-Fi", | |
102 [72] = "Tribal", | |
103 [73] = "Acid Punk", | |
104 [74] = "Acid Jazz", | |
105 [75] = "Polka", | |
106 [76] = "Retro", | |
107 [77] = "Musical", | |
108 [78] = "Rock & Roll", | |
109 [79] = "Hard Rock", | |
110 [80] = "Folk", | |
111 [81] = "Folk-Rock", | |
112 [82] = "National Folk", | |
113 [83] = "Swing", | |
114 [84] = "Fast Fusion", | |
115 [85] = "Bebob", | |
116 [86] = "Latin", | |
117 [87] = "Revival", | |
118 [88] = "Celtic", | |
119 [89] = "Bluegrass", | |
120 [90] = "Avantgarde", | |
121 [91] = "Gothic Rock", | |
122 [92] = "Progressive Rock", | |
123 [93] = "Psychedelic Rock", | |
124 [94] = "Symphonic Rock", | |
125 [95] = "Slow Rock", | |
126 [96] = "Big Band", | |
127 [97] = "Chorus", | |
128 [98] = "Easy Listening", | |
129 [99] = "Acoustic", | |
130 [100] = "Humour", | |
131 [101] = "Speech", | |
132 [102] = "Chanson", | |
133 [103] = "Opera", | |
134 [104] = "Chamber Music", | |
135 [105] = "Sonata", | |
136 [106] = "Symphony", | |
137 [107] = "Booty Bass", | |
138 [108] = "Primus", | |
139 [109] = "Porn Groove", | |
140 [110] = "Satire", | |
141 [111] = "Slow Jam", | |
142 [112] = "Club", | |
143 [113] = "Tango", | |
144 [114] = "Samba", | |
145 [115] = "Folklore", | |
146 [116] = "Ballad", | |
147 [117] = "Power Ballad", | |
148 [118] = "Rhythmic Soul", | |
149 [119] = "Freestyle", | |
150 [120] = "Duet", | |
151 [121] = "Punk Rock", | |
152 [122] = "Drum Solo", | |
153 [123] = "A capella", | |
154 [124] = "Euro-House", | |
155 [125] = "Dance Hall", | |
156 }; | |
157 | |
2096 | 158 /* buf must be ID3v2_HEADER_SIZE byte long */ |
159 static int id3v2_match(const uint8_t *buf) | |
234 | 160 { |
161 return (buf[0] == 'I' && | |
162 buf[1] == 'D' && | |
163 buf[2] == '3' && | |
164 buf[3] != 0xff && | |
165 buf[4] != 0xff && | |
166 (buf[6] & 0x80) == 0 && | |
167 (buf[7] & 0x80) == 0 && | |
168 (buf[8] & 0x80) == 0 && | |
169 (buf[9] & 0x80) == 0); | |
170 } | |
171 | |
2097 | 172 static unsigned int id3v2_get_size(ByteIOContext *s, int len) |
173 { | |
174 int v=0; | |
175 while(len--) | |
176 v= (v<<7) + (get_byte(s)&0x7F); | |
177 return v; | |
178 } | |
179 | |
180 static void id3v2_read_ttag(AVFormatContext *s, int taglen, char *dst, int dstlen) | |
181 { | |
182 char *q; | |
183 int len; | |
184 | |
185 if(taglen < 1) | |
186 return; | |
187 | |
188 taglen--; /* account for encoding type byte */ | |
189 dstlen--; /* Leave space for zero terminator */ | |
190 | |
191 switch(get_byte(&s->pb)) { /* encoding type */ | |
192 | |
193 case 0: /* ISO-8859-1 (0 - 255 maps directly into unicode) */ | |
194 q = dst; | |
195 while(taglen--) { | |
196 uint8_t tmp; | |
197 PUT_UTF8(get_byte(&s->pb), tmp, if (q - dst < dstlen - 1) *q++ = tmp;) | |
198 } | |
199 *q = '\0'; | |
200 break; | |
201 | |
202 case 3: /* UTF-8 */ | |
203 len = FFMIN(taglen, dstlen); | |
204 get_buffer(&s->pb, dst, len); | |
205 dst[len] = 0; | |
206 break; | |
207 } | |
208 } | |
209 | |
210 /** | |
211 * ID3v2 parser | |
212 * | |
213 * Handles ID3v2.2, 2.3 and 2.4. | |
214 * | |
215 */ | |
216 | |
217 static void id3v2_parse(AVFormatContext *s, int len, uint8_t version, uint8_t flags) | |
218 { | |
219 int isv34, tlen; | |
220 uint32_t tag; | |
221 offset_t next; | |
222 char tmp[16]; | |
223 int taghdrlen; | |
224 const char *reason; | |
225 | |
226 switch(version) { | |
227 case 2: | |
228 if(flags & 0x40) { | |
229 reason = "compression"; | |
230 goto error; | |
231 } | |
232 isv34 = 0; | |
233 taghdrlen = 6; | |
234 break; | |
235 | |
236 case 3 ... 4: | |
237 isv34 = 1; | |
238 taghdrlen = 10; | |
239 break; | |
240 | |
241 default: | |
242 reason = "version"; | |
243 goto error; | |
244 } | |
245 | |
246 if(flags & 0x80) { | |
247 reason = "unsynchronization"; | |
248 goto error; | |
249 } | |
250 | |
251 if(isv34 && flags & 0x40) /* Extended header present, just skip over it */ | |
252 url_fskip(&s->pb, id3v2_get_size(&s->pb, 4)); | |
253 | |
254 while(len >= taghdrlen) { | |
255 if(isv34) { | |
256 tag = get_be32(&s->pb); | |
257 tlen = id3v2_get_size(&s->pb, 4); | |
258 get_be16(&s->pb); /* flags */ | |
259 } else { | |
260 tag = get_be24(&s->pb); | |
261 tlen = id3v2_get_size(&s->pb, 3); | |
262 } | |
263 len -= taghdrlen + tlen; | |
264 | |
265 if(len < 0) | |
266 break; | |
267 | |
268 next = url_ftell(&s->pb) + tlen; | |
269 | |
270 switch(tag) { | |
271 case MKBETAG('T', 'I', 'T', '2'): | |
272 case MKBETAG(0, 'T', 'T', '2'): | |
273 id3v2_read_ttag(s, tlen, s->title, sizeof(s->title)); | |
274 break; | |
275 case MKBETAG('T', 'P', 'E', '1'): | |
276 case MKBETAG(0, 'T', 'P', '1'): | |
277 id3v2_read_ttag(s, tlen, s->author, sizeof(s->author)); | |
278 break; | |
279 case MKBETAG('T', 'A', 'L', 'B'): | |
280 case MKBETAG(0, 'T', 'A', 'L'): | |
281 id3v2_read_ttag(s, tlen, s->album, sizeof(s->album)); | |
282 break; | |
283 case MKBETAG('T', 'C', 'O', 'N'): | |
284 case MKBETAG(0, 'T', 'C', 'O'): | |
285 id3v2_read_ttag(s, tlen, s->genre, sizeof(s->genre)); | |
286 break; | |
287 case MKBETAG('T', 'C', 'O', 'P'): | |
288 case MKBETAG(0, 'T', 'C', 'R'): | |
289 id3v2_read_ttag(s, tlen, s->copyright, sizeof(s->copyright)); | |
290 break; | |
291 case MKBETAG('T', 'R', 'C', 'K'): | |
292 case MKBETAG(0, 'T', 'R', 'K'): | |
293 id3v2_read_ttag(s, tlen, tmp, sizeof(tmp)); | |
294 s->track = atoi(tmp); | |
295 break; | |
296 case 0: | |
297 /* padding, skip to end */ | |
298 url_fskip(&s->pb, len); | |
299 len = 0; | |
300 continue; | |
301 } | |
302 /* Skip to end of tag */ | |
303 url_fseek(&s->pb, next, SEEK_SET); | |
304 } | |
305 | |
306 if(version == 4 && flags & 0x10) /* Footer preset, always 10 bytes, skip over it */ | |
307 url_fskip(&s->pb, 10); | |
308 return; | |
309 | |
310 error: | |
311 av_log(s, AV_LOG_INFO, "ID3v2.%d tag skipped, cannot handle %s\n", version, reason); | |
312 url_fskip(&s->pb, len); | |
313 } | |
314 | |
2096 | 315 static void id3v1_get_string(char *str, int str_size, |
316 const uint8_t *buf, int buf_size) | |
234 | 317 { |
318 int i, c; | |
319 char *q; | |
320 | |
321 q = str; | |
322 for(i = 0; i < buf_size; i++) { | |
323 c = buf[i]; | |
324 if (c == '\0') | |
325 break; | |
326 if ((q - str) >= str_size - 1) | |
327 break; | |
328 *q++ = c; | |
329 } | |
330 *q = '\0'; | |
331 } | |
332 | |
2096 | 333 /* 'buf' must be ID3v1_TAG_SIZE byte long */ |
334 static int id3v1_parse_tag(AVFormatContext *s, const uint8_t *buf) | |
234 | 335 { |
336 char str[5]; | |
337 int genre; | |
885 | 338 |
234 | 339 if (!(buf[0] == 'T' && |
340 buf[1] == 'A' && | |
341 buf[2] == 'G')) | |
342 return -1; | |
2096 | 343 id3v1_get_string(s->title, sizeof(s->title), buf + 3, 30); |
344 id3v1_get_string(s->author, sizeof(s->author), buf + 33, 30); | |
345 id3v1_get_string(s->album, sizeof(s->album), buf + 63, 30); | |
346 id3v1_get_string(str, sizeof(str), buf + 93, 4); | |
234 | 347 s->year = atoi(str); |
2096 | 348 id3v1_get_string(s->comment, sizeof(s->comment), buf + 97, 30); |
234 | 349 if (buf[125] == 0 && buf[126] != 0) |
350 s->track = buf[126]; | |
351 genre = buf[127]; | |
2096 | 352 if (genre <= ID3v1_GENRE_MAX) |
353 pstrcpy(s->genre, sizeof(s->genre), id3v1_genre_str[genre]); | |
234 | 354 return 0; |
355 } | |
356 | |
2096 | 357 static void id3v1_create_tag(AVFormatContext *s, uint8_t *buf) |
234 | 358 { |
359 int v, i; | |
360 | |
2096 | 361 memset(buf, 0, ID3v1_TAG_SIZE); /* fail safe */ |
234 | 362 buf[0] = 'T'; |
363 buf[1] = 'A'; | |
364 buf[2] = 'G'; | |
365 strncpy(buf + 3, s->title, 30); | |
366 strncpy(buf + 33, s->author, 30); | |
367 strncpy(buf + 63, s->album, 30); | |
368 v = s->year; | |
369 if (v > 0) { | |
370 for(i = 0;i < 4; i++) { | |
371 buf[96 - i] = '0' + (v % 10); | |
372 v = v / 10; | |
373 } | |
374 } | |
375 strncpy(buf + 97, s->comment, 30); | |
376 if (s->track != 0) { | |
377 buf[125] = 0; | |
378 buf[126] = s->track; | |
379 } | |
2096 | 380 for(i = 0; i <= ID3v1_GENRE_MAX; i++) { |
381 if (!strcasecmp(s->genre, id3v1_genre_str[i])) { | |
234 | 382 buf[127] = i; |
383 break; | |
384 } | |
385 } | |
386 } | |
387 | |
388 /* mp3 read */ | |
1107 | 389 |
390 static int mp3_read_probe(AVProbeData *p) | |
391 { | |
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
|
392 int max_frames, first_frames; |
1433
dababce8f69e
dont set the sampling rate just because 1 mp3 packet header says so (fixes playback speed on some old mencoder generated avis which where then dumped to mp3)
michael
parents:
1415
diff
changeset
|
393 int fsize, frames, sample_rate; |
1308
866d43ed0a67
allow ffmpeg to read mp3s beginning with partial frames
gpoirier
parents:
1169
diff
changeset
|
394 uint32_t header; |
866d43ed0a67
allow ffmpeg to read mp3s beginning with partial frames
gpoirier
parents:
1169
diff
changeset
|
395 uint8_t *buf, *buf2, *end; |
866d43ed0a67
allow ffmpeg to read mp3s beginning with partial frames
gpoirier
parents:
1169
diff
changeset
|
396 AVCodecContext avctx; |
1107 | 397 |
2096 | 398 if(id3v2_match(p->buf)) |
399 return AVPROBE_SCORE_MAX/2+1; // this must be less than mpeg-ps because some retards put id3v2 tags before mpeg-ps files | |
1107 | 400 |
1308
866d43ed0a67
allow ffmpeg to read mp3s beginning with partial frames
gpoirier
parents:
1169
diff
changeset
|
401 max_frames = 0; |
866d43ed0a67
allow ffmpeg to read mp3s beginning with partial frames
gpoirier
parents:
1169
diff
changeset
|
402 buf = p->buf; |
866d43ed0a67
allow ffmpeg to read mp3s beginning with partial frames
gpoirier
parents:
1169
diff
changeset
|
403 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
|
404 |
866d43ed0a67
allow ffmpeg to read mp3s beginning with partial frames
gpoirier
parents:
1169
diff
changeset
|
405 for(; buf < end; buf++) { |
866d43ed0a67
allow ffmpeg to read mp3s beginning with partial frames
gpoirier
parents:
1169
diff
changeset
|
406 buf2 = buf; |
1107 | 407 |
1312 | 408 for(frames = 0; buf2 < end; frames++) { |
1308
866d43ed0a67
allow ffmpeg to read mp3s beginning with partial frames
gpoirier
parents:
1169
diff
changeset
|
409 header = (buf2[0] << 24) | (buf2[1] << 16) | (buf2[2] << 8) | buf2[3]; |
2090 | 410 fsize = ff_mpa_decode_header(&avctx, header, &sample_rate); |
1308
866d43ed0a67
allow ffmpeg to read mp3s beginning with partial frames
gpoirier
parents:
1169
diff
changeset
|
411 if(fsize < 0) |
866d43ed0a67
allow ffmpeg to read mp3s beginning with partial frames
gpoirier
parents:
1169
diff
changeset
|
412 break; |
866d43ed0a67
allow ffmpeg to read mp3s beginning with partial frames
gpoirier
parents:
1169
diff
changeset
|
413 buf2 += fsize; |
866d43ed0a67
allow ffmpeg to read mp3s beginning with partial frames
gpoirier
parents:
1169
diff
changeset
|
414 } |
866d43ed0a67
allow ffmpeg to read mp3s beginning with partial frames
gpoirier
parents:
1169
diff
changeset
|
415 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
|
416 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
|
417 first_frames= frames; |
1308
866d43ed0a67
allow ffmpeg to read mp3s beginning with partial frames
gpoirier
parents:
1169
diff
changeset
|
418 } |
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
|
419 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
|
420 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
|
421 else if(max_frames>=1) return 1; |
1308
866d43ed0a67
allow ffmpeg to read mp3s beginning with partial frames
gpoirier
parents:
1169
diff
changeset
|
422 else return 0; |
1107 | 423 } |
424 | |
234 | 425 static int mp3_read_header(AVFormatContext *s, |
426 AVFormatParameters *ap) | |
427 { | |
428 AVStream *st; | |
2096 | 429 uint8_t buf[ID3v1_TAG_SIZE]; |
234 | 430 int len, ret, filesize; |
431 | |
432 st = av_new_stream(s, 0); | |
433 if (!st) | |
434 return AVERROR_NOMEM; | |
435 | |
820
feca73904e67
changing AVCodecContext codec -> *codec in AVStream so additions to AVCodecContext dont randomize AVStream and break binary compatibility
michael
parents:
814
diff
changeset
|
436 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
|
437 st->codec->codec_id = CODEC_ID_MP3; |
2023 | 438 st->need_parsing = AVSTREAM_PARSE_FULL; |
885 | 439 |
234 | 440 /* try to get the TAG */ |
441 if (!url_is_streamed(&s->pb)) { | |
442 /* 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
|
443 filesize = url_fsize(&s->pb); |
234 | 444 if (filesize > 128) { |
445 url_fseek(&s->pb, filesize - 128, SEEK_SET); | |
2096 | 446 ret = get_buffer(&s->pb, buf, ID3v1_TAG_SIZE); |
447 if (ret == ID3v1_TAG_SIZE) { | |
448 id3v1_parse_tag(s, buf); | |
234 | 449 } |
450 url_fseek(&s->pb, 0, SEEK_SET); | |
451 } | |
452 } | |
453 | |
2096 | 454 /* if ID3v2 header found, skip it */ |
455 ret = get_buffer(&s->pb, buf, ID3v2_HEADER_SIZE); | |
456 if (ret != ID3v2_HEADER_SIZE) | |
234 | 457 return -1; |
2096 | 458 if (id3v2_match(buf)) { |
2097 | 459 /* parse ID3v2 header */ |
234 | 460 len = ((buf[6] & 0x7f) << 21) | |
461 ((buf[7] & 0x7f) << 14) | | |
462 ((buf[8] & 0x7f) << 7) | | |
463 (buf[9] & 0x7f); | |
2097 | 464 id3v2_parse(s, len, buf[3], buf[5]); |
234 | 465 } else { |
466 url_fseek(&s->pb, 0, SEEK_SET); | |
467 } | |
468 | |
469 /* the parameters will be extracted from the compressed bitstream */ | |
470 return 0; | |
471 } | |
472 | |
473 #define MP3_PACKET_SIZE 1024 | |
474 | |
475 static int mp3_read_packet(AVFormatContext *s, AVPacket *pkt) | |
476 { | |
477 int ret, size; | |
478 // AVStream *st = s->streams[0]; | |
885 | 479 |
234 | 480 size= MP3_PACKET_SIZE; |
481 | |
775 | 482 ret= av_get_packet(&s->pb, pkt, size); |
234 | 483 |
484 pkt->stream_index = 0; | |
485 if (ret <= 0) { | |
482 | 486 return AVERROR_IO; |
234 | 487 } |
488 /* note: we need to modify the packet size here to handle the last | |
489 packet */ | |
490 pkt->size = ret; | |
491 return ret; | |
492 } | |
493 | |
494 static int mp3_read_close(AVFormatContext *s) | |
495 { | |
496 return 0; | |
497 } | |
498 | |
858
66cc656ea404
Replace CONFIG_ENCODERS/CONFIG_DECODERS with CONFIG_MUXERS/CONFIG_DEMUXERS
diego
parents:
820
diff
changeset
|
499 #ifdef CONFIG_MUXERS |
234 | 500 /* simple formats */ |
2098 | 501 |
502 static void id3v2_put_size(AVFormatContext *s, int size) | |
503 { | |
504 put_byte(&s->pb, size >> 21 & 0x7f); | |
505 put_byte(&s->pb, size >> 14 & 0x7f); | |
506 put_byte(&s->pb, size >> 7 & 0x7f); | |
507 put_byte(&s->pb, size & 0x7f); | |
508 } | |
509 | |
510 static void id3v2_put_ttag(AVFormatContext *s, char *string, uint32_t tag) | |
511 { | |
512 int len = strlen(string); | |
513 put_be32(&s->pb, tag); | |
514 id3v2_put_size(s, len + 1); | |
515 put_be16(&s->pb, 0); | |
516 put_byte(&s->pb, 3); /* UTF-8 */ | |
517 put_buffer(&s->pb, string, len); | |
518 } | |
519 | |
520 | |
521 /** | |
522 * Write an ID3v2.4 header at beginning of stream | |
523 */ | |
524 | |
234 | 525 static int mp3_write_header(struct AVFormatContext *s) |
526 { | |
2098 | 527 int totlen = 0; |
528 char tracktxt[10]; | |
529 | |
530 if(s->track) | |
531 snprintf(tracktxt, sizeof(tracktxt) - 1, "%d", s->track); | |
532 | |
533 if(s->title[0]) totlen += 11 + strlen(s->title); | |
534 if(s->author[0]) totlen += 11 + strlen(s->author); | |
535 if(s->album[0]) totlen += 11 + strlen(s->album); | |
536 if(s->genre[0]) totlen += 11 + strlen(s->genre); | |
537 if(s->copyright[0]) totlen += 11 + strlen(s->copyright); | |
538 if(s->track) totlen += 11 + strlen(tracktxt); | |
539 if(!(s->streams[0]->codec->flags & CODEC_FLAG_BITEXACT)) | |
540 totlen += strlen(LIBAVFORMAT_IDENT) + 11; | |
541 | |
542 if(totlen == 0) | |
543 return 0; | |
544 | |
545 put_be32(&s->pb, MKBETAG('I', 'D', '3', 0x04)); /* ID3v2.4 */ | |
546 put_byte(&s->pb, 0); | |
547 put_byte(&s->pb, 0); /* flags */ | |
548 | |
549 id3v2_put_size(s, totlen); | |
550 | |
551 if(s->title[0]) id3v2_put_ttag(s, s->title, MKBETAG('T', 'I', 'T', '2')); | |
552 if(s->author[0]) id3v2_put_ttag(s, s->author, MKBETAG('T', 'P', 'E', '1')); | |
553 if(s->album[0]) id3v2_put_ttag(s, s->album, MKBETAG('T', 'A', 'L', 'B')); | |
554 if(s->genre[0]) id3v2_put_ttag(s, s->genre, MKBETAG('T', 'C', 'O', 'N')); | |
555 if(s->copyright[0]) id3v2_put_ttag(s, s->copyright, MKBETAG('T', 'C', 'O', 'P')); | |
556 if(s->track) id3v2_put_ttag(s, tracktxt, MKBETAG('T', 'R', 'C', 'K')); | |
557 if(!(s->streams[0]->codec->flags & CODEC_FLAG_BITEXACT)) | |
558 id3v2_put_ttag(s, LIBAVFORMAT_IDENT, MKBETAG('T', 'E', 'N', 'C')); | |
234 | 559 return 0; |
560 } | |
561 | |
468 | 562 static int mp3_write_packet(struct AVFormatContext *s, AVPacket *pkt) |
234 | 563 { |
468 | 564 put_buffer(&s->pb, pkt->data, pkt->size); |
234 | 565 put_flush_packet(&s->pb); |
566 return 0; | |
567 } | |
568 | |
569 static int mp3_write_trailer(struct AVFormatContext *s) | |
570 { | |
2096 | 571 uint8_t buf[ID3v1_TAG_SIZE]; |
234 | 572 |
2096 | 573 /* write the id3v1 tag */ |
234 | 574 if (s->title[0] != '\0') { |
2096 | 575 id3v1_create_tag(s, buf); |
576 put_buffer(&s->pb, buf, ID3v1_TAG_SIZE); | |
234 | 577 put_flush_packet(&s->pb); |
578 } | |
579 return 0; | |
580 } | |
858
66cc656ea404
Replace CONFIG_ENCODERS/CONFIG_DECODERS with CONFIG_MUXERS/CONFIG_DEMUXERS
diego
parents:
820
diff
changeset
|
581 #endif //CONFIG_MUXERS |
234 | 582 |
1169 | 583 #ifdef CONFIG_MP3_DEMUXER |
1167 | 584 AVInputFormat mp3_demuxer = { |
234 | 585 "mp3", |
586 "MPEG audio", | |
587 0, | |
1107 | 588 mp3_read_probe, |
234 | 589 mp3_read_header, |
590 mp3_read_packet, | |
591 mp3_read_close, | |
1756 | 592 .flags= AVFMT_GENERIC_INDEX, |
814 | 593 .extensions = "mp2,mp3,m2a", /* XXX: use probe */ |
234 | 594 }; |
1169 | 595 #endif |
596 #ifdef CONFIG_MP2_MUXER | |
1167 | 597 AVOutputFormat mp2_muxer = { |
234 | 598 "mp2", |
599 "MPEG audio layer 2", | |
600 "audio/x-mpeg", | |
1622 | 601 #ifdef CONFIG_LIBMP3LAME |
814 | 602 "mp2,m2a", |
234 | 603 #else |
814 | 604 "mp2,mp3,m2a", |
234 | 605 #endif |
606 0, | |
607 CODEC_ID_MP2, | |
608 0, | |
609 mp3_write_header, | |
610 mp3_write_packet, | |
611 mp3_write_trailer, | |
612 }; | |
1169 | 613 #endif |
614 #ifdef CONFIG_MP3_MUXER | |
1167 | 615 AVOutputFormat mp3_muxer = { |
234 | 616 "mp3", |
617 "MPEG audio layer 3", | |
618 "audio/x-mpeg", | |
619 "mp3", | |
620 0, | |
621 CODEC_ID_MP3, | |
622 0, | |
623 mp3_write_header, | |
624 mp3_write_packet, | |
625 mp3_write_trailer, | |
626 }; | |
627 #endif |