10 #include "stream/stream.h"
11 #include "libmpdemux/demuxer.h"
13 #include "codec-cfg.h"
14 #include "libmpdemux/stheader.h"
16 #include "dec_audio.h"
18 #include "libaf/af_format.h"
26 #ifdef CONFIG_DYNAMIC_PLUGINS
30 #ifdef CONFIG_FAKE_MONO
34 /* used for ac3surround decoder - set using -channels option */
35 int audio_output_channels
= 2;
36 af_cfg_t af_cfg
= { 1, NULL
}; // Configuration for audio filters
41 mp_msg(MSGT_DECAUDIO
, MSGL_INFO
, MSGTR_AvailableAudioFm
);
42 mp_msg(MSGT_IDENTIFY
, MSGL_INFO
, "ID_AUDIO_DRIVERS\n");
43 mp_msg(MSGT_DECAUDIO
, MSGL_INFO
, " afm: info: (comment)\n");
44 for (i
= 0; mpcodecs_ad_drivers
[i
] != NULL
; i
++)
45 if (mpcodecs_ad_drivers
[i
]->info
->comment
46 && mpcodecs_ad_drivers
[i
]->info
->comment
[0])
47 mp_msg(MSGT_DECAUDIO
, MSGL_INFO
, "%9s %s (%s)\n",
48 mpcodecs_ad_drivers
[i
]->info
->short_name
,
49 mpcodecs_ad_drivers
[i
]->info
->name
,
50 mpcodecs_ad_drivers
[i
]->info
->comment
);
52 mp_msg(MSGT_DECAUDIO
, MSGL_INFO
, "%9s %s\n",
53 mpcodecs_ad_drivers
[i
]->info
->short_name
,
54 mpcodecs_ad_drivers
[i
]->info
->name
);
57 static int init_audio_codec(sh_audio_t
*sh_audio
)
59 if ((af_cfg
.force
& AF_INIT_FORMAT_MASK
) == AF_INIT_FLOAT
) {
60 int fmt
= AF_FORMAT_FLOAT_NE
;
61 if (sh_audio
->ad_driver
->control(sh_audio
, ADCTRL_QUERY_FORMAT
,
62 &fmt
) == CONTROL_TRUE
) {
63 sh_audio
->sample_format
= fmt
;
64 sh_audio
->samplesize
= 4;
67 if (!sh_audio
->ad_driver
->preinit(sh_audio
)) {
68 mp_msg(MSGT_DECAUDIO
, MSGL_ERR
, MSGTR_ADecoderPreinitFailed
);
72 /* allocate audio in buffer: */
73 if (sh_audio
->audio_in_minsize
> 0) {
74 sh_audio
->a_in_buffer_size
= sh_audio
->audio_in_minsize
;
75 mp_msg(MSGT_DECAUDIO
, MSGL_V
, MSGTR_AllocatingBytesForInputBuffer
,
76 sh_audio
->a_in_buffer_size
);
77 sh_audio
->a_in_buffer
= memalign(16, sh_audio
->a_in_buffer_size
);
78 memset(sh_audio
->a_in_buffer
, 0, sh_audio
->a_in_buffer_size
);
79 sh_audio
->a_in_buffer_len
= 0;
82 sh_audio
->a_buffer_size
= sh_audio
->audio_out_minsize
+ MAX_OUTBURST
;
84 mp_msg(MSGT_DECAUDIO
, MSGL_V
, MSGTR_AllocatingBytesForOutputBuffer
,
85 sh_audio
->audio_out_minsize
, MAX_OUTBURST
, sh_audio
->a_buffer_size
);
87 sh_audio
->a_buffer
= memalign(16, sh_audio
->a_buffer_size
);
88 if (!sh_audio
->a_buffer
) {
89 mp_msg(MSGT_DECAUDIO
, MSGL_ERR
, MSGTR_CantAllocAudioBuf
);
92 memset(sh_audio
->a_buffer
, 0, sh_audio
->a_buffer_size
);
93 sh_audio
->a_buffer_len
= 0;
95 if (!sh_audio
->ad_driver
->init(sh_audio
)) {
96 mp_msg(MSGT_DECAUDIO
, MSGL_WARN
, MSGTR_ADecoderInitFailed
);
97 uninit_audio(sh_audio
); // free buffers
101 sh_audio
->initialized
= 1;
103 if (!sh_audio
->channels
|| !sh_audio
->samplerate
) {
104 mp_msg(MSGT_DECAUDIO
, MSGL_WARN
, MSGTR_UnknownAudio
);
105 uninit_audio(sh_audio
); // free buffers
109 if (!sh_audio
->o_bps
)
110 sh_audio
->o_bps
= sh_audio
->channels
* sh_audio
->samplerate
111 * sh_audio
->samplesize
;
113 mp_msg(MSGT_DECAUDIO
, MSGL_INFO
,
114 "AUDIO: %d Hz, %d ch, %s, %3.1f kbit/%3.2f%% (ratio: %d->%d)\n",
115 sh_audio
->samplerate
, sh_audio
->channels
,
116 af_fmt2str_short(sh_audio
->sample_format
),
117 sh_audio
->i_bps
* 8 * 0.001,
118 ((float) sh_audio
->i_bps
/ sh_audio
->o_bps
) * 100.0,
119 sh_audio
->i_bps
, sh_audio
->o_bps
);
120 mp_msg(MSGT_IDENTIFY
, MSGL_INFO
,
121 "ID_AUDIO_BITRATE=%d\nID_AUDIO_RATE=%d\n" "ID_AUDIO_NCH=%d\n",
122 sh_audio
->i_bps
* 8, sh_audio
->samplerate
, sh_audio
->channels
);
124 sh_audio
->a_out_buffer_size
= 0;
125 sh_audio
->a_out_buffer
= NULL
;
126 sh_audio
->a_out_buffer_len
= 0;
131 static int init_audio(sh_audio_t
*sh_audio
, char *codecname
, char *afm
,
132 int status
, stringset_t
*selected
)
134 unsigned int orig_fourcc
= sh_audio
->wf
? sh_audio
->wf
->wFormatTag
: 0;
136 if (codecname
&& codecname
[0] == '+') {
137 codecname
= &codecname
[1];
140 sh_audio
->codec
= NULL
;
142 ad_functions_t
*mpadec
;
144 sh_audio
->ad_driver
= 0;
145 // restore original fourcc:
147 sh_audio
->wf
->wFormatTag
= i
= orig_fourcc
;
148 if (!(sh_audio
->codec
= find_audio_codec(sh_audio
->format
,
149 sh_audio
->wf
? (&i
) : NULL
,
150 sh_audio
->codec
, force
)))
153 sh_audio
->wf
->wFormatTag
= i
;
154 // ok we found one codec
155 if (stringset_test(selected
, sh_audio
->codec
->name
))
156 continue; // already tried & failed
157 if (codecname
&& strcmp(sh_audio
->codec
->name
, codecname
))
159 if (afm
&& strcmp(sh_audio
->codec
->drv
, afm
))
160 continue; // afm doesn't match
161 if (!force
&& sh_audio
->codec
->status
< status
)
162 continue; // too unstable
163 stringset_add(selected
, sh_audio
->codec
->name
); // tagging it
164 // ok, it matches all rules, let's find the driver!
165 for (i
= 0; mpcodecs_ad_drivers
[i
] != NULL
; i
++)
166 if (!strcmp(mpcodecs_ad_drivers
[i
]->info
->short_name
,
167 sh_audio
->codec
->drv
))
169 mpadec
= mpcodecs_ad_drivers
[i
];
170 #ifdef CONFIG_DYNAMIC_PLUGINS
172 /* try to open shared decoder plugin */
175 ad_functions_t
*funcs_sym
;
179 strlen(MPLAYER_LIBDIR
) + strlen(sh_audio
->codec
->drv
) + 16;
180 buf
= malloc(buf_len
);
183 snprintf(buf
, buf_len
, "%s/mplayer/ad_%s.so", MPLAYER_LIBDIR
,
184 sh_audio
->codec
->drv
);
185 mp_msg(MSGT_DECAUDIO
, MSGL_DBG2
,
186 "Trying to open external plugin: %s\n", buf
);
187 sh_audio
->dec_handle
= dlopen(buf
, RTLD_LAZY
);
188 if (!sh_audio
->dec_handle
)
190 snprintf(buf
, buf_len
, "mpcodecs_ad_%s", sh_audio
->codec
->drv
);
191 funcs_sym
= dlsym(sh_audio
->dec_handle
, buf
);
192 if (!funcs_sym
|| !funcs_sym
->info
|| !funcs_sym
->preinit
193 || !funcs_sym
->init
|| !funcs_sym
->uninit
194 || !funcs_sym
->control
|| !funcs_sym
->decode_audio
)
196 info_sym
= funcs_sym
->info
;
197 if (strcmp(info_sym
->short_name
, sh_audio
->codec
->drv
))
201 mp_msg(MSGT_DECAUDIO
, MSGL_V
,
202 "Using external decoder plugin (%s/mplayer/ad_%s.so)!\n",
203 MPLAYER_LIBDIR
, sh_audio
->codec
->drv
);
206 if (!mpadec
) { // driver not available (==compiled in)
207 mp_msg(MSGT_DECAUDIO
, MSGL_ERR
,
208 MSGTR_AudioCodecFamilyNotAvailableStr
,
209 sh_audio
->codec
->name
, sh_audio
->codec
->drv
);
212 // it's available, let's try to init!
214 mp_msg(MSGT_DECAUDIO
, MSGL_INFO
, MSGTR_OpeningAudioDecoder
,
215 mpadec
->info
->short_name
, mpadec
->info
->name
);
216 sh_audio
->ad_driver
= mpadec
;
217 if (!init_audio_codec(sh_audio
)) {
218 mp_msg(MSGT_DECAUDIO
, MSGL_INFO
, MSGTR_ADecoderInitFailed
);
219 continue; // try next...
227 int init_best_audio_codec(sh_audio_t
*sh_audio
, char **audio_codec_list
,
228 char **audio_fm_list
)
230 stringset_t selected
;
231 char *ac_l_default
[2] = { "", (char *) NULL
};
233 if (!audio_codec_list
)
234 audio_codec_list
= ac_l_default
;
235 // Go through the codec.conf and find the best codec...
236 sh_audio
->initialized
= 0;
237 stringset_init(&selected
);
238 while (!sh_audio
->initialized
&& *audio_codec_list
) {
239 char *audio_codec
= *(audio_codec_list
++);
240 if (audio_codec
[0]) {
241 if (audio_codec
[0] == '-') {
242 // disable this codec:
243 stringset_add(&selected
, audio_codec
+ 1);
245 // forced codec by name:
246 mp_msg(MSGT_DECAUDIO
, MSGL_INFO
, MSGTR_ForcedAudioCodec
,
248 init_audio(sh_audio
, audio_codec
, NULL
, -1, &selected
);
252 // try in stability order: UNTESTED, WORKING, BUGGY.
253 // never try CRASHING.
255 char **fmlist
= audio_fm_list
;
256 // try first the preferred codec families:
257 while (!sh_audio
->initialized
&& *fmlist
) {
258 char *audio_fm
= *(fmlist
++);
259 mp_msg(MSGT_DECAUDIO
, MSGL_INFO
, MSGTR_TryForceAudioFmtStr
,
261 for (status
= CODECS_STATUS__MAX
;
262 status
>= CODECS_STATUS__MIN
; --status
)
263 if (init_audio(sh_audio
, NULL
, audio_fm
, status
, &selected
))
267 if (!sh_audio
->initialized
)
268 for (status
= CODECS_STATUS__MAX
; status
>= CODECS_STATUS__MIN
;
270 if (init_audio(sh_audio
, NULL
, NULL
, status
, &selected
))
274 stringset_free(&selected
);
276 if (!sh_audio
->initialized
) {
277 mp_msg(MSGT_DECAUDIO
, MSGL_ERR
, MSGTR_CantFindAudioCodec
,
279 mp_msg(MSGT_DECAUDIO
, MSGL_HINT
, MSGTR_RTFMCodecs
);
283 mp_msg(MSGT_DECAUDIO
, MSGL_INFO
, MSGTR_SelectedAudioCodec
,
284 sh_audio
->codec
->name
, sh_audio
->codec
->drv
, sh_audio
->codec
->info
);
288 void uninit_audio(sh_audio_t
*sh_audio
)
290 if (sh_audio
->afilter
) {
291 mp_msg(MSGT_DECAUDIO
, MSGL_V
, "Uninit audio filters...\n");
292 af_uninit(sh_audio
->afilter
);
293 free(sh_audio
->afilter
);
294 sh_audio
->afilter
= NULL
;
296 if (sh_audio
->initialized
) {
297 mp_msg(MSGT_DECAUDIO
, MSGL_V
, MSGTR_UninitAudioStr
,
298 sh_audio
->codec
->drv
);
299 sh_audio
->ad_driver
->uninit(sh_audio
);
300 #ifdef CONFIG_DYNAMIC_PLUGINS
301 if (sh_audio
->dec_handle
)
302 dlclose(sh_audio
->dec_handle
);
304 sh_audio
->initialized
= 0;
306 free(sh_audio
->a_out_buffer
);
307 sh_audio
->a_out_buffer
= NULL
;
308 sh_audio
->a_out_buffer_size
= 0;
309 if (sh_audio
->a_buffer
)
310 free(sh_audio
->a_buffer
);
311 sh_audio
->a_buffer
= NULL
;
312 if (sh_audio
->a_in_buffer
)
313 free(sh_audio
->a_in_buffer
);
314 sh_audio
->a_in_buffer
= NULL
;
318 int init_audio_filters(sh_audio_t
*sh_audio
, int in_samplerate
,
319 int *out_samplerate
, int *out_channels
, int *out_format
)
321 af_stream_t
*afs
= sh_audio
->afilter
;
323 afs
= malloc(sizeof(af_stream_t
));
324 memset(afs
, 0, sizeof(af_stream_t
));
326 // input format: same as codec's output format:
327 afs
->input
.rate
= in_samplerate
;
328 afs
->input
.nch
= sh_audio
->channels
;
329 afs
->input
.format
= sh_audio
->sample_format
;
330 af_fix_parameters(&(afs
->input
));
332 // output format: same as ao driver's input format (if missing, fallback to input)
333 afs
->output
.rate
= *out_samplerate
;
334 afs
->output
.nch
= *out_channels
;
335 afs
->output
.format
= *out_format
;
336 af_fix_parameters(&(afs
->output
));
339 memcpy(&afs
->cfg
, &af_cfg
, sizeof(af_cfg_t
));
341 mp_msg(MSGT_DECAUDIO
, MSGL_V
, MSGTR_BuildingAudioFilterChain
,
342 afs
->input
.rate
, afs
->input
.nch
,
343 af_fmt2str_short(afs
->input
.format
), afs
->output
.rate
,
344 afs
->output
.nch
, af_fmt2str_short(afs
->output
.format
));
346 // let's autoprobe it!
347 if (0 != af_init(afs
)) {
348 sh_audio
->afilter
= NULL
;
350 return 0; // failed :(
353 *out_samplerate
= afs
->output
.rate
;
354 *out_channels
= afs
->output
.nch
;
355 *out_format
= afs
->output
.format
;
357 sh_audio
->a_out_buffer_len
= 0;
360 sh_audio
->afilter
= (void *) afs
;
364 static int filter_n_bytes(sh_audio_t
*sh
, int len
)
368 af_data_t filter_input
= {
369 .audio
= sh
->a_buffer
,
370 .rate
= sh
->samplerate
,
372 .format
= sh
->sample_format
374 af_data_t
*filter_output
;
376 assert(len
-1 + sh
->audio_out_minsize
<= sh
->a_buffer_size
);
378 // Decode more bytes if needed
379 while (sh
->a_buffer_len
< len
) {
380 unsigned char *buf
= sh
->a_buffer
+ sh
->a_buffer_len
;
381 int minlen
= len
- sh
->a_buffer_len
;
382 int maxlen
= sh
->a_buffer_size
- sh
->a_buffer_len
;
383 int ret
= sh
->ad_driver
->decode_audio(sh
, buf
, minlen
, maxlen
);
386 len
= sh
->a_buffer_len
;
389 sh
->a_buffer_len
+= ret
;
392 filter_input
.len
= len
;
393 af_fix_parameters(&filter_input
);
394 filter_output
= af_play(sh
->afilter
, &filter_input
);
397 if (sh
->a_out_buffer_size
< sh
->a_out_buffer_len
+ filter_output
->len
) {
398 int newlen
= sh
->a_out_buffer_len
+ filter_output
->len
;
399 mp_msg(MSGT_DECAUDIO
, MSGL_V
, "Increasing filtered audio buffer size "
400 "from %d to %d\n", sh
->a_out_buffer_size
, newlen
);
401 sh
->a_out_buffer
= realloc(sh
->a_out_buffer
, newlen
);
402 sh
->a_out_buffer_size
= newlen
;
404 memcpy(sh
->a_out_buffer
+ sh
->a_out_buffer_len
, filter_output
->audio
,
406 sh
->a_out_buffer_len
+= filter_output
->len
;
408 // remove processed data from decoder buffer:
409 sh
->a_buffer_len
-= len
;
410 memmove(sh
->a_buffer
, sh
->a_buffer
+ len
, sh
->a_buffer_len
);
415 /* Try to get at least minlen decoded+filtered bytes in sh_audio->a_out_buffer
416 * (total length including possible existing data).
417 * Return 0 on success, -1 on error/EOF (not distinguished).
418 * In the former case sh_audio->a_out_buffer_len is always >= minlen
419 * on return. In case of EOF/error it might or might not be.
420 * Can reallocate sh_audio->a_out_buffer if needed to fit all filter output. */
421 int decode_audio(sh_audio_t
*sh_audio
, int minlen
)
423 // Indicates that a filter seems to be buffering large amounts of data
424 int huge_filter_buffer
= 0;
425 // Decoded audio must be cut at boundaries of this many bytes
426 int unitsize
= sh_audio
->channels
* sh_audio
->samplesize
* 16;
428 /* Filter output size will be about filter_multiplier times input size.
429 * If some filter buffers audio in big blocks this might only hold
430 * as average over time. */
431 double filter_multiplier
= af_calc_filter_multiplier(sh_audio
->afilter
);
433 /* If the decoder set audio_out_minsize then it can do the equivalent of
434 * "while (output_len < target_len) output_len += audio_out_minsize;",
435 * so we must guarantee there is at least audio_out_minsize-1 bytes
436 * more space in the output buffer than the minimum length we try to
438 int max_decode_len
= sh_audio
->a_buffer_size
- sh_audio
->audio_out_minsize
;
439 max_decode_len
-= max_decode_len
% unitsize
;
441 while (sh_audio
->a_out_buffer_len
< minlen
) {
442 int declen
= (minlen
- sh_audio
->a_out_buffer_len
) / filter_multiplier
443 + (unitsize
<< 5); // some extra for possible filter buffering
444 if (huge_filter_buffer
)
445 /* Some filter must be doing significant buffering if the estimated
446 * input length didn't produce enough output from filters.
447 * Feed the filters 2k bytes at a time until we have enough output.
448 * Very small amounts could make filtering inefficient while large
449 * amounts can make MPlayer demux the file unnecessarily far ahead
450 * to get audio data and buffer video frames in memory while doing
451 * so. However the performance impact of either is probably not too
452 * significant as long as the value is not completely insane. */
454 declen
-= declen
% unitsize
;
455 if (declen
> max_decode_len
)
456 declen
= max_decode_len
;
458 /* if this iteration does not fill buffer, we must have lots
459 * of buffering in filters */
460 huge_filter_buffer
= 1;
461 if (filter_n_bytes(sh_audio
, declen
) < 0)
467 void resync_audio_stream(sh_audio_t
*sh_audio
)
469 sh_audio
->a_in_buffer_len
= 0; // clear audio input buffer
470 if (!sh_audio
->initialized
)
472 sh_audio
->ad_driver
->control(sh_audio
, ADCTRL_RESYNC_STREAM
, NULL
);
475 void skip_audio_frame(sh_audio_t
*sh_audio
)
477 if (!sh_audio
->initialized
)
479 if (sh_audio
->ad_driver
->control(sh_audio
, ADCTRL_SKIP_FRAME
, NULL
) ==
482 // default skip code:
483 ds_fill_buffer(sh_audio
->ds
); // skip block