Statistics
| Branch: | Revision:

ffmpeg / libavcodec / mp3lameaudio.c @ 46b4feec

History | View | Annotate | Download (2.66 KB)

1
/*
2
 * Interface to libmp3lame for mp3 encoding
3
 * Copyright (c) 2002 Lennert Buytenhek <buytenh@gnu.org>
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., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
18
 */
19
 
20
/**
21
 * @file mp3lameaudio.c
22
 * Interface to libmp3lame for mp3 encoding.
23
 */
24

    
25
#include "avcodec.h"
26
#include "mpegaudio.h"
27
#include <lame/lame.h>
28

    
29
typedef struct Mp3AudioContext {
30
        lame_global_flags *gfp;
31
        int stereo;
32
} Mp3AudioContext;
33

    
34

    
35
static int MP3lame_encode_init(AVCodecContext *avctx)
36
{
37
        Mp3AudioContext *s = avctx->priv_data;
38

    
39
        if (avctx->channels > 2)
40
                return -1;
41

    
42
        s->stereo = avctx->channels > 1 ? 1 : 0;
43

    
44
        if ((s->gfp = lame_init()) == NULL)
45
                goto err;
46
        lame_set_in_samplerate(s->gfp, avctx->sample_rate);
47
        lame_set_out_samplerate(s->gfp, avctx->sample_rate);
48
        lame_set_num_channels(s->gfp, avctx->channels);
49
        /* lame 3.91 dies on quality != 5 */
50
        lame_set_quality(s->gfp, 5);
51
        /* lame 3.91 doesn't work in mono */
52
        lame_set_mode(s->gfp, JOINT_STEREO);
53
        lame_set_brate(s->gfp, avctx->bit_rate/1000);
54
        if (lame_init_params(s->gfp) < 0)
55
                goto err_close;
56

    
57
        avctx->frame_size = MPA_FRAME_SIZE;
58
    
59
        avctx->coded_frame= avcodec_alloc_frame();
60
        avctx->coded_frame->key_frame= 1;
61

    
62
        return 0;
63

    
64
err_close:
65
        lame_close(s->gfp);
66
err:
67
        return -1;
68
}
69

    
70
int MP3lame_encode_frame(AVCodecContext *avctx,
71
                     unsigned char *frame, int buf_size, void *data)
72
{
73
        Mp3AudioContext *s = avctx->priv_data;
74
        int num;
75

    
76
        /* lame 3.91 dies on '1-channel interleaved' data */
77
        if (s->stereo) {
78
                num = lame_encode_buffer_interleaved(s->gfp, data,
79
                        MPA_FRAME_SIZE, frame, buf_size);
80
        } else {
81
                num = lame_encode_buffer(s->gfp, data, data, MPA_FRAME_SIZE,
82
                        frame, buf_size);
83
        }
84

    
85
        return num;
86
}
87

    
88
int MP3lame_encode_close(AVCodecContext *avctx)
89
{
90
        Mp3AudioContext *s = avctx->priv_data;
91
    
92
        av_freep(&avctx->coded_frame);
93

    
94
        lame_close(s->gfp);
95
        return 0;
96
}
97

    
98

    
99
AVCodec mp3lame_encoder = {
100
    "mp3",
101
    CODEC_TYPE_AUDIO,
102
    CODEC_ID_MP3LAME,
103
    sizeof(Mp3AudioContext),
104
    MP3lame_encode_init,
105
    MP3lame_encode_frame,
106
    MP3lame_encode_close
107
};