/*1* Copyright (c) 2012 Justin Ruggles <[email protected]>2*3* This file is part of FFmpeg.4*5* FFmpeg is free software; you can redistribute it and/or6* modify it under the terms of the GNU Lesser General Public7* License as published by the Free Software Foundation; either8* version 2.1 of the License, or (at your option) any later version.9*10* FFmpeg is distributed in the hope that it will be useful,11* but WITHOUT ANY WARRANTY; without even the implied warranty of12* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU13* Lesser General Public License for more details.14*15* You should have received a copy of the GNU Lesser General Public16* License along with FFmpeg; if not, write to the Free Software17* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA18*/1920#ifndef AVRESAMPLE_AVRESAMPLE_H21#define AVRESAMPLE_AVRESAMPLE_H2223/**24* @file25* @ingroup lavr26* external API header27*/2829/**30* @defgroup lavr Libavresample31* @{32*33* Libavresample (lavr) is a library that handles audio resampling, sample34* format conversion and mixing.35*36* Interaction with lavr is done through AVAudioResampleContext, which is37* allocated with avresample_alloc_context(). It is opaque, so all parameters38* must be set with the @ref avoptions API.39*40* For example the following code will setup conversion from planar float sample41* format to interleaved signed 16-bit integer, downsampling from 48kHz to42* 44.1kHz and downmixing from 5.1 channels to stereo (using the default mixing43* matrix):44* @code45* AVAudioResampleContext *avr = avresample_alloc_context();46* av_opt_set_int(avr, "in_channel_layout", AV_CH_LAYOUT_5POINT1, 0);47* av_opt_set_int(avr, "out_channel_layout", AV_CH_LAYOUT_STEREO, 0);48* av_opt_set_int(avr, "in_sample_rate", 48000, 0);49* av_opt_set_int(avr, "out_sample_rate", 44100, 0);50* av_opt_set_int(avr, "in_sample_fmt", AV_SAMPLE_FMT_FLTP, 0);51* av_opt_set_int(avr, "out_sample_fmt", AV_SAMPLE_FMT_S16, 0);52* @endcode53*54* Once the context is initialized, it must be opened with avresample_open(). If55* you need to change the conversion parameters, you must close the context with56* avresample_close(), change the parameters as described above, then reopen it57* again.58*59* The conversion itself is done by repeatedly calling avresample_convert().60* Note that the samples may get buffered in two places in lavr. The first one61* is the output FIFO, where the samples end up if the output buffer is not62* large enough. The data stored in there may be retrieved at any time with63* avresample_read(). The second place is the resampling delay buffer,64* applicable only when resampling is done. The samples in it require more input65* before they can be processed. Their current amount is returned by66* avresample_get_delay(). At the end of conversion the resampling buffer can be67* flushed by calling avresample_convert() with NULL input.68*69* The following code demonstrates the conversion loop assuming the parameters70* from above and caller-defined functions get_input() and handle_output():71* @code72* uint8_t **input;73* int in_linesize, in_samples;74*75* while (get_input(&input, &in_linesize, &in_samples)) {76* uint8_t *output77* int out_linesize;78* int out_samples = avresample_get_out_samples(avr, in_samples);79*80* av_samples_alloc(&output, &out_linesize, 2, out_samples,81* AV_SAMPLE_FMT_S16, 0);82* out_samples = avresample_convert(avr, &output, out_linesize, out_samples,83* input, in_linesize, in_samples);84* handle_output(output, out_linesize, out_samples);85* av_freep(&output);86* }87* @endcode88*89* When the conversion is finished and the FIFOs are flushed if required, the90* conversion context and everything associated with it must be freed with91* avresample_free().92*/9394#include "libavutil/avutil.h"95#include "libavutil/channel_layout.h"96#include "libavutil/dict.h"97#include "libavutil/frame.h"98#include "libavutil/log.h"99#include "libavutil/mathematics.h"100101#include "libavresample/version.h"102103#define AVRESAMPLE_MAX_CHANNELS 32104105typedef struct AVAudioResampleContext AVAudioResampleContext;106107/** Mixing Coefficient Types */108enum AVMixCoeffType {109AV_MIX_COEFF_TYPE_Q8, /** 16-bit 8.8 fixed-point */110AV_MIX_COEFF_TYPE_Q15, /** 32-bit 17.15 fixed-point */111AV_MIX_COEFF_TYPE_FLT, /** floating-point */112AV_MIX_COEFF_TYPE_NB, /** Number of coeff types. Not part of ABI */113};114115/** Resampling Filter Types */116enum AVResampleFilterType {117AV_RESAMPLE_FILTER_TYPE_CUBIC, /**< Cubic */118AV_RESAMPLE_FILTER_TYPE_BLACKMAN_NUTTALL, /**< Blackman Nuttall Windowed Sinc */119AV_RESAMPLE_FILTER_TYPE_KAISER, /**< Kaiser Windowed Sinc */120};121122enum AVResampleDitherMethod {123AV_RESAMPLE_DITHER_NONE, /**< Do not use dithering */124AV_RESAMPLE_DITHER_RECTANGULAR, /**< Rectangular Dither */125AV_RESAMPLE_DITHER_TRIANGULAR, /**< Triangular Dither*/126AV_RESAMPLE_DITHER_TRIANGULAR_HP, /**< Triangular Dither with High Pass */127AV_RESAMPLE_DITHER_TRIANGULAR_NS, /**< Triangular Dither with Noise Shaping */128AV_RESAMPLE_DITHER_NB, /**< Number of dither types. Not part of ABI. */129};130131/**132* Return the LIBAVRESAMPLE_VERSION_INT constant.133*/134unsigned avresample_version(void);135136/**137* Return the libavresample build-time configuration.138* @return configure string139*/140const char *avresample_configuration(void);141142/**143* Return the libavresample license.144*/145const char *avresample_license(void);146147/**148* Get the AVClass for AVAudioResampleContext.149*150* Can be used in combination with AV_OPT_SEARCH_FAKE_OBJ for examining options151* without allocating a context.152*153* @see av_opt_find().154*155* @return AVClass for AVAudioResampleContext156*/157const AVClass *avresample_get_class(void);158159/**160* Allocate AVAudioResampleContext and set options.161*162* @return allocated audio resample context, or NULL on failure163*/164AVAudioResampleContext *avresample_alloc_context(void);165166/**167* Initialize AVAudioResampleContext.168* @note The context must be configured using the AVOption API.169* @note The fields "in_channel_layout", "out_channel_layout",170* "in_sample_rate", "out_sample_rate", "in_sample_fmt",171* "out_sample_fmt" must be set.172*173* @see av_opt_set_int()174* @see av_opt_set_dict()175* @see av_get_default_channel_layout()176*177* @param avr audio resample context178* @return 0 on success, negative AVERROR code on failure179*/180int avresample_open(AVAudioResampleContext *avr);181182/**183* Check whether an AVAudioResampleContext is open or closed.184*185* @param avr AVAudioResampleContext to check186* @return 1 if avr is open, 0 if avr is closed.187*/188int avresample_is_open(AVAudioResampleContext *avr);189190/**191* Close AVAudioResampleContext.192*193* This closes the context, but it does not change the parameters. The context194* can be reopened with avresample_open(). It does, however, clear the output195* FIFO and any remaining leftover samples in the resampling delay buffer. If196* there was a custom matrix being used, that is also cleared.197*198* @see avresample_convert()199* @see avresample_set_matrix()200*201* @param avr audio resample context202*/203void avresample_close(AVAudioResampleContext *avr);204205/**206* Free AVAudioResampleContext and associated AVOption values.207*208* This also calls avresample_close() before freeing.209*210* @param avr audio resample context211*/212void avresample_free(AVAudioResampleContext **avr);213214/**215* Generate a channel mixing matrix.216*217* This function is the one used internally by libavresample for building the218* default mixing matrix. It is made public just as a utility function for219* building custom matrices.220*221* @param in_layout input channel layout222* @param out_layout output channel layout223* @param center_mix_level mix level for the center channel224* @param surround_mix_level mix level for the surround channel(s)225* @param lfe_mix_level mix level for the low-frequency effects channel226* @param normalize if 1, coefficients will be normalized to prevent227* overflow. if 0, coefficients will not be228* normalized.229* @param[out] matrix mixing coefficients; matrix[i + stride * o] is230* the weight of input channel i in output channel o.231* @param stride distance between adjacent input channels in the232* matrix array233* @param matrix_encoding matrixed stereo downmix mode (e.g. dplii)234* @return 0 on success, negative AVERROR code on failure235*/236int avresample_build_matrix(uint64_t in_layout, uint64_t out_layout,237double center_mix_level, double surround_mix_level,238double lfe_mix_level, int normalize, double *matrix,239int stride, enum AVMatrixEncoding matrix_encoding);240241/**242* Get the current channel mixing matrix.243*244* If no custom matrix has been previously set or the AVAudioResampleContext is245* not open, an error is returned.246*247* @param avr audio resample context248* @param matrix mixing coefficients; matrix[i + stride * o] is the weight of249* input channel i in output channel o.250* @param stride distance between adjacent input channels in the matrix array251* @return 0 on success, negative AVERROR code on failure252*/253int avresample_get_matrix(AVAudioResampleContext *avr, double *matrix,254int stride);255256/**257* Set channel mixing matrix.258*259* Allows for setting a custom mixing matrix, overriding the default matrix260* generated internally during avresample_open(). This function can be called261* anytime on an allocated context, either before or after calling262* avresample_open(), as long as the channel layouts have been set.263* avresample_convert() always uses the current matrix.264* Calling avresample_close() on the context will clear the current matrix.265*266* @see avresample_close()267*268* @param avr audio resample context269* @param matrix mixing coefficients; matrix[i + stride * o] is the weight of270* input channel i in output channel o.271* @param stride distance between adjacent input channels in the matrix array272* @return 0 on success, negative AVERROR code on failure273*/274int avresample_set_matrix(AVAudioResampleContext *avr, const double *matrix,275int stride);276277/**278* Set a customized input channel mapping.279*280* This function can only be called when the allocated context is not open.281* Also, the input channel layout must have already been set.282*283* Calling avresample_close() on the context will clear the channel mapping.284*285* The map for each input channel specifies the channel index in the source to286* use for that particular channel, or -1 to mute the channel. Source channels287* can be duplicated by using the same index for multiple input channels.288*289* Examples:290*291* Reordering 5.1 AAC order (C,L,R,Ls,Rs,LFE) to FFmpeg order (L,R,C,LFE,Ls,Rs):292* { 1, 2, 0, 5, 3, 4 }293*294* Muting the 3rd channel in 4-channel input:295* { 0, 1, -1, 3 }296*297* Duplicating the left channel of stereo input:298* { 0, 0 }299*300* @param avr audio resample context301* @param channel_map customized input channel mapping302* @return 0 on success, negative AVERROR code on failure303*/304int avresample_set_channel_mapping(AVAudioResampleContext *avr,305const int *channel_map);306307/**308* Set compensation for resampling.309*310* This can be called anytime after avresample_open(). If resampling is not311* automatically enabled because of a sample rate conversion, the312* "force_resampling" option must have been set to 1 when opening the context313* in order to use resampling compensation.314*315* @param avr audio resample context316* @param sample_delta compensation delta, in samples317* @param compensation_distance compensation distance, in samples318* @return 0 on success, negative AVERROR code on failure319*/320int avresample_set_compensation(AVAudioResampleContext *avr, int sample_delta,321int compensation_distance);322323/**324* Provide the upper bound on the number of samples the configured325* conversion would output.326*327* @param avr audio resample context328* @param in_nb_samples number of input samples329*330* @return number of samples or AVERROR(EINVAL) if the value331* would exceed INT_MAX332*/333334int avresample_get_out_samples(AVAudioResampleContext *avr, int in_nb_samples);335336/**337* Convert input samples and write them to the output FIFO.338*339* The upper bound on the number of output samples can be obtained through340* avresample_get_out_samples().341*342* The output data can be NULL or have fewer allocated samples than required.343* In this case, any remaining samples not written to the output will be added344* to an internal FIFO buffer, to be returned at the next call to this function345* or to avresample_read().346*347* If converting sample rate, there may be data remaining in the internal348* resampling delay buffer. avresample_get_delay() tells the number of remaining349* samples. To get this data as output, call avresample_convert() with NULL350* input.351*352* At the end of the conversion process, there may be data remaining in the353* internal FIFO buffer. avresample_available() tells the number of remaining354* samples. To get this data as output, either call avresample_convert() with355* NULL input or call avresample_read().356*357* @see avresample_get_out_samples()358* @see avresample_read()359* @see avresample_get_delay()360*361* @param avr audio resample context362* @param output output data pointers363* @param out_plane_size output plane size, in bytes.364* This can be 0 if unknown, but that will lead to365* optimized functions not being used directly on the366* output, which could slow down some conversions.367* @param out_samples maximum number of samples that the output buffer can hold368* @param input input data pointers369* @param in_plane_size input plane size, in bytes370* This can be 0 if unknown, but that will lead to371* optimized functions not being used directly on the372* input, which could slow down some conversions.373* @param in_samples number of input samples to convert374* @return number of samples written to the output buffer,375* not including converted samples added to the internal376* output FIFO377*/378int avresample_convert(AVAudioResampleContext *avr, uint8_t **output,379int out_plane_size, int out_samples,380uint8_t * const *input, int in_plane_size,381int in_samples);382383/**384* Return the number of samples currently in the resampling delay buffer.385*386* When resampling, there may be a delay between the input and output. Any387* unconverted samples in each call are stored internally in a delay buffer.388* This function allows the user to determine the current number of samples in389* the delay buffer, which can be useful for synchronization.390*391* @see avresample_convert()392*393* @param avr audio resample context394* @return number of samples currently in the resampling delay buffer395*/396int avresample_get_delay(AVAudioResampleContext *avr);397398/**399* Return the number of available samples in the output FIFO.400*401* During conversion, if the user does not specify an output buffer or402* specifies an output buffer that is smaller than what is needed, remaining403* samples that are not written to the output are stored to an internal FIFO404* buffer. The samples in the FIFO can be read with avresample_read() or405* avresample_convert().406*407* @see avresample_read()408* @see avresample_convert()409*410* @param avr audio resample context411* @return number of samples available for reading412*/413int avresample_available(AVAudioResampleContext *avr);414415/**416* Read samples from the output FIFO.417*418* During conversion, if the user does not specify an output buffer or419* specifies an output buffer that is smaller than what is needed, remaining420* samples that are not written to the output are stored to an internal FIFO421* buffer. This function can be used to read samples from that internal FIFO.422*423* @see avresample_available()424* @see avresample_convert()425*426* @param avr audio resample context427* @param output output data pointers. May be NULL, in which case428* nb_samples of data is discarded from output FIFO.429* @param nb_samples number of samples to read from the FIFO430* @return the number of samples written to output431*/432int avresample_read(AVAudioResampleContext *avr, uint8_t **output, int nb_samples);433434/**435* Convert the samples in the input AVFrame and write them to the output AVFrame.436*437* Input and output AVFrames must have channel_layout, sample_rate and format set.438*439* The upper bound on the number of output samples is obtained through440* avresample_get_out_samples().441*442* If the output AVFrame does not have the data pointers allocated the nb_samples443* field will be set using avresample_get_out_samples() and av_frame_get_buffer()444* is called to allocate the frame.445*446* The output AVFrame can be NULL or have fewer allocated samples than required.447* In this case, any remaining samples not written to the output will be added448* to an internal FIFO buffer, to be returned at the next call to this function449* or to avresample_convert() or to avresample_read().450*451* If converting sample rate, there may be data remaining in the internal452* resampling delay buffer. avresample_get_delay() tells the number of453* remaining samples. To get this data as output, call this function or454* avresample_convert() with NULL input.455*456* At the end of the conversion process, there may be data remaining in the457* internal FIFO buffer. avresample_available() tells the number of remaining458* samples. To get this data as output, either call this function or459* avresample_convert() with NULL input or call avresample_read().460*461* If the AVAudioResampleContext configuration does not match the output and462* input AVFrame settings the conversion does not take place and depending on463* which AVFrame is not matching AVERROR_OUTPUT_CHANGED, AVERROR_INPUT_CHANGED464* or AVERROR_OUTPUT_CHANGED|AVERROR_INPUT_CHANGED is returned.465*466* @see avresample_get_out_samples()467* @see avresample_available()468* @see avresample_convert()469* @see avresample_read()470* @see avresample_get_delay()471*472* @param avr audio resample context473* @param output output AVFrame474* @param input input AVFrame475* @return 0 on success, AVERROR on failure or nonmatching476* configuration.477*/478int avresample_convert_frame(AVAudioResampleContext *avr,479AVFrame *output, AVFrame *input);480481/**482* Configure or reconfigure the AVAudioResampleContext using the information483* provided by the AVFrames.484*485* The original resampling context is reset even on failure.486* The function calls avresample_close() internally if the context is open.487*488* @see avresample_open();489* @see avresample_close();490*491* @param avr audio resample context492* @param output output AVFrame493* @param input input AVFrame494* @return 0 on success, AVERROR on failure.495*/496int avresample_config(AVAudioResampleContext *avr, AVFrame *out, AVFrame *in);497498/**499* @}500*/501502#endif /* AVRESAMPLE_AVRESAMPLE_H */503504505