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: pulse_nc.c
21
22------------------------------------------------------------------------------
23 REVISION HISTORY
24
25 Description:  Modified from original shareware code
26
27 Description:  Modified to pass variables by reference to eliminate use
28               of global variables.
29
30 Description:  Modified to bring code in-line with PV standards.
31
32 Description: Pass in max as input argument.
33
34 Description: Went back to the if-statement to check for max.
35
36 Who:                       Date:
37 Description:
38
39------------------------------------------------------------------------------
40 INPUT AND OUTPUT DEFINITIONS
41
42 Inputs:
43    coef[]         =  Array of quantized spectral coefficents.
44                      (Int [])
45
46    pPulseInfo     =  Pointer to structure which contains noiseless
47                      encoding info, includes information about the pulse data,
48                      pulse amplitude, etc.
49                      (const PulseInfo *)
50
51    pLongFrameInfo =  Pointer to structure that holds information about
52                      each group. (long block flag, number of windows,
53                      scalefactor bands per group, etc.)
54
55                      Variable is named (pLongFrameInfo) because this function
56                      is only used for LONG windows.
57                      (FrameInfo *)
58    max             = Pointer to the maximum value of coef[]
59
60 Local Stores/Buffers/Pointers Needed:
61    None
62
63 Global Stores/Buffers/Pointers Needed:
64    None
65
66 Outputs:
67    None
68
69 Pointers and Buffers Modified:
70    coef[]  =  coefficient contents are modified by the encoded pulse
71
72 Local Stores Modified:
73
74 Global Stores Modified:
75
76------------------------------------------------------------------------------
77 FUNCTION DESCRIPTION
78
79 This function adds pulses to defined ranges of coefficients in the window,
80 for the case of LONG windows.  The pulses are unsigned, so
81 negative coefficients subtract the pulse, and positive coefficients add it.
82 (The ampltiude of the coefficient is always increased by the pulse)
83
84 A maximum of 4 coefficients may be modified by a pulse, and these
85 coefficients must all occur in the same scalefactor band.
86
87 The number of pulse-encoded coefficients to be processed by this function
88 is communicated to this function via pPulseInfo->number_pulse
89
90 This value is equal to the actual number of pulses - 1.
91 (e.g if pPulseInfo->number_pulse == 0, one pulse is assumed)
92 This function must not be called if no pulse encoded data exists.
93 The function assumes that at least one pulse exists.
94------------------------------------------------------------------------------
95 REQUIREMENTS
96
97 This module shall correctly add transmitted pulse(s) to the correct
98 coefficients in a LONG window.
99
100------------------------------------------------------------------------------
101 REFERENCES
102 (1) ISO/IEC 14496-3:1999(E)
103     Part 3
104        Subpart 4.6.3.3 Decoding Process
105
106 (2) MPEG-2 NBC Audio Decoder
107   "This software module was originally developed by AT&T, Dolby
108   Laboratories, Fraunhofer Gesellschaft IIS in the course of development
109   of the MPEG-2 NBC/MPEG-4 Audio standard ISO/IEC 13818-7, 14496-1,2 and
110   3. This software module is an implementation of a part of one or more
111   MPEG-2 NBC/MPEG-4 Audio tools as specified by the MPEG-2 NBC/MPEG-4
112   Audio standard. ISO/IEC  gives users of the MPEG-2 NBC/MPEG-4 Audio
113   standards free license to this software module or modifications thereof
114   for use in hardware or software products claiming conformance to the
115   MPEG-2 NBC/MPEG-4 Audio  standards. Those intending to use this software
116   module in hardware or software products are advised that this use may
117   infringe existing patents. The original developer of this software
118   module and his/her company, the subsequent editors and their companies,
119   and ISO/IEC have no liability for use of this software module or
120   modifications thereof in an implementation. Copyright is not released
121   for non MPEG-2 NBC/MPEG-4 Audio conforming products.The original
122   developer retains full right to use the code for his/her  own purpose,
123   assign or donate the code to a third party and to inhibit third party
124   from using the code for non MPEG-2 NBC/MPEG-4 Audio conforming products.
125   This copyright notice must be included in all copies or derivative
126   works."
127   Copyright(c)1996.
128
129------------------------------------------------------------------------------
130 PSEUDO-CODE
131
132    index = pLongFrameInfo->win_sfb_top[0][pPulseInfo->pulse_start_sfb];
133
134    pPulseOffset = &(pPulseInfo->pulse_offset[0]);
135
136    pPulseAmp    = &(pPulseInfo->pulse_amp[0]);
137
138    pCoef        = &(Coef[index]);
139
140    FOR (index = pPulseInfo->number_pulse; index >= 0; index--)
141
142        pCoef   = pCoef + *(pPulseOffset);
143        pPulseOffset = pPulseOffset + 1;
144
145        IF (*pCoef > 0)
146            *(pCoef) = *(pCoef) + *(pPulseAmp);
147            pPulseAmp     = pPulseAmp + 1;
148        ELSE
149            *(pCoef) = *(pCoef) - *(pPulseAmp);
150            pPulseAmp     = pPulseAmp + 1;
151        ENDIF
152
153    ENDFOR
154
155------------------------------------------------------------------------------
156 RESOURCES USED
157   When the code is written for a specific target processor the
158     the resources used should be documented below.
159
160 STACK USAGE: [stack count for this module] + [variable to represent
161          stack usage for each subroutine called]
162
163     where: [stack usage variable] = stack usage for [subroutine
164         name] (see [filename].ext)
165
166 DATA MEMORY USED: x words
167
168 PROGRAM MEMORY USED: x words
169
170 CLOCK CYCLES: [cycle count equation for this module] + [variable
171           used to represent cycle count for each subroutine
172           called]
173
174     where: [cycle count variable] = cycle count for [subroutine
175        name] (see [filename].ext)
176
177------------------------------------------------------------------------------
178*/
179
180
181/*----------------------------------------------------------------------------
182; INCLUDES
183----------------------------------------------------------------------------*/
184#include "pv_audio_type_defs.h"
185#include "s_frameinfo.h"
186#include "s_pulseinfo.h"
187#include "pulse_nc.h"
188
189/*----------------------------------------------------------------------------
190; MACROS
191; Define module specific macros here
192----------------------------------------------------------------------------*/
193
194/*---------------------------------------------------------------------------
195; DEFINES
196; Include all pre-processor statements here. Include conditional
197; compile variables also.
198----------------------------------------------------------------------------*/
199
200/*----------------------------------------------------------------------------
201; LOCAL FUNCTION DEFINITIONS
202; Function Prototype declaration
203----------------------------------------------------------------------------*/
204
205/*----------------------------------------------------------------------------
206; LOCAL STORE/BUFFER/POINTER DEFINITIONS
207; Variable declaration - defined here and used outside this module
208----------------------------------------------------------------------------*/
209
210/*----------------------------------------------------------------------------
211; EXTERNAL FUNCTION REFERENCES
212; Declare functions defined elsewhere and referenced in this module
213----------------------------------------------------------------------------*/
214
215/*----------------------------------------------------------------------------
216; EXTERNAL GLOBAL STORE/BUFFER/POINTER REFERENCES
217; Declare variables used in this module but defined elsewhere
218----------------------------------------------------------------------------*/
219
220/*----------------------------------------------------------------------------
221; FUNCTION CODE
222----------------------------------------------------------------------------*/
223
224void pulse_nc(
225    Int16      coef[],
226    const PulseInfo  *pPulseInfo,
227    const FrameInfo  *pLongFrameInfo,
228    Int      *max)
229{
230    Int index;
231
232    Int16 *pCoef;
233    Int temp;
234
235    const Int *pPulseOffset;
236    const Int *pPulseAmp;
237
238    /*--- Find the scalefactor band where pulse-encoded data starts ---*/
239
240    if (pPulseInfo->pulse_start_sfb > 0)
241    {
242        index = pLongFrameInfo->win_sfb_top[0][pPulseInfo->pulse_start_sfb - 1];
243    }
244    else
245    {
246        index = 0;
247    }
248
249    /*-------------------------------------------------------------------------
250      Each pulse index is stored as an offset from the previous pulse
251
252      Example - here we have a sfb that is 20 coefficients in length:
253
254      [0][1][2][3][4][5][6][7][8][9][10][11][12][13][14][15][16][17][18][19]
255      [ ][ ][ ][ ][ ][P][P][ ][ ][ ][  ][  ][  ][  ][  ][ P][  ][  ][  ][ P]
256
257      The array pointed to by pPulseOffset == [5][1][9][4]
258
259      pPulseAmp is of the same length as pPulseOffset, and contains
260      an individual pulse amplitude for each coefficient.
261    --------------------------------------------------------------------------*/
262
263    pCoef        = &(coef[index]);
264
265    pPulseOffset = &(pPulseInfo->pulse_offset[0]);
266
267    pPulseAmp    = &(pPulseInfo->pulse_amp[0]);
268
269    for (index = pPulseInfo->number_pulse; index > 0; index--)
270    {
271        pCoef  += *pPulseOffset++;
272
273        temp = *pCoef;
274
275        if (temp > 0)
276        {
277            temp += *(pPulseAmp++);
278            *pCoef = (Int16)temp;
279            if (temp > *max)
280            {
281                *max = temp;
282            }
283        }
284        else
285        {
286            temp -= *(pPulseAmp++);
287            *pCoef = (Int16)temp;
288            if (-temp > *max)
289            {
290                *max = -temp;
291            }
292        }
293
294    } /* for() */
295
296    return;
297
298} /* pulse_nc */
299