yading@11: /* yading@11: * Copyright (c) 2012 Justin Ruggles yading@11: * yading@11: * This file is part of Libav. yading@11: * yading@11: * Libav 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: * Libav 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 Libav; 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 AVRESAMPLE_AVRESAMPLE_H yading@11: #define AVRESAMPLE_AVRESAMPLE_H yading@11: yading@11: /** yading@11: * @file yading@11: * @ingroup lavr yading@11: * external API header yading@11: */ yading@11: yading@11: /** yading@11: * @defgroup lavr Libavresample yading@11: * @{ yading@11: * yading@11: * Libavresample (lavr) is a library that handles audio resampling, sample yading@11: * format conversion and mixing. yading@11: * yading@11: * Interaction with lavr is done through AVAudioResampleContext, which is yading@11: * allocated with avresample_alloc_context(). 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: * AVAudioResampleContext *avr = avresample_alloc_context(); yading@11: * av_opt_set_int(avr, "in_channel_layout", AV_CH_LAYOUT_5POINT1, 0); yading@11: * av_opt_set_int(avr, "out_channel_layout", AV_CH_LAYOUT_STEREO, 0); yading@11: * av_opt_set_int(avr, "in_sample_rate", 48000, 0); yading@11: * av_opt_set_int(avr, "out_sample_rate", 44100, 0); yading@11: * av_opt_set_int(avr, "in_sample_fmt", AV_SAMPLE_FMT_FLTP, 0); yading@11: * av_opt_set_int(avr, "out_sample_fmt", AV_SAMPLE_FMT_S16, 0); yading@11: * @endcode yading@11: * yading@11: * Once the context is initialized, it must be opened with avresample_open(). If yading@11: * you need to change the conversion parameters, you must close the context with yading@11: * avresample_close(), change the parameters as described above, then reopen it yading@11: * again. yading@11: * yading@11: * The conversion itself is done by repeatedly calling avresample_convert(). yading@11: * Note that the samples may get buffered in two places in lavr. The first one yading@11: * is the output FIFO, where the samples end up if the output buffer is not yading@11: * large enough. The data stored in there may be retrieved at any time with yading@11: * avresample_read(). The second place is the resampling delay buffer, yading@11: * applicable only when resampling is done. The samples in it require more input yading@11: * before they can be processed. Their current amount is returned by yading@11: * avresample_get_delay(). At the end of conversion the resampling buffer can be yading@11: * flushed by calling avresample_convert() with NULL input. 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_linesize, in_samples; yading@11: * yading@11: * while (get_input(&input, &in_linesize, &in_samples)) { yading@11: * uint8_t *output yading@11: * int out_linesize; yading@11: * int out_samples = avresample_available(avr) + yading@11: * av_rescale_rnd(avresample_get_delay(avr) + yading@11: * in_samples, 44100, 48000, AV_ROUND_UP); yading@11: * av_samples_alloc(&output, &out_linesize, 2, out_samples, yading@11: * AV_SAMPLE_FMT_S16, 0); yading@11: * out_samples = avresample_convert(avr, &output, out_linesize, out_samples, yading@11: * input, in_linesize, in_samples); yading@11: * handle_output(output, out_linesize, out_samples); yading@11: * av_freep(&output); yading@11: * } yading@11: * @endcode yading@11: * yading@11: * When the conversion is finished and the FIFOs are flushed if required, the yading@11: * conversion context and everything associated with it must be freed with yading@11: * avresample_free(). yading@11: */ yading@11: yading@11: #include "libavutil/avutil.h" yading@11: #include "libavutil/channel_layout.h" yading@11: #include "libavutil/dict.h" yading@11: #include "libavutil/log.h" yading@11: yading@11: #include "libavresample/version.h" yading@11: yading@11: #define AVRESAMPLE_MAX_CHANNELS 32 yading@11: yading@11: typedef struct AVAudioResampleContext AVAudioResampleContext; yading@11: yading@11: /** Mixing Coefficient Types */ yading@11: enum AVMixCoeffType { yading@11: AV_MIX_COEFF_TYPE_Q8, /** 16-bit 8.8 fixed-point */ yading@11: AV_MIX_COEFF_TYPE_Q15, /** 32-bit 17.15 fixed-point */ yading@11: AV_MIX_COEFF_TYPE_FLT, /** floating-point */ yading@11: AV_MIX_COEFF_TYPE_NB, /** Number of coeff types. Not part of ABI */ yading@11: }; yading@11: yading@11: /** Resampling Filter Types */ yading@11: enum AVResampleFilterType { yading@11: AV_RESAMPLE_FILTER_TYPE_CUBIC, /**< Cubic */ yading@11: AV_RESAMPLE_FILTER_TYPE_BLACKMAN_NUTTALL, /**< Blackman Nuttall Windowed Sinc */ yading@11: AV_RESAMPLE_FILTER_TYPE_KAISER, /**< Kaiser Windowed Sinc */ yading@11: }; yading@11: yading@11: enum AVResampleDitherMethod { yading@11: AV_RESAMPLE_DITHER_NONE, /**< Do not use dithering */ yading@11: AV_RESAMPLE_DITHER_RECTANGULAR, /**< Rectangular Dither */ yading@11: AV_RESAMPLE_DITHER_TRIANGULAR, /**< Triangular Dither*/ yading@11: AV_RESAMPLE_DITHER_TRIANGULAR_HP, /**< Triangular Dither with High Pass */ yading@11: AV_RESAMPLE_DITHER_TRIANGULAR_NS, /**< Triangular Dither with Noise Shaping */ yading@11: AV_RESAMPLE_DITHER_NB, /**< Number of dither types. Not part of ABI. */ yading@11: }; yading@11: yading@11: /** yading@11: * Return the LIBAVRESAMPLE_VERSION_INT constant. yading@11: */ yading@11: unsigned avresample_version(void); yading@11: yading@11: /** yading@11: * Return the libavresample build-time configuration. yading@11: * @return configure string yading@11: */ yading@11: const char *avresample_configuration(void); yading@11: yading@11: /** yading@11: * Return the libavresample license. yading@11: */ yading@11: const char *avresample_license(void); yading@11: yading@11: /** yading@11: * Get the AVClass for AVAudioResampleContext. yading@11: * yading@11: * Can be used in combination with AV_OPT_SEARCH_FAKE_OBJ for examining options yading@11: * without allocating a context. yading@11: * yading@11: * @see av_opt_find(). yading@11: * yading@11: * @return AVClass for AVAudioResampleContext yading@11: */ yading@11: const AVClass *avresample_get_class(void); yading@11: yading@11: /** yading@11: * Allocate AVAudioResampleContext and set options. yading@11: * yading@11: * @return allocated audio resample context, or NULL on failure yading@11: */ yading@11: AVAudioResampleContext *avresample_alloc_context(void); yading@11: yading@11: /** yading@11: * Initialize AVAudioResampleContext. yading@11: * yading@11: * @param avr audio resample context yading@11: * @return 0 on success, negative AVERROR code on failure yading@11: */ yading@11: int avresample_open(AVAudioResampleContext *avr); yading@11: yading@11: /** yading@11: * Close AVAudioResampleContext. yading@11: * yading@11: * This closes the context, but it does not change the parameters. The context yading@11: * can be reopened with avresample_open(). It does, however, clear the output yading@11: * FIFO and any remaining leftover samples in the resampling delay buffer. If yading@11: * there was a custom matrix being used, that is also cleared. yading@11: * yading@11: * @see avresample_convert() yading@11: * @see avresample_set_matrix() yading@11: * yading@11: * @param avr audio resample context yading@11: */ yading@11: void avresample_close(AVAudioResampleContext *avr); yading@11: yading@11: /** yading@11: * Free AVAudioResampleContext and associated AVOption values. yading@11: * yading@11: * This also calls avresample_close() before freeing. yading@11: * yading@11: * @param avr audio resample context yading@11: */ yading@11: void avresample_free(AVAudioResampleContext **avr); yading@11: yading@11: /** yading@11: * Generate a channel mixing matrix. yading@11: * yading@11: * This function is the one used internally by libavresample for building the yading@11: * default mixing matrix. It is made public just as a utility function for yading@11: * building custom matrices. yading@11: * yading@11: * @param in_layout input channel layout yading@11: * @param out_layout output channel layout yading@11: * @param center_mix_level mix level for the center channel yading@11: * @param surround_mix_level mix level for the surround channel(s) yading@11: * @param lfe_mix_level mix level for the low-frequency effects channel yading@11: * @param normalize if 1, coefficients will be normalized to prevent yading@11: * overflow. if 0, coefficients will not be yading@11: * normalized. yading@11: * @param[out] matrix mixing coefficients; matrix[i + stride * o] is yading@11: * the weight of input channel i in output channel o. yading@11: * @param stride distance between adjacent input channels in the yading@11: * matrix array yading@11: * @param matrix_encoding matrixed stereo downmix mode (e.g. dplii) yading@11: * @return 0 on success, negative AVERROR code on failure yading@11: */ yading@11: int avresample_build_matrix(uint64_t in_layout, uint64_t out_layout, yading@11: double center_mix_level, double surround_mix_level, yading@11: double lfe_mix_level, int normalize, double *matrix, yading@11: int stride, enum AVMatrixEncoding matrix_encoding); yading@11: yading@11: /** yading@11: * Get the current channel mixing matrix. yading@11: * yading@11: * If no custom matrix has been previously set or the AVAudioResampleContext is yading@11: * not open, an error is returned. yading@11: * yading@11: * @param avr audio resample context yading@11: * @param matrix mixing coefficients; matrix[i + stride * o] is the weight of yading@11: * input channel i in output channel o. yading@11: * @param stride distance between adjacent input channels in the matrix array yading@11: * @return 0 on success, negative AVERROR code on failure yading@11: */ yading@11: int avresample_get_matrix(AVAudioResampleContext *avr, double *matrix, yading@11: int stride); yading@11: yading@11: /** yading@11: * Set channel mixing matrix. yading@11: * yading@11: * Allows for setting a custom mixing matrix, overriding the default matrix yading@11: * generated internally during avresample_open(). This function can be called yading@11: * anytime on an allocated context, either before or after calling yading@11: * avresample_open(), as long as the channel layouts have been set. yading@11: * avresample_convert() always uses the current matrix. yading@11: * Calling avresample_close() on the context will clear the current matrix. yading@11: * yading@11: * @see avresample_close() yading@11: * yading@11: * @param avr audio resample context yading@11: * @param matrix mixing coefficients; matrix[i + stride * o] is the weight of yading@11: * input channel i in output channel o. yading@11: * @param stride distance between adjacent input channels in the matrix array yading@11: * @return 0 on success, negative AVERROR code on failure yading@11: */ yading@11: int avresample_set_matrix(AVAudioResampleContext *avr, const double *matrix, yading@11: int stride); yading@11: yading@11: /** yading@11: * Set a customized input channel mapping. yading@11: * yading@11: * This function can only be called when the allocated context is not open. yading@11: * Also, the input channel layout must have already been set. yading@11: * yading@11: * Calling avresample_close() on the context will clear the channel mapping. yading@11: * yading@11: * The map for each input channel specifies the channel index in the source to yading@11: * use for that particular channel, or -1 to mute the channel. Source channels yading@11: * can be duplicated by using the same index for multiple input channels. yading@11: * yading@11: * Examples: yading@11: * yading@11: * Reordering 5.1 AAC order (C,L,R,Ls,Rs,LFE) to Libav order (L,R,C,LFE,Ls,Rs): yading@11: * { 1, 2, 0, 5, 3, 4 } yading@11: * yading@11: * Muting the 3rd channel in 4-channel input: yading@11: * { 0, 1, -1, 3 } yading@11: * yading@11: * Duplicating the left channel of stereo input: yading@11: * { 0, 0 } yading@11: * yading@11: * @param avr audio resample context yading@11: * @param channel_map customized input channel mapping yading@11: * @return 0 on success, negative AVERROR code on failure yading@11: */ yading@11: int avresample_set_channel_mapping(AVAudioResampleContext *avr, yading@11: const int *channel_map); yading@11: yading@11: /** yading@11: * Set compensation for resampling. yading@11: * yading@11: * This can be called anytime after avresample_open(). If resampling is not yading@11: * automatically enabled because of a sample rate conversion, the yading@11: * "force_resampling" option must have been set to 1 when opening the context yading@11: * in order to use resampling compensation. yading@11: * yading@11: * @param avr audio resample context yading@11: * @param sample_delta compensation delta, in samples yading@11: * @param compensation_distance compensation distance, in samples yading@11: * @return 0 on success, negative AVERROR code on failure yading@11: */ yading@11: int avresample_set_compensation(AVAudioResampleContext *avr, int sample_delta, yading@11: int compensation_distance); yading@11: yading@11: /** yading@11: * Convert input samples and write them to the output FIFO. yading@11: * yading@11: * The upper bound on the number of output samples is given by yading@11: * avresample_available() + (avresample_get_delay() + number of input samples) * yading@11: * output sample rate / input sample rate. yading@11: * yading@11: * The output data can be NULL or have fewer allocated samples than required. yading@11: * In this case, any remaining samples not written to the output will be added yading@11: * to an internal FIFO buffer, to be returned at the next call to this function yading@11: * or to avresample_read(). yading@11: * yading@11: * If converting sample rate, there may be data remaining in the internal yading@11: * resampling delay buffer. avresample_get_delay() tells the number of remaining yading@11: * samples. To get this data as output, call avresample_convert() with NULL yading@11: * input. yading@11: * yading@11: * At the end of the conversion process, there may be data remaining in the yading@11: * internal FIFO buffer. avresample_available() tells the number of remaining yading@11: * samples. To get this data as output, either call avresample_convert() with yading@11: * NULL input or call avresample_read(). yading@11: * yading@11: * @see avresample_available() yading@11: * @see avresample_read() yading@11: * @see avresample_get_delay() yading@11: * yading@11: * @param avr audio resample context yading@11: * @param output output data pointers yading@11: * @param out_plane_size output plane size, in bytes. yading@11: * This can be 0 if unknown, but that will lead to yading@11: * optimized functions not being used directly on the yading@11: * output, which could slow down some conversions. yading@11: * @param out_samples maximum number of samples that the output buffer can hold yading@11: * @param input input data pointers yading@11: * @param in_plane_size input plane size, in bytes yading@11: * This can be 0 if unknown, but that will lead to yading@11: * optimized functions not being used directly on the yading@11: * input, which could slow down some conversions. yading@11: * @param in_samples number of input samples to convert yading@11: * @return number of samples written to the output buffer, yading@11: * not including converted samples added to the internal yading@11: * output FIFO yading@11: */ yading@11: int avresample_convert(AVAudioResampleContext *avr, uint8_t **output, yading@11: int out_plane_size, int out_samples, uint8_t **input, yading@11: int in_plane_size, int in_samples); yading@11: yading@11: /** yading@11: * Return the number of samples currently in the resampling delay buffer. yading@11: * yading@11: * When resampling, there may be a delay between the input and output. Any yading@11: * unconverted samples in each call are stored internally in a delay buffer. yading@11: * This function allows the user to determine the current number of samples in yading@11: * the delay buffer, which can be useful for synchronization. yading@11: * yading@11: * @see avresample_convert() yading@11: * yading@11: * @param avr audio resample context yading@11: * @return number of samples currently in the resampling delay buffer yading@11: */ yading@11: int avresample_get_delay(AVAudioResampleContext *avr); yading@11: yading@11: /** yading@11: * Return the number of available samples in the output FIFO. yading@11: * yading@11: * During conversion, if the user does not specify an output buffer or yading@11: * specifies an output buffer that is smaller than what is needed, remaining yading@11: * samples that are not written to the output are stored to an internal FIFO yading@11: * buffer. The samples in the FIFO can be read with avresample_read() or yading@11: * avresample_convert(). yading@11: * yading@11: * @see avresample_read() yading@11: * @see avresample_convert() yading@11: * yading@11: * @param avr audio resample context yading@11: * @return number of samples available for reading yading@11: */ yading@11: int avresample_available(AVAudioResampleContext *avr); yading@11: yading@11: /** yading@11: * Read samples from the output FIFO. yading@11: * yading@11: * During conversion, if the user does not specify an output buffer or yading@11: * specifies an output buffer that is smaller than what is needed, remaining yading@11: * samples that are not written to the output are stored to an internal FIFO yading@11: * buffer. This function can be used to read samples from that internal FIFO. yading@11: * yading@11: * @see avresample_available() yading@11: * @see avresample_convert() yading@11: * yading@11: * @param avr audio resample context yading@11: * @param output output data pointers. May be NULL, in which case yading@11: * nb_samples of data is discarded from output FIFO. yading@11: * @param nb_samples number of samples to read from the FIFO yading@11: * @return the number of samples written to output yading@11: */ yading@11: int avresample_read(AVAudioResampleContext *avr, uint8_t **output, int nb_samples); yading@11: yading@11: /** yading@11: * @} yading@11: */ yading@11: yading@11: #endif /* AVRESAMPLE_AVRESAMPLE_H */