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: infoinit.c
21
22
23------------------------------------------------------------------------------
24 REVISION HISTORY
25
26 Description:  Modified from original shareware code
27
28 Description:  Modified to pass variables by reference to eliminate use
29               of global variables.
30
31 Description:  Pass eight_short_info and the array 'sfbwidth128'.
32               Change function arguments' names for clarity
33
34 Description:  move sfb definitions to "sfb.h", and "sfb.c", eliminated
35               the function "huffbookinit.c"
36
37 Description:  Remove initialization of the never used array,
38               pFrameInfo->group_offs
39
40 Description:
41 (1) Changed "stdinc.h" to <stdlib.h> - this avoids linking in the math
42 library and stdio.h.  (All for just defining the NULL pointer macro)
43
44 (2) Updated copyright header.
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: Addresses of constant vectors are now found by means of a
50              switch statement, this solve linking problem when using the
51              /ropi option (Read-only position independent) for some
52              compilers
53
54 Who:                               Date:
55 Description:
56
57------------------------------------------------------------------------------
58 INPUT AND OUTPUT DEFINITIONS
59
60 Inputs:
61    pSi              = pointer to sampling rate info
62    ppWin_seq_info   = pointer array to window sequence Info struct
63    pSfbwidth128     = pointer to sfb bandwidth array of short window
64
65 Local Stores/Buffers/Pointers Needed:
66
67 Global Stores/Buffers/Pointers Needed:
68
69 Outputs:
70
71 Pointers and Buffers Modified:
72
73    ppWin_seq_info[ONLY_LONG_WINDOW]{all structure members} = setup values
74    ppWin_seq_info[EIGHT_SHORT_WINDOW]{all structure members} = setup values
75
76 Local Stores Modified:
77
78 Global Stores Modified:
79
80
81------------------------------------------------------------------------------
82 FUNCTION DESCRIPTION
83
84 This function sets the values of 'Info' structure for blocks containing long
85 and short window sequences, the following structures are being set:
86
87 win_seq_info[ONLY_LONG_WINDOW], win_seq_info[EIGHT_SHORT_WINDOW],
88 only_long_info and eight_short_info
89
90------------------------------------------------------------------------------
91 REQUIREMENTS
92
93------------------------------------------------------------------------------
94 REFERENCES
95
96 (1) MPEG-2 NBC Audio Decoder
97   "This software module was originally developed by AT&T, Dolby
98   Laboratories, Fraunhofer Gesellschaft IIS in the course of development
99   of the MPEG-2 NBC/MPEG-4 Audio standard ISO/IEC 13818-7, 14496-1,2 and
100   3. This software module is an implementation of a part of one or more
101   MPEG-2 NBC/MPEG-4 Audio tools as specified by the MPEG-2 NBC/MPEG-4
102   Audio standard. ISO/IEC  gives users of the MPEG-2 NBC/MPEG-4 Audio
103   standards free license to this software module or modifications thereof
104   for use in hardware or software products claiming conformance to the
105   MPEG-2 NBC/MPEG-4 Audio  standards. Those intending to use this software
106   module in hardware or software products are advised that this use may
107   infringe existing patents. The original developer of this software
108   module and his/her company, the subsequent editors and their companies,
109   and ISO/IEC have no liability for use of this software module or
110   modifications thereof in an implementation. Copyright is not released
111   for non MPEG-2 NBC/MPEG-4 Audio conforming products.The original
112   developer retains full right to use the code for his/her own purpose,
113   assign or donate the code to a third party and to inhibit third party
114   from using the code for non MPEG-2 NBC/MPEG-4 Audio conforming products.
115   This copyright notice must be included in all copies or derivative
116   works."
117   Copyright(c)1996.
118
119 (2) ISO/IEC 14496-3: 1999(E)
120    Subpart 4       p66     (sfb tables)
121                    p111    (4.6.10)
122                    p200    (Annex 4.B.5)
123------------------------------------------------------------------------------
124 PSEUDO-CODE
125
126    pFrameInfo  =   pointer to only_long_info;
127    win_seq_info[ONLY_LONG_WINDOW]  =   pFrameInfo;
128    pFrameInfo{all structure members} = setup values;
129
130
131    pFrameInfo  =   pointer to eight_short_info;
132    win_seq_info[EIGHT_SHORT_WINDOW]  =   pFrameInfo;
133    pFrameInfo{all structure.members} =   setup values;
134
135
136    FOR (window_seq = 0; window_seq < NUM_WIN_SEQ; win_seq++)
137
138        win_seq_info[window_seq].members = setup values;
139
140    ENDFOR
141
142------------------------------------------------------------------------------
143 RESOURCES USED
144   When the code is written for a specific target processor the
145     the resources used should be documented below.
146
147 STACK USAGE:
148
149 DATA MEMORY USED: x words
150
151 PROGRAM MEMORY USED: x words
152
153 CLOCK CYCLES:
154
155------------------------------------------------------------------------------
156*/
157
158
159
160/*----------------------------------------------------------------------------
161; INCLUDES
162----------------------------------------------------------------------------*/
163#include    "pv_audio_type_defs.h"
164#include    "s_sr_info.h"
165#include    "s_frameinfo.h"
166#include    "e_blockswitching.h"
167#include    "e_huffmanconst.h"
168#include    "sfb.h"
169#include    "huffman.h"
170
171/*----------------------------------------------------------------------------
172; MACROS
173; Define module specific macros here
174----------------------------------------------------------------------------*/
175
176
177/*----------------------------------------------------------------------------
178; DEFINES
179; Include all pre-processor statements here. Include conditional
180; compile variables also.
181----------------------------------------------------------------------------*/
182
183
184/*----------------------------------------------------------------------------
185; LOCAL FUNCTION DEFINITIONS
186; Function Prototype declaration
187----------------------------------------------------------------------------*/
188
189
190/*----------------------------------------------------------------------------
191; LOCAL STORE/BUFFER/POINTER DEFINITIONS
192; Variable declaration - defined here and used outside this module
193----------------------------------------------------------------------------*/
194
195
196/*----------------------------------------------------------------------------
197; EXTERNAL FUNCTION REFERENCES
198; Declare functions defined elsewhere and referenced in this module
199----------------------------------------------------------------------------*/
200
201/*----------------------------------------------------------------------------
202; EXTERNAL GLOBAL STORE/BUFFER/POINTER REFERENCES
203; Declare variables used in this module but defined elsewhere
204----------------------------------------------------------------------------*/
205
206/*----------------------------------------------------------------------------
207; FUNCTION CODE
208----------------------------------------------------------------------------*/
209
210Int infoinit(
211    const Int samp_rate_idx,
212    FrameInfo   **ppWin_seq_info,
213    Int    *pSfbwidth128)
214
215{
216    /*----------------------------------------------------------------------------
217    ; Define all local variables
218    ----------------------------------------------------------------------------*/
219
220    Int     i;
221    Int     sfb_idx, sfb_sbk;
222    Int     bins_sbk;
223    Int     win_seq;
224    Int     start_idx, end_idx;
225    Int     nsfb_short;
226    Int16   *sfbands;
227    FrameInfo    *pFrameInfo;
228
229    const SR_Info *pSi = &(samp_rate_info[samp_rate_idx]);
230
231    const Int16 * pt_SFbands1024 = NULL;
232    const Int16 * pt_SFbands128  = NULL;
233
234    /*----------------------------------------------------------------------------
235    ; Function body here
236    ----------------------------------------------------------------------------*/
237
238    switch (pSi->samp_rate)
239    {
240        case 96000:
241        case 88200:
242            pt_SFbands1024  = sfb_96_1024;
243            pt_SFbands128   = sfb_64_128;  /* equal to table sfb_96_128, (eliminated) */
244            break;
245        case 64000:
246            pt_SFbands1024  = sfb_64_1024;
247            pt_SFbands128   = sfb_64_128;
248            break;
249        case 48000:
250        case 44100:
251            pt_SFbands1024  = sfb_48_1024;
252            pt_SFbands128   = sfb_48_128;
253            break;
254        case 32000:
255            pt_SFbands1024  = sfb_32_1024;
256            pt_SFbands128   = sfb_48_128;
257            break;
258        case 24000:
259        case 22050:
260            pt_SFbands1024  = sfb_24_1024;
261            pt_SFbands128   = sfb_24_128;
262            break;
263        case 16000:
264        case 12000:
265        case 11025:
266            pt_SFbands1024  = sfb_16_1024;
267            pt_SFbands128   = sfb_16_128;
268            break;
269        case 8000:
270            pt_SFbands1024  = sfb_8_1024;
271            pt_SFbands128   = sfb_8_128;
272            break;
273        default:
274            // sampling rate not supported
275            return -1;
276    }
277
278    /* long block info */
279
280    pFrameInfo = ppWin_seq_info[ONLY_LONG_WINDOW];
281    pFrameInfo->islong               = 1;
282    pFrameInfo->num_win              = 1;
283    pFrameInfo->coef_per_frame       = LN2; /* = 1024 */
284
285    pFrameInfo->sfb_per_win[0]  = pSi->nsfb1024;
286    pFrameInfo->sectbits[0]     = LONG_SECT_BITS;
287    pFrameInfo->win_sfb_top[0]  = (Int16 *)pt_SFbands1024;
288
289    pFrameInfo->sfb_width_128 = NULL; /* no short block sfb */
290    pFrameInfo->num_groups    = 1; /* long block, one group */
291    pFrameInfo->group_len[0]  = 1; /* only one window */
292
293    /* short block info */
294    pFrameInfo = ppWin_seq_info[EIGHT_SHORT_WINDOW];
295    pFrameInfo->islong                  = 0;
296    pFrameInfo->num_win                 = NSHORT;
297    pFrameInfo->coef_per_frame          = LN2;
298
299    for (i = 0; i < pFrameInfo->num_win; i++)
300    {
301        pFrameInfo->sfb_per_win[i] = pSi->nsfb128;
302        pFrameInfo->sectbits[i]    = SHORT_SECT_BITS;
303        pFrameInfo->win_sfb_top[i] = (Int16 *)pt_SFbands128;
304    }
305
306    /* construct sfb width table */
307    pFrameInfo->sfb_width_128 = pSfbwidth128;
308    for (i = 0, start_idx = 0, nsfb_short = pSi->nsfb128; i < nsfb_short; i++)
309    {
310        end_idx = pt_SFbands128[i];
311        pSfbwidth128[i] = end_idx - start_idx;
312        start_idx = end_idx;
313    }
314
315
316    /* common to long and short */
317    for (win_seq = 0; win_seq < NUM_WIN_SEQ; win_seq++)
318    {
319
320        if (ppWin_seq_info[win_seq] != NULL)
321        {
322            pFrameInfo                 = ppWin_seq_info[win_seq];
323            pFrameInfo->sfb_per_frame  = 0;
324            sfb_sbk                    = 0;
325            bins_sbk                   = 0;
326
327            for (i = 0; i < pFrameInfo->num_win; i++)
328            {
329
330                /* compute coef_per_win */
331                pFrameInfo->coef_per_win[i] =
332                    pFrameInfo->coef_per_frame / pFrameInfo->num_win;
333
334                /* compute sfb_per_frame */
335                pFrameInfo->sfb_per_frame += pFrameInfo->sfb_per_win[i];
336
337                /* construct default (non-interleaved) bk_sfb_top[] */
338                sfbands = pFrameInfo->win_sfb_top[i];
339                for (sfb_idx = 0; sfb_idx < pFrameInfo->sfb_per_win[i];
340                        sfb_idx++)
341                {
342                    pFrameInfo->frame_sfb_top[sfb_idx+sfb_sbk] =
343                        sfbands[sfb_idx] + bins_sbk;
344                }
345
346                bins_sbk += pFrameInfo->coef_per_win[i];
347                sfb_sbk  += pFrameInfo->sfb_per_win[i];
348            } /* for i = sbk ends */
349        }
350
351    } /* for win_seq ends */
352
353    return SUCCESS;
354
355} /* infoinit */
356