vo_gl: Some small whitespace and {} cosmetics
[mplayer/glamo.git] / libmpcodecs / dec_audio.c
blob218203272dd61b73cbe3193f2215782e1e895e5f
1 /*
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.
19 #include <stdio.h>
20 #include <stdlib.h>
21 #include <unistd.h>
22 #include <assert.h>
24 #include "config.h"
25 #include "mp_msg.h"
27 #include "stream/stream.h"
28 #include "libmpdemux/demuxer.h"
30 #include "codec-cfg.h"
31 #include "libmpdemux/stheader.h"
33 #include "dec_audio.h"
34 #include "ad.h"
35 #include "libaf/af_format.h"
37 #include "libaf/af.h"
39 #ifdef CONFIG_DYNAMIC_PLUGINS
40 #include <dlfcn.h>
41 #endif
43 #ifdef CONFIG_FAKE_MONO
44 int fakemono = 0;
45 #endif
47 /* used for ac3surround decoder - set using -channels option */
48 int audio_output_channels = 2;
49 af_cfg_t af_cfg = { 1, NULL }; // Configuration for audio filters
51 void afm_help(void)
53 int i;
54 mp_tmsg(MSGT_DECAUDIO, MSGL_INFO, "Available (compiled-in) audio codec families/drivers:\n");
55 mp_msg(MSGT_IDENTIFY, MSGL_INFO, "ID_AUDIO_DRIVERS\n");
56 mp_msg(MSGT_DECAUDIO, MSGL_INFO, " afm: info: (comment)\n");
57 for (i = 0; mpcodecs_ad_drivers[i] != NULL; i++)
58 if (mpcodecs_ad_drivers[i]->info->comment
59 && mpcodecs_ad_drivers[i]->info->comment[0])
60 mp_msg(MSGT_DECAUDIO, MSGL_INFO, "%9s %s (%s)\n",
61 mpcodecs_ad_drivers[i]->info->short_name,
62 mpcodecs_ad_drivers[i]->info->name,
63 mpcodecs_ad_drivers[i]->info->comment);
64 else
65 mp_msg(MSGT_DECAUDIO, MSGL_INFO, "%9s %s\n",
66 mpcodecs_ad_drivers[i]->info->short_name,
67 mpcodecs_ad_drivers[i]->info->name);
70 static int init_audio_codec(sh_audio_t *sh_audio)
72 assert(!sh_audio->initialized);
73 resync_audio_stream(sh_audio);
74 if ((af_cfg.force & AF_INIT_FORMAT_MASK) == AF_INIT_FLOAT) {
75 int fmt = AF_FORMAT_FLOAT_NE;
76 if (sh_audio->ad_driver->control(sh_audio, ADCTRL_QUERY_FORMAT,
77 &fmt) == CONTROL_TRUE) {
78 sh_audio->sample_format = fmt;
79 sh_audio->samplesize = 4;
82 sh_audio->audio_out_minsize = 8192; // default, preinit() may change it
83 if (!sh_audio->ad_driver->preinit(sh_audio)) {
84 mp_tmsg(MSGT_DECAUDIO, MSGL_ERR, "ADecoder preinit failed :(\n");
85 return 0;
88 /* allocate audio in buffer: */
89 if (sh_audio->audio_in_minsize > 0) {
90 sh_audio->a_in_buffer_size = sh_audio->audio_in_minsize;
91 mp_tmsg(MSGT_DECAUDIO, MSGL_V, "dec_audio: Allocating %d bytes for input buffer.\n",
92 sh_audio->a_in_buffer_size);
93 sh_audio->a_in_buffer = av_mallocz(sh_audio->a_in_buffer_size);
96 const int base_size = 65536;
97 // At least 64 KiB plus rounding up to next decodable unit size
98 sh_audio->a_buffer_size = base_size + sh_audio->audio_out_minsize;
100 mp_tmsg(MSGT_DECAUDIO, MSGL_V, "dec_audio: Allocating %d + %d = %d bytes for output buffer.\n",
101 sh_audio->audio_out_minsize, base_size, sh_audio->a_buffer_size);
103 sh_audio->a_buffer = av_mallocz(sh_audio->a_buffer_size);
104 if (!sh_audio->a_buffer) {
105 mp_tmsg(MSGT_DECAUDIO, MSGL_ERR, "Cannot allocate audio out buffer.\n");
106 return 0;
108 sh_audio->a_buffer_len = 0;
110 if (!sh_audio->ad_driver->init(sh_audio)) {
111 mp_tmsg(MSGT_DECAUDIO, MSGL_WARN, "ADecoder init failed :(\n");
112 uninit_audio(sh_audio); // free buffers
113 return 0;
116 sh_audio->initialized = 1;
118 if (!sh_audio->channels || !sh_audio->samplerate) {
119 mp_tmsg(MSGT_DECAUDIO, MSGL_WARN, "Unknown/missing audio format -> no sound\n");
120 uninit_audio(sh_audio); // free buffers
121 return 0;
124 if (!sh_audio->o_bps)
125 sh_audio->o_bps = sh_audio->channels * sh_audio->samplerate
126 * sh_audio->samplesize;
128 mp_msg(MSGT_DECAUDIO, MSGL_INFO,
129 "AUDIO: %d Hz, %d ch, %s, %3.1f kbit/%3.2f%% (ratio: %d->%d)\n",
130 sh_audio->samplerate, sh_audio->channels,
131 af_fmt2str_short(sh_audio->sample_format),
132 sh_audio->i_bps * 8 * 0.001,
133 ((float) sh_audio->i_bps / sh_audio->o_bps) * 100.0,
134 sh_audio->i_bps, sh_audio->o_bps);
135 mp_msg(MSGT_IDENTIFY, MSGL_INFO,
136 "ID_AUDIO_BITRATE=%d\nID_AUDIO_RATE=%d\n" "ID_AUDIO_NCH=%d\n",
137 sh_audio->i_bps * 8, sh_audio->samplerate, sh_audio->channels);
139 sh_audio->a_out_buffer_size = 0;
140 sh_audio->a_out_buffer = NULL;
141 sh_audio->a_out_buffer_len = 0;
143 return 1;
146 static int init_audio(sh_audio_t *sh_audio, char *codecname, char *afm,
147 int status, stringset_t *selected)
149 unsigned int orig_fourcc = sh_audio->wf ? sh_audio->wf->wFormatTag : 0;
150 int force = 0;
151 if (codecname && codecname[0] == '+') {
152 codecname = &codecname[1];
153 force = 1;
155 sh_audio->codec = NULL;
156 while (1) {
157 const ad_functions_t *mpadec;
158 int i;
159 sh_audio->ad_driver = 0;
160 // restore original fourcc:
161 if (sh_audio->wf)
162 sh_audio->wf->wFormatTag = i = orig_fourcc;
163 if (!(sh_audio->codec = find_audio_codec(sh_audio->format,
164 sh_audio->wf ? (&i) : NULL,
165 sh_audio->codec, force)))
166 break;
167 if (sh_audio->wf)
168 sh_audio->wf->wFormatTag = i;
169 // ok we found one codec
170 if (stringset_test(selected, sh_audio->codec->name))
171 continue; // already tried & failed
172 if (codecname && strcmp(sh_audio->codec->name, codecname))
173 continue; // -ac
174 if (afm && strcmp(sh_audio->codec->drv, afm))
175 continue; // afm doesn't match
176 if (!force && sh_audio->codec->status < status)
177 continue; // too unstable
178 stringset_add(selected, sh_audio->codec->name); // tagging it
179 // ok, it matches all rules, let's find the driver!
180 for (i = 0; mpcodecs_ad_drivers[i] != NULL; i++)
181 if (!strcmp(mpcodecs_ad_drivers[i]->info->short_name,
182 sh_audio->codec->drv))
183 break;
184 mpadec = mpcodecs_ad_drivers[i];
185 #ifdef CONFIG_DYNAMIC_PLUGINS
186 if (!mpadec) {
187 /* try to open shared decoder plugin */
188 int buf_len;
189 char *buf;
190 ad_functions_t *funcs_sym;
191 ad_info_t *info_sym;
193 buf_len =
194 strlen(MPLAYER_LIBDIR) + strlen(sh_audio->codec->drv) + 16;
195 buf = malloc(buf_len);
196 if (!buf)
197 break;
198 snprintf(buf, buf_len, "%s/mplayer/ad_%s.so", MPLAYER_LIBDIR,
199 sh_audio->codec->drv);
200 mp_msg(MSGT_DECAUDIO, MSGL_DBG2,
201 "Trying to open external plugin: %s\n", buf);
202 sh_audio->dec_handle = dlopen(buf, RTLD_LAZY);
203 if (!sh_audio->dec_handle)
204 break;
205 snprintf(buf, buf_len, "mpcodecs_ad_%s", sh_audio->codec->drv);
206 funcs_sym = dlsym(sh_audio->dec_handle, buf);
207 if (!funcs_sym || !funcs_sym->info || !funcs_sym->preinit
208 || !funcs_sym->init || !funcs_sym->uninit
209 || !funcs_sym->control || !funcs_sym->decode_audio)
210 break;
211 info_sym = funcs_sym->info;
212 if (strcmp(info_sym->short_name, sh_audio->codec->drv))
213 break;
214 free(buf);
215 mpadec = funcs_sym;
216 mp_msg(MSGT_DECAUDIO, MSGL_V,
217 "Using external decoder plugin (%s/mplayer/ad_%s.so)!\n",
218 MPLAYER_LIBDIR, sh_audio->codec->drv);
220 #endif
221 if (!mpadec) { // driver not available (==compiled in)
222 mp_tmsg(MSGT_DECAUDIO, MSGL_ERR,
223 "Requested audio codec family [%s] (afm=%s) not available.\nEnable it at compilation.\n",
224 sh_audio->codec->name, sh_audio->codec->drv);
225 continue;
227 // it's available, let's try to init!
228 // init()
229 mp_tmsg(MSGT_DECAUDIO, MSGL_INFO, "Opening audio decoder: [%s] %s\n",
230 mpadec->info->short_name, mpadec->info->name);
231 sh_audio->ad_driver = mpadec;
232 if (!init_audio_codec(sh_audio)) {
233 mp_tmsg(MSGT_DECAUDIO, MSGL_INFO, "ADecoder init failed :(\n");
234 continue; // try next...
236 // Yeah! We got it!
237 return 1;
239 return 0;
242 int init_best_audio_codec(sh_audio_t *sh_audio, char **audio_codec_list,
243 char **audio_fm_list)
245 stringset_t selected;
246 char *ac_l_default[2] = { "", (char *) NULL };
247 // hack:
248 if (!audio_codec_list)
249 audio_codec_list = ac_l_default;
250 // Go through the codec.conf and find the best codec...
251 sh_audio->initialized = 0;
252 stringset_init(&selected);
253 while (!sh_audio->initialized && *audio_codec_list) {
254 char *audio_codec = *(audio_codec_list++);
255 if (audio_codec[0]) {
256 if (audio_codec[0] == '-') {
257 // disable this codec:
258 stringset_add(&selected, audio_codec + 1);
259 } else {
260 // forced codec by name:
261 mp_tmsg(MSGT_DECAUDIO, MSGL_INFO, "Forced audio codec: %s\n",
262 audio_codec);
263 init_audio(sh_audio, audio_codec, NULL, -1, &selected);
265 } else {
266 int status;
267 // try in stability order: UNTESTED, WORKING, BUGGY.
268 // never try CRASHING.
269 if (audio_fm_list) {
270 char **fmlist = audio_fm_list;
271 // try first the preferred codec families:
272 while (!sh_audio->initialized && *fmlist) {
273 char *audio_fm = *(fmlist++);
274 mp_tmsg(MSGT_DECAUDIO, MSGL_INFO, "Trying to force audio codec driver family %s...\n",
275 audio_fm);
276 for (status = CODECS_STATUS__MAX;
277 status >= CODECS_STATUS__MIN; --status)
278 if (init_audio(sh_audio, NULL, audio_fm, status, &selected))
279 break;
282 if (!sh_audio->initialized)
283 for (status = CODECS_STATUS__MAX; status >= CODECS_STATUS__MIN;
284 --status)
285 if (init_audio(sh_audio, NULL, NULL, status, &selected))
286 break;
289 stringset_free(&selected);
291 if (!sh_audio->initialized) {
292 mp_tmsg(MSGT_DECAUDIO, MSGL_ERR, "Cannot find codec for audio format 0x%X.\n",
293 sh_audio->format);
294 return 0; // failed
297 mp_tmsg(MSGT_DECAUDIO, MSGL_INFO, "Selected audio codec: [%s] afm: %s (%s)\n",
298 sh_audio->codec->name, sh_audio->codec->drv, sh_audio->codec->info);
299 return 1; // success
302 void uninit_audio(sh_audio_t *sh_audio)
304 if (sh_audio->afilter) {
305 mp_msg(MSGT_DECAUDIO, MSGL_V, "Uninit audio filters...\n");
306 af_uninit(sh_audio->afilter);
307 free(sh_audio->afilter);
308 sh_audio->afilter = NULL;
310 if (sh_audio->initialized) {
311 mp_tmsg(MSGT_DECAUDIO, MSGL_V, "Uninit audio: %s\n",
312 sh_audio->codec->drv);
313 sh_audio->ad_driver->uninit(sh_audio);
314 #ifdef CONFIG_DYNAMIC_PLUGINS
315 if (sh_audio->dec_handle)
316 dlclose(sh_audio->dec_handle);
317 #endif
318 sh_audio->initialized = 0;
320 free(sh_audio->a_out_buffer);
321 sh_audio->a_out_buffer = NULL;
322 sh_audio->a_out_buffer_size = 0;
323 av_freep(&sh_audio->a_buffer);
324 av_freep(&sh_audio->a_in_buffer);
328 int init_audio_filters(sh_audio_t *sh_audio, int in_samplerate,
329 int *out_samplerate, int *out_channels, int *out_format)
331 af_stream_t *afs = sh_audio->afilter;
332 if (!afs) {
333 afs = malloc(sizeof(af_stream_t));
334 memset(afs, 0, sizeof(af_stream_t));
336 // input format: same as codec's output format:
337 afs->input.rate = in_samplerate;
338 afs->input.nch = sh_audio->channels;
339 afs->input.format = sh_audio->sample_format;
340 af_fix_parameters(&(afs->input));
342 // output format: same as ao driver's input format (if missing, fallback to input)
343 afs->output.rate = *out_samplerate;
344 afs->output.nch = *out_channels;
345 afs->output.format = *out_format;
346 af_fix_parameters(&(afs->output));
348 // filter config:
349 memcpy(&afs->cfg, &af_cfg, sizeof(af_cfg_t));
351 mp_tmsg(MSGT_DECAUDIO, MSGL_V, "Building audio filter chain for %dHz/%dch/%s -> %dHz/%dch/%s...\n",
352 afs->input.rate, afs->input.nch,
353 af_fmt2str_short(afs->input.format), afs->output.rate,
354 afs->output.nch, af_fmt2str_short(afs->output.format));
356 // let's autoprobe it!
357 if (0 != af_init(afs)) {
358 sh_audio->afilter = NULL;
359 free(afs);
360 return 0; // failed :(
363 *out_samplerate = afs->output.rate;
364 *out_channels = afs->output.nch;
365 *out_format = afs->output.format;
367 sh_audio->a_out_buffer_len = 0;
369 // ok!
370 sh_audio->afilter = (void *) afs;
371 return 1;
374 static int filter_n_bytes(sh_audio_t *sh, int len)
376 assert(len-1 + sh->audio_out_minsize <= sh->a_buffer_size);
378 int error = 0;
380 // Decode more bytes if needed
381 while (sh->a_buffer_len < len) {
382 unsigned char *buf = sh->a_buffer + sh->a_buffer_len;
383 int minlen = len - sh->a_buffer_len;
384 int maxlen = sh->a_buffer_size - sh->a_buffer_len;
385 int ret = sh->ad_driver->decode_audio(sh, buf, minlen, maxlen);
386 if (ret <= 0) {
387 error = -1;
388 len = sh->a_buffer_len;
389 break;
391 sh->a_buffer_len += ret;
394 // Filter
395 af_data_t filter_input = {
396 .audio = sh->a_buffer,
397 .len = len,
398 .rate = sh->samplerate,
399 .nch = sh->channels,
400 .format = sh->sample_format
402 af_fix_parameters(&filter_input);
403 af_data_t *filter_output = af_play(sh->afilter, &filter_input);
404 if (!filter_output)
405 return -1;
406 if (sh->a_out_buffer_size < sh->a_out_buffer_len + filter_output->len) {
407 int newlen = sh->a_out_buffer_len + filter_output->len;
408 mp_msg(MSGT_DECAUDIO, MSGL_V, "Increasing filtered audio buffer size "
409 "from %d to %d\n", sh->a_out_buffer_size, newlen);
410 sh->a_out_buffer = realloc(sh->a_out_buffer, newlen);
411 sh->a_out_buffer_size = newlen;
413 memcpy(sh->a_out_buffer + sh->a_out_buffer_len, filter_output->audio,
414 filter_output->len);
415 sh->a_out_buffer_len += filter_output->len;
417 // remove processed data from decoder buffer:
418 sh->a_buffer_len -= len;
419 memmove(sh->a_buffer, sh->a_buffer + len, sh->a_buffer_len);
421 return error;
424 /* Try to get at least minlen decoded+filtered bytes in sh_audio->a_out_buffer
425 * (total length including possible existing data).
426 * Return 0 on success, -1 on error/EOF (not distinguished).
427 * In the former case sh_audio->a_out_buffer_len is always >= minlen
428 * on return. In case of EOF/error it might or might not be.
429 * Can reallocate sh_audio->a_out_buffer if needed to fit all filter output. */
430 int decode_audio(sh_audio_t *sh_audio, int minlen)
432 // Indicates that a filter seems to be buffering large amounts of data
433 int huge_filter_buffer = 0;
434 // Decoded audio must be cut at boundaries of this many bytes
435 int unitsize = sh_audio->channels * sh_audio->samplesize * 16;
437 /* Filter output size will be about filter_multiplier times input size.
438 * If some filter buffers audio in big blocks this might only hold
439 * as average over time. */
440 double filter_multiplier = af_calc_filter_multiplier(sh_audio->afilter);
442 /* If the decoder set audio_out_minsize then it can do the equivalent of
443 * "while (output_len < target_len) output_len += audio_out_minsize;",
444 * so we must guarantee there is at least audio_out_minsize-1 bytes
445 * more space in the output buffer than the minimum length we try to
446 * decode. */
447 int max_decode_len = sh_audio->a_buffer_size - sh_audio->audio_out_minsize;
448 max_decode_len -= max_decode_len % unitsize;
450 while (sh_audio->a_out_buffer_len < minlen) {
451 int declen = (minlen - sh_audio->a_out_buffer_len) / filter_multiplier
452 + (unitsize << 5); // some extra for possible filter buffering
453 if (huge_filter_buffer)
454 /* Some filter must be doing significant buffering if the estimated
455 * input length didn't produce enough output from filters.
456 * Feed the filters 2k bytes at a time until we have enough output.
457 * Very small amounts could make filtering inefficient while large
458 * amounts can make MPlayer demux the file unnecessarily far ahead
459 * to get audio data and buffer video frames in memory while doing
460 * so. However the performance impact of either is probably not too
461 * significant as long as the value is not completely insane. */
462 declen = 2000;
463 declen -= declen % unitsize;
464 if (declen > max_decode_len)
465 declen = max_decode_len;
466 else
467 /* if this iteration does not fill buffer, we must have lots
468 * of buffering in filters */
469 huge_filter_buffer = 1;
470 if (filter_n_bytes(sh_audio, declen) < 0)
471 return -1;
473 return 0;
476 void resync_audio_stream(sh_audio_t *sh_audio)
478 sh_audio->a_in_buffer_len = 0; // clear audio input buffer
479 sh_audio->pts = MP_NOPTS_VALUE;
480 if (!sh_audio->initialized)
481 return;
482 sh_audio->ad_driver->control(sh_audio, ADCTRL_RESYNC_STREAM, NULL);
485 void skip_audio_frame(sh_audio_t *sh_audio)
487 if (!sh_audio->initialized)
488 return;
489 if (sh_audio->ad_driver->control(sh_audio, ADCTRL_SKIP_FRAME, NULL) ==
490 CONTROL_TRUE)
491 return;
492 // default skip code:
493 ds_fill_buffer(sh_audio->ds); // skip block