ic_predict.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 [email protected].
00029 **
00030 ** $Id: ic_predict.c,v 1.2 2005/11/01 21:41:43 gabest Exp $
00031 **/
00032 
00033 #include "common.h"
00034 #include "structs.h"
00035 
00036 #ifdef MAIN_DEC
00037 
00038 #include "syntax.h"
00039 #include "ic_predict.h"
00040 #include "pns.h"
00041 
00042 
00043 static void flt_round(float32_t *pf)
00044 {
00045     int32_t flg;
00046     uint32_t tmp, tmp1, tmp2;
00047 
00048     tmp = *(uint32_t*)pf;
00049     flg = tmp & (uint32_t)0x00008000;
00050     tmp &= (uint32_t)0xffff0000;
00051     tmp1 = tmp;
00052     /* round 1/2 lsb toward infinity */
00053     if (flg)
00054     {
00055         tmp &= (uint32_t)0xff800000;       /* extract exponent and sign */
00056         tmp |= (uint32_t)0x00010000;       /* insert 1 lsb */
00057         tmp2 = tmp;                             /* add 1 lsb and elided one */
00058         tmp &= (uint32_t)0xff800000;       /* extract exponent and sign */
00059         
00060         *pf = *(float32_t*)&tmp1 + *(float32_t*)&tmp2 - *(float32_t*)&tmp;
00061     } else {
00062         *pf = *(float32_t*)&tmp;
00063     }
00064 }
00065 
00066 static int16_t quant_pred(float32_t x)
00067 {
00068     int16_t q;
00069     uint32_t *tmp = (uint32_t*)&x;
00070 
00071     q = (int16_t)(*tmp>>16);
00072 
00073     return q;
00074 }
00075 
00076 static float32_t inv_quant_pred(int16_t q)
00077 {
00078     float32_t x;
00079     uint32_t *tmp = (uint32_t*)&x;
00080     *tmp = ((uint32_t)q)<<16;
00081 
00082     return x;
00083 }
00084 
00085 static void ic_predict(pred_state *state, real_t input, real_t *output, uint8_t pred)
00086 {
00087     uint16_t tmp;
00088     int16_t i, j;
00089     real_t dr1, predictedvalue;
00090     real_t e0, e1;
00091     real_t k1, k2;
00092 
00093     real_t r[2];
00094     real_t COR[2];
00095     real_t VAR[2];
00096 
00097     r[0] = inv_quant_pred(state->r[0]);
00098     r[1] = inv_quant_pred(state->r[1]);
00099     COR[0] = inv_quant_pred(state->COR[0]);
00100     COR[1] = inv_quant_pred(state->COR[1]);
00101     VAR[0] = inv_quant_pred(state->VAR[0]);
00102     VAR[1] = inv_quant_pred(state->VAR[1]);
00103 
00104 
00105 #if 1
00106     tmp = state->VAR[0];
00107     j = (tmp >> 7);
00108     i = tmp & 0x7f;
00109     if (j >= 128)
00110     {
00111         j -= 128;
00112         k1 = COR[0] * exp_table[j] * mnt_table[i];
00113     } else {
00114         k1 = REAL_CONST(0);
00115     }
00116 #else
00117 
00118     {
00119 #define B 0.953125
00120         real_t c = COR[0];
00121         real_t v = VAR[0];
00122         real_t tmp;
00123         if (c == 0 || v <= 1)
00124         {
00125             k1 = 0;
00126         } else {
00127             tmp = B / v;
00128             flt_round(&tmp);
00129             k1 = c * tmp;
00130         }
00131     }
00132 #endif
00133 
00134     if (pred)
00135     {
00136 #if 1
00137         tmp = state->VAR[1];
00138         j = (tmp >> 7);
00139         i = tmp & 0x7f;
00140         if (j >= 128)
00141         {
00142             j -= 128;
00143             k2 = COR[1] * exp_table[j] * mnt_table[i];
00144         } else {
00145             k2 = REAL_CONST(0);
00146         }
00147 #else
00148 
00149 #define B 0.953125
00150         real_t c = COR[1];
00151         real_t v = VAR[1];
00152         real_t tmp;
00153         if (c == 0 || v <= 1)
00154         {
00155             k2 = 0;
00156         } else {
00157             tmp = B / v;
00158             flt_round(&tmp);
00159             k2 = c * tmp;
00160         }
00161 #endif
00162 
00163         predictedvalue = k1*r[0] + k2*r[1];
00164         flt_round(&predictedvalue);
00165         *output = input + predictedvalue;
00166     }
00167 
00168     /* calculate new state data */
00169     e0 = *output;
00170     e1 = e0 - k1*r[0];
00171     dr1 = k1*e0;
00172 
00173     VAR[0] = ALPHA*VAR[0] + 0.5f * (r[0]*r[0] + e0*e0);
00174     COR[0] = ALPHA*COR[0] + r[0]*e0;
00175     VAR[1] = ALPHA*VAR[1] + 0.5f * (r[1]*r[1] + e1*e1);
00176     COR[1] = ALPHA*COR[1] + r[1]*e1;
00177 
00178     r[1] = A * (r[0]-dr1);
00179     r[0] = A * e0;
00180 
00181     state->r[0] = quant_pred(r[0]);
00182     state->r[1] = quant_pred(r[1]);
00183     state->COR[0] = quant_pred(COR[0]);
00184     state->COR[1] = quant_pred(COR[1]);
00185     state->VAR[0] = quant_pred(VAR[0]);
00186     state->VAR[1] = quant_pred(VAR[1]);
00187 }
00188 
00189 static void reset_pred_state(pred_state *state)
00190 {
00191     state->r[0]   = 0;
00192     state->r[1]   = 0;
00193     state->COR[0] = 0;
00194     state->COR[1] = 0;
00195     state->VAR[0] = 0x3F80;
00196     state->VAR[1] = 0x3F80;
00197 }
00198 
00199 void pns_reset_pred_state(ic_stream *ics, pred_state *state)
00200 {
00201     uint8_t sfb, g, b;
00202     uint16_t i, offs, offs2;
00203 
00204     /* prediction only for long blocks */
00205     if (ics->window_sequence == EIGHT_SHORT_SEQUENCE)
00206         return;
00207 
00208     for (g = 0; g < ics->num_window_groups; g++)
00209     {
00210         for (b = 0; b < ics->window_group_length[g]; b++)
00211         {
00212             for (sfb = 0; sfb < ics->max_sfb; sfb++)
00213             {
00214                 if (is_noise(ics, g, sfb))
00215                 {
00216                     offs = ics->swb_offset[sfb];
00217                     offs2 = ics->swb_offset[sfb+1];
00218 
00219                     for (i = offs; i < offs2; i++)
00220                         reset_pred_state(&state[i]);
00221                 }
00222             }
00223         }
00224     }
00225 }
00226 
00227 void reset_all_predictors(pred_state *state, uint16_t frame_len)
00228 {
00229     uint16_t i;
00230 
00231     for (i = 0; i < frame_len; i++)
00232         reset_pred_state(&state[i]);
00233 }
00234 
00235 /* intra channel prediction */
00236 void ic_prediction(ic_stream *ics, real_t *spec, pred_state *state,
00237                    uint16_t frame_len, uint8_t sf_index)
00238 {
00239     uint8_t sfb;
00240     uint16_t bin;
00241 
00242     if (ics->window_sequence == EIGHT_SHORT_SEQUENCE)
00243     {
00244         reset_all_predictors(state, frame_len);
00245     } else {
00246         for (sfb = 0; sfb < max_pred_sfb(sf_index); sfb++)
00247         {
00248             uint16_t low  = ics->swb_offset[sfb];
00249             uint16_t high = ics->swb_offset[sfb+1];
00250 
00251             for (bin = low; bin < high; bin++)
00252             {
00253                 ic_predict(&state[bin], spec[bin], &spec[bin],
00254                     (ics->predictor_data_present && ics->pred.prediction_used[sfb]));
00255             }
00256         }
00257 
00258         if (ics->predictor_data_present)
00259         {
00260             if (ics->pred.predictor_reset)
00261             {
00262                 for (bin = ics->pred.predictor_reset_group_number - 1;
00263                      bin < frame_len; bin += 30)
00264                 {
00265                     reset_pred_state(&state[bin]);
00266                 }
00267             }
00268         }
00269     }
00270 }
00271 
00272 #endif

Generated on Tue Dec 13 14:47:31 2005 for guliverkli by  doxygen 1.4.5