Initialize Tizen 2.3
[external/opencore-amr.git] / opencore / codecs_v2 / audio / gsm_amr / amr_nb / enc / src / ton_stab.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: ton_stab.cpp
35
36 ------------------------------------------------------------------------------
37 */
38
39
40 /*----------------------------------------------------------------------------
41 ; INCLUDES
42 ----------------------------------------------------------------------------*/
43 #include "ton_stab.h"
44 #include "oper_32b.h"
45 #include "cnst.h"
46 #include "set_zero.h"
47 #include "basic_op.h"
48 #include "oscl_mem.h"
49
50 /*----------------------------------------------------------------------------
51 ; MACROS
52 ; Define module specific macros here
53 ----------------------------------------------------------------------------*/
54
55
56 /*----------------------------------------------------------------------------
57 ; DEFINES
58 ; Include all pre-processor statements here. Include conditional
59 ; compile variables also.
60 ----------------------------------------------------------------------------*/
61
62
63 /*----------------------------------------------------------------------------
64 ; LOCAL FUNCTION DEFINITIONS
65 ; Function Prototype declaration
66 ----------------------------------------------------------------------------*/
67
68 /*----------------------------------------------------------------------------
69 ; LOCAL VARIABLE DEFINITIONS
70 ; Variable declaration - defined here and used outside this module
71 ----------------------------------------------------------------------------*/
72
73
74 /*
75 ------------------------------------------------------------------------------
76  FUNCTION NAME: ton_stab_init
77 ------------------------------------------------------------------------------
78  INPUT AND OUTPUT DEFINITIONS
79
80  Inputs:
81     state = pointer to pointer to structure type tonStabState.
82
83  Outputs:
84     None
85
86  Returns:
87     None.
88
89  Global Variables Used:
90     None
91
92  Local Variables Needed:
93     None
94
95 ------------------------------------------------------------------------------
96  FUNCTION DESCRIPTION
97
98   Function:   ton_stab_init
99   Purpose:    Allocates state memory and initializes state memory
100
101 ------------------------------------------------------------------------------
102  REQUIREMENTS
103
104   None.
105
106 ------------------------------------------------------------------------------
107  REFERENCES
108
109  ton_stab.c, UMTS GSM AMR speech codec, R99 - Version 3.2.0, March 2, 2001
110
111 ------------------------------------------------------------------------------
112  PSEUDO-CODE
113
114 int ton_stab_init (tonStabState **state)
115 {
116     tonStabState* s;
117
118     if (state == (tonStabState **) NULL){
119         // fprintf(stderr, "ton_stab_init: invalid parameter\n");
120         return -1;
121     }
122     *state = NULL;
123
124     // allocate memory
125     if ((s= (tonStabState *) malloc(sizeof(tonStabState))) == NULL){
126         // fprintf(stderr, "ton_stab_init: can not malloc state structure\n");
127         return -1;
128     }
129
130     ton_stab_reset(s);
131
132     *state = s;
133
134     return 0;
135 }
136
137
138 ------------------------------------------------------------------------------
139  CAUTION [optional]
140  [State any special notes, constraints or cautions for users of this function]
141
142 ------------------------------------------------------------------------------
143 */
144
145 Word16 ton_stab_init(tonStabState **state)
146 {
147     tonStabState* s;
148
149     if (state == (tonStabState **) NULL)
150     {
151         /* fprintf(stderr, "ton_stab_init: invalid parameter\n"); */
152         return -1;
153     }
154     *state = NULL;
155
156     /* allocate memory */
157     if ((s = (tonStabState *) oscl_malloc(sizeof(tonStabState))) == NULL)
158     {
159         /* fprintf(stderr, "ton_stab_init: can not malloc state structure\n"); */
160         return -1;
161     }
162
163     ton_stab_reset(s);
164
165     *state = s;
166
167     return 0;
168 }
169
170 /****************************************************************************/
171
172
173 /*
174 ------------------------------------------------------------------------------
175  FUNCTION NAME: ton_stab_reset
176 ------------------------------------------------------------------------------
177  INPUT AND OUTPUT DEFINITIONS
178
179  Inputs:
180     st = pointer to pointer to structure type tonStabState.
181
182  Outputs:
183     None
184
185  Returns:
186     None.
187
188  Global Variables Used:
189     None
190
191  Local Variables Needed:
192     None
193
194 ------------------------------------------------------------------------------
195  FUNCTION DESCRIPTION
196
197   Function:   ton_stab_reset
198   Purpose:    Initializes state memory to zero
199
200 ------------------------------------------------------------------------------
201  REQUIREMENTS
202
203  None.
204
205 ------------------------------------------------------------------------------
206  REFERENCES
207
208  ton_stab.c, UMTS GSM AMR speech codec, R99 - Version 3.2.0, March 2, 2001
209
210 ------------------------------------------------------------------------------
211  PSEUDO-CODE
212
213 int ton_stab_reset (tonStabState *st)
214 {
215     if (st == (tonStabState *) NULL){
216         // fprintf(stderr, "ton_stab_init: invalid parameter\n");
217         return -1;
218     }
219
220     // initialize tone stabilizer state
221     st->count = 0;
222     Set_zero(st->gp, N_FRAME);    // Init Gp_Clipping
223
224     return 0;
225 }
226
227 ------------------------------------------------------------------------------
228  CAUTION [optional]
229  [State any special notes, constraints or cautions for users of this function]
230
231 ------------------------------------------------------------------------------
232 */
233
234 Word16 ton_stab_reset(tonStabState *st)
235 {
236     if (st == (tonStabState *) NULL)
237     {
238         /* fprintf(stderr, "ton_stab_init: invalid parameter\n"); */
239         return -1;
240     }
241
242     /* initialize tone stabilizer state */
243     st->count = 0;
244     /* Init Gp_Clipping */
245     oscl_memset((void *)st->gp,  0, N_FRAME*sizeof(*st->gp));
246
247
248     return 0;
249 }
250
251 /****************************************************************************/
252
253 /*
254 ------------------------------------------------------------------------------
255  FUNCTION NAME: ton_stab_exit
256 ------------------------------------------------------------------------------
257  INPUT AND OUTPUT DEFINITIONS
258
259  Inputs:
260     state = pointer to pointer to structure type tonStabState.
261
262  Outputs:
263     None
264
265  Returns:
266     None.
267
268  Global Variables Used:
269     None
270
271  Local Variables Needed:
272     None
273
274 ------------------------------------------------------------------------------
275  FUNCTION DESCRIPTION
276
277   Function:   ton_stab_exit
278   Purpose:    The memory used for state memory is freed
279
280 ------------------------------------------------------------------------------
281  REQUIREMENTS
282
283  None.
284
285 ------------------------------------------------------------------------------
286  REFERENCES
287
288  ton_stab.c, UMTS GSM AMR speech codec, R99 - Version 3.2.0, March 2, 2001
289
290 ------------------------------------------------------------------------------
291  PSEUDO-CODE
292
293 void ton_stab_exit (tonStabState **state)
294 {
295     if (state == NULL || *state == NULL)
296         return;
297
298     // deallocate memory
299     free(*state);
300     *state = NULL;
301
302     return;
303 }
304
305 ------------------------------------------------------------------------------
306  CAUTION [optional]
307  [State any special notes, constraints or cautions for users of this function]
308
309 ------------------------------------------------------------------------------
310 */
311
312 void ton_stab_exit(tonStabState **state)
313 {
314     if (state == NULL || *state == NULL)
315         return;
316
317     /* deallocate memory */
318     oscl_free(*state);
319     *state = NULL;
320
321     return;
322 }
323
324 /****************************************************************************/
325
326 /*
327 ------------------------------------------------------------------------------
328  FUNCTION NAME: check_lsp
329 ------------------------------------------------------------------------------
330  INPUT AND OUTPUT DEFINITIONS
331
332  Inputs:
333     state = pointer to pointer to structure type tonStabState.
334     lsp   = pointer to unquantized LSPs of type Word16
335
336  Outputs:
337     pOverflow = 1 if there is an overflow else it is zero.
338
339  Returns:
340     None.
341
342  Global Variables Used:
343     None
344
345  Local Variables Needed:
346     None
347
348 ------------------------------------------------------------------------------
349  FUNCTION DESCRIPTION
350
351   Function:  check_lsp()
352   Purpose:   Check the LSP's to detect resonances
353
354 ------------------------------------------------------------------------------
355  REQUIREMENTS
356
357  None.
358
359 ------------------------------------------------------------------------------
360  REFERENCES
361
362  ton_stab.c, UMTS GSM AMR speech codec, R99 - Version 3.2.0, March 2, 2001
363
364 ------------------------------------------------------------------------------
365  PSEUDO-CODE
366
367 Word16 check_lsp(tonStabState *st, // i/o : State struct
368                  Word16 *lsp       // i   : unquantized LSP's
369 )
370 {
371    Word16 i, dist, dist_min1, dist_min2, dist_th;
372
373    // Check for a resonance:
374    // Find minimum distance between lsp[i] and lsp[i+1]
375
376    dist_min1 = MAX_16;
377    for (i = 3; i < M-2; i++)
378    {
379       dist = sub(lsp[i], lsp[i+1]);
380
381       if (sub(dist, dist_min1) < 0)
382       {
383          dist_min1 = dist;
384       }
385    }
386
387    dist_min2 = MAX_16;
388    for (i = 1; i < 3; i++)
389    {
390       dist = sub(lsp[i], lsp[i+1]);
391
392       if (sub(dist, dist_min2) < 0)
393       {
394          dist_min2 = dist;
395       }
396    }
397
398    if (sub(lsp[1], 32000) > 0)
399    {
400       dist_th = 600;
401    }
402    else if (sub(lsp[1], 30500) > 0)
403    {
404       dist_th = 800;
405    }
406    else
407    {
408       dist_th = 1100;
409    }
410
411    if (sub(dist_min1, 1500) < 0 ||
412        sub(dist_min2, dist_th) < 0)
413    {
414       st->count = add(st->count, 1);
415    }
416    else
417    {
418       st->count = 0;
419    }
420
421    // Need 12 consecutive frames to set the flag
422    if (sub(st->count, 12) >= 0)
423    {
424       st->count = 12;
425       return 1;
426    }
427    else
428    {
429       return 0;
430    }
431 }
432
433 ------------------------------------------------------------------------------
434  CAUTION [optional]
435  [State any special notes, constraints or cautions for users of this function]
436
437 ------------------------------------------------------------------------------
438 */
439
440 Word16 check_lsp(tonStabState *st, /* i/o : State struct            */
441                  Word16 *lsp,      /* i   : unquantized LSP's       */
442                  Flag  *pOverflow
443                 )
444 {
445     Word16 i;
446     Word16 dist;
447     Word16 dist_min1;
448     Word16 dist_min2;
449     Word16 dist_th;
450     Word16 *p_lsp   = &lsp[3];
451     Word16 *p_lsp_1 = &lsp[4];
452
453     OSCL_UNUSED_ARG(pOverflow);
454     /* Check for a resonance:                             */
455     /* Find minimum distance between lsp[i] and lsp[i+1]  */
456
457     dist_min1 = MAX_16;
458     for (i = 3; i < M - 2; i++)
459     {
460         dist = *(p_lsp++) - *(p_lsp_1++);
461
462         if (dist < dist_min1)
463         {
464             dist_min1 = dist;
465         }
466     }
467
468     dist_min2 = MAX_16;
469     p_lsp   = &lsp[1];
470     p_lsp_1 = &lsp[2];
471
472     for (i = 1; i < 3; i++)
473     {
474         dist = *(p_lsp++) - *(p_lsp_1++);
475
476         if (dist < dist_min2)
477         {
478             dist_min2 = dist;
479         }
480     }
481
482     if (lsp[1] > 32000)
483     {
484         dist_th = 600;
485     }
486     else if (lsp[1] > 30500)
487     {
488         dist_th = 800;
489     }
490     else
491     {
492         dist_th = 1100;
493     }
494
495     if ((dist_min1 < 1500) || (dist_min2 < dist_th))
496     {
497         st->count++;
498     }
499     else
500     {
501         st->count = 0;
502     }
503
504     /* Need 12 consecutive frames to set the flag */
505     if (st->count >= 12)
506     {
507         st->count = 12;
508         return 1;
509     }
510     else
511     {
512         return 0;
513     }
514 }
515
516 /****************************************************************************/
517
518 /*
519 ------------------------------------------------------------------------------
520  FUNCTION NAME: check_gp_clipping
521 ------------------------------------------------------------------------------
522  INPUT AND OUTPUT DEFINITIONS
523
524  Inputs:
525     state = pointer to pointer to structure type tonStabState.
526     g_pitch = pitch gain of type Word16
527
528  Outputs:
529     pOverflow = 1 if there is an overflow else it is zero.
530
531  Returns:
532     None.
533
534  Global Variables Used:
535     None
536
537  Local Variables Needed:
538     None
539
540 ------------------------------------------------------------------------------
541  FUNCTION DESCRIPTION
542
543   Function:   Check_Gp_Clipping()
544   Purpose:    Verify that the sum of the last (N_FRAME+1) pitch
545               gains is under a certain threshold.
546
547 ------------------------------------------------------------------------------
548  REQUIREMENTS
549
550  None.
551
552 ------------------------------------------------------------------------------
553  REFERENCES
554
555  ton_stab.c, UMTS GSM AMR speech codec, R99 - Version 3.2.0, March 2, 2001
556
557 ------------------------------------------------------------------------------
558  PSEUDO-CODE
559
560 Word16 check_gp_clipping(tonStabState *st, // i/o : State struct
561                          Word16 g_pitch    // i   : pitch gain
562 )
563 {
564    Word16 i, sum;
565
566    sum = shr(g_pitch, 3);          // Division by 8
567    for (i = 0; i < N_FRAME; i++)
568    {
569       sum = add(sum, st->gp[i]);
570    }
571
572    if (sub(sum, GP_CLIP) > 0)
573    {
574       return 1;
575    }
576    else
577    {
578       return 0;
579    }
580 }
581
582 ------------------------------------------------------------------------------
583  CAUTION [optional]
584  [State any special notes, constraints or cautions for users of this function]
585
586 ------------------------------------------------------------------------------
587 */
588
589 Word16 check_gp_clipping(tonStabState *st, /* i/o : State struct            */
590                          Word16 g_pitch,   /* i   : pitch gain              */
591                          Flag   *pOverflow
592                         )
593 {
594     Word16 i;
595     Word16 sum;
596
597     sum = shr(g_pitch, 3, pOverflow);        /* Division by 8 */
598     for (i = 0; i < N_FRAME; i++)
599     {
600         sum = add_16(sum, st->gp[i], pOverflow);
601     }
602
603     if (sum > GP_CLIP)
604     {
605         return 1;
606     }
607     else
608     {
609         return 0;
610     }
611 }
612
613 /****************************************************************************/
614
615 /*
616 ------------------------------------------------------------------------------
617  FUNCTION NAME: update_gp_clipping
618 ------------------------------------------------------------------------------
619  INPUT AND OUTPUT DEFINITIONS
620
621  Inputs:
622     state = pointer to pointer to structure type tonStabState.
623     g_pitch = pitch gain of type Word16
624
625  Outputs:
626     pOverflow = 1 if there is an overflow else it is zero.
627
628  Returns:
629     None.
630
631  Global Variables Used:
632     None
633
634  Local Variables Needed:
635     None
636
637 ------------------------------------------------------------------------------
638  FUNCTION DESCRIPTION
639
640   Function:  Update_Gp_Clipping()
641   Purpose:   Update past pitch gain memory
642
643 ------------------------------------------------------------------------------
644  REQUIREMENTS
645
646  None.
647
648 ------------------------------------------------------------------------------
649  REFERENCES
650
651  ton_stab.c, UMTS GSM AMR speech codec, R99 - Version 3.2.0, March 2, 2001
652
653 ------------------------------------------------------------------------------
654  PSEUDO-CODE
655
656 void update_gp_clipping(tonStabState *st, // i/o : State struct
657                         Word16 g_pitch    // i   : pitch gain
658 )
659 {
660    Copy(&st->gp[1], &st->gp[0], N_FRAME-1);
661    st->gp[N_FRAME-1] = shr(g_pitch, 3);
662 }
663
664 ------------------------------------------------------------------------------
665  CAUTION [optional]
666  [State any special notes, constraints or cautions for users of this function]
667
668 ------------------------------------------------------------------------------
669 */
670
671 void update_gp_clipping(tonStabState *st, /* i/o : State struct            */
672                         Word16 g_pitch,   /* i   : pitch gain              */
673                         Flag   *pOverflow
674                        )
675 {
676     OSCL_UNUSED_ARG(pOverflow);
677     for (int i = 0; i < N_FRAME - 1; i++)
678     {
679         st->gp[i] = st->gp[i+1];
680     }
681     st->gp[N_FRAME-1] =  g_pitch >> 3;
682 }
683