• Main Page
  • Related Pages
  • Modules
  • Data Structures
  • Files
  • File List
  • Globals

libavcodec/ffwavesynth.c

Go to the documentation of this file.
00001 /*
00002  * Wavesynth pseudo-codec
00003  * Copyright (c) 2011 Nicolas George
00004  *
00005  * This file is part of FFmpeg.
00006  *
00007  * FFmpeg is free software; you can redistribute it and/or
00008  * modify it under the terms of the GNU Lesser General Public
00009  * License as published by the Free Software Foundation; either
00010  * version 2.1 of the License, or (at your option) any later version.
00011  *
00012  * FFmpeg is distributed in the hope that it will be useful,
00013  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00014  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00015  * Lesser General Public License for more details.
00016  *
00017  * You should have received a copy of the GNU Lesser General Public
00018  * License along with FFmpeg; if not, write to the Free Software
00019  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
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    Format of the extradata and packets
00034 
00035    THIS INFORMATION IS NOT PART OF THE PUBLIC API OR ABI.
00036    IT CAN CHANGE WITHOUT NOTIFICATION.
00037 
00038    All numbers are in little endian.
00039 
00040    The codec extradata define a set of intervals with uniform content.
00041    Overlapping intervals are added together.
00042 
00043    extradata:
00044        uint32      number of intervals
00045        ...         intervals
00046 
00047    interval:
00048        int64       start timestamp; time_base must be 1/sample_rate;
00049                    start timestamps must be in ascending order
00050        int64       end timestamp
00051        uint32      type
00052        uint32      channels mask
00053        ...         additional information, depends on type
00054 
00055    sine interval (type fourcc "SINE"):
00056        int32       start frequency, in 1/(1<<16) Hz
00057        int32       end frequency
00058        int32       start amplitude, 1<<16 is the full amplitude
00059        int32       end amplitude
00060        uint32      start phase, 0 is sin(0), 0x20000000 is sin(pi/2), etc.;
00061                    n | (1<<31) means to match the phase of previous channel #n
00062 
00063    pink noise interval (type fourcc "NOIS"):
00064        int32       start amplitude
00065        int32       end amplitude
00066 
00067    The input packets encode the time and duration of the requested segment.
00068 
00069    packet:
00070        int64       start timestamp
00071        int32       duration
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 /* A*AI = 1 [mod 1<<32] */
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 { /* coefficients for a step backward */
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; /* coefficients for a double step */
00131         a *= a;
00132         dt >>= 1;
00133     }
00134     *s = t;
00135 }
00136 
00137 /* Emulate pink noise by summing white noise at the sampling frequency,
00138  * white noise at half the sampling frequency (each value taken twice),
00139  * etc., with a total of 8 octaves.
00140  * This is known as the Voss-McCartney algorithm. */
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); /* so we use exactly 256 steps */
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) { /* b small, use two 32-bits steps */
00172         a <<= 32;
00173         return ((a / b) << 32) | ((a % b) << 32) / b;
00174     }
00175     if (b < (uint64_t)1 << 48) { /* b medium, use four 16-bits steps */
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 ? /* dt * (dt - 1) / 2 without overflow */
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 };
Generated on Fri Feb 1 2013 14:34:34 for FFmpeg by doxygen 1.7.1