Commit | Line | Data |
---|---|---|
0716b577 J |
1 | /* |
2 | * Interface to libmp3lame for mp3 encoding | |
3 | * Copyright (c) 2002 Lennert Buytenhek <buytenh@gnu.org> | |
4 | * | |
5 | * This program is free software; you can redistribute it and/or modify | |
6 | * it under the terms of the GNU General Public License as published by | |
7 | * the Free Software Foundation; either version 2 of the License, or | |
8 | * (at your option) any later version. | |
9 | * | |
10 | * This program 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 | |
13 | * GNU General Public License for more details. | |
14 | * | |
15 | * You should have received a copy of the GNU General Public License | |
16 | * along with this program; if not, write to the Free Software | |
17 | * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. | |
18 | */ | |
19 | ||
20 | #include "avcodec.h" | |
0716b577 J |
21 | #include "mpegaudio.h" |
22 | #include <lame/lame.h> | |
23 | ||
24 | typedef struct Mp3AudioContext { | |
25 | lame_global_flags *gfp; | |
26 | int first_frame; | |
27 | int stereo; | |
28 | } Mp3AudioContext; | |
29 | ||
30 | ||
31 | static int MP3lame_encode_init(AVCodecContext *avctx) | |
32 | { | |
33 | Mp3AudioContext *s = avctx->priv_data; | |
34 | ||
35 | if (avctx->channels > 2) | |
36 | return -1; | |
37 | ||
38 | s->first_frame = 1; | |
39 | s->stereo = avctx->channels > 1 ? 1 : 0; | |
40 | ||
41 | if ((s->gfp = lame_init()) == NULL) | |
42 | goto err; | |
43 | lame_set_in_samplerate(s->gfp, avctx->sample_rate); | |
e2263827 | 44 | lame_set_out_samplerate(s->gfp, avctx->sample_rate); |
0716b577 J |
45 | lame_set_num_channels(s->gfp, avctx->channels); |
46 | /* lame 3.91 dies on quality != 5 */ | |
47 | lame_set_quality(s->gfp, 5); | |
48 | /* lame 3.91 doesn't work in mono */ | |
49 | lame_set_mode(s->gfp, JOINT_STEREO); | |
50 | lame_set_brate(s->gfp, avctx->bit_rate/1000); | |
51 | if (lame_init_params(s->gfp) < 0) | |
52 | goto err_close; | |
53 | ||
54 | avctx->frame_size = MPA_FRAME_SIZE; | |
55 | avctx->key_frame = 1; | |
56 | ||
57 | return 0; | |
58 | ||
59 | err_close: | |
60 | lame_close(s->gfp); | |
61 | err: | |
62 | return -1; | |
63 | } | |
64 | ||
65 | int MP3lame_encode_frame(AVCodecContext *avctx, | |
66 | unsigned char *frame, int buf_size, void *data) | |
67 | { | |
68 | Mp3AudioContext *s = avctx->priv_data; | |
69 | int num; | |
70 | ||
71 | /* lame 3.91 dies on '1-channel interleaved' data */ | |
72 | if (s->stereo) { | |
73 | num = lame_encode_buffer_interleaved(s->gfp, data, | |
74 | MPA_FRAME_SIZE, frame, buf_size); | |
75 | } else { | |
76 | num = lame_encode_buffer(s->gfp, data, data, MPA_FRAME_SIZE, | |
77 | frame, buf_size); | |
78 | } | |
79 | ||
80 | /* lame 3.91 outputs the first frame as garbage */ | |
81 | if (s->first_frame) | |
82 | s->first_frame = num = 0; | |
0716b577 J |
83 | return num; |
84 | } | |
85 | ||
86 | int MP3lame_encode_close(AVCodecContext *avctx) | |
87 | { | |
88 | Mp3AudioContext *s = avctx->priv_data; | |
89 | ||
90 | lame_close(s->gfp); | |
91 | return 0; | |
92 | } | |
93 | ||
94 | ||
95 | AVCodec mp3lame_encoder = { | |
96 | "mp3", | |
97 | CODEC_TYPE_AUDIO, | |
98 | CODEC_ID_MP3LAME, | |
99 | sizeof(Mp3AudioContext), | |
100 | MP3lame_encode_init, | |
101 | MP3lame_encode_frame, | |
102 | MP3lame_encode_close | |
103 | }; |