Git init
[external/opencore-amr.git] / opencore / codecs_v2 / audio / gsm_amr / amr_nb / enc / src / prm2bits.cpp
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 Portions 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)
26 Permission to distribute, modify and use this file under the standard license
27 terms listed above has been obtained from the copyright holder.
28 ****************************************************************************************/
29 /*
30
31
32
33
34  Filename: prm2bits.cpp
35
36 ------------------------------------------------------------------------------
37 */
38
39 /*----------------------------------------------------------------------------
40 ; INCLUDES
41 ----------------------------------------------------------------------------*/
42 #include "prm2bits.h"
43 #include "mode.h"
44
45
46 /*----------------------------------------------------------------------------
47 ; MACROS
48 ; [Define module specific macros here]
49 ----------------------------------------------------------------------------*/
50
51 /*----------------------------------------------------------------------------
52 ; DEFINES
53 ; [Include all pre-processor statements here. Include conditional
54 ; compile variables also.]
55 ----------------------------------------------------------------------------*/
56 #define MASK      0x0001
57 /*----------------------------------------------------------------------------
58 ; LOCAL FUNCTION DEFINITIONS
59 ; [List function prototypes here]
60 ----------------------------------------------------------------------------*/
61
62 /*----------------------------------------------------------------------------
63 ; LOCAL VARIABLE DEFINITIONS
64 ; [Variable declaration - defined here and used outside this module]
65 ----------------------------------------------------------------------------*/
66
67 /*
68 ------------------------------------------------------------------------------
69  FUNCTION NAME: Int2bin
70 ------------------------------------------------------------------------------
71  INPUT AND OUTPUT DEFINITIONS
72
73  Inputs:
74     value = value to be converted to binary of type Word16
75     no_of_bits = number of bits associated with value of type Word16
76
77  Outputs:
78     bitstream = pointer to address where bits are written of type Word16
79
80  Returns:
81     None
82
83  Global Variables Used:
84     None
85
86  Local Variables Needed:
87     None
88
89 ------------------------------------------------------------------------------
90  FUNCTION DESCRIPTION
91
92   FUNCTION:  Int2bin
93
94   PURPOSE:  convert integer to binary and write the bits to the array
95             bitstream[]. The most significant bits are written first.
96
97 ------------------------------------------------------------------------------
98  REQUIREMENTS
99
100  None
101
102 ------------------------------------------------------------------------------
103  REFERENCES
104
105  prm2bits.c, UMTS GSM AMR speech codec, R99 - Version 3.2.0, March 2, 2001
106
107 ------------------------------------------------------------------------------
108  PSEUDO-CODE
109
110 static void Int2bin (
111     Word16 value,       // input : value to be converted to binary
112     Word16 no_of_bits,  // input : number of bits associated with value
113     Word16 *bitstream   // output: address where bits are written
114 )
115 {
116     Word16 *pt_bitstream, i, bit;
117
118     pt_bitstream = &bitstream[no_of_bits];
119
120     for (i = 0; i < no_of_bits; i++)
121     {
122         bit = value & MASK;
123         if (bit == 0)
124         {
125             *--pt_bitstream = BIT_0;
126         }
127         else
128         {
129             *--pt_bitstream = BIT_1;
130         }
131         value = shr (value, 1);
132     }
133 }
134
135 ------------------------------------------------------------------------------
136  CAUTION [optional]
137  [State any special notes, constraints or cautions for users of this function]
138
139 ------------------------------------------------------------------------------
140 */
141
142 /*----------------------------------------------------------------------------
143 ; FUNCTION CODE
144 ----------------------------------------------------------------------------*/
145 static void Int2bin(
146     Word16 value,       /* input : value to be converted to binary      */
147     Word16 no_of_bits,  /* input : number of bits associated with value */
148     Word16 *bitstream   /* output: address where bits are written       */
149 )
150 {
151     Word16 *pt_bitstream;
152     Word16 i;
153
154     pt_bitstream = &bitstream[no_of_bits-1];
155
156     for (i = no_of_bits; i != 0; i--)
157     {
158         *(pt_bitstream--) = value & MASK;
159         value >>= 1;
160     }
161
162 }
163
164
165 /*
166 ------------------------------------------------------------------------------
167  FUNCTION NAME: prm2bits
168 ------------------------------------------------------------------------------
169  INPUT AND OUTPUT DEFINITIONS
170
171  Inputs:
172     mode  = AMR mode of type enum Mode
173     prm[] = pointer to analysis parameters of type Word16
174
175  Outputs:
176     bits[] = pointer to serial bits of type Word16
177
178  Returns:
179     None
180
181  Global Variables Used:
182     None
183
184  Local Variables Needed:
185     None
186
187 ------------------------------------------------------------------------------
188  FUNCTION DESCRIPTION
189
190   FUNCTION:    Prm2bits
191
192   PURPOSE:     converts the encoder parameter vector into a vector of serial
193                bits.
194
195   DESCRIPTION: depending on the mode, different numbers of parameters
196                (with differing numbers of bits) are processed. Details
197                are found in bitno.tab
198
199 ------------------------------------------------------------------------------
200  REQUIREMENTS
201
202  None
203
204 ------------------------------------------------------------------------------
205  REFERENCES
206
207  prm2bits.c, UMTS GSM AMR speech codec, R99 - Version 3.2.0, March 2, 2001
208
209 ------------------------------------------------------------------------------
210  PSEUDO-CODE
211
212 void Prm2bits (
213     enum Mode mode,    // i : AMR mode
214     Word16 prm[],      // i : analysis parameters (size <= MAX_PRM_SIZE)
215     Word16 bits[]      // o : serial bits         (size <= MAX_SERIAL_SIZE)
216 )
217 {
218    Word16 i;
219
220    for (i = 0; i < prmno[mode]; i++)
221    {
222        Int2bin (prm[i], bitno[mode][i], bits);
223        bits += bitno[mode][i];
224        add(0,0);       // account for above pointer update
225    }
226
227    return;
228 }
229
230 ------------------------------------------------------------------------------
231  CAUTION [optional]
232  [State any special notes, constraints or cautions for users of this function]
233
234 ------------------------------------------------------------------------------
235 */
236
237 /*----------------------------------------------------------------------------
238 ; FUNCTION CODE
239 ----------------------------------------------------------------------------*/
240 void Prm2bits(
241     enum Mode mode,    /* i : AMR mode                                      */
242     Word16 prm[],      /* i : analysis parameters (size <= MAX_PRM_SIZE)    */
243     Word16 bits[],      /* o : serial bits         (size <= MAX_SERIAL_SIZE) */
244     CommonAmrTbls* common_amr_tbls /* i : ptr to strcut of table ptrs        */
245 )
246 {
247     Word16 i;
248     const Word16 *p_mode;
249     Word16 *p_prm;
250     const Word16* prmno_ptr = common_amr_tbls->prmno_ptr;
251
252     p_mode = &common_amr_tbls->bitno_ptr[mode][0];
253     p_prm  = &prm[0];
254
255     for (i = prmno_ptr[mode]; i != 0; i--)
256     {
257         Int2bin(*(p_prm++), *(p_mode), bits);
258         bits += *(p_mode++);
259     }
260
261     return;
262 }
263