2
|
1
|
|
2 /******************************************************************
|
|
3
|
|
4 iLBC Speech Coder ANSI-C Source Code
|
|
5
|
|
6
|
|
7
|
|
8
|
|
9
|
|
10
|
|
11 gainquant.c
|
|
12
|
|
13 Copyright (C) The Internet Society (2004).
|
|
14 All Rights Reserved.
|
|
15
|
|
16 ******************************************************************/
|
|
17
|
|
18 #include <string.h>
|
|
19 #include <math.h>
|
|
20 #include "constants.h"
|
|
21 #include "filter.h"
|
|
22
|
|
23 /*----------------------------------------------------------------*
|
|
24 * quantizer for the gain in the gain-shape coding of residual
|
|
25 *---------------------------------------------------------------*/
|
|
26
|
|
27 float gainquant( /* (o) quantized gain value */
|
|
28 float in, /* (i) gain value */
|
|
29 float maxIn, /* (i) maximum of gain value */
|
|
30 int cblen, /* (i) number of quantization indices */
|
|
31 int *index /* (o) quantization index */
|
|
32 )
|
|
33 {
|
|
34 int i, tindex;
|
|
35 float minmeasure, measure, *cb, scale;
|
|
36
|
|
37 /* ensure a lower bound on the scaling factor */
|
|
38
|
|
39 scale = maxIn;
|
|
40
|
|
41 if (scale < 0.1) {
|
|
42 scale = (float) 0.1;
|
|
43 }
|
|
44
|
|
45 /* select the quantization table */
|
|
46
|
|
47 if (cblen == 8) {
|
|
48 cb = gain_sq3Tbl;
|
|
49 } else if (cblen == 16) {
|
|
50 cb = gain_sq4Tbl;
|
|
51 } else {
|
|
52 cb = gain_sq5Tbl;
|
|
53 }
|
|
54
|
|
55 /* select the best index in the quantization table */
|
|
56
|
|
57 minmeasure = 10000000.0;
|
|
58 tindex = 0;
|
|
59 for (i = 0; i < cblen; i++) {
|
|
60
|
|
61
|
|
62
|
|
63
|
|
64
|
|
65 measure = (in - scale * cb[i]) * (in - scale * cb[i]);
|
|
66
|
|
67 if (measure < minmeasure) {
|
|
68 tindex = i;
|
|
69 minmeasure = measure;
|
|
70 }
|
|
71 }
|
|
72 *index = tindex;
|
|
73
|
|
74 /* return the quantized value */
|
|
75
|
|
76 return scale * cb[tindex];
|
|
77 }
|
|
78
|
|
79 /*----------------------------------------------------------------*
|
|
80 * decoder for quantized gains in the gain-shape coding of
|
|
81 * residual
|
|
82 *---------------------------------------------------------------*/
|
|
83
|
|
84 float gaindequant( /* (o) quantized gain value */
|
|
85 int index, /* (i) quantization index */
|
|
86 float maxIn, /* (i) maximum of unquantized gain */
|
|
87 int cblen /* (i) number of quantization indices */
|
|
88 )
|
|
89 {
|
|
90 float scale;
|
|
91
|
|
92 /* obtain correct scale factor */
|
|
93
|
|
94 scale = (float) fabs(maxIn);
|
|
95
|
|
96 if (scale < 0.1) {
|
|
97 scale = (float) 0.1;
|
|
98 }
|
|
99
|
|
100 /* select the quantization table and return the decoded value */
|
|
101
|
|
102 if (cblen == 8) {
|
|
103 return scale * gain_sq3Tbl[index];
|
|
104 } else if (cblen == 16) {
|
|
105 return scale * gain_sq4Tbl[index];
|
|
106 } else if (cblen == 32) {
|
|
107 return scale * gain_sq5Tbl[index];
|
|
108 }
|
|
109
|
|
110 return 0.0;
|
|
111 }
|