- Main Page
- Namespaces
- Classes
- Files
- Directories

- File List
- File Members

# specrec.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 Mpeg4AAClicense@nero.com.
    00029 **
    00030 ** $Id: specrec.c,v 1.3 2005/11/01 21:41:43 gabest Exp $
    00031 **/
    00032
    00033 /*
    00034   Spectral reconstruction:
    00035    - grouping/sectioning
    00036    - inverse quantization
    00037    - applying scalefactors
    00038 */
    00039
    00040 #include "common.h"
    00041 #include "structs.h"
    00042
    00043 #include <string.h>
    00044 #include <stdlib.h>
    00045 #include "specrec.h"
    00046 #include "filtbank.h"
    00047 #include "syntax.h"
    00048 #include "iq_table.h"
    00049 #include "ms.h"
    00050 #include "is.h"
    00051 #include "pns.h"
    00052 #include "tns.h"
    00053 #include "drc.h"
    00054 #include "lt_predict.h"
    00055 #include "ic_predict.h"
    00056 #ifdef SSR_DEC
    00057 #include "ssr.h"
    00058 #include "ssr_fb.h"
    00059 #endif
    00060
    00061
    00062 /* static function declarations */
    00063 static uint8_t quant_to_spec(NeAACDecHandle hDecoder,
    00064                              ic_stream *ics, int16_t *quant_data,
    00065                              real_t *spec_data, uint16_t frame_len);
    00066
    00067
    00068 #ifdef LD_DEC
    00069 ALIGN static const uint8_t num_swb_512_window[] =
    00070 {
    00071     0, 0, 0, 36, 36, 37, 31, 31, 0, 0, 0, 0
    00072 };
    00073 ALIGN static const uint8_t num_swb_480_window[] =
    00074 {
    00075     0, 0, 0, 35, 35, 37, 30, 30, 0, 0, 0, 0
    00076 };
    00077 #endif
    00078
    00079 ALIGN static const uint8_t num_swb_960_window[] =
    00080 {
    00081     40, 40, 45, 49, 49, 49, 46, 46, 42, 42, 42, 40
    00082 };
    00083
    00084 ALIGN static const uint8_t num_swb_1024_window[] =
    00085 {
    00086     41, 41, 47, 49, 49, 51, 47, 47, 43, 43, 43, 40
    00087 };
    00088
    00089 ALIGN static const uint8_t num_swb_128_window[] =
    00090 {
    00091     12, 12, 12, 14, 14, 14, 15, 15, 15, 15, 15, 15
    00092 };
    00093
    00094 ALIGN static const uint16_t swb_offset_1024_96[] =
    00095 {
    00096     0, 4, 8, 12, 16, 20, 24, 28, 32, 36, 40, 44, 48, 52, 56,
    00097     64, 72, 80, 88, 96, 108, 120, 132, 144, 156, 172, 188, 212, 240,
    00098     276, 320, 384, 448, 512, 576, 640, 704, 768, 832, 896, 960, 1024
    00099 };
    00100
    00101 ALIGN static const uint16_t swb_offset_128_96[] =
    00102 {
    00103     0, 4, 8, 12, 16, 20, 24, 32, 40, 48, 64, 92, 128
    00104 };
    00105
    00106 ALIGN static const uint16_t swb_offset_1024_64[] =
    00107 {
    00108     0, 4, 8, 12, 16, 20, 24, 28, 32, 36, 40, 44, 48, 52, 56,
    00109     64, 72, 80, 88, 100, 112, 124, 140, 156, 172, 192, 216, 240, 268,
    00110     304, 344, 384, 424, 464, 504, 544, 584, 624, 664, 704, 744, 784, 824,
    00111     864, 904, 944, 984, 1024
    00112 };
    00113
    00114 ALIGN static const uint16_t swb_offset_128_64[] =
    00115 {
    00116     0, 4, 8, 12, 16, 20, 24, 32, 40, 48, 64, 92, 128
    00117 };
    00118
    00119 ALIGN static const uint16_t swb_offset_1024_48[] =
    00120 {
    00121     0, 4, 8, 12, 16, 20, 24, 28, 32, 36, 40, 48, 56, 64, 72,
    00122     80, 88, 96, 108, 120, 132, 144, 160, 176, 196, 216, 240, 264, 292,
    00123     320, 352, 384, 416, 448, 480, 512, 544, 576, 608, 640, 672, 704, 736,
    00124     768, 800, 832, 864, 896, 928, 1024
    00125 };
    00126
    00127 #ifdef LD_DEC
    00128 ALIGN static const uint16_t swb_offset_512_48[] =
    00129 {
    00130     0, 4, 8, 12, 16, 20, 24, 28, 32, 36, 40, 44, 48, 52, 56, 60, 68, 76, 84,
    00131     92, 100, 112, 124, 136, 148, 164, 184, 208, 236, 268, 300, 332, 364, 396,
    00132     428, 460, 512
    00133 };
    00134
    00135 ALIGN static const uint16_t swb_offset_480_48[] =
    00136 {
    00137     0, 4, 8, 12, 16, 20, 24, 28, 32, 36, 40, 44, 48, 52, 56, 64, 72 ,80 ,88,
    00138     96, 108, 120, 132, 144, 156, 172, 188, 212, 240, 272, 304, 336, 368, 400,
    00139     432, 480
    00140 };
    00141 #endif
    00142
    00143 ALIGN static const uint16_t swb_offset_128_48[] =
    00144 {
    00145     0, 4, 8, 12, 16, 20, 28, 36, 44, 56, 68, 80, 96, 112, 128
    00146 };
    00147
    00148 ALIGN static const uint16_t swb_offset_1024_32[] =
    00149 {
    00150     0, 4, 8, 12, 16, 20, 24, 28, 32, 36, 40, 48, 56, 64, 72,
    00151     80, 88, 96, 108, 120, 132, 144, 160, 176, 196, 216, 240, 264, 292,
    00152     320, 352, 384, 416, 448, 480, 512, 544, 576, 608, 640, 672, 704, 736,
    00153     768, 800, 832, 864, 896, 928, 960, 992, 1024
    00154 };
    00155
    00156 #ifdef LD_DEC
    00157 ALIGN static const uint16_t swb_offset_512_32[] =
    00158 {
    00159     0, 4, 8, 12, 16, 20, 24, 28, 32, 36, 40, 44, 48, 52, 56, 64, 72, 80,
    00160     88, 96, 108, 120, 132, 144, 160, 176, 192, 212, 236, 260, 288, 320, 352,
    00161     384, 416, 448, 480, 512
    00162 };
    00163
    00164 ALIGN static const uint16_t swb_offset_480_32[] =
    00165 {
    00166     0, 4, 8, 12, 16, 20, 24, 28, 32, 36, 40, 44, 48, 52, 56, 60, 64, 72, 80,
    00167     88, 96, 104, 112, 124, 136, 148, 164, 180, 200, 224, 256, 288, 320, 352,
    00168     384, 416, 448, 480
    00169 };
    00170 #endif
    00171
    00172 ALIGN static const uint16_t swb_offset_1024_24[] =
    00173 {
    00174     0, 4, 8, 12, 16, 20, 24, 28, 32, 36, 40, 44, 52, 60, 68,
    00175     76, 84, 92, 100, 108, 116, 124, 136, 148, 160, 172, 188, 204, 220,
    00176     240, 260, 284, 308, 336, 364, 396, 432, 468, 508, 552, 600, 652, 704,
    00177     768, 832, 896, 960, 1024
    00178 };
    00179
    00180 #ifdef LD_DEC
    00181 ALIGN static const uint16_t swb_offset_512_24[] =
    00182 {
    00183     0, 4, 8, 12, 16, 20, 24, 28, 32, 36, 40, 44, 52, 60, 68,
    00184     80, 92, 104, 120, 140, 164, 192, 224, 256, 288, 320, 352, 384, 416,
    00185     448, 480, 512
    00186 };
    00187
    00188 ALIGN static const uint16_t swb_offset_480_24[] =
    00189 {
    00190     0, 4, 8, 12, 16, 20, 24, 28, 32, 36, 40, 44, 52, 60, 68, 80, 92, 104, 120,
    00191     140, 164, 192, 224, 256, 288, 320, 352, 384, 416, 448, 480
    00192 };
    00193 #endif
    00194
    00195 ALIGN static const uint16_t swb_offset_128_24[] =
    00196 {
    00197     0, 4, 8, 12, 16, 20, 24, 28, 36, 44, 52, 64, 76, 92, 108, 128
    00198 };
    00199
    00200 ALIGN static const uint16_t swb_offset_1024_16[] =
    00201 {
    00202     0, 8, 16, 24, 32, 40, 48, 56, 64, 72, 80, 88, 100, 112, 124,
    00203     136, 148, 160, 172, 184, 196, 212, 228, 244, 260, 280, 300, 320, 344,
    00204     368, 396, 424, 456, 492, 532, 572, 616, 664, 716, 772, 832, 896, 960, 1024
    00205 };
    00206
    00207 ALIGN static const uint16_t swb_offset_128_16[] =
    00208 {
    00209     0, 4, 8, 12, 16, 20, 24, 28, 32, 40, 48, 60, 72, 88, 108, 128
    00210 };
    00211
    00212 ALIGN static const uint16_t swb_offset_1024_8[] =
    00213 {
    00214     0, 12, 24, 36, 48, 60, 72, 84, 96, 108, 120, 132, 144, 156, 172,
    00215     188, 204, 220, 236, 252, 268, 288, 308, 328, 348, 372, 396, 420, 448,
    00216     476, 508, 544, 580, 620, 664, 712, 764, 820, 880, 944, 1024
    00217 };
    00218
    00219 ALIGN static const uint16_t swb_offset_128_8[] =
    00220 {
    00221     0, 4, 8, 12, 16, 20, 24, 28, 36, 44, 52, 60, 72, 88, 108, 128
    00222 };
    00223
    00224 ALIGN static const uint16_t *swb_offset_1024_window[] =
    00225 {
    00226     swb_offset_1024_96,      /* 96000 */
    00227     swb_offset_1024_96,      /* 88200 */
    00228     swb_offset_1024_64,      /* 64000 */
    00229     swb_offset_1024_48,      /* 48000 */
    00230     swb_offset_1024_48,      /* 44100 */
    00231     swb_offset_1024_32,      /* 32000 */
    00232     swb_offset_1024_24,      /* 24000 */
    00233     swb_offset_1024_24,      /* 22050 */
    00234     swb_offset_1024_16,      /* 16000 */
    00235     swb_offset_1024_16,      /* 12000 */
    00236     swb_offset_1024_16,      /* 11025 */
    00237     swb_offset_1024_8        /* 8000  */
    00238 };
    00239
    00240 #ifdef LD_DEC
    00241 ALIGN static const uint16_t *swb_offset_512_window[] =
    00242 {
    00243     0,                       /* 96000 */
    00244     0,                       /* 88200 */
    00245     0,                       /* 64000 */
    00246     swb_offset_512_48,       /* 48000 */
    00247     swb_offset_512_48,       /* 44100 */
    00248     swb_offset_512_32,       /* 32000 */
    00249     swb_offset_512_24,       /* 24000 */
    00250     swb_offset_512_24,       /* 22050 */
    00251     0,                       /* 16000 */
    00252     0,                       /* 12000 */
    00253     0,                       /* 11025 */
    00254     0                        /* 8000  */
    00255 };
    00256
    00257 ALIGN static const uint16_t *swb_offset_480_window[] =
    00258 {
    00259     0,                       /* 96000 */
    00260     0,                       /* 88200 */
    00261     0,                       /* 64000 */
    00262     swb_offset_480_48,       /* 48000 */
    00263     swb_offset_480_48,       /* 44100 */
    00264     swb_offset_480_32,       /* 32000 */
    00265     swb_offset_480_24,       /* 24000 */
    00266     swb_offset_480_24,       /* 22050 */
    00267     0,                       /* 16000 */
    00268     0,                       /* 12000 */
    00269     0,                       /* 11025 */
    00270     0                        /* 8000  */
    00271 };
    00272 #endif
    00273
    00274 ALIGN static const  uint16_t *swb_offset_128_window[] =
    00275 {
    00276     swb_offset_128_96,       /* 96000 */
    00277     swb_offset_128_96,       /* 88200 */
    00278     swb_offset_128_64,       /* 64000 */
    00279     swb_offset_128_48,       /* 48000 */
    00280     swb_offset_128_48,       /* 44100 */
    00281     swb_offset_128_48,       /* 32000 */
    00282     swb_offset_128_24,       /* 24000 */
    00283     swb_offset_128_24,       /* 22050 */
    00284     swb_offset_128_16,       /* 16000 */
    00285     swb_offset_128_16,       /* 12000 */
    00286     swb_offset_128_16,       /* 11025 */
    00287     swb_offset_128_8         /* 8000  */
    00288 };
    00289
    00290 #define bit_set(A, B) ((A) & (1<<(B)))
    00291
    00292 /* 4.5.2.3.4 */
    00293 /*
    00294   - determine the number of windows in a window_sequence named num_windows
    00295   - determine the number of window_groups named num_window_groups
    00296   - determine the number of windows in each group named window_group_length[g]
    00297   - determine the total number of scalefactor window bands named num_swb for
    00298     the actual window type
    00299   - determine swb_offset[swb], the offset of the first coefficient in
    00300     scalefactor window band named swb of the window actually used
    00301   - determine sect_sfb_offset[g][section],the offset of the first coefficient
    00302     in section named section. This offset depends on window_sequence and
    00303     scale_factor_grouping and is needed to decode the spectral_data().
    00304 */
    00305 uint8_t window_grouping_info(NeAACDecHandle hDecoder, ic_stream *ics)
    00306 {
    00307     uint8_t i, g;
    00308
    00309     uint8_t sf_index = hDecoder->sf_index;
    00310
    00311     switch (ics->window_sequence) {
    00312     case ONLY_LONG_SEQUENCE:
    00313     case LONG_START_SEQUENCE:
    00314     case LONG_STOP_SEQUENCE:
    00315         ics->num_windows = 1;
    00316         ics->num_window_groups = 1;
    00317         ics->window_group_length[ics->num_window_groups-1] = 1;
    00318 #ifdef LD_DEC
    00319         if (hDecoder->object_type == LD)
    00320         {
    00321             if (hDecoder->frameLength == 512)
    00322                 ics->num_swb = num_swb_512_window[sf_index];
    00323             else /* if (hDecoder->frameLength == 480) */
    00324                 ics->num_swb = num_swb_480_window[sf_index];
    00325         } else {
    00326 #endif
    00327             if (hDecoder->frameLength == 1024)
    00328                 ics->num_swb = num_swb_1024_window[sf_index];
    00329             else /* if (hDecoder->frameLength == 960) */
    00330                 ics->num_swb = num_swb_960_window[sf_index];
    00331 #ifdef LD_DEC
    00332         }
    00333 #endif
    00334
    00335         if (ics->max_sfb > ics->num_swb)
    00336         {
    00337             return 32;
    00338         }
    00339
    00340         /* preparation of sect_sfb_offset for long blocks */
    00341         /* also copy the last value! */
    00342 #ifdef LD_DEC
    00343         if (hDecoder->object_type == LD)
    00344         {
    00345             if (hDecoder->frameLength == 512)
    00346             {
    00347                 for (i = 0; i < ics->num_swb; i++)
    00348                 {
    00349                     ics->sect_sfb_offset[0][i] = swb_offset_512_window[sf_index][i];
    00350                     ics->swb_offset[i] = swb_offset_512_window[sf_index][i];
    00351                 }
    00352             } else /* if (hDecoder->frameLength == 480) */ {
    00353                 for (i = 0; i < ics->num_swb; i++)
    00354                 {
    00355                     ics->sect_sfb_offset[0][i] = swb_offset_480_window[sf_index][i];
    00356                     ics->swb_offset[i] = swb_offset_480_window[sf_index][i];
    00357                 }
    00358             }
    00359             ics->sect_sfb_offset[0][ics->num_swb] = hDecoder->frameLength;
    00360             ics->swb_offset[ics->num_swb] = hDecoder->frameLength;
    00361         } else {
    00362 #endif
    00363             for (i = 0; i < ics->num_swb; i++)
    00364             {
    00365                 ics->sect_sfb_offset[0][i] = swb_offset_1024_window[sf_index][i];
    00366                 ics->swb_offset[i] = swb_offset_1024_window[sf_index][i];
    00367             }
    00368             ics->sect_sfb_offset[0][ics->num_swb] = hDecoder->frameLength;
    00369             ics->swb_offset[ics->num_swb] = hDecoder->frameLength;
    00370 #ifdef LD_DEC
    00371         }
    00372 #endif
    00373         return 0;
    00374     case EIGHT_SHORT_SEQUENCE:
    00375         ics->num_windows = 8;
    00376         ics->num_window_groups = 1;
    00377         ics->window_group_length[ics->num_window_groups-1] = 1;
    00378         ics->num_swb = num_swb_128_window[sf_index];
    00379
    00380         if (ics->max_sfb > ics->num_swb)
    00381         {
    00382             return 32;
    00383         }
    00384
    00385         for (i = 0; i < ics->num_swb; i++)
    00386             ics->swb_offset[i] = swb_offset_128_window[sf_index][i];
    00387         ics->swb_offset[ics->num_swb] = hDecoder->frameLength/8;
    00388
    00389         for (i = 0; i < ics->num_windows-1; i++) {
    00390             if (bit_set(ics->scale_factor_grouping, 6-i) == 0)
    00391             {
    00392                 ics->num_window_groups += 1;
    00393                 ics->window_group_length[ics->num_window_groups-1] = 1;
    00394             } else {
    00395                 ics->window_group_length[ics->num_window_groups-1] += 1;
    00396             }
    00397         }
    00398
    00399         /* preparation of sect_sfb_offset for short blocks */
    00400         for (g = 0; g < ics->num_window_groups; g++)
    00401         {
    00402             uint16_t width;
    00403             uint8_t sect_sfb = 0;
    00404             uint16_t offset = 0;
    00405
    00406             for (i = 0; i < ics->num_swb; i++)
    00407             {
    00408                 if (i+1 == ics->num_swb)
    00409                 {
    00410                     width = (hDecoder->frameLength/8) - swb_offset_128_window[sf_index][i];
    00411                 } else {
    00412                     width = swb_offset_128_window[sf_index][i+1] -
    00413                         swb_offset_128_window[sf_index][i];
    00414                 }
    00415                 width *= ics->window_group_length[g];
    00416                 ics->sect_sfb_offset[g][sect_sfb++] = offset;
    00417                 offset += width;
    00418             }
    00419             ics->sect_sfb_offset[g][sect_sfb] = offset;
    00420         }
    00421         return 0;
    00422     default:
    00423         return 32;
    00424     }
    00425 }
    00426
    00427 /* iquant() *
    00428 /* output = sign(input)*abs(input)^(4/3) */
    00429
    00430 static INLINE real_t iquant(int16_t q, const real_t *tab, uint8_t *error)
    00431 {
    00432 #ifdef FIXED_POINT
    00433 /* For FIXED_POINT the iq_table is prescaled by 3 bits (iq_table[]/8) */
    00434 /* BIG_IQ_TABLE allows you to use the full 8192 value table, if this is not
    00435  * defined a 1026 value table and interpolation will be used
    00436  */
    00437 #ifndef BIG_IQ_TABLE
    00438     static const real_t errcorr[] = {
    00439         REAL_CONST(0), REAL_CONST(1.0/8.0), REAL_CONST(2.0/8.0), REAL_CONST(3.0/8.0),
    00440         REAL_CONST(4.0/8.0),  REAL_CONST(5.0/8.0), REAL_CONST(6.0/8.0), REAL_CONST(7.0/8.0),
    00441         REAL_CONST(0)
    00442     };
    00443     real_t x1, x2;
    00444 #endif
    00445     int16_t sgn = 1;
    00446
    00447     if (q < 0)
    00448     {
    00449         q = -q;
    00450         sgn = -1;
    00451     }
    00452
    00453     if (q < IQ_TABLE_SIZE)
    00454     {
    00455 //#define IQUANT_PRINT
    00456 #ifdef IQUANT_PRINT
    00457         //printf("0x%.8X\n", sgn * tab[q]);
    00458         printf("%d\n", sgn * tab[q]);
    00459 #endif
    00460         return sgn * tab[q];
    00461     }
    00462
    00463 #ifndef BIG_IQ_TABLE
    00464     if (q >= 8192)
    00465     {
    00466         *error = 17;
    00467         return 0;
    00468     }
    00469
    00470     /* linear interpolation */
    00471     x1 = tab[q>>3];
    00472     x2 = tab[(q>>3) + 1];
    00473     return sgn * 16 * (MUL_R(errcorr[q&7],(x2-x1)) + x1);
    00474 #else
    00475     *error = 17;
    00476     return 0;
    00477 #endif
    00478
    00479 #else
    00480     if (q < 0)
    00481     {
    00482         /* tab contains a value for all possible q [0,8192] */
    00483         if (-q < IQ_TABLE_SIZE)
    00484             return -tab[-q];
    00485
    00486         *error = 17;
    00487         return 0;
    00488     } else {
    00489         /* tab contains a value for all possible q [0,8192] */
    00490         if (q < IQ_TABLE_SIZE)
    00491             return tab[q];
    00492
    00493         *error = 17;
    00494         return 0;
    00495     }
    00496 #endif
    00497 }
    00498
    00499 #ifndef FIXED_POINT
    00500 ALIGN static const real_t pow2sf_tab[] = {
    00501     2.9802322387695313E-008, 5.9604644775390625E-008, 1.1920928955078125E-007,
    00502     2.384185791015625E-007, 4.76837158203125E-007, 9.5367431640625E-007,
    00503     1.9073486328125E-006, 3.814697265625E-006, 7.62939453125E-006,
    00504     1.52587890625E-005, 3.0517578125E-005, 6.103515625E-005,
    00505     0.0001220703125, 0.000244140625, 0.00048828125,
    00506     0.0009765625, 0.001953125, 0.00390625,
    00507     0.0078125, 0.015625, 0.03125,
    00508     0.0625, 0.125, 0.25,
    00509     0.5, 1.0, 2.0,
    00510     4.0, 8.0, 16.0, 32.0,
    00511     64.0, 128.0, 256.0,
    00512     512.0, 1024.0, 2048.0,
    00513     4096.0, 8192.0, 16384.0,
    00514     32768.0, 65536.0, 131072.0,
    00515     262144.0, 524288.0, 1048576.0,
    00516     2097152.0, 4194304.0, 8388608.0,
    00517     16777216.0, 33554432.0, 67108864.0,
    00518     134217728.0, 268435456.0, 536870912.0,
    00519     1073741824.0, 2147483648.0, 4294967296.0,
    00520     8589934592.0, 17179869184.0, 34359738368.0,
    00521     68719476736.0, 137438953472.0, 274877906944.0
    00522 };
    00523 #endif
    00524
    00525 /* quant_to_spec: perform dequantisation and scaling
    00526  * and in case of short block it also does the deinterleaving
    00527  */
    00528 /*
    00529   For ONLY_LONG_SEQUENCE windows (num_window_groups = 1,
    00530   window_group_length[0] = 1) the spectral data is in ascending spectral
    00531   order.
    00532   For the EIGHT_SHORT_SEQUENCE window, the spectral order depends on the
    00533   grouping in the following manner:
    00534   - Groups are ordered sequentially
    00535   - Within a group, a scalefactor band consists of the spectral data of all
    00536     grouped SHORT_WINDOWs for the associated scalefactor window band. To
    00537     clarify via example, the length of a group is in the range of one to eight
    00538     SHORT_WINDOWs.
    00539   - If there are eight groups each with length one (num_window_groups = 8,
    00540     window_group_length[0..7] = 1), the result is a sequence of eight spectra,
    00541     each in ascending spectral order.
    00542   - If there is only one group with length eight (num_window_groups = 1,
    00543     window_group_length[0] = 8), the result is that spectral data of all eight
    00544     SHORT_WINDOWs is interleaved by scalefactor window bands.
    00545   - Within a scalefactor window band, the coefficients are in ascending
    00546     spectral order.
    00547 */
    00548 static uint8_t quant_to_spec(NeAACDecHandle hDecoder,
    00549                              ic_stream *ics, int16_t *quant_data,
    00550                              real_t *spec_data, uint16_t frame_len)
    00551 {
    00552     ALIGN static const real_t pow2_table[] =
    00553     {
    00554         COEF_CONST(1.0),
    00555         COEF_CONST(1.1892071150027210667174999705605), /* 2^0.25 */
    00556         COEF_CONST(1.4142135623730950488016887242097), /* 2^0.5 */
    00557         COEF_CONST(1.6817928305074290860622509524664) /* 2^0.75 */
    00558     };
    00559     const real_t *tab = iq_table;
    00560
    00561     uint8_t g, sfb, win;
    00562     uint16_t width, bin, k, gindex, wa, wb;
    00563     uint8_t error = 0; /* Init error flag */
    00564 #ifndef FIXED_POINT
    00565     real_t scf;
    00566 #endif
    00567
    00568     k = 0;
    00569     gindex = 0;
    00570
    00571     for (g = 0; g < ics->num_window_groups; g++)
    00572     {
    00573         uint16_t j = 0;
    00574         uint16_t gincrease = 0;
    00575         uint16_t win_inc = ics->swb_offset[ics->num_swb];
    00576
    00577         for (sfb = 0; sfb < ics->num_swb; sfb++)
    00578         {
    00579             int32_t exp, frac;
    00580
    00581             width = ics->swb_offset[sfb+1] - ics->swb_offset[sfb];
    00582
    00583             /* this could be scalefactor for IS or PNS, those can be negative or bigger then 255 */
    00584             /* just ignore them */
    00585             if (ics->scale_factors[g][sfb] < 0 || ics->scale_factors[g][sfb] > 255)
    00586             {
    00587                 exp = 0;
    00588                 frac = 0;
    00589             } else {
    00590                 /* ics->scale_factors[g][sfb] must be between 0 and 255 */
    00591                 exp = (ics->scale_factors[g][sfb] /* - 100 */) >> 2;
    00592                 /* frac must always be > 0 */
    00593                 frac = (ics->scale_factors[g][sfb] /* - 100 */) & 3;
    00594             }
    00595
    00596 #ifdef FIXED_POINT
    00597             exp -= 25;
    00598             /* IMDCT pre-scaling */
    00599             if (hDecoder->object_type == LD)
    00600             {
    00601                 exp -= 6 /*9*/;
    00602             } else {
    00603                 if (ics->window_sequence == EIGHT_SHORT_SEQUENCE)
    00604                     exp -= 4 /*7*/;
    00605                 else
    00606                     exp -= 7 /*10*/;
    00607             }
    00608 #endif
    00609
    00610             wa = gindex + j;
    00611
    00612 #ifndef FIXED_POINT
    00613             scf = pow2sf_tab[exp/*+25*/] * pow2_table[frac];
    00614 #endif
    00615
    00616             for (win = 0; win < ics->window_group_length[g]; win++)
    00617             {
    00618                 for (bin = 0; bin < width; bin += 4)
    00619                 {
    00620 #ifndef FIXED_POINT
    00621                     wb = wa + bin;
    00622
    00623                     spec_data[wb+0] = iquant(quant_data[k+0], tab, &error) * scf;
    00624                     spec_data[wb+1] = iquant(quant_data[k+1], tab, &error) * scf;
    00625                     spec_data[wb+2] = iquant(quant_data[k+2], tab, &error) * scf;
    00626                     spec_data[wb+3] = iquant(quant_data[k+3], tab, &error) * scf;
    00627
    00628 #else
    00629                     real_t iq0 = iquant(quant_data[k+0], tab, &error);
    00630                     real_t iq1 = iquant(quant_data[k+1], tab, &error);
    00631                     real_t iq2 = iquant(quant_data[k+2], tab, &error);
    00632                     real_t iq3 = iquant(quant_data[k+3], tab, &error);
    00633
    00634                     wb = wa + bin;
    00635
    00636                     if (exp < 0)
    00637                     {
    00638                         spec_data[wb+0] = iq0 >>= -exp;
    00639                         spec_data[wb+1] = iq1 >>= -exp;
    00640                         spec_data[wb+2] = iq2 >>= -exp;
    00641                         spec_data[wb+3] = iq3 >>= -exp;
    00642                     } else {
    00643                         spec_data[wb+0] = iq0 <<= exp;
    00644                         spec_data[wb+1] = iq1 <<= exp;
    00645                         spec_data[wb+2] = iq2 <<= exp;
    00646                         spec_data[wb+3] = iq3 <<= exp;
    00647                     }
    00648                     if (frac != 0)
    00649                     {
    00650                         spec_data[wb+0] = MUL_C(spec_data[wb+0],pow2_table[frac]);
    00651                         spec_data[wb+1] = MUL_C(spec_data[wb+1],pow2_table[frac]);
    00652                         spec_data[wb+2] = MUL_C(spec_data[wb+2],pow2_table[frac]);
    00653                         spec_data[wb+3] = MUL_C(spec_data[wb+3],pow2_table[frac]);
    00654                     }
    00655
    00656 //#define SCFS_PRINT
    00657 #ifdef SCFS_PRINT
    00658                     printf("%d\n", spec_data[gindex+(win*win_inc)+j+bin+0]);
    00659                     printf("%d\n", spec_data[gindex+(win*win_inc)+j+bin+1]);
    00660                     printf("%d\n", spec_data[gindex+(win*win_inc)+j+bin+2]);
    00661                     printf("%d\n", spec_data[gindex+(win*win_inc)+j+bin+3]);
    00662                     //printf("0x%.8X\n", spec_data[gindex+(win*win_inc)+j+bin+0]);
    00663                     //printf("0x%.8X\n", spec_data[gindex+(win*win_inc)+j+bin+1]);
    00664                     //printf("0x%.8X\n", spec_data[gindex+(win*win_inc)+j+bin+2]);
    00665                     //printf("0x%.8X\n", spec_data[gindex+(win*win_inc)+j+bin+3]);
    00666 #endif
    00667 #endif
    00668
    00669                     gincrease += 4;
    00670                     k += 4;
    00671                 }
    00672                 wa += win_inc;
    00673             }
    00674             j += width;
    00675         }
    00676         gindex += gincrease;
    00677     }
    00678
    00679     return error;
    00680 }
    00681
    00682 static uint8_t allocate_single_channel(NeAACDecHandle hDecoder, uint8_t channel,
    00683                                        uint8_t output_channels)
    00684 {
    00685     uint8_t mul = 1;
    00686
    00687 #ifdef MAIN_DEC
    00688     /* MAIN object type prediction */
    00689     if (hDecoder->object_type == MAIN)
    00690     {
    00691         /* allocate the state only when needed */
    00692         if (hDecoder->pred_stat[channel] != NULL)
    00693         {
    00694             faad_free(hDecoder->pred_stat[channel]);
    00695             hDecoder->pred_stat[channel] = NULL;
    00696         }
    00697
    00698         hDecoder->pred_stat[channel] = (pred_state*)faad_malloc(hDecoder->frameLength * sizeof(pred_state));
    00699         reset_all_predictors(hDecoder->pred_stat[channel], hDecoder->frameLength);
    00700     }
    00701 #endif
    00702
    00703 #ifdef LTP_DEC
    00704     if (is_ltp_ot(hDecoder->object_type))
    00705     {
    00706         /* allocate the state only when needed */
    00707         if (hDecoder->lt_pred_stat[channel] != NULL)
    00708         {
    00709             faad_free(hDecoder->lt_pred_stat[channel]);
    00710             hDecoder->lt_pred_stat[channel] = NULL;
    00711         }
    00712
    00713         hDecoder->lt_pred_stat[channel] = (int16_t*)faad_malloc(hDecoder->frameLength*4 * sizeof(int16_t));
    00714         memset(hDecoder->lt_pred_stat[channel], 0, hDecoder->frameLength*4 * sizeof(int16_t));
    00715     }
    00716 #endif
    00717
    00718     if (hDecoder->time_out[channel] != NULL)
    00719     {
    00720         faad_free(hDecoder->time_out[channel]);
    00721         hDecoder->time_out[channel] = NULL;
    00722     }
    00723
    00724     {
    00725         mul = 1;
    00726 #ifdef SBR_DEC
    00727         hDecoder->sbr_alloced[hDecoder->fr_ch_ele] = 0;
    00728         if ((hDecoder->sbr_present_flag == 1) || (hDecoder->forceUpSampling == 1))
    00729         {
    00730             /* SBR requires 2 times as much output data */
    00731             mul = 2;
    00732             hDecoder->sbr_alloced[hDecoder->fr_ch_ele] = 1;
    00733         }
    00734 #endif
    00735         hDecoder->time_out[channel] = (real_t*)faad_malloc(mul*hDecoder->frameLength*sizeof(real_t));
    00736         memset(hDecoder->time_out[channel], 0, mul*hDecoder->frameLength*sizeof(real_t));
    00737     }
    00738
    00739 #if (defined(PS_DEC) || defined(DRM_PS))
    00740     if (output_channels == 2)
    00741     {
    00742         if (hDecoder->time_out[channel+1] != NULL)
    00743         {
    00744             faad_free(hDecoder->time_out[channel+1]);
    00745             hDecoder->time_out[channel+1] = NULL;
    00746         }
    00747
    00748         hDecoder->time_out[channel+1] = (real_t*)faad_malloc(mul*hDecoder->frameLength*sizeof(real_t));
    00749         memset(hDecoder->time_out[channel+1], 0, mul*hDecoder->frameLength*sizeof(real_t));
    00750     }
    00751 #endif
    00752
    00753     if (hDecoder->fb_intermed[channel] != NULL)
    00754     {
    00755         faad_free(hDecoder->fb_intermed[channel]);
    00756         hDecoder->fb_intermed[channel] = NULL;
    00757     }
    00758
    00759     hDecoder->fb_intermed[channel] = (real_t*)faad_malloc(hDecoder->frameLength*sizeof(real_t));
    00760     memset(hDecoder->fb_intermed[channel], 0, hDecoder->frameLength*sizeof(real_t));
    00761
    00762 #ifdef SSR_DEC
    00763     if (hDecoder->object_type == SSR)
    00764     {
    00765         if (hDecoder->ssr_overlap[channel] == NULL)
    00766         {
    00767             hDecoder->ssr_overlap[channel] = (real_t*)faad_malloc(2*hDecoder->frameLength*sizeof(real_t));
    00768             memset(hDecoder->ssr_overlap[channel], 0, 2*hDecoder->frameLength*sizeof(real_t));
    00769         }
    00770         if (hDecoder->prev_fmd[channel] == NULL)
    00771         {
    00772             uint16_t k;
    00773             hDecoder->prev_fmd[channel] = (real_t*)faad_malloc(2*hDecoder->frameLength*sizeof(real_t));
    00774             for (k = 0; k < 2*hDecoder->frameLength; k++)
    00775                 hDecoder->prev_fmd[channel][k] = REAL_CONST(-1);
    00776         }
    00777     }
    00778 #endif
    00779
    00780     return 0;
    00781 }
    00782
    00783 static uint8_t allocate_channel_pair(NeAACDecHandle hDecoder,
    00784                                      uint8_t channel, uint8_t paired_channel)
    00785 {
    00786     uint8_t mul = 1;
    00787
    00788 #ifdef MAIN_DEC
    00789     /* MAIN object type prediction */
    00790     if (hDecoder->object_type == MAIN)
    00791     {
    00792         /* allocate the state only when needed */
    00793         if (hDecoder->pred_stat[channel] == NULL)
    00794         {
    00795             hDecoder->pred_stat[channel] = (pred_state*)faad_malloc(hDecoder->frameLength * sizeof(pred_state));
    00796             reset_all_predictors(hDecoder->pred_stat[channel], hDecoder->frameLength);
    00797         }
    00798         if (hDecoder->pred_stat[paired_channel] == NULL)
    00799         {
    00800             hDecoder->pred_stat[paired_channel] = (pred_state*)faad_malloc(hDecoder->frameLength * sizeof(pred_state));
    00801             reset_all_predictors(hDecoder->pred_stat[paired_channel], hDecoder->frameLength);
    00802         }
    00803     }
    00804 #endif
    00805
    00806 #ifdef LTP_DEC
    00807     if (is_ltp_ot(hDecoder->object_type))
    00808     {
    00809         /* allocate the state only when needed */
    00810         if (hDecoder->lt_pred_stat[channel] == NULL)
    00811         {
    00812             hDecoder->lt_pred_stat[channel] = (int16_t*)faad_malloc(hDecoder->frameLength*4 * sizeof(int16_t));
    00813             memset(hDecoder->lt_pred_stat[channel], 0, hDecoder->frameLength*4 * sizeof(int16_t));
    00814         }
    00815         if (hDecoder->lt_pred_stat[paired_channel] == NULL)
    00816         {
    00817             hDecoder->lt_pred_stat[paired_channel] = (int16_t*)faad_malloc(hDecoder->frameLength*4 * sizeof(int16_t));
    00818             memset(hDecoder->lt_pred_stat[paired_channel], 0, hDecoder->frameLength*4 * sizeof(int16_t));
    00819         }
    00820     }
    00821 #endif
    00822
    00823     if (hDecoder->time_out[channel] == NULL)
    00824     {
    00825         mul = 1;
    00826 #ifdef SBR_DEC
    00827         hDecoder->sbr_alloced[hDecoder->fr_ch_ele] = 0;
    00828         if ((hDecoder->sbr_present_flag == 1) || (hDecoder->forceUpSampling == 1))
    00829         {
    00830             /* SBR requires 2 times as much output data */
    00831             mul = 2;
    00832             hDecoder->sbr_alloced[hDecoder->fr_ch_ele] = 1;
    00833         }
    00834 #endif
    00835         hDecoder->time_out[channel] = (real_t*)faad_malloc(mul*hDecoder->frameLength*sizeof(real_t));
    00836         memset(hDecoder->time_out[channel], 0, mul*hDecoder->frameLength*sizeof(real_t));
    00837     }
    00838     if (hDecoder->time_out[paired_channel] == NULL)
    00839     {
    00840         hDecoder->time_out[paired_channel] = (real_t*)faad_malloc(mul*hDecoder->frameLength*sizeof(real_t));
    00841         memset(hDecoder->time_out[paired_channel], 0, mul*hDecoder->frameLength*sizeof(real_t));
    00842     }
    00843
    00844     if (hDecoder->fb_intermed[channel] == NULL)
    00845     {
    00846         hDecoder->fb_intermed[channel] = (real_t*)faad_malloc(hDecoder->frameLength*sizeof(real_t));
    00847         memset(hDecoder->fb_intermed[channel], 0, hDecoder->frameLength*sizeof(real_t));
    00848     }
    00849     if (hDecoder->fb_intermed[paired_channel] == NULL)
    00850     {
    00851         hDecoder->fb_intermed[paired_channel] = (real_t*)faad_malloc(hDecoder->frameLength*sizeof(real_t));
    00852         memset(hDecoder->fb_intermed[paired_channel], 0, hDecoder->frameLength*sizeof(real_t));
    00853     }
    00854
    00855 #ifdef SSR_DEC
    00856     if (hDecoder->object_type == SSR)
    00857     {
    00858         if (hDecoder->ssr_overlap[cpe->channel] == NULL)
    00859         {
    00860             hDecoder->ssr_overlap[cpe->channel] = (real_t*)faad_malloc(2*hDecoder->frameLength*sizeof(real_t));
    00861             memset(hDecoder->ssr_overlap[cpe->channel], 0, 2*hDecoder->frameLength*sizeof(real_t));
    00862         }
    00863         if (hDecoder->ssr_overlap[cpe->paired_channel] == NULL)
    00864         {
    00865             hDecoder->ssr_overlap[cpe->paired_channel] = (real_t*)faad_malloc(2*hDecoder->frameLength*sizeof(real_t));
    00866             memset(hDecoder->ssr_overlap[cpe->paired_channel], 0, 2*hDecoder->frameLength*sizeof(real_t));
    00867         }
    00868         if (hDecoder->prev_fmd[cpe->channel] == NULL)
    00869         {
    00870             uint16_t k;
    00871             hDecoder->prev_fmd[cpe->channel] = (real_t*)faad_malloc(2*hDecoder->frameLength*sizeof(real_t));
    00872             for (k = 0; k < 2*hDecoder->frameLength; k++)
    00873                 hDecoder->prev_fmd[cpe->channel][k] = REAL_CONST(-1);
    00874         }
    00875         if (hDecoder->prev_fmd[cpe->paired_channel] == NULL)
    00876         {
    00877             uint16_t k;
    00878             hDecoder->prev_fmd[cpe->paired_channel] = (real_t*)faad_malloc(2*hDecoder->frameLength*sizeof(real_t));
    00879             for (k = 0; k < 2*hDecoder->frameLength; k++)
    00880                 hDecoder->prev_fmd[cpe->paired_channel][k] = REAL_CONST(-1);
    00881         }
    00882     }
    00883 #endif
    00884
    00885     return 0;
    00886 }
    00887
    00888 uint8_t reconstruct_single_channel(NeAACDecHandle hDecoder, ic_stream *ics,
    00889                                    element *sce, int16_t *spec_data)
    00890 {
    00891     uint8_t retval, output_channels;
    00892     ALIGN real_t spec_coef[1024];
    00893
    00894 #ifdef PROFILE
    00895     int64_t count = faad_get_ts();
    00896 #endif
    00897
    00898
    00899     /* always allocate 2 channels, PS can always "suddenly" turn up */
    00900 #if ( (defined(DRM) && defined(DRM_PS)) )
    00901     output_channels = 2;
    00902 #elif defined(PS_DEC)
    00903     if (hDecoder->ps_used[hDecoder->fr_ch_ele])
    00904         output_channels = 2;
    00905     else
    00906         output_channels = 1;
    00907 #endif
    00908
    00909     if (hDecoder->element_output_channels[hDecoder->fr_ch_ele] == 0)
    00910     {
    00911         /* element_output_channels not set yet */
    00912         hDecoder->element_output_channels[hDecoder->fr_ch_ele] = output_channels;
    00913     } else if (hDecoder->element_output_channels[hDecoder->fr_ch_ele] != output_channels) {
    00914         /* element inconsistency */
    00915
    00916         /* this only happens if PS is actually found but not in the first frame
    00917          * this means that there is only 1 bitstream element!
    00918          */
    00919
    00920         /* reset the allocation */
    00921         hDecoder->element_alloced[hDecoder->fr_ch_ele] = 0;
    00922
    00923         hDecoder->element_output_channels[hDecoder->fr_ch_ele] = output_channels;
    00924
    00925         //return 21;
    00926     }
    00927
    00928     if (hDecoder->element_alloced[hDecoder->fr_ch_ele] == 0)
    00929     {
    00930         retval = allocate_single_channel(hDecoder, sce->channel, output_channels);
    00931         if (retval > 0)
    00932             return retval;
    00933
    00934         hDecoder->element_alloced[hDecoder->fr_ch_ele] = 1;
    00935     }
    00936
    00937
    00938     /* dequantisation and scaling */
    00939     retval = quant_to_spec(hDecoder, ics, spec_data, spec_coef, hDecoder->frameLength);
    00940     if (retval > 0)
    00941         return retval;
    00942
    00943 #ifdef PROFILE
    00944     count = faad_get_ts() - count;
    00945     hDecoder->requant_cycles += count;
    00946 #endif
    00947
    00948
    00949     /* pns decoding */
    00950     pns_decode(ics, NULL, spec_coef, NULL, hDecoder->frameLength, 0, hDecoder->object_type);
    00951
    00952 #ifdef MAIN_DEC
    00953     /* MAIN object type prediction */
    00954     if (hDecoder->object_type == MAIN)
    00955     {
    00956         /* intra channel prediction */
    00957         ic_prediction(ics, spec_coef, hDecoder->pred_stat[sce->channel], hDecoder->frameLength,
    00958             hDecoder->sf_index);
    00959
    00960         /* In addition, for scalefactor bands coded by perceptual
    00961            noise substitution the predictors belonging to the
    00962            corresponding spectral coefficients are reset.
    00963         */
    00964         pns_reset_pred_state(ics, hDecoder->pred_stat[sce->channel]);
    00965     }
    00966 #endif
    00967
    00968 #ifdef LTP_DEC
    00969     if (is_ltp_ot(hDecoder->object_type))
    00970     {
    00971 #ifdef LD_DEC
    00972         if (hDecoder->object_type == LD)
    00973         {
    00974             if (ics->ltp.data_present)
    00975             {
    00976                 if (ics->ltp.lag_update)
    00977                     hDecoder->ltp_lag[sce->channel] = ics->ltp.lag;
    00978             }
    00979             ics->ltp.lag = hDecoder->ltp_lag[sce->channel];
    00980         }
    00981 #endif
    00982
    00983         /* long term prediction */
    00984         lt_prediction(ics, &(ics->ltp), spec_coef, hDecoder->lt_pred_stat[sce->channel], hDecoder->fb,
    00985             ics->window_shape, hDecoder->window_shape_prev[sce->channel],
    00986             hDecoder->sf_index, hDecoder->object_type, hDecoder->frameLength);
    00987     }
    00988 #endif
    00989
    00990     /* tns decoding */
    00991     tns_decode_frame(ics, &(ics->tns), hDecoder->sf_index, hDecoder->object_type,
    00992         spec_coef, hDecoder->frameLength);
    00993
    00994     /* drc decoding */
    00995     if (hDecoder->drc->present)
    00996     {
    00997         if (!hDecoder->drc->exclude_mask[sce->channel] || !hDecoder->drc->excluded_chns_present)
    00998             drc_decode(hDecoder->drc, spec_coef);
    00999     }
    01000
    01001     /* filter bank */
    01002 #ifdef SSR_DEC
    01003     if (hDecoder->object_type != SSR)
    01004     {
    01005 #endif
    01006         ifilter_bank(hDecoder->fb, ics->window_sequence, ics->window_shape,
    01007             hDecoder->window_shape_prev[sce->channel], spec_coef,
    01008             hDecoder->time_out[sce->channel], hDecoder->fb_intermed[sce->channel],
    01009             hDecoder->object_type, hDecoder->frameLength);
    01010 #ifdef SSR_DEC
    01011     } else {
    01012         ssr_decode(&(ics->ssr), hDecoder->fb, ics->window_sequence, ics->window_shape,
    01013             hDecoder->window_shape_prev[sce->channel], spec_coef, hDecoder->time_out[sce->channel],
    01014             hDecoder->ssr_overlap[sce->channel], hDecoder->ipqf_buffer[sce->channel], hDecoder->prev_fmd[sce->channel],
    01015             hDecoder->frameLength);
    01016     }
    01017 #endif
    01018
    01019     /* save window shape for next frame */
    01020     hDecoder->window_shape_prev[sce->channel] = ics->window_shape;
    01021
    01022 #ifdef LTP_DEC
    01023     if (is_ltp_ot(hDecoder->object_type))
    01024     {
    01025         lt_update_state(hDecoder->lt_pred_stat[sce->channel], hDecoder->time_out[sce->channel],
    01026             hDecoder->fb_intermed[sce->channel], hDecoder->frameLength, hDecoder->object_type);
    01027     }
    01028 #endif
    01029
    01030 #ifdef SBR_DEC
    01031     if (((hDecoder->sbr_present_flag == 1) || (hDecoder->forceUpSampling == 1))
    01032         && hDecoder->sbr_alloced[hDecoder->fr_ch_ele])
    01033     {
    01034         uint8_t ele = hDecoder->fr_ch_ele;
    01035         uint8_t ch = sce->channel;
    01036
    01037         /* following case can happen when forceUpSampling == 1 */
    01038         if (hDecoder->sbr[ele] == NULL)
    01039         {
    01040             hDecoder->sbr[ele] = sbrDecodeInit(hDecoder->frameLength,
    01041                 hDecoder->element_id[ele], 2*get_sample_rate(hDecoder->sf_index),
    01042                 hDecoder->downSampledSBR
    01043 #ifdef DRM
    01044                 , 0
    01045 #endif
    01046                 );
    01047         }
    01048
    01049         if (sce->ics1.window_sequence == EIGHT_SHORT_SEQUENCE)
    01050             hDecoder->sbr[ele]->maxAACLine = 8*sce->ics1.swb_offset[max(sce->ics1.max_sfb-1, 0)];
    01051         else
    01052             hDecoder->sbr[ele]->maxAACLine = sce->ics1.swb_offset[max(sce->ics1.max_sfb-1, 0)];
    01053
    01054         /* check if any of the PS tools is used */
    01055 #if (defined(PS_DEC) || defined(DRM_PS))
    01056         if (hDecoder->ps_used[ele] == 0)
    01057         {
    01058 #endif
    01059             retval = sbrDecodeSingleFrame(hDecoder->sbr[ele], hDecoder->time_out[ch],
    01060                 hDecoder->postSeekResetFlag, hDecoder->downSampledSBR);
    01061 #if (defined(PS_DEC) || defined(DRM_PS))
    01062         } else {
    01063             retval = sbrDecodeSingleFramePS(hDecoder->sbr[ele], hDecoder->time_out[ch],
    01064                 hDecoder->time_out[ch+1], hDecoder->postSeekResetFlag,
    01065                 hDecoder->downSampledSBR);
    01066         }
    01067 #endif
    01068         if (retval > 0)
    01069             return retval;
    01070     } else if (((hDecoder->sbr_present_flag == 1) || (hDecoder->forceUpSampling == 1))
    01071         && !hDecoder->sbr_alloced[hDecoder->fr_ch_ele])
    01072     {
    01073         return 23;
    01074     }
    01075 #endif
    01076
    01077     /* copy L to R when no PS is used */
    01078 #if (defined(PS_DEC) || defined(DRM_PS))
    01079     if ((hDecoder->ps_used[hDecoder->fr_ch_ele] == 0) &&
    01080         (hDecoder->element_output_channels[hDecoder->fr_ch_ele] == 2))
    01081     {
    01082         uint8_t ele = hDecoder->fr_ch_ele;
    01083         uint8_t ch = sce->channel;
    01084         uint16_t frame_size = (hDecoder->sbr_alloced[ele]) ? 2 : 1;
    01085         frame_size *= hDecoder->frameLength*sizeof(real_t);
    01086
    01087         memcpy(hDecoder->time_out[ch+1], hDecoder->time_out[ch], frame_size);
    01088     }
    01089 #endif
    01090
    01091     return 0;
    01092 }
    01093
    01094 uint8_t reconstruct_channel_pair(NeAACDecHandle hDecoder, ic_stream *ics1, ic_stream *ics2,
    01095                                  element *cpe, int16_t *spec_data1, int16_t *spec_data2)
    01096 {
    01097     uint8_t retval;
    01098     ALIGN real_t spec_coef1[1024];
    01099     ALIGN real_t spec_coef2[1024];
    01100
    01101 #ifdef PROFILE
    01102     int64_t count = faad_get_ts();
    01103 #endif
    01104     if (hDecoder->element_alloced[hDecoder->fr_ch_ele] == 0)
    01105     {
    01106         retval = allocate_channel_pair(hDecoder, cpe->channel, (uint8_t)cpe->paired_channel);
    01107         if (retval > 0)
    01108             return retval;
    01109
    01110         hDecoder->element_alloced[hDecoder->fr_ch_ele] = 1;
    01111     }
    01112
    01113     /* dequantisation and scaling */
    01114     retval = quant_to_spec(hDecoder, ics1, spec_data1, spec_coef1, hDecoder->frameLength);
    01115     if (retval > 0)
    01116         return retval;
    01117     retval = quant_to_spec(hDecoder, ics2, spec_data2, spec_coef2, hDecoder->frameLength);
    01118     if (retval > 0)
    01119         return retval;
    01120
    01121 #ifdef PROFILE
    01122     count = faad_get_ts() - count;
    01123     hDecoder->requant_cycles += count;
    01124 #endif
    01125
    01126
    01127     /* pns decoding */
    01128     if (ics1->ms_mask_present)
    01129     {
    01130         pns_decode(ics1, ics2, spec_coef1, spec_coef2, hDecoder->frameLength, 1, hDecoder->object_type);
    01131     } else {
    01132         pns_decode(ics1, NULL, spec_coef1, NULL, hDecoder->frameLength, 0, hDecoder->object_type);
    01133         pns_decode(ics2, NULL, spec_coef2, NULL, hDecoder->frameLength, 0, hDecoder->object_type);
    01134     }
    01135
    01136     /* mid/side decoding */
    01137     ms_decode(ics1, ics2, spec_coef1, spec_coef2, hDecoder->frameLength);
    01138
    01139 #if 0
    01140     {
    01141         int i;
    01142         for (i = 0; i < 1024; i++)
    01143         {
    01144             //printf("%d\n", spec_coef1[i]);
    01145             printf("0x%.8X\n", spec_coef1[i]);
    01146         }
    01147         for (i = 0; i < 1024; i++)
    01148         {
    01149             //printf("%d\n", spec_coef2[i]);
    01150             printf("0x%.8X\n", spec_coef2[i]);
    01151         }
    01152     }
    01153 #endif
    01154
    01155     /* intensity stereo decoding */
    01156     is_decode(ics1, ics2, spec_coef1, spec_coef2, hDecoder->frameLength);
    01157
    01158 #if 0
    01159     {
    01160         int i;
    01161         for (i = 0; i < 1024; i++)
    01162         {
    01163             printf("%d\n", spec_coef1[i]);
    01164             //printf("0x%.8X\n", spec_coef1[i]);
    01165         }
    01166         for (i = 0; i < 1024; i++)
    01167         {
    01168             printf("%d\n", spec_coef2[i]);
    01169             //printf("0x%.8X\n", spec_coef2[i]);
    01170         }
    01171     }
    01172 #endif
    01173
    01174 #ifdef MAIN_DEC
    01175     /* MAIN object type prediction */
    01176     if (hDecoder->object_type == MAIN)
    01177     {
    01178         /* intra channel prediction */
    01179         ic_prediction(ics1, spec_coef1, hDecoder->pred_stat[cpe->channel], hDecoder->frameLength,
    01180             hDecoder->sf_index);
    01181         ic_prediction(ics2, spec_coef2, hDecoder->pred_stat[cpe->paired_channel], hDecoder->frameLength,
    01182             hDecoder->sf_index);
    01183
    01184         /* In addition, for scalefactor bands coded by perceptual
    01185            noise substitution the predictors belonging to the
    01186            corresponding spectral coefficients are reset.
    01187         */
    01188         pns_reset_pred_state(ics1, hDecoder->pred_stat[cpe->channel]);
    01189         pns_reset_pred_state(ics2, hDecoder->pred_stat[cpe->paired_channel]);
    01190     }
    01191 #endif
    01192
    01193 #ifdef LTP_DEC
    01194     if (is_ltp_ot(hDecoder->object_type))
    01195     {
    01196         ltp_info *ltp1 = &(ics1->ltp);
    01197         ltp_info *ltp2 = (cpe->common_window) ? &(ics2->ltp2) : &(ics2->ltp);
    01198 #ifdef LD_DEC
    01199         if (hDecoder->object_type == LD)
    01200         {
    01201             if (ltp1->data_present)
    01202             {
    01203                 if (ltp1->lag_update)
    01204                     hDecoder->ltp_lag[cpe->channel] = ltp1->lag;
    01205             }
    01206             ltp1->lag = hDecoder->ltp_lag[cpe->channel];
    01207             if (ltp2->data_present)
    01208             {
    01209                 if (ltp2->lag_update)
    01210                     hDecoder->ltp_lag[cpe->paired_channel] = ltp2->lag;
    01211             }
    01212             ltp2->lag = hDecoder->ltp_lag[cpe->paired_channel];
    01213         }
    01214 #endif
    01215
    01216         /* long term prediction */
    01217         lt_prediction(ics1, ltp1, spec_coef1, hDecoder->lt_pred_stat[cpe->channel], hDecoder->fb,
    01218             ics1->window_shape, hDecoder->window_shape_prev[cpe->channel],
    01219             hDecoder->sf_index, hDecoder->object_type, hDecoder->frameLength);
    01220         lt_prediction(ics2, ltp2, spec_coef2, hDecoder->lt_pred_stat[cpe->paired_channel], hDecoder->fb,
    01221             ics2->window_shape, hDecoder->window_shape_prev[cpe->paired_channel],
    01222             hDecoder->sf_index, hDecoder->object_type, hDecoder->frameLength);
    01223     }
    01224 #endif
    01225
    01226     /* tns decoding */
    01227     tns_decode_frame(ics1, &(ics1->tns), hDecoder->sf_index, hDecoder->object_type,
    01228         spec_coef1, hDecoder->frameLength);
    01229     tns_decode_frame(ics2, &(ics2->tns), hDecoder->sf_index, hDecoder->object_type,
    01230         spec_coef2, hDecoder->frameLength);
    01231
    01232     /* drc decoding */
    01233     if (hDecoder->drc->present)
    01234     {
    01235         if (!hDecoder->drc->exclude_mask[cpe->channel] || !hDecoder->drc->excluded_chns_present)
    01236             drc_decode(hDecoder->drc, spec_coef1);
    01237         if (!hDecoder->drc->exclude_mask[cpe->paired_channel] || !hDecoder->drc->excluded_chns_present)
    01238             drc_decode(hDecoder->drc, spec_coef2);
    01239     }
    01240
    01241     /* filter bank */
    01242 #ifdef SSR_DEC
    01243     if (hDecoder->object_type != SSR)
    01244     {
    01245 #endif
    01246         ifilter_bank(hDecoder->fb, ics1->window_sequence, ics1->window_shape,
    01247             hDecoder->window_shape_prev[cpe->channel], spec_coef1,
    01248             hDecoder->time_out[cpe->channel], hDecoder->fb_intermed[cpe->channel],
    01249             hDecoder->object_type, hDecoder->frameLength);
    01250         ifilter_bank(hDecoder->fb, ics2->window_sequence, ics2->window_shape,
    01251             hDecoder->window_shape_prev[cpe->paired_channel], spec_coef2,
    01252             hDecoder->time_out[cpe->paired_channel], hDecoder->fb_intermed[cpe->paired_channel],
    01253             hDecoder->object_type, hDecoder->frameLength);
    01254 #ifdef SSR_DEC
    01255     } else {
    01256         ssr_decode(&(ics1->ssr), hDecoder->fb, ics1->window_sequence, ics1->window_shape,
    01257             hDecoder->window_shape_prev[cpe->channel], spec_coef1, hDecoder->time_out[cpe->channel],
    01258             hDecoder->ssr_overlap[cpe->channel], hDecoder->ipqf_buffer[cpe->channel],
    01259             hDecoder->prev_fmd[cpe->channel], hDecoder->frameLength);
    01260         ssr_decode(&(ics2->ssr), hDecoder->fb, ics2->window_sequence, ics2->window_shape,
    01261             hDecoder->window_shape_prev[cpe->paired_channel], spec_coef2, hDecoder->time_out[cpe->paired_channel],
    01262             hDecoder->ssr_overlap[cpe->paired_channel], hDecoder->ipqf_buffer[cpe->paired_channel],
    01263             hDecoder->prev_fmd[cpe->paired_channel], hDecoder->frameLength);
    01264     }
    01265 #endif
    01266
    01267     /* save window shape for next frame */
    01268     hDecoder->window_shape_prev[cpe->channel] = ics1->window_shape;
    01269     hDecoder->window_shape_prev[cpe->paired_channel] = ics2->window_shape;
    01270
    01271 #ifdef LTP_DEC
    01272     if (is_ltp_ot(hDecoder->object_type))
    01273     {
    01274         lt_update_state(hDecoder->lt_pred_stat[cpe->channel], hDecoder->time_out[cpe->channel],
    01275             hDecoder->fb_intermed[cpe->channel], hDecoder->frameLength, hDecoder->object_type);
    01276         lt_update_state(hDecoder->lt_pred_stat[cpe->paired_channel], hDecoder->time_out[cpe->paired_channel],
    01277             hDecoder->fb_intermed[cpe->paired_channel], hDecoder->frameLength, hDecoder->object_type);
    01278     }
    01279 #endif
    01280
    01281 #ifdef SBR_DEC
    01282     if (((hDecoder->sbr_present_flag == 1) || (hDecoder->forceUpSampling == 1))
    01283         && hDecoder->sbr_alloced[hDecoder->fr_ch_ele])
    01284     {
    01285         uint8_t ele = hDecoder->fr_ch_ele;
    01286         uint8_t ch0 = cpe->channel;
    01287         uint8_t ch1 = cpe->paired_channel;
    01288
    01289         /* following case can happen when forceUpSampling == 1 */
    01290         if (hDecoder->sbr[ele] == NULL)
    01291         {
    01292             hDecoder->sbr[ele] = sbrDecodeInit(hDecoder->frameLength,
    01293                 hDecoder->element_id[ele], 2*get_sample_rate(hDecoder->sf_index),
    01294                 hDecoder->downSampledSBR
    01295 #ifdef DRM
    01296                 , 0
    01297 #endif
    01298                 );
    01299         }
    01300
    01301         if (cpe->ics1.window_sequence == EIGHT_SHORT_SEQUENCE)
    01302             hDecoder->sbr[ele]->maxAACLine = 8*cpe->ics1.swb_offset[max(cpe->ics1.max_sfb-1, 0)];
    01303         else
    01304             hDecoder->sbr[ele]->maxAACLine = cpe->ics1.swb_offset[max(cpe->ics1.max_sfb-1, 0)];
    01305
    01306         retval = sbrDecodeCoupleFrame(hDecoder->sbr[ele],
    01307             hDecoder->time_out[ch0], hDecoder->time_out[ch1],
    01308             hDecoder->postSeekResetFlag, hDecoder->downSampledSBR);
    01309         if (retval > 0)
    01310             return retval;
    01311     } else if (((hDecoder->sbr_present_flag == 1) || (hDecoder->forceUpSampling == 1))
    01312         && !hDecoder->sbr_alloced[hDecoder->fr_ch_ele])
    01313     {
    01314         return 23;
    01315     }
    01316 #endif
    01317
    01318     return 0;
    01319 }