filtbank.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: filtbank.c,v 1.2 2005/11/01 21:41:43 gabest Exp $
00031 **/
00032 
00033 #include "common.h"
00034 #include "structs.h"
00035 
00036 #include <stdlib.h>
00037 #include <string.h>
00038 #ifdef _WIN32_WCE
00039 #define assert(x)
00040 #else
00041 #include <assert.h>
00042 #endif
00043 
00044 #include "filtbank.h"
00045 #include "decoder.h"
00046 #include "syntax.h"
00047 #include "kbd_win.h"
00048 #include "sine_win.h"
00049 #include "mdct.h"
00050 
00051 
00052 fb_info *filter_bank_init(uint16_t frame_len)
00053 {
00054     uint16_t nshort = frame_len/8;
00055 #ifdef LD_DEC
00056     uint16_t frame_len_ld = frame_len/2;
00057 #endif
00058 
00059     fb_info *fb = (fb_info*)faad_malloc(sizeof(fb_info));
00060     memset(fb, 0, sizeof(fb_info));
00061 
00062     /* normal */
00063     fb->mdct256 = faad_mdct_init(2*nshort);
00064     fb->mdct2048 = faad_mdct_init(2*frame_len);
00065 #ifdef LD_DEC
00066     /* LD */
00067     fb->mdct1024 = faad_mdct_init(2*frame_len_ld);
00068 #endif
00069 
00070 #ifdef ALLOW_SMALL_FRAMELENGTH
00071     if (frame_len == 1024)
00072     {
00073 #endif
00074         fb->long_window[0]  = sine_long_1024;
00075         fb->short_window[0] = sine_short_128;
00076         fb->long_window[1]  = kbd_long_1024;
00077         fb->short_window[1] = kbd_short_128;
00078 #ifdef LD_DEC
00079         fb->ld_window[0] = sine_mid_512;
00080         fb->ld_window[1] = ld_mid_512;
00081 #endif
00082 #ifdef ALLOW_SMALL_FRAMELENGTH
00083     } else /* (frame_len == 960) */ {
00084         fb->long_window[0]  = sine_long_960;
00085         fb->short_window[0] = sine_short_120;
00086         fb->long_window[1]  = kbd_long_960;
00087         fb->short_window[1] = kbd_short_120;
00088 #ifdef LD_DEC
00089         fb->ld_window[0] = sine_mid_480;
00090         fb->ld_window[1] = ld_mid_480;
00091 #endif
00092     }
00093 #endif
00094 
00095     return fb;
00096 }
00097 
00098 void filter_bank_end(fb_info *fb)
00099 {
00100     if (fb != NULL)
00101     {
00102 #ifdef PROFILE
00103         printf("FB:                 %I64d cycles\n", fb->cycles);
00104 #endif
00105 
00106         faad_mdct_end(fb->mdct256);
00107         faad_mdct_end(fb->mdct2048);
00108 #ifdef LD_DEC
00109         faad_mdct_end(fb->mdct1024);
00110 #endif
00111 
00112         faad_free(fb);
00113     }
00114 }
00115 
00116 static INLINE void imdct_long(fb_info *fb, real_t *in_data, real_t *out_data, uint16_t len)
00117 {
00118 #ifdef LD_DEC
00119     mdct_info *mdct = NULL;
00120 
00121     switch (len)
00122     {
00123     case 2048:
00124     case 1920:
00125         mdct = fb->mdct2048;
00126         break;
00127     case 1024:
00128     case 960:
00129         mdct = fb->mdct1024;
00130         break;
00131     }
00132 
00133     faad_imdct(mdct, in_data, out_data);
00134 #else
00135     faad_imdct(fb->mdct2048, in_data, out_data);
00136 #endif
00137 }
00138 
00139 
00140 #ifdef LTP_DEC
00141 static INLINE void mdct(fb_info *fb, real_t *in_data, real_t *out_data, uint16_t len)
00142 {
00143     mdct_info *mdct = NULL;
00144 
00145     switch (len)
00146     {
00147     case 2048:
00148     case 1920:
00149         mdct = fb->mdct2048;
00150         break;
00151     case 256:
00152     case 240:
00153         mdct = fb->mdct256;
00154         break;
00155 #ifdef LD_DEC
00156     case 1024:
00157     case 960:
00158         mdct = fb->mdct1024;
00159         break;
00160 #endif
00161     }
00162 
00163     faad_mdct(mdct, in_data, out_data);
00164 }
00165 #endif
00166 
00167 void ifilter_bank(fb_info *fb, uint8_t window_sequence, uint8_t window_shape,
00168                   uint8_t window_shape_prev, real_t *freq_in,
00169                   real_t *time_out, real_t *overlap,
00170                   uint8_t object_type, uint16_t frame_len)
00171 {
00172     int16_t i;
00173     ALIGN real_t transf_buf[2*1024] = {0};
00174 
00175     const real_t *window_long = NULL;
00176     const real_t *window_long_prev = NULL;
00177     const real_t *window_short = NULL;
00178     const real_t *window_short_prev = NULL;
00179 
00180     uint16_t nlong = frame_len;
00181     uint16_t nshort = frame_len/8;
00182     uint16_t trans = nshort/2;
00183 
00184     uint16_t nflat_ls = (nlong-nshort)/2;
00185 
00186 #ifdef PROFILE
00187     int64_t count = faad_get_ts();
00188 #endif
00189 
00190     /* select windows of current frame and previous frame (Sine or KBD) */
00191 #ifdef LD_DEC
00192     if (object_type == LD)
00193     {
00194         window_long       = fb->ld_window[window_shape];
00195         window_long_prev  = fb->ld_window[window_shape_prev];
00196     } else {
00197 #endif
00198         window_long       = fb->long_window[window_shape];
00199         window_long_prev  = fb->long_window[window_shape_prev];
00200         window_short      = fb->short_window[window_shape];
00201         window_short_prev = fb->short_window[window_shape_prev];
00202 #ifdef LD_DEC
00203     }
00204 #endif
00205 
00206 #if 0
00207     for (i = 0; i < 1024; i++)
00208     {
00209         printf("%d\n", freq_in[i]);
00210     }
00211 #endif
00212 
00213 #if 0
00214     printf("%d %d\n", window_sequence, window_shape);
00215 #endif
00216 
00217     switch (window_sequence)
00218     {
00219     case ONLY_LONG_SEQUENCE:
00220         /* perform iMDCT */
00221         imdct_long(fb, freq_in, transf_buf, 2*nlong);
00222 
00223         /* add second half output of previous frame to windowed output of current frame */
00224         for (i = 0; i < nlong; i+=4)
00225         {
00226             time_out[i]   = overlap[i]   + MUL_F(transf_buf[i],window_long_prev[i]);
00227             time_out[i+1] = overlap[i+1] + MUL_F(transf_buf[i+1],window_long_prev[i+1]);
00228             time_out[i+2] = overlap[i+2] + MUL_F(transf_buf[i+2],window_long_prev[i+2]);
00229             time_out[i+3] = overlap[i+3] + MUL_F(transf_buf[i+3],window_long_prev[i+3]);
00230         }
00231 
00232         /* window the second half and save as overlap for next frame */
00233         for (i = 0; i < nlong; i+=4)
00234         {
00235             overlap[i]   = MUL_F(transf_buf[nlong+i],window_long[nlong-1-i]);
00236             overlap[i+1] = MUL_F(transf_buf[nlong+i+1],window_long[nlong-2-i]);
00237             overlap[i+2] = MUL_F(transf_buf[nlong+i+2],window_long[nlong-3-i]);
00238             overlap[i+3] = MUL_F(transf_buf[nlong+i+3],window_long[nlong-4-i]);
00239         }
00240         break;
00241 
00242     case LONG_START_SEQUENCE:
00243         /* perform iMDCT */
00244         imdct_long(fb, freq_in, transf_buf, 2*nlong);
00245 
00246         /* add second half output of previous frame to windowed output of current frame */
00247         for (i = 0; i < nlong; i+=4)
00248         {
00249             time_out[i]   = overlap[i]   + MUL_F(transf_buf[i],window_long_prev[i]);
00250             time_out[i+1] = overlap[i+1] + MUL_F(transf_buf[i+1],window_long_prev[i+1]);
00251             time_out[i+2] = overlap[i+2] + MUL_F(transf_buf[i+2],window_long_prev[i+2]);
00252             time_out[i+3] = overlap[i+3] + MUL_F(transf_buf[i+3],window_long_prev[i+3]);
00253         }
00254 
00255         /* window the second half and save as overlap for next frame */
00256         /* construct second half window using padding with 1's and 0's */
00257         for (i = 0; i < nflat_ls; i++)
00258             overlap[i] = transf_buf[nlong+i];
00259         for (i = 0; i < nshort; i++)
00260             overlap[nflat_ls+i] = MUL_F(transf_buf[nlong+nflat_ls+i],window_short[nshort-i-1]);
00261         for (i = 0; i < nflat_ls; i++)
00262             overlap[nflat_ls+nshort+i] = 0;
00263         break;
00264 
00265     case EIGHT_SHORT_SEQUENCE:
00266         /* perform iMDCT for each short block */
00267         faad_imdct(fb->mdct256, freq_in+0*nshort, transf_buf+2*nshort*0);
00268         faad_imdct(fb->mdct256, freq_in+1*nshort, transf_buf+2*nshort*1);
00269         faad_imdct(fb->mdct256, freq_in+2*nshort, transf_buf+2*nshort*2);
00270         faad_imdct(fb->mdct256, freq_in+3*nshort, transf_buf+2*nshort*3);
00271         faad_imdct(fb->mdct256, freq_in+4*nshort, transf_buf+2*nshort*4);
00272         faad_imdct(fb->mdct256, freq_in+5*nshort, transf_buf+2*nshort*5);
00273         faad_imdct(fb->mdct256, freq_in+6*nshort, transf_buf+2*nshort*6);
00274         faad_imdct(fb->mdct256, freq_in+7*nshort, transf_buf+2*nshort*7);
00275 
00276         /* add second half output of previous frame to windowed output of current frame */
00277         for (i = 0; i < nflat_ls; i++)
00278             time_out[i] = overlap[i];
00279         for(i = 0; i < nshort; i++)
00280         {
00281             time_out[nflat_ls+         i] = overlap[nflat_ls+         i] + MUL_F(transf_buf[nshort*0+i],window_short_prev[i]);
00282             time_out[nflat_ls+1*nshort+i] = overlap[nflat_ls+nshort*1+i] + MUL_F(transf_buf[nshort*1+i],window_short[nshort-1-i]) + MUL_F(transf_buf[nshort*2+i],window_short[i]);
00283             time_out[nflat_ls+2*nshort+i] = overlap[nflat_ls+nshort*2+i] + MUL_F(transf_buf[nshort*3+i],window_short[nshort-1-i]) + MUL_F(transf_buf[nshort*4+i],window_short[i]);
00284             time_out[nflat_ls+3*nshort+i] = overlap[nflat_ls+nshort*3+i] + MUL_F(transf_buf[nshort*5+i],window_short[nshort-1-i]) + MUL_F(transf_buf[nshort*6+i],window_short[i]);
00285             if (i < trans)
00286                 time_out[nflat_ls+4*nshort+i] = overlap[nflat_ls+nshort*4+i] + MUL_F(transf_buf[nshort*7+i],window_short[nshort-1-i]) + MUL_F(transf_buf[nshort*8+i],window_short[i]);
00287         }
00288 
00289         /* window the second half and save as overlap for next frame */
00290         for(i = 0; i < nshort; i++)
00291         {
00292             if (i >= trans)
00293                 overlap[nflat_ls+4*nshort+i-nlong] = MUL_F(transf_buf[nshort*7+i],window_short[nshort-1-i]) + MUL_F(transf_buf[nshort*8+i],window_short[i]);
00294             overlap[nflat_ls+5*nshort+i-nlong] = MUL_F(transf_buf[nshort*9+i],window_short[nshort-1-i]) + MUL_F(transf_buf[nshort*10+i],window_short[i]);
00295             overlap[nflat_ls+6*nshort+i-nlong] = MUL_F(transf_buf[nshort*11+i],window_short[nshort-1-i]) + MUL_F(transf_buf[nshort*12+i],window_short[i]);
00296             overlap[nflat_ls+7*nshort+i-nlong] = MUL_F(transf_buf[nshort*13+i],window_short[nshort-1-i]) + MUL_F(transf_buf[nshort*14+i],window_short[i]);
00297             overlap[nflat_ls+8*nshort+i-nlong] = MUL_F(transf_buf[nshort*15+i],window_short[nshort-1-i]);
00298         }
00299         for (i = 0; i < nflat_ls; i++)
00300             overlap[nflat_ls+nshort+i] = 0;
00301         break;
00302 
00303     case LONG_STOP_SEQUENCE:
00304         /* perform iMDCT */
00305         imdct_long(fb, freq_in, transf_buf, 2*nlong);
00306 
00307         /* add second half output of previous frame to windowed output of current frame */
00308         /* construct first half window using padding with 1's and 0's */
00309         for (i = 0; i < nflat_ls; i++)
00310             time_out[i] = overlap[i];
00311         for (i = 0; i < nshort; i++)
00312             time_out[nflat_ls+i] = overlap[nflat_ls+i] + MUL_F(transf_buf[nflat_ls+i],window_short_prev[i]);
00313         for (i = 0; i < nflat_ls; i++)
00314             time_out[nflat_ls+nshort+i] = overlap[nflat_ls+nshort+i] + transf_buf[nflat_ls+nshort+i];
00315 
00316         /* window the second half and save as overlap for next frame */
00317         for (i = 0; i < nlong; i++)
00318             overlap[i] = MUL_F(transf_buf[nlong+i],window_long[nlong-1-i]);
00319                 break;
00320     }
00321 
00322 #if 0
00323     for (i = 0; i < 1024; i++)
00324     {
00325         printf("%d\n", time_out[i]);
00326         //printf("0x%.8X\n", time_out[i]);
00327     }
00328 #endif
00329 
00330 
00331 #ifdef PROFILE
00332     count = faad_get_ts() - count;
00333     fb->cycles += count;
00334 #endif
00335 }
00336 
00337 
00338 #ifdef LTP_DEC
00339 /* only works for LTP -> no overlapping, no short blocks */
00340 void filter_bank_ltp(fb_info *fb, uint8_t window_sequence, uint8_t window_shape,
00341                      uint8_t window_shape_prev, real_t *in_data, real_t *out_mdct,
00342                      uint8_t object_type, uint16_t frame_len)
00343 {
00344     int16_t i;
00345     ALIGN real_t windowed_buf[2*1024] = {0};
00346 
00347     const real_t *window_long = NULL;
00348     const real_t *window_long_prev = NULL;
00349     const real_t *window_short = NULL;
00350     const real_t *window_short_prev = NULL;
00351 
00352     uint16_t nlong = frame_len;
00353     uint16_t nshort = frame_len/8;
00354     uint16_t nflat_ls = (nlong-nshort)/2;
00355 
00356     assert(window_sequence != EIGHT_SHORT_SEQUENCE);
00357 
00358 #ifdef LD_DEC
00359     if (object_type == LD)
00360     {
00361         window_long       = fb->ld_window[window_shape];
00362         window_long_prev  = fb->ld_window[window_shape_prev];
00363     } else {
00364 #endif
00365         window_long       = fb->long_window[window_shape];
00366         window_long_prev  = fb->long_window[window_shape_prev];
00367         window_short      = fb->short_window[window_shape];
00368         window_short_prev = fb->short_window[window_shape_prev];
00369 #ifdef LD_DEC
00370     }
00371 #endif
00372 
00373     switch(window_sequence)
00374     {
00375     case ONLY_LONG_SEQUENCE:
00376         for (i = nlong-1; i >= 0; i--)
00377         {
00378             windowed_buf[i] = MUL_F(in_data[i], window_long_prev[i]);
00379             windowed_buf[i+nlong] = MUL_F(in_data[i+nlong], window_long[nlong-1-i]);
00380         }
00381         mdct(fb, windowed_buf, out_mdct, 2*nlong);
00382         break;
00383 
00384     case LONG_START_SEQUENCE:
00385         for (i = 0; i < nlong; i++)
00386             windowed_buf[i] = MUL_F(in_data[i], window_long_prev[i]);
00387         for (i = 0; i < nflat_ls; i++)
00388             windowed_buf[i+nlong] = in_data[i+nlong];
00389         for (i = 0; i < nshort; i++)
00390             windowed_buf[i+nlong+nflat_ls] = MUL_F(in_data[i+nlong+nflat_ls], window_short[nshort-1-i]);
00391         for (i = 0; i < nflat_ls; i++)
00392             windowed_buf[i+nlong+nflat_ls+nshort] = 0;
00393         mdct(fb, windowed_buf, out_mdct, 2*nlong);
00394         break;
00395 
00396     case LONG_STOP_SEQUENCE:
00397         for (i = 0; i < nflat_ls; i++)
00398             windowed_buf[i] = 0;
00399         for (i = 0; i < nshort; i++)
00400             windowed_buf[i+nflat_ls] = MUL_F(in_data[i+nflat_ls], window_short_prev[i]);
00401         for (i = 0; i < nflat_ls; i++)
00402             windowed_buf[i+nflat_ls+nshort] = in_data[i+nflat_ls+nshort];
00403         for (i = 0; i < nlong; i++)
00404             windowed_buf[i+nlong] = MUL_F(in_data[i+nlong], window_long[nlong-1-i]);
00405         mdct(fb, windowed_buf, out_mdct, 2*nlong);
00406         break;
00407     }
00408 }
00409 #endif

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