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
122 while (dt) {
123 if (dt & 1)
125 c *=
a + 1;
/* coefficients for a double step */
127 dt >>= 1;
128 }
130 }
131
132 /* Emulate pink noise by summing white noise at the sampling frequency,
133 * white noise at half the sampling frequency (each value taken twice),
134 * etc., with a total of 8 octaves.
135 * This is known as the Voss-McCartney algorithm. */
136
138 {
141
144 return;
146 for (j = 0; j < 7; j++) {
148 break;
149 v -= vt[j];
151 v += vt[j];
152 }
154 }
156 }
157
158 /**
159 * @return (1<<64) * a / b, without overflow, if a < b
160 */
162 {
165
166 if (
b < (uint64_t)1 << 32) {
/* b small, use two 32-bits steps */
168 return ((
a /
b) << 32) | ((
a %
b) << 32) /
b;
169 }
170 if (
b < (uint64_t)1 << 48) {
/* b medium, use four 16-bits steps */
171 for (
i = 0;
i < 4;
i++) {
173 r = (
r << 16) | (
a /
b);
175 }
177 }
178 for (
i = 63;
i >= 0;
i--) {
179 if (
a >= (uint64_t)1 << 63 || a << 1 >=
b) {
180 r |= (uint64_t)1 <<
i;
182 } else {
184 }
185 }
187 }
188
190 {
191 uint64_t dt = ts - (uint64_t)in->
ts_start;
192 uint64_t dt2 = dt & 1 ? /* dt * (dt - 1) / 2 without overflow */
193 dt * ((dt - 1) >> 1) : (dt >> 1) * (dt - 1);
195 }
196
198 {
201
206 break;
208 continue;
214 }
217 *last = -1;
221 uint64_t pink_ts_next = ts & ~(
PINK_UNIT - 1);
227 } else {
229 }
230 }
232 }
233
235 {
238 uint8_t *edata, *edata_end;
241 int64_t dphi1, dphi2, dt, cur_ts = -0x8000000000000000;
243
249 edata += 4;
257 if (edata_end - edata < 24)
263 edata += 24;
267 )
273 if (edata_end - edata < 20 || avc->
sample_rate <= 0)
280 edata += 20;
284 in->
ddphi = (int64_t)(dphi2 - (uint64_t)dphi1) / dt;
285 if (
phi & 0x80000000) {
290 } else {
291 in->
phi0 = (uint64_t)
phi << 33;
292 }
293 break;
295 if (edata_end - edata < 8)
299 edata += 8;
300 break;
301 default:
303 }
304 in->
amp0 = (uint64_t)
a1 << 32;
305 in->
damp = (int64_t)(((uint64_t)
a2 << 32) - ((uint64_t)
a1 << 32)) / dt;
306 }
307 if (edata != edata_end)
309 return 0;
310 }
311
313 {
316
319 "This implementation is limited to %d channels.\n",
322 }
327 }
340 return 0;
341 }
342
345 {
350 uint32_t
c, all_ch = 0;
351
362 continue;
363 }
372 break;
374 val =
amp * (unsigned)pink;
375 break;
376 default:
378 }
382 *cv += (unsigned)
val;
383 }
388 }
389
391 {
394
401 break;
403 continue;
409 }
412 *last = -1;
413 }
414
417 {
420 int64_t ts;
423 int16_t *pcm;
425
426 *rgot_frame = 0;
427 if (packet->
size != 12)
439 pcm = (int16_t *)
frame->data[0];
447 }
449 *rgot_frame = 1;
451 }
452
454 {
456
459 return 0;
460 }
461
473 };