2 * Copyright (c) 2012 Justin Ruggles <justin.ruggles@gmail.com>
4 * This file is part of Libav.
6 * Libav is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2.1 of the License, or (at your option) any later version.
11 * Libav is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with Libav; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
21 #ifndef AVRESAMPLE_AVRESAMPLE_H
22 #define AVRESAMPLE_AVRESAMPLE_H
29 #include "libavutil/audioconvert.h"
30 #include "libavutil/avutil.h"
31 #include "libavutil/dict.h"
32 #include "libavutil/log.h"
34 #include "libavresample/version.h"
36 #define AVRESAMPLE_MAX_CHANNELS 32
38 typedef struct AVAudioResampleContext AVAudioResampleContext;
40 /** Mixing Coefficient Types */
42 AV_MIX_COEFF_TYPE_Q6, /** 16-bit 10.6 fixed-point */
43 AV_MIX_COEFF_TYPE_Q15, /** 32-bit 17.15 fixed-point */
44 AV_MIX_COEFF_TYPE_FLT, /** floating-point */
45 AV_MIX_COEFF_TYPE_NB, /** Number of coeff types. Not part of ABI */
49 * Return the LIBAVRESAMPLE_VERSION_INT constant.
51 unsigned avresample_version(void);
54 * Return the libavresample build-time configuration.
55 * @return configure string
57 const char *avresample_configuration(void);
60 * Return the libavresample license.
62 const char *avresample_license(void);
65 * Get the AVClass for AVAudioResampleContext.
67 * Can be used in combination with AV_OPT_SEARCH_FAKE_OBJ for examining options
68 * without allocating a context.
72 * @return AVClass for AVAudioResampleContext
74 const AVClass *avresample_get_class(void);
77 * Allocate AVAudioResampleContext and set options.
79 * @return allocated audio resample context, or NULL on failure
81 AVAudioResampleContext *avresample_alloc_context(void);
84 * Initialize AVAudioResampleContext.
86 * @param avr audio resample context
87 * @return 0 on success, negative AVERROR code on failure
89 int avresample_open(AVAudioResampleContext *avr);
92 * Close AVAudioResampleContext.
94 * This closes the context, but it does not change the parameters. The context
95 * can be reopened with avresample_open(). It does, however, clear the output
96 * FIFO and any remaining leftover samples in the resampling delay buffer. If
97 * there was a custom matrix being used, that is also cleared.
99 * @see avresample_convert()
100 * @see avresample_set_matrix()
102 * @param avr audio resample context
104 void avresample_close(AVAudioResampleContext *avr);
107 * Free AVAudioResampleContext and associated AVOption values.
109 * This also calls avresample_close() before freeing.
111 * @param avr audio resample context
113 void avresample_free(AVAudioResampleContext **avr);
116 * Generate a channel mixing matrix.
118 * This function is the one used internally by libavresample for building the
119 * default mixing matrix. It is made public just as a utility function for
120 * building custom matrices.
122 * @param in_layout input channel layout
123 * @param out_layout output channel layout
124 * @param center_mix_level mix level for the center channel
125 * @param surround_mix_level mix level for the surround channel(s)
126 * @param lfe_mix_level mix level for the low-frequency effects channel
127 * @param normalize if 1, coefficients will be normalized to prevent
128 * overflow. if 0, coefficients will not be
130 * @param[out] matrix mixing coefficients; matrix[i + stride * o] is
131 * the weight of input channel i in output channel o.
132 * @param stride distance between adjacent input channels in the
134 * @return 0 on success, negative AVERROR code on failure
136 int avresample_build_matrix(uint64_t in_layout, uint64_t out_layout,
137 double center_mix_level, double surround_mix_level,
138 double lfe_mix_level, int normalize, double *matrix,
142 * Get the current channel mixing matrix.
144 * @param avr audio resample context
145 * @param matrix mixing coefficients; matrix[i + stride * o] is the weight of
146 * input channel i in output channel o.
147 * @param stride distance between adjacent input channels in the matrix array
148 * @return 0 on success, negative AVERROR code on failure
150 int avresample_get_matrix(AVAudioResampleContext *avr, double *matrix,
154 * Set channel mixing matrix.
156 * Allows for setting a custom mixing matrix, overriding the default matrix
157 * generated internally during avresample_open(). This function can be called
158 * anytime on an allocated context, either before or after calling
159 * avresample_open(). avresample_convert() always uses the current matrix.
160 * Calling avresample_close() on the context will clear the current matrix.
162 * @see avresample_close()
164 * @param avr audio resample context
165 * @param matrix mixing coefficients; matrix[i + stride * o] is the weight of
166 * input channel i in output channel o.
167 * @param stride distance between adjacent input channels in the matrix array
168 * @return 0 on success, negative AVERROR code on failure
170 int avresample_set_matrix(AVAudioResampleContext *avr, const double *matrix,
174 * Set compensation for resampling.
176 * This can be called anytime after avresample_open(). If resampling was not
177 * being done previously, the AVAudioResampleContext is closed and reopened
178 * with resampling enabled. In this case, any samples remaining in the output
179 * FIFO and the current channel mixing matrix will be restored after reopening
182 * @param avr audio resample context
183 * @param sample_delta compensation delta, in samples
184 * @param compensation_distance compensation distance, in samples
185 * @return 0 on success, negative AVERROR code on failure
187 int avresample_set_compensation(AVAudioResampleContext *avr, int sample_delta,
188 int compensation_distance);
191 * Convert input samples and write them to the output FIFO.
193 * The output data can be NULL or have fewer allocated samples than required.
194 * In this case, any remaining samples not written to the output will be added
195 * to an internal FIFO buffer, to be returned at the next call to this function
196 * or to avresample_read().
198 * If converting sample rate, there may be data remaining in the internal
199 * resampling delay buffer. avresample_get_delay() tells the number of remaining
200 * samples. To get this data as output, call avresample_convert() with NULL
203 * At the end of the conversion process, there may be data remaining in the
204 * internal FIFO buffer. avresample_available() tells the number of remaining
205 * samples. To get this data as output, either call avresample_convert() with
206 * NULL input or call avresample_read().
208 * @see avresample_available()
209 * @see avresample_read()
210 * @see avresample_get_delay()
212 * @param avr audio resample context
213 * @param output output data pointers
214 * @param out_plane_size output plane size, in bytes.
215 * This can be 0 if unknown, but that will lead to
216 * optimized functions not being used directly on the
217 * output, which could slow down some conversions.
218 * @param out_samples maximum number of samples that the output buffer can hold
219 * @param input input data pointers
220 * @param in_plane_size input plane size, in bytes
221 * This can be 0 if unknown, but that will lead to
222 * optimized functions not being used directly on the
223 * input, which could slow down some conversions.
224 * @param in_samples number of input samples to convert
225 * @return number of samples written to the output buffer,
226 * not including converted samples added to the internal
229 int avresample_convert(AVAudioResampleContext *avr, void **output,
230 int out_plane_size, int out_samples, void **input,
231 int in_plane_size, int in_samples);
234 * Return the number of samples currently in the resampling delay buffer.
236 * When resampling, there may be a delay between the input and output. Any
237 * unconverted samples in each call are stored internally in a delay buffer.
238 * This function allows the user to determine the current number of samples in
239 * the delay buffer, which can be useful for synchronization.
241 * @see avresample_convert()
243 * @param avr audio resample context
244 * @return number of samples currently in the resampling delay buffer
246 int avresample_get_delay(AVAudioResampleContext *avr);
249 * Return the number of available samples in the output FIFO.
251 * During conversion, if the user does not specify an output buffer or
252 * specifies an output buffer that is smaller than what is needed, remaining
253 * samples that are not written to the output are stored to an internal FIFO
254 * buffer. The samples in the FIFO can be read with avresample_read() or
255 * avresample_convert().
257 * @see avresample_read()
258 * @see avresample_convert()
260 * @param avr audio resample context
261 * @return number of samples available for reading
263 int avresample_available(AVAudioResampleContext *avr);
266 * Read samples from the output FIFO.
268 * During conversion, if the user does not specify an output buffer or
269 * specifies an output buffer that is smaller than what is needed, remaining
270 * samples that are not written to the output are stored to an internal FIFO
271 * buffer. This function can be used to read samples from that internal FIFO.
273 * @see avresample_available()
274 * @see avresample_convert()
276 * @param avr audio resample context
277 * @param output output data pointers
278 * @param nb_samples number of samples to read from the FIFO
279 * @return the number of samples written to output
281 int avresample_read(AVAudioResampleContext *avr, void **output, int nb_samples);
283 #endif /* AVRESAMPLE_AVRESAMPLE_H */