libavcodec/truespeech.c
Go to the documentation of this file.
00001 /*
00002  * DSP Group TrueSpeech compatible decoder
00003  * Copyright (c) 2005 Konstantin Shishkov
00004  *
00005  * This file is part of Libav.
00006  *
00007  * Libav 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  * Libav 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 Libav; 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 "avcodec.h"
00024 #include "internal.h"
00025 #include "dsputil.h"
00026 #include "get_bits.h"
00027 
00028 #include "truespeech_data.h"
00037 typedef struct {
00038     AVFrame frame;
00039     DSPContext dsp;
00040     /* input data */
00041     uint8_t buffer[32];
00042     int16_t vector[8];  
00043     int offset1[2];     
00044     int offset2[4];     
00045     int pulseoff[4];    
00046     int pulsepos[4];    
00047     int pulseval[4];    
00048     int flag;           
00049     /* temporary data */
00050     int filtbuf[146];   // some big vector used for storing filters
00051     int prevfilt[8];    // filter from previous frame
00052     int16_t tmp1[8];    // coefficients for adding to out
00053     int16_t tmp2[8];    // coefficients for adding to out
00054     int16_t tmp3[8];    // coefficients for adding to out
00055     int16_t cvector[8]; // correlated input vector
00056     int filtval;        // gain value for one function
00057     int16_t newvec[60]; // tmp vector
00058     int16_t filters[32]; // filters for every subframe
00059 } TSContext;
00060 
00061 static av_cold int truespeech_decode_init(AVCodecContext * avctx)
00062 {
00063     TSContext *c = avctx->priv_data;
00064 
00065     if (avctx->channels != 1) {
00066         av_log_ask_for_sample(avctx, "Unsupported channel count: %d\n", avctx->channels);
00067         return AVERROR(EINVAL);
00068     }
00069 
00070     avctx->sample_fmt = AV_SAMPLE_FMT_S16;
00071 
00072     dsputil_init(&c->dsp, avctx);
00073 
00074     avcodec_get_frame_defaults(&c->frame);
00075     avctx->coded_frame = &c->frame;
00076 
00077     return 0;
00078 }
00079 
00080 static void truespeech_read_frame(TSContext *dec, const uint8_t *input)
00081 {
00082     GetBitContext gb;
00083 
00084     dec->dsp.bswap_buf((uint32_t *)dec->buffer, (const uint32_t *)input, 8);
00085     init_get_bits(&gb, dec->buffer, 32 * 8);
00086 
00087     dec->vector[7] = ts_codebook[7][get_bits(&gb, 3)];
00088     dec->vector[6] = ts_codebook[6][get_bits(&gb, 3)];
00089     dec->vector[5] = ts_codebook[5][get_bits(&gb, 3)];
00090     dec->vector[4] = ts_codebook[4][get_bits(&gb, 4)];
00091     dec->vector[3] = ts_codebook[3][get_bits(&gb, 4)];
00092     dec->vector[2] = ts_codebook[2][get_bits(&gb, 4)];
00093     dec->vector[1] = ts_codebook[1][get_bits(&gb, 5)];
00094     dec->vector[0] = ts_codebook[0][get_bits(&gb, 5)];
00095     dec->flag      = get_bits1(&gb);
00096 
00097     dec->offset1[0] = get_bits(&gb, 4) << 4;
00098     dec->offset2[3] = get_bits(&gb, 7);
00099     dec->offset2[2] = get_bits(&gb, 7);
00100     dec->offset2[1] = get_bits(&gb, 7);
00101     dec->offset2[0] = get_bits(&gb, 7);
00102 
00103     dec->offset1[1]  = get_bits(&gb, 4);
00104     dec->pulseval[1] = get_bits(&gb, 14);
00105     dec->pulseval[0] = get_bits(&gb, 14);
00106 
00107     dec->offset1[1] |= get_bits(&gb, 4) << 4;
00108     dec->pulseval[3] = get_bits(&gb, 14);
00109     dec->pulseval[2] = get_bits(&gb, 14);
00110 
00111     dec->offset1[0] |= get_bits1(&gb);
00112     dec->pulsepos[0] = get_bits_long(&gb, 27);
00113     dec->pulseoff[0] = get_bits(&gb, 4);
00114 
00115     dec->offset1[0] |= get_bits1(&gb) << 1;
00116     dec->pulsepos[1] = get_bits_long(&gb, 27);
00117     dec->pulseoff[1] = get_bits(&gb, 4);
00118 
00119     dec->offset1[0] |= get_bits1(&gb) << 2;
00120     dec->pulsepos[2] = get_bits_long(&gb, 27);
00121     dec->pulseoff[2] = get_bits(&gb, 4);
00122 
00123     dec->offset1[0] |= get_bits1(&gb) << 3;
00124     dec->pulsepos[3] = get_bits_long(&gb, 27);
00125     dec->pulseoff[3] = get_bits(&gb, 4);
00126 }
00127 
00128 static void truespeech_correlate_filter(TSContext *dec)
00129 {
00130     int16_t tmp[8];
00131     int i, j;
00132 
00133     for(i = 0; i < 8; i++){
00134         if(i > 0){
00135             memcpy(tmp, dec->cvector, i * sizeof(*tmp));
00136             for(j = 0; j < i; j++)
00137                 dec->cvector[j] = ((tmp[i - j - 1] * dec->vector[i]) +
00138                                    (dec->cvector[j] << 15) + 0x4000) >> 15;
00139         }
00140         dec->cvector[i] = (8 - dec->vector[i]) >> 3;
00141     }
00142     for(i = 0; i < 8; i++)
00143         dec->cvector[i] = (dec->cvector[i] * ts_decay_994_1000[i]) >> 15;
00144 
00145     dec->filtval = dec->vector[0];
00146 }
00147 
00148 static void truespeech_filters_merge(TSContext *dec)
00149 {
00150     int i;
00151 
00152     if(!dec->flag){
00153         for(i = 0; i < 8; i++){
00154             dec->filters[i + 0] = dec->prevfilt[i];
00155             dec->filters[i + 8] = dec->prevfilt[i];
00156         }
00157     }else{
00158         for(i = 0; i < 8; i++){
00159             dec->filters[i + 0]=(dec->cvector[i] * 21846 + dec->prevfilt[i] * 10923 + 16384) >> 15;
00160             dec->filters[i + 8]=(dec->cvector[i] * 10923 + dec->prevfilt[i] * 21846 + 16384) >> 15;
00161         }
00162     }
00163     for(i = 0; i < 8; i++){
00164         dec->filters[i + 16] = dec->cvector[i];
00165         dec->filters[i + 24] = dec->cvector[i];
00166     }
00167 }
00168 
00169 static void truespeech_apply_twopoint_filter(TSContext *dec, int quart)
00170 {
00171     int16_t tmp[146 + 60], *ptr0, *ptr1;
00172     const int16_t *filter;
00173     int i, t, off;
00174 
00175     t = dec->offset2[quart];
00176     if(t == 127){
00177         memset(dec->newvec, 0, 60 * sizeof(*dec->newvec));
00178         return;
00179     }
00180     for(i = 0; i < 146; i++)
00181         tmp[i] = dec->filtbuf[i];
00182     off = (t / 25) + dec->offset1[quart >> 1] + 18;
00183     off = av_clip(off, 0, 145);
00184     ptr0 = tmp + 145 - off;
00185     ptr1 = tmp + 146;
00186     filter = (const int16_t*)ts_order2_coeffs + (t % 25) * 2;
00187     for(i = 0; i < 60; i++){
00188         t = (ptr0[0] * filter[0] + ptr0[1] * filter[1] + 0x2000) >> 14;
00189         ptr0++;
00190         dec->newvec[i] = t;
00191         ptr1[i] = t;
00192     }
00193 }
00194 
00195 static void truespeech_place_pulses(TSContext *dec, int16_t *out, int quart)
00196 {
00197     int16_t tmp[7];
00198     int i, j, t;
00199     const int16_t *ptr1;
00200     int16_t *ptr2;
00201     int coef;
00202 
00203     memset(out, 0, 60 * sizeof(*out));
00204     for(i = 0; i < 7; i++) {
00205         t = dec->pulseval[quart] & 3;
00206         dec->pulseval[quart] >>= 2;
00207         tmp[6 - i] = ts_pulse_scales[dec->pulseoff[quart] * 4 + t];
00208     }
00209 
00210     coef = dec->pulsepos[quart] >> 15;
00211     ptr1 = (const int16_t*)ts_pulse_values + 30;
00212     ptr2 = tmp;
00213     for(i = 0, j = 3; (i < 30) && (j > 0); i++){
00214         t = *ptr1++;
00215         if(coef >= t)
00216             coef -= t;
00217         else{
00218             out[i] = *ptr2++;
00219             ptr1 += 30;
00220             j--;
00221         }
00222     }
00223     coef = dec->pulsepos[quart] & 0x7FFF;
00224     ptr1 = (const int16_t*)ts_pulse_values;
00225     for(i = 30, j = 4; (i < 60) && (j > 0); i++){
00226         t = *ptr1++;
00227         if(coef >= t)
00228             coef -= t;
00229         else{
00230             out[i] = *ptr2++;
00231             ptr1 += 30;
00232             j--;
00233         }
00234     }
00235 
00236 }
00237 
00238 static void truespeech_update_filters(TSContext *dec, int16_t *out, int quart)
00239 {
00240     int i;
00241 
00242     memmove(dec->filtbuf, &dec->filtbuf[60], 86 * sizeof(*dec->filtbuf));
00243     for(i = 0; i < 60; i++){
00244         dec->filtbuf[i + 86] = out[i] + dec->newvec[i] - (dec->newvec[i] >> 3);
00245         out[i] += dec->newvec[i];
00246     }
00247 }
00248 
00249 static void truespeech_synth(TSContext *dec, int16_t *out, int quart)
00250 {
00251     int i,k;
00252     int t[8];
00253     int16_t *ptr0, *ptr1;
00254 
00255     ptr0 = dec->tmp1;
00256     ptr1 = dec->filters + quart * 8;
00257     for(i = 0; i < 60; i++){
00258         int sum = 0;
00259         for(k = 0; k < 8; k++)
00260             sum += ptr0[k] * ptr1[k];
00261         sum = (sum + (out[i] << 12) + 0x800) >> 12;
00262         out[i] = av_clip(sum, -0x7FFE, 0x7FFE);
00263         for(k = 7; k > 0; k--)
00264             ptr0[k] = ptr0[k - 1];
00265         ptr0[0] = out[i];
00266     }
00267 
00268     for(i = 0; i < 8; i++)
00269         t[i] = (ts_decay_35_64[i] * ptr1[i]) >> 15;
00270 
00271     ptr0 = dec->tmp2;
00272     for(i = 0; i < 60; i++){
00273         int sum = 0;
00274         for(k = 0; k < 8; k++)
00275             sum += ptr0[k] * t[k];
00276         for(k = 7; k > 0; k--)
00277             ptr0[k] = ptr0[k - 1];
00278         ptr0[0] = out[i];
00279         out[i] = ((out[i] << 12) - sum) >> 12;
00280     }
00281 
00282     for(i = 0; i < 8; i++)
00283         t[i] = (ts_decay_3_4[i] * ptr1[i]) >> 15;
00284 
00285     ptr0 = dec->tmp3;
00286     for(i = 0; i < 60; i++){
00287         int sum = out[i] << 12;
00288         for(k = 0; k < 8; k++)
00289             sum += ptr0[k] * t[k];
00290         for(k = 7; k > 0; k--)
00291             ptr0[k] = ptr0[k - 1];
00292         ptr0[0] = av_clip((sum + 0x800) >> 12, -0x7FFE, 0x7FFE);
00293 
00294         sum = ((ptr0[1] * (dec->filtval - (dec->filtval >> 2))) >> 4) + sum;
00295         sum = sum - (sum >> 3);
00296         out[i] = av_clip((sum + 0x800) >> 12, -0x7FFE, 0x7FFE);
00297     }
00298 }
00299 
00300 static void truespeech_save_prevvec(TSContext *c)
00301 {
00302     int i;
00303 
00304     for(i = 0; i < 8; i++)
00305         c->prevfilt[i] = c->cvector[i];
00306 }
00307 
00308 static int truespeech_decode_frame(AVCodecContext *avctx, void *data,
00309                                    int *got_frame_ptr, AVPacket *avpkt)
00310 {
00311     const uint8_t *buf = avpkt->data;
00312     int buf_size = avpkt->size;
00313     TSContext *c = avctx->priv_data;
00314 
00315     int i, j;
00316     int16_t *samples;
00317     int iterations, ret;
00318 
00319     iterations = buf_size / 32;
00320 
00321     if (!iterations) {
00322         av_log(avctx, AV_LOG_ERROR,
00323                "Too small input buffer (%d bytes), need at least 32 bytes\n", buf_size);
00324         return -1;
00325     }
00326 
00327     /* get output buffer */
00328     c->frame.nb_samples = iterations * 240;
00329     if ((ret = ff_get_buffer(avctx, &c->frame)) < 0) {
00330         av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n");
00331         return ret;
00332     }
00333     samples = (int16_t *)c->frame.data[0];
00334 
00335     memset(samples, 0, iterations * 240 * sizeof(*samples));
00336 
00337     for(j = 0; j < iterations; j++) {
00338         truespeech_read_frame(c, buf);
00339         buf += 32;
00340 
00341         truespeech_correlate_filter(c);
00342         truespeech_filters_merge(c);
00343 
00344         for(i = 0; i < 4; i++) {
00345             truespeech_apply_twopoint_filter(c, i);
00346             truespeech_place_pulses  (c, samples, i);
00347             truespeech_update_filters(c, samples, i);
00348             truespeech_synth         (c, samples, i);
00349             samples += 60;
00350         }
00351 
00352         truespeech_save_prevvec(c);
00353     }
00354 
00355     *got_frame_ptr   = 1;
00356     *(AVFrame *)data = c->frame;
00357 
00358     return buf_size;
00359 }
00360 
00361 AVCodec ff_truespeech_decoder = {
00362     .name           = "truespeech",
00363     .type           = AVMEDIA_TYPE_AUDIO,
00364     .id             = CODEC_ID_TRUESPEECH,
00365     .priv_data_size = sizeof(TSContext),
00366     .init           = truespeech_decode_init,
00367     .decode         = truespeech_decode_frame,
00368     .capabilities   = CODEC_CAP_DR1,
00369     .long_name = NULL_IF_CONFIG_SMALL("DSP Group TrueSpeech"),
00370 };