]> git.sesse.net Git - ffmpeg/blob - libavresample/avresample.h
configure: formatting cosmetics
[ffmpeg] / libavresample / avresample.h
1 /*
2  * Copyright (c) 2012 Justin Ruggles <justin.ruggles@gmail.com>
3  *
4  * This file is part of Libav.
5  *
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.
10  *
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.
15  *
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
19  */
20
21 #ifndef AVRESAMPLE_AVRESAMPLE_H
22 #define AVRESAMPLE_AVRESAMPLE_H
23
24 /**
25  * @file
26  * @ingroup lavr
27  * external API header
28  */
29
30 /**
31  * @defgroup lavr Libavresample
32  * @{
33  *
34  * Libavresample (lavr) is a library that handles audio resampling, sample
35  * format conversion and mixing.
36  *
37  * Interaction with lavr is done through AVAudioResampleContext, which is
38  * allocated with avresample_alloc_context(). It is opaque, so all parameters
39  * must be set with the @ref avoptions API.
40  *
41  * For example the following code will setup conversion from planar float sample
42  * format to interleaved signed 16-bit integer, downsampling from 48kHz to
43  * 44.1kHz and downmixing from 5.1 channels to stereo (using the default mixing
44  * matrix):
45  * @code
46  * AVAudioResampleContext *avr = avresample_alloc_context();
47  * av_opt_set_int(avr, "in_channel_layout",  AV_CH_LAYOUT_5POINT1, 0);
48  * av_opt_set_int(avr, "out_channel_layout", AV_CH_LAYOUT_STEREO,  0);
49  * av_opt_set_int(avr, "in_sample_rate",     48000,                0);
50  * av_opt_set_int(avr, "out_sample_rate",    44100,                0);
51  * av_opt_set_int(avr, "in_sample_fmt",      AV_SAMPLE_FMT_FLTP,   0);
52  * av_opt_set_int(avr, "out_sample_fmt,      AV_SAMPLE_FMT_S16,    0);
53  * @endcode
54  *
55  * Once the context is initialized, it must be opened with avresample_open(). If
56  * you need to change the conversion parameters, you must close the context with
57  * avresample_close(), change the parameters as described above, then reopen it
58  * again.
59  *
60  * The conversion itself is done by repeatedly calling avresample_convert().
61  * Note that the samples may get buffered in two places in lavr. The first one
62  * is the output FIFO, where the samples end up if the output buffer is not
63  * large enough. The data stored in there may be retrieved at any time with
64  * avresample_read(). The second place is the resampling delay buffer,
65  * applicable only when resampling is done. The samples in it require more input
66  * before they can be processed. Their current amount is returned by
67  * avresample_get_delay(). At the end of conversion the resampling buffer can be
68  * flushed by calling avresample_convert() with NULL input.
69  *
70  * The following code demonstrates the conversion loop assuming the parameters
71  * from above and caller-defined functions get_input() and handle_output():
72  * @code
73  * uint8_t **input;
74  * int in_linesize, in_samples;
75  *
76  * while (get_input(&input, &in_linesize, &in_samples)) {
77  *     uint8_t *output
78  *     int out_linesize;
79  *     int out_samples = avresample_available(avr) +
80  *                       av_rescale_rnd(avresample_get_delay(avr) +
81  *                                      in_samples, 44100, 48000, AV_ROUND_UP);
82  *     av_samples_alloc(&output, &out_linesize, 2, out_samples,
83  *                      AV_SAMPLE_FMT_S16, 0);
84  *     out_samples = avresample_convert(avr, &output, out_linesize, out_samples,
85  *                                      input, in_linesize, in_samples);
86  *     handle_output(output, out_linesize, out_samples);
87  *     av_freep(&output);
88  *  }
89  *  @endcode
90  *
91  *  When the conversion is finished and the FIFOs are flushed if required, the
92  *  conversion context and everything associated with it must be freed with
93  *  avresample_free().
94  */
95
96 #include "libavutil/avutil.h"
97 #include "libavutil/channel_layout.h"
98 #include "libavutil/dict.h"
99 #include "libavutil/log.h"
100
101 #include "libavresample/version.h"
102
103 #define AVRESAMPLE_MAX_CHANNELS 32
104
105 typedef struct AVAudioResampleContext AVAudioResampleContext;
106
107 /** Mixing Coefficient Types */
108 enum AVMixCoeffType {
109     AV_MIX_COEFF_TYPE_Q8,   /** 16-bit 8.8 fixed-point                      */
110     AV_MIX_COEFF_TYPE_Q15,  /** 32-bit 17.15 fixed-point                    */
111     AV_MIX_COEFF_TYPE_FLT,  /** floating-point                              */
112     AV_MIX_COEFF_TYPE_NB,   /** Number of coeff types. Not part of ABI      */
113 };
114
115 /** Resampling Filter Types */
116 enum AVResampleFilterType {
117     AV_RESAMPLE_FILTER_TYPE_CUBIC,              /**< Cubic */
118     AV_RESAMPLE_FILTER_TYPE_BLACKMAN_NUTTALL,   /**< Blackman Nuttall Windowed Sinc */
119     AV_RESAMPLE_FILTER_TYPE_KAISER,             /**< Kaiser Windowed Sinc */
120 };
121
122 /**
123  * Return the LIBAVRESAMPLE_VERSION_INT constant.
124  */
125 unsigned avresample_version(void);
126
127 /**
128  * Return the libavresample build-time configuration.
129  * @return  configure string
130  */
131 const char *avresample_configuration(void);
132
133 /**
134  * Return the libavresample license.
135  */
136 const char *avresample_license(void);
137
138 /**
139  * Get the AVClass for AVAudioResampleContext.
140  *
141  * Can be used in combination with AV_OPT_SEARCH_FAKE_OBJ for examining options
142  * without allocating a context.
143  *
144  * @see av_opt_find().
145  *
146  * @return AVClass for AVAudioResampleContext
147  */
148 const AVClass *avresample_get_class(void);
149
150 /**
151  * Allocate AVAudioResampleContext and set options.
152  *
153  * @return  allocated audio resample context, or NULL on failure
154  */
155 AVAudioResampleContext *avresample_alloc_context(void);
156
157 /**
158  * Initialize AVAudioResampleContext.
159  *
160  * @param avr  audio resample context
161  * @return     0 on success, negative AVERROR code on failure
162  */
163 int avresample_open(AVAudioResampleContext *avr);
164
165 /**
166  * Close AVAudioResampleContext.
167  *
168  * This closes the context, but it does not change the parameters. The context
169  * can be reopened with avresample_open(). It does, however, clear the output
170  * FIFO and any remaining leftover samples in the resampling delay buffer. If
171  * there was a custom matrix being used, that is also cleared.
172  *
173  * @see avresample_convert()
174  * @see avresample_set_matrix()
175  *
176  * @param avr  audio resample context
177  */
178 void avresample_close(AVAudioResampleContext *avr);
179
180 /**
181  * Free AVAudioResampleContext and associated AVOption values.
182  *
183  * This also calls avresample_close() before freeing.
184  *
185  * @param avr  audio resample context
186  */
187 void avresample_free(AVAudioResampleContext **avr);
188
189 /**
190  * Generate a channel mixing matrix.
191  *
192  * This function is the one used internally by libavresample for building the
193  * default mixing matrix. It is made public just as a utility function for
194  * building custom matrices.
195  *
196  * @param in_layout           input channel layout
197  * @param out_layout          output channel layout
198  * @param center_mix_level    mix level for the center channel
199  * @param surround_mix_level  mix level for the surround channel(s)
200  * @param lfe_mix_level       mix level for the low-frequency effects channel
201  * @param normalize           if 1, coefficients will be normalized to prevent
202  *                            overflow. if 0, coefficients will not be
203  *                            normalized.
204  * @param[out] matrix         mixing coefficients; matrix[i + stride * o] is
205  *                            the weight of input channel i in output channel o.
206  * @param stride              distance between adjacent input channels in the
207  *                            matrix array
208  * @param matrix_encoding     matrixed stereo downmix mode (e.g. dplii)
209  * @return                    0 on success, negative AVERROR code on failure
210  */
211 int avresample_build_matrix(uint64_t in_layout, uint64_t out_layout,
212                             double center_mix_level, double surround_mix_level,
213                             double lfe_mix_level, int normalize, double *matrix,
214                             int stride, enum AVMatrixEncoding matrix_encoding);
215
216 /**
217  * Get the current channel mixing matrix.
218  *
219  * If no custom matrix has been previously set or the AVAudioResampleContext is
220  * not open, an error is returned.
221  *
222  * @param avr     audio resample context
223  * @param matrix  mixing coefficients; matrix[i + stride * o] is the weight of
224  *                input channel i in output channel o.
225  * @param stride  distance between adjacent input channels in the matrix array
226  * @return        0 on success, negative AVERROR code on failure
227  */
228 int avresample_get_matrix(AVAudioResampleContext *avr, double *matrix,
229                           int stride);
230
231 /**
232  * Set channel mixing matrix.
233  *
234  * Allows for setting a custom mixing matrix, overriding the default matrix
235  * generated internally during avresample_open(). This function can be called
236  * anytime on an allocated context, either before or after calling
237  * avresample_open(), as long as the channel layouts have been set.
238  * avresample_convert() always uses the current matrix.
239  * Calling avresample_close() on the context will clear the current matrix.
240  *
241  * @see avresample_close()
242  *
243  * @param avr     audio resample context
244  * @param matrix  mixing coefficients; matrix[i + stride * o] is the weight of
245  *                input channel i in output channel o.
246  * @param stride  distance between adjacent input channels in the matrix array
247  * @return        0 on success, negative AVERROR code on failure
248  */
249 int avresample_set_matrix(AVAudioResampleContext *avr, const double *matrix,
250                           int stride);
251
252 /**
253  * Set compensation for resampling.
254  *
255  * This can be called anytime after avresample_open(). If resampling is not
256  * automatically enabled because of a sample rate conversion, the
257  * "force_resampling" option must have been set to 1 when opening the context
258  * in order to use resampling compensation.
259  *
260  * @param avr                    audio resample context
261  * @param sample_delta           compensation delta, in samples
262  * @param compensation_distance  compensation distance, in samples
263  * @return                       0 on success, negative AVERROR code on failure
264  */
265 int avresample_set_compensation(AVAudioResampleContext *avr, int sample_delta,
266                                 int compensation_distance);
267
268 /**
269  * Convert input samples and write them to the output FIFO.
270  *
271  * The upper bound on the number of output samples is given by
272  * avresample_available() + (avresample_get_delay() + number of input samples) *
273  * output sample rate / input sample rate.
274  *
275  * The output data can be NULL or have fewer allocated samples than required.
276  * In this case, any remaining samples not written to the output will be added
277  * to an internal FIFO buffer, to be returned at the next call to this function
278  * or to avresample_read().
279  *
280  * If converting sample rate, there may be data remaining in the internal
281  * resampling delay buffer. avresample_get_delay() tells the number of remaining
282  * samples. To get this data as output, call avresample_convert() with NULL
283  * input.
284  *
285  * At the end of the conversion process, there may be data remaining in the
286  * internal FIFO buffer. avresample_available() tells the number of remaining
287  * samples. To get this data as output, either call avresample_convert() with
288  * NULL input or call avresample_read().
289  *
290  * @see avresample_available()
291  * @see avresample_read()
292  * @see avresample_get_delay()
293  *
294  * @param avr             audio resample context
295  * @param output          output data pointers
296  * @param out_plane_size  output plane size, in bytes.
297  *                        This can be 0 if unknown, but that will lead to
298  *                        optimized functions not being used directly on the
299  *                        output, which could slow down some conversions.
300  * @param out_samples     maximum number of samples that the output buffer can hold
301  * @param input           input data pointers
302  * @param in_plane_size   input plane size, in bytes
303  *                        This can be 0 if unknown, but that will lead to
304  *                        optimized functions not being used directly on the
305  *                        input, which could slow down some conversions.
306  * @param in_samples      number of input samples to convert
307  * @return                number of samples written to the output buffer,
308  *                        not including converted samples added to the internal
309  *                        output FIFO
310  */
311 int avresample_convert(AVAudioResampleContext *avr, uint8_t **output,
312                        int out_plane_size, int out_samples, uint8_t **input,
313                        int in_plane_size, int in_samples);
314
315 /**
316  * Return the number of samples currently in the resampling delay buffer.
317  *
318  * When resampling, there may be a delay between the input and output. Any
319  * unconverted samples in each call are stored internally in a delay buffer.
320  * This function allows the user to determine the current number of samples in
321  * the delay buffer, which can be useful for synchronization.
322  *
323  * @see avresample_convert()
324  *
325  * @param avr  audio resample context
326  * @return     number of samples currently in the resampling delay buffer
327  */
328 int avresample_get_delay(AVAudioResampleContext *avr);
329
330 /**
331  * Return the number of available samples in the output FIFO.
332  *
333  * During conversion, if the user does not specify an output buffer or
334  * specifies an output buffer that is smaller than what is needed, remaining
335  * samples that are not written to the output are stored to an internal FIFO
336  * buffer. The samples in the FIFO can be read with avresample_read() or
337  * avresample_convert().
338  *
339  * @see avresample_read()
340  * @see avresample_convert()
341  *
342  * @param avr  audio resample context
343  * @return     number of samples available for reading
344  */
345 int avresample_available(AVAudioResampleContext *avr);
346
347 /**
348  * Read samples from the output FIFO.
349  *
350  * During conversion, if the user does not specify an output buffer or
351  * specifies an output buffer that is smaller than what is needed, remaining
352  * samples that are not written to the output are stored to an internal FIFO
353  * buffer. This function can be used to read samples from that internal FIFO.
354  *
355  * @see avresample_available()
356  * @see avresample_convert()
357  *
358  * @param avr         audio resample context
359  * @param output      output data pointers. May be NULL, in which case
360  *                    nb_samples of data is discarded from output FIFO.
361  * @param nb_samples  number of samples to read from the FIFO
362  * @return            the number of samples written to output
363  */
364 int avresample_read(AVAudioResampleContext *avr, uint8_t **output, int nb_samples);
365
366 /**
367  * @}
368  */
369
370 #endif /* AVRESAMPLE_AVRESAMPLE_H */