yading@11: /* yading@11: * Copyright (C) 2011-2013 Michael Niedermayer (michaelni@gmx.at) yading@11: * yading@11: * This file is part of libswresample yading@11: * yading@11: * libswresample is free software; you can redistribute it and/or yading@11: * modify it under the terms of the GNU Lesser General Public yading@11: * License as published by the Free Software Foundation; either yading@11: * version 2.1 of the License, or (at your option) any later version. yading@11: * yading@11: * libswresample is distributed in the hope that it will be useful, yading@11: * but WITHOUT ANY WARRANTY; without even the implied warranty of yading@11: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU yading@11: * Lesser General Public License for more details. yading@11: * yading@11: * You should have received a copy of the GNU Lesser General Public yading@11: * License along with libswresample; if not, write to the Free Software yading@11: * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA yading@11: */ yading@11: yading@11: #ifndef SWRESAMPLE_SWRESAMPLE_H yading@11: #define SWRESAMPLE_SWRESAMPLE_H yading@11: yading@11: /** yading@11: * @file yading@11: * @ingroup lswr yading@11: * libswresample public header yading@11: */ yading@11: yading@11: /** yading@11: * @defgroup lswr Libswresample yading@11: * @{ yading@11: * yading@11: * Libswresample (lswr) is a library that handles audio resampling, sample yading@11: * format conversion and mixing. yading@11: * yading@11: * Interaction with lswr is done through SwrContext, which is yading@11: * allocated with swr_alloc() or swr_alloc_set_opts(). It is opaque, so all parameters yading@11: * must be set with the @ref avoptions API. yading@11: * yading@11: * For example the following code will setup conversion from planar float sample yading@11: * format to interleaved signed 16-bit integer, downsampling from 48kHz to yading@11: * 44.1kHz and downmixing from 5.1 channels to stereo (using the default mixing yading@11: * matrix): yading@11: * @code yading@11: * SwrContext *swr = swr_alloc(); yading@11: * av_opt_set_int(swr, "in_channel_layout", AV_CH_LAYOUT_5POINT1, 0); yading@11: * av_opt_set_int(swr, "out_channel_layout", AV_CH_LAYOUT_STEREO, 0); yading@11: * av_opt_set_int(swr, "in_sample_rate", 48000, 0); yading@11: * av_opt_set_int(swr, "out_sample_rate", 44100, 0); yading@11: * av_opt_set_sample_fmt(swr, "in_sample_fmt", AV_SAMPLE_FMT_FLTP, 0); yading@11: * av_opt_set_sample_fmt(swr, "out_sample_fmt", AV_SAMPLE_FMT_S16, 0); yading@11: * @endcode yading@11: * yading@11: * Once all values have been set, it must be initialized with swr_init(). If yading@11: * you need to change the conversion parameters, you can change the parameters yading@11: * as described above, or by using swr_alloc_set_opts(), then call swr_init() yading@11: * again. yading@11: * yading@11: * The conversion itself is done by repeatedly calling swr_convert(). yading@11: * Note that the samples may get buffered in swr if you provide insufficient yading@11: * output space or if sample rate conversion is done, which requires "future" yading@11: * samples. Samples that do not require future input can be retrieved at any yading@11: * time by using swr_convert() (in_count can be set to 0). yading@11: * At the end of conversion the resampling buffer can be flushed by calling yading@11: * swr_convert() with NULL in and 0 in_count. yading@11: * yading@11: * The delay between input and output, can at any time be found by using yading@11: * swr_get_delay(). yading@11: * yading@11: * The following code demonstrates the conversion loop assuming the parameters yading@11: * from above and caller-defined functions get_input() and handle_output(): yading@11: * @code yading@11: * uint8_t **input; yading@11: * int in_samples; yading@11: * yading@11: * while (get_input(&input, &in_samples)) { yading@11: * uint8_t *output; yading@11: * int out_samples = av_rescale_rnd(swr_get_delay(swr, 48000) + yading@11: * in_samples, 44100, 48000, AV_ROUND_UP); yading@11: * av_samples_alloc(&output, NULL, 2, out_samples, yading@11: * AV_SAMPLE_FMT_S16, 0); yading@11: * out_samples = swr_convert(swr, &output, out_samples, yading@11: * input, in_samples); yading@11: * handle_output(output, out_samples); yading@11: * av_freep(&output); yading@11: * } yading@11: * @endcode yading@11: * yading@11: * When the conversion is finished, the conversion yading@11: * context and everything associated with it must be freed with swr_free(). yading@11: * There will be no memory leak if the data is not completely flushed before yading@11: * swr_free(). yading@11: */ yading@11: yading@11: #include yading@11: #include "libavutil/samplefmt.h" yading@11: yading@11: #include "libswresample/version.h" yading@11: yading@11: #if LIBSWRESAMPLE_VERSION_MAJOR < 1 yading@11: #define SWR_CH_MAX 32 ///< Maximum number of channels yading@11: #endif yading@11: yading@11: #define SWR_FLAG_RESAMPLE 1 ///< Force resampling even if equal sample rate yading@11: //TODO use int resample ? yading@11: //long term TODO can we enable this dynamically? yading@11: yading@11: enum SwrDitherType { yading@11: SWR_DITHER_NONE = 0, yading@11: SWR_DITHER_RECTANGULAR, yading@11: SWR_DITHER_TRIANGULAR, yading@11: SWR_DITHER_TRIANGULAR_HIGHPASS, yading@11: yading@11: SWR_DITHER_NS = 64, ///< not part of API/ABI yading@11: SWR_DITHER_NS_LIPSHITZ, yading@11: SWR_DITHER_NS_F_WEIGHTED, yading@11: SWR_DITHER_NS_MODIFIED_E_WEIGHTED, yading@11: SWR_DITHER_NS_IMPROVED_E_WEIGHTED, yading@11: SWR_DITHER_NS_SHIBATA, yading@11: SWR_DITHER_NS_LOW_SHIBATA, yading@11: SWR_DITHER_NS_HIGH_SHIBATA, yading@11: SWR_DITHER_NB, ///< not part of API/ABI yading@11: }; yading@11: yading@11: /** Resampling Engines */ yading@11: enum SwrEngine { yading@11: SWR_ENGINE_SWR, /**< SW Resampler */ yading@11: SWR_ENGINE_SOXR, /**< SoX Resampler */ yading@11: SWR_ENGINE_NB, ///< not part of API/ABI yading@11: }; yading@11: yading@11: /** Resampling Filter Types */ yading@11: enum SwrFilterType { yading@11: SWR_FILTER_TYPE_CUBIC, /**< Cubic */ yading@11: SWR_FILTER_TYPE_BLACKMAN_NUTTALL, /**< Blackman Nuttall Windowed Sinc */ yading@11: SWR_FILTER_TYPE_KAISER, /**< Kaiser Windowed Sinc */ yading@11: }; yading@11: yading@11: typedef struct SwrContext SwrContext; yading@11: yading@11: /** yading@11: * Get the AVClass for swrContext. It can be used in combination with yading@11: * AV_OPT_SEARCH_FAKE_OBJ for examining options. yading@11: * yading@11: * @see av_opt_find(). yading@11: */ yading@11: const AVClass *swr_get_class(void); yading@11: yading@11: /** yading@11: * Allocate SwrContext. yading@11: * yading@11: * If you use this function you will need to set the parameters (manually or yading@11: * with swr_alloc_set_opts()) before calling swr_init(). yading@11: * yading@11: * @see swr_alloc_set_opts(), swr_init(), swr_free() yading@11: * @return NULL on error, allocated context otherwise yading@11: */ yading@11: struct SwrContext *swr_alloc(void); yading@11: yading@11: /** yading@11: * Initialize context after user parameters have been set. yading@11: * yading@11: * @return AVERROR error code in case of failure. yading@11: */ yading@11: int swr_init(struct SwrContext *s); yading@11: yading@11: /** yading@11: * Allocate SwrContext if needed and set/reset common parameters. yading@11: * yading@11: * This function does not require s to be allocated with swr_alloc(). On the yading@11: * other hand, swr_alloc() can use swr_alloc_set_opts() to set the parameters yading@11: * on the allocated context. yading@11: * yading@11: * @param s Swr context, can be NULL yading@11: * @param out_ch_layout output channel layout (AV_CH_LAYOUT_*) yading@11: * @param out_sample_fmt output sample format (AV_SAMPLE_FMT_*). yading@11: * @param out_sample_rate output sample rate (frequency in Hz) yading@11: * @param in_ch_layout input channel layout (AV_CH_LAYOUT_*) yading@11: * @param in_sample_fmt input sample format (AV_SAMPLE_FMT_*). yading@11: * @param in_sample_rate input sample rate (frequency in Hz) yading@11: * @param log_offset logging level offset yading@11: * @param log_ctx parent logging context, can be NULL yading@11: * yading@11: * @see swr_init(), swr_free() yading@11: * @return NULL on error, allocated context otherwise yading@11: */ yading@11: struct SwrContext *swr_alloc_set_opts(struct SwrContext *s, yading@11: int64_t out_ch_layout, enum AVSampleFormat out_sample_fmt, int out_sample_rate, yading@11: int64_t in_ch_layout, enum AVSampleFormat in_sample_fmt, int in_sample_rate, yading@11: int log_offset, void *log_ctx); yading@11: yading@11: /** yading@11: * Free the given SwrContext and set the pointer to NULL. yading@11: */ yading@11: void swr_free(struct SwrContext **s); yading@11: yading@11: /** yading@11: * Convert audio. yading@11: * yading@11: * in and in_count can be set to 0 to flush the last few samples out at the yading@11: * end. yading@11: * yading@11: * If more input is provided than output space then the input will be buffered. yading@11: * You can avoid this buffering by providing more output space than input. yading@11: * Convertion will run directly without copying whenever possible. yading@11: * yading@11: * @param s allocated Swr context, with parameters set yading@11: * @param out output buffers, only the first one need be set in case of packed audio yading@11: * @param out_count amount of space available for output in samples per channel yading@11: * @param in input buffers, only the first one need to be set in case of packed audio yading@11: * @param in_count number of input samples available in one channel yading@11: * yading@11: * @return number of samples output per channel, negative value on error yading@11: */ yading@11: int swr_convert(struct SwrContext *s, uint8_t **out, int out_count, yading@11: const uint8_t **in , int in_count); yading@11: yading@11: /** yading@11: * Convert the next timestamp from input to output yading@11: * timestamps are in 1/(in_sample_rate * out_sample_rate) units. yading@11: * yading@11: * @note There are 2 slightly differently behaving modes. yading@11: * First is when automatic timestamp compensation is not used, (min_compensation >= FLT_MAX) yading@11: * in this case timestamps will be passed through with delays compensated yading@11: * Second is when automatic timestamp compensation is used, (min_compensation < FLT_MAX) yading@11: * in this case the output timestamps will match output sample numbers yading@11: * yading@11: * @param pts timestamp for the next input sample, INT64_MIN if unknown yading@11: * @return the output timestamp for the next output sample yading@11: */ yading@11: int64_t swr_next_pts(struct SwrContext *s, int64_t pts); yading@11: yading@11: /** yading@11: * Activate resampling compensation. yading@11: */ yading@11: int swr_set_compensation(struct SwrContext *s, int sample_delta, int compensation_distance); yading@11: yading@11: /** yading@11: * Set a customized input channel mapping. yading@11: * yading@11: * @param s allocated Swr context, not yet initialized yading@11: * @param channel_map customized input channel mapping (array of channel yading@11: * indexes, -1 for a muted channel) yading@11: * @return AVERROR error code in case of failure. yading@11: */ yading@11: int swr_set_channel_mapping(struct SwrContext *s, const int *channel_map); yading@11: yading@11: /** yading@11: * Set a customized remix matrix. yading@11: * yading@11: * @param s allocated Swr context, not yet initialized yading@11: * @param matrix remix coefficients; matrix[i + stride * o] is yading@11: * the weight of input channel i in output channel o yading@11: * @param stride offset between lines of the matrix yading@11: * @return AVERROR error code in case of failure. yading@11: */ yading@11: int swr_set_matrix(struct SwrContext *s, const double *matrix, int stride); yading@11: yading@11: /** yading@11: * Drops the specified number of output samples. yading@11: */ yading@11: int swr_drop_output(struct SwrContext *s, int count); yading@11: yading@11: /** yading@11: * Injects the specified number of silence samples. yading@11: */ yading@11: int swr_inject_silence(struct SwrContext *s, int count); yading@11: yading@11: /** yading@11: * Gets the delay the next input sample will experience relative to the next output sample. yading@11: * yading@11: * Swresample can buffer data if more input has been provided than available yading@11: * output space, also converting between sample rates needs a delay. yading@11: * This function returns the sum of all such delays. yading@11: * The exact delay is not necessarily an integer value in either input or yading@11: * output sample rate. Especially when downsampling by a large value, the yading@11: * output sample rate may be a poor choice to represent the delay, similarly yading@11: * for upsampling and the input sample rate. yading@11: * yading@11: * @param s swr context yading@11: * @param base timebase in which the returned delay will be yading@11: * if its set to 1 the returned delay is in seconds yading@11: * if its set to 1000 the returned delay is in milli seconds yading@11: * if its set to the input sample rate then the returned delay is in input samples yading@11: * if its set to the output sample rate then the returned delay is in output samples yading@11: * an exact rounding free delay can be found by using LCM(in_sample_rate, out_sample_rate) yading@11: * @returns the delay in 1/base units. yading@11: */ yading@11: int64_t swr_get_delay(struct SwrContext *s, int64_t base); yading@11: yading@11: /** yading@11: * Return the LIBSWRESAMPLE_VERSION_INT constant. yading@11: */ yading@11: unsigned swresample_version(void); yading@11: yading@11: /** yading@11: * Return the swr build-time configuration. yading@11: */ yading@11: const char *swresample_configuration(void); yading@11: yading@11: /** yading@11: * Return the swr license. yading@11: */ yading@11: const char *swresample_license(void); yading@11: yading@11: /** yading@11: * @} yading@11: */ yading@11: yading@11: #endif /* SWRESAMPLE_SWRESAMPLE_H */