prm2bits.cpp revision b841f14f8e51f2365945281fbfa54ef6a1b1b5a6
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/****************************************************************************************
19Portions of this file are derived from the following 3GPP standard:
20
21    3GPP TS 26.073
22    ANSI-C code for the Adaptive Multi-Rate (AMR) speech codec
23    Available from http://www.3gpp.org
24
25(C) 2004, 3GPP Organizational Partners (ARIB, ATIS, CCSA, ETSI, TTA, TTC)
26Permission to distribute, modify and use this file under the standard license
27terms listed above has been obtained from the copyright holder.
28****************************************************************************************/
29/*
30
31
32
33
34 Filename: /audio/gsm_amr/c/src/prm2bits.c
35
36     Date: 02/04/2002
37
38------------------------------------------------------------------------------
39 REVISION HISTORY
40
41 Description: Improved the code as per review comments.
42
43 Description:  For Int2bin() and Prm2bits()
44              1. Eliminated unused include file typedef.h.
45              2. Replaced array addressing by pointers
46              3. Changed to decrement loops
47
48 Description:  Replaced "int" and/or "char" with OSCL defined types.
49
50 Description:
51
52------------------------------------------------------------------------------
53*/
54
55/*----------------------------------------------------------------------------
56; INCLUDES
57----------------------------------------------------------------------------*/
58#include "prm2bits.h"
59#include "mode.h"
60#include "bitno_tab.h"
61
62
63/*----------------------------------------------------------------------------
64; MACROS
65; [Define module specific macros here]
66----------------------------------------------------------------------------*/
67
68/*----------------------------------------------------------------------------
69; DEFINES
70; [Include all pre-processor statements here. Include conditional
71; compile variables also.]
72----------------------------------------------------------------------------*/
73#define MASK      0x0001
74/*----------------------------------------------------------------------------
75; LOCAL FUNCTION DEFINITIONS
76; [List function prototypes here]
77----------------------------------------------------------------------------*/
78
79/*----------------------------------------------------------------------------
80; LOCAL VARIABLE DEFINITIONS
81; [Variable declaration - defined here and used outside this module]
82----------------------------------------------------------------------------*/
83
84/*
85------------------------------------------------------------------------------
86 FUNCTION NAME: Int2bin
87------------------------------------------------------------------------------
88 INPUT AND OUTPUT DEFINITIONS
89
90 Inputs:
91    value = value to be converted to binary of type Word16
92    no_of_bits = number of bits associated with value of type Word16
93
94 Outputs:
95    bitstream = pointer to address where bits are written of type Word16
96
97 Returns:
98    None
99
100 Global Variables Used:
101    None
102
103 Local Variables Needed:
104    None
105
106------------------------------------------------------------------------------
107 FUNCTION DESCRIPTION
108
109  FUNCTION:  Int2bin
110
111  PURPOSE:  convert integer to binary and write the bits to the array
112            bitstream[]. The most significant bits are written first.
113
114------------------------------------------------------------------------------
115 REQUIREMENTS
116
117 None
118
119------------------------------------------------------------------------------
120 REFERENCES
121
122 prm2bits.c, UMTS GSM AMR speech codec, R99 - Version 3.2.0, March 2, 2001
123
124------------------------------------------------------------------------------
125 PSEUDO-CODE
126
127static void Int2bin (
128    Word16 value,       // input : value to be converted to binary
129    Word16 no_of_bits,  // input : number of bits associated with value
130    Word16 *bitstream   // output: address where bits are written
131)
132{
133    Word16 *pt_bitstream, i, bit;
134
135    pt_bitstream = &bitstream[no_of_bits];
136
137    for (i = 0; i < no_of_bits; i++)
138    {
139        bit = value & MASK;
140        if (bit == 0)
141        {
142            *--pt_bitstream = BIT_0;
143        }
144        else
145        {
146            *--pt_bitstream = BIT_1;
147        }
148        value = shr (value, 1);
149    }
150}
151
152------------------------------------------------------------------------------
153 RESOURCES USED [optional]
154
155 When the code is written for a specific target processor the
156 the resources used should be documented below.
157
158 HEAP MEMORY USED: x bytes
159
160 STACK MEMORY USED: x bytes
161
162 CLOCK CYCLES: (cycle count equation for this function) + (variable
163                used to represent cycle count for each subroutine
164                called)
165     where: (cycle count variable) = cycle count for [subroutine
166                                     name]
167
168------------------------------------------------------------------------------
169 CAUTION [optional]
170 [State any special notes, constraints or cautions for users of this function]
171
172------------------------------------------------------------------------------
173*/
174
175/*----------------------------------------------------------------------------
176; FUNCTION CODE
177----------------------------------------------------------------------------*/
178static void Int2bin(
179    Word16 value,       /* input : value to be converted to binary      */
180    Word16 no_of_bits,  /* input : number of bits associated with value */
181    Word16 *bitstream   /* output: address where bits are written       */
182)
183{
184    Word16 *pt_bitstream;
185    Word16 i;
186
187    pt_bitstream = &bitstream[no_of_bits-1];
188
189    for (i = no_of_bits; i != 0; i--)
190    {
191        *(pt_bitstream--) = value & MASK;
192        value >>= 1;
193    }
194
195}
196
197
198/*
199------------------------------------------------------------------------------
200 FUNCTION NAME: prm2bits
201------------------------------------------------------------------------------
202 INPUT AND OUTPUT DEFINITIONS
203
204 Inputs:
205    mode  = AMR mode of type enum Mode
206    prm[] = pointer to analysis parameters of type Word16
207
208 Outputs:
209    bits[] = pointer to serial bits of type Word16
210
211 Returns:
212    None
213
214 Global Variables Used:
215    None
216
217 Local Variables Needed:
218    None
219
220------------------------------------------------------------------------------
221 FUNCTION DESCRIPTION
222
223  FUNCTION:    Prm2bits
224
225  PURPOSE:     converts the encoder parameter vector into a vector of serial
226               bits.
227
228  DESCRIPTION: depending on the mode, different numbers of parameters
229               (with differing numbers of bits) are processed. Details
230               are found in bitno.tab
231
232------------------------------------------------------------------------------
233 REQUIREMENTS
234
235 None
236
237------------------------------------------------------------------------------
238 REFERENCES
239
240 prm2bits.c, UMTS GSM AMR speech codec, R99 - Version 3.2.0, March 2, 2001
241
242------------------------------------------------------------------------------
243 PSEUDO-CODE
244
245void Prm2bits (
246    enum Mode mode,    // i : AMR mode
247    Word16 prm[],      // i : analysis parameters (size <= MAX_PRM_SIZE)
248    Word16 bits[]      // o : serial bits         (size <= MAX_SERIAL_SIZE)
249)
250{
251   Word16 i;
252
253   for (i = 0; i < prmno[mode]; i++)
254   {
255       Int2bin (prm[i], bitno[mode][i], bits);
256       bits += bitno[mode][i];
257       add(0,0);       // account for above pointer update
258   }
259
260   return;
261}
262
263------------------------------------------------------------------------------
264 RESOURCES USED [optional]
265
266 When the code is written for a specific target processor the
267 the resources used should be documented below.
268
269 HEAP MEMORY USED: x bytes
270
271 STACK MEMORY USED: x bytes
272
273 CLOCK CYCLES: (cycle count equation for this function) + (variable
274                used to represent cycle count for each subroutine
275                called)
276     where: (cycle count variable) = cycle count for [subroutine
277                                     name]
278
279------------------------------------------------------------------------------
280 CAUTION [optional]
281 [State any special notes, constraints or cautions for users of this function]
282
283------------------------------------------------------------------------------
284*/
285
286/*----------------------------------------------------------------------------
287; FUNCTION CODE
288----------------------------------------------------------------------------*/
289void Prm2bits(
290    enum Mode mode,    /* i : AMR mode                                      */
291    Word16 prm[],      /* i : analysis parameters (size <= MAX_PRM_SIZE)    */
292    Word16 bits[]      /* o : serial bits         (size <= MAX_SERIAL_SIZE) */
293)
294{
295    Word16 i;
296    const Word16 *p_mode;
297    Word16 *p_prm;
298
299    p_mode = &bitno[mode][0];
300    p_prm  = &prm[0];
301
302    for (i = prmno[mode]; i != 0; i--)
303    {
304        Int2bin(*(p_prm++), *(p_mode), bits);
305        bits += *(p_mode++);
306    }
307
308    return;
309}
310
311