lt_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: lt_predict.c,v 1.2 2005/11/01 21:41:43 gabest Exp $
00031 **/
00032 
00033 
00034 #include "common.h"
00035 #include "structs.h"
00036 
00037 #ifdef LTP_DEC
00038 
00039 #include <stdlib.h>
00040 #include "syntax.h"
00041 #include "lt_predict.h"
00042 #include "filtbank.h"
00043 #include "tns.h"
00044 
00045 
00046 /* static function declarations */
00047 static int16_t real_to_int16(real_t sig_in);
00048 
00049 
00050 /* check if the object type is an object type that can have LTP */
00051 uint8_t is_ltp_ot(uint8_t object_type)
00052 {
00053 #ifdef LTP_DEC
00054     if ((object_type == LTP)
00055 #ifdef ERROR_RESILIENCE
00056         || (object_type == ER_LTP)
00057 #endif
00058 #ifdef LD_DEC
00059         || (object_type == LD)
00060 #endif
00061         )
00062     {
00063         return 1;
00064     }
00065 #endif
00066 
00067     return 0;
00068 }
00069 
00070 ALIGN static const real_t codebook[8] =
00071 {
00072     REAL_CONST(0.570829),
00073     REAL_CONST(0.696616),
00074     REAL_CONST(0.813004),
00075     REAL_CONST(0.911304),
00076     REAL_CONST(0.984900),
00077     REAL_CONST(1.067894),
00078     REAL_CONST(1.194601),
00079     REAL_CONST(1.369533)
00080 };
00081 
00082 void lt_prediction(ic_stream *ics, ltp_info *ltp, real_t *spec,
00083                    int16_t *lt_pred_stat, fb_info *fb, uint8_t win_shape,
00084                    uint8_t win_shape_prev, uint8_t sr_index,
00085                    uint8_t object_type, uint16_t frame_len)
00086 {
00087     uint8_t sfb;
00088     uint16_t bin, i, num_samples;
00089     ALIGN real_t x_est[2048];
00090     ALIGN real_t X_est[2048];
00091 
00092     if (ics->window_sequence != EIGHT_SHORT_SEQUENCE)
00093     {
00094         if (ltp->data_present)
00095         {
00096             num_samples = frame_len << 1;
00097 
00098             for(i = 0; i < num_samples; i++)
00099             {
00100                 /* The extra lookback M (N/2 for LD, 0 for LTP) is handled
00101                    in the buffer updating */
00102 
00103 #if 0
00104                 x_est[i] = MUL_R_C(lt_pred_stat[num_samples + i - ltp->lag],
00105                     codebook[ltp->coef]);
00106 #else
00107                 /* lt_pred_stat is a 16 bit int, multiplied with the fixed point real
00108                    this gives a real for x_est
00109                 */
00110                 x_est[i] = (real_t)lt_pred_stat[num_samples + i - ltp->lag] * codebook[ltp->coef];
00111 #endif
00112             }
00113 
00114             filter_bank_ltp(fb, ics->window_sequence, win_shape, win_shape_prev,
00115                 x_est, X_est, object_type, frame_len);
00116 
00117             tns_encode_frame(ics, &(ics->tns), sr_index, object_type, X_est,
00118                 frame_len);
00119 
00120             for (sfb = 0; sfb < ltp->last_band; sfb++)
00121             {
00122                 if (ltp->long_used[sfb])
00123                 {
00124                     uint16_t low  = ics->swb_offset[sfb];
00125                     uint16_t high = ics->swb_offset[sfb+1];
00126 
00127                     for (bin = low; bin < high; bin++)
00128                     {
00129                         spec[bin] += X_est[bin];
00130                     }
00131                 }
00132             }
00133         }
00134     }
00135 }
00136 
00137 #ifdef FIXED_POINT
00138 static INLINE int16_t real_to_int16(real_t sig_in)
00139 {
00140     if (sig_in >= 0)
00141     {
00142         sig_in += (1 << (REAL_BITS-1));
00143         if (sig_in >= REAL_CONST(32768))
00144             return 32767;
00145     } else {
00146         sig_in += -(1 << (REAL_BITS-1));
00147         if (sig_in <= REAL_CONST(-32768))
00148             return -32768;
00149     }
00150 
00151     return (sig_in >> REAL_BITS);
00152 }
00153 #else
00154 static INLINE int16_t real_to_int16(real_t sig_in)
00155 {
00156     if (sig_in >= 0)
00157     {
00158 #ifndef HAS_LRINTF
00159         sig_in += 0.5f;
00160 #endif
00161         if (sig_in >= 32768.0f)
00162             return 32767;
00163     } else {
00164 #ifndef HAS_LRINTF
00165         sig_in += -0.5f;
00166 #endif
00167         if (sig_in <= -32768.0f)
00168             return -32768;
00169     }
00170 
00171     return lrintf(sig_in);
00172 }
00173 #endif
00174 
00175 void lt_update_state(int16_t *lt_pred_stat, real_t *time, real_t *overlap,
00176                      uint16_t frame_len, uint8_t object_type)
00177 {
00178     uint16_t i;
00179 
00180     /*
00181      * The reference point for index i and the content of the buffer
00182      * lt_pred_stat are arranged so that lt_pred_stat(0 ... N/2 - 1) contains the
00183      * last aliased half window from the IMDCT, and lt_pred_stat(N/2 ... N-1)
00184      * is always all zeros. The rest of lt_pred_stat (i<0) contains the previous
00185      * fully reconstructed time domain samples, i.e., output of the decoder.
00186      *
00187      * These values are shifted up by N*2 to avoid (i<0)
00188      *
00189      * For the LD object type an extra 512 samples lookback is accomodated here.
00190      */
00191 #ifdef LD_DEC
00192     if (object_type == LD)
00193     {
00194         for (i = 0; i < frame_len; i++)
00195         {
00196             lt_pred_stat[i]  /* extra 512 */  = lt_pred_stat[i + frame_len];
00197             lt_pred_stat[frame_len + i]       = lt_pred_stat[i + (frame_len * 2)];
00198             lt_pred_stat[(frame_len * 2) + i] = real_to_int16(time[i]);
00199             lt_pred_stat[(frame_len * 3) + i] = real_to_int16(overlap[i]);
00200         }
00201     } else {
00202 #endif
00203         for (i = 0; i < frame_len; i++)
00204         {
00205             lt_pred_stat[i]                   = lt_pred_stat[i + frame_len];
00206             lt_pred_stat[frame_len + i]       = real_to_int16(time[i]);
00207             lt_pred_stat[(frame_len * 2) + i] = real_to_int16(overlap[i]);
00208 #if 0 /* set to zero once upon initialisation */
00209             lt_pred_stat[(frame_len * 3) + i] = 0;
00210 #endif
00211         }
00212 #ifdef LD_DEC
00213     }
00214 #endif
00215 }
00216 
00217 #endif

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