History

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.

wm4 committed 8 years ago

Commit 6d4b4c01 parent 23edaf412 files · +116 −30Browse files

Unified · Split

12 files
  1. DOCS/interface-changes.rst +2 −0
  2. DOCS/man/options.rst +38 −10
  3. audio/aconverter.c +20 −8
  4. audio/aconverter.h +3 −1
  5. audio/filter/af.c +2 −1
  6. audio/filter/af.h +2 −0
  7. audio/filter/af_lavrresample.c +26 −0
  8. options/m_config.c +12 −5
  9. options/m_config.h +0 −3
  10. options/m_option.h +5 −0
  11. options/options.c +4 −1
  12. options/options.h +2 −1
DOCS/interface-changes.rst+2−0
@@ -25,6 +25,8 @@ Interface changes · 24 unmodified lines
2525 - fix --external-files: strictly never select any tracks from them, unless
2626 explicitly selected (this may or may not be expected)
2727 - --ytdl is now always enabled, even for libmpv
28+ - add a number of --audio-resample-* options, which should from now on be
29+ used instead of --af-defaults=lavrresample:...
2830 --- mpv 0.28.0 ---
2931 - rename --hwdec=mediacodec option to mediacodec-copy, to reflect
3032 conventions followed by other hardware video decoding APIs
DOCS/man/options.rst+38−10
@@ -1441,16 +1441,6 @@ Audio · 1440 unmodified lines
14411441 want. For example, most A/V receivers connected via HDMI and that can
14421442 do 7.1 would be served by: ``--audio-channels=7.1,5.1,stereo``
14431443
1444−``--audio-normalize-downmix=<yes|no>``
1445− Enable/disable normalization if surround audio is downmixed to stereo
1446− (default: no). If this is disabled, downmix can cause clipping. If it's
1447− enabled, the output might be too silent. It depends on the source audio.
1448−
1449− Technically, this changes the ``normalize`` suboption of the
1450− ``lavrresample`` audio filter, which performs the downmixing.
1451−
1452− If downmix happens outside of mpv for some reason, this has no effect.
1453−
14541444 ``--audio-display=<no|attachment>``
14551445 Setting this option to ``attachment`` (default) will display image
14561446 attachments (e.g. album cover art) when playing audio files. It will
@@ -3453,6 +3443,44 @@ Software Scaler · 1996 unmodified lines
34533443 ``--sws-cvs=<v>``
34543444 Software scaler chroma vertical shifting. See ``--sws-scaler``.
34553445
3446+Audio Resampler
3447+---------------
3448+
3449+This controls the default options of any resampling done by mpv (but not within
3450+libavfilter, within the system audio API resampler, or any other places).
3451+
3452+It also sets the defaults for the ``lavrresample`` audio filter.
3453+
3454+``--audio-resample-filter-size=<length>``
3455+ Length of the filter with respect to the lower sampling rate. (default:
3456+ 16)
3457+
3458+``--audio-resample-phase-shift=<count>``
3459+ Log2 of the number of polyphase entries. (..., 10->1024, 11->2048,
3460+ 12->4096, ...) (default: 10->1024)
3461+
3462+``--audio-resample-cutoff=<cutoff>``
3463+ Cutoff frequency (0.0-1.0), default set depending upon filter length.
3464+
3465+``--audio-resample-linear=<yes|no>``
3466+ If set then filters will be linearly interpolated between polyphase
3467+ entries. (default: no)
3468+
3469+``--audio-normalize-downmix=<yes|no>``
3470+ Enable/disable normalization if surround audio is downmixed to stereo
3471+ (default: no). If this is disabled, downmix can cause clipping. If it's
3472+ enabled, the output might be too quiet. It depends on the source audio.
3473+
3474+ Technically, this changes the ``normalize`` suboption of the
3475+ ``lavrresample`` audio filter, which performs the downmixing.
3476+
3477+ If downmix happens outside of mpv for some reason, or in the decoder
3478+ (decoder downmixing), or in the audio output (system mixer), this has no
3479+ effect.
3480+
3481+``--audio-swresample-o=<string>``
3482+ Set AVOptions on the SwrContext or AVAudioResampleContext. These should
3483+ be documented by FFmpeg or Libav.
34563484
34573485 Terminal
34583486 --------
audio/aconverter.c+20−8
@@ -86,6 +86,23 @@ struct mp_aconverter { · 85 unmodified lines
8686 bool output_eof; // queued output EOF
8787 };
8888
89+#define OPT_BASE_STRUCT struct mp_resample_opts
90+const struct m_sub_options resample_config = {
91+ .opts = (const m_option_t[]) {
92+ OPT_INTRANGE("audio-resample-filter-size", filter_size, 0, 0, 32),
93+ OPT_INTRANGE("audio-resample-phase-shift", phase_shift, 0, 0, 30),
94+ OPT_FLAG("audio-resample-linear", linear, 0),
95+ OPT_DOUBLE("audio-resample-cutoff", cutoff, M_OPT_RANGE,
96+ .min = 0, .max = 1),
97+ OPT_FLAG("audio-normalize-downmix", normalize, 0),
98+ OPT_KEYVALUELIST("audio-swresample-o", avopts, 0),
99+ {0}
100+ },
101+ .size = sizeof(struct mp_resample_opts),
102+ .defaults = &(const struct mp_resample_opts)MP_RESAMPLE_OPTS_DEF,
103+ .change_flags = UPDATE_AUDIO,
104+};
105+
89106 #if HAVE_LIBAVRESAMPLE
90107 static double get_delay(struct mp_aconverter *p)
91108 {
@@ -211,12 +228,7 @@ static bool configure_lavrr(struct mp_aconverter *p, bool verbose) · 119 unmodified lines
211228 cutoff = MPMAX(1.0 - 6.5 / (p->opts->filter_size + 8), 0.80);
212229 av_opt_set_double(p->avrctx, "cutoff", cutoff, 0);
213230
214− int global_normalize;
215− mp_read_option_raw(p->global, "audio-normalize-downmix", &m_option_type_flag,
216− &global_normalize);
217231 int normalize = p->opts->normalize;
218− if (normalize < 0)
219− normalize = global_normalize;
220232 #if HAVE_LIBSWRESAMPLE
221233 av_opt_set_double(p->avrctx, "rematrix_maxval", normalize ? 1 : 1000, 0);
222234 #else
@@ -628,9 +640,9 @@ struct mp_aconverter *mp_aconverter_create(struct mpv_global *global, · 405 unmodified lines
628640 p->log = log;
629641 p->global = global;
630642
631− static const struct mp_resample_opts defs = MP_RESAMPLE_OPTS_DEF;
632−
633− p->opts = opts ? opts : &defs;
643+ p->opts = opts;
644+ if (!p->opts)
645+ p->opts = mp_get_config_group(p, global, &resample_config);
634646
635647 p->reorder_buffer = mp_aframe_pool_create(p);
636648 p->out_pool = mp_aframe_pool_create(p);
audio/aconverter.h+3−1
@@ -23,9 +23,11 @@ struct mp_resample_opts { · 22 unmodified lines
2323 .filter_size = 16, \
2424 .cutoff = 0.0, \
2525 .phase_shift = 10, \
26− .normalize = -1, \
26+ .normalize = 0, \
2727 }
2828
29+extern const struct m_sub_options resample_config;
30+
2931 struct mp_aconverter *mp_aconverter_create(struct mpv_global *global,
3032 struct mp_log *log,
3133 const struct mp_resample_opts *opts);
audio/filter/af.c+2−1
@@ -63,6 +63,7 @@ static bool get_desc(struct m_obj_desc *dst, int index) · 62 unmodified lines
6363 .priv_size = af->priv_size,
6464 .priv_defaults = af->priv_defaults,
6565 .options = af->options,
66+ .set_defaults = af->set_defaults,
6667 .p = af,
6768 };
6869 return true;
@@ -170,7 +171,7 @@ static struct af_instance *af_create(struct af_stream *s, char *name, · 101 unmodified lines
170171 .out_pool = mp_audio_pool_create(af),
171172 };
172173 struct m_config *config =
173− m_config_from_obj_desc_and_args(af, s->log, NULL, &desc,
174+ m_config_from_obj_desc_and_args(af, s->log, s->global, &desc,
174175 name, s->opts->af_defs, args);
175176 if (!config)
176177 goto error;
audio/filter/af.h+2−0
@@ -52,6 +52,8 @@ struct af_info { · 51 unmodified lines
5252 int priv_size;
5353 const void *priv_defaults;
5454 const struct m_option *options;
55+ // For m_obj_desc.set_defaults
56+ void (*set_defaults)(struct mpv_global *global, void *p);
5557 };
5658
5759 // Linked list of audio filters
audio/filter/af_lavrresample.c+26−0
@@ -32,6 +32,7 @@ · 31 unmodified lines
3232
3333 #include "common/av_common.h"
3434 #include "common/msg.h"
35+#include "options/m_config.h"
3536 #include "options/m_option.h"
3637 #include "audio/filter/af.h"
3738 #include "audio/fmt-conversion.h"
@@ -42,6 +43,7 @@ struct af_resample { · 4 unmodified lines
4243 int allow_detach;
4344 double playback_speed;
4445 struct mp_resample_opts opts;
46+ int global_normalize;
4547 struct mp_aconverter *converter;
4648 };
4749
@@ -142,11 +144,34 @@ static int af_open(struct af_instance *af) · 94 unmodified lines
142144 af->filter_frame = filter;
143145 af->filter_out = filter_out;
144146
147+ if (s->opts.normalize < 0)
148+ s->opts.normalize = s->global_normalize;
149+
145150 s->converter = mp_aconverter_create(af->global, af->log, &s->opts);
146151
147152 return AF_OK;
148153 }
149154
155+static void set_defaults(struct mpv_global *global, void *p)
156+{
157+ struct af_resample *s = p;
158+
159+ struct mp_resample_opts *opts = &s->opts;
160+
161+ struct mp_resample_opts *src_opts =
162+ mp_get_config_group(s, global, &resample_config);
163+
164+ s->global_normalize = src_opts->normalize;
165+
166+ assert(!opts->avopts); // we don't set a default value, so it must be NULL
167+
168+ *opts = *src_opts;
169+
170+ opts->avopts = NULL;
171+ struct m_option dummy = {.type = &m_option_type_keyvalue_list};
172+ m_option_copy(&dummy, &opts->avopts, &src_opts->avopts);
173+}
174+
150175 #define OPT_BASE_STRUCT struct af_resample
151176
152177 const struct af_info af_info_lavrresample = {
@@ -170,4 +195,5 @@ const struct af_info af_info_lavrresample = { · 17 unmodified lines
170195 OPT_KEYVALUELIST("o", opts.avopts, 0),
171196 {0}
172197 },
198+ .set_defaults = set_defaults,
173199 };
options/m_config.c+12−5
@@ -194,11 +194,18 @@ struct m_config *m_config_new(void *talloc_ctx, struct mp_log *log, · 193 unmodified lines
194194 return config;
195195 }
196196
197−struct m_config *m_config_from_obj_desc(void *talloc_ctx, struct mp_log *log,
198− struct m_obj_desc *desc)
197+static struct m_config *m_config_from_obj_desc(void *talloc_ctx,
198+ struct mp_log *log,
199+ struct mpv_global *global,
200+ struct m_obj_desc *desc)
199201 {
200− return m_config_new(talloc_ctx, log, desc->priv_size, desc->priv_defaults,
201− desc->options);
202+ struct m_config *c =
203+ m_config_new(talloc_ctx, log, desc->priv_size, desc->priv_defaults,
204+ desc->options);
205+ c->global = global;
206+ if (desc->set_defaults && c->global)
207+ desc->set_defaults(c->global, c->optstruct);
208+ return c;
202209 }
203210
204211 // Like m_config_from_obj_desc(), but don't allocate option struct.
@@ -260,7 +267,7 @@ struct m_config *m_config_from_obj_desc_and_args(void *ta_parent, · 55 unmodified lines
260267 struct mp_log *log, struct mpv_global *global, struct m_obj_desc *desc,
261268 const char *name, struct m_obj_settings *defaults, char **args)
262269 {
263− struct m_config *config = m_config_from_obj_desc(ta_parent, log, desc);
270+ struct m_config *config = m_config_from_obj_desc(ta_parent, log, global, desc);
264271
265272 for (int n = 0; defaults && defaults[n].name; n++) {
266273 struct m_obj_settings *entry = &defaults[n];
options/m_config.h+0−3
@@ -123,9 +123,6 @@ struct m_config *m_config_new(void *talloc_ctx, struct mp_log *log, · 122 unmodified lines
123123 // mpv_global. Expected to be called at early init on the main m_config.
124124 void m_config_create_shadow(struct m_config *config);
125125
126−struct m_config *m_config_from_obj_desc(void *talloc_ctx, struct mp_log *log,
127− struct m_obj_desc *desc);
128−
129126 struct m_config *m_config_from_obj_desc_noalloc(void *talloc_ctx,
130127 struct mp_log *log,
131128 struct m_obj_desc *desc);
options/m_option.h+5−0
@@ -32,6 +32,7 @@ typedef struct m_option m_option_t; · 31 unmodified lines
3232 struct m_config;
3333 struct mp_log;
3434 struct mpv_node;
35+struct mpv_global;
3536
3637 ///////////////////////////// Options types declarations ////////////////////
3738
@@ -129,6 +130,10 @@ struct m_obj_desc { · 91 unmodified lines
129130 bool hidden;
130131 // Callback to print custom help if "help" is passed
131132 void (*print_help)(struct mp_log *log);
133+ // Callback that allows you to override the static default values. The
134+ // pointer p points to the struct described by options/priv_size, with
135+ // priv_defaults already applied. You can write to it to set any defaults.
136+ void (*set_defaults)(struct mpv_global *global, void *p);
132137 // Set by m_obj_list_find(). If the requested name is an old alias, this
133138 // is set to the old name (while the name field uses the new name).
134139 const char *replaced_name;
options/options.c+4−1
@@ -94,6 +94,8 @@ extern const struct m_sub_options angle_conf; · 93 unmodified lines
9494 extern const struct m_sub_options cocoa_conf;
9595 extern const struct m_sub_options android_conf;
9696
97+extern const struct m_sub_options resample_config;
98+
9799 static const struct m_sub_options screenshot_conf = {
98100 .opts = image_writer_opts,
99101 .size = sizeof(struct image_writer_opts),
@@ -474,7 +476,6 @@ const m_option_t mp_opts[] = { · 374 unmodified lines
474476 OPT_INTRANGE("audio-samplerate", force_srate, UPDATE_AUDIO, 1000, 16*48000),
475477 OPT_CHANNELS("audio-channels", audio_output_channels, UPDATE_AUDIO),
476478 OPT_AUDIOFORMAT("audio-format", audio_output_format, UPDATE_AUDIO),
477− OPT_FLAG("audio-normalize-downmix", audio_normalize, UPDATE_AUDIO),
478479 OPT_DOUBLE("speed", playback_speed, M_OPT_RANGE, .min = 0.01, .max = 100.0),
479480
480481 OPT_FLAG("audio-pitch-correction", pitch_correction, 0),
@@ -691,6 +692,8 @@ const m_option_t mp_opts[] = { · 210 unmodified lines
691692
692693 OPT_STRING("record-file", record_file, M_OPT_FILE),
693694
695+ OPT_SUBSTRUCT("", resample_opts, resample_config, 0),
696+
694697 OPT_SUBSTRUCT("", input_opts, input_config, 0),
695698
696699 OPT_SUBSTRUCT("", vo, vo_sub_opts, 0),
options/options.h+2−1
@@ -275,7 +275,6 @@ typedef struct MPOpts { · 274 unmodified lines
275275
276276 struct m_channels audio_output_channels;
277277 int audio_output_format;
278− int audio_normalize;
279278 int force_srate;
280279 double playback_speed;
281280 int pitch_correction;
@@ -331,6 +330,8 @@ typedef struct MPOpts { · 49 unmodified lines
331330
332331 int wingl_dwm_flush;
333332
333+ struct mp_resample_opts *resample_opts;
334+
334335 struct gl_video_opts *gl_video_opts;
335336 struct angle_opts *angle_opts;
336337 struct opengl_opts *opengl_opts;

Keyboard shortcuts

General

Show keyboard shortcuts
?
Focus the current search
/

Issues and pull requests

Open filters
f

Issues, pull requests and Inbox

Select next item
j↓
Select previous item
k↑
Open selected item
Enter
Dismiss filters or clear selection
Esc

Repository code

Go to file
t