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
21 #define ID3_HEADER_SIZE 10
22 #define ID3_TAG_SIZE 128
24 #define ID3_GENRE_MAX 125
26 static const char *id3_genre_str[ID3_GENRE_MAX + 1] = {
60 [33] = "Instrumental",
73 [46] = "Instrumental Pop",
74 [47] = "Instrumental Rock",
78 [51] = "Techno-Industrial",
83 [56] = "Southern Rock",
88 [61] = "Christian Rap",
91 [64] = "Native American",
105 [78] = "Rock & Roll",
109 [82] = "National Folk",
111 [84] = "Fast Fusion",
118 [91] = "Gothic Rock",
119 [92] = "Progressive Rock",
120 [93] = "Psychedelic Rock",
121 [94] = "Symphonic Rock",
125 [98] = "Easy Listening",
131 [104] = "Chamber Music",
134 [107] = "Booty Bass",
136 [109] = "Porn Groove",
144 [117] = "Power Ballad",
145 [118] = "Rhythmic Soul",
151 [124] = "Euro-House",
152 [125] = "Dance Hall",
155 /* buf must be ID3_HEADER_SIZE byte long */
156 static int id3_match(const uint8_t *buf)
158 return (buf[0] == 'I' &&
163 (buf[6] & 0x80) == 0 &&
164 (buf[7] & 0x80) == 0 &&
165 (buf[8] & 0x80) == 0 &&
166 (buf[9] & 0x80) == 0);
169 static void id3_get_string(char *str, int str_size,
170 const uint8_t *buf, int buf_size)
176 for(i = 0; i < buf_size; i++) {
180 if ((q - str) >= str_size - 1)
187 /* 'buf' must be ID3_TAG_SIZE byte long */
188 static int id3_parse_tag(AVFormatContext *s, const uint8_t *buf)
193 if (!(buf[0] == 'T' &&
197 id3_get_string(s->title, sizeof(s->title), buf + 3, 30);
198 id3_get_string(s->author, sizeof(s->author), buf + 33, 30);
199 id3_get_string(s->album, sizeof(s->album), buf + 63, 30);
200 id3_get_string(str, sizeof(str), buf + 93, 4);
202 id3_get_string(s->comment, sizeof(s->comment), buf + 97, 30);
203 if (buf[125] == 0 && buf[126] != 0)
206 if (genre <= ID3_GENRE_MAX)
207 pstrcpy(s->genre, sizeof(s->genre), id3_genre_str[genre]);
211 static void id3_create_tag(AVFormatContext *s, uint8_t *buf)
215 memset(buf, 0, ID3_TAG_SIZE); /* fail safe */
219 strncpy(buf + 3, s->title, 30);
220 strncpy(buf + 33, s->author, 30);
221 strncpy(buf + 63, s->album, 30);
224 for(i = 0;i < 4; i++) {
225 buf[96 - i] = '0' + (v % 10);
229 strncpy(buf + 97, s->comment, 30);
234 for(i = 0; i <= ID3_GENRE_MAX; i++) {
235 if (!strcasecmp(s->genre, id3_genre_str[i])) {
244 static int mp3_read_probe(AVProbeData *p)
251 if(p->buf[0] == 'I' && p->buf[1] == 'D' && p->buf[2] == '3' &&
253 return AVPROBE_SCORE_MAX;
255 if(p->buf[0] != 0xff)
259 if((d & 0xe0) != 0xe0 || ((d & 0x18) == 0x08 || (d & 0x06) == 0))
263 if((d & 0xf0) == 0xf0 || (d & 0x0c) == 0x0c)
266 return AVPROBE_SCORE_MAX;
269 static int mp3_read_header(AVFormatContext *s,
270 AVFormatParameters *ap)
273 uint8_t buf[ID3_TAG_SIZE];
274 int len, ret, filesize;
276 st = av_new_stream(s, 0);
278 return AVERROR_NOMEM;
280 st->codec->codec_type = CODEC_TYPE_AUDIO;
281 st->codec->codec_id = CODEC_ID_MP3;
282 st->need_parsing = 1;
284 /* try to get the TAG */
285 if (!url_is_streamed(&s->pb)) {
286 /* XXX: change that */
287 filesize = url_fsize(&s->pb);
288 if (filesize > 128) {
289 url_fseek(&s->pb, filesize - 128, SEEK_SET);
290 ret = get_buffer(&s->pb, buf, ID3_TAG_SIZE);
291 if (ret == ID3_TAG_SIZE) {
292 id3_parse_tag(s, buf);
294 url_fseek(&s->pb, 0, SEEK_SET);
298 /* if ID3 header found, skip it */
299 ret = get_buffer(&s->pb, buf, ID3_HEADER_SIZE);
300 if (ret != ID3_HEADER_SIZE)
302 if (id3_match(buf)) {
303 /* skip ID3 header */
304 len = ((buf[6] & 0x7f) << 21) |
305 ((buf[7] & 0x7f) << 14) |
306 ((buf[8] & 0x7f) << 7) |
308 url_fskip(&s->pb, len);
310 url_fseek(&s->pb, 0, SEEK_SET);
313 /* the parameters will be extracted from the compressed bitstream */
317 #define MP3_PACKET_SIZE 1024
319 static int mp3_read_packet(AVFormatContext *s, AVPacket *pkt)
322 // AVStream *st = s->streams[0];
324 size= MP3_PACKET_SIZE;
326 ret= av_get_packet(&s->pb, pkt, size);
328 pkt->stream_index = 0;
332 /* note: we need to modify the packet size here to handle the last
338 static int mp3_read_close(AVFormatContext *s)
345 static int mp3_write_header(struct AVFormatContext *s)
350 static int mp3_write_packet(struct AVFormatContext *s, AVPacket *pkt)
352 put_buffer(&s->pb, pkt->data, pkt->size);
353 put_flush_packet(&s->pb);
357 static int mp3_write_trailer(struct AVFormatContext *s)
359 uint8_t buf[ID3_TAG_SIZE];
361 /* write the id3 header */
362 if (s->title[0] != '\0') {
363 id3_create_tag(s, buf);
364 put_buffer(&s->pb, buf, ID3_TAG_SIZE);
365 put_flush_packet(&s->pb);
369 #endif //CONFIG_MUXERS
371 #ifdef CONFIG_MP3_DEMUXER
372 AVInputFormat mp3_demuxer = {
380 .extensions = "mp2,mp3,m2a", /* XXX: use probe */
383 #ifdef CONFIG_MP2_MUXER
384 AVOutputFormat mp2_muxer = {
386 "MPEG audio layer 2",
388 #ifdef CONFIG_MP3LAME
401 #ifdef CONFIG_MP3_MUXER
402 AVOutputFormat mp3_muxer = {
404 "MPEG audio layer 3",