af_lavrresample: move libavresample setup to separate function

Helps with readability. Also remove the ctx_opt_set_* helper macros and
use av_opt_set_* directly (I think these macros were used because the
lines ended up too long, but this commit removes two indentation levels,
giving more space).
This commit is contained in:
wm4 2013-11-09 23:20:58 +01:00
parent 5735b684de
commit a89549e8db
1 changed files with 98 additions and 90 deletions

View File

@ -126,46 +126,17 @@ static bool test_conversion(int src_format, int dst_format)
af_to_avformat(dst_format) != AV_SAMPLE_FMT_NONE;
}
#define ctx_opt_set_int(a,b) av_opt_set_int(s->avrctx, (a), (b), 0)
#define ctx_opt_set_dbl(a,b) av_opt_set_double(s->avrctx, (a), (b), 0)
static int control(struct af_instance *af, int cmd, void *arg)
static int configure_lavrr(struct af_instance *af, struct mp_audio *in,
struct mp_audio *out)
{
struct af_resample *s = (struct af_resample *) af->priv;
struct mp_audio *in = (struct mp_audio *) arg;
struct mp_audio *out = (struct mp_audio *) af->data;
switch (cmd) {
case AF_CONTROL_REINIT: {
struct mp_audio orig_in = *in;
if (((out->rate == in->rate) || (out->rate == 0)) &&
(out->format == in->format) &&
(mp_chmap_equals(&out->channels, &in->channels) || out->nch == 0) &&
s->allow_detach)
return AF_DETACH;
if (out->rate == 0)
out->rate = in->rate;
if (mp_chmap_is_empty(&out->channels))
mp_audio_set_channels(out, &in->channels);
struct af_resample *s = af->priv;
enum AVSampleFormat in_samplefmt = af_to_avformat(in->format);
if (in_samplefmt == AV_SAMPLE_FMT_NONE) {
mp_audio_set_format(in, AF_FORMAT_FLOAT_NE);
in_samplefmt = af_to_avformat(in->format);
}
enum AVSampleFormat out_samplefmt = af_to_avformat(out->format);
if (out_samplefmt == AV_SAMPLE_FMT_NONE) {
mp_audio_set_format(out, in->format);
out_samplefmt = in_samplefmt;
}
af->mul = (double) (out->rate * out->nch) / (in->rate * in->nch);
af->delay = out->nch * s->opts.filter_size / FFMIN(af->mul, 1);
if (in_samplefmt == AV_SAMPLE_FMT_NONE || out_samplefmt == AV_SAMPLE_FMT_NONE)
return AF_ERROR;
if (needs_lavrctx_reconfigure(s, in, out)) {
avresample_close(s->avrctx);
avresample_close(s->avrctx_out);
@ -180,11 +151,11 @@ static int control(struct af_instance *af, int cmd, void *arg)
s->ctx.linear = s->opts.linear;
s->ctx.cutoff = s->opts.cutoff;
ctx_opt_set_int("filter_size", s->ctx.filter_size);
ctx_opt_set_int("phase_shift", s->ctx.phase_shift);
ctx_opt_set_int("linear_interp", s->ctx.linear);
av_opt_set_int(s->avrctx, "filter_size", s->ctx.filter_size, 0);
av_opt_set_int(s->avrctx, "phase_shift", s->ctx.phase_shift, 0);
av_opt_set_int(s->avrctx, "linear_interp", s->ctx.linear, 0);
ctx_opt_set_dbl("cutoff", s->ctx.cutoff);
av_opt_set_int(s->avrctx, "cutoff", s->ctx.cutoff, 0);
if (parse_avopts(s->avrctx, s->avopts) < 0) {
mp_msg(MSGT_VFILTER, MSGL_FATAL,
@ -205,14 +176,14 @@ static int control(struct af_instance *af, int cmd, void *arg)
uint64_t in_ch_layout = mp_chmap_to_lavc_unchecked(&map_in);
uint64_t out_ch_layout = mp_chmap_to_lavc_unchecked(&map_out);
ctx_opt_set_int("in_channel_layout", in_ch_layout);
ctx_opt_set_int("out_channel_layout", out_ch_layout);
av_opt_set_int(s->avrctx, "in_channel_layout", in_ch_layout, 0);
av_opt_set_int(s->avrctx, "out_channel_layout", out_ch_layout, 0);
ctx_opt_set_int("in_sample_rate", s->ctx.in_rate);
ctx_opt_set_int("out_sample_rate", s->ctx.out_rate);
av_opt_set_int(s->avrctx, "in_sample_rate", s->ctx.in_rate, 0);
av_opt_set_int(s->avrctx, "out_sample_rate", s->ctx.out_rate, 0);
ctx_opt_set_int("in_sample_fmt", in_samplefmt);
ctx_opt_set_int("out_sample_fmt", out_samplefmt);
av_opt_set_int(s->avrctx, "in_sample_fmt", in_samplefmt, 0);
av_opt_set_int(s->avrctx, "out_sample_fmt", out_samplefmt, 0);
struct mp_chmap in_lavc;
mp_chmap_from_lavc(&in_lavc, in_ch_layout);
@ -245,8 +216,45 @@ static int control(struct af_instance *af, int cmd, void *arg)
"Libavresample Context. \n");
return AF_ERROR;
}
return AF_OK;
}
static int control(struct af_instance *af, int cmd, void *arg)
{
struct af_resample *s = (struct af_resample *) af->priv;
struct mp_audio *in = (struct mp_audio *) arg;
struct mp_audio *out = (struct mp_audio *) af->data;
switch (cmd) {
case AF_CONTROL_REINIT: {
struct mp_audio orig_in = *in;
if (((out->rate == in->rate) || (out->rate == 0)) &&
(out->format == in->format) &&
(mp_chmap_equals(&out->channels, &in->channels) || out->nch == 0) &&
s->allow_detach)
return AF_DETACH;
if (out->rate == 0)
out->rate = in->rate;
if (mp_chmap_is_empty(&out->channels))
mp_audio_set_channels(out, &in->channels);
if (af_to_avformat(in->format) == AV_SAMPLE_FMT_NONE)
mp_audio_set_format(in, AF_FORMAT_FLOAT_NE);
if (af_to_avformat(out->format) == AV_SAMPLE_FMT_NONE)
mp_audio_set_format(out, in->format);
af->mul = (double) (out->rate * out->nch) / (in->rate * in->nch);
af->delay = out->nch * s->opts.filter_size / FFMIN(af->mul, 1);
if (needs_lavrctx_reconfigure(s, in, out)) {
int r = configure_lavrr(af, in, out);
if (r != AF_OK)
return r;
}
return ((in->format == orig_in.format) &&
mp_chmap_equals(&in->channels, &orig_in.channels))
? AF_OK : AF_FALSE;