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_FAKE_MONO
44 af_cfg_t af_cfg
= { 1, NULL
}; // Configuration for audio filters
49 mp_tmsg(MSGT_DECAUDIO
, MSGL_INFO
, "Available (compiled-in) audio codec families/drivers:\n");
50 mp_msg(MSGT_IDENTIFY
, MSGL_INFO
, "ID_AUDIO_DRIVERS\n");
51 mp_msg(MSGT_DECAUDIO
, MSGL_INFO
, " afm: info: (comment)\n");
52 for (i
= 0; mpcodecs_ad_drivers
[i
] != NULL
; i
++)
53 if (mpcodecs_ad_drivers
[i
]->info
->comment
54 && mpcodecs_ad_drivers
[i
]->info
->comment
[0])
55 mp_msg(MSGT_DECAUDIO
, MSGL_INFO
, "%9s %s (%s)\n",
56 mpcodecs_ad_drivers
[i
]->info
->short_name
,
57 mpcodecs_ad_drivers
[i
]->info
->name
,
58 mpcodecs_ad_drivers
[i
]->info
->comment
);
60 mp_msg(MSGT_DECAUDIO
, MSGL_INFO
, "%9s %s\n",
61 mpcodecs_ad_drivers
[i
]->info
->short_name
,
62 mpcodecs_ad_drivers
[i
]->info
->name
);
65 static int init_audio_codec(sh_audio_t
*sh_audio
)
67 assert(!sh_audio
->initialized
);
68 resync_audio_stream(sh_audio
);
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");
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 mp_tmsg(MSGT_DECAUDIO
, MSGL_ERR
, "Cannot allocate audio out buffer.\n");
103 sh_audio
->a_buffer_len
= 0;
105 if (!sh_audio
->ad_driver
->init(sh_audio
)) {
106 mp_tmsg(MSGT_DECAUDIO
, MSGL_WARN
, "ADecoder init failed :(\n");
107 uninit_audio(sh_audio
); // free buffers
111 sh_audio
->initialized
= 1;
113 if (!sh_audio
->channels
|| !sh_audio
->samplerate
) {
114 mp_tmsg(MSGT_DECAUDIO
, MSGL_WARN
, "Unknown/missing audio format -> no sound\n");
115 uninit_audio(sh_audio
); // free buffers
119 if (!sh_audio
->o_bps
)
120 sh_audio
->o_bps
= sh_audio
->channels
* sh_audio
->samplerate
121 * sh_audio
->samplesize
;
123 mp_msg(MSGT_DECAUDIO
, MSGL_INFO
,
124 "AUDIO: %d Hz, %d ch, %s, %3.1f kbit/%3.2f%% (ratio: %d->%d)\n",
125 sh_audio
->samplerate
, sh_audio
->channels
,
126 af_fmt2str_short(sh_audio
->sample_format
),
127 sh_audio
->i_bps
* 8 * 0.001,
128 ((float) sh_audio
->i_bps
/ sh_audio
->o_bps
) * 100.0,
129 sh_audio
->i_bps
, sh_audio
->o_bps
);
130 mp_msg(MSGT_IDENTIFY
, MSGL_INFO
,
131 "ID_AUDIO_BITRATE=%d\nID_AUDIO_RATE=%d\n" "ID_AUDIO_NCH=%d\n",
132 sh_audio
->i_bps
* 8, sh_audio
->samplerate
, sh_audio
->channels
);
137 static int init_audio(sh_audio_t
*sh_audio
, char *codecname
, char *afm
,
138 int status
, stringset_t
*selected
)
140 unsigned int orig_fourcc
= sh_audio
->wf
? sh_audio
->wf
->wFormatTag
: 0;
142 if (codecname
&& codecname
[0] == '+') {
143 codecname
= &codecname
[1];
146 sh_audio
->codec
= NULL
;
148 const ad_functions_t
*mpadec
;
150 sh_audio
->ad_driver
= 0;
151 // restore original fourcc:
153 sh_audio
->wf
->wFormatTag
= i
= orig_fourcc
;
154 if (!(sh_audio
->codec
= find_audio_codec(sh_audio
->format
,
155 sh_audio
->wf
? (&i
) : NULL
,
156 sh_audio
->codec
, force
)))
159 sh_audio
->wf
->wFormatTag
= i
;
160 // ok we found one codec
161 if (stringset_test(selected
, sh_audio
->codec
->name
))
162 continue; // already tried & failed
163 if (codecname
&& strcmp(sh_audio
->codec
->name
, codecname
))
165 if (afm
&& strcmp(sh_audio
->codec
->drv
, afm
))
166 continue; // afm doesn't match
167 if (!force
&& sh_audio
->codec
->status
< status
)
168 continue; // too unstable
169 stringset_add(selected
, sh_audio
->codec
->name
); // tagging it
170 // ok, it matches all rules, let's find the driver!
171 for (i
= 0; mpcodecs_ad_drivers
[i
] != NULL
; i
++)
172 if (!strcmp(mpcodecs_ad_drivers
[i
]->info
->short_name
,
173 sh_audio
->codec
->drv
))
175 mpadec
= mpcodecs_ad_drivers
[i
];
176 if (!mpadec
) { // driver not available (==compiled in)
177 mp_tmsg(MSGT_DECAUDIO
, MSGL_ERR
,
178 "Requested audio codec family [%s] (afm=%s) not available.\nEnable it at compilation.\n",
179 sh_audio
->codec
->name
, sh_audio
->codec
->drv
);
182 // it's available, let's try to init!
184 mp_tmsg(MSGT_DECAUDIO
, MSGL_INFO
, "Opening audio decoder: [%s] %s\n",
185 mpadec
->info
->short_name
, mpadec
->info
->name
);
186 sh_audio
->ad_driver
= mpadec
;
187 if (!init_audio_codec(sh_audio
)) {
188 mp_tmsg(MSGT_DECAUDIO
, MSGL_WARN
,
189 "Could not open audio decoder %s.\n",
190 mpadec
->info
->short_name
);
191 continue; // try next...
199 int init_best_audio_codec(sh_audio_t
*sh_audio
, char **audio_codec_list
,
200 char **audio_fm_list
)
202 stringset_t selected
;
203 char *ac_l_default
[2] = { "", (char *) NULL
};
205 if (!audio_codec_list
)
206 audio_codec_list
= ac_l_default
;
207 // Go through the codec.conf and find the best codec...
208 sh_audio
->initialized
= 0;
209 stringset_init(&selected
);
210 while (!sh_audio
->initialized
&& *audio_codec_list
) {
211 char *audio_codec
= *(audio_codec_list
++);
212 if (audio_codec
[0]) {
213 if (audio_codec
[0] == '-') {
214 // disable this codec:
215 stringset_add(&selected
, audio_codec
+ 1);
217 // forced codec by name:
218 mp_tmsg(MSGT_DECAUDIO
, MSGL_INFO
, "Forced audio codec: %s\n",
220 init_audio(sh_audio
, audio_codec
, NULL
, -1, &selected
);
224 // try in stability order: UNTESTED, WORKING, BUGGY.
225 // never try CRASHING.
227 char **fmlist
= audio_fm_list
;
228 // try first the preferred codec families:
229 while (!sh_audio
->initialized
&& *fmlist
) {
230 char *audio_fm
= *(fmlist
++);
231 mp_tmsg(MSGT_DECAUDIO
, MSGL_INFO
, "Trying to force audio codec driver family %s...\n",
233 for (status
= CODECS_STATUS__MAX
;
234 status
>= CODECS_STATUS__MIN
; --status
)
235 if (init_audio(sh_audio
, NULL
, audio_fm
, status
, &selected
))
239 if (!sh_audio
->initialized
)
240 for (status
= CODECS_STATUS__MAX
; status
>= CODECS_STATUS__MIN
;
242 if (init_audio(sh_audio
, NULL
, NULL
, status
, &selected
))
246 stringset_free(&selected
);
248 if (!sh_audio
->initialized
) {
249 mp_tmsg(MSGT_DECAUDIO
, MSGL_ERR
, "Cannot find codec for audio format 0x%X.\n",
254 mp_tmsg(MSGT_DECAUDIO
, MSGL_INFO
, "Selected audio codec: [%s] afm: %s (%s)\n",
255 sh_audio
->codec
->name
, sh_audio
->codec
->drv
, sh_audio
->codec
->info
);
259 void uninit_audio(sh_audio_t
*sh_audio
)
261 if (sh_audio
->afilter
) {
262 mp_msg(MSGT_DECAUDIO
, MSGL_V
, "Uninit audio filters...\n");
263 af_uninit(sh_audio
->afilter
);
264 free(sh_audio
->afilter
);
265 sh_audio
->afilter
= NULL
;
267 if (sh_audio
->initialized
) {
268 mp_tmsg(MSGT_DECAUDIO
, MSGL_V
, "Uninit audio: %s\n",
269 sh_audio
->codec
->drv
);
270 sh_audio
->ad_driver
->uninit(sh_audio
);
271 sh_audio
->initialized
= 0;
273 av_freep(&sh_audio
->a_buffer
);
274 av_freep(&sh_audio
->a_in_buffer
);
278 int init_audio_filters(sh_audio_t
*sh_audio
, int in_samplerate
,
279 int *out_samplerate
, int *out_channels
, int *out_format
)
281 af_stream_t
*afs
= sh_audio
->afilter
;
283 afs
= calloc(1, sizeof(struct af_stream
));
284 afs
->opts
= sh_audio
->opts
;
286 // input format: same as codec's output format:
287 afs
->input
.rate
= in_samplerate
;
288 afs
->input
.nch
= sh_audio
->channels
;
289 afs
->input
.format
= sh_audio
->sample_format
;
290 af_fix_parameters(&(afs
->input
));
292 // output format: same as ao driver's input format (if missing, fallback to input)
293 afs
->output
.rate
= *out_samplerate
;
294 afs
->output
.nch
= *out_channels
;
295 afs
->output
.format
= *out_format
;
296 af_fix_parameters(&(afs
->output
));
299 memcpy(&afs
->cfg
, &af_cfg
, sizeof(af_cfg_t
));
301 mp_tmsg(MSGT_DECAUDIO
, MSGL_V
, "Building audio filter chain for %dHz/%dch/%s -> %dHz/%dch/%s...\n",
302 afs
->input
.rate
, afs
->input
.nch
,
303 af_fmt2str_short(afs
->input
.format
), afs
->output
.rate
,
304 afs
->output
.nch
, af_fmt2str_short(afs
->output
.format
));
306 // let's autoprobe it!
307 if (0 != af_init(afs
)) {
308 sh_audio
->afilter
= NULL
;
310 return 0; // failed :(
313 *out_samplerate
= afs
->output
.rate
;
314 *out_channels
= afs
->output
.nch
;
315 *out_format
= afs
->output
.format
;
318 sh_audio
->afilter
= (void *) afs
;
322 static void set_min_out_buffer_size(struct bstr
*outbuf
, int len
)
324 size_t oldlen
= talloc_get_size(outbuf
->start
);
326 assert(outbuf
->start
); // talloc context should be already set
327 mp_msg(MSGT_DECAUDIO
, MSGL_V
, "Increasing filtered audio buffer size "
328 "from %zd to %d\n", oldlen
, len
);
329 outbuf
->start
= talloc_realloc_size(NULL
, outbuf
->start
, len
);
333 static int filter_n_bytes(sh_audio_t
*sh
, struct bstr
*outbuf
, int len
)
335 assert(len
-1 + sh
->audio_out_minsize
<= sh
->a_buffer_size
);
339 // Decode more bytes if needed
340 int old_samplerate
= sh
->samplerate
;
341 int old_channels
= sh
->channels
;
342 int old_sample_format
= sh
->sample_format
;
343 while (sh
->a_buffer_len
< len
) {
344 unsigned char *buf
= sh
->a_buffer
+ sh
->a_buffer_len
;
345 int minlen
= len
- sh
->a_buffer_len
;
346 int maxlen
= sh
->a_buffer_size
- sh
->a_buffer_len
;
347 int ret
= sh
->ad_driver
->decode_audio(sh
, buf
, minlen
, maxlen
);
348 int format_change
= sh
->samplerate
!= old_samplerate
349 || sh
->channels
!= old_channels
350 || sh
->sample_format
!= old_sample_format
;
351 if (ret
<= 0 || format_change
) {
352 error
= format_change
? -2 : -1;
353 // samples from format-changing call get discarded too
354 len
= sh
->a_buffer_len
;
357 sh
->a_buffer_len
+= ret
;
361 af_data_t filter_input
= {
362 .audio
= sh
->a_buffer
,
364 .rate
= sh
->samplerate
,
366 .format
= sh
->sample_format
368 af_fix_parameters(&filter_input
);
369 af_data_t
*filter_output
= af_play(sh
->afilter
, &filter_input
);
372 set_min_out_buffer_size(outbuf
, outbuf
->len
+ filter_output
->len
);
373 memcpy(outbuf
->start
+ outbuf
->len
, filter_output
->audio
,
375 outbuf
->len
+= filter_output
->len
;
377 // remove processed data from decoder buffer:
378 sh
->a_buffer_len
-= len
;
379 memmove(sh
->a_buffer
, sh
->a_buffer
+ len
, sh
->a_buffer_len
);
384 /* Try to get at least minlen decoded+filtered bytes in outbuf
385 * (total length including possible existing data).
386 * Return 0 on success, -1 on error/EOF (not distinguished).
387 * In the former case outbuf->len is always >= minlen on return.
388 * In case of EOF/error it might or might not be.
389 * Outbuf.start must be talloc-allocated, and will be reallocated
390 * if needed to fit all filter output. */
391 int decode_audio(sh_audio_t
*sh_audio
, struct bstr
*outbuf
, int minlen
)
393 // Indicates that a filter seems to be buffering large amounts of data
394 int huge_filter_buffer
= 0;
395 // Decoded audio must be cut at boundaries of this many bytes
396 int unitsize
= sh_audio
->channels
* sh_audio
->samplesize
* 16;
398 /* Filter output size will be about filter_multiplier times input size.
399 * If some filter buffers audio in big blocks this might only hold
400 * as average over time. */
401 double filter_multiplier
= af_calc_filter_multiplier(sh_audio
->afilter
);
403 /* If the decoder set audio_out_minsize then it can do the equivalent of
404 * "while (output_len < target_len) output_len += audio_out_minsize;",
405 * so we must guarantee there is at least audio_out_minsize-1 bytes
406 * more space in the output buffer than the minimum length we try to
408 int max_decode_len
= sh_audio
->a_buffer_size
- sh_audio
->audio_out_minsize
;
409 max_decode_len
-= max_decode_len
% unitsize
;
411 while (outbuf
->len
< minlen
) {
412 int declen
= (minlen
- outbuf
->len
) / filter_multiplier
413 + (unitsize
<< 5); // some extra for possible filter buffering
414 if (huge_filter_buffer
)
415 /* Some filter must be doing significant buffering if the estimated
416 * input length didn't produce enough output from filters.
417 * Feed the filters 2k bytes at a time until we have enough output.
418 * Very small amounts could make filtering inefficient while large
419 * amounts can make MPlayer demux the file unnecessarily far ahead
420 * to get audio data and buffer video frames in memory while doing
421 * so. However the performance impact of either is probably not too
422 * significant as long as the value is not completely insane. */
424 declen
-= declen
% unitsize
;
425 if (declen
> max_decode_len
)
426 declen
= max_decode_len
;
428 /* if this iteration does not fill buffer, we must have lots
429 * of buffering in filters */
430 huge_filter_buffer
= 1;
431 int res
= filter_n_bytes(sh_audio
, outbuf
, declen
);
438 void decode_audio_prepend_bytes(struct bstr
*outbuf
, int count
, int byte
)
440 set_min_out_buffer_size(outbuf
, outbuf
->len
+ count
);
441 memmove(outbuf
->start
+ count
, outbuf
->start
, outbuf
->len
);
442 memset(outbuf
->start
, byte
, count
);
443 outbuf
->len
+= count
;
447 void resync_audio_stream(sh_audio_t
*sh_audio
)
449 sh_audio
->a_in_buffer_len
= 0; // clear audio input buffer
450 sh_audio
->pts
= MP_NOPTS_VALUE
;
451 if (!sh_audio
->initialized
)
453 sh_audio
->ad_driver
->control(sh_audio
, ADCTRL_RESYNC_STREAM
, NULL
);
456 void skip_audio_frame(sh_audio_t
*sh_audio
)
458 if (!sh_audio
->initialized
)
460 if (sh_audio
->ad_driver
->control(sh_audio
, ADCTRL_SKIP_FRAME
, NULL
) ==
463 // default skip code:
464 ds_fill_buffer(sh_audio
->ds
); // skip block