Statistics
| Branch: | Revision:

ffmpeg / libavcodec / mp3lameaudio.c @ 58c2182d

History | View | Annotate | Download (3.01 KB)

1 0716b577 Juanjo
/*
2
 * Interface to libmp3lame for mp3 encoding
3
 * Copyright (c) 2002 Lennert Buytenhek <buytenh@gnu.org>
4
 *
5 ff4ec49e Fabrice Bellard
 * 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 0716b577 Juanjo
 *
10 ff4ec49e Fabrice Bellard
 * This library is distributed in the hope that it will be useful,
11 0716b577 Juanjo
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 ff4ec49e Fabrice Bellard
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13
 * Lesser General Public License for more details.
14 0716b577 Juanjo
 *
15 ff4ec49e Fabrice Bellard
 * 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 0716b577 Juanjo
 */
19 983e3246 Michael Niedermayer
 
20
/**
21
 * @file mp3lameaudio.c
22
 * Interface to libmp3lame for mp3 encoding.
23
 */
24 0716b577 Juanjo
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 e2263827 Juanjo
        lame_set_out_samplerate(s->gfp, avctx->sample_rate);
48 0716b577 Juanjo
        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 c276af04 Michael Niedermayer
        lame_set_bWriteVbrTag(s->gfp,0);
55 0716b577 Juanjo
        if (lame_init_params(s->gfp) < 0)
56
                goto err_close;
57
58
        avctx->frame_size = MPA_FRAME_SIZE;
59 befaced5 Michael Niedermayer
    
60
        avctx->coded_frame= avcodec_alloc_frame();
61
        avctx->coded_frame->key_frame= 1;
62 0716b577 Juanjo
63
        return 0;
64
65
err_close:
66
        lame_close(s->gfp);
67
err:
68
        return -1;
69
}
70
71
int MP3lame_encode_frame(AVCodecContext *avctx,
72
                     unsigned char *frame, int buf_size, void *data)
73
{
74
        Mp3AudioContext *s = avctx->priv_data;
75 c276af04 Michael Niedermayer
        int num, i;
76
//av_log(avctx, AV_LOG_DEBUG, "%X %d %X\n", (int)frame, buf_size, (int)data);
77
//        if(data==NULL)
78
//            return lame_encode_flush(s->gfp, frame, buf_size);
79 0716b577 Juanjo
80
        /* lame 3.91 dies on '1-channel interleaved' data */
81
        if (s->stereo) {
82
                num = lame_encode_buffer_interleaved(s->gfp, data,
83
                        MPA_FRAME_SIZE, frame, buf_size);
84
        } else {
85
                num = lame_encode_buffer(s->gfp, data, data, MPA_FRAME_SIZE,
86
                        frame, buf_size);
87 c276af04 Michael Niedermayer
88
/*av_log(avctx, AV_LOG_DEBUG, "in:%d out:%d\n", MPA_FRAME_SIZE, num);
89
for(i=0; i<num; i++){
90
    av_log(avctx, AV_LOG_DEBUG, "%2X ", frame[i]);
91
}*/
92 0716b577 Juanjo
        }
93
94
        return num;
95
}
96
97
int MP3lame_encode_close(AVCodecContext *avctx)
98
{
99
        Mp3AudioContext *s = avctx->priv_data;
100 befaced5 Michael Niedermayer
    
101
        av_freep(&avctx->coded_frame);
102 0716b577 Juanjo
103
        lame_close(s->gfp);
104
        return 0;
105
}
106
107
108
AVCodec mp3lame_encoder = {
109
    "mp3",
110
    CODEC_TYPE_AUDIO,
111 80783dc2 Fabrice Bellard
    CODEC_ID_MP3,
112 0716b577 Juanjo
    sizeof(Mp3AudioContext),
113
    MP3lame_encode_init,
114
    MP3lame_encode_frame,
115
    MP3lame_encode_close
116
};