00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022 #include "libavutil/intreadwrite.h"
00023 #include "libavutil/log.h"
00024 #include "avcodec.h"
00025
00026 #define SIN_BITS 14
00027 #define WS_MAX_CHANNELS 32
00028 #define INF_TS 0x7FFFFFFFFFFFFFFF
00029
00030 #define PINK_UNIT 128
00031
00032
00033
00034
00035
00036
00037
00038
00039
00040
00041
00042
00043
00044
00045
00046
00047
00048
00049
00050
00051
00052
00053
00054
00055
00056
00057
00058
00059
00060
00061
00062
00063
00064
00065
00066
00067
00068
00069
00070
00071
00072
00073
00074
00075 enum ws_interval_type {
00076 WS_SINE = MKTAG('S','I','N','E'),
00077 WS_NOISE = MKTAG('N','O','I','S'),
00078 };
00079
00080 struct ws_interval {
00081 int64_t ts_start, ts_end;
00082 uint64_t phi0, dphi0, ddphi;
00083 uint64_t amp0, damp;
00084 uint64_t phi, dphi, amp;
00085 uint32_t channels;
00086 enum ws_interval_type type;
00087 int next;
00088 };
00089
00090 struct wavesynth_context {
00091 int64_t cur_ts;
00092 int64_t next_ts;
00093 int32_t *sin;
00094 AVFrame frame;
00095 struct ws_interval *inter;
00096 uint32_t dither_state;
00097 uint32_t pink_state;
00098 int32_t pink_pool[PINK_UNIT];
00099 unsigned pink_need, pink_pos;
00100 int nb_inter;
00101 int cur_inter;
00102 int next_inter;
00103 };
00104
00105 #define LCG_A 1284865837
00106 #define LCG_C 4150755663
00107 #define LCG_AI 849225893
00108
00109 static uint32_t lcg_next(uint32_t *s)
00110 {
00111 *s = *s * LCG_A + LCG_C;
00112 return *s;
00113 }
00114
00115 static void lcg_seek(uint32_t *s, int64_t dt)
00116 {
00117 uint32_t a, c, t = *s;
00118
00119 if (dt >= 0) {
00120 a = LCG_A;
00121 c = LCG_C;
00122 } else {
00123 a = LCG_AI;
00124 c = (uint32_t)(LCG_AI * LCG_C);
00125 dt = -dt;
00126 }
00127 while (dt) {
00128 if (dt & 1)
00129 t = a * t + c;
00130 c *= a + 1;
00131 a *= a;
00132 dt >>= 1;
00133 }
00134 *s = t;
00135 }
00136
00137
00138
00139
00140
00141
00142 static void pink_fill(struct wavesynth_context *ws)
00143 {
00144 int32_t vt[7] = { 0 }, v = 0;
00145 int i, j;
00146
00147 ws->pink_pos = 0;
00148 if (!ws->pink_need)
00149 return;
00150 for (i = 0; i < PINK_UNIT; i++) {
00151 for (j = 0; j < 7; j++) {
00152 if ((i >> j) & 1)
00153 break;
00154 v -= vt[j];
00155 vt[j] = (int32_t)lcg_next(&ws->pink_state) >> 3;
00156 v += vt[j];
00157 }
00158 ws->pink_pool[i] = v + ((int32_t)lcg_next(&ws->pink_state) >> 3);
00159 }
00160 lcg_next(&ws->pink_state);
00161 }
00162
00166 static uint64_t frac64(uint64_t a, uint64_t b)
00167 {
00168 uint64_t r = 0;
00169 int i;
00170
00171 if (b < (uint64_t)1 << 32) {
00172 a <<= 32;
00173 return ((a / b) << 32) | ((a % b) << 32) / b;
00174 }
00175 if (b < (uint64_t)1 << 48) {
00176 for (i = 0; i < 4; i++) {
00177 a <<= 16;
00178 r = (r << 16) | (a / b);
00179 a %= b;
00180 }
00181 return r;
00182 }
00183 for (i = 63; i >= 0; i--) {
00184 if (a >= (uint64_t)1 << 63 || a << 1 >= b) {
00185 r |= (uint64_t)1 << i;
00186 a = (a << 1) - b;
00187 } else {
00188 a <<= 1;
00189 }
00190 }
00191 return r;
00192 }
00193
00194 static uint64_t phi_at(struct ws_interval *in, int64_t ts)
00195 {
00196 uint64_t dt = ts - in->ts_start;
00197 uint64_t dt2 = dt & 1 ?
00198 dt * ((dt - 1) >> 1) : (dt >> 1) * (dt - 1);
00199 return in->phi0 + dt * in->dphi0 + dt2 * in->ddphi;
00200 }
00201
00202 static void wavesynth_seek(struct wavesynth_context *ws, int64_t ts)
00203 {
00204 int *last, i;
00205 struct ws_interval *in;
00206
00207 last = &ws->cur_inter;
00208 for (i = 0; i < ws->nb_inter; i++) {
00209 in = &ws->inter[i];
00210 if (ts < in->ts_start)
00211 break;
00212 if (ts >= in->ts_end)
00213 continue;
00214 *last = i;
00215 last = &in->next;
00216 in->phi = phi_at(in, ts);
00217 in->dphi = in->dphi0 + (ts - in->ts_start) * in->ddphi;
00218 in->amp = in->amp0 + (ts - in->ts_start) * in->damp;
00219 }
00220 ws->next_inter = i;
00221 ws->next_ts = i < ws->nb_inter ? ws->inter[i].ts_start : INF_TS;
00222 *last = -1;
00223 lcg_seek(&ws->dither_state, ts - ws->cur_ts);
00224 if (ws->pink_need) {
00225 int64_t pink_ts_cur = (ws->cur_ts + PINK_UNIT - 1) & ~(PINK_UNIT - 1);
00226 int64_t pink_ts_next = ts & ~(PINK_UNIT - 1);
00227 int pos = ts & (PINK_UNIT - 1);
00228 lcg_seek(&ws->pink_state, (pink_ts_next - pink_ts_cur) << 1);
00229 if (pos) {
00230 pink_fill(ws);
00231 ws->pink_pos = pos;
00232 } else {
00233 ws->pink_pos = PINK_UNIT;
00234 }
00235 }
00236 ws->cur_ts = ts;
00237 }
00238
00239 static int wavesynth_parse_extradata(AVCodecContext *avc)
00240 {
00241 struct wavesynth_context *ws = avc->priv_data;
00242 struct ws_interval *in;
00243 uint8_t *edata, *edata_end;
00244 int32_t f1, f2, a1, a2;
00245 uint32_t phi;
00246 int64_t dphi1, dphi2, dt, cur_ts = -0x8000000000000000;
00247 int i;
00248
00249 if (avc->extradata_size < 4)
00250 return AVERROR(EINVAL);
00251 edata = avc->extradata;
00252 edata_end = edata + avc->extradata_size;
00253 ws->nb_inter = AV_RL32(edata);
00254 edata += 4;
00255 if (ws->nb_inter < 0)
00256 return AVERROR(EINVAL);
00257 ws->inter = av_calloc(ws->nb_inter, sizeof(*ws->inter));
00258 if (!ws->inter)
00259 return AVERROR(ENOMEM);
00260 for (i = 0; i < ws->nb_inter; i++) {
00261 in = &ws->inter[i];
00262 if (edata_end - edata < 24)
00263 return AVERROR(EINVAL);
00264 in->ts_start = AV_RL64(edata + 0);
00265 in->ts_end = AV_RL64(edata + 8);
00266 in->type = AV_RL32(edata + 16);
00267 in->channels = AV_RL32(edata + 20);
00268 edata += 24;
00269 if (in->ts_start < cur_ts || in->ts_end <= in->ts_start)
00270 return AVERROR(EINVAL);
00271 cur_ts = in->ts_start;
00272 dt = in->ts_end - in->ts_start;
00273 switch (in->type) {
00274 case WS_SINE:
00275 if (edata_end - edata < 20)
00276 return AVERROR(EINVAL);
00277 f1 = AV_RL32(edata + 0);
00278 f2 = AV_RL32(edata + 4);
00279 a1 = AV_RL32(edata + 8);
00280 a2 = AV_RL32(edata + 12);
00281 phi = AV_RL32(edata + 16);
00282 edata += 20;
00283 dphi1 = frac64(f1, (int64_t)avc->sample_rate << 16);
00284 dphi2 = frac64(f2, (int64_t)avc->sample_rate << 16);
00285 in->dphi0 = dphi1;
00286 in->ddphi = (dphi2 - dphi1) / dt;
00287 if (phi & 0x80000000) {
00288 phi &= ~0x80000000;
00289 if (phi >= i)
00290 return AVERROR(EINVAL);
00291 in->phi0 = phi_at(&ws->inter[phi], in->ts_start);
00292 } else {
00293 in->phi0 = (uint64_t)phi << 33;
00294 }
00295 break;
00296 case WS_NOISE:
00297 if (edata_end - edata < 8)
00298 return AVERROR(EINVAL);
00299 a1 = AV_RL32(edata + 0);
00300 a2 = AV_RL32(edata + 4);
00301 edata += 8;
00302 break;
00303 default:
00304 return AVERROR(EINVAL);
00305 }
00306 in->amp0 = (int64_t)a1 << 32;
00307 in->damp = (((int64_t)a2 << 32) - ((int64_t)a1 << 32)) / dt;
00308 }
00309 if (edata != edata_end)
00310 return AVERROR(EINVAL);
00311 return 0;
00312 }
00313
00314 static av_cold int wavesynth_init(AVCodecContext *avc)
00315 {
00316 struct wavesynth_context *ws = avc->priv_data;
00317 int i, r;
00318
00319 if (avc->channels > WS_MAX_CHANNELS) {
00320 av_log(avc, AV_LOG_ERROR,
00321 "This implementation is limited to %d channels.\n",
00322 WS_MAX_CHANNELS);
00323 return AVERROR(EINVAL);
00324 }
00325 r = wavesynth_parse_extradata(avc);
00326 if (r < 0) {
00327 av_log(avc, AV_LOG_ERROR, "Invalid intervals definitions.\n");
00328 goto fail;
00329 }
00330 ws->sin = av_malloc(sizeof(*ws->sin) << SIN_BITS);
00331 if (!ws->sin) {
00332 r = AVERROR(ENOMEM);
00333 goto fail;
00334 }
00335 for (i = 0; i < 1 << SIN_BITS; i++)
00336 ws->sin[i] = floor(32767 * sin(2 * M_PI * i / (1 << SIN_BITS)));
00337 ws->dither_state = MKTAG('D','I','T','H');
00338 for (i = 0; i < ws->nb_inter; i++)
00339 ws->pink_need += ws->inter[i].type == WS_NOISE;
00340 ws->pink_state = MKTAG('P','I','N','K');
00341 ws->pink_pos = PINK_UNIT;
00342 avcodec_get_frame_defaults(&ws->frame);
00343 avc->coded_frame = &ws->frame;
00344 wavesynth_seek(ws, 0);
00345 avc->sample_fmt = AV_SAMPLE_FMT_S16;
00346 return 0;
00347
00348 fail:
00349 av_free(ws->inter);
00350 av_free(ws->sin);
00351 return r;
00352 }
00353
00354 static void wavesynth_synth_sample(struct wavesynth_context *ws, int64_t ts,
00355 int32_t *channels)
00356 {
00357 int32_t amp, val, *cv;
00358 struct ws_interval *in;
00359 int i, *last, pink;
00360 uint32_t c, all_ch = 0;
00361
00362 i = ws->cur_inter;
00363 last = &ws->cur_inter;
00364 if (ws->pink_pos == PINK_UNIT)
00365 pink_fill(ws);
00366 pink = ws->pink_pool[ws->pink_pos++] >> 16;
00367 while (i >= 0) {
00368 in = &ws->inter[i];
00369 i = in->next;
00370 if (ts >= in->ts_end) {
00371 *last = i;
00372 continue;
00373 }
00374 last = &in->next;
00375 amp = in->amp >> 32;
00376 in->amp += in->damp;
00377 switch (in->type) {
00378 case WS_SINE:
00379 val = amp * ws->sin[in->phi >> (64 - SIN_BITS)];
00380 in->phi += in->dphi;
00381 in->dphi += in->ddphi;
00382 break;
00383 case WS_NOISE:
00384 val = amp * pink;
00385 break;
00386 default:
00387 val = 0;
00388 }
00389 all_ch |= in->channels;
00390 for (c = in->channels, cv = channels; c; c >>= 1, cv++)
00391 if (c & 1)
00392 *cv += val;
00393 }
00394 val = (int32_t)lcg_next(&ws->dither_state) >> 16;
00395 for (c = all_ch, cv = channels; c; c >>= 1, cv++)
00396 if (c & 1)
00397 *cv += val;
00398 }
00399
00400 static void wavesynth_enter_intervals(struct wavesynth_context *ws, int64_t ts)
00401 {
00402 int *last, i;
00403 struct ws_interval *in;
00404
00405 last = &ws->cur_inter;
00406 for (i = ws->cur_inter; i >= 0; i = ws->inter[i].next)
00407 last = &ws->inter[i].next;
00408 for (i = ws->next_inter; i < ws->nb_inter; i++) {
00409 in = &ws->inter[i];
00410 if (ts < in->ts_start)
00411 break;
00412 if (ts >= in->ts_end)
00413 continue;
00414 *last = i;
00415 last = &in->next;
00416 in->phi = in->phi0;
00417 in->dphi = in->dphi0;
00418 in->amp = in->amp0;
00419 }
00420 ws->next_inter = i;
00421 ws->next_ts = i < ws->nb_inter ? ws->inter[i].ts_start : INF_TS;
00422 *last = -1;
00423 }
00424
00425 static int wavesynth_decode(AVCodecContext *avc, void *rframe, int *rgot_frame,
00426 AVPacket *packet)
00427 {
00428 struct wavesynth_context *ws = avc->priv_data;
00429 int64_t ts;
00430 int duration;
00431 int s, c, r;
00432 int16_t *pcm;
00433 int32_t channels[WS_MAX_CHANNELS];
00434
00435 *rgot_frame = 0;
00436 if (packet->size != 12)
00437 return AVERROR_INVALIDDATA;
00438 ts = AV_RL64(packet->data);
00439 if (ts != ws->cur_ts)
00440 wavesynth_seek(ws, ts);
00441 duration = AV_RL32(packet->data + 8);
00442 if (duration <= 0)
00443 return AVERROR(EINVAL);
00444 ws->frame.nb_samples = duration;
00445 r = avc->get_buffer(avc, &ws->frame);
00446 if (r < 0)
00447 return r;
00448 pcm = (int16_t *)ws->frame.data[0];
00449 for (s = 0; s < duration; s++, ts++) {
00450 memset(channels, 0, avc->channels * sizeof(*channels));
00451 if (ts >= ws->next_ts)
00452 wavesynth_enter_intervals(ws, ts);
00453 wavesynth_synth_sample(ws, ts, channels);
00454 for (c = 0; c < avc->channels; c++)
00455 *(pcm++) = channels[c] >> 16;
00456 }
00457 ws->cur_ts += duration;
00458 *rgot_frame = 1;
00459 *(AVFrame *)rframe = ws->frame;
00460 return packet->size;
00461 }
00462
00463 static av_cold int wavesynth_close(AVCodecContext *avc)
00464 {
00465 struct wavesynth_context *ws = avc->priv_data;
00466
00467 av_free(ws->sin);
00468 av_free(ws->inter);
00469 return 0;
00470 }
00471
00472 AVCodec ff_ffwavesynth_decoder = {
00473 .name = "wavesynth",
00474 .type = AVMEDIA_TYPE_AUDIO,
00475 .id = CODEC_ID_FFWAVESYNTH,
00476 .priv_data_size = sizeof(struct wavesynth_context),
00477 .init = wavesynth_init,
00478 .close = wavesynth_close,
00479 .decode = wavesynth_decode,
00480 .capabilities = CODEC_CAP_DR1,
00481 .long_name = NULL_IF_CONFIG_SMALL("Wave synthesis pseudo-codec"),
00482 };