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/****************************************************************************************
19Portions 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)
26Permission to distribute, modify and use this file under the standard license
27terms listed above has been obtained from the copyright holder.
28****************************************************************************************/
29/*
30------------------------------------------------------------------------------
31
32
33
34 Filename: voice_factor.cpp
35
36     Date: 05/08/2007
37
38------------------------------------------------------------------------------
39 REVISION HISTORY
40
41
42 Description:
43
44------------------------------------------------------------------------------
45 INPUT AND OUTPUT DEFINITIONS
46
47     int16 exc[],        (i) Q_exc : pitch excitation
48     int16 Q_exc,        (i)       : exc format
49     int16 gain_pit,     (i) Q14   : gain of pitch
50     int16 code[],       (i) Q9    : Fixed codebook excitation
51     int16 gain_code,    (i) Q0    : gain of code
52     int16 L_subfr       (i)       : subframe length
53
54------------------------------------------------------------------------------
55 FUNCTION DESCRIPTION
56
57    Find the voicing factor (1=voice to -1=unvoiced).
58
59------------------------------------------------------------------------------
60 REQUIREMENTS
61
62
63------------------------------------------------------------------------------
64 REFERENCES
65
66------------------------------------------------------------------------------
67 PSEUDO-CODE
68
69------------------------------------------------------------------------------
70*/
71
72
73/*----------------------------------------------------------------------------
74; INCLUDES
75----------------------------------------------------------------------------*/
76
77#include "pv_amr_wb_type_defs.h"
78#include "pvamrwbdecoder_basic_op.h"
79#include "pvamrwb_math_op.h"
80#include "pvamrwbdecoder_acelp.h"
81
82/*----------------------------------------------------------------------------
83; MACROS
84; Define module specific macros here
85----------------------------------------------------------------------------*/
86
87
88/*----------------------------------------------------------------------------
89; DEFINES
90; Include all pre-processor statements here. Include conditional
91; compile variables also.
92----------------------------------------------------------------------------*/
93
94/*----------------------------------------------------------------------------
95; LOCAL FUNCTION DEFINITIONS
96; Function Prototype declaration
97----------------------------------------------------------------------------*/
98
99/*----------------------------------------------------------------------------
100; LOCAL STORE/BUFFER/POINTER DEFINITIONS
101; Variable declaration - defined here and used outside this module
102----------------------------------------------------------------------------*/
103
104/*----------------------------------------------------------------------------
105; EXTERNAL FUNCTION REFERENCES
106; Declare functions defined elsewhere and referenced in this module
107----------------------------------------------------------------------------*/
108
109/*----------------------------------------------------------------------------
110; EXTERNAL GLOBAL STORE/BUFFER/POINTER REFERENCES
111; Declare variables used in this module but defined elsewhere
112----------------------------------------------------------------------------*/
113
114/*----------------------------------------------------------------------------
115; FUNCTION CODE
116----------------------------------------------------------------------------*/
117
118int16 voice_factor(    /* (o) Q15   : factor (-1=unvoiced to 1=voiced) */
119    int16 exc[],      /* (i) Q_exc : pitch excitation                 */
120    int16 Q_exc,      /* (i)       : exc format                       */
121    int16 gain_pit,   /* (i) Q14   : gain of pitch                    */
122    int16 code[],     /* (i) Q9    : Fixed codebook excitation        */
123    int16 gain_code,  /* (i) Q0    : gain of code                     */
124    int16 L_subfr     /* (i)       : subframe length                  */
125)
126{
127    int16 i, tmp, exp, ener1, exp1, ener2, exp2;
128    int32 L_tmp;
129
130    ener1 = extract_h(Dot_product12(exc, exc, L_subfr, &exp1));
131    exp1 = sub_int16(exp1, Q_exc << 1);
132    L_tmp = mul_16by16_to_int32(gain_pit, gain_pit);
133    exp = normalize_amr_wb(L_tmp);
134
135    tmp = (int16)((L_tmp << exp) >> 16);
136    ener1 = mult_int16(ener1, tmp);
137    exp1 -= (exp + 10);        /* 10 -> gain_pit Q14 to Q9 */
138
139    ener2 = extract_h(Dot_product12(code, code, L_subfr, &exp2));
140
141    exp = norm_s(gain_code);
142    tmp = shl_int16(gain_code, exp);
143    tmp = mult_int16(tmp, tmp);
144    ener2 = mult_int16(ener2, tmp);
145    exp2 -= (exp << 1);
146
147    i = exp1 - exp2;
148
149
150    if (i >= 0)
151    {
152        ener1 >>=  1;
153        ener2 >>= (i + 1);
154    }
155    else
156    {
157        ener1 >>= (1 - i);
158        ener2 >>= 1;
159    }
160
161    tmp = ener1 - ener2;
162    ener1 += ener2 + 1;
163
164
165    if (tmp >= 0)
166    {
167        tmp = div_16by16(tmp, ener1);
168    }
169    else
170    {
171        tmp = negate_int16(div_16by16(negate_int16(tmp), ener1));
172    }
173
174    return (tmp);
175}
176