1 /*
2 * Wavesynth pseudo-codec
3 * Copyright (c) 2011 Nicolas George
4 *
5 * This file is part of FFmpeg.
6 *
7 * FFmpeg is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Lesser General Public
9 * License as published by the Free Software Foundation; either
10 * version 2.1 of the License, or (at your option) any later version.
11 *
12 * FFmpeg is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
16 *
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with FFmpeg; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
20 */
21
26
27
29 #define WS_MAX_CHANNELS 32
30 #define INF_TS 0x7FFFFFFFFFFFFFFF
31
33
34 /*
35 Format of the extradata and packets
36
37 THIS INFORMATION IS NOT PART OF THE PUBLIC API OR ABI.
38 IT CAN CHANGE WITHOUT NOTIFICATION.
39
40 All numbers are in little endian.
41
42 The codec extradata define a set of intervals with uniform content.
43 Overlapping intervals are added together.
44
45 extradata:
46 uint32 number of intervals
47 ... intervals
48
49 interval:
50 int64 start timestamp; time_base must be 1/sample_rate;
51 start timestamps must be in ascending order
52 int64 end timestamp
53 uint32 type
54 uint32 channels mask
55 ... additional information, depends on type
56
57 sine interval (type fourcc "SINE"):
58 int32 start frequency, in 1/(1<<16) Hz
59 int32 end frequency
60 int32 start amplitude, 1<<16 is the full amplitude
61 int32 end amplitude
62 uint32 start phase, 0 is sin(0), 0x20000000 is sin(pi/2), etc.;
63 n | (1<<31) means to match the phase of previous channel #n
64
65 pink noise interval (type fourcc "NOIS"):
66 int32 start amplitude
67 int32 end amplitude
68
69 The input packets encode the time and duration of the requested segment.
70
71 packet:
72 int64 start timestamp
73 int32 duration
74
75 */
76
80 };
81
90 };
91
104 };
105
106 #define LCG_A 1284865837
107 #define LCG_C 4150755663
108 #define LCG_AI 849225893 /* A*AI = 1 [mod 1<<32] */
109
111 {
114 }
115
117 {
118 uint32_t
a,
c, t = *
s;
119
120 if (dt >= 0) {
123 } else { /* coefficients for a step backward */
126 dt = -dt;
127 }
128 while (dt) {
129 if (dt & 1)
131 c *= a + 1; /* coefficients for a double step */
133 dt >>= 1;
134 }
135 *s = t;
136 }
137
138 /* Emulate pink noise by summing white noise at the sampling frequency,
139 * white noise at half the sampling frequency (each value taken twice),
140 * etc., with a total of 8 octaves.
141 * This is known as the Voss-McCartney algorithm. */
142
144 {
146 int i, j;
147
150 return;
152 for (j = 0; j < 7; j++) {
153 if ((i >> j) & 1)
154 break;
158 }
160 }
162 }
163
164 /**
165 * @return (1<<64) * a / b, without overflow, if a < b
166 */
168 {
170 int i;
171
172 if (b < (uint64_t)1 << 32) { /* b small, use two 32-bits steps */
173 a <<= 32;
174 return ((a / b) << 32) | ((a %
b) << 32) /
b;
175 }
176 if (b < (uint64_t)1 << 48) { /* b medium, use four 16-bits steps */
177 for (i = 0; i < 4; i++) {
178 a <<= 16;
179 r = (r << 16) | (a / b);
181 }
183 }
184 for (i = 63; i >= 0; i--) {
185 if (a >= (uint64_t)1 << 63 || a << 1 >=
b) {
186 r |= (uint64_t)1 << i;
187 a = (a << 1) - b;
188 } else {
189 a <<= 1;
190 }
191 }
193 }
194
196 {
198 uint64_t dt2 = dt & 1 ? /* dt * (dt - 1) / 2 without overflow */
199 dt * ((dt - 1) >> 1) : (dt >> 1) * (dt - 1);
201 }
202
204 {
205 int *last, i;
207
209 for (i = 0; i < ws->
nb_inter; i++) {
212 break;
214 continue;
215 *last = i;
220 }
223 *last = -1;
227 int64_t pink_ts_next = ts & ~(
PINK_UNIT - 1);
230 if (pos) {
233 } else {
235 }
236 }
238 }
239
241 {
247 int64_t dphi1, dphi2, dt, cur_ts = -0x8000000000000000;
248 int i;
249
255 edata += 4;
261 for (i = 0; i < ws->
nb_inter; i++) {
263 if (edata_end - edata < 24)
269 edata += 24;
276 if (edata_end - edata < 20)
283 edata += 20;
287 in->
ddphi = (dphi2 - dphi1) / dt;
288 if (phi & 0x80000000) {
289 phi &= ~0x80000000;
290 if (phi >= i)
293 } else {
294 in->
phi0 = (uint64_t)phi << 33;
295 }
296 break;
298 if (edata_end - edata < 8)
302 edata += 8;
303 break;
304 default:
306 }
307 in->
amp0 = (int64_t)a1 << 32;
308 in->
damp = (((int64_t)a2 << 32) - ((int64_t)a1 << 32)) / dt;
309 }
310 if (edata != edata_end)
312 return 0;
313 }
314
316 {
319
322 "This implementation is limited to %d channels.\n",
325 }
327 if (r < 0) {
329 goto fail;
330 }
334 goto fail;
335 }
337 ws->
sin[i] = floor(32767 *
sin(2 *
M_PI * i / (1 << SIN_BITS)));
345 return 0;
346
347 fail:
351 }
352
355 {
358 int i, *last, pink;
359 uint32_t
c, all_ch = 0;
360
366 while (i >= 0) {
370 *last = i;
371 continue;
372 }
381 break;
383 val = amp * pink;
384 break;
385 default:
386 val = 0;
387 }
389 for (c = in->
channels, cv = channels; c; c >>= 1, cv++)
390 if (c & 1)
392 }
394 for (c = all_ch, cv = channels;
c; c >>= 1, cv++)
395 if (c & 1)
397 }
398
400 {
401 int *last, i;
403
407 for (i = ws->
next_inter; i < ws->nb_inter; i++) {
410 break;
412 continue;
413 *last = i;
418 }
421 *last = -1;
422 }
423
426 {
429 int64_t ts;
432 int16_t *pcm;
434
435 *rgot_frame = 0;
436 if (packet->
size != 12)
442 if (duration <= 0)
446 if (r < 0)
448 pcm = (int16_t *)frame->
data[0];
450 memset(channels, 0, avc->
channels *
sizeof(*channels));
455 *(pcm++) = channels[
c] >> 16;
456 }
458 *rgot_frame = 1;
460 }
461
463 {
465
468 return 0;
469 }
470
481 };