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 ID3_HEADER_SIZE 10
25 #define ID3_TAG_SIZE 128
27 #define ID3_GENRE_MAX 125
29 static const char *id3_genre_str[ID3_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 ID3_HEADER_SIZE byte long */
159 static int id3_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 void id3_get_string(char *str, int str_size,
173 const uint8_t *buf, int buf_size)
179 for(i = 0; i < buf_size; i++) {
183 if ((q - str) >= str_size - 1)
190 /* 'buf' must be ID3_TAG_SIZE byte long */
191 static int id3_parse_tag(AVFormatContext *s, const uint8_t *buf)
196 if (!(buf[0] == 'T' &&
200 id3_get_string(s->title, sizeof(s->title), buf + 3, 30);
201 id3_get_string(s->author, sizeof(s->author), buf + 33, 30);
202 id3_get_string(s->album, sizeof(s->album), buf + 63, 30);
203 id3_get_string(str, sizeof(str), buf + 93, 4);
205 id3_get_string(s->comment, sizeof(s->comment), buf + 97, 30);
206 if (buf[125] == 0 && buf[126] != 0)
209 if (genre <= ID3_GENRE_MAX)
210 pstrcpy(s->genre, sizeof(s->genre), id3_genre_str[genre]);
214 static void id3_create_tag(AVFormatContext *s, uint8_t *buf)
218 memset(buf, 0, ID3_TAG_SIZE); /* fail safe */
222 strncpy(buf + 3, s->title, 30);
223 strncpy(buf + 33, s->author, 30);
224 strncpy(buf + 63, s->album, 30);
227 for(i = 0;i < 4; i++) {
228 buf[96 - i] = '0' + (v % 10);
232 strncpy(buf + 97, s->comment, 30);
237 for(i = 0; i <= ID3_GENRE_MAX; i++) {
238 if (!strcasecmp(s->genre, id3_genre_str[i])) {
247 static int mp3_read_probe(AVProbeData *p)
249 int max_frames, first_frames;
250 int fsize, frames, sample_rate;
252 uint8_t *buf, *buf2, *end;
253 AVCodecContext avctx;
255 if(p->buf_size < ID3_HEADER_SIZE)
258 if(id3_match(p->buf))
259 return AVPROBE_SCORE_MAX/2+1; // this must be less then mpeg-ps because some retards put id3 tage before mpeg-ps files
263 end = buf + FFMIN(4096, p->buf_size - sizeof(uint32_t));
265 for(; buf < end; buf++) {
268 for(frames = 0; buf2 < end; frames++) {
269 header = (buf2[0] << 24) | (buf2[1] << 16) | (buf2[2] << 8) | buf2[3];
270 fsize = mpa_decode_header(&avctx, header, &sample_rate);
275 max_frames = FFMAX(max_frames, frames);
277 first_frames= frames;
279 if (first_frames>=3) return AVPROBE_SCORE_MAX/2+1;
280 else if(max_frames>=3) return AVPROBE_SCORE_MAX/4;
281 else if(max_frames>=1) return 1;
285 static int mp3_read_header(AVFormatContext *s,
286 AVFormatParameters *ap)
289 uint8_t buf[ID3_TAG_SIZE];
290 int len, ret, filesize;
292 st = av_new_stream(s, 0);
294 return AVERROR_NOMEM;
296 st->codec->codec_type = CODEC_TYPE_AUDIO;
297 st->codec->codec_id = CODEC_ID_MP3;
298 st->need_parsing = 1;
300 /* try to get the TAG */
301 if (!url_is_streamed(&s->pb)) {
302 /* XXX: change that */
303 filesize = url_fsize(&s->pb);
304 if (filesize > 128) {
305 url_fseek(&s->pb, filesize - 128, SEEK_SET);
306 ret = get_buffer(&s->pb, buf, ID3_TAG_SIZE);
307 if (ret == ID3_TAG_SIZE) {
308 id3_parse_tag(s, buf);
310 url_fseek(&s->pb, 0, SEEK_SET);
314 /* if ID3 header found, skip it */
315 ret = get_buffer(&s->pb, buf, ID3_HEADER_SIZE);
316 if (ret != ID3_HEADER_SIZE)
318 if (id3_match(buf)) {
319 /* skip ID3 header */
320 len = ((buf[6] & 0x7f) << 21) |
321 ((buf[7] & 0x7f) << 14) |
322 ((buf[8] & 0x7f) << 7) |
324 url_fskip(&s->pb, len);
326 url_fseek(&s->pb, 0, SEEK_SET);
329 /* the parameters will be extracted from the compressed bitstream */
333 #define MP3_PACKET_SIZE 1024
335 static int mp3_read_packet(AVFormatContext *s, AVPacket *pkt)
338 // AVStream *st = s->streams[0];
340 size= MP3_PACKET_SIZE;
342 ret= av_get_packet(&s->pb, pkt, size);
344 pkt->stream_index = 0;
348 /* note: we need to modify the packet size here to handle the last
354 static int mp3_read_close(AVFormatContext *s)
361 static int mp3_write_header(struct AVFormatContext *s)
366 static int mp3_write_packet(struct AVFormatContext *s, AVPacket *pkt)
368 put_buffer(&s->pb, pkt->data, pkt->size);
369 put_flush_packet(&s->pb);
373 static int mp3_write_trailer(struct AVFormatContext *s)
375 uint8_t buf[ID3_TAG_SIZE];
377 /* write the id3 header */
378 if (s->title[0] != '\0') {
379 id3_create_tag(s, buf);
380 put_buffer(&s->pb, buf, ID3_TAG_SIZE);
381 put_flush_packet(&s->pb);
385 #endif //CONFIG_MUXERS
387 #ifdef CONFIG_MP3_DEMUXER
388 AVInputFormat mp3_demuxer = {
396 .extensions = "mp2,mp3,m2a", /* XXX: use probe */
399 #ifdef CONFIG_MP2_MUXER
400 AVOutputFormat mp2_muxer = {
402 "MPEG audio layer 2",
404 #ifdef CONFIG_MP3LAME
417 #ifdef CONFIG_MP3_MUXER
418 AVOutputFormat mp3_muxer = {
420 "MPEG audio layer 3",