dither.c
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2012 Justin Ruggles <justin.ruggles@gmail.com>
3  *
4  * Triangular with Noise Shaping is based on opusfile.
5  * Copyright (c) 1994-2012 by the Xiph.Org Foundation and contributors
6  *
7  * This file is part of Libav.
8  *
9  * Libav is free software; you can redistribute it and/or
10  * modify it under the terms of the GNU Lesser General Public
11  * License as published by the Free Software Foundation; either
12  * version 2.1 of the License, or (at your option) any later version.
13  *
14  * Libav is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17  * Lesser General Public License for more details.
18  *
19  * You should have received a copy of the GNU Lesser General Public
20  * License along with Libav; if not, write to the Free Software
21  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
22  */
23 
31 #include <math.h>
32 #include <stdint.h>
33 
34 #include "libavutil/common.h"
35 #include "libavutil/lfg.h"
36 #include "libavutil/mem.h"
37 #include "libavutil/samplefmt.h"
38 #include "audio_convert.h"
39 #include "dither.h"
40 #include "internal.h"
41 
42 typedef struct DitherState {
43  int mute;
44  unsigned int seed;
46  float *noise_buf;
49  float dither_a[4];
50  float dither_b[4];
51 } DitherState;
52 
53 struct DitherContext {
56 
57  int mute_dither_threshold; // threshold for disabling dither
58  int mute_reset_threshold; // threshold for resetting noise shaping
59  const float *ns_coef_b; // noise shaping coeffs
60  const float *ns_coef_a; // noise shaping coeffs
61 
62  int channels;
63  DitherState *state; // dither states for each channel
64 
65  AudioData *flt_data; // input data in fltp
66  AudioData *s16_data; // dithered output in s16p
67  AudioConvert *ac_in; // converter for input to fltp
68  AudioConvert *ac_out; // converter for s16p to s16 (if needed)
69 
70  void (*quantize)(int16_t *dst, const float *src, float *dither, int len);
72 };
73 
74 /* mute threshold, in seconds */
75 #define MUTE_THRESHOLD_SEC 0.000333
76 
77 /* scale factor for 16-bit output.
78  The signal is attenuated slightly to avoid clipping */
79 #define S16_SCALE 32753.0f
80 
81 /* scale to convert lfg from INT_MIN/INT_MAX to -0.5/0.5 */
82 #define LFG_SCALE (1.0f / (2.0f * INT32_MAX))
83 
84 /* noise shaping coefficients */
85 
86 static const float ns_48_coef_b[4] = {
87  2.2374f, -0.7339f, -0.1251f, -0.6033f
88 };
89 
90 static const float ns_48_coef_a[4] = {
91  0.9030f, 0.0116f, -0.5853f, -0.2571f
92 };
93 
94 static const float ns_44_coef_b[4] = {
95  2.2061f, -0.4707f, -0.2534f, -0.6213f
96 };
97 
98 static const float ns_44_coef_a[4] = {
99  1.0587f, 0.0676f, -0.6054f, -0.2738f
100 };
101 
102 static void dither_int_to_float_rectangular_c(float *dst, int *src, int len)
103 {
104  int i;
105  for (i = 0; i < len; i++)
106  dst[i] = src[i] * LFG_SCALE;
107 }
108 
109 static void dither_int_to_float_triangular_c(float *dst, int *src0, int len)
110 {
111  int i;
112  int *src1 = src0 + len;
113 
114  for (i = 0; i < len; i++) {
115  float r = src0[i] * LFG_SCALE;
116  r += src1[i] * LFG_SCALE;
117  dst[i] = r;
118  }
119 }
120 
121 static void quantize_c(int16_t *dst, const float *src, float *dither, int len)
122 {
123  int i;
124  for (i = 0; i < len; i++)
125  dst[i] = av_clip_int16(lrintf(src[i] * S16_SCALE + dither[i]));
126 }
127 
128 #define SQRT_1_6 0.40824829046386301723f
129 
130 static void dither_highpass_filter(float *src, int len)
131 {
132  int i;
133 
134  /* filter is from libswresample in FFmpeg */
135  for (i = 0; i < len - 2; i++)
136  src[i] = (-src[i] + 2 * src[i + 1] - src[i + 2]) * SQRT_1_6;
137 }
138 
140  int min_samples)
141 {
142  int i;
143  int nb_samples = FFALIGN(min_samples, 16) + 16;
144  int buf_samples = nb_samples *
145  (c->method == AV_RESAMPLE_DITHER_RECTANGULAR ? 1 : 2);
146  unsigned int *noise_buf_ui;
147 
148  av_freep(&state->noise_buf);
149  state->noise_buf_size = state->noise_buf_ptr = 0;
150 
151  state->noise_buf = av_malloc(buf_samples * sizeof(*state->noise_buf));
152  if (!state->noise_buf)
153  return AVERROR(ENOMEM);
154  state->noise_buf_size = FFALIGN(min_samples, 16);
155  noise_buf_ui = (unsigned int *)state->noise_buf;
156 
157  av_lfg_init(&state->lfg, state->seed);
158  for (i = 0; i < buf_samples; i++)
159  noise_buf_ui[i] = av_lfg_get(&state->lfg);
160 
161  c->ddsp.dither_int_to_float(state->noise_buf, noise_buf_ui, nb_samples);
162 
164  dither_highpass_filter(state->noise_buf, nb_samples);
165 
166  return 0;
167 }
168 
170  int16_t *dst, const float *src,
171  int nb_samples)
172 {
173  int i, j;
174  float *dither = &state->noise_buf[state->noise_buf_ptr];
175 
176  if (state->mute > c->mute_reset_threshold)
177  memset(state->dither_a, 0, sizeof(state->dither_a));
178 
179  for (i = 0; i < nb_samples; i++) {
180  float err = 0;
181  float sample = src[i] * S16_SCALE;
182 
183  for (j = 0; j < 4; j++) {
184  err += c->ns_coef_b[j] * state->dither_b[j] -
185  c->ns_coef_a[j] * state->dither_a[j];
186  }
187  for (j = 3; j > 0; j--) {
188  state->dither_a[j] = state->dither_a[j - 1];
189  state->dither_b[j] = state->dither_b[j - 1];
190  }
191  state->dither_a[0] = err;
192  sample -= err;
193 
194  if (state->mute > c->mute_dither_threshold) {
195  dst[i] = av_clip_int16(lrintf(sample));
196  state->dither_b[0] = 0;
197  } else {
198  dst[i] = av_clip_int16(lrintf(sample + dither[i]));
199  state->dither_b[0] = av_clipf(dst[i] - sample, -1.5f, 1.5f);
200  }
201 
202  state->mute++;
203  if (src[i])
204  state->mute = 0;
205  }
206 }
207 
208 static int convert_samples(DitherContext *c, int16_t **dst, float * const *src,
209  int channels, int nb_samples)
210 {
211  int ch, ret;
212  int aligned_samples = FFALIGN(nb_samples, 16);
213 
214  for (ch = 0; ch < channels; ch++) {
215  DitherState *state = &c->state[ch];
216 
217  if (state->noise_buf_size < aligned_samples) {
218  ret = generate_dither_noise(c, state, nb_samples);
219  if (ret < 0)
220  return ret;
221  } else if (state->noise_buf_size - state->noise_buf_ptr < aligned_samples) {
222  state->noise_buf_ptr = 0;
223  }
224 
226  quantize_triangular_ns(c, state, dst[ch], src[ch], nb_samples);
227  } else {
228  c->quantize(dst[ch], src[ch],
229  &state->noise_buf[state->noise_buf_ptr],
230  FFALIGN(nb_samples, c->samples_align));
231  }
232 
233  state->noise_buf_ptr += aligned_samples;
234  }
235 
236  return 0;
237 }
238 
240 {
241  int ret;
242  AudioData *flt_data;
243 
244  /* output directly to dst if it is planar */
245  if (dst->sample_fmt == AV_SAMPLE_FMT_S16P)
246  c->s16_data = dst;
247  else {
248  /* make sure s16_data is large enough for the output */
249  ret = ff_audio_data_realloc(c->s16_data, src->nb_samples);
250  if (ret < 0)
251  return ret;
252  }
253 
254  if (src->sample_fmt != AV_SAMPLE_FMT_FLTP) {
255  /* make sure flt_data is large enough for the input */
256  ret = ff_audio_data_realloc(c->flt_data, src->nb_samples);
257  if (ret < 0)
258  return ret;
259  flt_data = c->flt_data;
260 
261  /* convert input samples to fltp and scale to s16 range */
262  ret = ff_audio_convert(c->ac_in, flt_data, src);
263  if (ret < 0)
264  return ret;
265  } else {
266  flt_data = src;
267  }
268 
269  /* check alignment and padding constraints */
271  int ptr_align = FFMIN(flt_data->ptr_align, c->s16_data->ptr_align);
274 
275  if (!(ptr_align % c->ddsp.ptr_align) && samples_align >= aligned_len) {
276  c->quantize = c->ddsp.quantize;
278  } else {
279  c->quantize = quantize_c;
280  c->samples_align = 1;
281  }
282  }
283 
284  ret = convert_samples(c, (int16_t **)c->s16_data->data,
285  (float * const *)flt_data->data, src->channels,
286  src->nb_samples);
287  if (ret < 0)
288  return ret;
289 
290  c->s16_data->nb_samples = src->nb_samples;
291 
292  /* interleave output to dst if needed */
293  if (dst->sample_fmt == AV_SAMPLE_FMT_S16) {
294  ret = ff_audio_convert(c->ac_out, dst, c->s16_data);
295  if (ret < 0)
296  return ret;
297  } else
298  c->s16_data = NULL;
299 
300  return 0;
301 }
302 
304 {
305  DitherContext *c = *cp;
306  int ch;
307 
308  if (!c)
309  return;
314  for (ch = 0; ch < c->channels; ch++)
315  av_free(c->state[ch].noise_buf);
316  av_free(c->state);
317  av_freep(cp);
318 }
319 
320 static void dither_init(DitherDSPContext *ddsp,
321  enum AVResampleDitherMethod method)
322 {
323  ddsp->quantize = quantize_c;
324  ddsp->ptr_align = 1;
325  ddsp->samples_align = 1;
326 
327  if (method == AV_RESAMPLE_DITHER_RECTANGULAR)
329  else
331 }
332 
334  enum AVSampleFormat out_fmt,
335  enum AVSampleFormat in_fmt,
336  int channels, int sample_rate)
337 {
338  AVLFG seed_gen;
339  DitherContext *c;
340  int ch;
341 
343  av_get_bytes_per_sample(in_fmt) <= 2) {
344  av_log(avr, AV_LOG_ERROR, "dithering %s to %s is not supported\n",
346  return NULL;
347  }
348 
349  c = av_mallocz(sizeof(*c));
350  if (!c)
351  return NULL;
352 
354  sample_rate != 48000 && sample_rate != 44100) {
355  av_log(avr, AV_LOG_WARNING, "sample rate must be 48000 or 44100 Hz "
356  "for triangular_ns dither. using triangular_hp instead.\n");
358  }
359  c->method = avr->dither_method;
360  dither_init(&c->ddsp, c->method);
361 
363  if (sample_rate == 48000) {
364  c->ns_coef_b = ns_48_coef_b;
365  c->ns_coef_a = ns_48_coef_a;
366  } else {
367  c->ns_coef_b = ns_44_coef_b;
368  c->ns_coef_a = ns_44_coef_a;
369  }
370  }
371 
372  /* Either s16 or s16p output format is allowed, but s16p is used
373  internally, so we need to use a temp buffer and interleave if the output
374  format is s16 */
375  if (out_fmt != AV_SAMPLE_FMT_S16P) {
376  c->s16_data = ff_audio_data_alloc(channels, 1024, AV_SAMPLE_FMT_S16P,
377  "dither s16 buffer");
378  if (!c->s16_data)
379  goto fail;
380 
382  channels, sample_rate);
383  if (!c->ac_out)
384  goto fail;
385  }
386 
387  if (in_fmt != AV_SAMPLE_FMT_FLTP) {
388  c->flt_data = ff_audio_data_alloc(channels, 1024, AV_SAMPLE_FMT_FLTP,
389  "dither flt buffer");
390  if (!c->flt_data)
391  goto fail;
392 
394  channels, sample_rate);
395  if (!c->ac_in)
396  goto fail;
397  }
398 
399  c->state = av_mallocz(channels * sizeof(*c->state));
400  if (!c->state)
401  goto fail;
402  c->channels = channels;
403 
404  /* calculate thresholds for turning off dithering during periods of
405  silence to avoid replacing digital silence with quiet dither noise */
406  c->mute_dither_threshold = lrintf(sample_rate * MUTE_THRESHOLD_SEC);
408 
409  /* initialize dither states */
410  av_lfg_init(&seed_gen, 0xC0FFEE);
411  for (ch = 0; ch < channels; ch++) {
412  DitherState *state = &c->state[ch];
413  state->mute = c->mute_reset_threshold + 1;
414  state->seed = av_lfg_get(&seed_gen);
415  generate_dither_noise(c, state, FFMAX(32768, sample_rate / 2));
416  }
417 
418  return c;
419 
420 fail:
421  ff_dither_free(&c);
422  return NULL;
423 }
float * noise_buf
Definition: dither.c:46
Definition: lfg.h:25
void * av_malloc(size_t size)
Allocate a block of size bytes with alignment suitable for all memory accesses (including vectors if ...
Definition: mem.c:61
static const float ns_44_coef_b[4]
Definition: dither.c:94
int mute
Definition: dither.c:43
int ff_audio_data_realloc(AudioData *a, int nb_samples)
Reallocate AudioData.
Definition: audio_data.c:153
Audio buffer used for intermediate storage between conversion phases.
Definition: oss_audio.c:46
AudioData * flt_data
Definition: dither.c:65
enum AVResampleDitherMethod method
Definition: dither.c:55
memory handling functions
AudioData * ff_audio_data_alloc(int channels, int nb_samples, enum AVSampleFormat sample_fmt, const char *name)
Allocate AudioData.
Definition: audio_data.c:110
static const float ns_48_coef_b[4]
Definition: dither.c:86
static void dither_int_to_float_triangular_c(float *dst, int *src0, int len)
Definition: dither.c:109
void(* quantize)(int16_t *dst, const float *src, float *dither, int len)
Definition: dither.c:70
int ff_audio_convert(AudioConvert *ac, AudioData *out, AudioData *in)
Convert audio data from one sample format to another.
DitherContext * ff_dither_alloc(AVAudioResampleContext *avr, enum AVSampleFormat out_fmt, enum AVSampleFormat in_fmt, int channels, int sample_rate)
Allocate and initialize a DitherContext.
Definition: dither.c:333
signed 16 bits
Definition: samplefmt.h:52
#define sample
int aligned_len
unsigned int seed
Definition: dither.c:44
AudioConvert * ac_out
Definition: dither.c:68
int nb_samples
current number of samples
Definition: audio_data.h:40
void av_freep(void *arg)
Free a memory block which has been allocated with av_malloc(z)() or av_realloc() and set the pointer ...
Definition: mem.c:151
Triangular Dither with Noise Shaping.
Definition: avresample.h:127
Rectangular Dither.
Definition: avresample.h:124
Triangular Dither with High Pass.
Definition: avresample.h:126
static void dither_init(DitherDSPContext *ddsp, enum AVResampleDitherMethod method)
Definition: dither.c:320
DitherState * state
Definition: dither.c:63
const float * ns_coef_a
Definition: dither.c:60
int samples_align
Definition: dither.c:71
float, planar
Definition: samplefmt.h:60
#define r
Definition: input.c:51
int samples_align
enum AVResampleDitherMethod dither_method
dither method
Definition: internal.h:56
static void quantize_c(int16_t *dst, const float *src, float *dither, int len)
Definition: dither.c:121
void av_free(void *ptr)
Free a memory block which has been allocated with av_malloc(z)() or av_realloc(). ...
Definition: mem.c:139
static const float ns_48_coef_a[4]
Definition: dither.c:90
int noise_buf_size
Definition: dither.c:47
int mute_reset_threshold
Definition: dither.c:58
int ptr_align
src and dst constraits for quantize()
Definition: dither.h:40
int channels
channel count
Definition: oss_audio.c:50
#define LFG_SCALE
Definition: dither.c:82
int noise_buf_ptr
Definition: dither.c:48
void av_log(void *avcl, int level, const char *fmt,...)
Definition: log.c:146
void ff_audio_convert_free(AudioConvert **ac)
Free AudioConvert.
int channels
Definition: dither.c:62
static int convert_samples(DitherContext *c, int16_t **dst, float *const *src, int channels, int nb_samples)
Definition: dither.c:208
AudioConvert * ac_in
Definition: dither.c:67
DitherDSPContext ddsp
Definition: dither.c:54
int ff_convert_dither(DitherContext *c, AudioData *dst, AudioData *src)
Convert audio sample format with dithering.
Definition: dither.c:239
void ff_dither_free(DitherContext **cp)
Free a DitherContext.
Definition: dither.c:303
static av_always_inline av_const long int lrintf(float x)
Definition: libm.h:144
enum AVSampleFormat av_get_packed_sample_fmt(enum AVSampleFormat sample_fmt)
Get the packed alternative form of the given sample format.
Definition: samplefmt.c:64
struct DitherState DitherState
const float * ns_coef_b
Definition: dither.c:59
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.
Definition: samplefmt.c:47
AudioConvert * ff_audio_convert_alloc(AVAudioResampleContext *avr, enum AVSampleFormat out_fmt, enum AVSampleFormat in_fmt, int channels, int sample_rate)
Allocate and initialize AudioConvert context for sample format conversion.
int samples_align
len constraits for quantize()
Definition: dither.h:41
int av_get_bytes_per_sample(enum AVSampleFormat sample_fmt)
Return number of bytes per sample.
Definition: samplefmt.c:95
NULL
Definition: eval.c:52
static const uint16_t dither[8][8]
Definition: vf_gradfun.c:45
int mute_dither_threshold
Definition: dither.c:57
static void quantize_triangular_ns(DitherContext *c, DitherState *state, int16_t *dst, const float *src, int nb_samples)
Definition: dither.c:169
AVLFG lfg
Definition: dither.c:45
static void(WINAPI *cond_broadcast)(pthread_cond_t *cond)
#define S16_SCALE
Definition: dither.c:79
out nb_samples
static unsigned int av_lfg_get(AVLFG *c)
Get the next random unsigned 32-bit number using an ALFG.
Definition: lfg.h:38
av_cold void av_lfg_init(AVLFG *c, unsigned int seed)
Definition: lfg.c:30
static void dither_int_to_float_rectangular_c(float *dst, int *src, int len)
Definition: dither.c:102
static uint32_t state
Definition: trasher.c:27
static void dither_highpass_filter(float *src, int len)
Definition: dither.c:130
int samples_align
allocated samples alignment
Definition: audio_data.h:51
#define SQRT_1_6
Definition: dither.c:128
static const float ns_44_coef_a[4]
Definition: dither.c:98
#define MUTE_THRESHOLD_SEC
Definition: dither.c:75
static int generate_dither_noise(DitherContext *c, DitherState *state, int min_samples)
Definition: dither.c:139
common internal and external API header
AudioData * s16_data
Definition: dither.c:66
float dither_a[4]
Definition: dither.c:49
AVSampleFormat
Audio Sample Formats.
Definition: samplefmt.h:49
int len
void(* dither_int_to_float)(float *dst, int *src0, int len)
Convert dither noise from int to float with triangular distribution.
Definition: dither.h:54
signed 16 bits, planar
Definition: samplefmt.h:58
enum AVSampleFormat sample_fmt
sample format
Definition: audio_data.h:41
void ff_audio_data_free(AudioData **a)
Free AudioData.
Definition: audio_data.c:208
int ptr_align
minimum data pointer alignment
Definition: audio_data.h:50
AVResampleDitherMethod
Definition: avresample.h:122
void(* quantize)(int16_t *dst, const float *src, float *dither, int len)
Convert samples from flt to s16 with added dither noise.
Definition: dither.h:38
void * av_mallocz(size_t size)
Allocate a block of size bytes with alignment suitable for all memory accesses (including vectors if ...
Definition: mem.c:158
uint8_t * data[AVRESAMPLE_MAX_CHANNELS]
data plane pointers
Definition: audio_data.h:36
float dither_b[4]
Definition: dither.c:50