1 module ffmpeg.libswresample.swresample; 2 import std.stdint; 3 import ffmpeg.libavutil.avutil; 4 import ffmpeg.libavutil.frame; 5 import ffmpeg.libavutil.samplefmt; 6 import ffmpeg.libswresample.swresample_version; 7 8 @nogc nothrow extern(C): 9 10 /** 11 * @defgroup libswresample Color conversion and scaling 12 * @{ 13 * 14 * Return the LIBSWRESAMPLE_VERSION_INT constant. 15 */ 16 uint swresample_version(); 17 18 /** 19 * Return the libswresample build-time configuration. 20 */ 21 char* swresample_configuration(); 22 23 /** 24 * Return the libswresample license. 25 */ 26 char* swresample_license(); 27 28 29 30 static if (LIBSWRESAMPLE_VERSION_MAJOR) { 31 enum SWR_CH_MAX = 32; 32 } 33 34 35 36 /* values for the flags, the stuff on the command line is different */ 37 enum SWR_FLAG_RESAMPLE = 1; 38 39 enum SwrDitherType { 40 SWR_DITHER_NONE = 0, 41 SWR_DITHER_RECTANGULAR, 42 SWR_DITHER_TRIANGULAR, 43 SWR_DITHER_TRIANGULAR_HIGHPASS, 44 45 SWR_DITHER_NS = 64, ///< not part of API/ABI 46 SWR_DITHER_NS_LIPSHITZ, 47 SWR_DITHER_NS_F_WEIGHTED, 48 SWR_DITHER_NS_MODIFIED_E_WEIGHTED, 49 SWR_DITHER_NS_IMPROVED_E_WEIGHTED, 50 SWR_DITHER_NS_SHIBATA, 51 SWR_DITHER_NS_LOW_SHIBATA, 52 SWR_DITHER_NS_HIGH_SHIBATA, 53 SWR_DITHER_NB, ///< not part of API/ABI 54 }; 55 56 /** Resampling Engines */ 57 enum SwrEngine { 58 SWR_ENGINE_SWR, /**< SW Resampler */ 59 SWR_ENGINE_SOXR, /**< SoX Resampler */ 60 SWR_ENGINE_NB, ///< not part of API/ABI 61 }; 62 63 /** Resampling Filter Types */ 64 enum SwrFilterType { 65 SWR_FILTER_TYPE_CUBIC, /**< Cubic */ 66 SWR_FILTER_TYPE_BLACKMAN_NUTTALL, /**< Blackman Nuttall windowed sinc */ 67 SWR_FILTER_TYPE_KAISER, /**< Kaiser windowed sinc */ 68 }; 69 70 /** 71 * @} 72 */ 73 74 /** 75 * The libswresample context. Unlike libavcodec and libavformat, this structure 76 * is opaque. This means that if you would like to set options, you must use 77 * the @ref avoptions API and cannot directly set values to members of the 78 * structure. 79 */ 80 struct SwrContext{} 81 82 /** 83 * Get the AVClass for SwrContext. It can be used in combination with 84 * AV_OPT_SEARCH_FAKE_OBJ for examining options. 85 * 86 * @see av_opt_find(). 87 * @return the AVClass of SwrContext 88 */ 89 AVClass *swr_get_class(); 90 91 /** 92 * @name SwrContext constructor functions 93 * @{ 94 */ 95 96 /** 97 * Allocate SwrContext. 98 * 99 * If you use this function you will need to set the parameters (manually or 100 * with swr_alloc_set_opts()) before calling swr_init(). 101 * 102 * @see swr_alloc_set_opts(), swr_init(), swr_free() 103 * @return NULL on error, allocated context otherwise 104 */ 105 SwrContext *swr_alloc(); 106 107 /** 108 * Initialize context after user parameters have been set. 109 * @note The context must be configured using the AVOption API. 110 * 111 * @see av_opt_set_int() 112 * @see av_opt_set_dict() 113 * 114 * @param[in,out] s Swr context to initialize 115 * @return AVERROR error code in case of failure. 116 */ 117 int swr_init(SwrContext *s); 118 119 /** 120 * Check whether an swr context has been initialized or not. 121 * 122 * @param[in] s Swr context to check 123 * @see swr_init() 124 * @return positive if it has been initialized, 0 if not initialized 125 */ 126 int swr_is_initialized(SwrContext *s); 127 128 /** 129 * Allocate SwrContext if needed and set/reset common parameters. 130 * 131 * This function does not require s to be allocated with swr_alloc(). On the 132 * other hand, swr_alloc() can use swr_alloc_set_opts() to set the parameters 133 * on the allocated context. 134 * 135 * @param s existing Swr context if available, or NULL if not 136 * @param out_ch_layout output channel layout (AV_CH_LAYOUT_*) 137 * @param out_sample_fmt output sample format (AV_SAMPLE_FMT_*). 138 * @param out_sample_rate output sample rate (frequency in Hz) 139 * @param in_ch_layout input channel layout (AV_CH_LAYOUT_*) 140 * @param in_sample_fmt input sample format (AV_SAMPLE_FMT_*). 141 * @param in_sample_rate input sample rate (frequency in Hz) 142 * @param log_offset logging level offset 143 * @param log_ctx parent logging context, can be NULL 144 * 145 * @see swr_init(), swr_free() 146 * @return NULL on error, allocated context otherwise 147 */ 148 SwrContext *swr_alloc_set_opts(SwrContext *s, 149 int64_t out_ch_layout, const AVSampleFormat out_sample_fmt, int out_sample_rate, 150 int64_t in_ch_layout, const AVSampleFormat in_sample_fmt, int in_sample_rate, 151 int log_offset, void *log_ctx); 152 153 /** 154 * @} 155 * 156 * @name SwrContext destructor functions 157 * @{ 158 */ 159 160 /** 161 * Free the given SwrContext and set the pointer to NULL. 162 * 163 * @param[in] s a pointer to a pointer to Swr context 164 */ 165 void swr_free(SwrContext **s); 166 167 /** 168 * Closes the context so that swr_is_initialized() returns 0. 169 * 170 * The context can be brought back to life by running swr_init(), 171 * swr_init() can also be used without swr_close(). 172 * This function is mainly provided for simplifying the usecase 173 * where one tries to support libavresample and libswresample. 174 * 175 * @param[in,out] s Swr context to be closed 176 */ 177 void swr_close(SwrContext *s); 178 179 /** 180 * @} 181 * 182 * @name Core conversion functions 183 * @{ 184 */ 185 186 /** Convert audio. 187 * 188 * in and in_count can be set to 0 to flush the last few samples out at the 189 * end. 190 * 191 * If more input is provided than output space, then the input will be buffered. 192 * You can avoid this buffering by using swr_get_out_samples() to retrieve an 193 * upper bound on the required number of output samples for the given number of 194 * input samples. Conversion will run directly without copying whenever possible. 195 * 196 * @param s allocated Swr context, with parameters set 197 * @param out output buffers, only the first one need be set in case of packed audio 198 * @param out_count amount of space available for output in samples per channel 199 * @param in input buffers, only the first one need to be set in case of packed audio 200 * @param in_count number of input samples available in one channel 201 * 202 * @return number of samples output per channel, negative value on error 203 */ 204 int swr_convert(SwrContext *s, uint8_t **out_b, int out_count, 205 const uint8_t **in_b, int in_count); 206 207 /** 208 * Convert the next timestamp from input to output 209 * timestamps are in 1/(in_sample_rate * out_sample_rate) units. 210 * 211 * @note There are 2 slightly differently behaving modes. 212 * @li When automatic timestamp compensation is not used, (min_compensation >= FLT_MAX) 213 * in this case timestamps will be passed through with delays compensated 214 * @li When automatic timestamp compensation is used, (min_compensation < FLT_MAX) 215 * in this case the output timestamps will match output sample numbers. 216 * See ffmpeg-resampler(1) for the two modes of compensation. 217 * 218 * @param s[in] initialized Swr context 219 * @param pts[in] timestamp for the next input sample, INT64_MIN if unknown 220 * @see swr_set_compensation(), swr_drop_output(), and swr_inject_silence() are 221 * function used internally for timestamp compensation. 222 * @return the output timestamp for the next output sample 223 */ 224 int64_t swr_next_pts(SwrContext *s, int64_t pts); 225 226 /** 227 * @} 228 * 229 * @name Low-level option setting functions 230 * These functons provide a means to set low-level options that is not possible 231 * with the AVOption API. 232 * @{ 233 */ 234 235 /** 236 * Activate resampling compensation ("soft" compensation). This function is 237 * internally called when needed in swr_next_pts(). 238 * 239 * @param[in,out] s allocated Swr context. If it is not initialized, 240 * or SWR_FLAG_RESAMPLE is not set, swr_init() is 241 * called with the flag set. 242 * @param[in] sample_delta delta in PTS per sample 243 * @param[in] compensation_distance number of samples to compensate for 244 * @return >= 0 on success, AVERROR error codes if: 245 * @li @c s is NULL, 246 * @li @c compensation_distance is less than 0, 247 * @li @c compensation_distance is 0 but sample_delta is not, 248 * @li compensation unsupported by resampler, or 249 * @li swr_init() fails when called. 250 */ 251 int swr_set_compensation(SwrContext *s, int sample_delta, int compensation_distance); 252 253 /** 254 * Set a customized input channel mapping. 255 * 256 * @param[in,out] s allocated Swr context, not yet initialized 257 * @param[in] channel_map customized input channel mapping (array of channel 258 * indexes, -1 for a muted channel) 259 * @return >= 0 on success, or AVERROR error code in case of failure. 260 */ 261 int swr_set_channel_mapping(SwrContext *s, const int *channel_map); 262 263 /** 264 * Set a customized remix matrix. 265 * 266 * @param s allocated Swr context, not yet initialized 267 * @param matrix remix coefficients; matrix[i + stride * o] is 268 * the weight of input channel i in output channel o 269 * @param stride offset between lines of the matrix 270 * @return >= 0 on success, or AVERROR error code in case of failure. 271 */ 272 int swr_set_matrix(SwrContext *s, const double *matrix, int stride); 273 274 /** 275 * @} 276 * 277 * @name Sample handling functions 278 * @{ 279 */ 280 281 /** 282 * Drops the specified number of output samples. 283 * 284 * This function, along with swr_inject_silence(), is called by swr_next_pts() 285 * if needed for "hard" compensation. 286 * 287 * @param s allocated Swr context 288 * @param count number of samples to be dropped 289 * 290 * @return >= 0 on success, or a negative AVERROR code on failure 291 */ 292 int swr_drop_output(SwrContext *s, int count); 293 294 /** 295 * Injects the specified number of silence samples. 296 * 297 * This function, along with swr_drop_output(), is called by swr_next_pts() 298 * if needed for "hard" compensation. 299 * 300 * @param s allocated Swr context 301 * @param count number of samples to be dropped 302 * 303 * @return >= 0 on success, or a negative AVERROR code on failure 304 */ 305 int swr_inject_silence(SwrContext *s, int count); 306 307 /** 308 * Gets the delay the next input sample will experience relative to the next output sample. 309 * 310 * Swresample can buffer data if more input has been provided than available 311 * output space, also converting between sample rates needs a delay. 312 * This function returns the sum of all such delays. 313 * The exact delay is not necessarily an integer value in either input or 314 * output sample rate. Especially when downsampling by a large value, the 315 * output sample rate may be a poor choice to represent the delay, similarly 316 * for upsampling and the input sample rate. 317 * 318 * @param s swr context 319 * @param base timebase in which the returned delay will be: 320 * @li if it's set to 1 the returned delay is in seconds 321 * @li if it's set to 1000 the returned delay is in milliseconds 322 * @li if it's set to the input sample rate then the returned 323 * delay is in input samples 324 * @li if it's set to the output sample rate then the returned 325 * delay is in output samples 326 * @li if it's the least common multiple of in_sample_rate and 327 * out_sample_rate then an exact rounding-free delay will be 328 * returned 329 * @returns the delay in 1 / @c base units. 330 */ 331 int64_t swr_get_delay(SwrContext *s, int64_t base); 332 333 /** 334 * Find an upper bound on the number of samples that the next swr_convert 335 * call will output, if called with in_samples of input samples. This 336 * depends on the internal state, and anything changing the internal state 337 * (like further swr_convert() calls) will may change the number of samples 338 * swr_get_out_samples() returns for the same number of input samples. 339 * 340 * @param in_samples number of input samples. 341 * @note any call to swr_inject_silence(), swr_convert(), swr_next_pts() 342 * or swr_set_compensation() invalidates this limit 343 * @note it is recommended to pass the correct available buffer size 344 * to all functions like swr_convert() even if swr_get_out_samples() 345 * indicates that less would be used. 346 * @returns an upper bound on the number of samples that the next swr_convert 347 * will output or a negative value to indicate an error 348 */ 349 int swr_get_out_samples(SwrContext *s, int in_samples); 350 351 /** 352 * @} 353 * 354 * @name AVFrame based API 355 * @{ 356 */ 357 358 /** 359 * Convert the samples in the input AVFrame and write them to the output AVFrame. 360 * 361 * Input and output AVFrames must have channel_layout, sample_rate and format set. 362 * 363 * If the output AVFrame does not have the data pointers allocated the nb_samples 364 * field will be set using av_frame_get_buffer() 365 * is called to allocate the frame. 366 * 367 * The output AVFrame can be NULL or have fewer allocated samples than required. 368 * In this case, any remaining samples not written to the output will be added 369 * to an internal FIFO buffer, to be returned at the next call to this function 370 * or to swr_convert(). 371 * 372 * If converting sample rate, there may be data remaining in the internal 373 * resampling delay buffer. swr_get_delay() tells the number of 374 * remaining samples. To get this data as output, call this function or 375 * swr_convert() with NULL input. 376 * 377 * If the SwrContext configuration does not match the output and 378 * input AVFrame settings the conversion does not take place and depending on 379 * which AVFrame is not matching AVERROR_OUTPUT_CHANGED, AVERROR_INPUT_CHANGED 380 * or the result of a bitwise-OR of them is returned. 381 * 382 * @see swr_delay() 383 * @see swr_convert() 384 * @see swr_get_delay() 385 * 386 * @param swr audio resample context 387 * @param output output AVFrame 388 * @param input input AVFrame 389 * @return 0 on success, AVERROR on failure or nonmatching 390 * configuration. 391 */ 392 int swr_convert_frame(SwrContext *swr, 393 AVFrame *output, const AVFrame *input); 394 395 /** 396 * Configure or reconfigure the SwrContext using the information 397 * provided by the AVFrames. 398 * 399 * The original resampling context is reset even on failure. 400 * The function calls swr_close() internally if the context is open. 401 * 402 * @see swr_close(); 403 * 404 * @param swr audio resample context 405 * @param output output AVFrame 406 * @param input input AVFrame 407 * @return 0 on success, AVERROR on failure. 408 */ 409 int swr_config_frame(SwrContext *swr, const AVFrame *out_frame, const AVFrame *in_frame); 410 411 /** 412 * @} 413 * @} 414 */