/src/ffmpeg/libavcodec/ffwavesynth.c
Line | Count | Source |
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 | | |
22 | | #include "libavutil/intreadwrite.h" |
23 | | #include "libavutil/log.h" |
24 | | #include "libavutil/mem.h" |
25 | | #include "avcodec.h" |
26 | | #include "codec_internal.h" |
27 | | #include "decode.h" |
28 | | |
29 | | |
30 | 17.4M | #define SIN_BITS 14 |
31 | 1.09k | #define WS_MAX_CHANNELS 32 |
32 | 130k | #define INF_TS 0x7FFFFFFFFFFFFFFF |
33 | | |
34 | 245M | #define PINK_UNIT 128 |
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 | | |
79 | | enum ws_interval_type { |
80 | | WS_SINE = MKTAG('S','I','N','E'), |
81 | | WS_NOISE = MKTAG('N','O','I','S'), |
82 | | }; |
83 | | |
84 | | struct ws_interval { |
85 | | int64_t ts_start, ts_end; |
86 | | uint64_t phi0, dphi0, ddphi; |
87 | | uint64_t amp0, damp; |
88 | | uint64_t phi, dphi, amp; |
89 | | uint32_t channels; |
90 | | enum ws_interval_type type; |
91 | | int next; |
92 | | }; |
93 | | |
94 | | struct wavesynth_context { |
95 | | int64_t cur_ts; |
96 | | int64_t next_ts; |
97 | | int32_t *sin; |
98 | | struct ws_interval *inter; |
99 | | uint32_t dither_state; |
100 | | uint32_t pink_state; |
101 | | int32_t pink_pool[PINK_UNIT]; |
102 | | unsigned pink_need, pink_pos; |
103 | | int nb_inter; |
104 | | int cur_inter; |
105 | | int next_inter; |
106 | | }; |
107 | | |
108 | 333M | #define LCG_A 1284865837 |
109 | 333M | #define LCG_C 4150755663 |
110 | | #define LCG_AI 849225893 /* A*AI = 1 [mod 1<<32] */ |
111 | | |
112 | | static uint32_t lcg_next(uint32_t *s) |
113 | 333M | { |
114 | 333M | *s = *s * LCG_A + LCG_C; |
115 | 333M | return *s; |
116 | 333M | } |
117 | | |
118 | | static void lcg_seek(uint32_t *s, uint32_t dt) |
119 | 117k | { |
120 | 117k | uint32_t a, c, t = *s; |
121 | | |
122 | 117k | a = LCG_A; |
123 | 117k | c = LCG_C; |
124 | 3.71M | while (dt) { |
125 | 3.59M | if (dt & 1) |
126 | 2.80M | t = a * t + c; |
127 | 3.59M | c *= a + 1; /* coefficients for a double step */ |
128 | 3.59M | a *= a; |
129 | 3.59M | dt >>= 1; |
130 | 3.59M | } |
131 | 117k | *s = t; |
132 | 117k | } |
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 | | |
139 | | static void pink_fill(struct wavesynth_context *ws) |
140 | 1.25M | { |
141 | 1.25M | int32_t vt[7] = { 0 }, v = 0; |
142 | 1.25M | int i, j; |
143 | | |
144 | 1.25M | ws->pink_pos = 0; |
145 | 1.25M | if (!ws->pink_need) |
146 | 558k | return; |
147 | 89.7M | for (i = 0; i < PINK_UNIT; i++) { |
148 | 177M | for (j = 0; j < 7; j++) { |
149 | 176M | if ((i >> j) & 1) |
150 | 88.3M | break; |
151 | 88.3M | v -= vt[j]; |
152 | 88.3M | vt[j] = (int32_t)lcg_next(&ws->pink_state) >> 3; |
153 | 88.3M | v += vt[j]; |
154 | 88.3M | } |
155 | 89.0M | ws->pink_pool[i] = v + ((int32_t)lcg_next(&ws->pink_state) >> 3); |
156 | 89.0M | } |
157 | 695k | lcg_next(&ws->pink_state); /* so we use exactly 256 steps */ |
158 | 695k | } |
159 | | |
160 | | /** |
161 | | * @return (1<<64) * a / b, without overflow, if a < b |
162 | | */ |
163 | | static uint64_t frac64(uint64_t a, uint64_t b) |
164 | 226 | { |
165 | 226 | uint64_t r = 0; |
166 | 226 | int i; |
167 | | |
168 | 226 | if (b < (uint64_t)1 << 32) { /* b small, use two 32-bits steps */ |
169 | 58 | a <<= 32; |
170 | 58 | return ((a / b) << 32) | ((a % b) << 32) / b; |
171 | 58 | } |
172 | 168 | if (b < (uint64_t)1 << 48) { /* b medium, use four 16-bits steps */ |
173 | 840 | for (i = 0; i < 4; i++) { |
174 | 672 | a <<= 16; |
175 | 672 | r = (r << 16) | (a / b); |
176 | 672 | a %= b; |
177 | 672 | } |
178 | 168 | return r; |
179 | 168 | } |
180 | 0 | for (i = 63; i >= 0; i--) { |
181 | 0 | if (a >= (uint64_t)1 << 63 || a << 1 >= b) { |
182 | 0 | r |= (uint64_t)1 << i; |
183 | 0 | a = (a << 1) - b; |
184 | 0 | } else { |
185 | 0 | a <<= 1; |
186 | 0 | } |
187 | 0 | } |
188 | 0 | return r; |
189 | 168 | } |
190 | | |
191 | | static uint64_t phi_at(struct ws_interval *in, int64_t ts) |
192 | 49.5k | { |
193 | 49.5k | uint64_t dt = ts - (uint64_t)in->ts_start; |
194 | 49.5k | uint64_t dt2 = dt & 1 ? /* dt * (dt - 1) / 2 without overflow */ |
195 | 47.5k | dt * ((dt - 1) >> 1) : (dt >> 1) * (dt - 1); |
196 | 49.5k | return in->phi0 + dt * in->dphi0 + dt2 * in->ddphi; |
197 | 49.5k | } |
198 | | |
199 | | static void wavesynth_seek(struct wavesynth_context *ws, int64_t ts) |
200 | 61.1k | { |
201 | 61.1k | int *last, i; |
202 | 61.1k | struct ws_interval *in; |
203 | | |
204 | 61.1k | last = &ws->cur_inter; |
205 | 118k | for (i = 0; i < ws->nb_inter; i++) { |
206 | 110k | in = &ws->inter[i]; |
207 | 110k | if (ts < in->ts_start) |
208 | 53.5k | break; |
209 | 57.0k | if (ts >= in->ts_end) |
210 | 7.55k | continue; |
211 | 49.5k | *last = i; |
212 | 49.5k | last = &in->next; |
213 | 49.5k | in->phi = phi_at(in, ts); |
214 | 49.5k | in->dphi = in->dphi0 + (ts - in->ts_start) * in->ddphi; |
215 | 49.5k | in->amp = in->amp0 + (ts - in->ts_start) * in->damp; |
216 | 49.5k | } |
217 | 61.1k | ws->next_inter = i; |
218 | 61.1k | ws->next_ts = i < ws->nb_inter ? ws->inter[i].ts_start : INF_TS; |
219 | 61.1k | *last = -1; |
220 | 61.1k | lcg_seek(&ws->dither_state, (uint32_t)ts - (uint32_t)ws->cur_ts); |
221 | 61.1k | if (ws->pink_need) { |
222 | 56.7k | uint64_t pink_ts_cur = (ws->cur_ts + (uint64_t)PINK_UNIT - 1) & ~(PINK_UNIT - 1); |
223 | 56.7k | uint64_t pink_ts_next = ts & ~(PINK_UNIT - 1); |
224 | 56.7k | int pos = ts & (PINK_UNIT - 1); |
225 | 56.7k | lcg_seek(&ws->pink_state, (uint32_t)(pink_ts_next - pink_ts_cur) * 2); |
226 | 56.7k | if (pos) { |
227 | 3.40k | pink_fill(ws); |
228 | 3.40k | ws->pink_pos = pos; |
229 | 53.3k | } else { |
230 | 53.3k | ws->pink_pos = PINK_UNIT; |
231 | 53.3k | } |
232 | 56.7k | } |
233 | 61.1k | ws->cur_ts = ts; |
234 | 61.1k | } |
235 | | |
236 | | static int wavesynth_parse_extradata(AVCodecContext *avc) |
237 | 933 | { |
238 | 933 | struct wavesynth_context *ws = avc->priv_data; |
239 | 933 | struct ws_interval *in; |
240 | 933 | uint8_t *edata, *edata_end; |
241 | 933 | int32_t f1, f2, a1, a2; |
242 | 933 | uint32_t phi; |
243 | 933 | int64_t dphi1, dphi2, dt, cur_ts = -0x8000000000000000; |
244 | 933 | int i; |
245 | | |
246 | 933 | if (avc->extradata_size < 4) |
247 | 41 | return AVERROR(EINVAL); |
248 | 892 | edata = avc->extradata; |
249 | 892 | edata_end = edata + avc->extradata_size; |
250 | 892 | ws->nb_inter = AV_RL32(edata); |
251 | 892 | edata += 4; |
252 | 892 | if (ws->nb_inter < 0 || (edata_end - edata) / 24 < ws->nb_inter) |
253 | 74 | return AVERROR(EINVAL); |
254 | 818 | ws->inter = av_calloc(ws->nb_inter, sizeof(*ws->inter)); |
255 | 818 | if (!ws->inter) |
256 | 0 | return AVERROR(ENOMEM); |
257 | 1.33k | for (i = 0; i < ws->nb_inter; i++) { |
258 | 809 | in = &ws->inter[i]; |
259 | 809 | if (edata_end - edata < 24) |
260 | 5 | return AVERROR(EINVAL); |
261 | 804 | in->ts_start = AV_RL64(edata + 0); |
262 | 804 | in->ts_end = AV_RL64(edata + 8); |
263 | 804 | in->type = AV_RL32(edata + 16); |
264 | 804 | in->channels = AV_RL32(edata + 20); |
265 | 804 | edata += 24; |
266 | 804 | if (in->ts_start < cur_ts || |
267 | 802 | in->ts_end <= in->ts_start || |
268 | 778 | (uint64_t)in->ts_end - in->ts_start > INT64_MAX |
269 | 804 | ) |
270 | 86 | return AVERROR(EINVAL); |
271 | 718 | cur_ts = in->ts_start; |
272 | 718 | dt = in->ts_end - in->ts_start; |
273 | 718 | switch (in->type) { |
274 | 116 | case WS_SINE: |
275 | 116 | if (edata_end - edata < 20 || avc->sample_rate <= 0) |
276 | 3 | return AVERROR(EINVAL); |
277 | 113 | f1 = AV_RL32(edata + 0); |
278 | 113 | f2 = AV_RL32(edata + 4); |
279 | 113 | a1 = AV_RL32(edata + 8); |
280 | 113 | a2 = AV_RL32(edata + 12); |
281 | 113 | phi = AV_RL32(edata + 16); |
282 | 113 | edata += 20; |
283 | 113 | dphi1 = frac64(f1, (int64_t)avc->sample_rate << 16); |
284 | 113 | dphi2 = frac64(f2, (int64_t)avc->sample_rate << 16); |
285 | 113 | in->dphi0 = dphi1; |
286 | 113 | in->ddphi = (int64_t)(dphi2 - (uint64_t)dphi1) / dt; |
287 | 113 | if (phi & 0x80000000) { |
288 | 60 | phi &= ~0x80000000; |
289 | 60 | if (phi >= i) |
290 | 37 | return AVERROR(EINVAL); |
291 | 23 | in->phi0 = phi_at(&ws->inter[phi], in->ts_start); |
292 | 53 | } else { |
293 | 53 | in->phi0 = (uint64_t)phi << 33; |
294 | 53 | } |
295 | 76 | break; |
296 | 442 | case WS_NOISE: |
297 | 442 | if (edata_end - edata < 8) |
298 | 2 | return AVERROR(EINVAL); |
299 | 440 | a1 = AV_RL32(edata + 0); |
300 | 440 | a2 = AV_RL32(edata + 4); |
301 | 440 | edata += 8; |
302 | 440 | break; |
303 | 160 | default: |
304 | 160 | return AVERROR(EINVAL); |
305 | 718 | } |
306 | 516 | in->amp0 = (uint64_t)a1 << 32; |
307 | 516 | in->damp = (int64_t)(((uint64_t)a2 << 32) - ((uint64_t)a1 << 32)) / dt; |
308 | 516 | } |
309 | 525 | if (edata != edata_end) |
310 | 17 | return AVERROR(EINVAL); |
311 | 508 | return 0; |
312 | 525 | } |
313 | | |
314 | | static av_cold int wavesynth_init(AVCodecContext *avc) |
315 | 1.01k | { |
316 | 1.01k | struct wavesynth_context *ws = avc->priv_data; |
317 | 1.01k | int i, r; |
318 | | |
319 | 1.01k | if (avc->ch_layout.nb_channels > WS_MAX_CHANNELS) { |
320 | 79 | av_log(avc, AV_LOG_ERROR, |
321 | 79 | "This implementation is limited to %d channels.\n", |
322 | 79 | WS_MAX_CHANNELS); |
323 | 79 | return AVERROR(EINVAL); |
324 | 79 | } |
325 | 933 | r = wavesynth_parse_extradata(avc); |
326 | 933 | if (r < 0) { |
327 | 425 | av_log(avc, AV_LOG_ERROR, "Invalid intervals definitions.\n"); |
328 | 425 | return r; |
329 | 425 | } |
330 | 508 | ws->sin = av_malloc(sizeof(*ws->sin) << SIN_BITS); |
331 | 508 | if (!ws->sin) |
332 | 0 | return AVERROR(ENOMEM); |
333 | 8.32M | for (i = 0; i < 1 << SIN_BITS; i++) |
334 | 8.32M | ws->sin[i] = floor(32767 * sin(2 * M_PI * i / (1 << SIN_BITS))); |
335 | 508 | ws->dither_state = MKTAG('D','I','T','H'); |
336 | 934 | for (i = 0; i < ws->nb_inter; i++) |
337 | 426 | ws->pink_need += ws->inter[i].type == WS_NOISE; |
338 | 508 | ws->pink_state = MKTAG('P','I','N','K'); |
339 | 508 | ws->pink_pos = PINK_UNIT; |
340 | 508 | wavesynth_seek(ws, 0); |
341 | 508 | avc->sample_fmt = AV_SAMPLE_FMT_S16; |
342 | 508 | return 0; |
343 | 508 | } |
344 | | |
345 | | static void wavesynth_synth_sample(struct wavesynth_context *ws, int64_t ts, |
346 | | int32_t *channels) |
347 | 155M | { |
348 | 155M | int32_t amp, *cv; |
349 | 155M | unsigned val; |
350 | 155M | struct ws_interval *in; |
351 | 155M | int i, *last, pink; |
352 | 155M | uint32_t c, all_ch = 0; |
353 | | |
354 | 155M | i = ws->cur_inter; |
355 | 155M | last = &ws->cur_inter; |
356 | 155M | if (ws->pink_pos == PINK_UNIT) |
357 | 1.25M | pink_fill(ws); |
358 | 155M | pink = ws->pink_pool[ws->pink_pos++] >> 16; |
359 | 217M | while (i >= 0) { |
360 | 62.6M | in = &ws->inter[i]; |
361 | 62.6M | i = in->next; |
362 | 62.6M | if (ts >= in->ts_end) { |
363 | 44.2k | *last = i; |
364 | 44.2k | continue; |
365 | 44.2k | } |
366 | 62.5M | last = &in->next; |
367 | 62.5M | amp = in->amp >> 32; |
368 | 62.5M | in->amp += in->damp; |
369 | 62.5M | switch (in->type) { |
370 | 754k | case WS_SINE: |
371 | 754k | val = amp * (unsigned)ws->sin[in->phi >> (64 - SIN_BITS)]; |
372 | 754k | in->phi += in->dphi; |
373 | 754k | in->dphi += in->ddphi; |
374 | 754k | break; |
375 | 61.8M | case WS_NOISE: |
376 | 61.8M | val = amp * (unsigned)pink; |
377 | 61.8M | break; |
378 | 0 | default: |
379 | 0 | val = 0; |
380 | 62.5M | } |
381 | 62.5M | all_ch |= in->channels; |
382 | 1.77G | for (c = in->channels, cv = channels; c; c >>= 1, cv++) |
383 | 1.71G | if (c & 1) |
384 | 693M | *cv += (unsigned)val; |
385 | 62.5M | } |
386 | 155M | val = (int32_t)lcg_next(&ws->dither_state) >> 16; |
387 | 1.39G | for (c = all_ch, cv = channels; c; c >>= 1, cv++) |
388 | 1.23G | if (c & 1) |
389 | 628M | *cv += val; |
390 | 155M | } |
391 | | |
392 | | static void wavesynth_enter_intervals(struct wavesynth_context *ws, int64_t ts) |
393 | 56.0k | { |
394 | 56.0k | int *last, i; |
395 | 56.0k | struct ws_interval *in; |
396 | | |
397 | 56.0k | last = &ws->cur_inter; |
398 | 111k | for (i = ws->cur_inter; i >= 0; i = ws->inter[i].next) |
399 | 55.1k | last = &ws->inter[i].next; |
400 | 111k | for (i = ws->next_inter; i < ws->nb_inter; i++) { |
401 | 106k | in = &ws->inter[i]; |
402 | 106k | if (ts < in->ts_start) |
403 | 50.9k | break; |
404 | 55.8k | if (ts >= in->ts_end) |
405 | 0 | continue; |
406 | 55.8k | *last = i; |
407 | 55.8k | last = &in->next; |
408 | 55.8k | in->phi = in->phi0; |
409 | 55.8k | in->dphi = in->dphi0; |
410 | 55.8k | in->amp = in->amp0; |
411 | 55.8k | } |
412 | 56.0k | ws->next_inter = i; |
413 | 56.0k | ws->next_ts = i < ws->nb_inter ? ws->inter[i].ts_start : INF_TS; |
414 | 56.0k | *last = -1; |
415 | 56.0k | } |
416 | | |
417 | | static int wavesynth_decode(AVCodecContext *avc, AVFrame *frame, |
418 | | int *rgot_frame, AVPacket *packet) |
419 | 231k | { |
420 | 231k | struct wavesynth_context *ws = avc->priv_data; |
421 | 231k | int64_t ts; |
422 | 231k | int duration; |
423 | 231k | int s, c, r; |
424 | 231k | int16_t *pcm; |
425 | 231k | int32_t channels[WS_MAX_CHANNELS]; |
426 | | |
427 | 231k | *rgot_frame = 0; |
428 | 231k | if (packet->size != 12) |
429 | 170k | return AVERROR_INVALIDDATA; |
430 | 61.2k | ts = AV_RL64(packet->data); |
431 | 61.2k | if (ts != ws->cur_ts) |
432 | 60.6k | wavesynth_seek(ws, ts); |
433 | 61.2k | duration = AV_RL32(packet->data + 8); |
434 | 61.2k | if (duration <= 0) |
435 | 1.39k | return AVERROR(EINVAL); |
436 | 59.8k | frame->nb_samples = duration; |
437 | 59.8k | r = ff_get_buffer(avc, frame, 0); |
438 | 59.8k | if (r < 0) |
439 | 3.56k | return r; |
440 | 56.2k | pcm = (int16_t *)frame->data[0]; |
441 | 155M | for (s = 0; s < duration; s++, ts+=(uint64_t)1) { |
442 | 155M | memset(channels, 0, avc->ch_layout.nb_channels * sizeof(*channels)); |
443 | 155M | if (ts >= ws->next_ts) |
444 | 56.0k | wavesynth_enter_intervals(ws, ts); |
445 | 155M | wavesynth_synth_sample(ws, ts, channels); |
446 | 2.29G | for (c = 0; c < avc->ch_layout.nb_channels; c++) |
447 | 2.13G | *(pcm++) = channels[c] >> 16; |
448 | 155M | } |
449 | 56.2k | ws->cur_ts += (uint64_t)duration; |
450 | 56.2k | *rgot_frame = 1; |
451 | 56.2k | return packet->size; |
452 | 59.8k | } |
453 | | |
454 | | static av_cold int wavesynth_close(AVCodecContext *avc) |
455 | 1.01k | { |
456 | 1.01k | struct wavesynth_context *ws = avc->priv_data; |
457 | | |
458 | 1.01k | av_freep(&ws->sin); |
459 | 1.01k | av_freep(&ws->inter); |
460 | 1.01k | return 0; |
461 | 1.01k | } |
462 | | |
463 | | const FFCodec ff_ffwavesynth_decoder = { |
464 | | .p.name = "wavesynth", |
465 | | CODEC_LONG_NAME("Wave synthesis pseudo-codec"), |
466 | | .p.type = AVMEDIA_TYPE_AUDIO, |
467 | | .p.id = AV_CODEC_ID_FFWAVESYNTH, |
468 | | .priv_data_size = sizeof(struct wavesynth_context), |
469 | | .init = wavesynth_init, |
470 | | .close = wavesynth_close, |
471 | | FF_CODEC_DECODE_CB(wavesynth_decode), |
472 | | .p.capabilities = AV_CODEC_CAP_DR1, |
473 | | .caps_internal = FF_CODEC_CAP_INIT_CLEANUP, |
474 | | }; |