sd_ass: initialize structs for external tracks properly
[mplayer.git] / libmpcodecs / dec_audio.c
blob1343af422c22b4e518eee8ec68cad7b06071148f
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 int fakemono = 0;
42 af_cfg_t af_cfg = { 1, NULL }; // Configuration for audio filters
44 void afm_help(void)
46 int i;
47 mp_tmsg(MSGT_DECAUDIO, MSGL_INFO, "Available (compiled-in) audio codec families/drivers:\n");
48 mp_msg(MSGT_IDENTIFY, MSGL_INFO, "ID_AUDIO_DRIVERS\n");
49 mp_msg(MSGT_DECAUDIO, MSGL_INFO, " afm: info: (comment)\n");
50 for (i = 0; mpcodecs_ad_drivers[i] != NULL; i++)
51 if (mpcodecs_ad_drivers[i]->info->comment
52 && mpcodecs_ad_drivers[i]->info->comment[0])
53 mp_msg(MSGT_DECAUDIO, MSGL_INFO, "%9s %s (%s)\n",
54 mpcodecs_ad_drivers[i]->info->short_name,
55 mpcodecs_ad_drivers[i]->info->name,
56 mpcodecs_ad_drivers[i]->info->comment);
57 else
58 mp_msg(MSGT_DECAUDIO, MSGL_INFO, "%9s %s\n",
59 mpcodecs_ad_drivers[i]->info->short_name,
60 mpcodecs_ad_drivers[i]->info->name);
63 static int init_audio_codec(sh_audio_t *sh_audio)
65 assert(!sh_audio->initialized);
66 resync_audio_stream(sh_audio);
67 sh_audio->samplesize = 2;
68 sh_audio->sample_format = AF_FORMAT_S16_NE;
69 if ((af_cfg.force & AF_INIT_FORMAT_MASK) == AF_INIT_FLOAT) {
70 int fmt = AF_FORMAT_FLOAT_NE;
71 if (sh_audio->ad_driver->control(sh_audio, ADCTRL_QUERY_FORMAT,
72 &fmt) == CONTROL_TRUE) {
73 sh_audio->sample_format = fmt;
74 sh_audio->samplesize = 4;
77 sh_audio->audio_out_minsize = 8192; // default, preinit() may change it
78 if (!sh_audio->ad_driver->preinit(sh_audio)) {
79 mp_tmsg(MSGT_DECAUDIO, MSGL_ERR, "ADecoder preinit failed :(\n");
80 return 0;
83 /* allocate audio in buffer: */
84 if (sh_audio->audio_in_minsize > 0) {
85 sh_audio->a_in_buffer_size = sh_audio->audio_in_minsize;
86 mp_tmsg(MSGT_DECAUDIO, MSGL_V, "dec_audio: Allocating %d bytes for input buffer.\n",
87 sh_audio->a_in_buffer_size);
88 sh_audio->a_in_buffer = av_mallocz(sh_audio->a_in_buffer_size);
91 const int base_size = 65536;
92 // At least 64 KiB plus rounding up to next decodable unit size
93 sh_audio->a_buffer_size = base_size + sh_audio->audio_out_minsize;
95 mp_tmsg(MSGT_DECAUDIO, MSGL_V, "dec_audio: Allocating %d + %d = %d bytes for output buffer.\n",
96 sh_audio->audio_out_minsize, base_size, sh_audio->a_buffer_size);
98 sh_audio->a_buffer = av_mallocz(sh_audio->a_buffer_size);
99 if (!sh_audio->a_buffer)
100 abort();
101 sh_audio->a_buffer_len = 0;
103 if (!sh_audio->ad_driver->init(sh_audio)) {
104 mp_tmsg(MSGT_DECAUDIO, MSGL_V, "ADecoder init failed :(\n");
105 uninit_audio(sh_audio); // free buffers
106 return 0;
109 sh_audio->initialized = 1;
111 if (!sh_audio->channels || !sh_audio->samplerate) {
112 mp_tmsg(MSGT_DECAUDIO, MSGL_ERR, "Audio decoder did not specify "
113 "audio format!\n");
114 uninit_audio(sh_audio); // free buffers
115 return 0;
118 if (!sh_audio->o_bps)
119 sh_audio->o_bps = sh_audio->channels * sh_audio->samplerate
120 * sh_audio->samplesize;
121 return 1;
124 static int init_audio(sh_audio_t *sh_audio, char *codecname, char *afm,
125 int status, stringset_t *selected)
127 unsigned int orig_fourcc = sh_audio->wf ? sh_audio->wf->wFormatTag : 0;
128 int force = 0;
129 if (codecname && codecname[0] == '+') {
130 codecname = &codecname[1];
131 force = 1;
133 sh_audio->codec = NULL;
134 while (1) {
135 const ad_functions_t *mpadec;
136 int i;
137 sh_audio->ad_driver = 0;
138 // restore original fourcc:
139 if (sh_audio->wf)
140 sh_audio->wf->wFormatTag = i = orig_fourcc;
141 if (!(sh_audio->codec = find_audio_codec(sh_audio->format,
142 sh_audio->wf ? (&i) : NULL,
143 sh_audio->codec, force)))
144 break;
145 if (sh_audio->wf)
146 sh_audio->wf->wFormatTag = i;
147 // ok we found one codec
148 if (stringset_test(selected, sh_audio->codec->name))
149 continue; // already tried & failed
150 if (codecname && strcmp(sh_audio->codec->name, codecname))
151 continue; // -ac
152 if (afm && strcmp(sh_audio->codec->drv, afm))
153 continue; // afm doesn't match
154 if (!force && sh_audio->codec->status < status)
155 continue; // too unstable
156 stringset_add(selected, sh_audio->codec->name); // tagging it
157 // ok, it matches all rules, let's find the driver!
158 for (i = 0; mpcodecs_ad_drivers[i] != NULL; i++)
159 if (!strcmp(mpcodecs_ad_drivers[i]->info->short_name,
160 sh_audio->codec->drv))
161 break;
162 mpadec = mpcodecs_ad_drivers[i];
163 if (!mpadec) { // driver not available (==compiled in)
164 mp_tmsg(MSGT_DECAUDIO, MSGL_ERR,
165 "Requested audio codec family [%s] (afm=%s) not available.\nEnable it at compilation.\n",
166 sh_audio->codec->name, sh_audio->codec->drv);
167 continue;
169 // it's available, let's try to init!
170 // init()
171 mp_tmsg(MSGT_DECAUDIO, MSGL_V, "Opening audio decoder: [%s] %s\n",
172 mpadec->info->short_name, mpadec->info->name);
173 sh_audio->ad_driver = mpadec;
174 if (!init_audio_codec(sh_audio)) {
175 mp_tmsg(MSGT_DECAUDIO, MSGL_WARN, "Audio decoder init failed for "
176 "codecs.conf entry \"%s\".\n", sh_audio->codec->name);
177 continue; // try next...
179 // Yeah! We got it!
180 return 1;
182 return 0;
185 int init_best_audio_codec(sh_audio_t *sh_audio, char **audio_codec_list,
186 char **audio_fm_list)
188 stringset_t selected;
189 char *ac_l_default[2] = { "", (char *) NULL };
190 // hack:
191 if (!audio_codec_list)
192 audio_codec_list = ac_l_default;
193 // Go through the codec.conf and find the best codec...
194 sh_audio->initialized = 0;
195 stringset_init(&selected);
196 while (!sh_audio->initialized && *audio_codec_list) {
197 char *audio_codec = *(audio_codec_list++);
198 if (audio_codec[0]) {
199 if (audio_codec[0] == '-') {
200 // disable this codec:
201 stringset_add(&selected, audio_codec + 1);
202 } else {
203 // forced codec by name:
204 mp_tmsg(MSGT_DECAUDIO, MSGL_INFO, "Forced audio codec: %s\n",
205 audio_codec);
206 init_audio(sh_audio, audio_codec, NULL, -1, &selected);
208 } else {
209 int status;
210 // try in stability order: UNTESTED, WORKING, BUGGY.
211 // never try CRASHING.
212 if (audio_fm_list) {
213 char **fmlist = audio_fm_list;
214 // try first the preferred codec families:
215 while (!sh_audio->initialized && *fmlist) {
216 char *audio_fm = *(fmlist++);
217 mp_tmsg(MSGT_DECAUDIO, MSGL_INFO, "Trying to force audio codec driver family %s...\n",
218 audio_fm);
219 for (status = CODECS_STATUS__MAX;
220 status >= CODECS_STATUS__MIN; --status)
221 if (init_audio(sh_audio, NULL, audio_fm, status, &selected))
222 break;
225 if (!sh_audio->initialized)
226 for (status = CODECS_STATUS__MAX; status >= CODECS_STATUS__MIN;
227 --status)
228 if (init_audio(sh_audio, NULL, NULL, status, &selected))
229 break;
232 stringset_free(&selected);
234 if (!sh_audio->initialized) {
235 mp_tmsg(MSGT_DECAUDIO, MSGL_ERR, "Cannot find codec for audio format 0x%X.\n",
236 sh_audio->format);
237 return 0; // failed
240 mp_tmsg(MSGT_DECAUDIO, MSGL_INFO, "Selected audio codec: %s [%s]\n",
241 sh_audio->codecname ? sh_audio->codecname : sh_audio->codec->info,
242 sh_audio->ad_driver->info->print_name ?
243 sh_audio->ad_driver->info->print_name :
244 sh_audio->ad_driver->info->short_name);
245 mp_tmsg(MSGT_DECAUDIO, MSGL_V,
246 "Audio codecs.conf entry: %s (%s) afm: %s\n",
247 sh_audio->codec->name, sh_audio->codec->info, sh_audio->codec->drv);
248 mp_msg(MSGT_DECAUDIO, MSGL_INFO,
249 "AUDIO: %d Hz, %d ch, %s, %3.1f kbit/%3.2f%% (ratio: %d->%d)\n",
250 sh_audio->samplerate, sh_audio->channels,
251 af_fmt2str_short(sh_audio->sample_format),
252 sh_audio->i_bps * 8 * 0.001,
253 ((float) sh_audio->i_bps / sh_audio->o_bps) * 100.0,
254 sh_audio->i_bps, sh_audio->o_bps);
255 mp_msg(MSGT_IDENTIFY, MSGL_INFO,
256 "ID_AUDIO_BITRATE=%d\nID_AUDIO_RATE=%d\n" "ID_AUDIO_NCH=%d\n",
257 sh_audio->i_bps * 8, sh_audio->samplerate, sh_audio->channels);
259 return 1; // success
262 void uninit_audio(sh_audio_t *sh_audio)
264 if (sh_audio->afilter) {
265 mp_msg(MSGT_DECAUDIO, MSGL_V, "Uninit audio filters...\n");
266 af_uninit(sh_audio->afilter);
267 free(sh_audio->afilter);
268 sh_audio->afilter = NULL;
270 if (sh_audio->initialized) {
271 mp_tmsg(MSGT_DECAUDIO, MSGL_V, "Uninit audio: %s\n",
272 sh_audio->codec->drv);
273 sh_audio->ad_driver->uninit(sh_audio);
274 sh_audio->initialized = 0;
276 av_freep(&sh_audio->a_buffer);
277 av_freep(&sh_audio->a_in_buffer);
281 int init_audio_filters(sh_audio_t *sh_audio, int in_samplerate,
282 int *out_samplerate, int *out_channels, int *out_format)
284 af_stream_t *afs = sh_audio->afilter;
285 if (!afs) {
286 afs = calloc(1, sizeof(struct af_stream));
287 afs->opts = sh_audio->opts;
289 // input format: same as codec's output format:
290 afs->input.rate = in_samplerate;
291 afs->input.nch = sh_audio->channels;
292 afs->input.format = sh_audio->sample_format;
293 af_fix_parameters(&(afs->input));
295 // output format: same as ao driver's input format (if missing, fallback to input)
296 afs->output.rate = *out_samplerate;
297 afs->output.nch = *out_channels;
298 afs->output.format = *out_format;
299 af_fix_parameters(&(afs->output));
301 // filter config:
302 memcpy(&afs->cfg, &af_cfg, sizeof(af_cfg_t));
304 mp_tmsg(MSGT_DECAUDIO, MSGL_V, "Building audio filter chain for %dHz/%dch/%s -> %dHz/%dch/%s...\n",
305 afs->input.rate, afs->input.nch,
306 af_fmt2str_short(afs->input.format), afs->output.rate,
307 afs->output.nch, af_fmt2str_short(afs->output.format));
309 // let's autoprobe it!
310 if (0 != af_init(afs)) {
311 sh_audio->afilter = NULL;
312 free(afs);
313 return 0; // failed :(
316 *out_samplerate = afs->output.rate;
317 *out_channels = afs->output.nch;
318 *out_format = afs->output.format;
320 // ok!
321 sh_audio->afilter = (void *) afs;
322 return 1;
325 static void set_min_out_buffer_size(struct bstr *outbuf, int len)
327 size_t oldlen = talloc_get_size(outbuf->start);
328 if (oldlen < len) {
329 assert(outbuf->start); // talloc context should be already set
330 mp_msg(MSGT_DECAUDIO, MSGL_V, "Increasing filtered audio buffer size "
331 "from %zd to %d\n", oldlen, len);
332 outbuf->start = talloc_realloc_size(NULL, outbuf->start, len);
336 static int filter_n_bytes(sh_audio_t *sh, struct bstr *outbuf, int len)
338 assert(len-1 + sh->audio_out_minsize <= sh->a_buffer_size);
340 int error = 0;
342 // Decode more bytes if needed
343 int old_samplerate = sh->samplerate;
344 int old_channels = sh->channels;
345 int old_sample_format = sh->sample_format;
346 while (sh->a_buffer_len < len) {
347 unsigned char *buf = sh->a_buffer + sh->a_buffer_len;
348 int minlen = len - sh->a_buffer_len;
349 int maxlen = sh->a_buffer_size - sh->a_buffer_len;
350 int ret = sh->ad_driver->decode_audio(sh, buf, minlen, maxlen);
351 int format_change = sh->samplerate != old_samplerate
352 || sh->channels != old_channels
353 || sh->sample_format != old_sample_format;
354 if (ret <= 0 || format_change) {
355 error = format_change ? -2 : -1;
356 // samples from format-changing call get discarded too
357 len = sh->a_buffer_len;
358 break;
360 sh->a_buffer_len += ret;
363 // Filter
364 af_data_t filter_input = {
365 .audio = sh->a_buffer,
366 .len = len,
367 .rate = sh->samplerate,
368 .nch = sh->channels,
369 .format = sh->sample_format
371 af_fix_parameters(&filter_input);
372 af_data_t *filter_output = af_play(sh->afilter, &filter_input);
373 if (!filter_output)
374 return -1;
375 set_min_out_buffer_size(outbuf, outbuf->len + filter_output->len);
376 memcpy(outbuf->start + outbuf->len, filter_output->audio,
377 filter_output->len);
378 outbuf->len += filter_output->len;
380 // remove processed data from decoder buffer:
381 sh->a_buffer_len -= len;
382 memmove(sh->a_buffer, sh->a_buffer + len, sh->a_buffer_len);
384 return error;
387 /* Try to get at least minlen decoded+filtered bytes in outbuf
388 * (total length including possible existing data).
389 * Return 0 on success, -1 on error/EOF (not distinguished).
390 * In the former case outbuf->len is always >= minlen on return.
391 * In case of EOF/error it might or might not be.
392 * Outbuf.start must be talloc-allocated, and will be reallocated
393 * if needed to fit all filter output. */
394 int decode_audio(sh_audio_t *sh_audio, struct bstr *outbuf, int minlen)
396 // Indicates that a filter seems to be buffering large amounts of data
397 int huge_filter_buffer = 0;
398 // Decoded audio must be cut at boundaries of this many bytes
399 int unitsize = sh_audio->channels * sh_audio->samplesize * 16;
401 /* Filter output size will be about filter_multiplier times input size.
402 * If some filter buffers audio in big blocks this might only hold
403 * as average over time. */
404 double filter_multiplier = af_calc_filter_multiplier(sh_audio->afilter);
406 /* If the decoder set audio_out_minsize then it can do the equivalent of
407 * "while (output_len < target_len) output_len += audio_out_minsize;",
408 * so we must guarantee there is at least audio_out_minsize-1 bytes
409 * more space in the output buffer than the minimum length we try to
410 * decode. */
411 int max_decode_len = sh_audio->a_buffer_size - sh_audio->audio_out_minsize;
412 max_decode_len -= max_decode_len % unitsize;
414 while (outbuf->len < minlen) {
415 int declen = (minlen - outbuf->len) / filter_multiplier
416 + (unitsize << 5); // some extra for possible filter buffering
417 if (huge_filter_buffer)
418 /* Some filter must be doing significant buffering if the estimated
419 * input length didn't produce enough output from filters.
420 * Feed the filters 2k bytes at a time until we have enough output.
421 * Very small amounts could make filtering inefficient while large
422 * amounts can make MPlayer demux the file unnecessarily far ahead
423 * to get audio data and buffer video frames in memory while doing
424 * so. However the performance impact of either is probably not too
425 * significant as long as the value is not completely insane. */
426 declen = 2000;
427 declen -= declen % unitsize;
428 if (declen > max_decode_len)
429 declen = max_decode_len;
430 else
431 /* if this iteration does not fill buffer, we must have lots
432 * of buffering in filters */
433 huge_filter_buffer = 1;
434 int res = filter_n_bytes(sh_audio, outbuf, declen);
435 if (res < 0)
436 return res;
438 return 0;
441 void decode_audio_prepend_bytes(struct bstr *outbuf, int count, int byte)
443 set_min_out_buffer_size(outbuf, outbuf->len + count);
444 memmove(outbuf->start + count, outbuf->start, outbuf->len);
445 memset(outbuf->start, byte, count);
446 outbuf->len += count;
450 void resync_audio_stream(sh_audio_t *sh_audio)
452 sh_audio->a_in_buffer_len = 0; // clear audio input buffer
453 sh_audio->pts = MP_NOPTS_VALUE;
454 if (!sh_audio->initialized)
455 return;
456 sh_audio->ad_driver->control(sh_audio, ADCTRL_RESYNC_STREAM, NULL);
459 void skip_audio_frame(sh_audio_t *sh_audio)
461 if (!sh_audio->initialized)
462 return;
463 if (sh_audio->ad_driver->control(sh_audio, ADCTRL_SKIP_FRAME, NULL) ==
464 CONTROL_TRUE)
465 return;
466 // default skip code:
467 ds_fill_buffer(sh_audio->ds); // skip block