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: lt_decode.c
21
22
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:  First round of optimizations.
33
34 Description:  pInputStream is now the 2nd parameter to this function.
35
36 Description:  Changed to work with MT's new get_ics_info.c function, which
37 only calls lt_decode if LTP is enabled.  This removes one grab from the
38 bitstream and one "if" from this code.  Also, changed setting of weight.
39 Now, rather than setting the actual weight, I only set the index into
40 a table in this function.
41
42 Description: Replace some instances of getbits to get9_n_lessbits
43              when the number of bits read is 9 or less and get1bits
44              when only 1 bit is read.
45
46 Who:                                   Date:
47 Description:
48------------------------------------------------------------------------------
49 INPUT AND OUTPUT DEFINITIONS
50
51 Inputs:
52    win_type        Type of window (SHORT or LONG)
53                    [WINDOW_TYPE]
54
55    max_sfb         Maximum number of active scalefactor bands
56                    [Int]
57
58    pLt_pred        Pointer to structure containing information for
59                    long-term prediction.
60                    [LT_PRED_STATUS *]
61
62    pInputStream    Pointer to structure containing bitstream
63                    information.
64                    [BITS *]
65
66 Local Stores/Buffers/Pointers Needed:
67    None
68
69 Global Stores/Buffers/Pointers Needed:
70    None
71
72 Outputs:
73    None
74
75 Pointers and Buffers Modified:
76    pLt_pred->weight_index - updated with index into weight table for LTP.
77
78 Local Stores Modified:
79    None
80
81 Global Stores Modified:
82    None
83
84------------------------------------------------------------------------------
85 FUNCTION DESCRIPTION
86
87 This function decodes the bitstream elements for long term prediction
88
89------------------------------------------------------------------------------
90 REQUIREMENTS
91
92
93------------------------------------------------------------------------------
94 REFERENCES
95
96 (1) MPEG-2 NBC Audio Decoder
97   "This software module was originally developed by Nokia
98   in the course of development of the MPEG-2 AAC/MPEG-4 Audio standard
99   ISO/IEC13818-7, 14496-1, 2 and 3.  This software module is an implementation
100   of a part of one or more MPEG-2 AAC/MPEG-4 Audio tools as specified by the
101   MPEG-2 aac/MPEG-4 Audio standard. ISO/IEC  gives users of the
102   MPEG-2aac/MPEG-4 Audio standards free license to this software module or
103   modifications thereof for use in hardware or software products claiming
104   conformance to the MPEG-2 aac/MPEG-4 Audio  standards. Those intending to
105   use this software module in hardware or software products are advised that
106   this use may infringe existing patents. The original developer of this
107   software module, the subsequent editors and their companies, and ISO/IEC
108   have no liability for use of this software module or modifications thereof
109   in an implementation. Copyright is not released for non MPEG-2 aac/MPEG-4
110   Audio conforming products. The original developer retains full right to use
111   the code for the developer's own purpose, assign or donate the code to a
112   third party and to inhibit third party from using the code for non
113   MPEG-2 aac/MPEG-4 Audio conforming products. This copyright notice
114   must be included in all copies or derivative works."
115   Copyright (c)1997.
116
117------------------------------------------------------------------------------
118 PSEUDO-CODE
119
120    pDelay[0] = (Int) getbits(
121                        LEN_LTP_LAG,
122                        pInputStream);
123
124    temp_reg  = (Int) getbits(
125                        LEN_LTP_COEF,
126                        pInputStream);
127
128    pLt_pred->weight = codebook[temp_reg];
129
130    last_band = max_sfb;
131
132    IF (win_type != EIGHT_SHORT_SEQUENCE)
133
134        IF (last_band > MAX_LT_PRED_LONG_SFB)
135
136            last_band = MAX_LT_PRED_LONG_SFB;
137
138        ENDIF
139
140        FOR (m = last_band; m > 0; m--)
141
142            *(pSfbPredictionUsed++) = (Int) getbits(
143                                               LEN_LTP_LONG_USED,
144                                               pInputStream);
145        ENDFOR
146
147        FOR (m = (max_sfb - last_band); m > 0; m--)
148
149            *(pSfbPredictionUsed++) = 0;
150
151        ENDFOR
152
153    ELSE
154
155        IF (last_band > MAX_LT_PRED_SHORT_SFB)
156
157            last_band = MAX_LT_PRED_SHORT_SFB;
158
159        ENDIF
160
161        prev_subblock = pDelay[0];
162
163        pWinPredictionUsed++;
164
165        pTempPtr = &pSfbPredictionUsed[0];
166
167        FOR (m = NUM_SHORT_WINDOWS; m > 0;)
168
169            m--;
170            temp_reg = (Int) getbits(
171                                LEN_LTP_SHORT_USED,
172                                pInputStream);
173
174            *(pWinPredictionUsed++) = temp_reg;
175
176            IF (temp_reg != FALSE)
177            {
178                *(pDelay++) = prev_subblock;
179
180                FOR (k = last_band; k > 0; k--)
181                {
182                    *(pTempPtr++) = 1;
183                }
184                break;
185            ELSE
186            {
187                pDelay++;
188                pTempPtr += last_band;
189            }
190
191        ENDFOR (m = NUM_SHORT_WINDOWS; m > 0;)
192
193        prev_subblock += LTP_LAG_OFFSET;
194
195        FOR (; m > 0; m--)
196
197            temp_reg = (Int) getbits (
198                                LEN_LTP_SHORT_USED,
199                                pInputStream);
200
201            *(pWinPredictionUsed++) = temp_reg;
202
203            IF (temp_reg != FALSE)
204
205                temp_reg = (Int) getbits(
206                                    LEN_LTP_SHORT_LAG_PRESENT,
207                                    pInputStream);
208                IF (temp_reg != 0)
209
210                    temp_reg  = (Int) getbits(
211                                         LEN_LTP_SHORT_LAG,
212                                         pInputStream);
213
214                    *(pDelay++) = prev_subblock - temp_reg;
215
216                ELSE
217
218                    *(pDelay++) = prev_subblock - LTP_LAG_OFFSET;
219
220                ENDIF
221
222                FOR (k = last_band; k > 0; k--)
223                    *(pTempPtr++) = 1;
224                ENDFOR
225
226            ELSE
227
228                pDelay++;
229                pTempPtr += last_band;
230
231            ENDIF
232
233        ENDFOR (; m > 0; m--)
234
235    ENDIF (win_type != EIGHT_SHORT_SEQUENCE)
236
237------------------------------------------------------------------------------
238 RESOURCES USED
239   When the code is written for a specific target processor the
240     the resources used should be documented below.
241
242 STACK USAGE: [stack count for this module] + [variable to represent
243          stack usage for each subroutine called]
244
245     where: [stack usage variable] = stack usage for [subroutine
246         name] (see [filename].ext)
247
248 DATA MEMORY USED: x words
249
250 PROGRAM MEMORY USED: x words
251
252 CLOCK CYCLES: [cycle count equation for this module] + [variable
253           used to represent cycle count for each subroutine
254           called]
255
256     where: [cycle count variable] = cycle count for [subroutine
257        name] (see [filename].ext)
258
259------------------------------------------------------------------------------
260*/
261
262/*----------------------------------------------------------------------------
263; INCLUDES
264----------------------------------------------------------------------------*/
265#include "pv_audio_type_defs.h"
266#include "lt_decode.h"
267#include "ltp_common_internal.h"
268#include "window_block_fxp.h"
269#include "e_window_sequence.h"
270#include "s_lt_pred_status.h"
271#include "s_bits.h"
272#include "ibstream.h"
273
274/*----------------------------------------------------------------------------
275; MACROS
276; Define module specific macros here
277----------------------------------------------------------------------------*/
278
279/*----------------------------------------------------------------------------
280; DEFINES
281; Include all pre-processor statements here. Include conditional
282; compile variables also.
283----------------------------------------------------------------------------*/
284
285/*----------------------------------------------------------------------------
286; LOCAL FUNCTION DEFINITIONS
287; Function Prototype declaration
288----------------------------------------------------------------------------*/
289
290/*----------------------------------------------------------------------------
291; LOCAL STORE/BUFFER/POINTER DEFINITIONS
292; Variable declaration - defined here and used outside this module
293----------------------------------------------------------------------------*/
294
295/*----------------------------------------------------------------------------
296; EXTERNAL FUNCTION REFERENCES
297; Declare functions defined elsewhere and referenced in this module
298----------------------------------------------------------------------------*/
299
300/*----------------------------------------------------------------------------
301; EXTERNAL GLOBAL STORE/BUFFER/POINTER REFERENCES
302; Declare variables used in this module but defined elsewhere
303----------------------------------------------------------------------------*/
304
305/*----------------------------------------------------------------------------
306; FUNCTION CODE
307----------------------------------------------------------------------------*/
308void lt_decode(
309    const WINDOW_SEQUENCE  win_type,
310    BITS            *pInputStream,
311    const Int              max_sfb,
312    LT_PRED_STATUS  *pLt_pred)
313{
314    Int wnd_num;
315    Int k;
316    Int last_band;
317    Int prev_subblock;
318    Int prev_subblock_nonzero;
319    Int temp_reg;
320
321    Bool *pWinPredictionUsed = pLt_pred->win_prediction_used;
322    Bool *pSfbPredictionUsed = pLt_pred->sfb_prediction_used;
323    Int  *pTempPtr;
324    Int  *pDelay = pLt_pred->delay;
325
326    pDelay[0] = (Int) get17_n_lessbits(
327                    LEN_LTP_LAG,  /* 11 bits */
328                    pInputStream);
329
330    pLt_pred->weight_index  = (Int) get9_n_lessbits(
331                                  LEN_LTP_COEF, /*  3 bits */
332                                  pInputStream);
333
334    last_band = max_sfb;
335
336    if (win_type != EIGHT_SHORT_SEQUENCE)
337    {
338
339        /* last_band = min(MAX_LT_PRED_LONG_SFB, max_sfb) MAX_SCFAC_BANDS */
340        if (last_band > MAX_LT_PRED_LONG_SFB)
341        {
342            last_band = MAX_LT_PRED_LONG_SFB;
343        }
344
345        for (k = last_band; k > 0; k--)
346        {
347            *(pSfbPredictionUsed++) = (Int) get1bits(pInputStream);
348        }
349
350        /*
351         * This is not a call to memset, because
352         * (max_sfb - last_band) should typically be a small value.
353         */
354        for (k = (max_sfb - last_band); k > 0; k--)
355        {
356            *(pSfbPredictionUsed++) = FALSE;
357        }
358    }
359    else /* (win_type == EIGHT_SHORT_SEQUENCE) */
360    {
361        /* last_band = min(MAX_LT_PRED_SHORT_SFB, max_sfb) */
362
363        if (last_band > MAX_LT_PRED_SHORT_SFB)
364        {
365            last_band = MAX_LT_PRED_SHORT_SFB;
366        }
367
368        /*
369         * The following two coding constructs are equivalent...
370         *
371         *  first_time == 1
372         *  for (wnd_num=NUM_SHORT_WINDOWS; wnd_num > 0; wnd_num--)
373         *  {
374         *     if (condition)
375         *     {
376         *       if (first_time == 1)
377         *       {
378         *           CODE SECTION A
379         *           first_time = 0;
380         *       }
381         *       else
382         *       {
383         *           CODE SECTION B
384         *       }
385         *     }
386         *  }
387         *
388         * -----------------------------------EQUIVALENT TO------------
389         *
390         *  wnd_num=NUM_SHORT_WINDOWS;
391         *
392         *  do
393         *  {
394         *     wnd_num--;
395         *     if (condition)
396         *     {
397         *         CODE SECTION A
398         *         break;
399         *     }
400         *  } while( wnd_num > 0)
401         *
402         *  while (wnd_num > 0)
403         *  {
404         *     if (condition)
405         *     {
406         *         CODE SECTION B
407         *     }
408         *     wnd_num--;
409         *  }
410         *
411         */
412
413        prev_subblock = pDelay[0];
414
415        pTempPtr = &pSfbPredictionUsed[0];
416
417        wnd_num = NUM_SHORT_WINDOWS;
418
419        prev_subblock_nonzero = prev_subblock;
420        prev_subblock += LTP_LAG_OFFSET;
421
422        do
423        {
424            /*
425             * Place decrement of wnd_num here, to insure
426             * that the decrement occurs before the
427             * break out of the do-while loop.
428             */
429            wnd_num--;
430
431            temp_reg = (Int) get1bits(pInputStream);
432
433            *(pWinPredictionUsed++) = temp_reg;
434
435            if (temp_reg != FALSE)
436            {
437                *(pDelay++) = prev_subblock_nonzero;
438
439                for (k = last_band; k > 0; k--)
440                {
441                    *(pTempPtr++) = TRUE;
442                }
443                for (k = (max_sfb - last_band); k > 0; k--)
444                {
445                    *(pTempPtr++) = FALSE;
446                }
447                break;
448
449            } /* if(pWinPredictionUsed) */
450            else
451            {
452                pDelay++;
453                pTempPtr += max_sfb;
454            }
455
456        }
457        while (wnd_num > 0);
458
459        /*
460         * This while loop picks up where the previous one left off.
461         * Notice that the code functions differently inside the loop
462         */
463
464        while (wnd_num > 0)
465        {
466            temp_reg = (Int) get1bits(pInputStream);
467
468            *(pWinPredictionUsed++) = temp_reg;
469
470            if (temp_reg != FALSE)
471            {
472                temp_reg = (Int) get1bits(pInputStream);
473                if (temp_reg != 0)
474                {
475                    temp_reg  = (Int) get9_n_lessbits(
476                                    LEN_LTP_SHORT_LAG,
477                                    pInputStream);
478
479                    *(pDelay++) = prev_subblock - temp_reg;
480                }
481                else
482                {
483                    *(pDelay++) = prev_subblock_nonzero;
484                }
485                for (k = last_band; k > 0; k--)
486                {
487                    *(pTempPtr++) = TRUE;
488                }
489                for (k = (max_sfb - last_band); k > 0; k--)
490                {
491                    *(pTempPtr++) = FALSE;
492                }
493
494            } /* if (temp_reg) */
495            else
496            {
497                pDelay++;
498                pTempPtr += max_sfb;
499            }
500
501            wnd_num--;
502
503        } /* while(wnd_num) */
504
505    } /* else (win_type == EIGHT_SHORT_SEQUENCE) */
506
507} /* lt_decode */
508