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"
25 #define ID3v2_HEADER_SIZE 10
26 #define ID3v1_TAG_SIZE 128
28 #define ID3v1_GENRE_MAX 125
30 static const char *id3v1_genre_str[ID3v1_GENRE_MAX + 1] = {
64 [33] = "Instrumental",
77 [46] = "Instrumental Pop",
78 [47] = "Instrumental Rock",
82 [51] = "Techno-Industrial",
87 [56] = "Southern Rock",
92 [61] = "Christian Rap",
95 [64] = "Native American",
109 [78] = "Rock & Roll",
113 [82] = "National Folk",
115 [84] = "Fast Fusion",
122 [91] = "Gothic Rock",
123 [92] = "Progressive Rock",
124 [93] = "Psychedelic Rock",
125 [94] = "Symphonic Rock",
129 [98] = "Easy Listening",
135 [104] = "Chamber Music",
138 [107] = "Booty Bass",
140 [109] = "Porn Groove",
148 [117] = "Power Ballad",
149 [118] = "Rhythmic Soul",
155 [124] = "Euro-House",
156 [125] = "Dance Hall",
159 /* buf must be ID3v2_HEADER_SIZE byte long */
160 static int id3v2_match(const uint8_t *buf)
162 return (buf[0] == 'I' &&
167 (buf[6] & 0x80) == 0 &&
168 (buf[7] & 0x80) == 0 &&
169 (buf[8] & 0x80) == 0 &&
170 (buf[9] & 0x80) == 0);
173 static unsigned int id3v2_get_size(ByteIOContext *s, int len)
177 v= (v<<7) + (get_byte(s)&0x7F);
181 static void id3v2_read_ttag(AVFormatContext *s, int taglen, char *dst, int dstlen)
189 taglen--; /* account for encoding type byte */
190 dstlen--; /* Leave space for zero terminator */
192 switch(get_byte(&s->pb)) { /* encoding type */
194 case 0: /* ISO-8859-1 (0 - 255 maps directly into unicode) */
198 PUT_UTF8(get_byte(&s->pb), tmp, if (q - dst < dstlen - 1) *q++ = tmp;)
204 len = FFMIN(taglen, dstlen);
205 get_buffer(&s->pb, dst, len);
214 * Handles ID3v2.2, 2.3 and 2.4.
218 static void id3v2_parse(AVFormatContext *s, int len, uint8_t version, uint8_t flags)
230 reason = "compression";
249 reason = "unsynchronization";
253 if(isv34 && flags & 0x40) /* Extended header present, just skip over it */
254 url_fskip(&s->pb, id3v2_get_size(&s->pb, 4));
256 while(len >= taghdrlen) {
258 tag = get_be32(&s->pb);
259 tlen = id3v2_get_size(&s->pb, 4);
260 get_be16(&s->pb); /* flags */
262 tag = get_be24(&s->pb);
263 tlen = id3v2_get_size(&s->pb, 3);
265 len -= taghdrlen + tlen;
270 next = url_ftell(&s->pb) + tlen;
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));
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));
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));
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));
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));
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);
299 /* padding, skip to end */
300 url_fskip(&s->pb, len);
304 /* Skip to end of tag */
305 url_fseek(&s->pb, next, SEEK_SET);
308 if(version == 4 && flags & 0x10) /* Footer preset, always 10 bytes, skip over it */
309 url_fskip(&s->pb, 10);
313 av_log(s, AV_LOG_INFO, "ID3v2.%d tag skipped, cannot handle %s\n", version, reason);
314 url_fskip(&s->pb, len);
317 static void id3v1_get_string(char *str, int str_size,
318 const uint8_t *buf, int buf_size)
324 for(i = 0; i < buf_size; i++) {
328 if ((q - str) >= str_size - 1)
335 /* 'buf' must be ID3v1_TAG_SIZE byte long */
336 static int id3v1_parse_tag(AVFormatContext *s, const uint8_t *buf)
341 if (!(buf[0] == 'T' &&
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);
350 id3v1_get_string(s->comment, sizeof(s->comment), buf + 97, 30);
351 if (buf[125] == 0 && buf[126] != 0)
354 if (genre <= ID3v1_GENRE_MAX)
355 av_strlcpy(s->genre, id3v1_genre_str[genre], sizeof(s->genre));
359 static void id3v1_create_tag(AVFormatContext *s, uint8_t *buf)
363 memset(buf, 0, ID3v1_TAG_SIZE); /* fail safe */
367 strncpy(buf + 3, s->title, 30);
368 strncpy(buf + 33, s->author, 30);
369 strncpy(buf + 63, s->album, 30);
372 for(i = 0;i < 4; i++) {
373 buf[96 - i] = '0' + (v % 10);
377 strncpy(buf + 97, s->comment, 30);
382 for(i = 0; i <= ID3v1_GENRE_MAX; i++) {
383 if (!strcasecmp(s->genre, id3v1_genre_str[i])) {
392 static int mp3_read_probe(AVProbeData *p)
394 int max_frames, first_frames = 0;
395 int fsize, frames, sample_rate;
397 uint8_t *buf, *buf2, *end;
398 AVCodecContext avctx;
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
405 end = buf + FFMIN(4096, p->buf_size - sizeof(uint32_t));
407 for(; buf < end; buf++) {
410 for(frames = 0; buf2 < end; frames++) {
411 header = AV_RB32(buf2);
412 fsize = ff_mpa_decode_header(&avctx, header, &sample_rate);
417 max_frames = FFMAX(max_frames, frames);
419 first_frames= frames;
421 if (first_frames>=3) return AVPROBE_SCORE_MAX/2+1;
422 else if(max_frames>=3) return AVPROBE_SCORE_MAX/4;
423 else if(max_frames>=1) return 1;
427 static int mp3_read_header(AVFormatContext *s,
428 AVFormatParameters *ap)
431 uint8_t buf[ID3v1_TAG_SIZE];
432 int len, ret, filesize;
434 st = av_new_stream(s, 0);
436 return AVERROR(ENOMEM);
438 st->codec->codec_type = CODEC_TYPE_AUDIO;
439 st->codec->codec_id = CODEC_ID_MP3;
440 st->need_parsing = AVSTREAM_PARSE_FULL;
442 /* try to get the TAG */
443 if (!url_is_streamed(&s->pb)) {
444 /* XXX: change that */
445 filesize = url_fsize(&s->pb);
446 if (filesize > 128) {
447 url_fseek(&s->pb, filesize - 128, SEEK_SET);
448 ret = get_buffer(&s->pb, buf, ID3v1_TAG_SIZE);
449 if (ret == ID3v1_TAG_SIZE) {
450 id3v1_parse_tag(s, buf);
452 url_fseek(&s->pb, 0, SEEK_SET);
456 /* if ID3v2 header found, skip it */
457 ret = get_buffer(&s->pb, buf, ID3v2_HEADER_SIZE);
458 if (ret != ID3v2_HEADER_SIZE)
460 if (id3v2_match(buf)) {
461 /* parse ID3v2 header */
462 len = ((buf[6] & 0x7f) << 21) |
463 ((buf[7] & 0x7f) << 14) |
464 ((buf[8] & 0x7f) << 7) |
466 id3v2_parse(s, len, buf[3], buf[5]);
468 url_fseek(&s->pb, 0, SEEK_SET);
471 /* the parameters will be extracted from the compressed bitstream */
475 #define MP3_PACKET_SIZE 1024
477 static int mp3_read_packet(AVFormatContext *s, AVPacket *pkt)
480 // AVStream *st = s->streams[0];
482 size= MP3_PACKET_SIZE;
484 ret= av_get_packet(&s->pb, pkt, size);
486 pkt->stream_index = 0;
490 /* note: we need to modify the packet size here to handle the last
496 static int mp3_read_close(AVFormatContext *s)
504 static void id3v2_put_size(AVFormatContext *s, int size)
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);
512 static void id3v2_put_ttag(AVFormatContext *s, char *string, uint32_t tag)
514 int len = strlen(string);
515 put_be32(&s->pb, tag);
516 id3v2_put_size(s, len + 1);
518 put_byte(&s->pb, 3); /* UTF-8 */
519 put_buffer(&s->pb, string, len);
524 * Write an ID3v2.4 header at beginning of stream
527 static int mp3_write_header(struct AVFormatContext *s)
533 snprintf(tracktxt, sizeof(tracktxt) - 1, "%d", s->track);
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;
547 put_be32(&s->pb, MKBETAG('I', 'D', '3', 0x04)); /* ID3v2.4 */
549 put_byte(&s->pb, 0); /* flags */
551 id3v2_put_size(s, totlen);
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'));
564 static int mp3_write_packet(struct AVFormatContext *s, AVPacket *pkt)
566 put_buffer(&s->pb, pkt->data, pkt->size);
567 put_flush_packet(&s->pb);
571 static int mp3_write_trailer(struct AVFormatContext *s)
573 uint8_t buf[ID3v1_TAG_SIZE];
575 /* write the id3v1 tag */
576 if (s->title[0] != '\0') {
577 id3v1_create_tag(s, buf);
578 put_buffer(&s->pb, buf, ID3v1_TAG_SIZE);
579 put_flush_packet(&s->pb);
583 #endif //CONFIG_MUXERS
585 #ifdef CONFIG_MP3_DEMUXER
586 AVInputFormat mp3_demuxer = {
594 .flags= AVFMT_GENERIC_INDEX,
595 .extensions = "mp2,mp3,m2a", /* XXX: use probe */
598 #ifdef CONFIG_MP2_MUXER
599 AVOutputFormat mp2_muxer = {
601 "MPEG audio layer 2",
603 #ifdef CONFIG_LIBMP3LAME
616 #ifdef CONFIG_MP3_MUXER
617 AVOutputFormat mp3_muxer = {
619 "MPEG audio layer 3",