Git init
[external/opencore-amr.git] / opencore / codecs_v2 / audio / gsm_amr / amr_nb / enc / src / ets_to_wmf.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: ets_to_wmf.cpp
35  Functions: ets_to_wmf
36
37 ------------------------------------------------------------------------------
38 */
39
40 /*----------------------------------------------------------------------------
41 ; INCLUDES
42 ----------------------------------------------------------------------------*/
43 #include "ets_to_wmf.h"
44 #include "typedef.h"
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
57
58 /*----------------------------------------------------------------------------
59 ; LOCAL FUNCTION DEFINITIONS
60 ; Function Prototype declaration
61 ----------------------------------------------------------------------------*/
62
63 /*----------------------------------------------------------------------------
64 ; LOCAL VARIABLE DEFINITIONS
65 ; Variable declaration - defined here and used outside this module
66 ----------------------------------------------------------------------------*/
67
68
69 /*
70 ------------------------------------------------------------------------------
71  FUNCTION NAME: ets_to_wmf
72 ------------------------------------------------------------------------------
73  INPUT AND OUTPUT DEFINITIONS
74
75  Inputs:
76     frame_type_3gpp = decoder speech bit rate (enum Frame_Type_3GPP)
77     ets_input_ptr   = pointer to input encoded speech bits in ETS format (Word16)
78     wmf_output_ptr  = pointer to output encoded speech bits in WMF format(UWord8)
79
80  Outputs:
81     wmf_output_ptr  = pointer to encoded speech bits in the WMF format (UWord8)
82
83  Returns:
84     None
85
86  Global Variables Used:
87     numOfBits = table of values that describe the number of bits per frame for
88                 each 3GPP frame type mode. The table is type const Word16 and has
89                 NUM_MODES elements. This table is located in bitreorder_tab.c.
90     reorderBits = table of pointers that point to tables used to reorder the
91                   encoded speech bits when converting from ETS to WMF or IF2
92                   format. The table is of type const Word16 * and contains
93                   NUM_MODES-1 elements. This table is located in bitreorder_tab.c.
94
95  Local Variables Needed:
96     None
97
98 ------------------------------------------------------------------------------
99  FUNCTION DESCRIPTION
100
101  This function performs a transformation on the data buffers. It converts the
102  data format from ETS (European Telecommunication Standard) to WMF (wireless
103  multimedia forum). ETS format has the encoded speech bits each separate with
104  only one bit stored in each word. WMF is the storage format where the frame
105  type is in the first four bits of the first byte. This first byte has the
106  upper four bits as padded zeroes. The following bytes contain the rest of the
107  encoded speech bits. The final byte has padded zeros to make the frame byte
108  aligned.
109 ------------------------------------------------------------------------------
110  REQUIREMENTS
111
112  None
113
114 ------------------------------------------------------------------------------
115  REFERENCES
116
117  None
118
119 ------------------------------------------------------------------------------
120  PSEUDO-CODE
121
122
123
124 ------------------------------------------------------------------------------
125  CAUTION [optional]
126  [State any special notes, constraints or cautions for users of this function]
127
128 ------------------------------------------------------------------------------
129 */
130
131 void ets_to_wmf(
132     enum Frame_Type_3GPP frame_type_3gpp,
133     Word16 *ets_input_ptr,
134     UWord8 *wmf_output_ptr,
135     CommonAmrTbls* common_amr_tbls)
136 {
137     Word16  i;
138     Word16  k = 0;
139     Word16  j = 0;
140     Word16 *ptr_temp;
141     Word16  bits_left;
142     UWord8  accum;
143     const Word16* const* reorderBits_ptr = common_amr_tbls->reorderBits_ptr;
144     const Word16* numOfBits_ptr = common_amr_tbls->numOfBits_ptr;
145
146     wmf_output_ptr[j++] = (UWord8)(frame_type_3gpp) & 0x0f;
147
148     if (frame_type_3gpp < AMR_SID)
149     {
150
151         for (i = 0; i < numOfBits_ptr[frame_type_3gpp] - 7;)
152         {
153             wmf_output_ptr[j]  =
154                 (UWord8) ets_input_ptr[reorderBits_ptr[frame_type_3gpp][i++]] << 7;
155             wmf_output_ptr[j] |=
156                 (UWord8) ets_input_ptr[reorderBits_ptr[frame_type_3gpp][i++]] << 6;
157             wmf_output_ptr[j] |=
158                 (UWord8) ets_input_ptr[reorderBits_ptr[frame_type_3gpp][i++]] << 5;
159             wmf_output_ptr[j] |=
160                 (UWord8) ets_input_ptr[reorderBits_ptr[frame_type_3gpp][i++]] << 4;
161             wmf_output_ptr[j] |=
162                 (UWord8) ets_input_ptr[reorderBits_ptr[frame_type_3gpp][i++]] << 3;
163             wmf_output_ptr[j] |=
164                 (UWord8) ets_input_ptr[reorderBits_ptr[frame_type_3gpp][i++]] << 2;
165             wmf_output_ptr[j] |=
166                 (UWord8) ets_input_ptr[reorderBits_ptr[frame_type_3gpp][i++]] << 1;
167             wmf_output_ptr[j++] |=
168                 (UWord8) ets_input_ptr[reorderBits_ptr[frame_type_3gpp][i++]];
169         }
170
171         bits_left = numOfBits_ptr[frame_type_3gpp] -
172                     (numOfBits_ptr[frame_type_3gpp] & 0xFFF8);
173
174         wmf_output_ptr[j] = 0;
175
176         for (k = 0; k < bits_left; k++)
177         {
178             wmf_output_ptr[j] |=
179                 (UWord8) ets_input_ptr[reorderBits_ptr[frame_type_3gpp][i++]] << (7 - k);
180
181         }
182     }
183     else
184     {
185
186         ptr_temp = &ets_input_ptr[0];
187
188         for (i = numOfBits_ptr[frame_type_3gpp] - 7; i > 0; i -= 8)
189         {
190             accum  = (UWord8) * (ptr_temp++) << 7;
191             accum |= (UWord8) * (ptr_temp++) << 6;
192             accum |= (UWord8) * (ptr_temp++) << 5;
193             accum |= (UWord8) * (ptr_temp++) << 4;
194             accum |= (UWord8) * (ptr_temp++) << 3;
195             accum |= (UWord8) * (ptr_temp++) << 2;
196             accum |= (UWord8) * (ptr_temp++) << 1;
197             accum |= (UWord8) * (ptr_temp++);
198
199             wmf_output_ptr[j++] = accum;
200         }
201
202         bits_left = numOfBits_ptr[frame_type_3gpp] -
203                     (numOfBits_ptr[frame_type_3gpp] & 0xFFF8);
204
205         wmf_output_ptr[j] = 0;
206
207         for (i = 0; i < bits_left; i++)
208         {
209             wmf_output_ptr[j] |= *(ptr_temp++) << (7 - i);
210         }
211     }
212
213     return;
214 }
215
216
217
218 void ets_to_ietf(
219     enum Frame_Type_3GPP frame_type_3gpp,
220     Word16 *ets_input_ptr,
221     UWord8 *ietf_output_ptr,
222     CommonAmrTbls* common_amr_tbls)
223 {
224     Word16  i;
225     Word16  k = 0;
226     Word16  j = 0;
227     Word16 *ptr_temp;
228     Word16  bits_left;
229     UWord8  accum;
230     const Word16* const* reorderBits_ptr = common_amr_tbls->reorderBits_ptr;
231     const Word16* numOfBits_ptr = common_amr_tbls->numOfBits_ptr;
232
233     ietf_output_ptr[j++] = (UWord8)(frame_type_3gpp << 3);
234
235     if (frame_type_3gpp < AMR_SID)
236     {
237         for (i = 0; i < numOfBits_ptr[frame_type_3gpp] - 7;)
238         {
239             ietf_output_ptr[j]  =
240                 (UWord8) ets_input_ptr[reorderBits_ptr[frame_type_3gpp][i++]] << 7;
241             ietf_output_ptr[j] |=
242                 (UWord8) ets_input_ptr[reorderBits_ptr[frame_type_3gpp][i++]] << 6;
243             ietf_output_ptr[j] |=
244                 (UWord8) ets_input_ptr[reorderBits_ptr[frame_type_3gpp][i++]] << 5;
245             ietf_output_ptr[j] |=
246                 (UWord8) ets_input_ptr[reorderBits_ptr[frame_type_3gpp][i++]] << 4;
247             ietf_output_ptr[j] |=
248                 (UWord8) ets_input_ptr[reorderBits_ptr[frame_type_3gpp][i++]] << 3;
249             ietf_output_ptr[j] |=
250                 (UWord8) ets_input_ptr[reorderBits_ptr[frame_type_3gpp][i++]] << 2;
251             ietf_output_ptr[j] |=
252                 (UWord8) ets_input_ptr[reorderBits_ptr[frame_type_3gpp][i++]] << 1;
253             ietf_output_ptr[j++] |=
254                 (UWord8) ets_input_ptr[reorderBits_ptr[frame_type_3gpp][i++]];
255         }
256
257         bits_left = numOfBits_ptr[frame_type_3gpp] -
258                     (numOfBits_ptr[frame_type_3gpp] & 0xFFF8);
259
260         ietf_output_ptr[j] = 0;
261
262         for (k = 0; k < bits_left; k++)
263         {
264             ietf_output_ptr[j] |=
265                 (UWord8) ets_input_ptr[reorderBits_ptr[frame_type_3gpp][i++]] << (7 - k);
266
267         }
268     }
269     else
270     {
271
272         ptr_temp = &ets_input_ptr[0];
273
274         for (i = numOfBits_ptr[frame_type_3gpp] - 7; i > 0; i -= 8)
275         {
276             accum  = (UWord8) * (ptr_temp++) << 7;
277             accum |= (UWord8) * (ptr_temp++) << 6;
278             accum |= (UWord8) * (ptr_temp++) << 5;
279             accum |= (UWord8) * (ptr_temp++) << 4;
280             accum |= (UWord8) * (ptr_temp++) << 3;
281             accum |= (UWord8) * (ptr_temp++) << 2;
282             accum |= (UWord8) * (ptr_temp++) << 1;
283             accum |= (UWord8) * (ptr_temp++);
284
285             ietf_output_ptr[j++] = accum;
286         }
287
288         bits_left = numOfBits_ptr[frame_type_3gpp] -
289                     (numOfBits_ptr[frame_type_3gpp] & 0xFFF8);
290
291         ietf_output_ptr[j] = 0;
292
293         for (i = 0; i < bits_left; i++)
294         {
295             ietf_output_ptr[j] |= *(ptr_temp++) << (7 - i);
296         }
297     }
298
299     return;
300 }