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 "avcodec.h"
00024
00025 #include "truespeech_data.h"
00034 typedef struct {
00035
00036 int16_t vector[8];
00037 int offset1[2];
00038 int offset2[4];
00039 int pulseoff[4];
00040 int pulsepos[4];
00041 int pulseval[4];
00042 int flag;
00043
00044 int filtbuf[146];
00045 int prevfilt[8];
00046 int16_t tmp1[8];
00047 int16_t tmp2[8];
00048 int16_t tmp3[8];
00049 int16_t cvector[8];
00050 int filtval;
00051 int16_t newvec[60];
00052 int16_t filters[32];
00053 } TSContext;
00054
00055 static av_cold int truespeech_decode_init(AVCodecContext * avctx)
00056 {
00057
00058
00059 if (avctx->channels != 1) {
00060 av_log_ask_for_sample(avctx, "Unsupported channel count: %d\n", avctx->channels);
00061 return AVERROR(EINVAL);
00062 }
00063
00064 avctx->sample_fmt = AV_SAMPLE_FMT_S16;
00065 return 0;
00066 }
00067
00068 static void truespeech_read_frame(TSContext *dec, const uint8_t *input)
00069 {
00070 uint32_t t;
00071
00072
00073 t = AV_RL32(input);
00074 input += 4;
00075
00076 dec->flag = t & 1;
00077
00078 dec->vector[0] = ts_codebook[0][(t >> 1) & 0x1F];
00079 dec->vector[1] = ts_codebook[1][(t >> 6) & 0x1F];
00080 dec->vector[2] = ts_codebook[2][(t >> 11) & 0xF];
00081 dec->vector[3] = ts_codebook[3][(t >> 15) & 0xF];
00082 dec->vector[4] = ts_codebook[4][(t >> 19) & 0xF];
00083 dec->vector[5] = ts_codebook[5][(t >> 23) & 0x7];
00084 dec->vector[6] = ts_codebook[6][(t >> 26) & 0x7];
00085 dec->vector[7] = ts_codebook[7][(t >> 29) & 0x7];
00086
00087
00088 t = AV_RL32(input);
00089 input += 4;
00090
00091 dec->offset2[0] = (t >> 0) & 0x7F;
00092 dec->offset2[1] = (t >> 7) & 0x7F;
00093 dec->offset2[2] = (t >> 14) & 0x7F;
00094 dec->offset2[3] = (t >> 21) & 0x7F;
00095
00096 dec->offset1[0] = ((t >> 28) & 0xF) << 4;
00097
00098
00099 t = AV_RL32(input);
00100 input += 4;
00101
00102 dec->pulseval[0] = (t >> 0) & 0x3FFF;
00103 dec->pulseval[1] = (t >> 14) & 0x3FFF;
00104
00105 dec->offset1[1] = (t >> 28) & 0x0F;
00106
00107
00108 t = AV_RL32(input);
00109 input += 4;
00110
00111 dec->pulseval[2] = (t >> 0) & 0x3FFF;
00112 dec->pulseval[3] = (t >> 14) & 0x3FFF;
00113
00114 dec->offset1[1] |= ((t >> 28) & 0x0F) << 4;
00115
00116
00117 t = AV_RL32(input);
00118 input += 4;
00119
00120 dec->pulsepos[0] = (t >> 4) & 0x7FFFFFF;
00121
00122 dec->pulseoff[0] = (t >> 0) & 0xF;
00123
00124 dec->offset1[0] |= (t >> 31) & 1;
00125
00126
00127 t = AV_RL32(input);
00128 input += 4;
00129
00130 dec->pulsepos[1] = (t >> 4) & 0x7FFFFFF;
00131
00132 dec->pulseoff[1] = (t >> 0) & 0xF;
00133
00134 dec->offset1[0] |= ((t >> 31) & 1) << 1;
00135
00136
00137 t = AV_RL32(input);
00138 input += 4;
00139
00140 dec->pulsepos[2] = (t >> 4) & 0x7FFFFFF;
00141
00142 dec->pulseoff[2] = (t >> 0) & 0xF;
00143
00144 dec->offset1[0] |= ((t >> 31) & 1) << 2;
00145
00146
00147 t = AV_RL32(input);
00148 input += 4;
00149
00150 dec->pulsepos[3] = (t >> 4) & 0x7FFFFFF;
00151
00152 dec->pulseoff[3] = (t >> 0) & 0xF;
00153
00154 dec->offset1[0] |= ((t >> 31) & 1) << 3;
00155
00156 }
00157
00158 static void truespeech_correlate_filter(TSContext *dec)
00159 {
00160 int16_t tmp[8];
00161 int i, j;
00162
00163 for(i = 0; i < 8; i++){
00164 if(i > 0){
00165 memcpy(tmp, dec->cvector, i * 2);
00166 for(j = 0; j < i; j++)
00167 dec->cvector[j] = ((tmp[i - j - 1] * dec->vector[i]) +
00168 (dec->cvector[j] << 15) + 0x4000) >> 15;
00169 }
00170 dec->cvector[i] = (8 - dec->vector[i]) >> 3;
00171 }
00172 for(i = 0; i < 8; i++)
00173 dec->cvector[i] = (dec->cvector[i] * ts_decay_994_1000[i]) >> 15;
00174
00175 dec->filtval = dec->vector[0];
00176 }
00177
00178 static void truespeech_filters_merge(TSContext *dec)
00179 {
00180 int i;
00181
00182 if(!dec->flag){
00183 for(i = 0; i < 8; i++){
00184 dec->filters[i + 0] = dec->prevfilt[i];
00185 dec->filters[i + 8] = dec->prevfilt[i];
00186 }
00187 }else{
00188 for(i = 0; i < 8; i++){
00189 dec->filters[i + 0]=(dec->cvector[i] * 21846 + dec->prevfilt[i] * 10923 + 16384) >> 15;
00190 dec->filters[i + 8]=(dec->cvector[i] * 10923 + dec->prevfilt[i] * 21846 + 16384) >> 15;
00191 }
00192 }
00193 for(i = 0; i < 8; i++){
00194 dec->filters[i + 16] = dec->cvector[i];
00195 dec->filters[i + 24] = dec->cvector[i];
00196 }
00197 }
00198
00199 static void truespeech_apply_twopoint_filter(TSContext *dec, int quart)
00200 {
00201 int16_t tmp[146 + 60], *ptr0, *ptr1;
00202 const int16_t *filter;
00203 int i, t, off;
00204
00205 t = dec->offset2[quart];
00206 if(t == 127){
00207 memset(dec->newvec, 0, 60 * 2);
00208 return;
00209 }
00210 for(i = 0; i < 146; i++)
00211 tmp[i] = dec->filtbuf[i];
00212 off = (t / 25) + dec->offset1[quart >> 1] + 18;
00213 ptr0 = tmp + 145 - off;
00214 ptr1 = tmp + 146;
00215 filter = (const int16_t*)ts_order2_coeffs + (t % 25) * 2;
00216 for(i = 0; i < 60; i++){
00217 t = (ptr0[0] * filter[0] + ptr0[1] * filter[1] + 0x2000) >> 14;
00218 ptr0++;
00219 dec->newvec[i] = t;
00220 ptr1[i] = t;
00221 }
00222 }
00223
00224 static void truespeech_place_pulses(TSContext *dec, int16_t *out, int quart)
00225 {
00226 int16_t tmp[7];
00227 int i, j, t;
00228 const int16_t *ptr1;
00229 int16_t *ptr2;
00230 int coef;
00231
00232 memset(out, 0, 60 * 2);
00233 for(i = 0; i < 7; i++) {
00234 t = dec->pulseval[quart] & 3;
00235 dec->pulseval[quart] >>= 2;
00236 tmp[6 - i] = ts_pulse_scales[dec->pulseoff[quart] * 4 + t];
00237 }
00238
00239 coef = dec->pulsepos[quart] >> 15;
00240 ptr1 = (const int16_t*)ts_pulse_values + 30;
00241 ptr2 = tmp;
00242 for(i = 0, j = 3; (i < 30) && (j > 0); i++){
00243 t = *ptr1++;
00244 if(coef >= t)
00245 coef -= t;
00246 else{
00247 out[i] = *ptr2++;
00248 ptr1 += 30;
00249 j--;
00250 }
00251 }
00252 coef = dec->pulsepos[quart] & 0x7FFF;
00253 ptr1 = (const int16_t*)ts_pulse_values;
00254 for(i = 30, j = 4; (i < 60) && (j > 0); i++){
00255 t = *ptr1++;
00256 if(coef >= t)
00257 coef -= t;
00258 else{
00259 out[i] = *ptr2++;
00260 ptr1 += 30;
00261 j--;
00262 }
00263 }
00264
00265 }
00266
00267 static void truespeech_update_filters(TSContext *dec, int16_t *out, int quart)
00268 {
00269 int i;
00270
00271 for(i = 0; i < 86; i++)
00272 dec->filtbuf[i] = dec->filtbuf[i + 60];
00273 for(i = 0; i < 60; i++){
00274 dec->filtbuf[i + 86] = out[i] + dec->newvec[i] - (dec->newvec[i] >> 3);
00275 out[i] += dec->newvec[i];
00276 }
00277 }
00278
00279 static void truespeech_synth(TSContext *dec, int16_t *out, int quart)
00280 {
00281 int i,k;
00282 int t[8];
00283 int16_t *ptr0, *ptr1;
00284
00285 ptr0 = dec->tmp1;
00286 ptr1 = dec->filters + quart * 8;
00287 for(i = 0; i < 60; i++){
00288 int sum = 0;
00289 for(k = 0; k < 8; k++)
00290 sum += ptr0[k] * ptr1[k];
00291 sum = (sum + (out[i] << 12) + 0x800) >> 12;
00292 out[i] = av_clip(sum, -0x7FFE, 0x7FFE);
00293 for(k = 7; k > 0; k--)
00294 ptr0[k] = ptr0[k - 1];
00295 ptr0[0] = out[i];
00296 }
00297
00298 for(i = 0; i < 8; i++)
00299 t[i] = (ts_decay_35_64[i] * ptr1[i]) >> 15;
00300
00301 ptr0 = dec->tmp2;
00302 for(i = 0; i < 60; i++){
00303 int sum = 0;
00304 for(k = 0; k < 8; k++)
00305 sum += ptr0[k] * t[k];
00306 for(k = 7; k > 0; k--)
00307 ptr0[k] = ptr0[k - 1];
00308 ptr0[0] = out[i];
00309 out[i] = ((out[i] << 12) - sum) >> 12;
00310 }
00311
00312 for(i = 0; i < 8; i++)
00313 t[i] = (ts_decay_3_4[i] * ptr1[i]) >> 15;
00314
00315 ptr0 = dec->tmp3;
00316 for(i = 0; i < 60; i++){
00317 int sum = out[i] << 12;
00318 for(k = 0; k < 8; k++)
00319 sum += ptr0[k] * t[k];
00320 for(k = 7; k > 0; k--)
00321 ptr0[k] = ptr0[k - 1];
00322 ptr0[0] = av_clip((sum + 0x800) >> 12, -0x7FFE, 0x7FFE);
00323
00324 sum = ((ptr0[1] * (dec->filtval - (dec->filtval >> 2))) >> 4) + sum;
00325 sum = sum - (sum >> 3);
00326 out[i] = av_clip((sum + 0x800) >> 12, -0x7FFE, 0x7FFE);
00327 }
00328 }
00329
00330 static void truespeech_save_prevvec(TSContext *c)
00331 {
00332 int i;
00333
00334 for(i = 0; i < 8; i++)
00335 c->prevfilt[i] = c->cvector[i];
00336 }
00337
00338 static int truespeech_decode_frame(AVCodecContext *avctx,
00339 void *data, int *data_size,
00340 AVPacket *avpkt)
00341 {
00342 const uint8_t *buf = avpkt->data;
00343 int buf_size = avpkt->size;
00344 TSContext *c = avctx->priv_data;
00345
00346 int i, j;
00347 short *samples = data;
00348 int consumed = 0;
00349 int16_t out_buf[240];
00350 int iterations;
00351
00352 if (!buf_size)
00353 return 0;
00354
00355 if (buf_size < 32) {
00356 av_log(avctx, AV_LOG_ERROR,
00357 "Too small input buffer (%d bytes), need at least 32 bytes\n", buf_size);
00358 return -1;
00359 }
00360 iterations = FFMIN(buf_size / 32, *data_size / 480);
00361 for(j = 0; j < iterations; j++) {
00362 truespeech_read_frame(c, buf + consumed);
00363 consumed += 32;
00364
00365 truespeech_correlate_filter(c);
00366 truespeech_filters_merge(c);
00367
00368 memset(out_buf, 0, 240 * 2);
00369 for(i = 0; i < 4; i++) {
00370 truespeech_apply_twopoint_filter(c, i);
00371 truespeech_place_pulses(c, out_buf + i * 60, i);
00372 truespeech_update_filters(c, out_buf + i * 60, i);
00373 truespeech_synth(c, out_buf + i * 60, i);
00374 }
00375
00376 truespeech_save_prevvec(c);
00377
00378
00379 for(i = 0; i < 240; i++)
00380 *samples++ = out_buf[i];
00381
00382 }
00383
00384 *data_size = consumed * 15;
00385
00386 return consumed;
00387 }
00388
00389 AVCodec ff_truespeech_decoder = {
00390 "truespeech",
00391 AVMEDIA_TYPE_AUDIO,
00392 CODEC_ID_TRUESPEECH,
00393 sizeof(TSContext),
00394 truespeech_decode_init,
00395 NULL,
00396 NULL,
00397 truespeech_decode_frame,
00398 .long_name = NULL_IF_CONFIG_SMALL("DSP Group TrueSpeech"),
00399 };