1 /* ------------------------------------------------------------------
2 * Copyright (C) 1998-2009 PacketVideo
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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
14 * See the License for the specific language governing permissions
15 * and limitations under the License.
16 * -------------------------------------------------------------------
18 /****************************************************************************************
19 Portions of this file are derived from the following 3GPP standard:
22 ANSI-C code for the Adaptive Multi-Rate (AMR) speech codec
23 Available from http://www.3gpp.org
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 ****************************************************************************************/
30 ------------------------------------------------------------------------------
32 Filename: ets_to_if2.cpp
37 /*----------------------------------------------------------------------------
39 ----------------------------------------------------------------------------*/
40 #include "frame_type_3gpp.h"
41 #include "ets_to_if2.h"
44 /*----------------------------------------------------------------------------
46 ; Define module specific macros here
47 ----------------------------------------------------------------------------*/
49 /*----------------------------------------------------------------------------
51 ; Include all pre-processor statements here. Include conditional
52 ; compile variables also.
53 ----------------------------------------------------------------------------*/
55 /*----------------------------------------------------------------------------
56 ; LOCAL FUNCTION DEFINITIONS
57 ; Function Prototype declaration
58 ----------------------------------------------------------------------------*/
60 /*----------------------------------------------------------------------------
61 ; LOCAL VARIABLE DEFINITIONS
62 ; Variable declaration - defined here and used outside this module
63 ----------------------------------------------------------------------------*/
67 ------------------------------------------------------------------------------
68 FUNCTION NAME: ets_to_if2
69 ------------------------------------------------------------------------------
70 INPUT AND OUTPUT DEFINITIONS
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)
78 if2_output_ptr = pointer to encoded speech bits in the IF2 format (UWord8)
83 Global Variables Used:
86 Local Variables Needed:
89 ------------------------------------------------------------------------------
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
100 ------------------------------------------------------------------------------
105 ------------------------------------------------------------------------------
108 AMR Speech Codec Frame Structure", 3GPP TS 26.101 version 4.1.0 Release 4, June 2001
110 ------------------------------------------------------------------------------
115 ------------------------------------------------------------------------------
117 [State any special notes, constraints or cautions for users of this function]
119 ------------------------------------------------------------------------------
123 enum Frame_Type_3GPP frame_type_3gpp,
124 Word16 *ets_input_ptr,
125 UWord8 *if2_output_ptr,
126 CommonAmrTbls* common_amr_tbls)
134 const Word16* const* reorderBits_ptr = common_amr_tbls->reorderBits_ptr;
135 const Word16* numOfBits_ptr = common_amr_tbls->numOfBits_ptr;
137 if (frame_type_3gpp < AMR_SID)
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);
145 for (i = 4; i < numOfBits_ptr[frame_type_3gpp] - 7;)
148 (UWord8) ets_input_ptr[reorderBits_ptr[frame_type_3gpp][i++]];
150 (UWord8) ets_input_ptr[reorderBits_ptr[frame_type_3gpp][i++]] << 1;
152 (UWord8) ets_input_ptr[reorderBits_ptr[frame_type_3gpp][i++]] << 2;
154 (UWord8) ets_input_ptr[reorderBits_ptr[frame_type_3gpp][i++]] << 3;
156 (UWord8) ets_input_ptr[reorderBits_ptr[frame_type_3gpp][i++]] << 4;
158 (UWord8) ets_input_ptr[reorderBits_ptr[frame_type_3gpp][i++]] << 5;
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;
165 bits_left = 4 + numOfBits_ptr[frame_type_3gpp] -
166 ((4 + numOfBits_ptr[frame_type_3gpp]) & 0xFFF8);
170 if2_output_ptr[j] = 0;
172 for (k = 0; k < bits_left; k++)
175 (UWord8) ets_input_ptr[reorderBits_ptr[frame_type_3gpp][i++]] << k;
181 if (frame_type_3gpp != AMR_NO_DATA)
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];
190 bits_left = ((4 + numOfBits_ptr[frame_type_3gpp]) & 0xFFF8);
192 for (i = (bits_left - 7) >> 3; i > 0; i--)
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;
203 if2_output_ptr[j++] = accum;
206 bits_left = 4 + numOfBits_ptr[frame_type_3gpp] - bits_left;
210 if2_output_ptr[j] = 0;
212 for (i = 0; i < bits_left; i++)
214 if2_output_ptr[j] |= (ptr_temp[i] << i);
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);