2 * MP3 muxer and demuxer
3 * Copyright (c) 2003 Fabrice Bellard.
5 * This file is part of FFmpeg.
7 * FFmpeg is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Lesser General Public
9 * License as published by the Free Software Foundation; either
10 * version 2.1 of the License, or (at your option) any later version.
12 * FFmpeg is distributed in the hope that it will be useful,
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.
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with FFmpeg; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
22 #include "mpegaudio.h"
24 #define ID3v2_HEADER_SIZE 10
25 #define ID3v1_TAG_SIZE 128
27 #define ID3v1_GENRE_MAX 125
29 static const char *id3v1_genre_str
[ID3v1_GENRE_MAX
+ 1] = {
63 [33] = "Instrumental",
76 [46] = "Instrumental Pop",
77 [47] = "Instrumental Rock",
81 [51] = "Techno-Industrial",
86 [56] = "Southern Rock",
91 [61] = "Christian Rap",
94 [64] = "Native American",
108 [78] = "Rock & Roll",
112 [82] = "National Folk",
114 [84] = "Fast Fusion",
121 [91] = "Gothic Rock",
122 [92] = "Progressive Rock",
123 [93] = "Psychedelic Rock",
124 [94] = "Symphonic Rock",
128 [98] = "Easy Listening",
134 [104] = "Chamber Music",
137 [107] = "Booty Bass",
139 [109] = "Porn Groove",
147 [117] = "Power Ballad",
148 [118] = "Rhythmic Soul",
154 [124] = "Euro-House",
155 [125] = "Dance Hall",
158 /* buf must be ID3v2_HEADER_SIZE byte long */
159 static int id3v2_match(const uint8_t *buf
)
161 return (buf
[0] == 'I' &&
166 (buf
[6] & 0x80) == 0 &&
167 (buf
[7] & 0x80) == 0 &&
168 (buf
[8] & 0x80) == 0 &&
169 (buf
[9] & 0x80) == 0);
172 static unsigned int id3v2_get_size(ByteIOContext
*s
, int len
)
176 v
= (v
<<7) + (get_byte(s
)&0x7F);
180 static void id3v2_read_ttag(AVFormatContext
*s
, int taglen
, char *dst
, int dstlen
)
188 taglen
--; /* account for encoding type byte */
189 dstlen
--; /* Leave space for zero terminator */
191 switch(get_byte(&s
->pb
)) { /* encoding type */
193 case 0: /* ISO-8859-1 (0 - 255 maps directly into unicode) */
197 PUT_UTF8(get_byte(&s
->pb
), tmp
, if (q
- dst
< dstlen
- 1) *q
++ = tmp
;)
203 len
= FFMIN(taglen
, dstlen
);
204 get_buffer(&s
->pb
, dst
, len
);
213 * Handles ID3v2.2, 2.3 and 2.4.
217 static void id3v2_parse(AVFormatContext
*s
, int len
, uint8_t version
, uint8_t flags
)
229 reason
= "compression";
247 reason
= "unsynchronization";
251 if(isv34
&& flags
& 0x40) /* Extended header present, just skip over it */
252 url_fskip(&s
->pb
, id3v2_get_size(&s
->pb
, 4));
254 while(len
>= taghdrlen
) {
256 tag
= get_be32(&s
->pb
);
257 tlen
= id3v2_get_size(&s
->pb
, 4);
258 get_be16(&s
->pb
); /* flags */
260 tag
= get_be24(&s
->pb
);
261 tlen
= id3v2_get_size(&s
->pb
, 3);
263 len
-= taghdrlen
+ tlen
;
268 next
= url_ftell(&s
->pb
) + tlen
;
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
));
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
));
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
));
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
));
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
));
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
);
297 /* padding, skip to end */
298 url_fskip(&s
->pb
, len
);
302 /* Skip to end of tag */
303 url_fseek(&s
->pb
, next
, SEEK_SET
);
306 if(version
== 4 && flags
& 0x10) /* Footer preset, always 10 bytes, skip over it */
307 url_fskip(&s
->pb
, 10);
311 av_log(s
, AV_LOG_INFO
, "ID3v2.%d tag skipped, cannot handle %s\n", version
, reason
);
312 url_fskip(&s
->pb
, len
);
315 static void id3v1_get_string(char *str
, int str_size
,
316 const uint8_t *buf
, int buf_size
)
322 for(i
= 0; i
< buf_size
; i
++) {
326 if ((q
- str
) >= str_size
- 1)
333 /* 'buf' must be ID3v1_TAG_SIZE byte long */
334 static int id3v1_parse_tag(AVFormatContext
*s
, const uint8_t *buf
)
339 if (!(buf
[0] == 'T' &&
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);
348 id3v1_get_string(s
->comment
, sizeof(s
->comment
), buf
+ 97, 30);
349 if (buf
[125] == 0 && buf
[126] != 0)
352 if (genre
<= ID3v1_GENRE_MAX
)
353 pstrcpy(s
->genre
, sizeof(s
->genre
), id3v1_genre_str
[genre
]);
357 static void id3v1_create_tag(AVFormatContext
*s
, uint8_t *buf
)
361 memset(buf
, 0, ID3v1_TAG_SIZE
); /* fail safe */
365 strncpy(buf
+ 3, s
->title
, 30);
366 strncpy(buf
+ 33, s
->author
, 30);
367 strncpy(buf
+ 63, s
->album
, 30);
370 for(i
= 0;i
< 4; i
++) {
371 buf
[96 - i
] = '0' + (v
% 10);
375 strncpy(buf
+ 97, s
->comment
, 30);
380 for(i
= 0; i
<= ID3v1_GENRE_MAX
; i
++) {
381 if (!strcasecmp(s
->genre
, id3v1_genre_str
[i
])) {
390 static int mp3_read_probe(AVProbeData
*p
)
392 int max_frames
, first_frames
;
393 int fsize
, frames
, sample_rate
;
395 uint8_t *buf
, *buf2
, *end
;
396 AVCodecContext avctx
;
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
403 end
= buf
+ FFMIN(4096, p
->buf_size
- sizeof(uint32_t));
405 for(; buf
< end
; buf
++) {
408 for(frames
= 0; buf2
< end
; frames
++) {
409 header
= (buf2
[0] << 24) | (buf2
[1] << 16) | (buf2
[2] << 8) | buf2
[3];
410 fsize
= ff_mpa_decode_header(&avctx
, header
, &sample_rate
);
415 max_frames
= FFMAX(max_frames
, frames
);
417 first_frames
= frames
;
419 if (first_frames
>=3) return AVPROBE_SCORE_MAX
/2+1;
420 else if(max_frames
>=3) return AVPROBE_SCORE_MAX
/4;
421 else if(max_frames
>=1) return 1;
425 static int mp3_read_header(AVFormatContext
*s
,
426 AVFormatParameters
*ap
)
429 uint8_t buf
[ID3v1_TAG_SIZE
];
430 int len
, ret
, filesize
;
432 st
= av_new_stream(s
, 0);
434 return AVERROR_NOMEM
;
436 st
->codec
->codec_type
= CODEC_TYPE_AUDIO
;
437 st
->codec
->codec_id
= CODEC_ID_MP3
;
438 st
->need_parsing
= AVSTREAM_PARSE_FULL
;
440 /* try to get the TAG */
441 if (!url_is_streamed(&s
->pb
)) {
442 /* XXX: change that */
443 filesize
= url_fsize(&s
->pb
);
444 if (filesize
> 128) {
445 url_fseek(&s
->pb
, filesize
- 128, SEEK_SET
);
446 ret
= get_buffer(&s
->pb
, buf
, ID3v1_TAG_SIZE
);
447 if (ret
== ID3v1_TAG_SIZE
) {
448 id3v1_parse_tag(s
, buf
);
450 url_fseek(&s
->pb
, 0, SEEK_SET
);
454 /* if ID3v2 header found, skip it */
455 ret
= get_buffer(&s
->pb
, buf
, ID3v2_HEADER_SIZE
);
456 if (ret
!= ID3v2_HEADER_SIZE
)
458 if (id3v2_match(buf
)) {
459 /* parse ID3v2 header */
460 len
= ((buf
[6] & 0x7f) << 21) |
461 ((buf
[7] & 0x7f) << 14) |
462 ((buf
[8] & 0x7f) << 7) |
464 id3v2_parse(s
, len
, buf
[3], buf
[5]);
466 url_fseek(&s
->pb
, 0, SEEK_SET
);
469 /* the parameters will be extracted from the compressed bitstream */
473 #define MP3_PACKET_SIZE 1024
475 static int mp3_read_packet(AVFormatContext
*s
, AVPacket
*pkt
)
478 // AVStream *st = s->streams[0];
480 size
= MP3_PACKET_SIZE
;
482 ret
= av_get_packet(&s
->pb
, pkt
, size
);
484 pkt
->stream_index
= 0;
488 /* note: we need to modify the packet size here to handle the last
494 static int mp3_read_close(AVFormatContext
*s
)
502 static void id3v2_put_size(AVFormatContext
*s
, int size
)
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);
510 static void id3v2_put_ttag(AVFormatContext
*s
, char *string
, uint32_t tag
)
512 int len
= strlen(string
);
513 put_be32(&s
->pb
, tag
);
514 id3v2_put_size(s
, len
+ 1);
516 put_byte(&s
->pb
, 3); /* UTF-8 */
517 put_buffer(&s
->pb
, string
, len
);
522 * Write an ID3v2.4 header at beginning of stream
525 static int mp3_write_header(struct AVFormatContext
*s
)
531 snprintf(tracktxt
, sizeof(tracktxt
) - 1, "%d", s
->track
);
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;
545 put_be32(&s
->pb
, MKBETAG('I', 'D', '3', 0x04)); /* ID3v2.4 */
547 put_byte(&s
->pb
, 0); /* flags */
549 id3v2_put_size(s
, totlen
);
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'));
562 static int mp3_write_packet(struct AVFormatContext
*s
, AVPacket
*pkt
)
564 put_buffer(&s
->pb
, pkt
->data
, pkt
->size
);
565 put_flush_packet(&s
->pb
);
569 static int mp3_write_trailer(struct AVFormatContext
*s
)
571 uint8_t buf
[ID3v1_TAG_SIZE
];
573 /* write the id3v1 tag */
574 if (s
->title
[0] != '\0') {
575 id3v1_create_tag(s
, buf
);
576 put_buffer(&s
->pb
, buf
, ID3v1_TAG_SIZE
);
577 put_flush_packet(&s
->pb
);
581 #endif //CONFIG_MUXERS
583 #ifdef CONFIG_MP3_DEMUXER
584 AVInputFormat mp3_demuxer
= {
592 .flags
= AVFMT_GENERIC_INDEX
,
593 .extensions
= "mp2,mp3,m2a", /* XXX: use probe */
596 #ifdef CONFIG_MP2_MUXER
597 AVOutputFormat mp2_muxer
= {
599 "MPEG audio layer 2",
601 #ifdef CONFIG_LIBMP3LAME
614 #ifdef CONFIG_MP3_MUXER
615 AVOutputFormat mp3_muxer
= {
617 "MPEG audio layer 3",