1
mirror of https://github.com/mpv-player/mpv synced 2024-11-18 21:16:10 +01:00
mpv/audio/filter/af.h
wm4 824e6550f8 audio/filter: fix mul/delay scale and values
Before this commit, the af_instance->mul/delay values were in bytes.
Using bytes is confusing for non-interleaved audio, so switch mul to
samples, and delay to seconds. For delay, seconds are more intuitive
than bytes or samples, because it's used for the latency calculation.
We also might want to replace the delay mechanism with real PTS
tracking inside the filter chain some time in the future, and PTS
will also require time-adjustments to be done in seconds.

For most filters, we just remove the redundant mul=1 initialization.
(Setting this used to be required, but not anymore.)
2013-11-12 23:34:35 +01:00

249 lines
7.5 KiB
C

/*
* This file is part of MPlayer.
*
* MPlayer 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.
*
* MPlayer 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 MPlayer; if not, write to the Free Software Foundation, Inc.,
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*/
#ifndef MPLAYER_AF_H
#define MPLAYER_AF_H
#include <stdio.h>
#include <stdbool.h>
#include "config.h"
#include "mpvcore/options.h"
#include "audio/format.h"
#include "audio/chmap.h"
#include "audio/audio.h"
#include "control.h"
#include "mpvcore/mp_msg.h"
struct af_instance;
// Number of channels
#define AF_NCH MP_NUM_CHANNELS
// Flags used for defining the behavior of an audio filter
#define AF_FLAGS_REENTRANT 0x00000000
#define AF_FLAGS_NOT_REENTRANT 0x00000001
/* Audio filter information not specific for current instance, but for
a specific filter */
struct af_info {
const char *info;
const char *name;
const int flags;
int (*open)(struct af_instance *vf);
bool (*test_conversion)(int src_format, int dst_format);
int priv_size;
const void *priv_defaults;
const struct m_option *options;
};
// Linked list of audio filters
struct af_instance {
const struct af_info *info;
int (*control)(struct af_instance *af, int cmd, void *arg);
void (*uninit)(struct af_instance *af);
struct mp_audio * (*play)(struct af_instance *af, struct mp_audio *data);
void *setup; // old field for priv structs
void *priv;
struct mp_audio *data; // configuration and buffer for outgoing data stream
struct af_instance *next;
struct af_instance *prev;
double delay; /* Delay caused by the filter, in seconds of audio consumed
* without corresponding output */
double mul; /* length multiplier: how much does this instance change
* the number of samples passed though. (Ratio of input
* and output, e.g. mul=4 => 1 sample becomes 4 samples) .*/
bool auto_inserted; // inserted by af.c, such as conversion filters
};
// Current audio stream
struct af_stream {
// The first and last filter in the list
struct af_instance *first;
struct af_instance *last;
// The user sets the input format (what the decoder outputs), and sets some
// or all fields in output to the output format the AO accepts.
// See fixup_output_format().
struct mp_audio input;
struct mp_audio output;
struct mp_audio filter_output;
struct MPOpts *opts;
};
/*********************************************
// Return values
*/
#define AF_DETACH 2
#define AF_OK 1
#define AF_TRUE 1
#define AF_FALSE 0
#define AF_UNKNOWN -1
#define AF_ERROR -2
#define AF_FATAL -3
/*********************************************
// Export functions
*/
/**
* \defgroup af_chain Audio filter chain functions
* \{
* \param s filter chain
*/
struct af_stream *af_new(struct MPOpts *opts);
void af_destroy(struct af_stream *s);
/**
* \brief Initialize the stream "s".
* \return 0 on success, -1 on failure
*
* This function creates a new filter list if necessary, according
* to the values set in input and output. Input and output should contain
* the format of the current movie and the format of the preferred output
* respectively.
* Filters to convert to the preferred output format are inserted
* automatically, except when they are set to 0.
* The function is reentrant i.e. if called with an already initialized
* stream the stream will be reinitialized.
*/
int af_init(struct af_stream *s);
/**
* \brief Uninit and remove all filters from audio filter chain
*/
void af_uninit(struct af_stream *s);
/**
* \brief This function adds the filter "name" to the stream s.
* \param name name of filter to add
* \return pointer to the new filter, NULL if insert failed
*
* The filter will be inserted somewhere nice in the
* list of filters (i.e. at the beginning unless the
* first filter is the format filter (why??).
*/
struct af_instance *af_add(struct af_stream *s, char *name, char **args);
/**
* \brief filter data chunk through the filters in the list
* \param data data to play
* \return resulting data
* \ingroup af_chain
*/
struct mp_audio *af_play(struct af_stream *s, struct mp_audio *data);
/**
* \brief send control to all filters, starting with the last until
* one accepts the command with AF_OK.
* \param cmd filter control command
* \param arg argument for filter command
* \return the accepting filter or NULL if none was found
*/
struct af_instance *af_control_any_rev(struct af_stream *s, int cmd, void *arg);
/**
* \brief calculate average ratio of filter output lenth to input length
* \return the ratio
*/
double af_calc_filter_multiplier(struct af_stream *s);
/**
* \brief Calculate the total delay caused by the filters
* \return delay in bytes of "missing" output
*/
double af_calc_delay(struct af_stream *s);
/** \} */ // end of af_chain group
// Helper functions and macros used inside the audio filters
/**
* \defgroup af_filter Audio filter helper functions
* \{
*/
/**
* \brief convert dB to gain value
* \param n number of values to convert
* \param in [in] values in dB, <= -200 will become 0 gain
* \param out [out] gain values
* \param k input values are divided by this
* \param mi minimum dB value, input will be clamped to this
* \param ma maximum dB value, input will be clamped to this
* \return AF_ERROR on error, AF_OK otherwise
*/
int af_from_dB(int n, float *in, float *out, float k, float mi, float ma);
/**
* \brief convert gain value to dB
* \param n number of values to convert
* \param in [in] gain values, 0 wil become -200 dB
* \param out [out] values in dB
* \param k output values will be multiplied by this
* \return AF_ERROR on error, AF_OK otherwise
*/
int af_to_dB(int n, float *in, float *out, float k);
/**
* \brief convert milliseconds to sample time
* \param n number of values to convert
* \param in [in] values in milliseconds
* \param out [out] sample time values
* \param rate sample rate
* \param mi minimum ms value, input will be clamped to this
* \param ma maximum ms value, input will be clamped to this
* \return AF_ERROR on error, AF_OK otherwise
*/
int af_from_ms(int n, float *in, int *out, int rate, float mi, float ma);
/**
* \brief convert sample time to milliseconds
* \param n number of values to convert
* \param in [in] sample time values
* \param out [out] values in milliseconds
* \param rate sample rate
* \return AF_ERROR on error, AF_OK otherwise
*/
int af_to_ms(int n, int *in, float *out, int rate);
/**
* \brief test if output format matches
* \param af audio filter
* \param out needed format, will be overwritten by available
* format if they do not match
* \return AF_FALSE if formats do not match, AF_OK if they match
*
* compares the format, bps, rate and nch values of af->data with out
*/
int af_test_output(struct af_instance *af, struct mp_audio *out);
/**
* \brief soft clipping function using sin()
* \param a input value
* \return clipped value
*/
float af_softclip(float a);
#endif /* MPLAYER_AF_H */