example.conf: Disambiguate remarks about profiles
[mplayer.git] / libmpcodecs / dec_audio.c
blob7bbbdeb058f1cbffcfeffa6104d0be5486294860
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 af_cfg_t af_cfg = { 1, NULL }; // Configuration for audio filters
49 void afm_help(void)
51 int i;
52 mp_tmsg(MSGT_DECAUDIO, MSGL_INFO, "Available (compiled-in) audio codec families/drivers:\n");
53 mp_msg(MSGT_IDENTIFY, MSGL_INFO, "ID_AUDIO_DRIVERS\n");
54 mp_msg(MSGT_DECAUDIO, MSGL_INFO, " afm: info: (comment)\n");
55 for (i = 0; mpcodecs_ad_drivers[i] != NULL; i++)
56 if (mpcodecs_ad_drivers[i]->info->comment
57 && mpcodecs_ad_drivers[i]->info->comment[0])
58 mp_msg(MSGT_DECAUDIO, MSGL_INFO, "%9s %s (%s)\n",
59 mpcodecs_ad_drivers[i]->info->short_name,
60 mpcodecs_ad_drivers[i]->info->name,
61 mpcodecs_ad_drivers[i]->info->comment);
62 else
63 mp_msg(MSGT_DECAUDIO, MSGL_INFO, "%9s %s\n",
64 mpcodecs_ad_drivers[i]->info->short_name,
65 mpcodecs_ad_drivers[i]->info->name);
68 static int init_audio_codec(sh_audio_t *sh_audio)
70 assert(!sh_audio->initialized);
71 resync_audio_stream(sh_audio);
72 if ((af_cfg.force & AF_INIT_FORMAT_MASK) == AF_INIT_FLOAT) {
73 int fmt = AF_FORMAT_FLOAT_NE;
74 if (sh_audio->ad_driver->control(sh_audio, ADCTRL_QUERY_FORMAT,
75 &fmt) == CONTROL_TRUE) {
76 sh_audio->sample_format = fmt;
77 sh_audio->samplesize = 4;
80 sh_audio->audio_out_minsize = 8192; // default, preinit() may change it
81 if (!sh_audio->ad_driver->preinit(sh_audio)) {
82 mp_tmsg(MSGT_DECAUDIO, MSGL_ERR, "ADecoder preinit failed :(\n");
83 return 0;
86 /* allocate audio in buffer: */
87 if (sh_audio->audio_in_minsize > 0) {
88 sh_audio->a_in_buffer_size = sh_audio->audio_in_minsize;
89 mp_tmsg(MSGT_DECAUDIO, MSGL_V, "dec_audio: Allocating %d bytes for input buffer.\n",
90 sh_audio->a_in_buffer_size);
91 sh_audio->a_in_buffer = av_mallocz(sh_audio->a_in_buffer_size);
94 const int base_size = 65536;
95 // At least 64 KiB plus rounding up to next decodable unit size
96 sh_audio->a_buffer_size = base_size + sh_audio->audio_out_minsize;
98 mp_tmsg(MSGT_DECAUDIO, MSGL_V, "dec_audio: Allocating %d + %d = %d bytes for output buffer.\n",
99 sh_audio->audio_out_minsize, base_size, sh_audio->a_buffer_size);
101 sh_audio->a_buffer = av_mallocz(sh_audio->a_buffer_size);
102 if (!sh_audio->a_buffer) {
103 mp_tmsg(MSGT_DECAUDIO, MSGL_ERR, "Cannot allocate audio out buffer.\n");
104 return 0;
106 sh_audio->a_buffer_len = 0;
108 if (!sh_audio->ad_driver->init(sh_audio)) {
109 mp_tmsg(MSGT_DECAUDIO, MSGL_WARN, "ADecoder init failed :(\n");
110 uninit_audio(sh_audio); // free buffers
111 return 0;
114 sh_audio->initialized = 1;
116 if (!sh_audio->channels || !sh_audio->samplerate) {
117 mp_tmsg(MSGT_DECAUDIO, MSGL_WARN, "Unknown/missing audio format -> no sound\n");
118 uninit_audio(sh_audio); // free buffers
119 return 0;
122 if (!sh_audio->o_bps)
123 sh_audio->o_bps = sh_audio->channels * sh_audio->samplerate
124 * sh_audio->samplesize;
126 mp_msg(MSGT_DECAUDIO, MSGL_INFO,
127 "AUDIO: %d Hz, %d ch, %s, %3.1f kbit/%3.2f%% (ratio: %d->%d)\n",
128 sh_audio->samplerate, sh_audio->channels,
129 af_fmt2str_short(sh_audio->sample_format),
130 sh_audio->i_bps * 8 * 0.001,
131 ((float) sh_audio->i_bps / sh_audio->o_bps) * 100.0,
132 sh_audio->i_bps, sh_audio->o_bps);
133 mp_msg(MSGT_IDENTIFY, MSGL_INFO,
134 "ID_AUDIO_BITRATE=%d\nID_AUDIO_RATE=%d\n" "ID_AUDIO_NCH=%d\n",
135 sh_audio->i_bps * 8, sh_audio->samplerate, sh_audio->channels);
137 sh_audio->a_out_buffer_size = 0;
138 sh_audio->a_out_buffer = NULL;
139 sh_audio->a_out_buffer_len = 0;
141 return 1;
144 static int init_audio(sh_audio_t *sh_audio, char *codecname, char *afm,
145 int status, stringset_t *selected)
147 unsigned int orig_fourcc = sh_audio->wf ? sh_audio->wf->wFormatTag : 0;
148 int force = 0;
149 if (codecname && codecname[0] == '+') {
150 codecname = &codecname[1];
151 force = 1;
153 sh_audio->codec = NULL;
154 while (1) {
155 const ad_functions_t *mpadec;
156 int i;
157 sh_audio->ad_driver = 0;
158 // restore original fourcc:
159 if (sh_audio->wf)
160 sh_audio->wf->wFormatTag = i = orig_fourcc;
161 if (!(sh_audio->codec = find_audio_codec(sh_audio->format,
162 sh_audio->wf ? (&i) : NULL,
163 sh_audio->codec, force)))
164 break;
165 if (sh_audio->wf)
166 sh_audio->wf->wFormatTag = i;
167 // ok we found one codec
168 if (stringset_test(selected, sh_audio->codec->name))
169 continue; // already tried & failed
170 if (codecname && strcmp(sh_audio->codec->name, codecname))
171 continue; // -ac
172 if (afm && strcmp(sh_audio->codec->drv, afm))
173 continue; // afm doesn't match
174 if (!force && sh_audio->codec->status < status)
175 continue; // too unstable
176 stringset_add(selected, sh_audio->codec->name); // tagging it
177 // ok, it matches all rules, let's find the driver!
178 for (i = 0; mpcodecs_ad_drivers[i] != NULL; i++)
179 if (!strcmp(mpcodecs_ad_drivers[i]->info->short_name,
180 sh_audio->codec->drv))
181 break;
182 mpadec = mpcodecs_ad_drivers[i];
183 #ifdef CONFIG_DYNAMIC_PLUGINS
184 if (!mpadec) {
185 /* try to open shared decoder plugin */
186 int buf_len;
187 char *buf;
188 ad_functions_t *funcs_sym;
189 ad_info_t *info_sym;
191 buf_len =
192 strlen(MPLAYER_LIBDIR) + strlen(sh_audio->codec->drv) + 16;
193 buf = malloc(buf_len);
194 if (!buf)
195 break;
196 snprintf(buf, buf_len, "%s/mplayer/ad_%s.so", MPLAYER_LIBDIR,
197 sh_audio->codec->drv);
198 mp_msg(MSGT_DECAUDIO, MSGL_DBG2,
199 "Trying to open external plugin: %s\n", buf);
200 sh_audio->dec_handle = dlopen(buf, RTLD_LAZY);
201 if (!sh_audio->dec_handle)
202 break;
203 snprintf(buf, buf_len, "mpcodecs_ad_%s", sh_audio->codec->drv);
204 funcs_sym = dlsym(sh_audio->dec_handle, buf);
205 if (!funcs_sym || !funcs_sym->info || !funcs_sym->preinit
206 || !funcs_sym->init || !funcs_sym->uninit
207 || !funcs_sym->control || !funcs_sym->decode_audio)
208 break;
209 info_sym = funcs_sym->info;
210 if (strcmp(info_sym->short_name, sh_audio->codec->drv))
211 break;
212 free(buf);
213 mpadec = funcs_sym;
214 mp_msg(MSGT_DECAUDIO, MSGL_V,
215 "Using external decoder plugin (%s/mplayer/ad_%s.so)!\n",
216 MPLAYER_LIBDIR, sh_audio->codec->drv);
218 #endif
219 if (!mpadec) { // driver not available (==compiled in)
220 mp_tmsg(MSGT_DECAUDIO, MSGL_ERR,
221 "Requested audio codec family [%s] (afm=%s) not available.\nEnable it at compilation.\n",
222 sh_audio->codec->name, sh_audio->codec->drv);
223 continue;
225 // it's available, let's try to init!
226 // init()
227 mp_tmsg(MSGT_DECAUDIO, MSGL_INFO, "Opening audio decoder: [%s] %s\n",
228 mpadec->info->short_name, mpadec->info->name);
229 sh_audio->ad_driver = mpadec;
230 if (!init_audio_codec(sh_audio)) {
231 mp_tmsg(MSGT_DECAUDIO, MSGL_INFO, "ADecoder init failed :(\n");
232 continue; // try next...
234 // Yeah! We got it!
235 return 1;
237 return 0;
240 int init_best_audio_codec(sh_audio_t *sh_audio, char **audio_codec_list,
241 char **audio_fm_list)
243 stringset_t selected;
244 char *ac_l_default[2] = { "", (char *) NULL };
245 // hack:
246 if (!audio_codec_list)
247 audio_codec_list = ac_l_default;
248 // Go through the codec.conf and find the best codec...
249 sh_audio->initialized = 0;
250 stringset_init(&selected);
251 while (!sh_audio->initialized && *audio_codec_list) {
252 char *audio_codec = *(audio_codec_list++);
253 if (audio_codec[0]) {
254 if (audio_codec[0] == '-') {
255 // disable this codec:
256 stringset_add(&selected, audio_codec + 1);
257 } else {
258 // forced codec by name:
259 mp_tmsg(MSGT_DECAUDIO, MSGL_INFO, "Forced audio codec: %s\n",
260 audio_codec);
261 init_audio(sh_audio, audio_codec, NULL, -1, &selected);
263 } else {
264 int status;
265 // try in stability order: UNTESTED, WORKING, BUGGY.
266 // never try CRASHING.
267 if (audio_fm_list) {
268 char **fmlist = audio_fm_list;
269 // try first the preferred codec families:
270 while (!sh_audio->initialized && *fmlist) {
271 char *audio_fm = *(fmlist++);
272 mp_tmsg(MSGT_DECAUDIO, MSGL_INFO, "Trying to force audio codec driver family %s...\n",
273 audio_fm);
274 for (status = CODECS_STATUS__MAX;
275 status >= CODECS_STATUS__MIN; --status)
276 if (init_audio(sh_audio, NULL, audio_fm, status, &selected))
277 break;
280 if (!sh_audio->initialized)
281 for (status = CODECS_STATUS__MAX; status >= CODECS_STATUS__MIN;
282 --status)
283 if (init_audio(sh_audio, NULL, NULL, status, &selected))
284 break;
287 stringset_free(&selected);
289 if (!sh_audio->initialized) {
290 mp_tmsg(MSGT_DECAUDIO, MSGL_ERR, "Cannot find codec for audio format 0x%X.\n",
291 sh_audio->format);
292 return 0; // failed
295 mp_tmsg(MSGT_DECAUDIO, MSGL_INFO, "Selected audio codec: [%s] afm: %s (%s)\n",
296 sh_audio->codec->name, sh_audio->codec->drv, sh_audio->codec->info);
297 return 1; // success
300 void uninit_audio(sh_audio_t *sh_audio)
302 if (sh_audio->afilter) {
303 mp_msg(MSGT_DECAUDIO, MSGL_V, "Uninit audio filters...\n");
304 af_uninit(sh_audio->afilter);
305 free(sh_audio->afilter);
306 sh_audio->afilter = NULL;
308 if (sh_audio->initialized) {
309 mp_tmsg(MSGT_DECAUDIO, MSGL_V, "Uninit audio: %s\n",
310 sh_audio->codec->drv);
311 sh_audio->ad_driver->uninit(sh_audio);
312 #ifdef CONFIG_DYNAMIC_PLUGINS
313 if (sh_audio->dec_handle)
314 dlclose(sh_audio->dec_handle);
315 #endif
316 sh_audio->initialized = 0;
318 free(sh_audio->a_out_buffer);
319 sh_audio->a_out_buffer = NULL;
320 sh_audio->a_out_buffer_size = 0;
321 av_freep(&sh_audio->a_buffer);
322 av_freep(&sh_audio->a_in_buffer);
326 int init_audio_filters(sh_audio_t *sh_audio, int in_samplerate,
327 int *out_samplerate, int *out_channels, int *out_format)
329 af_stream_t *afs = sh_audio->afilter;
330 if (!afs) {
331 afs = calloc(1, sizeof(struct af_stream));
332 afs->opts = sh_audio->opts;
334 // input format: same as codec's output format:
335 afs->input.rate = in_samplerate;
336 afs->input.nch = sh_audio->channels;
337 afs->input.format = sh_audio->sample_format;
338 af_fix_parameters(&(afs->input));
340 // output format: same as ao driver's input format (if missing, fallback to input)
341 afs->output.rate = *out_samplerate;
342 afs->output.nch = *out_channels;
343 afs->output.format = *out_format;
344 af_fix_parameters(&(afs->output));
346 // filter config:
347 memcpy(&afs->cfg, &af_cfg, sizeof(af_cfg_t));
349 mp_tmsg(MSGT_DECAUDIO, MSGL_V, "Building audio filter chain for %dHz/%dch/%s -> %dHz/%dch/%s...\n",
350 afs->input.rate, afs->input.nch,
351 af_fmt2str_short(afs->input.format), afs->output.rate,
352 afs->output.nch, af_fmt2str_short(afs->output.format));
354 // let's autoprobe it!
355 if (0 != af_init(afs)) {
356 sh_audio->afilter = NULL;
357 free(afs);
358 return 0; // failed :(
361 *out_samplerate = afs->output.rate;
362 *out_channels = afs->output.nch;
363 *out_format = afs->output.format;
365 sh_audio->a_out_buffer_len = 0;
367 // ok!
368 sh_audio->afilter = (void *) afs;
369 return 1;
372 static void set_min_out_buffer_size(struct sh_audio *sh, int len)
374 if (sh->a_out_buffer_size < len) {
375 mp_msg(MSGT_DECAUDIO, MSGL_V, "Increasing filtered audio buffer size "
376 "from %d to %d\n", sh->a_out_buffer_size, len);
377 sh->a_out_buffer = realloc(sh->a_out_buffer, len);
378 sh->a_out_buffer_size = len;
382 static int filter_n_bytes(sh_audio_t *sh, int len)
384 assert(len-1 + sh->audio_out_minsize <= sh->a_buffer_size);
386 int error = 0;
388 // Decode more bytes if needed
389 int old_samplerate = sh->samplerate;
390 int old_channels = sh->channels;
391 int old_sample_format = sh->sample_format;
392 while (sh->a_buffer_len < len) {
393 unsigned char *buf = sh->a_buffer + sh->a_buffer_len;
394 int minlen = len - sh->a_buffer_len;
395 int maxlen = sh->a_buffer_size - sh->a_buffer_len;
396 int ret = sh->ad_driver->decode_audio(sh, buf, minlen, maxlen);
397 int format_change = sh->samplerate != old_samplerate
398 || sh->channels != old_channels
399 || sh->sample_format != old_sample_format;
400 if (ret <= 0 || format_change) {
401 error = format_change ? -2 : -1;
402 // samples from format-changing call get discarded too
403 len = sh->a_buffer_len;
404 break;
406 sh->a_buffer_len += ret;
409 // Filter
410 af_data_t filter_input = {
411 .audio = sh->a_buffer,
412 .len = len,
413 .rate = sh->samplerate,
414 .nch = sh->channels,
415 .format = sh->sample_format
417 af_fix_parameters(&filter_input);
418 af_data_t *filter_output = af_play(sh->afilter, &filter_input);
419 if (!filter_output)
420 return -1;
421 set_min_out_buffer_size(sh, sh->a_out_buffer_len + filter_output->len);
422 memcpy(sh->a_out_buffer + sh->a_out_buffer_len, filter_output->audio,
423 filter_output->len);
424 sh->a_out_buffer_len += filter_output->len;
426 // remove processed data from decoder buffer:
427 sh->a_buffer_len -= len;
428 memmove(sh->a_buffer, sh->a_buffer + len, sh->a_buffer_len);
430 return error;
433 /* Try to get at least minlen decoded+filtered bytes in sh_audio->a_out_buffer
434 * (total length including possible existing data).
435 * Return 0 on success, -1 on error/EOF (not distinguished).
436 * In the former case sh_audio->a_out_buffer_len is always >= minlen
437 * on return. In case of EOF/error it might or might not be.
438 * Can reallocate sh_audio->a_out_buffer if needed to fit all filter output. */
439 int decode_audio(sh_audio_t *sh_audio, int minlen)
441 // Indicates that a filter seems to be buffering large amounts of data
442 int huge_filter_buffer = 0;
443 // Decoded audio must be cut at boundaries of this many bytes
444 int unitsize = sh_audio->channels * sh_audio->samplesize * 16;
446 /* Filter output size will be about filter_multiplier times input size.
447 * If some filter buffers audio in big blocks this might only hold
448 * as average over time. */
449 double filter_multiplier = af_calc_filter_multiplier(sh_audio->afilter);
451 /* If the decoder set audio_out_minsize then it can do the equivalent of
452 * "while (output_len < target_len) output_len += audio_out_minsize;",
453 * so we must guarantee there is at least audio_out_minsize-1 bytes
454 * more space in the output buffer than the minimum length we try to
455 * decode. */
456 int max_decode_len = sh_audio->a_buffer_size - sh_audio->audio_out_minsize;
457 max_decode_len -= max_decode_len % unitsize;
459 while (sh_audio->a_out_buffer_len < minlen) {
460 int declen = (minlen - sh_audio->a_out_buffer_len) / filter_multiplier
461 + (unitsize << 5); // some extra for possible filter buffering
462 if (huge_filter_buffer)
463 /* Some filter must be doing significant buffering if the estimated
464 * input length didn't produce enough output from filters.
465 * Feed the filters 2k bytes at a time until we have enough output.
466 * Very small amounts could make filtering inefficient while large
467 * amounts can make MPlayer demux the file unnecessarily far ahead
468 * to get audio data and buffer video frames in memory while doing
469 * so. However the performance impact of either is probably not too
470 * significant as long as the value is not completely insane. */
471 declen = 2000;
472 declen -= declen % unitsize;
473 if (declen > max_decode_len)
474 declen = max_decode_len;
475 else
476 /* if this iteration does not fill buffer, we must have lots
477 * of buffering in filters */
478 huge_filter_buffer = 1;
479 int res = filter_n_bytes(sh_audio, declen);
480 if (res < 0)
481 return res;
483 return 0;
486 void decode_audio_prepend_bytes(struct sh_audio *sh, int count, int byte)
488 set_min_out_buffer_size(sh, sh->a_out_buffer_len + count);
489 memmove(sh->a_out_buffer + count, sh->a_out_buffer, sh->a_out_buffer_len);
490 memset(sh->a_out_buffer, byte, count);
491 sh->a_out_buffer_len += count;
495 void resync_audio_stream(sh_audio_t *sh_audio)
497 sh_audio->a_in_buffer_len = 0; // clear audio input buffer
498 sh_audio->pts = MP_NOPTS_VALUE;
499 if (!sh_audio->initialized)
500 return;
501 sh_audio->ad_driver->control(sh_audio, ADCTRL_RESYNC_STREAM, NULL);
504 void skip_audio_frame(sh_audio_t *sh_audio)
506 if (!sh_audio->initialized)
507 return;
508 if (sh_audio->ad_driver->control(sh_audio, ADCTRL_SKIP_FRAME, NULL) ==
509 CONTROL_TRUE)
510 return;
511 // default skip code:
512 ds_fill_buffer(sh_audio->ds); // skip block