2002-03-25 21:23:06 +00:00
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <unistd.h>
|
2007-11-01 06:52:19 +00:00
|
|
|
#include <assert.h>
|
2002-03-25 21:23:06 +00:00
|
|
|
|
|
|
|
#include "config.h"
|
|
|
|
#include "mp_msg.h"
|
|
|
|
#include "help_mp.h"
|
|
|
|
|
2007-03-15 17:10:36 +00:00
|
|
|
#include "stream/stream.h"
|
2007-03-15 17:51:32 +00:00
|
|
|
#include "libmpdemux/demuxer.h"
|
2002-03-25 21:23:06 +00:00
|
|
|
|
|
|
|
#include "codec-cfg.h"
|
2007-03-15 17:51:32 +00:00
|
|
|
#include "libmpdemux/stheader.h"
|
2002-03-25 21:23:06 +00:00
|
|
|
|
|
|
|
#include "dec_audio.h"
|
|
|
|
#include "ad.h"
|
2005-11-18 14:39:25 +00:00
|
|
|
#include "libaf/af_format.h"
|
2002-03-25 21:23:06 +00:00
|
|
|
|
2005-11-18 14:39:25 +00:00
|
|
|
#include "libaf/af.h"
|
2002-10-05 22:55:45 +00:00
|
|
|
|
2006-02-16 22:43:42 +00:00
|
|
|
#ifdef HAVE_MALLOC_H
|
|
|
|
#include <malloc.h>
|
|
|
|
#endif
|
|
|
|
|
2002-11-11 17:28:41 +00:00
|
|
|
#ifdef DYNAMIC_PLUGINS
|
|
|
|
#include <dlfcn.h>
|
|
|
|
#endif
|
|
|
|
|
2002-03-25 21:23:06 +00:00
|
|
|
#ifdef USE_FAKE_MONO
|
2007-11-01 06:51:38 +00:00
|
|
|
int fakemono = 0;
|
2002-03-25 21:23:06 +00:00
|
|
|
#endif
|
2007-11-01 06:51:38 +00:00
|
|
|
|
2002-03-25 21:23:06 +00:00
|
|
|
/* used for ac3surround decoder - set using -channels option */
|
|
|
|
int audio_output_channels = 2;
|
2007-11-01 06:51:38 +00:00
|
|
|
af_cfg_t af_cfg = { 1, NULL }; // Configuration for audio filters
|
2002-03-25 21:23:06 +00:00
|
|
|
|
2007-11-01 06:51:38 +00:00
|
|
|
void afm_help(void)
|
|
|
|
{
|
2002-08-31 13:09:23 +00:00
|
|
|
int i;
|
2007-11-01 06:51:38 +00:00
|
|
|
mp_msg(MSGT_DECAUDIO, MSGL_INFO, MSGTR_AvailableAudioFm);
|
2006-04-24 07:20:34 +00:00
|
|
|
mp_msg(MSGT_IDENTIFY, MSGL_INFO, "ID_AUDIO_DRIVERS\n");
|
2007-11-01 06:51:38 +00:00
|
|
|
mp_msg(MSGT_DECAUDIO, MSGL_INFO, " afm: info: (comment)\n");
|
|
|
|
for (i = 0; mpcodecs_ad_drivers[i] != NULL; i++)
|
|
|
|
if (mpcodecs_ad_drivers[i]->info->comment
|
|
|
|
&& mpcodecs_ad_drivers[i]->info->comment[0])
|
|
|
|
mp_msg(MSGT_DECAUDIO, MSGL_INFO, "%9s %s (%s)\n",
|
|
|
|
mpcodecs_ad_drivers[i]->info->short_name,
|
|
|
|
mpcodecs_ad_drivers[i]->info->name,
|
|
|
|
mpcodecs_ad_drivers[i]->info->comment);
|
|
|
|
else
|
|
|
|
mp_msg(MSGT_DECAUDIO, MSGL_INFO, "%9s %s\n",
|
|
|
|
mpcodecs_ad_drivers[i]->info->short_name,
|
|
|
|
mpcodecs_ad_drivers[i]->info->name);
|
2002-08-31 13:09:23 +00:00
|
|
|
}
|
|
|
|
|
2007-11-01 06:51:49 +00:00
|
|
|
static int init_audio_codec(sh_audio_t *sh_audio)
|
2002-03-25 21:23:06 +00:00
|
|
|
{
|
2007-11-01 06:51:38 +00:00
|
|
|
if ((af_cfg.force & AF_INIT_FORMAT_MASK) == AF_INIT_FLOAT) {
|
|
|
|
int fmt = AF_FORMAT_FLOAT_NE;
|
|
|
|
if (sh_audio->ad_driver->control(sh_audio, ADCTRL_QUERY_FORMAT,
|
|
|
|
&fmt) == CONTROL_TRUE) {
|
|
|
|
sh_audio->sample_format = fmt;
|
|
|
|
sh_audio->samplesize = 4;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (!sh_audio->ad_driver->preinit(sh_audio)) {
|
|
|
|
mp_msg(MSGT_DECAUDIO, MSGL_ERR, MSGTR_ADecoderPreinitFailed);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* allocate audio in buffer: */
|
|
|
|
if (sh_audio->audio_in_minsize > 0) {
|
|
|
|
sh_audio->a_in_buffer_size = sh_audio->audio_in_minsize;
|
|
|
|
mp_msg(MSGT_DECAUDIO, MSGL_V, MSGTR_AllocatingBytesForInputBuffer,
|
|
|
|
sh_audio->a_in_buffer_size);
|
|
|
|
sh_audio->a_in_buffer = memalign(16, sh_audio->a_in_buffer_size);
|
|
|
|
memset(sh_audio->a_in_buffer, 0, sh_audio->a_in_buffer_size);
|
|
|
|
sh_audio->a_in_buffer_len = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
sh_audio->a_buffer_size = sh_audio->audio_out_minsize + MAX_OUTBURST;
|
|
|
|
|
|
|
|
mp_msg(MSGT_DECAUDIO, MSGL_V, MSGTR_AllocatingBytesForOutputBuffer,
|
|
|
|
sh_audio->audio_out_minsize, MAX_OUTBURST, sh_audio->a_buffer_size);
|
|
|
|
|
|
|
|
sh_audio->a_buffer = memalign(16, sh_audio->a_buffer_size);
|
|
|
|
if (!sh_audio->a_buffer) {
|
|
|
|
mp_msg(MSGT_DECAUDIO, MSGL_ERR, MSGTR_CantAllocAudioBuf);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
memset(sh_audio->a_buffer, 0, sh_audio->a_buffer_size);
|
|
|
|
sh_audio->a_buffer_len = 0;
|
|
|
|
|
|
|
|
if (!sh_audio->ad_driver->init(sh_audio)) {
|
|
|
|
mp_msg(MSGT_DECAUDIO, MSGL_WARN, MSGTR_ADecoderInitFailed);
|
|
|
|
uninit_audio(sh_audio); // free buffers
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2008-02-14 14:23:55 +00:00
|
|
|
sh_audio->initialized = 1;
|
2007-11-01 06:51:38 +00:00
|
|
|
|
|
|
|
if (!sh_audio->channels || !sh_audio->samplerate) {
|
|
|
|
mp_msg(MSGT_DECAUDIO, MSGL_WARN, MSGTR_UnknownAudio);
|
|
|
|
uninit_audio(sh_audio); // free buffers
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!sh_audio->o_bps)
|
|
|
|
sh_audio->o_bps = sh_audio->channels * sh_audio->samplerate
|
|
|
|
* sh_audio->samplesize;
|
|
|
|
|
|
|
|
mp_msg(MSGT_DECAUDIO, MSGL_INFO,
|
|
|
|
"AUDIO: %d Hz, %d ch, %s, %3.1f kbit/%3.2f%% (ratio: %d->%d)\n",
|
|
|
|
sh_audio->samplerate, sh_audio->channels,
|
|
|
|
af_fmt2str_short(sh_audio->sample_format),
|
|
|
|
sh_audio->i_bps * 8 * 0.001,
|
|
|
|
((float) sh_audio->i_bps / sh_audio->o_bps) * 100.0,
|
|
|
|
sh_audio->i_bps, sh_audio->o_bps);
|
|
|
|
mp_msg(MSGT_IDENTIFY, MSGL_INFO,
|
|
|
|
"ID_AUDIO_BITRATE=%d\nID_AUDIO_RATE=%d\n" "ID_AUDIO_NCH=%d\n",
|
|
|
|
sh_audio->i_bps * 8, sh_audio->samplerate, sh_audio->channels);
|
|
|
|
|
2007-11-01 06:51:44 +00:00
|
|
|
sh_audio->a_out_buffer_size = 0;
|
|
|
|
sh_audio->a_out_buffer = NULL;
|
|
|
|
sh_audio->a_out_buffer_len = 0;
|
2007-11-01 06:51:38 +00:00
|
|
|
|
|
|
|
return 1;
|
2002-03-25 21:23:06 +00:00
|
|
|
}
|
|
|
|
|
2007-11-01 06:51:49 +00:00
|
|
|
static int init_audio(sh_audio_t *sh_audio, char *codecname, char *afm,
|
2008-01-12 14:05:46 +00:00
|
|
|
int status, stringset_t *selected)
|
2007-11-01 06:51:38 +00:00
|
|
|
{
|
|
|
|
unsigned int orig_fourcc = sh_audio->wf ? sh_audio->wf->wFormatTag : 0;
|
2005-08-30 23:56:24 +00:00
|
|
|
int force = 0;
|
|
|
|
if (codecname && codecname[0] == '+') {
|
2007-11-01 06:51:38 +00:00
|
|
|
codecname = &codecname[1];
|
|
|
|
force = 1;
|
2005-08-30 23:56:24 +00:00
|
|
|
}
|
2007-11-01 06:51:38 +00:00
|
|
|
sh_audio->codec = NULL;
|
|
|
|
while (1) {
|
|
|
|
ad_functions_t *mpadec;
|
2002-09-28 02:23:20 +00:00
|
|
|
int i;
|
2005-02-25 11:17:43 +00:00
|
|
|
sh_audio->ad_driver = 0;
|
2002-09-28 02:23:20 +00:00
|
|
|
// restore original fourcc:
|
2007-11-01 06:51:38 +00:00
|
|
|
if (sh_audio->wf)
|
|
|
|
sh_audio->wf->wFormatTag = i = orig_fourcc;
|
|
|
|
if (!(sh_audio->codec = find_audio_codec(sh_audio->format,
|
|
|
|
sh_audio->wf ? (&i) : NULL,
|
|
|
|
sh_audio->codec, force)))
|
|
|
|
break;
|
|
|
|
if (sh_audio->wf)
|
|
|
|
sh_audio->wf->wFormatTag = i;
|
2002-09-28 02:23:20 +00:00
|
|
|
// ok we found one codec
|
2008-01-12 14:05:46 +00:00
|
|
|
if (stringset_test(selected, sh_audio->codec->name))
|
2007-11-01 06:51:38 +00:00
|
|
|
continue; // already tried & failed
|
|
|
|
if (codecname && strcmp(sh_audio->codec->name, codecname))
|
|
|
|
continue; // -ac
|
|
|
|
if (afm && strcmp(sh_audio->codec->drv, afm))
|
|
|
|
continue; // afm doesn't match
|
|
|
|
if (!force && sh_audio->codec->status < status)
|
|
|
|
continue; // too unstable
|
2008-01-12 14:05:46 +00:00
|
|
|
stringset_add(selected, sh_audio->codec->name); // tagging it
|
2002-09-28 02:23:20 +00:00
|
|
|
// ok, it matches all rules, let's find the driver!
|
2007-11-01 06:51:38 +00:00
|
|
|
for (i = 0; mpcodecs_ad_drivers[i] != NULL; i++)
|
|
|
|
if (!strcmp(mpcodecs_ad_drivers[i]->info->short_name,
|
|
|
|
sh_audio->codec->drv))
|
|
|
|
break;
|
|
|
|
mpadec = mpcodecs_ad_drivers[i];
|
2002-11-11 17:28:41 +00:00
|
|
|
#ifdef DYNAMIC_PLUGINS
|
2007-11-01 06:51:38 +00:00
|
|
|
if (!mpadec) {
|
2002-11-11 17:28:41 +00:00
|
|
|
/* try to open shared decoder plugin */
|
|
|
|
int buf_len;
|
|
|
|
char *buf;
|
|
|
|
ad_functions_t *funcs_sym;
|
|
|
|
ad_info_t *info_sym;
|
2007-11-01 06:51:38 +00:00
|
|
|
|
|
|
|
buf_len =
|
|
|
|
strlen(MPLAYER_LIBDIR) + strlen(sh_audio->codec->drv) + 16;
|
2002-11-11 17:28:41 +00:00
|
|
|
buf = malloc(buf_len);
|
|
|
|
if (!buf)
|
|
|
|
break;
|
2007-11-01 06:51:38 +00:00
|
|
|
snprintf(buf, buf_len, "%s/mplayer/ad_%s.so", MPLAYER_LIBDIR,
|
|
|
|
sh_audio->codec->drv);
|
|
|
|
mp_msg(MSGT_DECAUDIO, MSGL_DBG2,
|
|
|
|
"Trying to open external plugin: %s\n", buf);
|
2002-11-11 17:28:41 +00:00
|
|
|
sh_audio->dec_handle = dlopen(buf, RTLD_LAZY);
|
|
|
|
if (!sh_audio->dec_handle)
|
|
|
|
break;
|
|
|
|
snprintf(buf, buf_len, "mpcodecs_ad_%s", sh_audio->codec->drv);
|
|
|
|
funcs_sym = dlsym(sh_audio->dec_handle, buf);
|
2007-11-01 06:51:38 +00:00
|
|
|
if (!funcs_sym || !funcs_sym->info || !funcs_sym->preinit
|
|
|
|
|| !funcs_sym->init || !funcs_sym->uninit
|
|
|
|
|| !funcs_sym->control || !funcs_sym->decode_audio)
|
2002-11-11 17:28:41 +00:00
|
|
|
break;
|
|
|
|
info_sym = funcs_sym->info;
|
|
|
|
if (strcmp(info_sym->short_name, sh_audio->codec->drv))
|
|
|
|
break;
|
|
|
|
free(buf);
|
|
|
|
mpadec = funcs_sym;
|
2007-11-01 06:51:38 +00:00
|
|
|
mp_msg(MSGT_DECAUDIO, MSGL_V,
|
|
|
|
"Using external decoder plugin (%s/mplayer/ad_%s.so)!\n",
|
|
|
|
MPLAYER_LIBDIR, sh_audio->codec->drv);
|
2002-11-11 17:28:41 +00:00
|
|
|
}
|
|
|
|
#endif
|
2007-11-01 06:51:38 +00:00
|
|
|
if (!mpadec) { // driver not available (==compiled in)
|
|
|
|
mp_msg(MSGT_DECAUDIO, MSGL_ERR,
|
|
|
|
MSGTR_AudioCodecFamilyNotAvailableStr,
|
|
|
|
sh_audio->codec->name, sh_audio->codec->drv);
|
2002-09-28 02:23:20 +00:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
// it's available, let's try to init!
|
|
|
|
// init()
|
2007-11-01 06:51:38 +00:00
|
|
|
mp_msg(MSGT_DECAUDIO, MSGL_INFO, MSGTR_OpeningAudioDecoder,
|
|
|
|
mpadec->info->short_name, mpadec->info->name);
|
2005-02-25 11:17:43 +00:00
|
|
|
sh_audio->ad_driver = mpadec;
|
2007-11-01 06:51:38 +00:00
|
|
|
if (!init_audio_codec(sh_audio)) {
|
|
|
|
mp_msg(MSGT_DECAUDIO, MSGL_INFO, MSGTR_ADecoderInitFailed);
|
|
|
|
continue; // try next...
|
2002-09-28 02:23:20 +00:00
|
|
|
}
|
|
|
|
// Yeah! We got it!
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2007-11-01 06:51:38 +00:00
|
|
|
int init_best_audio_codec(sh_audio_t *sh_audio, char **audio_codec_list,
|
|
|
|
char **audio_fm_list)
|
|
|
|
{
|
2008-01-12 14:05:46 +00:00
|
|
|
stringset_t selected;
|
2007-11-01 06:51:38 +00:00
|
|
|
char *ac_l_default[2] = { "", (char *) NULL };
|
|
|
|
// hack:
|
|
|
|
if (!audio_codec_list)
|
|
|
|
audio_codec_list = ac_l_default;
|
|
|
|
// Go through the codec.conf and find the best codec...
|
2008-02-14 14:23:55 +00:00
|
|
|
sh_audio->initialized = 0;
|
2008-01-12 14:05:46 +00:00
|
|
|
stringset_init(&selected);
|
2008-02-14 14:23:55 +00:00
|
|
|
while (!sh_audio->initialized && *audio_codec_list) {
|
2007-11-01 06:51:38 +00:00
|
|
|
char *audio_codec = *(audio_codec_list++);
|
|
|
|
if (audio_codec[0]) {
|
|
|
|
if (audio_codec[0] == '-') {
|
|
|
|
// disable this codec:
|
2008-01-12 14:05:46 +00:00
|
|
|
stringset_add(&selected, audio_codec + 1);
|
2007-11-01 06:51:38 +00:00
|
|
|
} else {
|
|
|
|
// forced codec by name:
|
|
|
|
mp_msg(MSGT_DECAUDIO, MSGL_INFO, MSGTR_ForcedAudioCodec,
|
|
|
|
audio_codec);
|
2008-01-12 14:05:46 +00:00
|
|
|
init_audio(sh_audio, audio_codec, NULL, -1, &selected);
|
2007-11-01 06:51:38 +00:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
int status;
|
|
|
|
// try in stability order: UNTESTED, WORKING, BUGGY.
|
|
|
|
// never try CRASHING.
|
|
|
|
if (audio_fm_list) {
|
|
|
|
char **fmlist = audio_fm_list;
|
|
|
|
// try first the preferred codec families:
|
2008-02-14 14:23:55 +00:00
|
|
|
while (!sh_audio->initialized && *fmlist) {
|
2007-11-01 06:51:38 +00:00
|
|
|
char *audio_fm = *(fmlist++);
|
|
|
|
mp_msg(MSGT_DECAUDIO, MSGL_INFO, MSGTR_TryForceAudioFmtStr,
|
|
|
|
audio_fm);
|
|
|
|
for (status = CODECS_STATUS__MAX;
|
|
|
|
status >= CODECS_STATUS__MIN; --status)
|
2008-01-12 14:05:46 +00:00
|
|
|
if (init_audio(sh_audio, NULL, audio_fm, status, &selected))
|
2007-11-01 06:51:38 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2008-02-14 14:23:55 +00:00
|
|
|
if (!sh_audio->initialized)
|
2007-11-01 06:51:38 +00:00
|
|
|
for (status = CODECS_STATUS__MAX; status >= CODECS_STATUS__MIN;
|
|
|
|
--status)
|
2008-01-12 14:05:46 +00:00
|
|
|
if (init_audio(sh_audio, NULL, NULL, status, &selected))
|
2007-11-01 06:51:38 +00:00
|
|
|
break;
|
|
|
|
}
|
2002-09-28 02:23:20 +00:00
|
|
|
}
|
2008-01-12 14:05:46 +00:00
|
|
|
stringset_free(&selected);
|
2002-09-28 02:23:20 +00:00
|
|
|
|
2008-02-14 14:23:55 +00:00
|
|
|
if (!sh_audio->initialized) {
|
2007-11-01 06:51:38 +00:00
|
|
|
mp_msg(MSGT_DECAUDIO, MSGL_ERR, MSGTR_CantFindAudioCodec,
|
|
|
|
sh_audio->format);
|
|
|
|
mp_msg(MSGT_DECAUDIO, MSGL_HINT, MSGTR_RTFMCodecs);
|
|
|
|
return 0; // failed
|
|
|
|
}
|
2002-09-28 02:23:20 +00:00
|
|
|
|
2007-11-01 06:51:38 +00:00
|
|
|
mp_msg(MSGT_DECAUDIO, MSGL_INFO, MSGTR_SelectedAudioCodec,
|
|
|
|
sh_audio->codec->name, sh_audio->codec->drv, sh_audio->codec->info);
|
|
|
|
return 1; // success
|
2002-09-28 02:23:20 +00:00
|
|
|
}
|
|
|
|
|
2002-03-25 21:23:06 +00:00
|
|
|
void uninit_audio(sh_audio_t *sh_audio)
|
|
|
|
{
|
2007-11-01 06:51:38 +00:00
|
|
|
if (sh_audio->afilter) {
|
|
|
|
mp_msg(MSGT_DECAUDIO, MSGL_V, "Uninit audio filters...\n");
|
2002-10-05 22:55:45 +00:00
|
|
|
af_uninit(sh_audio->afilter);
|
2003-09-02 20:02:37 +00:00
|
|
|
free(sh_audio->afilter);
|
2007-11-01 06:51:38 +00:00
|
|
|
sh_audio->afilter = NULL;
|
2002-10-05 22:55:45 +00:00
|
|
|
}
|
2008-02-14 14:23:55 +00:00
|
|
|
if (sh_audio->initialized) {
|
2007-11-01 06:51:38 +00:00
|
|
|
mp_msg(MSGT_DECAUDIO, MSGL_V, MSGTR_UninitAudioStr,
|
|
|
|
sh_audio->codec->drv);
|
2005-02-25 11:17:43 +00:00
|
|
|
sh_audio->ad_driver->uninit(sh_audio);
|
2002-11-11 17:28:41 +00:00
|
|
|
#ifdef DYNAMIC_PLUGINS
|
|
|
|
if (sh_audio->dec_handle)
|
|
|
|
dlclose(sh_audio->dec_handle);
|
|
|
|
#endif
|
2008-02-14 14:23:55 +00:00
|
|
|
sh_audio->initialized = 0;
|
2002-09-01 23:33:30 +00:00
|
|
|
}
|
2007-11-01 06:51:44 +00:00
|
|
|
free(sh_audio->a_out_buffer);
|
2007-11-01 06:51:38 +00:00
|
|
|
sh_audio->a_out_buffer = NULL;
|
|
|
|
sh_audio->a_out_buffer_size = 0;
|
|
|
|
if (sh_audio->a_buffer)
|
|
|
|
free(sh_audio->a_buffer);
|
|
|
|
sh_audio->a_buffer = NULL;
|
|
|
|
if (sh_audio->a_in_buffer)
|
|
|
|
free(sh_audio->a_in_buffer);
|
|
|
|
sh_audio->a_in_buffer = NULL;
|
2002-03-25 21:23:06 +00:00
|
|
|
}
|
|
|
|
|
2003-01-18 17:34:02 +00:00
|
|
|
|
2007-11-01 06:51:38 +00:00
|
|
|
int init_audio_filters(sh_audio_t *sh_audio, int in_samplerate,
|
2007-11-01 06:52:28 +00:00
|
|
|
int *out_samplerate, int *out_channels, int *out_format)
|
2002-03-25 21:23:06 +00:00
|
|
|
{
|
2006-11-07 23:16:55 +00:00
|
|
|
af_stream_t *afs = sh_audio->afilter;
|
2007-11-01 06:51:38 +00:00
|
|
|
if (!afs) {
|
|
|
|
afs = malloc(sizeof(af_stream_t));
|
|
|
|
memset(afs, 0, sizeof(af_stream_t));
|
|
|
|
}
|
|
|
|
// input format: same as codec's output format:
|
|
|
|
afs->input.rate = in_samplerate;
|
2007-11-01 06:52:28 +00:00
|
|
|
afs->input.nch = sh_audio->channels;
|
|
|
|
afs->input.format = sh_audio->sample_format;
|
2007-11-01 06:51:38 +00:00
|
|
|
af_fix_parameters(&(afs->input));
|
|
|
|
|
|
|
|
// output format: same as ao driver's input format (if missing, fallback to input)
|
|
|
|
afs->output.rate = *out_samplerate;
|
|
|
|
afs->output.nch = *out_channels;
|
|
|
|
afs->output.format = *out_format;
|
|
|
|
af_fix_parameters(&(afs->output));
|
|
|
|
|
|
|
|
// filter config:
|
|
|
|
memcpy(&afs->cfg, &af_cfg, sizeof(af_cfg_t));
|
|
|
|
|
|
|
|
mp_msg(MSGT_DECAUDIO, MSGL_V, MSGTR_BuildingAudioFilterChain,
|
|
|
|
afs->input.rate, afs->input.nch,
|
|
|
|
af_fmt2str_short(afs->input.format), afs->output.rate,
|
|
|
|
afs->output.nch, af_fmt2str_short(afs->output.format));
|
|
|
|
|
|
|
|
// let's autoprobe it!
|
|
|
|
if (0 != af_init(afs)) {
|
|
|
|
sh_audio->afilter = NULL;
|
|
|
|
free(afs);
|
|
|
|
return 0; // failed :(
|
|
|
|
}
|
|
|
|
|
|
|
|
*out_samplerate = afs->output.rate;
|
|
|
|
*out_channels = afs->output.nch;
|
|
|
|
*out_format = afs->output.format;
|
|
|
|
|
2007-11-01 06:52:23 +00:00
|
|
|
sh_audio->a_out_buffer_len = 0;
|
|
|
|
|
2007-11-01 06:51:38 +00:00
|
|
|
// ok!
|
|
|
|
sh_audio->afilter = (void *) afs;
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2007-11-01 06:52:19 +00:00
|
|
|
static int filter_n_bytes(sh_audio_t *sh, int len)
|
2007-11-01 06:51:38 +00:00
|
|
|
{
|
2007-11-01 06:52:19 +00:00
|
|
|
int error = 0;
|
2007-12-03 09:07:31 +00:00
|
|
|
// Filter
|
|
|
|
af_data_t filter_input = {
|
|
|
|
.audio = sh->a_buffer,
|
|
|
|
.rate = sh->samplerate,
|
|
|
|
.nch = sh->channels,
|
|
|
|
.format = sh->sample_format
|
|
|
|
};
|
|
|
|
af_data_t *filter_output;
|
2007-11-01 06:52:19 +00:00
|
|
|
|
2007-12-03 09:03:45 +00:00
|
|
|
assert(len-1 + sh->audio_out_minsize <= sh->a_buffer_size);
|
|
|
|
|
2007-11-01 06:52:19 +00:00
|
|
|
// Decode more bytes if needed
|
|
|
|
while (sh->a_buffer_len < len) {
|
|
|
|
unsigned char *buf = sh->a_buffer + sh->a_buffer_len;
|
|
|
|
int minlen = len - sh->a_buffer_len;
|
|
|
|
int maxlen = sh->a_buffer_size - sh->a_buffer_len;
|
|
|
|
int ret = sh->ad_driver->decode_audio(sh, buf, minlen, maxlen);
|
|
|
|
if (ret <= 0) {
|
|
|
|
error = -1;
|
|
|
|
len = sh->a_buffer_len;
|
2007-11-01 06:51:38 +00:00
|
|
|
break;
|
2007-11-01 06:52:19 +00:00
|
|
|
}
|
|
|
|
sh->a_buffer_len += ret;
|
|
|
|
}
|
2007-11-01 06:51:38 +00:00
|
|
|
|
2007-12-03 09:57:21 +00:00
|
|
|
filter_input.len = len;
|
2007-11-01 06:52:19 +00:00
|
|
|
af_fix_parameters(&filter_input);
|
2007-12-03 09:07:31 +00:00
|
|
|
filter_output = af_play(sh->afilter, &filter_input);
|
2007-11-01 06:52:19 +00:00
|
|
|
if (!filter_output)
|
|
|
|
return -1;
|
|
|
|
if (sh->a_out_buffer_size < sh->a_out_buffer_len + filter_output->len) {
|
|
|
|
int newlen = sh->a_out_buffer_len + filter_output->len;
|
|
|
|
mp_msg(MSGT_DECAUDIO, MSGL_V, "Increasing filtered audio buffer size "
|
|
|
|
"from %d to %d\n", sh->a_out_buffer_size, newlen);
|
|
|
|
sh->a_out_buffer = realloc(sh->a_out_buffer, newlen);
|
|
|
|
sh->a_out_buffer_size = newlen;
|
2007-11-01 06:51:38 +00:00
|
|
|
}
|
2007-11-01 06:52:19 +00:00
|
|
|
memcpy(sh->a_out_buffer + sh->a_out_buffer_len, filter_output->audio,
|
|
|
|
filter_output->len);
|
|
|
|
sh->a_out_buffer_len += filter_output->len;
|
2007-11-01 06:51:38 +00:00
|
|
|
|
|
|
|
// remove processed data from decoder buffer:
|
2007-11-01 06:52:19 +00:00
|
|
|
sh->a_buffer_len -= len;
|
|
|
|
memmove(sh->a_buffer, sh->a_buffer + len, sh->a_buffer_len);
|
2007-11-01 06:51:38 +00:00
|
|
|
|
2007-11-01 06:52:19 +00:00
|
|
|
return error;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Try to get at least minlen decoded+filtered bytes in sh_audio->a_out_buffer
|
|
|
|
* (total length including possible existing data).
|
|
|
|
* Return 0 on success, -1 on error/EOF (not distinguished).
|
|
|
|
* In the former case sh_audio->a_out_buffer_len is always >= minlen
|
|
|
|
* on return. In case of EOF/error it might or might not be.
|
|
|
|
* Can reallocate sh_audio->a_out_buffer if needed to fit all filter output. */
|
|
|
|
int decode_audio(sh_audio_t *sh_audio, int minlen)
|
|
|
|
{
|
2007-11-08 18:14:49 +00:00
|
|
|
// Indicates that a filter seems to be buffering large amounts of data
|
|
|
|
int huge_filter_buffer = 0;
|
2007-11-01 06:52:19 +00:00
|
|
|
// Decoded audio must be cut at boundaries of this many bytes
|
|
|
|
int unitsize = sh_audio->channels * sh_audio->samplesize;
|
|
|
|
|
|
|
|
/* Filter output size will be about filter_multiplier times input size.
|
|
|
|
* If some filter buffers audio in big blocks this might only hold
|
|
|
|
* as average over time. */
|
|
|
|
double filter_multiplier = af_calc_filter_multiplier(sh_audio->afilter);
|
|
|
|
|
|
|
|
/* If the decoder set audio_out_minsize then it can do the equivalent of
|
|
|
|
* "while (output_len < target_len) output_len += audio_out_minsize;",
|
|
|
|
* so we must guarantee there is at least audio_out_minsize-1 bytes
|
|
|
|
* more space in the output buffer than the minimum length we try to
|
|
|
|
* decode. */
|
|
|
|
int max_decode_len = sh_audio->a_buffer_size - sh_audio->audio_out_minsize;
|
|
|
|
max_decode_len -= max_decode_len % unitsize;
|
|
|
|
|
|
|
|
while (sh_audio->a_out_buffer_len < minlen) {
|
|
|
|
int declen = (minlen - sh_audio->a_out_buffer_len) / filter_multiplier
|
|
|
|
+ (unitsize << 5); // some extra for possible filter buffering
|
2007-11-08 18:14:49 +00:00
|
|
|
if (huge_filter_buffer)
|
2007-11-01 06:52:19 +00:00
|
|
|
/* Some filter must be doing significant buffering if the estimated
|
|
|
|
* input length didn't produce enough output from filters.
|
|
|
|
* Feed the filters 2k bytes at a time until we have enough output.
|
|
|
|
* Very small amounts could make filtering inefficient while large
|
|
|
|
* amounts can make MPlayer demux the file unnecessarily far ahead
|
|
|
|
* to get audio data and buffer video frames in memory while doing
|
|
|
|
* so. However the performance impact of either is probably not too
|
|
|
|
* significant as long as the value is not completely insane. */
|
2007-11-08 18:14:49 +00:00
|
|
|
declen = 2000;
|
2007-11-01 06:52:19 +00:00
|
|
|
declen -= declen % unitsize;
|
2007-11-08 18:14:49 +00:00
|
|
|
if (declen > max_decode_len)
|
|
|
|
declen = max_decode_len;
|
|
|
|
else
|
|
|
|
/* if this iteration does not fill buffer, we must have lots
|
|
|
|
* of buffering in filters */
|
|
|
|
huge_filter_buffer = 1;
|
|
|
|
if (filter_n_bytes(sh_audio, declen) < 0)
|
|
|
|
return -1;
|
2007-11-01 06:52:19 +00:00
|
|
|
}
|
|
|
|
return 0;
|
2002-03-25 21:23:06 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void resync_audio_stream(sh_audio_t *sh_audio)
|
|
|
|
{
|
2007-11-01 06:51:38 +00:00
|
|
|
sh_audio->a_in_buffer_len = 0; // clear audio input buffer
|
2008-02-14 14:23:55 +00:00
|
|
|
if (!sh_audio->initialized)
|
2007-11-01 06:51:38 +00:00
|
|
|
return;
|
|
|
|
sh_audio->ad_driver->control(sh_audio, ADCTRL_RESYNC_STREAM, NULL);
|
2002-03-25 21:23:06 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void skip_audio_frame(sh_audio_t *sh_audio)
|
|
|
|
{
|
2008-02-14 14:23:55 +00:00
|
|
|
if (!sh_audio->initialized)
|
2007-11-01 06:51:38 +00:00
|
|
|
return;
|
|
|
|
if (sh_audio->ad_driver->control(sh_audio, ADCTRL_SKIP_FRAME, NULL) ==
|
|
|
|
CONTROL_TRUE)
|
|
|
|
return;
|
|
|
|
// default skip code:
|
|
|
|
ds_fill_buffer(sh_audio->ds); // skip block
|
2002-03-25 21:23:06 +00:00
|
|
|
}
|