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: ./src/huffcb.c
21 Funtions:
22    huffcb
23
24
25------------------------------------------------------------------------------
26 REVISION HISTORY
27
28 Description:  Modified from original shareware code
29
30 Description:  Modified to pass variables by reference to eliminate use
31               of global variables.
32
33 Description:  Change variable names for clarity,
34               change variables 'base', 'sect_len_inc', and 'esc_val' to
35               UChar type.
36
37 Description:  Add "if ((pSect[-1] % sfb_per_win) > max_sfb)" statement to
38               detect the error condition.
39               add more white space.
40
41 Description: eliminated "pSect[-1]%sfb_per_win" operation
42
43 Description: eliminated "pSect[-1]%sfb_per_win" operation
44
45 Description: (1) Pass in SectInfo pSect
46              (2) put BITS *pInputStream as second parameter
47
48 Description:  Fix a failure for thrid party AAC encoding.
49               The problem came when the total and the
50               maximun number of active scale factor bands do not coincide.
51               This is a rare situation but produces a problem when decoding
52               encoders that tolerate this.
53
54 Description: Replace some instances of getbits to get9_n_lessbits
55              when the number of bits read is 9 or less and get1bits
56              when only 1 bit is read.
57
58 Description:
59
60------------------------------------------------------------------------------
61 INPUT AND OUTPUT DEFINITIONS
62
63 Inputs:
64
65    UChar   *pSect  = pointer to array that contains the interleaved
66                      information of huffman codebook index and section
67                      length. Array contains:
68                      [codebook index]
69                      [section boundary]
70                      [codebook index]
71                      [section boundary]
72                      ...
73
74    Int     sectbits  =   array that defines the number of bits
75                          used for expressing the escape value of
76                          section length
77
78    Int     tot_sfb     = total number of sfb in one Frame
79
80    Int     sfb_per_win = number of sfb in each sub-block (window)
81
82    UChar   max_sfb     = 1 + number of active sfbs - see reference (2) p56
83
84    BITS    *pInputStream = pointer to input stream
85
86
87 Local Stores/Buffers/Pointers Needed:
88
89    UChar    base     = number of sfb in already detected sections
90
91    UChar    sect_len_inc = section length increment in number of sfbs'
92
93    UChar    esc_val  = escape value for section length
94
95    Int     bits     = number of bits needed for expressing section length
96
97
98 Global Stores/Buffers/Pointers Needed:
99
100
101 Outputs:
102
103    num_sect = total number of sections in one frame
104
105
106 Pointers and Buffers Modified:
107
108    UChar    *pSect = pointer to array where huffman codebook index and
109                     section length are stored
110
111 Local Stores Modified:
112
113 Global Stores Modified:
114
115------------------------------------------------------------------------------
116 FUNCTION DESCRIPTION
117
118 Background knowledge: 1024(960) coef's are separated into several sections,
119 each section is encoded with one single Huffman codebook, and each section
120 has a length of multiples of sfb.
121
122 max_sfb <= sfb_per_win <= tot_sfb
123 tot_sfb = total number of scalefactor bands in one frame (1024 coefs)
124
125 This function reads the codebook index and section boundaries (expressed
126 in number of sfb) from the input bitstream, store these information in
127 *pSect, and return the number of sections been detected. Returns 0 if there
128 is an error.
129
130------------------------------------------------------------------------------
131 REQUIREMENTS
132
133 This function should fill the array *pSect with section Huffman codebook
134 indexes and section boundaries
135
136------------------------------------------------------------------------------
137 REFERENCES
138
139 (1) MPEG-2 NBC Audio Decoder
140   "This software module was originally developed by AT&T, Dolby
141   Laboratories, Fraunhofer Gesellschaft IIS in the course of development
142   of the MPEG-2 NBC/MPEG-4 Audio standard ISO/IEC 13818-7, 14496-1,2 and
143   3. This software module is an implementation of a part of one or more
144   MPEG-2 NBC/MPEG-4 Audio tools as specified by the MPEG-2 NBC/MPEG-4
145   Audio standard. ISO/IEC  gives users of the MPEG-2 NBC/MPEG-4 Audio
146   standards free license to this software module or modifications thereof
147   for use in hardware or software products claiming conformance to the
148   MPEG-2 NBC/MPEG-4 Audio  standards. Those intending to use this software
149   module in hardware or software products are advised that this use may
150   infringe existing patents. The original developer of this software
151   module and his/her company, the subsequent editors and their companies,
152   and ISO/IEC have no liability for use of this software module or
153   modifications thereof in an implementation. Copyright is not released
154   for non MPEG-2 NBC/MPEG-4 Audio conforming products.The original
155   developer retains full right to use the code for his/her own purpose,
156   assign or donate the code to a third party and to inhibit third party
157   from using the code for non MPEG-2 NBC/MPEG-4 Audio conforming products.
158   This copyright notice must be included in all copies or derivative
159   works."
160   Copyright(c)1996.
161
162 (2) ISO/IEC 14496-3 1999(E)
163   Subpart 4    p55     (Recovering section_data())
164                p24-25  (Syntax of section_data())
165
166 (3) M. Bosi, K. Brandenburg, etc., "ISO/IEC MPEG-2 Advanced Audio Coding,"
167     J. Audio Eng. Soc., Vol.45, No.10, 1997 October
168
169------------------------------------------------------------------------------
170 PSEUDO-CODE
171
172 bits_needed_for_ESC  = sectbits[0];
173 ESC_value            = (1<<bits_needed_for_ESC) - 1;
174 num_of_section       = 0;
175
176
177 FOR (base = 0; base<total_sfb AND num_of_section<total_sfb)
178 {
179    *pSect++     = getbits(LEN_CB, pInputStream);   (read huffman_codebook_num)
180    sect_length_incr  = getbits(bits_needed_for_ESC, pInputStream);
181
182    WHILE (sect_length_incr == ESC_value AND base < total_sfb)
183    {
184        base              += ESC_value;
185        sect_length_incr  =  getbits(bits_needed_for_ESC, ebits);
186    }
187    ENDWHILE
188
189    base      += sect_length_incr;
190    *pSect++   =  base;
191    num_of_section++;
192
193   IF (num_of_sfb_for_this_group==max_sfb)
194   {
195        *pSect++    = 0; (use huffman codebook 0)
196        base       += sfb_per_win - max_sfb;
197        *pSect++    = base;
198        num_of_section++;
199   }
200   ENDIF
201
202   IF (num_of_sfb_for_this_group > max_sfb)
203        break;
204   ENDIF
205
206 }
207 ENDFOR
208
209 IF (base != total_sfb OR num_of_section>total_sfb)
210      return 0;
211 ENDIF
212
213 return num_sect;
214
215------------------------------------------------------------------------------
216 RESOURCES USED
217   When the code is written for a specific target processor the
218     the resources used should be documented below.
219
220 STACK USAGE: [stack count for this module] + [variable to represent
221          stack usage for each subroutine called]
222
223     where: [stack usage variable] = stack usage for [subroutine
224         name] (see [filename].ext)
225
226 DATA MEMORY USED: x words
227
228 PROGRAM MEMORY USED: x words
229
230 CLOCK CYCLES: [cycle count equation for this module] + [variable
231           used to represent cycle count for each subroutine
232           called]
233
234     where: [cycle count variable] = cycle count for [subroutine
235        name] (see [filename].ext)
236
237------------------------------------------------------------------------------
238*/
239
240
241/*----------------------------------------------------------------------------
242; INCLUDES
243----------------------------------------------------------------------------*/
244#include    "pv_audio_type_defs.h"
245#include    "huffman.h"
246
247/*----------------------------------------------------------------------------
248; MACROS
249; Define module specific macros here
250----------------------------------------------------------------------------*/
251
252
253/*----------------------------------------------------------------------------
254; DEFINES
255; Include all pre-processor statements here. Include conditional
256; compile variables also.
257----------------------------------------------------------------------------*/
258
259
260/*----------------------------------------------------------------------------
261; LOCAL FUNCTION DEFINITIONS
262; Function Prototype declaration
263----------------------------------------------------------------------------*/
264
265/*----------------------------------------------------------------------------
266; LOCAL STORE/BUFFER/POINTER DEFINITIONS
267; Variable declaration - defined here and used outside this module
268----------------------------------------------------------------------------*/
269
270/*----------------------------------------------------------------------------
271; EXTERNAL FUNCTION REFERENCES
272; Declare functions defined elsewhere and referenced in this module
273----------------------------------------------------------------------------*/
274
275/*----------------------------------------------------------------------------
276; EXTERNAL GLOBAL STORE/BUFFER/POINTER REFERENCES
277; Declare variables used in this module but defined elsewhere
278----------------------------------------------------------------------------*/
279
280/*----------------------------------------------------------------------------
281; FUNCTION CODE
282----------------------------------------------------------------------------*/
283Int huffcb(
284    SectInfo    *pSect,
285    BITS        *pInputStream,
286    Int         sectbits[],
287    Int         tot_sfb,
288    Int         sfb_per_win,
289    Int         max_sfb)
290{
291    /*----------------------------------------------------------------------------
292    ; Define all local variables
293    ----------------------------------------------------------------------------*/
294
295    Int   base;        /* section boundary */
296    Int   sect_len_incr;
297    Int   esc_val;     /* ESC of section length = 31(long), =7 (short) */
298    Int     bits;        /* # of bits used to express esc_val */
299    Int     num_sect;
300    Int     active_sfb;
301    Int   group_base;
302
303
304    /*----------------------------------------------------------------------------
305    ; Function body here
306    ----------------------------------------------------------------------------*/
307
308    bits       =  sectbits[0];     /* 3 for SHORT_WIN, 5 for LONG_WIN */
309    esc_val    = (1 << bits) - 1;   /* ESC_value for section length */
310    num_sect   =  0;
311    base       =  0;
312    group_base =  0;
313
314    /* read until the end of one frame */
315    while ((base < tot_sfb) && (num_sect < tot_sfb))
316    {
317
318        pSect->sect_cb  = get9_n_lessbits(
319                              LEN_CB,
320                              pInputStream); /* section codebook */
321
322        sect_len_incr   = get9_n_lessbits(
323                              bits,
324                              pInputStream); /* length_incr */
325
326
327        /* read until non-ESC value, see p55 reference 2 */
328        while ((sect_len_incr == esc_val) && (base < tot_sfb))
329        {
330            base            +=  esc_val;
331
332            sect_len_incr   = get9_n_lessbits(
333                                  bits,
334                                  pInputStream);
335        }
336
337        base      += sect_len_incr;
338        pSect->sect_end  =  base; /* total # of sfb until current section */
339        pSect++;
340        num_sect++;
341
342        /* active_sfb = base % sfb_per_win; */
343        active_sfb = base - group_base;
344
345        /*
346         *  insert a zero section for regions above max_sfb for each group
347         *  Make sure that active_sfb is also lesser than tot_sfb
348         */
349
350        if ((active_sfb == max_sfb) && (active_sfb < tot_sfb))
351        {
352            base      += (sfb_per_win - max_sfb);
353            pSect->sect_cb   =   0; /* huffman codebook 0 */
354            pSect->sect_end  =   base;
355            num_sect++;
356            pSect++;
357            group_base = base;
358        }
359        else if (active_sfb > max_sfb)
360        {
361            /* within each group, the sections must delineate the sfb
362             * from zero to max_sfb so that the 1st section within each
363             * group starts at sfb0 and the last section ends at max_sfb
364             * see p55 reference 2
365             */
366            break;
367        }
368
369    } /* while (base=0) */
370
371
372    if (base != tot_sfb || num_sect > tot_sfb)
373    {
374        num_sect = 0;   /* error */
375    }
376
377    return num_sect;
378
379} /* huffcb */
380
381
382