Git init
[external/opencore-amr.git] / opencore / codecs_v2 / audio / gsm_amr / amr_nb / enc / src / ets_to_if2.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  Filename: ets_to_if2.cpp
33  Funtions: ets_to_if2
34
35 */
36
37 /*----------------------------------------------------------------------------
38 ; INCLUDES
39 ----------------------------------------------------------------------------*/
40 #include "frame_type_3gpp.h"
41 #include "ets_to_if2.h"
42 #include "typedef.h"
43
44 /*----------------------------------------------------------------------------
45 ; MACROS
46 ; Define module specific macros here
47 ----------------------------------------------------------------------------*/
48
49 /*----------------------------------------------------------------------------
50 ; DEFINES
51 ; Include all pre-processor statements here. Include conditional
52 ; compile variables also.
53 ----------------------------------------------------------------------------*/
54
55 /*----------------------------------------------------------------------------
56 ; LOCAL FUNCTION DEFINITIONS
57 ; Function Prototype declaration
58 ----------------------------------------------------------------------------*/
59
60 /*----------------------------------------------------------------------------
61 ; LOCAL VARIABLE DEFINITIONS
62 ; Variable declaration - defined here and used outside this module
63 ----------------------------------------------------------------------------*/
64
65
66 /*
67 ------------------------------------------------------------------------------
68  FUNCTION NAME: ets_to_if2
69 ------------------------------------------------------------------------------
70  INPUT AND OUTPUT DEFINITIONS
71
72  Inputs:
73     frame_type_3gpp = decoder speech bit rate (enum Frame_Type_3GPP)
74     ets_input_ptr   = pointer to input encoded speech bits in ETS format (Word16)
75     if2_output_ptr  = pointer to output encoded speech bits in IF2 format (UWord8)
76
77  Outputs:
78     if2_output_ptr  = pointer to encoded speech bits in the IF2 format (UWord8)
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  This function performs a transformation on the data buffers. It converts the
93  data format from ETS (European Telecommunication Standard) to IF2. ETS format
94  has the encoded speech bits each separate with only one bit stored in each
95  word. IF2 is the storage format where the frame type is in the first four bits
96  of the first byte. The upper four bits of that byte contain the first four
97  encoded speech bits for the frame. The following bytes contain the rest of
98  the encoded speech bits. The final byte has padded zeros  to make the frame
99  byte aligned.
100 ------------------------------------------------------------------------------
101  REQUIREMENTS
102
103  None
104
105 ------------------------------------------------------------------------------
106  REFERENCES
107
108 AMR Speech Codec Frame Structure", 3GPP TS 26.101 version 4.1.0 Release 4, June 2001
109
110 ------------------------------------------------------------------------------
111  PSEUDO-CODE
112
113
114
115 ------------------------------------------------------------------------------
116  CAUTION [optional]
117  [State any special notes, constraints or cautions for users of this function]
118
119 ------------------------------------------------------------------------------
120 */
121
122 void ets_to_if2(
123     enum Frame_Type_3GPP frame_type_3gpp,
124     Word16 *ets_input_ptr,
125     UWord8 *if2_output_ptr,
126     CommonAmrTbls* common_amr_tbls)
127 {
128     Word16  i;
129     Word16  k;
130     Word16  j = 0;
131     Word16 *ptr_temp;
132     Word16  bits_left;
133     UWord8  accum;
134     const Word16* const* reorderBits_ptr = common_amr_tbls->reorderBits_ptr;
135     const Word16* numOfBits_ptr = common_amr_tbls->numOfBits_ptr;
136
137     if (frame_type_3gpp < AMR_SID)
138     {
139         if2_output_ptr[j++] = (UWord8)(frame_type_3gpp) |
140                               (ets_input_ptr[reorderBits_ptr[frame_type_3gpp][0]] << 4) |
141                               (ets_input_ptr[reorderBits_ptr[frame_type_3gpp][1]] << 5) |
142                               (ets_input_ptr[reorderBits_ptr[frame_type_3gpp][2]] << 6) |
143                               (ets_input_ptr[reorderBits_ptr[frame_type_3gpp][3]] << 7);
144
145         for (i = 4; i < numOfBits_ptr[frame_type_3gpp] - 7;)
146         {
147             if2_output_ptr[j]  =
148                 (UWord8) ets_input_ptr[reorderBits_ptr[frame_type_3gpp][i++]];
149             if2_output_ptr[j] |=
150                 (UWord8) ets_input_ptr[reorderBits_ptr[frame_type_3gpp][i++]] << 1;
151             if2_output_ptr[j] |=
152                 (UWord8) ets_input_ptr[reorderBits_ptr[frame_type_3gpp][i++]] << 2;
153             if2_output_ptr[j] |=
154                 (UWord8) ets_input_ptr[reorderBits_ptr[frame_type_3gpp][i++]] << 3;
155             if2_output_ptr[j] |=
156                 (UWord8) ets_input_ptr[reorderBits_ptr[frame_type_3gpp][i++]] << 4;
157             if2_output_ptr[j] |=
158                 (UWord8) ets_input_ptr[reorderBits_ptr[frame_type_3gpp][i++]] << 5;
159             if2_output_ptr[j] |=
160                 (UWord8) ets_input_ptr[reorderBits_ptr[frame_type_3gpp][i++]] << 6;
161             if2_output_ptr[j++] |=
162                 (UWord8) ets_input_ptr[reorderBits_ptr[frame_type_3gpp][i++]] << 7;
163         }
164
165         bits_left = 4 + numOfBits_ptr[frame_type_3gpp] -
166                     ((4 + numOfBits_ptr[frame_type_3gpp]) & 0xFFF8);
167
168         if (bits_left != 0)
169         {
170             if2_output_ptr[j] = 0;
171
172             for (k = 0; k < bits_left; k++)
173             {
174                 if2_output_ptr[j] |=
175                     (UWord8) ets_input_ptr[reorderBits_ptr[frame_type_3gpp][i++]] << k;
176             }
177         }
178     }
179     else
180     {
181         if (frame_type_3gpp != AMR_NO_DATA)
182         {
183             /* First octet contains 3GPP frame type and */
184             /* first 4 bits of encoded parameters       */
185             if2_output_ptr[j++] = (UWord8)(frame_type_3gpp) |
186                                   (ets_input_ptr[0] << 4) | (ets_input_ptr[1] << 5) |
187                                   (ets_input_ptr[2] << 6) | (ets_input_ptr[3] << 7);
188             ptr_temp = &ets_input_ptr[4];
189
190             bits_left = ((4 + numOfBits_ptr[frame_type_3gpp]) & 0xFFF8);
191
192             for (i = (bits_left - 7) >> 3; i > 0; i--)
193             {
194                 accum  = (UWord8) * (ptr_temp++);
195                 accum |= (UWord8) * (ptr_temp++) << 1;
196                 accum |= (UWord8) * (ptr_temp++) << 2;
197                 accum |= (UWord8) * (ptr_temp++) << 3;
198                 accum |= (UWord8) * (ptr_temp++) << 4;
199                 accum |= (UWord8) * (ptr_temp++) << 5;
200                 accum |= (UWord8) * (ptr_temp++) << 6;
201                 accum |= (UWord8) * (ptr_temp++) << 7;
202
203                 if2_output_ptr[j++] = accum;
204             }
205
206             bits_left = 4 + numOfBits_ptr[frame_type_3gpp] - bits_left;
207
208             if (bits_left != 0)
209             {
210                 if2_output_ptr[j] = 0;
211
212                 for (i = 0; i < bits_left; i++)
213                 {
214                     if2_output_ptr[j] |= (ptr_temp[i] << i);
215                 }
216             }
217         }
218         else
219         {
220             /* When there is no data, LSnibble of first octet */
221             /* is the 3GPP frame type, MSnibble is zeroed out */
222             if2_output_ptr[j++] = (UWord8)(frame_type_3gpp);
223         }
224
225     }
226
227     return;
228 }