From 6d4b4c0de3152bc2deb2df09ec3e98e032124593 Mon Sep 17 00:00:00 2001 From: wm4 Date: Fri, 12 Jan 2018 04:02:55 +0100 Subject: audio: add global options for resampler defaults This is part of trying to get rid of --af-defaults, and the af resample filter. It requires a complicated mechanism to set the defaults on the resample filter for backwards compatibility. --- audio/aconverter.c | 28 ++++++++++++++++++++-------- audio/aconverter.h | 4 +++- audio/filter/af.c | 3 ++- audio/filter/af.h | 2 ++ audio/filter/af_lavrresample.c | 26 ++++++++++++++++++++++++++ 5 files changed, 53 insertions(+), 10 deletions(-) (limited to 'audio') diff --git a/audio/aconverter.c b/audio/aconverter.c index 19b8960de2..2475df878d 100644 --- a/audio/aconverter.c +++ b/audio/aconverter.c @@ -86,6 +86,23 @@ struct mp_aconverter { bool output_eof; // queued output EOF }; +#define OPT_BASE_STRUCT struct mp_resample_opts +const struct m_sub_options resample_config = { + .opts = (const m_option_t[]) { + OPT_INTRANGE("audio-resample-filter-size", filter_size, 0, 0, 32), + OPT_INTRANGE("audio-resample-phase-shift", phase_shift, 0, 0, 30), + OPT_FLAG("audio-resample-linear", linear, 0), + OPT_DOUBLE("audio-resample-cutoff", cutoff, M_OPT_RANGE, + .min = 0, .max = 1), + OPT_FLAG("audio-normalize-downmix", normalize, 0), + OPT_KEYVALUELIST("audio-swresample-o", avopts, 0), + {0} + }, + .size = sizeof(struct mp_resample_opts), + .defaults = &(const struct mp_resample_opts)MP_RESAMPLE_OPTS_DEF, + .change_flags = UPDATE_AUDIO, +}; + #if HAVE_LIBAVRESAMPLE static double get_delay(struct mp_aconverter *p) { @@ -211,12 +228,7 @@ static bool configure_lavrr(struct mp_aconverter *p, bool verbose) cutoff = MPMAX(1.0 - 6.5 / (p->opts->filter_size + 8), 0.80); av_opt_set_double(p->avrctx, "cutoff", cutoff, 0); - int global_normalize; - mp_read_option_raw(p->global, "audio-normalize-downmix", &m_option_type_flag, - &global_normalize); int normalize = p->opts->normalize; - if (normalize < 0) - normalize = global_normalize; #if HAVE_LIBSWRESAMPLE av_opt_set_double(p->avrctx, "rematrix_maxval", normalize ? 1 : 1000, 0); #else @@ -628,9 +640,9 @@ struct mp_aconverter *mp_aconverter_create(struct mpv_global *global, p->log = log; p->global = global; - static const struct mp_resample_opts defs = MP_RESAMPLE_OPTS_DEF; - - p->opts = opts ? opts : &defs; + p->opts = opts; + if (!p->opts) + p->opts = mp_get_config_group(p, global, &resample_config); p->reorder_buffer = mp_aframe_pool_create(p); p->out_pool = mp_aframe_pool_create(p); diff --git a/audio/aconverter.h b/audio/aconverter.h index 57c5524c2f..22ca93e4c1 100644 --- a/audio/aconverter.h +++ b/audio/aconverter.h @@ -23,9 +23,11 @@ struct mp_resample_opts { .filter_size = 16, \ .cutoff = 0.0, \ .phase_shift = 10, \ - .normalize = -1, \ + .normalize = 0, \ } +extern const struct m_sub_options resample_config; + struct mp_aconverter *mp_aconverter_create(struct mpv_global *global, struct mp_log *log, const struct mp_resample_opts *opts); diff --git a/audio/filter/af.c b/audio/filter/af.c index 5838c2e70b..35525d0774 100644 --- a/audio/filter/af.c +++ b/audio/filter/af.c @@ -63,6 +63,7 @@ static bool get_desc(struct m_obj_desc *dst, int index) .priv_size = af->priv_size, .priv_defaults = af->priv_defaults, .options = af->options, + .set_defaults = af->set_defaults, .p = af, }; return true; @@ -170,7 +171,7 @@ static struct af_instance *af_create(struct af_stream *s, char *name, .out_pool = mp_audio_pool_create(af), }; struct m_config *config = - m_config_from_obj_desc_and_args(af, s->log, NULL, &desc, + m_config_from_obj_desc_and_args(af, s->log, s->global, &desc, name, s->opts->af_defs, args); if (!config) goto error; diff --git a/audio/filter/af.h b/audio/filter/af.h index f27edee71a..3a07a5465f 100644 --- a/audio/filter/af.h +++ b/audio/filter/af.h @@ -52,6 +52,8 @@ struct af_info { int priv_size; const void *priv_defaults; const struct m_option *options; + // For m_obj_desc.set_defaults + void (*set_defaults)(struct mpv_global *global, void *p); }; // Linked list of audio filters diff --git a/audio/filter/af_lavrresample.c b/audio/filter/af_lavrresample.c index 55eb6b0f20..96387060b1 100644 --- a/audio/filter/af_lavrresample.c +++ b/audio/filter/af_lavrresample.c @@ -32,6 +32,7 @@ #include "common/av_common.h" #include "common/msg.h" +#include "options/m_config.h" #include "options/m_option.h" #include "audio/filter/af.h" #include "audio/fmt-conversion.h" @@ -42,6 +43,7 @@ struct af_resample { int allow_detach; double playback_speed; struct mp_resample_opts opts; + int global_normalize; struct mp_aconverter *converter; }; @@ -142,11 +144,34 @@ static int af_open(struct af_instance *af) af->filter_frame = filter; af->filter_out = filter_out; + if (s->opts.normalize < 0) + s->opts.normalize = s->global_normalize; + s->converter = mp_aconverter_create(af->global, af->log, &s->opts); return AF_OK; } +static void set_defaults(struct mpv_global *global, void *p) +{ + struct af_resample *s = p; + + struct mp_resample_opts *opts = &s->opts; + + struct mp_resample_opts *src_opts = + mp_get_config_group(s, global, &resample_config); + + s->global_normalize = src_opts->normalize; + + assert(!opts->avopts); // we don't set a default value, so it must be NULL + + *opts = *src_opts; + + opts->avopts = NULL; + struct m_option dummy = {.type = &m_option_type_keyvalue_list}; + m_option_copy(&dummy, &opts->avopts, &src_opts->avopts); +} + #define OPT_BASE_STRUCT struct af_resample const struct af_info af_info_lavrresample = { @@ -170,4 +195,5 @@ const struct af_info af_info_lavrresample = { OPT_KEYVALUELIST("o", opts.avopts, 0), {0} }, + .set_defaults = set_defaults, }; -- cgit v1.2.3