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
105 };
106
107 #define LCG_A 1284865837
108 #define LCG_C 4150755663
109 #define LCG_AI 849225893 /* A*AI = 1 [mod 1<<32] */
110
112 {
114 return *s;
115 }
116
118 {
119 uint32_t
a,
c,
t = *s;
120
121 if (dt >= 0) {
124 } else { /* coefficients for a step backward */
127 dt = -dt;
128 }
129 while (dt) {
130 if (dt & 1)
132 c *= a + 1; /* coefficients for a double step */
134 dt >>= 1;
135 }
137 }
138
139 /* Emulate pink noise by summing white noise at the sampling frequency,
140 * white noise at half the sampling frequency (each value taken twice),
141 * etc., with a total of 8 octaves.
142 * This is known as the Voss-McCartney algorithm. */
143
145 {
147 int i, j;
148
151 return;
153 for (j = 0; j < 7; j++) {
154 if ((i >> j) & 1)
155 break;
156 v -= vt[j];
158 v += vt[j];
159 }
161 }
163 }
164
165 /**
166 * @return (1<<64) * a / b, without overflow, if a < b
167 */
169 {
171 int i;
172
173 if (b < (uint64_t)1 << 32) { /* b small, use two 32-bits steps */
174 a <<= 32;
175 return ((a / b) << 32) | ((a %
b) << 32) /
b;
176 }
177 if (b < (uint64_t)1 << 48) { /* b medium, use four 16-bits steps */
178 for (i = 0; i < 4; i++) {
179 a <<= 16;
180 r = (r << 16) | (a / b);
182 }
184 }
185 for (i = 63; i >= 0; i--) {
186 if (a >= (uint64_t)1 << 63 || a << 1 >=
b) {
187 r |= (uint64_t)1 << i;
188 a = (a << 1) - b;
189 } else {
190 a <<= 1;
191 }
192 }
194 }
195
197 {
199 uint64_t dt2 = dt & 1 ? /* dt * (dt - 1) / 2 without overflow */
200 dt * ((dt - 1) >> 1) : (dt >> 1) * (dt - 1);
202 }
203
205 {
206 int *last, i;
208
210 for (i = 0; i < ws->
nb_inter; i++) {
213 break;
215 continue;
216 *last = i;
221 }
224 *last = -1;
228 int64_t pink_ts_next = ts & ~(
PINK_UNIT - 1);
231 if (pos) {
234 } else {
236 }
237 }
239 }
240
242 {
248 int64_t dphi1, dphi2, dt, cur_ts = -0x8000000000000000;
249 int i;
250
256 edata += 4;
262 for (i = 0; i < ws->
nb_inter; i++) {
264 if (edata_end - edata < 24)
270 edata += 24;
277 if (edata_end - edata < 20)
284 edata += 20;
288 in->
ddphi = (dphi2 - dphi1) / dt;
289 if (phi & 0x80000000) {
290 phi &= ~0x80000000;
291 if (phi >= i)
294 } else {
295 in->
phi0 = (uint64_t)phi << 33;
296 }
297 break;
299 if (edata_end - edata < 8)
303 edata += 8;
304 break;
305 default:
307 }
308 in->
amp0 = (int64_t)a1 << 32;
309 in->
damp = (((int64_t)a2 << 32) - ((int64_t)a1 << 32)) / dt;
310 }
311 if (edata != edata_end)
313 return 0;
314 }
315
317 {
320
323 "This implementation is limited to %d channels.\n",
326 }
328 if (r < 0) {
330 goto fail;
331 }
335 goto fail;
336 }
338 ws->
sin[i] = floor(32767 *
sin(2 *
M_PI * i / (1 << SIN_BITS)));
348 return 0;
349
350 fail:
354 }
355
358 {
361 int i, *last, pink;
362 uint32_t
c, all_ch = 0;
363
369 while (i >= 0) {
373 *last = i;
374 continue;
375 }
384 break;
386 val = amp * pink;
387 break;
388 default:
389 val = 0;
390 }
392 for (c = in->
channels, cv = channels; c; c >>= 1, cv++)
393 if (c & 1)
394 *cv += val;
395 }
397 for (c = all_ch, cv = channels;
c; c >>= 1, cv++)
398 if (c & 1)
399 *cv += val;
400 }
401
403 {
404 int *last, i;
406
410 for (i = ws->
next_inter; i < ws->nb_inter; i++) {
413 break;
415 continue;
416 *last = i;
421 }
424 *last = -1;
425 }
426
429 {
431 int64_t ts;
434 int16_t *pcm;
436
437 *rgot_frame = 0;
438 if (packet->
size != 12)
444 if (duration <= 0)
448 if (r < 0)
452 memset(channels, 0, avc->
channels *
sizeof(*channels));
457 *(pcm++) = channels[
c] >> 16;
458 }
460 *rgot_frame = 1;
463 }
464
466 {
468
471 return 0;
472 }
473
484 };