Mercurial > mplayer.hg
annotate libmpcodecs/ae_lame.c @ 30526:68e260df472b
Just use goto instead of reimplementing it badly with a do { } while (0) and
break.
author | reimar |
---|---|
date | Sun, 14 Feb 2010 11:17:12 +0000 |
parents | bbb6ebec87a0 |
children | 321e9ea69b9f |
rev | line source |
---|---|
30421
bbb6ebec87a0
Add missing license headers to all files in the libmpcodecs directory.
diego
parents:
29263
diff
changeset
|
1 /* |
bbb6ebec87a0
Add missing license headers to all files in the libmpcodecs directory.
diego
parents:
29263
diff
changeset
|
2 * This file is part of MPlayer. |
bbb6ebec87a0
Add missing license headers to all files in the libmpcodecs directory.
diego
parents:
29263
diff
changeset
|
3 * |
bbb6ebec87a0
Add missing license headers to all files in the libmpcodecs directory.
diego
parents:
29263
diff
changeset
|
4 * MPlayer is free software; you can redistribute it and/or modify |
bbb6ebec87a0
Add missing license headers to all files in the libmpcodecs directory.
diego
parents:
29263
diff
changeset
|
5 * it under the terms of the GNU General Public License as published by |
bbb6ebec87a0
Add missing license headers to all files in the libmpcodecs directory.
diego
parents:
29263
diff
changeset
|
6 * the Free Software Foundation; either version 2 of the License, or |
bbb6ebec87a0
Add missing license headers to all files in the libmpcodecs directory.
diego
parents:
29263
diff
changeset
|
7 * (at your option) any later version. |
bbb6ebec87a0
Add missing license headers to all files in the libmpcodecs directory.
diego
parents:
29263
diff
changeset
|
8 * |
bbb6ebec87a0
Add missing license headers to all files in the libmpcodecs directory.
diego
parents:
29263
diff
changeset
|
9 * MPlayer is distributed in the hope that it will be useful, |
bbb6ebec87a0
Add missing license headers to all files in the libmpcodecs directory.
diego
parents:
29263
diff
changeset
|
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of |
bbb6ebec87a0
Add missing license headers to all files in the libmpcodecs directory.
diego
parents:
29263
diff
changeset
|
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
bbb6ebec87a0
Add missing license headers to all files in the libmpcodecs directory.
diego
parents:
29263
diff
changeset
|
12 * GNU General Public License for more details. |
bbb6ebec87a0
Add missing license headers to all files in the libmpcodecs directory.
diego
parents:
29263
diff
changeset
|
13 * |
bbb6ebec87a0
Add missing license headers to all files in the libmpcodecs directory.
diego
parents:
29263
diff
changeset
|
14 * You should have received a copy of the GNU General Public License along |
bbb6ebec87a0
Add missing license headers to all files in the libmpcodecs directory.
diego
parents:
29263
diff
changeset
|
15 * with MPlayer; if not, write to the Free Software Foundation, Inc., |
bbb6ebec87a0
Add missing license headers to all files in the libmpcodecs directory.
diego
parents:
29263
diff
changeset
|
16 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. |
bbb6ebec87a0
Add missing license headers to all files in the libmpcodecs directory.
diego
parents:
29263
diff
changeset
|
17 */ |
bbb6ebec87a0
Add missing license headers to all files in the libmpcodecs directory.
diego
parents:
29263
diff
changeset
|
18 |
15234 | 19 #include <stdio.h> |
20 #include <stdlib.h> | |
21 #include <inttypes.h> | |
15238 | 22 #include <unistd.h> |
15234 | 23 #include <string.h> |
15240 | 24 #include <sys/types.h> |
15234 | 25 #include "m_option.h" |
17012 | 26 #include "mp_msg.h" |
22601
ed8f90096c65
Add explicit location for headers from the libmpdemux/ directory.
diego
parents:
22600
diff
changeset
|
27 #include "libmpdemux/aviheader.h" |
ed8f90096c65
Add explicit location for headers from the libmpdemux/ directory.
diego
parents:
22600
diff
changeset
|
28 #include "libmpdemux/ms_hdr.h" |
22600
3c2b4a866c6a
Add explicit location for headers from the stream/ directory.
diego
parents:
21660
diff
changeset
|
29 #include "stream/stream.h" |
22601
ed8f90096c65
Add explicit location for headers from the libmpdemux/ directory.
diego
parents:
22600
diff
changeset
|
30 #include "libmpdemux/muxer.h" |
17012 | 31 #include "help_mp.h" |
15234 | 32 #include "ae_pcm.h" |
17012 | 33 #include "libaf/af_format.h" |
34 #include "libmpdemux/mp3_hdr.h" | |
15234 | 35 |
36 #undef CDECL | |
37 #include <lame/lame.h> | |
38 | |
39 lame_global_flags *lame; | |
40 static int lame_param_quality=0; // best | |
41 static int lame_param_algqual=5; // same as old default | |
42 static int lame_param_vbr=vbr_default; | |
43 static int lame_param_mode=-1; // unset | |
44 static int lame_param_padding=-1; // unset | |
45 static int lame_param_br=-1; // unset | |
46 static int lame_param_ratio=-1; // unset | |
47 static float lame_param_scale=-1; // unset | |
48 static int lame_param_lowpassfreq = 0; //auto | |
49 static int lame_param_highpassfreq = 0; //auto | |
50 static int lame_param_free_format = 0; //disabled | |
51 static int lame_param_br_min = 0; //not specified | |
52 static int lame_param_br_max = 0; //not specified | |
53 | |
27363
40057010b1fa
Change a bunch of codec-specific preprocessor directives from a HAVE_
diego
parents:
26754
diff
changeset
|
54 #ifdef CONFIG_MP3LAME_PRESET |
15234 | 55 int lame_param_fast=0; // unset |
56 static char* lame_param_preset=NULL; // unset | |
57 static int lame_presets_set( lame_t gfp, int fast, int cbr, const char* preset_name ); | |
58 #endif | |
59 | |
60 | |
61 m_option_t lameopts_conf[]={ | |
62 {"q", &lame_param_quality, CONF_TYPE_INT, CONF_RANGE, 0, 9, NULL}, | |
63 {"aq", &lame_param_algqual, CONF_TYPE_INT, CONF_RANGE, 0, 9, NULL}, | |
64 {"vbr", &lame_param_vbr, CONF_TYPE_INT, CONF_RANGE, 0, vbr_max_indicator, NULL}, | |
65 {"cbr", &lame_param_vbr, CONF_TYPE_FLAG, 0, 0, 0, NULL}, | |
66 {"abr", &lame_param_vbr, CONF_TYPE_FLAG, 0, 0, vbr_abr, NULL}, | |
67 {"mode", &lame_param_mode, CONF_TYPE_INT, CONF_RANGE, 0, MAX_INDICATOR, NULL}, | |
68 {"padding", &lame_param_padding, CONF_TYPE_INT, CONF_RANGE, 0, PAD_MAX_INDICATOR, NULL}, | |
69 {"br", &lame_param_br, CONF_TYPE_INT, CONF_RANGE, 0, 1024, NULL}, | |
70 {"ratio", &lame_param_ratio, CONF_TYPE_INT, CONF_RANGE, 0, 100, NULL}, | |
71 {"vol", &lame_param_scale, CONF_TYPE_FLOAT, CONF_RANGE, 0, 10, NULL}, | |
72 {"lowpassfreq",&lame_param_lowpassfreq, CONF_TYPE_INT, CONF_RANGE, -1, 48000,0}, | |
73 {"highpassfreq",&lame_param_highpassfreq, CONF_TYPE_INT, CONF_RANGE, -1, 48000,0}, | |
74 {"nofree", &lame_param_free_format, CONF_TYPE_FLAG, 0, 0, 0, NULL}, | |
75 {"free", &lame_param_free_format, CONF_TYPE_FLAG, 0, 0, 1, NULL}, | |
76 {"br_min", &lame_param_br_min, CONF_TYPE_INT, CONF_RANGE, 0, 1024, NULL}, | |
77 {"br_max", &lame_param_br_max, CONF_TYPE_INT, CONF_RANGE, 0, 1024, NULL}, | |
27363
40057010b1fa
Change a bunch of codec-specific preprocessor directives from a HAVE_
diego
parents:
26754
diff
changeset
|
78 #ifdef CONFIG_MP3LAME_PRESET |
15234 | 79 {"fast", &lame_param_fast, CONF_TYPE_FLAG, 0, 0, 1, NULL}, |
80 {"preset", &lame_param_preset, CONF_TYPE_STRING, 0, 0, 0, NULL}, | |
81 #else | |
82 {"fast", "MPlayer was built without -lameopts fast support (requires libmp3lame >=3.92).\n", CONF_TYPE_PRINT, CONF_NOCFG, 0, 0, NULL}, | |
83 {"preset", "MPlayer was built without -lameopts preset support (requires libmp3lame >=3.92).\n", CONF_TYPE_PRINT, CONF_NOCFG, 0, 0, NULL}, | |
84 #endif | |
85 {"help", MSGTR_MEncoderMP3LameHelp, CONF_TYPE_PRINT, CONF_NOCFG, 0, 0, NULL}, | |
86 {NULL, NULL, 0, 0, 0, 0, NULL} | |
87 }; | |
88 | |
89 | |
90 static int bind_lame(audio_encoder_t *encoder, muxer_stream_t *mux_a) | |
91 { | |
21621 | 92 mp_msg(MSGT_MENCODER, MSGL_INFO, MSGTR_MP3AudioSelected); |
15234 | 93 mux_a->h.dwSampleSize=0; // VBR |
94 mux_a->h.dwRate=encoder->params.sample_rate; | |
95 mux_a->h.dwScale=encoder->params.samples_per_frame; // samples/frame | |
96 if(sizeof(MPEGLAYER3WAVEFORMAT)!=30) mp_msg(MSGT_MENCODER,MSGL_WARN,MSGTR_MP3WaveFormatSizeNot30,sizeof(MPEGLAYER3WAVEFORMAT)); | |
97 mux_a->wf=malloc(sizeof(MPEGLAYER3WAVEFORMAT)); // should be 30 | |
98 mux_a->wf->wFormatTag=0x55; // MP3 | |
99 mux_a->wf->nChannels= (lame_param_mode<0) ? encoder->params.channels : ((lame_param_mode==3) ? 1 : 2); | |
100 mux_a->wf->nSamplesPerSec=mux_a->h.dwRate; | |
101 if(! lame_param_vbr) | |
102 mux_a->wf->nAvgBytesPerSec=lame_param_br * 125; | |
103 else | |
104 mux_a->wf->nAvgBytesPerSec=192000/8; // FIXME! | |
105 mux_a->wf->nBlockAlign=encoder->params.samples_per_frame; // required for l3codeca.acm + WMP 6.4 | |
106 mux_a->wf->wBitsPerSample=0; //16; | |
107 // from NaNdub: (requires for l3codeca.acm) | |
108 mux_a->wf->cbSize=12; | |
109 ((MPEGLAYER3WAVEFORMAT*)(mux_a->wf))->wID=1; | |
110 ((MPEGLAYER3WAVEFORMAT*)(mux_a->wf))->fdwFlags=2; | |
111 ((MPEGLAYER3WAVEFORMAT*)(mux_a->wf))->nBlockSize=encoder->params.samples_per_frame; // ??? | |
112 ((MPEGLAYER3WAVEFORMAT*)(mux_a->wf))->nFramesPerBlock=1; | |
113 ((MPEGLAYER3WAVEFORMAT*)(mux_a->wf))->nCodecDelay=0; | |
29263
0f1b5b68af32
whitespace cosmetics: Remove all trailing whitespace.
diego
parents:
28347
diff
changeset
|
114 |
19397
0074c9c77a0e
Set encoder's input format to "native endiannes", instead of little endian.
gpoirier
parents:
17932
diff
changeset
|
115 encoder->input_format = AF_FORMAT_S16_NE; |
15234 | 116 encoder->min_buffer_size = 4608; |
117 encoder->max_buffer_size = mux_a->h.dwRate * mux_a->wf->nChannels * 2; | |
29263
0f1b5b68af32
whitespace cosmetics: Remove all trailing whitespace.
diego
parents:
28347
diff
changeset
|
118 |
15234 | 119 return 1; |
120 } | |
121 | |
122 #define min(a, b) ((a) <= (b) ? (a) : (b)) | |
123 | |
124 static int get_frame_size(audio_encoder_t *encoder) | |
125 { | |
126 int sz; | |
127 if(encoder->stream->buffer_len < 4) | |
128 return 0; | |
129 sz = mp_decode_mp3_header(encoder->stream->buffer); | |
130 if(sz <= 0) | |
131 return 0; | |
132 return sz; | |
133 } | |
134 | |
135 static int encode_lame(audio_encoder_t *encoder, uint8_t *dest, void *src, int len, int max_size) | |
136 { | |
137 int n = 0; | |
138 if(encoder->params.channels == 1) | |
139 n = lame_encode_buffer(lame, (short *)src, (short *)src, len/2, dest, max_size); | |
140 else | |
141 n = lame_encode_buffer_interleaved(lame,(short *)src, len/4, dest, max_size); | |
142 | |
26754
63630c09e237
cosmetics: Remove pointless parentheses from return calls.
diego
parents:
25503
diff
changeset
|
143 return n < 0 ? 0 : n; |
15234 | 144 } |
145 | |
146 | |
147 static int close_lame(audio_encoder_t *encoder) | |
148 { | |
149 return 1; | |
150 } | |
151 | |
152 static void fixup(audio_encoder_t *encoder) | |
153 { | |
154 // fixup CBR mp3 audio header: | |
155 if(!lame_param_vbr) { | |
156 encoder->stream->h.dwSampleSize=1; | |
28347
1f4b3aaefcd8
Avoid a division by 0 when using -oac mp3lame but no audio data actually is encoded.
reimar
parents:
27363
diff
changeset
|
157 if (encoder->stream->h.dwLength) |
15234 | 158 ((MPEGLAYER3WAVEFORMAT*)(encoder->stream->wf))->nBlockSize= |
159 (encoder->stream->size+(encoder->stream->h.dwLength>>1))/encoder->stream->h.dwLength; | |
160 encoder->stream->h.dwLength=encoder->stream->size; | |
161 encoder->stream->h.dwRate=encoder->stream->wf->nAvgBytesPerSec; | |
162 encoder->stream->h.dwScale=1; | |
163 encoder->stream->wf->nBlockAlign=1; | |
21621 | 164 mp_msg(MSGT_MENCODER, MSGL_V, MSGTR_CBRAudioByterate, |
15234 | 165 encoder->stream->h.dwRate,((MPEGLAYER3WAVEFORMAT*)(encoder->stream->wf))->nBlockSize); |
166 } | |
167 } | |
168 | |
169 int mpae_init_lame(audio_encoder_t *encoder) | |
170 { | |
171 encoder->params.bitrate = lame_param_br * 125; | |
172 encoder->params.samples_per_frame = encoder->params.sample_rate < 32000 ? 576 : 1152; | |
173 encoder->decode_buffer_size = 2304; | |
174 | |
175 lame=lame_init(); | |
176 lame_set_bWriteVbrTag(lame,0); | |
177 lame_set_in_samplerate(lame,encoder->params.sample_rate); | |
178 //lame_set_in_samplerate(lame,sh_audio->samplerate); // if resampling done by lame | |
179 lame_set_num_channels(lame,encoder->params.channels); | |
180 lame_set_out_samplerate(lame,encoder->params.sample_rate); | |
181 lame_set_quality(lame,lame_param_algqual); // 0 = best q | |
182 if(lame_param_free_format) lame_set_free_format(lame,1); | |
183 if(lame_param_vbr){ // VBR: | |
184 lame_set_VBR(lame,lame_param_vbr); // vbr mode | |
185 lame_set_VBR_q(lame,lame_param_quality); // 0 = best vbr q 5=~128k | |
186 if(lame_param_br>0) lame_set_VBR_mean_bitrate_kbps(lame,lame_param_br); | |
187 if(lame_param_br_min>0) lame_set_VBR_min_bitrate_kbps(lame,lame_param_br_min); | |
188 if(lame_param_br_max>0) lame_set_VBR_max_bitrate_kbps(lame,lame_param_br_max); | |
189 } else { // CBR: | |
190 if(lame_param_br>0) lame_set_brate(lame,lame_param_br); | |
191 } | |
192 if(lame_param_mode>=0) lame_set_mode(lame,lame_param_mode); // j-st | |
193 if(lame_param_ratio>0) lame_set_compression_ratio(lame,lame_param_ratio); | |
194 if(lame_param_scale>0) { | |
21621 | 195 mp_msg(MSGT_MENCODER, MSGL_V, MSGTR_SettingAudioInputGain, lame_param_scale); |
15234 | 196 lame_set_scale(lame,lame_param_scale); |
197 } | |
198 if(lame_param_lowpassfreq>=-1) lame_set_lowpassfreq(lame,lame_param_lowpassfreq); | |
199 if(lame_param_highpassfreq>=-1) lame_set_highpassfreq(lame,lame_param_highpassfreq); | |
27363
40057010b1fa
Change a bunch of codec-specific preprocessor directives from a HAVE_
diego
parents:
26754
diff
changeset
|
200 #ifdef CONFIG_MP3LAME_PRESET |
15234 | 201 if(lame_param_preset != NULL) { |
21621 | 202 mp_msg(MSGT_MENCODER, MSGL_V, MSGTR_LamePresetEquals,lame_param_preset); |
15234 | 203 if(lame_presets_set(lame,lame_param_fast, (lame_param_vbr==0), lame_param_preset) < 0) |
204 return 0; | |
205 } | |
206 #endif | |
207 if(lame_init_params(lame) == -1) { | |
29263
0f1b5b68af32
whitespace cosmetics: Remove all trailing whitespace.
diego
parents:
28347
diff
changeset
|
208 mp_msg(MSGT_MENCODER, MSGL_FATAL, MSGTR_LameCantInit); |
15234 | 209 return 0; |
210 } | |
17932 | 211 if( mp_msg_test(MSGT_MENCODER,MSGL_V) ) { |
15234 | 212 lame_print_config(lame); |
213 lame_print_internals(lame); | |
214 } | |
29263
0f1b5b68af32
whitespace cosmetics: Remove all trailing whitespace.
diego
parents:
28347
diff
changeset
|
215 |
15234 | 216 encoder->bind = bind_lame; |
217 encoder->get_frame_size = get_frame_size; | |
218 encoder->encode = encode_lame; | |
219 encoder->fixup = fixup; | |
220 encoder->close = close_lame; | |
221 return 1; | |
222 } | |
223 | |
27363
40057010b1fa
Change a bunch of codec-specific preprocessor directives from a HAVE_
diego
parents:
26754
diff
changeset
|
224 #ifdef CONFIG_MP3LAME_PRESET |
29263
0f1b5b68af32
whitespace cosmetics: Remove all trailing whitespace.
diego
parents:
28347
diff
changeset
|
225 /* lame_presets_set |
15234 | 226 taken out of presets_set in lame-3.93.1/frontend/parse.c and modified */ |
227 static int lame_presets_set( lame_t gfp, int fast, int cbr, const char* preset_name ) | |
228 { | |
229 int mono = 0; | |
230 | |
231 if (strcmp(preset_name, "help") == 0) { | |
21621 | 232 mp_msg(MSGT_MENCODER, MSGL_FATAL, MSGTR_LameVersion, get_lame_version(), get_lame_url()); |
21622
5cd2a0041019
Simplify: get rid of useless lame_presets_longinfo_dm function.
reimar
parents:
21621
diff
changeset
|
233 mp_msg(MSGT_MENCODER, MSGL_FATAL, MSGTR_LamePresetsLongInfo); |
15234 | 234 return -1; |
235 } | |
236 | |
237 //aliases for compatibility with old presets | |
238 | |
239 if (strcmp(preset_name, "phone") == 0) { | |
240 preset_name = "16"; | |
241 mono = 1; | |
242 } | |
243 if ( (strcmp(preset_name, "phon+") == 0) || | |
244 (strcmp(preset_name, "lw") == 0) || | |
245 (strcmp(preset_name, "mw-eu") == 0) || | |
246 (strcmp(preset_name, "sw") == 0)) { | |
247 preset_name = "24"; | |
248 mono = 1; | |
249 } | |
250 if (strcmp(preset_name, "mw-us") == 0) { | |
251 preset_name = "40"; | |
252 mono = 1; | |
253 } | |
254 if (strcmp(preset_name, "voice") == 0) { | |
255 preset_name = "56"; | |
256 mono = 1; | |
257 } | |
258 if (strcmp(preset_name, "fm") == 0) { | |
259 preset_name = "112"; | |
260 } | |
261 if ( (strcmp(preset_name, "radio") == 0) || | |
262 (strcmp(preset_name, "tape") == 0)) { | |
263 preset_name = "112"; | |
264 } | |
265 if (strcmp(preset_name, "hifi") == 0) { | |
266 preset_name = "160"; | |
267 } | |
268 if (strcmp(preset_name, "cd") == 0) { | |
269 preset_name = "192"; | |
270 } | |
271 if (strcmp(preset_name, "studio") == 0) { | |
272 preset_name = "256"; | |
273 } | |
274 | |
27363
40057010b1fa
Change a bunch of codec-specific preprocessor directives from a HAVE_
diego
parents:
26754
diff
changeset
|
275 #ifdef CONFIG_MP3LAME_PRESET_MEDIUM |
15234 | 276 if (strcmp(preset_name, "medium") == 0) { |
277 if (fast > 0) | |
278 lame_set_preset(gfp, MEDIUM_FAST); | |
279 else | |
280 lame_set_preset(gfp, MEDIUM); | |
281 | |
282 return 0; | |
283 } | |
284 #endif | |
29263
0f1b5b68af32
whitespace cosmetics: Remove all trailing whitespace.
diego
parents:
28347
diff
changeset
|
285 |
15234 | 286 if (strcmp(preset_name, "standard") == 0) { |
287 if (fast > 0) | |
288 lame_set_preset(gfp, STANDARD_FAST); | |
289 else | |
290 lame_set_preset(gfp, STANDARD); | |
291 | |
292 return 0; | |
293 } | |
29263
0f1b5b68af32
whitespace cosmetics: Remove all trailing whitespace.
diego
parents:
28347
diff
changeset
|
294 |
15234 | 295 else if (strcmp(preset_name, "extreme") == 0){ |
296 if (fast > 0) | |
297 lame_set_preset(gfp, EXTREME_FAST); | |
298 else | |
299 lame_set_preset(gfp, EXTREME); | |
300 | |
301 return 0; | |
302 } | |
29263
0f1b5b68af32
whitespace cosmetics: Remove all trailing whitespace.
diego
parents:
28347
diff
changeset
|
303 |
0f1b5b68af32
whitespace cosmetics: Remove all trailing whitespace.
diego
parents:
28347
diff
changeset
|
304 else if (((strcmp(preset_name, "insane") == 0) || |
15234 | 305 (strcmp(preset_name, "320" ) == 0)) && (fast < 1)) { |
306 | |
307 lame_set_preset(gfp, INSANE); | |
29263
0f1b5b68af32
whitespace cosmetics: Remove all trailing whitespace.
diego
parents:
28347
diff
changeset
|
308 |
15234 | 309 return 0; |
310 } | |
311 | |
312 // Generic ABR Preset | |
313 if (((atoi(preset_name)) > 0) && (fast < 1)) { | |
314 if ((atoi(preset_name)) >= 8 && (atoi(preset_name)) <= 320){ | |
315 lame_set_preset(gfp, atoi(preset_name)); | |
316 | |
317 if (cbr == 1 ) | |
318 lame_set_VBR(gfp, vbr_off); | |
319 | |
320 if (mono == 1 ) { | |
321 lame_set_mode(gfp, MONO); | |
322 } | |
323 | |
324 return 0; | |
325 | |
326 } | |
327 else { | |
21621 | 328 mp_msg(MSGT_MENCODER, MSGL_FATAL, MSGTR_LameVersion, get_lame_version(), get_lame_url()); |
329 mp_msg(MSGT_MENCODER, MSGL_FATAL, MSGTR_InvalidBitrateForLamePreset); | |
15234 | 330 return -1; |
331 } | |
332 } | |
333 | |
21621 | 334 mp_msg(MSGT_MENCODER, MSGL_FATAL, MSGTR_LameVersion, get_lame_version(), get_lame_url()); |
335 mp_msg(MSGT_MENCODER, MSGL_FATAL, MSGTR_InvalidLamePresetOptions); | |
15234 | 336 return -1; |
337 } | |
338 #endif |