swresample.h 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226
  1. /*
  2. * Copyright (C) 2011-2012 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. * @file
  22. * libswresample public header
  23. */
  24. #ifndef SWR_H
  25. #define SWR_H
  26. #include <inttypes.h>
  27. #include "libavutil/samplefmt.h"
  28. #define LIBSWRESAMPLE_VERSION_MAJOR 0
  29. #define LIBSWRESAMPLE_VERSION_MINOR 15
  30. #define LIBSWRESAMPLE_VERSION_MICRO 100
  31. #define LIBSWRESAMPLE_VERSION_INT AV_VERSION_INT(LIBSWRESAMPLE_VERSION_MAJOR, \
  32. LIBSWRESAMPLE_VERSION_MINOR, \
  33. LIBSWRESAMPLE_VERSION_MICRO)
  34. #if LIBSWRESAMPLE_VERSION_MAJOR < 1
  35. #define SWR_CH_MAX 32 ///< Maximum number of channels
  36. #endif
  37. #define SWR_FLAG_RESAMPLE 1 ///< Force resampling even if equal sample rate
  38. //TODO use int resample ?
  39. //long term TODO can we enable this dynamically?
  40. enum SwrDitherType {
  41. SWR_DITHER_NONE = 0,
  42. SWR_DITHER_RECTANGULAR,
  43. SWR_DITHER_TRIANGULAR,
  44. SWR_DITHER_TRIANGULAR_HIGHPASS,
  45. SWR_DITHER_NB, ///< not part of API/ABI
  46. };
  47. /** Resampling Filter Types */
  48. enum SwrFilterType {
  49. SWR_FILTER_TYPE_CUBIC, /**< Cubic */
  50. SWR_FILTER_TYPE_BLACKMAN_NUTTALL, /**< Blackman Nuttall Windowed Sinc */
  51. SWR_FILTER_TYPE_KAISER, /**< Kaiser Windowed Sinc */
  52. };
  53. typedef struct SwrContext SwrContext;
  54. /**
  55. * Get the AVClass for swrContext. It can be used in combination with
  56. * AV_OPT_SEARCH_FAKE_OBJ for examining options.
  57. *
  58. * @see av_opt_find().
  59. */
  60. const AVClass *swr_get_class(void);
  61. /**
  62. * Allocate SwrContext.
  63. *
  64. * If you use this function you will need to set the parameters (manually or
  65. * with swr_alloc_set_opts()) before calling swr_init().
  66. *
  67. * @see swr_alloc_set_opts(), swr_init(), swr_free()
  68. * @return NULL on error, allocated context otherwise
  69. */
  70. struct SwrContext *swr_alloc(void);
  71. /**
  72. * Initialize context after user parameters have been set.
  73. *
  74. * @return AVERROR error code in case of failure.
  75. */
  76. int swr_init(struct SwrContext *s);
  77. /**
  78. * Allocate SwrContext if needed and set/reset common parameters.
  79. *
  80. * This function does not require s to be allocated with swr_alloc(). On the
  81. * other hand, swr_alloc() can use swr_alloc_set_opts() to set the parameters
  82. * on the allocated context.
  83. *
  84. * @param s Swr context, can be NULL
  85. * @param out_ch_layout output channel layout (AV_CH_LAYOUT_*)
  86. * @param out_sample_fmt output sample format (AV_SAMPLE_FMT_*).
  87. * @param out_sample_rate output sample rate (frequency in Hz)
  88. * @param in_ch_layout input channel layout (AV_CH_LAYOUT_*)
  89. * @param in_sample_fmt input sample format (AV_SAMPLE_FMT_*).
  90. * @param in_sample_rate input sample rate (frequency in Hz)
  91. * @param log_offset logging level offset
  92. * @param log_ctx parent logging context, can be NULL
  93. *
  94. * @see swr_init(), swr_free()
  95. * @return NULL on error, allocated context otherwise
  96. */
  97. struct SwrContext *swr_alloc_set_opts(struct SwrContext *s,
  98. int64_t out_ch_layout, enum AVSampleFormat out_sample_fmt, int out_sample_rate,
  99. int64_t in_ch_layout, enum AVSampleFormat in_sample_fmt, int in_sample_rate,
  100. int log_offset, void *log_ctx);
  101. /**
  102. * Free the given SwrContext and set the pointer to NULL.
  103. */
  104. void swr_free(struct SwrContext **s);
  105. /**
  106. * Convert audio.
  107. *
  108. * in and in_count can be set to 0 to flush the last few samples out at the
  109. * end.
  110. *
  111. * If more input is provided than output space then the input will be buffered.
  112. * You can avoid this buffering by providing more output space than input.
  113. * Convertion will run directly without copying whenever possible.
  114. *
  115. * @param s allocated Swr context, with parameters set
  116. * @param out output buffers, only the first one need be set in case of packed audio
  117. * @param out_count amount of space available for output in samples per channel
  118. * @param in input buffers, only the first one need to be set in case of packed audio
  119. * @param in_count number of input samples available in one channel
  120. *
  121. * @return number of samples output per channel, negative value on error
  122. */
  123. int swr_convert(struct SwrContext *s, uint8_t **out, int out_count,
  124. const uint8_t **in , int in_count);
  125. /**
  126. * Convert the next timestamp from input to output
  127. * timestampe are in 1/(in_sample_rate * out_sample_rate) units.
  128. *
  129. * @note There are 2 slightly differently behaving modes.
  130. * First is when automatic timestamp compensation is not used, (min_compensation >= FLT_MAX)
  131. * in this case timestamps will be passed through with delays compensated
  132. * Second is when automatic timestamp compensation is used, (min_compensation < FLT_MAX)
  133. * in this case the output timestamps will match output sample numbers
  134. *
  135. * @param pts timstamp for the next input sample, INT64_MIN if unknown
  136. * @returns the output timestamp for the next output sample
  137. */
  138. int64_t swr_next_pts(struct SwrContext *s, int64_t pts);
  139. /**
  140. * Activate resampling compensation.
  141. */
  142. int swr_set_compensation(struct SwrContext *s, int sample_delta, int compensation_distance);
  143. /**
  144. * Set a customized input channel mapping.
  145. *
  146. * @param s allocated Swr context, not yet initialized
  147. * @param channel_map customized input channel mapping (array of channel
  148. * indexes, -1 for a muted channel)
  149. * @return AVERROR error code in case of failure.
  150. */
  151. int swr_set_channel_mapping(struct SwrContext *s, const int *channel_map);
  152. /**
  153. * Set a customized remix matrix.
  154. *
  155. * @param s allocated Swr context, not yet initialized
  156. * @param matrix remix coefficients; matrix[i + stride * o] is
  157. * the weight of input channel i in output channel o
  158. * @param stride offset between lines of the matrix
  159. * @return AVERROR error code in case of failure.
  160. */
  161. int swr_set_matrix(struct SwrContext *s, const double *matrix, int stride);
  162. /**
  163. * Drops the specified number of output samples.
  164. */
  165. int swr_drop_output(struct SwrContext *s, int count);
  166. /**
  167. * Injects the specified number of silence samples.
  168. */
  169. int swr_inject_silence(struct SwrContext *s, int count);
  170. /**
  171. * Gets the delay the next input sample will experience relative to the next output sample.
  172. *
  173. * Swresample can buffer data if more input has been provided than available
  174. * output space, also converting between sample rates needs a delay.
  175. * This function returns the sum of all such delays.
  176. *
  177. * @param s swr context
  178. * @param base timebase in which the returned delay will be
  179. * if its set to 1 the returned delay is in seconds
  180. * if its set to 1000 the returned delay is in milli seconds
  181. * if its set to the input sample rate then the returned delay is in input samples
  182. * if its set to the output sample rate then the returned delay is in output samples
  183. * an exact rounding free delay can be found by using LCM(in_sample_rate, out_sample_rate)
  184. * @returns the delay in 1/base units.
  185. */
  186. int64_t swr_get_delay(struct SwrContext *s, int64_t base);
  187. /**
  188. * Return the LIBSWRESAMPLE_VERSION_INT constant.
  189. */
  190. unsigned swresample_version(void);
  191. /**
  192. * Return the swr build-time configuration.
  193. */
  194. const char *swresample_configuration(void);
  195. /**
  196. * Return the swr license.
  197. */
  198. const char *swresample_license(void);
  199. #endif