2 * Wavesynth pseudo-codec
3 * Copyright (c) 2011 Nicolas George
5 * This file is part of FFmpeg.
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.
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.
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
22 #include "libavutil/intreadwrite.h"
23 #include "libavutil/log.h"
25 #include "codec_internal.h"
30 #define WS_MAX_CHANNELS 32
31 #define INF_TS 0x7FFFFFFFFFFFFFFF
36 Format of the extradata and packets
38 THIS INFORMATION IS NOT PART OF THE PUBLIC API OR ABI.
39 IT CAN CHANGE WITHOUT NOTIFICATION.
41 All numbers are in little endian.
43 The codec extradata define a set of intervals with uniform content.
44 Overlapping intervals are added together.
47 uint32 number of intervals
51 int64 start timestamp; time_base must be 1/sample_rate;
52 start timestamps must be in ascending order
56 ... additional information, depends on type
58 sine interval (type fourcc "SINE"):
59 int32 start frequency, in 1/(1<<16) Hz
61 int32 start amplitude, 1<<16 is the full amplitude
63 uint32 start phase, 0 is sin(0), 0x20000000 is sin(pi/2), etc.;
64 n | (1<<31) means to match the phase of previous channel #n
66 pink noise interval (type fourcc "NOIS"):
70 The input packets encode the time and duration of the requested segment.
78 enum ws_interval_type
{
79 WS_SINE
= MKTAG('S','I','N','E'),
80 WS_NOISE
= MKTAG('N','O','I','S'),
84 int64_t ts_start
, ts_end
;
85 uint64_t phi0
, dphi0
, ddphi
;
87 uint64_t phi
, dphi
, amp
;
89 enum ws_interval_type type
;
93 struct wavesynth_context
{
97 struct ws_interval
*inter
;
98 uint32_t dither_state
;
100 int32_t pink_pool
[PINK_UNIT
];
101 unsigned pink_need
, pink_pos
;
107 #define LCG_A 1284865837
108 #define LCG_C 4150755663
109 #define LCG_AI 849225893 /* A*AI = 1 [mod 1<<32] */
111 static uint32_t lcg_next(uint32_t *s
)
113 *s
= *s
* LCG_A
+ LCG_C
;
117 static void lcg_seek(uint32_t *s
, uint32_t dt
)
119 uint32_t a
, c
, t
= *s
;
126 c
*= a
+ 1; /* coefficients for a double step */
133 /* Emulate pink noise by summing white noise at the sampling frequency,
134 * white noise at half the sampling frequency (each value taken twice),
135 * etc., with a total of 8 octaves.
136 * This is known as the Voss-McCartney algorithm. */
138 static void pink_fill(struct wavesynth_context
*ws
)
140 int32_t vt
[7] = { 0 }, v
= 0;
146 for (i
= 0; i
< PINK_UNIT
; i
++) {
147 for (j
= 0; j
< 7; j
++) {
151 vt
[j
] = (int32_t)lcg_next(&ws
->pink_state
) >> 3;
154 ws
->pink_pool
[i
] = v
+ ((int32_t)lcg_next(&ws
->pink_state
) >> 3);
156 lcg_next(&ws
->pink_state
); /* so we use exactly 256 steps */
160 * @return (1<<64) * a / b, without overflow, if a < b
162 static uint64_t frac64(uint64_t a
, uint64_t b
)
167 if (b
< (uint64_t)1 << 32) { /* b small, use two 32-bits steps */
169 return ((a
/ b
) << 32) | ((a
% b
) << 32) / b
;
171 if (b
< (uint64_t)1 << 48) { /* b medium, use four 16-bits steps */
172 for (i
= 0; i
< 4; i
++) {
174 r
= (r
<< 16) | (a
/ b
);
179 for (i
= 63; i
>= 0; i
--) {
180 if (a
>= (uint64_t)1 << 63 || a
<< 1 >= b
) {
181 r
|= (uint64_t)1 << i
;
190 static uint64_t phi_at(struct ws_interval
*in
, int64_t ts
)
192 uint64_t dt
= ts
- (uint64_t)in
->ts_start
;
193 uint64_t dt2
= dt
& 1 ? /* dt * (dt - 1) / 2 without overflow */
194 dt
* ((dt
- 1) >> 1) : (dt
>> 1) * (dt
- 1);
195 return in
->phi0
+ dt
* in
->dphi0
+ dt2
* in
->ddphi
;
198 static void wavesynth_seek(struct wavesynth_context
*ws
, int64_t ts
)
201 struct ws_interval
*in
;
203 last
= &ws
->cur_inter
;
204 for (i
= 0; i
< ws
->nb_inter
; i
++) {
206 if (ts
< in
->ts_start
)
208 if (ts
>= in
->ts_end
)
212 in
->phi
= phi_at(in
, ts
);
213 in
->dphi
= in
->dphi0
+ (ts
- in
->ts_start
) * in
->ddphi
;
214 in
->amp
= in
->amp0
+ (ts
- in
->ts_start
) * in
->damp
;
217 ws
->next_ts
= i
< ws
->nb_inter
? ws
->inter
[i
].ts_start
: INF_TS
;
219 lcg_seek(&ws
->dither_state
, (uint32_t)ts
- (uint32_t)ws
->cur_ts
);
221 uint64_t pink_ts_cur
= (ws
->cur_ts
+ (uint64_t)PINK_UNIT
- 1) & ~(PINK_UNIT
- 1);
222 uint64_t pink_ts_next
= ts
& ~(PINK_UNIT
- 1);
223 int pos
= ts
& (PINK_UNIT
- 1);
224 lcg_seek(&ws
->pink_state
, (uint32_t)(pink_ts_next
- pink_ts_cur
) * 2);
229 ws
->pink_pos
= PINK_UNIT
;
235 static int wavesynth_parse_extradata(AVCodecContext
*avc
)
237 struct wavesynth_context
*ws
= avc
->priv_data
;
238 struct ws_interval
*in
;
239 uint8_t *edata
, *edata_end
;
240 int32_t f1
, f2
, a1
, a2
;
242 int64_t dphi1
, dphi2
, dt
, cur_ts
= -0x8000000000000000;
245 if (avc
->extradata_size
< 4)
246 return AVERROR(EINVAL
);
247 edata
= avc
->extradata
;
248 edata_end
= edata
+ avc
->extradata_size
;
249 ws
->nb_inter
= AV_RL32(edata
);
251 if (ws
->nb_inter
< 0 || (edata_end
- edata
) / 24 < ws
->nb_inter
)
252 return AVERROR(EINVAL
);
253 ws
->inter
= av_calloc(ws
->nb_inter
, sizeof(*ws
->inter
));
255 return AVERROR(ENOMEM
);
256 for (i
= 0; i
< ws
->nb_inter
; i
++) {
258 if (edata_end
- edata
< 24)
259 return AVERROR(EINVAL
);
260 in
->ts_start
= AV_RL64(edata
+ 0);
261 in
->ts_end
= AV_RL64(edata
+ 8);
262 in
->type
= AV_RL32(edata
+ 16);
263 in
->channels
= AV_RL32(edata
+ 20);
265 if (in
->ts_start
< cur_ts
||
266 in
->ts_end
<= in
->ts_start
||
267 (uint64_t)in
->ts_end
- in
->ts_start
> INT64_MAX
269 return AVERROR(EINVAL
);
270 cur_ts
= in
->ts_start
;
271 dt
= in
->ts_end
- in
->ts_start
;
274 if (edata_end
- edata
< 20 || avc
->sample_rate
<= 0)
275 return AVERROR(EINVAL
);
276 f1
= AV_RL32(edata
+ 0);
277 f2
= AV_RL32(edata
+ 4);
278 a1
= AV_RL32(edata
+ 8);
279 a2
= AV_RL32(edata
+ 12);
280 phi
= AV_RL32(edata
+ 16);
282 dphi1
= frac64(f1
, (int64_t)avc
->sample_rate
<< 16);
283 dphi2
= frac64(f2
, (int64_t)avc
->sample_rate
<< 16);
285 in
->ddphi
= (int64_t)(dphi2
- (uint64_t)dphi1
) / dt
;
286 if (phi
& 0x80000000) {
289 return AVERROR(EINVAL
);
290 in
->phi0
= phi_at(&ws
->inter
[phi
], in
->ts_start
);
292 in
->phi0
= (uint64_t)phi
<< 33;
296 if (edata_end
- edata
< 8)
297 return AVERROR(EINVAL
);
298 a1
= AV_RL32(edata
+ 0);
299 a2
= AV_RL32(edata
+ 4);
303 return AVERROR(EINVAL
);
305 in
->amp0
= (uint64_t)a1
<< 32;
306 in
->damp
= (int64_t)(((uint64_t)a2
<< 32) - ((uint64_t)a1
<< 32)) / dt
;
308 if (edata
!= edata_end
)
309 return AVERROR(EINVAL
);
313 static av_cold
int wavesynth_init(AVCodecContext
*avc
)
315 struct wavesynth_context
*ws
= avc
->priv_data
;
318 if (avc
->ch_layout
.nb_channels
> WS_MAX_CHANNELS
) {
319 av_log(avc
, AV_LOG_ERROR
,
320 "This implementation is limited to %d channels.\n",
322 return AVERROR(EINVAL
);
324 r
= wavesynth_parse_extradata(avc
);
326 av_log(avc
, AV_LOG_ERROR
, "Invalid intervals definitions.\n");
329 ws
->sin
= av_malloc(sizeof(*ws
->sin
) << SIN_BITS
);
331 return AVERROR(ENOMEM
);
332 for (i
= 0; i
< 1 << SIN_BITS
; i
++)
333 ws
->sin
[i
] = floor(32767 * sin(2 * M_PI
* i
/ (1 << SIN_BITS
)));
334 ws
->dither_state
= MKTAG('D','I','T','H');
335 for (i
= 0; i
< ws
->nb_inter
; i
++)
336 ws
->pink_need
+= ws
->inter
[i
].type
== WS_NOISE
;
337 ws
->pink_state
= MKTAG('P','I','N','K');
338 ws
->pink_pos
= PINK_UNIT
;
339 wavesynth_seek(ws
, 0);
340 avc
->sample_fmt
= AV_SAMPLE_FMT_S16
;
344 static void wavesynth_synth_sample(struct wavesynth_context
*ws
, int64_t ts
,
349 struct ws_interval
*in
;
351 uint32_t c
, all_ch
= 0;
354 last
= &ws
->cur_inter
;
355 if (ws
->pink_pos
== PINK_UNIT
)
357 pink
= ws
->pink_pool
[ws
->pink_pos
++] >> 16;
361 if (ts
>= in
->ts_end
) {
370 val
= amp
* (unsigned)ws
->sin
[in
->phi
>> (64 - SIN_BITS
)];
372 in
->dphi
+= in
->ddphi
;
375 val
= amp
* (unsigned)pink
;
380 all_ch
|= in
->channels
;
381 for (c
= in
->channels
, cv
= channels
; c
; c
>>= 1, cv
++)
383 *cv
+= (unsigned)val
;
385 val
= (int32_t)lcg_next(&ws
->dither_state
) >> 16;
386 for (c
= all_ch
, cv
= channels
; c
; c
>>= 1, cv
++)
391 static void wavesynth_enter_intervals(struct wavesynth_context
*ws
, int64_t ts
)
394 struct ws_interval
*in
;
396 last
= &ws
->cur_inter
;
397 for (i
= ws
->cur_inter
; i
>= 0; i
= ws
->inter
[i
].next
)
398 last
= &ws
->inter
[i
].next
;
399 for (i
= ws
->next_inter
; i
< ws
->nb_inter
; i
++) {
401 if (ts
< in
->ts_start
)
403 if (ts
>= in
->ts_end
)
408 in
->dphi
= in
->dphi0
;
412 ws
->next_ts
= i
< ws
->nb_inter
? ws
->inter
[i
].ts_start
: INF_TS
;
416 static int wavesynth_decode(AVCodecContext
*avc
, AVFrame
*frame
,
417 int *rgot_frame
, AVPacket
*packet
)
419 struct wavesynth_context
*ws
= avc
->priv_data
;
424 int32_t channels
[WS_MAX_CHANNELS
];
427 if (packet
->size
!= 12)
428 return AVERROR_INVALIDDATA
;
429 ts
= AV_RL64(packet
->data
);
430 if (ts
!= ws
->cur_ts
)
431 wavesynth_seek(ws
, ts
);
432 duration
= AV_RL32(packet
->data
+ 8);
434 return AVERROR(EINVAL
);
435 frame
->nb_samples
= duration
;
436 r
= ff_get_buffer(avc
, frame
, 0);
439 pcm
= (int16_t *)frame
->data
[0];
440 for (s
= 0; s
< duration
; s
++, ts
+=(uint64_t)1) {
441 memset(channels
, 0, avc
->ch_layout
.nb_channels
* sizeof(*channels
));
442 if (ts
>= ws
->next_ts
)
443 wavesynth_enter_intervals(ws
, ts
);
444 wavesynth_synth_sample(ws
, ts
, channels
);
445 for (c
= 0; c
< avc
->ch_layout
.nb_channels
; c
++)
446 *(pcm
++) = channels
[c
] >> 16;
448 ws
->cur_ts
+= (uint64_t)duration
;
453 static av_cold
int wavesynth_close(AVCodecContext
*avc
)
455 struct wavesynth_context
*ws
= avc
->priv_data
;
458 av_freep(&ws
->inter
);
462 const FFCodec ff_ffwavesynth_decoder
= {
463 .p
.name
= "wavesynth",
464 .p
.long_name
= NULL_IF_CONFIG_SMALL("Wave synthesis pseudo-codec"),
465 .p
.type
= AVMEDIA_TYPE_AUDIO
,
466 .p
.id
= AV_CODEC_ID_FFWAVESYNTH
,
467 .priv_data_size
= sizeof(struct wavesynth_context
),
468 .init
= wavesynth_init
,
469 .close
= wavesynth_close
,
470 FF_CODEC_DECODE_CB(wavesynth_decode
),
471 .p
.capabilities
= AV_CODEC_CAP_DR1
,
472 .caps_internal
= FF_CODEC_CAP_INIT_THREADSAFE
| FF_CODEC_CAP_INIT_CLEANUP
,