Initialize Tizen 2.3
[external/opencore-amr.git] / opencore / codecs_v2 / audio / gsm_amr / amr_wb / dec / src / voice_factor.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.173
22     ANSI-C code for the Adaptive Multi-Rate - Wideband (AMR-WB) speech codec
23     Available from http://www.3gpp.org
24
25 (C) 2007, 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: voice_factor.cpp
35
36 ------------------------------------------------------------------------------
37  INPUT AND OUTPUT DEFINITIONS
38
39      int16 exc[],        (i) Q_exc : pitch excitation
40      int16 Q_exc,        (i)       : exc format
41      int16 gain_pit,     (i) Q14   : gain of pitch
42      int16 code[],       (i) Q9    : Fixed codebook excitation
43      int16 gain_code,    (i) Q0    : gain of code
44      int16 L_subfr       (i)       : subframe length
45
46 ------------------------------------------------------------------------------
47  FUNCTION DESCRIPTION
48
49     Find the voicing factor (1=voice to -1=unvoiced).
50
51 ------------------------------------------------------------------------------
52  REQUIREMENTS
53
54
55 ------------------------------------------------------------------------------
56  REFERENCES
57
58 ------------------------------------------------------------------------------
59  PSEUDO-CODE
60
61 ------------------------------------------------------------------------------
62 */
63
64
65 /*----------------------------------------------------------------------------
66 ; INCLUDES
67 ----------------------------------------------------------------------------*/
68
69 #include "pv_amr_wb_type_defs.h"
70 #include "pvamrwbdecoder_basic_op.h"
71 #include "pvamrwb_math_op.h"
72 #include "pvamrwbdecoder_acelp.h"
73
74 /*----------------------------------------------------------------------------
75 ; MACROS
76 ; Define module specific macros here
77 ----------------------------------------------------------------------------*/
78
79
80 /*----------------------------------------------------------------------------
81 ; DEFINES
82 ; Include all pre-processor statements here. Include conditional
83 ; compile variables also.
84 ----------------------------------------------------------------------------*/
85
86 /*----------------------------------------------------------------------------
87 ; LOCAL FUNCTION DEFINITIONS
88 ; Function Prototype declaration
89 ----------------------------------------------------------------------------*/
90
91 /*----------------------------------------------------------------------------
92 ; LOCAL STORE/BUFFER/POINTER DEFINITIONS
93 ; Variable declaration - defined here and used outside this module
94 ----------------------------------------------------------------------------*/
95
96 /*----------------------------------------------------------------------------
97 ; EXTERNAL FUNCTION REFERENCES
98 ; Declare functions defined elsewhere and referenced in this module
99 ----------------------------------------------------------------------------*/
100
101 /*----------------------------------------------------------------------------
102 ; EXTERNAL GLOBAL STORE/BUFFER/POINTER REFERENCES
103 ; Declare variables used in this module but defined elsewhere
104 ----------------------------------------------------------------------------*/
105
106 /*----------------------------------------------------------------------------
107 ; FUNCTION CODE
108 ----------------------------------------------------------------------------*/
109
110 int16 voice_factor(    /* (o) Q15   : factor (-1=unvoiced to 1=voiced) */
111     int16 exc[],      /* (i) Q_exc : pitch excitation                 */
112     int16 Q_exc,      /* (i)       : exc format                       */
113     int16 gain_pit,   /* (i) Q14   : gain of pitch                    */
114     int16 code[],     /* (i) Q9    : Fixed codebook excitation        */
115     int16 gain_code,  /* (i) Q0    : gain of code                     */
116     int16 L_subfr     /* (i)       : subframe length                  */
117 )
118 {
119     int16 i, tmp, exp, ener1, exp1, ener2, exp2;
120     int32 L_tmp;
121
122     ener1 = extract_h(Dot_product12(exc, exc, L_subfr, &exp1));
123     exp1 = sub_int16(exp1, Q_exc << 1);
124     L_tmp = mul_16by16_to_int32(gain_pit, gain_pit);
125     exp = normalize_amr_wb(L_tmp);
126
127     tmp = (int16)((L_tmp << exp) >> 16);
128     ener1 = mult_int16(ener1, tmp);
129     exp1 -= (exp + 10);        /* 10 -> gain_pit Q14 to Q9 */
130
131     ener2 = extract_h(Dot_product12(code, code, L_subfr, &exp2));
132
133     exp = norm_s(gain_code);
134     tmp = shl_int16(gain_code, exp);
135     tmp = mult_int16(tmp, tmp);
136     ener2 = mult_int16(ener2, tmp);
137     exp2 -= (exp << 1);
138
139     i = exp1 - exp2;
140
141
142     if (i >= 0)
143     {
144         ener1 >>=  1;
145         ener2 >>= (i + 1);
146     }
147     else
148     {
149         ener1 >>= (1 - i);
150         ener2 >>= 1;
151     }
152
153     tmp = ener1 - ener2;
154     ener1 += ener2 + 1;
155
156
157     if (tmp >= 0)
158     {
159         tmp = div_16by16(tmp, ener1);
160     }
161     else
162     {
163         tmp = negate_int16(div_16by16(negate_int16(tmp), ener1));
164     }
165
166     return (tmp);
167 }