Git init
[external/opencore-amr.git] / opencore / codecs_v2 / audio / gsm_amr / amr_nb / dec / src / d_plsf_3.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
33
34  Filename: d_plsf_3.cpp
35  Functions: D_plsf_3
36
37  ------------------------------------------------------------------------------
38  INPUT AND OUTPUT DEFINITIONS
39
40  Inputs:
41     st -- Pointer to type struct D_plsfState
42     mode -- enum Mode -- coder mode
43     bfi -- Word16 -- bad frame indicator (set to 1 if a bad frame is received)
44     indice -- Pointer to type Word16 -- quantization indices of
45                                         3 submatrices, Q0
46
47  Outputs:
48     st -- Pointer to type struct D_plsfState
49     lsp1_q -- Pointer to type Word16 -- quantized 1st LSP vector Q15
50     pOverflow -- Pointer to type Flag -- Flag set when overflow occurs
51
52  Returns:
53     None.
54
55  Global Variables Used:
56     None
57
58  Local Variables Needed:
59     None
60
61 ------------------------------------------------------------------------------
62  FUNCTION DESCRIPTION
63
64  PURPOSE: Decodes the LSP parameters using the received quantization
65           indices.1st order MA prediction and split by 3 vector
66           quantization (split-VQ)
67
68 ------------------------------------------------------------------------------
69  REQUIREMENTS
70
71
72
73 ------------------------------------------------------------------------------
74  REFERENCES
75
76  d_plsf_3.c, UMTS GSM AMR speech codec, R99 - Version 3.2.0, March 2, 2001
77
78 ------------------------------------------------------------------------------
79  PSEUDO-CODE
80
81
82
83 ------------------------------------------------------------------------------
84 */
85
86
87 /*----------------------------------------------------------------------------
88 ; INCLUDES
89 ----------------------------------------------------------------------------*/
90 #include "d_plsf.h"
91 #include "typedef.h"
92 #include "basic_op.h"
93 #include "lsp_lsf.h"
94 #include "reorder.h"
95 #include "oscl_mem.h"
96 #include "q_plsf_3_tbl.h"
97
98
99 /*----------------------------------------------------------------------------
100 ; MACROS
101 ; Define module specific macros here
102 ----------------------------------------------------------------------------*/
103
104
105 /*----------------------------------------------------------------------------
106 ; DEFINES
107 ; Include all pre-processor statements here. Include conditional
108 ; compile variables also.
109 ----------------------------------------------------------------------------*/
110 #define ALPHA     29491     /* ALPHA    ->  0.9                            */
111 #define ONE_ALPHA 3277      /* ONE_ALPHA-> (1.0-ALPHA)                     */
112
113
114 /*----------------------------------------------------------------------------
115 ; LOCAL FUNCTION DEFINITIONS
116 ; Function Prototype declaration
117 ----------------------------------------------------------------------------*/
118
119
120 /*----------------------------------------------------------------------------
121 ; LOCAL STORE/BUFFER/POINTER DEFINITIONS
122 ; Variable declaration - defined here and used outside this module
123 ----------------------------------------------------------------------------*/
124
125 /*----------------------------------------------------------------------------
126 ; EXTERNAL FUNCTION REFERENCES
127 ; Declare functions defined elsewhere and referenced in this module
128 ----------------------------------------------------------------------------*/
129
130 /*----------------------------------------------------------------------------
131 ; EXTERNAL GLOBAL STORE/BUFFER/POINTER REFERENCES
132 ; Declare variables used in this module but defined elsewhere
133 ----------------------------------------------------------------------------*/
134
135
136 /*----------------------------------------------------------------------------
137 ; FUNCTION CODE
138 ----------------------------------------------------------------------------*/
139
140 void D_plsf_3(
141     D_plsfState *st,   /* i/o: State struct                               */
142     enum Mode mode,    /* i  : coder mode                                 */
143     Word16 bfi,        /* i  : bad frame indicator (set to 1 if a         */
144     /*      bad frame is received)                     */
145     Word16 * indice,   /* i  : quantization indices of 3 submatrices, Q0  */
146     CommonAmrTbls* common_amr_tbls, /* i : structure containing ptrs to read-only tables */
147     Word16 * lsp1_q,   /* o  : quantized 1st LSP vector,              Q15 */
148     Flag  *pOverflow   /* o : Flag set when overflow occurs               */
149 )
150 {
151     Word16 i;
152     Word16 temp;
153     Word16 index;
154
155     Word16 lsf1_r[M];
156     Word16 lsf1_q[M];
157
158     const Word16* mean_lsf_3_ptr = common_amr_tbls->mean_lsf_3_ptr;
159     const Word16* pred_fac_3_ptr = common_amr_tbls->pred_fac_3_ptr;
160     const Word16* dico1_lsf_3_ptr = common_amr_tbls->dico1_lsf_3_ptr;
161     const Word16* dico2_lsf_3_ptr = common_amr_tbls->dico2_lsf_3_ptr;
162     const Word16* dico3_lsf_3_ptr = common_amr_tbls->dico3_lsf_3_ptr;
163     const Word16* mr515_3_lsf_ptr = common_amr_tbls->mr515_3_lsf_ptr;
164     const Word16* mr795_1_lsf_ptr = common_amr_tbls->mr795_1_lsf_ptr;
165
166     if (bfi != 0)   /* if bad frame */
167     {
168         /* use the past LSFs slightly shifted towards their mean */
169
170         for (i = 0; i < M; i++)
171         {
172             /* lsfi_q[i] = ALPHA*past_lsf_q[i] + ONE_ALPHA*mean_lsf[i]; */
173             temp =
174                 mult(
175                     st->past_lsf_q[i],
176                     ALPHA,
177                     pOverflow);
178
179             index =
180                 mult(
181                     mean_lsf_3_ptr[i],
182                     ONE_ALPHA,
183                     pOverflow);
184
185             lsf1_q[i] =
186                 add_16(
187                     index,
188                     temp,
189                     pOverflow);
190         }
191
192         /* estimate past quantized residual to be used in next frame */
193         if (mode != MRDTX)
194         {
195             for (i = 0; i < M; i++)
196             {
197                 /* temp  = mean_lsf[i] +  past_r2_q[i] * PRED_FAC; */
198
199                 temp =
200                     mult(
201                         st->past_r_q[i],
202                         pred_fac_3_ptr[i],
203                         pOverflow);
204
205                 temp =
206                     add_16(
207                         mean_lsf_3_ptr[i],
208                         temp,
209                         pOverflow);
210
211                 st->past_r_q[i] =
212                     sub(
213                         lsf1_q[i],
214                         temp,
215                         pOverflow);
216             }
217
218         } /* if (mode == MRDTX) */
219         else
220         {
221             for (i = 0; i < M; i++)
222             {
223                 /* temp  = mean_lsf[i] +  past_r2_q[i]; */
224
225                 temp =
226                     add_16(
227                         mean_lsf_3_ptr[i],
228                         st->past_r_q[i],
229                         pOverflow);
230
231                 st->past_r_q[i] =
232                     sub(
233                         lsf1_q[i],
234                         temp,
235                         pOverflow);
236             }
237         }
238
239     } /* if (bfi != 0) */
240
241     else  /* if good LSFs received */
242     {
243
244         Word16 index_limit_1 = 0;
245         Word16 index_limit_2 = (DICO2_SIZE - 1) * 3;
246         Word16 index_limit_3 = 0;
247
248         const Word16 *p_cb1;
249         const Word16 *p_cb2;
250         const Word16 *p_cb3;
251         const Word16 *p_dico;
252
253
254         p_cb2 = dico2_lsf_3_ptr;    /* size DICO2_SIZE*3 */
255
256         if ((mode == MR475) || (mode == MR515))
257         {   /* MR475, MR515 */
258             p_cb1 = dico1_lsf_3_ptr;    /* size DICO1_SIZE*3 */
259             p_cb3 = mr515_3_lsf_ptr;    /* size MR515_3_SIZE*4 */
260
261             index_limit_1 = (DICO1_SIZE - 1) * 3;
262             index_limit_3 = (MR515_3_SIZE - 1) * 4;
263
264         }
265         else if (mode == MR795)
266         {   /* MR795 */
267             p_cb1 = mr795_1_lsf_ptr;    /* size MR795_1_SIZE*3 */
268             p_cb3 = dico3_lsf_3_ptr;    /* size DICO3_SIZE*4 */
269
270             index_limit_1 = (MR795_1_SIZE - 1) * 3;
271             index_limit_3 = (DICO3_SIZE - 1) * 4;
272
273         }
274         else
275         {   /* MR59, MR67, MR74, MR102, MRDTX */
276             p_cb1 = dico1_lsf_3_ptr;    /* size DICO1_SIZE*3 */
277             p_cb3 = dico3_lsf_3_ptr;    /* size DICO3_SIZE*4 */
278
279             index_limit_1 = (DICO1_SIZE - 1) * 3;
280             index_limit_3 = (DICO3_SIZE - 1) * 4;
281
282         }
283
284         /* decode prediction residuals from 3 received indices */
285
286         index = *indice++;
287
288         /* temp = 3*index; */
289         temp = index + (index << 1);
290
291         if (temp > index_limit_1)
292         {
293             temp = index_limit_1;  /* avoid buffer overrun */
294         }
295
296         p_dico = &p_cb1[temp];
297
298         lsf1_r[0] = *p_dico++;
299         lsf1_r[1] = *p_dico++;
300         lsf1_r[2] = *p_dico++;
301
302         index = *indice++;
303
304         if (mode == MR475 || mode == MR515)
305         {   /* MR475, MR515 only using every second entry */
306             index <<= 1;
307         }
308
309         /* temp = 3*index */
310         temp = index + (index << 1);
311
312         if (temp > index_limit_2)
313         {
314             temp = index_limit_2;  /* avoid buffer overrun */
315         }
316
317         p_dico = &p_cb2[temp];
318
319         lsf1_r[3] = *p_dico++;
320         lsf1_r[4] = *p_dico++;
321         lsf1_r[5] = *p_dico++;
322
323         index = *indice++;
324
325         temp = index << 2;
326
327         if (temp > index_limit_3)
328         {
329             temp = index_limit_3;  /* avoid buffer overrun */
330         }
331
332
333         p_dico = &p_cb3[temp];
334
335         lsf1_r[6] = *p_dico++;
336         lsf1_r[7] = *p_dico++;
337         lsf1_r[8] = *p_dico++;
338         lsf1_r[9] = *p_dico++;
339
340         /* Compute quantized LSFs and update the past quantized residual */
341
342         if (mode != MRDTX)
343         {
344             for (i = 0; i < M; i++)
345             {
346                 temp =
347                     mult(
348                         st->past_r_q[i],
349                         pred_fac_3_ptr[i],
350                         pOverflow);
351
352                 temp =
353                     add_16(
354                         mean_lsf_3_ptr[i],
355                         temp,
356                         pOverflow);
357
358                 lsf1_q[i] =
359                     add_16(
360                         lsf1_r[i],
361                         temp,
362                         pOverflow);
363
364                 st->past_r_q[i] = lsf1_r[i];
365             }
366         }
367         else
368         {
369             for (i = 0; i < M; i++)
370             {
371                 temp =
372                     add_16(
373                         mean_lsf_3_ptr[i],
374                         st->past_r_q[i],
375                         pOverflow);
376
377                 lsf1_q[i] =
378                     add_16(
379                         lsf1_r[i],
380                         temp,
381                         pOverflow);
382
383                 st->past_r_q[i] = lsf1_r[i];
384             }
385         }
386
387     }
388
389     /* verification that LSFs has minimum distance of LSF_GAP Hz */
390
391     Reorder_lsf(
392         lsf1_q,
393         LSF_GAP,
394         M,
395         pOverflow);
396
397     oscl_memmove(
398         (void *)st->past_lsf_q,
399         lsf1_q,
400         M*sizeof(*lsf1_q));
401
402     /*  convert LSFs to the cosine domain */
403
404     Lsf_lsp(
405         lsf1_q,
406         lsp1_q,
407         M,
408         pOverflow);
409
410     return;
411 }
412
413 /*
414 ------------------------------------------------------------------------------
415  FUNCTION NAME: Init_D_plsf_3
416 ------------------------------------------------------------------------------
417  INPUT AND OUTPUT DEFINITIONS
418
419  Inputs:
420     st = pointer to a structure of type D_plsfState
421     index = Word16, past_rq_init[] index [0, 7]
422
423  Outputs:
424     st = pointer to a structure of type D_plsfState
425
426  Returns:
427     None
428
429  Global Variables Used:
430     None.
431
432  Local Variables Needed:
433     None.
434
435 ------------------------------------------------------------------------------
436  FUNCTION DESCRIPTION
437
438  This function initializes the D_plsfState structure.
439
440 ------------------------------------------------------------------------------
441  REQUIREMENTS
442
443  None.
444
445 ------------------------------------------------------------------------------
446  REFERENCES
447
448  d_plsf_3.c, UMTS GSM AMR speech codec, R99 - Version 3.2.0, March 2, 2001
449
450 ------------------------------------------------------------------------------
451  PSEUDO-CODE
452
453 ------------------------------------------------------------------------------
454  CAUTION [optional]
455  [State any special notes, constraints or cautions for users of this function]
456
457 ------------------------------------------------------------------------------
458 */
459 void Init_D_plsf_3(
460     D_plsfState *st,      /* i/o: State struct                */
461     Word16       index,   /* i  : past_rq_init[] index [0, 7] */
462     const Word16* past_rq_init_ptr /* ptr to read-only table */)
463 {
464     oscl_memmove(
465         (void *)st->past_r_q,
466         &past_rq_init_ptr[index * M],
467         M*sizeof(*past_rq_init_ptr));
468 }