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