78 #define MUTE_THRESHOLD_SEC 0.000333
82 #define S16_SCALE 32753.0f
85 #define LFG_SCALE (1.0f / (2.0f * INT32_MAX))
90 2.2374f, -0.7339f, -0.1251f, -0.6033f
94 0.9030f, 0.0116f, -0.5853f, -0.2571f
98 2.2061f, -0.4707f, -0.2534f, -0.6213f
102 1.0587f, 0.0676f, -0.6054f, -0.2738f
117 for (
i = 0;
i <
len;
i++) {
131 #define SQRT_1_6 0.40824829046386301723f
138 for (
i = 0;
i <
len - 2;
i++)
146 int nb_samples =
FFALIGN(min_samples, 16) + 16;
147 int buf_samples = nb_samples *
149 unsigned int *noise_buf_ui;
152 state->noise_buf_size =
state->noise_buf_ptr = 0;
155 if (!
state->noise_buf)
158 noise_buf_ui = (
unsigned int *)
state->noise_buf;
161 for (
i = 0;
i < buf_samples;
i++)
164 c->ddsp.dither_int_to_float(
state->noise_buf, noise_buf_ui, nb_samples);
173 int16_t *dst,
const float *
src,
179 if (
state->mute >
c->mute_reset_threshold)
180 memset(
state->dither_a, 0,
sizeof(
state->dither_a));
182 for (
i = 0;
i < nb_samples;
i++) {
186 for (j = 0; j < 4; j++) {
187 err +=
c->ns_coef_b[j] *
state->dither_b[j] -
188 c->ns_coef_a[j] *
state->dither_a[j];
190 for (j = 3; j > 0; j--) {
194 state->dither_a[0] = err;
197 if (
state->mute >
c->mute_dither_threshold) {
199 state->dither_b[0] = 0;
215 int aligned_samples =
FFALIGN(nb_samples, 16);
220 if (
state->noise_buf_size < aligned_samples) {
224 }
else if (
state->noise_buf_size -
state->noise_buf_ptr < aligned_samples) {
225 state->noise_buf_ptr = 0;
231 c->quantize(dst[ch],
src[ch],
233 FFALIGN(nb_samples,
c->samples_align));
236 state->noise_buf_ptr += aligned_samples;
262 flt_data =
c->flt_data;
270 }
else if (
c->apply_map) {
282 int aligned_len =
FFALIGN(
src->nb_samples,
c->ddsp.samples_align);
284 if (!(ptr_align %
c->ddsp.ptr_align) && samples_align >= aligned_len) {
285 c->quantize =
c->ddsp.quantize;
286 c->samples_align =
c->ddsp.samples_align;
289 c->samples_align = 1;
294 (
float *
const *)flt_data->
data,
src->channels,
299 c->s16_data->nb_samples =
src->nb_samples;
323 for (ch = 0; ch <
c->channels; ch++)
365 c->apply_map = apply_map;
372 "for triangular_ns dither. using triangular_hp instead.\n");
393 "dither s16 buffer");
405 "dither flt buffer");
424 c->mute_reset_threshold =
c->mute_dither_threshold * 4;
430 state->mute =
c->mute_reset_threshold + 1;
Macro definitions for various function/variable attributes.
void ff_audio_convert_free(AudioConvert **ac)
Free AudioConvert.
AudioConvert * ff_audio_convert_alloc(AVAudioResampleContext *avr, enum AVSampleFormat out_fmt, enum AVSampleFormat in_fmt, int channels, int sample_rate, int apply_map)
Allocate and initialize AudioConvert context for sample format conversion.
int ff_audio_convert(AudioConvert *ac, AudioData *out, AudioData *in)
Convert audio data from one sample format to another.
AudioData * ff_audio_data_alloc(int channels, int nb_samples, enum AVSampleFormat sample_fmt, const char *name)
Allocate AudioData.
int ff_audio_data_realloc(AudioData *a, int nb_samples)
Reallocate AudioData.
int ff_audio_data_copy(AudioData *dst, AudioData *src, ChannelMapInfo *map)
Copy data from one AudioData to another.
void ff_audio_data_free(AudioData **a)
Free AudioData.
AV_RESAMPLE_DITHER_TRIANGULAR_HP
Triangular Dither with High Pass.
AV_RESAMPLE_DITHER_RECTANGULAR
Rectangular Dither.
AV_RESAMPLE_DITHER_TRIANGULAR_NS
Triangular Dither with Noise Shaping.
common internal and external API header
void ff_dither_init_x86(DitherDSPContext *ddsp, enum AVResampleDitherMethod method)
enum attribute_deprecated AVResampleDitherMethod
#define AV_LOG_WARNING
Something somehow does not look correct.
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
void * av_mallocz(size_t size)
Allocate a memory block with alignment suitable for all memory accesses (including vectors if availab...
int av_get_bytes_per_sample(enum AVSampleFormat sample_fmt)
Return number of bytes per sample.
const char * av_get_sample_fmt_name(enum AVSampleFormat sample_fmt)
Return the name of sample_fmt, or NULL if sample_fmt is not recognized.
enum AVSampleFormat av_get_packed_sample_fmt(enum AVSampleFormat sample_fmt)
Get the packed alternative form of the given sample format.
AVSampleFormat
Audio sample formats.
@ AV_SAMPLE_FMT_FLTP
float, planar
@ AV_SAMPLE_FMT_S16P
signed 16 bits, planar
@ AV_SAMPLE_FMT_S16
signed 16 bits
av_cold void av_lfg_init(AVLFG *c, unsigned int seed)
static unsigned int av_lfg_get(AVLFG *c)
Get the next random unsigned 32-bit number using an ALFG.
static const float ns_44_coef_a[4]
void ff_dither_free(DitherContext **cp)
Free a DitherContext.
static const float ns_44_coef_b[4]
static int convert_samples(DitherContext *c, int16_t **dst, float *const *src, int channels, int nb_samples)
static void quantize_triangular_ns(DitherContext *c, DitherState *state, int16_t *dst, const float *src, int nb_samples)
static int generate_dither_noise(DitherContext *c, DitherState *state, int min_samples)
int ff_convert_dither(DitherContext *c, AudioData *dst, AudioData *src)
Convert audio sample format with dithering.
static void dither_int_to_float_triangular_c(float *dst, int *src0, int len)
static void dither_int_to_float_rectangular_c(float *dst, int *src, int len)
static void dither_highpass_filter(float *src, int len)
#define MUTE_THRESHOLD_SEC
DitherContext * ff_dither_alloc(AVAudioResampleContext *avr, enum AVSampleFormat out_fmt, enum AVSampleFormat in_fmt, int channels, int sample_rate, int apply_map)
Allocate and initialize a DitherContext.
static const float ns_48_coef_b[4]
static av_cold void dither_init(DitherDSPContext *ddsp, enum AVResampleDitherMethod method)
static void quantize_c(int16_t *dst, const float *src, float *dither, int len)
static const float ns_48_coef_a[4]
common internal API header
Memory handling functions.
typedef void(RENAME(mix_any_func_type))
ChannelMapInfo ch_map_info
enum AVResampleDitherMethod dither_method
dither method
Context structure for the Lagged Fibonacci PRNG.
Audio buffer used for intermediate storage between conversion phases.
int ptr_align
minimum data pointer alignment
enum AVSampleFormat sample_fmt
sample format
uint8_t * data[AVRESAMPLE_MAX_CHANNELS]
data plane pointers
int samples_align
allocated samples alignment
enum AVResampleDitherMethod method
int mute_dither_threshold
void(* quantize)(int16_t *dst, const float *src, float *dither, int len)
ChannelMapInfo * ch_map_info
int ptr_align
src and dst constraints for quantize()
int samples_align
len constraints for quantize()
void(* dither_int_to_float)(float *dst, int *src0, int len)
Convert dither noise from int to float with triangular distribution.
void(* quantize)(int16_t *dst, const float *src, float *dither, int len)
Convert samples from flt to s16 with added dither noise.
static const uint8_t dither[8][8]