filtbank.c
上传用户:xjjlds
上传日期:2015-12-05
资源大小:22823k
文件大小:14k
源码类别:

多媒体编程

开发平台:

Visual C++

  1. /*
  2. ** FAAD2 - Freeware Advanced Audio (AAC) Decoder including SBR decoding
  3. ** Copyright (C) 2003-2005 M. Bakker, Ahead Software AG, http://www.nero.com
  4. **  
  5. ** This program is free software; you can redistribute it and/or modify
  6. ** it under the terms of the GNU General Public License as published by
  7. ** the Free Software Foundation; either version 2 of the License, or
  8. ** (at your option) any later version.
  9. ** 
  10. ** This program is distributed in the hope that it will be useful,
  11. ** but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  13. ** GNU General Public License for more details.
  14. ** 
  15. ** You should have received a copy of the GNU General Public License
  16. ** along with this program; if not, write to the Free Software 
  17. ** Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
  18. **
  19. ** Any non-GPL usage of this software or parts of this software is strictly
  20. ** forbidden.
  21. **
  22. ** Software using this code must display the following message visibly in the
  23. ** software:
  24. ** "FAAD2 AAC/HE-AAC/HE-AACv2/DRM decoder (c) Ahead Software, www.nero.com"
  25. ** in, for example, the about-box or help/startup screen.
  26. **
  27. ** Commercial non-GPL licensing of this software is possible.
  28. ** For more info contact Ahead Software through Mpeg4AAClicense@nero.com.
  29. **
  30. ** $Id: filtbank.c,v 1.2 2005/11/01 21:41:43 gabest Exp $
  31. **/
  32. #include "common.h"
  33. #include "structs.h"
  34. #include <stdlib.h>
  35. #include <string.h>
  36. #ifdef _WIN32_WCE
  37. #define assert(x)
  38. #else
  39. #include <assert.h>
  40. #endif
  41. #include "filtbank.h"
  42. #include "decoder.h"
  43. #include "syntax.h"
  44. #include "kbd_win.h"
  45. #include "sine_win.h"
  46. #include "mdct.h"
  47. fb_info *filter_bank_init(uint16_t frame_len)
  48. {
  49.     uint16_t nshort = frame_len/8;
  50. #ifdef LD_DEC
  51.     uint16_t frame_len_ld = frame_len/2;
  52. #endif
  53.     fb_info *fb = (fb_info*)faad_malloc(sizeof(fb_info));
  54.     memset(fb, 0, sizeof(fb_info));
  55.     /* normal */
  56.     fb->mdct256 = faad_mdct_init(2*nshort);
  57.     fb->mdct2048 = faad_mdct_init(2*frame_len);
  58. #ifdef LD_DEC
  59.     /* LD */
  60.     fb->mdct1024 = faad_mdct_init(2*frame_len_ld);
  61. #endif
  62. #ifdef ALLOW_SMALL_FRAMELENGTH
  63.     if (frame_len == 1024)
  64.     {
  65. #endif
  66.         fb->long_window[0]  = sine_long_1024;
  67.         fb->short_window[0] = sine_short_128;
  68.         fb->long_window[1]  = kbd_long_1024;
  69.         fb->short_window[1] = kbd_short_128;
  70. #ifdef LD_DEC
  71.         fb->ld_window[0] = sine_mid_512;
  72.         fb->ld_window[1] = ld_mid_512;
  73. #endif
  74. #ifdef ALLOW_SMALL_FRAMELENGTH
  75.     } else /* (frame_len == 960) */ {
  76.         fb->long_window[0]  = sine_long_960;
  77.         fb->short_window[0] = sine_short_120;
  78.         fb->long_window[1]  = kbd_long_960;
  79.         fb->short_window[1] = kbd_short_120;
  80. #ifdef LD_DEC
  81.         fb->ld_window[0] = sine_mid_480;
  82.         fb->ld_window[1] = ld_mid_480;
  83. #endif
  84.     }
  85. #endif
  86.     return fb;
  87. }
  88. void filter_bank_end(fb_info *fb)
  89. {
  90.     if (fb != NULL)
  91.     {
  92. #ifdef PROFILE
  93.         printf("FB:                 %I64d cyclesn", fb->cycles);
  94. #endif
  95.         faad_mdct_end(fb->mdct256);
  96.         faad_mdct_end(fb->mdct2048);
  97. #ifdef LD_DEC
  98.         faad_mdct_end(fb->mdct1024);
  99. #endif
  100.         faad_free(fb);
  101.     }
  102. }
  103. static INLINE void imdct_long(fb_info *fb, real_t *in_data, real_t *out_data, uint16_t len)
  104. {
  105. #ifdef LD_DEC
  106.     mdct_info *mdct = NULL;
  107.     switch (len)
  108.     {
  109.     case 2048:
  110.     case 1920:
  111.         mdct = fb->mdct2048;
  112.         break;
  113.     case 1024:
  114.     case 960:
  115.         mdct = fb->mdct1024;
  116.         break;
  117.     }
  118.     faad_imdct(mdct, in_data, out_data);
  119. #else
  120.     faad_imdct(fb->mdct2048, in_data, out_data);
  121. #endif
  122. }
  123. #ifdef LTP_DEC
  124. static INLINE void mdct(fb_info *fb, real_t *in_data, real_t *out_data, uint16_t len)
  125. {
  126.     mdct_info *mdct = NULL;
  127.     switch (len)
  128.     {
  129.     case 2048:
  130.     case 1920:
  131.         mdct = fb->mdct2048;
  132.         break;
  133.     case 256:
  134.     case 240:
  135.         mdct = fb->mdct256;
  136.         break;
  137. #ifdef LD_DEC
  138.     case 1024:
  139.     case 960:
  140.         mdct = fb->mdct1024;
  141.         break;
  142. #endif
  143.     }
  144.     faad_mdct(mdct, in_data, out_data);
  145. }
  146. #endif
  147. void ifilter_bank(fb_info *fb, uint8_t window_sequence, uint8_t window_shape,
  148.                   uint8_t window_shape_prev, real_t *freq_in,
  149.                   real_t *time_out, real_t *overlap,
  150.                   uint8_t object_type, uint16_t frame_len)
  151. {
  152.     int16_t i;
  153.     ALIGN real_t transf_buf[2*1024] = {0};
  154.     const real_t *window_long = NULL;
  155.     const real_t *window_long_prev = NULL;
  156.     const real_t *window_short = NULL;
  157.     const real_t *window_short_prev = NULL;
  158.     uint16_t nlong = frame_len;
  159.     uint16_t nshort = frame_len/8;
  160.     uint16_t trans = nshort/2;
  161.     uint16_t nflat_ls = (nlong-nshort)/2;
  162. #ifdef PROFILE
  163.     int64_t count = faad_get_ts();
  164. #endif
  165.     /* select windows of current frame and previous frame (Sine or KBD) */
  166. #ifdef LD_DEC
  167.     if (object_type == LD)
  168.     {
  169.         window_long       = fb->ld_window[window_shape];
  170.         window_long_prev  = fb->ld_window[window_shape_prev];
  171.     } else {
  172. #endif
  173.         window_long       = fb->long_window[window_shape];
  174.         window_long_prev  = fb->long_window[window_shape_prev];
  175.         window_short      = fb->short_window[window_shape];
  176.         window_short_prev = fb->short_window[window_shape_prev];
  177. #ifdef LD_DEC
  178.     }
  179. #endif
  180. #if 0
  181.     for (i = 0; i < 1024; i++)
  182.     {
  183.         printf("%dn", freq_in[i]);
  184.     }
  185. #endif
  186. #if 0
  187.     printf("%d %dn", window_sequence, window_shape);
  188. #endif
  189.     switch (window_sequence)
  190.     {
  191.     case ONLY_LONG_SEQUENCE:
  192.         /* perform iMDCT */
  193.         imdct_long(fb, freq_in, transf_buf, 2*nlong);
  194.         /* add second half output of previous frame to windowed output of current frame */
  195.         for (i = 0; i < nlong; i+=4)
  196.         {
  197.             time_out[i]   = overlap[i]   + MUL_F(transf_buf[i],window_long_prev[i]);
  198.             time_out[i+1] = overlap[i+1] + MUL_F(transf_buf[i+1],window_long_prev[i+1]);
  199.             time_out[i+2] = overlap[i+2] + MUL_F(transf_buf[i+2],window_long_prev[i+2]);
  200.             time_out[i+3] = overlap[i+3] + MUL_F(transf_buf[i+3],window_long_prev[i+3]);
  201.         }
  202.         /* window the second half and save as overlap for next frame */
  203.         for (i = 0; i < nlong; i+=4)
  204.         {
  205.             overlap[i]   = MUL_F(transf_buf[nlong+i],window_long[nlong-1-i]);
  206.             overlap[i+1] = MUL_F(transf_buf[nlong+i+1],window_long[nlong-2-i]);
  207.             overlap[i+2] = MUL_F(transf_buf[nlong+i+2],window_long[nlong-3-i]);
  208.             overlap[i+3] = MUL_F(transf_buf[nlong+i+3],window_long[nlong-4-i]);
  209.         }
  210.         break;
  211.     case LONG_START_SEQUENCE:
  212.         /* perform iMDCT */
  213.         imdct_long(fb, freq_in, transf_buf, 2*nlong);
  214.         /* add second half output of previous frame to windowed output of current frame */
  215.         for (i = 0; i < nlong; i+=4)
  216.         {
  217.             time_out[i]   = overlap[i]   + MUL_F(transf_buf[i],window_long_prev[i]);
  218.             time_out[i+1] = overlap[i+1] + MUL_F(transf_buf[i+1],window_long_prev[i+1]);
  219.             time_out[i+2] = overlap[i+2] + MUL_F(transf_buf[i+2],window_long_prev[i+2]);
  220.             time_out[i+3] = overlap[i+3] + MUL_F(transf_buf[i+3],window_long_prev[i+3]);
  221.         }
  222.         /* window the second half and save as overlap for next frame */
  223.         /* construct second half window using padding with 1's and 0's */
  224.         for (i = 0; i < nflat_ls; i++)
  225.             overlap[i] = transf_buf[nlong+i];
  226.         for (i = 0; i < nshort; i++)
  227.             overlap[nflat_ls+i] = MUL_F(transf_buf[nlong+nflat_ls+i],window_short[nshort-i-1]);
  228.         for (i = 0; i < nflat_ls; i++)
  229.             overlap[nflat_ls+nshort+i] = 0;
  230.         break;
  231.     case EIGHT_SHORT_SEQUENCE:
  232.         /* perform iMDCT for each short block */
  233.         faad_imdct(fb->mdct256, freq_in+0*nshort, transf_buf+2*nshort*0);
  234.         faad_imdct(fb->mdct256, freq_in+1*nshort, transf_buf+2*nshort*1);
  235.         faad_imdct(fb->mdct256, freq_in+2*nshort, transf_buf+2*nshort*2);
  236.         faad_imdct(fb->mdct256, freq_in+3*nshort, transf_buf+2*nshort*3);
  237.         faad_imdct(fb->mdct256, freq_in+4*nshort, transf_buf+2*nshort*4);
  238.         faad_imdct(fb->mdct256, freq_in+5*nshort, transf_buf+2*nshort*5);
  239.         faad_imdct(fb->mdct256, freq_in+6*nshort, transf_buf+2*nshort*6);
  240.         faad_imdct(fb->mdct256, freq_in+7*nshort, transf_buf+2*nshort*7);
  241.         /* add second half output of previous frame to windowed output of current frame */
  242.         for (i = 0; i < nflat_ls; i++)
  243.             time_out[i] = overlap[i];
  244.         for(i = 0; i < nshort; i++)
  245.         {
  246.             time_out[nflat_ls+         i] = overlap[nflat_ls+         i] + MUL_F(transf_buf[nshort*0+i],window_short_prev[i]);
  247.             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]);
  248.             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]);
  249.             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]);
  250.             if (i < trans)
  251.                 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]);
  252.         }
  253.         /* window the second half and save as overlap for next frame */
  254.         for(i = 0; i < nshort; i++)
  255.         {
  256.             if (i >= trans)
  257.                 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]);
  258.             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]);
  259.             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]);
  260.             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]);
  261.             overlap[nflat_ls+8*nshort+i-nlong] = MUL_F(transf_buf[nshort*15+i],window_short[nshort-1-i]);
  262.         }
  263.         for (i = 0; i < nflat_ls; i++)
  264.             overlap[nflat_ls+nshort+i] = 0;
  265.         break;
  266.     case LONG_STOP_SEQUENCE:
  267.         /* perform iMDCT */
  268.         imdct_long(fb, freq_in, transf_buf, 2*nlong);
  269.         /* add second half output of previous frame to windowed output of current frame */
  270.         /* construct first half window using padding with 1's and 0's */
  271.         for (i = 0; i < nflat_ls; i++)
  272.             time_out[i] = overlap[i];
  273.         for (i = 0; i < nshort; i++)
  274.             time_out[nflat_ls+i] = overlap[nflat_ls+i] + MUL_F(transf_buf[nflat_ls+i],window_short_prev[i]);
  275.         for (i = 0; i < nflat_ls; i++)
  276.             time_out[nflat_ls+nshort+i] = overlap[nflat_ls+nshort+i] + transf_buf[nflat_ls+nshort+i];
  277.         /* window the second half and save as overlap for next frame */
  278.         for (i = 0; i < nlong; i++)
  279.             overlap[i] = MUL_F(transf_buf[nlong+i],window_long[nlong-1-i]);
  280. break;
  281.     }
  282. #if 0
  283.     for (i = 0; i < 1024; i++)
  284.     {
  285.         printf("%dn", time_out[i]);
  286.         //printf("0x%.8Xn", time_out[i]);
  287.     }
  288. #endif
  289. #ifdef PROFILE
  290.     count = faad_get_ts() - count;
  291.     fb->cycles += count;
  292. #endif
  293. }
  294. #ifdef LTP_DEC
  295. /* only works for LTP -> no overlapping, no short blocks */
  296. void filter_bank_ltp(fb_info *fb, uint8_t window_sequence, uint8_t window_shape,
  297.                      uint8_t window_shape_prev, real_t *in_data, real_t *out_mdct,
  298.                      uint8_t object_type, uint16_t frame_len)
  299. {
  300.     int16_t i;
  301.     ALIGN real_t windowed_buf[2*1024] = {0};
  302.     const real_t *window_long = NULL;
  303.     const real_t *window_long_prev = NULL;
  304.     const real_t *window_short = NULL;
  305.     const real_t *window_short_prev = NULL;
  306.     uint16_t nlong = frame_len;
  307.     uint16_t nshort = frame_len/8;
  308.     uint16_t nflat_ls = (nlong-nshort)/2;
  309.     assert(window_sequence != EIGHT_SHORT_SEQUENCE);
  310. #ifdef LD_DEC
  311.     if (object_type == LD)
  312.     {
  313.         window_long       = fb->ld_window[window_shape];
  314.         window_long_prev  = fb->ld_window[window_shape_prev];
  315.     } else {
  316. #endif
  317.         window_long       = fb->long_window[window_shape];
  318.         window_long_prev  = fb->long_window[window_shape_prev];
  319.         window_short      = fb->short_window[window_shape];
  320.         window_short_prev = fb->short_window[window_shape_prev];
  321. #ifdef LD_DEC
  322.     }
  323. #endif
  324.     switch(window_sequence)
  325.     {
  326.     case ONLY_LONG_SEQUENCE:
  327.         for (i = nlong-1; i >= 0; i--)
  328.         {
  329.             windowed_buf[i] = MUL_F(in_data[i], window_long_prev[i]);
  330.             windowed_buf[i+nlong] = MUL_F(in_data[i+nlong], window_long[nlong-1-i]);
  331.         }
  332.         mdct(fb, windowed_buf, out_mdct, 2*nlong);
  333.         break;
  334.     case LONG_START_SEQUENCE:
  335.         for (i = 0; i < nlong; i++)
  336.             windowed_buf[i] = MUL_F(in_data[i], window_long_prev[i]);
  337.         for (i = 0; i < nflat_ls; i++)
  338.             windowed_buf[i+nlong] = in_data[i+nlong];
  339.         for (i = 0; i < nshort; i++)
  340.             windowed_buf[i+nlong+nflat_ls] = MUL_F(in_data[i+nlong+nflat_ls], window_short[nshort-1-i]);
  341.         for (i = 0; i < nflat_ls; i++)
  342.             windowed_buf[i+nlong+nflat_ls+nshort] = 0;
  343.         mdct(fb, windowed_buf, out_mdct, 2*nlong);
  344.         break;
  345.     case LONG_STOP_SEQUENCE:
  346.         for (i = 0; i < nflat_ls; i++)
  347.             windowed_buf[i] = 0;
  348.         for (i = 0; i < nshort; i++)
  349.             windowed_buf[i+nflat_ls] = MUL_F(in_data[i+nflat_ls], window_short_prev[i]);
  350.         for (i = 0; i < nflat_ls; i++)
  351.             windowed_buf[i+nflat_ls+nshort] = in_data[i+nflat_ls+nshort];
  352.         for (i = 0; i < nlong; i++)
  353.             windowed_buf[i+nlong] = MUL_F(in_data[i+nlong], window_long[nlong-1-i]);
  354.         mdct(fb, windowed_buf, out_mdct, 2*nlong);
  355.         break;
  356.     }
  357. }
  358. #endif