forked from nostar/imbe_vocoder
-
Notifications
You must be signed in to change notification settings - Fork 0
/
qnt_sub.cc
144 lines (122 loc) · 3.73 KB
/
qnt_sub.cc
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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
/*
* Project 25 IMBE Encoder/Decoder Fixed-Point implementation
* Developed by Pavel Yazev E-mail: [email protected]
* Version 1.0 (c) Copyright 2009
*
* This is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 3, or (at your option)
* any later version.
*
* The software is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
* See the GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this; see the file COPYING. If not, write to the Free
* Software Foundation, Inc., 51 Franklin Street, Boston, MA
* 02110-1301, USA.
*/
#include "typedef.h"
#include "basic_op.h"
#include "qnt_sub.h"
//-----------------------------------------------------------------------------
// PURPOSE:
// Dequantize by quantizer step size
//
//
// INPUT:
// qval - quantized value
// step_size - step size used to quantize in unsigned Q0.16 format
// bit_num - the number of bits
//
// OUTPUT:
// None
//
// RETURN:
// Quantized Value in signed (bit_num).16 format
//
//-----------------------------------------------------------------------------
Word32 deqnt_by_step(Word16 qval, UWord16 step_size, Word16 bit_num)
{
Word32 res;
if(bit_num == 0)
return (Word32)0;
res = (Word32)step_size * (qval - (1 << (bit_num - 1)));
res = L_add(res, ((Word32)step_size * CNST_0_5_Q0_16) >> 16);
return res;
}
//-----------------------------------------------------------------------------
// PURPOSE:
// Quantize by quantizer step size
//
//
// INPUT:
// val - value to be quantized in Q5.10 format
// step_size - step size used to quantize in unsigned Q0.16 format
// bit_num - the number of bits
//
// OUTPUT:
// None
//
// RETURN:
// Quantized Value in integer
//
//-----------------------------------------------------------------------------
Word16 qnt_by_step(Word16 val, UWord16 step_size, Word16 bit_num)
{
Word16 index, min_val, max_val;
Word16 q_index, shift, tmp;
shift = norm_s(step_size);
tmp = div_s(0x4000, shl(step_size, shift)); // Remark: To get result in Qxx.16 format it is necessary left shift tmp by (shift + 3)
q_index = shr_r(mult(val, tmp), sub(9, shift)); // q_index here is rounded to the nearest integer
max_val = 1 << (bit_num - 1);
min_val = negate(max_val);
if(q_index < min_val)
index = 0;
else if(q_index >= max_val)
index = (1 << bit_num) - 1;
else
index = max_val + q_index;
return index;
}
//-----------------------------------------------------------------------------
// PURPOSE:
// Quantize by table
//
//
// INPUT:
// val - value to be quantized
// q_tbl - pointer to table
// q_tbl_size - size of table
//
// OUTPUT:
// None
//
// RETURN:
// Quantized Value in integer
//
//-----------------------------------------------------------------------------
Word16 tbl_quant(Word16 val, Word16 *q_tbl, Word16 q_tbl_size)
{
Word16 min_index, max_index, index;
min_index = 0;
max_index = q_tbl_size - 1;
if(val >= q_tbl[max_index])
return max_index;
if(val <= q_tbl[min_index])
return min_index;
while(max_index - min_index != 1)
{
index = min_index + ((max_index - min_index) >> 1);
if(q_tbl[index] > val)
max_index = index;
else
min_index = index;
}
if(q_tbl[max_index] - val <= val - q_tbl[min_index])
return max_index;
else
return min_index;
}