FFmpeg  4.0
Data Structures | Macros | Functions | Variables
dither.c File Reference

Dithered Audio Sample Quantization. More...

#include <math.h>
#include <stdint.h>
#include "libavutil/attributes.h"
#include "libavutil/common.h"
#include "libavutil/lfg.h"
#include "libavutil/mem.h"
#include "libavutil/samplefmt.h"
#include "audio_convert.h"
#include "dither.h"
#include "internal.h"

Go to the source code of this file.

Data Structures

struct  DitherState
 
struct  DitherContext
 

Macros

#define MUTE_THRESHOLD_SEC   0.000333
 
#define S16_SCALE   32753.0f
 
#define LFG_SCALE   (1.0f / (2.0f * INT32_MAX))
 
#define SQRT_1_6   0.40824829046386301723f
 

Functions

static void dither_int_to_float_rectangular_c (float *dst, int *src, int len)
 
static void dither_int_to_float_triangular_c (float *dst, int *src0, int len)
 
static void quantize_c (int16_t *dst, const float *src, float *dither, int len)
 
static void dither_highpass_filter (float *src, int len)
 
static int generate_dither_noise (DitherContext *c, DitherState *state, int min_samples)
 
static void quantize_triangular_ns (DitherContext *c, DitherState *state, int16_t *dst, const float *src, int nb_samples)
 
static int convert_samples (DitherContext *c, int16_t **dst, float *const *src, int channels, int nb_samples)
 
int ff_convert_dither (DitherContext *c, AudioData *dst, AudioData *src)
 Convert audio sample format with dithering. More...
 
void ff_dither_free (DitherContext **cp)
 Free a DitherContext. More...
 
static av_cold void dither_init (DitherDSPContext *ddsp, enum AVResampleDitherMethod method)
 
DitherContextff_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. More...
 

Variables

static const float ns_48_coef_b [4]
 
static const float ns_48_coef_a [4]
 
static const float ns_44_coef_b [4]
 
static const float ns_44_coef_a [4]
 

Detailed Description

Dithered Audio Sample Quantization.

Converts from dbl, flt, or s32 to s16 using dithering.

Definition in file dither.c.

Macro Definition Documentation

◆ MUTE_THRESHOLD_SEC

#define MUTE_THRESHOLD_SEC   0.000333

Definition at line 78 of file dither.c.

Referenced by ff_dither_alloc().

◆ S16_SCALE

#define S16_SCALE   32753.0f

Definition at line 82 of file dither.c.

Referenced by quantize_c(), and quantize_triangular_ns().

◆ LFG_SCALE

#define LFG_SCALE   (1.0f / (2.0f * INT32_MAX))

Definition at line 85 of file dither.c.

Referenced by dither_int_to_float_rectangular_c(), and dither_int_to_float_triangular_c().

◆ SQRT_1_6

#define SQRT_1_6   0.40824829046386301723f

Definition at line 131 of file dither.c.

Referenced by dither_highpass_filter().

Function Documentation

◆ dither_int_to_float_rectangular_c()

static void dither_int_to_float_rectangular_c ( float *  dst,
int src,
int  len 
)
static

Definition at line 105 of file dither.c.

Referenced by dither_init().

◆ dither_int_to_float_triangular_c()

static void dither_int_to_float_triangular_c ( float *  dst,
int src0,
int  len 
)
static

Definition at line 112 of file dither.c.

Referenced by dither_init().

◆ quantize_c()

static void quantize_c ( int16_t *  dst,
const float *  src,
float *  dither,
int  len 
)
static

Definition at line 124 of file dither.c.

Referenced by dither_init(), and ff_convert_dither().

◆ dither_highpass_filter()

static void dither_highpass_filter ( float *  src,
int  len 
)
static

Definition at line 133 of file dither.c.

Referenced by generate_dither_noise().

◆ generate_dither_noise()

static int generate_dither_noise ( DitherContext c,
DitherState state,
int  min_samples 
)
static

Definition at line 142 of file dither.c.

Referenced by convert_samples(), and ff_dither_alloc().

◆ quantize_triangular_ns()

static void quantize_triangular_ns ( DitherContext c,
DitherState state,
int16_t *  dst,
const float *  src,
int  nb_samples 
)
static

Definition at line 172 of file dither.c.

Referenced by convert_samples().

◆ convert_samples()

static int convert_samples ( DitherContext c,
int16_t **  dst,
float *const *  src,
int  channels,
int  nb_samples 
)
static

Definition at line 211 of file dither.c.

Referenced by ff_convert_dither().

◆ ff_convert_dither()

int ff_convert_dither ( DitherContext c,
AudioData dst,
AudioData src 
)

Convert audio sample format with dithering.

Parameters
cDitherContext
dstdestination audio data
srcsource audio data
Returns
0 if ok, negative AVERROR code on failure

Definition at line 242 of file dither.c.

◆ ff_dither_free()

void ff_dither_free ( DitherContext **  c)

Free a DitherContext.

Parameters
cDitherContext

Definition at line 312 of file dither.c.

Referenced by ff_dither_alloc().

◆ dither_init()

static av_cold void dither_init ( DitherDSPContext ddsp,
enum AVResampleDitherMethod  method 
)
static

Definition at line 329 of file dither.c.

Referenced by ff_dither_alloc().

◆ ff_dither_alloc()

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.

The parameters in the AVAudioResampleContext are used to initialize the DitherContext.

Parameters
avrAVAudioResampleContext
Returns
newly-allocated DitherContext

Definition at line 345 of file dither.c.

Variable Documentation

◆ ns_48_coef_b

const float ns_48_coef_b[4]
static
Initial value:
= {
2.2374f, -0.7339f, -0.1251f, -0.6033f
}

Definition at line 89 of file dither.c.

Referenced by ff_dither_alloc().

◆ ns_48_coef_a

const float ns_48_coef_a[4]
static
Initial value:
= {
0.9030f, 0.0116f, -0.5853f, -0.2571f
}

Definition at line 93 of file dither.c.

Referenced by ff_dither_alloc().

◆ ns_44_coef_b

const float ns_44_coef_b[4]
static
Initial value:
= {
2.2061f, -0.4707f, -0.2534f, -0.6213f
}

Definition at line 97 of file dither.c.

Referenced by ff_dither_alloc().

◆ ns_44_coef_a

const float ns_44_coef_a[4]
static
Initial value:
= {
1.0587f, 0.0676f, -0.6054f, -0.2738f
}

Definition at line 101 of file dither.c.

Referenced by ff_dither_alloc().