summaryrefslogtreecommitdiff
path: root/codecs/ilbc/gainquant.c
blob: 0e74ff827050bc56fd50755ce31944c92e319ad2 (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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116

   /******************************************************************

       iLBC Speech Coder ANSI-C Source Code






       gainquant.c

       Copyright (C) The Internet Society (2004).
       All Rights Reserved.

   ******************************************************************/

   #include <string.h>
   #include <math.h>
   #include "constants.h"
   #include "filter.h"

   /*----------------------------------------------------------------*
    *  quantizer for the gain in the gain-shape coding of residual
    *---------------------------------------------------------------*/

   float gainquant(/* (o) quantized gain value */
       float in,       /* (i) gain value */
       float maxIn,/* (i) maximum of gain value */
       int cblen,      /* (i) number of quantization indices */
       int *index      /* (o) quantization index */
   ){
       int i, tindex;
       float minmeasure,measure, *cb, scale;

       /* ensure a lower bound on the scaling factor */

       scale=maxIn;

       if (scale<0.1) {
           scale=(float)0.1;
       }

       /* select the quantization table */

       if (cblen == 8) {
           cb = gain_sq3Tbl;
       } else if (cblen == 16) {
           cb = gain_sq4Tbl;
       } else  {
           cb = gain_sq5Tbl;
       }

       /* select the best index in the quantization table */

       minmeasure=10000000.0;
       tindex=0;
       for (i=0; i<cblen; i++) {





           measure=(in-scale*cb[i])*(in-scale*cb[i]);

           if (measure<minmeasure) {
               tindex=i;
               minmeasure=measure;
           }
       }
       *index=tindex;

       /* return the quantized value */

       return scale*cb[tindex];
   }

   /*----------------------------------------------------------------*
    *  decoder for quantized gains in the gain-shape coding of
    *  residual
    *---------------------------------------------------------------*/

   float gaindequant(  /* (o) quantized gain value */
       int index,      /* (i) quantization index */
       float maxIn,/* (i) maximum of unquantized gain */
       int cblen       /* (i) number of quantization indices */
   ){
       float scale;

       /* obtain correct scale factor */

       scale=(float)fabs(maxIn);

       if (scale<0.1) {
           scale=(float)0.1;
       }

       /* select the quantization table and return the decoded value */

       if (cblen==8) {
           return scale*gain_sq3Tbl[index];
       } else if (cblen==16) {
           return scale*gain_sq4Tbl[index];
       }
       else if (cblen==32) {
           return scale*gain_sq5Tbl[index];
       }

       return 0.0;
   }