1
mirror of https://github.com/mpv-player/mpv synced 2024-08-20 08:55:06 +02:00
mpv/filters/f_autoconvert.h
wm4 3c123281a7 audio: change format negotiation, remove channel remix fudging
The audio format neogitation code was pretty complicated, although the
idea was simple: when the format changes (or on the first audio frame),
filter only the new frame through the entire filter chain, discard the
resulting frame, but use the format to initialize the AO.

This was useful for "fudging" the channel remix behavior (upmix or
downmix), and moving it before other filters. Apparently this was useful
for things like DRC filters, which might work better in stereo, and
which also can only achieve the desired volume levels by doing it before
a downmix, which would modify the volume. This mechanism was introduced
in commit 60048b7eb9 (which the commit message also describes as
"idiotic heuristic"). Knowing the output format is inherently necessary
for this, because otherwise we can't know what the hell the user defined
filters will do.

There were problems with robustness. Some filters needed more than one
frame. Resampling in particular would discard initial audio at high
resampling ratios. Some filters might drop audio intentionally (like
clipping data on timestamp ranges). There were also allegations that
some decoders output 0 length frames (although that is invalid in
libavcodec). The state machine was excessively complex and hard to
understand too.

There are 3 things that could have been done:

1. Fix robustness problems by doing more heuristics, like repeating
   audio frames or simply decoding several frames. Since filters can
   behave differently, this would have added lots of complexity.
2. Make use of libavfilter's format negotiation, and add the same to
   mpv builtin filters. This is sort of annoying, because the format
   negotiation in libavfilter changes the state of the filters. It also
   reports only some parameters (mostly all for audio, but a lot of
   holes for video). It would remove some of the state machine, but not
   all.
3. Drop the channel remix fudging, and do the same as the video chain.
   This would not require format negotiation, but instead you can just
   filter the audio frames, and look what comes out of it. If nothing
   comes out, simply never create an AO.

This commit selects option 3. It removes the remix fudging, which means
the loss of a feature. Users can instead add "--af=format=channels=2"
before their DRC filter, or something. I'm also considering changing the
default for --audio-channels back to stereo, and downmix in the decoder
or at the start of the filter chain, which would give the same results,
except requiring more configuration.

Implementation-wise, this is still a bit different from the video path.
The VO always remains the same instance, while the AO might have to be
recreated on configuration changes. This still requires explicit format
change handling + draining old data, but by putting it into
f_autoconvert, not much new code is needed.
2018-04-15 23:11:33 +03:00

61 lines
2.5 KiB
C

#pragma once
#include "filter.h"
// A filter which automatically creates and uses a conversion filter based on
// the filter settings, or passes through data unchanged if no conversion is
// required.
struct mp_autoconvert {
// f->pins[0] is input, f->pins[1] is output
struct mp_filter *f;
// If this is set, the callback is invoked (from the process function), and
// further data flow is blocked until mp_autoconvert_format_change_continue()
// is called. The idea is that you can reselect the output parameters on
// format changes and continue filtering when ready.
void (*on_audio_format_change)(void *opaque);
void *on_audio_format_change_opaque;
};
// (to free this, free the filter itself, mp_autoconvert.f)
struct mp_autoconvert *mp_autoconvert_create(struct mp_filter *parent);
// Add the imgfmt as allowed video image format, and error on non-video frames.
// Each call adds to the list of allowed formats. Before the first call, all
// formats are allowed (even non-video).
// subfmt can be used to specify underlying surface formats for hardware formats,
// otherwise must be 0.
void mp_autoconvert_add_imgfmt(struct mp_autoconvert *c, int imgfmt, int subfmt);
// Add the formats supported by the hwdec interops (or essentially refine them),
// and trigger conversion if hw_subfmts mismatch. This is mostly a hack for
// D3D11/ANGLE (which supports NV12 only).
// Must be called mp_autoconvert_add_imgfmt(), and overrides them where formats
// collide.
struct mp_hwdec_devices;
void mp_autoconvert_add_vo_hwdec_subfmts(struct mp_autoconvert *c,
struct mp_hwdec_devices *devs);
// Add afmt (an AF_FORMAT_* value) as allowed audio format.
// See mp_autoconvert_add_imgfmt() for other remarks.
void mp_autoconvert_add_afmt(struct mp_autoconvert *c, int afmt);
// Add allowed audio channel configuration.
struct mp_chmap;
void mp_autoconvert_add_chmap(struct mp_autoconvert *c, struct mp_chmap *chmap);
// Add allowed audio sample rate.
void mp_autoconvert_add_srate(struct mp_autoconvert *c, int rate);
// Reset set of allowed formats back to initial state. (This does not flush
// any frames or remove currently active filters, although to get reasonable
// behavior, you need to readd all previously allowed formats, or reset the
// filter.)
void mp_autoconvert_clear(struct mp_autoconvert *c);
// See mp_autoconvert.on_audio_format_change.
void mp_autoconvert_format_change_continue(struct mp_autoconvert *c);
// vf_d3d11vpp.c
struct mp_filter *vf_d3d11_create_outconv(struct mp_filter *parent);