vo_gl: fix image corruption with PBOs when playing 10 bit video
[mplayer.git] / libmpcodecs / dec_audio.c
blob0541947f604d98002ecb876a22f2d84a2011cb95
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"
26 #include "bstr.h"
28 #include "stream/stream.h"
29 #include "libmpdemux/demuxer.h"
31 #include "codec-cfg.h"
32 #include "libmpdemux/stheader.h"
34 #include "dec_audio.h"
35 #include "ad.h"
36 #include "libaf/af_format.h"
38 #include "libaf/af.h"
40 #ifdef CONFIG_DYNAMIC_PLUGINS
41 #include <dlfcn.h>
42 #endif
44 #ifdef CONFIG_FAKE_MONO
45 int fakemono = 0;
46 #endif
48 af_cfg_t af_cfg = { 1, NULL }; // Configuration for audio filters
50 void afm_help(void)
52 int i;
53 mp_tmsg(MSGT_DECAUDIO, MSGL_INFO, "Available (compiled-in) audio codec families/drivers:\n");
54 mp_msg(MSGT_IDENTIFY, MSGL_INFO, "ID_AUDIO_DRIVERS\n");
55 mp_msg(MSGT_DECAUDIO, MSGL_INFO, " afm: info: (comment)\n");
56 for (i = 0; mpcodecs_ad_drivers[i] != NULL; i++)
57 if (mpcodecs_ad_drivers[i]->info->comment
58 && mpcodecs_ad_drivers[i]->info->comment[0])
59 mp_msg(MSGT_DECAUDIO, MSGL_INFO, "%9s %s (%s)\n",
60 mpcodecs_ad_drivers[i]->info->short_name,
61 mpcodecs_ad_drivers[i]->info->name,
62 mpcodecs_ad_drivers[i]->info->comment);
63 else
64 mp_msg(MSGT_DECAUDIO, MSGL_INFO, "%9s %s\n",
65 mpcodecs_ad_drivers[i]->info->short_name,
66 mpcodecs_ad_drivers[i]->info->name);
69 static int init_audio_codec(sh_audio_t *sh_audio)
71 assert(!sh_audio->initialized);
72 resync_audio_stream(sh_audio);
73 if ((af_cfg.force & AF_INIT_FORMAT_MASK) == AF_INIT_FLOAT) {
74 int fmt = AF_FORMAT_FLOAT_NE;
75 if (sh_audio->ad_driver->control(sh_audio, ADCTRL_QUERY_FORMAT,
76 &fmt) == CONTROL_TRUE) {
77 sh_audio->sample_format = fmt;
78 sh_audio->samplesize = 4;
81 sh_audio->audio_out_minsize = 8192; // default, preinit() may change it
82 if (!sh_audio->ad_driver->preinit(sh_audio)) {
83 mp_tmsg(MSGT_DECAUDIO, MSGL_ERR, "ADecoder preinit failed :(\n");
84 return 0;
87 /* allocate audio in buffer: */
88 if (sh_audio->audio_in_minsize > 0) {
89 sh_audio->a_in_buffer_size = sh_audio->audio_in_minsize;
90 mp_tmsg(MSGT_DECAUDIO, MSGL_V, "dec_audio: Allocating %d bytes for input buffer.\n",
91 sh_audio->a_in_buffer_size);
92 sh_audio->a_in_buffer = av_mallocz(sh_audio->a_in_buffer_size);
95 const int base_size = 65536;
96 // At least 64 KiB plus rounding up to next decodable unit size
97 sh_audio->a_buffer_size = base_size + sh_audio->audio_out_minsize;
99 mp_tmsg(MSGT_DECAUDIO, MSGL_V, "dec_audio: Allocating %d + %d = %d bytes for output buffer.\n",
100 sh_audio->audio_out_minsize, base_size, sh_audio->a_buffer_size);
102 sh_audio->a_buffer = av_mallocz(sh_audio->a_buffer_size);
103 if (!sh_audio->a_buffer) {
104 mp_tmsg(MSGT_DECAUDIO, MSGL_ERR, "Cannot allocate audio out buffer.\n");
105 return 0;
107 sh_audio->a_buffer_len = 0;
109 if (!sh_audio->ad_driver->init(sh_audio)) {
110 mp_tmsg(MSGT_DECAUDIO, MSGL_WARN, "ADecoder init failed :(\n");
111 uninit_audio(sh_audio); // free buffers
112 return 0;
115 sh_audio->initialized = 1;
117 if (!sh_audio->channels || !sh_audio->samplerate) {
118 mp_tmsg(MSGT_DECAUDIO, MSGL_WARN, "Unknown/missing audio format -> no sound\n");
119 uninit_audio(sh_audio); // free buffers
120 return 0;
123 if (!sh_audio->o_bps)
124 sh_audio->o_bps = sh_audio->channels * sh_audio->samplerate
125 * sh_audio->samplesize;
127 mp_msg(MSGT_DECAUDIO, MSGL_INFO,
128 "AUDIO: %d Hz, %d ch, %s, %3.1f kbit/%3.2f%% (ratio: %d->%d)\n",
129 sh_audio->samplerate, sh_audio->channels,
130 af_fmt2str_short(sh_audio->sample_format),
131 sh_audio->i_bps * 8 * 0.001,
132 ((float) sh_audio->i_bps / sh_audio->o_bps) * 100.0,
133 sh_audio->i_bps, sh_audio->o_bps);
134 mp_msg(MSGT_IDENTIFY, MSGL_INFO,
135 "ID_AUDIO_BITRATE=%d\nID_AUDIO_RATE=%d\n" "ID_AUDIO_NCH=%d\n",
136 sh_audio->i_bps * 8, sh_audio->samplerate, sh_audio->channels);
138 return 1;
141 static int init_audio(sh_audio_t *sh_audio, char *codecname, char *afm,
142 int status, stringset_t *selected)
144 unsigned int orig_fourcc = sh_audio->wf ? sh_audio->wf->wFormatTag : 0;
145 int force = 0;
146 if (codecname && codecname[0] == '+') {
147 codecname = &codecname[1];
148 force = 1;
150 sh_audio->codec = NULL;
151 while (1) {
152 const ad_functions_t *mpadec;
153 int i;
154 sh_audio->ad_driver = 0;
155 // restore original fourcc:
156 if (sh_audio->wf)
157 sh_audio->wf->wFormatTag = i = orig_fourcc;
158 if (!(sh_audio->codec = find_audio_codec(sh_audio->format,
159 sh_audio->wf ? (&i) : NULL,
160 sh_audio->codec, force)))
161 break;
162 if (sh_audio->wf)
163 sh_audio->wf->wFormatTag = i;
164 // ok we found one codec
165 if (stringset_test(selected, sh_audio->codec->name))
166 continue; // already tried & failed
167 if (codecname && strcmp(sh_audio->codec->name, codecname))
168 continue; // -ac
169 if (afm && strcmp(sh_audio->codec->drv, afm))
170 continue; // afm doesn't match
171 if (!force && sh_audio->codec->status < status)
172 continue; // too unstable
173 stringset_add(selected, sh_audio->codec->name); // tagging it
174 // ok, it matches all rules, let's find the driver!
175 for (i = 0; mpcodecs_ad_drivers[i] != NULL; i++)
176 if (!strcmp(mpcodecs_ad_drivers[i]->info->short_name,
177 sh_audio->codec->drv))
178 break;
179 mpadec = mpcodecs_ad_drivers[i];
180 #ifdef CONFIG_DYNAMIC_PLUGINS
181 if (!mpadec) {
182 /* try to open shared decoder plugin */
183 int buf_len;
184 char *buf;
185 ad_functions_t *funcs_sym;
186 ad_info_t *info_sym;
188 buf_len =
189 strlen(MPLAYER_LIBDIR) + strlen(sh_audio->codec->drv) + 16;
190 buf = malloc(buf_len);
191 if (!buf)
192 break;
193 snprintf(buf, buf_len, "%s/mplayer/ad_%s.so", MPLAYER_LIBDIR,
194 sh_audio->codec->drv);
195 mp_msg(MSGT_DECAUDIO, MSGL_DBG2,
196 "Trying to open external plugin: %s\n", buf);
197 sh_audio->dec_handle = dlopen(buf, RTLD_LAZY);
198 if (!sh_audio->dec_handle)
199 break;
200 snprintf(buf, buf_len, "mpcodecs_ad_%s", sh_audio->codec->drv);
201 funcs_sym = dlsym(sh_audio->dec_handle, buf);
202 if (!funcs_sym || !funcs_sym->info || !funcs_sym->preinit
203 || !funcs_sym->init || !funcs_sym->uninit
204 || !funcs_sym->control || !funcs_sym->decode_audio)
205 break;
206 info_sym = funcs_sym->info;
207 if (strcmp(info_sym->short_name, sh_audio->codec->drv))
208 break;
209 free(buf);
210 mpadec = funcs_sym;
211 mp_msg(MSGT_DECAUDIO, MSGL_V,
212 "Using external decoder plugin (%s/mplayer/ad_%s.so)!\n",
213 MPLAYER_LIBDIR, sh_audio->codec->drv);
215 #endif
216 if (!mpadec) { // driver not available (==compiled in)
217 mp_tmsg(MSGT_DECAUDIO, MSGL_ERR,
218 "Requested audio codec family [%s] (afm=%s) not available.\nEnable it at compilation.\n",
219 sh_audio->codec->name, sh_audio->codec->drv);
220 continue;
222 // it's available, let's try to init!
223 // init()
224 mp_tmsg(MSGT_DECAUDIO, MSGL_INFO, "Opening audio decoder: [%s] %s\n",
225 mpadec->info->short_name, mpadec->info->name);
226 sh_audio->ad_driver = mpadec;
227 if (!init_audio_codec(sh_audio)) {
228 mp_tmsg(MSGT_DECAUDIO, MSGL_WARN,
229 "Could not open audio decoder %s.\n",
230 mpadec->info->short_name);
231 continue; // try next...
233 // Yeah! We got it!
234 return 1;
236 return 0;
239 int init_best_audio_codec(sh_audio_t *sh_audio, char **audio_codec_list,
240 char **audio_fm_list)
242 stringset_t selected;
243 char *ac_l_default[2] = { "", (char *) NULL };
244 // hack:
245 if (!audio_codec_list)
246 audio_codec_list = ac_l_default;
247 // Go through the codec.conf and find the best codec...
248 sh_audio->initialized = 0;
249 stringset_init(&selected);
250 while (!sh_audio->initialized && *audio_codec_list) {
251 char *audio_codec = *(audio_codec_list++);
252 if (audio_codec[0]) {
253 if (audio_codec[0] == '-') {
254 // disable this codec:
255 stringset_add(&selected, audio_codec + 1);
256 } else {
257 // forced codec by name:
258 mp_tmsg(MSGT_DECAUDIO, MSGL_INFO, "Forced audio codec: %s\n",
259 audio_codec);
260 init_audio(sh_audio, audio_codec, NULL, -1, &selected);
262 } else {
263 int status;
264 // try in stability order: UNTESTED, WORKING, BUGGY.
265 // never try CRASHING.
266 if (audio_fm_list) {
267 char **fmlist = audio_fm_list;
268 // try first the preferred codec families:
269 while (!sh_audio->initialized && *fmlist) {
270 char *audio_fm = *(fmlist++);
271 mp_tmsg(MSGT_DECAUDIO, MSGL_INFO, "Trying to force audio codec driver family %s...\n",
272 audio_fm);
273 for (status = CODECS_STATUS__MAX;
274 status >= CODECS_STATUS__MIN; --status)
275 if (init_audio(sh_audio, NULL, audio_fm, status, &selected))
276 break;
279 if (!sh_audio->initialized)
280 for (status = CODECS_STATUS__MAX; status >= CODECS_STATUS__MIN;
281 --status)
282 if (init_audio(sh_audio, NULL, NULL, status, &selected))
283 break;
286 stringset_free(&selected);
288 if (!sh_audio->initialized) {
289 mp_tmsg(MSGT_DECAUDIO, MSGL_ERR, "Cannot find codec for audio format 0x%X.\n",
290 sh_audio->format);
291 return 0; // failed
294 mp_tmsg(MSGT_DECAUDIO, MSGL_INFO, "Selected audio codec: [%s] afm: %s (%s)\n",
295 sh_audio->codec->name, sh_audio->codec->drv, sh_audio->codec->info);
296 return 1; // success
299 void uninit_audio(sh_audio_t *sh_audio)
301 if (sh_audio->afilter) {
302 mp_msg(MSGT_DECAUDIO, MSGL_V, "Uninit audio filters...\n");
303 af_uninit(sh_audio->afilter);
304 free(sh_audio->afilter);
305 sh_audio->afilter = NULL;
307 if (sh_audio->initialized) {
308 mp_tmsg(MSGT_DECAUDIO, MSGL_V, "Uninit audio: %s\n",
309 sh_audio->codec->drv);
310 sh_audio->ad_driver->uninit(sh_audio);
311 #ifdef CONFIG_DYNAMIC_PLUGINS
312 if (sh_audio->dec_handle)
313 dlclose(sh_audio->dec_handle);
314 #endif
315 sh_audio->initialized = 0;
317 av_freep(&sh_audio->a_buffer);
318 av_freep(&sh_audio->a_in_buffer);
322 int init_audio_filters(sh_audio_t *sh_audio, int in_samplerate,
323 int *out_samplerate, int *out_channels, int *out_format)
325 af_stream_t *afs = sh_audio->afilter;
326 if (!afs) {
327 afs = calloc(1, sizeof(struct af_stream));
328 afs->opts = sh_audio->opts;
330 // input format: same as codec's output format:
331 afs->input.rate = in_samplerate;
332 afs->input.nch = sh_audio->channels;
333 afs->input.format = sh_audio->sample_format;
334 af_fix_parameters(&(afs->input));
336 // output format: same as ao driver's input format (if missing, fallback to input)
337 afs->output.rate = *out_samplerate;
338 afs->output.nch = *out_channels;
339 afs->output.format = *out_format;
340 af_fix_parameters(&(afs->output));
342 // filter config:
343 memcpy(&afs->cfg, &af_cfg, sizeof(af_cfg_t));
345 mp_tmsg(MSGT_DECAUDIO, MSGL_V, "Building audio filter chain for %dHz/%dch/%s -> %dHz/%dch/%s...\n",
346 afs->input.rate, afs->input.nch,
347 af_fmt2str_short(afs->input.format), afs->output.rate,
348 afs->output.nch, af_fmt2str_short(afs->output.format));
350 // let's autoprobe it!
351 if (0 != af_init(afs)) {
352 sh_audio->afilter = NULL;
353 free(afs);
354 return 0; // failed :(
357 *out_samplerate = afs->output.rate;
358 *out_channels = afs->output.nch;
359 *out_format = afs->output.format;
361 // ok!
362 sh_audio->afilter = (void *) afs;
363 return 1;
366 static void set_min_out_buffer_size(struct bstr *outbuf, int len)
368 size_t oldlen = talloc_get_size(outbuf->start);
369 if (oldlen < len) {
370 assert(outbuf->start); // talloc context should be already set
371 mp_msg(MSGT_DECAUDIO, MSGL_V, "Increasing filtered audio buffer size "
372 "from %zd to %d\n", oldlen, len);
373 outbuf->start = talloc_realloc_size(NULL, outbuf->start, len);
377 static int filter_n_bytes(sh_audio_t *sh, struct bstr *outbuf, int len)
379 assert(len-1 + sh->audio_out_minsize <= sh->a_buffer_size);
381 int error = 0;
383 // Decode more bytes if needed
384 int old_samplerate = sh->samplerate;
385 int old_channels = sh->channels;
386 int old_sample_format = sh->sample_format;
387 while (sh->a_buffer_len < len) {
388 unsigned char *buf = sh->a_buffer + sh->a_buffer_len;
389 int minlen = len - sh->a_buffer_len;
390 int maxlen = sh->a_buffer_size - sh->a_buffer_len;
391 int ret = sh->ad_driver->decode_audio(sh, buf, minlen, maxlen);
392 int format_change = sh->samplerate != old_samplerate
393 || sh->channels != old_channels
394 || sh->sample_format != old_sample_format;
395 if (ret <= 0 || format_change) {
396 error = format_change ? -2 : -1;
397 // samples from format-changing call get discarded too
398 len = sh->a_buffer_len;
399 break;
401 sh->a_buffer_len += ret;
404 // Filter
405 af_data_t filter_input = {
406 .audio = sh->a_buffer,
407 .len = len,
408 .rate = sh->samplerate,
409 .nch = sh->channels,
410 .format = sh->sample_format
412 af_fix_parameters(&filter_input);
413 af_data_t *filter_output = af_play(sh->afilter, &filter_input);
414 if (!filter_output)
415 return -1;
416 set_min_out_buffer_size(outbuf, outbuf->len + filter_output->len);
417 memcpy(outbuf->start + outbuf->len, filter_output->audio,
418 filter_output->len);
419 outbuf->len += filter_output->len;
421 // remove processed data from decoder buffer:
422 sh->a_buffer_len -= len;
423 memmove(sh->a_buffer, sh->a_buffer + len, sh->a_buffer_len);
425 return error;
428 /* Try to get at least minlen decoded+filtered bytes in outbuf
429 * (total length including possible existing data).
430 * Return 0 on success, -1 on error/EOF (not distinguished).
431 * In the former case outbuf->len is always >= minlen on return.
432 * In case of EOF/error it might or might not be.
433 * Outbuf.start must be talloc-allocated, and will be reallocated
434 * if needed to fit all filter output. */
435 int decode_audio(sh_audio_t *sh_audio, struct bstr *outbuf, int minlen)
437 // Indicates that a filter seems to be buffering large amounts of data
438 int huge_filter_buffer = 0;
439 // Decoded audio must be cut at boundaries of this many bytes
440 int unitsize = sh_audio->channels * sh_audio->samplesize * 16;
442 /* Filter output size will be about filter_multiplier times input size.
443 * If some filter buffers audio in big blocks this might only hold
444 * as average over time. */
445 double filter_multiplier = af_calc_filter_multiplier(sh_audio->afilter);
447 /* If the decoder set audio_out_minsize then it can do the equivalent of
448 * "while (output_len < target_len) output_len += audio_out_minsize;",
449 * so we must guarantee there is at least audio_out_minsize-1 bytes
450 * more space in the output buffer than the minimum length we try to
451 * decode. */
452 int max_decode_len = sh_audio->a_buffer_size - sh_audio->audio_out_minsize;
453 max_decode_len -= max_decode_len % unitsize;
455 while (outbuf->len < minlen) {
456 int declen = (minlen - outbuf->len) / filter_multiplier
457 + (unitsize << 5); // some extra for possible filter buffering
458 if (huge_filter_buffer)
459 /* Some filter must be doing significant buffering if the estimated
460 * input length didn't produce enough output from filters.
461 * Feed the filters 2k bytes at a time until we have enough output.
462 * Very small amounts could make filtering inefficient while large
463 * amounts can make MPlayer demux the file unnecessarily far ahead
464 * to get audio data and buffer video frames in memory while doing
465 * so. However the performance impact of either is probably not too
466 * significant as long as the value is not completely insane. */
467 declen = 2000;
468 declen -= declen % unitsize;
469 if (declen > max_decode_len)
470 declen = max_decode_len;
471 else
472 /* if this iteration does not fill buffer, we must have lots
473 * of buffering in filters */
474 huge_filter_buffer = 1;
475 int res = filter_n_bytes(sh_audio, outbuf, declen);
476 if (res < 0)
477 return res;
479 return 0;
482 void decode_audio_prepend_bytes(struct bstr *outbuf, int count, int byte)
484 set_min_out_buffer_size(outbuf, outbuf->len + count);
485 memmove(outbuf->start + count, outbuf->start, outbuf->len);
486 memset(outbuf->start, byte, count);
487 outbuf->len += count;
491 void resync_audio_stream(sh_audio_t *sh_audio)
493 sh_audio->a_in_buffer_len = 0; // clear audio input buffer
494 sh_audio->pts = MP_NOPTS_VALUE;
495 if (!sh_audio->initialized)
496 return;
497 sh_audio->ad_driver->control(sh_audio, ADCTRL_RESYNC_STREAM, NULL);
500 void skip_audio_frame(sh_audio_t *sh_audio)
502 if (!sh_audio->initialized)
503 return;
504 if (sh_audio->ad_driver->control(sh_audio, ADCTRL_SKIP_FRAME, NULL) ==
505 CONTROL_TRUE)
506 return;
507 // default skip code:
508 ds_fill_buffer(sh_audio->ds); // skip block