]> git.sesse.net Git - ffmpeg/blob - libavcodec/ffwavesynth.c
aasc: fix 16bpp on big-endian
[ffmpeg] / libavcodec / ffwavesynth.c
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 "avcodec.h"
25 #include "internal.h"
26
27
28 #define SIN_BITS 14
29 #define WS_MAX_CHANNELS 32
30 #define INF_TS 0x7FFFFFFFFFFFFFFF
31
32 #define PINK_UNIT 128
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
77 enum ws_interval_type {
78     WS_SINE  = MKTAG('S','I','N','E'),
79     WS_NOISE = MKTAG('N','O','I','S'),
80 };
81
82 struct ws_interval {
83     int64_t ts_start, ts_end;
84     uint64_t phi0, dphi0, ddphi;
85     uint64_t amp0, damp;
86     uint64_t phi, dphi, amp;
87     uint32_t channels;
88     enum ws_interval_type type;
89     int next;
90 };
91
92 struct wavesynth_context {
93     int64_t cur_ts;
94     int64_t next_ts;
95     int32_t *sin;
96     AVFrame frame;
97     struct ws_interval *inter;
98     uint32_t dither_state;
99     uint32_t pink_state;
100     int32_t pink_pool[PINK_UNIT];
101     unsigned pink_need, pink_pos;
102     int nb_inter;
103     int cur_inter;
104     int next_inter;
105 };
106
107 #define LCG_A 1284865837
108 #define LCG_C 4150755663
109 #define LCG_AI 849225893 /* A*AI = 1 [mod 1<<32] */
110
111 static uint32_t lcg_next(uint32_t *s)
112 {
113     *s = *s * LCG_A + LCG_C;
114     return *s;
115 }
116
117 static void lcg_seek(uint32_t *s, int64_t dt)
118 {
119     uint32_t a, c, t = *s;
120
121     if (dt >= 0) {
122         a = LCG_A;
123         c = LCG_C;
124     } else { /* coefficients for a step backward */
125         a = LCG_AI;
126         c = (uint32_t)(LCG_AI * LCG_C);
127         dt = -dt;
128     }
129     while (dt) {
130         if (dt & 1)
131             t = a * t + c;
132         c *= a + 1; /* coefficients for a double step */
133         a *= a;
134         dt >>= 1;
135     }
136     *s = t;
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
144 static void pink_fill(struct wavesynth_context *ws)
145 {
146     int32_t vt[7] = { 0 }, v = 0;
147     int i, j;
148
149     ws->pink_pos = 0;
150     if (!ws->pink_need)
151         return;
152     for (i = 0; i < PINK_UNIT; i++) {
153         for (j = 0; j < 7; j++) {
154             if ((i >> j) & 1)
155                 break;
156             v -= vt[j];
157             vt[j] = (int32_t)lcg_next(&ws->pink_state) >> 3;
158             v += vt[j];
159         }
160         ws->pink_pool[i] = v + ((int32_t)lcg_next(&ws->pink_state) >> 3);
161     }
162     lcg_next(&ws->pink_state); /* so we use exactly 256 steps */
163 }
164
165 /**
166  * @return  (1<<64) * a / b, without overflow, if a < b
167  */
168 static uint64_t frac64(uint64_t a, uint64_t b)
169 {
170     uint64_t r = 0;
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);
181             a %= b;
182         }
183         return r;
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     }
193     return r;
194 }
195
196 static uint64_t phi_at(struct ws_interval *in, int64_t ts)
197 {
198     uint64_t dt = ts - in->ts_start;
199     uint64_t dt2 = dt & 1 ? /* dt * (dt - 1) / 2 without overflow */
200                    dt * ((dt - 1) >> 1) : (dt >> 1) * (dt - 1);
201     return in->phi0 + dt * in->dphi0 + dt2 * in->ddphi;
202 }
203
204 static void wavesynth_seek(struct wavesynth_context *ws, int64_t ts)
205 {
206     int *last, i;
207     struct ws_interval *in;
208
209     last = &ws->cur_inter;
210     for (i = 0; i < ws->nb_inter; i++) {
211         in = &ws->inter[i];
212         if (ts < in->ts_start)
213             break;
214         if (ts >= in->ts_end)
215             continue;
216         *last = i;
217         last = &in->next;
218         in->phi  = phi_at(in, ts);
219         in->dphi = in->dphi0 + (ts - in->ts_start) * in->ddphi;
220         in->amp  = in->amp0  + (ts - in->ts_start) * in->damp;
221     }
222     ws->next_inter = i;
223     ws->next_ts = i < ws->nb_inter ? ws->inter[i].ts_start : INF_TS;
224     *last = -1;
225     lcg_seek(&ws->dither_state, ts - ws->cur_ts);
226     if (ws->pink_need) {
227         int64_t pink_ts_cur  = (ws->cur_ts + PINK_UNIT - 1) & ~(PINK_UNIT - 1);
228         int64_t pink_ts_next = ts & ~(PINK_UNIT - 1);
229         int pos = ts & (PINK_UNIT - 1);
230         lcg_seek(&ws->pink_state, (pink_ts_next - pink_ts_cur) << 1);
231         if (pos) {
232             pink_fill(ws);
233             ws->pink_pos = pos;
234         } else {
235             ws->pink_pos = PINK_UNIT;
236         }
237     }
238     ws->cur_ts = ts;
239 }
240
241 static int wavesynth_parse_extradata(AVCodecContext *avc)
242 {
243     struct wavesynth_context *ws = avc->priv_data;
244     struct ws_interval *in;
245     uint8_t *edata, *edata_end;
246     int32_t f1, f2, a1, a2;
247     uint32_t phi;
248     int64_t dphi1, dphi2, dt, cur_ts = -0x8000000000000000;
249     int i;
250
251     if (avc->extradata_size < 4)
252         return AVERROR(EINVAL);
253     edata = avc->extradata;
254     edata_end = edata + avc->extradata_size;
255     ws->nb_inter = AV_RL32(edata);
256     edata += 4;
257     if (ws->nb_inter < 0)
258         return AVERROR(EINVAL);
259     ws->inter = av_calloc(ws->nb_inter, sizeof(*ws->inter));
260     if (!ws->inter)
261         return AVERROR(ENOMEM);
262     for (i = 0; i < ws->nb_inter; i++) {
263         in = &ws->inter[i];
264         if (edata_end - edata < 24)
265             return AVERROR(EINVAL);
266         in->ts_start = AV_RL64(edata +  0);
267         in->ts_end   = AV_RL64(edata +  8);
268         in->type     = AV_RL32(edata + 16);
269         in->channels = AV_RL32(edata + 20);
270         edata += 24;
271         if (in->ts_start < cur_ts || in->ts_end <= in->ts_start)
272             return AVERROR(EINVAL);
273         cur_ts = in->ts_start;
274         dt = in->ts_end - in->ts_start;
275         switch (in->type) {
276             case WS_SINE:
277                 if (edata_end - edata < 20)
278                     return AVERROR(EINVAL);
279                 f1  = AV_RL32(edata +  0);
280                 f2  = AV_RL32(edata +  4);
281                 a1  = AV_RL32(edata +  8);
282                 a2  = AV_RL32(edata + 12);
283                 phi = AV_RL32(edata + 16);
284                 edata += 20;
285                 dphi1 = frac64(f1, (int64_t)avc->sample_rate << 16);
286                 dphi2 = frac64(f2, (int64_t)avc->sample_rate << 16);
287                 in->dphi0 = dphi1;
288                 in->ddphi = (dphi2 - dphi1) / dt;
289                 if (phi & 0x80000000) {
290                     phi &= ~0x80000000;
291                     if (phi >= i)
292                         return AVERROR(EINVAL);
293                     in->phi0 = phi_at(&ws->inter[phi], in->ts_start);
294                 } else {
295                     in->phi0 = (uint64_t)phi << 33;
296                 }
297                 break;
298             case WS_NOISE:
299                 if (edata_end - edata < 8)
300                     return AVERROR(EINVAL);
301                 a1  = AV_RL32(edata +  0);
302                 a2  = AV_RL32(edata +  4);
303                 edata += 8;
304                 break;
305             default:
306                 return AVERROR(EINVAL);
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)
312         return AVERROR(EINVAL);
313     return 0;
314 }
315
316 static av_cold int wavesynth_init(AVCodecContext *avc)
317 {
318     struct wavesynth_context *ws = avc->priv_data;
319     int i, r;
320
321     if (avc->channels > WS_MAX_CHANNELS) {
322         av_log(avc, AV_LOG_ERROR,
323                "This implementation is limited to %d channels.\n",
324                WS_MAX_CHANNELS);
325         return AVERROR(EINVAL);
326     }
327     r = wavesynth_parse_extradata(avc);
328     if (r < 0) {
329         av_log(avc, AV_LOG_ERROR, "Invalid intervals definitions.\n");
330         goto fail;
331     }
332     ws->sin = av_malloc(sizeof(*ws->sin) << SIN_BITS);
333     if (!ws->sin) {
334         r = AVERROR(ENOMEM);
335         goto fail;
336     }
337     for (i = 0; i < 1 << SIN_BITS; i++)
338         ws->sin[i] = floor(32767 * sin(2 * M_PI * i / (1 << SIN_BITS)));
339     ws->dither_state = MKTAG('D','I','T','H');
340     for (i = 0; i < ws->nb_inter; i++)
341         ws->pink_need += ws->inter[i].type == WS_NOISE;
342     ws->pink_state = MKTAG('P','I','N','K');
343     ws->pink_pos = PINK_UNIT;
344     avcodec_get_frame_defaults(&ws->frame);
345     avc->coded_frame = &ws->frame;
346     wavesynth_seek(ws, 0);
347     avc->sample_fmt = AV_SAMPLE_FMT_S16;
348     return 0;
349
350 fail:
351     av_free(ws->inter);
352     av_free(ws->sin);
353     return r;
354 }
355
356 static void wavesynth_synth_sample(struct wavesynth_context *ws, int64_t ts,
357                                    int32_t *channels)
358 {
359     int32_t amp, val, *cv;
360     struct ws_interval *in;
361     int i, *last, pink;
362     uint32_t c, all_ch = 0;
363
364     i = ws->cur_inter;
365     last = &ws->cur_inter;
366     if (ws->pink_pos == PINK_UNIT)
367         pink_fill(ws);
368     pink = ws->pink_pool[ws->pink_pos++] >> 16;
369     while (i >= 0) {
370         in = &ws->inter[i];
371         i = in->next;
372         if (ts >= in->ts_end) {
373             *last = i;
374             continue;
375         }
376         last = &in->next;
377         amp = in->amp >> 32;
378         in->amp  += in->damp;
379         switch (in->type) {
380             case WS_SINE:
381                 val = amp * ws->sin[in->phi >> (64 - SIN_BITS)];
382                 in->phi  += in->dphi;
383                 in->dphi += in->ddphi;
384                 break;
385             case WS_NOISE:
386                 val = amp * pink;
387                 break;
388             default:
389                 val = 0;
390         }
391         all_ch |= in->channels;
392         for (c = in->channels, cv = channels; c; c >>= 1, cv++)
393             if (c & 1)
394                 *cv += val;
395     }
396     val = (int32_t)lcg_next(&ws->dither_state) >> 16;
397     for (c = all_ch, cv = channels; c; c >>= 1, cv++)
398         if (c & 1)
399             *cv += val;
400 }
401
402 static void wavesynth_enter_intervals(struct wavesynth_context *ws, int64_t ts)
403 {
404     int *last, i;
405     struct ws_interval *in;
406
407     last = &ws->cur_inter;
408     for (i = ws->cur_inter; i >= 0; i = ws->inter[i].next)
409         last = &ws->inter[i].next;
410     for (i = ws->next_inter; i < ws->nb_inter; i++) {
411         in = &ws->inter[i];
412         if (ts < in->ts_start)
413             break;
414         if (ts >= in->ts_end)
415             continue;
416         *last = i;
417         last = &in->next;
418         in->phi = in->phi0;
419         in->dphi = in->dphi0;
420         in->amp = in->amp0;
421     }
422     ws->next_inter = i;
423     ws->next_ts = i < ws->nb_inter ? ws->inter[i].ts_start : INF_TS;
424     *last = -1;
425 }
426
427 static int wavesynth_decode(AVCodecContext *avc, void *rframe, int *rgot_frame,
428                             AVPacket *packet)
429 {
430     struct wavesynth_context *ws = avc->priv_data;
431     int64_t ts;
432     int duration;
433     int s, c, r;
434     int16_t *pcm;
435     int32_t channels[WS_MAX_CHANNELS];
436
437     *rgot_frame = 0;
438     if (packet->size != 12)
439         return AVERROR_INVALIDDATA;
440     ts = AV_RL64(packet->data);
441     if (ts != ws->cur_ts)
442         wavesynth_seek(ws, ts);
443     duration = AV_RL32(packet->data + 8);
444     if (duration <= 0)
445         return AVERROR(EINVAL);
446     ws->frame.nb_samples = duration;
447     r = ff_get_buffer(avc, &ws->frame);
448     if (r < 0)
449         return r;
450     pcm = (int16_t *)ws->frame.data[0];
451     for (s = 0; s < duration; s++, ts++) {
452         memset(channels, 0, avc->channels * sizeof(*channels));
453         if (ts >= ws->next_ts)
454             wavesynth_enter_intervals(ws, ts);
455         wavesynth_synth_sample(ws, ts, channels);
456         for (c = 0; c < avc->channels; c++)
457             *(pcm++) = channels[c] >> 16;
458     }
459     ws->cur_ts += duration;
460     *rgot_frame = 1;
461     *(AVFrame *)rframe = ws->frame;
462     return packet->size;
463 }
464
465 static av_cold int wavesynth_close(AVCodecContext *avc)
466 {
467     struct wavesynth_context *ws = avc->priv_data;
468
469     av_free(ws->sin);
470     av_free(ws->inter);
471     return 0;
472 }
473
474 AVCodec ff_ffwavesynth_decoder = {
475     .name           = "wavesynth",
476     .type           = AVMEDIA_TYPE_AUDIO,
477     .id             = AV_CODEC_ID_FFWAVESYNTH,
478     .priv_data_size = sizeof(struct wavesynth_context),
479     .init           = wavesynth_init,
480     .close          = wavesynth_close,
481     .decode         = wavesynth_decode,
482     .capabilities   = CODEC_CAP_DR1,
483     .long_name      = NULL_IF_CONFIG_SMALL("Wave synthesis pseudo-codec"),
484 };