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
24 /**
25 * @file
26 * Dithered Audio Sample Quantization
27 *
28 * Converts from dbl, flt, or s32 to s16 using dithering.
29 */
30
32 #include <stdint.h>
33
41
52
58
63
66
71
74 };
75
76 /* mute threshold, in seconds */
77 #define MUTE_THRESHOLD_SEC 0.000333
78
79 /* scale factor for 16-bit output.
80 The signal is attenuated slightly to avoid clipping */
81 #define S16_SCALE 32753.0f
82
83 /* scale to convert lfg from INT_MIN/INT_MAX to -0.5/0.5 */
84 #define LFG_SCALE (1.0f / (2.0f * INT32_MAX))
85
86 /* noise shaping coefficients */
87
89 2.2374f, -0.7339f, -0.1251f, -0.6033f
90 };
91
93 0.9030f, 0.0116f, -0.5853f, -0.2571f
94 };
95
97 2.2061f, -0.4707f, -0.2534f, -0.6213f
98 };
99
101 1.0587f, 0.0676f, -0.6054f, -0.2738f
102 };
103
105 {
106 int i;
107 for (i = 0; i <
len; i++)
109 }
110
112 {
113 int i;
114 int *src1 = src0 +
len;
115
116 for (i = 0; i <
len; i++) {
120 }
121 }
122
124 {
125 int i;
126 for (i = 0; i <
len; i++)
128 }
129
130 #define SQRT_1_6 0.40824829046386301723f
131
133 {
134 int i;
135
136 /* filter is from libswresample in FFmpeg */
137 for (i = 0; i < len - 2; i++)
138 src[i] = (-src[i] + 2 * src[i + 1] - src[i + 2]) *
SQRT_1_6;
139 }
140
142 int min_samples)
143 {
144 int i;
146 int buf_samples = nb_samples *
148 unsigned int *noise_buf_ui;
149
152
157 noise_buf_ui = (
unsigned int *)state->
noise_buf;
158
160 for (i = 0; i < buf_samples; i++)
162
164
167
168 return 0;
169 }
170
172 int16_t *
dst,
const float *src,
174 {
175 int i, j;
177
180
182 float err = 0;
184
185 for (j = 0; j < 4; j++) {
188 }
189 for (j = 3; j > 0; j--) {
192 }
194 sample -= err;
195
197 dst[i] = av_clip_int16(
lrintf(sample));
199 } else {
200 dst[i] = av_clip_int16(
lrintf(sample + dither[i]));
201 state->
dither_b[0] = av_clipf(dst[i] - sample, -1.5f, 1.5f);
202 }
203
205 if (src[i])
207 }
208 }
209
212 {
213 int ch, ret;
214 int aligned_samples =
FFALIGN(nb_samples, 16);
215
216 for (ch = 0; ch < channels; ch++) {
218
221 if (ret < 0)
222 return ret;
225 }
226
229 } else {
233 }
234
236 }
237
238 return 0;
239 }
240
242 {
243 int ret;
245
246 /* output directly to dst if it is planar */
249 else {
250 /* make sure s16_data is large enough for the output */
252 if (ret < 0)
253 return ret;
254 }
255
257 /* make sure flt_data is large enough for the input */
259 if (ret < 0)
260 return ret;
262 }
263
265 /* convert input samples to fltp and scale to s16 range */
267 if (ret < 0)
268 return ret;
271 if (ret < 0)
272 return ret;
273 } else {
274 flt_data = src;
275 }
276
277 /* check alignment and padding constraints */
282
283 if (!(ptr_align % c->
ddsp.
ptr_align) && samples_align >= aligned_len) {
286 } else {
289 }
290 }
291
295 if (ret < 0)
296 return ret;
297
299
300 /* interleave output to dst if needed */
303 if (ret < 0)
304 return ret;
305 } else
307
308 return 0;
309 }
310
312 {
314 int ch;
315
316 if (!c)
317 return;
322 for (ch = 0; ch < c->
channels; ch++)
326 }
327
330 {
334
337 else
339
340 if (ARCH_X86)
342 }
343
348 {
351 int ch;
352
358 }
359
361 if (!c)
363
365 if (apply_map)
367
369 sample_rate != 48000 && sample_rate != 44100) {
371 "for triangular_ns dither. using triangular_hp instead.\n");
373 }
376
378 if (sample_rate == 48000) {
381 } else {
384 }
385 }
386
387 /* Either s16 or s16p output format is allowed, but s16p is used
388 internally, so we need to use a temp buffer and interleave if the output
389 format is s16 */
392 "dither s16 buffer");
394 goto fail;
395
397 channels, sample_rate, 0);
399 goto fail;
400 }
401
404 "dither flt buffer");
406 goto fail;
407 }
412 goto fail;
413 }
414
417 goto fail;
419
420 /* calculate thresholds for turning off dithering during periods of
421 silence to avoid replacing digital silence with quiet dither noise */
424
425 /* initialize dither states */
427 for (ch = 0; ch < channels; ch++) {
432 }
433
435
436 fail:
439 }