Merge remote branch 'mplayer/master'
[mplayer/glamo.git] / libmpcodecs / ae_lame.c
blobb719ce3865b13ffd59896d226014b300ee0655ca
1 #include <stdio.h>
2 #include <stdlib.h>
3 #include <inttypes.h>
4 #include <unistd.h>
5 #include <string.h>
6 #include <sys/types.h>
7 #include "m_option.h"
8 #include "mp_msg.h"
9 #include "libmpdemux/aviheader.h"
10 #include "libmpdemux/ms_hdr.h"
11 #include "stream/stream.h"
12 #include "libmpdemux/muxer.h"
13 #include "help_mp.h"
14 #include "ae_pcm.h"
15 #include "libaf/af_format.h"
16 #include "libmpdemux/mp3_hdr.h"
18 #undef CDECL
19 #include <lame/lame.h>
21 lame_global_flags *lame;
22 static int lame_param_quality=0; // best
23 static int lame_param_algqual=5; // same as old default
24 static int lame_param_vbr=vbr_default;
25 static int lame_param_mode=-1; // unset
26 static int lame_param_padding=-1; // unset
27 static int lame_param_br=-1; // unset
28 static int lame_param_ratio=-1; // unset
29 static float lame_param_scale=-1; // unset
30 static int lame_param_lowpassfreq = 0; //auto
31 static int lame_param_highpassfreq = 0; //auto
32 static int lame_param_free_format = 0; //disabled
33 static int lame_param_br_min = 0; //not specified
34 static int lame_param_br_max = 0; //not specified
36 #ifdef CONFIG_MP3LAME_PRESET
37 int lame_param_fast=0; // unset
38 static char* lame_param_preset=NULL; // unset
39 static int lame_presets_set( lame_t gfp, int fast, int cbr, const char* preset_name );
40 #endif
42 #define MEncoderMP3LameHelp _("\n\n"\
43 " vbr=<0-4> variable bitrate method\n"\
44 " 0: cbr (constant bitrate)\n"\
45 " 1: mt (Mark Taylor VBR algorithm)\n"\
46 " 2: rh (Robert Hegemann VBR algorithm - default)\n"\
47 " 3: abr (average bitrate)\n"\
48 " 4: mtrh (Mark Taylor Robert Hegemann VBR algorithm)\n"\
49 "\n"\
50 " abr average bitrate\n"\
51 "\n"\
52 " cbr constant bitrate\n"\
53 " Also forces CBR mode encoding on subsequent ABR presets modes.\n"\
54 "\n"\
55 " br=<0-1024> specify bitrate in kBit (CBR and ABR only)\n"\
56 "\n"\
57 " q=<0-9> quality (0-highest, 9-lowest) (only for VBR)\n"\
58 "\n"\
59 " aq=<0-9> algorithmic quality (0-best/slowest, 9-worst/fastest)\n"\
60 "\n"\
61 " ratio=<1-100> compression ratio\n"\
62 "\n"\
63 " vol=<0-10> set audio input gain\n"\
64 "\n"\
65 " mode=<0-3> (default: auto)\n"\
66 " 0: stereo\n"\
67 " 1: joint-stereo\n"\
68 " 2: dualchannel\n"\
69 " 3: mono\n"\
70 "\n"\
71 " padding=<0-2>\n"\
72 " 0: no\n"\
73 " 1: all\n"\
74 " 2: adjust\n"\
75 "\n"\
76 " fast Switch on faster encoding on subsequent VBR presets modes,\n"\
77 " slightly lower quality and higher bitrates.\n"\
78 "\n"\
79 " preset=<value> Provide the highest possible quality settings.\n"\
80 " medium: VBR encoding, good quality\n"\
81 " (150-180 kbps bitrate range)\n"\
82 " standard: VBR encoding, high quality\n"\
83 " (170-210 kbps bitrate range)\n"\
84 " extreme: VBR encoding, very high quality\n"\
85 " (200-240 kbps bitrate range)\n"\
86 " insane: CBR encoding, highest preset quality\n"\
87 " (320 kbps bitrate)\n"\
88 " <8-320>: ABR encoding at average given kbps bitrate.\n\n")
92 m_option_t lameopts_conf[]={
93 {"q", &lame_param_quality, CONF_TYPE_INT, CONF_RANGE, 0, 9, NULL},
94 {"aq", &lame_param_algqual, CONF_TYPE_INT, CONF_RANGE, 0, 9, NULL},
95 {"vbr", &lame_param_vbr, CONF_TYPE_INT, CONF_RANGE, 0, vbr_max_indicator, NULL},
96 {"cbr", &lame_param_vbr, CONF_TYPE_FLAG, 0, 0, 0, NULL},
97 {"abr", &lame_param_vbr, CONF_TYPE_FLAG, 0, 0, vbr_abr, NULL},
98 {"mode", &lame_param_mode, CONF_TYPE_INT, CONF_RANGE, 0, MAX_INDICATOR, NULL},
99 {"padding", &lame_param_padding, CONF_TYPE_INT, CONF_RANGE, 0, PAD_MAX_INDICATOR, NULL},
100 {"br", &lame_param_br, CONF_TYPE_INT, CONF_RANGE, 0, 1024, NULL},
101 {"ratio", &lame_param_ratio, CONF_TYPE_INT, CONF_RANGE, 0, 100, NULL},
102 {"vol", &lame_param_scale, CONF_TYPE_FLOAT, CONF_RANGE, 0, 10, NULL},
103 {"lowpassfreq",&lame_param_lowpassfreq, CONF_TYPE_INT, CONF_RANGE, -1, 48000,0},
104 {"highpassfreq",&lame_param_highpassfreq, CONF_TYPE_INT, CONF_RANGE, -1, 48000,0},
105 {"nofree", &lame_param_free_format, CONF_TYPE_FLAG, 0, 0, 0, NULL},
106 {"free", &lame_param_free_format, CONF_TYPE_FLAG, 0, 0, 1, NULL},
107 {"br_min", &lame_param_br_min, CONF_TYPE_INT, CONF_RANGE, 0, 1024, NULL},
108 {"br_max", &lame_param_br_max, CONF_TYPE_INT, CONF_RANGE, 0, 1024, NULL},
109 #ifdef CONFIG_MP3LAME_PRESET
110 {"fast", &lame_param_fast, CONF_TYPE_FLAG, 0, 0, 1, NULL},
111 {"preset", &lame_param_preset, CONF_TYPE_STRING, 0, 0, 0, NULL},
112 #else
113 {"fast", "MPlayer was built without -lameopts fast support (requires libmp3lame >=3.92).\n", CONF_TYPE_PRINT, CONF_NOCFG, 0, 0, NULL},
114 {"preset", "MPlayer was built without -lameopts preset support (requires libmp3lame >=3.92).\n", CONF_TYPE_PRINT, CONF_NOCFG, 0, 0, NULL},
115 #endif
116 {"help", MEncoderMP3LameHelp, CONF_TYPE_PRINT, CONF_NOCFG, 0, 0, NULL},
117 {NULL, NULL, 0, 0, 0, 0, NULL}
121 static int bind_lame(audio_encoder_t *encoder, muxer_stream_t *mux_a)
123 mp_tmsg(MSGT_MENCODER, MSGL_INFO, "MP3 audio selected.\n");
124 mux_a->h.dwSampleSize=0; // VBR
125 mux_a->h.dwRate=encoder->params.sample_rate;
126 mux_a->h.dwScale=encoder->params.samples_per_frame; // samples/frame
127 if(sizeof(MPEGLAYER3WAVEFORMAT)!=30) mp_tmsg(MSGT_MENCODER,MSGL_WARN,"sizeof(MPEGLAYER3WAVEFORMAT)==%d!=30, maybe broken C compiler?\n",sizeof(MPEGLAYER3WAVEFORMAT));
128 mux_a->wf=malloc(sizeof(MPEGLAYER3WAVEFORMAT)); // should be 30
129 mux_a->wf->wFormatTag=0x55; // MP3
130 mux_a->wf->nChannels= (lame_param_mode<0) ? encoder->params.channels : ((lame_param_mode==3) ? 1 : 2);
131 mux_a->wf->nSamplesPerSec=mux_a->h.dwRate;
132 if(! lame_param_vbr)
133 mux_a->wf->nAvgBytesPerSec=lame_param_br * 125;
134 else
135 mux_a->wf->nAvgBytesPerSec=192000/8; // FIXME!
136 mux_a->wf->nBlockAlign=encoder->params.samples_per_frame; // required for l3codeca.acm + WMP 6.4
137 mux_a->wf->wBitsPerSample=0; //16;
138 // from NaNdub: (requires for l3codeca.acm)
139 mux_a->wf->cbSize=12;
140 ((MPEGLAYER3WAVEFORMAT*)(mux_a->wf))->wID=1;
141 ((MPEGLAYER3WAVEFORMAT*)(mux_a->wf))->fdwFlags=2;
142 ((MPEGLAYER3WAVEFORMAT*)(mux_a->wf))->nBlockSize=encoder->params.samples_per_frame; // ???
143 ((MPEGLAYER3WAVEFORMAT*)(mux_a->wf))->nFramesPerBlock=1;
144 ((MPEGLAYER3WAVEFORMAT*)(mux_a->wf))->nCodecDelay=0;
146 encoder->input_format = AF_FORMAT_S16_NE;
147 encoder->min_buffer_size = 4608;
148 encoder->max_buffer_size = mux_a->h.dwRate * mux_a->wf->nChannels * 2;
150 return 1;
153 #define min(a, b) ((a) <= (b) ? (a) : (b))
155 static int get_frame_size(audio_encoder_t *encoder)
157 int sz;
158 if(encoder->stream->buffer_len < 4)
159 return 0;
160 sz = mp_decode_mp3_header(encoder->stream->buffer);
161 if(sz <= 0)
162 return 0;
163 return sz;
166 static int encode_lame(audio_encoder_t *encoder, uint8_t *dest, void *src, int len, int max_size)
168 int n = 0;
169 if(encoder->params.channels == 1)
170 n = lame_encode_buffer(lame, (short *)src, (short *)src, len/2, dest, max_size);
171 else
172 n = lame_encode_buffer_interleaved(lame,(short *)src, len/4, dest, max_size);
174 return n < 0 ? 0 : n;
178 static int close_lame(audio_encoder_t *encoder)
180 return 1;
183 static void fixup(audio_encoder_t *encoder)
185 // fixup CBR mp3 audio header:
186 if(!lame_param_vbr) {
187 encoder->stream->h.dwSampleSize=1;
188 if (encoder->stream->h.dwLength)
189 ((MPEGLAYER3WAVEFORMAT*)(encoder->stream->wf))->nBlockSize=
190 (encoder->stream->size+(encoder->stream->h.dwLength>>1))/encoder->stream->h.dwLength;
191 encoder->stream->h.dwLength=encoder->stream->size;
192 encoder->stream->h.dwRate=encoder->stream->wf->nAvgBytesPerSec;
193 encoder->stream->h.dwScale=1;
194 encoder->stream->wf->nBlockAlign=1;
195 mp_tmsg(MSGT_MENCODER, MSGL_V, "\n\nCBR audio: %d bytes/sec, %d bytes/block\n",
196 encoder->stream->h.dwRate,((MPEGLAYER3WAVEFORMAT*)(encoder->stream->wf))->nBlockSize);
200 int mpae_init_lame(audio_encoder_t *encoder)
202 encoder->params.bitrate = lame_param_br * 125;
203 encoder->params.samples_per_frame = encoder->params.sample_rate < 32000 ? 576 : 1152;
204 encoder->decode_buffer_size = 2304;
206 lame=lame_init();
207 lame_set_bWriteVbrTag(lame,0);
208 lame_set_in_samplerate(lame,encoder->params.sample_rate);
209 //lame_set_in_samplerate(lame,sh_audio->samplerate); // if resampling done by lame
210 lame_set_num_channels(lame,encoder->params.channels);
211 lame_set_out_samplerate(lame,encoder->params.sample_rate);
212 lame_set_quality(lame,lame_param_algqual); // 0 = best q
213 if(lame_param_free_format) lame_set_free_format(lame,1);
214 if(lame_param_vbr){ // VBR:
215 lame_set_VBR(lame,lame_param_vbr); // vbr mode
216 lame_set_VBR_q(lame,lame_param_quality); // 0 = best vbr q 5=~128k
217 if(lame_param_br>0) lame_set_VBR_mean_bitrate_kbps(lame,lame_param_br);
218 if(lame_param_br_min>0) lame_set_VBR_min_bitrate_kbps(lame,lame_param_br_min);
219 if(lame_param_br_max>0) lame_set_VBR_max_bitrate_kbps(lame,lame_param_br_max);
220 } else { // CBR:
221 if(lame_param_br>0) lame_set_brate(lame,lame_param_br);
223 if(lame_param_mode>=0) lame_set_mode(lame,lame_param_mode); // j-st
224 if(lame_param_ratio>0) lame_set_compression_ratio(lame,lame_param_ratio);
225 if(lame_param_scale>0) {
226 mp_tmsg(MSGT_MENCODER, MSGL_V, "Setting audio input gain to %f.\n", lame_param_scale);
227 lame_set_scale(lame,lame_param_scale);
229 if(lame_param_lowpassfreq>=-1) lame_set_lowpassfreq(lame,lame_param_lowpassfreq);
230 if(lame_param_highpassfreq>=-1) lame_set_highpassfreq(lame,lame_param_highpassfreq);
231 #ifdef CONFIG_MP3LAME_PRESET
232 if(lame_param_preset != NULL) {
233 mp_tmsg(MSGT_MENCODER, MSGL_V, "\npreset=%s\n\n",lame_param_preset);
234 if(lame_presets_set(lame,lame_param_fast, (lame_param_vbr==0), lame_param_preset) < 0)
235 return 0;
237 #endif
238 if(lame_init_params(lame) == -1) {
239 mp_tmsg(MSGT_MENCODER, MSGL_FATAL,
240 "Cannot set LAME options, check bitrate/samplerate, some very low bitrates\n"\
241 "(<32) need lower samplerates (i.e. -srate 8000).\n"\
242 "If everything else fails, try a preset.");
243 return 0;
245 if( mp_msg_test(MSGT_MENCODER,MSGL_V) ) {
246 lame_print_config(lame);
247 lame_print_internals(lame);
250 encoder->bind = bind_lame;
251 encoder->get_frame_size = get_frame_size;
252 encoder->encode = encode_lame;
253 encoder->fixup = fixup;
254 encoder->close = close_lame;
255 return 1;
258 #ifdef CONFIG_MP3LAME_PRESET
259 /* lame_presets_set
260 taken out of presets_set in lame-3.93.1/frontend/parse.c and modified */
261 static int lame_presets_set( lame_t gfp, int fast, int cbr, const char* preset_name )
263 int mono = 0;
265 if (strcmp(preset_name, "help") == 0) {
266 mp_tmsg(MSGT_MENCODER, MSGL_FATAL, "LAME version %s (%s)\n\n", get_lame_version(), get_lame_url());
268 #define LamePresetsLongInfo _("\n"\
269 "The preset switches are designed to provide the highest possible quality.\n"\
270 "\n"\
271 "They have for the most part been subjected to and tuned via rigorous double\n"\
272 "blind listening tests to verify and achieve this objective.\n"\
273 "\n"\
274 "These are continually updated to coincide with the latest developments that\n"\
275 "occur and as a result should provide you with nearly the best quality\n"\
276 "currently possible from LAME.\n"\
277 "\n"\
278 "To activate these presets:\n"\
279 "\n"\
280 " For VBR modes (generally highest quality):\n"\
281 "\n"\
282 " \"preset=standard\" This preset should generally be transparent\n"\
283 " to most people on most music and is already\n"\
284 " quite high in quality.\n"\
285 "\n"\
286 " \"preset=extreme\" If you have extremely good hearing and similar\n"\
287 " equipment, this preset will generally provide\n"\
288 " slightly higher quality than the \"standard\"\n"\
289 " mode.\n"\
290 "\n"\
291 " For CBR 320kbps (highest quality possible from the preset switches):\n"\
292 "\n"\
293 " \"preset=insane\" This preset will usually be overkill for most\n"\
294 " people and most situations, but if you must\n"\
295 " have the absolute highest quality with no\n"\
296 " regard to filesize, this is the way to go.\n"\
297 "\n"\
298 " For ABR modes (high quality per given bitrate but not as high as VBR):\n"\
299 "\n"\
300 " \"preset=<kbps>\" Using this preset will usually give you good\n"\
301 " quality at a specified bitrate. Depending on the\n"\
302 " bitrate entered, this preset will determine the\n"\
303 " optimal settings for that particular situation.\n"\
304 " While this approach works, it is not nearly as\n"\
305 " flexible as VBR, and usually will not attain the\n"\
306 " same level of quality as VBR at higher bitrates.\n"\
307 "\n"\
308 "The following options are also available for the corresponding profiles:\n"\
309 "\n"\
310 " <fast> standard\n"\
311 " <fast> extreme\n"\
312 " insane\n"\
313 " <cbr> (ABR Mode) - The ABR Mode is implied. To use it,\n"\
314 " simply specify a bitrate. For example:\n"\
315 " \"preset=185\" activates this\n"\
316 " preset and uses 185 as an average kbps.\n"\
317 "\n"\
318 " \"fast\" - Enables the new fast VBR for a particular profile. The\n"\
319 " disadvantage to the speed switch is that often times the\n"\
320 " bitrate will be slightly higher than with the normal mode\n"\
321 " and quality may be slightly lower also.\n"\
322 " Warning: with the current version fast presets might result in too\n"\
323 " high bitrate compared to regular presets.\n"\
324 "\n"\
325 " \"cbr\" - If you use the ABR mode (read above) with a significant\n"\
326 " bitrate such as 80, 96, 112, 128, 160, 192, 224, 256, 320,\n"\
327 " you can use the \"cbr\" option to force CBR mode encoding\n"\
328 " instead of the standard abr mode. ABR does provide higher\n"\
329 " quality but CBR may be useful in situations such as when\n"\
330 " streaming an MP3 over the internet may be important.\n"\
331 "\n"\
332 " For example:\n"\
333 "\n"\
334 " \"-lameopts fast:preset=standard \"\n"\
335 " or \"-lameopts cbr:preset=192 \"\n"\
336 " or \"-lameopts preset=172 \"\n"\
337 " or \"-lameopts preset=extreme \"\n"\
338 "\n"\
339 "\n"\
340 "A few aliases are available for ABR mode:\n"\
341 "phone => 16kbps/mono phon+/lw/mw-eu/sw => 24kbps/mono\n"\
342 "mw-us => 40kbps/mono voice => 56kbps/mono\n"\
343 "fm/radio/tape => 112kbps hifi => 160kbps\n"\
344 "cd => 192kbps studio => 256kbps")
346 mp_tmsg(MSGT_MENCODER, MSGL_FATAL, LamePresetsLongInfo);
347 return -1;
350 //aliases for compatibility with old presets
352 if (strcmp(preset_name, "phone") == 0) {
353 preset_name = "16";
354 mono = 1;
356 if ( (strcmp(preset_name, "phon+") == 0) ||
357 (strcmp(preset_name, "lw") == 0) ||
358 (strcmp(preset_name, "mw-eu") == 0) ||
359 (strcmp(preset_name, "sw") == 0)) {
360 preset_name = "24";
361 mono = 1;
363 if (strcmp(preset_name, "mw-us") == 0) {
364 preset_name = "40";
365 mono = 1;
367 if (strcmp(preset_name, "voice") == 0) {
368 preset_name = "56";
369 mono = 1;
371 if (strcmp(preset_name, "fm") == 0) {
372 preset_name = "112";
374 if ( (strcmp(preset_name, "radio") == 0) ||
375 (strcmp(preset_name, "tape") == 0)) {
376 preset_name = "112";
378 if (strcmp(preset_name, "hifi") == 0) {
379 preset_name = "160";
381 if (strcmp(preset_name, "cd") == 0) {
382 preset_name = "192";
384 if (strcmp(preset_name, "studio") == 0) {
385 preset_name = "256";
388 #ifdef CONFIG_MP3LAME_PRESET_MEDIUM
389 if (strcmp(preset_name, "medium") == 0) {
390 if (fast > 0)
391 lame_set_preset(gfp, MEDIUM_FAST);
392 else
393 lame_set_preset(gfp, MEDIUM);
395 return 0;
397 #endif
399 if (strcmp(preset_name, "standard") == 0) {
400 if (fast > 0)
401 lame_set_preset(gfp, STANDARD_FAST);
402 else
403 lame_set_preset(gfp, STANDARD);
405 return 0;
408 else if (strcmp(preset_name, "extreme") == 0){
409 if (fast > 0)
410 lame_set_preset(gfp, EXTREME_FAST);
411 else
412 lame_set_preset(gfp, EXTREME);
414 return 0;
417 else if (((strcmp(preset_name, "insane") == 0) ||
418 (strcmp(preset_name, "320" ) == 0)) && (fast < 1)) {
420 lame_set_preset(gfp, INSANE);
422 return 0;
425 // Generic ABR Preset
426 if (((atoi(preset_name)) > 0) && (fast < 1)) {
427 if ((atoi(preset_name)) >= 8 && (atoi(preset_name)) <= 320){
428 lame_set_preset(gfp, atoi(preset_name));
430 if (cbr == 1 )
431 lame_set_VBR(gfp, vbr_off);
433 if (mono == 1 ) {
434 lame_set_mode(gfp, MONO);
437 return 0;
440 else {
441 mp_tmsg(MSGT_MENCODER, MSGL_FATAL, "LAME version %s (%s)\n\n", get_lame_version(), get_lame_url());
442 mp_tmsg(MSGT_MENCODER, MSGL_FATAL,
443 "Error: The bitrate specified is out of the valid range for this preset.\n"\
444 "\n"\
445 "When using this mode you must enter a value between \"8\" and \"320\".\n"\
446 "\n"\
447 "For further information try: \"-lameopts preset=help\"\n");
448 return -1;
452 mp_tmsg(MSGT_MENCODER, MSGL_FATAL, "LAME version %s (%s)\n\n", get_lame_version(), get_lame_url());
453 #define InvalidLamePresetOptions _("Error: You did not enter a valid profile and/or options with preset.\n"\
454 "\n"\
455 "Available profiles are:\n"\
456 "\n"\
457 " <fast> standard\n"\
458 " <fast> extreme\n"\
459 " insane\n"\
460 " <cbr> (ABR Mode) - The ABR Mode is implied. To use it,\n"\
461 " simply specify a bitrate. For example:\n"\
462 " \"preset=185\" activates this\n"\
463 " preset and uses 185 as an average kbps.\n"\
464 "\n"\
465 " Some examples:\n"\
466 "\n"\
467 " \"-lameopts fast:preset=standard \"\n"\
468 " or \"-lameopts cbr:preset=192 \"\n"\
469 " or \"-lameopts preset=172 \"\n"\
470 " or \"-lameopts preset=extreme \"\n"\
471 "\n"\
472 "For further information try: \"-lameopts preset=help\"\n")
473 mp_tmsg(MSGT_MENCODER, MSGL_FATAL, InvalidLamePresetOptions);
474 return -1;
476 #endif