Initialize Tizen 2.3
[external/opencore-amr.git] / opencore / codecs_v2 / audio / gsm_amr / amr_nb / dec / src / if2_to_ets.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: if2_to_ets.cpp
33  Funtions: if2_to_ets
34
35 */
36
37 /*----------------------------------------------------------------------------
38 ; INCLUDES
39 ----------------------------------------------------------------------------*/
40 #include "frame_type_3gpp.h"
41 #include "if2_to_ets.h"
42 #include "typedef.h"
43 /*----------------------------------------------------------------------------
44 ; MACROS
45 ; Define module specific macros here
46 ----------------------------------------------------------------------------*/
47
48 /*----------------------------------------------------------------------------
49 ; DEFINES
50 ; Include all pre-processor statements here. Include conditional
51 ; compile variables also.
52 ----------------------------------------------------------------------------*/
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 ------------------------------------------------------------------------------
69  FUNCTION NAME: if2_to_ets
70 ------------------------------------------------------------------------------
71  INPUT AND OUTPUT DEFINITIONS
72
73  Inputs:
74     frame_type_3gpp = decoder speech bit rate (enum Frame_Type_3GPP)
75     if2_input_ptr   = pointer to input encoded speech bits in IF2 format (Word8)
76     ets_output_ptr  = pointer to output encoded speech bits in ETS format (Word16)
77
78  Outputs:
79     ets_output_ptr  = pointer to encoded speech bits in the ETS format (Word16)
80
81  Returns:
82     None
83
84  Global Variables Used:
85     None
86
87  Local Variables Needed:
88     None
89
90 ------------------------------------------------------------------------------
91  FUNCTION DESCRIPTION
92
93  This function performs a transformation on the data buffers. It converts the
94  data format from IF2 to ETS. IF2 is the storage format where the frame type
95  is in the first four bits of the first byte. The upper four bits of that byte
96  contain the first four encoded speech bits for the frame. The following bytes
97  contain the rest of the encoded speech bits. The final byte has padded zeros
98  to make the frame byte aligned. ETS format has the encoded speech
99  bits each separate with only one bit stored in each word.
100
101 ------------------------------------------------------------------------------
102  REQUIREMENTS
103
104  None
105
106 ------------------------------------------------------------------------------
107  REFERENCES
108
109 AMR Speech Codec Frame Structure", 3GPP TS 26.101 version 4.1.0 Release 4, June 2001
110
111 ------------------------------------------------------------------------------
112  PSEUDO-CODE
113
114
115
116 ------------------------------------------------------------------------------
117  CAUTION [optional]
118  [State any special notes, constraints or cautions for users of this function]
119
120 ------------------------------------------------------------------------------
121 */
122
123 void if2_to_ets(
124     enum Frame_Type_3GPP frame_type_3gpp,
125     UWord8   *if2_input_ptr,
126     Word16   *ets_output_ptr,
127     CommonAmrTbls* common_amr_tbls)
128 {
129
130     Word16 i;
131     Word16 j;
132     Word16 x = 0;
133     const Word16* numCompressedBytes_ptr = common_amr_tbls->numCompressedBytes_ptr;
134     const Word16* numOfBits_ptr = common_amr_tbls->numOfBits_ptr;
135     const Word16* const* reorderBits_ptr = common_amr_tbls->reorderBits_ptr;
136
137     /*
138      * The following section of code accesses bits in the IF2 method of
139      * bit ordering. Each bit is given its own location in the buffer pointed
140      * to by ets_output_ptr. The bits (for modes less than AMR_SID) are
141      * reordered using the tables in bitreorder.c before the data is stored
142      * into the buffer pointed to by ets_output_ptr.
143      */
144
145     if (frame_type_3gpp < AMR_SID)
146     {
147         for (j = 4; j < 8; j++)
148         {
149             ets_output_ptr[reorderBits_ptr[frame_type_3gpp][x++]] =
150                 (if2_input_ptr[0] >> j) & 0x01;
151         }
152         for (i = 1; i < numCompressedBytes_ptr[frame_type_3gpp]; i++)
153         {
154             for (j = 0; j < 8; j++)
155             {
156                 if (x >= numOfBits_ptr[frame_type_3gpp])
157                 {
158                     break;
159                 }
160                 ets_output_ptr[reorderBits_ptr[frame_type_3gpp][x++]] =
161                     (if2_input_ptr[i] >> j) & 0x01;
162             }
163         }
164     }
165     else
166     {
167         for (j = 4; j < 8; j++)
168         {
169             ets_output_ptr[x++] =
170                 (if2_input_ptr[0] >> j) & 0x01;
171         }
172         for (i = 1; i < numCompressedBytes_ptr[frame_type_3gpp]; i++)
173         {
174             for (j = 0; j < 8; j++)
175             {
176                 ets_output_ptr[x++] =
177                     (if2_input_ptr[i] >> j) & 0x01;
178             }
179         }
180     }
181
182     return;
183 }