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  Pathname: ./audio/gsm-amr/c/src/d_plsf_3.c
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  RESOURCES USED
85    When the code is written for a specific target processor the
86      the resources used should be documented below.
87 
88  STACK USAGE: [stack count for this module] + [variable to represent
89           stack usage for each subroutine called]
90 
91      where: [stack usage variable] = stack usage for [subroutine
92          name] (see [filename].ext)
93 
94  DATA MEMORY USED: x words
95 
96  PROGRAM MEMORY USED: x words
97 
98  CLOCK CYCLES: [cycle count equation for this module] + [variable
99            used to represent cycle count for each subroutine
100            called]
101 
102      where: [cycle count variable] = cycle count for [subroutine
103         name] (see [filename].ext)
104 
105 ------------------------------------------------------------------------------
106 */
107 
108 
109 /*----------------------------------------------------------------------------
110 ; INCLUDES
111 ----------------------------------------------------------------------------*/
112 #include "d_plsf.h"
113 #include "typedef.h"
114 #include "basic_op.h"
115 #include "lsp_lsf.h"
116 #include "reorder.h"
117 #include "copy.h"
118 #include "q_plsf_3_tbl.h"
119 
120 
121 /*----------------------------------------------------------------------------
122 ; MACROS
123 ; Define module specific macros here
124 ----------------------------------------------------------------------------*/
125 
126 
127 /*----------------------------------------------------------------------------
128 ; DEFINES
129 ; Include all pre-processor statements here. Include conditional
130 ; compile variables also.
131 ----------------------------------------------------------------------------*/
132 #define ALPHA     29491     /* ALPHA    ->  0.9                            */
133 #define ONE_ALPHA 3277      /* ONE_ALPHA-> (1.0-ALPHA)                     */
134 
135 
136 /*----------------------------------------------------------------------------
137 ; LOCAL FUNCTION DEFINITIONS
138 ; Function Prototype declaration
139 ----------------------------------------------------------------------------*/
140 
141 
142 /*----------------------------------------------------------------------------
143 ; LOCAL STORE/BUFFER/POINTER DEFINITIONS
144 ; Variable declaration - defined here and used outside this module
145 ----------------------------------------------------------------------------*/
146 
147 /*----------------------------------------------------------------------------
148 ; EXTERNAL FUNCTION REFERENCES
149 ; Declare functions defined elsewhere and referenced in this module
150 ----------------------------------------------------------------------------*/
151 
152 /*----------------------------------------------------------------------------
153 ; EXTERNAL GLOBAL STORE/BUFFER/POINTER REFERENCES
154 ; Declare variables used in this module but defined elsewhere
155 ----------------------------------------------------------------------------*/
156 
157 
158 /*----------------------------------------------------------------------------
159 ; FUNCTION CODE
160 ----------------------------------------------------------------------------*/
161 
D_plsf_3(D_plsfState * st,enum Mode mode,Word16 bfi,Word16 * indice,Word16 * lsp1_q,Flag * pOverflow)162 void D_plsf_3(
163     D_plsfState *st,   /* i/o: State struct                               */
164     enum Mode mode,    /* i  : coder mode                                 */
165     Word16 bfi,        /* i  : bad frame indicator (set to 1 if a         */
166     /*      bad frame is received)                     */
167     Word16 * indice,   /* i  : quantization indices of 3 submatrices, Q0  */
168     Word16 * lsp1_q,   /* o  : quantized 1st LSP vector,              Q15 */
169     Flag  *pOverflow   /* o : Flag set when overflow occurs               */
170 )
171 {
172     Word16 i;
173     Word16 temp;
174     Word16 index;
175 
176     Word16 lsf1_r[M];
177     Word16 lsf1_q[M];
178 
179     if (bfi != 0)   /* if bad frame */
180     {
181         /* use the past LSFs slightly shifted towards their mean */
182 
183         for (i = 0; i < M; i++)
184         {
185             /* lsfi_q[i] = ALPHA*past_lsf_q[i] + ONE_ALPHA*mean_lsf[i]; */
186             temp =
187                 mult(
188                     st->past_lsf_q[i],
189                     ALPHA,
190                     pOverflow);
191 
192             index =
193                 mult(
194                     mean_lsf_3[i],
195                     ONE_ALPHA,
196                     pOverflow);
197 
198             lsf1_q[i] =
199                 add(
200                     index,
201                     temp,
202                     pOverflow);
203         }
204 
205         /* estimate past quantized residual to be used in next frame */
206         if (mode != MRDTX)
207         {
208             for (i = 0; i < M; i++)
209             {
210                 /* temp  = mean_lsf[i] +  past_r2_q[i] * PRED_FAC; */
211 
212                 temp =
213                     mult(
214                         st->past_r_q[i],
215                         pred_fac_3[i],
216                         pOverflow);
217 
218                 temp =
219                     add(
220                         mean_lsf_3[i],
221                         temp,
222                         pOverflow);
223 
224                 st->past_r_q[i] =
225                     sub(
226                         lsf1_q[i],
227                         temp,
228                         pOverflow);
229             }
230 
231         } /* if (mode == MRDTX) */
232         else
233         {
234             for (i = 0; i < M; i++)
235             {
236                 /* temp  = mean_lsf[i] +  past_r2_q[i]; */
237 
238                 temp =
239                     add(
240                         mean_lsf_3[i],
241                         st->past_r_q[i],
242                         pOverflow);
243 
244                 st->past_r_q[i] =
245                     sub(
246                         lsf1_q[i],
247                         temp,
248                         pOverflow);
249             }
250         }
251 
252     } /* if (bfi != 0) */
253 
254     else  /* if good LSFs received */
255     {
256 
257         Word16 index_limit_1 = 0;
258         Word16 index_limit_2 = (DICO2_SIZE - 1) * 3;
259         Word16 index_limit_3 = 0;
260 
261         const Word16 *p_cb1;
262         const Word16 *p_cb2;
263         const Word16 *p_cb3;
264         const Word16 *p_dico;
265 
266 
267         p_cb2 = dico2_lsf_3;    /* size DICO2_SIZE*3 */
268 
269         if ((mode == MR475) || (mode == MR515))
270         {   /* MR475, MR515 */
271             p_cb1 = dico1_lsf_3;    /* size DICO1_SIZE*3 */
272             p_cb3 = mr515_3_lsf;    /* size MR515_3_SIZE*4 */
273 
274             index_limit_1 = (DICO1_SIZE - 1) * 3;
275             index_limit_3 = (MR515_3_SIZE - 1) * 4;
276 
277         }
278         else if (mode == MR795)
279         {   /* MR795 */
280             p_cb1 = mr795_1_lsf;    /* size MR795_1_SIZE*3 */
281             p_cb3 = dico3_lsf_3;    /* size DICO3_SIZE*4 */
282 
283             index_limit_1 = (MR795_1_SIZE - 1) * 3;
284             index_limit_3 = (DICO3_SIZE - 1) * 4;
285 
286         }
287         else
288         {   /* MR59, MR67, MR74, MR102, MRDTX */
289             p_cb1 = dico1_lsf_3;    /* size DICO1_SIZE*3 */
290             p_cb3 = dico3_lsf_3;    /* size DICO3_SIZE*4 */
291 
292             index_limit_1 = (DICO1_SIZE - 1) * 3;
293             index_limit_3 = (DICO3_SIZE - 1) * 4;
294 
295         }
296 
297         /* decode prediction residuals from 3 received indices */
298 
299         index = *indice++;
300 
301         /* temp = 3*index; */
302         temp = index + (index << 1);
303 
304         if (temp > index_limit_1)
305         {
306             temp = index_limit_1;  /* avoid buffer overrun */
307         }
308 
309         p_dico = &p_cb1[temp];
310 
311         lsf1_r[0] = *p_dico++;
312         lsf1_r[1] = *p_dico++;
313         lsf1_r[2] = *p_dico++;
314 
315         index = *indice++;
316 
317         if (mode == MR475 || mode == MR515)
318         {   /* MR475, MR515 only using every second entry */
319             index <<= 1;
320         }
321 
322         /* temp = 3*index */
323         temp = index + (index << 1);
324 
325         if (temp > index_limit_2)
326         {
327             temp = index_limit_2;  /* avoid buffer overrun */
328         }
329 
330         p_dico = &p_cb2[temp];
331 
332         lsf1_r[3] = *p_dico++;
333         lsf1_r[4] = *p_dico++;
334         lsf1_r[5] = *p_dico++;
335 
336         index = *indice++;
337 
338         temp = index << 2;
339 
340         if (temp > index_limit_3)
341         {
342             temp = index_limit_3;  /* avoid buffer overrun */
343         }
344 
345 
346         p_dico = &p_cb3[temp];
347 
348         lsf1_r[6] = *p_dico++;
349         lsf1_r[7] = *p_dico++;
350         lsf1_r[8] = *p_dico++;
351         lsf1_r[9] = *p_dico++;
352 
353         /* Compute quantized LSFs and update the past quantized residual */
354 
355         if (mode != MRDTX)
356         {
357             for (i = 0; i < M; i++)
358             {
359                 temp =
360                     mult(
361                         st->past_r_q[i],
362                         pred_fac_3[i],
363                         pOverflow);
364 
365                 temp =
366                     add(
367                         mean_lsf_3[i],
368                         temp,
369                         pOverflow);
370 
371                 lsf1_q[i] =
372                     add(
373                         lsf1_r[i],
374                         temp,
375                         pOverflow);
376 
377                 st->past_r_q[i] = lsf1_r[i];
378             }
379         }
380         else
381         {
382             for (i = 0; i < M; i++)
383             {
384                 temp =
385                     add(
386                         mean_lsf_3[i],
387                         st->past_r_q[i],
388                         pOverflow);
389 
390                 lsf1_q[i] =
391                     add(
392                         lsf1_r[i],
393                         temp,
394                         pOverflow);
395 
396                 st->past_r_q[i] = lsf1_r[i];
397             }
398         }
399 
400     }
401 
402     /* verification that LSFs has minimum distance of LSF_GAP Hz */
403 
404     Reorder_lsf(
405         lsf1_q,
406         LSF_GAP,
407         M,
408         pOverflow);
409 
410     Copy(
411         lsf1_q,
412         st->past_lsf_q,
413         M);
414 
415     /*  convert LSFs to the cosine domain */
416 
417     Lsf_lsp(
418         lsf1_q,
419         lsp1_q,
420         M,
421         pOverflow);
422 
423     return;
424 }
425 
426 /*
427 ------------------------------------------------------------------------------
428  FUNCTION NAME: Init_D_plsf_3
429 ------------------------------------------------------------------------------
430  INPUT AND OUTPUT DEFINITIONS
431 
432  Inputs:
433     st = pointer to a structure of type D_plsfState
434     index = Word16, past_rq_init[] index [0, 7]
435 
436  Outputs:
437     st = pointer to a structure of type D_plsfState
438 
439  Returns:
440     None
441 
442  Global Variables Used:
443     None.
444 
445  Local Variables Needed:
446     None.
447 
448 ------------------------------------------------------------------------------
449  FUNCTION DESCRIPTION
450 
451  This function initializes the D_plsfState structure.
452 
453 ------------------------------------------------------------------------------
454  REQUIREMENTS
455 
456  None.
457 
458 ------------------------------------------------------------------------------
459  REFERENCES
460 
461  d_plsf_3.c, UMTS GSM AMR speech codec, R99 - Version 3.2.0, March 2, 2001
462 
463 ------------------------------------------------------------------------------
464  PSEUDO-CODE
465 
466 ------------------------------------------------------------------------------
467  RESOURCES USED [optional]
468 
469  When the code is written for a specific target processor the
470  the resources used should be documented below.
471 
472  HEAP MEMORY USED: x bytes
473 
474  STACK MEMORY USED: x bytes
475 
476  CLOCK CYCLES: (cycle count equation for this function) + (variable
477                 used to represent cycle count for each subroutine
478                 called)
479      where: (cycle count variable) = cycle count for [subroutine
480                                      name]
481 
482 ------------------------------------------------------------------------------
483  CAUTION [optional]
484  [State any special notes, constraints or cautions for users of this function]
485 
486 ------------------------------------------------------------------------------
487 */
Init_D_plsf_3(D_plsfState * st,Word16 index)488 void Init_D_plsf_3(
489     D_plsfState *st,      /* i/o: State struct                */
490     Word16       index    /* i  : past_rq_init[] index [0, 7] */)
491 {
492     Copy(
493         &past_rq_init[index * M],
494         st->past_r_q,
495         M);
496 }
497