Home | History | Annotate | Download | only in aacdec
      1 /* ------------------------------------------------------------------
      2  * Copyright (C) 1998-2009 PacketVideo
      3  *
      4  * Licensed under the Apache License, Version 2.0 (the "License");
      5  * you may not use this file except in compliance with the License.
      6  * You may obtain a copy of the License at
      7  *
      8  *      http://www.apache.org/licenses/LICENSE-2.0
      9  *
     10  * Unless required by applicable law or agreed to in writing, software
     11  * distributed under the License is distributed on an "AS IS" BASIS,
     12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either
     13  * express or implied.
     14  * See the License for the specific language governing permissions
     15  * and limitations under the License.
     16  * -------------------------------------------------------------------
     17  */
     18 /*
     19 
     20  Pathname: get_tns.c
     21 
     22      Date: 10/25/2000
     23 
     24 ------------------------------------------------------------------------------
     25  REVISION HISTORY
     26 
     27  Description:  Modified from original shareware code
     28 
     29  Description:  Modified to pass variables by reference to eliminate use
     30                of global variables.
     31 
     32  Description:  Brought code in-line with PV standards.  Some minor
     33                optimizations (count-down for loops, etc.) were made.
     34 
     35  Description:  Made cosmetic changes as suggested during review.  Also,
     36  changed calculation of s_mask and n_mask from table-based to being
     37  calculated based on res_index.  Also, the flag coef_res was changed
     38  from having a range of [3,4] to having a range of [0,1], which corresponds
     39  exactly with the true value that is passed via the bitstream.
     40 
     41  Description:  Modified to use more efficient TNS memory structure.
     42 
     43  Description: Updated to reflect more efficient usage of memory by the TNS
     44  filters.
     45 
     46  Description: Updated the SW template to include the full pathname to the
     47  source file and a slightly modified copyright header.
     48 
     49  Description: Moved pInputStream to be the 2nd parameter, for a slight
     50  optimization on some platforms.
     51 
     52  Description: Moved pSfbTop outside of the loops, since its value does
     53  not change.
     54 
     55  Description: Replace some instances of getbits to get1bits
     56               when only 1 bit is read.
     57 
     58  Who:                       Date:
     59  Description:
     60 
     61 ------------------------------------------------------------------------------
     62  INPUT AND OUTPUT DEFINITIONS
     63 
     64  Inputs:
     65     FrameInfo *pFrameInfo
     66         Pointer to structure that holds information about each block.
     67         (long block flag,
     68          number of subblocks,
     69          scalefactor bands per subblock, etc.)
     70 
     71     BITS *pInputStream
     72         Pointer to a BITS structure that is
     73         passed on to function getbits to pull information from the bitstream.
     74 
     75     TNS_Frame_info *pTnsFrameInfo
     76         Pointer to filter data structure - to be populated by this function.
     77 
     78  Local Stores/Buffers/Pointers Needed:
     79     None
     80 
     81  Global Stores/Buffers/Pointers Needed:
     82     None
     83 
     84  Outputs:
     85     None
     86 
     87  Pointers and Buffers Modified:
     88     TNS_frame_info *pTnsFrameInfo
     89 
     90     pTnsFrameInfo->n_filt = Number of tns filters to be applied to the data.
     91 
     92     pTnsFrameInfo->filt[]->order = The order of each individual TNS filter.
     93 
     94     pTnsFrameInfo->filt[]->coef_res = The resolution of the filter coefficients
     95 
     96     pTnsFrameInfo->filt[]->start_band = start of spectral band
     97 
     98     pTnsFrameInfo->filt[]->stop_band = end of spectral band
     99 
    100     pTnsFrameInfo->filt[]->coef[] = Each filter's coefficients are filled with
    101     data read from the input bitstream.
    102 
    103     pTnsFrameInfo->filt[]->direction = A flag is set for each TNS filter.
    104 
    105     If the direction flag (on the bitstream) = 0, then the filter
    106     is applied to the block of spectral data in normal (upward) fashion.
    107 
    108     If the direction flag (on the bitstream) = 1, then the filter
    109     is applied in a reverse (downward) fashion.
    110     (Starting with the last element in the block of data.)
    111 
    112     The value stored in filt[]->direction maps the values [0,1] to [1,-1] for
    113     a more intuitive storage of this flag's meaning.
    114 
    115  Local Stores Modified:
    116 
    117  Global Stores Modified:
    118 
    119 
    120 ------------------------------------------------------------------------------
    121  FUNCTION DESCRIPTION
    122 
    123  This function reads the TNS filter information from the bitstream, and stores
    124  the filter order, LPC coefficients, and the number of TNS filters to
    125  be applied in the structure TNS_frame_info.
    126 
    127 ------------------------------------------------------------------------------
    128  REQUIREMENTS
    129 
    130  This code should match the ISO code in functionality, with the exception
    131  that coef_res has range of [0,1] (PV code) instead of [3,4] (ISO code)
    132 
    133  coef_res is only used by tns_decode_coef.
    134 
    135 ------------------------------------------------------------------------------
    136  REFERENCES
    137  (1) ISO/IEC 14496-3:1999(E)
    138      Part 3
    139         Subpart 4.6.8 (Temporal Noise Shaping)
    140 
    141  (2) MPEG-2 NBC Audio Decoder
    142    "This software module was originally developed by AT&T, Dolby
    143    Laboratories, Fraunhofer Gesellschaft IIS in the course of development
    144    of the MPEG-2 NBC/MPEG-4 Audio standard ISO/IEC 13818-7, 14496-1,2 and
    145    3. This software module is an implementation of a part of one or more
    146    MPEG-2 NBC/MPEG-4 Audio tools as specified by the MPEG-2 NBC/MPEG-4
    147    Audio standard. ISO/IEC  gives users of the MPEG-2 NBC/MPEG-4 Audio
    148    standards free license to this software module or modifications thereof
    149    for use in hardware or software products claiming conformance to the
    150    MPEG-2 NBC/MPEG-4 Audio  standards. Those intending to use this software
    151    module in hardware or software products are advised that this use may
    152    infringe existing patents. The original developer of this software
    153    module and his/her company, the subsequent editors and their companies,
    154    and ISO/IEC have no liability for use of this software module or
    155    modifications thereof in an implementation. Copyright is not released
    156    for non MPEG-2 NBC/MPEG-4 Audio conforming products.The original
    157    developer retains full right to use the code for his/her own purpose,
    158    assign or donate the code to a third party and to inhibit third party
    159    from using the code for non MPEG-2 NBC/MPEG-4 Audio conforming products.
    160    This copyright notice must be included in all copies or derivative
    161    works."
    162    Copyright(c)1996.
    163 
    164 ------------------------------------------------------------------------------
    165  PSEUDO-CODE
    166 
    167 
    168 ------------------------------------------------------------------------------
    169  RESOURCES USED
    170    When the code is written for a specific target processor
    171      the resources used should be documented below.
    172 
    173  STACK USAGE: [stack count for this module] + [variable to represent
    174           stack usage for each subroutine called]
    175 
    176      where: [stack usage variable] = stack usage for [subroutine
    177          name] (see [filename].ext)
    178 
    179  DATA MEMORY USED: x words
    180 
    181  PROGRAM MEMORY USED: x words
    182 
    183  CLOCK CYCLES: [cycle count equation for this module] + [variable
    184            used to represent cycle count for each subroutine
    185            called]
    186 
    187      where: [cycle count variable] = cycle count for [subroutine
    188         name] (see [filename].ext)
    189 
    190 ----------------------------------------------------------------------------*/
    191 /*----------------------------------------------------------------------------
    192 ; INCLUDES
    193 ----------------------------------------------------------------------------*/
    194 #include "pv_audio_type_defs.h"
    195 #include "get_tns.h"
    196 #include "s_mc_info.h"
    197 #include "s_frameinfo.h"
    198 #include "s_tnsfilt.h"
    199 #include "s_tns_frame_info.h"
    200 #include "s_bits.h"
    201 #include "ibstream.h"
    202 #include "e_window_sequence.h"
    203 #include "e_progconfigconst.h"
    204 
    205 #include "tns_decode_coef.h"
    206 
    207 
    208 /*----------------------------------------------------------------------------
    209 ; MACROS
    210 ; Define module specific macros here
    211 ----------------------------------------------------------------------------*/
    212 #define SCALE_FACTOR_BAND_OFFSET(x) ( ((x) > 0) ? pSFB_top[(x)-1] : 0 )
    213 #define MINIMUM(x,y) ( ((x) < (y)) ? (x) : (y) )
    214 
    215 /*----------------------------------------------------------------------------
    216 ; DEFINES
    217 ; Include all pre-processor statements here. Include conditional
    218 ; compile variables also.
    219 ----------------------------------------------------------------------------*/
    220 /*----------------------------------------------------------------------------
    221 ; LOCAL FUNCTION DEFINITIONS
    222 ; Function Prototype declaration
    223 ----------------------------------------------------------------------------*/
    224 /*----------------------------------------------------------------------------
    225 ; LOCAL STORE/BUFFER/POINTER DEFINITIONS
    226 ; Variable declaration - defined here and used outside this module
    227 ----------------------------------------------------------------------------*/
    228 /*
    229  * The entries in the ensuing tables provide the maximum permissable
    230  * number of scalefactor bands for each TNS filter.  This value is effected
    231  * by the sampling rate, and window type.
    232  */
    233 
    234 const Int tns_max_bands_tbl_long_wndw[(1<<LEN_SAMP_IDX)] =
    235     {31,       /* 96000 Hz */
    236      31,       /* 88200 Hz */
    237      34,       /* 64000 Hz */
    238      40,       /* 48000 Hz */
    239      42,       /* 44100 Hz */
    240      51,       /* 32000 Hz */
    241      46,       /* 24000 Hz */
    242      46,       /* 22050 Hz */
    243      42,       /* 16000 Hz */
    244      42,       /* 12000 Hz */
    245      42,       /* 11025 Hz */
    246      39,       /* 8000  Hz */
    247      0,
    248      0,
    249      0,
    250      0
    251     };
    252 
    253 const Int tns_max_bands_tbl_short_wndw[(1<<LEN_SAMP_IDX)] =
    254     {9,       /* 96000 Hz */
    255      9,       /* 88200 Hz */
    256      10,       /* 64000 Hz */
    257      14,       /* 48000 Hz */
    258      14,       /* 44100 Hz */
    259      14,       /* 32000 Hz */
    260      14,       /* 24000 Hz */
    261      14,       /* 22050 Hz */
    262      14,       /* 16000 Hz */
    263      14,       /* 12000 Hz */
    264      14,       /* 11025 Hz */
    265      14,       /* 8000  Hz */
    266      0,
    267      0,
    268      0,
    269      0
    270     };
    271 
    272 /*
    273  * For completeness, here are the table entries for object types that make
    274  * use of PQF filter bank.  We do not currently support this; these are
    275  * given here only to ease future implementation.
    276  *
    277  *  const Int tns_max_bands_tbl_long_wndw_PQF[(1<<LEN_SAMP_IDX)] =
    278  *         {28,       ; 96000
    279  *          28,       ; 88200
    280  *          27,       ; 64000
    281  *          26,       ; 48000
    282  *          26,       ; 44100
    283  *          26,       ; 32000
    284  *          29,       ; 24000
    285  *          29,       ; 22050
    286  *          23,       ; 16000
    287  *          23,       ; 12000
    288  *          23,       ; 11025
    289  *          19,       ; 8000
    290  *           0,
    291  *           0,
    292  *           0,
    293  *           0};
    294  *
    295  *  const Int tns_max_bands_tbl_short_wndw_PQF[(1<<LEN_SAMP_IDX)] =
    296  *         {7,       ; 96000
    297  *          7,       ; 88200
    298  *          7,       ; 64000
    299  *          6,       ; 48000
    300  *          6,       ; 44100
    301  *          6,       ; 32000
    302  *          7,       ; 24000
    303  *          7,       ; 22050
    304  *          8,       ; 16000
    305  *          8,       ; 12000
    306  *          8,       ; 11025
    307  *          7,       ; 8000
    308  *          0,
    309  *          0,
    310  *          0,
    311  *          0};
    312  */
    313 
    314 /*----------------------------------------------------------------------------
    315 ; EXTERNAL FUNCTION REFERENCES
    316 ; Declare functions defined elsewhere and referenced in this module
    317 ----------------------------------------------------------------------------*/
    318 /*----------------------------------------------------------------------------
    319 ; EXTERNAL GLOBAL STORE/BUFFER/POINTER REFERENCES
    320 ; Declare variables used in this module but defined elsewhere
    321 ----------------------------------------------------------------------------*/
    322 /*----------------------------------------------------------------------------
    323 ; FUNCTION CODE
    324 ----------------------------------------------------------------------------*/
    325 
    326 
    327 void get_tns(
    328     const Int               max_bands,
    329     BITS            * const pInputStream,
    330     const WINDOW_SEQUENCE   wnd_seq,
    331     const FrameInfo * const pFrameInfo,
    332     const MC_Info   * const pMC_Info,
    333     TNS_frame_info  * const pTnsFrameInfo,
    334     Int32                   scratchTnsDecCoefMem[])
    335 {
    336 
    337     const Int16 * const pSFB_top = pFrameInfo->win_sfb_top[0];
    338 
    339     Int f;
    340     Int t;
    341     Int win;
    342     UInt tempInt;
    343 
    344     Int num_filt_bits;
    345     Int num_order_bits;
    346     Int num_start_band_bits;
    347 
    348     Int top;
    349     Int res;
    350     Int res_index;
    351     Int compress;
    352 
    353     Int sfb_per_win;
    354 
    355     Int32 *pLpcCoef;
    356     Int32 *pStartLpcCoef;
    357     Int s_mask;
    358     Int n_mask;
    359 
    360     Int tns_bands;
    361     UInt max_order;
    362     Int coef_res;
    363 
    364 
    365     TNSfilt *pFilt;
    366 
    367     if (wnd_seq != EIGHT_SHORT_SEQUENCE)
    368     {
    369         num_filt_bits  = 2;
    370         num_order_bits = 5;
    371         num_start_band_bits = 6;
    372 
    373         tns_bands = tns_max_bands_tbl_long_wndw[pMC_Info->sampling_rate_idx];
    374 
    375         /*
    376          *  Definition from 14496-3:1999 doc. Our first encoder follows this rule,
    377          *  later encoders don't
    378          */
    379 
    380         if (pMC_Info->sampling_rate_idx > 4)  /* if (sampling_rate <= 32000 */
    381         {
    382             max_order = 20;
    383         }
    384         else
    385         {
    386             max_order = 12;
    387         }
    388     }
    389     else
    390     {
    391         num_filt_bits  = 1;
    392         num_order_bits = 3;
    393         num_start_band_bits = 4;
    394 
    395         tns_bands = tns_max_bands_tbl_short_wndw[pMC_Info->sampling_rate_idx];
    396 
    397         max_order = 7;
    398     }
    399 
    400     /*
    401      * After this branch, tns_bands will be equal to the minimum of
    402      * the passed in variable, nbands, and the result from the
    403      * tns_max_bands_tbl
    404      */
    405 
    406     if (max_bands < tns_bands)
    407     {
    408         tns_bands = max_bands;
    409     }
    410 
    411     sfb_per_win = pFrameInfo->sfb_per_win[0];
    412 
    413     win = 0;
    414 
    415     pLpcCoef = pTnsFrameInfo->lpc_coef;
    416 
    417     pFilt = pTnsFrameInfo->filt;
    418 
    419     do
    420     {
    421         tempInt = get9_n_lessbits(num_filt_bits,
    422                                   pInputStream);
    423 
    424         pTnsFrameInfo->n_filt[win] = tempInt;
    425 
    426         if (tempInt != 0)
    427         {
    428             /*
    429              * coef_res = [0, 1]
    430              * Switch between a resolution of 3 and 4 bits respectively
    431              *
    432              * if coef_res = 0, the coefficients have a range of
    433              *
    434              *                 -4  -3  -2  -1  0   1   2   3
    435              *
    436              * if coef_res = 1, the coefficients have a range of
    437              *
    438              * -8  -7  -6  -5  -4  -3  -2  -1  0   1   2   3   4   5   6   7
    439              *
    440              * The arrays in ./src/tns_tab.c are completely based on
    441              * the value of coef_res.
    442              */
    443             res = get1bits(
    444                       pInputStream);
    445 
    446             /* res is post-incremented for correct calculation of res_index */
    447             coef_res = res++;
    448 
    449             top = sfb_per_win;
    450 
    451             for (f = pTnsFrameInfo->n_filt[win]; f > 0; f--)
    452             {
    453                 tempInt = MINIMUM(top, tns_bands);
    454 
    455                 pFilt->stop_coef = SCALE_FACTOR_BAND_OFFSET(tempInt);
    456 
    457                 pFilt->stop_band = tempInt;
    458 
    459                 top -= get9_n_lessbits(num_start_band_bits,
    460                                        pInputStream);
    461 
    462                 tempInt = MINIMUM(top, tns_bands);
    463 
    464                 pFilt->start_coef = SCALE_FACTOR_BAND_OFFSET(tempInt);
    465 
    466                 pFilt->start_band = tempInt;
    467 
    468                 tempInt = get9_n_lessbits(num_order_bits,
    469                                           pInputStream);
    470 
    471                 pFilt->order = tempInt;
    472 
    473                 if (tempInt != 0)
    474                 {
    475                     if (tempInt > max_order)
    476                     {
    477                         pFilt->order = max_order;
    478                     }
    479 
    480                     /*
    481                      * This maps the bitstream's [0,1] to
    482                      * pFilt->direction = [1,-1]
    483                      */
    484 
    485                     tempInt = get1bits(pInputStream);
    486 
    487                     pFilt->direction = (-(Int)tempInt) | 0x1;
    488 
    489                     /*
    490                      * compress = [0,1]
    491                      * If compress is true, the MSB has
    492                      * been omitted from transmission (Ref. 1)
    493                      *
    494                      * For coef_res = 0, this limits the range of
    495                      * transmitted coefficients to...
    496                      *
    497                      *         -2  -1  0   1
    498                      *
    499                      * For coef_res = 1, the coefficients have
    500                      * a range of...
    501                      *
    502                      * -4  -3  -2  -1  0   1   2   3
    503                      */
    504                     compress = get1bits(pInputStream);
    505 
    506                     /*
    507                      * res has a range of [1,2]
    508                      * compress has a range of [0,1]
    509                      * So (res - compress) has range [0,2];
    510                      */
    511                     res_index = res - compress;
    512 
    513                     s_mask =  2 << res_index;
    514 
    515                     /*
    516                      * If res_index = 0, grab 2 bits of data
    517                      * If res_index = 1, grab 3 bits of data
    518                      * If res_index = 2, grab 4 bits of data
    519                      */
    520                     res_index += 2;
    521 
    522                     pStartLpcCoef = pLpcCoef;
    523 
    524                     for (t = pFilt->order; t > 0; t--)
    525                     {
    526                         /*
    527                          * These are the encoded coefficients, which will
    528                          * later be decoded into LPC coefficients by
    529                          * the function tns_decode_coef()
    530                          */
    531                         tempInt = get9_n_lessbits(res_index,
    532                                                   pInputStream);
    533 
    534                         n_mask  = -((Int)tempInt & s_mask);
    535 
    536                         /*
    537                          * n_mask is used to sign_extend the
    538                          * value, if it is negative.
    539                          *
    540                          */
    541                         *(pLpcCoef++) = tempInt | n_mask;
    542                     }
    543 
    544                     /* Decode the TNS coefficients */
    545 
    546                     tempInt = pFilt->stop_coef - pFilt->start_coef;
    547 
    548                     if (tempInt > 0)
    549                     {
    550                         pFilt->q_lpc =
    551                             tns_decode_coef(
    552                                 pFilt->order,
    553                                 coef_res,
    554                                 pStartLpcCoef,
    555                                 scratchTnsDecCoefMem);
    556                     }
    557 
    558                 } /* if (pTnsFilt->order != 0) */
    559 
    560                 pFilt++;
    561 
    562             } /* END for (f=pTnsInfo->n_filt; f>0; f--, pTnsFilt++) */
    563 
    564         } /* if (pTnsInfo->n_filt != 0) */
    565 
    566         win++;
    567 
    568     }
    569     while (win < pFrameInfo->num_win);
    570 
    571     return;
    572 
    573 } /* get_tns */
    574