Statistics
| Branch: | Revision:

ffmpeg / libavformat / mp3.c @ 578688fa

History | View | Annotate | Download (10.2 KB)

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