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)
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 if (max_frames>=3) return AVPROBE_SCORE_MAX/2+1;
276 else if(max_frames==2) return AVPROBE_SCORE_MAX/4;
277 else if(max_frames==1) return 1;
281 static int mp3_read_header(AVFormatContext *s,
282 AVFormatParameters *ap)
285 uint8_t buf[ID3_TAG_SIZE];
286 int len, ret, filesize;
288 st = av_new_stream(s, 0);
290 return AVERROR_NOMEM;
292 st->codec->codec_type = CODEC_TYPE_AUDIO;
293 st->codec->codec_id = CODEC_ID_MP3;
294 st->need_parsing = 1;
296 /* try to get the TAG */
297 if (!url_is_streamed(&s->pb)) {
298 /* XXX: change that */
299 filesize = url_fsize(&s->pb);
300 if (filesize > 128) {
301 url_fseek(&s->pb, filesize - 128, SEEK_SET);
302 ret = get_buffer(&s->pb, buf, ID3_TAG_SIZE);
303 if (ret == ID3_TAG_SIZE) {
304 id3_parse_tag(s, buf);
306 url_fseek(&s->pb, 0, SEEK_SET);
310 /* if ID3 header found, skip it */
311 ret = get_buffer(&s->pb, buf, ID3_HEADER_SIZE);
312 if (ret != ID3_HEADER_SIZE)
314 if (id3_match(buf)) {
315 /* skip ID3 header */
316 len = ((buf[6] & 0x7f) << 21) |
317 ((buf[7] & 0x7f) << 14) |
318 ((buf[8] & 0x7f) << 7) |
320 url_fskip(&s->pb, len);
322 url_fseek(&s->pb, 0, SEEK_SET);
325 /* the parameters will be extracted from the compressed bitstream */
329 #define MP3_PACKET_SIZE 1024
331 static int mp3_read_packet(AVFormatContext *s, AVPacket *pkt)
334 // AVStream *st = s->streams[0];
336 size= MP3_PACKET_SIZE;
338 ret= av_get_packet(&s->pb, pkt, size);
340 pkt->stream_index = 0;
344 /* note: we need to modify the packet size here to handle the last
350 static int mp3_read_close(AVFormatContext *s)
357 static int mp3_write_header(struct AVFormatContext *s)
362 static int mp3_write_packet(struct AVFormatContext *s, AVPacket *pkt)
364 put_buffer(&s->pb, pkt->data, pkt->size);
365 put_flush_packet(&s->pb);
369 static int mp3_write_trailer(struct AVFormatContext *s)
371 uint8_t buf[ID3_TAG_SIZE];
373 /* write the id3 header */
374 if (s->title[0] != '\0') {
375 id3_create_tag(s, buf);
376 put_buffer(&s->pb, buf, ID3_TAG_SIZE);
377 put_flush_packet(&s->pb);
381 #endif //CONFIG_MUXERS
383 #ifdef CONFIG_MP3_DEMUXER
384 AVInputFormat mp3_demuxer = {
392 .extensions = "mp2,mp3,m2a", /* XXX: use probe */
395 #ifdef CONFIG_MP2_MUXER
396 AVOutputFormat mp2_muxer = {
398 "MPEG audio layer 2",
400 #ifdef CONFIG_MP3LAME
413 #ifdef CONFIG_MP3_MUXER
414 AVOutputFormat mp3_muxer = {
416 "MPEG audio layer 3",