2 * MP3 encoder and decoder
3 * Copyright (c) 2003 Fabrice Bellard.
5 * This library is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU Lesser General Public
7 * License as published by the Free Software Foundation; either
8 * version 2 of the License, or (at your option) any later version.
10 * This library is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * Lesser General Public License for more details.
15 * You should have received a copy of the GNU Lesser General Public
16 * License along with this library; if not, write to the Free Software
17 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
20 #include "mpegaudio.h"
22 #define ID3_HEADER_SIZE 10
23 #define ID3_TAG_SIZE 128
25 #define ID3_GENRE_MAX 125
27 static const char *id3_genre_str[ID3_GENRE_MAX + 1] = {
61 [33] = "Instrumental",
74 [46] = "Instrumental Pop",
75 [47] = "Instrumental Rock",
79 [51] = "Techno-Industrial",
84 [56] = "Southern Rock",
89 [61] = "Christian Rap",
92 [64] = "Native American",
106 [78] = "Rock & Roll",
110 [82] = "National Folk",
112 [84] = "Fast Fusion",
119 [91] = "Gothic Rock",
120 [92] = "Progressive Rock",
121 [93] = "Psychedelic Rock",
122 [94] = "Symphonic Rock",
126 [98] = "Easy Listening",
132 [104] = "Chamber Music",
135 [107] = "Booty Bass",
137 [109] = "Porn Groove",
145 [117] = "Power Ballad",
146 [118] = "Rhythmic Soul",
152 [124] = "Euro-House",
153 [125] = "Dance Hall",
156 /* buf must be ID3_HEADER_SIZE byte long */
157 static int id3_match(const uint8_t *buf)
159 return (buf[0] == 'I' &&
164 (buf[6] & 0x80) == 0 &&
165 (buf[7] & 0x80) == 0 &&
166 (buf[8] & 0x80) == 0 &&
167 (buf[9] & 0x80) == 0);
170 static void id3_get_string(char *str, int str_size,
171 const uint8_t *buf, int buf_size)
177 for(i = 0; i < buf_size; i++) {
181 if ((q - str) >= str_size - 1)
188 /* 'buf' must be ID3_TAG_SIZE byte long */
189 static int id3_parse_tag(AVFormatContext *s, const uint8_t *buf)
194 if (!(buf[0] == 'T' &&
198 id3_get_string(s->title, sizeof(s->title), buf + 3, 30);
199 id3_get_string(s->author, sizeof(s->author), buf + 33, 30);
200 id3_get_string(s->album, sizeof(s->album), buf + 63, 30);
201 id3_get_string(str, sizeof(str), buf + 93, 4);
203 id3_get_string(s->comment, sizeof(s->comment), buf + 97, 30);
204 if (buf[125] == 0 && buf[126] != 0)
207 if (genre <= ID3_GENRE_MAX)
208 pstrcpy(s->genre, sizeof(s->genre), id3_genre_str[genre]);
212 static void id3_create_tag(AVFormatContext *s, uint8_t *buf)
216 memset(buf, 0, ID3_TAG_SIZE); /* fail safe */
220 strncpy(buf + 3, s->title, 30);
221 strncpy(buf + 33, s->author, 30);
222 strncpy(buf + 63, s->album, 30);
225 for(i = 0;i < 4; i++) {
226 buf[96 - i] = '0' + (v % 10);
230 strncpy(buf + 97, s->comment, 30);
235 for(i = 0; i <= ID3_GENRE_MAX; i++) {
236 if (!strcasecmp(s->genre, id3_genre_str[i])) {
245 static int mp3_read_probe(AVProbeData *p)
247 int max_frames, first_frames;
250 uint8_t *buf, *buf2, *end;
251 AVCodecContext avctx;
253 if(p->buf_size < ID3_HEADER_SIZE)
256 if(id3_match(p->buf))
257 return AVPROBE_SCORE_MAX;
261 end = buf + FFMIN(4096, p->buf_size - sizeof(uint32_t));
263 for(; buf < end; buf++) {
266 for(frames = 0; buf2 < end; frames++) {
267 header = (buf2[0] << 24) | (buf2[1] << 16) | (buf2[2] << 8) | buf2[3];
268 fsize = mpa_decode_header(&avctx, header);
273 max_frames = FFMAX(max_frames, frames);
275 first_frames= frames;
277 if (first_frames>=3) return AVPROBE_SCORE_MAX/2+1;
278 else if(max_frames>=3) return AVPROBE_SCORE_MAX/4;
279 else if(max_frames>=1) return 1;
283 static int mp3_read_header(AVFormatContext *s,
284 AVFormatParameters *ap)
287 uint8_t buf[ID3_TAG_SIZE];
288 int len, ret, filesize;
290 st = av_new_stream(s, 0);
292 return AVERROR_NOMEM;
294 st->codec->codec_type = CODEC_TYPE_AUDIO;
295 st->codec->codec_id = CODEC_ID_MP3;
296 st->need_parsing = 1;
298 /* try to get the TAG */
299 if (!url_is_streamed(&s->pb)) {
300 /* XXX: change that */
301 filesize = url_fsize(&s->pb);
302 if (filesize > 128) {
303 url_fseek(&s->pb, filesize - 128, SEEK_SET);
304 ret = get_buffer(&s->pb, buf, ID3_TAG_SIZE);
305 if (ret == ID3_TAG_SIZE) {
306 id3_parse_tag(s, buf);
308 url_fseek(&s->pb, 0, SEEK_SET);
312 /* if ID3 header found, skip it */
313 ret = get_buffer(&s->pb, buf, ID3_HEADER_SIZE);
314 if (ret != ID3_HEADER_SIZE)
316 if (id3_match(buf)) {
317 /* skip ID3 header */
318 len = ((buf[6] & 0x7f) << 21) |
319 ((buf[7] & 0x7f) << 14) |
320 ((buf[8] & 0x7f) << 7) |
322 url_fskip(&s->pb, len);
324 url_fseek(&s->pb, 0, SEEK_SET);
327 /* the parameters will be extracted from the compressed bitstream */
331 #define MP3_PACKET_SIZE 1024
333 static int mp3_read_packet(AVFormatContext *s, AVPacket *pkt)
336 // AVStream *st = s->streams[0];
338 size= MP3_PACKET_SIZE;
340 ret= av_get_packet(&s->pb, pkt, size);
342 pkt->stream_index = 0;
346 /* note: we need to modify the packet size here to handle the last
352 static int mp3_read_close(AVFormatContext *s)
359 static int mp3_write_header(struct AVFormatContext *s)
364 static int mp3_write_packet(struct AVFormatContext *s, AVPacket *pkt)
366 put_buffer(&s->pb, pkt->data, pkt->size);
367 put_flush_packet(&s->pb);
371 static int mp3_write_trailer(struct AVFormatContext *s)
373 uint8_t buf[ID3_TAG_SIZE];
375 /* write the id3 header */
376 if (s->title[0] != '\0') {
377 id3_create_tag(s, buf);
378 put_buffer(&s->pb, buf, ID3_TAG_SIZE);
379 put_flush_packet(&s->pb);
383 #endif //CONFIG_MUXERS
385 #ifdef CONFIG_MP3_DEMUXER
386 AVInputFormat mp3_demuxer = {
394 .extensions = "mp2,mp3,m2a", /* XXX: use probe */
397 #ifdef CONFIG_MP2_MUXER
398 AVOutputFormat mp2_muxer = {
400 "MPEG audio layer 2",
402 #ifdef CONFIG_MP3LAME
415 #ifdef CONFIG_MP3_MUXER
416 AVOutputFormat mp3_muxer = {
418 "MPEG audio layer 3",