Path: blob/a-new-beginning/libswresample.xcframework/macos-arm64/libswresample.framework/Versions/A/Headers/swresample.h
2 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_channel_layout(swr, "in_channel_layout", AV_CH_LAYOUT_5POINT1, 0);52* av_opt_set_channel_layout(swr, "out_channel_layout", AV_CH_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#if FF_API_OLD_CHANNEL_LAYOUT237/**238* Allocate SwrContext if needed and set/reset common parameters.239*240* This function does not require s to be allocated with swr_alloc(). On the241* other hand, swr_alloc() can use swr_alloc_set_opts() to set the parameters242* on the allocated context.243*244* @param s existing Swr context if available, or NULL if not245* @param out_ch_layout output channel layout (AV_CH_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 (AV_CH_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 NULL on error, allocated context otherwise256* @deprecated use @ref swr_alloc_set_opts2()257*/258attribute_deprecated259struct SwrContext *swr_alloc_set_opts(struct SwrContext *s,260int64_t out_ch_layout, enum AVSampleFormat out_sample_fmt, int out_sample_rate,261int64_t in_ch_layout, enum AVSampleFormat in_sample_fmt, int in_sample_rate,262int log_offset, void *log_ctx);263#endif264265/**266* Allocate SwrContext if needed and set/reset common parameters.267*268* This function does not require *ps to be allocated with swr_alloc(). On the269* other hand, swr_alloc() can use swr_alloc_set_opts2() to set the parameters270* on the allocated context.271*272* @param ps Pointer to an existing Swr context if available, or to NULL if not.273* On success, *ps will be set to the allocated context.274* @param out_ch_layout output channel layout (e.g. AV_CHANNEL_LAYOUT_*)275* @param out_sample_fmt output sample format (AV_SAMPLE_FMT_*).276* @param out_sample_rate output sample rate (frequency in Hz)277* @param in_ch_layout input channel layout (e.g. AV_CHANNEL_LAYOUT_*)278* @param in_sample_fmt input sample format (AV_SAMPLE_FMT_*).279* @param in_sample_rate input sample rate (frequency in Hz)280* @param log_offset logging level offset281* @param log_ctx parent logging context, can be NULL282*283* @see swr_init(), swr_free()284* @return 0 on success, a negative AVERROR code on error.285* On error, the Swr context is freed and *ps set to NULL.286*/287int swr_alloc_set_opts2(struct SwrContext **ps,288const AVChannelLayout *out_ch_layout, enum AVSampleFormat out_sample_fmt, int out_sample_rate,289const AVChannelLayout *in_ch_layout, enum AVSampleFormat in_sample_fmt, int in_sample_rate,290int log_offset, void *log_ctx);291/**292* @}293*294* @name SwrContext destructor functions295* @{296*/297298/**299* Free the given SwrContext and set the pointer to NULL.300*301* @param[in] s a pointer to a pointer to Swr context302*/303void swr_free(struct SwrContext **s);304305/**306* Closes the context so that swr_is_initialized() returns 0.307*308* The context can be brought back to life by running swr_init(),309* swr_init() can also be used without swr_close().310* This function is mainly provided for simplifying the usecase311* where one tries to support libavresample and libswresample.312*313* @param[in,out] s Swr context to be closed314*/315void swr_close(struct SwrContext *s);316317/**318* @}319*320* @name Core conversion functions321* @{322*/323324/** Convert audio.325*326* in and in_count can be set to 0 to flush the last few samples out at the327* end.328*329* If more input is provided than output space, then the input will be buffered.330* You can avoid this buffering by using swr_get_out_samples() to retrieve an331* upper bound on the required number of output samples for the given number of332* input samples. Conversion will run directly without copying whenever possible.333*334* @param s allocated Swr context, with parameters set335* @param out output buffers, only the first one need be set in case of packed audio336* @param out_count amount of space available for output in samples per channel337* @param in input buffers, only the first one need to be set in case of packed audio338* @param in_count number of input samples available in one channel339*340* @return number of samples output per channel, negative value on error341*/342int swr_convert(struct SwrContext *s, uint8_t **out, int out_count,343const uint8_t **in , int in_count);344345/**346* Convert the next timestamp from input to output347* timestamps are in 1/(in_sample_rate * out_sample_rate) units.348*349* @note There are 2 slightly differently behaving modes.350* @li When automatic timestamp compensation is not used, (min_compensation >= FLT_MAX)351* in this case timestamps will be passed through with delays compensated352* @li When automatic timestamp compensation is used, (min_compensation < FLT_MAX)353* in this case the output timestamps will match output sample numbers.354* See ffmpeg-resampler(1) for the two modes of compensation.355*356* @param[in] s initialized Swr context357* @param[in] pts timestamp for the next input sample, INT64_MIN if unknown358* @see swr_set_compensation(), swr_drop_output(), and swr_inject_silence() are359* function used internally for timestamp compensation.360* @return the output timestamp for the next output sample361*/362int64_t swr_next_pts(struct SwrContext *s, int64_t pts);363364/**365* @}366*367* @name Low-level option setting functions368* These functons provide a means to set low-level options that is not possible369* with the AVOption API.370* @{371*/372373/**374* Activate resampling compensation ("soft" compensation). This function is375* internally called when needed in swr_next_pts().376*377* @param[in,out] s allocated Swr context. If it is not initialized,378* or SWR_FLAG_RESAMPLE is not set, swr_init() is379* called with the flag set.380* @param[in] sample_delta delta in PTS per sample381* @param[in] compensation_distance number of samples to compensate for382* @return >= 0 on success, AVERROR error codes if:383* @li @c s is NULL,384* @li @c compensation_distance is less than 0,385* @li @c compensation_distance is 0 but sample_delta is not,386* @li compensation unsupported by resampler, or387* @li swr_init() fails when called.388*/389int swr_set_compensation(struct SwrContext *s, int sample_delta, int compensation_distance);390391/**392* Set a customized input channel mapping.393*394* @param[in,out] s allocated Swr context, not yet initialized395* @param[in] channel_map customized input channel mapping (array of channel396* indexes, -1 for a muted channel)397* @return >= 0 on success, or AVERROR error code in case of failure.398*/399int swr_set_channel_mapping(struct SwrContext *s, const int *channel_map);400401#if FF_API_OLD_CHANNEL_LAYOUT402/**403* Generate a channel mixing matrix.404*405* This function is the one used internally by libswresample for building the406* default mixing matrix. It is made public just as a utility function for407* building custom matrices.408*409* @param in_layout input channel layout410* @param out_layout output channel layout411* @param center_mix_level mix level for the center channel412* @param surround_mix_level mix level for the surround channel(s)413* @param lfe_mix_level mix level for the low-frequency effects channel414* @param rematrix_maxval if 1.0, coefficients will be normalized to prevent415* overflow. if INT_MAX, coefficients will not be416* normalized.417* @param[out] matrix mixing coefficients; matrix[i + stride * o] is418* the weight of input channel i in output channel o.419* @param stride distance between adjacent input channels in the420* matrix array421* @param matrix_encoding matrixed stereo downmix mode (e.g. dplii)422* @param log_ctx parent logging context, can be NULL423* @return 0 on success, negative AVERROR code on failure424* @deprecated use @ref swr_build_matrix2()425*/426attribute_deprecated427int swr_build_matrix(uint64_t in_layout, uint64_t out_layout,428double center_mix_level, double surround_mix_level,429double lfe_mix_level, double rematrix_maxval,430double rematrix_volume, double *matrix,431int stride, enum AVMatrixEncoding matrix_encoding,432void *log_ctx);433#endif434435/**436* Generate a channel mixing matrix.437*438* This function is the one used internally by libswresample for building the439* default mixing matrix. It is made public just as a utility function for440* building custom matrices.441*442* @param in_layout input channel layout443* @param out_layout output channel layout444* @param center_mix_level mix level for the center channel445* @param surround_mix_level mix level for the surround channel(s)446* @param lfe_mix_level mix level for the low-frequency effects channel447* @param rematrix_maxval if 1.0, coefficients will be normalized to prevent448* overflow. if INT_MAX, coefficients will not be449* normalized.450* @param[out] matrix mixing coefficients; matrix[i + stride * o] is451* the weight of input channel i in output channel o.452* @param stride distance between adjacent input channels in the453* matrix array454* @param matrix_encoding matrixed stereo downmix mode (e.g. dplii)455* @param log_ctx parent logging context, can be NULL456* @return 0 on success, negative AVERROR code on failure457*/458int swr_build_matrix2(const AVChannelLayout *in_layout, const AVChannelLayout *out_layout,459double center_mix_level, double surround_mix_level,460double lfe_mix_level, double maxval,461double rematrix_volume, double *matrix,462ptrdiff_t stride, enum AVMatrixEncoding matrix_encoding,463void *log_context);464465/**466* Set a customized remix matrix.467*468* @param s allocated Swr context, not yet initialized469* @param matrix remix coefficients; matrix[i + stride * o] is470* the weight of input channel i in output channel o471* @param stride offset between lines of the matrix472* @return >= 0 on success, or AVERROR error code in case of failure.473*/474int swr_set_matrix(struct SwrContext *s, const double *matrix, int stride);475476/**477* @}478*479* @name Sample handling functions480* @{481*/482483/**484* Drops the specified number of output samples.485*486* This function, along with swr_inject_silence(), is called by swr_next_pts()487* if needed for "hard" compensation.488*489* @param s allocated Swr context490* @param count number of samples to be dropped491*492* @return >= 0 on success, or a negative AVERROR code on failure493*/494int swr_drop_output(struct SwrContext *s, int count);495496/**497* Injects the specified number of silence samples.498*499* This function, along with swr_drop_output(), is called by swr_next_pts()500* if needed for "hard" compensation.501*502* @param s allocated Swr context503* @param count number of samples to be dropped504*505* @return >= 0 on success, or a negative AVERROR code on failure506*/507int swr_inject_silence(struct SwrContext *s, int count);508509/**510* Gets the delay the next input sample will experience relative to the next output sample.511*512* Swresample can buffer data if more input has been provided than available513* output space, also converting between sample rates needs a delay.514* This function returns the sum of all such delays.515* The exact delay is not necessarily an integer value in either input or516* output sample rate. Especially when downsampling by a large value, the517* output sample rate may be a poor choice to represent the delay, similarly518* for upsampling and the input sample rate.519*520* @param s swr context521* @param base timebase in which the returned delay will be:522* @li if it's set to 1 the returned delay is in seconds523* @li if it's set to 1000 the returned delay is in milliseconds524* @li if it's set to the input sample rate then the returned525* delay is in input samples526* @li if it's set to the output sample rate then the returned527* delay is in output samples528* @li if it's the least common multiple of in_sample_rate and529* out_sample_rate then an exact rounding-free delay will be530* returned531* @returns the delay in 1 / @c base units.532*/533int64_t swr_get_delay(struct SwrContext *s, int64_t base);534535/**536* Find an upper bound on the number of samples that the next swr_convert537* call will output, if called with in_samples of input samples. This538* depends on the internal state, and anything changing the internal state539* (like further swr_convert() calls) will may change the number of samples540* swr_get_out_samples() returns for the same number of input samples.541*542* @param in_samples number of input samples.543* @note any call to swr_inject_silence(), swr_convert(), swr_next_pts()544* or swr_set_compensation() invalidates this limit545* @note it is recommended to pass the correct available buffer size546* to all functions like swr_convert() even if swr_get_out_samples()547* indicates that less would be used.548* @returns an upper bound on the number of samples that the next swr_convert549* will output or a negative value to indicate an error550*/551int swr_get_out_samples(struct SwrContext *s, int in_samples);552553/**554* @}555*556* @name Configuration accessors557* @{558*/559560/**561* Return the @ref LIBSWRESAMPLE_VERSION_INT constant.562*563* This is useful to check if the build-time libswresample has the same version564* as the run-time one.565*566* @returns the unsigned int-typed version567*/568unsigned swresample_version(void);569570/**571* Return the swr build-time configuration.572*573* @returns the build-time @c ./configure flags574*/575const char *swresample_configuration(void);576577/**578* Return the swr license.579*580* @returns the license of libswresample, determined at build-time581*/582const char *swresample_license(void);583584/**585* @}586*587* @name AVFrame based API588* @{589*/590591/**592* Convert the samples in the input AVFrame and write them to the output AVFrame.593*594* Input and output AVFrames must have channel_layout, sample_rate and format set.595*596* If the output AVFrame does not have the data pointers allocated the nb_samples597* field will be set using av_frame_get_buffer()598* is called to allocate the frame.599*600* The output AVFrame can be NULL or have fewer allocated samples than required.601* In this case, any remaining samples not written to the output will be added602* to an internal FIFO buffer, to be returned at the next call to this function603* or to swr_convert().604*605* If converting sample rate, there may be data remaining in the internal606* resampling delay buffer. swr_get_delay() tells the number of607* remaining samples. To get this data as output, call this function or608* swr_convert() with NULL input.609*610* If the SwrContext configuration does not match the output and611* input AVFrame settings the conversion does not take place and depending on612* which AVFrame is not matching AVERROR_OUTPUT_CHANGED, AVERROR_INPUT_CHANGED613* or the result of a bitwise-OR of them is returned.614*615* @see swr_delay()616* @see swr_convert()617* @see swr_get_delay()618*619* @param swr audio resample context620* @param output output AVFrame621* @param input input AVFrame622* @return 0 on success, AVERROR on failure or nonmatching623* configuration.624*/625int swr_convert_frame(SwrContext *swr,626AVFrame *output, const AVFrame *input);627628/**629* Configure or reconfigure the SwrContext using the information630* provided by the AVFrames.631*632* The original resampling context is reset even on failure.633* The function calls swr_close() internally if the context is open.634*635* @see swr_close();636*637* @param swr audio resample context638* @param out output AVFrame639* @param in input AVFrame640* @return 0 on success, AVERROR on failure.641*/642int swr_config_frame(SwrContext *swr, const AVFrame *out, const AVFrame *in);643644/**645* @}646* @}647*/648649#endif /* SWRESAMPLE_SWRESAMPLE_H */650651652