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 - Wideband (AMR-WB) speech codec
23 Available from http://www.3gpp.org
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 ****************************************************************************************/
30 ------------------------------------------------------------------------------
34 Filename: lagconceal.cpp
36 ------------------------------------------------------------------------------
37 INPUT AND OUTPUT DEFINITIONS
39 int16 gain_hist[], (i) : Gain history
40 int16 lag_hist[], (i) : Subframe size
41 int16 * T0, (i/o): current lag
42 int16 * old_T0, (i/o): previous lag
46 ------------------------------------------------------------------------------
49 Concealment of LTP lags during bad frames
51 ------------------------------------------------------------------------------
55 ------------------------------------------------------------------------------
58 ------------------------------------------------------------------------------
61 ------------------------------------------------------------------------------
65 /*----------------------------------------------------------------------------
67 ----------------------------------------------------------------------------*/
69 #include "pv_amr_wb_type_defs.h"
70 #include "pvamrwbdecoder_basic_op.h"
71 #include "pvamrwbdecoder_cnst.h"
72 #include "pvamrwbdecoder_acelp.h"
74 /*----------------------------------------------------------------------------
76 ; Define module specific macros here
77 ----------------------------------------------------------------------------*/
80 /*----------------------------------------------------------------------------
82 ; Include all pre-processor statements here. Include conditional
83 ; compile variables also.
84 ----------------------------------------------------------------------------*/
86 #define ONE_PER_3 10923
87 #define ONE_PER_LTPHIST 6554
89 /*----------------------------------------------------------------------------
90 ; LOCAL FUNCTION DEFINITIONS
91 ; Function Prototype declaration
92 ----------------------------------------------------------------------------*/
93 void insertion_sort(int16 array[], int16 n);
94 void insert(int16 array[], int16 num, int16 x);
96 /*----------------------------------------------------------------------------
97 ; LOCAL STORE/BUFFER/POINTER DEFINITIONS
98 ; Variable declaration - defined here and used outside this module
99 ----------------------------------------------------------------------------*/
101 /*----------------------------------------------------------------------------
102 ; EXTERNAL FUNCTION REFERENCES
103 ; Declare functions defined elsewhere and referenced in this module
104 ----------------------------------------------------------------------------*/
106 /*----------------------------------------------------------------------------
107 ; EXTERNAL GLOBAL STORE/BUFFER/POINTER REFERENCES
108 ; Declare variables used in this module but defined elsewhere
109 ----------------------------------------------------------------------------*/
111 /*----------------------------------------------------------------------------
113 ----------------------------------------------------------------------------*/
116 void Init_Lagconc(int16 lag_hist[])
120 for (i = 0; i < L_LTPHIST; i++)
126 /*----------------------------------------------------------------------------
128 ----------------------------------------------------------------------------*/
131 int16 gain_hist[], /* (i) : Gain history */
132 int16 lag_hist[], /* (i) : Subframe size */
139 int16 maxLag, minLag, lastLag, lagDif, meanLag = 0;
140 int16 lag_hist2[L_LTPHIST] = {0};
142 int16 minGain, lastGain, secLastGain;
145 /* Is lag index such that it can be aplied directly or does it has to be subtituted */
147 lastGain = gain_hist[4];
148 secLastGain = gain_hist[3];
150 lastLag = lag_hist[0];
152 /******* SMALLEST history lag *******/
153 minLag = lag_hist[0];
154 /******* BIGGEST history lag *******/
155 maxLag = lag_hist[0];
156 for (i = 1; i < L_LTPHIST; i++)
158 if (lag_hist[i] < minLag)
160 minLag = lag_hist[i];
162 if (lag_hist[i] > maxLag)
164 maxLag = lag_hist[i];
167 /***********SMALLEST history gain***********/
168 minGain = gain_hist[0];
169 for (i = 1; i < L_LTPHIST; i++)
172 if (gain_hist[i] < minGain)
174 minGain = gain_hist[i];
177 /***Difference between MAX and MIN lag**/
178 lagDif = sub_int16(maxLag, minLag);
181 if (unusable_frame != 0)
183 /* LTP-lag for RX_SPEECH_LOST */
184 /**********Recognition of the LTP-history*********/
186 if ((minGain > 8192) && (lagDif < 10))
190 else if (lastGain > 8192 && secLastGain > 8192)
196 /********SORT************/
197 /* The sorting of the lag history */
198 for (i = 0; i < L_LTPHIST; i++)
200 lag_hist2[i] = lag_hist[i];
202 insertion_sort(lag_hist2, 5);
204 /* Lag is weighted towards bigger lags */
205 /* and random variation is added */
206 lagDif = sub_int16(lag_hist2[4], lag_hist2[2]);
214 D = noise_gen_amrwb(seed); /* D={-1, ...,1} */
215 /* D2={-lagDif/2..lagDif/2} */
217 D2 = mult_int16(tmp, D);
218 tmp = add_int16(add_int16(lag_hist2[2], lag_hist2[3]), lag_hist2[4]);
219 *T0 = add_int16(mult_int16(tmp, ONE_PER_3), D2);
221 /* New lag is not allowed to be bigger or smaller than last lag values */
235 /* LTP-lag for RX_BAD_FRAME */
237 /***********MEAN lag**************/
239 for (i = 0; i < L_LTPHIST; i++)
241 meanLag = add_int16(meanLag, lag_hist[i]);
243 meanLag = mult_int16(meanLag, ONE_PER_LTPHIST);
246 tmp2 = *T0 - lastLag;
248 if ((lagDif < 10) && (*T0 > (minLag - 5)) && (tmp < 5))
252 else if ((lastGain > 8192) && (secLastGain > 8192) && ((tmp2 + 10) > 0 && tmp2 < 10))
256 else if ((minGain < 6554) && (lastGain == minGain) && (*T0 > minLag && *T0 < maxLag))
260 else if ((lagDif < 70) && (*T0 > minLag) && (*T0 < maxLag))
264 else if ((*T0 > meanLag) && (*T0 < maxLag))
272 if ((minGain > 8192) & (lagDif < 10))
276 else if ((lastGain > 8192) && (secLastGain > 8192))
282 /********SORT************/
283 /* The sorting of the lag history */
284 for (i = 0; i < L_LTPHIST; i++)
286 lag_hist2[i] = lag_hist[i];
288 insertion_sort(lag_hist2, 5);
290 /* Lag is weighted towards bigger lags */
291 /* and random variation is added */
292 lagDif = sub_int16(lag_hist2[4], lag_hist2[2]);
299 D = noise_gen_amrwb(seed); /* D={-1,.., 1} */
300 /* D2={-lagDif/2..lagDif/2} */
302 D2 = mult_int16(tmp, D);
303 tmp = add_int16(add_int16(lag_hist2[2], lag_hist2[3]), lag_hist2[4]);
304 *T0 = add_int16(mult_int16(tmp, ONE_PER_3), D2);
306 /* New lag is not allowed to be bigger or smaller than last lag values */
321 /*----------------------------------------------------------------------------
323 ----------------------------------------------------------------------------*/
325 void insertion_sort(int16 array[], int16 n)
329 for (i = 0; i < n; i++)
331 insert(array, i, array[i]);
335 /*----------------------------------------------------------------------------
337 ----------------------------------------------------------------------------*/
339 void insert(int16 array[], int16 n, int16 x)
343 for (i = (n - 1); i >= 0; i--)
348 array[i + 1] = array[i];