~ubuntu-branches/ubuntu/jaunty/xvidcap/jaunty-proposed

« back to all changes in this revision

Viewing changes to ffmpeg/libavformat/mp3.c

  • Committer: Bazaar Package Importer
  • Author(s): John Dong
  • Date: 2008-02-25 15:47:12 UTC
  • mfrom: (1.1.1 upstream)
  • Revision ID: james.westby@ubuntu.com-20080225154712-qvr11ekcea4c9ry8
Tags: 1.1.6-0.1ubuntu1
* Merge from debian-multimedia (LP: #120003), Ubuntu Changes:
 - For ffmpeg-related build-deps, remove cvs from package names.
 - Standards-Version 3.7.3
 - Maintainer Spec

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
/* 
2
 
 * MP3 encoder and decoder
 
1
/*
 
2
 * MP3 muxer and demuxer
3
3
 * Copyright (c) 2003 Fabrice Bellard.
4
4
 *
5
 
 * This library is free software; you can redistribute it and/or
 
5
 * This file is part of FFmpeg.
 
6
 *
 
7
 * FFmpeg is free software; you can redistribute it and/or
6
8
 * modify it under the terms of the GNU Lesser General Public
7
9
 * License as published by the Free Software Foundation; either
8
 
 * version 2 of the License, or (at your option) any later version.
 
10
 * version 2.1 of the License, or (at your option) any later version.
9
11
 *
10
 
 * This library is distributed in the hope that it will be useful,
 
12
 * FFmpeg is distributed in the hope that it will be useful,
11
13
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12
14
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13
15
 * Lesser General Public License for more details.
14
16
 *
15
17
 * 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., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 
18
 * License along with FFmpeg; if not, write to the Free Software
 
19
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
18
20
 */
19
21
#include "avformat.h"
 
22
#include "mpegaudio.h"
20
23
 
21
24
#define ID3_HEADER_SIZE 10
22
25
#define ID3_TAG_SIZE 128
166
169
            (buf[9] & 0x80) == 0);
167
170
}
168
171
 
169
 
static void id3_get_string(char *str, int str_size, 
 
172
static void id3_get_string(char *str, int str_size,
170
173
                           const uint8_t *buf, int buf_size)
171
174
{
172
175
    int i, c;
189
192
{
190
193
    char str[5];
191
194
    int genre;
192
 
    
 
195
 
193
196
    if (!(buf[0] == 'T' &&
194
197
          buf[1] == 'A' &&
195
198
          buf[2] == 'G'))
240
243
}
241
244
 
242
245
/* mp3 read */
 
246
 
 
247
static int mp3_read_probe(AVProbeData *p)
 
248
{
 
249
    int max_frames, first_frames;
 
250
    int fsize, frames, sample_rate;
 
251
    uint32_t header;
 
252
    uint8_t *buf, *buf2, *end;
 
253
    AVCodecContext avctx;
 
254
 
 
255
    if(p->buf_size < ID3_HEADER_SIZE)
 
256
        return 0;
 
257
 
 
258
    if(id3_match(p->buf))
 
259
        return AVPROBE_SCORE_MAX/2+1; // this must be less then mpeg-ps because some retards put id3 tage before mpeg-ps files
 
260
 
 
261
    max_frames = 0;
 
262
    buf = p->buf;
 
263
    end = buf + FFMIN(4096, p->buf_size - sizeof(uint32_t));
 
264
 
 
265
    for(; buf < end; buf++) {
 
266
        buf2 = buf;
 
267
 
 
268
        for(frames = 0; buf2 < end; frames++) {
 
269
            header = (buf2[0] << 24) | (buf2[1] << 16) | (buf2[2] << 8) | buf2[3];
 
270
            fsize = mpa_decode_header(&avctx, header, &sample_rate);
 
271
            if(fsize < 0)
 
272
                break;
 
273
            buf2 += fsize;
 
274
        }
 
275
        max_frames = FFMAX(max_frames, frames);
 
276
        if(buf == p->buf)
 
277
            first_frames= frames;
 
278
    }
 
279
    if   (first_frames>=3) return AVPROBE_SCORE_MAX/2+1;
 
280
    else if(max_frames>=3) return AVPROBE_SCORE_MAX/4;
 
281
    else if(max_frames>=1) return 1;
 
282
    else                   return 0;
 
283
}
 
284
 
243
285
static int mp3_read_header(AVFormatContext *s,
244
286
                           AVFormatParameters *ap)
245
287
{
251
293
    if (!st)
252
294
        return AVERROR_NOMEM;
253
295
 
254
 
    st->codec.codec_type = CODEC_TYPE_AUDIO;
255
 
    st->codec.codec_id = CODEC_ID_MP3;
 
296
    st->codec->codec_type = CODEC_TYPE_AUDIO;
 
297
    st->codec->codec_id = CODEC_ID_MP3;
 
298
    st->need_parsing = 1;
256
299
 
257
300
    /* try to get the TAG */
258
301
    if (!url_is_streamed(&s->pb)) {
259
302
        /* XXX: change that */
260
 
        filesize = url_filesize(url_fileno(&s->pb));
 
303
        filesize = url_fsize(&s->pb);
261
304
        if (filesize > 128) {
262
305
            url_fseek(&s->pb, filesize - 128, SEEK_SET);
263
306
            ret = get_buffer(&s->pb, buf, ID3_TAG_SIZE);
293
336
{
294
337
    int ret, size;
295
338
    //    AVStream *st = s->streams[0];
296
 
    
 
339
 
297
340
    size= MP3_PACKET_SIZE;
298
341
 
299
 
    if (av_new_packet(pkt, size) < 0)
300
 
        return -EIO;
 
342
    ret= av_get_packet(&s->pb, pkt, size);
301
343
 
302
344
    pkt->stream_index = 0;
303
 
    ret = get_buffer(&s->pb, pkt->data, size);
304
345
    if (ret <= 0) {
305
 
        av_free_packet(pkt);
306
 
        return -EIO;
 
346
        return AVERROR_IO;
307
347
    }
308
348
    /* note: we need to modify the packet size here to handle the last
309
349
       packet */
316
356
    return 0;
317
357
}
318
358
 
 
359
#ifdef CONFIG_MUXERS
319
360
/* simple formats */
320
361
static int mp3_write_header(struct AVFormatContext *s)
321
362
{
322
363
    return 0;
323
364
}
324
365
 
325
 
static int mp3_write_packet(struct AVFormatContext *s, int stream_index,
326
 
                            const uint8_t *buf, int size, int64_t pts)
 
366
static int mp3_write_packet(struct AVFormatContext *s, AVPacket *pkt)
327
367
{
328
 
    put_buffer(&s->pb, buf, size);
 
368
    put_buffer(&s->pb, pkt->data, pkt->size);
329
369
    put_flush_packet(&s->pb);
330
370
    return 0;
331
371
}
342
382
    }
343
383
    return 0;
344
384
}
 
385
#endif //CONFIG_MUXERS
345
386
 
346
 
AVInputFormat mp3_iformat = {
 
387
#ifdef CONFIG_MP3_DEMUXER
 
388
AVInputFormat mp3_demuxer = {
347
389
    "mp3",
348
390
    "MPEG audio",
349
391
    0,
350
 
    NULL,
 
392
    mp3_read_probe,
351
393
    mp3_read_header,
352
394
    mp3_read_packet,
353
395
    mp3_read_close,
354
 
    .extensions = "mp2,mp3", /* XXX: use probe */
 
396
    .flags= AVFMT_GENERIC_INDEX,
 
397
    .extensions = "mp2,mp3,m2a", /* XXX: use probe */
355
398
};
356
 
 
357
 
AVOutputFormat mp2_oformat = {
 
399
#endif
 
400
#ifdef CONFIG_MP2_MUXER
 
401
AVOutputFormat mp2_muxer = {
358
402
    "mp2",
359
403
    "MPEG audio layer 2",
360
404
    "audio/x-mpeg",
361
 
#ifdef CONFIG_MP3LAME
362
 
    "mp2",
 
405
#ifdef CONFIG_LIBMP3LAME
 
406
    "mp2,m2a",
363
407
#else
364
 
    "mp2,mp3",
 
408
    "mp2,mp3,m2a",
365
409
#endif
366
410
    0,
367
411
    CODEC_ID_MP2,
370
414
    mp3_write_packet,
371
415
    mp3_write_trailer,
372
416
};
373
 
 
374
 
#ifdef CONFIG_MP3LAME
375
 
AVOutputFormat mp3_oformat = {
 
417
#endif
 
418
#ifdef CONFIG_MP3_MUXER
 
419
AVOutputFormat mp3_muxer = {
376
420
    "mp3",
377
421
    "MPEG audio layer 3",
378
422
    "audio/x-mpeg",
385
429
    mp3_write_trailer,
386
430
};
387
431
#endif
388
 
 
389
 
int mp3_init(void)
390
 
{
391
 
    av_register_input_format(&mp3_iformat);
392
 
    av_register_output_format(&mp2_oformat);
393
 
#ifdef CONFIG_MP3LAME
394
 
    av_register_output_format(&mp3_oformat);
395
 
#endif    
396
 
    return 0;
397
 
}