2 * This file is part of MPlayer.
4 * MPlayer is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 2 of the License, or
7 * (at your option) any later version.
9 * MPlayer is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License along
15 * with MPlayer; if not, write to the Free Software Foundation, Inc.,
16 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
24 #include <sys/types.h>
27 #include "libmpdemux/aviheader.h"
28 #include "libmpdemux/ms_hdr.h"
29 #include "stream/stream.h"
30 #include "libmpdemux/muxer.h"
34 #include "libaf/af_format.h"
35 #include "libmpdemux/mp3_hdr.h"
38 #include <lame/lame.h>
40 lame_global_flags
*lame
;
41 static int lame_param_quality
=0; // best
42 static int lame_param_algqual
=5; // same as old default
43 static int lame_param_vbr
=vbr_default
;
44 static int lame_param_mode
=-1; // unset
45 static int lame_param_padding
=-1; // unset
46 static int lame_param_br
=-1; // unset
47 static int lame_param_ratio
=-1; // unset
48 static float lame_param_scale
=-1; // unset
49 static int lame_param_lowpassfreq
= 0; //auto
50 static int lame_param_highpassfreq
= 0; //auto
51 static int lame_param_free_format
= 0; //disabled
52 static int lame_param_br_min
= 0; //not specified
53 static int lame_param_br_max
= 0; //not specified
55 #ifdef CONFIG_MP3LAME_PRESET
56 int lame_param_fast
=0; // unset
57 static char* lame_param_preset
=NULL
; // unset
58 static int lame_presets_set( lame_t gfp
, int fast
, int cbr
, const char* preset_name
);
62 const m_option_t lameopts_conf
[] = {
63 {"q", &lame_param_quality
, CONF_TYPE_INT
, CONF_RANGE
, 0, 9, NULL
},
64 {"aq", &lame_param_algqual
, CONF_TYPE_INT
, CONF_RANGE
, 0, 9, NULL
},
65 {"vbr", &lame_param_vbr
, CONF_TYPE_INT
, CONF_RANGE
, 0, vbr_max_indicator
, NULL
},
66 {"cbr", &lame_param_vbr
, CONF_TYPE_FLAG
, 0, 0, 0, NULL
},
67 {"abr", &lame_param_vbr
, CONF_TYPE_FLAG
, 0, 0, vbr_abr
, NULL
},
68 {"mode", &lame_param_mode
, CONF_TYPE_INT
, CONF_RANGE
, 0, MAX_INDICATOR
, NULL
},
69 {"padding", &lame_param_padding
, CONF_TYPE_INT
, CONF_RANGE
, 0, PAD_MAX_INDICATOR
, NULL
},
70 {"br", &lame_param_br
, CONF_TYPE_INT
, CONF_RANGE
, 0, 1024, NULL
},
71 {"ratio", &lame_param_ratio
, CONF_TYPE_INT
, CONF_RANGE
, 0, 100, NULL
},
72 {"vol", &lame_param_scale
, CONF_TYPE_FLOAT
, CONF_RANGE
, 0, 10, NULL
},
73 {"lowpassfreq",&lame_param_lowpassfreq
, CONF_TYPE_INT
, CONF_RANGE
, -1, 48000,0},
74 {"highpassfreq",&lame_param_highpassfreq
, CONF_TYPE_INT
, CONF_RANGE
, -1, 48000,0},
75 {"nofree", &lame_param_free_format
, CONF_TYPE_FLAG
, 0, 0, 0, NULL
},
76 {"free", &lame_param_free_format
, CONF_TYPE_FLAG
, 0, 0, 1, NULL
},
77 {"br_min", &lame_param_br_min
, CONF_TYPE_INT
, CONF_RANGE
, 0, 1024, NULL
},
78 {"br_max", &lame_param_br_max
, CONF_TYPE_INT
, CONF_RANGE
, 0, 1024, NULL
},
79 #ifdef CONFIG_MP3LAME_PRESET
80 {"fast", &lame_param_fast
, CONF_TYPE_FLAG
, 0, 0, 1, NULL
},
81 {"preset", &lame_param_preset
, CONF_TYPE_STRING
, 0, 0, 0, NULL
},
83 {"fast", "MPlayer was built without -lameopts fast support (requires libmp3lame >=3.92).\n", CONF_TYPE_PRINT
, CONF_NOCFG
, 0, 0, NULL
},
84 {"preset", "MPlayer was built without -lameopts preset support (requires libmp3lame >=3.92).\n", CONF_TYPE_PRINT
, CONF_NOCFG
, 0, 0, NULL
},
86 {"help", MSGTR_MEncoderMP3LameHelp
, CONF_TYPE_PRINT
, CONF_NOCFG
, 0, 0, NULL
},
87 {NULL
, NULL
, 0, 0, 0, 0, NULL
}
91 static int bind_lame(audio_encoder_t
*encoder
, muxer_stream_t
*mux_a
)
93 mp_msg(MSGT_MENCODER
, MSGL_INFO
, MSGTR_MP3AudioSelected
);
94 mux_a
->h
.dwSampleSize
=0; // VBR
95 mux_a
->h
.dwRate
=encoder
->params
.sample_rate
;
96 mux_a
->h
.dwScale
=encoder
->params
.samples_per_frame
; // samples/frame
97 if(sizeof(MPEGLAYER3WAVEFORMAT
)!=30) mp_msg(MSGT_MENCODER
,MSGL_WARN
,MSGTR_MP3WaveFormatSizeNot30
,sizeof(MPEGLAYER3WAVEFORMAT
));
98 mux_a
->wf
=malloc(sizeof(MPEGLAYER3WAVEFORMAT
)); // should be 30
99 mux_a
->wf
->wFormatTag
=0x55; // MP3
100 mux_a
->wf
->nChannels
= (lame_param_mode
<0) ? encoder
->params
.channels
: ((lame_param_mode
==3) ? 1 : 2);
101 mux_a
->wf
->nSamplesPerSec
=mux_a
->h
.dwRate
;
103 mux_a
->wf
->nAvgBytesPerSec
=lame_param_br
* 125;
105 mux_a
->wf
->nAvgBytesPerSec
=192000/8; // FIXME!
106 mux_a
->wf
->nBlockAlign
=encoder
->params
.samples_per_frame
; // required for l3codeca.acm + WMP 6.4
107 mux_a
->wf
->wBitsPerSample
=0; //16;
108 // from NaNdub: (requires for l3codeca.acm)
109 mux_a
->wf
->cbSize
=12;
110 ((MPEGLAYER3WAVEFORMAT
*)(mux_a
->wf
))->wID
=1;
111 ((MPEGLAYER3WAVEFORMAT
*)(mux_a
->wf
))->fdwFlags
=2;
112 ((MPEGLAYER3WAVEFORMAT
*)(mux_a
->wf
))->nBlockSize
=encoder
->params
.samples_per_frame
; // ???
113 ((MPEGLAYER3WAVEFORMAT
*)(mux_a
->wf
))->nFramesPerBlock
=1;
114 ((MPEGLAYER3WAVEFORMAT
*)(mux_a
->wf
))->nCodecDelay
=0;
116 encoder
->input_format
= AF_FORMAT_S16_NE
;
117 encoder
->min_buffer_size
= 4608;
118 encoder
->max_buffer_size
= mux_a
->h
.dwRate
* mux_a
->wf
->nChannels
* 2;
123 #define min(a, b) ((a) <= (b) ? (a) : (b))
125 static int get_frame_size(audio_encoder_t
*encoder
)
128 if(encoder
->stream
->buffer_len
< 4)
130 sz
= mp_decode_mp3_header(encoder
->stream
->buffer
);
136 static int encode_lame(audio_encoder_t
*encoder
, uint8_t *dest
, void *src
, int len
, int max_size
)
139 if(encoder
->params
.channels
== 1)
140 n
= lame_encode_buffer(lame
, (short *)src
, (short *)src
, len
/2, dest
, max_size
);
142 n
= lame_encode_buffer_interleaved(lame
,(short *)src
, len
/4, dest
, max_size
);
144 return n
< 0 ? 0 : n
;
148 static int close_lame(audio_encoder_t
*encoder
)
153 static void fixup(audio_encoder_t
*encoder
)
155 // fixup CBR mp3 audio header:
156 if(!lame_param_vbr
) {
157 encoder
->stream
->h
.dwSampleSize
=1;
158 if (encoder
->stream
->h
.dwLength
)
159 ((MPEGLAYER3WAVEFORMAT
*)(encoder
->stream
->wf
))->nBlockSize
=
160 (encoder
->stream
->size
+(encoder
->stream
->h
.dwLength
>>1))/encoder
->stream
->h
.dwLength
;
161 encoder
->stream
->h
.dwLength
=encoder
->stream
->size
;
162 encoder
->stream
->h
.dwRate
=encoder
->stream
->wf
->nAvgBytesPerSec
;
163 encoder
->stream
->h
.dwScale
=1;
164 encoder
->stream
->wf
->nBlockAlign
=1;
165 mp_msg(MSGT_MENCODER
, MSGL_V
, MSGTR_CBRAudioByterate
,
166 encoder
->stream
->h
.dwRate
,((MPEGLAYER3WAVEFORMAT
*)(encoder
->stream
->wf
))->nBlockSize
);
170 int mpae_init_lame(audio_encoder_t
*encoder
)
172 encoder
->params
.bitrate
= lame_param_br
* 125;
173 encoder
->params
.samples_per_frame
= encoder
->params
.sample_rate
< 32000 ? 576 : 1152;
174 encoder
->decode_buffer_size
= 2304;
177 lame_set_bWriteVbrTag(lame
,0);
178 lame_set_in_samplerate(lame
,encoder
->params
.sample_rate
);
179 //lame_set_in_samplerate(lame,sh_audio->samplerate); // if resampling done by lame
180 lame_set_num_channels(lame
,encoder
->params
.channels
);
181 lame_set_out_samplerate(lame
,encoder
->params
.sample_rate
);
182 lame_set_quality(lame
,lame_param_algqual
); // 0 = best q
183 if(lame_param_free_format
) lame_set_free_format(lame
,1);
184 if(lame_param_vbr
){ // VBR:
185 lame_set_VBR(lame
,lame_param_vbr
); // vbr mode
186 lame_set_VBR_q(lame
,lame_param_quality
); // 0 = best vbr q 5=~128k
187 if(lame_param_br
>0) lame_set_VBR_mean_bitrate_kbps(lame
,lame_param_br
);
188 if(lame_param_br_min
>0) lame_set_VBR_min_bitrate_kbps(lame
,lame_param_br_min
);
189 if(lame_param_br_max
>0) lame_set_VBR_max_bitrate_kbps(lame
,lame_param_br_max
);
191 if(lame_param_br
>0) lame_set_brate(lame
,lame_param_br
);
193 if(lame_param_mode
>=0) lame_set_mode(lame
,lame_param_mode
); // j-st
194 if(lame_param_ratio
>0) lame_set_compression_ratio(lame
,lame_param_ratio
);
195 if(lame_param_scale
>0) {
196 mp_msg(MSGT_MENCODER
, MSGL_V
, MSGTR_SettingAudioInputGain
, lame_param_scale
);
197 lame_set_scale(lame
,lame_param_scale
);
199 if(lame_param_lowpassfreq
>=-1) lame_set_lowpassfreq(lame
,lame_param_lowpassfreq
);
200 if(lame_param_highpassfreq
>=-1) lame_set_highpassfreq(lame
,lame_param_highpassfreq
);
201 #ifdef CONFIG_MP3LAME_PRESET
202 if(lame_param_preset
!= NULL
) {
203 mp_msg(MSGT_MENCODER
, MSGL_V
, MSGTR_LamePresetEquals
,lame_param_preset
);
204 if(lame_presets_set(lame
,lame_param_fast
, (lame_param_vbr
==0), lame_param_preset
) < 0)
208 if(lame_init_params(lame
) == -1) {
209 mp_msg(MSGT_MENCODER
, MSGL_FATAL
, MSGTR_LameCantInit
);
212 if( mp_msg_test(MSGT_MENCODER
,MSGL_V
) ) {
213 lame_print_config(lame
);
214 lame_print_internals(lame
);
217 encoder
->bind
= bind_lame
;
218 encoder
->get_frame_size
= get_frame_size
;
219 encoder
->encode
= encode_lame
;
220 encoder
->fixup
= fixup
;
221 encoder
->close
= close_lame
;
225 #ifdef CONFIG_MP3LAME_PRESET
227 taken out of presets_set in lame-3.93.1/frontend/parse.c and modified */
228 static int lame_presets_set( lame_t gfp
, int fast
, int cbr
, const char* preset_name
)
232 if (strcmp(preset_name
, "help") == 0) {
233 mp_msg(MSGT_MENCODER
, MSGL_FATAL
, MSGTR_LameVersion
, get_lame_version(), get_lame_url());
234 mp_msg(MSGT_MENCODER
, MSGL_FATAL
, MSGTR_LamePresetsLongInfo
);
238 //aliases for compatibility with old presets
240 if (strcmp(preset_name
, "phone") == 0) {
244 if ( (strcmp(preset_name
, "phon+") == 0) ||
245 (strcmp(preset_name
, "lw") == 0) ||
246 (strcmp(preset_name
, "mw-eu") == 0) ||
247 (strcmp(preset_name
, "sw") == 0)) {
251 if (strcmp(preset_name
, "mw-us") == 0) {
255 if (strcmp(preset_name
, "voice") == 0) {
259 if (strcmp(preset_name
, "fm") == 0) {
262 if ( (strcmp(preset_name
, "radio") == 0) ||
263 (strcmp(preset_name
, "tape") == 0)) {
266 if (strcmp(preset_name
, "hifi") == 0) {
269 if (strcmp(preset_name
, "cd") == 0) {
272 if (strcmp(preset_name
, "studio") == 0) {
276 #ifdef CONFIG_MP3LAME_PRESET_MEDIUM
277 if (strcmp(preset_name
, "medium") == 0) {
279 lame_set_preset(gfp
, MEDIUM_FAST
);
281 lame_set_preset(gfp
, MEDIUM
);
287 if (strcmp(preset_name
, "standard") == 0) {
289 lame_set_preset(gfp
, STANDARD_FAST
);
291 lame_set_preset(gfp
, STANDARD
);
296 else if (strcmp(preset_name
, "extreme") == 0){
298 lame_set_preset(gfp
, EXTREME_FAST
);
300 lame_set_preset(gfp
, EXTREME
);
305 else if (((strcmp(preset_name
, "insane") == 0) ||
306 (strcmp(preset_name
, "320" ) == 0)) && (fast
< 1)) {
308 lame_set_preset(gfp
, INSANE
);
313 // Generic ABR Preset
314 if (((atoi(preset_name
)) > 0) && (fast
< 1)) {
315 if ((atoi(preset_name
)) >= 8 && (atoi(preset_name
)) <= 320){
316 lame_set_preset(gfp
, atoi(preset_name
));
319 lame_set_VBR(gfp
, vbr_off
);
322 lame_set_mode(gfp
, MONO
);
329 mp_msg(MSGT_MENCODER
, MSGL_FATAL
, MSGTR_LameVersion
, get_lame_version(), get_lame_url());
330 mp_msg(MSGT_MENCODER
, MSGL_FATAL
, MSGTR_InvalidBitrateForLamePreset
);
335 mp_msg(MSGT_MENCODER
, MSGL_FATAL
, MSGTR_LameVersion
, get_lame_version(), get_lame_url());
336 mp_msg(MSGT_MENCODER
, MSGL_FATAL
, MSGTR_InvalidLamePresetOptions
);