2013-05-09 10:09:23 +00:00
|
|
|
/*
|
|
|
|
* This file is part of mpv.
|
|
|
|
*
|
|
|
|
* mpv 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.
|
|
|
|
*
|
|
|
|
* mpv 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 mpv. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <stdlib.h>
|
|
|
|
|
|
|
|
#include <libavutil/common.h>
|
|
|
|
|
2013-12-17 01:02:25 +00:00
|
|
|
#include "options/m_option.h"
|
2013-05-09 10:09:23 +00:00
|
|
|
|
|
|
|
#include "audio/format.h"
|
|
|
|
#include "af.h"
|
|
|
|
|
|
|
|
struct priv {
|
|
|
|
struct m_config *config;
|
|
|
|
|
|
|
|
int in_format;
|
|
|
|
int in_srate;
|
2016-08-04 18:49:20 +00:00
|
|
|
struct m_channels in_channels;
|
2013-05-09 10:09:23 +00:00
|
|
|
int out_format;
|
|
|
|
int out_srate;
|
2016-08-04 18:49:20 +00:00
|
|
|
struct m_channels out_channels;
|
2013-05-09 10:09:23 +00:00
|
|
|
|
2013-07-22 12:43:34 +00:00
|
|
|
int fail;
|
2013-05-09 10:09:23 +00:00
|
|
|
};
|
|
|
|
|
2013-10-23 16:14:30 +00:00
|
|
|
static void force_in_params(struct af_instance *af, struct mp_audio *in)
|
|
|
|
{
|
|
|
|
struct priv *priv = af->priv;
|
|
|
|
|
|
|
|
if (priv->in_format != AF_FORMAT_UNKNOWN)
|
|
|
|
mp_audio_set_format(in, priv->in_format);
|
|
|
|
|
2016-08-04 18:49:20 +00:00
|
|
|
if (priv->in_channels.num_chmaps > 0)
|
|
|
|
mp_audio_set_channels(in, &priv->in_channels.chmaps[0]);
|
2013-10-23 16:14:30 +00:00
|
|
|
|
|
|
|
if (priv->in_srate)
|
|
|
|
in->rate = priv->in_srate;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void force_out_params(struct af_instance *af, struct mp_audio *out)
|
|
|
|
{
|
|
|
|
struct priv *priv = af->priv;
|
|
|
|
|
|
|
|
if (priv->out_format != AF_FORMAT_UNKNOWN)
|
|
|
|
mp_audio_set_format(out, priv->out_format);
|
|
|
|
|
2016-08-04 18:49:20 +00:00
|
|
|
if (priv->out_channels.num_chmaps > 0)
|
|
|
|
mp_audio_set_channels(out, &priv->out_channels.chmaps[0]);
|
2013-10-23 16:14:30 +00:00
|
|
|
|
|
|
|
if (priv->out_srate)
|
|
|
|
out->rate = priv->out_srate;
|
|
|
|
}
|
|
|
|
|
2013-05-09 10:09:23 +00:00
|
|
|
static int control(struct af_instance *af, int cmd, void *arg)
|
|
|
|
{
|
2013-07-22 12:45:12 +00:00
|
|
|
struct priv *priv = af->priv;
|
2013-05-09 10:09:23 +00:00
|
|
|
|
|
|
|
switch (cmd) {
|
|
|
|
case AF_CONTROL_REINIT: {
|
|
|
|
struct mp_audio *in = arg;
|
|
|
|
struct mp_audio orig_in = *in;
|
|
|
|
struct mp_audio *out = af->data;
|
|
|
|
|
2013-10-23 16:14:30 +00:00
|
|
|
force_in_params(af, in);
|
2013-05-09 10:09:23 +00:00
|
|
|
mp_audio_copy_config(out, in);
|
2013-10-23 16:14:30 +00:00
|
|
|
force_out_params(af, out);
|
2013-05-09 10:09:23 +00:00
|
|
|
|
|
|
|
if (in->nch != out->nch || in->bps != out->bps) {
|
2014-11-10 18:33:36 +00:00
|
|
|
MP_ERR(af, "Forced input/output formats are incompatible.\n");
|
2013-07-22 12:43:34 +00:00
|
|
|
return AF_ERROR;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (priv->fail) {
|
2014-11-10 18:33:36 +00:00
|
|
|
MP_ERR(af, "Failing on purpose.\n");
|
2013-05-09 10:09:23 +00:00
|
|
|
return AF_ERROR;
|
|
|
|
}
|
|
|
|
|
|
|
|
return mp_audio_config_equals(in, &orig_in) ? AF_OK : AF_FALSE;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return AF_UNKNOWN;
|
|
|
|
}
|
|
|
|
|
2015-01-13 19:16:02 +00:00
|
|
|
static int filter(struct af_instance *af, struct mp_audio *data)
|
2013-05-09 10:09:23 +00:00
|
|
|
{
|
2015-01-13 19:16:02 +00:00
|
|
|
if (data)
|
|
|
|
mp_audio_copy_config(data, af->data);
|
|
|
|
af_add_output_frame(af, data);
|
2013-12-04 23:01:46 +00:00
|
|
|
return 0;
|
2013-05-09 10:09:23 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static int af_open(struct af_instance *af)
|
|
|
|
{
|
|
|
|
af->control = control;
|
2015-01-13 19:16:02 +00:00
|
|
|
af->filter_frame = filter;
|
2013-10-23 16:14:30 +00:00
|
|
|
|
|
|
|
force_in_params(af, af->data);
|
|
|
|
force_out_params(af, af->data);
|
|
|
|
|
2013-05-09 10:09:23 +00:00
|
|
|
return AF_OK;
|
|
|
|
}
|
|
|
|
|
2013-07-22 12:45:12 +00:00
|
|
|
#define OPT_BASE_STRUCT struct priv
|
|
|
|
|
2014-06-10 21:56:05 +00:00
|
|
|
const struct af_info af_info_format = {
|
2013-10-23 17:05:47 +00:00
|
|
|
.info = "Force audio format",
|
|
|
|
.name = "format",
|
|
|
|
.open = af_open,
|
2013-07-22 12:45:12 +00:00
|
|
|
.priv_size = sizeof(struct priv),
|
|
|
|
.options = (const struct m_option[]) {
|
|
|
|
OPT_AUDIOFORMAT("format", in_format, 0),
|
|
|
|
OPT_INTRANGE("srate", in_srate, 0, 1000, 8*48000),
|
2016-08-04 18:49:20 +00:00
|
|
|
OPT_CHANNELS("channels", in_channels, 0, .min = 1),
|
2013-07-22 12:45:12 +00:00
|
|
|
OPT_AUDIOFORMAT("out-format", out_format, 0),
|
|
|
|
OPT_INTRANGE("out-srate", out_srate, 0, 1000, 8*48000),
|
2016-08-04 18:49:20 +00:00
|
|
|
OPT_CHANNELS("out-channels", out_channels, 0, .min = 1),
|
2013-07-22 12:45:12 +00:00
|
|
|
OPT_FLAG("fail", fail, 0),
|
|
|
|
{0}
|
|
|
|
},
|
2013-05-09 10:09:23 +00:00
|
|
|
};
|