00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033 #include "common.h"
00034 #include "structs.h"
00035
00036 #include "syntax.h"
00037 #include "tns.h"
00038
00039
00040
00041 static void tns_decode_coef(uint8_t order, uint8_t coef_res_bits, uint8_t coef_compress,
00042 uint8_t *coef, real_t *a);
00043 static void tns_ar_filter(real_t *spectrum, uint16_t size, int8_t inc, real_t *lpc,
00044 uint8_t order);
00045 static void tns_ma_filter(real_t *spectrum, uint16_t size, int8_t inc, real_t *lpc,
00046 uint8_t order);
00047
00048
00049 #ifdef _MSC_VER
00050 #pragma warning(disable:4305)
00051 #pragma warning(disable:4244)
00052 #endif
00053 static real_t tns_coef_0_3[] =
00054 {
00055 COEF_CONST(0.0), COEF_CONST(0.4338837391), COEF_CONST(0.7818314825), COEF_CONST(0.9749279122),
00056 COEF_CONST(-0.9848077530), COEF_CONST(-0.8660254038), COEF_CONST(-0.6427876097), COEF_CONST(-0.3420201433),
00057 COEF_CONST(-0.4338837391), COEF_CONST(-0.7818314825), COEF_CONST(-0.9749279122), COEF_CONST(-0.9749279122),
00058 COEF_CONST(-0.9848077530), COEF_CONST(-0.8660254038), COEF_CONST(-0.6427876097), COEF_CONST(-0.3420201433)
00059 };
00060 static real_t tns_coef_0_4[] =
00061 {
00062 COEF_CONST(0.0), COEF_CONST(0.2079116908), COEF_CONST(0.4067366431), COEF_CONST(0.5877852523),
00063 COEF_CONST(0.7431448255), COEF_CONST(0.8660254038), COEF_CONST(0.9510565163), COEF_CONST(0.9945218954),
00064 COEF_CONST(-0.9957341763), COEF_CONST(-0.9618256432), COEF_CONST(-0.8951632914), COEF_CONST(-0.7980172273),
00065 COEF_CONST(-0.6736956436), COEF_CONST(-0.5264321629), COEF_CONST(-0.3612416662), COEF_CONST(-0.1837495178)
00066 };
00067 static real_t tns_coef_1_3[] =
00068 {
00069 COEF_CONST(0.0), COEF_CONST(0.4338837391), COEF_CONST(-0.6427876097), COEF_CONST(-0.3420201433),
00070 COEF_CONST(0.9749279122), COEF_CONST(0.7818314825), COEF_CONST(-0.6427876097), COEF_CONST(-0.3420201433),
00071 COEF_CONST(-0.4338837391), COEF_CONST(-0.7818314825), COEF_CONST(-0.6427876097), COEF_CONST(-0.3420201433),
00072 COEF_CONST(-0.7818314825), COEF_CONST(-0.4338837391), COEF_CONST(-0.6427876097), COEF_CONST(-0.3420201433)
00073 };
00074 static real_t tns_coef_1_4[] =
00075 {
00076 COEF_CONST(0.0), COEF_CONST(0.2079116908), COEF_CONST(0.4067366431), COEF_CONST(0.5877852523),
00077 COEF_CONST(-0.6736956436), COEF_CONST(-0.5264321629), COEF_CONST(-0.3612416662), COEF_CONST(-0.1837495178),
00078 COEF_CONST(0.9945218954), COEF_CONST(0.9510565163), COEF_CONST(0.8660254038), COEF_CONST(0.7431448255),
00079 COEF_CONST(-0.6736956436), COEF_CONST(-0.5264321629), COEF_CONST(-0.3612416662), COEF_CONST(-0.1837495178)
00080 };
00081
00082
00083
00084 void tns_decode_frame(ic_stream *ics, tns_info *tns, uint8_t sr_index,
00085 uint8_t object_type, real_t *spec, uint16_t frame_len)
00086 {
00087 uint8_t w, f, tns_order;
00088 int8_t inc;
00089 int16_t size;
00090 uint16_t bottom, top, start, end;
00091 uint16_t nshort = frame_len/8;
00092 real_t lpc[TNS_MAX_ORDER+1];
00093
00094 if (!ics->tns_data_present)
00095 return;
00096
00097 for (w = 0; w < ics->num_windows; w++)
00098 {
00099 bottom = ics->num_swb;
00100
00101 for (f = 0; f < tns->n_filt[w]; f++)
00102 {
00103 top = bottom;
00104 bottom = max(top - tns->length[w][f], 0);
00105 tns_order = min(tns->order[w][f], TNS_MAX_ORDER);
00106 if (!tns_order)
00107 continue;
00108
00109 tns_decode_coef(tns_order, tns->coef_res[w]+3,
00110 tns->coef_compress[w][f], tns->coef[w][f], lpc);
00111
00112 start = min(bottom, max_tns_sfb(sr_index, object_type, (ics->window_sequence == EIGHT_SHORT_SEQUENCE)));
00113 start = min(start, ics->max_sfb);
00114 start = ics->swb_offset[start];
00115
00116 end = min(top, max_tns_sfb(sr_index, object_type, (ics->window_sequence == EIGHT_SHORT_SEQUENCE)));
00117 end = min(end, ics->max_sfb);
00118 end = ics->swb_offset[end];
00119
00120 size = end - start;
00121 if (size <= 0)
00122 continue;
00123
00124 if (tns->direction[w][f])
00125 {
00126 inc = -1;
00127 start = end - 1;
00128 } else {
00129 inc = 1;
00130 }
00131
00132 tns_ar_filter(&spec[(w*nshort)+start], size, inc, lpc, tns_order);
00133 }
00134 }
00135 }
00136
00137
00138 void tns_encode_frame(ic_stream *ics, tns_info *tns, uint8_t sr_index,
00139 uint8_t object_type, real_t *spec, uint16_t frame_len)
00140 {
00141 uint8_t w, f, tns_order;
00142 int8_t inc;
00143 int16_t size;
00144 uint16_t bottom, top, start, end;
00145 uint16_t nshort = frame_len/8;
00146 real_t lpc[TNS_MAX_ORDER+1];
00147
00148 if (!ics->tns_data_present)
00149 return;
00150
00151 for (w = 0; w < ics->num_windows; w++)
00152 {
00153 bottom = ics->num_swb;
00154
00155 for (f = 0; f < tns->n_filt[w]; f++)
00156 {
00157 top = bottom;
00158 bottom = max(top - tns->length[w][f], 0);
00159 tns_order = min(tns->order[w][f], TNS_MAX_ORDER);
00160 if (!tns_order)
00161 continue;
00162
00163 tns_decode_coef(tns_order, tns->coef_res[w]+3,
00164 tns->coef_compress[w][f], tns->coef[w][f], lpc);
00165
00166 start = min(bottom, max_tns_sfb(sr_index, object_type, (ics->window_sequence == EIGHT_SHORT_SEQUENCE)));
00167 start = min(start, ics->max_sfb);
00168 start = ics->swb_offset[start];
00169
00170 end = min(top, max_tns_sfb(sr_index, object_type, (ics->window_sequence == EIGHT_SHORT_SEQUENCE)));
00171 end = min(end, ics->max_sfb);
00172 end = ics->swb_offset[end];
00173
00174 size = end - start;
00175 if (size <= 0)
00176 continue;
00177
00178 if (tns->direction[w][f])
00179 {
00180 inc = -1;
00181 start = end - 1;
00182 } else {
00183 inc = 1;
00184 }
00185
00186 tns_ma_filter(&spec[(w*nshort)+start], size, inc, lpc, tns_order);
00187 }
00188 }
00189 }
00190
00191
00192 static void tns_decode_coef(uint8_t order, uint8_t coef_res_bits, uint8_t coef_compress,
00193 uint8_t *coef, real_t *a)
00194 {
00195 uint8_t i, m;
00196 real_t tmp2[TNS_MAX_ORDER+1], b[TNS_MAX_ORDER+1];
00197
00198
00199 for (i = 0; i < order; i++)
00200 {
00201 if (coef_compress == 0)
00202 {
00203 if (coef_res_bits == 3)
00204 {
00205 tmp2[i] = tns_coef_0_3[coef[i]];
00206 } else {
00207 tmp2[i] = tns_coef_0_4[coef[i]];
00208 }
00209 } else {
00210 if (coef_res_bits == 3)
00211 {
00212 tmp2[i] = tns_coef_1_3[coef[i]];
00213 } else {
00214 tmp2[i] = tns_coef_1_4[coef[i]];
00215 }
00216 }
00217 }
00218
00219
00220 a[0] = COEF_CONST(1.0);
00221 for (m = 1; m <= order; m++)
00222 {
00223 for (i = 1; i < m; i++)
00224 b[i] = a[i] + MUL_C(tmp2[m-1], a[m-i]);
00225
00226 for (i = 1; i < m; i++)
00227 a[i] = b[i];
00228
00229 a[m] = tmp2[m-1];
00230 }
00231 }
00232
00233 static void tns_ar_filter(real_t *spectrum, uint16_t size, int8_t inc, real_t *lpc,
00234 uint8_t order)
00235 {
00236
00237
00238
00239
00240
00241
00242
00243
00244
00245 uint8_t j;
00246 uint16_t i;
00247 real_t y;
00248
00249 real_t state[2*TNS_MAX_ORDER] = {0};
00250 int8_t state_index = 0;
00251
00252 for (i = 0; i < size; i++)
00253 {
00254 y = *spectrum;
00255
00256 for (j = 0; j < order; j++)
00257 y -= MUL_C(state[state_index+j], lpc[j+1]);
00258
00259
00260 state_index--;
00261 if (state_index < 0)
00262 state_index = order-1;
00263 state[state_index] = state[state_index + order] = y;
00264
00265 *spectrum = y;
00266 spectrum += inc;
00267
00268
00269 #ifdef TNS_PRINT
00270
00271 printf("0x%.8X\n", y);
00272 #endif
00273 }
00274 }
00275
00276 static void tns_ma_filter(real_t *spectrum, uint16_t size, int8_t inc, real_t *lpc,
00277 uint8_t order)
00278 {
00279
00280
00281
00282
00283
00284
00285
00286
00287
00288 uint8_t j;
00289 uint16_t i;
00290 real_t y;
00291
00292 real_t state[2*TNS_MAX_ORDER] = {0};
00293 int8_t state_index = 0;
00294
00295 for (i = 0; i < size; i++)
00296 {
00297 y = *spectrum;
00298
00299 for (j = 0; j < order; j++)
00300 y += MUL_C(state[j], lpc[j+1]);
00301
00302
00303 state_index--;
00304 if (state_index < 0)
00305 state_index = order-1;
00306 state[state_index] = state[state_index + order] = *spectrum;
00307
00308 *spectrum = y;
00309 spectrum += inc;
00310 }
00311 }