annotate ffmpeg/libavresample/avresample.h @ 13:844d341cf643 tip

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