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"
32 #include "libaf/af_format.h"
33 #include "libmpdemux/mp3_hdr.h"
36 #include <lame/lame.h>
38 lame_global_flags
*lame
;
39 static int lame_param_quality
=0; // best
40 static int lame_param_algqual
=5; // same as old default
41 static int lame_param_vbr
=vbr_default
;
42 static int lame_param_mode
=-1; // unset
43 static int lame_param_padding
=-1; // unset
44 static int lame_param_br
=-1; // unset
45 static int lame_param_ratio
=-1; // unset
46 static float lame_param_scale
=-1; // unset
47 static int lame_param_lowpassfreq
= 0; //auto
48 static int lame_param_highpassfreq
= 0; //auto
49 static int lame_param_free_format
= 0; //disabled
50 static int lame_param_br_min
= 0; //not specified
51 static int lame_param_br_max
= 0; //not specified
53 #ifdef CONFIG_MP3LAME_PRESET
54 int lame_param_fast
=0; // unset
55 static char* lame_param_preset
=NULL
; // unset
56 static int lame_presets_set( lame_t gfp
, int fast
, int cbr
, const char* preset_name
);
59 #define MEncoderMP3LameHelp _("\n\n"\
60 " vbr=<0-4> variable bitrate method\n"\
61 " 0: cbr (constant bitrate)\n"\
62 " 1: mt (Mark Taylor VBR algorithm)\n"\
63 " 2: rh (Robert Hegemann VBR algorithm - default)\n"\
64 " 3: abr (average bitrate)\n"\
65 " 4: mtrh (Mark Taylor Robert Hegemann VBR algorithm)\n"\
67 " abr average bitrate\n"\
69 " cbr constant bitrate\n"\
70 " Also forces CBR mode encoding on subsequent ABR presets modes.\n"\
72 " br=<0-1024> specify bitrate in kBit (CBR and ABR only)\n"\
74 " q=<0-9> quality (0-highest, 9-lowest) (only for VBR)\n"\
76 " aq=<0-9> algorithmic quality (0-best/slowest, 9-worst/fastest)\n"\
78 " ratio=<1-100> compression ratio\n"\
80 " vol=<0-10> set audio input gain\n"\
82 " mode=<0-3> (default: auto)\n"\
93 " fast Switch on faster encoding on subsequent VBR presets modes,\n"\
94 " slightly lower quality and higher bitrates.\n"\
96 " preset=<value> Provide the highest possible quality settings.\n"\
97 " medium: VBR encoding, good quality\n"\
98 " (150-180 kbps bitrate range)\n"\
99 " standard: VBR encoding, high quality\n"\
100 " (170-210 kbps bitrate range)\n"\
101 " extreme: VBR encoding, very high quality\n"\
102 " (200-240 kbps bitrate range)\n"\
103 " insane: CBR encoding, highest preset quality\n"\
104 " (320 kbps bitrate)\n"\
105 " <8-320>: ABR encoding at average given kbps bitrate.\n\n")
109 const m_option_t lameopts_conf
[] = {
110 {"q", &lame_param_quality
, CONF_TYPE_INT
, CONF_RANGE
, 0, 9, NULL
},
111 {"aq", &lame_param_algqual
, CONF_TYPE_INT
, CONF_RANGE
, 0, 9, NULL
},
112 {"vbr", &lame_param_vbr
, CONF_TYPE_INT
, CONF_RANGE
, 0, vbr_max_indicator
, NULL
},
113 {"cbr", &lame_param_vbr
, CONF_TYPE_FLAG
, 0, 0, 0, NULL
},
114 {"abr", &lame_param_vbr
, CONF_TYPE_FLAG
, 0, 0, vbr_abr
, NULL
},
115 {"mode", &lame_param_mode
, CONF_TYPE_INT
, CONF_RANGE
, 0, MAX_INDICATOR
, NULL
},
116 {"padding", &lame_param_padding
, CONF_TYPE_INT
, CONF_RANGE
, 0, PAD_MAX_INDICATOR
, NULL
},
117 {"br", &lame_param_br
, CONF_TYPE_INT
, CONF_RANGE
, 0, 1024, NULL
},
118 {"ratio", &lame_param_ratio
, CONF_TYPE_INT
, CONF_RANGE
, 0, 100, NULL
},
119 {"vol", &lame_param_scale
, CONF_TYPE_FLOAT
, CONF_RANGE
, 0, 10, NULL
},
120 {"lowpassfreq",&lame_param_lowpassfreq
, CONF_TYPE_INT
, CONF_RANGE
, -1, 48000,0},
121 {"highpassfreq",&lame_param_highpassfreq
, CONF_TYPE_INT
, CONF_RANGE
, -1, 48000,0},
122 {"nofree", &lame_param_free_format
, CONF_TYPE_FLAG
, 0, 0, 0, NULL
},
123 {"free", &lame_param_free_format
, CONF_TYPE_FLAG
, 0, 0, 1, NULL
},
124 {"br_min", &lame_param_br_min
, CONF_TYPE_INT
, CONF_RANGE
, 0, 1024, NULL
},
125 {"br_max", &lame_param_br_max
, CONF_TYPE_INT
, CONF_RANGE
, 0, 1024, NULL
},
126 #ifdef CONFIG_MP3LAME_PRESET
127 {"fast", &lame_param_fast
, CONF_TYPE_FLAG
, 0, 0, 1, NULL
},
128 {"preset", &lame_param_preset
, CONF_TYPE_STRING
, 0, 0, 0, NULL
},
130 {"fast", "MPlayer was built without -lameopts fast support (requires libmp3lame >=3.92).\n", CONF_TYPE_PRINT
, CONF_NOCFG
, 0, 0, NULL
},
131 {"preset", "MPlayer was built without -lameopts preset support (requires libmp3lame >=3.92).\n", CONF_TYPE_PRINT
, CONF_NOCFG
, 0, 0, NULL
},
133 {"help", MEncoderMP3LameHelp
, CONF_TYPE_PRINT
, CONF_NOCFG
, 0, 0, NULL
},
134 {NULL
, NULL
, 0, 0, 0, 0, NULL
}
138 static int bind_lame(audio_encoder_t
*encoder
, muxer_stream_t
*mux_a
)
140 mp_tmsg(MSGT_MENCODER
, MSGL_INFO
, "MP3 audio selected.\n");
141 mux_a
->h
.dwSampleSize
=0; // VBR
142 mux_a
->h
.dwRate
=encoder
->params
.sample_rate
;
143 mux_a
->h
.dwScale
=encoder
->params
.samples_per_frame
; // samples/frame
144 if(sizeof(MPEGLAYER3WAVEFORMAT
)!=30) mp_tmsg(MSGT_MENCODER
,MSGL_WARN
,"sizeof(MPEGLAYER3WAVEFORMAT)==%d!=30, maybe broken C compiler?\n",sizeof(MPEGLAYER3WAVEFORMAT
));
145 mux_a
->wf
=malloc(sizeof(MPEGLAYER3WAVEFORMAT
)); // should be 30
146 mux_a
->wf
->wFormatTag
=0x55; // MP3
147 mux_a
->wf
->nChannels
= (lame_param_mode
<0) ? encoder
->params
.channels
: ((lame_param_mode
==3) ? 1 : 2);
148 mux_a
->wf
->nSamplesPerSec
=mux_a
->h
.dwRate
;
150 mux_a
->wf
->nAvgBytesPerSec
=lame_param_br
* 125;
152 mux_a
->wf
->nAvgBytesPerSec
=192000/8; // FIXME!
153 mux_a
->wf
->nBlockAlign
=encoder
->params
.samples_per_frame
; // required for l3codeca.acm + WMP 6.4
154 mux_a
->wf
->wBitsPerSample
=0; //16;
155 // from NaNdub: (requires for l3codeca.acm)
156 mux_a
->wf
->cbSize
=12;
157 ((MPEGLAYER3WAVEFORMAT
*)(mux_a
->wf
))->wID
=1;
158 ((MPEGLAYER3WAVEFORMAT
*)(mux_a
->wf
))->fdwFlags
=2;
159 ((MPEGLAYER3WAVEFORMAT
*)(mux_a
->wf
))->nBlockSize
=encoder
->params
.samples_per_frame
; // ???
160 ((MPEGLAYER3WAVEFORMAT
*)(mux_a
->wf
))->nFramesPerBlock
=1;
161 ((MPEGLAYER3WAVEFORMAT
*)(mux_a
->wf
))->nCodecDelay
=0;
163 encoder
->input_format
= AF_FORMAT_S16_NE
;
164 encoder
->min_buffer_size
= 4608;
165 encoder
->max_buffer_size
= mux_a
->h
.dwRate
* mux_a
->wf
->nChannels
* 2;
170 #define min(a, b) ((a) <= (b) ? (a) : (b))
172 static int get_frame_size(audio_encoder_t
*encoder
)
175 if(encoder
->stream
->buffer_len
< 4)
177 sz
= mp_decode_mp3_header(encoder
->stream
->buffer
);
183 static int encode_lame(audio_encoder_t
*encoder
, uint8_t *dest
, void *src
, int len
, int max_size
)
186 if(encoder
->params
.channels
== 1)
187 n
= lame_encode_buffer(lame
, (short *)src
, (short *)src
, len
/2, dest
, max_size
);
189 n
= lame_encode_buffer_interleaved(lame
,(short *)src
, len
/4, dest
, max_size
);
191 return n
< 0 ? 0 : n
;
195 static int close_lame(audio_encoder_t
*encoder
)
200 static void fixup(audio_encoder_t
*encoder
)
202 // fixup CBR mp3 audio header:
203 if(!lame_param_vbr
) {
204 encoder
->stream
->h
.dwSampleSize
=1;
205 if (encoder
->stream
->h
.dwLength
)
206 ((MPEGLAYER3WAVEFORMAT
*)(encoder
->stream
->wf
))->nBlockSize
=
207 (encoder
->stream
->size
+(encoder
->stream
->h
.dwLength
>>1))/encoder
->stream
->h
.dwLength
;
208 encoder
->stream
->h
.dwLength
=encoder
->stream
->size
;
209 encoder
->stream
->h
.dwRate
=encoder
->stream
->wf
->nAvgBytesPerSec
;
210 encoder
->stream
->h
.dwScale
=1;
211 encoder
->stream
->wf
->nBlockAlign
=1;
212 mp_tmsg(MSGT_MENCODER
, MSGL_V
, "\n\nCBR audio: %d bytes/sec, %d bytes/block\n",
213 encoder
->stream
->h
.dwRate
,((MPEGLAYER3WAVEFORMAT
*)(encoder
->stream
->wf
))->nBlockSize
);
217 int mpae_init_lame(audio_encoder_t
*encoder
)
219 encoder
->params
.bitrate
= lame_param_br
* 125;
220 encoder
->params
.samples_per_frame
= encoder
->params
.sample_rate
< 32000 ? 576 : 1152;
221 encoder
->decode_buffer_size
= 2304;
224 lame_set_bWriteVbrTag(lame
,0);
225 lame_set_in_samplerate(lame
,encoder
->params
.sample_rate
);
226 //lame_set_in_samplerate(lame,sh_audio->samplerate); // if resampling done by lame
227 lame_set_num_channels(lame
,encoder
->params
.channels
);
228 lame_set_out_samplerate(lame
,encoder
->params
.sample_rate
);
229 lame_set_quality(lame
,lame_param_algqual
); // 0 = best q
230 if(lame_param_free_format
) lame_set_free_format(lame
,1);
231 if(lame_param_vbr
){ // VBR:
232 lame_set_VBR(lame
,lame_param_vbr
); // vbr mode
233 lame_set_VBR_q(lame
,lame_param_quality
); // 0 = best vbr q 5=~128k
234 if(lame_param_br
>0) lame_set_VBR_mean_bitrate_kbps(lame
,lame_param_br
);
235 if(lame_param_br_min
>0) lame_set_VBR_min_bitrate_kbps(lame
,lame_param_br_min
);
236 if(lame_param_br_max
>0) lame_set_VBR_max_bitrate_kbps(lame
,lame_param_br_max
);
238 if(lame_param_br
>0) lame_set_brate(lame
,lame_param_br
);
240 if(lame_param_mode
>=0) lame_set_mode(lame
,lame_param_mode
); // j-st
241 if(lame_param_ratio
>0) lame_set_compression_ratio(lame
,lame_param_ratio
);
242 if(lame_param_scale
>0) {
243 mp_tmsg(MSGT_MENCODER
, MSGL_V
, "Setting audio input gain to %f.\n", lame_param_scale
);
244 lame_set_scale(lame
,lame_param_scale
);
246 if(lame_param_lowpassfreq
>=-1) lame_set_lowpassfreq(lame
,lame_param_lowpassfreq
);
247 if(lame_param_highpassfreq
>=-1) lame_set_highpassfreq(lame
,lame_param_highpassfreq
);
248 #ifdef CONFIG_MP3LAME_PRESET
249 if(lame_param_preset
!= NULL
) {
250 mp_tmsg(MSGT_MENCODER
, MSGL_V
, "\npreset=%s\n\n",lame_param_preset
);
251 if(lame_presets_set(lame
,lame_param_fast
, (lame_param_vbr
==0), lame_param_preset
) < 0)
255 if(lame_init_params(lame
) == -1) {
256 mp_tmsg(MSGT_MENCODER
, MSGL_FATAL
,
257 "Cannot set LAME options, check bitrate/samplerate, some very low bitrates\n"\
258 "(<32) need lower samplerates (i.e. -srate 8000).\n"\
259 "If everything else fails, try a preset.");
262 if( mp_msg_test(MSGT_MENCODER
,MSGL_V
) ) {
263 lame_print_config(lame
);
264 lame_print_internals(lame
);
267 encoder
->bind
= bind_lame
;
268 encoder
->get_frame_size
= get_frame_size
;
269 encoder
->encode
= encode_lame
;
270 encoder
->fixup
= fixup
;
271 encoder
->close
= close_lame
;
275 #ifdef CONFIG_MP3LAME_PRESET
277 taken out of presets_set in lame-3.93.1/frontend/parse.c and modified */
278 static int lame_presets_set( lame_t gfp
, int fast
, int cbr
, const char* preset_name
)
282 if (strcmp(preset_name
, "help") == 0) {
283 mp_tmsg(MSGT_MENCODER
, MSGL_FATAL
, "LAME version %s (%s)\n\n", get_lame_version(), get_lame_url());
285 #define LamePresetsLongInfo _("\n"\
286 "The preset switches are designed to provide the highest possible quality.\n"\
288 "They have for the most part been subjected to and tuned via rigorous double\n"\
289 "blind listening tests to verify and achieve this objective.\n"\
291 "These are continually updated to coincide with the latest developments that\n"\
292 "occur and as a result should provide you with nearly the best quality\n"\
293 "currently possible from LAME.\n"\
295 "To activate these presets:\n"\
297 " For VBR modes (generally highest quality):\n"\
299 " \"preset=standard\" This preset should generally be transparent\n"\
300 " to most people on most music and is already\n"\
301 " quite high in quality.\n"\
303 " \"preset=extreme\" If you have extremely good hearing and similar\n"\
304 " equipment, this preset will generally provide\n"\
305 " slightly higher quality than the \"standard\"\n"\
308 " For CBR 320kbps (highest quality possible from the preset switches):\n"\
310 " \"preset=insane\" This preset will usually be overkill for most\n"\
311 " people and most situations, but if you must\n"\
312 " have the absolute highest quality with no\n"\
313 " regard to filesize, this is the way to go.\n"\
315 " For ABR modes (high quality per given bitrate but not as high as VBR):\n"\
317 " \"preset=<kbps>\" Using this preset will usually give you good\n"\
318 " quality at a specified bitrate. Depending on the\n"\
319 " bitrate entered, this preset will determine the\n"\
320 " optimal settings for that particular situation.\n"\
321 " While this approach works, it is not nearly as\n"\
322 " flexible as VBR, and usually will not attain the\n"\
323 " same level of quality as VBR at higher bitrates.\n"\
325 "The following options are also available for the corresponding profiles:\n"\
327 " <fast> standard\n"\
330 " <cbr> (ABR Mode) - The ABR Mode is implied. To use it,\n"\
331 " simply specify a bitrate. For example:\n"\
332 " \"preset=185\" activates this\n"\
333 " preset and uses 185 as an average kbps.\n"\
335 " \"fast\" - Enables the new fast VBR for a particular profile. The\n"\
336 " disadvantage to the speed switch is that often times the\n"\
337 " bitrate will be slightly higher than with the normal mode\n"\
338 " and quality may be slightly lower also.\n"\
339 " Warning: with the current version fast presets might result in too\n"\
340 " high bitrate compared to regular presets.\n"\
342 " \"cbr\" - If you use the ABR mode (read above) with a significant\n"\
343 " bitrate such as 80, 96, 112, 128, 160, 192, 224, 256, 320,\n"\
344 " you can use the \"cbr\" option to force CBR mode encoding\n"\
345 " instead of the standard abr mode. ABR does provide higher\n"\
346 " quality but CBR may be useful in situations such as when\n"\
347 " streaming an MP3 over the internet may be important.\n"\
351 " \"-lameopts fast:preset=standard \"\n"\
352 " or \"-lameopts cbr:preset=192 \"\n"\
353 " or \"-lameopts preset=172 \"\n"\
354 " or \"-lameopts preset=extreme \"\n"\
357 "A few aliases are available for ABR mode:\n"\
358 "phone => 16kbps/mono phon+/lw/mw-eu/sw => 24kbps/mono\n"\
359 "mw-us => 40kbps/mono voice => 56kbps/mono\n"\
360 "fm/radio/tape => 112kbps hifi => 160kbps\n"\
361 "cd => 192kbps studio => 256kbps")
363 mp_tmsg(MSGT_MENCODER
, MSGL_FATAL
, LamePresetsLongInfo
);
367 //aliases for compatibility with old presets
369 if (strcmp(preset_name
, "phone") == 0) {
373 if ( (strcmp(preset_name
, "phon+") == 0) ||
374 (strcmp(preset_name
, "lw") == 0) ||
375 (strcmp(preset_name
, "mw-eu") == 0) ||
376 (strcmp(preset_name
, "sw") == 0)) {
380 if (strcmp(preset_name
, "mw-us") == 0) {
384 if (strcmp(preset_name
, "voice") == 0) {
388 if (strcmp(preset_name
, "fm") == 0) {
391 if ( (strcmp(preset_name
, "radio") == 0) ||
392 (strcmp(preset_name
, "tape") == 0)) {
395 if (strcmp(preset_name
, "hifi") == 0) {
398 if (strcmp(preset_name
, "cd") == 0) {
401 if (strcmp(preset_name
, "studio") == 0) {
405 #ifdef CONFIG_MP3LAME_PRESET_MEDIUM
406 if (strcmp(preset_name
, "medium") == 0) {
408 lame_set_preset(gfp
, MEDIUM_FAST
);
410 lame_set_preset(gfp
, MEDIUM
);
416 if (strcmp(preset_name
, "standard") == 0) {
418 lame_set_preset(gfp
, STANDARD_FAST
);
420 lame_set_preset(gfp
, STANDARD
);
425 else if (strcmp(preset_name
, "extreme") == 0){
427 lame_set_preset(gfp
, EXTREME_FAST
);
429 lame_set_preset(gfp
, EXTREME
);
434 else if (((strcmp(preset_name
, "insane") == 0) ||
435 (strcmp(preset_name
, "320" ) == 0)) && (fast
< 1)) {
437 lame_set_preset(gfp
, INSANE
);
442 // Generic ABR Preset
443 if (((atoi(preset_name
)) > 0) && (fast
< 1)) {
444 if ((atoi(preset_name
)) >= 8 && (atoi(preset_name
)) <= 320){
445 lame_set_preset(gfp
, atoi(preset_name
));
448 lame_set_VBR(gfp
, vbr_off
);
451 lame_set_mode(gfp
, MONO
);
458 mp_tmsg(MSGT_MENCODER
, MSGL_FATAL
, "LAME version %s (%s)\n\n", get_lame_version(), get_lame_url());
459 mp_tmsg(MSGT_MENCODER
, MSGL_FATAL
,
460 "Error: The bitrate specified is out of the valid range for this preset.\n"\
462 "When using this mode you must enter a value between \"8\" and \"320\".\n"\
464 "For further information try: \"-lameopts preset=help\"\n");
469 mp_tmsg(MSGT_MENCODER
, MSGL_FATAL
, "LAME version %s (%s)\n\n", get_lame_version(), get_lame_url());
470 #define InvalidLamePresetOptions _("Error: You did not enter a valid profile and/or options with preset.\n"\
472 "Available profiles are:\n"\
474 " <fast> standard\n"\
477 " <cbr> (ABR Mode) - The ABR Mode is implied. To use it,\n"\
478 " simply specify a bitrate. For example:\n"\
479 " \"preset=185\" activates this\n"\
480 " preset and uses 185 as an average kbps.\n"\
484 " \"-lameopts fast:preset=standard \"\n"\
485 " or \"-lameopts cbr:preset=192 \"\n"\
486 " or \"-lameopts preset=172 \"\n"\
487 " or \"-lameopts preset=extreme \"\n"\
489 "For further information try: \"-lameopts preset=help\"\n")
490 mp_tmsg(MSGT_MENCODER
, MSGL_FATAL
, InvalidLamePresetOptions
);