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
28
29
31 #define WS_MAX_CHANNELS 32
32 #define INF_TS 0x7FFFFFFFFFFFFFFF
33
35
36 /*
37 Format of the extradata and packets
38
39 THIS INFORMATION IS NOT PART OF THE PUBLIC API OR ABI.
40 IT CAN CHANGE WITHOUT NOTIFICATION.
41
42 All numbers are in little endian.
43
44 The codec extradata define a set of intervals with uniform content.
45 Overlapping intervals are added together.
46
47 extradata:
48 uint32 number of intervals
49 ... intervals
50
51 interval:
52 int64 start timestamp; time_base must be 1/sample_rate;
53 start timestamps must be in ascending order
54 int64 end timestamp
55 uint32 type
56 uint32 channels mask
57 ... additional information, depends on type
58
59 sine interval (type fourcc "SINE"):
60 int32 start frequency, in 1/(1<<16) Hz
61 int32 end frequency
62 int32 start amplitude, 1<<16 is the full amplitude
63 int32 end amplitude
64 uint32 start phase, 0 is sin(0), 0x20000000 is sin(pi/2), etc.;
65 n | (1<<31) means to match the phase of previous channel #n
66
67 pink noise interval (type fourcc "NOIS"):
68 int32 start amplitude
69 int32 end amplitude
70
71 The input packets encode the time and duration of the requested segment.
72
73 packet:
74 int64 start timestamp
75 int32 duration
76
77 */
78
82 };
83
92 };
93
106 };
107
108 #define LCG_A 1284865837
109 #define LCG_C 4150755663
110 #define LCG_AI 849225893 /* A*AI = 1 [mod 1<<32] */
111
113 {
116 }
117
119 {
120 uint32_t
a,
c, t = *
s;
121
124 while (dt) {
125 if (dt & 1)
127 c *=
a + 1;
/* coefficients for a double step */
129 dt >>= 1;
130 }
132 }
133
134 /* Emulate pink noise by summing white noise at the sampling frequency,
135 * white noise at half the sampling frequency (each value taken twice),
136 * etc., with a total of 8 octaves.
137 * This is known as the Voss-McCartney algorithm. */
138
140 {
143
146 return;
148 for (j = 0; j < 7; j++) {
150 break;
151 v -= vt[j];
153 v += vt[j];
154 }
156 }
158 }
159
160 /**
161 * @return (1<<64) * a / b, without overflow, if a < b
162 */
164 {
167
168 if (
b < (uint64_t)1 << 32) {
/* b small, use two 32-bits steps */
170 return ((
a /
b) << 32) | ((
a %
b) << 32) /
b;
171 }
172 if (
b < (uint64_t)1 << 48) {
/* b medium, use four 16-bits steps */
173 for (
i = 0;
i < 4;
i++) {
175 r = (
r << 16) | (
a /
b);
177 }
179 }
180 for (
i = 63;
i >= 0;
i--) {
181 if (
a >= (uint64_t)1 << 63 || a << 1 >=
b) {
182 r |= (uint64_t)1 <<
i;
184 } else {
186 }
187 }
189 }
190
192 {
193 uint64_t dt = ts - (uint64_t)in->
ts_start;
194 uint64_t dt2 = dt & 1 ? /* dt * (dt - 1) / 2 without overflow */
195 dt * ((dt - 1) >> 1) : (dt >> 1) * (dt - 1);
197 }
198
200 {
203
208 break;
210 continue;
216 }
219 *last = -1;
223 uint64_t pink_ts_next = ts & ~(
PINK_UNIT - 1);
229 } else {
231 }
232 }
234 }
235
237 {
240 uint8_t *edata, *edata_end;
243 int64_t dphi1, dphi2, dt, cur_ts = -0x8000000000000000;
245
251 edata += 4;
259 if (edata_end - edata < 24)
265 edata += 24;
269 )
275 if (edata_end - edata < 20 || avc->sample_rate <= 0)
282 edata += 20;
287 if (
phi & 0x80000000) {
292 } else {
293 in->
phi0 = (uint64_t)
phi << 33;
294 }
295 break;
297 if (edata_end - edata < 8)
301 edata += 8;
302 break;
303 default:
305 }
306 in->
amp0 = (uint64_t)
a1 << 32;
307 in->
damp = (
int64_t)(((uint64_t)
a2 << 32) - ((uint64_t)
a1 << 32)) / dt;
308 }
309 if (edata != edata_end)
311 return 0;
312 }
313
315 {
318
321 "This implementation is limited to %d channels.\n",
324 }
329 }
342 return 0;
343 }
344
347 {
352 uint32_t
c, all_ch = 0;
353
364 continue;
365 }
374 break;
376 val =
amp * (unsigned)pink;
377 break;
378 default:
380 }
384 *cv += (unsigned)
val;
385 }
390 }
391
393 {
396
403 break;
405 continue;
411 }
414 *last = -1;
415 }
416
419 {
424 int16_t *pcm;
426
427 *rgot_frame = 0;
428 if (packet->
size != 12)
440 pcm = (int16_t *)
frame->data[0];
448 }
450 *rgot_frame = 1;
452 }
453
455 {
457
460 return 0;
461 }
462
464 .
p.
name =
"wavesynth",
474 };