Git init
[external/opencore-amr.git] / opencore / codecs_v2 / audio / gsm_amr / amr_wb / dec / src / dtx.h
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.173
22     ANSI-C code for the Adaptive Multi-Rate - Wideband (AMR-WB) speech codec
23     Available from http://www.3gpp.org
24
25 (C) 2007, 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  Pathname: ./cpp/include/dtx.h
35
36 ------------------------------------------------------------------------------
37  INCLUDE DESCRIPTION
38     Static memory, constants and frametypes for the DTX
39 ------------------------------------------------------------------------------
40 */
41 #ifndef DTX_H
42 #define DTX_H
43
44
45 /*----------------------------------------------------------------------------
46 ; INCLUDES
47 ----------------------------------------------------------------------------*/
48
49 #ifdef __cplusplus
50 extern "C"
51 {
52 #endif
53
54     /*----------------------------------------------------------------------------
55     ; MACROS
56     ; Define module specific macros here
57     ----------------------------------------------------------------------------*/
58
59     /*----------------------------------------------------------------------------
60     ; EXTERNAL VARIABLES REFERENCES
61     ----------------------------------------------------------------------------*/
62
63     /*----------------------------------------------------------------------------
64     ; DEFINES AND SIMPLE TYPEDEF'S
65     ----------------------------------------------------------------------------*/
66
67 #define DTX_MAX_EMPTY_THRESH 50
68 #define DTX_HIST_SIZE 8
69 #define DTX_HIST_SIZE_MIN_ONE 7
70 #define DTX_ELAPSED_FRAMES_THRESH (24 + 7 -1)
71 #define DTX_HANG_CONST 7                   /* yields eight frames of SP HANGOVER  */
72 #define INV_MED_THRESH 14564
73 #define ISF_GAP  128                       /* 50 */
74 #define ONE_MINUS_ISF_GAP 16384 - ISF_GAP
75
76 #define ISF_GAP   128
77 #define ISF_DITH_GAP   448
78 #define ISF_FACTOR_LOW 256
79 #define ISF_FACTOR_STEP 2
80
81 #define GAIN_THR 180
82 #define GAIN_FACTOR 75
83
84     typedef struct
85     {
86         int16 isf_hist[M * DTX_HIST_SIZE];
87         int16 log_en_hist[DTX_HIST_SIZE];
88         int16 hist_ptr;
89         int16 log_en_index;
90         int16 cng_seed;
91
92         /* DTX handler stuff */
93         int16 dtxHangoverCount;
94         int16 decAnaElapsedCount;
95         int32 D[28];
96         int32 sumD[DTX_HIST_SIZE];
97     } dtx_encState;
98
99 #define SPEECH 0
100 #define DTX 1
101 #define DTX_MUTE 2
102
103 #define TX_SPEECH 0
104 #define TX_SID_FIRST 1
105 #define TX_SID_UPDATE 2
106 #define TX_NO_DATA 3
107
108 #define RX_SPEECH_GOOD 0
109 #define RX_SPEECH_PROBABLY_DEGRADED 1
110 #define RX_SPEECH_LOST 2
111 #define RX_SPEECH_BAD 3
112 #define RX_SID_FIRST 4
113 #define RX_SID_UPDATE 5
114 #define RX_SID_BAD 6
115 #define RX_NO_DATA 7
116
117     /*****************************************************************************
118      *
119      * DEFINITION OF DATA TYPES
120      *****************************************************************************/
121
122     typedef struct
123     {
124         int16 since_last_sid;
125         int16 true_sid_period_inv;
126         int16 log_en;
127         int16 old_log_en;
128         int16 level;
129         int16 isf[M];
130         int16 isf_old[M];
131         int16 cng_seed;
132
133         int16 isf_hist[M * DTX_HIST_SIZE];
134         int16 log_en_hist[DTX_HIST_SIZE];
135         int16 hist_ptr;
136
137         int16 dtxHangoverCount;
138         int16 decAnaElapsedCount;
139
140         int16 sid_frame;
141         int16 valid_data;
142         int16 dtxHangoverAdded;
143
144         int16 dtxGlobalState;                 /* contains previous state */
145         /* updated in main decoder */
146
147         int16 data_updated;                   /* marker to know if CNI data is ever renewed */
148
149         int16 dither_seed;
150         int16 CN_dith;
151
152     } dtx_decState;
153
154     int16 dtx_enc_init(dtx_encState ** st, int16 isf_init[]);
155     int16 dtx_enc_reset(dtx_encState * st, int16 isf_init[]);
156     void dtx_enc_exit(dtx_encState ** st);
157
158     int16 dtx_enc(
159         dtx_encState * st,                    /* i/o : State struct                                         */
160         int16 isf[M],                        /* o   : CN ISF vector                                        */
161         int16 * exc2,                        /* o   : CN excitation                                        */
162         int16 ** prms
163     );
164
165     int16 dtx_buffer(
166         dtx_encState * st,                    /* i/o : State struct                    */
167         int16 isf_new[],                     /* i   : isf vector                      */
168         int32 enr,                           /* i   : residual energy (in L_FRAME)    */
169         int16 codec_mode
170     );
171
172     void tx_dtx_handler(dtx_encState * st,     /* i/o : State struct           */
173                         int16 vad_flag,                      /* i   : vad decision           */
174                         int16 * usedMode                     /* i/o : mode changed or not    */
175                        );
176
177     void Qisf_ns(
178         int16 * isf1,                        /* input : ISF in the frequency domain (0..0.5) */
179         int16 * isf_q,                       /* output: quantized ISF                        */
180         int16 * indice                       /* output: quantization indices                 */
181     );
182
183
184     int16 dtx_dec_amr_wb_reset(dtx_decState * st, const int16 isf_init[]);
185
186     int16 dtx_dec_amr_wb(
187         dtx_decState * st,                    /* i/o : State struct                                          */
188         int16 * exc2,                        /* o   : CN excitation                                          */
189         int16 new_state,                     /* i   : New DTX state                                          */
190         int16 isf[],                         /* o   : CN ISF vector                                          */
191         int16 ** prms
192     );
193
194     void dtx_dec_amr_wb_activity_update(
195         dtx_decState * st,
196         int16 isf[],
197         int16 exc[]);
198
199
200     int16 rx_amr_wb_dtx_handler(
201         dtx_decState * st,                    /* i/o : State struct     */
202         int16 frame_type                     /* i   : Frame type       */
203     );
204
205     void Disf_ns(
206         int16 * indice,                      /* input:  quantization indices                  */
207         int16 * isf_q                        /* input : ISF in the frequency domain (0..0.5)  */
208     );
209
210     void aver_isf_history(
211         int16 isf_old[],
212         int16 indices[],
213         int32 isf_aver[]
214     );
215     void find_frame_indices(
216         int16 isf_old_tx[],
217         int16 indices[],
218         dtx_encState * st
219     );
220
221     int16 dithering_control(
222         dtx_encState * st
223     );
224     void CN_dithering(
225         int16 isf[M],
226         int32 * L_log_en_int,
227         int16 * dither_seed
228     );
229
230 #ifdef __cplusplus
231 }
232 #endif
233
234 #endif  /*  DTX_H  */
235
236