summaryrefslogtreecommitdiffstats
path: root/media/libstagefright/codecs/amrwb/src/lagconceal.cpp
blob: c699808f95d44281ab456cdee49dc5d04e243222 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
/* ------------------------------------------------------------------
 * Copyright (C) 1998-2009 PacketVideo
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either
 * express or implied.
 * See the License for the specific language governing permissions
 * and limitations under the License.
 * -------------------------------------------------------------------
 */
/****************************************************************************************
Portions of this file are derived from the following 3GPP standard:

    3GPP TS 26.173
    ANSI-C code for the Adaptive Multi-Rate - Wideband (AMR-WB) speech codec
    Available from http://www.3gpp.org

(C) 2007, 3GPP Organizational Partners (ARIB, ATIS, CCSA, ETSI, TTA, TTC)
Permission to distribute, modify and use this file under the standard license
terms listed above has been obtained from the copyright holder.
****************************************************************************************/
/*
------------------------------------------------------------------------------



 Filename: lagconceal.cpp

     Date: 05/08/2007

------------------------------------------------------------------------------
 REVISION HISTORY


 Description:

------------------------------------------------------------------------------
 INPUT AND OUTPUT DEFINITIONS

     int16 gain_hist[],                     (i)  : Gain history
     int16 lag_hist[],                      (i)  : Subframe size
     int16 * T0,                            (i/o): current lag
     int16 * old_T0,                        (i/o): previous lag
     int16 * seed,
     int16 unusable_frame

------------------------------------------------------------------------------
 FUNCTION DESCRIPTION

    Concealment of LTP lags during bad frames

------------------------------------------------------------------------------
 REQUIREMENTS


------------------------------------------------------------------------------
 REFERENCES

------------------------------------------------------------------------------
 PSEUDO-CODE

------------------------------------------------------------------------------
*/


/*----------------------------------------------------------------------------
; INCLUDES
----------------------------------------------------------------------------*/

#include "pv_amr_wb_type_defs.h"
#include "pvamrwbdecoder_basic_op.h"
#include "pvamrwbdecoder_cnst.h"
#include "pvamrwbdecoder_acelp.h"

/*----------------------------------------------------------------------------
; MACROS
; Define module specific macros here
----------------------------------------------------------------------------*/


/*----------------------------------------------------------------------------
; DEFINES
; Include all pre-processor statements here. Include conditional
; compile variables also.
----------------------------------------------------------------------------*/
#define L_LTPHIST 5
#define ONE_PER_3 10923
#define ONE_PER_LTPHIST 6554

/*----------------------------------------------------------------------------
; LOCAL FUNCTION DEFINITIONS
; Function Prototype declaration
----------------------------------------------------------------------------*/
void insertion_sort(int16 array[], int16 n);
void insert(int16 array[], int16 num, int16 x);

/*----------------------------------------------------------------------------
; LOCAL STORE/BUFFER/POINTER DEFINITIONS
; Variable declaration - defined here and used outside this module
----------------------------------------------------------------------------*/

/*----------------------------------------------------------------------------
; EXTERNAL FUNCTION REFERENCES
; Declare functions defined elsewhere and referenced in this module
----------------------------------------------------------------------------*/

/*----------------------------------------------------------------------------
; EXTERNAL GLOBAL STORE/BUFFER/POINTER REFERENCES
; Declare variables used in this module but defined elsewhere
----------------------------------------------------------------------------*/

/*----------------------------------------------------------------------------
; FUNCTION CODE
----------------------------------------------------------------------------*/


void Init_Lagconc(int16 lag_hist[])
{
    int16 i;

    for (i = 0; i < L_LTPHIST; i++)
    {
        lag_hist[i] = 64;
    }
}

/*----------------------------------------------------------------------------
; FUNCTION CODE
----------------------------------------------------------------------------*/

void lagconceal(
    int16 gain_hist[],                   /* (i) : Gain history     */
    int16 lag_hist[],                    /* (i) : Subframe size    */
    int16 * T0,
    int16 * old_T0,
    int16 * seed,
    int16 unusable_frame
)
{
    int16 maxLag, minLag, lastLag, lagDif, meanLag = 0;
    int16 lag_hist2[L_LTPHIST] = {0};
    int16 i, tmp, tmp2;
    int16 minGain, lastGain, secLastGain;
    int16 D, D2;

    /* Is lag index such that it can be aplied directly or does it has to be subtituted */

    lastGain = gain_hist[4];
    secLastGain = gain_hist[3];

    lastLag = lag_hist[0];

    /******* SMALLEST history lag *******/
    minLag = lag_hist[0];
    /*******  BIGGEST history lag *******/
    maxLag = lag_hist[0];
    for (i = 1; i < L_LTPHIST; i++)
    {
        if (lag_hist[i] < minLag)
        {
            minLag = lag_hist[i];
        }
        if (lag_hist[i] > maxLag)
        {
            maxLag = lag_hist[i];
        }
    }
    /***********SMALLEST history gain***********/
    minGain = gain_hist[0];
    for (i = 1; i < L_LTPHIST; i++)
    {

        if (gain_hist[i] < minGain)
        {
            minGain = gain_hist[i];
        }
    }
    /***Difference between MAX and MIN lag**/
    lagDif = sub_int16(maxLag, minLag);


    if (unusable_frame != 0)
    {
        /* LTP-lag for RX_SPEECH_LOST */
        /**********Recognition of the LTP-history*********/

        if ((minGain > 8192) && (lagDif < 10))
        {
            *T0 = *old_T0;
        }
        else if (lastGain > 8192 && secLastGain > 8192)
        {
            *T0 = lag_hist[0];
        }
        else
        {
            /********SORT************/
            /* The sorting of the lag history */
            for (i = 0; i < L_LTPHIST; i++)
            {
                lag_hist2[i] = lag_hist[i];
            }
            insertion_sort(lag_hist2, 5);

            /* Lag is weighted towards bigger lags */
            /* and random variation is added */
            lagDif = sub_int16(lag_hist2[4], lag_hist2[2]);


            if (lagDif > 40)
            {
                lagDif = 40;
            }

            D = noise_gen_amrwb(seed);              /* D={-1, ...,1} */
            /* D2={-lagDif/2..lagDif/2} */
            tmp = lagDif >> 1;
            D2 = mult_int16(tmp, D);
            tmp = add_int16(add_int16(lag_hist2[2], lag_hist2[3]), lag_hist2[4]);
            *T0 = add_int16(mult_int16(tmp, ONE_PER_3), D2);
        }
        /* New lag is not allowed to be bigger or smaller than last lag values */

        if (*T0 > maxLag)
        {
            *T0 = maxLag;
        }

        if (*T0 < minLag)
        {
            *T0 = minLag;
        }
    }
    else
    {
        /* LTP-lag for RX_BAD_FRAME */

        /***********MEAN lag**************/
        meanLag = 0;
        for (i = 0; i < L_LTPHIST; i++)
        {
            meanLag = add_int16(meanLag, lag_hist[i]);
        }
        meanLag = mult_int16(meanLag, ONE_PER_LTPHIST);

        tmp  = *T0 - maxLag;
        tmp2 = *T0 - lastLag;

        if ((lagDif < 10) && (*T0 > (minLag - 5)) && (tmp < 5))
        {
            *T0 = *T0;
        }
        else if ((lastGain > 8192) && (secLastGain > 8192) && ((tmp2 + 10) > 0 && tmp2 < 10))
        {
            *T0 = *T0;
        }
        else if ((minGain < 6554) && (lastGain == minGain) && (*T0 > minLag && *T0 < maxLag))
        {
            *T0 = *T0;
        }
        else if ((lagDif < 70) && (*T0 > minLag) && (*T0 < maxLag))
        {
            *T0 = *T0;
        }
        else if ((*T0 > meanLag) && (*T0 < maxLag))
        {
            *T0 = *T0;
        }
        else
        {


            if ((minGain > 8192) & (lagDif < 10))
            {
                *T0 = lag_hist[0];
            }
            else if ((lastGain > 8192) && (secLastGain > 8192))
            {
                *T0 = lag_hist[0];
            }
            else
            {
                /********SORT************/
                /* The sorting of the lag history */
                for (i = 0; i < L_LTPHIST; i++)
                {
                    lag_hist2[i] = lag_hist[i];
                }
                insertion_sort(lag_hist2, 5);

                /* Lag is weighted towards bigger lags */
                /* and random variation is added */
                lagDif = sub_int16(lag_hist2[4], lag_hist2[2]);

                if (lagDif > 40)
                {
                    lagDif = 40;
                }

                D = noise_gen_amrwb(seed);          /* D={-1,.., 1} */
                /* D2={-lagDif/2..lagDif/2} */
                tmp = lagDif >> 1;
                D2 = mult_int16(tmp, D);
                tmp = add_int16(add_int16(lag_hist2[2], lag_hist2[3]), lag_hist2[4]);
                *T0 = add_int16(mult_int16(tmp, ONE_PER_3), D2);
            }
            /* New lag is not allowed to be bigger or smaller than last lag values */

            if (*T0 > maxLag)
            {
                *T0 = maxLag;
            }

            if (*T0 < minLag)
            {
                *T0 = minLag;
            }
        }
    }
}

/*----------------------------------------------------------------------------
; FUNCTION CODE
----------------------------------------------------------------------------*/

void insertion_sort(int16 array[], int16 n)
{
    int16 i;

    for (i = 0; i < n; i++)
    {
        insert(array, i, array[i]);
    }
}

/*----------------------------------------------------------------------------
; FUNCTION CODE
----------------------------------------------------------------------------*/

void insert(int16 array[], int16 n, int16 x)
{
    int16 i;

    for (i = (n - 1); i >= 0; i--)
    {

        if (x < array[i])
        {
            array[i + 1] = array[i];
        }
        else
        {
            break;
        }
    }
    array[i + 1] = x;
}