Path: blob/master/dep/ffmpeg/include/libswresample/swresample.h
4216 views
/*1* Copyright (C) 2011-2013 Michael Niedermayer ([email protected])2*3* This file is part of libswresample4*5* libswresample 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* libswresample 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 libswresample; if not, write to the Free Software17* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA18*/1920#ifndef SWRESAMPLE_SWRESAMPLE_H21#define SWRESAMPLE_SWRESAMPLE_H2223/**24* @file25* @ingroup lswr26* libswresample public header27*/2829/**30* @defgroup lswr libswresample31* @{32*33* Audio resampling, sample format conversion and mixing library.34*35* Interaction with lswr is done through SwrContext, which is36* allocated with swr_alloc() or swr_alloc_set_opts2(). It is opaque, so all parameters37* must be set with the @ref avoptions API.38*39* The first thing you will need to do in order to use lswr is to allocate40* SwrContext. This can be done with swr_alloc() or swr_alloc_set_opts2(). If you41* are using the former, you must set options through the @ref avoptions API.42* The latter function provides the same feature, but it allows you to set some43* common options in the same statement.44*45* For example the following code will setup conversion from planar float sample46* format to interleaved signed 16-bit integer, downsampling from 48kHz to47* 44.1kHz and downmixing from 5.1 channels to stereo (using the default mixing48* matrix). This is using the swr_alloc() function.49* @code50* SwrContext *swr = swr_alloc();51* av_opt_set_chlayout(swr, "in_chlayout", &(AVChannelLayout)AV_CHANNEL_LAYOUT_5POINT1, 0);52* av_opt_set_chlayout(swr, "out_chlayout", &(AVChannelLayout)AV_CHANNEL_LAYOUT_STEREO, 0);53* av_opt_set_int(swr, "in_sample_rate", 48000, 0);54* av_opt_set_int(swr, "out_sample_rate", 44100, 0);55* av_opt_set_sample_fmt(swr, "in_sample_fmt", AV_SAMPLE_FMT_FLTP, 0);56* av_opt_set_sample_fmt(swr, "out_sample_fmt", AV_SAMPLE_FMT_S16, 0);57* @endcode58*59* The same job can be done using swr_alloc_set_opts2() as well:60* @code61* SwrContext *swr = NULL;62* int ret = swr_alloc_set_opts2(&swr, // we're allocating a new context63* &(AVChannelLayout)AV_CHANNEL_LAYOUT_STEREO, // out_ch_layout64* AV_SAMPLE_FMT_S16, // out_sample_fmt65* 44100, // out_sample_rate66* &(AVChannelLayout)AV_CHANNEL_LAYOUT_5POINT1, // in_ch_layout67* AV_SAMPLE_FMT_FLTP, // in_sample_fmt68* 48000, // in_sample_rate69* 0, // log_offset70* NULL); // log_ctx71* @endcode72*73* Once all values have been set, it must be initialized with swr_init(). If74* you need to change the conversion parameters, you can change the parameters75* using @ref avoptions, as described above in the first example; or by using76* swr_alloc_set_opts2(), but with the first argument the allocated context.77* You must then call swr_init() again.78*79* The conversion itself is done by repeatedly calling swr_convert().80* Note that the samples may get buffered in swr if you provide insufficient81* output space or if sample rate conversion is done, which requires "future"82* samples. Samples that do not require future input can be retrieved at any83* time by using swr_convert() (in_count can be set to 0).84* At the end of conversion the resampling buffer can be flushed by calling85* swr_convert() with NULL in and 0 in_count.86*87* The samples used in the conversion process can be managed with the libavutil88* @ref lavu_sampmanip "samples manipulation" API, including av_samples_alloc()89* function used in the following example.90*91* The delay between input and output, can at any time be found by using92* swr_get_delay().93*94* The following code demonstrates the conversion loop assuming the parameters95* from above and caller-defined functions get_input() and handle_output():96* @code97* uint8_t **input;98* int in_samples;99*100* while (get_input(&input, &in_samples)) {101* uint8_t *output;102* int out_samples = av_rescale_rnd(swr_get_delay(swr, 48000) +103* in_samples, 44100, 48000, AV_ROUND_UP);104* av_samples_alloc(&output, NULL, 2, out_samples,105* AV_SAMPLE_FMT_S16, 0);106* out_samples = swr_convert(swr, &output, out_samples,107* input, in_samples);108* handle_output(output, out_samples);109* av_freep(&output);110* }111* @endcode112*113* When the conversion is finished, the conversion114* context and everything associated with it must be freed with swr_free().115* A swr_close() function is also available, but it exists mainly for116* compatibility with libavresample, and is not required to be called.117*118* There will be no memory leak if the data is not completely flushed before119* swr_free().120*/121122#include <stdint.h>123#include "libavutil/channel_layout.h"124#include "libavutil/frame.h"125#include "libavutil/samplefmt.h"126127#include "libswresample/version_major.h"128#ifndef HAVE_AV_CONFIG_H129/* When included as part of the ffmpeg build, only include the major version130* to avoid unnecessary rebuilds. When included externally, keep including131* the full version information. */132#include "libswresample/version.h"133#endif134135/**136* @name Option constants137* These constants are used for the @ref avoptions interface for lswr.138* @{139*140*/141142#define SWR_FLAG_RESAMPLE 1 ///< Force resampling even if equal sample rate143//TODO use int resample ?144//long term TODO can we enable this dynamically?145146/** Dithering algorithms */147enum SwrDitherType {148SWR_DITHER_NONE = 0,149SWR_DITHER_RECTANGULAR,150SWR_DITHER_TRIANGULAR,151SWR_DITHER_TRIANGULAR_HIGHPASS,152153SWR_DITHER_NS = 64, ///< not part of API/ABI154SWR_DITHER_NS_LIPSHITZ,155SWR_DITHER_NS_F_WEIGHTED,156SWR_DITHER_NS_MODIFIED_E_WEIGHTED,157SWR_DITHER_NS_IMPROVED_E_WEIGHTED,158SWR_DITHER_NS_SHIBATA,159SWR_DITHER_NS_LOW_SHIBATA,160SWR_DITHER_NS_HIGH_SHIBATA,161SWR_DITHER_NB, ///< not part of API/ABI162};163164/** Resampling Engines */165enum SwrEngine {166SWR_ENGINE_SWR, /**< SW Resampler */167SWR_ENGINE_SOXR, /**< SoX Resampler */168SWR_ENGINE_NB, ///< not part of API/ABI169};170171/** Resampling Filter Types */172enum SwrFilterType {173SWR_FILTER_TYPE_CUBIC, /**< Cubic */174SWR_FILTER_TYPE_BLACKMAN_NUTTALL, /**< Blackman Nuttall windowed sinc */175SWR_FILTER_TYPE_KAISER, /**< Kaiser windowed sinc */176};177178/**179* @}180*/181182/**183* The libswresample context. Unlike libavcodec and libavformat, this structure184* is opaque. This means that if you would like to set options, you must use185* the @ref avoptions API and cannot directly set values to members of the186* structure.187*/188typedef struct SwrContext SwrContext;189190/**191* Get the AVClass for SwrContext. It can be used in combination with192* AV_OPT_SEARCH_FAKE_OBJ for examining options.193*194* @see av_opt_find().195* @return the AVClass of SwrContext196*/197const AVClass *swr_get_class(void);198199/**200* @name SwrContext constructor functions201* @{202*/203204/**205* Allocate SwrContext.206*207* If you use this function you will need to set the parameters (manually or208* with swr_alloc_set_opts2()) before calling swr_init().209*210* @see swr_alloc_set_opts2(), swr_init(), swr_free()211* @return NULL on error, allocated context otherwise212*/213struct SwrContext *swr_alloc(void);214215/**216* Initialize context after user parameters have been set.217* @note The context must be configured using the AVOption API.218*219* @see av_opt_set_int()220* @see av_opt_set_dict()221*222* @param[in,out] s Swr context to initialize223* @return AVERROR error code in case of failure.224*/225int swr_init(struct SwrContext *s);226227/**228* Check whether an swr context has been initialized or not.229*230* @param[in] s Swr context to check231* @see swr_init()232* @return positive if it has been initialized, 0 if not initialized233*/234int swr_is_initialized(struct SwrContext *s);235236/**237* Allocate SwrContext if needed and set/reset common parameters.238*239* This function does not require *ps to be allocated with swr_alloc(). On the240* other hand, swr_alloc() can use swr_alloc_set_opts2() to set the parameters241* on the allocated context.242*243* @param ps Pointer to an existing Swr context if available, or to NULL if not.244* On success, *ps will be set to the allocated context.245* @param out_ch_layout output channel layout (e.g. AV_CHANNEL_LAYOUT_*)246* @param out_sample_fmt output sample format (AV_SAMPLE_FMT_*).247* @param out_sample_rate output sample rate (frequency in Hz)248* @param in_ch_layout input channel layout (e.g. AV_CHANNEL_LAYOUT_*)249* @param in_sample_fmt input sample format (AV_SAMPLE_FMT_*).250* @param in_sample_rate input sample rate (frequency in Hz)251* @param log_offset logging level offset252* @param log_ctx parent logging context, can be NULL253*254* @see swr_init(), swr_free()255* @return 0 on success, a negative AVERROR code on error.256* On error, the Swr context is freed and *ps set to NULL.257*/258int swr_alloc_set_opts2(struct SwrContext **ps,259const AVChannelLayout *out_ch_layout, enum AVSampleFormat out_sample_fmt, int out_sample_rate,260const AVChannelLayout *in_ch_layout, enum AVSampleFormat in_sample_fmt, int in_sample_rate,261int log_offset, void *log_ctx);262/**263* @}264*265* @name SwrContext destructor functions266* @{267*/268269/**270* Free the given SwrContext and set the pointer to NULL.271*272* @param[in] s a pointer to a pointer to Swr context273*/274void swr_free(struct SwrContext **s);275276/**277* Closes the context so that swr_is_initialized() returns 0.278*279* The context can be brought back to life by running swr_init(),280* swr_init() can also be used without swr_close().281* This function is mainly provided for simplifying the usecase282* where one tries to support libavresample and libswresample.283*284* @param[in,out] s Swr context to be closed285*/286void swr_close(struct SwrContext *s);287288/**289* @}290*291* @name Core conversion functions292* @{293*/294295/** Convert audio.296*297* in and in_count can be set to 0 to flush the last few samples out at the298* end.299*300* If more input is provided than output space, then the input will be buffered.301* You can avoid this buffering by using swr_get_out_samples() to retrieve an302* upper bound on the required number of output samples for the given number of303* input samples. Conversion will run directly without copying whenever possible.304*305* @param s allocated Swr context, with parameters set306* @param out output buffers, only the first one need be set in case of packed audio307* @param out_count amount of space available for output in samples per channel308* @param in input buffers, only the first one need to be set in case of packed audio309* @param in_count number of input samples available in one channel310*311* @return number of samples output per channel, negative value on error312*/313int swr_convert(struct SwrContext *s, uint8_t * const *out, int out_count,314const uint8_t * const *in , int in_count);315316/**317* Convert the next timestamp from input to output318* timestamps are in 1/(in_sample_rate * out_sample_rate) units.319*320* @note There are 2 slightly differently behaving modes.321* @li When automatic timestamp compensation is not used, (min_compensation >= FLT_MAX)322* in this case timestamps will be passed through with delays compensated323* @li When automatic timestamp compensation is used, (min_compensation < FLT_MAX)324* in this case the output timestamps will match output sample numbers.325* See ffmpeg-resampler(1) for the two modes of compensation.326*327* @param[in] s initialized Swr context328* @param[in] pts timestamp for the next input sample, INT64_MIN if unknown329* @see swr_set_compensation(), swr_drop_output(), and swr_inject_silence() are330* function used internally for timestamp compensation.331* @return the output timestamp for the next output sample332*/333int64_t swr_next_pts(struct SwrContext *s, int64_t pts);334335/**336* @}337*338* @name Low-level option setting functions339* These functons provide a means to set low-level options that is not possible340* with the AVOption API.341* @{342*/343344/**345* Activate resampling compensation ("soft" compensation). This function is346* internally called when needed in swr_next_pts().347*348* @param[in,out] s allocated Swr context. If it is not initialized,349* or SWR_FLAG_RESAMPLE is not set, swr_init() is350* called with the flag set.351* @param[in] sample_delta delta in PTS per sample352* @param[in] compensation_distance number of samples to compensate for353* @return >= 0 on success, AVERROR error codes if:354* @li @c s is NULL,355* @li @c compensation_distance is less than 0,356* @li @c compensation_distance is 0 but sample_delta is not,357* @li compensation unsupported by resampler, or358* @li swr_init() fails when called.359*/360int swr_set_compensation(struct SwrContext *s, int sample_delta, int compensation_distance);361362/**363* Set a customized input channel mapping.364*365* @param[in,out] s allocated Swr context, not yet initialized366* @param[in] channel_map customized input channel mapping (array of channel367* indexes, -1 for a muted channel)368* @return >= 0 on success, or AVERROR error code in case of failure.369*/370int swr_set_channel_mapping(struct SwrContext *s, const int *channel_map);371372/**373* Generate a channel mixing matrix.374*375* This function is the one used internally by libswresample for building the376* default mixing matrix. It is made public just as a utility function for377* building custom matrices.378*379* @param in_layout input channel layout380* @param out_layout output channel layout381* @param center_mix_level mix level for the center channel382* @param surround_mix_level mix level for the surround channel(s)383* @param lfe_mix_level mix level for the low-frequency effects channel384* @param rematrix_maxval if 1.0, coefficients will be normalized to prevent385* overflow. if INT_MAX, coefficients will not be386* normalized.387* @param[out] matrix mixing coefficients; matrix[i + stride * o] is388* the weight of input channel i in output channel o.389* @param stride distance between adjacent input channels in the390* matrix array391* @param matrix_encoding matrixed stereo downmix mode (e.g. dplii)392* @param log_ctx parent logging context, can be NULL393* @return 0 on success, negative AVERROR code on failure394*/395int swr_build_matrix2(const AVChannelLayout *in_layout, const AVChannelLayout *out_layout,396double center_mix_level, double surround_mix_level,397double lfe_mix_level, double maxval,398double rematrix_volume, double *matrix,399ptrdiff_t stride, enum AVMatrixEncoding matrix_encoding,400void *log_context);401402/**403* Set a customized remix matrix.404*405* @param s allocated Swr context, not yet initialized406* @param matrix remix coefficients; matrix[i + stride * o] is407* the weight of input channel i in output channel o408* @param stride offset between lines of the matrix409* @return >= 0 on success, or AVERROR error code in case of failure.410*/411int swr_set_matrix(struct SwrContext *s, const double *matrix, int stride);412413/**414* @}415*416* @name Sample handling functions417* @{418*/419420/**421* Drops the specified number of output samples.422*423* This function, along with swr_inject_silence(), is called by swr_next_pts()424* if needed for "hard" compensation.425*426* @param s allocated Swr context427* @param count number of samples to be dropped428*429* @return >= 0 on success, or a negative AVERROR code on failure430*/431int swr_drop_output(struct SwrContext *s, int count);432433/**434* Injects the specified number of silence samples.435*436* This function, along with swr_drop_output(), is called by swr_next_pts()437* if needed for "hard" compensation.438*439* @param s allocated Swr context440* @param count number of samples to be dropped441*442* @return >= 0 on success, or a negative AVERROR code on failure443*/444int swr_inject_silence(struct SwrContext *s, int count);445446/**447* Gets the delay the next input sample will experience relative to the next output sample.448*449* Swresample can buffer data if more input has been provided than available450* output space, also converting between sample rates needs a delay.451* This function returns the sum of all such delays.452* The exact delay is not necessarily an integer value in either input or453* output sample rate. Especially when downsampling by a large value, the454* output sample rate may be a poor choice to represent the delay, similarly455* for upsampling and the input sample rate.456*457* @param s swr context458* @param base timebase in which the returned delay will be:459* @li if it's set to 1 the returned delay is in seconds460* @li if it's set to 1000 the returned delay is in milliseconds461* @li if it's set to the input sample rate then the returned462* delay is in input samples463* @li if it's set to the output sample rate then the returned464* delay is in output samples465* @li if it's the least common multiple of in_sample_rate and466* out_sample_rate then an exact rounding-free delay will be467* returned468* @returns the delay in 1 / @c base units.469*/470int64_t swr_get_delay(struct SwrContext *s, int64_t base);471472/**473* Find an upper bound on the number of samples that the next swr_convert474* call will output, if called with in_samples of input samples. This475* depends on the internal state, and anything changing the internal state476* (like further swr_convert() calls) will may change the number of samples477* swr_get_out_samples() returns for the same number of input samples.478*479* @param in_samples number of input samples.480* @note any call to swr_inject_silence(), swr_convert(), swr_next_pts()481* or swr_set_compensation() invalidates this limit482* @note it is recommended to pass the correct available buffer size483* to all functions like swr_convert() even if swr_get_out_samples()484* indicates that less would be used.485* @returns an upper bound on the number of samples that the next swr_convert486* will output or a negative value to indicate an error487*/488int swr_get_out_samples(struct SwrContext *s, int in_samples);489490/**491* @}492*493* @name Configuration accessors494* @{495*/496497/**498* Return the @ref LIBSWRESAMPLE_VERSION_INT constant.499*500* This is useful to check if the build-time libswresample has the same version501* as the run-time one.502*503* @returns the unsigned int-typed version504*/505unsigned swresample_version(void);506507/**508* Return the swr build-time configuration.509*510* @returns the build-time @c ./configure flags511*/512const char *swresample_configuration(void);513514/**515* Return the swr license.516*517* @returns the license of libswresample, determined at build-time518*/519const char *swresample_license(void);520521/**522* @}523*524* @name AVFrame based API525* @{526*/527528/**529* Convert the samples in the input AVFrame and write them to the output AVFrame.530*531* Input and output AVFrames must have channel_layout, sample_rate and format set.532*533* If the output AVFrame does not have the data pointers allocated the nb_samples534* field will be set using av_frame_get_buffer()535* is called to allocate the frame.536*537* The output AVFrame can be NULL or have fewer allocated samples than required.538* In this case, any remaining samples not written to the output will be added539* to an internal FIFO buffer, to be returned at the next call to this function540* or to swr_convert().541*542* If converting sample rate, there may be data remaining in the internal543* resampling delay buffer. swr_get_delay() tells the number of544* remaining samples. To get this data as output, call this function or545* swr_convert() with NULL input.546*547* If the SwrContext configuration does not match the output and548* input AVFrame settings the conversion does not take place and depending on549* which AVFrame is not matching AVERROR_OUTPUT_CHANGED, AVERROR_INPUT_CHANGED550* or the result of a bitwise-OR of them is returned.551*552* @see swr_delay()553* @see swr_convert()554* @see swr_get_delay()555*556* @param swr audio resample context557* @param output output AVFrame558* @param input input AVFrame559* @return 0 on success, AVERROR on failure or nonmatching560* configuration.561*/562int swr_convert_frame(SwrContext *swr,563AVFrame *output, const AVFrame *input);564565/**566* Configure or reconfigure the SwrContext using the information567* provided by the AVFrames.568*569* The original resampling context is reset even on failure.570* The function calls swr_close() internally if the context is open.571*572* @see swr_close();573*574* @param swr audio resample context575* @param out output AVFrame576* @param in input AVFrame577* @return 0 on success, AVERROR on failure.578*/579int swr_config_frame(SwrContext *swr, const AVFrame *out, const AVFrame *in);580581/**582* @}583* @}584*/585586#endif /* SWRESAMPLE_SWRESAMPLE_H */587588589