Initialize Tizen 2.3
[external/opencore-amr.git] / opencore / codecs_v2 / audio / gsm_amr / amr_nb / common / src / int_lpc.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  Filename: int_lpc.cpp
31  Functions:
32
33 ------------------------------------------------------------------------------
34  MODULE DESCRIPTION
35
36
37 ------------------------------------------------------------------------------
38 */
39
40 /*----------------------------------------------------------------------------
41 ; INCLUDES
42 ----------------------------------------------------------------------------*/
43 #include "int_lpc.h"
44 #include "typedef.h"
45 #include "cnst.h"
46 #include "lsp_az.h"
47 #include "basic_op.h"
48
49 /*----------------------------------------------------------------------------
50 ; MACROS
51 ; Define module specific macros here
52 ----------------------------------------------------------------------------*/
53
54 /*----------------------------------------------------------------------------
55 ; DEFINES
56 ; Include all pre-processor statements here. Include conditional
57 ; compile variables also.
58 ----------------------------------------------------------------------------*/
59
60 /*----------------------------------------------------------------------------
61 ; LOCAL FUNCTION DEFINITIONS
62 ; Function Prototype declaration
63 ----------------------------------------------------------------------------*/
64
65 /*----------------------------------------------------------------------------
66 ; LOCAL VARIABLE DEFINITIONS
67 ; Variable declaration - defined here and used outside this module
68 ----------------------------------------------------------------------------*/
69
70
71 /*
72 ------------------------------------------------------------------------------
73  FUNCTION NAME: Int_lpc_1and3
74 ------------------------------------------------------------------------------
75  INPUT AND OUTPUT DEFINITIONS
76
77  Inputs:
78     lsp_old -- array of type Word16 -- LSP vector at the
79                                        4th subfr. of past frame (M)
80     lsp_mid -- array of type Word16 -- LSP vector at the 2nd subfr. of
81                                        present frame (M)
82     lsp_new -- array of type Word16 -- LSP vector at the 4th subfr. of
83                                        present frame (M)
84
85  Outputs:
86     Az -- array of type Word16 -- interpolated LP parameters in all subfr.
87                                   (AZ_SIZE)
88     pOverflow -- pointer to type Flag -- Overflow indicator
89
90  Returns:
91     None
92
93  Global Variables Used:
94     None
95
96  Local Variables Needed:
97     None
98
99 ------------------------------------------------------------------------------
100  FUNCTION DESCRIPTION
101
102  Purpose     : Interpolates the LSPs and converts to LPC parameters
103                to get a different LP filter in each subframe.
104  Description : The 20 ms speech frame is divided into 4 subframes.
105                The LSPs are quantized and transmitted at the 2nd and
106                4th subframes (twice per frame) and interpolated at the
107                1st and 3rd subframe.
108
109                      |------|------|------|------|
110                         sf1    sf2    sf3    sf4
111                   F0            Fm            F1
112
113                 sf1:   1/2 Fm + 1/2 F0         sf3:   1/2 F1 + 1/2 Fm
114                 sf2:       Fm                  sf4:       F1
115
116 ------------------------------------------------------------------------------
117  REQUIREMENTS
118
119  None
120
121 ------------------------------------------------------------------------------
122  REFERENCES
123
124  int_lpc.c, UMTS GSM AMR speech codec, R99 - Version 3.2.0, March 2, 2001
125
126 ------------------------------------------------------------------------------
127  PSEUDO-CODE
128
129
130 ------------------------------------------------------------------------------
131  CAUTION [optional]
132  [State any special notes, constraints or cautions for users of this function]
133
134 ------------------------------------------------------------------------------
135 */
136
137 OSCL_EXPORT_REF void Int_lpc_1and3(
138     Word16 lsp_old[],  /* i : LSP vector at the 4th subfr. of past frame (M) */
139     Word16 lsp_mid[],  /* i : LSP vector at the 2nd subfr. of
140                               present frame (M)                              */
141     Word16 lsp_new[],  /* i : LSP vector at the 4th subfr. of
142                               present frame (M)                              */
143     Word16 Az[],       /* o : interpolated LP parameters in all subfr.
144                               (AZ_SIZE)                                      */
145     Flag  *pOverflow
146 )
147 {
148     Word16 i;
149     Word16 lsp[M];
150     Word16 *p_lsp_old = &lsp_old[0];
151     Word16 *p_lsp_mid = &lsp_mid[0];
152     Word16 *p_lsp_new = &lsp_new[0];
153     Word16 *p_lsp     = &lsp[0];
154
155     /*  lsp[i] = lsp_mid[i] * 0.5 + lsp_old[i] * 0.5 */
156
157     for (i = M >> 1; i != 0; i--)
158     {
159         *(p_lsp++) = (*(p_lsp_old++) >> 1) + (*(p_lsp_mid++) >> 1);
160         *(p_lsp++) = (*(p_lsp_old++) >> 1) + (*(p_lsp_mid++) >> 1);
161     }
162
163     Lsp_Az(
164         lsp,
165         Az,
166         pOverflow);       /* Subframe 1 */
167
168     Az += MP1;
169
170     Lsp_Az(
171         lsp_mid,
172         Az,
173         pOverflow);       /* Subframe 2 */
174
175     Az += MP1;
176
177     p_lsp_mid = &lsp_mid[0];
178     p_lsp     = &lsp[0];
179
180     for (i = M >> 1; i != 0; i--)
181     {
182         *(p_lsp++) = (*(p_lsp_mid++) >> 1) + (*(p_lsp_new++) >> 1);
183         *(p_lsp++) = (*(p_lsp_mid++) >> 1) + (*(p_lsp_new++) >> 1);
184     }
185
186     Lsp_Az(
187         lsp,
188         Az,
189         pOverflow);           /* Subframe 3 */
190
191     Az += MP1;
192
193     Lsp_Az(
194         lsp_new,
195         Az,
196         pOverflow);       /* Subframe 4 */
197
198     return;
199 }
200
201
202 /*
203 ------------------------------------------------------------------------------
204  FUNCTION NAME: Int_lpc_1and3_2
205 ------------------------------------------------------------------------------
206  INPUT AND OUTPUT DEFINITIONS
207
208  Inputs:
209     lsp_old -- array of type Word16 -- LSP vector at the
210                                        4th subfr. of past frame (M)
211     lsp_mid -- array of type Word16 -- LSP vector at the 2nd subfr. of
212                                        present frame (M)
213     lsp_new -- array of type Word16 -- LSP vector at the 4th subfr. of
214                                        present frame (M)
215
216  Outputs:
217     Az -- array of type Word16 -- interpolated LP parameters in.
218                                   subfr 1 and 2.
219     pOverflow -- pointer to type Flag -- Overflow indicator
220
221  Returns:
222     None
223
224  Global Variables Used:
225
226
227  Local Variables Needed:
228     None
229
230 ------------------------------------------------------------------------------
231  FUNCTION DESCRIPTION
232
233  Purpose : Interpolation of the LPC parameters. Same as the Int_lpc
234            function but we do not recompute Az() for subframe 2 and
235            4 because it is already available.
236
237 ------------------------------------------------------------------------------
238  REQUIREMENTS
239
240  None
241
242 ------------------------------------------------------------------------------
243  REFERENCES
244
245  int_lpc.c, UMTS GSM AMR speech codec, R99 - Version 3.2.0, March 2, 2001
246
247 ------------------------------------------------------------------------------
248  PSEUDO-CODE
249
250
251 ------------------------------------------------------------------------------
252  CAUTION [optional]
253  [State any special notes, constraints or cautions for users of this function]
254
255 ------------------------------------------------------------------------------
256 */
257
258 void Int_lpc_1and3_2(
259     Word16 lsp_old[],  /* i : LSP vector at the 4th subfr. of past frame (M) */
260     Word16 lsp_mid[],  /* i : LSP vector at the 2nd subframe of
261                              present frame (M)                               */
262     Word16 lsp_new[],  /* i : LSP vector at the 4th subframe of
263                              present frame (M)                               */
264     Word16 Az[],       /* o :interpolated LP parameters
265                              in subframes 1 and 3 (AZ_SIZE)                  */
266     Flag  *pOverflow
267 )
268 {
269     Word16 i;
270     Word16 lsp[M];
271     Word16 *p_lsp_old = &lsp_old[0];
272     Word16 *p_lsp_mid = &lsp_mid[0];
273     Word16 *p_lsp_new = &lsp_new[0];
274     Word16 *p_lsp     = &lsp[0];
275
276     /*  lsp[i] = lsp_mid[i] * 0.5 + lsp_old[i] * 0.5 */
277
278     for (i = M >> 1; i != 0; i--)
279     {
280         *(p_lsp++) = (*(p_lsp_old++) >> 1) + (*(p_lsp_mid++) >> 1);
281         *(p_lsp++) = (*(p_lsp_old++) >> 1) + (*(p_lsp_mid++) >> 1);
282     }
283     Lsp_Az(lsp, Az, pOverflow);            /* Subframe 1 */
284     Az += MP1 * 2;
285
286     p_lsp_mid = &lsp_mid[0];
287     p_lsp     = &lsp[0];
288
289     for (i = M >> 1; i != 0; i--)
290     {
291         *(p_lsp++) = (*(p_lsp_mid++) >> 1) + (*(p_lsp_new++) >> 1);
292         *(p_lsp++) = (*(p_lsp_mid++) >> 1) + (*(p_lsp_new++) >> 1);
293     }
294
295     Lsp_Az(lsp, Az, pOverflow);            /* Subframe 3 */
296
297     return;
298 }
299
300
301 /*
302 ------------------------------------------------------------------------------
303  FUNCTION NAME: lsp
304 ------------------------------------------------------------------------------
305  INPUT AND OUTPUT DEFINITIONS
306
307  Inputs:
308     lsp_old -- array of type Word16 -- LSP vector at the
309                                        4th subfr. of past frame (M)
310     lsp_new -- array of type Word16 -- LSP vector at the 4th subfr. of
311                                        present frame (M)
312
313  Outputs:
314     Az -- array of type Word16 -- interpolated LP parameters in.
315                                   all subframes.
316     pOverflow -- pointer to type Flag -- Overflow indicator
317
318  Returns:
319     None
320
321  Global Variables Used:
322
323
324  Local Variables Needed:
325     None
326
327 ------------------------------------------------------------------------------
328  FUNCTION DESCRIPTION
329
330  PURPOSE:  Interpolates the LSPs and convert to LP parameters to get
331            a different LP filter in each subframe.
332
333  DESCRIPTION:
334     The 20 ms speech frame is divided into 4 subframes.
335     The LSPs are quantized and transmitted at the 4th subframe
336     (once per frame) and interpolated at the 1st, 2nd and 3rd subframe.
337
338          |------|------|------|------|
339             sf1    sf2    sf3    sf4
340       F0                          F1
341
342     sf1:   3/4 F0 + 1/4 F1         sf3:   1/4 F0 + 3/4 F1
343     sf2:   1/2 F0 + 1/2 F1         sf4:       F1
344
345 ------------------------------------------------------------------------------
346  REQUIREMENTS
347
348  None
349
350 ------------------------------------------------------------------------------
351  REFERENCES
352
353  int_lpc.c, UMTS GSM AMR speech codec, R99 - Version 3.2.0, March 2, 2001
354
355 ------------------------------------------------------------------------------
356  PSEUDO-CODE
357
358
359 ------------------------------------------------------------------------------
360  CAUTION [optional]
361  [State any special notes, constraints or cautions for users of this function]
362
363 ------------------------------------------------------------------------------
364 */
365
366 OSCL_EXPORT_REF void Int_lpc_1to3(
367     Word16 lsp_old[], /* input : LSP vector at the 4th SF of past frame    */
368     Word16 lsp_new[], /* input : LSP vector at the 4th SF of present frame */
369     Word16 Az[],      /* output: interpolated LP parameters in all SFs     */
370     Flag   *pOverflow
371 )
372 {
373     Word16 i;
374     Word16 temp;
375
376     Word16 lsp[M];
377
378     for (i = 0; i < M; i++)
379     {
380         temp = lsp_old[i] - (lsp_old[i] >> 2);
381         lsp[i] = temp + (lsp_new[i] >> 2);
382     }
383
384     Lsp_Az(
385         lsp,
386         Az,
387         pOverflow);        /* Subframe 1 */
388
389     Az += MP1;
390
391
392     for (i = 0; i < M; i++)
393     {
394         lsp[i] = (lsp_new[i] >> 1) + (lsp_old[i] >> 1);
395
396     }
397
398     Lsp_Az(lsp, Az, pOverflow);        /* Subframe 2 */
399
400     Az += MP1;
401
402     for (i = 0; i < M; i++)
403     {
404
405         temp = lsp_new[i] - (lsp_new[i] >> 2);
406         lsp[i] = temp + (lsp_old[i] >> 2);
407
408     }
409
410     Lsp_Az(lsp, Az, pOverflow);        /* Subframe 3 */
411
412     Az += MP1;
413
414     Lsp_Az(lsp_new, Az, pOverflow);    /* Subframe 4 */
415
416     return;
417 }
418 /*
419 ------------------------------------------------------------------------------
420  FUNCTION NAME: Int_lpc_1to3_2
421 ------------------------------------------------------------------------------
422  INPUT AND OUTPUT DEFINITIONS
423
424  Inputs:
425     lsp_old -- array of type Word16 -- LSP vector at the
426                                        4th subfr. of past frame (M)
427     lsp_new -- array of type Word16 -- LSP vector at the 4th subfr. of
428                                        present frame (M)
429
430  Outputs:
431     Az -- array of type Word16 -- interpolated LP parameters in.
432                                   subfr 1, 2, and 3.
433     pOverflow -- pointer to type Flag -- Overflow indicator
434
435  Returns:
436     None
437
438  Global Variables Used:
439     None
440
441  Local Variables Needed:
442     None
443
444 ------------------------------------------------------------------------------
445  FUNCTION DESCRIPTION
446
447  Interpolation of the LPC parameters.
448  Same as the previous function but we do not recompute Az() for
449  subframe 4 because it is already available.
450
451 ------------------------------------------------------------------------------
452  REQUIREMENTS
453
454  None
455
456 ------------------------------------------------------------------------------
457  REFERENCES
458
459  int_lpc.c, UMTS GSM AMR speech codec, R99 - Version 3.2.0, March 2, 2001
460
461 ------------------------------------------------------------------------------
462  PSEUDO-CODE
463
464
465 ------------------------------------------------------------------------------
466  CAUTION [optional]
467  [State any special notes, constraints or cautions for users of this function]
468
469 ------------------------------------------------------------------------------
470 */
471
472 void Int_lpc_1to3_2(
473     Word16 lsp_old[],  /* input : LSP vector at the 4th SF of past frame    */
474     Word16 lsp_new[],  /* input : LSP vector at the 4th SF of present frame */
475     Word16 Az[],       /* output: interpolated LP parameters in SFs 1,2,3   */
476     Flag   *pOverflow
477 )
478 {
479     Word16 i;
480     Word16 temp;
481     Word16 lsp[M];
482
483     for (i = 0; i < M; i++)
484     {
485         temp = lsp_old[i] - (lsp_old[i] >> 2);
486         lsp[i] = temp + (lsp_new[i] >> 2);
487
488     }
489
490     Lsp_Az(lsp, Az, pOverflow);         /* Subframe 1 */
491
492     Az += MP1;
493
494     for (i = 0; i < M; i++)
495     {
496         lsp[i] = (lsp_new[i] >> 1) + (lsp_old[i] >> 1);
497
498     }
499
500     Lsp_Az(lsp, Az, pOverflow);         /* Subframe 2 */
501
502     Az += MP1;
503
504     for (i = 0; i < M; i++)
505     {
506         temp = lsp_new[i] - (lsp_new[i] >> 2);
507         lsp[i] = temp + (lsp_old[i] >> 2);
508
509     }
510
511     Lsp_Az(lsp, Az, pOverflow);         /* Subframe 3 */
512
513     return;
514 }
515