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 FFmpeg.
8 *
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.
13 *
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.
18 *
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
22 */
23
24 /**
25 * @file
26 * Dithered Audio Sample Quantization
27 *
28 * Converts from dbl, flt, or s32 to s16 using dithering.
29 */
30
31 #include <math.h>
32 #include <stdint.h>
33
42
53
59
64
67
72
75 };
76
77 /* mute threshold, in seconds */
78 #define MUTE_THRESHOLD_SEC 0.000333
79
80 /* scale factor for 16-bit output.
81 The signal is attenuated slightly to avoid clipping */
82 #define S16_SCALE 32753.0f
83
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))
86
87 /* noise shaping coefficients */
88
90 2.2374f, -0.7339f, -0.1251f, -0.6033f
91 };
92
94 0.9030f, 0.0116f, -0.5853f, -0.2571f
95 };
96
98 2.2061f, -0.4707f, -0.2534f, -0.6213f
99 };
100
102 1.0587f, 0.0676f, -0.6054f, -0.2738f
103 };
104
106 {
107 int i;
108 for (i = 0; i <
len; i++)
110 }
111
113 {
114 int i;
115 int *src1 = src0 +
len;
116
117 for (i = 0; i <
len; i++) {
121 }
122 }
123
125 {
126 int i;
127 for (i = 0; i <
len; i++)
129 }
130
131 #define SQRT_1_6 0.40824829046386301723f
132
134 {
135 int i;
136
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;
140 }
141
143 int min_samples)
144 {
145 int i;
146 int nb_samples =
FFALIGN(min_samples, 16) + 16;
147 int buf_samples = nb_samples *
149 unsigned int *noise_buf_ui;
150
153
158 noise_buf_ui = (
unsigned int *)state->
noise_buf;
159
161 for (i = 0; i < buf_samples; i++)
163
165
168
169 return 0;
170 }
171
173 int16_t *dst,
const float *
src,
174 int nb_samples)
175 {
176 int i, j;
178
181
182 for (i = 0; i < nb_samples; i++) {
183 float err = 0;
185
186 for (j = 0; j < 4; j++) {
189 }
190 for (j = 3; j > 0; j--) {
193 }
195 sample -= err;
196
198 dst[i] = av_clip_int16(
lrintf(sample));
200 } else {
201 dst[i] = av_clip_int16(
lrintf(sample + dither[i]));
202 state->
dither_b[0] = av_clipf(dst[i] - sample, -1.5f, 1.5f);
203 }
204
206 if (src[i])
208 }
209 }
210
212 int channels, int nb_samples)
213 {
215 int aligned_samples =
FFALIGN(nb_samples, 16);
216
217 for (ch = 0; ch < channels; ch++) {
219
222 if (ret < 0)
226 }
227
230 } else {
234 }
235
237 }
238
239 return 0;
240 }
241
243 {
246
247 /* output directly to dst if it is planar */
250 else {
251 /* make sure s16_data is large enough for the output */
253 if (ret < 0)
255 }
256
258 /* make sure flt_data is large enough for the input */
260 if (ret < 0)
263 }
264
266 /* convert input samples to fltp and scale to s16 range */
268 if (ret < 0)
272 if (ret < 0)
274 } else {
276 }
277
278 /* check alignment and padding constraints */
283
284 if (!(ptr_align % c->
ddsp.
ptr_align) && samples_align >= aligned_len) {
287 } else {
290 }
291 }
292
296 if (ret < 0)
298
300
301 /* interleave output to dst if needed */
304 if (ret < 0)
306 } else
308
309 return 0;
310 }
311
313 {
315 int ch;
316
317 if (!c)
318 return;
323 for (ch = 0; ch < c->
channels; ch++)
327 }
328
331 {
335
338 else
340
341 if (ARCH_X86)
343 }
344
349 {
352 int ch;
353
358 return NULL;
359 }
360
362 if (!c)
363 return NULL;
364
366 if (apply_map)
368
370 sample_rate != 48000 && sample_rate != 44100) {
372 "for triangular_ns dither. using triangular_hp instead.\n");
374 }
377
379 if (sample_rate == 48000) {
382 } else {
385 }
386 }
387
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
390 format is s16 */
393 "dither s16 buffer");
395 goto fail;
396
398 channels, sample_rate, 0);
400 goto fail;
401 }
402
405 "dither flt buffer");
407 goto fail;
408 }
413 goto fail;
414 }
415
418 goto fail;
420
421 /* calculate thresholds for turning off dithering during periods of
422 silence to avoid replacing digital silence with quiet dither noise */
425
426 /* initialize dither states */
428 for (ch = 0; ch < channels; ch++) {
433 }
434
436
437 fail:
439 return NULL;
440 }