FFmpeg
|
Audio resampling, sample format conversion and mixing library. More...
Files | |
file | swresample.h |
Functions | |
const AVClass * | swr_get_class (void) |
Get the AVClass for SwrContext. More... | |
Option constants | |
These constants are used for the AVOptions interface for lswr. | |
enum | SwrDitherType { SWR_DITHER_NONE = 0, SWR_DITHER_RECTANGULAR, SWR_DITHER_TRIANGULAR, SWR_DITHER_TRIANGULAR_HIGHPASS, SWR_DITHER_NS = 64, SWR_DITHER_NS_LIPSHITZ, SWR_DITHER_NS_F_WEIGHTED, SWR_DITHER_NS_MODIFIED_E_WEIGHTED, SWR_DITHER_NS_IMPROVED_E_WEIGHTED, SWR_DITHER_NS_SHIBATA, SWR_DITHER_NS_LOW_SHIBATA, SWR_DITHER_NS_HIGH_SHIBATA, SWR_DITHER_NB } |
Dithering algorithms. More... | |
enum | SwrEngine { SWR_ENGINE_SWR, SWR_ENGINE_SOXR, SWR_ENGINE_NB } |
Resampling Engines. More... | |
enum | SwrFilterType { SWR_FILTER_TYPE_CUBIC, SWR_FILTER_TYPE_BLACKMAN_NUTTALL, SWR_FILTER_TYPE_KAISER } |
Resampling Filter Types. More... | |
#define | SWR_FLAG_RESAMPLE 1 |
Force resampling even if equal sample rate. More... | |
SwrContext constructor functions | |
struct SwrContext * | swr_alloc (void) |
Allocate SwrContext. More... | |
int | swr_init (struct SwrContext *s) |
Initialize context after user parameters have been set. More... | |
int | swr_is_initialized (struct SwrContext *s) |
Check whether an swr context has been initialized or not. More... | |
struct SwrContext * | swr_alloc_set_opts (struct SwrContext *s, int64_t out_ch_layout, enum AVSampleFormat out_sample_fmt, int out_sample_rate, int64_t in_ch_layout, enum AVSampleFormat in_sample_fmt, int in_sample_rate, int log_offset, void *log_ctx) |
Allocate SwrContext if needed and set/reset common parameters. More... | |
SwrContext destructor functions | |
void | swr_free (struct SwrContext **s) |
Free the given SwrContext and set the pointer to NULL. More... | |
void | swr_close (struct SwrContext *s) |
Closes the context so that swr_is_initialized() returns 0. More... | |
Core conversion functions | |
int | swr_convert (struct SwrContext *s, uint8_t **out, int out_count, const uint8_t **in, int in_count) |
Convert audio. More... | |
int64_t | swr_next_pts (struct SwrContext *s, int64_t pts) |
Convert the next timestamp from input to output timestamps are in 1/(in_sample_rate * out_sample_rate) units. More... | |
Low-level option setting functions | |
These functons provide a means to set low-level options that is not possible with the AVOption API. | |
int | swr_set_compensation (struct SwrContext *s, int sample_delta, int compensation_distance) |
Activate resampling compensation ("soft" compensation). More... | |
int | swr_set_channel_mapping (struct SwrContext *s, const int *channel_map) |
Set a customized input channel mapping. More... | |
int | swr_build_matrix (uint64_t in_layout, uint64_t out_layout, double center_mix_level, double surround_mix_level, double lfe_mix_level, double rematrix_maxval, double rematrix_volume, double *matrix, int stride, enum AVMatrixEncoding matrix_encoding, void *log_ctx) |
Generate a channel mixing matrix. More... | |
int | swr_set_matrix (struct SwrContext *s, const double *matrix, int stride) |
Set a customized remix matrix. More... | |
Sample handling functions | |
int | swr_drop_output (struct SwrContext *s, int count) |
Drops the specified number of output samples. More... | |
int | swr_inject_silence (struct SwrContext *s, int count) |
Injects the specified number of silence samples. More... | |
int64_t | swr_get_delay (struct SwrContext *s, int64_t base) |
Gets the delay the next input sample will experience relative to the next output sample. More... | |
int | swr_get_out_samples (struct SwrContext *s, int in_samples) |
Find an upper bound on the number of samples that the next swr_convert call will output, if called with in_samples of input samples. More... | |
Configuration accessors | |
unsigned | swresample_version (void) |
Return the LIBSWRESAMPLE_VERSION_INT constant. More... | |
const char * | swresample_configuration (void) |
Return the swr build-time configuration. More... | |
const char * | swresample_license (void) |
Return the swr license. More... | |
AVFrame based API | |
int | swr_convert_frame (SwrContext *swr, AVFrame *output, const AVFrame *input) |
Convert the samples in the input AVFrame and write them to the output AVFrame. More... | |
int | swr_config_frame (SwrContext *swr, const AVFrame *out, const AVFrame *in) |
Configure or reconfigure the SwrContext using the information provided by the AVFrames. More... | |
Audio resampling, sample format conversion and mixing library.
Interaction with lswr is done through SwrContext, which is allocated with swr_alloc() or swr_alloc_set_opts(). It is opaque, so all parameters must be set with the AVOptions API.
The first thing you will need to do in order to use lswr is to allocate SwrContext. This can be done with swr_alloc() or swr_alloc_set_opts(). If you are using the former, you must set options through the AVOptions API. The latter function provides the same feature, but it allows you to set some common options in the same statement.
For example the following code will setup conversion from planar float sample format to interleaved signed 16-bit integer, downsampling from 48kHz to 44.1kHz and downmixing from 5.1 channels to stereo (using the default mixing matrix). This is using the swr_alloc() function.
The same job can be done using swr_alloc_set_opts() as well:
Once all values have been set, it must be initialized with swr_init(). If you need to change the conversion parameters, you can change the parameters using AVOptions, as described above in the first example; or by using swr_alloc_set_opts(), but with the first argument the allocated context. You must then call swr_init() again.
The conversion itself is done by repeatedly calling swr_convert(). Note that the samples may get buffered in swr if you provide insufficient output space or if sample rate conversion is done, which requires "future" samples. Samples that do not require future input can be retrieved at any time by using swr_convert() (in_count can be set to 0). At the end of conversion the resampling buffer can be flushed by calling swr_convert() with NULL in and 0 in_count.
The samples used in the conversion process can be managed with the libavutil samples manipulation API, including av_samples_alloc() function used in the following example.
The delay between input and output, can at any time be found by using swr_get_delay().
The following code demonstrates the conversion loop assuming the parameters from above and caller-defined functions get_input() and handle_output():
When the conversion is finished, the conversion context and everything associated with it must be freed with swr_free(). A swr_close() function is also available, but it exists mainly for compatibility with libavresample, and is not required to be called.
There will be no memory leak if the data is not completely flushed before swr_free().
#define SWR_FLAG_RESAMPLE 1 |
Force resampling even if equal sample rate.
Definition at line 136 of file swresample.h.
enum SwrDitherType |
Dithering algorithms.
Definition at line 141 of file swresample.h.
enum SwrEngine |
Resampling Engines.
Enumerator | |
---|---|
SWR_ENGINE_SWR | SW Resampler. |
SWR_ENGINE_SOXR | SoX Resampler. |
SWR_ENGINE_NB | not part of API/ABI |
Definition at line 159 of file swresample.h.
enum SwrFilterType |
Resampling Filter Types.
Enumerator | |
---|---|
SWR_FILTER_TYPE_CUBIC | Cubic. |
SWR_FILTER_TYPE_BLACKMAN_NUTTALL | Blackman Nuttall windowed sinc. |
SWR_FILTER_TYPE_KAISER | Kaiser windowed sinc. |
Definition at line 166 of file swresample.h.
const AVClass* swr_get_class | ( | void | ) |
Get the AVClass for SwrContext.
It can be used in combination with AV_OPT_SEARCH_FAKE_OBJ for examining options.
Definition at line 145 of file options.c.
Referenced by opt_default(), resample_child_class_iterate(), and show_help_default().
struct SwrContext* swr_alloc | ( | void | ) |
Allocate SwrContext.
If you use this function you will need to set the parameters (manually or with swr_alloc_set_opts()) before calling swr_init().
Definition at line 150 of file options.c.
Referenced by config_audio_output(), main(), open_audio(), opt_default(), opus_decode_init(), preinit(), and swr_alloc_set_opts().
int swr_init | ( | struct SwrContext * | s | ) |
Initialize context after user parameters have been set.
[in,out] | s | Swr context to initialize |
Definition at line 152 of file swresample.c.
Referenced by audio_decode_frame(), config_audio_output(), config_output(), config_props(), init_resampler(), main(), open_audio(), opus_init_resample(), swr_convert_frame(), and swr_set_compensation().
int swr_is_initialized | ( | struct SwrContext * | s | ) |
Check whether an swr context has been initialized or not.
[in] | s | Swr context to check |
Definition at line 712 of file swresample.c.
Referenced by opus_decode_frame(), opus_decode_subpacket(), swr_convert(), and swr_convert_frame().
struct SwrContext* swr_alloc_set_opts | ( | struct SwrContext * | s, |
int64_t | out_ch_layout, | ||
enum AVSampleFormat | out_sample_fmt, | ||
int | out_sample_rate, | ||
int64_t | in_ch_layout, | ||
enum AVSampleFormat | in_sample_fmt, | ||
int | in_sample_rate, | ||
int | log_offset, | ||
void * | log_ctx | ||
) |
Allocate SwrContext if needed and set/reset common parameters.
This function does not require s to be allocated with swr_alloc(). On the other hand, swr_alloc() can use swr_alloc_set_opts() to set the parameters on the allocated context.
s | existing Swr context if available, or NULL if not |
out_ch_layout | output channel layout (AV_CH_LAYOUT_*) |
out_sample_fmt | output sample format (AV_SAMPLE_FMT_*). |
out_sample_rate | output sample rate (frequency in Hz) |
in_ch_layout | input channel layout (AV_CH_LAYOUT_*) |
in_sample_fmt | input sample format (AV_SAMPLE_FMT_*). |
in_sample_rate | input sample rate (frequency in Hz) |
log_offset | logging level offset |
log_ctx | parent logging context, can be NULL |
Definition at line 59 of file swresample.c.
Referenced by audio_decode_frame(), config_output(), config_props(), init_resampler(), and main().
void swr_free | ( | struct SwrContext ** | s | ) |
Free the given SwrContext and set the pointer to NULL.
[in] | s | a pointer to a pointer to Swr context |
Definition at line 137 of file swresample.c.
Referenced by audio_decode_frame(), close_stream(), init_resampler(), main(), opt_default(), opus_decode_close(), stream_component_close(), swr_alloc_set_opts(), and uninit().
void swr_close | ( | struct SwrContext * | s | ) |
Closes the context so that swr_is_initialized() returns 0.
The context can be brought back to life by running swr_init(), swr_init() can also be used without swr_close(). This function is mainly provided for simplifying the usecase where one tries to support libavresample and libswresample.
[in,out] | s | Swr context to be closed |
Definition at line 148 of file swresample.c.
Referenced by opus_decode_flush(), opus_decode_subpacket(), swr_config_frame(), swr_convert_frame(), and swr_init().
int swr_convert | ( | struct SwrContext * | s, |
uint8_t ** | out, | ||
int | out_count, | ||
const uint8_t ** | in, | ||
int | in_count | ||
) |
Convert audio.
in and in_count can be set to 0 to flush the last few samples out at the end.
If more input is provided than output space, then the input will be buffered. You can avoid this buffering by using swr_get_out_samples() to retrieve an upper bound on the required number of output samples for the given number of input samples. Conversion will run directly without copying whenever possible.
s | allocated Swr context, with parameters set |
out | output buffers, only the first one need be set in case of packed audio |
out_count | amount of space available for output in samples per channel |
in | input buffers, only the first one need to be set in case of packed audio |
in_count | number of input samples available in one channel |
Definition at line 716 of file swresample.c.
Referenced by audio_decode_frame(), convert_frame(), convert_samples(), filter_frame(), flush_frame(), main(), opus_decode_frame(), opus_flush_resample(), opus_init_resample(), swr_convert(), swr_drop_output(), swr_inject_silence(), wrap(), and write_audio_frame().
int64_t swr_next_pts | ( | struct SwrContext * | s, |
int64_t | pts | ||
) |
Convert the next timestamp from input to output timestamps are in 1/(in_sample_rate * out_sample_rate) units.
s[in] | initialized Swr context |
pts[in] | timestamp for the next input sample, INT64_MIN if unknown |
Definition at line 921 of file swresample.c.
Referenced by filter_frame(), and flush_frame().
int swr_set_compensation | ( | struct SwrContext * | s, |
int | sample_delta, | ||
int | compensation_distance | ||
) |
Activate resampling compensation ("soft" compensation).
This function is internally called when needed in swr_next_pts().
[in,out] | s | allocated Swr context. If it is not initialized, or SWR_FLAG_RESAMPLE is not set, swr_init() is called with the flag set. |
[in] | sample_delta | delta in PTS per sample |
[in] | compensation_distance | number of samples to compensate for |
s
is NULL, compensation_distance
is less than 0, compensation_distance
is 0 but sample_delta is not, Definition at line 901 of file swresample.c.
Referenced by audio_decode_frame(), and swr_next_pts().
int swr_set_channel_mapping | ( | struct SwrContext * | s, |
const int * | channel_map | ||
) |
Set a customized input channel mapping.
[in,out] | s | allocated Swr context, not yet initialized |
[in] | channel_map | customized input channel mapping (array of channel indexes, -1 for a muted channel) |
Definition at line 52 of file swresample.c.
Referenced by config_props().
int swr_build_matrix | ( | uint64_t | in_layout, |
uint64_t | out_layout, | ||
double | center_mix_level, | ||
double | surround_mix_level, | ||
double | lfe_mix_level, | ||
double | rematrix_maxval, | ||
double | rematrix_volume, | ||
double * | matrix, | ||
int | stride, | ||
enum AVMatrixEncoding | matrix_encoding, | ||
void * | log_ctx | ||
) |
Generate a channel mixing matrix.
This function is the one used internally by libswresample for building the default mixing matrix. It is made public just as a utility function for building custom matrices.
in_layout | input channel layout | |
out_layout | output channel layout | |
center_mix_level | mix level for the center channel | |
surround_mix_level | mix level for the surround channel(s) | |
lfe_mix_level | mix level for the low-frequency effects channel | |
rematrix_maxval | if 1.0, coefficients will be normalized to prevent overflow. if INT_MAX, coefficients will not be normalized. | |
[out] | matrix | mixing coefficients; matrix[i + stride * o] is the weight of input channel i in output channel o. |
stride | distance between adjacent input channels in the matrix array | |
matrix_encoding | matrixed stereo downmix mode (e.g. dplii) | |
log_ctx | parent logging context, can be NULL |
Definition at line 119 of file rematrix.c.
Referenced by auto_matrix().
int swr_set_matrix | ( | struct SwrContext * | s, |
const double * | matrix, | ||
int | stride | ||
) |
Set a customized remix matrix.
s | allocated Swr context, not yet initialized |
matrix | remix coefficients; matrix[i + stride * o] is the weight of input channel i in output channel o |
stride | offset between lines of the matrix |
Definition at line 64 of file rematrix.c.
Referenced by config_props().
int swr_drop_output | ( | struct SwrContext * | s, |
int | count | ||
) |
Drops the specified number of output samples.
This function, along with swr_inject_silence(), is called by swr_next_pts() if needed for "hard" compensation.
s | allocated Swr context |
count | number of samples to be dropped |
Definition at line 832 of file swresample.c.
Referenced by swr_next_pts().
int swr_inject_silence | ( | struct SwrContext * | s, |
int | count | ||
) |
Injects the specified number of silence samples.
This function, along with swr_drop_output(), is called by swr_next_pts() if needed for "hard" compensation.
s | allocated Swr context |
count | number of samples to be dropped |
Definition at line 843 of file swresample.c.
Referenced by swr_inject_silence(), and swr_next_pts().
int64_t swr_get_delay | ( | struct SwrContext * | s, |
int64_t | base | ||
) |
Gets the delay the next input sample will experience relative to the next output sample.
Swresample can buffer data if more input has been provided than available output space, also converting between sample rates needs a delay. This function returns the sum of all such delays. The exact delay is not necessarily an integer value in either input or output sample rate. Especially when downsampling by a large value, the output sample rate may be a poor choice to represent the delay, similarly for upsampling and the input sample rate.
s | swr context |
base | timebase in which the returned delay will be:
|
base
units. Definition at line 871 of file swresample.c.
Referenced by filter_frame(), main(), swr_convert_frame(), swr_next_pts(), and write_audio_frame().
int swr_get_out_samples | ( | struct SwrContext * | s, |
int | in_samples | ||
) |
Find an upper bound on the number of samples that the next swr_convert call will output, if called with in_samples of input samples.
This depends on the internal state, and anything changing the internal state (like further swr_convert() calls) will may change the number of samples swr_get_out_samples() returns for the same number of input samples.
in_samples | number of input samples. |
Definition at line 879 of file swresample.c.
Referenced by swr_convert().
unsigned swresample_version | ( | void | ) |
Return the LIBSWRESAMPLE_VERSION_INT constant.
This is useful to check if the build-time libswresample has the same version as the run-time one.
Definition at line 35 of file swresample.c.
const char* swresample_configuration | ( | void | ) |
Return the swr build-time configuration.
./configure flags Definition at line 41 of file swresample.c.
const char* swresample_license | ( | void | ) |
Return the swr license.
Definition at line 46 of file swresample.c.
int swr_convert_frame | ( | SwrContext * | swr, |
AVFrame * | output, | ||
const AVFrame * | input | ||
) |
Convert the samples in the input AVFrame and write them to the output AVFrame.
Input and output AVFrames must have channel_layout, sample_rate and format set.
If the output AVFrame does not have the data pointers allocated the nb_samples field will be set using av_frame_get_buffer() is called to allocate the frame.
The output AVFrame can be NULL or have fewer allocated samples than required. In this case, any remaining samples not written to the output will be added to an internal FIFO buffer, to be returned at the next call to this function or to swr_convert().
If converting sample rate, there may be data remaining in the internal resampling delay buffer. swr_get_delay() tells the number of remaining samples. To get this data as output, call this function or swr_convert() with NULL input.
If the SwrContext configuration does not match the output and input AVFrame settings the conversion does not take place and depending on which AVFrame is not matching AVERROR_OUTPUT_CHANGED, AVERROR_INPUT_CHANGED or the result of a bitwise-OR of them is returned.
Definition at line 124 of file swresample_frame.c.
int swr_config_frame | ( | SwrContext * | swr, |
const AVFrame * | out, | ||
const AVFrame * | in | ||
) |
Configure or reconfigure the SwrContext using the information provided by the AVFrames.
The original resampling context is reset even on failure. The function calls swr_close() internally if the context is open.
Definition at line 27 of file swresample_frame.c.
Referenced by swr_convert_frame().