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.
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"
36 #include "libaf/af_format.h"
40 #ifdef CONFIG_DYNAMIC_PLUGINS
44 #ifdef CONFIG_FAKE_MONO
48 /* used for ac3surround decoder - set using -channels option */
49 int audio_output_channels
= 2;
50 af_cfg_t af_cfg
= { 1, NULL
}; // Configuration for audio filters
55 mp_tmsg(MSGT_DECAUDIO
, MSGL_INFO
, "Available (compiled-in) audio codec families/drivers:\n");
56 mp_msg(MSGT_IDENTIFY
, MSGL_INFO
, "ID_AUDIO_DRIVERS\n");
57 mp_msg(MSGT_DECAUDIO
, MSGL_INFO
, " afm: info: (comment)\n");
58 for (i
= 0; mpcodecs_ad_drivers
[i
] != NULL
; i
++)
59 if (mpcodecs_ad_drivers
[i
]->info
->comment
60 && mpcodecs_ad_drivers
[i
]->info
->comment
[0])
61 mp_msg(MSGT_DECAUDIO
, MSGL_INFO
, "%9s %s (%s)\n",
62 mpcodecs_ad_drivers
[i
]->info
->short_name
,
63 mpcodecs_ad_drivers
[i
]->info
->name
,
64 mpcodecs_ad_drivers
[i
]->info
->comment
);
66 mp_msg(MSGT_DECAUDIO
, MSGL_INFO
, "%9s %s\n",
67 mpcodecs_ad_drivers
[i
]->info
->short_name
,
68 mpcodecs_ad_drivers
[i
]->info
->name
);
71 static int init_audio_codec(sh_audio_t
*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 if (!sh_audio
->ad_driver
->preinit(sh_audio
)) {
82 mp_tmsg(MSGT_DECAUDIO
, MSGL_ERR
, "ADecoder preinit failed :(\n");
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
);
92 sh_audio
->a_in_buffer_len
= 0;
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");
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
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
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 sh_audio
->a_out_buffer_size
= 0;
139 sh_audio
->a_out_buffer
= NULL
;
140 sh_audio
->a_out_buffer_len
= 0;
145 static int init_audio(sh_audio_t
*sh_audio
, char *codecname
, char *afm
,
146 int status
, stringset_t
*selected
)
148 unsigned int orig_fourcc
= sh_audio
->wf
? sh_audio
->wf
->wFormatTag
: 0;
150 if (codecname
&& codecname
[0] == '+') {
151 codecname
= &codecname
[1];
154 sh_audio
->codec
= NULL
;
156 ad_functions_t
*mpadec
;
158 sh_audio
->ad_driver
= 0;
159 // restore original fourcc:
161 sh_audio
->wf
->wFormatTag
= i
= orig_fourcc
;
162 if (!(sh_audio
->codec
= find_audio_codec(sh_audio
->format
,
163 sh_audio
->wf
? (&i
) : NULL
,
164 sh_audio
->codec
, force
)))
167 sh_audio
->wf
->wFormatTag
= i
;
168 // ok we found one codec
169 if (stringset_test(selected
, sh_audio
->codec
->name
))
170 continue; // already tried & failed
171 if (codecname
&& strcmp(sh_audio
->codec
->name
, codecname
))
173 if (afm
&& strcmp(sh_audio
->codec
->drv
, afm
))
174 continue; // afm doesn't match
175 if (!force
&& sh_audio
->codec
->status
< status
)
176 continue; // too unstable
177 stringset_add(selected
, sh_audio
->codec
->name
); // tagging it
178 // ok, it matches all rules, let's find the driver!
179 for (i
= 0; mpcodecs_ad_drivers
[i
] != NULL
; i
++)
180 if (!strcmp(mpcodecs_ad_drivers
[i
]->info
->short_name
,
181 sh_audio
->codec
->drv
))
183 mpadec
= mpcodecs_ad_drivers
[i
];
184 #ifdef CONFIG_DYNAMIC_PLUGINS
186 /* try to open shared decoder plugin */
189 ad_functions_t
*funcs_sym
;
193 strlen(MPLAYER_LIBDIR
) + strlen(sh_audio
->codec
->drv
) + 16;
194 buf
= malloc(buf_len
);
197 snprintf(buf
, buf_len
, "%s/mplayer/ad_%s.so", MPLAYER_LIBDIR
,
198 sh_audio
->codec
->drv
);
199 mp_msg(MSGT_DECAUDIO
, MSGL_DBG2
,
200 "Trying to open external plugin: %s\n", buf
);
201 sh_audio
->dec_handle
= dlopen(buf
, RTLD_LAZY
);
202 if (!sh_audio
->dec_handle
)
204 snprintf(buf
, buf_len
, "mpcodecs_ad_%s", sh_audio
->codec
->drv
);
205 funcs_sym
= dlsym(sh_audio
->dec_handle
, buf
);
206 if (!funcs_sym
|| !funcs_sym
->info
|| !funcs_sym
->preinit
207 || !funcs_sym
->init
|| !funcs_sym
->uninit
208 || !funcs_sym
->control
|| !funcs_sym
->decode_audio
)
210 info_sym
= funcs_sym
->info
;
211 if (strcmp(info_sym
->short_name
, sh_audio
->codec
->drv
))
215 mp_msg(MSGT_DECAUDIO
, MSGL_V
,
216 "Using external decoder plugin (%s/mplayer/ad_%s.so)!\n",
217 MPLAYER_LIBDIR
, sh_audio
->codec
->drv
);
220 if (!mpadec
) { // driver not available (==compiled in)
221 mp_tmsg(MSGT_DECAUDIO
, MSGL_ERR
,
222 "Requested audio codec family [%s] (afm=%s) not available.\nEnable it at compilation.\n",
223 sh_audio
->codec
->name
, sh_audio
->codec
->drv
);
226 // it's available, let's try to init!
228 mp_tmsg(MSGT_DECAUDIO
, MSGL_INFO
, "Opening audio decoder: [%s] %s\n",
229 mpadec
->info
->short_name
, mpadec
->info
->name
);
230 sh_audio
->ad_driver
= mpadec
;
231 if (!init_audio_codec(sh_audio
)) {
232 mp_tmsg(MSGT_DECAUDIO
, MSGL_INFO
, "ADecoder init failed :(\n");
233 continue; // try next...
241 int init_best_audio_codec(sh_audio_t
*sh_audio
, char **audio_codec_list
,
242 char **audio_fm_list
)
244 stringset_t selected
;
245 char *ac_l_default
[2] = { "", (char *) NULL
};
247 if (!audio_codec_list
)
248 audio_codec_list
= ac_l_default
;
249 // Go through the codec.conf and find the best codec...
250 sh_audio
->initialized
= 0;
251 stringset_init(&selected
);
252 while (!sh_audio
->initialized
&& *audio_codec_list
) {
253 char *audio_codec
= *(audio_codec_list
++);
254 if (audio_codec
[0]) {
255 if (audio_codec
[0] == '-') {
256 // disable this codec:
257 stringset_add(&selected
, audio_codec
+ 1);
259 // forced codec by name:
260 mp_tmsg(MSGT_DECAUDIO
, MSGL_INFO
, "Forced audio codec: %s\n",
262 init_audio(sh_audio
, audio_codec
, NULL
, -1, &selected
);
266 // try in stability order: UNTESTED, WORKING, BUGGY.
267 // never try CRASHING.
269 char **fmlist
= audio_fm_list
;
270 // try first the preferred codec families:
271 while (!sh_audio
->initialized
&& *fmlist
) {
272 char *audio_fm
= *(fmlist
++);
273 mp_tmsg(MSGT_DECAUDIO
, MSGL_INFO
, "Trying to force audio codec driver family %s...\n",
275 for (status
= CODECS_STATUS__MAX
;
276 status
>= CODECS_STATUS__MIN
; --status
)
277 if (init_audio(sh_audio
, NULL
, audio_fm
, status
, &selected
))
281 if (!sh_audio
->initialized
)
282 for (status
= CODECS_STATUS__MAX
; status
>= CODECS_STATUS__MIN
;
284 if (init_audio(sh_audio
, NULL
, NULL
, status
, &selected
))
288 stringset_free(&selected
);
290 if (!sh_audio
->initialized
) {
291 mp_tmsg(MSGT_DECAUDIO
, MSGL_ERR
, "Cannot find codec for audio format 0x%X.\n",
296 mp_tmsg(MSGT_DECAUDIO
, MSGL_INFO
, "Selected audio codec: [%s] afm: %s (%s)\n",
297 sh_audio
->codec
->name
, sh_audio
->codec
->drv
, sh_audio
->codec
->info
);
301 void uninit_audio(sh_audio_t
*sh_audio
)
303 if (sh_audio
->afilter
) {
304 mp_msg(MSGT_DECAUDIO
, MSGL_V
, "Uninit audio filters...\n");
305 af_uninit(sh_audio
->afilter
);
306 free(sh_audio
->afilter
);
307 sh_audio
->afilter
= NULL
;
309 if (sh_audio
->initialized
) {
310 mp_tmsg(MSGT_DECAUDIO
, MSGL_V
, "Uninit audio: %s\n",
311 sh_audio
->codec
->drv
);
312 sh_audio
->ad_driver
->uninit(sh_audio
);
313 #ifdef CONFIG_DYNAMIC_PLUGINS
314 if (sh_audio
->dec_handle
)
315 dlclose(sh_audio
->dec_handle
);
317 sh_audio
->initialized
= 0;
319 free(sh_audio
->a_out_buffer
);
320 sh_audio
->a_out_buffer
= NULL
;
321 sh_audio
->a_out_buffer_size
= 0;
322 av_freep(&sh_audio
->a_buffer
);
323 av_freep(&sh_audio
->a_in_buffer
);
327 int init_audio_filters(sh_audio_t
*sh_audio
, int in_samplerate
,
328 int *out_samplerate
, int *out_channels
, int *out_format
)
330 af_stream_t
*afs
= sh_audio
->afilter
;
332 afs
= malloc(sizeof(af_stream_t
));
333 memset(afs
, 0, sizeof(af_stream_t
));
335 // input format: same as codec's output format:
336 afs
->input
.rate
= in_samplerate
;
337 afs
->input
.nch
= sh_audio
->channels
;
338 afs
->input
.format
= sh_audio
->sample_format
;
339 af_fix_parameters(&(afs
->input
));
341 // output format: same as ao driver's input format (if missing, fallback to input)
342 afs
->output
.rate
= *out_samplerate
;
343 afs
->output
.nch
= *out_channels
;
344 afs
->output
.format
= *out_format
;
345 af_fix_parameters(&(afs
->output
));
348 memcpy(&afs
->cfg
, &af_cfg
, sizeof(af_cfg_t
));
350 mp_tmsg(MSGT_DECAUDIO
, MSGL_V
, "Building audio filter chain for %dHz/%dch/%s -> %dHz/%dch/%s...\n",
351 afs
->input
.rate
, afs
->input
.nch
,
352 af_fmt2str_short(afs
->input
.format
), afs
->output
.rate
,
353 afs
->output
.nch
, af_fmt2str_short(afs
->output
.format
));
355 // let's autoprobe it!
356 if (0 != af_init(afs
)) {
357 sh_audio
->afilter
= NULL
;
359 return 0; // failed :(
362 *out_samplerate
= afs
->output
.rate
;
363 *out_channels
= afs
->output
.nch
;
364 *out_format
= afs
->output
.format
;
366 sh_audio
->a_out_buffer_len
= 0;
369 sh_audio
->afilter
= (void *) afs
;
373 static int filter_n_bytes(sh_audio_t
*sh
, int len
)
375 assert(len
-1 + sh
->audio_out_minsize
<= sh
->a_buffer_size
);
379 // Decode more bytes if needed
380 while (sh
->a_buffer_len
< len
) {
381 unsigned char *buf
= sh
->a_buffer
+ sh
->a_buffer_len
;
382 int minlen
= len
- sh
->a_buffer_len
;
383 int maxlen
= sh
->a_buffer_size
- sh
->a_buffer_len
;
384 int ret
= sh
->ad_driver
->decode_audio(sh
, buf
, minlen
, maxlen
);
387 len
= sh
->a_buffer_len
;
390 sh
->a_buffer_len
+= ret
;
394 af_data_t filter_input
= {
395 .audio
= sh
->a_buffer
,
397 .rate
= sh
->samplerate
,
399 .format
= sh
->sample_format
401 af_fix_parameters(&filter_input
);
402 af_data_t
*filter_output
= af_play(sh
->afilter
, &filter_input
);
405 if (sh
->a_out_buffer_size
< sh
->a_out_buffer_len
+ filter_output
->len
) {
406 int newlen
= sh
->a_out_buffer_len
+ filter_output
->len
;
407 mp_msg(MSGT_DECAUDIO
, MSGL_V
, "Increasing filtered audio buffer size "
408 "from %d to %d\n", sh
->a_out_buffer_size
, newlen
);
409 sh
->a_out_buffer
= realloc(sh
->a_out_buffer
, newlen
);
410 sh
->a_out_buffer_size
= newlen
;
412 memcpy(sh
->a_out_buffer
+ sh
->a_out_buffer_len
, filter_output
->audio
,
414 sh
->a_out_buffer_len
+= filter_output
->len
;
416 // remove processed data from decoder buffer:
417 sh
->a_buffer_len
-= len
;
418 memmove(sh
->a_buffer
, sh
->a_buffer
+ len
, sh
->a_buffer_len
);
423 /* Try to get at least minlen decoded+filtered bytes in sh_audio->a_out_buffer
424 * (total length including possible existing data).
425 * Return 0 on success, -1 on error/EOF (not distinguished).
426 * In the former case sh_audio->a_out_buffer_len is always >= minlen
427 * on return. In case of EOF/error it might or might not be.
428 * Can reallocate sh_audio->a_out_buffer if needed to fit all filter output. */
429 int decode_audio(sh_audio_t
*sh_audio
, int minlen
)
431 // Indicates that a filter seems to be buffering large amounts of data
432 int huge_filter_buffer
= 0;
433 // Decoded audio must be cut at boundaries of this many bytes
434 int unitsize
= sh_audio
->channels
* sh_audio
->samplesize
* 16;
436 /* Filter output size will be about filter_multiplier times input size.
437 * If some filter buffers audio in big blocks this might only hold
438 * as average over time. */
439 double filter_multiplier
= af_calc_filter_multiplier(sh_audio
->afilter
);
441 /* If the decoder set audio_out_minsize then it can do the equivalent of
442 * "while (output_len < target_len) output_len += audio_out_minsize;",
443 * so we must guarantee there is at least audio_out_minsize-1 bytes
444 * more space in the output buffer than the minimum length we try to
446 int max_decode_len
= sh_audio
->a_buffer_size
- sh_audio
->audio_out_minsize
;
447 max_decode_len
-= max_decode_len
% unitsize
;
449 while (sh_audio
->a_out_buffer_len
< minlen
) {
450 int declen
= (minlen
- sh_audio
->a_out_buffer_len
) / filter_multiplier
451 + (unitsize
<< 5); // some extra for possible filter buffering
452 if (huge_filter_buffer
)
453 /* Some filter must be doing significant buffering if the estimated
454 * input length didn't produce enough output from filters.
455 * Feed the filters 2k bytes at a time until we have enough output.
456 * Very small amounts could make filtering inefficient while large
457 * amounts can make MPlayer demux the file unnecessarily far ahead
458 * to get audio data and buffer video frames in memory while doing
459 * so. However the performance impact of either is probably not too
460 * significant as long as the value is not completely insane. */
462 declen
-= declen
% unitsize
;
463 if (declen
> max_decode_len
)
464 declen
= max_decode_len
;
466 /* if this iteration does not fill buffer, we must have lots
467 * of buffering in filters */
468 huge_filter_buffer
= 1;
469 if (filter_n_bytes(sh_audio
, declen
) < 0)
475 void resync_audio_stream(sh_audio_t
*sh_audio
)
477 sh_audio
->a_in_buffer_len
= 0; // clear audio input buffer
478 if (!sh_audio
->initialized
)
480 sh_audio
->ad_driver
->control(sh_audio
, ADCTRL_RESYNC_STREAM
, NULL
);
483 void skip_audio_frame(sh_audio_t
*sh_audio
)
485 if (!sh_audio
->initialized
)
487 if (sh_audio
->ad_driver
->control(sh_audio
, ADCTRL_SKIP_FRAME
, NULL
) ==
490 // default skip code:
491 ds_fill_buffer(sh_audio
->ds
); // skip block