summaryrefslogtreecommitdiffstats
path: root/media/libstagefright/codecs/amrwbenc/src/voicefac.c
blob: d8900446e552853223495ec213f8d7cd408c233e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
/*
 ** Copyright 2003-2010, VisualOn, Inc.
 **
 ** Licensed under the Apache License, Version 2.0 (the "License");
 ** you may not use this file except in compliance with the License.
 ** You may obtain a copy of the License at
 **
 **     http://www.apache.org/licenses/LICENSE-2.0
 **
 ** Unless required by applicable law or agreed to in writing, software
 ** distributed under the License is distributed on an "AS IS" BASIS,
 ** WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 ** See the License for the specific language governing permissions and
 ** limitations under the License.
 */

/***********************************************************************
*   File: voicefac.c                                                   *
*                                                                      *
*   Description: Find the voicing factors (1 = voice to -1 = unvoiced) *
*                                                                      *
************************************************************************/

#include "typedef.h"
#include "basic_op.h"
#include "math_op.h"

Word16 voice_factor(                                  /* (o) Q15   : factor (-1=unvoiced to 1=voiced) */
		Word16 exc[],                         /* (i) Q_exc : pitch excitation                 */
		Word16 Q_exc,                         /* (i)       : exc format                       */
		Word16 gain_pit,                      /* (i) Q14   : gain of pitch                    */
		Word16 code[],                        /* (i) Q9    : Fixed codebook excitation        */
		Word16 gain_code,                     /* (i) Q0    : gain of code                     */
		Word16 L_subfr                        /* (i)       : subframe length                  */
		)
{
	Word16 tmp, exp, ener1, exp1, ener2, exp2;
	Word32 i, L_tmp;

#ifdef ASM_OPT               /* asm optimization branch */
	ener1 = extract_h(Dot_product12_asm(exc, exc, L_subfr, &exp1));
#else
	ener1 = extract_h(Dot_product12(exc, exc, L_subfr, &exp1));
#endif
	exp1 = exp1 - (Q_exc + Q_exc);
	L_tmp = vo_L_mult(gain_pit, gain_pit);
	exp = norm_l(L_tmp);
	tmp = extract_h(L_tmp << exp);
	ener1 = vo_mult(ener1, tmp);
	exp1 = exp1 - exp - 10;        /* 10 -> gain_pit Q14 to Q9 */

#ifdef ASM_OPT                /* asm optimization branch */
	ener2 = extract_h(Dot_product12_asm(code, code, L_subfr, &exp2));
#else
	ener2 = extract_h(Dot_product12(code, code, L_subfr, &exp2));
#endif

	exp = norm_s(gain_code);
	tmp = gain_code << exp;
	tmp = vo_mult(tmp, tmp);
	ener2 = vo_mult(ener2, tmp);
	exp2 = exp2 - (exp + exp);

	i = exp1 - exp2;

	if (i >= 0)
	{
		ener1 = ener1 >> 1;
		ener2 = ener2 >> (i + 1);
	} else
	{
		ener1 = ener1 >> (1 - i);
		ener2 = ener2 >> 1;
	}

	tmp = vo_sub(ener1, ener2);
	ener1 = add1(add1(ener1, ener2), 1);

	if (tmp >= 0)
	{
		tmp = div_s(tmp, ener1);
	} else
	{
		tmp = vo_negate(div_s(vo_negate(tmp), ener1));
	}

	return (tmp);
}