yading@11: /* yading@11: * Copyright (C) 2011-2013 Michael Niedermayer (michaelni@gmx.at) yading@11: * yading@11: * This file is part of libswresample yading@11: * yading@11: * libswresample is free software; you can redistribute it and/or yading@11: * modify it under the terms of the GNU Lesser General Public yading@11: * License as published by the Free Software Foundation; either yading@11: * version 2.1 of the License, or (at your option) any later version. yading@11: * yading@11: * libswresample is distributed in the hope that it will be useful, yading@11: * but WITHOUT ANY WARRANTY; without even the implied warranty of yading@11: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU yading@11: * Lesser General Public License for more details. yading@11: * yading@11: * You should have received a copy of the GNU Lesser General Public yading@11: * License along with libswresample; if not, write to the Free Software yading@11: * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA yading@11: */ yading@11: yading@11: #ifndef SWR_INTERNAL_H yading@11: #define SWR_INTERNAL_H yading@11: yading@11: #include "swresample.h" yading@11: #include "libavutil/channel_layout.h" yading@11: #include "config.h" yading@11: yading@11: #define SQRT3_2 1.22474487139158904909 /* sqrt(3/2) */ yading@11: yading@11: #define NS_TAPS 20 yading@11: yading@11: #if ARCH_X86_64 yading@11: typedef int64_t integer; yading@11: #else yading@11: typedef int integer; yading@11: #endif yading@11: yading@11: typedef void (mix_1_1_func_type)(void *out, const void *in, void *coeffp, integer index, integer len); yading@11: typedef void (mix_2_1_func_type)(void *out, const void *in1, const void *in2, void *coeffp, integer index1, integer index2, integer len); yading@11: yading@11: typedef void (mix_any_func_type)(uint8_t **out, const uint8_t **in1, void *coeffp, integer len); yading@11: yading@11: typedef struct AudioData{ yading@11: uint8_t *ch[SWR_CH_MAX]; ///< samples buffer per channel yading@11: uint8_t *data; ///< samples buffer yading@11: int ch_count; ///< number of channels yading@11: int bps; ///< bytes per sample yading@11: int count; ///< number of samples yading@11: int planar; ///< 1 if planar audio, 0 otherwise yading@11: enum AVSampleFormat fmt; ///< sample format yading@11: } AudioData; yading@11: yading@11: struct DitherContext { yading@11: enum SwrDitherType method; yading@11: int noise_pos; yading@11: float scale; yading@11: float noise_scale; ///< Noise scale yading@11: int ns_taps; ///< Noise shaping dither taps yading@11: float ns_scale; ///< Noise shaping dither scale yading@11: float ns_scale_1; ///< Noise shaping dither scale^-1 yading@11: int ns_pos; ///< Noise shaping dither position yading@11: float ns_coeffs[NS_TAPS]; ///< Noise shaping filter coefficients yading@11: float ns_errors[SWR_CH_MAX][2*NS_TAPS]; yading@11: AudioData noise; ///< noise used for dithering yading@11: AudioData temp; ///< temporary storage when writing into the input buffer isnt possible yading@11: int output_sample_bits; ///< the number of used output bits, needed to scale dither correctly yading@11: }; yading@11: yading@11: struct SwrContext { yading@11: const AVClass *av_class; ///< AVClass used for AVOption and av_log() yading@11: int log_level_offset; ///< logging level offset yading@11: void *log_ctx; ///< parent logging context yading@11: enum AVSampleFormat in_sample_fmt; ///< input sample format yading@11: enum AVSampleFormat int_sample_fmt; ///< internal sample format (AV_SAMPLE_FMT_FLTP or AV_SAMPLE_FMT_S16P) yading@11: enum AVSampleFormat out_sample_fmt; ///< output sample format yading@11: int64_t in_ch_layout; ///< input channel layout yading@11: int64_t out_ch_layout; ///< output channel layout yading@11: int in_sample_rate; ///< input sample rate yading@11: int out_sample_rate; ///< output sample rate yading@11: int flags; ///< miscellaneous flags such as SWR_FLAG_RESAMPLE yading@11: float slev; ///< surround mixing level yading@11: float clev; ///< center mixing level yading@11: float lfe_mix_level; ///< LFE mixing level yading@11: float rematrix_volume; ///< rematrixing volume coefficient yading@11: enum AVMatrixEncoding matrix_encoding; /**< matrixed stereo encoding */ yading@11: const int *channel_map; ///< channel index (or -1 if muted channel) map yading@11: int used_ch_count; ///< number of used input channels (mapped channel count if channel_map, otherwise in.ch_count) yading@11: enum SwrEngine engine; yading@11: yading@11: struct DitherContext dither; yading@11: yading@11: int filter_size; /**< length of each FIR filter in the resampling filterbank relative to the cutoff frequency */ yading@11: int phase_shift; /**< log2 of the number of entries in the resampling polyphase filterbank */ yading@11: int linear_interp; /**< if 1 then the resampling FIR filter will be linearly interpolated */ yading@11: double cutoff; /**< resampling cutoff frequency (swr: 6dB point; soxr: 0dB point). 1.0 corresponds to half the output sample rate */ yading@11: enum SwrFilterType filter_type; /**< swr resampling filter type */ yading@11: int kaiser_beta; /**< swr beta value for Kaiser window (only applicable if filter_type == AV_FILTER_TYPE_KAISER) */ yading@11: double precision; /**< soxr resampling precision (in bits) */ yading@11: int cheby; /**< soxr: if 1 then passband rolloff will be none (Chebyshev) & irrational ratio approximation precision will be higher */ yading@11: yading@11: float min_compensation; ///< swr minimum below which no compensation will happen yading@11: float min_hard_compensation; ///< swr minimum below which no silence inject / sample drop will happen yading@11: float soft_compensation_duration; ///< swr duration over which soft compensation is applied yading@11: float max_soft_compensation; ///< swr maximum soft compensation in seconds over soft_compensation_duration yading@11: float async; ///< swr simple 1 parameter async, similar to ffmpegs -async yading@11: int64_t firstpts_in_samples; ///< swr first pts in samples yading@11: yading@11: int resample_first; ///< 1 if resampling must come first, 0 if rematrixing yading@11: int rematrix; ///< flag to indicate if rematrixing is needed (basically if input and output layouts mismatch) yading@11: int rematrix_custom; ///< flag to indicate that a custom matrix has been defined yading@11: yading@11: AudioData in; ///< input audio data yading@11: AudioData postin; ///< post-input audio data: used for rematrix/resample yading@11: AudioData midbuf; ///< intermediate audio data (postin/preout) yading@11: AudioData preout; ///< pre-output audio data: used for rematrix/resample yading@11: AudioData out; ///< converted output audio data yading@11: AudioData in_buffer; ///< cached audio data (convert and resample purpose) yading@11: AudioData silence; ///< temporary with silence yading@11: AudioData drop_temp; ///< temporary used to discard output yading@11: int in_buffer_index; ///< cached buffer position yading@11: int in_buffer_count; ///< cached buffer length yading@11: int resample_in_constraint; ///< 1 if the input end was reach before the output end, 0 otherwise yading@11: int flushed; ///< 1 if data is to be flushed and no further input is expected yading@11: int64_t outpts; ///< output PTS yading@11: int64_t firstpts; ///< first PTS yading@11: int drop_output; ///< number of output samples to drop yading@11: yading@11: struct AudioConvert *in_convert; ///< input conversion context yading@11: struct AudioConvert *out_convert; ///< output conversion context yading@11: struct AudioConvert *full_convert; ///< full conversion context (single conversion for input and output) yading@11: struct ResampleContext *resample; ///< resampling context yading@11: struct Resampler const *resampler; ///< resampler virtual function table yading@11: yading@11: float matrix[SWR_CH_MAX][SWR_CH_MAX]; ///< floating point rematrixing coefficients yading@11: uint8_t *native_matrix; yading@11: uint8_t *native_one; yading@11: uint8_t *native_simd_matrix; yading@11: int32_t matrix32[SWR_CH_MAX][SWR_CH_MAX]; ///< 17.15 fixed point rematrixing coefficients yading@11: uint8_t matrix_ch[SWR_CH_MAX][SWR_CH_MAX+1]; ///< Lists of input channels per output channel that have non zero rematrixing coefficients yading@11: mix_1_1_func_type *mix_1_1_f; yading@11: mix_1_1_func_type *mix_1_1_simd; yading@11: yading@11: mix_2_1_func_type *mix_2_1_f; yading@11: mix_2_1_func_type *mix_2_1_simd; yading@11: yading@11: mix_any_func_type *mix_any_f; yading@11: yading@11: /* TODO: callbacks for ASM optimizations */ yading@11: }; yading@11: yading@11: typedef struct ResampleContext * (* resample_init_func)(struct ResampleContext *c, int out_rate, int in_rate, int filter_size, int phase_shift, int linear, yading@11: double cutoff, enum AVSampleFormat format, enum SwrFilterType filter_type, int kaiser_beta, double precision, int cheby); yading@11: typedef void (* resample_free_func)(struct ResampleContext **c); yading@11: typedef int (* multiple_resample_func)(struct ResampleContext *c, AudioData *dst, int dst_size, AudioData *src, int src_size, int *consumed); yading@11: typedef int (* resample_flush_func)(struct SwrContext *c); yading@11: typedef int (* set_compensation_func)(struct ResampleContext *c, int sample_delta, int compensation_distance); yading@11: typedef int64_t (* get_delay_func)(struct SwrContext *s, int64_t base); yading@11: yading@11: struct Resampler { yading@11: resample_init_func init; yading@11: resample_free_func free; yading@11: multiple_resample_func multiple_resample; yading@11: resample_flush_func flush; yading@11: set_compensation_func set_compensation; yading@11: get_delay_func get_delay; yading@11: }; yading@11: yading@11: extern struct Resampler const swri_resampler; yading@11: yading@11: int swri_realloc_audio(AudioData *a, int count); yading@11: int swri_resample_int16(struct ResampleContext *c, int16_t *dst, const int16_t *src, int *consumed, int src_size, int dst_size, int update_ctx); yading@11: int swri_resample_int32(struct ResampleContext *c, int32_t *dst, const int32_t *src, int *consumed, int src_size, int dst_size, int update_ctx); yading@11: int swri_resample_float(struct ResampleContext *c, float *dst, const float *src, int *consumed, int src_size, int dst_size, int update_ctx); yading@11: int swri_resample_double(struct ResampleContext *c,double *dst, const double *src, int *consumed, int src_size, int dst_size, int update_ctx); yading@11: yading@11: void swri_noise_shaping_int16 (SwrContext *s, AudioData *dsts, const AudioData *srcs, const AudioData *noises, int count); yading@11: void swri_noise_shaping_int32 (SwrContext *s, AudioData *dsts, const AudioData *srcs, const AudioData *noises, int count); yading@11: void swri_noise_shaping_float (SwrContext *s, AudioData *dsts, const AudioData *srcs, const AudioData *noises, int count); yading@11: void swri_noise_shaping_double(SwrContext *s, AudioData *dsts, const AudioData *srcs, const AudioData *noises, int count); yading@11: yading@11: int swri_rematrix_init(SwrContext *s); yading@11: void swri_rematrix_free(SwrContext *s); yading@11: int swri_rematrix(SwrContext *s, AudioData *out, AudioData *in, int len, int mustcopy); yading@11: void swri_rematrix_init_x86(struct SwrContext *s); yading@11: yading@11: void swri_get_dither(SwrContext *s, void *dst, int len, unsigned seed, enum AVSampleFormat noise_fmt); yading@11: int swri_dither_init(SwrContext *s, enum AVSampleFormat out_fmt, enum AVSampleFormat in_fmt); yading@11: yading@11: void swri_audio_convert_init_arm(struct AudioConvert *ac, yading@11: enum AVSampleFormat out_fmt, yading@11: enum AVSampleFormat in_fmt, yading@11: int channels); yading@11: void swri_audio_convert_init_x86(struct AudioConvert *ac, yading@11: enum AVSampleFormat out_fmt, yading@11: enum AVSampleFormat in_fmt, yading@11: int channels); yading@11: #endif