comparison libtwamr/g_code.c @ 366:1588a7d9e732

libtwamr: integrate g_code.c
author Mychaela Falconia <falcon@freecalypso.org>
date Mon, 06 May 2024 03:04:57 +0000
parents
children
comparison
equal deleted inserted replaced
365:2a265be82195 366:1588a7d9e732
1 /*
2 ********************************************************************************
3 *
4 * GSM AMR-NB speech codec R98 Version 7.6.0 December 12, 2001
5 * R99 Version 3.3.0
6 * REL-4 Version 4.1.0
7 *
8 ********************************************************************************
9 *
10 * File : g_code.c
11 * Purpose : Compute the innovative codebook gain.
12 *
13 ********************************************************************************
14 */
15 /*
16 ********************************************************************************
17 * MODULE INCLUDE FILE AND VERSION ID
18 ********************************************************************************
19 */
20 #include "namespace.h"
21 #include "g_code.h"
22
23 /*
24 ********************************************************************************
25 * INCLUDE FILES
26 ********************************************************************************
27 */
28 #include "typedef.h"
29 #include "basic_op.h"
30 #include "no_count.h"
31 #include "cnst.h"
32
33 /*
34 ********************************************************************************
35 * LOCAL VARIABLES AND TABLES
36 ********************************************************************************
37 */
38
39 /*
40 ********************************************************************************
41 * PUBLIC PROGRAM CODE
42 ********************************************************************************
43 */
44 /*************************************************************************
45 *
46 * FUNCTION: G_code
47 *
48 * PURPOSE: Compute the innovative codebook gain.
49 *
50 * DESCRIPTION:
51 * The innovative codebook gain is given by
52 *
53 * g = <x[], y[]> / <y[], y[]>
54 *
55 * where x[] is the target vector, y[] is the filtered innovative
56 * codevector, and <> denotes dot product.
57 *
58 *************************************************************************/
59 Word16 G_code ( /* out : Gain of innovation code */
60 Word16 xn2[], /* in : target vector */
61 Word16 y2[] /* in : filtered innovation vector */
62 )
63 {
64 Word16 i;
65 Word16 xy, yy, exp_xy, exp_yy, gain;
66 Word16 scal_y2[L_SUBFR];
67 Word32 s;
68
69 /* Scale down Y[] by 2 to avoid overflow */
70
71 for (i = 0; i < L_SUBFR; i++)
72 {
73 scal_y2[i] = shr (y2[i], 1); move16 ();
74 }
75
76 /* Compute scalar product <X[],Y[]> */
77
78 s = 1L; move32 (); /* Avoid case of all zeros */
79 for (i = 0; i < L_SUBFR; i++)
80 {
81 s = L_mac (s, xn2[i], scal_y2[i]);
82 }
83 exp_xy = norm_l (s);
84 xy = extract_h (L_shl (s, exp_xy));
85
86 /* If (xy < 0) gain = 0 */
87
88 test ();
89 if (xy <= 0)
90 return ((Word16) 0);
91
92 /* Compute scalar product <Y[],Y[]> */
93
94 s = 0L; move32 ();
95 for (i = 0; i < L_SUBFR; i++)
96 {
97 s = L_mac (s, scal_y2[i], scal_y2[i]);
98 }
99 exp_yy = norm_l (s);
100 yy = extract_h (L_shl (s, exp_yy));
101
102 /* compute gain = xy/yy */
103
104 xy = shr (xy, 1); /* Be sure xy < yy */
105 gain = div_s (xy, yy);
106
107 /* Denormalization of division */
108 i = add (exp_xy, 5); /* 15-1+9-18 = 5 */
109 i = sub (i, exp_yy);
110
111 gain = shl (shr (gain, i), 1); /* Q0 -> Q1 */
112
113 return (gain);
114 }