1 /*
2  * Copyright (C) 2011-2013 Michael Niedermayer (michaelni@gmx.at)
3  *
4  * This file is part of libswresample
5  *
6  * libswresample 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  * libswresample 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 libswresample; if not, write to the Free Software
18  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
19  */
20 
21 module ffmpeg.libswresample.swresample;
22 
23 extern (C):
24 import ffmpeg; @nogc nothrow:
25 
26 /**
27  * @file
28  * @ingroup lswr
29  * libswresample public header
30  */
31 
32 /**
33  * @defgroup lswr libswresample
34  * @{
35  *
36  * Audio resampling, sample format conversion and mixing library.
37  *
38  * Interaction with lswr is done through SwrContext, which is
39  * allocated with swr_alloc() or swr_alloc_set_opts(). It is opaque, so all parameters
40  * must be set with the @ref avoptions API.
41  *
42  * The first thing you will need to do in order to use lswr is to allocate
43  * SwrContext. This can be done with swr_alloc() or swr_alloc_set_opts(). If you
44  * are using the former, you must set options through the @ref avoptions API.
45  * The latter function provides the same feature, but it allows you to set some
46  * common options in the same statement.
47  *
48  * For example the following code will setup conversion from planar float sample
49  * format to interleaved signed 16-bit integer, downsampling from 48kHz to
50  * 44.1kHz and downmixing from 5.1 channels to stereo (using the default mixing
51  * matrix). This is using the swr_alloc() function.
52  * @code
53  * SwrContext *swr = swr_alloc();
54  * av_opt_set_channel_layout(swr, "in_channel_layout",  AV_CH_LAYOUT_5POINT1, 0);
55  * av_opt_set_channel_layout(swr, "out_channel_layout", AV_CH_LAYOUT_STEREO,  0);
56  * av_opt_set_int(swr, "in_sample_rate",     48000,                0);
57  * av_opt_set_int(swr, "out_sample_rate",    44100,                0);
58  * av_opt_set_sample_fmt(swr, "in_sample_fmt",  AV_SAMPLE_FMT_FLTP, 0);
59  * av_opt_set_sample_fmt(swr, "out_sample_fmt", AV_SAMPLE_FMT_S16,  0);
60  * @endcode
61  *
62  * The same job can be done using swr_alloc_set_opts() as well:
63  * @code
64  * SwrContext *swr = swr_alloc_set_opts(NULL,  // we're allocating a new context
65  *                       AV_CH_LAYOUT_STEREO,  // out_ch_layout
66  *                       AV_SAMPLE_FMT_S16,    // out_sample_fmt
67  *                       44100,                // out_sample_rate
68  *                       AV_CH_LAYOUT_5POINT1, // in_ch_layout
69  *                       AV_SAMPLE_FMT_FLTP,   // in_sample_fmt
70  *                       48000,                // in_sample_rate
71  *                       0,                    // log_offset
72  *                       NULL);                // log_ctx
73  * @endcode
74  *
75  * Once all values have been set, it must be initialized with swr_init(). If
76  * you need to change the conversion parameters, you can change the parameters
77  * using @ref AVOptions, as described above in the first example; or by using
78  * swr_alloc_set_opts(), but with the first argument the allocated context.
79  * You must then call swr_init() again.
80  *
81  * The conversion itself is done by repeatedly calling swr_convert().
82  * Note that the samples may get buffered in swr if you provide insufficient
83  * output space or if sample rate conversion is done, which requires "future"
84  * samples. Samples that do not require future input can be retrieved at any
85  * time by using swr_convert() (in_count can be set to 0).
86  * At the end of conversion the resampling buffer can be flushed by calling
87  * swr_convert() with NULL in and 0 in_count.
88  *
89  * The samples used in the conversion process can be managed with the libavutil
90  * @ref lavu_sampmanip "samples manipulation" API, including av_samples_alloc()
91  * function used in the following example.
92  *
93  * The delay between input and output, can at any time be found by using
94  * swr_get_delay().
95  *
96  * The following code demonstrates the conversion loop assuming the parameters
97  * from above and caller-defined functions get_input() and handle_output():
98  * @code
99  * uint8_t **input;
100  * int in_samples;
101  *
102  * while (get_input(&input, &in_samples)) {
103  *     uint8_t *output;
104  *     int out_samples = av_rescale_rnd(swr_get_delay(swr, 48000) +
105  *                                      in_samples, 44100, 48000, AV_ROUND_UP);
106  *     av_samples_alloc(&output, NULL, 2, out_samples,
107  *                      AV_SAMPLE_FMT_S16, 0);
108  *     out_samples = swr_convert(swr, &output, out_samples,
109  *                                      input, in_samples);
110  *     handle_output(output, out_samples);
111  *     av_freep(&output);
112  * }
113  * @endcode
114  *
115  * When the conversion is finished, the conversion
116  * context and everything associated with it must be freed with swr_free().
117  * A swr_close() function is also available, but it exists mainly for
118  * compatibility with libavresample, and is not required to be called.
119  *
120  * There will be no memory leak if the data is not completely flushed before
121  * swr_free().
122  */
123 
124 /**
125  * @name Option constants
126  * These constants are used for the @ref avoptions interface for lswr.
127  * @{
128  *
129  */
130 
131 enum SWR_FLAG_RESAMPLE = 1; ///< Force resampling even if equal sample rate
132 //TODO use int resample ?
133 //long term TODO can we enable this dynamically?
134 
135 /** Dithering algorithms */
136 enum SwrDitherType
137 {
138     SWR_DITHER_NONE = 0,
139     SWR_DITHER_RECTANGULAR = 1,
140     SWR_DITHER_TRIANGULAR = 2,
141     SWR_DITHER_TRIANGULAR_HIGHPASS = 3,
142 
143     SWR_DITHER_NS = 64, ///< not part of API/ABI
144     SWR_DITHER_NS_LIPSHITZ = 65,
145     SWR_DITHER_NS_F_WEIGHTED = 66,
146     SWR_DITHER_NS_MODIFIED_E_WEIGHTED = 67,
147     SWR_DITHER_NS_IMPROVED_E_WEIGHTED = 68,
148     SWR_DITHER_NS_SHIBATA = 69,
149     SWR_DITHER_NS_LOW_SHIBATA = 70,
150     SWR_DITHER_NS_HIGH_SHIBATA = 71,
151     SWR_DITHER_NB = 72 ///< not part of API/ABI
152 }
153 
154 /** Resampling Engines */
155 enum SwrEngine
156 {
157     SWR_ENGINE_SWR = 0, /**< SW Resampler */
158     SWR_ENGINE_SOXR = 1, /**< SoX Resampler */
159     SWR_ENGINE_NB = 2 ///< not part of API/ABI
160 }
161 
162 /** Resampling Filter Types */
163 enum SwrFilterType
164 {
165     SWR_FILTER_TYPE_CUBIC = 0, /**< Cubic */
166     SWR_FILTER_TYPE_BLACKMAN_NUTTALL = 1, /**< Blackman Nuttall windowed sinc */
167     SWR_FILTER_TYPE_KAISER = 2 /**< Kaiser windowed sinc */
168 }
169 
170 /**
171  * @}
172  */
173 
174 /**
175  * The libswresample context. Unlike libavcodec and libavformat, this structure
176  * is opaque. This means that if you would like to set options, you must use
177  * the @ref avoptions API and cannot directly set values to members of the
178  * structure.
179  */
180 struct SwrContext;
181 
182 /**
183  * Get the AVClass for SwrContext. It can be used in combination with
184  * AV_OPT_SEARCH_FAKE_OBJ for examining options.
185  *
186  * @see av_opt_find().
187  * @return the AVClass of SwrContext
188  */
189 const(AVClass)* swr_get_class ();
190 
191 /**
192  * @name SwrContext constructor functions
193  * @{
194  */
195 
196 /**
197  * Allocate SwrContext.
198  *
199  * If you use this function you will need to set the parameters (manually or
200  * with swr_alloc_set_opts()) before calling swr_init().
201  *
202  * @see swr_alloc_set_opts(), swr_init(), swr_free()
203  * @return NULL on error, allocated context otherwise
204  */
205 SwrContext* swr_alloc ();
206 
207 /**
208  * Initialize context after user parameters have been set.
209  * @note The context must be configured using the AVOption API.
210  *
211  * @see av_opt_set_int()
212  * @see av_opt_set_dict()
213  *
214  * @param[in,out]   s Swr context to initialize
215  * @return AVERROR error code in case of failure.
216  */
217 int swr_init (SwrContext* s);
218 
219 /**
220  * Check whether an swr context has been initialized or not.
221  *
222  * @param[in]       s Swr context to check
223  * @see swr_init()
224  * @return positive if it has been initialized, 0 if not initialized
225  */
226 int swr_is_initialized (SwrContext* s);
227 
228 /**
229  * Allocate SwrContext if needed and set/reset common parameters.
230  *
231  * This function does not require s to be allocated with swr_alloc(). On the
232  * other hand, swr_alloc() can use swr_alloc_set_opts() to set the parameters
233  * on the allocated context.
234  *
235  * @param s               existing Swr context if available, or NULL if not
236  * @param out_ch_layout   output channel layout (AV_CH_LAYOUT_*)
237  * @param out_sample_fmt  output sample format (AV_SAMPLE_FMT_*).
238  * @param out_sample_rate output sample rate (frequency in Hz)
239  * @param in_ch_layout    input channel layout (AV_CH_LAYOUT_*)
240  * @param in_sample_fmt   input sample format (AV_SAMPLE_FMT_*).
241  * @param in_sample_rate  input sample rate (frequency in Hz)
242  * @param log_offset      logging level offset
243  * @param log_ctx         parent logging context, can be NULL
244  *
245  * @see swr_init(), swr_free()
246  * @return NULL on error, allocated context otherwise
247  */
248 SwrContext* swr_alloc_set_opts (
249     SwrContext* s,
250     long out_ch_layout,
251     AVSampleFormat out_sample_fmt,
252     int out_sample_rate,
253     long in_ch_layout,
254     AVSampleFormat in_sample_fmt,
255     int in_sample_rate,
256     int log_offset,
257     void* log_ctx);
258 
259 /**
260  * @}
261  *
262  * @name SwrContext destructor functions
263  * @{
264  */
265 
266 /**
267  * Free the given SwrContext and set the pointer to NULL.
268  *
269  * @param[in] s a pointer to a pointer to Swr context
270  */
271 void swr_free (SwrContext** s);
272 
273 /**
274  * Closes the context so that swr_is_initialized() returns 0.
275  *
276  * The context can be brought back to life by running swr_init(),
277  * swr_init() can also be used without swr_close().
278  * This function is mainly provided for simplifying the usecase
279  * where one tries to support libavresample and libswresample.
280  *
281  * @param[in,out] s Swr context to be closed
282  */
283 void swr_close (SwrContext* s);
284 
285 /**
286  * @}
287  *
288  * @name Core conversion functions
289  * @{
290  */
291 
292 /** Convert audio.
293  *
294  * in and in_count can be set to 0 to flush the last few samples out at the
295  * end.
296  *
297  * If more input is provided than output space, then the input will be buffered.
298  * You can avoid this buffering by using swr_get_out_samples() to retrieve an
299  * upper bound on the required number of output samples for the given number of
300  * input samples. Conversion will run directly without copying whenever possible.
301  *
302  * @param s         allocated Swr context, with parameters set
303  * @param out       output buffers, only the first one need be set in case of packed audio
304  * @param out_count amount of space available for output in samples per channel
305  * @param in        input buffers, only the first one need to be set in case of packed audio
306  * @param in_count  number of input samples available in one channel
307  *
308  * @return number of samples output per channel, negative value on error
309  */
310 int swr_convert (
311     SwrContext* s,
312     ubyte** out_,
313     int out_count,
314     const(ubyte*)* in_,
315     int in_count);
316 
317 /**
318  * Convert the next timestamp from input to output
319  * timestamps are in 1/(in_sample_rate * out_sample_rate) units.
320  *
321  * @note There are 2 slightly differently behaving modes.
322  *       @li When automatic timestamp compensation is not used, (min_compensation >= FLT_MAX)
323  *              in this case timestamps will be passed through with delays compensated
324  *       @li When automatic timestamp compensation is used, (min_compensation < FLT_MAX)
325  *              in this case the output timestamps will match output sample numbers.
326  *              See ffmpeg-resampler(1) for the two modes of compensation.
327  *
328  * @param s[in]     initialized Swr context
329  * @param pts[in]   timestamp for the next input sample, INT64_MIN if unknown
330  * @see swr_set_compensation(), swr_drop_output(), and swr_inject_silence() are
331  *      function used internally for timestamp compensation.
332  * @return the output timestamp for the next output sample
333  */
334 long swr_next_pts (SwrContext* s, long pts);
335 
336 /**
337  * @}
338  *
339  * @name Low-level option setting functions
340  * These functons provide a means to set low-level options that is not possible
341  * with the AVOption API.
342  * @{
343  */
344 
345 /**
346  * Activate resampling compensation ("soft" compensation). This function is
347  * internally called when needed in swr_next_pts().
348  *
349  * @param[in,out] s             allocated Swr context. If it is not initialized,
350  *                              or SWR_FLAG_RESAMPLE is not set, swr_init() is
351  *                              called with the flag set.
352  * @param[in]     sample_delta  delta in PTS per sample
353  * @param[in]     compensation_distance number of samples to compensate for
354  * @return    >= 0 on success, AVERROR error codes if:
355  *            @li @c s is NULL,
356  *            @li @c compensation_distance is less than 0,
357  *            @li @c compensation_distance is 0 but sample_delta is not,
358  *            @li compensation unsupported by resampler, or
359  *            @li swr_init() fails when called.
360  */
361 int swr_set_compensation (SwrContext* s, int sample_delta, int compensation_distance);
362 
363 /**
364  * Set a customized input channel mapping.
365  *
366  * @param[in,out] s           allocated Swr context, not yet initialized
367  * @param[in]     channel_map customized input channel mapping (array of channel
368  *                            indexes, -1 for a muted channel)
369  * @return >= 0 on success, or AVERROR error code in case of failure.
370  */
371 int swr_set_channel_mapping (SwrContext* s, const(int)* channel_map);
372 
373 /**
374  * Generate a channel mixing matrix.
375  *
376  * This function is the one used internally by libswresample for building the
377  * default mixing matrix. It is made public just as a utility function for
378  * building custom matrices.
379  *
380  * @param in_layout           input channel layout
381  * @param out_layout          output channel layout
382  * @param center_mix_level    mix level for the center channel
383  * @param surround_mix_level  mix level for the surround channel(s)
384  * @param lfe_mix_level       mix level for the low-frequency effects channel
385  * @param rematrix_maxval     if 1.0, coefficients will be normalized to prevent
386  *                            overflow. if INT_MAX, coefficients will not be
387  *                            normalized.
388  * @param[out] matrix         mixing coefficients; matrix[i + stride * o] is
389  *                            the weight of input channel i in output channel o.
390  * @param stride              distance between adjacent input channels in the
391  *                            matrix array
392  * @param matrix_encoding     matrixed stereo downmix mode (e.g. dplii)
393  * @param log_ctx             parent logging context, can be NULL
394  * @return                    0 on success, negative AVERROR code on failure
395  */
396 int swr_build_matrix (
397     ulong in_layout,
398     ulong out_layout,
399     double center_mix_level,
400     double surround_mix_level,
401     double lfe_mix_level,
402     double rematrix_maxval,
403     double rematrix_volume,
404     double* matrix,
405     int stride,
406     AVMatrixEncoding matrix_encoding,
407     void* log_ctx);
408 
409 /**
410  * Set a customized remix matrix.
411  *
412  * @param s       allocated Swr context, not yet initialized
413  * @param matrix  remix coefficients; matrix[i + stride * o] is
414  *                the weight of input channel i in output channel o
415  * @param stride  offset between lines of the matrix
416  * @return  >= 0 on success, or AVERROR error code in case of failure.
417  */
418 int swr_set_matrix (SwrContext* s, const(double)* matrix, int stride);
419 
420 /**
421  * @}
422  *
423  * @name Sample handling functions
424  * @{
425  */
426 
427 /**
428  * Drops the specified number of output samples.
429  *
430  * This function, along with swr_inject_silence(), is called by swr_next_pts()
431  * if needed for "hard" compensation.
432  *
433  * @param s     allocated Swr context
434  * @param count number of samples to be dropped
435  *
436  * @return >= 0 on success, or a negative AVERROR code on failure
437  */
438 int swr_drop_output (SwrContext* s, int count);
439 
440 /**
441  * Injects the specified number of silence samples.
442  *
443  * This function, along with swr_drop_output(), is called by swr_next_pts()
444  * if needed for "hard" compensation.
445  *
446  * @param s     allocated Swr context
447  * @param count number of samples to be dropped
448  *
449  * @return >= 0 on success, or a negative AVERROR code on failure
450  */
451 int swr_inject_silence (SwrContext* s, int count);
452 
453 /**
454  * Gets the delay the next input sample will experience relative to the next output sample.
455  *
456  * Swresample can buffer data if more input has been provided than available
457  * output space, also converting between sample rates needs a delay.
458  * This function returns the sum of all such delays.
459  * The exact delay is not necessarily an integer value in either input or
460  * output sample rate. Especially when downsampling by a large value, the
461  * output sample rate may be a poor choice to represent the delay, similarly
462  * for upsampling and the input sample rate.
463  *
464  * @param s     swr context
465  * @param base  timebase in which the returned delay will be:
466  *              @li if it's set to 1 the returned delay is in seconds
467  *              @li if it's set to 1000 the returned delay is in milliseconds
468  *              @li if it's set to the input sample rate then the returned
469  *                  delay is in input samples
470  *              @li if it's set to the output sample rate then the returned
471  *                  delay is in output samples
472  *              @li if it's the least common multiple of in_sample_rate and
473  *                  out_sample_rate then an exact rounding-free delay will be
474  *                  returned
475  * @returns     the delay in 1 / @c base units.
476  */
477 long swr_get_delay (SwrContext* s, long base);
478 
479 /**
480  * Find an upper bound on the number of samples that the next swr_convert
481  * call will output, if called with in_samples of input samples. This
482  * depends on the internal state, and anything changing the internal state
483  * (like further swr_convert() calls) will may change the number of samples
484  * swr_get_out_samples() returns for the same number of input samples.
485  *
486  * @param in_samples    number of input samples.
487  * @note any call to swr_inject_silence(), swr_convert(), swr_next_pts()
488  *       or swr_set_compensation() invalidates this limit
489  * @note it is recommended to pass the correct available buffer size
490  *       to all functions like swr_convert() even if swr_get_out_samples()
491  *       indicates that less would be used.
492  * @returns an upper bound on the number of samples that the next swr_convert
493  *          will output or a negative value to indicate an error
494  */
495 int swr_get_out_samples (SwrContext* s, int in_samples);
496 
497 /**
498  * @}
499  *
500  * @name Configuration accessors
501  * @{
502  */
503 
504 /**
505  * Return the @ref LIBSWRESAMPLE_VERSION_INT constant.
506  *
507  * This is useful to check if the build-time libswresample has the same version
508  * as the run-time one.
509  *
510  * @returns     the unsigned int-typed version
511  */
512 uint swresample_version ();
513 
514 /**
515  * Return the swr build-time configuration.
516  *
517  * @returns     the build-time @c ./configure flags
518  */
519 const(char)* swresample_configuration ();
520 
521 /**
522  * Return the swr license.
523  *
524  * @returns     the license of libswresample, determined at build-time
525  */
526 const(char)* swresample_license ();
527 
528 /**
529  * @}
530  *
531  * @name AVFrame based API
532  * @{
533  */
534 
535 /**
536  * Convert the samples in the input AVFrame and write them to the output AVFrame.
537  *
538  * Input and output AVFrames must have channel_layout, sample_rate and format set.
539  *
540  * If the output AVFrame does not have the data pointers allocated the nb_samples
541  * field will be set using av_frame_get_buffer()
542  * is called to allocate the frame.
543  *
544  * The output AVFrame can be NULL or have fewer allocated samples than required.
545  * In this case, any remaining samples not written to the output will be added
546  * to an internal FIFO buffer, to be returned at the next call to this function
547  * or to swr_convert().
548  *
549  * If converting sample rate, there may be data remaining in the internal
550  * resampling delay buffer. swr_get_delay() tells the number of
551  * remaining samples. To get this data as output, call this function or
552  * swr_convert() with NULL input.
553  *
554  * If the SwrContext configuration does not match the output and
555  * input AVFrame settings the conversion does not take place and depending on
556  * which AVFrame is not matching AVERROR_OUTPUT_CHANGED, AVERROR_INPUT_CHANGED
557  * or the result of a bitwise-OR of them is returned.
558  *
559  * @see swr_delay()
560  * @see swr_convert()
561  * @see swr_get_delay()
562  *
563  * @param swr             audio resample context
564  * @param output          output AVFrame
565  * @param input           input AVFrame
566  * @return                0 on success, AVERROR on failure or nonmatching
567  *                        configuration.
568  */
569 int swr_convert_frame (SwrContext* swr, AVFrame* output, const(AVFrame)* input);
570 
571 /**
572  * Configure or reconfigure the SwrContext using the information
573  * provided by the AVFrames.
574  *
575  * The original resampling context is reset even on failure.
576  * The function calls swr_close() internally if the context is open.
577  *
578  * @see swr_close();
579  *
580  * @param swr             audio resample context
581  * @param output          output AVFrame
582  * @param input           input AVFrame
583  * @return                0 on success, AVERROR on failure.
584  */
585 int swr_config_frame (SwrContext* swr, const(AVFrame)* out_, const(AVFrame)* in_);
586 
587 /**
588  * @}
589  * @}
590  */
591 
592 /* SWRESAMPLE_SWRESAMPLE_H */