- Main Page
- Namespaces
- Classes
- Files
- Directories

- File List
- File Members

# tns.c

    00001 /*
    00002 ** FAAD2 - Freeware Advanced Audio (AAC) Decoder including SBR decoding
    00003 ** Copyright (C) 2003-2005 M. Bakker, Ahead Software AG, http://www.nero.com
    00004 **
    00005 ** This program is free software; you can redistribute it and/or modify
    00006 ** it under the terms of the GNU General Public License as published by
    00007 ** the Free Software Foundation; either version 2 of the License, or
    00008 ** (at your option) any later version.
    00009 **
    00010 ** This program is distributed in the hope that it will be useful,
    00011 ** but WITHOUT ANY WARRANTY; without even the implied warranty of
    00012 ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    00013 ** GNU General Public License for more details.
    00014 **
    00015 ** You should have received a copy of the GNU General Public License
    00016 ** along with this program; if not, write to the Free Software
    00017 ** Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
    00018 **
    00019 ** Any non-GPL usage of this software or parts of this software is strictly
    00020 ** forbidden.
    00021 **
    00022 ** Software using this code must display the following message visibly in the
    00023 ** software:
    00024 ** "FAAD2 AAC/HE-AAC/HE-AACv2/DRM decoder (c) Ahead Software, www.nero.com"
    00025 ** in, for example, the about-box or help/startup screen.
    00026 **
    00027 ** Commercial non-GPL licensing of this software is possible.
    00028 ** For more info contact Ahead Software through Mpeg4AAClicense@nero.com.
    00029 **
    00030 ** $Id: tns.c,v 1.3 2005/11/01 21:41:43 gabest Exp $
    00031 **/
    00032
    00033 #include "common.h"
    00034 #include "structs.h"
    00035
    00036 #include "syntax.h"
    00037 #include "tns.h"
    00038
    00039
    00040 /* static function declarations */
    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 /* TNS decoding for one channel and frame */
    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 /* TNS encoding for one channel and frame */
    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 /* Decoder transmitted coefficients for one TNS filter */
    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     /* Conversion to signed integer */
    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     /* Conversion to LPC coefficients */
    00220     a[0] = COEF_CONST(1.0);
    00221     for (m = 1; m <= order; m++)
    00222     {
    00223         for (i = 1; i < m; i++) /* loop only while i<m */
    00224             b[i] = a[i] + MUL_C(tmp2[m-1], a[m-i]);
    00225
    00226         for (i = 1; i < m; i++) /* loop only while i<m */
    00227             a[i] = b[i];
    00228
    00229         a[m] = tmp2[m-1]; /* changed */
    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      - Simple all-pole filter of order "order" defined by
    00238        y(n) = x(n) - lpc[1]*y(n-1) - ... - lpc[order]*y(n-order)
    00239      - The state variables of the filter are initialized to zero every time
    00240      - The output data is written over the input data ("in-place operation")
    00241      - An input vector of "size" samples is processed and the index increment
    00242        to the next data sample is given by "inc"
    00243     */
    00244
    00245     uint8_t j;
    00246     uint16_t i;
    00247     real_t y;
    00248     /* state is stored as a double ringbuffer */
    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         /* double ringbuffer state */
    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 //#define TNS_PRINT
    00269 #ifdef TNS_PRINT
    00270         //printf("%d\n", y);
    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      - Simple all-zero filter of order "order" defined by
    00281        y(n) =  x(n) + a(2)*x(n-1) + ... + a(order+1)*x(n-order)
    00282      - The state variables of the filter are initialized to zero every time
    00283      - The output data is written over the input data ("in-place operation")
    00284      - An input vector of "size" samples is processed and the index increment
    00285        to the next data sample is given by "inc"
    00286     */
    00287
    00288     uint8_t j;
    00289     uint16_t i;
    00290     real_t y;
    00291     /* state is stored as a double ringbuffer */
    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         /* double ringbuffer state */
    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 }