]> rtime.felk.cvut.cz Git - frescor/ffmpeg.git/blob - libavformat/mp3.c
10l typo
[frescor/ffmpeg.git] / libavformat / mp3.c
1 /*
2  * MP3 encoder and decoder
3  * Copyright (c) 2003 Fabrice Bellard.
4  *
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.
9  *
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.
14  *
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
18  */
19 #include "avformat.h"
20 #include "mpegaudio.h"
21
22 #define ID3_HEADER_SIZE 10
23 #define ID3_TAG_SIZE 128
24
25 #define ID3_GENRE_MAX 125
26
27 static const char *id3_genre_str[ID3_GENRE_MAX + 1] = {
28     [0] = "Blues",
29     [1] = "Classic Rock",
30     [2] = "Country",
31     [3] = "Dance",
32     [4] = "Disco",
33     [5] = "Funk",
34     [6] = "Grunge",
35     [7] = "Hip-Hop",
36     [8] = "Jazz",
37     [9] = "Metal",
38     [10] = "New Age",
39     [11] = "Oldies",
40     [12] = "Other",
41     [13] = "Pop",
42     [14] = "R&B",
43     [15] = "Rap",
44     [16] = "Reggae",
45     [17] = "Rock",
46     [18] = "Techno",
47     [19] = "Industrial",
48     [20] = "Alternative",
49     [21] = "Ska",
50     [22] = "Death Metal",
51     [23] = "Pranks",
52     [24] = "Soundtrack",
53     [25] = "Euro-Techno",
54     [26] = "Ambient",
55     [27] = "Trip-Hop",
56     [28] = "Vocal",
57     [29] = "Jazz+Funk",
58     [30] = "Fusion",
59     [31] = "Trance",
60     [32] = "Classical",
61     [33] = "Instrumental",
62     [34] = "Acid",
63     [35] = "House",
64     [36] = "Game",
65     [37] = "Sound Clip",
66     [38] = "Gospel",
67     [39] = "Noise",
68     [40] = "AlternRock",
69     [41] = "Bass",
70     [42] = "Soul",
71     [43] = "Punk",
72     [44] = "Space",
73     [45] = "Meditative",
74     [46] = "Instrumental Pop",
75     [47] = "Instrumental Rock",
76     [48] = "Ethnic",
77     [49] = "Gothic",
78     [50] = "Darkwave",
79     [51] = "Techno-Industrial",
80     [52] = "Electronic",
81     [53] = "Pop-Folk",
82     [54] = "Eurodance",
83     [55] = "Dream",
84     [56] = "Southern Rock",
85     [57] = "Comedy",
86     [58] = "Cult",
87     [59] = "Gangsta",
88     [60] = "Top 40",
89     [61] = "Christian Rap",
90     [62] = "Pop/Funk",
91     [63] = "Jungle",
92     [64] = "Native American",
93     [65] = "Cabaret",
94     [66] = "New Wave",
95     [67] = "Psychadelic",
96     [68] = "Rave",
97     [69] = "Showtunes",
98     [70] = "Trailer",
99     [71] = "Lo-Fi",
100     [72] = "Tribal",
101     [73] = "Acid Punk",
102     [74] = "Acid Jazz",
103     [75] = "Polka",
104     [76] = "Retro",
105     [77] = "Musical",
106     [78] = "Rock & Roll",
107     [79] = "Hard Rock",
108     [80] = "Folk",
109     [81] = "Folk-Rock",
110     [82] = "National Folk",
111     [83] = "Swing",
112     [84] = "Fast Fusion",
113     [85] = "Bebob",
114     [86] = "Latin",
115     [87] = "Revival",
116     [88] = "Celtic",
117     [89] = "Bluegrass",
118     [90] = "Avantgarde",
119     [91] = "Gothic Rock",
120     [92] = "Progressive Rock",
121     [93] = "Psychedelic Rock",
122     [94] = "Symphonic Rock",
123     [95] = "Slow Rock",
124     [96] = "Big Band",
125     [97] = "Chorus",
126     [98] = "Easy Listening",
127     [99] = "Acoustic",
128     [100] = "Humour",
129     [101] = "Speech",
130     [102] = "Chanson",
131     [103] = "Opera",
132     [104] = "Chamber Music",
133     [105] = "Sonata",
134     [106] = "Symphony",
135     [107] = "Booty Bass",
136     [108] = "Primus",
137     [109] = "Porn Groove",
138     [110] = "Satire",
139     [111] = "Slow Jam",
140     [112] = "Club",
141     [113] = "Tango",
142     [114] = "Samba",
143     [115] = "Folklore",
144     [116] = "Ballad",
145     [117] = "Power Ballad",
146     [118] = "Rhythmic Soul",
147     [119] = "Freestyle",
148     [120] = "Duet",
149     [121] = "Punk Rock",
150     [122] = "Drum Solo",
151     [123] = "A capella",
152     [124] = "Euro-House",
153     [125] = "Dance Hall",
154 };
155
156 /* buf must be ID3_HEADER_SIZE byte long */
157 static int id3_match(const uint8_t *buf)
158 {
159     return (buf[0] == 'I' &&
160             buf[1] == 'D' &&
161             buf[2] == '3' &&
162             buf[3] != 0xff &&
163             buf[4] != 0xff &&
164             (buf[6] & 0x80) == 0 &&
165             (buf[7] & 0x80) == 0 &&
166             (buf[8] & 0x80) == 0 &&
167             (buf[9] & 0x80) == 0);
168 }
169
170 static void id3_get_string(char *str, int str_size,
171                            const uint8_t *buf, int buf_size)
172 {
173     int i, c;
174     char *q;
175
176     q = str;
177     for(i = 0; i < buf_size; i++) {
178         c = buf[i];
179         if (c == '\0')
180             break;
181         if ((q - str) >= str_size - 1)
182             break;
183         *q++ = c;
184     }
185     *q = '\0';
186 }
187
188 /* 'buf' must be ID3_TAG_SIZE byte long */
189 static int id3_parse_tag(AVFormatContext *s, const uint8_t *buf)
190 {
191     char str[5];
192     int genre;
193
194     if (!(buf[0] == 'T' &&
195           buf[1] == 'A' &&
196           buf[2] == 'G'))
197         return -1;
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);
202     s->year = atoi(str);
203     id3_get_string(s->comment, sizeof(s->comment), buf + 97, 30);
204     if (buf[125] == 0 && buf[126] != 0)
205         s->track = buf[126];
206     genre = buf[127];
207     if (genre <= ID3_GENRE_MAX)
208         pstrcpy(s->genre, sizeof(s->genre), id3_genre_str[genre]);
209     return 0;
210 }
211
212 static void id3_create_tag(AVFormatContext *s, uint8_t *buf)
213 {
214     int v, i;
215
216     memset(buf, 0, ID3_TAG_SIZE); /* fail safe */
217     buf[0] = 'T';
218     buf[1] = 'A';
219     buf[2] = 'G';
220     strncpy(buf + 3, s->title, 30);
221     strncpy(buf + 33, s->author, 30);
222     strncpy(buf + 63, s->album, 30);
223     v = s->year;
224     if (v > 0) {
225         for(i = 0;i < 4; i++) {
226             buf[96 - i] = '0' + (v % 10);
227             v = v / 10;
228         }
229     }
230     strncpy(buf + 97, s->comment, 30);
231     if (s->track != 0) {
232         buf[125] = 0;
233         buf[126] = s->track;
234     }
235     for(i = 0; i <= ID3_GENRE_MAX; i++) {
236         if (!strcasecmp(s->genre, id3_genre_str[i])) {
237             buf[127] = i;
238             break;
239         }
240     }
241 }
242
243 /* mp3 read */
244
245 static int mp3_read_probe(AVProbeData *p)
246 {
247     int max_frames;
248     int fsize, frames;
249     uint32_t header;
250     uint8_t *buf, *buf2, *end;
251     AVCodecContext avctx;
252
253     if(p->buf_size < ID3_HEADER_SIZE)
254         return 0;
255
256     if(id3_match(p->buf))
257         return AVPROBE_SCORE_MAX;
258
259     max_frames = 0;
260     buf = p->buf;
261     end = buf + FFMIN(4096, p->buf_size - sizeof(uint32_t));
262
263     for(; buf < end; buf++) {
264         buf2 = buf;
265
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);
269             if(fsize < 0)
270                 break;
271             buf2 += fsize;
272         }
273         max_frames = FFMAX(max_frames, frames);
274     }
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;
278     else                   return 0;
279 }
280
281 static int mp3_read_header(AVFormatContext *s,
282                            AVFormatParameters *ap)
283 {
284     AVStream *st;
285     uint8_t buf[ID3_TAG_SIZE];
286     int len, ret, filesize;
287
288     st = av_new_stream(s, 0);
289     if (!st)
290         return AVERROR_NOMEM;
291
292     st->codec->codec_type = CODEC_TYPE_AUDIO;
293     st->codec->codec_id = CODEC_ID_MP3;
294     st->need_parsing = 1;
295
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);
305             }
306             url_fseek(&s->pb, 0, SEEK_SET);
307         }
308     }
309
310     /* if ID3 header found, skip it */
311     ret = get_buffer(&s->pb, buf, ID3_HEADER_SIZE);
312     if (ret != ID3_HEADER_SIZE)
313         return -1;
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) |
319             (buf[9] & 0x7f);
320         url_fskip(&s->pb, len);
321     } else {
322         url_fseek(&s->pb, 0, SEEK_SET);
323     }
324
325     /* the parameters will be extracted from the compressed bitstream */
326     return 0;
327 }
328
329 #define MP3_PACKET_SIZE 1024
330
331 static int mp3_read_packet(AVFormatContext *s, AVPacket *pkt)
332 {
333     int ret, size;
334     //    AVStream *st = s->streams[0];
335
336     size= MP3_PACKET_SIZE;
337
338     ret= av_get_packet(&s->pb, pkt, size);
339
340     pkt->stream_index = 0;
341     if (ret <= 0) {
342         return AVERROR_IO;
343     }
344     /* note: we need to modify the packet size here to handle the last
345        packet */
346     pkt->size = ret;
347     return ret;
348 }
349
350 static int mp3_read_close(AVFormatContext *s)
351 {
352     return 0;
353 }
354
355 #ifdef CONFIG_MUXERS
356 /* simple formats */
357 static int mp3_write_header(struct AVFormatContext *s)
358 {
359     return 0;
360 }
361
362 static int mp3_write_packet(struct AVFormatContext *s, AVPacket *pkt)
363 {
364     put_buffer(&s->pb, pkt->data, pkt->size);
365     put_flush_packet(&s->pb);
366     return 0;
367 }
368
369 static int mp3_write_trailer(struct AVFormatContext *s)
370 {
371     uint8_t buf[ID3_TAG_SIZE];
372
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);
378     }
379     return 0;
380 }
381 #endif //CONFIG_MUXERS
382
383 #ifdef CONFIG_MP3_DEMUXER
384 AVInputFormat mp3_demuxer = {
385     "mp3",
386     "MPEG audio",
387     0,
388     mp3_read_probe,
389     mp3_read_header,
390     mp3_read_packet,
391     mp3_read_close,
392     .extensions = "mp2,mp3,m2a", /* XXX: use probe */
393 };
394 #endif
395 #ifdef CONFIG_MP2_MUXER
396 AVOutputFormat mp2_muxer = {
397     "mp2",
398     "MPEG audio layer 2",
399     "audio/x-mpeg",
400 #ifdef CONFIG_MP3LAME
401     "mp2,m2a",
402 #else
403     "mp2,mp3,m2a",
404 #endif
405     0,
406     CODEC_ID_MP2,
407     0,
408     mp3_write_header,
409     mp3_write_packet,
410     mp3_write_trailer,
411 };
412 #endif
413 #ifdef CONFIG_MP3_MUXER
414 AVOutputFormat mp3_muxer = {
415     "mp3",
416     "MPEG audio layer 3",
417     "audio/x-mpeg",
418     "mp3",
419     0,
420     CODEC_ID_MP3,
421     0,
422     mp3_write_header,
423     mp3_write_packet,
424     mp3_write_trailer,
425 };
426 #endif