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