drm_dec.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: drm_dec.c,v 1.1 2005/11/06 10:54:47 gabest Exp $
00031 **/
00032 
00033 #include <stdlib.h>
00034 #include <stdio.h>
00035 #include <string.h>
00036 #include <math.h>
00037 #include "common.h"
00038 
00039 #ifdef DRM
00040 
00041 #include "sbr_dec.h"
00042 #include "drm_dec.h"
00043 #include "bits.h"
00044 
00045 /* constants */
00046 #define DECAY_CUTOFF         3
00047 #define DECAY_SLOPE          0.05f
00048 
00049 /* type definitaions */
00050 typedef const int8_t (*drm_ps_huff_tab)[2];
00051 
00052 
00053 /* binary search huffman tables */
00054 static const int8_t f_huffman_sa[][2] = 
00055 {
00056     { /*0*/ -15, 1 },             /* index 0: 1 bits:  x */
00057     { 2, 3 },                     /* index 1: 2 bits:  1x */
00058     { /*7*/ -8, 4 },              /* index 2: 3 bits:  10x */
00059     { 5, 6 },                     /* index 3: 3 bits:  11x */
00060     { /*1*/ -14, /*-1*/ -16 },    /* index 4: 4 bits:  101x */
00061     { /*-2*/ -17, 7 },            /* index 5: 4 bits:  110x */
00062     { 8, 9 },                     /* index 6: 4 bits:  111x */
00063     { /*2*/ -13, /*-3*/ -18 },    /* index 7: 5 bits:  1101x */
00064     { /*3*/ -12, 10 },            /* index 8: 5 bits:  1110x */
00065     { 11, 12 },                   /* index 9: 5 bits:  1111x */
00066     { /*4*/ -11, /*5*/ -10 },     /* index 10: 6 bits: 11101x */
00067     { /*-4*/ -19, /*-5*/ -20 },   /* index 11: 6 bits: 11110x */
00068     { /*6*/ -9, 13 },             /* index 12: 6 bits: 11111x */
00069     { /*-7*/ -22, /*-6*/ -21 }    /* index 13: 7 bits: 111111x */
00070 };
00071 
00072 static const int8_t t_huffman_sa[][2] = 
00073 {
00074     { /*0*/ -15, 1 },             /* index 0: 1 bits: x */
00075     { 2, 3 },                     /* index 1: 2 bits: 1x */
00076     { /*-1*/ -16, /*1*/ -14 },    /* index 2: 3 bits: 10x */
00077     { 4, 5 },                     /* index 3: 3 bits: 11x */
00078     { /*-2*/ -17, /*2*/ -13 },    /* index 4: 4 bits: 110x */
00079     { 6, 7 },                     /* index 5: 4 bits: 111x */
00080     { /*-3*/ -18, /*3*/ -12 },    /* index 6: 5 bits: 1110x */
00081     { 8, 9 },                     /* index 7: 5 bits: 1111x */
00082     { /*-4*/ -19, /*4*/ -11 },    /* index 8: 6 bits: 11110x */
00083     { 10, 11 },                   /* index 9: 6 bits: 11111x */
00084     { /*-5*/ -20, /*5*/ -10 },    /* index 10: 7 bits: 111110x */
00085     { /*-6*/ -21, 12 },           /* index 11: 7 bits: 111111x */
00086     { /*-7*/ -22, 13 },           /* index 12: 8 bits: 1111111x */
00087     { /*6*/ -9, /*7*/ -8 }        /* index 13: 9 bits: 11111111x */
00088 };
00089 
00090 static const int8_t f_huffman_pan[][2] = 
00091 {
00092     { /*0*/ -15, 1 },             /* index 0: 1 bits: x */
00093     { /*-1*/ -16, 2 },            /* index 1: 2 bits: 1x */
00094     { /*1*/ -14, 3 },             /* index 2: 3 bits: 11x */
00095     { 4, 5 },                     /* index 3: 4 bits: 111x */
00096     { /*-2*/ -17, /*2*/ -13 },    /* index 4: 5 bits: 1110x */
00097     { 6, 7 },                     /* index 5: 5 bits: 1111x */
00098     { /*-3*/ -18, /*3*/ -12 },    /* index 6: 6 bits: 11110x */
00099     { 8, 9 },                     /* index 7: 6 bits: 11111x */
00100     { /*-4*/ -19, /*4*/ -11 },    /* index 8: 7 bits: 111110x */
00101     { 10, 11 },                   /* index 9: 7 bits: 111111x */
00102     { /*-5*/ -20, /*5*/ -10 },    /* index 10: 8 bits: 1111110x */
00103     { 12, 13 },                   /* index 11: 8 bits: 1111111x */
00104     { /*-6*/ -21, /*6*/ -9 },     /* index 12: 9 bits: 11111110x */
00105     { /*-7*/ -22, 14 },           /* index 13: 9 bits: 11111111x */
00106     { /*7*/ -8, 15 },             /* index 14: 10 bits: 111111111x */
00107     { 16, 17 },                   /* index 15: 11 bits: 1111111111x */
00108     { /*-8*/ -23, /*8*/ -7 },     /* index 16: 12 bits: 11111111110x */
00109     { 18, 19 },                   /* index 17: 12 bits: 11111111111x */
00110     { /*-10*/ -25, 20 },          /* index 18: 13 bits: 111111111110x */
00111     { 21, 22 },                   /* index 19: 13 bits: 111111111111x */
00112     { /*-9*/ -24, /*9*/ -6 },     /* index 20: 14 bits: 1111111111101x */
00113     { /*10*/ -5, 23 },            /* index 21: 14 bits: 1111111111110x */
00114     { 24, 25 },                   /* index 22: 14 bits: 1111111111111x */
00115     { /*-13*/ -28, /*-11*/ -26 }, /* index 23: 15 bits: 11111111111101x */
00116     { /*11*/ -4, /*13*/ -2 },     /* index 24: 15 bits: 11111111111110x */
00117     { 26, 27 },                   /* index 25: 15 bits: 11111111111111x */
00118     { /*-14*/ -29, /*-12*/ -27 }, /* index 26: 16 bits: 111111111111110x */
00119     { /*12*/ -3, /*14*/ -1 }      /* index 27: 16 bits: 111111111111111x */
00120 };
00121 
00122 static const int8_t t_huffman_pan[][2] = 
00123 {
00124     { /*0*/ -15, 1 },             /* index 0: 1 bits: x */
00125     { /*-1*/ -16, 2 },            /* index 1: 2 bits: 1x */
00126     { /*1*/ -14, 3 },             /* index 2: 3 bits: 11x */
00127     { /*-2*/ -17, 4 },            /* index 3: 4 bits: 111x */
00128     { /*2*/ -13, 5 },             /* index 4: 5 bits: 1111x */
00129     { /*-3*/ -18, 6 },            /* index 5: 6 bits: 11111x */
00130     { /*3*/ -12, 7 },             /* index 6: 7 bits: 111111x */
00131     { /*-4*/ -19, 8 },            /* index 7: 8 bits: 1111111x */
00132     { /*4*/ -11, 9 },             /* index 8: 9 bits: 11111111x */
00133     { 10, 11 },                   /* index 9: 10 bits: 111111111x */
00134     { /*-5*/ -20, /*5*/ -10 },    /* index 10: 11 bits: 1111111110x */
00135     { 12, 13 },                   /* index 11: 11 bits: 1111111111x */
00136     { /*-6*/ -21, /*6*/ -9 },     /* index 12: 12 bits: 11111111110x */
00137     { 14, 15 },                   /* index 13: 12 bits: 11111111111x */
00138     { /*-7*/ -22, /*7*/ -8 },     /* index 14: 13 bits: 111111111110x */
00139     { 16, 17 },                   /* index 15: 13 bits: 111111111111x */
00140     { /*-8*/ -23, /*8*/ -7 },     /* index 16: 14 bits: 1111111111110x */
00141     { 18, 19 },                   /* index 17: 14 bits: 1111111111111x */
00142     { /*-10*/ -25, /*10*/ -5 },   /* index 18: 15 bits: 11111111111110x */
00143     { 20, 21 },                   /* index 19: 15 bits: 11111111111111x */
00144     { /*-9*/ -24, /*9*/ -6 },     /* index 20: 16 bits: 111111111111110x */
00145     { 22, 23 },                   /* index 21: 16 bits: 111111111111111x */
00146     { 24, 25 },                   /* index 22: 17 bits: 1111111111111110x */
00147     { 26, 27 },                   /* index 23: 17 bits: 1111111111111111x */
00148     { /*-14*/ -29, /*-13*/ -28 }, /* index 24: 18 bits: 11111111111111100x */
00149     { /*-12*/ -27, /*-11*/ -26 }, /* index 25: 18 bits: 11111111111111101x */
00150     { /*11*/ -4, /*12*/ -3 },     /* index 26: 18 bits: 11111111111111110x */
00151     { /*13*/ -2, /*14*/ -1 }      /* index 27: 18 bits: 11111111111111111x */
00152 };
00153 
00154 /* There are 3 classes in the standard but the last 2 are identical */
00155 static const real_t sa_quant[8][2] = 
00156 {
00157     { FRAC_CONST(0.0000), FRAC_CONST(0.0000) },
00158     { FRAC_CONST(0.0501), FRAC_CONST(0.1778) },
00159     { FRAC_CONST(0.0706), FRAC_CONST(0.2818) },
00160     { FRAC_CONST(0.0995), FRAC_CONST(0.4467) },
00161     { FRAC_CONST(0.1399), FRAC_CONST(0.5623) },
00162     { FRAC_CONST(0.1957), FRAC_CONST(0.7079) },
00163     { FRAC_CONST(0.2713), FRAC_CONST(0.8913) },
00164     { FRAC_CONST(0.3699), FRAC_CONST(1.0000) },
00165 };
00166 
00167 /* We don't need the actual quantizer values */
00168 #if 0
00169 static const real_t pan_quant[8][5] = 
00170 {
00171     { COEF_CONST(0.0000), COEF_CONST(0.0000), COEF_CONST(0.0000), COEF_CONST(0.0000), COEF_CONST(0.0000) },
00172     { COEF_CONST(0.1661), COEF_CONST(0.1661), COEF_CONST(0.3322), COEF_CONST(0.3322), COEF_CONST(0.3322) },
00173     { COEF_CONST(0.3322), COEF_CONST(0.3322), COEF_CONST(0.6644), COEF_CONST(0.8305), COEF_CONST(0.8305) },
00174     { COEF_CONST(0.4983), COEF_CONST(0.6644), COEF_CONST(0.9966), COEF_CONST(1.4949), COEF_CONST(1.6610) },
00175     { COEF_CONST(0.6644), COEF_CONST(0.9966), COEF_CONST(1.4949), COEF_CONST(2.1593), COEF_CONST(2.4914) },
00176     { COEF_CONST(0.8305), COEF_CONST(1.3288), COEF_CONST(2.1593), COEF_CONST(2.9897), COEF_CONST(3.4880) },
00177     { COEF_CONST(0.9966), COEF_CONST(1.8271), COEF_CONST(2.8236), COEF_CONST(3.8202), COEF_CONST(4.6507) },
00178     { COEF_CONST(1.3288), COEF_CONST(2.3253), COEF_CONST(3.4880), COEF_CONST(4.6507), COEF_CONST(5.8134) },
00179 };
00180 #endif
00181 
00182 /* 2^(pan_quant[x][y] */
00183 static const real_t pan_pow_2_pos[8][5] = {
00184     { REAL_CONST(1.0000000), REAL_CONST(1.0000000), REAL_CONST(1.0000000), REAL_CONST(1.0000000), REAL_CONST(1.0000000)  },
00185     { REAL_CONST(1.1220021), REAL_CONST(1.1220021), REAL_CONST(1.2589312), REAL_CONST(1.2589312), REAL_CONST(1.2589312)  },
00186     { REAL_CONST(1.2589312), REAL_CONST(1.2589312), REAL_CONST(1.5849090), REAL_CONST(1.7783016), REAL_CONST(1.7783016)  },
00187     { REAL_CONST(1.4125481), REAL_CONST(1.5849090), REAL_CONST(1.9952921), REAL_CONST(2.8184461), REAL_CONST(3.1623565)  },
00188     { REAL_CONST(1.5849090), REAL_CONST(1.9952922), REAL_CONST(2.8184461), REAL_CONST(4.4669806), REAL_CONST(5.6232337)  },
00189     { REAL_CONST(1.7783016), REAL_CONST(2.5119365), REAL_CONST(4.4669806), REAL_CONST(7.9430881), REAL_CONST(11.219994)  },
00190     { REAL_CONST(1.9952921), REAL_CONST(3.5482312), REAL_CONST(7.0792671), REAL_CONST(14.125206), REAL_CONST(25.118876)  },
00191     { REAL_CONST(2.5119365), REAL_CONST(5.0116998), REAL_CONST(11.219994), REAL_CONST(25.118876), REAL_CONST(56.235140)  }
00192 };
00193 
00194 /* 2^(-pan_quant[x][y] */
00195 static const real_t pan_pow_2_neg[8][5] = {
00196     { REAL_CONST(1),         REAL_CONST(1),         REAL_CONST(1),         REAL_CONST(1),         REAL_CONST(1)          },
00197     { REAL_CONST(0.8912487), REAL_CONST(0.8912487), REAL_CONST(0.7943242), REAL_CONST(0.7943242), REAL_CONST(0.7943242)  },
00198     { REAL_CONST(0.7943242), REAL_CONST(0.7943242), REAL_CONST(0.6309511), REAL_CONST(0.5623344), REAL_CONST(0.5623344)  },
00199     { REAL_CONST(0.7079405), REAL_CONST(0.6309511), REAL_CONST(0.5011797), REAL_CONST(0.3548054), REAL_CONST(0.3162199)  },
00200     { REAL_CONST(0.6309511), REAL_CONST(0.5011797), REAL_CONST(0.3548054), REAL_CONST(0.2238649), REAL_CONST(0.1778336)  },
00201     { REAL_CONST(0.5623343), REAL_CONST(0.3980992), REAL_CONST(0.2238649), REAL_CONST(0.1258956), REAL_CONST(0.0891266)  },
00202     { REAL_CONST(0.5011797), REAL_CONST(0.2818306), REAL_CONST(0.1412576), REAL_CONST(0.0707954), REAL_CONST(0.0398107)  },
00203     { REAL_CONST(0.3980992), REAL_CONST(0.1995331), REAL_CONST(0.0891267), REAL_CONST(0.0398107), REAL_CONST(0.0177825)  }
00204 };
00205 
00206 /* 2^(pan_quant[x][y]/30) */
00207 static const real_t pan_pow_2_30_pos[8][5] = {
00208     { COEF_CONST(1),           COEF_CONST(1),           COEF_CONST(1),           COEF_CONST(1),           COEF_CONST(1)           }, 
00209     { COEF_CONST(1.003845098), COEF_CONST(1.003845098), COEF_CONST(1.007704982), COEF_CONST(1.007704982), COEF_CONST(1.007704982) }, 
00210     { COEF_CONST(1.007704982), COEF_CONST(1.007704982), COEF_CONST(1.01546933),  COEF_CONST(1.019373909), COEF_CONST(1.019373909) }, 
00211     { COEF_CONST(1.011579706), COEF_CONST(1.01546933),  COEF_CONST(1.023293502), COEF_CONST(1.035142941), COEF_CONST(1.039123167) }, 
00212     { COEF_CONST(1.01546933),  COEF_CONST(1.023293502), COEF_CONST(1.035142941), COEF_CONST(1.051155908), COEF_CONST(1.059252598) },
00213     { COEF_CONST(1.019373909), COEF_CONST(1.03117796),  COEF_CONST(1.051155908), COEF_CONST(1.071518432), COEF_CONST(1.0839263)   }, 
00214     { COEF_CONST(1.023293502), COEF_CONST(1.043118698), COEF_CONST(1.067414119), COEF_CONST(1.092277933), COEF_CONST(1.113439626) }, 
00215     { COEF_CONST(1.03117796),  COEF_CONST(1.055195268), COEF_CONST(1.0839263),   COEF_CONST(1.113439626), COEF_CONST(1.143756546) }
00216 };
00217 
00218 /* 2^(-pan_quant[x][y]/30) */
00219 static const real_t pan_pow_2_30_neg[8][5] = {
00220     { COEF_CONST(1),           COEF_CONST(1),           COEF_CONST(1),           COEF_CONST(1),           COEF_CONST(1)           },
00221     { COEF_CONST(0.99616963),  COEF_CONST(0.99616963),  COEF_CONST(0.992353931), COEF_CONST(0.992353931), COEF_CONST(0.99235393)  }, 
00222     { COEF_CONST(0.992353931), COEF_CONST(0.992353931), COEF_CONST(0.984766325), COEF_CONST(0.980994305), COEF_CONST(0.980994305) }, 
00223     { COEF_CONST(0.988552848), COEF_CONST(0.984766325), COEF_CONST(0.977236734), COEF_CONST(0.966050157), COEF_CONST(0.962349827) }, 
00224     { COEF_CONST(0.984766325), COEF_CONST(0.977236734), COEF_CONST(0.966050157), COEF_CONST(0.951333663), COEF_CONST(0.944061881) }, 
00225     { COEF_CONST(0.980994305), COEF_CONST(0.969764715), COEF_CONST(0.951333663), COEF_CONST(0.933255062), COEF_CONST(0.922571949) }, 
00226     { COEF_CONST(0.977236734), COEF_CONST(0.958663671), COEF_CONST(0.936843519), COEF_CONST(0.915517901), COEF_CONST(0.898117847) }, 
00227     { COEF_CONST(0.969764715), COEF_CONST(0.947691892), COEF_CONST(0.922571949), COEF_CONST(0.898117847), COEF_CONST(0.874311936) }
00228 };
00229 
00230 static const real_t g_decayslope[MAX_SA_BAND] = {
00231     FRAC_CONST(1),   FRAC_CONST(1),   FRAC_CONST(1),   FRAC_CONST(0.95),FRAC_CONST(0.9), FRAC_CONST(0.85), FRAC_CONST(0.8), 
00232     FRAC_CONST(0.75),FRAC_CONST(0.7), FRAC_CONST(0.65),FRAC_CONST(0.6), FRAC_CONST(0.55),FRAC_CONST(0.5),  FRAC_CONST(0.45), 
00233     FRAC_CONST(0.4), FRAC_CONST(0.35),FRAC_CONST(0.3), FRAC_CONST(0.25),FRAC_CONST(0.2), FRAC_CONST(0.15), FRAC_CONST(0.1),
00234     FRAC_CONST(0.05),FRAC_CONST(0),   FRAC_CONST(0),   FRAC_CONST(0),   FRAC_CONST(0),   FRAC_CONST(0),    FRAC_CONST(0), 
00235     FRAC_CONST(0),   FRAC_CONST(0),   FRAC_CONST(0),   FRAC_CONST(0),   FRAC_CONST(0),   FRAC_CONST(0),    FRAC_CONST(0),  
00236     FRAC_CONST(0),   FRAC_CONST(0),   FRAC_CONST(0),   FRAC_CONST(0),   FRAC_CONST(0),   FRAC_CONST(0),    FRAC_CONST(0),   
00237     FRAC_CONST(0),   FRAC_CONST(0),   FRAC_CONST(0)
00238 };
00239 
00240 static const real_t sa_sqrt_1_minus[8][2] = {
00241     { FRAC_CONST(1),            FRAC_CONST(1)           },
00242     { FRAC_CONST(0.998744206),  FRAC_CONST(0.984066644) },
00243     { FRAC_CONST(0.997504707),  FRAC_CONST(0.959473168) },
00244     { FRAC_CONST(0.995037562),  FRAC_CONST(0.894683804) },
00245     { FRAC_CONST(0.990165638),  FRAC_CONST(0.826933317) },
00246     { FRAC_CONST(0.980663811),  FRAC_CONST(0.706312672) },
00247     { FRAC_CONST(0.962494836),  FRAC_CONST(0.45341406)  },
00248     { FRAC_CONST(0.929071574),  FRAC_CONST(0)           }
00249 };
00250 
00251 static const uint8_t sa_freq_scale[9] = 
00252 {
00253     0, 1, 2, 3, 5, 7, 10, 13, 23
00254 };
00255 
00256 static const uint8_t pan_freq_scale[21] = 
00257 {
00258     0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10,
00259     11, 12, 13, 14, 15, 18, 22, 26, 32, 64
00260 };
00261 
00262 static const uint8_t pan_quant_class[20] = 
00263 {
00264     0, 1, 1, 1, 1, 1, 1, 1, 1, 1,
00265     2, 2, 2, 2, 3, 3, 3, 4, 4, 4
00266 };
00267 
00268 /* Inverse mapping lookup */
00269 static const uint8_t pan_inv_freq[64] = {
00270      0,  1,  2,  3,  4,  5,  6,  7,  8,  9, 10, 11, 12, 13, 14, 15, 
00271     15, 15, 16, 16, 16, 16, 17, 17, 17, 17, 18, 18, 18, 18, 18, 18, 
00272     19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19,
00273     19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19
00274 };
00275 
00276 static const uint8_t sa_inv_freq[MAX_SA_BAND] = {
00277     0, 1, 2, 3, 3, 4, 4, 5, 5, 5, 6, 6, 6,
00278     7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 
00279     7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 
00280     7, 7, 7, 7, 7, 7, 7
00281 };
00282 
00283 static const real_t filter_coeff[] = 
00284 {
00285     FRAC_CONST(0.65143905754106),
00286     FRAC_CONST(0.56471812200776),
00287     FRAC_CONST(0.48954165955695)
00288 };
00289 
00290 static const uint8_t delay_length[3] = 
00291 {
00292     3, 4, 5
00293 };
00294 
00295 static const real_t delay_fraction[] = 
00296 {
00297     FRAC_CONST(0.43), FRAC_CONST(0.75), FRAC_CONST(0.347)
00298 };
00299 
00300 static const real_t peak_decay = FRAC_CONST(0.76592833836465);
00301 
00302 static const real_t smooth_coeff = FRAC_CONST(0.25);
00303 
00304 /* Please note that these are the same tables as in plain PS */
00305 static const complex_t Q_Fract_allpass_Qmf[][3] = {
00306     { { FRAC_CONST(0.7804303765), FRAC_CONST(0.6252426505) }, { FRAC_CONST(0.3826834261), FRAC_CONST(0.9238795042) }, { FRAC_CONST(0.8550928831), FRAC_CONST(0.5184748173) } },
00307     { { FRAC_CONST(-0.4399392009), FRAC_CONST(0.8980275393) }, { FRAC_CONST(-0.9238795042), FRAC_CONST(-0.3826834261) }, { FRAC_CONST(-0.0643581524), FRAC_CONST(0.9979268909) } },
00308     { { FRAC_CONST(-0.9723699093), FRAC_CONST(-0.2334454209) }, { FRAC_CONST(0.9238795042), FRAC_CONST(-0.3826834261) }, { FRAC_CONST(-0.9146071672), FRAC_CONST(0.4043435752) } },
00309     { { FRAC_CONST(0.0157073960), FRAC_CONST(-0.9998766184) }, { FRAC_CONST(-0.3826834261), FRAC_CONST(0.9238795042) }, { FRAC_CONST(-0.7814115286), FRAC_CONST(-0.6240159869) } },
00310     { { FRAC_CONST(0.9792228341), FRAC_CONST(-0.2027871907) }, { FRAC_CONST(-0.3826834261), FRAC_CONST(-0.9238795042) }, { FRAC_CONST(0.1920081824), FRAC_CONST(-0.9813933372) } },
00311     { { FRAC_CONST(0.4115142524), FRAC_CONST(0.9114032984) }, { FRAC_CONST(0.9238795042), FRAC_CONST(0.3826834261) }, { FRAC_CONST(0.9589683414), FRAC_CONST(-0.2835132182) } },
00312     { { FRAC_CONST(-0.7996847630), FRAC_CONST(0.6004201174) }, { FRAC_CONST(-0.9238795042), FRAC_CONST(0.3826834261) }, { FRAC_CONST(0.6947838664), FRAC_CONST(0.7192186117) } },
00313     { { FRAC_CONST(-0.7604058385), FRAC_CONST(-0.6494481564) }, { FRAC_CONST(0.3826834261), FRAC_CONST(-0.9238795042) }, { FRAC_CONST(-0.3164770305), FRAC_CONST(0.9486001730) } },
00314     { { FRAC_CONST(0.4679299891), FRAC_CONST(-0.8837655187) }, { FRAC_CONST(0.3826834261), FRAC_CONST(0.9238795042) }, { FRAC_CONST(-0.9874414206), FRAC_CONST(0.1579856575) } },
00315     { { FRAC_CONST(0.9645573497), FRAC_CONST(0.2638732493) }, { FRAC_CONST(-0.9238795042), FRAC_CONST(-0.3826834261) }, { FRAC_CONST(-0.5966450572), FRAC_CONST(-0.8025052547) } },
00316     { { FRAC_CONST(-0.0471066870), FRAC_CONST(0.9988898635) }, { FRAC_CONST(0.9238795042), FRAC_CONST(-0.3826834261) }, { FRAC_CONST(0.4357025325), FRAC_CONST(-0.9000906944) } },
00317     { { FRAC_CONST(-0.9851093888), FRAC_CONST(0.1719288528) }, { FRAC_CONST(-0.3826834261), FRAC_CONST(0.9238795042) }, { FRAC_CONST(0.9995546937), FRAC_CONST(-0.0298405960) } },
00318     { { FRAC_CONST(-0.3826831877), FRAC_CONST(-0.9238796234) }, { FRAC_CONST(-0.3826834261), FRAC_CONST(-0.9238795042) }, { FRAC_CONST(0.4886211455), FRAC_CONST(0.8724960685) } },
00319     { { FRAC_CONST(0.8181498647), FRAC_CONST(-0.5750049949) }, { FRAC_CONST(0.9238795042), FRAC_CONST(0.3826834261) }, { FRAC_CONST(-0.5477093458), FRAC_CONST(0.8366686702) } },
00320     { { FRAC_CONST(0.7396308780), FRAC_CONST(0.6730127335) }, { FRAC_CONST(-0.9238795042), FRAC_CONST(0.3826834261) }, { FRAC_CONST(-0.9951074123), FRAC_CONST(-0.0987988561) } },
00321     { { FRAC_CONST(-0.4954589605), FRAC_CONST(0.8686313629) }, { FRAC_CONST(0.3826834261), FRAC_CONST(-0.9238795042) }, { FRAC_CONST(-0.3725017905), FRAC_CONST(-0.9280315042) } },
00322     { { FRAC_CONST(-0.9557929039), FRAC_CONST(-0.2940406799) }, { FRAC_CONST(0.3826834261), FRAC_CONST(0.9238795042) }, { FRAC_CONST(0.6506417990), FRAC_CONST(-0.7593847513) } },
00323     { { FRAC_CONST(0.0784594864), FRAC_CONST(-0.9969173074) }, { FRAC_CONST(-0.9238795042), FRAC_CONST(-0.3826834261) }, { FRAC_CONST(0.9741733670), FRAC_CONST(0.2258014232) } },
00324     { { FRAC_CONST(0.9900237322), FRAC_CONST(-0.1409008205) }, { FRAC_CONST(0.9238795042), FRAC_CONST(-0.3826834261) }, { FRAC_CONST(0.2502108514), FRAC_CONST(0.9681913853) } },
00325     { { FRAC_CONST(0.3534744382), FRAC_CONST(0.9354441762) }, { FRAC_CONST(-0.3826834261), FRAC_CONST(0.9238795042) }, { FRAC_CONST(-0.7427945137), FRAC_CONST(0.6695194840) } },
00326     { { FRAC_CONST(-0.8358076215), FRAC_CONST(0.5490224361) }, { FRAC_CONST(-0.3826834261), FRAC_CONST(-0.9238795042) }, { FRAC_CONST(-0.9370992780), FRAC_CONST(-0.3490629196) } },
00327     { { FRAC_CONST(-0.7181259394), FRAC_CONST(-0.6959131360) }, { FRAC_CONST(0.9238795042), FRAC_CONST(0.3826834261) }, { FRAC_CONST(-0.1237744763), FRAC_CONST(-0.9923103452) } },
00328     { { FRAC_CONST(0.5224990249), FRAC_CONST(-0.8526399136) }, { FRAC_CONST(-0.9238795042), FRAC_CONST(0.3826834261) }, { FRAC_CONST(0.8226406574), FRAC_CONST(-0.5685616732) } },
00329     { { FRAC_CONST(0.9460852146), FRAC_CONST(0.3239179254) }, { FRAC_CONST(0.3826834261), FRAC_CONST(-0.9238795042) }, { FRAC_CONST(0.8844994903), FRAC_CONST(0.4665412009) } },
00330     { { FRAC_CONST(-0.1097348556), FRAC_CONST(0.9939609170) }, { FRAC_CONST(0.3826834261), FRAC_CONST(0.9238795042) }, { FRAC_CONST(-0.0047125919), FRAC_CONST(0.9999889135) } },
00331     { { FRAC_CONST(-0.9939610362), FRAC_CONST(0.1097337380) }, { FRAC_CONST(-0.9238795042), FRAC_CONST(-0.3826834261) }, { FRAC_CONST(-0.8888573647), FRAC_CONST(0.4581840038) } },
00332     { { FRAC_CONST(-0.3239168525), FRAC_CONST(-0.9460855722) }, { FRAC_CONST(0.9238795042), FRAC_CONST(-0.3826834261) }, { FRAC_CONST(-0.8172453642), FRAC_CONST(-0.5762898922) } },
00333     { { FRAC_CONST(0.8526405096), FRAC_CONST(-0.5224980116) }, { FRAC_CONST(-0.3826834261), FRAC_CONST(0.9238795042) }, { FRAC_CONST(0.1331215799), FRAC_CONST(-0.9910997152) } },
00334     { { FRAC_CONST(0.6959123611), FRAC_CONST(0.7181267142) }, { FRAC_CONST(-0.3826834261), FRAC_CONST(-0.9238795042) }, { FRAC_CONST(0.9403476119), FRAC_CONST(-0.3402152061) } },
00335     { { FRAC_CONST(-0.5490233898), FRAC_CONST(0.8358070254) }, { FRAC_CONST(0.9238795042), FRAC_CONST(0.3826834261) }, { FRAC_CONST(0.7364512086), FRAC_CONST(0.6764906645) } },
00336     { { FRAC_CONST(-0.9354437590), FRAC_CONST(-0.3534754813) }, { FRAC_CONST(-0.9238795042), FRAC_CONST(0.3826834261) }, { FRAC_CONST(-0.2593250275), FRAC_CONST(0.9657900929) } },
00337     { { FRAC_CONST(0.1409019381), FRAC_CONST(-0.9900235534) }, { FRAC_CONST(0.3826834261), FRAC_CONST(-0.9238795042) }, { FRAC_CONST(-0.9762582779), FRAC_CONST(0.2166097313) } },
00338     { { FRAC_CONST(0.9969173670), FRAC_CONST(-0.0784583688) }, { FRAC_CONST(0.3826834261), FRAC_CONST(0.9238795042) }, { FRAC_CONST(-0.6434556246), FRAC_CONST(-0.7654833794) } },
00339     { { FRAC_CONST(0.2940396070), FRAC_CONST(0.9557932615) }, { FRAC_CONST(-0.9238795042), FRAC_CONST(-0.3826834261) }, { FRAC_CONST(0.3812320232), FRAC_CONST(-0.9244794250) } },
00340     { { FRAC_CONST(-0.8686318994), FRAC_CONST(0.4954580069) }, { FRAC_CONST(0.9238795042), FRAC_CONST(-0.3826834261) }, { FRAC_CONST(0.9959943891), FRAC_CONST(-0.0894154981) } },
00341     { { FRAC_CONST(-0.6730118990), FRAC_CONST(-0.7396316528) }, { FRAC_CONST(-0.3826834261), FRAC_CONST(0.9238795042) }, { FRAC_CONST(0.5397993922), FRAC_CONST(0.8417937160) } },
00342     { { FRAC_CONST(0.5750059485), FRAC_CONST(-0.8181492686) }, { FRAC_CONST(-0.3826834261), FRAC_CONST(-0.9238795042) }, { FRAC_CONST(-0.4968227744), FRAC_CONST(0.8678520322) } },
00343     { { FRAC_CONST(0.9238792062), FRAC_CONST(0.3826842010) }, { FRAC_CONST(0.9238795042), FRAC_CONST(0.3826834261) }, { FRAC_CONST(-0.9992290139), FRAC_CONST(-0.0392601527) } },
00344     { { FRAC_CONST(-0.1719299555), FRAC_CONST(0.9851091504) }, { FRAC_CONST(-0.9238795042), FRAC_CONST(0.3826834261) }, { FRAC_CONST(-0.4271997511), FRAC_CONST(-0.9041572809) } },
00345     { { FRAC_CONST(-0.9988899231), FRAC_CONST(0.0471055657) }, { FRAC_CONST(0.3826834261), FRAC_CONST(-0.9238795042) }, { FRAC_CONST(0.6041822433), FRAC_CONST(-0.7968461514) } },
00346     { { FRAC_CONST(-0.2638721764), FRAC_CONST(-0.9645576477) }, { FRAC_CONST(0.3826834261), FRAC_CONST(0.9238795042) }, { FRAC_CONST(0.9859085083), FRAC_CONST(0.1672853529) } },
00347     { { FRAC_CONST(0.8837660551), FRAC_CONST(-0.4679289758) }, { FRAC_CONST(-0.9238795042), FRAC_CONST(-0.3826834261) }, { FRAC_CONST(0.3075223565), FRAC_CONST(0.9515408874) } },
00348     { { FRAC_CONST(0.6494473219), FRAC_CONST(0.7604066133) }, { FRAC_CONST(0.9238795042), FRAC_CONST(-0.3826834261) }, { FRAC_CONST(-0.7015317082), FRAC_CONST(0.7126382589) } },
00349     { { FRAC_CONST(-0.6004210114), FRAC_CONST(0.7996840477) }, { FRAC_CONST(-0.3826834261), FRAC_CONST(0.9238795042) }, { FRAC_CONST(-0.9562535882), FRAC_CONST(-0.2925389707) } },
00350     { { FRAC_CONST(-0.9114028811), FRAC_CONST(-0.4115152657) }, { FRAC_CONST(-0.3826834261), FRAC_CONST(-0.9238795042) }, { FRAC_CONST(-0.1827499419), FRAC_CONST(-0.9831594229) } },
00351     { { FRAC_CONST(0.2027882934), FRAC_CONST(-0.9792225957) }, { FRAC_CONST(0.9238795042), FRAC_CONST(0.3826834261) }, { FRAC_CONST(0.7872582674), FRAC_CONST(-0.6166234016) } },
00352     { { FRAC_CONST(0.9998766780), FRAC_CONST(-0.0157062728) }, { FRAC_CONST(-0.9238795042), FRAC_CONST(0.3826834261) }, { FRAC_CONST(0.9107555747), FRAC_CONST(0.4129458666) } },
00353     { { FRAC_CONST(0.2334443331), FRAC_CONST(0.9723701477) }, { FRAC_CONST(0.3826834261), FRAC_CONST(-0.9238795042) }, { FRAC_CONST(0.0549497530), FRAC_CONST(0.9984891415) } },
00354     { { FRAC_CONST(-0.8980280757), FRAC_CONST(0.4399381876) }, { FRAC_CONST(0.3826834261), FRAC_CONST(0.9238795042) }, { FRAC_CONST(-0.8599416018), FRAC_CONST(0.5103924870) } },
00355     { { FRAC_CONST(-0.6252418160), FRAC_CONST(-0.7804310918) }, { FRAC_CONST(-0.9238795042), FRAC_CONST(-0.3826834261) }, { FRAC_CONST(-0.8501682281), FRAC_CONST(-0.5265110731) } },
00356     { { FRAC_CONST(0.6252435446), FRAC_CONST(-0.7804297209) }, { FRAC_CONST(0.9238795042), FRAC_CONST(-0.3826834261) }, { FRAC_CONST(0.0737608299), FRAC_CONST(-0.9972759485) } },
00357     { { FRAC_CONST(0.8980270624), FRAC_CONST(0.4399402142) }, { FRAC_CONST(-0.3826834261), FRAC_CONST(0.9238795042) }, { FRAC_CONST(0.9183775187), FRAC_CONST(-0.3957053721) } },
00358     { { FRAC_CONST(-0.2334465086), FRAC_CONST(0.9723696709) }, { FRAC_CONST(-0.3826834261), FRAC_CONST(-0.9238795042) }, { FRAC_CONST(0.7754954696), FRAC_CONST(0.6313531399) } },
00359     { { FRAC_CONST(-0.9998766184), FRAC_CONST(-0.0157085191) }, { FRAC_CONST(0.9238795042), FRAC_CONST(0.3826834261) }, { FRAC_CONST(-0.2012493610), FRAC_CONST(0.9795400500) } },
00360     { { FRAC_CONST(-0.2027861029), FRAC_CONST(-0.9792230725) }, { FRAC_CONST(-0.9238795042), FRAC_CONST(0.3826834261) }, { FRAC_CONST(-0.9615978599), FRAC_CONST(0.2744622827) } },
00361     { { FRAC_CONST(0.9114037752), FRAC_CONST(-0.4115132093) }, { FRAC_CONST(0.3826834261), FRAC_CONST(-0.9238795042) }, { FRAC_CONST(-0.6879743338), FRAC_CONST(-0.7257350087) } },
00362     { { FRAC_CONST(0.6004192233), FRAC_CONST(0.7996854186) }, { FRAC_CONST(0.3826834261), FRAC_CONST(0.9238795042) }, { FRAC_CONST(0.3254036009), FRAC_CONST(-0.9455752373) } },
00363     { { FRAC_CONST(-0.6494490504), FRAC_CONST(0.7604051232) }, { FRAC_CONST(-0.9238795042), FRAC_CONST(-0.3826834261) }, { FRAC_CONST(0.9888865948), FRAC_CONST(-0.1486719251) } },
00364     { { FRAC_CONST(-0.8837650418), FRAC_CONST(-0.4679309726) }, { FRAC_CONST(0.9238795042), FRAC_CONST(-0.3826834261) }, { FRAC_CONST(0.5890548825), FRAC_CONST(0.8080930114) } },
00365     { { FRAC_CONST(0.2638743520), FRAC_CONST(-0.9645570517) }, { FRAC_CONST(-0.3826834261), FRAC_CONST(0.9238795042) }, { FRAC_CONST(-0.4441666007), FRAC_CONST(0.8959442377) } },
00366     { { FRAC_CONST(0.9988898039), FRAC_CONST(0.0471078083) }, { FRAC_CONST(-0.3826834261), FRAC_CONST(-0.9238795042) }, { FRAC_CONST(-0.9997915030), FRAC_CONST(0.0204183888) } },
00367     { { FRAC_CONST(0.1719277352), FRAC_CONST(0.9851095676) }, { FRAC_CONST(0.9238795042), FRAC_CONST(0.3826834261) }, { FRAC_CONST(-0.4803760946), FRAC_CONST(-0.8770626187) } },
00368     { { FRAC_CONST(-0.9238800406), FRAC_CONST(0.3826821446) }, { FRAC_CONST(-0.9238795042), FRAC_CONST(0.3826834261) }, { FRAC_CONST(0.5555707216), FRAC_CONST(-0.8314692974) } },
00369     { { FRAC_CONST(-0.5750041008), FRAC_CONST(-0.8181505203) }, { FRAC_CONST(0.3826834261), FRAC_CONST(-0.9238795042) }, { FRAC_CONST(0.9941320419), FRAC_CONST(0.1081734300) } }
00370 };
00371 
00372 static const complex_t Phi_Fract_Qmf[] = {
00373     { FRAC_CONST(0.8181497455), FRAC_CONST(0.5750052333) },
00374     { FRAC_CONST(-0.2638730407), FRAC_CONST(0.9645574093) },
00375     { FRAC_CONST(-0.9969173074), FRAC_CONST(0.0784590989) },
00376     { FRAC_CONST(-0.4115143716), FRAC_CONST(-0.9114032984) },
00377     { FRAC_CONST(0.7181262970), FRAC_CONST(-0.6959127784) },
00378     { FRAC_CONST(0.8980275989), FRAC_CONST(0.4399391711) },
00379     { FRAC_CONST(-0.1097343117), FRAC_CONST(0.9939609766) },
00380     { FRAC_CONST(-0.9723699093), FRAC_CONST(0.2334453613) },
00381     { FRAC_CONST(-0.5490227938), FRAC_CONST(-0.8358073831) },
00382     { FRAC_CONST(0.6004202366), FRAC_CONST(-0.7996846437) },
00383     { FRAC_CONST(0.9557930231), FRAC_CONST(0.2940403223) },
00384     { FRAC_CONST(0.0471064523), FRAC_CONST(0.9988898635) },
00385     { FRAC_CONST(-0.9238795042), FRAC_CONST(0.3826834261) },
00386     { FRAC_CONST(-0.6730124950), FRAC_CONST(-0.7396311164) },
00387     { FRAC_CONST(0.4679298103), FRAC_CONST(-0.8837656379) },
00388     { FRAC_CONST(0.9900236726), FRAC_CONST(0.1409012377) },
00389     { FRAC_CONST(0.2027872950), FRAC_CONST(0.9792228341) },
00390     { FRAC_CONST(-0.8526401520), FRAC_CONST(0.5224985480) },
00391     { FRAC_CONST(-0.7804304361), FRAC_CONST(-0.6252426505) },
00392     { FRAC_CONST(0.3239174187), FRAC_CONST(-0.9460853338) },
00393     { FRAC_CONST(0.9998766184), FRAC_CONST(-0.0157073177) },
00394     { FRAC_CONST(0.3534748554), FRAC_CONST(0.9354440570) },
00395     { FRAC_CONST(-0.7604059577), FRAC_CONST(0.6494480371) },
00396     { FRAC_CONST(-0.8686315417), FRAC_CONST(-0.4954586625) },
00397     { FRAC_CONST(0.1719291061), FRAC_CONST(-0.9851093292) },
00398     { FRAC_CONST(0.9851093292), FRAC_CONST(-0.1719291061) },
00399     { FRAC_CONST(0.4954586625), FRAC_CONST(0.8686315417) },
00400     { FRAC_CONST(-0.6494480371), FRAC_CONST(0.7604059577) },
00401     { FRAC_CONST(-0.9354440570), FRAC_CONST(-0.3534748554) },
00402     { FRAC_CONST(0.0157073177), FRAC_CONST(-0.9998766184) },
00403     { FRAC_CONST(0.9460853338), FRAC_CONST(-0.3239174187) },
00404     { FRAC_CONST(0.6252426505), FRAC_CONST(0.7804304361) },
00405     { FRAC_CONST(-0.5224985480), FRAC_CONST(0.8526401520) },
00406     { FRAC_CONST(-0.9792228341), FRAC_CONST(-0.2027872950) },
00407     { FRAC_CONST(-0.1409012377), FRAC_CONST(-0.9900236726) },
00408     { FRAC_CONST(0.8837656379), FRAC_CONST(-0.4679298103) },
00409     { FRAC_CONST(0.7396311164), FRAC_CONST(0.6730124950) },
00410     { FRAC_CONST(-0.3826834261), FRAC_CONST(0.9238795042) },
00411     { FRAC_CONST(-0.9988898635), FRAC_CONST(-0.0471064523) },
00412     { FRAC_CONST(-0.2940403223), FRAC_CONST(-0.9557930231) },
00413     { FRAC_CONST(0.7996846437), FRAC_CONST(-0.6004202366) },
00414     { FRAC_CONST(0.8358073831), FRAC_CONST(0.5490227938) },
00415     { FRAC_CONST(-0.2334453613), FRAC_CONST(0.9723699093) },
00416     { FRAC_CONST(-0.9939609766), FRAC_CONST(0.1097343117) },
00417     { FRAC_CONST(-0.4399391711), FRAC_CONST(-0.8980275989) },
00418     { FRAC_CONST(0.6959127784), FRAC_CONST(-0.7181262970) },
00419     { FRAC_CONST(0.9114032984), FRAC_CONST(0.4115143716) },
00420     { FRAC_CONST(-0.0784590989), FRAC_CONST(0.9969173074) },
00421     { FRAC_CONST(-0.9645574093), FRAC_CONST(0.2638730407) },
00422     { FRAC_CONST(-0.5750052333), FRAC_CONST(-0.8181497455) },
00423     { FRAC_CONST(0.5750052333), FRAC_CONST(-0.8181497455) },
00424     { FRAC_CONST(0.9645574093), FRAC_CONST(0.2638730407) },
00425     { FRAC_CONST(0.0784590989), FRAC_CONST(0.9969173074) },
00426     { FRAC_CONST(-0.9114032984), FRAC_CONST(0.4115143716) },
00427     { FRAC_CONST(-0.6959127784), FRAC_CONST(-0.7181262970) },
00428     { FRAC_CONST(0.4399391711), FRAC_CONST(-0.8980275989) },
00429     { FRAC_CONST(0.9939609766), FRAC_CONST(0.1097343117) },
00430     { FRAC_CONST(0.2334453613), FRAC_CONST(0.9723699093) },
00431     { FRAC_CONST(-0.8358073831), FRAC_CONST(0.5490227938) },
00432     { FRAC_CONST(-0.7996846437), FRAC_CONST(-0.6004202366) },
00433     { FRAC_CONST(0.2940403223), FRAC_CONST(-0.9557930231) },
00434     { FRAC_CONST(0.9988898635), FRAC_CONST(-0.0471064523) },
00435     { FRAC_CONST(0.3826834261), FRAC_CONST(0.9238795042) },
00436     { FRAC_CONST(-0.7396311164), FRAC_CONST(0.6730124950) }
00437 };
00438 
00439 
00440 /* static function declarations */
00441 static void drm_ps_sa_element(drm_ps_info *ps, bitfile *ld);
00442 static void drm_ps_pan_element(drm_ps_info *ps, bitfile *ld);
00443 static int8_t huff_dec(bitfile *ld, drm_ps_huff_tab huff);
00444 
00445 
00446 uint16_t drm_ps_data(drm_ps_info *ps, bitfile *ld)
00447 {
00448     uint16_t bits = (uint16_t)faad_get_processed_bits(ld);
00449 
00450     ps->drm_ps_data_available = 1;
00451 
00452     ps->bs_enable_sa = faad_get1bit(ld);
00453     ps->bs_enable_pan = faad_get1bit(ld);
00454 
00455     if (ps->bs_enable_sa)
00456     {
00457         drm_ps_sa_element(ps, ld);
00458     }
00459 
00460     if (ps->bs_enable_pan)
00461     {
00462         drm_ps_pan_element(ps, ld);
00463     }
00464 
00465     bits = (uint16_t)faad_get_processed_bits(ld) - bits;
00466 
00467     return bits;
00468 }
00469 
00470 static void drm_ps_sa_element(drm_ps_info *ps, bitfile *ld)
00471 {
00472     drm_ps_huff_tab huff;
00473     uint8_t band;
00474 
00475     ps->bs_sa_dt_flag = faad_get1bit(ld);
00476     if (ps->bs_sa_dt_flag)
00477     {
00478         huff = t_huffman_sa;
00479     } else {
00480         huff = f_huffman_sa;
00481     }
00482 
00483     for (band = 0; band < DRM_NUM_SA_BANDS; band++)
00484     {
00485         ps->bs_sa_data[band] = huff_dec(ld, huff);
00486     }
00487 }
00488 
00489 static void drm_ps_pan_element(drm_ps_info *ps, bitfile *ld)
00490 {
00491     drm_ps_huff_tab huff;
00492     uint8_t band;
00493 
00494     ps->bs_pan_dt_flag = faad_get1bit(ld);
00495     if (ps->bs_pan_dt_flag)
00496     {
00497         huff = t_huffman_pan;
00498     } else {
00499         huff = f_huffman_pan;
00500     }
00501 
00502     for (band = 0; band < DRM_NUM_PAN_BANDS; band++)
00503     {
00504         ps->bs_pan_data[band] = huff_dec(ld, huff);
00505     }
00506 }
00507 
00508 /* binary search huffman decoding */
00509 static int8_t huff_dec(bitfile *ld, drm_ps_huff_tab huff)
00510 {
00511     uint8_t bit;
00512     int16_t index = 0;
00513 
00514     while (index >= 0)
00515     {
00516         bit = (uint8_t)faad_get1bit(ld);
00517         index = huff[index][bit];
00518     }
00519 
00520     return index + 15;
00521 }
00522 
00523 
00524 static int8_t sa_delta_clip(drm_ps_info *ps, int8_t i)
00525 {
00526     if (i < 0) {
00527       /*  printf(" SAminclip %d", i); */
00528         ps->sa_decode_error = 1;
00529         return 0;
00530     } else if (i > 7) {
00531      /*   printf(" SAmaxclip %d", i); */
00532         ps->sa_decode_error = 1;
00533         return 7;
00534     } else
00535         return i;
00536 }
00537 
00538 static int8_t pan_delta_clip(drm_ps_info *ps, int8_t i)
00539 {   
00540     if (i < -7) {
00541         /* printf(" PANminclip %d", i); */
00542         ps->pan_decode_error = 1;
00543         return -7;
00544     } else if (i > 7) {
00545        /* printf(" PANmaxclip %d", i);  */
00546         ps->pan_decode_error = 1;
00547         return 7;
00548     } else
00549         return i;
00550 }
00551 
00552 static void drm_ps_delta_decode(drm_ps_info *ps) 
00553 {
00554     uint8_t band;    
00555 
00556     if (ps->bs_enable_sa) 
00557     {    
00558         if (ps->bs_sa_dt_flag && !ps->g_last_had_sa) 
00559         {        
00560             /* wait until we get a DT frame */
00561             ps->bs_enable_sa = 0;
00562         } else if (ps->bs_sa_dt_flag) {
00563             /* DT frame, we have a last frame, so we can decode */
00564             ps->g_sa_index[0] = sa_delta_clip(ps, ps->g_prev_sa_index[0]+ps->bs_sa_data[0]);            
00565         } else {
00566             /* DF always decodable */
00567             ps->g_sa_index[0] = sa_delta_clip(ps,ps->bs_sa_data[0]);          
00568         }
00569         
00570         for (band = 1; band < DRM_NUM_SA_BANDS; band++)
00571         {   
00572             if (ps->bs_sa_dt_flag && ps->g_last_had_sa)
00573             {
00574                 ps->g_sa_index[band] = sa_delta_clip(ps, ps->g_prev_sa_index[band] + ps->bs_sa_data[band]);
00575             } else if (!ps->bs_sa_dt_flag) {
00576                 ps->g_sa_index[band] = sa_delta_clip(ps, ps->g_sa_index[band-1] + ps->bs_sa_data[band]);                
00577             }
00578         }
00579     }
00580 
00581     /* An error during SA decoding implies PAN data will be undecodable, too */
00582     /* Also, we don't like on/off switching in PS, so we force to last settings */
00583     if (ps->sa_decode_error) {
00584         ps->pan_decode_error = 1;
00585         ps->bs_enable_pan = ps->g_last_had_pan;
00586         ps->bs_enable_sa = ps->g_last_had_sa;
00587     }
00588     
00589        
00590     if (ps->bs_enable_sa) 
00591     {    
00592         if (ps->sa_decode_error) {
00593             for (band = 0; band < DRM_NUM_SA_BANDS; band++)
00594             {   
00595                 ps->g_sa_index[band] = ps->g_last_good_sa_index[band];
00596             }
00597         } else {
00598             for (band = 0; band < DRM_NUM_SA_BANDS; band++)
00599             {   
00600                 ps->g_last_good_sa_index[band] = ps->g_sa_index[band];
00601             }
00602         }
00603     }
00604     
00605     if (ps->bs_enable_pan) 
00606     {
00607         if (ps->bs_pan_dt_flag && !ps->g_last_had_pan) 
00608         {
00609             ps->bs_enable_pan = 0;
00610         }  else if (ps->bs_pan_dt_flag) {   
00611             ps->g_pan_index[0] = pan_delta_clip(ps,  ps->g_prev_pan_index[0]+ps->bs_pan_data[0]);
00612         } else {
00613             ps->g_pan_index[0] = pan_delta_clip(ps, ps->bs_pan_data[0]);
00614         }
00615     
00616         for (band = 1; band < DRM_NUM_PAN_BANDS; band++)
00617         {   
00618             if (ps->bs_pan_dt_flag && ps->g_last_had_pan)
00619             {
00620                 ps->g_pan_index[band] = pan_delta_clip(ps, ps->g_prev_pan_index[band] + ps->bs_pan_data[band]);
00621             } else if (!ps->bs_pan_dt_flag) {
00622                 ps->g_pan_index[band] = pan_delta_clip(ps, ps->g_pan_index[band-1] + ps->bs_pan_data[band]);
00623             }
00624         }
00625  
00626         if (ps->pan_decode_error) {
00627             for (band = 0; band < DRM_NUM_PAN_BANDS; band++)
00628             {   
00629                 ps->g_pan_index[band] = ps->g_last_good_pan_index[band];
00630             }
00631         } else {
00632             for (band = 0; band < DRM_NUM_PAN_BANDS; band++)
00633             {   
00634                 ps->g_last_good_pan_index[band] = ps->g_pan_index[band];
00635             }
00636         }
00637     }
00638 }
00639 
00640 static void drm_calc_sa_side_signal(drm_ps_info *ps, qmf_t X[38][64]) 
00641 {      
00642     uint8_t s, b, k;
00643     complex_t qfrac, tmp0, tmp, in, R0;
00644     real_t peakdiff;
00645     real_t nrg;
00646     real_t power;
00647     real_t transratio;
00648     real_t new_delay_slopes[NUM_OF_LINKS];
00649     uint8_t temp_delay_ser[NUM_OF_LINKS];
00650     complex_t Phi_Fract;
00651 #ifdef FIXED_POINT
00652     uint32_t in_re, in_im;
00653 #endif
00654 
00655     for (b = 0; b < sa_freq_scale[DRM_NUM_SA_BANDS]; b++)
00656     {
00657         /* set delay indices */    
00658         for (k = 0; k < NUM_OF_LINKS; k++)
00659             temp_delay_ser[k] = ps->delay_buf_index_ser[k];
00660 
00661         RE(Phi_Fract) = RE(Phi_Fract_Qmf[b]);
00662         IM(Phi_Fract) = IM(Phi_Fract_Qmf[b]);
00663 
00664         for (s = 0; s < NUM_OF_SUBSAMPLES; s++)
00665         {            
00666             const real_t gamma = REAL_CONST(1.5);
00667             const real_t sigma = REAL_CONST(1.5625);
00668 
00669             RE(in) = QMF_RE(X[s][b]);
00670             IM(in) = QMF_IM(X[s][b]);
00671 
00672 #ifdef FIXED_POINT
00673             /* NOTE: all input is scaled by 2^(-5) because of fixed point QMF
00674             * meaning that P will be scaled by 2^(-10) compared to floating point version
00675             */
00676             in_re = ((abs(RE(in))+(1<<(REAL_BITS-1)))>>REAL_BITS);
00677             in_im = ((abs(IM(in))+(1<<(REAL_BITS-1)))>>REAL_BITS);
00678             power = in_re*in_re + in_im*in_im;
00679 #else
00680             power = MUL_R(RE(in),RE(in)) + MUL_R(IM(in),IM(in));
00681 #endif
00682 
00683             ps->peakdecay_fast[b] = MUL_F(ps->peakdecay_fast[b], peak_decay);
00684             if (ps->peakdecay_fast[b] < power)
00685                 ps->peakdecay_fast[b] = power;
00686 
00687             peakdiff = ps->prev_peakdiff[b];
00688             peakdiff += MUL_F((ps->peakdecay_fast[b] - power - ps->prev_peakdiff[b]), smooth_coeff);
00689             ps->prev_peakdiff[b] = peakdiff;
00690 
00691             nrg = ps->prev_nrg[b];
00692             nrg += MUL_F((power - ps->prev_nrg[b]), smooth_coeff);
00693             ps->prev_nrg[b] = nrg;
00694 
00695             if (MUL_R(peakdiff, gamma) <= nrg) {
00696                 transratio = sigma;
00697             } else {
00698                 transratio = MUL_R(DIV_R(nrg, MUL_R(peakdiff, gamma)), sigma);
00699             }
00700             
00701             for (k = 0; k < NUM_OF_LINKS; k++) 
00702             {
00703                 new_delay_slopes[k] = MUL_F(g_decayslope[b], filter_coeff[k]);
00704             }
00705 
00706             RE(tmp0) = RE(ps->d_buff[0][b]);
00707             IM(tmp0) = IM(ps->d_buff[0][b]);
00708 
00709             RE(ps->d_buff[0][b]) = RE(ps->d_buff[1][b]);
00710             IM(ps->d_buff[0][b]) = IM(ps->d_buff[1][b]);
00711 
00712             RE(ps->d_buff[1][b]) = RE(in);
00713             IM(ps->d_buff[1][b]) = IM(in);               
00714 
00715             ComplexMult(&RE(tmp), &IM(tmp), RE(tmp0), IM(tmp0), RE(Phi_Fract), IM(Phi_Fract));
00716 
00717             RE(R0) = RE(tmp);
00718             IM(R0) = IM(tmp);
00719 
00720             for (k = 0; k < NUM_OF_LINKS; k++) 
00721             {
00722                 RE(qfrac) = RE(Q_Fract_allpass_Qmf[b][k]);
00723                 IM(qfrac) = IM(Q_Fract_allpass_Qmf[b][k]);
00724 
00725                 RE(tmp0) = RE(ps->d2_buff[k][temp_delay_ser[k]][b]);
00726                 IM(tmp0) = IM(ps->d2_buff[k][temp_delay_ser[k]][b]);
00727 
00728                 ComplexMult(&RE(tmp), &IM(tmp), RE(tmp0), IM(tmp0), RE(qfrac), IM(qfrac));
00729 
00730                 RE(tmp) += -MUL_F(new_delay_slopes[k], RE(R0));
00731                 IM(tmp) += -MUL_F(new_delay_slopes[k], IM(R0));
00732 
00733                 RE(ps->d2_buff[k][temp_delay_ser[k]][b]) = RE(R0) + MUL_F(new_delay_slopes[k], RE(tmp));
00734                 IM(ps->d2_buff[k][temp_delay_ser[k]][b]) = IM(R0) + MUL_F(new_delay_slopes[k], IM(tmp));
00735 
00736                 RE(R0) = RE(tmp);
00737                 IM(R0) = IM(tmp);
00738             }
00739 
00740             QMF_RE(ps->SA[s][b]) = MUL_R(RE(R0), transratio);
00741             QMF_IM(ps->SA[s][b]) = MUL_R(IM(R0), transratio);
00742 
00743             for (k = 0; k < NUM_OF_LINKS; k++)
00744             {
00745                 if (++temp_delay_ser[k] >= delay_length[k])
00746                     temp_delay_ser[k] = 0;
00747             }
00748         }       
00749     }
00750 
00751     for (k = 0; k < NUM_OF_LINKS; k++)
00752         ps->delay_buf_index_ser[k] = temp_delay_ser[k];
00753 }
00754 
00755 static void drm_add_ambiance(drm_ps_info *ps, qmf_t X_left[38][64], qmf_t X_right[38][64]) 
00756 {
00757     uint8_t s, b, ifreq, qclass;    
00758     real_t sa_map[MAX_SA_BAND], sa_dir_map[MAX_SA_BAND], k_sa_map[MAX_SA_BAND], k_sa_dir_map[MAX_SA_BAND];
00759     real_t new_dir_map, new_sa_map;
00760     
00761     if (ps->bs_enable_sa)
00762     {
00763         /* Instead of dequantization and mapping, we use an inverse mapping
00764            to look up all the values we need */
00765         for (b = 0; b < sa_freq_scale[DRM_NUM_SA_BANDS]; b++)
00766         {
00767             const real_t inv_f_num_of_subsamples = FRAC_CONST(0.03333333333);
00768 
00769             ifreq = sa_inv_freq[b];
00770             qclass = (b != 0);
00771 
00772             sa_map[b]  = sa_quant[ps->g_prev_sa_index[ifreq]][qclass];
00773             new_sa_map = sa_quant[ps->g_sa_index[ifreq]][qclass];
00774 
00775             k_sa_map[b] = MUL_F(inv_f_num_of_subsamples, (new_sa_map - sa_map[b]));    
00776             
00777             sa_dir_map[b] = sa_sqrt_1_minus[ps->g_prev_sa_index[ifreq]][qclass];                        
00778             new_dir_map   = sa_sqrt_1_minus[ps->g_sa_index[ifreq]][qclass];
00779                                                    
00780             k_sa_dir_map[b] = MUL_F(inv_f_num_of_subsamples, (new_dir_map - sa_dir_map[b]));
00781 
00782         }
00783 
00784         for (s = 0; s < NUM_OF_SUBSAMPLES; s++)
00785         {
00786             for (b = 0; b < sa_freq_scale[DRM_NUM_SA_BANDS]; b++)
00787             {                
00788                 QMF_RE(X_right[s][b]) = MUL_F(QMF_RE(X_left[s][b]), sa_dir_map[b]) - MUL_F(QMF_RE(ps->SA[s][b]), sa_map[b]);
00789                 QMF_IM(X_right[s][b]) = MUL_F(QMF_IM(X_left[s][b]), sa_dir_map[b]) - MUL_F(QMF_IM(ps->SA[s][b]), sa_map[b]);
00790                 QMF_RE(X_left[s][b]) = MUL_F(QMF_RE(X_left[s][b]), sa_dir_map[b]) + MUL_F(QMF_RE(ps->SA[s][b]), sa_map[b]);
00791                 QMF_IM(X_left[s][b]) = MUL_F(QMF_IM(X_left[s][b]), sa_dir_map[b]) + MUL_F(QMF_IM(ps->SA[s][b]), sa_map[b]);
00792       
00793                 sa_map[b]     += k_sa_map[b];
00794                 sa_dir_map[b] += k_sa_dir_map[b];
00795             }
00796             for (b = sa_freq_scale[DRM_NUM_SA_BANDS]; b < NUM_OF_QMF_CHANNELS; b++)
00797             {                
00798                 QMF_RE(X_right[s][b]) = QMF_RE(X_left[s][b]);
00799                 QMF_IM(X_right[s][b]) = QMF_IM(X_left[s][b]);
00800             }
00801         }
00802     } 
00803     else {
00804         for (s = 0; s < NUM_OF_SUBSAMPLES; s++)
00805         {
00806             for (b = 0; b < NUM_OF_QMF_CHANNELS; b++)
00807             {
00808                 QMF_RE(X_right[s][b]) = QMF_RE(X_left[s][b]);
00809                 QMF_IM(X_right[s][b]) = QMF_IM(X_left[s][b]);                
00810             }
00811         }
00812     }
00813 }
00814 
00815 static void drm_add_pan(drm_ps_info *ps, qmf_t X_left[38][64], qmf_t X_right[38][64]) 
00816 {
00817     uint8_t s, b, qclass, ifreq;
00818     real_t tmp, coeff1, coeff2;
00819     real_t pan_base[MAX_PAN_BAND];
00820     real_t pan_delta[MAX_PAN_BAND];
00821     qmf_t temp_l, temp_r;
00822 
00823     if (ps->bs_enable_pan)
00824     {
00825         for (b = 0; b < NUM_OF_QMF_CHANNELS; b++) 
00826         {
00827             /* Instead of dequantization, 20->64 mapping and 2^G(x,y) we do an
00828                inverse mapping 64->20 and look up the 2^G(x,y) values directly */
00829             ifreq = pan_inv_freq[b];
00830             qclass = pan_quant_class[ifreq];
00831 
00832             if (ps->g_prev_pan_index[ifreq] >= 0)
00833             {
00834                 pan_base[b] = pan_pow_2_pos[ps->g_prev_pan_index[ifreq]][qclass]; 
00835             } else {
00836                 pan_base[b] = pan_pow_2_neg[-ps->g_prev_pan_index[ifreq]][qclass];
00837             }
00838 
00839             /* 2^((a-b)/30) = 2^(a/30) * 1/(2^(b/30)) */
00840             /* a en b can be negative so we may need to inverse parts */
00841             if (ps->g_pan_index[ifreq] >= 0)
00842             {
00843                 if (ps->g_prev_pan_index[ifreq] >= 0) 
00844                 {
00845                     pan_delta[b] = MUL_C(pan_pow_2_30_pos[ps->g_pan_index[ifreq]][qclass],
00846                                          pan_pow_2_30_neg[ps->g_prev_pan_index[ifreq]][qclass]);
00847                 } else {
00848                     pan_delta[b] = MUL_C(pan_pow_2_30_pos[ps->g_pan_index[ifreq]][qclass],
00849                                          pan_pow_2_30_pos[-ps->g_prev_pan_index[ifreq]][qclass]);
00850                 }
00851             } else {
00852                 if (ps->g_prev_pan_index[ifreq] >= 0) 
00853                 {
00854                     pan_delta[b] = MUL_C(pan_pow_2_30_neg[-ps->g_pan_index[ifreq]][qclass],
00855                                          pan_pow_2_30_neg[ps->g_prev_pan_index[ifreq]][qclass]);
00856                 } else {
00857                     pan_delta[b] = MUL_C(pan_pow_2_30_neg[-ps->g_pan_index[ifreq]][qclass],
00858                                          pan_pow_2_30_pos[-ps->g_prev_pan_index[ifreq]][qclass]);
00859                 }
00860             }
00861         }
00862 
00863         for (s = 0; s < NUM_OF_SUBSAMPLES; s++)
00864         {
00865             /* PAN always uses all 64 channels */
00866             for (b = 0; b < NUM_OF_QMF_CHANNELS; b++)
00867             {
00868                 tmp = pan_base[b];
00869 
00870                 coeff2 = DIV_R(REAL_CONST(2.0), (REAL_CONST(1.0) + tmp));
00871                 coeff1 = MUL_R(coeff2, tmp);                
00872 
00873                 QMF_RE(temp_l) = QMF_RE(X_left[s][b]);
00874                 QMF_IM(temp_l) = QMF_IM(X_left[s][b]);
00875                 QMF_RE(temp_r) = QMF_RE(X_right[s][b]);
00876                 QMF_IM(temp_r) = QMF_IM(X_right[s][b]);
00877 
00878                 QMF_RE(X_left[s][b]) = MUL_R(QMF_RE(temp_l), coeff1);
00879                 QMF_IM(X_left[s][b]) = MUL_R(QMF_IM(temp_l), coeff1);
00880                 QMF_RE(X_right[s][b]) = MUL_R(QMF_RE(temp_r), coeff2);
00881                 QMF_IM(X_right[s][b]) = MUL_R(QMF_IM(temp_r), coeff2);
00882                 
00883                 /* 2^(a+k*b) = 2^a * 2^b * ... * 2^b */
00884                 /*                   ^^^^^^^^^^^^^^^ k times */
00885                 pan_base[b] = MUL_C(pan_base[b], pan_delta[b]);
00886             }           
00887         }       
00888     }     
00889 }
00890 
00891 drm_ps_info *drm_ps_init(void)
00892 {
00893     drm_ps_info *ps = (drm_ps_info*)faad_malloc(sizeof(drm_ps_info));
00894 
00895     memset(ps, 0, sizeof(drm_ps_info));     
00896 
00897     return ps;
00898 }
00899 
00900 void drm_ps_free(drm_ps_info *ps)
00901 {
00902     faad_free(ps);
00903 }
00904 
00905 /* main DRM PS decoding function */
00906 uint8_t drm_ps_decode(drm_ps_info *ps, uint8_t guess, qmf_t X_left[38][64], qmf_t X_right[38][64])
00907 {       
00908     if (ps == NULL) 
00909     {
00910         memcpy(X_right, X_left, sizeof(qmf_t)*30*64);
00911         return 0;    
00912     }     
00913 
00914     if (!ps->drm_ps_data_available && !guess) 
00915     {
00916         memcpy(X_right, X_left, sizeof(qmf_t)*30*64);
00917         memset(ps->g_prev_sa_index, 0, sizeof(ps->g_prev_sa_index));
00918         memset(ps->g_prev_pan_index, 0, sizeof(ps->g_prev_pan_index));
00919         return 0;
00920     }
00921 
00922     /* if SBR CRC doesn't match out, we can assume decode errors to start with,
00923        and we'll guess what the parameters should be */
00924     if (!guess)
00925     {
00926         ps->sa_decode_error = 0;
00927         ps->pan_decode_error = 0;
00928         drm_ps_delta_decode(ps);
00929     } else 
00930     {
00931         ps->sa_decode_error = 1;
00932         ps->pan_decode_error = 1;
00933         /* don't even bother decoding */
00934     }
00935   
00936     ps->drm_ps_data_available = 0;
00937 
00938     drm_calc_sa_side_signal(ps, X_left);
00939     drm_add_ambiance(ps, X_left, X_right);
00940 
00941     if (ps->bs_enable_sa)
00942     {
00943         ps->g_last_had_sa = 1;        
00944 
00945         memcpy(ps->g_prev_sa_index, ps->g_sa_index, sizeof(int8_t) * DRM_NUM_SA_BANDS);       
00946 
00947     } else {
00948         ps->g_last_had_sa = 0;
00949     }
00950     
00951     if (ps->bs_enable_pan)
00952     {
00953         drm_add_pan(ps, X_left, X_right);
00954     
00955         ps->g_last_had_pan = 1;        
00956 
00957         memcpy(ps->g_prev_pan_index, ps->g_pan_index, sizeof(int8_t) * DRM_NUM_PAN_BANDS);
00958 
00959     } else {
00960         ps->g_last_had_pan = 0;
00961     }
00962 
00963 
00964     return 0;
00965 }
00966 
00967 #endif

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