1 module ffmpeg.libavresample.avresample;
2 
3 import std.stdint;
4 
5 import ffmpeg.libavutil.avutil;
6 import ffmpeg.libavutil.frame;
7 
8 import ffmpeg.libavresample.avresample_version;
9 
10 @nogc nothrow extern(C):
11 
12 /**
13 * @defgroup libavresample Color conversion and scaling
14 * @{
15 *
16 * Return the LIBSWRESAMPLE_VERSION_INT constant.
17 */
18 uint avresample_version();
19 
20 /**
21 * Return the libavresample build-time configuration.
22 * @return  configure string
23 */
24 char* avresample_configuration();
25 
26 /**
27 * Return the libavresample license.
28 */
29 char* avresample_license();
30 
31 
32 struct AVAudioResampleContext;
33 
34 enum AVRESAMPLE_MAX_CHANNELS =   32;
35 
36 /** Mixing Coefficient Types */
37 enum AVMixCoeffType {
38     AV_MIX_COEFF_TYPE_Q8,   /** 16-bit 8.8 fixed-point                      */
39     AV_MIX_COEFF_TYPE_Q15,  /** 32-bit 17.15 fixed-point                    */
40     AV_MIX_COEFF_TYPE_FLT,  /** floating-point                              */
41     AV_MIX_COEFF_TYPE_NB,   /** Number of coeff types. Not part of ABI      */
42 }
43 
44 /** Resampling Filter Types */
45 enum AVResampleFilterType {
46     AV_RESAMPLE_FILTER_TYPE_CUBIC,              /**< Cubic */
47     AV_RESAMPLE_FILTER_TYPE_BLACKMAN_NUTTALL,   /**< Blackman Nuttall Windowed Sinc */
48     AV_RESAMPLE_FILTER_TYPE_KAISER,             /**< Kaiser Windowed Sinc */
49 }
50 
51 enum AVResampleDitherMethod {
52     AV_RESAMPLE_DITHER_NONE,            /**< Do not use dithering */
53     AV_RESAMPLE_DITHER_RECTANGULAR,     /**< Rectangular Dither */
54     AV_RESAMPLE_DITHER_TRIANGULAR,      /**< Triangular Dither*/
55     AV_RESAMPLE_DITHER_TRIANGULAR_HP,   /**< Triangular Dither with High Pass */
56     AV_RESAMPLE_DITHER_TRIANGULAR_NS,   /**< Triangular Dither with Noise Shaping */
57     AV_RESAMPLE_DITHER_NB,              /**< Number of dither types. Not part of ABI. */
58 }
59 
60 /**
61  * Get the AVClass for AVAudioResampleContext.
62  *
63  * Can be used in combination with AV_OPT_SEARCH_FAKE_OBJ for examining options
64  * without allocating a context.
65  *
66  * @see av_opt_find().
67  *
68  * @return AVClass for AVAudioResampleContext
69  */
70 AVClass *avresample_get_class();
71 
72 /**
73  * Allocate AVAudioResampleContext and set options.
74  *
75  * @return  allocated audio resample context, or NULL on failure
76  */
77 AVAudioResampleContext *avresample_alloc_context();
78 
79 /**
80  * Initialize AVAudioResampleContext.
81  * @note The context must be configured using the AVOption API.
82  * @note The fields "in_channel_layout", "out_channel_layout",
83  *       "in_sample_rate", "out_sample_rate", "in_sample_fmt",
84  *       "out_sample_fmt" must be set.
85  *
86  * @see av_opt_set_int()
87  * @see av_opt_set_dict()
88  * @see av_get_default_channel_layout()
89  *
90  * @param avr  audio resample context
91  * @return     0 on success, negative AVERROR code on failure
92  */
93 int avresample_open(AVAudioResampleContext *avr);
94 
95 /**
96  * Check whether an AVAudioResampleContext is open or closed.
97  *
98  * @param avr AVAudioResampleContext to check
99  * @return 1 if avr is open, 0 if avr is closed.
100  */
101 int avresample_is_open(AVAudioResampleContext *avr);
102 
103 /**
104  * Close AVAudioResampleContext.
105  *
106  * This closes the context, but it does not change the parameters. The context
107  * can be reopened with avresample_open(). It does, however, clear the output
108  * FIFO and any remaining leftover samples in the resampling delay buffer. If
109  * there was a custom matrix being used, that is also cleared.
110  *
111  * @see avresample_convert()
112  * @see avresample_set_matrix()
113  *
114  * @param avr  audio resample context
115  */
116 void avresample_close(AVAudioResampleContext *avr);
117 
118 /**
119  * Free AVAudioResampleContext and associated AVOption values.
120  *
121  * This also calls avresample_close() before freeing.
122  *
123  * @param avr  audio resample context
124  */
125 void avresample_free(AVAudioResampleContext **avr);
126 
127 /**
128  * Generate a channel mixing matrix.
129  *
130  * This function is the one used internally by libavresample for building the
131  * default mixing matrix. It is made public just as a utility function for
132  * building custom matrices.
133  *
134  * @param in_layout           input channel layout
135  * @param out_layout          output channel layout
136  * @param center_mix_level    mix level for the center channel
137  * @param surround_mix_level  mix level for the surround channel(s)
138  * @param lfe_mix_level       mix level for the low-frequency effects channel
139  * @param normalize           if 1, coefficients will be normalized to prevent
140  *                            overflow. if 0, coefficients will not be
141  *                            normalized.
142  * @param[out] matrix         mixing coefficients; matrix[i + stride * o] is
143  *                            the weight of input channel i in output channel o.
144  * @param stride              distance between adjacent input channels in the
145  *                            matrix array
146  * @param matrix_encoding     matrixed stereo downmix mode (e.g. dplii)
147  * @return                    0 on success, negative AVERROR code on failure
148  */
149 int avresample_build_matrix(uint64_t in_layout, uint64_t out_layout,
150                             double center_mix_level, double surround_mix_level,
151                             double lfe_mix_level, int normalize, double *matrix,
152                             int stride, AVMatrixEncoding matrix_encoding);
153 
154 /**
155  * Get the current channel mixing matrix.
156  *
157  * If no custom matrix has been previously set or the AVAudioResampleContext is
158  * not open, an error is returned.
159  *
160  * @param avr     audio resample context
161  * @param matrix  mixing coefficients; matrix[i + stride * o] is the weight of
162  *                input channel i in output channel o.
163  * @param stride  distance between adjacent input channels in the matrix array
164  * @return        0 on success, negative AVERROR code on failure
165  */
166 int avresample_get_matrix(AVAudioResampleContext *avr, double *matrix,
167                           int stride);
168 
169 /**
170  * Set channel mixing matrix.
171  *
172  * Allows for setting a custom mixing matrix, overriding the default matrix
173  * generated internally during avresample_open(). This function can be called
174  * anytime on an allocated context, either before or after calling
175  * avresample_open(), as long as the channel layouts have been set.
176  * avresample_convert() always uses the current matrix.
177  * Calling avresample_close() on the context will clear the current matrix.
178  *
179  * @see avresample_close()
180  *
181  * @param avr     audio resample context
182  * @param matrix  mixing coefficients; matrix[i + stride * o] is the weight of
183  *                input channel i in output channel o.
184  * @param stride  distance between adjacent input channels in the matrix array
185  * @return        0 on success, negative AVERROR code on failure
186  */
187 int avresample_set_matrix(AVAudioResampleContext *avr, const double *matrix,
188                           int stride);
189 
190 /**
191  * Set a customized input channel mapping.
192  *
193  * This function can only be called when the allocated context is not open.
194  * Also, the input channel layout must have already been set.
195  *
196  * Calling avresample_close() on the context will clear the channel mapping.
197  *
198  * The map for each input channel specifies the channel index in the source to
199  * use for that particular channel, or -1 to mute the channel. Source channels
200  * can be duplicated by using the same index for multiple input channels.
201  *
202  * Examples:
203  *
204  * Reordering 5.1 AAC order (C,L,R,Ls,Rs,LFE) to FFmpeg order (L,R,C,LFE,Ls,Rs):
205  * { 1, 2, 0, 5, 3, 4 }
206  *
207  * Muting the 3rd channel in 4-channel input:
208  * { 0, 1, -1, 3 }
209  *
210  * Duplicating the left channel of stereo input:
211  * { 0, 0 }
212  *
213  * @param avr         audio resample context
214  * @param channel_map customized input channel mapping
215  * @return            0 on success, negative AVERROR code on failure
216  */
217 int avresample_set_channel_mapping(AVAudioResampleContext *avr,
218                                    const int *channel_map);
219 
220 /**
221  * Set compensation for resampling.
222  *
223  * This can be called anytime after avresample_open(). If resampling is not
224  * automatically enabled because of a sample rate conversion, the
225  * "force_resampling" option must have been set to 1 when opening the context
226  * in order to use resampling compensation.
227  *
228  * @param avr                    audio resample context
229  * @param sample_delta           compensation delta, in samples
230  * @param compensation_distance  compensation distance, in samples
231  * @return                       0 on success, negative AVERROR code on failure
232  */
233 int avresample_set_compensation(AVAudioResampleContext *avr, int sample_delta,
234                                 int compensation_distance);
235 
236 /**
237  * Provide the upper bound on the number of samples the configured
238  * conversion would output.
239  *
240  * @param avr           audio resample context
241  * @param in_nb_samples number of input samples
242  *
243  * @return              number of samples or AVERROR(EINVAL) if the value
244  *                      would exceed INT_MAX
245  */
246 
247 int avresample_get_out_samples(AVAudioResampleContext *avr, int in_nb_samples);
248 
249 /**
250  * Convert input samples and write them to the output FIFO.
251  *
252  * The upper bound on the number of output samples can be obtained through
253  * avresample_get_out_samples().
254  *
255  * The output data can be NULL or have fewer allocated samples than required.
256  * In this case, any remaining samples not written to the output will be added
257  * to an internal FIFO buffer, to be returned at the next call to this function
258  * or to avresample_read().
259  *
260  * If converting sample rate, there may be data remaining in the internal
261  * resampling delay buffer. avresample_get_delay() tells the number of remaining
262  * samples. To get this data as output, call avresample_convert() with NULL
263  * input.
264  *
265  * At the end of the conversion process, there may be data remaining in the
266  * internal FIFO buffer. avresample_available() tells the number of remaining
267  * samples. To get this data as output, either call avresample_convert() with
268  * NULL input or call avresample_read().
269  *
270  * @see avresample_get_out_samples()
271  * @see avresample_read()
272  * @see avresample_get_delay()
273  *
274  * @param avr             audio resample context
275  * @param output          output data pointers
276  * @param out_plane_size  output plane size, in bytes.
277  *                        This can be 0 if unknown, but that will lead to
278  *                        optimized functions not being used directly on the
279  *                        output, which could slow down some conversions.
280  * @param out_samples     maximum number of samples that the output buffer can hold
281  * @param input           input data pointers
282  * @param in_plane_size   input plane size, in bytes
283  *                        This can be 0 if unknown, but that will lead to
284  *                        optimized functions not being used directly on the
285  *                        input, which could slow down some conversions.
286  * @param in_samples      number of input samples to convert
287  * @return                number of samples written to the output buffer,
288  *                        not including converted samples added to the internal
289  *                        output FIFO
290  */
291 int avresample_convert(AVAudioResampleContext *avr, uint8_t **output,
292                        int out_plane_size, int out_samples,
293                        uint8_t * *input, int in_plane_size,
294                        int in_samples);
295 
296 /**
297  * Return the number of samples currently in the resampling delay buffer.
298  *
299  * When resampling, there may be a delay between the input and output. Any
300  * unconverted samples in each call are stored internally in a delay buffer.
301  * This function allows the user to determine the current number of samples in
302  * the delay buffer, which can be useful for synchronization.
303  *
304  * @see avresample_convert()
305  *
306  * @param avr  audio resample context
307  * @return     number of samples currently in the resampling delay buffer
308  */
309 int avresample_get_delay(AVAudioResampleContext *avr);
310 
311 /**
312  * Return the number of available samples in the output FIFO.
313  *
314  * During conversion, if the user does not specify an output buffer or
315  * specifies an output buffer that is smaller than what is needed, remaining
316  * samples that are not written to the output are stored to an internal FIFO
317  * buffer. The samples in the FIFO can be read with avresample_read() or
318  * avresample_convert().
319  *
320  * @see avresample_read()
321  * @see avresample_convert()
322  *
323  * @param avr  audio resample context
324  * @return     number of samples available for reading
325  */
326 int avresample_available(AVAudioResampleContext *avr);
327 
328 /**
329  * Read samples from the output FIFO.
330  *
331  * During conversion, if the user does not specify an output buffer or
332  * specifies an output buffer that is smaller than what is needed, remaining
333  * samples that are not written to the output are stored to an internal FIFO
334  * buffer. This function can be used to read samples from that internal FIFO.
335  *
336  * @see avresample_available()
337  * @see avresample_convert()
338  *
339  * @param avr         audio resample context
340  * @param output      output data pointers. May be NULL, in which case
341  *                    nb_samples of data is discarded from output FIFO.
342  * @param nb_samples  number of samples to read from the FIFO
343  * @return            the number of samples written to output
344  */
345 int avresample_read(AVAudioResampleContext *avr, uint8_t **output, int nb_samples);
346 
347 /**
348  * Convert the samples in the input AVFrame and write them to the output AVFrame.
349  *
350  * Input and output AVFrames must have channel_layout, sample_rate and format set.
351  *
352  * The upper bound on the number of output samples is obtained through
353  * avresample_get_out_samples().
354  *
355  * If the output AVFrame does not have the data pointers allocated the nb_samples
356  * field will be set using avresample_get_out_samples() and av_frame_get_buffer()
357  * is called to allocate the frame.
358  *
359  * The output AVFrame can be NULL or have fewer allocated samples than required.
360  * In this case, any remaining samples not written to the output will be added
361  * to an internal FIFO buffer, to be returned at the next call to this function
362  * or to avresample_convert() or to avresample_read().
363  *
364  * If converting sample rate, there may be data remaining in the internal
365  * resampling delay buffer. avresample_get_delay() tells the number of
366  * remaining samples. To get this data as output, call this function or
367  * avresample_convert() with NULL input.
368  *
369  * At the end of the conversion process, there may be data remaining in the
370  * internal FIFO buffer. avresample_available() tells the number of remaining
371  * samples. To get this data as output, either call this function or
372  * avresample_convert() with NULL input or call avresample_read().
373  *
374  * If the AVAudioResampleContext configuration does not match the output and
375  * input AVFrame settings the conversion does not take place and depending on
376  * which AVFrame is not matching AVERROR_OUTPUT_CHANGED, AVERROR_INPUT_CHANGED
377  * or AVERROR_OUTPUT_CHANGED|AVERROR_INPUT_CHANGED is returned.
378  *
379  * @see avresample_get_out_samples()
380  * @see avresample_available()
381  * @see avresample_convert()
382  * @see avresample_read()
383  * @see avresample_get_delay()
384  *
385  * @param avr             audio resample context
386  * @param output          output AVFrame
387  * @param input           input AVFrame
388  * @return                0 on success, AVERROR on failure or nonmatching
389  *                        configuration.
390  */
391 int avresample_convert_frame(AVAudioResampleContext *avr,
392                              AVFrame *output, AVFrame *input);
393 
394 /**
395  * Configure or reconfigure the AVAudioResampleContext using the information
396  * provided by the AVFrames.
397  *
398  * The original resampling context is reset even on failure.
399  * The function calls avresample_close() internally if the context is open.
400  *
401  * @see avresample_open();
402  * @see avresample_close();
403  *
404  * @param avr             audio resample context
405  * @param output          output AVFrame
406  * @param input           input AVFrame
407  * @return                0 on success, AVERROR on failure.
408  */
409 int avresample_config(AVAudioResampleContext *avr, AVFrame *output, AVFrame *input);