2009-01-05 12:41:40 +00:00
|
|
|
/*
|
|
|
|
* This file is part of MPlayer.
|
|
|
|
*
|
|
|
|
* MPlayer is free software; you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation; either version 2 of the License, or
|
|
|
|
* (at your option) any later version.
|
|
|
|
*
|
|
|
|
* MPlayer is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License along
|
|
|
|
* with MPlayer; if not, write to the Free Software Foundation, Inc.,
|
|
|
|
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
|
|
|
|
*/
|
|
|
|
|
2009-02-21 21:29:38 +00:00
|
|
|
#include "config.h"
|
2002-10-01 06:45:08 +00:00
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
2013-03-23 12:02:59 +00:00
|
|
|
#include <assert.h>
|
2002-10-01 06:45:08 +00:00
|
|
|
|
|
|
|
#include "af.h"
|
|
|
|
|
|
|
|
// Static list of filters
|
2012-11-01 11:20:35 +00:00
|
|
|
extern struct af_info af_info_dummy;
|
|
|
|
extern struct af_info af_info_delay;
|
|
|
|
extern struct af_info af_info_channels;
|
|
|
|
extern struct af_info af_info_format;
|
|
|
|
extern struct af_info af_info_volume;
|
|
|
|
extern struct af_info af_info_equalizer;
|
|
|
|
extern struct af_info af_info_pan;
|
|
|
|
extern struct af_info af_info_surround;
|
|
|
|
extern struct af_info af_info_sub;
|
|
|
|
extern struct af_info af_info_export;
|
2013-02-12 08:53:33 +00:00
|
|
|
extern struct af_info af_info_drc;
|
2012-11-01 11:20:35 +00:00
|
|
|
extern struct af_info af_info_extrastereo;
|
|
|
|
extern struct af_info af_info_lavcac3enc;
|
2013-03-09 08:30:26 +00:00
|
|
|
extern struct af_info af_info_lavrresample;
|
2012-11-01 11:20:35 +00:00
|
|
|
extern struct af_info af_info_sweep;
|
|
|
|
extern struct af_info af_info_hrtf;
|
|
|
|
extern struct af_info af_info_ladspa;
|
|
|
|
extern struct af_info af_info_center;
|
|
|
|
extern struct af_info af_info_sinesuppress;
|
|
|
|
extern struct af_info af_info_karaoke;
|
|
|
|
extern struct af_info af_info_scaletempo;
|
|
|
|
extern struct af_info af_info_bs2b;
|
2002-10-01 06:45:08 +00:00
|
|
|
|
2013-03-20 23:49:16 +00:00
|
|
|
static struct af_info* filter_list[] = {
|
|
|
|
&af_info_dummy,
|
|
|
|
&af_info_delay,
|
|
|
|
&af_info_channels,
|
|
|
|
&af_info_volume,
|
|
|
|
&af_info_equalizer,
|
|
|
|
&af_info_pan,
|
|
|
|
&af_info_surround,
|
|
|
|
&af_info_sub,
|
2003-09-21 12:16:16 +00:00
|
|
|
#ifdef HAVE_SYS_MMAN_H
|
2013-03-20 23:49:16 +00:00
|
|
|
&af_info_export,
|
2003-09-21 12:16:16 +00:00
|
|
|
#endif
|
2013-03-20 23:49:16 +00:00
|
|
|
&af_info_drc,
|
|
|
|
&af_info_extrastereo,
|
|
|
|
&af_info_lavcac3enc,
|
|
|
|
&af_info_lavrresample,
|
|
|
|
&af_info_sweep,
|
|
|
|
&af_info_hrtf,
|
2008-08-07 08:58:07 +00:00
|
|
|
#ifdef CONFIG_LADSPA
|
2013-03-20 23:49:16 +00:00
|
|
|
&af_info_ladspa,
|
2004-12-23 02:09:52 +00:00
|
|
|
#endif
|
2013-03-20 23:49:16 +00:00
|
|
|
&af_info_center,
|
|
|
|
&af_info_sinesuppress,
|
|
|
|
&af_info_karaoke,
|
|
|
|
&af_info_scaletempo,
|
2009-04-02 19:01:57 +00:00
|
|
|
#ifdef CONFIG_LIBBS2B
|
2013-03-20 23:49:16 +00:00
|
|
|
&af_info_bs2b,
|
2009-04-02 19:01:57 +00:00
|
|
|
#endif
|
2013-03-23 12:05:32 +00:00
|
|
|
// Must come last, because it's the fallback format conversion filter
|
|
|
|
&af_info_format,
|
2013-03-20 23:49:16 +00:00
|
|
|
NULL
|
2002-10-01 06:45:08 +00:00
|
|
|
};
|
|
|
|
|
2002-11-12 12:33:56 +00:00
|
|
|
// CPU speed
|
2013-03-20 23:49:16 +00:00
|
|
|
int *af_cpu_speed = NULL;
|
2002-11-12 12:33:56 +00:00
|
|
|
|
2002-10-01 06:45:08 +00:00
|
|
|
/* Find a filter in the static list of filters using it's name. This
|
|
|
|
function is used internally */
|
2013-03-20 23:49:16 +00:00
|
|
|
static struct af_info *af_find(char *name)
|
2002-10-01 06:45:08 +00:00
|
|
|
{
|
2013-03-20 23:49:16 +00:00
|
|
|
int i = 0;
|
|
|
|
while (filter_list[i]) {
|
|
|
|
if (!strcmp(filter_list[i]->name, name))
|
|
|
|
return filter_list[i];
|
|
|
|
i++;
|
|
|
|
}
|
|
|
|
mp_msg(MSGT_AFILTER, MSGL_ERR, "Couldn't find audio filter '%s'\n", name);
|
|
|
|
return NULL;
|
2009-07-06 23:26:13 +00:00
|
|
|
}
|
2002-10-01 06:45:08 +00:00
|
|
|
|
2002-10-06 11:26:14 +00:00
|
|
|
/* Find filter in the dynamic filter list using it's name This
|
|
|
|
function is used for finding already initialized filters */
|
2013-03-20 23:49:16 +00:00
|
|
|
struct af_instance *af_get(struct af_stream *s, char *name)
|
2002-10-06 11:26:14 +00:00
|
|
|
{
|
2013-03-20 23:49:16 +00:00
|
|
|
struct af_instance *af = s->first;
|
|
|
|
// Find the filter
|
|
|
|
while (af != NULL) {
|
|
|
|
if (!strcmp(af->info->name, name))
|
|
|
|
return af;
|
|
|
|
af = af->next;
|
|
|
|
}
|
|
|
|
return NULL;
|
2002-10-06 11:26:14 +00:00
|
|
|
}
|
|
|
|
|
2013-03-20 23:49:16 +00:00
|
|
|
/* Function for creating a new filter of type name.The name may
|
|
|
|
contain the commandline parameters for the filter */
|
|
|
|
static struct af_instance *af_create(struct af_stream *s,
|
|
|
|
const char *name_with_cmd)
|
2002-10-01 06:45:08 +00:00
|
|
|
{
|
2013-03-20 23:49:16 +00:00
|
|
|
char *name = strdup(name_with_cmd);
|
|
|
|
char *cmdline = name;
|
|
|
|
|
|
|
|
// Allocate space for the new filter and reset all pointers
|
|
|
|
struct af_instance *new = malloc(sizeof(struct af_instance));
|
|
|
|
if (!name || !new) {
|
|
|
|
mp_msg(MSGT_AFILTER, MSGL_ERR, "[libaf] Could not allocate memory\n");
|
|
|
|
goto err_out;
|
2002-10-06 11:26:14 +00:00
|
|
|
}
|
2013-03-20 23:49:16 +00:00
|
|
|
memset(new, 0, sizeof(struct af_instance));
|
2009-07-06 23:26:13 +00:00
|
|
|
|
2013-03-20 23:49:16 +00:00
|
|
|
// Check for commandline parameters
|
|
|
|
char *skip = strstr(cmdline, "=");
|
|
|
|
if (skip) {
|
|
|
|
*skip = '\0'; // for name
|
|
|
|
cmdline = skip + 1;
|
|
|
|
} else {
|
|
|
|
cmdline = NULL;
|
|
|
|
}
|
2009-07-06 23:26:13 +00:00
|
|
|
|
2013-03-20 23:49:16 +00:00
|
|
|
// Find filter from name
|
|
|
|
if (NULL == (new->info = af_find(name)))
|
2007-12-10 01:43:33 +00:00
|
|
|
goto err_out;
|
2013-03-20 23:49:16 +00:00
|
|
|
|
|
|
|
/* Make sure that the filter is not already in the list if it is
|
|
|
|
non-reentrant */
|
|
|
|
if (new->info->flags & AF_FLAGS_NOT_REENTRANT) {
|
|
|
|
if (af_get(s, name)) {
|
|
|
|
mp_msg(MSGT_AFILTER, MSGL_ERR, "[libaf] There can only be one "
|
|
|
|
"instance of the filter '%s' in each stream\n", name);
|
|
|
|
goto err_out;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
mp_msg(MSGT_AFILTER, MSGL_V, "[libaf] Adding filter %s \n", name);
|
|
|
|
|
|
|
|
// Initialize the new filter
|
|
|
|
if (AF_OK == new->info->open(new) &&
|
|
|
|
AF_ERROR < new->control(new, AF_CONTROL_POST_CREATE, &s->cfg))
|
|
|
|
{
|
|
|
|
if (cmdline) {
|
|
|
|
if (AF_ERROR >= new->control(new, AF_CONTROL_COMMAND_LINE, cmdline))
|
|
|
|
goto err_out;
|
|
|
|
}
|
|
|
|
free(name);
|
|
|
|
return new;
|
2002-10-31 08:03:51 +00:00
|
|
|
}
|
2009-07-06 23:26:13 +00:00
|
|
|
|
2006-03-08 15:39:53 +00:00
|
|
|
err_out:
|
2013-03-20 23:49:16 +00:00
|
|
|
free(new);
|
|
|
|
mp_msg(MSGT_AFILTER, MSGL_ERR,
|
|
|
|
"[libaf] Couldn't create or open audio filter '%s'\n", name);
|
|
|
|
free(name);
|
|
|
|
return NULL;
|
2002-10-01 06:45:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Create and insert a new filter of type name before the filter in the
|
|
|
|
argument. This function can be called during runtime, the return
|
|
|
|
value is the new filter */
|
2013-03-20 23:49:16 +00:00
|
|
|
static struct af_instance *af_prepend(struct af_stream *s,
|
|
|
|
struct af_instance *af,
|
|
|
|
const char *name)
|
2002-10-01 06:45:08 +00:00
|
|
|
{
|
2013-03-20 23:49:16 +00:00
|
|
|
// Create the new filter and make sure it is OK
|
|
|
|
struct af_instance *new = af_create(s, name);
|
|
|
|
if (!new)
|
|
|
|
return NULL;
|
|
|
|
// Update pointers
|
|
|
|
new->next = af;
|
|
|
|
if (af) {
|
|
|
|
new->prev = af->prev;
|
|
|
|
af->prev = new;
|
|
|
|
} else
|
|
|
|
s->last = new;
|
|
|
|
if (new->prev)
|
|
|
|
new->prev->next = new;
|
|
|
|
else
|
|
|
|
s->first = new;
|
|
|
|
return new;
|
2002-10-01 06:45:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Create and insert a new filter of type name after the filter in the
|
|
|
|
argument. This function can be called during runtime, the return
|
|
|
|
value is the new filter */
|
2013-03-20 23:49:16 +00:00
|
|
|
static struct af_instance *af_append(struct af_stream *s,
|
|
|
|
struct af_instance *af,
|
|
|
|
const char *name)
|
2002-10-01 06:45:08 +00:00
|
|
|
{
|
2013-03-20 23:49:16 +00:00
|
|
|
// Create the new filter and make sure it is OK
|
|
|
|
struct af_instance *new = af_create(s, name);
|
|
|
|
if (!new)
|
|
|
|
return NULL;
|
|
|
|
// Update pointers
|
|
|
|
new->prev = af;
|
|
|
|
if (af) {
|
|
|
|
new->next = af->next;
|
|
|
|
af->next = new;
|
|
|
|
} else
|
|
|
|
s->first = new;
|
|
|
|
if (new->next)
|
|
|
|
new->next->prev = new;
|
|
|
|
else
|
|
|
|
s->last = new;
|
|
|
|
return new;
|
2002-10-01 06:45:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Uninit and remove the filter "af"
|
2013-03-20 23:49:16 +00:00
|
|
|
void af_remove(struct af_stream *s, struct af_instance *af)
|
2002-10-01 06:45:08 +00:00
|
|
|
{
|
2013-03-20 23:49:16 +00:00
|
|
|
if (!af)
|
|
|
|
return;
|
|
|
|
|
|
|
|
// Print friendly message
|
|
|
|
mp_msg(MSGT_AFILTER, MSGL_V, "[libaf] Removing filter %s \n",
|
|
|
|
af->info->name);
|
|
|
|
|
|
|
|
// Notify filter before changing anything
|
|
|
|
af->control(af, AF_CONTROL_PRE_DESTROY, 0);
|
|
|
|
|
|
|
|
// Detach pointers
|
|
|
|
if (af->prev)
|
|
|
|
af->prev->next = af->next;
|
|
|
|
else
|
|
|
|
s->first = af->next;
|
|
|
|
if (af->next)
|
|
|
|
af->next->prev = af->prev;
|
|
|
|
else
|
|
|
|
s->last = af->prev;
|
|
|
|
|
|
|
|
// Uninitialize af and free memory
|
|
|
|
af->uninit(af);
|
|
|
|
free(af);
|
2002-10-01 06:45:08 +00:00
|
|
|
}
|
|
|
|
|
2013-03-23 12:02:59 +00:00
|
|
|
static void remove_auto_inserted_filters(struct af_stream *s, bool dummy_only)
|
|
|
|
{
|
|
|
|
repeat:
|
|
|
|
for (struct af_instance *af = s->first; af; af = af->next) {
|
|
|
|
if ((af->auto_inserted && !dummy_only) || af->info == &af_info_dummy) {
|
|
|
|
af_remove(s, af);
|
|
|
|
goto repeat;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-11-01 11:00:00 +00:00
|
|
|
static void print_fmt(struct mp_audio *d)
|
2011-12-22 07:25:41 +00:00
|
|
|
{
|
|
|
|
if (d) {
|
|
|
|
mp_msg(MSGT_AFILTER, MSGL_V, "%dHz/%dch/%s", d->rate, d->nch,
|
|
|
|
af_fmt2str_short(d->format));
|
2013-03-20 23:49:16 +00:00
|
|
|
} else
|
2011-12-22 07:25:41 +00:00
|
|
|
mp_msg(MSGT_AFILTER, MSGL_V, "(?)");
|
|
|
|
}
|
|
|
|
|
2013-03-20 23:49:16 +00:00
|
|
|
static void af_print_filter_chain(struct af_stream *s)
|
2011-12-22 07:25:41 +00:00
|
|
|
{
|
|
|
|
mp_msg(MSGT_AFILTER, MSGL_V, "Audio filter chain:\n");
|
|
|
|
|
|
|
|
mp_msg(MSGT_AFILTER, MSGL_V, " [in] ");
|
|
|
|
print_fmt(&s->input);
|
|
|
|
mp_msg(MSGT_AFILTER, MSGL_V, "\n");
|
|
|
|
|
2012-11-01 11:23:48 +00:00
|
|
|
struct af_instance *af = s->first;
|
2011-12-22 07:25:41 +00:00
|
|
|
while (af) {
|
|
|
|
mp_msg(MSGT_AFILTER, MSGL_V, " [%s] ", af->info->name);
|
|
|
|
print_fmt(af->data);
|
|
|
|
mp_msg(MSGT_AFILTER, MSGL_V, "\n");
|
|
|
|
|
|
|
|
af = af->next;
|
|
|
|
}
|
|
|
|
|
|
|
|
mp_msg(MSGT_AFILTER, MSGL_V, " [out] ");
|
|
|
|
print_fmt(&s->output);
|
|
|
|
mp_msg(MSGT_AFILTER, MSGL_V, "\n");
|
|
|
|
}
|
|
|
|
|
2013-03-23 12:05:32 +00:00
|
|
|
static const char *af_find_conversion_filter(int srcfmt, int dstfmt)
|
|
|
|
{
|
|
|
|
for (int n = 0; filter_list[n]; n++) {
|
|
|
|
struct af_info *af = filter_list[n];
|
|
|
|
if (af->test_conversion && af->test_conversion(srcfmt, dstfmt))
|
|
|
|
return af->name;
|
|
|
|
}
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
static bool af_is_conversion_filter(struct af_instance *af)
|
|
|
|
{
|
|
|
|
return af && af->info->test_conversion != NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
// in is what af can take as input - insert a conversion filter if the actual
|
|
|
|
// input format doesn't match what af expects.
|
|
|
|
// If af is NULL, in is the output format of the stream.
|
|
|
|
// Returns:
|
|
|
|
// AF_OK: must call af_reinit() or equivalent, format matches
|
|
|
|
// AF_FALSE: nothing was changed, format matches
|
|
|
|
// else: error
|
|
|
|
static int af_fix_format_conversion(struct af_stream *s,
|
|
|
|
struct af_instance **p_af,
|
|
|
|
struct mp_audio in)
|
|
|
|
{
|
|
|
|
int rv;
|
|
|
|
struct af_instance *af = p_af ? *p_af : NULL;
|
|
|
|
struct mp_audio actual;
|
|
|
|
if (af) {
|
|
|
|
actual = af->prev ? *af->prev->data : s->input;
|
|
|
|
} else {
|
|
|
|
actual = *s->last->data;
|
|
|
|
}
|
|
|
|
if (actual.format == in.format)
|
|
|
|
return AF_FALSE;
|
|
|
|
const char *filter = af_find_conversion_filter(actual.format, in.format);
|
|
|
|
if (!filter)
|
|
|
|
return AF_ERROR;
|
|
|
|
struct af_instance *new;
|
|
|
|
if (af) {
|
|
|
|
new = af_prepend(s, af, filter);
|
|
|
|
new->auto_inserted = true;
|
|
|
|
} else {
|
|
|
|
struct af_info *last = s->last->info;
|
|
|
|
if (last->test_conversion &&
|
|
|
|
last->test_conversion(actual.format, in.format))
|
|
|
|
{
|
|
|
|
new = s->last;
|
|
|
|
} else {
|
|
|
|
new = af_append(s, s->last, filter);
|
|
|
|
new->auto_inserted = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (new == NULL)
|
|
|
|
return AF_ERROR;
|
|
|
|
// Set output bits per sample
|
|
|
|
in.format |= af_bits2fmt(in.bps * 8);
|
|
|
|
if (AF_OK != (rv = new->control(new, AF_CONTROL_FORMAT_FMT, &in.format)))
|
|
|
|
return rv;
|
|
|
|
if (p_af)
|
|
|
|
*p_af = new;
|
|
|
|
return AF_OK;
|
|
|
|
}
|
|
|
|
|
2013-03-23 15:03:17 +00:00
|
|
|
// same as af_fix_format_conversion - only wrt. channels
|
|
|
|
static int af_fix_channels(struct af_stream *s, struct af_instance **p_af,
|
|
|
|
struct mp_audio in)
|
|
|
|
{
|
|
|
|
int rv;
|
|
|
|
struct af_instance *af = p_af ? *p_af : NULL;
|
|
|
|
struct mp_audio actual;
|
|
|
|
if (af) {
|
|
|
|
actual = af->prev ? *af->prev->data : s->input;
|
|
|
|
} else {
|
|
|
|
actual = *s->last->data;
|
|
|
|
}
|
|
|
|
if (actual.nch == in.nch)
|
|
|
|
return AF_FALSE;
|
audio: switch to libavcodec channel order, use libavresample for mixing
Switch the internal channel order to libavcodec's. If the channel number
mismatches at some point, use libavresample for up- or downmixing.
Remove the old af_pan automatic downmixing.
The libavcodec channel order should be equivalent to WAVEFORMATEX order,
at least nowadays. reorder_ch.h assumes that WAVEFORMATEX and libavcodec
might be different, but all defined channels have the same mappings.
Remove the downmixing with af_pan as well as the channel conversion with
af_channels from af.c, and prefer af_lavrresample for this. The
automatic downmixing behavior should be the same as before (if the
--channels option is set to 2, which is the default, the audio output
is forced to 2 channels, and libavresample does all downmixing).
Note that mpv still can't do channel layouts. It will pick the default
channel layout according to the channel count. This will be fixed later
by passing down the channel layout as well.
af_hrtf depends on the order of the input channels, so reorder to ALSA
(for which this code was written). This is better than changing the
filter code, which is more risky.
ao_pulse can accept waveext order directly, so set that as channel
mapping.
2013-03-23 16:49:52 +00:00
|
|
|
const char *filter = "lavrresample";
|
2013-03-23 15:03:17 +00:00
|
|
|
struct af_instance *new;
|
|
|
|
if (af) {
|
audio: switch to libavcodec channel order, use libavresample for mixing
Switch the internal channel order to libavcodec's. If the channel number
mismatches at some point, use libavresample for up- or downmixing.
Remove the old af_pan automatic downmixing.
The libavcodec channel order should be equivalent to WAVEFORMATEX order,
at least nowadays. reorder_ch.h assumes that WAVEFORMATEX and libavcodec
might be different, but all defined channels have the same mappings.
Remove the downmixing with af_pan as well as the channel conversion with
af_channels from af.c, and prefer af_lavrresample for this. The
automatic downmixing behavior should be the same as before (if the
--channels option is set to 2, which is the default, the audio output
is forced to 2 channels, and libavresample does all downmixing).
Note that mpv still can't do channel layouts. It will pick the default
channel layout according to the channel count. This will be fixed later
by passing down the channel layout as well.
af_hrtf depends on the order of the input channels, so reorder to ALSA
(for which this code was written). This is better than changing the
filter code, which is more risky.
ao_pulse can accept waveext order directly, so set that as channel
mapping.
2013-03-23 16:49:52 +00:00
|
|
|
new = af_prepend(s, af, filter);
|
2013-03-23 15:03:17 +00:00
|
|
|
new->auto_inserted = true;
|
|
|
|
} else {
|
audio: switch to libavcodec channel order, use libavresample for mixing
Switch the internal channel order to libavcodec's. If the channel number
mismatches at some point, use libavresample for up- or downmixing.
Remove the old af_pan automatic downmixing.
The libavcodec channel order should be equivalent to WAVEFORMATEX order,
at least nowadays. reorder_ch.h assumes that WAVEFORMATEX and libavcodec
might be different, but all defined channels have the same mappings.
Remove the downmixing with af_pan as well as the channel conversion with
af_channels from af.c, and prefer af_lavrresample for this. The
automatic downmixing behavior should be the same as before (if the
--channels option is set to 2, which is the default, the audio output
is forced to 2 channels, and libavresample does all downmixing).
Note that mpv still can't do channel layouts. It will pick the default
channel layout according to the channel count. This will be fixed later
by passing down the channel layout as well.
af_hrtf depends on the order of the input channels, so reorder to ALSA
(for which this code was written). This is better than changing the
filter code, which is more risky.
ao_pulse can accept waveext order directly, so set that as channel
mapping.
2013-03-23 16:49:52 +00:00
|
|
|
if (strcmp(s->last->info->name, filter) == 0) {
|
2013-03-23 15:03:17 +00:00
|
|
|
new = s->last;
|
|
|
|
} else {
|
audio: switch to libavcodec channel order, use libavresample for mixing
Switch the internal channel order to libavcodec's. If the channel number
mismatches at some point, use libavresample for up- or downmixing.
Remove the old af_pan automatic downmixing.
The libavcodec channel order should be equivalent to WAVEFORMATEX order,
at least nowadays. reorder_ch.h assumes that WAVEFORMATEX and libavcodec
might be different, but all defined channels have the same mappings.
Remove the downmixing with af_pan as well as the channel conversion with
af_channels from af.c, and prefer af_lavrresample for this. The
automatic downmixing behavior should be the same as before (if the
--channels option is set to 2, which is the default, the audio output
is forced to 2 channels, and libavresample does all downmixing).
Note that mpv still can't do channel layouts. It will pick the default
channel layout according to the channel count. This will be fixed later
by passing down the channel layout as well.
af_hrtf depends on the order of the input channels, so reorder to ALSA
(for which this code was written). This is better than changing the
filter code, which is more risky.
ao_pulse can accept waveext order directly, so set that as channel
mapping.
2013-03-23 16:49:52 +00:00
|
|
|
new = af_append(s, s->last, filter);
|
2013-03-23 15:03:17 +00:00
|
|
|
new->auto_inserted = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (new == NULL)
|
|
|
|
return AF_ERROR;
|
|
|
|
if (AF_OK != (rv = new->control(new, AF_CONTROL_CHANNELS, &in.nch)))
|
|
|
|
return rv;
|
|
|
|
if (p_af)
|
|
|
|
*p_af = new;
|
|
|
|
return AF_OK;
|
|
|
|
}
|
|
|
|
|
2012-01-14 08:06:16 +00:00
|
|
|
// Warning:
|
|
|
|
// A failed af_reinit() leaves the audio chain behind in a useless, broken
|
|
|
|
// state (for example, format filters that were tentatively inserted stay
|
|
|
|
// inserted).
|
|
|
|
// In that case, you should always rebuild the filter chain, or abort.
|
2013-03-23 12:02:59 +00:00
|
|
|
// Also, note that for complete reinit, fixup_output_format() must be called
|
|
|
|
// after this function.
|
|
|
|
int af_reinit(struct af_stream *s)
|
2002-10-01 06:45:08 +00:00
|
|
|
{
|
2013-03-23 12:02:59 +00:00
|
|
|
remove_auto_inserted_filters(s, true);
|
2013-03-20 23:49:16 +00:00
|
|
|
|
2013-03-23 12:02:59 +00:00
|
|
|
struct af_instance *af = s->first;
|
2013-03-23 15:08:17 +00:00
|
|
|
int retry = 0;
|
|
|
|
while (af && retry < 5) {
|
|
|
|
if (retry >= 5)
|
|
|
|
goto negotiate_error;
|
2013-03-20 23:49:16 +00:00
|
|
|
|
|
|
|
// Check if this is the first filter
|
2013-03-20 23:58:05 +00:00
|
|
|
struct mp_audio in = af->prev ? *(af->prev->data) : s->input;
|
2013-03-20 23:49:16 +00:00
|
|
|
// Reset just in case...
|
|
|
|
in.audio = NULL;
|
|
|
|
in.len = 0;
|
|
|
|
|
2013-03-23 15:08:17 +00:00
|
|
|
int rv = af->control(af, AF_CONTROL_REINIT, &in);
|
2013-03-20 23:49:16 +00:00
|
|
|
switch (rv) {
|
|
|
|
case AF_OK:
|
|
|
|
af = af->next;
|
|
|
|
break;
|
|
|
|
case AF_FALSE: { // Configuration filter is needed
|
|
|
|
// Do auto insertion only if force is not specified
|
|
|
|
if ((AF_INIT_TYPE_MASK & s->cfg.force) != AF_INIT_FORCE) {
|
2013-03-23 12:05:32 +00:00
|
|
|
int progress = 0;
|
2013-03-23 15:08:17 +00:00
|
|
|
if (af_fix_channels(s, &af, in) == AF_OK)
|
2013-03-23 12:05:32 +00:00
|
|
|
progress = 1;
|
2013-03-23 15:08:17 +00:00
|
|
|
if (af_fix_format_conversion(s, &af, in) == AF_OK)
|
2013-03-23 12:05:32 +00:00
|
|
|
progress = 1;
|
2013-03-23 15:08:17 +00:00
|
|
|
if (progress) {
|
|
|
|
retry++;
|
|
|
|
continue;
|
2013-03-20 23:49:16 +00:00
|
|
|
}
|
2013-03-23 15:08:17 +00:00
|
|
|
goto negotiate_error;
|
2013-03-20 23:49:16 +00:00
|
|
|
} else {
|
|
|
|
mp_msg(
|
|
|
|
MSGT_AFILTER, MSGL_ERR,
|
|
|
|
"[libaf] Automatic filter insertion disabled "
|
|
|
|
"but formats do not match. Giving up.\n");
|
|
|
|
return AF_ERROR;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case AF_DETACH: { // Filter is redundant and wants to be unloaded
|
|
|
|
// Do auto remove only if force is not specified
|
|
|
|
if ((AF_INIT_TYPE_MASK & s->cfg.force) != AF_INIT_FORCE) {
|
|
|
|
struct af_instance *aft = af->prev;
|
|
|
|
af_remove(s, af);
|
|
|
|
if (aft)
|
|
|
|
af = aft->next;
|
|
|
|
else
|
|
|
|
af = s->first; // Restart configuration
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
default:
|
|
|
|
mp_msg(MSGT_AFILTER, MSGL_ERR, "[libaf] Reinitialization did not "
|
|
|
|
"work, audio filter '%s' returned error code %i\n",
|
|
|
|
af->info->name, rv);
|
|
|
|
return AF_ERROR;
|
|
|
|
}
|
2013-03-23 12:02:59 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// At least one filter must exist in the chain.
|
|
|
|
if (!s->last) {
|
|
|
|
af = af_append(s, NULL, "dummy");
|
|
|
|
if (!af)
|
|
|
|
return AF_ERROR;
|
|
|
|
af->control(af, AF_CONTROL_REINIT, &s->input);
|
|
|
|
}
|
2013-03-20 23:49:16 +00:00
|
|
|
|
|
|
|
af_print_filter_chain(s);
|
2011-12-22 07:25:41 +00:00
|
|
|
|
2013-03-20 23:49:16 +00:00
|
|
|
return AF_OK;
|
2013-03-23 15:08:17 +00:00
|
|
|
|
|
|
|
negotiate_error:
|
|
|
|
mp_msg(MSGT_AFILTER, MSGL_ERR, "[libaf] Unable to correct audio format. "
|
|
|
|
"This error should never occur, please send a bug report.\n");
|
|
|
|
return AF_ERROR;
|
2002-10-01 06:45:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Uninit and remove all filters
|
2013-03-20 23:49:16 +00:00
|
|
|
void af_uninit(struct af_stream *s)
|
2002-10-01 06:45:08 +00:00
|
|
|
{
|
2013-03-20 23:49:16 +00:00
|
|
|
while (s->first)
|
|
|
|
af_remove(s, s->first);
|
2002-10-01 06:45:08 +00:00
|
|
|
}
|
|
|
|
|
2010-02-19 21:52:29 +00:00
|
|
|
/**
|
|
|
|
* Extend the filter chain so we get the required output format at the end.
|
|
|
|
* \return AF_ERROR on error, AF_OK if successful.
|
|
|
|
*/
|
2013-03-20 23:49:16 +00:00
|
|
|
static int fixup_output_format(struct af_stream *s)
|
2010-02-19 21:52:29 +00:00
|
|
|
{
|
2013-03-23 15:08:17 +00:00
|
|
|
if (s->output.nch != 0)
|
|
|
|
af_fix_channels(s, NULL, s->output);
|
2010-02-19 21:52:29 +00:00
|
|
|
|
2013-03-23 15:08:17 +00:00
|
|
|
if (s->output.format != AF_FORMAT_UNKNOWN)
|
|
|
|
af_fix_format_conversion(s, NULL, s->output);
|
2010-02-19 21:52:29 +00:00
|
|
|
|
2013-03-23 12:02:59 +00:00
|
|
|
if (AF_OK != af_reinit(s))
|
2013-03-20 23:49:16 +00:00
|
|
|
return AF_ERROR;
|
2010-02-19 21:52:29 +00:00
|
|
|
|
|
|
|
if (s->output.format == AF_FORMAT_UNKNOWN)
|
2013-03-20 23:49:16 +00:00
|
|
|
s->output.format = s->last->data->format;
|
|
|
|
if (!s->output.nch)
|
|
|
|
s->output.nch = s->last->data->nch;
|
|
|
|
if (!s->output.rate)
|
|
|
|
s->output.rate = s->last->data->rate;
|
|
|
|
if ((s->last->data->format != s->output.format) ||
|
|
|
|
(s->last->data->nch != s->output.nch) ||
|
|
|
|
(s->last->data->rate != s->output.rate))
|
|
|
|
return AF_ERROR;
|
2010-02-19 21:52:29 +00:00
|
|
|
return AF_OK;
|
|
|
|
}
|
|
|
|
|
2002-10-01 12:53:30 +00:00
|
|
|
/* Initialize the stream "s". This function creates a new filter list
|
|
|
|
if necessary according to the values set in input and output. Input
|
|
|
|
and output should contain the format of the current movie and the
|
|
|
|
formate of the preferred output respectively. The function is
|
|
|
|
reentrant i.e. if called with an already initialized stream the
|
2005-06-24 10:50:53 +00:00
|
|
|
stream will be reinitialized.
|
|
|
|
If one of the prefered output parameters is 0 the one that needs
|
|
|
|
no conversion is used (i.e. the output format in the last filter).
|
|
|
|
The return value is 0 if success and -1 if failure */
|
2013-03-20 23:49:16 +00:00
|
|
|
int af_init(struct af_stream *s)
|
2002-10-01 06:45:08 +00:00
|
|
|
{
|
2013-03-20 23:49:16 +00:00
|
|
|
int i = 0;
|
|
|
|
|
|
|
|
// Sanity check
|
|
|
|
if (!s)
|
|
|
|
return -1;
|
|
|
|
|
|
|
|
// Precaution in case caller is misbehaving
|
|
|
|
s->input.audio = s->output.audio = NULL;
|
|
|
|
s->input.len = s->output.len = 0;
|
|
|
|
|
|
|
|
// Figure out how fast the machine is
|
|
|
|
if (AF_INIT_AUTO == (AF_INIT_TYPE_MASK & s->cfg.force))
|
|
|
|
s->cfg.force = (s->cfg.force & ~AF_INIT_TYPE_MASK) | AF_INIT_TYPE;
|
|
|
|
|
|
|
|
// Check if this is the first call
|
|
|
|
if (!s->first) {
|
|
|
|
// Add all filters in the list (if there are any)
|
|
|
|
if (s->cfg.list) {
|
|
|
|
while (s->cfg.list[i]) {
|
|
|
|
if (!af_append(s, s->last, s->cfg.list[i++]))
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
}
|
2002-10-01 06:45:08 +00:00
|
|
|
}
|
2013-03-20 23:49:16 +00:00
|
|
|
|
2013-03-23 12:02:59 +00:00
|
|
|
remove_auto_inserted_filters(s, false);
|
2013-03-20 23:49:16 +00:00
|
|
|
|
|
|
|
// Init filters
|
2013-03-23 12:02:59 +00:00
|
|
|
if (AF_OK != af_reinit(s))
|
2013-03-20 23:49:16 +00:00
|
|
|
return -1;
|
|
|
|
|
|
|
|
// Check output format
|
|
|
|
if ((AF_INIT_TYPE_MASK & s->cfg.force) != AF_INIT_FORCE) {
|
|
|
|
struct af_instance *af = NULL; // New filter
|
|
|
|
// Check output frequency if not OK fix with resample
|
|
|
|
if (s->output.rate && s->last->data->rate != s->output.rate) {
|
|
|
|
// try to find a filter that can change samplrate
|
|
|
|
af = af_control_any_rev(s, AF_CONTROL_RESAMPLE_RATE | AF_CONTROL_SET,
|
|
|
|
&(s->output.rate));
|
|
|
|
if (!af) {
|
|
|
|
char *resampler = "lavrresample";
|
|
|
|
if ((AF_INIT_TYPE_MASK & s->cfg.force) == AF_INIT_SLOW) {
|
2013-03-23 12:05:32 +00:00
|
|
|
if (af_is_conversion_filter(s->first))
|
2013-03-20 23:49:16 +00:00
|
|
|
af = af_append(s, s->first, resampler);
|
|
|
|
else
|
|
|
|
af = af_prepend(s, s->first, resampler);
|
|
|
|
} else {
|
2013-03-23 12:05:32 +00:00
|
|
|
if (af_is_conversion_filter(s->last))
|
2013-03-20 23:49:16 +00:00
|
|
|
af = af_prepend(s, s->last, resampler);
|
|
|
|
else
|
|
|
|
af = af_append(s, s->last, resampler);
|
|
|
|
}
|
|
|
|
// Init the new filter
|
|
|
|
if (!af)
|
|
|
|
return -1;
|
2013-03-23 12:02:59 +00:00
|
|
|
af->auto_inserted = true;
|
2013-03-20 23:49:16 +00:00
|
|
|
if (af->control(af, AF_CONTROL_RESAMPLE_RATE | AF_CONTROL_SET,
|
|
|
|
&(s->output.rate)) != AF_OK)
|
|
|
|
return -1;
|
|
|
|
}
|
2013-03-23 12:02:59 +00:00
|
|
|
if (AF_OK != af_reinit(s))
|
2013-03-20 23:49:16 +00:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
if (AF_OK != fixup_output_format(s)) {
|
|
|
|
// Something is stuffed audio out will not work
|
|
|
|
mp_msg(
|
|
|
|
MSGT_AFILTER, MSGL_ERR,
|
|
|
|
"[libaf] Unable to setup filter system can not"
|
|
|
|
" meet sound-card demands, please send a bug report. \n");
|
|
|
|
af_uninit(s);
|
|
|
|
return -1;
|
|
|
|
}
|
2002-10-01 06:45:08 +00:00
|
|
|
}
|
2013-03-20 23:49:16 +00:00
|
|
|
return 0;
|
2002-10-01 06:45:08 +00:00
|
|
|
}
|
|
|
|
|
2002-10-07 10:46:01 +00:00
|
|
|
/* Add filter during execution. This function adds the filter "name"
|
|
|
|
to the stream s. The filter will be inserted somewhere nice in the
|
|
|
|
list of filters. The return value is a pointer to the new filter,
|
|
|
|
If the filter couldn't be added the return value is NULL. */
|
2013-03-20 23:49:16 +00:00
|
|
|
struct af_instance *af_add(struct af_stream *s, char *name)
|
|
|
|
{
|
|
|
|
struct af_instance *new;
|
|
|
|
// Sanity check
|
|
|
|
if (!s || !s->first || !name)
|
|
|
|
return NULL;
|
|
|
|
// Insert the filter somewhere nice
|
2013-03-23 12:05:32 +00:00
|
|
|
if (af_is_conversion_filter(s->first))
|
2013-03-20 23:49:16 +00:00
|
|
|
new = af_append(s, s->first, name);
|
|
|
|
else
|
|
|
|
new = af_prepend(s, s->first, name);
|
|
|
|
if (!new)
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
// Reinitalize the filter list
|
2013-03-23 12:02:59 +00:00
|
|
|
if (AF_OK != af_reinit(s) ||
|
2013-03-20 23:49:16 +00:00
|
|
|
AF_OK != fixup_output_format(s)) {
|
|
|
|
while (s->first)
|
|
|
|
af_remove(s, s->first);
|
|
|
|
af_init(s);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
return new;
|
2002-10-07 10:46:01 +00:00
|
|
|
}
|
|
|
|
|
2002-10-01 06:45:08 +00:00
|
|
|
// Filter data chunk through the filters in the list
|
2013-03-20 23:49:16 +00:00
|
|
|
struct mp_audio *af_play(struct af_stream *s, struct mp_audio *data)
|
2002-10-01 06:45:08 +00:00
|
|
|
{
|
2013-03-20 23:49:16 +00:00
|
|
|
struct af_instance *af = s->first;
|
|
|
|
// Iterate through all filters
|
|
|
|
do {
|
|
|
|
if (data->len <= 0)
|
|
|
|
break;
|
|
|
|
data = af->play(af, data);
|
|
|
|
af = af->next;
|
|
|
|
} while (af && data);
|
|
|
|
return data;
|
2002-10-01 06:45:08 +00:00
|
|
|
}
|
|
|
|
|
2007-11-01 06:52:01 +00:00
|
|
|
/* Calculate the minimum output buffer size for given input data d
|
|
|
|
* when using the RESIZE_LOCAL_BUFFER macro. The +t+1 part ensures the
|
|
|
|
* value is >= len*mul rounded upwards to whole samples even if the
|
|
|
|
* double 'mul' is inexact. */
|
2013-03-20 23:49:16 +00:00
|
|
|
int af_lencalc(double mul, struct mp_audio *d)
|
2007-11-01 06:52:01 +00:00
|
|
|
{
|
2013-03-20 23:49:16 +00:00
|
|
|
int t = d->bps * d->nch;
|
|
|
|
return d->len * mul + t + 1;
|
2002-10-01 06:45:08 +00:00
|
|
|
}
|
|
|
|
|
2007-11-01 06:52:19 +00:00
|
|
|
// Calculate average ratio of filter output size to input size
|
2013-03-20 23:49:16 +00:00
|
|
|
double af_calc_filter_multiplier(struct af_stream *s)
|
2002-10-05 11:02:39 +00:00
|
|
|
{
|
2013-03-20 23:49:16 +00:00
|
|
|
struct af_instance *af = s->first;
|
|
|
|
double mul = 1;
|
|
|
|
// Iterate through all filters and calculate total multiplication factor
|
|
|
|
do {
|
|
|
|
mul *= af->mul;
|
|
|
|
af = af->next;
|
|
|
|
} while (af);
|
|
|
|
|
|
|
|
return mul;
|
2002-10-05 11:02:39 +00:00
|
|
|
}
|
|
|
|
|
2007-11-01 06:52:50 +00:00
|
|
|
/* Calculate the total delay [bytes output] caused by the filters */
|
2013-03-20 23:49:16 +00:00
|
|
|
double af_calc_delay(struct af_stream *s)
|
2002-10-08 10:20:36 +00:00
|
|
|
{
|
2013-03-20 23:49:16 +00:00
|
|
|
struct af_instance *af = s->first;
|
|
|
|
register double delay = 0.0;
|
|
|
|
// Iterate through all filters
|
|
|
|
while (af) {
|
|
|
|
delay += af->delay;
|
|
|
|
delay *= af->mul;
|
|
|
|
af = af->next;
|
|
|
|
}
|
|
|
|
return delay;
|
2002-10-08 10:20:36 +00:00
|
|
|
}
|
|
|
|
|
2002-10-01 06:45:08 +00:00
|
|
|
/* Helper function called by the macro with the same name this
|
|
|
|
function should not be called directly */
|
2013-03-20 23:49:16 +00:00
|
|
|
int af_resize_local_buffer(struct af_instance *af, struct mp_audio *data)
|
2002-10-01 06:45:08 +00:00
|
|
|
{
|
2013-03-20 23:49:16 +00:00
|
|
|
// Calculate new length
|
|
|
|
register int len = af_lencalc(af->mul, data);
|
|
|
|
mp_msg(MSGT_AFILTER, MSGL_V, "[libaf] Reallocating memory in module %s, "
|
|
|
|
"old len = %i, new len = %i\n", af->info->name, af->data->len, len);
|
|
|
|
// If there is a buffer free it
|
|
|
|
free(af->data->audio);
|
|
|
|
// Create new buffer and check that it is OK
|
|
|
|
af->data->audio = malloc(len);
|
|
|
|
if (!af->data->audio) {
|
|
|
|
mp_msg(MSGT_AFILTER, MSGL_FATAL, "[libaf] Could not allocate memory \n");
|
|
|
|
return AF_ERROR;
|
|
|
|
}
|
|
|
|
af->data->len = len;
|
|
|
|
return AF_OK;
|
2002-10-01 06:45:08 +00:00
|
|
|
}
|
2004-06-25 19:02:53 +00:00
|
|
|
|
2005-10-01 12:55:34 +00:00
|
|
|
// documentation in af.h
|
2013-03-20 23:49:16 +00:00
|
|
|
struct af_instance *af_control_any_rev(struct af_stream *s, int cmd, void *arg)
|
|
|
|
{
|
|
|
|
int res = AF_UNKNOWN;
|
|
|
|
struct af_instance *filt = s->last;
|
|
|
|
while (filt) {
|
|
|
|
res = filt->control(filt, cmd, arg);
|
|
|
|
if (res == AF_OK)
|
|
|
|
return filt;
|
|
|
|
filt = filt->prev;
|
|
|
|
}
|
|
|
|
return NULL;
|
2004-06-25 19:02:53 +00:00
|
|
|
}
|
|
|
|
|
2013-03-20 23:49:16 +00:00
|
|
|
void af_help(void)
|
|
|
|
{
|
|
|
|
int i = 0;
|
|
|
|
mp_msg(MSGT_AFILTER, MSGL_INFO, "Available audio filters:\n");
|
|
|
|
while (filter_list[i]) {
|
|
|
|
if (filter_list[i]->comment && filter_list[i]->comment[0]) {
|
|
|
|
mp_msg(MSGT_AFILTER, MSGL_INFO, " %-15s: %s (%s)\n",
|
|
|
|
filter_list[i]->name, filter_list[i]->info,
|
|
|
|
filter_list[i]->comment);
|
|
|
|
} else {
|
|
|
|
mp_msg(MSGT_AFILTER, MSGL_INFO, " %-15s: %s\n",
|
|
|
|
filter_list[i]->name,
|
|
|
|
filter_list[i]->info);
|
|
|
|
}
|
|
|
|
i++;
|
|
|
|
}
|
2004-09-06 22:27:08 +00:00
|
|
|
}
|
|
|
|
|
2012-11-01 11:00:00 +00:00
|
|
|
void af_fix_parameters(struct mp_audio *data)
|
2005-02-25 11:11:07 +00:00
|
|
|
{
|
2011-11-11 17:15:13 +00:00
|
|
|
if (data->nch < 0 || data->nch > AF_NCH) {
|
2013-03-20 23:49:16 +00:00
|
|
|
mp_msg(MSGT_AFILTER, MSGL_ERR,
|
|
|
|
"Invalid number of channels %i, assuming 2.\n", data->nch);
|
|
|
|
data->nch = 2;
|
2011-11-11 17:15:13 +00:00
|
|
|
}
|
2013-03-20 23:49:16 +00:00
|
|
|
data->bps = af_fmt2bits(data->format) / 8;
|
2005-02-25 11:11:07 +00:00
|
|
|
}
|