diff options
author | Joshua Bakita <bakitajoshua@gmail.com> | 2019-10-07 19:13:39 -0400 |
---|---|---|
committer | Joshua Bakita <bakitajoshua@gmail.com> | 2019-10-07 19:13:39 -0400 |
commit | 386b7d3366f1359a265da207a9cafa3edf553b64 (patch) | |
tree | c76120c2c138faed822e4ae386be6ef22a738a78 /all_pairs/source/g723_enc | |
parent | 54a3f7091a2146b29c73a6fdc4b62a5c4ad7a3d8 (diff) |
Reorganize and commit all the modified TACLeBench code and run scripts
Diffstat (limited to 'all_pairs/source/g723_enc')
-rw-r--r-- | all_pairs/source/g723_enc/ChangeLog.txt | 34 | ||||
-rw-r--r-- | all_pairs/source/g723_enc/g723_enc.c | 887 | ||||
-rw-r--r-- | all_pairs/source/g723_enc/license.txt | 23 |
3 files changed, 944 insertions, 0 deletions
diff --git a/all_pairs/source/g723_enc/ChangeLog.txt b/all_pairs/source/g723_enc/ChangeLog.txt new file mode 100644 index 0000000..8657026 --- /dev/null +++ b/all_pairs/source/g723_enc/ChangeLog.txt | |||
@@ -0,0 +1,34 @@ | |||
1 | File: g723_enc.c | ||
2 | Original provenience: SUN Microsystems | ||
3 | |||
4 | 2016-03-02: | ||
5 | - Renamed file to g723_enc and removed all g721 dead code | ||
6 | - Added TACLeBench header to line 1 | ||
7 | - Moved SUN license to license.txt | ||
8 | - Deleted unused code that was commented out | ||
9 | - Renamed functions prepended g723_enc to all function names | ||
10 | - Renamed function main to g723_enc_main | ||
11 | - Created new function main, calling g723_enc_init, g723_enc_main and | ||
12 | returning g723_enc_return | ||
13 | - Reordered functions in source code: initialization- and | ||
14 | return-value-related functions first, followed by algorithm core | ||
15 | functions, followed by main functions | ||
16 | - Applied code formatting with astyle as in the example | ||
17 | |||
18 | 2016-03-09: | ||
19 | - Renamed global variables, prepended g723_enc_ | ||
20 | - Removed static keyword from global variables | ||
21 | - Renamed datatype from g723_enc_g72x_state to g723_enc_state | ||
22 | - Renamed function g723_enc_g72x_init_state to g723_enc_init_state | ||
23 | |||
24 | 2016-05-23: | ||
25 | - Added initialization with volatile int | ||
26 | - Added check_sum and comparison with expected result | ||
27 | |||
28 | 2016-05-25 | ||
29 | - Changed name of struct g723_enc_state to g723_enc_state_t | ||
30 | - Changed name of variable state to g723_enc_state | ||
31 | |||
32 | 2017-07-10 | ||
33 | - Fixed undefined behaviour introduced by accessing the result of a pointer | ||
34 | type cast. | ||
diff --git a/all_pairs/source/g723_enc/g723_enc.c b/all_pairs/source/g723_enc/g723_enc.c new file mode 100644 index 0000000..6f31210 --- /dev/null +++ b/all_pairs/source/g723_enc/g723_enc.c | |||
@@ -0,0 +1,887 @@ | |||
1 | /* | ||
2 | |||
3 | This program is part of the TACLeBench benchmark suite. | ||
4 | Version V 1.x | ||
5 | |||
6 | Name: g723_enc | ||
7 | |||
8 | Author: Unknown | ||
9 | |||
10 | Function: g723 encoder. | ||
11 | |||
12 | Source: SUN Microsystems | ||
13 | |||
14 | Changes: The benchmark was changed to use the g723 encoder | ||
15 | |||
16 | License: "Unrestricted use" (see license.txt) | ||
17 | |||
18 | */ | ||
19 | |||
20 | /* | ||
21 | Declaration of data types | ||
22 | */ | ||
23 | |||
24 | /* | ||
25 | The following is the definition of the state structure | ||
26 | used by the G.721/G.723 encoder and decoder to preserve their internal | ||
27 | state between successive calls. The meanings of the majority | ||
28 | of the state structure fields are explained in detail in the | ||
29 | CCITT Recommendation G.721. The field names are essentially indentical | ||
30 | to variable names in the bit level description of the coding algorithm | ||
31 | included in this Recommendation. | ||
32 | */ | ||
33 | |||
34 | #include "../extra.h" | ||
35 | struct g723_enc_state_t { | ||
36 | long yl; /* Locked or steady state step size multiplier. */ | ||
37 | short yu; /* Unlocked or non-steady state step size multiplier. */ | ||
38 | short dms; /* Short term energy estimate. */ | ||
39 | short dml; /* Long term energy estimate. */ | ||
40 | short ap; /* Linear weighting coefficient of 'yl' and 'yu'. */ | ||
41 | |||
42 | short a[2]; /* Coefficients of pole portion of prediction filter. */ | ||
43 | short b[6]; /* Coefficients of zero portion of prediction filter. */ | ||
44 | short pk[2]; /* | ||
45 | Signs of previous two samples of a partially | ||
46 | reconstructed signal. | ||
47 | */ | ||
48 | short dq[6]; /* | ||
49 | Previous 6 samples of the quantized difference | ||
50 | signal represented in an internal floating point | ||
51 | format. | ||
52 | */ | ||
53 | short sr[2]; /* | ||
54 | Previous 2 samples of the quantized difference | ||
55 | signal represented in an internal floating point | ||
56 | format. | ||
57 | */ | ||
58 | char td; /* delayed tone detect, new in 1988 version */ | ||
59 | }; | ||
60 | |||
61 | |||
62 | /* | ||
63 | Forward declaration of functions | ||
64 | */ | ||
65 | |||
66 | int g723_enc_abs( int num ); | ||
67 | void g723_enc_init_state( struct g723_enc_state_t *state_ptr ); | ||
68 | int g723_enc_predictor_zero( struct g723_enc_state_t *state_ptr ); | ||
69 | int g723_enc_fmult( int an, int srn ); | ||
70 | int g723_enc_predictor_pole( struct g723_enc_state_t *state_ptr ); | ||
71 | int g723_enc_step_size( struct g723_enc_state_t *state_ptr ); | ||
72 | int g723_enc_quantize( | ||
73 | int d, /* Raw difference signal sample */ | ||
74 | int y, /* Step size multiplier */ | ||
75 | short *table, /* quantization table */ | ||
76 | int size ); /* table size of short integers */ | ||
77 | int g723_enc_reconstruct( | ||
78 | int sign, /* 0 for non-negative value */ | ||
79 | int dqln, /* G.72x codeword */ | ||
80 | int y ); /* Step size multiplier */ | ||
81 | void g723_enc_update( | ||
82 | int code_size, /* distinguish 723_40 with others */ | ||
83 | int y, /* quantizer step size */ | ||
84 | int wi, /* scale factor multiplier */ | ||
85 | int fi, /* for long/short term energies */ | ||
86 | int dq, /* quantized prediction difference */ | ||
87 | int sr, /* reconstructed signal */ | ||
88 | int dqsez, /* difference from 2-pole predictor */ | ||
89 | struct g723_enc_state_t *state_ptr ); /* coder state pointer */ | ||
90 | int g723_enc_quan( | ||
91 | int val, | ||
92 | short *table, | ||
93 | int size ); | ||
94 | int g723_enc_search( | ||
95 | int val, | ||
96 | short *table, | ||
97 | int size ); | ||
98 | int g723_enc_alaw2linear( unsigned char a_val ); | ||
99 | int g723_enc_ulaw2linear( unsigned char u_val ); | ||
100 | int g723_enc_g723_24_encoder( | ||
101 | int sample, | ||
102 | int in_coding, | ||
103 | struct g723_enc_state_t *state_ptr ); | ||
104 | int g723_enc_pack_output( | ||
105 | unsigned char code, | ||
106 | int bits ); | ||
107 | |||
108 | void g723_enc_init(); | ||
109 | int g723_enc_return(); | ||
110 | void g723_enc_main(); | ||
111 | //int main( void ); | ||
112 | |||
113 | /* | ||
114 | Declaration of global variables | ||
115 | */ | ||
116 | |||
117 | struct g723_enc_state_t g723_enc_state; | ||
118 | |||
119 | unsigned int g723_enc_INPUT[256] = { | ||
120 | 51, 17, 31, 53, 95, 17, 70, 22, 49, 12, 8, 39, 28, 37, 99, 54, | ||
121 | 77, 65, 77, 78, 83, 15, 63, 31, 35, 92, 52, 40, 61, 79, 94, 87, | ||
122 | 87, 68, 76, 58, 39, 35, 20, 83, 42, 46, 98, 12, 21, 96, 74, 41, | ||
123 | 78, 76, 96, 2, 32, 76, 24, 59, 4, 96, 32, 5, 44, 92, 57, 12, | ||
124 | 57, 25, 50, 23, 48, 41, 88, 43, 36, 38, 4, 16, 52, 70, 9, 40, | ||
125 | 78, 24, 34, 23, 30, 30, 89, 3, 65, 40, 68, 73, 94, 23, 84, 97, | ||
126 | 78, 43, 68, 81, 16, 28, 13, 87, 75, 21, 14, 29, 81, 22, 56, 72, | ||
127 | 19, 99, 25, 43, 76, 86, 90, 98, 39, 43, 12, 46, 24, 99, 65, 61, | ||
128 | 24, 45, 79, 7, 48, 15, 24, 95, 62, 99, 48, 80, 75, 38, 48, 53, | ||
129 | 9, 60, 35, 14, 78, 71, 45, 71, 9, 97, 55, 74, 58, 64, 78, 18, | ||
130 | 30, 28, 69, 29, 57, 42, 30, 44, 57, 49, 61, 42, 13, 25, 3, 98, | ||
131 | 11, 38, 65, 35, 55, 36, 57, 48, 16, 62, 17, 56, 29, 88, 84, 85, | ||
132 | 90, 60, 54, 16, 66, 69, 26, 10, 82, 19, 42, 35, 84, 13, 26, 17, | ||
133 | 48, 38, 50, 50, 35, 53, 12, 52, 61, 74, 56, 34, 80, 59, 26, 67, | ||
134 | 55, 79, 89, 89, 6, 80, 91, 65, 16, 30, 16, 28, 85, 54, 3, 20, | ||
135 | 2, 36, 62, 52, 55, 15, 83, 3, 2, 38, 62, 2, 63, 92, 37, 73 | ||
136 | }; | ||
137 | |||
138 | |||
139 | |||
140 | unsigned int g723_enc_OUTPUT[256]; | ||
141 | |||
142 | short g723_enc_power2[15] = {1, 2, 4, 8, 0x10, 0x20, 0x40, 0x80, | ||
143 | 0x100, 0x200, 0x400, 0x800, 0x1000, 0x2000, 0x4000 | ||
144 | }; | ||
145 | |||
146 | |||
147 | /* | ||
148 | Maps G.723_24 code word to reconstructed scale factor normalized log | ||
149 | magnitude values. | ||
150 | */ | ||
151 | |||
152 | short g723_enc_qtab_723_24[3] = {8, 218, 331}; | ||
153 | |||
154 | /* | ||
155 | Maps G.721 code word to reconstructed scale factor normalized log | ||
156 | magnitude values. | ||
157 | */ | ||
158 | short g723_enc_dqlntab[16] = { -2048, 4, 135, 213, 273, 323, 373, 425, | ||
159 | 425, 373, 323, 273, 213, 135, 4, -2048 | ||
160 | }; | ||
161 | |||
162 | /* Maps G.721 code word to log of scale factor multiplier. */ | ||
163 | short g723_enc_witab[16] = { -12, 18, 41, 64, 112, 198, 355, 1122, | ||
164 | 1122, 355, 198, 112, 64, 41, 18, -12 | ||
165 | }; | ||
166 | /* | ||
167 | Maps G.721 code words to a set of values whose long and short | ||
168 | term averages are computed and then compared to give an indication | ||
169 | how stationary (steady state) the signal is. | ||
170 | */ | ||
171 | short g723_enc_fitab[16] = {0, 0, 0, 0x200, 0x200, 0x200, 0x600, 0xE00, | ||
172 | 0xE00, 0x600, 0x200, 0x200, 0x200, 0, 0, 0 | ||
173 | }; | ||
174 | |||
175 | |||
176 | /* | ||
177 | Declaration of macros | ||
178 | */ | ||
179 | |||
180 | |||
181 | #define AUDIO_ENCODING_ULAW (1) /* ISDN u-law */ | ||
182 | #define AUDIO_ENCODING_ALAW (2) /* ISDN A-law */ | ||
183 | #define AUDIO_ENCODING_LINEAR (3) /* PCM 2's-complement (0-center) */ | ||
184 | |||
185 | #define BIAS (0x84) /* Bias for linear code. */ | ||
186 | |||
187 | #define SIGN_BIT (0x80) /* Sign bit for a A-law byte. */ | ||
188 | #define QUANT_MASK (0xf) /* Quantization field mask. */ | ||
189 | #define SEG_SHIFT (4) /* Left shift for segment number. */ | ||
190 | #define SEG_MASK (0x70) /* Segment field mask. */ | ||
191 | |||
192 | /* | ||
193 | Arithmetic math functions | ||
194 | */ | ||
195 | |||
196 | /* | ||
197 | g723_enc_fmult() | ||
198 | |||
199 | returns the integer product of the 14-bit integer "an" and | ||
200 | "floating point" representation (4-bit exponent, 6-bit mantessa) "srn". | ||
201 | */ | ||
202 | int g723_enc_fmult( | ||
203 | int an, | ||
204 | int srn ) | ||
205 | { | ||
206 | short anmag, anexp, anmant; | ||
207 | short wanexp, wanmant; | ||
208 | short retval; | ||
209 | |||
210 | anmag = ( an > 0 ) ? an : ( ( -an ) & 0x1FFF ); | ||
211 | anexp = g723_enc_quan( anmag, g723_enc_power2, 3 ) - 6; | ||
212 | anmant = ( anmag == 0 ) ? 32 : | ||
213 | ( anexp >= 0 ) ? anmag >> anexp : anmag << -anexp; | ||
214 | wanexp = anexp + ( ( srn >> 6 ) & 0xF ) - 13; | ||
215 | |||
216 | wanmant = ( anmant * ( srn & 077 ) + 0x30 ) >> 4; | ||
217 | retval = ( wanexp >= 0 ) ? ( ( wanmant << wanexp ) & 0x7FFF ) : | ||
218 | ( wanmant >> -wanexp ); | ||
219 | |||
220 | return ( ( ( an ^ srn ) < 0 ) ? -retval : retval ); | ||
221 | } | ||
222 | |||
223 | |||
224 | /* Manish Verma */ | ||
225 | int g723_enc_abs( int num ) | ||
226 | { | ||
227 | return ( num < 0 ) ? -num : num; | ||
228 | } | ||
229 | |||
230 | |||
231 | /* | ||
232 | Algorithm core functions | ||
233 | */ | ||
234 | |||
235 | |||
236 | /* | ||
237 | g723_enc_quan() | ||
238 | |||
239 | quantizes the input val against the table of size short integers. | ||
240 | It returns i if table[i - 1] <= val < table[i]. | ||
241 | |||
242 | Using linear search for simple coding. | ||
243 | */ | ||
244 | int g723_enc_quan( | ||
245 | int val, | ||
246 | short *table, | ||
247 | int size ) | ||
248 | { | ||
249 | int i, | ||
250 | j = 0, | ||
251 | k = 1; | ||
252 | |||
253 | _Pragma( "loopbound min 3 max 15" ) | ||
254 | for ( i = 0; i < size; ++i ) { | ||
255 | |||
256 | if ( k ) { | ||
257 | if ( val < *table++ ) { | ||
258 | j = i; | ||
259 | k = 0; | ||
260 | } | ||
261 | } | ||
262 | } | ||
263 | |||
264 | return ( j ); | ||
265 | } | ||
266 | |||
267 | |||
268 | /* | ||
269 | g723_enc_predictor_zero() | ||
270 | |||
271 | computes the estimated signal from 6-zero predictor. | ||
272 | |||
273 | */ | ||
274 | int | ||
275 | g723_enc_predictor_zero( | ||
276 | struct g723_enc_state_t *state_ptr ) | ||
277 | { | ||
278 | int i; | ||
279 | int sezi; | ||
280 | |||
281 | sezi = g723_enc_fmult( state_ptr->b[0] >> 2, state_ptr->dq[0] ); | ||
282 | _Pragma( "loopbound min 5 max 5" ) | ||
283 | for ( i = 1; i < 6; i++ ) /* ACCUM */ | ||
284 | sezi += g723_enc_fmult( state_ptr->b[i] >> 2, state_ptr->dq[i] ); | ||
285 | |||
286 | return ( sezi ); | ||
287 | } | ||
288 | |||
289 | |||
290 | /* | ||
291 | g723_enc_predictor_pole() | ||
292 | |||
293 | computes the estimated signal from 2-pole predictor. | ||
294 | |||
295 | */ | ||
296 | int | ||
297 | g723_enc_predictor_pole( | ||
298 | struct g723_enc_state_t *state_ptr ) | ||
299 | { | ||
300 | return ( g723_enc_fmult( state_ptr->a[1] >> 2, state_ptr->sr[1] ) + | ||
301 | g723_enc_fmult( state_ptr->a[0] >> 2, state_ptr->sr[0] ) ); | ||
302 | } | ||
303 | |||
304 | /* | ||
305 | g723_enc_step_size() | ||
306 | |||
307 | computes the quantization step size of the adaptive quantizer. | ||
308 | |||
309 | */ | ||
310 | int | ||
311 | g723_enc_step_size( | ||
312 | struct g723_enc_state_t *state_ptr ) | ||
313 | { | ||
314 | int y; | ||
315 | int dif; | ||
316 | int al; | ||
317 | |||
318 | if ( state_ptr->ap >= 256 ) | ||
319 | return ( state_ptr->yu ); | ||
320 | else { | ||
321 | y = state_ptr->yl >> 6; | ||
322 | dif = state_ptr->yu - y; | ||
323 | al = state_ptr->ap >> 2; | ||
324 | if ( dif > 0 ) | ||
325 | y += ( dif * al ) >> 6; | ||
326 | else | ||
327 | if ( dif < 0 ) | ||
328 | y += ( dif * al + 0x3F ) >> 6; | ||
329 | |||
330 | return ( y ); | ||
331 | } | ||
332 | } | ||
333 | |||
334 | /* | ||
335 | g723_enc_quantize() | ||
336 | |||
337 | Given a raw sample, 'd', of the difference signal and a | ||
338 | quantization step size scale factor, 'y', this routine returns the | ||
339 | ADPCM codeword to which that sample gets quantized. The step | ||
340 | size scale factor division operation is done in the log base 2 domain | ||
341 | as a subtraction. | ||
342 | */ | ||
343 | int | ||
344 | g723_enc_quantize( | ||
345 | int d, /* Raw difference signal sample */ | ||
346 | int y, /* Step size multiplier */ | ||
347 | short *table, /* quantization table */ | ||
348 | int size ) /* table size of short integers */ | ||
349 | { | ||
350 | short dqm; /* Magnitude of 'd' */ | ||
351 | short exp; /* Integer part of base 2 log of 'd' */ | ||
352 | short mant; /* Fractional part of base 2 log */ | ||
353 | short dl; /* Log of magnitude of 'd' */ | ||
354 | short dln; /* Step size scale factor normalized log */ | ||
355 | int i; | ||
356 | |||
357 | /* | ||
358 | LOG | ||
359 | |||
360 | Compute base 2 log of 'd', and store in 'dl'. | ||
361 | */ | ||
362 | dqm = g723_enc_abs( d ); | ||
363 | exp = g723_enc_quan( dqm >> 1, g723_enc_power2, 15 ); | ||
364 | mant = ( ( dqm << 7 ) >> exp ) & 0x7F; /* Fractional portion. */ | ||
365 | dl = ( exp << 7 ) + mant; | ||
366 | |||
367 | /* | ||
368 | SUBTB | ||
369 | |||
370 | "Divide" by step size multiplier. | ||
371 | */ | ||
372 | dln = dl - ( y >> 2 ); | ||
373 | |||
374 | /* | ||
375 | QUAN | ||
376 | |||
377 | Obtain codword i for 'd'. | ||
378 | */ | ||
379 | i = g723_enc_quan( dln, table, size ); | ||
380 | |||
381 | if ( d < 0 ) /* take 1's complement of i */ | ||
382 | return ( ( size << 1 ) + 1 - i ); | ||
383 | else | ||
384 | if ( i == 0 ) /* take 1's complement of 0 */ | ||
385 | return ( ( size << 1 ) + 1 ); /* new in 1988 */ | ||
386 | else | ||
387 | return ( i ); | ||
388 | } | ||
389 | /* | ||
390 | g723_enc_reconstruct() | ||
391 | |||
392 | Returns reconstructed difference signal 'dq' obtained from | ||
393 | codeword 'i' and quantization step size scale factor 'y'. | ||
394 | Multiplication is performed in log base 2 domain as addition. | ||
395 | */ | ||
396 | int | ||
397 | g723_enc_reconstruct( | ||
398 | int sign, /* 0 for non-negative value */ | ||
399 | int dqln, /* G.72x codeword */ | ||
400 | int y ) /* Step size multiplier */ | ||
401 | { | ||
402 | short dql; /* Log of 'dq' magnitude */ | ||
403 | short dex; /* Integer part of log */ | ||
404 | short dqt; | ||
405 | short dq; /* Reconstructed difference signal sample */ | ||
406 | |||
407 | dql = dqln + ( y >> 2 ); /* ADDA */ | ||
408 | |||
409 | if ( dql < 0 ) | ||
410 | return ( ( sign ) ? -0x8000 : 0 ); | ||
411 | else { /* ANTILOG */ | ||
412 | dex = ( dql >> 7 ) & 15; | ||
413 | dqt = 128 + ( dql & 127 ); | ||
414 | dq = ( dqt << 7 ) >> ( 14 - dex ); | ||
415 | return ( ( sign ) ? ( dq - 0x8000 ) : dq ); | ||
416 | } | ||
417 | } | ||
418 | |||
419 | |||
420 | /* | ||
421 | g723_enc_update() | ||
422 | |||
423 | updates the state variables for each output code | ||
424 | */ | ||
425 | void | ||
426 | g723_enc_update( | ||
427 | int code_size, /* distinguish 723_40 with others */ | ||
428 | int y, /* quantizer step size */ | ||
429 | int wi, /* scale factor multiplier */ | ||
430 | int fi, /* for long/short term energies */ | ||
431 | int dq, /* quantized prediction difference */ | ||
432 | int sr, /* reconstructed signal */ | ||
433 | int dqsez, /* difference from 2-pole predictor */ | ||
434 | struct g723_enc_state_t *state_ptr ) /* coder state pointer */ | ||
435 | { | ||
436 | int cnt; | ||
437 | short mag, exp; /* Adaptive predictor, FLOAT A */ | ||
438 | short a2p; /* LIMC */ | ||
439 | short a1ul; /* UPA1 */ | ||
440 | short pks1; /* UPA2 */ | ||
441 | short fa1; | ||
442 | char tr; /* tone/transition detector */ | ||
443 | short ylint, thr2, dqthr; | ||
444 | short ylfrac, thr1; | ||
445 | short pk0; | ||
446 | |||
447 | pk0 = ( dqsez < 0 ) ? 1 : 0; /* needed in updating predictor poles */ | ||
448 | |||
449 | mag = dq & 0x7FFF; /* prediction difference magnitude */ | ||
450 | /* TRANS */ | ||
451 | ylint = state_ptr->yl >> 15; /* exponent part of yl */ | ||
452 | ylfrac = ( state_ptr->yl >> 10 ) & 0x1F; /* fractional part of yl */ | ||
453 | thr1 = ( 32 + ylfrac ) << ylint; /* threshold */ | ||
454 | thr2 = ( ylint > 9 ) ? 31 << 10 : thr1; /* limit thr2 to 31 << 10 */ | ||
455 | dqthr = ( thr2 + ( thr2 >> 1 ) ) >> 1; /* dqthr = 0.75 * thr2 */ | ||
456 | if ( state_ptr->td == 0 ) /* signal supposed voice */ | ||
457 | tr = 0; | ||
458 | else | ||
459 | if ( mag <= dqthr ) /* supposed data, but small mag */ | ||
460 | tr = 0; /* treated as voice */ | ||
461 | else /* signal is data (modem) */ | ||
462 | tr = 1; | ||
463 | |||
464 | /* | ||
465 | Quantizer scale factor adaptation. | ||
466 | */ | ||
467 | |||
468 | /* FUNCTW & FILTD & DELAY */ | ||
469 | /* update non-steady state step size multiplier */ | ||
470 | state_ptr->yu = y + ( ( wi - y ) >> 5 ); | ||
471 | |||
472 | /* LIMB */ | ||
473 | if ( state_ptr->yu < 544 ) /* 544 <= yu <= 5120 */ | ||
474 | state_ptr->yu = 544; | ||
475 | else | ||
476 | if ( state_ptr->yu > 5120 ) | ||
477 | state_ptr->yu = 5120; | ||
478 | |||
479 | /* FILTE & DELAY */ | ||
480 | /* update steady state step size multiplier */ | ||
481 | state_ptr->yl += state_ptr->yu + ( ( -state_ptr->yl ) >> 6 ); | ||
482 | |||
483 | /* | ||
484 | Adaptive predictor coefficients. | ||
485 | */ | ||
486 | if ( tr == 1 ) { /* reset a's and b's for modem signal */ | ||
487 | state_ptr->a[0] = 0; | ||
488 | state_ptr->a[1] = 0; | ||
489 | state_ptr->b[0] = 0; | ||
490 | state_ptr->b[1] = 0; | ||
491 | state_ptr->b[2] = 0; | ||
492 | state_ptr->b[3] = 0; | ||
493 | state_ptr->b[4] = 0; | ||
494 | state_ptr->b[5] = 0; | ||
495 | } else { /* update a's and b's */ | ||
496 | pks1 = pk0 ^ state_ptr->pk[0]; /* UPA2 */ | ||
497 | |||
498 | /* update predictor pole a[1] */ | ||
499 | a2p = state_ptr->a[1] - ( state_ptr->a[1] >> 7 ); | ||
500 | if ( dqsez != 0 ) { | ||
501 | fa1 = ( pks1 ) ? state_ptr->a[0] : -state_ptr->a[0]; | ||
502 | if ( fa1 < -8191 ) /* a2p = function of fa1 */ | ||
503 | a2p -= 0x100; | ||
504 | else | ||
505 | if ( fa1 > 8191 ) | ||
506 | a2p += 0xFF; | ||
507 | else | ||
508 | a2p += fa1 >> 5; | ||
509 | if ( pk0 ^ state_ptr->pk[1] ) | ||
510 | /* LIMC */ | ||
511 | if ( a2p <= -12160 ) | ||
512 | a2p = -12288; | ||
513 | else | ||
514 | if ( a2p >= 12416 ) | ||
515 | a2p = 12288; | ||
516 | else | ||
517 | a2p -= 0x80; | ||
518 | else | ||
519 | if ( a2p <= -12416 ) | ||
520 | a2p = -12288; | ||
521 | else | ||
522 | if ( a2p >= 12160 ) | ||
523 | a2p = 12288; | ||
524 | else | ||
525 | a2p += 0x80; | ||
526 | |||
527 | } | ||
528 | |||
529 | /* TRIGB & DELAY */ | ||
530 | state_ptr->a[1] = a2p; | ||
531 | |||
532 | /* UPA1 */ | ||
533 | /* update predictor pole a[0] */ | ||
534 | state_ptr->a[0] -= state_ptr->a[0] >> 8; | ||
535 | if ( dqsez != 0 ) { | ||
536 | if ( pks1 == 0 ) | ||
537 | state_ptr->a[0] += 192; | ||
538 | else | ||
539 | state_ptr->a[0] -= 192; | ||
540 | } | ||
541 | |||
542 | /* LIMD */ | ||
543 | a1ul = 15360 - a2p; | ||
544 | if ( state_ptr->a[0] < -a1ul ) | ||
545 | state_ptr->a[0] = -a1ul; | ||
546 | else | ||
547 | if ( state_ptr->a[0] > a1ul ) | ||
548 | state_ptr->a[0] = a1ul; | ||
549 | |||
550 | /* UPB : update predictor zeros b[6] */ | ||
551 | _Pragma( "loopbound min 6 max 6" ) | ||
552 | for ( cnt = 0; cnt < 6; cnt++ ) { | ||
553 | if ( code_size == 5 ) /* for 40Kbps G.723 */ | ||
554 | state_ptr->b[cnt] -= state_ptr->b[cnt] >> 9; | ||
555 | else /* for G.721 and 24Kbps G.723 */ | ||
556 | state_ptr->b[cnt] -= state_ptr->b[cnt] >> 8; | ||
557 | if ( dq & 0x7FFF ) { /* XOR */ | ||
558 | if ( ( dq ^ state_ptr->dq[cnt] ) >= 0 ) | ||
559 | state_ptr->b[cnt] += 128; | ||
560 | else | ||
561 | state_ptr->b[cnt] -= 128; | ||
562 | } | ||
563 | |||
564 | } | ||
565 | } | ||
566 | |||
567 | _Pragma( "loopbound min 5 max 5" ) | ||
568 | for ( cnt = 5; cnt > 0; cnt-- ) | ||
569 | state_ptr->dq[cnt] = state_ptr->dq[cnt - 1]; | ||
570 | /* FLOAT A : convert dq[0] to 4-bit exp, 6-bit mantissa f.p. */ | ||
571 | if ( mag == 0 ) | ||
572 | state_ptr->dq[0] = ( dq >= 0 ) ? 0x20 : 0xFC20; | ||
573 | else { | ||
574 | exp = g723_enc_quan( mag, g723_enc_power2, 15 ); | ||
575 | state_ptr->dq[0] = ( dq >= 0 ) ? | ||
576 | ( exp << 6 ) + ( ( mag << 6 ) >> exp ) : | ||
577 | ( exp << 6 ) + ( ( mag << 6 ) >> exp ) - 0x400; | ||
578 | |||
579 | } | ||
580 | |||
581 | state_ptr->sr[1] = state_ptr->sr[0]; | ||
582 | /* FLOAT B : convert sr to 4-bit exp., 6-bit mantissa f.p. */ | ||
583 | if ( sr == 0 ) | ||
584 | state_ptr->sr[0] = 0x20; | ||
585 | else | ||
586 | if ( sr > 0 ) { | ||
587 | exp = g723_enc_quan( sr, g723_enc_power2, 15 ); | ||
588 | state_ptr->sr[0] = ( exp << 6 ) + ( ( sr << 6 ) >> exp ); | ||
589 | } else | ||
590 | if ( sr > -32768 ) { | ||
591 | mag = -sr; | ||
592 | exp = g723_enc_quan( mag, g723_enc_power2, 15 ); | ||
593 | state_ptr->sr[0] = ( exp << 6 ) + ( ( mag << 6 ) >> exp ) - 0x400; | ||
594 | } else | ||
595 | state_ptr->sr[0] = 0xFC20; | ||
596 | |||
597 | /* DELAY A */ | ||
598 | state_ptr->pk[1] = state_ptr->pk[0]; | ||
599 | state_ptr->pk[0] = pk0; | ||
600 | |||
601 | /* TONE */ | ||
602 | if ( tr == 1 ) /* this sample has been treated as data */ | ||
603 | state_ptr->td = 0; /* next one will be treated as voice */ | ||
604 | else | ||
605 | if ( a2p < -11776 ) /* small sample-to-sample correlation */ | ||
606 | state_ptr->td = 1; /* signal may be data */ | ||
607 | else /* signal is voice */ | ||
608 | state_ptr->td = 0; | ||
609 | |||
610 | /* | ||
611 | Adaptation speed control. | ||
612 | */ | ||
613 | state_ptr->dms += ( fi - state_ptr->dms ) >> 5; /* FILTA */ | ||
614 | state_ptr->dml += ( ( ( fi << 2 ) - state_ptr->dml ) >> 7 ); /* FILTB */ | ||
615 | |||
616 | if ( tr == 1 ) | ||
617 | state_ptr->ap = 256; | ||
618 | else | ||
619 | if ( y < 1536 ) /* SUBTC */ | ||
620 | state_ptr->ap += ( 0x200 - state_ptr->ap ) >> 4; | ||
621 | else | ||
622 | if ( state_ptr->td == 1 ) | ||
623 | state_ptr->ap += ( 0x200 - state_ptr->ap ) >> 4; | ||
624 | else | ||
625 | if ( g723_enc_abs( ( state_ptr->dms << 2 ) - state_ptr->dml ) >= | ||
626 | ( state_ptr->dml >> 3 ) ) | ||
627 | state_ptr->ap += ( 0x200 - state_ptr->ap ) >> 4; | ||
628 | else | ||
629 | state_ptr->ap += ( -state_ptr->ap ) >> 4; | ||
630 | |||
631 | } | ||
632 | |||
633 | |||
634 | /* | ||
635 | g723_enc_alaw2linear() - Convert an A-law value to 16-bit linear PCM | ||
636 | |||
637 | */ | ||
638 | int | ||
639 | g723_enc_alaw2linear( | ||
640 | unsigned char a_val ) | ||
641 | { | ||
642 | int t; | ||
643 | int seg; | ||
644 | |||
645 | a_val ^= 0x55; | ||
646 | |||
647 | t = ( a_val & QUANT_MASK ) << 4; | ||
648 | seg = ( ( unsigned )a_val & SEG_MASK ) >> SEG_SHIFT; | ||
649 | switch ( seg ) { | ||
650 | case 0: | ||
651 | t += 8; | ||
652 | break; | ||
653 | case 1: | ||
654 | t += 0x108; | ||
655 | break; | ||
656 | default: | ||
657 | t += 0x108; | ||
658 | t <<= seg - 1; | ||
659 | } | ||
660 | return ( ( a_val & SIGN_BIT ) ? t : -t ); | ||
661 | } | ||
662 | |||
663 | |||
664 | /* | ||
665 | g723_enc_ulaw2linear() - Convert a u-law value to 16-bit linear PCM | ||
666 | |||
667 | First, a biased linear code is derived from the code word. An unbiased | ||
668 | output can then be obtained by subtracting 33 from the biased code. | ||
669 | |||
670 | Note that this function expects to be passed the complement of the | ||
671 | original code word. This is in keeping with ISDN conventions. | ||
672 | */ | ||
673 | int | ||
674 | g723_enc_ulaw2linear( | ||
675 | unsigned char u_val ) | ||
676 | { | ||
677 | int t; | ||
678 | |||
679 | /* Complement to obtain normal u-law value. */ | ||
680 | u_val = ~u_val; | ||
681 | |||
682 | /* | ||
683 | Extract and bias the quantization bits. Then | ||
684 | shift up by the segment number and subtract out the bias. | ||
685 | */ | ||
686 | t = ( ( u_val & QUANT_MASK ) << 3 ) + BIAS; | ||
687 | t <<= ( ( unsigned int )u_val & SEG_MASK ) >> SEG_SHIFT; | ||
688 | |||
689 | return ( ( u_val & SIGN_BIT ) ? ( BIAS - t ) : ( t - BIAS ) ); | ||
690 | } | ||
691 | |||
692 | |||
693 | /* | ||
694 | g723_enc_g723_24_encoder() | ||
695 | |||
696 | Encodes a linear PCM, A-law or u-law input sample and returns its 3-bit code. | ||
697 | Returns -1 if invalid input coding value. | ||
698 | */ | ||
699 | int | ||
700 | g723_enc_g723_24_encoder( | ||
701 | int sl, | ||
702 | int in_coding, | ||
703 | struct g723_enc_state_t *state_ptr ) | ||
704 | { | ||
705 | short sei, sezi, se, sez; /* ACCUM */ | ||
706 | short d; /* SUBTA */ | ||
707 | short y; /* MIX */ | ||
708 | short sr; /* ADDB */ | ||
709 | short dqsez; /* ADDC */ | ||
710 | short dq, i; | ||
711 | |||
712 | switch ( in_coding ) { /* linearize input sample to 14-bit PCM */ | ||
713 | case AUDIO_ENCODING_ALAW: | ||
714 | sl = g723_enc_alaw2linear( sl ) >> 2; | ||
715 | break; | ||
716 | case AUDIO_ENCODING_ULAW: | ||
717 | sl = g723_enc_ulaw2linear( sl ) >> 2; | ||
718 | break; | ||
719 | case AUDIO_ENCODING_LINEAR: | ||
720 | sl >>= 2; /* sl of 14-bit dynamic range */ | ||
721 | break; | ||
722 | default: | ||
723 | return ( -1 ); | ||
724 | } | ||
725 | |||
726 | sezi = g723_enc_predictor_zero( state_ptr ); | ||
727 | sez = sezi >> 1; | ||
728 | sei = sezi + g723_enc_predictor_pole( state_ptr ); | ||
729 | se = sei >> 1; /* se = estimated signal */ | ||
730 | |||
731 | d = sl - se; /* d = estimation diff. */ | ||
732 | |||
733 | /* quantize prediction difference d */ | ||
734 | y = g723_enc_step_size( state_ptr ); /* quantizer step size */ | ||
735 | i = g723_enc_quantize( d, y, g723_enc_qtab_723_24, 3 ); /* i = ADPCM code */ | ||
736 | dq = g723_enc_reconstruct( i & 4, g723_enc_dqlntab[i], y ); /* quantized diff. */ | ||
737 | |||
738 | sr = ( dq < 0 ) ? se - ( dq & 0x3FFF ) : se + dq; /* reconstructed signal */ | ||
739 | |||
740 | dqsez = sr + sez - se; /* pole prediction diff. */ | ||
741 | |||
742 | g723_enc_update( 3, y, g723_enc_witab[i], g723_enc_fitab[i], dq, sr, dqsez, state_ptr ); | ||
743 | |||
744 | return ( i ); | ||
745 | } | ||
746 | |||
747 | /* | ||
748 | Pack output codes into bytes and write them to stdout. | ||
749 | Returns 1 if there is residual output, else returns 0. | ||
750 | */ | ||
751 | int | ||
752 | g723_enc_pack_output( | ||
753 | unsigned char code, | ||
754 | int bits ) | ||
755 | { | ||
756 | static unsigned int out_buffer = 0; | ||
757 | static int out_bits = 0; | ||
758 | unsigned char out_byte; | ||
759 | static int i = 0; | ||
760 | |||
761 | out_buffer |= ( code << out_bits ); | ||
762 | out_bits += bits; | ||
763 | if ( out_bits >= 8 ) { | ||
764 | out_byte = out_buffer & 0xff; | ||
765 | out_bits -= 8; | ||
766 | out_buffer >>= 8; | ||
767 | //fwrite(&out_byte, sizeof (char), 1, fp_out); | ||
768 | //fwrite(&out_byte, 1, 1, fp_out); | ||
769 | g723_enc_OUTPUT[i] = out_byte; | ||
770 | i = i + 1; | ||
771 | } | ||
772 | |||
773 | return ( out_bits > 0 ); | ||
774 | } | ||
775 | |||
776 | /* | ||
777 | Initialization- and return-value-related functions | ||
778 | */ | ||
779 | |||
780 | /* | ||
781 | g723_enc_init_state() | ||
782 | |||
783 | This routine initializes and/or resets the g72x_state structure | ||
784 | pointed to by 'state_ptr'. | ||
785 | All the initial state values are specified in the CCITT G.721 document. | ||
786 | */ | ||
787 | void | ||
788 | g723_enc_init_state( | ||
789 | struct g723_enc_state_t *state_ptr ) | ||
790 | { | ||
791 | int cnta; | ||
792 | |||
793 | state_ptr->yl = 34816; | ||
794 | state_ptr->yu = 544; | ||
795 | state_ptr->dms = 0; | ||
796 | state_ptr->dml = 0; | ||
797 | state_ptr->ap = 0; | ||
798 | |||
799 | _Pragma( "loopbound min 2 max 2" ) | ||
800 | for ( cnta = 0; cnta < 2; cnta++ ) { | ||
801 | state_ptr->a[cnta] = 0; | ||
802 | state_ptr->pk[cnta] = 0; | ||
803 | state_ptr->sr[cnta] = 32; | ||
804 | } | ||
805 | _Pragma( "loopbound min 6 max 6" ) | ||
806 | for ( cnta = 0; cnta < 6; cnta++ ) { | ||
807 | state_ptr->b[cnta] = 0; | ||
808 | state_ptr->dq[cnta] = 32; | ||
809 | } | ||
810 | state_ptr->td = 0; | ||
811 | } | ||
812 | |||
813 | |||
814 | void g723_enc_init() | ||
815 | { | ||
816 | int i; | ||
817 | volatile int x = 0; | ||
818 | g723_enc_init_state( &g723_enc_state ); | ||
819 | |||
820 | _Pragma( "loopbound min 256 max 256" ) | ||
821 | for ( i = 0; i < 256; i++ ) { | ||
822 | g723_enc_INPUT[i] += x; | ||
823 | } | ||
824 | } | ||
825 | |||
826 | |||
827 | int g723_enc_return() | ||
828 | { | ||
829 | int i; | ||
830 | int check_sum = 0; | ||
831 | |||
832 | _Pragma( "loopbound min 256 max 256" ) | ||
833 | for ( i = 0; i < 256; i++ ) { | ||
834 | check_sum += g723_enc_OUTPUT[i]; | ||
835 | } | ||
836 | |||
837 | return ( check_sum != 24284 ); | ||
838 | } | ||
839 | |||
840 | /* | ||
841 | Main functions | ||
842 | */ | ||
843 | |||
844 | void _Pragma( "entrypoint" ) g723_enc_main() | ||
845 | { | ||
846 | // struct g72x_state state; | ||
847 | short sample_short; //mv | ||
848 | unsigned char code; | ||
849 | int resid; | ||
850 | int in_coding; | ||
851 | short *in_buf; | ||
852 | int enc_bits; | ||
853 | int i = 0; | ||
854 | |||
855 | enc_bits = 3; | ||
856 | in_coding = AUDIO_ENCODING_ALAW; | ||
857 | in_buf = &sample_short; | ||
858 | |||
859 | _Pragma( "loopbound min 256 max 256" ) | ||
860 | for ( i = 0; i < 256; i++ ) { | ||
861 | *in_buf = g723_enc_INPUT[i]; | ||
862 | code = g723_enc_g723_24_encoder( sample_short, in_coding, &g723_enc_state ); | ||
863 | resid = g723_enc_pack_output( code, enc_bits ); | ||
864 | } | ||
865 | |||
866 | /* Write zero codes until all residual codes are written out */ | ||
867 | _Pragma( "loopbound min 0 max 0" ) | ||
868 | while ( resid ) | ||
869 | resid = g723_enc_pack_output( 0, enc_bits ); | ||
870 | } | ||
871 | |||
872 | |||
873 | int main( int argc, char **argv ) | ||
874 | { | ||
875 | //SET_UP | ||
876 | int jobsComplete; | ||
877 | int maxJobs=9; | ||
878 | for (jobsComplete=-1; jobsComplete<maxJobs; jobsComplete++){ | ||
879 | // START_LOOP | ||
880 | g723_enc_init(); | ||
881 | g723_enc_main(); | ||
882 | // STOP_LOOP | ||
883 | } | ||
884 | //WRITE_TO_FILE | ||
885 | return ( g723_enc_return() ); | ||
886 | } | ||
887 | |||
diff --git a/all_pairs/source/g723_enc/license.txt b/all_pairs/source/g723_enc/license.txt new file mode 100644 index 0000000..80cf21f --- /dev/null +++ b/all_pairs/source/g723_enc/license.txt | |||
@@ -0,0 +1,23 @@ | |||
1 | This source code is a product of Sun Microsystems, Inc. and is provided | ||
2 | for unrestricted use. Users may copy or modify this source code without | ||
3 | charge. | ||
4 | |||
5 | SUN SOURCE CODE IS PROVIDED AS IS WITH NO WARRANTIES OF ANY KIND INCLUDING | ||
6 | THE WARRANTIES OF DESIGN, MERCHANTIBILITY AND FITNESS FOR A PARTICULAR | ||
7 | PURPOSE, OR ARISING FROM A COURSE OF DEALING, USAGE OR TRADE PRACTICE. | ||
8 | |||
9 | Sun source code is provided with no support and without any obligation on | ||
10 | the part of Sun Microsystems, Inc. to assist in its use, correction, | ||
11 | modification or enhancement. | ||
12 | |||
13 | SUN MICROSYSTEMS, INC. SHALL HAVE NO LIABILITY WITH RESPECT TO THE | ||
14 | INFRINGEMENT OF COPYRIGHTS, TRADE SECRETS OR ANY PATENTS BY THIS SOFTWARE | ||
15 | OR ANY PART THEREOF. | ||
16 | |||
17 | In no event will Sun Microsystems, Inc. be liable for any lost revenue | ||
18 | or profits or other special, indirect and consequential damages, even if | ||
19 | Sun has been advised of the possibility of such damages. | ||
20 | |||
21 | Sun Microsystems, Inc. | ||
22 | 2550 Garcia Avenue | ||
23 | Mountain View, California 94043 \ No newline at end of file | ||