2 * Copyright (c) 2012 Justin Ruggles <justin.ruggles@gmail.com>
4 * Triangular with Noise Shaping is based on opusfile.
5 * Copyright (c) 1994-2012 by the Xiph.Org Foundation and contributors
7 * This file is part of FFmpeg.
9 * FFmpeg 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.
14 * FFmpeg 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.
19 * You should have received a copy of the GNU Lesser General Public
20 * License along with FFmpeg; if not, write to the Free Software
21 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
26 * Dithered Audio Sample Quantization
28 * Converts from dbl, flt, or s32 to s16 using dithering.
34 #include "libavutil/attributes.h"
35 #include "libavutil/common.h"
36 #include "libavutil/lfg.h"
37 #include "libavutil/mem.h"
38 #include "libavutil/samplefmt.h"
39 #include "audio_convert.h"
43 typedef struct DitherState
{
54 struct DitherContext
{
55 DitherDSPContext ddsp
;
56 enum AVResampleDitherMethod method
;
58 ChannelMapInfo
*ch_map_info
;
60 int mute_dither_threshold
; // threshold for disabling dither
61 int mute_reset_threshold
; // threshold for resetting noise shaping
62 const float *ns_coef_b
; // noise shaping coeffs
63 const float *ns_coef_a
; // noise shaping coeffs
66 DitherState
*state
; // dither states for each channel
68 AudioData
*flt_data
; // input data in fltp
69 AudioData
*s16_data
; // dithered output in s16p
70 AudioConvert
*ac_in
; // converter for input to fltp
71 AudioConvert
*ac_out
; // converter for s16p to s16 (if needed)
73 void (*quantize
)(int16_t *dst
, const float *src
, float *dither
, int len
);
77 /* mute threshold, in seconds */
78 #define MUTE_THRESHOLD_SEC 0.000333
80 /* scale factor for 16-bit output.
81 The signal is attenuated slightly to avoid clipping */
82 #define S16_SCALE 32753.0f
84 /* scale to convert lfg from INT_MIN/INT_MAX to -0.5/0.5 */
85 #define LFG_SCALE (1.0f / (2.0f * INT32_MAX))
87 /* noise shaping coefficients */
89 static const float ns_48_coef_b
[4] = {
90 2.2374f
, -0.7339f
, -0.1251f
, -0.6033f
93 static const float ns_48_coef_a
[4] = {
94 0.9030f
, 0.0116f
, -0.5853f
, -0.2571f
97 static const float ns_44_coef_b
[4] = {
98 2.2061f
, -0.4707f
, -0.2534f
, -0.6213f
101 static const float ns_44_coef_a
[4] = {
102 1.0587f
, 0.0676f
, -0.6054f
, -0.2738f
105 static void dither_int_to_float_rectangular_c(float *dst
, int *src
, int len
)
108 for (i
= 0; i
< len
; i
++)
109 dst
[i
] = src
[i
] * LFG_SCALE
;
112 static void dither_int_to_float_triangular_c(float *dst
, int *src0
, int len
)
115 int *src1
= src0
+ len
;
117 for (i
= 0; i
< len
; i
++) {
118 float r
= src0
[i
] * LFG_SCALE
;
119 r
+= src1
[i
] * LFG_SCALE
;
124 static void quantize_c(int16_t *dst
, const float *src
, float *dither
, int len
)
127 for (i
= 0; i
< len
; i
++)
128 dst
[i
] = av_clip_int16(lrintf(src
[i
] * S16_SCALE
+ dither
[i
]));
131 #define SQRT_1_6 0.40824829046386301723f
133 static void dither_highpass_filter(float *src
, int len
)
137 /* filter is from libswresample in FFmpeg */
138 for (i
= 0; i
< len
- 2; i
++)
139 src
[i
] = (-src
[i
] + 2 * src
[i
+ 1] - src
[i
+ 2]) * SQRT_1_6
;
142 static int generate_dither_noise(DitherContext
*c
, DitherState
*state
,
146 int nb_samples
= FFALIGN(min_samples
, 16) + 16;
147 int buf_samples
= nb_samples
*
148 (c
->method
== AV_RESAMPLE_DITHER_RECTANGULAR
? 1 : 2);
149 unsigned int *noise_buf_ui
;
151 av_freep(&state
->noise_buf
);
152 state
->noise_buf_size
= state
->noise_buf_ptr
= 0;
154 state
->noise_buf
= av_malloc(buf_samples
* sizeof(*state
->noise_buf
));
155 if (!state
->noise_buf
)
156 return AVERROR(ENOMEM
);
157 state
->noise_buf_size
= FFALIGN(min_samples
, 16);
158 noise_buf_ui
= (unsigned int *)state
->noise_buf
;
160 av_lfg_init(&state
->lfg
, state
->seed
);
161 for (i
= 0; i
< buf_samples
; i
++)
162 noise_buf_ui
[i
] = av_lfg_get(&state
->lfg
);
164 c
->ddsp
.dither_int_to_float(state
->noise_buf
, noise_buf_ui
, nb_samples
);
166 if (c
->method
== AV_RESAMPLE_DITHER_TRIANGULAR_HP
)
167 dither_highpass_filter(state
->noise_buf
, nb_samples
);
172 static void quantize_triangular_ns(DitherContext
*c
, DitherState
*state
,
173 int16_t *dst
, const float *src
,
177 float *dither
= &state
->noise_buf
[state
->noise_buf_ptr
];
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
++) {
184 float sample
= src
[i
] * S16_SCALE
;
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
--) {
191 state
->dither_a
[j
] = state
->dither_a
[j
- 1];
192 state
->dither_b
[j
] = state
->dither_b
[j
- 1];
194 state
->dither_a
[0] = err
;
197 if (state
->mute
> c
->mute_dither_threshold
) {
198 dst
[i
] = av_clip_int16(lrintf(sample
));
199 state
->dither_b
[0] = 0;
201 dst
[i
] = av_clip_int16(lrintf(sample
+ dither
[i
]));
202 state
->dither_b
[0] = av_clipf(dst
[i
] - sample
, -1.5f
, 1.5f
);
211 static int convert_samples(DitherContext
*c
, int16_t **dst
, float * const *src
,
212 int channels
, int nb_samples
)
215 int aligned_samples
= FFALIGN(nb_samples
, 16);
217 for (ch
= 0; ch
< channels
; ch
++) {
218 DitherState
*state
= &c
->state
[ch
];
220 if (state
->noise_buf_size
< aligned_samples
) {
221 ret
= generate_dither_noise(c
, state
, nb_samples
);
224 } else if (state
->noise_buf_size
- state
->noise_buf_ptr
< aligned_samples
) {
225 state
->noise_buf_ptr
= 0;
228 if (c
->method
== AV_RESAMPLE_DITHER_TRIANGULAR_NS
) {
229 quantize_triangular_ns(c
, state
, dst
[ch
], src
[ch
], nb_samples
);
231 c
->quantize(dst
[ch
], src
[ch
],
232 &state
->noise_buf
[state
->noise_buf_ptr
],
233 FFALIGN(nb_samples
, c
->samples_align
));
236 state
->noise_buf_ptr
+= aligned_samples
;
242 int ff_convert_dither(DitherContext
*c
, AudioData
*dst
, AudioData
*src
)
247 /* output directly to dst if it is planar */
248 if (dst
->sample_fmt
== AV_SAMPLE_FMT_S16P
)
251 /* make sure s16_data is large enough for the output */
252 ret
= ff_audio_data_realloc(c
->s16_data
, src
->nb_samples
);
257 if (src
->sample_fmt
!= AV_SAMPLE_FMT_FLTP
|| c
->apply_map
) {
258 /* make sure flt_data is large enough for the input */
259 ret
= ff_audio_data_realloc(c
->flt_data
, src
->nb_samples
);
262 flt_data
= c
->flt_data
;
265 if (src
->sample_fmt
!= AV_SAMPLE_FMT_FLTP
) {
266 /* convert input samples to fltp and scale to s16 range */
267 ret
= ff_audio_convert(c
->ac_in
, flt_data
, src
);
270 } else if (c
->apply_map
) {
271 ret
= ff_audio_data_copy(flt_data
, src
, c
->ch_map_info
);
278 /* check alignment and padding constraints */
279 if (c
->method
!= AV_RESAMPLE_DITHER_TRIANGULAR_NS
) {
280 int ptr_align
= FFMIN(flt_data
->ptr_align
, c
->s16_data
->ptr_align
);
281 int samples_align
= FFMIN(flt_data
->samples_align
, c
->s16_data
->samples_align
);
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
;
288 c
->quantize
= quantize_c
;
289 c
->samples_align
= 1;
293 ret
= convert_samples(c
, (int16_t **)c
->s16_data
->data
,
294 (float * const *)flt_data
->data
, src
->channels
,
299 c
->s16_data
->nb_samples
= src
->nb_samples
;
301 /* interleave output to dst if needed */
302 if (dst
->sample_fmt
== AV_SAMPLE_FMT_S16
) {
303 ret
= ff_audio_convert(c
->ac_out
, dst
, c
->s16_data
);
312 void ff_dither_free(DitherContext
**cp
)
314 DitherContext
*c
= *cp
;
319 ff_audio_data_free(&c
->flt_data
);
320 ff_audio_data_free(&c
->s16_data
);
321 ff_audio_convert_free(&c
->ac_in
);
322 ff_audio_convert_free(&c
->ac_out
);
323 for (ch
= 0; ch
< c
->channels
; ch
++)
324 av_free(c
->state
[ch
].noise_buf
);
329 static av_cold
void dither_init(DitherDSPContext
*ddsp
,
330 enum AVResampleDitherMethod method
)
332 ddsp
->quantize
= quantize_c
;
334 ddsp
->samples_align
= 1;
336 if (method
== AV_RESAMPLE_DITHER_RECTANGULAR
)
337 ddsp
->dither_int_to_float
= dither_int_to_float_rectangular_c
;
339 ddsp
->dither_int_to_float
= dither_int_to_float_triangular_c
;
342 ff_dither_init_x86(ddsp
, method
);
345 DitherContext
*ff_dither_alloc(AVAudioResampleContext
*avr
,
346 enum AVSampleFormat out_fmt
,
347 enum AVSampleFormat in_fmt
,
348 int channels
, int sample_rate
, int apply_map
)
354 if (av_get_packed_sample_fmt(out_fmt
) != AV_SAMPLE_FMT_S16
||
355 av_get_bytes_per_sample(in_fmt
) <= 2) {
356 av_log(avr
, AV_LOG_ERROR
, "dithering %s to %s is not supported\n",
357 av_get_sample_fmt_name(in_fmt
), av_get_sample_fmt_name(out_fmt
));
361 c
= av_mallocz(sizeof(*c
));
365 c
->apply_map
= apply_map
;
367 c
->ch_map_info
= &avr
->ch_map_info
;
369 if (avr
->dither_method
== AV_RESAMPLE_DITHER_TRIANGULAR_NS
&&
370 sample_rate
!= 48000 && sample_rate
!= 44100) {
371 av_log(avr
, AV_LOG_WARNING
, "sample rate must be 48000 or 44100 Hz "
372 "for triangular_ns dither. using triangular_hp instead.\n");
373 avr
->dither_method
= AV_RESAMPLE_DITHER_TRIANGULAR_HP
;
375 c
->method
= avr
->dither_method
;
376 dither_init(&c
->ddsp
, c
->method
);
378 if (c
->method
== AV_RESAMPLE_DITHER_TRIANGULAR_NS
) {
379 if (sample_rate
== 48000) {
380 c
->ns_coef_b
= ns_48_coef_b
;
381 c
->ns_coef_a
= ns_48_coef_a
;
383 c
->ns_coef_b
= ns_44_coef_b
;
384 c
->ns_coef_a
= ns_44_coef_a
;
388 /* Either s16 or s16p output format is allowed, but s16p is used
389 internally, so we need to use a temp buffer and interleave if the output
391 if (out_fmt
!= AV_SAMPLE_FMT_S16P
) {
392 c
->s16_data
= ff_audio_data_alloc(channels
, 1024, AV_SAMPLE_FMT_S16P
,
393 "dither s16 buffer");
397 c
->ac_out
= ff_audio_convert_alloc(avr
, out_fmt
, AV_SAMPLE_FMT_S16P
,
398 channels
, sample_rate
, 0);
403 if (in_fmt
!= AV_SAMPLE_FMT_FLTP
|| c
->apply_map
) {
404 c
->flt_data
= ff_audio_data_alloc(channels
, 1024, AV_SAMPLE_FMT_FLTP
,
405 "dither flt buffer");
409 if (in_fmt
!= AV_SAMPLE_FMT_FLTP
) {
410 c
->ac_in
= ff_audio_convert_alloc(avr
, AV_SAMPLE_FMT_FLTP
, in_fmt
,
411 channels
, sample_rate
, c
->apply_map
);
416 c
->state
= av_mallocz(channels
* sizeof(*c
->state
));
419 c
->channels
= channels
;
421 /* calculate thresholds for turning off dithering during periods of
422 silence to avoid replacing digital silence with quiet dither noise */
423 c
->mute_dither_threshold
= lrintf(sample_rate
* MUTE_THRESHOLD_SEC
);
424 c
->mute_reset_threshold
= c
->mute_dither_threshold
* 4;
426 /* initialize dither states */
427 av_lfg_init(&seed_gen
, 0xC0FFEE);
428 for (ch
= 0; ch
< channels
; ch
++) {
429 DitherState
*state
= &c
->state
[ch
];
430 state
->mute
= c
->mute_reset_threshold
+ 1;
431 state
->seed
= av_lfg_get(&seed_gen
);
432 generate_dither_noise(c
, state
, FFMAX(32768, sample_rate
/ 2));