Mercurial > libavcodec.hg
changeset 3006:4007989367bc libavcodec
TrueSpeech compatible audio decoder by Konstantin Shishkov
author | diego |
---|---|
date | Tue, 03 Jan 2006 02:30:38 +0000 |
parents | 6f6f307cffac |
children | 5d9d55e2a8c6 |
files | Makefile allcodecs.c avcodec.h truespeech.c truespeech_data.h |
diffstat | 5 files changed, 521 insertions(+), 0 deletions(-) [+] |
line wrap: on
line diff
--- a/Makefile Sat Dec 31 14:14:38 2005 +0000 +++ b/Makefile Tue Jan 03 02:30:38 2006 +0000 @@ -161,6 +161,9 @@ ifeq ($(CONFIG_TRUEMOTION2_DECODER),yes) OBJS+= truemotion2.o endif +ifeq ($(CONFIG_TRUESPEECH_DECODER),yes) + OBJS+= truespeech.o +endif ifeq ($(CONFIG_TSCC_DECODER),yes) OBJS+= tscc.o endif
--- a/allcodecs.c Sat Dec 31 14:14:38 2005 +0000 +++ b/allcodecs.c Tue Jan 03 02:30:38 2006 +0000 @@ -497,6 +497,9 @@ #ifdef CONFIG_COOK_DECODER register_avcodec(&cook_decoder); #endif //CONFIG_COOK_DECODER +#ifdef CONFIG_TRUESPEECH_DECODER + register_avcodec(&truespeech_decoder); +#endif //CONFIG_TRUESPEECH_DECODER #ifdef CONFIG_RAWVIDEO_DECODER register_avcodec(&rawvideo_decoder); #endif //CONFIG_RAWVIDEO_DECODER
--- a/avcodec.h Sat Dec 31 14:14:38 2005 +0000 +++ b/avcodec.h Tue Jan 03 02:30:38 2006 +0000 @@ -189,6 +189,7 @@ CODEC_ID_GSM, CODEC_ID_QDM2, CODEC_ID_COOK, + CODEC_ID_TRUESPEECH, CODEC_ID_OGGTHEORA= 0x16000, @@ -2134,6 +2135,7 @@ extern AVCodec mp3on4_decoder; extern AVCodec qdm2_decoder; extern AVCodec cook_decoder; +extern AVCodec truespeech_decoder; extern AVCodec mace3_decoder; extern AVCodec mace6_decoder; extern AVCodec huffyuv_decoder;
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/truespeech.c Tue Jan 03 02:30:38 2006 +0000 @@ -0,0 +1,377 @@ +/* + * DSP Group TrueSpeech compatible decoder + * Copyright (c) 2005 Konstantin Shishkov + * + * This library is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 2 of the License, or (at your option) any later version. + * + * This library is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with this library; if not, write to the Free Software + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + */ +#include "avcodec.h" + +#include "truespeech_data.h" +/** + * @file truespeech.c + * TrueSpeech decoder. + */ + +/** + * TrueSpeech decoder context + */ +typedef struct { + /* input data */ + int16_t vector[8]; //< input vector: 5/5/4/4/4/3/3/3 + int offset1[2]; //< 8-bit value, used in one copying offset + int offset2[4]; //< 7-bit value, encodes offsets for copying and for two-point filter + int pulseoff[4]; //< 4-bit offset of pulse values block + int pulsepos[4]; //< 27-bit variable, encodes 7 pulse positions + int pulseval[4]; //< 7x2-bit pulse values + int flag; //< 1-bit flag, shows how to choose filters + /* temporary data */ + int filtbuf[146]; // some big vector used for storing filters + int prevfilt[8]; // filter from previous frame + int16_t tmp1[8]; // coefficients for adding to out + int16_t tmp2[8]; // coefficients for adding to out + int16_t tmp3[8]; // coefficients for adding to out + int16_t cvector[8]; // correlated input vector + int filtval; // gain value for one function + int16_t newvec[60]; // tmp vector + int16_t filters[32]; // filters for every subframe +} TSContext; + +static int truespeech_decode_init(AVCodecContext * avctx) +{ +// TSContext *c = avctx->priv_data; + + return 0; +} + +static void truespeech_read_frame(TSContext *dec, uint8_t *input) +{ + uint32_t t; + + /* first dword */ + t = LE_32(input); + input += 4; + + dec->flag = t & 1; + + dec->vector[0] = ts_codebook[0][(t >> 1) & 0x1F]; + dec->vector[1] = ts_codebook[1][(t >> 6) & 0x1F]; + dec->vector[2] = ts_codebook[2][(t >> 11) & 0xF]; + dec->vector[3] = ts_codebook[3][(t >> 15) & 0xF]; + dec->vector[4] = ts_codebook[4][(t >> 19) & 0xF]; + dec->vector[5] = ts_codebook[5][(t >> 23) & 0x7]; + dec->vector[6] = ts_codebook[6][(t >> 26) & 0x7]; + dec->vector[7] = ts_codebook[7][(t >> 29) & 0x7]; + + /* second dword */ + t = LE_32(input); + input += 4; + + dec->offset2[0] = (t >> 0) & 0x7F; + dec->offset2[1] = (t >> 7) & 0x7F; + dec->offset2[2] = (t >> 14) & 0x7F; + dec->offset2[3] = (t >> 21) & 0x7F; + + dec->offset1[0] = ((t >> 28) & 0xF) << 4; + + /* third dword */ + t = LE_32(input); + input += 4; + + dec->pulseval[0] = (t >> 0) & 0x3FFF; + dec->pulseval[1] = (t >> 14) & 0x3FFF; + + dec->offset1[1] = (t >> 28) & 0x0F; + + /* fourth dword */ + t = LE_32(input); + input += 4; + + dec->pulseval[2] = (t >> 0) & 0x3FFF; + dec->pulseval[3] = (t >> 14) & 0x3FFF; + + dec->offset1[1] |= ((t >> 28) & 0x0F) << 4; + + /* fifth dword */ + t = LE_32(input); + input += 4; + + dec->pulsepos[0] = (t >> 4) & 0x7FFFFFF; + + dec->pulseoff[0] = (t >> 0) & 0xF; + + dec->offset1[0] |= (t >> 31) & 1; + + /* sixth dword */ + t = LE_32(input); + input += 4; + + dec->pulsepos[1] = (t >> 4) & 0x7FFFFFF; + + dec->pulseoff[1] = (t >> 0) & 0xF; + + dec->offset1[0] |= ((t >> 31) & 1) << 1; + + /* seventh dword */ + t = LE_32(input); + input += 4; + + dec->pulsepos[2] = (t >> 4) & 0x7FFFFFF; + + dec->pulseoff[2] = (t >> 0) & 0xF; + + dec->offset1[0] |= ((t >> 31) & 1) << 2; + + /* eighth dword */ + t = LE_32(input); + input += 4; + + dec->pulsepos[3] = (t >> 4) & 0x7FFFFFF; + + dec->pulseoff[3] = (t >> 0) & 0xF; + + dec->offset1[0] |= ((t >> 31) & 1) << 3; + +} + +static void truespeech_correlate_filter(TSContext *dec) +{ + int16_t tmp[8]; + int i, j; + + for(i = 0; i < 8; i++){ + if(i > 0){ + memcpy(tmp, dec->cvector, i * 2); + for(j = 0; j < i; j++) + dec->cvector[j] = ((tmp[i - j - 1] * dec->vector[i]) + + (dec->cvector[j] << 15) + 0x4000) >> 15; + } + dec->cvector[i] = (8 - dec->vector[i]) >> 3; + } + for(i = 0; i < 8; i++) + dec->cvector[i] = (dec->cvector[i] * ts_230[i]) >> 15; + + dec->filtval = dec->vector[0]; +} + +static void truespeech_filters_merge(TSContext *dec) +{ + int i; + + if(!dec->flag){ + for(i = 0; i < 8; i++){ + dec->filters[i + 0] = dec->prevfilt[i]; + dec->filters[i + 8] = dec->prevfilt[i]; + } + }else{ + for(i = 0; i < 8; i++){ + dec->filters[i + 0]=(dec->cvector[i] * 21846 + dec->prevfilt[i] * 10923 + 16384) >> 15; + dec->filters[i + 8]=(dec->cvector[i] * 10923 + dec->prevfilt[i] * 21846 + 16384) >> 15; + } + } + for(i = 0; i < 8; i++){ + dec->filters[i + 16] = dec->cvector[i]; + dec->filters[i + 24] = dec->cvector[i]; + } +} + +static void truespeech_apply_twopoint_filter(TSContext *dec, int quart) +{ + int16_t tmp[146 + 60], *ptr0, *ptr1, *filter; + int i, t, off; + + t = dec->offset2[quart]; + if(t == 127){ + memset(dec->newvec, 0, 60 * 2); + return; + } + for(i = 0; i < 146; i++) + tmp[i] = dec->filtbuf[i]; + off = (t / 25) + dec->offset1[quart >> 1] + 18; + ptr0 = tmp + 145 - off; + ptr1 = tmp + 146; + filter = ts_240 + (t % 25) * 2; + for(i = 0; i < 60; i++){ + t = (ptr0[0] * filter[0] + ptr0[1] * filter[1] + 0x2000) >> 14; + ptr0++; + dec->newvec[i] = t; + ptr1[i] = t; + } +} + +static void truespeech_place_pulses(TSContext *dec, int16_t *out, int quart) +{ + int16_t tmp[7]; + int i, j, t; + int16_t *ptr1, *ptr2; + int coef; + + memset(out, 0, 60 * 2); + for(i = 0; i < 7; i++) { + t = dec->pulseval[quart] & 3; + dec->pulseval[quart] >>= 2; + tmp[6 - i] = ts_562[dec->pulseoff[quart] * 4 + t]; + } + + coef = dec->pulsepos[quart] >> 15; + ptr1 = ts_140 + 30; + ptr2 = tmp; + for(i = 0, j = 3; (i < 30) && (j > 0); i++){ + t = *ptr1++; + if(coef >= t) + coef -= t; + else{ + out[i] = *ptr2++; + ptr1 += 30; + j--; + } + } + coef = dec->pulsepos[quart] & 0x7FFF; + ptr1 = ts_140; + for(i = 30, j = 4; (i < 60) && (j > 0); i++){ + t = *ptr1++; + if(coef >= t) + coef -= t; + else{ + out[i] = *ptr2++; + ptr1 += 30; + j--; + } + } + +} + +static void truespeech_update_filters(TSContext *dec, int16_t *out, int quart) +{ + int i; + + for(i = 0; i < 86; i++) + dec->filtbuf[i] = dec->filtbuf[i + 60]; + for(i = 0; i < 60; i++){ + dec->filtbuf[i + 86] = out[i] + dec->newvec[i] - (dec->newvec[i] >> 3); + out[i] += dec->newvec[i]; + } +} + +static void truespeech_synth(TSContext *dec, int16_t *out, int quart) +{ + int i,k; + int t[8]; + int16_t *ptr0, *ptr1; + + ptr0 = dec->tmp1; + ptr1 = dec->filters + quart * 8; + for(i = 0; i < 60; i++){ + int sum = 0; + for(k = 0; k < 8; k++) + sum += ptr0[k] * ptr1[k]; + sum = (sum + (out[i] << 12) + 0x800) >> 12; + out[i] = clip(sum, -0x7FFE, 0x7FFE); + for(k = 7; k > 0; k--) + ptr0[k] = ptr0[k - 1]; + ptr0[0] = out[i]; + } + + for(i = 0; i < 8; i++) + t[i] = (ts_5E2[i] * ptr1[i]) >> 15; + + ptr0 = dec->tmp2; + for(i = 0; i < 60; i++){ + int sum = 0; + for(k = 0; k < 8; k++) + sum += ptr0[k] * t[k]; + for(k = 7; k > 0; k--) + ptr0[k] = ptr0[k - 1]; + ptr0[0] = out[i]; + out[i] = ((out[i] << 12) - sum) >> 12; + } + + for(i = 0; i < 8; i++) + t[i] = (ts_5F2[i] * ptr1[i]) >> 15; + + ptr0 = dec->tmp3; + for(i = 0; i < 60; i++){ + int sum = out[i] << 12; + for(k = 0; k < 8; k++) + sum += ptr0[k] * t[k]; + for(k = 7; k > 0; k--) + ptr0[k] = ptr0[k - 1]; + ptr0[0] = clip((sum + 0x800) >> 12, -0x7FFE, 0x7FFE); + + sum = ((ptr0[1] * (dec->filtval - (dec->filtval >> 2))) >> 4) + sum; + sum = sum - (sum >> 3); + out[i] = clip((sum + 0x800) >> 12, -0x7FFE, 0x7FFE); + } +} + +static void truespeech_save_prevvec(TSContext *c) +{ + int i; + + for(i = 0; i < 8; i++) + c->prevfilt[i] = c->cvector[i]; +} + +static int truespeech_decode_frame(AVCodecContext *avctx, + void *data, int *data_size, + uint8_t *buf, int buf_size) +{ + TSContext *c = avctx->priv_data; + + int i; + short *samples = data; + int consumed = 0; + int16_t out_buf[240]; + + if (!buf_size) + return 0; + + while (consumed < buf_size) { + truespeech_read_frame(c, buf + consumed); + consumed += 32; + + truespeech_correlate_filter(c); + truespeech_filters_merge(c); + + memset(out_buf, 0, 240 * 2); + for(i = 0; i < 4; i++) { + truespeech_apply_twopoint_filter(c, i); + truespeech_place_pulses(c, out_buf + i * 60, i); + truespeech_update_filters(c, out_buf + i * 60, i); + truespeech_synth(c, out_buf + i * 60, i); + } + + truespeech_save_prevvec(c); + + /* finally output decoded frame */ + for(i = 0; i < 240; i++) + *samples++ = out_buf[i]; + + } + + *data_size = consumed * 15; + + return buf_size; +} + +AVCodec truespeech_decoder = { + "truespeech", + CODEC_TYPE_AUDIO, + CODEC_ID_TRUESPEECH, + sizeof(TSContext), + truespeech_decode_init, + NULL, + NULL, + truespeech_decode_frame, +};
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/truespeech_data.h Tue Jan 03 02:30:38 2006 +0000 @@ -0,0 +1,136 @@ +#ifndef __TRUESPEECH_DATA__ +#define __TRUESPEECH_DATA__ + +/* codebooks fo expanding input filter */ +const int16_t ts_cb_0[32] = { + 0x8240, 0x8364, 0x84CE, 0x865D, 0x8805, 0x89DE, 0x8BD7, 0x8DF4, + 0x9051, 0x92E2, 0x95DE, 0x990F, 0x9C81, 0xA079, 0xA54C, 0xAAD2, + 0xB18A, 0xB90A, 0xC124, 0xC9CC, 0xD339, 0xDDD3, 0xE9D6, 0xF893, + 0x096F, 0x1ACA, 0x29EC, 0x381F, 0x45F9, 0x546A, 0x63C3, 0x73B5, +}; + +const int16_t ts_cb_1[32] = { + 0x9F65, 0xB56B, 0xC583, 0xD371, 0xE018, 0xEBB4, 0xF61C, 0xFF59, + 0x085B, 0x1106, 0x1952, 0x214A, 0x28C9, 0x2FF8, 0x36E6, 0x3D92, + 0x43DF, 0x49BB, 0x4F46, 0x5467, 0x5930, 0x5DA3, 0x61EC, 0x65F9, + 0x69D4, 0x6D5A, 0x709E, 0x73AD, 0x766B, 0x78F0, 0x7B5A, 0x7DA5, +}; + +const int16_t ts_cb_2[16] = { + 0x96F8, 0xA3B4, 0xAF45, 0xBA53, 0xC4B1, 0xCECC, 0xD86F, 0xE21E, + 0xEBF3, 0xF640, 0x00F7, 0x0C20, 0x1881, 0x269A, 0x376B, 0x4D60, +}; + +const int16_t ts_cb_3[16] = { + 0xC654, 0xDEF2, 0xEFAA, 0xFD94, 0x096A, 0x143F, 0x1E7B, 0x282C, + 0x3176, 0x3A89, 0x439F, 0x4CA2, 0x557F, 0x5E50, 0x6718, 0x6F8D, +}; + +const int16_t ts_cb_4[16] = { + 0xABE7, 0xBBA8, 0xC81C, 0xD326, 0xDD0E, 0xE5D4, 0xEE22, 0xF618, + 0xFE28, 0x064F, 0x0EB7, 0x17B8, 0x21AA, 0x2D8B, 0x3BA2, 0x4DF9, +}; + +const int16_t ts_cb_5[8] = { + 0xD51B, 0xF12E, 0x042E, 0x13C7, 0x2260, 0x311B, 0x40DE, 0x5385, +}; + +const int16_t ts_cb_6[8] = { + 0xB550, 0xC825, 0xD980, 0xE997, 0xF883, 0x0752, 0x1811, 0x2E18, +}; + +const int16_t ts_cb_7[8] = { + 0xCEF0, 0xE4F9, 0xF6BB, 0x0646, 0x14F5, 0x23FF, 0x356F, 0x4A8D, +}; + +const int16_t *ts_codebook[8] = { + ts_cb_0, ts_cb_1, ts_cb_2, ts_cb_3, ts_cb_4, ts_cb_5, ts_cb_6, ts_cb_7 +}; + +/* table used for decoding pulse positions */ +const int16_t ts_140[120] = { + 0x0E46, 0x0CCC, 0x0B6D, 0x0A28, 0x08FC, 0x07E8, 0x06EB, 0x0604, + 0x0532, 0x0474, 0x03C9, 0x0330, 0x02A8, 0x0230, 0x01C7, 0x016C, + 0x011E, 0x00DC, 0x00A5, 0x0078, 0x0054, 0x0038, 0x0023, 0x0014, + 0x000A, 0x0004, 0x0001, 0x0000, 0x0000, 0x0000, + + 0x0196, 0x017A, 0x015F, 0x0145, 0x012C, 0x0114, 0x00FD, 0x00E7, + 0x00D2, 0x00BE, 0x00AB, 0x0099, 0x0088, 0x0078, 0x0069, 0x005B, + 0x004E, 0x0042, 0x0037, 0x002D, 0x0024, 0x001C, 0x0015, 0x000F, + 0x000A, 0x0006, 0x0003, 0x0001, 0x0000, 0x0000, + + 0x001D, 0x001C, 0x001B, 0x001A, 0x0019, 0x0018, 0x0017, 0x0016, + 0x0015, 0x0014, 0x0013, 0x0012, 0x0011, 0x0010, 0x000F, 0x000E, + 0x000D, 0x000C, 0x000B, 0x000A, 0x0009, 0x0008, 0x0007, 0x0006, + 0x0005, 0x0004, 0x0003, 0x0002, 0x0001, 0x0000, + + 0x0001, 0x0001, 0x0001, 0x0001, 0x0001, 0x0001, 0x0001, 0x0001, + 0x0001, 0x0001, 0x0001, 0x0001, 0x0001, 0x0001, 0x0001, 0x0001, + 0x0001, 0x0001, 0x0001, 0x0001, 0x0001, 0x0001, 0x0001, 0x0001, + 0x0001, 0x0001, 0x0001, 0x0001, 0x0001, 0x0001 +}; + +/* filter for correlated input filter */ +const int16_t ts_230[8] = + { 0x7F3B, 0x7E78, 0x7DB6, 0x7CF5, 0x7C35, 0x7B76, 0x7AB8, 0x79FC }; + +/* two-point filters table */ +const int16_t ts_240[25 * 2] = { + 0xED2F, 0x5239, + 0x54F1, 0xE4A9, + 0x2620, 0xEE3E, + 0x09D6, 0x2C40, + 0xEFB5, 0x2BE0, + + 0x3FE1, 0x3339, + 0x442F, 0xE6FE, + 0x4458, 0xF9DF, + 0xF231, 0x43DB, + 0x3DB0, 0xF705, + + 0x4F7B, 0xFEFB, + 0x26AD, 0x0CDC, + 0x33C2, 0x0739, + 0x12BE, 0x43A2, + 0x1BDF, 0x1F3E, + + 0x0211, 0x0796, + 0x2AEB, 0x163F, + 0x050D, 0x3A38, + 0x0D1E, 0x0D78, + 0x150F, 0x3346, + + 0x38A4, 0x0B7D, + 0x2D5D, 0x1FDF, + 0x19B7, 0x2822, + 0x0D99, 0x1F12, + 0x194C, 0x0CE6 +}; + +/* possible pulse values */ +const int16_t ts_562[64] = { + 0x0002, 0x0006, 0xFFFE, 0xFFFA, + 0x0004, 0x000C, 0xFFFC, 0xFFF4, + 0x0006, 0x0012, 0xFFFA, 0xFFEE, + 0x000A, 0x001E, 0xFFF6, 0xFFE2, + 0x0010, 0x0030, 0xFFF0, 0xFFD0, + 0x0019, 0x004B, 0xFFE7, 0xFFB5, + 0x0028, 0x0078, 0xFFD8, 0xFF88, + 0x0040, 0x00C0, 0xFFC0, 0xFF40, + 0x0065, 0x012F, 0xFF9B, 0xFED1, + 0x00A1, 0x01E3, 0xFF5F, 0xFE1D, + 0x0100, 0x0300, 0xFF00, 0xFD00, + 0x0196, 0x04C2, 0xFE6A, 0xFB3E, + 0x0285, 0x078F, 0xFD7B, 0xF871, + 0x0400, 0x0C00, 0xFC00, 0xF400, + 0x0659, 0x130B, 0xF9A7, 0xECF5, + 0x0A14, 0x1E3C, 0xF5EC, 0xE1C4 +}; + +/* filters used in final output calculations */ +const int16_t ts_5E2[8] = + { 0x4666, 0x26B8, 0x154C, 0x0BB6, 0x0671, 0x038B, 0x01F3, 0x0112 }; +const int16_t ts_5F2[8] = + { 0x6000, 0x4800, 0x3600, 0x2880, 0x1E60, 0x16C8, 0x1116, 0x0CD1 }; + +#endif