IT++ Logo
g711.cpp
Go to the documentation of this file.
1 
28 #include <itpp/srccode/g711.h>
29 
30 namespace itpp
31 {
32 
33 namespace g711_details {
34  //define tables used in compression and expansion algorithms
35  uint8_t G711_Base_Properties::compression_table[128] =
36  {
37  0, 1, 2, 2, 3, 3, 3, 3, 4, 4, 4, 4, 4, 4, 4, 4,
38  5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5,
39  6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6,
40  6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6,
41  7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7,
42  7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7,
43  7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7,
44  7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7
45  };
46  int16_t MuLaw_Properties::expansion_table[256] =
47  {
48  -8031, -7775, -7519, -7263, -7007, -6751, -6495, -6239,
49  -5983, -5727, -5471, -5215, -4959, -4703, -4447, -4191,
50  -3999, -3871, -3743, -3615, -3487, -3359, -3231, -3103,
51  -2975, -2847, -2719, -2591, -2463, -2335, -2207, -2079,
52  -1983, -1919, -1855, -1791, -1727, -1663, -1599, -1535,
53  -1471, -1407, -1343, -1279, -1215, -1151, -1087, -1023,
54  -975, -943, -911, -879, -847, -815, -783, -751,
55  -719, -687, -655, -623, -591, -559, -527, -495,
56  -471, -455, -439, -423, -407, -391, -375, -359,
57  -343, -327, -311, -295, -279, -263, -247, -231,
58  -219, -211, -203, -195, -187, -179, -171, -163,
59  -155, -147, -139, -131, -123, -115, -107, -99,
60  -93, -89, -85, -81, -77, -73, -69, -65,
61  -61, -57, -53, -49, -45, -41, -37, -33,
62  -30, -28, -26, -24, -22, -20, -18, -16,
63  -14, -12, -10, -8, -6, -4, -2, 0,
64  8031, 7775, 7519, 7263, 7007, 6751, 6495, 6239,
65  5983, 5727, 5471, 5215, 4959, 4703, 4447, 4191,
66  3999, 3871, 3743, 3615, 3487, 3359, 3231, 3103,
67  2975, 2847, 2719, 2591, 2463, 2335, 2207, 2079,
68  1983, 1919, 1855, 1791, 1727, 1663, 1599, 1535,
69  1471, 1407, 1343, 1279, 1215, 1151, 1087, 1023,
70  975, 943, 911, 879, 847, 815, 783, 751,
71  719, 687, 655, 623, 591, 559, 527, 495,
72  471, 455, 439, 423, 407, 391, 375, 359,
73  343, 327, 311, 295, 279, 263, 247, 231,
74  219, 211, 203, 195, 187, 179, 171, 163,
75  155, 147, 139, 131, 123, 115, 107, 99,
76  93, 89, 85, 81, 77, 73, 69, 65,
77  61, 57, 53, 49, 45, 41, 37, 33,
78  30, 28, 26, 24, 22, 20, 18, 16,
79  14, 12, 10, 8, 6, 4, 2, 0
80  };
81  int16_t ALaw_Properties::expansion_table[256] =
82  {
83  -688, -656, -752, -720, -560, -528, -624, -592,
84  -944, -912, -1008, -976, -816, -784, -880, -848,
85  -344, -328, -376, -360, -280, -264, -312, -296,
86  -472, -456, -504, -488, -408, -392, -440, -424,
87  -2752, -2624, -3008, -2880, -2240, -2112, -2496, -2368,
88  -3776, -3648, -4032, -3904, -3264, -3136, -3520, -3392,
89  -1376, -1312, -1504, -1440, -1120, -1056, -1248, -1184,
90  -1888, -1824, -2016, -1952, -1632, -1568, -1760, -1696,
91  -43, -41, -47, -45, -35, -33, -39, -37,
92  -59, -57, -63, -61, -51, -49, -55, -53,
93  -11, -9, -15, -13, -3, -1, -7, -5,
94  -27, -25, -31, -29, -19, -17, -23, -21,
95  -172, -164, -188, -180, -140, -132, -156, -148,
96  -236, -228, -252, -244, -204, -196, -220, -212,
97  -86, -82, -94, -90, -70, -66, -78, -74,
98  -118, -114, -126, -122, -102, -98, -110, -106,
99  688, 656, 752, 720, 560, 528, 624, 592,
100  944, 912, 1008, 976, 816, 784, 880, 848,
101  344, 328, 376, 360, 280, 264, 312, 296,
102  472, 456, 504, 488, 408, 392, 440, 424,
103  2752, 2624, 3008, 2880, 2240, 2112, 2496, 2368,
104  3776, 3648, 4032, 3904, 3264, 3136, 3520, 3392,
105  1376, 1312, 1504, 1440, 1120, 1056, 1248, 1184,
106  1888, 1824, 2016, 1952, 1632, 1568, 1760, 1696,
107  43, 41, 47, 45, 35, 33, 39, 37,
108  59, 57, 63, 61, 51, 49, 55, 53,
109  11, 9, 15, 13, 3, 1, 7, 5,
110  27, 25, 31, 29, 19, 17, 23, 21,
111  172, 164, 188, 180, 140, 132, 156, 148,
112  236, 228, 252, 244, 204, 196, 220, 212,
113  86, 82, 94, 90, 70, 66, 78, 74,
114  118, 114, 126, 122, 102, 98, 110, 106
115  };
116 
117  /*
118  //These two functions generate and print compression-expansion tables defined above.
119  //They can be used to create these tables dynamically.
120  void generate_tables()
121  {
122  //generation of table used in compression - compute segment
123  //numbers for all possible values of MSBs
124  uint8_t cmp_val = 1;
125  uint8_t segno = 0;
126  for(uint8_t i = 0; i < 128; ++i) {
127  if(i == cmp_val) { //detect leading 1
128  cmp_val <<= 1; segno++;
129  }
130  G711_Base_Properties::compression_table[i] = segno;
131  }
132 
133  //generation of expansion table (inverse encoding)
134  for(uint16_t i = 0; i < 256; ++i){
135  uint16_t mantissa = ~i; // 1's complement of the input value
136  uint8_t exponent = (mantissa >> 4) & (0x0007); // extract exponent
137  uint8_t segment = exponent + 1; //compute segment number
138  mantissa = (mantissa & 0x0f) | 0x10; // extract mantissa and add leading 1
139 
140  int16_t rounding_value = 1 << exponent; //rounding value is equal to the half of the LSB
141  int16_t restored_magnitude = (mantissa << segment) + rounding_value
142  - MuLaw_Properties::magnitude_offset; //correct magnitude offset introduced during compression
143  MuLaw_Properties::expansion_table[i] = i & 0x80 ? restored_magnitude : -restored_magnitude;
144  }
145 
146  //generation of expansion table (inverse encoding)
147  for(uint16_t i = 0; i < 256; ++i){
148  uint16_t mantissa = i ^ 0x55; //remove even bits toggle during compression
149  uint8_t exponent = (mantissa >> 4) & (0x0007); //extract 3 bits of exponent from bits 6..4
150  mantissa = (mantissa & 0x0f); //get mantissa from bits 3..0
151  int16_t rounding_value;
152  if(exponent > 0) {
153  mantissa |= 0x10; ///add leading 1
154  rounding_value = 1 << (exponent - 1) ; //rounding value is equal to the half of the LSB
155  }
156  else {
157  mantissa <<= 1; //scale mantissa a to align with initial dynamic range
158  rounding_value = 1; //rounding value is equal to the LSB
159  }
160  //restore magnitude and sign
161  int16_t restored_magnitude = (mantissa << (exponent)) + rounding_value;
162  ALaw_Properties::expansion_table[i] = i & 0x80 ? restored_magnitude : -restored_magnitude;
163  }
164  }
165 
166  void print_tables()
167  {
168  std::cout << "compression:" << std::endl;
169  int j = 0;
170  for(int i = 0; i < 128; ++i, ++j) {
171  if(j == 16){
172  std::cout << std::endl; j = 0;
173  }
174  std::cout <<" "<< (int)G711_Base_Properties::compression_table[i];
175  if(i != 127) std::cout<<',';
176  }
177  std::cout << std::endl;
178  std::cout << "u expnasion:" << std::endl;
179  j = 0;
180  for(int i = 0; i < 256; ++i, ++j) {
181  if(j == 8) {
182  std::cout << std::endl; j = 0;
183  }
184  std::cout <<" "<< std::setw(5) << (int)MuLaw_Properties::expansion_table[i];
185  if(i != 255) std::cout<<',';
186  }
187  std::cout << std::endl;
188  std::cout << "a expansion:" << std::endl;
189  j = 0;
190  for(int i = 0; i < 256; ++i, ++j) {
191  if(j == 8) {
192  std::cout << std::endl; j = 0;
193  }
194  std::cout <<" "<< std::setw(5) << (int)ALaw_Properties::expansion_table[i];
195  if(i != 255) std::cout<<',';
196  }
197  std::cout << std::endl;
198  }
199  */
200 }
201 
202 }
203 
SourceForge Logo

Generated on Sat Jul 6 2013 10:54:25 for IT++ by Doxygen 1.8.2