└── G711_G721_G723 ├── encode.o ├── Makefile ├── decode.c ├── encode.c ├── README ├── g72x.h ├── g723_24.c ├── g721.c ├── g723_40.c ├── g711.c └── g72x.c /G711_G721_G723/encode.o: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/twstx1/codec-for-audio-in-G72X-G711-G723-G726-G729/HEAD/G711_G721_G723/encode.o -------------------------------------------------------------------------------- /G711_G721_G723/Makefile: -------------------------------------------------------------------------------- 1 | # Makefile for CCITT subroutines and sample programs 2 | # 3 | 4 | # Modify the CC definition to invoke your ANSI-C compiler 5 | # 6 | CC = gcc 7 | 8 | 9 | # Rules to build sample programs do not depend on a UNIX library program 10 | # 11 | ALL = encode decode 12 | all: $(ALL) 13 | 14 | OBJECTS = g711.o g72x.o g721.o g723_24.o g723_40.o 15 | 16 | encode: $(OBJECTS) 17 | $(CC) -o $@ encode.c $(OBJECTS) 18 | 19 | decode: $(OBJECTS) 20 | $(CC) -o $@ decode.c $(OBJECTS) 21 | 22 | 23 | # Library rules for UNIX systems 24 | # 25 | LIB = ccitt.a 26 | lib: $(LIB) 27 | 28 | $(LIB): $(OBJECTS) 29 | @rm -f $(LIB) 30 | ar cr $(LIB) $(OBJECTS) 31 | @ranlib $(LIB) 32 | 33 | clean: 34 | /bin/rm -f $(LIB) $(ALL) *.o 35 | -------------------------------------------------------------------------------- /G711_G721_G723/decode.c: -------------------------------------------------------------------------------- 1 | /* 2 | * decode.c 3 | * 4 | * CCITT ADPCM decoder 5 | * 6 | * Usage : decode [-3|4|5] [-a|u|l] < infile > outfile 7 | */ 8 | #include 9 | #include "g72x.h" 10 | 11 | 12 | /* 13 | * Unpack input codes and pass them back as bytes. 14 | * Returns 1 if there is residual input, returns -1 if eof, else returns 0. 15 | */ 16 | int 17 | unpack_input( 18 | unsigned char *code, 19 | int bits) 20 | { 21 | static unsigned int in_buffer = 0; 22 | static int in_bits = 0; 23 | unsigned char in_byte; 24 | 25 | if (in_bits < bits) { 26 | if (fread(&in_byte, sizeof (char), 1, stdin) != 1) { 27 | *code = 0; 28 | return (-1); 29 | } 30 | in_buffer |= (in_byte << in_bits); 31 | in_bits += 8; 32 | } 33 | *code = in_buffer & ((1 << bits) - 1); 34 | in_buffer >>= bits; 35 | in_bits -= bits; 36 | return (in_bits > 0); 37 | } 38 | 39 | 40 | main( 41 | int argc, 42 | char **argv) 43 | { 44 | short sample; 45 | unsigned char code; 46 | int n; 47 | struct g72x_state state; 48 | int out_coding; 49 | int out_size; 50 | int (*dec_routine)(); 51 | int dec_bits; 52 | 53 | g72x_init_state(&state); 54 | out_coding = AUDIO_ENCODING_ULAW; 55 | out_size = sizeof (char); 56 | dec_routine = g721_decoder; 57 | dec_bits = 4; 58 | 59 | /* Process encoding argument, if any */ 60 | while ((argc > 1) && (argv[1][0] == '-')) { 61 | switch (argv[1][1]) { 62 | case '3': 63 | dec_routine = g723_24_decoder; 64 | dec_bits = 3; 65 | break; 66 | case '4': 67 | dec_routine = g721_decoder; 68 | dec_bits = 4; 69 | break; 70 | case '5': 71 | dec_routine = g723_40_decoder; 72 | dec_bits = 5; 73 | break; 74 | case 'u': 75 | out_coding = AUDIO_ENCODING_ULAW; 76 | out_size = sizeof (char); 77 | break; 78 | case 'a': 79 | out_coding = AUDIO_ENCODING_ALAW; 80 | out_size = sizeof (char); 81 | break; 82 | case 'l': 83 | out_coding = AUDIO_ENCODING_LINEAR; 84 | out_size = sizeof (short); 85 | break; 86 | default: 87 | fprintf(stderr, "CCITT ADPCM Decoder -- usage:\n"); 88 | fprintf(stderr, "\tdecode [-3|4|5] [-a|u|l] < infile > outfile\n"); 89 | fprintf(stderr, "where:\n"); 90 | fprintf(stderr, "\t-3\tProcess G.723 24kbps (3-bit) input data\n"); 91 | fprintf(stderr, "\t-4\tProcess G.721 32kbps (4-bit) input data [default]\n"); 92 | fprintf(stderr, "\t-5\tProcess G.723 40kbps (5-bit) input data\n"); 93 | fprintf(stderr, "\t-a\tGenerate 8-bit A-law data\n"); 94 | fprintf(stderr, "\t-u\tGenerate 8-bit u-law data [default]\n"); 95 | fprintf(stderr, "\t-l\tGenerate 16-bit linear PCM data\n"); 96 | exit(1); 97 | } 98 | argc--; 99 | argv++; 100 | } 101 | 102 | /* Read and unpack input codes and process them */ 103 | while (unpack_input(&code, dec_bits) >= 0) { 104 | sample = (*dec_routine)(code, out_coding, &state); 105 | if (out_size == 2) { 106 | fwrite(&sample, out_size, 1, stdout); 107 | } else { 108 | code = (unsigned char)sample; 109 | fwrite(&code, out_size, 1, stdout); 110 | } 111 | } 112 | fclose(stdout); 113 | } 114 | -------------------------------------------------------------------------------- /G711_G721_G723/encode.c: -------------------------------------------------------------------------------- 1 | /* 2 | * encode.c 3 | * 4 | * CCITT ADPCM encoder 5 | * 6 | * Usage : encode [-3|4|5] [-a|u|l] < infile > outfile 7 | */ 8 | #include 9 | #include "g72x.h" 10 | 11 | 12 | /* 13 | * Pack output codes into bytes and write them to stdout. 14 | * Returns 1 if there is residual output, else returns 0. 15 | */ 16 | int 17 | pack_output( 18 | unsigned code, 19 | int bits) 20 | { 21 | static unsigned int out_buffer = 0; 22 | static int out_bits = 0; 23 | unsigned char out_byte; 24 | 25 | out_buffer |= (code << out_bits); 26 | out_bits += bits; 27 | if (out_bits >= 8) { 28 | out_byte = out_buffer & 0xff; 29 | out_bits -= 8; 30 | out_buffer >>= 8; 31 | fwrite(&out_byte, sizeof (char), 1, stdout); 32 | } 33 | return (out_bits > 0); 34 | } 35 | 36 | 37 | main( 38 | int argc, 39 | char **argv) 40 | { 41 | struct g72x_state state; 42 | unsigned char sample_char; 43 | short sample_short; 44 | unsigned char code; 45 | int resid; 46 | int in_coding; 47 | int in_size; 48 | unsigned *in_buf; 49 | int (*enc_routine)(); 50 | int enc_bits; 51 | 52 | g72x_init_state(&state); 53 | 54 | /* Set defaults to u-law input, G.721 output */ 55 | in_coding = AUDIO_ENCODING_ULAW; 56 | in_size = sizeof (char); 57 | in_buf = (unsigned *)&sample_char; 58 | enc_routine = g721_encoder; 59 | enc_bits = 4; 60 | 61 | /* Process encoding argument, if any */ 62 | while ((argc > 1) && (argv[1][0] == '-')) { 63 | switch (argv[1][1]) { 64 | case '3': 65 | enc_routine = g723_24_encoder; 66 | enc_bits = 3; 67 | break; 68 | case '4': 69 | enc_routine = g721_encoder; 70 | enc_bits = 4; 71 | break; 72 | case '5': 73 | enc_routine = g723_40_encoder; 74 | enc_bits = 5; 75 | break; 76 | case 'u': 77 | in_coding = AUDIO_ENCODING_ULAW; 78 | in_size = sizeof (char); 79 | in_buf = (unsigned *)&sample_char; 80 | break; 81 | case 'a': 82 | in_coding = AUDIO_ENCODING_ALAW; 83 | in_size = sizeof (char); 84 | in_buf = (unsigned *)&sample_char; 85 | break; 86 | case 'l': 87 | in_coding = AUDIO_ENCODING_LINEAR; 88 | in_size = sizeof (short); 89 | in_buf = (unsigned *)&sample_short; 90 | break; 91 | default: 92 | fprintf(stderr, "CCITT ADPCM Encoder -- usage:\n"); 93 | fprintf(stderr, "\tencode [-3|4|5] [-a|u|l] < infile > outfile\n"); 94 | fprintf(stderr, "where:\n"); 95 | fprintf(stderr, "\t-3\tGenerate G.723 24kbps (3-bit) data\n"); 96 | fprintf(stderr, "\t-4\tGenerate G.721 32kbps (4-bit) data [default]\n"); 97 | fprintf(stderr, "\t-5\tGenerate G.723 40kbps (5-bit) data\n"); 98 | fprintf(stderr, "\t-a\tProcess 8-bit A-law input data\n"); 99 | fprintf(stderr, "\t-u\tProcess 8-bit u-law input data [default]\n"); 100 | fprintf(stderr, "\t-l\tProcess 16-bit linear PCM input data\n"); 101 | exit(1); 102 | } 103 | argc--; 104 | argv++; 105 | } 106 | 107 | /* Read input file and process */ 108 | while (fread(in_buf, in_size, 1, stdin) == 1) { 109 | code = (*enc_routine)(in_size == 2 ? sample_short : sample_char, 110 | in_coding, &state); 111 | resid = pack_output(code, enc_bits); 112 | } 113 | 114 | /* Write zero codes until all residual codes are written out */ 115 | while (resid) { 116 | resid = pack_output(0, enc_bits); 117 | } 118 | fclose(stdout); 119 | } 120 | -------------------------------------------------------------------------------- /G711_G721_G723/README: -------------------------------------------------------------------------------- 1 | The files in this directory comprise ANSI-C language reference implementations 2 | of the CCITT (International Telegraph and Telephone Consultative Committee) 3 | G.711, G.721 and G.723 voice compressions. They have been tested on Sun 4 | SPARCstations and passed 82 out of 84 test vectors published by CCITT 5 | (Dec. 20, 1988) for G.721 and G.723. [The two remaining test vectors, 6 | which the G.721 decoder implementation for u-law samples did not pass, 7 | may be in error because they are identical to two other vectors for G.723_40.] 8 | 9 | This source code is released by Sun Microsystems, Inc. to the public domain. 10 | Please give your acknowledgement in product literature if this code is used 11 | in your product implementation. 12 | 13 | Sun Microsystems supports some CCITT audio formats in Solaris 2.0 system 14 | software. However, Sun's implementations have been optimized for higher 15 | performance on SPARCstations. 16 | 17 | 18 | The source files for CCITT conversion routines in this directory are: 19 | 20 | g72x.h header file for g721.c, g723_24.c and g723_40.c 21 | g711.c CCITT G.711 u-law and A-law compression 22 | g72x.c common denominator of G.721 and G.723 ADPCM codes 23 | g721.c CCITT G.721 32Kbps ADPCM coder (with g72x.c) 24 | g723_24.c CCITT G.723 24Kbps ADPCM coder (with g72x.c) 25 | g723_40.c CCITT G.723 40Kbps ADPCM coder (with g72x.c) 26 | 27 | 28 | Simple conversions between u-law, A-law, and 16-bit linear PCM are invoked 29 | as follows: 30 | 31 | unsigned char ucode, acode; 32 | short pcm_val; 33 | 34 | ucode = linear2ulaw(pcm_val); 35 | ucode = alaw2ulaw(acode); 36 | 37 | acode = linear2alaw(pcm_val); 38 | acode = ulaw2alaw(ucode); 39 | 40 | pcm_val = ulaw2linear(ucode); 41 | pcm_val = alaw2linear(acode); 42 | 43 | 44 | The other CCITT compression routines are invoked as follows: 45 | 46 | #include "g72x.h" 47 | 48 | struct g72x_state state; 49 | int sample, code; 50 | 51 | g72x_init_state(&state); 52 | code = {g721,g723_24,g723_40}_encoder(sample, coding, &state); 53 | sample = {g721,g723_24,g723_40}_decoder(code, coding, &state); 54 | 55 | where 56 | coding = AUDIO_ENCODING_ULAW for 8-bit u-law samples 57 | AUDIO_ENCODING_ALAW for 8-bit A-law samples 58 | AUDIO_ENCODING_LINEAR for 16-bit linear PCM samples 59 | 60 | 61 | 62 | This directory also includes the following sample programs: 63 | 64 | encode.c CCITT ADPCM encoder 65 | decode.c CCITT ADPCM decoder 66 | Makefile makefile for the sample programs 67 | 68 | 69 | The sample programs contain examples of how to call the various compression 70 | routines and pack/unpack the bits. The sample programs read byte streams from 71 | stdin and write to stdout. The input/output data is raw data (no file header 72 | or other identifying information is embedded). The sample programs are 73 | invoked as follows: 74 | 75 | encode [-3|4|5] [-a|u|l] outfile 76 | decode [-3|4|5] [-a|u|l] outfile 77 | where: 78 | -3 encode to (decode from) G.723 24kbps (3-bit) data 79 | -4 encode to (decode from) G.721 32kbps (4-bit) data [the default] 80 | -5 encode to (decode from) G.723 40kbps (5-bit) data 81 | -a encode from (decode to) A-law data 82 | -u encode from (decode to) u-law data [the default] 83 | -l encode from (decode to) 16-bit linear data 84 | 85 | Examples: 86 | # Read 16-bit linear and output G.721 87 | encode -4 -l g721file 88 | 89 | # Read 40Kbps G.723 and output A-law 90 | decode -5 -a alawfile 91 | 92 | # Compress and then decompress u-law data using 24Kbps G.723 93 | encode -3 ulawout 94 | 95 | -------------------------------------------------------------------------------- /G711_G721_G723/g72x.h: -------------------------------------------------------------------------------- 1 | /* 2 | * This source code is a product of Sun Microsystems, Inc. and is provided 3 | * for unrestricted use. Users may copy or modify this source code without 4 | * charge. 5 | * 6 | * SUN SOURCE CODE IS PROVIDED AS IS WITH NO WARRANTIES OF ANY KIND INCLUDING 7 | * THE WARRANTIES OF DESIGN, MERCHANTIBILITY AND FITNESS FOR A PARTICULAR 8 | * PURPOSE, OR ARISING FROM A COURSE OF DEALING, USAGE OR TRADE PRACTICE. 9 | * 10 | * Sun source code is provided with no support and without any obligation on 11 | * the part of Sun Microsystems, Inc. to assist in its use, correction, 12 | * modification or enhancement. 13 | * 14 | * SUN MICROSYSTEMS, INC. SHALL HAVE NO LIABILITY WITH RESPECT TO THE 15 | * INFRINGEMENT OF COPYRIGHTS, TRADE SECRETS OR ANY PATENTS BY THIS SOFTWARE 16 | * OR ANY PART THEREOF. 17 | * 18 | * In no event will Sun Microsystems, Inc. be liable for any lost revenue 19 | * or profits or other special, indirect and consequential damages, even if 20 | * Sun has been advised of the possibility of such damages. 21 | * 22 | * Sun Microsystems, Inc. 23 | * 2550 Garcia Avenue 24 | * Mountain View, California 94043 25 | */ 26 | 27 | /* 28 | * g72x.h 29 | * 30 | * Header file for CCITT conversion routines. 31 | * 32 | */ 33 | #ifndef _G72X_H 34 | #define _G72X_H 35 | 36 | #define AUDIO_ENCODING_ULAW (1) /* ISDN u-law */ 37 | #define AUDIO_ENCODING_ALAW (2) /* ISDN A-law */ 38 | #define AUDIO_ENCODING_LINEAR (3) /* PCM 2's-complement (0-center) */ 39 | 40 | /* 41 | * The following is the definition of the state structure 42 | * used by the G.721/G.723 encoder and decoder to preserve their internal 43 | * state between successive calls. The meanings of the majority 44 | * of the state structure fields are explained in detail in the 45 | * CCITT Recommendation G.721. The field names are essentially indentical 46 | * to variable names in the bit level description of the coding algorithm 47 | * included in this Recommendation. 48 | */ 49 | struct g72x_state { 50 | long yl; /* Locked or steady state step size multiplier. */ 51 | short yu; /* Unlocked or non-steady state step size multiplier. */ 52 | short dms; /* Short term energy estimate. */ 53 | short dml; /* Long term energy estimate. */ 54 | short ap; /* Linear weighting coefficient of 'yl' and 'yu'. */ 55 | 56 | short a[2]; /* Coefficients of pole portion of prediction filter. */ 57 | short b[6]; /* Coefficients of zero portion of prediction filter. */ 58 | short pk[2]; /* 59 | * Signs of previous two samples of a partially 60 | * reconstructed signal. 61 | */ 62 | short dq[6]; /* 63 | * Previous 6 samples of the quantized difference 64 | * signal represented in an internal floating point 65 | * format. 66 | */ 67 | short sr[2]; /* 68 | * Previous 2 samples of the quantized difference 69 | * signal represented in an internal floating point 70 | * format. 71 | */ 72 | char td; /* delayed tone detect, new in 1988 version */ 73 | }; 74 | 75 | /* External function definitions. */ 76 | 77 | extern void g72x_init_stat(struct g72x_state *); 78 | extern int g721_encoder( 79 | int sample, 80 | int in_coding, 81 | struct g72x_state *state_ptr); 82 | extern int g721_decoder( 83 | int code, 84 | int out_coding, 85 | struct g72x_state *state_ptr); 86 | extern int g723_24_encoder( 87 | int sample, 88 | int in_coding, 89 | struct g72x_state *state_ptr); 90 | extern int g723_24_decoder( 91 | int code, 92 | int out_coding, 93 | struct g72x_state *state_ptr); 94 | extern int g723_40_encoder( 95 | int sample, 96 | int in_coding, 97 | struct g72x_state *state_ptr); 98 | extern int g723_40_decoder( 99 | int code, 100 | int out_coding, 101 | struct g72x_state *state_ptr); 102 | 103 | #endif /* !_G72X_H */ 104 | -------------------------------------------------------------------------------- /G711_G721_G723/g723_24.c: -------------------------------------------------------------------------------- 1 | /* 2 | * This source code is a product of Sun Microsystems, Inc. and is provided 3 | * for unrestricted use. Users may copy or modify this source code without 4 | * charge. 5 | * 6 | * SUN SOURCE CODE IS PROVIDED AS IS WITH NO WARRANTIES OF ANY KIND INCLUDING 7 | * THE WARRANTIES OF DESIGN, MERCHANTIBILITY AND FITNESS FOR A PARTICULAR 8 | * PURPOSE, OR ARISING FROM A COURSE OF DEALING, USAGE OR TRADE PRACTICE. 9 | * 10 | * Sun source code is provided with no support and without any obligation on 11 | * the part of Sun Microsystems, Inc. to assist in its use, correction, 12 | * modification or enhancement. 13 | * 14 | * SUN MICROSYSTEMS, INC. SHALL HAVE NO LIABILITY WITH RESPECT TO THE 15 | * INFRINGEMENT OF COPYRIGHTS, TRADE SECRETS OR ANY PATENTS BY THIS SOFTWARE 16 | * OR ANY PART THEREOF. 17 | * 18 | * In no event will Sun Microsystems, Inc. be liable for any lost revenue 19 | * or profits or other special, indirect and consequential damages, even if 20 | * Sun has been advised of the possibility of such damages. 21 | * 22 | * Sun Microsystems, Inc. 23 | * 2550 Garcia Avenue 24 | * Mountain View, California 94043 25 | */ 26 | 27 | /* 28 | * g723_24.c 29 | * 30 | * Description: 31 | * 32 | * g723_24_encoder(), g723_24_decoder() 33 | * 34 | * These routines comprise an implementation of the CCITT G.723 24 Kbps 35 | * ADPCM coding algorithm. Essentially, this implementation is identical to 36 | * the bit level description except for a few deviations which take advantage 37 | * of workstation attributes, such as hardware 2's complement arithmetic. 38 | * 39 | */ 40 | #include "g72x.h" 41 | 42 | /* 43 | * Maps G.723_24 code word to reconstructed scale factor normalized log 44 | * magnitude values. 45 | */ 46 | static short _dqlntab[8] = {-2048, 135, 273, 373, 373, 273, 135, -2048}; 47 | 48 | /* Maps G.723_24 code word to log of scale factor multiplier. */ 49 | static short _witab[8] = {-128, 960, 4384, 18624, 18624, 4384, 960, -128}; 50 | 51 | /* 52 | * Maps G.723_24 code words to a set of values whose long and short 53 | * term averages are computed and then compared to give an indication 54 | * how stationary (steady state) the signal is. 55 | */ 56 | static short _fitab[8] = {0, 0x200, 0x400, 0xE00, 0xE00, 0x400, 0x200, 0}; 57 | 58 | static short qtab_723_24[3] = {8, 218, 331}; 59 | 60 | /* 61 | * g723_24_encoder() 62 | * 63 | * Encodes a linear PCM, A-law or u-law input sample and returns its 3-bit code. 64 | * Returns -1 if invalid input coding value. 65 | */ 66 | int 67 | g723_24_encoder( 68 | int sl, 69 | int in_coding, 70 | struct g72x_state *state_ptr) 71 | { 72 | short sei, sezi, se, sez; /* ACCUM */ 73 | short d; /* SUBTA */ 74 | short y; /* MIX */ 75 | short sr; /* ADDB */ 76 | short dqsez; /* ADDC */ 77 | short dq, i; 78 | 79 | switch (in_coding) { /* linearize input sample to 14-bit PCM */ 80 | case AUDIO_ENCODING_ALAW: 81 | sl = alaw2linear(sl) >> 2; 82 | break; 83 | case AUDIO_ENCODING_ULAW: 84 | sl = ulaw2linear(sl) >> 2; 85 | break; 86 | case AUDIO_ENCODING_LINEAR: 87 | sl >>= 2; /* sl of 14-bit dynamic range */ 88 | break; 89 | default: 90 | return (-1); 91 | } 92 | 93 | sezi = predictor_zero(state_ptr); 94 | sez = sezi >> 1; 95 | sei = sezi + predictor_pole(state_ptr); 96 | se = sei >> 1; /* se = estimated signal */ 97 | 98 | d = sl - se; /* d = estimation diff. */ 99 | 100 | /* quantize prediction difference d */ 101 | y = step_size(state_ptr); /* quantizer step size */ 102 | i = quantize(d, y, qtab_723_24, 3); /* i = ADPCM code */ 103 | dq = reconstruct(i & 4, _dqlntab[i], y); /* quantized diff. */ 104 | 105 | sr = (dq < 0) ? se - (dq & 0x3FFF) : se + dq; /* reconstructed signal */ 106 | 107 | dqsez = sr + sez - se; /* pole prediction diff. */ 108 | 109 | update(3, y, _witab[i], _fitab[i], dq, sr, dqsez, state_ptr); 110 | 111 | return (i); 112 | } 113 | 114 | /* 115 | * g723_24_decoder() 116 | * 117 | * Decodes a 3-bit CCITT G.723_24 ADPCM code and returns 118 | * the resulting 16-bit linear PCM, A-law or u-law sample value. 119 | * -1 is returned if the output coding is unknown. 120 | */ 121 | int 122 | g723_24_decoder( 123 | int i, 124 | int out_coding, 125 | struct g72x_state *state_ptr) 126 | { 127 | short sezi, sei, sez, se; /* ACCUM */ 128 | short y; /* MIX */ 129 | short sr; /* ADDB */ 130 | short dq; 131 | short dqsez; 132 | 133 | i &= 0x07; /* mask to get proper bits */ 134 | sezi = predictor_zero(state_ptr); 135 | sez = sezi >> 1; 136 | sei = sezi + predictor_pole(state_ptr); 137 | se = sei >> 1; /* se = estimated signal */ 138 | 139 | y = step_size(state_ptr); /* adaptive quantizer step size */ 140 | dq = reconstruct(i & 0x04, _dqlntab[i], y); /* unquantize pred diff */ 141 | 142 | sr = (dq < 0) ? (se - (dq & 0x3FFF)) : (se + dq); /* reconst. signal */ 143 | 144 | dqsez = sr - se + sez; /* pole prediction diff. */ 145 | 146 | update(3, y, _witab[i], _fitab[i], dq, sr, dqsez, state_ptr); 147 | 148 | switch (out_coding) { 149 | case AUDIO_ENCODING_ALAW: 150 | return (tandem_adjust_alaw(sr, se, y, i, 4, qtab_723_24)); 151 | case AUDIO_ENCODING_ULAW: 152 | return (tandem_adjust_ulaw(sr, se, y, i, 4, qtab_723_24)); 153 | case AUDIO_ENCODING_LINEAR: 154 | return (sr << 2); /* sr was of 14-bit dynamic range */ 155 | default: 156 | return (-1); 157 | } 158 | } 159 | -------------------------------------------------------------------------------- /G711_G721_G723/g721.c: -------------------------------------------------------------------------------- 1 | /* 2 | * This source code is a product of Sun Microsystems, Inc. and is provided 3 | * for unrestricted use. Users may copy or modify this source code without 4 | * charge. 5 | * 6 | * SUN SOURCE CODE IS PROVIDED AS IS WITH NO WARRANTIES OF ANY KIND INCLUDING 7 | * THE WARRANTIES OF DESIGN, MERCHANTIBILITY AND FITNESS FOR A PARTICULAR 8 | * PURPOSE, OR ARISING FROM A COURSE OF DEALING, USAGE OR TRADE PRACTICE. 9 | * 10 | * Sun source code is provided with no support and without any obligation on 11 | * the part of Sun Microsystems, Inc. to assist in its use, correction, 12 | * modification or enhancement. 13 | * 14 | * SUN MICROSYSTEMS, INC. SHALL HAVE NO LIABILITY WITH RESPECT TO THE 15 | * INFRINGEMENT OF COPYRIGHTS, TRADE SECRETS OR ANY PATENTS BY THIS SOFTWARE 16 | * OR ANY PART THEREOF. 17 | * 18 | * In no event will Sun Microsystems, Inc. be liable for any lost revenue 19 | * or profits or other special, indirect and consequential damages, even if 20 | * Sun has been advised of the possibility of such damages. 21 | * 22 | * Sun Microsystems, Inc. 23 | * 2550 Garcia Avenue 24 | * Mountain View, California 94043 25 | */ 26 | 27 | /* 28 | * g721.c 29 | * 30 | * Description: 31 | * 32 | * g721_encoder(), g721_decoder() 33 | * 34 | * These routines comprise an implementation of the CCITT G.721 ADPCM 35 | * coding algorithm. Essentially, this implementation is identical to 36 | * the bit level description except for a few deviations which 37 | * take advantage of work station attributes, such as hardware 2's 38 | * complement arithmetic and large memory. Specifically, certain time 39 | * consuming operations such as multiplications are replaced 40 | * with lookup tables and software 2's complement operations are 41 | * replaced with hardware 2's complement. 42 | * 43 | * The deviation from the bit level specification (lookup tables) 44 | * preserves the bit level performance specifications. 45 | * 46 | * As outlined in the G.721 Recommendation, the algorithm is broken 47 | * down into modules. Each section of code below is preceded by 48 | * the name of the module which it is implementing. 49 | * 50 | */ 51 | #include "g72x.h" 52 | 53 | static short qtab_721[7] = {-124, 80, 178, 246, 300, 349, 400}; 54 | /* 55 | * Maps G.721 code word to reconstructed scale factor normalized log 56 | * magnitude values. 57 | */ 58 | static short _dqlntab[16] = {-2048, 4, 135, 213, 273, 323, 373, 425, 59 | 425, 373, 323, 273, 213, 135, 4, -2048}; 60 | 61 | /* Maps G.721 code word to log of scale factor multiplier. */ 62 | static short _witab[16] = {-12, 18, 41, 64, 112, 198, 355, 1122, 63 | 1122, 355, 198, 112, 64, 41, 18, -12}; 64 | /* 65 | * Maps G.721 code words to a set of values whose long and short 66 | * term averages are computed and then compared to give an indication 67 | * how stationary (steady state) the signal is. 68 | */ 69 | static short _fitab[16] = {0, 0, 0, 0x200, 0x200, 0x200, 0x600, 0xE00, 70 | 0xE00, 0x600, 0x200, 0x200, 0x200, 0, 0, 0}; 71 | 72 | /* 73 | * g721_encoder() 74 | * 75 | * Encodes the input vale of linear PCM, A-law or u-law data sl and returns 76 | * the resulting code. -1 is returned for unknown input coding value. 77 | */ 78 | int 79 | g721_encoder( 80 | int sl, 81 | int in_coding, 82 | struct g72x_state *state_ptr) 83 | { 84 | short sezi, se, sez; /* ACCUM */ 85 | short d; /* SUBTA */ 86 | short sr; /* ADDB */ 87 | short y; /* MIX */ 88 | short dqsez; /* ADDC */ 89 | short dq, i; 90 | 91 | switch (in_coding) { /* linearize input sample to 14-bit PCM */ 92 | case AUDIO_ENCODING_ALAW: 93 | sl = alaw2linear(sl) >> 2; 94 | break; 95 | case AUDIO_ENCODING_ULAW: 96 | sl = ulaw2linear(sl) >> 2; 97 | break; 98 | case AUDIO_ENCODING_LINEAR: 99 | sl >>= 2; /* 14-bit dynamic range */ 100 | break; 101 | default: 102 | return (-1); 103 | } 104 | 105 | sezi = predictor_zero(state_ptr); 106 | sez = sezi >> 1; 107 | se = (sezi + predictor_pole(state_ptr)) >> 1; /* estimated signal */ 108 | 109 | d = sl - se; /* estimation difference */ 110 | 111 | /* quantize the prediction difference */ 112 | y = step_size(state_ptr); /* quantizer step size */ 113 | i = quantize(d, y, qtab_721, 7); /* i = ADPCM code */ 114 | 115 | dq = reconstruct(i & 8, _dqlntab[i], y); /* quantized est diff */ 116 | 117 | sr = (dq < 0) ? se - (dq & 0x3FFF) : se + dq; /* reconst. signal */ 118 | 119 | dqsez = sr + sez - se; /* pole prediction diff. */ 120 | 121 | update(4, y, _witab[i] << 5, _fitab[i], dq, sr, dqsez, state_ptr); 122 | 123 | return (i); 124 | } 125 | 126 | /* 127 | * g721_decoder() 128 | * 129 | * Description: 130 | * 131 | * Decodes a 4-bit code of G.721 encoded data of i and 132 | * returns the resulting linear PCM, A-law or u-law value. 133 | * return -1 for unknown out_coding value. 134 | */ 135 | int 136 | g721_decoder( 137 | int i, 138 | int out_coding, 139 | struct g72x_state *state_ptr) 140 | { 141 | short sezi, sei, sez, se; /* ACCUM */ 142 | short y; /* MIX */ 143 | short sr; /* ADDB */ 144 | short dq; 145 | short dqsez; 146 | 147 | i &= 0x0f; /* mask to get proper bits */ 148 | sezi = predictor_zero(state_ptr); 149 | sez = sezi >> 1; 150 | sei = sezi + predictor_pole(state_ptr); 151 | se = sei >> 1; /* se = estimated signal */ 152 | 153 | y = step_size(state_ptr); /* dynamic quantizer step size */ 154 | 155 | dq = reconstruct(i & 0x08, _dqlntab[i], y); /* quantized diff. */ 156 | 157 | sr = (dq < 0) ? (se - (dq & 0x3FFF)) : se + dq; /* reconst. signal */ 158 | 159 | dqsez = sr - se + sez; /* pole prediction diff. */ 160 | 161 | update(4, y, _witab[i] << 5, _fitab[i], dq, sr, dqsez, state_ptr); 162 | 163 | switch (out_coding) { 164 | case AUDIO_ENCODING_ALAW: 165 | return (tandem_adjust_alaw(sr, se, y, i, 8, qtab_721)); 166 | case AUDIO_ENCODING_ULAW: 167 | return (tandem_adjust_ulaw(sr, se, y, i, 8, qtab_721)); 168 | case AUDIO_ENCODING_LINEAR: 169 | return (sr << 2); /* sr was 14-bit dynamic range */ 170 | default: 171 | return (-1); 172 | } 173 | } 174 | -------------------------------------------------------------------------------- /G711_G721_G723/g723_40.c: -------------------------------------------------------------------------------- 1 | /* 2 | * This source code is a product of Sun Microsystems, Inc. and is provided 3 | * for unrestricted use. Users may copy or modify this source code without 4 | * charge. 5 | * 6 | * SUN SOURCE CODE IS PROVIDED AS IS WITH NO WARRANTIES OF ANY KIND INCLUDING 7 | * THE WARRANTIES OF DESIGN, MERCHANTIBILITY AND FITNESS FOR A PARTICULAR 8 | * PURPOSE, OR ARISING FROM A COURSE OF DEALING, USAGE OR TRADE PRACTICE. 9 | * 10 | * Sun source code is provided with no support and without any obligation on 11 | * the part of Sun Microsystems, Inc. to assist in its use, correction, 12 | * modification or enhancement. 13 | * 14 | * SUN MICROSYSTEMS, INC. SHALL HAVE NO LIABILITY WITH RESPECT TO THE 15 | * INFRINGEMENT OF COPYRIGHTS, TRADE SECRETS OR ANY PATENTS BY THIS SOFTWARE 16 | * OR ANY PART THEREOF. 17 | * 18 | * In no event will Sun Microsystems, Inc. be liable for any lost revenue 19 | * or profits or other special, indirect and consequential damages, even if 20 | * Sun has been advised of the possibility of such damages. 21 | * 22 | * Sun Microsystems, Inc. 23 | * 2550 Garcia Avenue 24 | * Mountain View, California 94043 25 | */ 26 | 27 | /* 28 | * g723_40.c 29 | * 30 | * Description: 31 | * 32 | * g723_40_encoder(), g723_40_decoder() 33 | * 34 | * These routines comprise an implementation of the CCITT G.723 40Kbps 35 | * ADPCM coding algorithm. Essentially, this implementation is identical to 36 | * the bit level description except for a few deviations which 37 | * take advantage of workstation attributes, such as hardware 2's 38 | * complement arithmetic. 39 | * 40 | * The deviation from the bit level specification (lookup tables), 41 | * preserves the bit level performance specifications. 42 | * 43 | * As outlined in the G.723 Recommendation, the algorithm is broken 44 | * down into modules. Each section of code below is preceded by 45 | * the name of the module which it is implementing. 46 | * 47 | */ 48 | #include "g72x.h" 49 | 50 | /* 51 | * Maps G.723_40 code word to ructeconstructed scale factor normalized log 52 | * magnitude values. 53 | */ 54 | static short _dqlntab[32] = {-2048, -66, 28, 104, 169, 224, 274, 318, 55 | 358, 395, 429, 459, 488, 514, 539, 566, 56 | 566, 539, 514, 488, 459, 429, 395, 358, 57 | 318, 274, 224, 169, 104, 28, -66, -2048}; 58 | 59 | /* Maps G.723_40 code word to log of scale factor multiplier. */ 60 | static short _witab[32] = {448, 448, 768, 1248, 1280, 1312, 1856, 3200, 61 | 4512, 5728, 7008, 8960, 11456, 14080, 16928, 22272, 62 | 22272, 16928, 14080, 11456, 8960, 7008, 5728, 4512, 63 | 3200, 1856, 1312, 1280, 1248, 768, 448, 448}; 64 | 65 | /* 66 | * Maps G.723_40 code words to a set of values whose long and short 67 | * term averages are computed and then compared to give an indication 68 | * how stationary (steady state) the signal is. 69 | */ 70 | static short _fitab[32] = {0, 0, 0, 0, 0, 0x200, 0x200, 0x200, 71 | 0x200, 0x200, 0x400, 0x600, 0x800, 0xA00, 0xC00, 0xC00, 72 | 0xC00, 0xC00, 0xA00, 0x800, 0x600, 0x400, 0x200, 0x200, 73 | 0x200, 0x200, 0x200, 0, 0, 0, 0, 0}; 74 | 75 | static short qtab_723_40[15] = {-122, -16, 68, 139, 198, 250, 298, 339, 76 | 378, 413, 445, 475, 502, 528, 553}; 77 | 78 | /* 79 | * g723_40_encoder() 80 | * 81 | * Encodes a 16-bit linear PCM, A-law or u-law input sample and retuens 82 | * the resulting 5-bit CCITT G.723 40Kbps code. 83 | * Returns -1 if the input coding value is invalid. 84 | */ 85 | int 86 | g723_40_encoder( 87 | int sl, 88 | int in_coding, 89 | struct g72x_state *state_ptr) 90 | { 91 | short sei, sezi, se, sez; /* ACCUM */ 92 | short d; /* SUBTA */ 93 | short y; /* MIX */ 94 | short sr; /* ADDB */ 95 | short dqsez; /* ADDC */ 96 | short dq, i; 97 | 98 | switch (in_coding) { /* linearize input sample to 14-bit PCM */ 99 | case AUDIO_ENCODING_ALAW: 100 | sl = alaw2linear(sl) >> 2; 101 | break; 102 | case AUDIO_ENCODING_ULAW: 103 | sl = ulaw2linear(sl) >> 2; 104 | break; 105 | case AUDIO_ENCODING_LINEAR: 106 | sl >>= 2; /* sl of 14-bit dynamic range */ 107 | break; 108 | default: 109 | return (-1); 110 | } 111 | 112 | sezi = predictor_zero(state_ptr); 113 | sez = sezi >> 1; 114 | sei = sezi + predictor_pole(state_ptr); 115 | se = sei >> 1; /* se = estimated signal */ 116 | 117 | d = sl - se; /* d = estimation difference */ 118 | 119 | /* quantize prediction difference */ 120 | y = step_size(state_ptr); /* adaptive quantizer step size */ 121 | i = quantize(d, y, qtab_723_40, 15); /* i = ADPCM code */ 122 | 123 | dq = reconstruct(i & 0x10, _dqlntab[i], y); /* quantized diff */ 124 | 125 | sr = (dq < 0) ? se - (dq & 0x7FFF) : se + dq; /* reconstructed signal */ 126 | 127 | dqsez = sr + sez - se; /* dqsez = pole prediction diff. */ 128 | 129 | update(5, y, _witab[i], _fitab[i], dq, sr, dqsez, state_ptr); 130 | 131 | return (i); 132 | } 133 | 134 | /* 135 | * g723_40_decoder() 136 | * 137 | * Decodes a 5-bit CCITT G.723 40Kbps code and returns 138 | * the resulting 16-bit linear PCM, A-law or u-law sample value. 139 | * -1 is returned if the output coding is unknown. 140 | */ 141 | int 142 | g723_40_decoder( 143 | int i, 144 | int out_coding, 145 | struct g72x_state *state_ptr) 146 | { 147 | short sezi, sei, sez, se; /* ACCUM */ 148 | short y, dif; /* MIX */ 149 | short sr; /* ADDB */ 150 | short dq; 151 | short dqsez; 152 | 153 | i &= 0x1f; /* mask to get proper bits */ 154 | sezi = predictor_zero(state_ptr); 155 | sez = sezi >> 1; 156 | sei = sezi + predictor_pole(state_ptr); 157 | se = sei >> 1; /* se = estimated signal */ 158 | 159 | y = step_size(state_ptr); /* adaptive quantizer step size */ 160 | dq = reconstruct(i & 0x10, _dqlntab[i], y); /* estimation diff. */ 161 | 162 | sr = (dq < 0) ? (se - (dq & 0x7FFF)) : (se + dq); /* reconst. signal */ 163 | 164 | dqsez = sr - se + sez; /* pole prediction diff. */ 165 | 166 | update(5, y, _witab[i], _fitab[i], dq, sr, dqsez, state_ptr); 167 | 168 | switch (out_coding) { 169 | case AUDIO_ENCODING_ALAW: 170 | return (tandem_adjust_alaw(sr, se, y, i, 0x10, qtab_723_40)); 171 | case AUDIO_ENCODING_ULAW: 172 | return (tandem_adjust_ulaw(sr, se, y, i, 0x10, qtab_723_40)); 173 | case AUDIO_ENCODING_LINEAR: 174 | return (sr << 2); /* sr was of 14-bit dynamic range */ 175 | default: 176 | return (-1); 177 | } 178 | } 179 | -------------------------------------------------------------------------------- /G711_G721_G723/g711.c: -------------------------------------------------------------------------------- 1 | /* 2 | * This source code is a product of Sun Microsystems, Inc. and is provided 3 | * for unrestricted use. Users may copy or modify this source code without 4 | * charge. 5 | * 6 | * SUN SOURCE CODE IS PROVIDED AS IS WITH NO WARRANTIES OF ANY KIND INCLUDING 7 | * THE WARRANTIES OF DESIGN, MERCHANTIBILITY AND FITNESS FOR A PARTICULAR 8 | * PURPOSE, OR ARISING FROM A COURSE OF DEALING, USAGE OR TRADE PRACTICE. 9 | * 10 | * Sun source code is provided with no support and without any obligation on 11 | * the part of Sun Microsystems, Inc. to assist in its use, correction, 12 | * modification or enhancement. 13 | * 14 | * SUN MICROSYSTEMS, INC. SHALL HAVE NO LIABILITY WITH RESPECT TO THE 15 | * INFRINGEMENT OF COPYRIGHTS, TRADE SECRETS OR ANY PATENTS BY THIS SOFTWARE 16 | * OR ANY PART THEREOF. 17 | * 18 | * In no event will Sun Microsystems, Inc. be liable for any lost revenue 19 | * or profits or other special, indirect and consequential damages, even if 20 | * Sun has been advised of the possibility of such damages. 21 | * 22 | * Sun Microsystems, Inc. 23 | * 2550 Garcia Avenue 24 | * Mountain View, California 94043 25 | */ 26 | 27 | /* 28 | * g711.c 29 | * 30 | * u-law, A-law and linear PCM conversions. 31 | */ 32 | #define SIGN_BIT (0x80) /* Sign bit for a A-law byte. */ 33 | #define QUANT_MASK (0xf) /* Quantization field mask. */ 34 | #define NSEGS (8) /* Number of A-law segments. */ 35 | #define SEG_SHIFT (4) /* Left shift for segment number. */ 36 | #define SEG_MASK (0x70) /* Segment field mask. */ 37 | 38 | static short seg_end[8] = {0xFF, 0x1FF, 0x3FF, 0x7FF, 39 | 0xFFF, 0x1FFF, 0x3FFF, 0x7FFF}; 40 | 41 | /* copy from CCITT G.711 specifications */ 42 | unsigned char _u2a[128] = { /* u- to A-law conversions */ 43 | 1, 1, 2, 2, 3, 3, 4, 4, 44 | 5, 5, 6, 6, 7, 7, 8, 8, 45 | 9, 10, 11, 12, 13, 14, 15, 16, 46 | 17, 18, 19, 20, 21, 22, 23, 24, 47 | 25, 27, 29, 31, 33, 34, 35, 36, 48 | 37, 38, 39, 40, 41, 42, 43, 44, 49 | 46, 48, 49, 50, 51, 52, 53, 54, 50 | 55, 56, 57, 58, 59, 60, 61, 62, 51 | 64, 65, 66, 67, 68, 69, 70, 71, 52 | 72, 73, 74, 75, 76, 77, 78, 79, 53 | 81, 82, 83, 84, 85, 86, 87, 88, 54 | 89, 90, 91, 92, 93, 94, 95, 96, 55 | 97, 98, 99, 100, 101, 102, 103, 104, 56 | 105, 106, 107, 108, 109, 110, 111, 112, 57 | 113, 114, 115, 116, 117, 118, 119, 120, 58 | 121, 122, 123, 124, 125, 126, 127, 128}; 59 | 60 | unsigned char _a2u[128] = { /* A- to u-law conversions */ 61 | 1, 3, 5, 7, 9, 11, 13, 15, 62 | 16, 17, 18, 19, 20, 21, 22, 23, 63 | 24, 25, 26, 27, 28, 29, 30, 31, 64 | 32, 32, 33, 33, 34, 34, 35, 35, 65 | 36, 37, 38, 39, 40, 41, 42, 43, 66 | 44, 45, 46, 47, 48, 48, 49, 49, 67 | 50, 51, 52, 53, 54, 55, 56, 57, 68 | 58, 59, 60, 61, 62, 63, 64, 64, 69 | 65, 66, 67, 68, 69, 70, 71, 72, 70 | 73, 74, 75, 76, 77, 78, 79, 79, 71 | 80, 81, 82, 83, 84, 85, 86, 87, 72 | 88, 89, 90, 91, 92, 93, 94, 95, 73 | 96, 97, 98, 99, 100, 101, 102, 103, 74 | 104, 105, 106, 107, 108, 109, 110, 111, 75 | 112, 113, 114, 115, 116, 117, 118, 119, 76 | 120, 121, 122, 123, 124, 125, 126, 127}; 77 | 78 | static int 79 | search( 80 | int val, 81 | short *table, 82 | int size) 83 | { 84 | int i; 85 | 86 | for (i = 0; i < size; i++) { 87 | if (val <= *table++) 88 | return (i); 89 | } 90 | return (size); 91 | } 92 | 93 | /* 94 | * linear2alaw() - Convert a 16-bit linear PCM value to 8-bit A-law 95 | * 96 | * linear2alaw() accepts an 16-bit integer and encodes it as A-law data. 97 | * 98 | * Linear Input Code Compressed Code 99 | * ------------------------ --------------- 100 | * 0000000wxyza 000wxyz 101 | * 0000001wxyza 001wxyz 102 | * 000001wxyzab 010wxyz 103 | * 00001wxyzabc 011wxyz 104 | * 0001wxyzabcd 100wxyz 105 | * 001wxyzabcde 101wxyz 106 | * 01wxyzabcdef 110wxyz 107 | * 1wxyzabcdefg 111wxyz 108 | * 109 | * For further information see John C. Bellamy's Digital Telephony, 1982, 110 | * John Wiley & Sons, pps 98-111 and 472-476. 111 | */ 112 | unsigned char 113 | linear2alaw( 114 | int pcm_val) /* 2's complement (16-bit range) */ 115 | { 116 | int mask; 117 | int seg; 118 | unsigned char aval; 119 | 120 | if (pcm_val >= 0) { 121 | mask = 0xD5; /* sign (7th) bit = 1 */ 122 | } else { 123 | mask = 0x55; /* sign bit = 0 */ 124 | pcm_val = -pcm_val - 8; 125 | } 126 | 127 | /* Convert the scaled magnitude to segment number. */ 128 | seg = search(pcm_val, seg_end, 8); 129 | 130 | /* Combine the sign, segment, and quantization bits. */ 131 | 132 | if (seg >= 8) /* out of range, return maximum value. */ 133 | return (0x7F ^ mask); 134 | else { 135 | aval = seg << SEG_SHIFT; 136 | if (seg < 2) 137 | aval |= (pcm_val >> 4) & QUANT_MASK; 138 | else 139 | aval |= (pcm_val >> (seg + 3)) & QUANT_MASK; 140 | return (aval ^ mask); 141 | } 142 | } 143 | 144 | /* 145 | * alaw2linear() - Convert an A-law value to 16-bit linear PCM 146 | * 147 | */ 148 | int 149 | alaw2linear( 150 | unsigned char a_val) 151 | { 152 | int t; 153 | int seg; 154 | 155 | a_val ^= 0x55; 156 | 157 | t = (a_val & QUANT_MASK) << 4; 158 | seg = ((unsigned)a_val & SEG_MASK) >> SEG_SHIFT; 159 | switch (seg) { 160 | case 0: 161 | t += 8; 162 | break; 163 | case 1: 164 | t += 0x108; 165 | break; 166 | default: 167 | t += 0x108; 168 | t <<= seg - 1; 169 | } 170 | return ((a_val & SIGN_BIT) ? t : -t); 171 | } 172 | 173 | #define BIAS (0x84) /* Bias for linear code. */ 174 | 175 | /* 176 | * linear2ulaw() - Convert a linear PCM value to u-law 177 | * 178 | * In order to simplify the encoding process, the original linear magnitude 179 | * is biased by adding 33 which shifts the encoding range from (0 - 8158) to 180 | * (33 - 8191). The result can be seen in the following encoding table: 181 | * 182 | * Biased Linear Input Code Compressed Code 183 | * ------------------------ --------------- 184 | * 00000001wxyza 000wxyz 185 | * 0000001wxyzab 001wxyz 186 | * 000001wxyzabc 010wxyz 187 | * 00001wxyzabcd 011wxyz 188 | * 0001wxyzabcde 100wxyz 189 | * 001wxyzabcdef 101wxyz 190 | * 01wxyzabcdefg 110wxyz 191 | * 1wxyzabcdefgh 111wxyz 192 | * 193 | * Each biased linear code has a leading 1 which identifies the segment 194 | * number. The value of the segment number is equal to 7 minus the number 195 | * of leading 0's. The quantization interval is directly available as the 196 | * four bits wxyz. * The trailing bits (a - h) are ignored. 197 | * 198 | * Ordinarily the complement of the resulting code word is used for 199 | * transmission, and so the code word is complemented before it is returned. 200 | * 201 | * For further information see John C. Bellamy's Digital Telephony, 1982, 202 | * John Wiley & Sons, pps 98-111 and 472-476. 203 | */ 204 | unsigned char 205 | linear2ulaw( 206 | int pcm_val) /* 2's complement (16-bit range) */ 207 | { 208 | int mask; 209 | int seg; 210 | unsigned char uval; 211 | 212 | /* Get the sign and the magnitude of the value. */ 213 | if (pcm_val < 0) { 214 | pcm_val = BIAS - pcm_val; 215 | mask = 0x7F; 216 | } else { 217 | pcm_val += BIAS; 218 | mask = 0xFF; 219 | } 220 | 221 | /* Convert the scaled magnitude to segment number. */ 222 | seg = search(pcm_val, seg_end, 8); 223 | 224 | /* 225 | * Combine the sign, segment, quantization bits; 226 | * and complement the code word. 227 | */ 228 | if (seg >= 8) /* out of range, return maximum value. */ 229 | return (0x7F ^ mask); 230 | else { 231 | uval = (seg << 4) | ((pcm_val >> (seg + 3)) & 0xF); 232 | return (uval ^ mask); 233 | } 234 | 235 | } 236 | 237 | /* 238 | * ulaw2linear() - Convert a u-law value to 16-bit linear PCM 239 | * 240 | * First, a biased linear code is derived from the code word. An unbiased 241 | * output can then be obtained by subtracting 33 from the biased code. 242 | * 243 | * Note that this function expects to be passed the complement of the 244 | * original code word. This is in keeping with ISDN conventions. 245 | */ 246 | int 247 | ulaw2linear( 248 | unsigned char u_val) 249 | { 250 | int t; 251 | 252 | /* Complement to obtain normal u-law value. */ 253 | u_val = ~u_val; 254 | 255 | /* 256 | * Extract and bias the quantization bits. Then 257 | * shift up by the segment number and subtract out the bias. 258 | */ 259 | t = ((u_val & QUANT_MASK) << 3) + BIAS; 260 | t <<= ((unsigned)u_val & SEG_MASK) >> SEG_SHIFT; 261 | 262 | return ((u_val & SIGN_BIT) ? (BIAS - t) : (t - BIAS)); 263 | } 264 | 265 | /* A-law to u-law conversion */ 266 | unsigned char 267 | alaw2ulaw( 268 | unsigned char aval) 269 | { 270 | aval &= 0xff; 271 | return ((aval & 0x80) ? (0xFF ^ _a2u[aval ^ 0xD5]) : 272 | (0x7F ^ _a2u[aval ^ 0x55])); 273 | } 274 | 275 | /* u-law to A-law conversion */ 276 | unsigned char 277 | ulaw2alaw( 278 | unsigned char uval) 279 | { 280 | uval &= 0xff; 281 | return ((uval & 0x80) ? (0xD5 ^ (_u2a[0xFF ^ uval] - 1)) : 282 | (0x55 ^ (_u2a[0x7F ^ uval] - 1))); 283 | } 284 | -------------------------------------------------------------------------------- /G711_G721_G723/g72x.c: -------------------------------------------------------------------------------- 1 | /* 2 | * This source code is a product of Sun Microsystems, Inc. and is provided 3 | * for unrestricted use. Users may copy or modify this source code without 4 | * charge. 5 | * 6 | * SUN SOURCE CODE IS PROVIDED AS IS WITH NO WARRANTIES OF ANY KIND INCLUDING 7 | * THE WARRANTIES OF DESIGN, MERCHANTIBILITY AND FITNESS FOR A PARTICULAR 8 | * PURPOSE, OR ARISING FROM A COURSE OF DEALING, USAGE OR TRADE PRACTICE. 9 | * 10 | * Sun source code is provided with no support and without any obligation on 11 | * the part of Sun Microsystems, Inc. to assist in its use, correction, 12 | * modification or enhancement. 13 | * 14 | * SUN MICROSYSTEMS, INC. SHALL HAVE NO LIABILITY WITH RESPECT TO THE 15 | * INFRINGEMENT OF COPYRIGHTS, TRADE SECRETS OR ANY PATENTS BY THIS SOFTWARE 16 | * OR ANY PART THEREOF. 17 | * 18 | * In no event will Sun Microsystems, Inc. be liable for any lost revenue 19 | * or profits or other special, indirect and consequential damages, even if 20 | * Sun has been advised of the possibility of such damages. 21 | * 22 | * Sun Microsystems, Inc. 23 | * 2550 Garcia Avenue 24 | * Mountain View, California 94043 25 | */ 26 | 27 | /* 28 | * g72x.c 29 | * 30 | * Common routines for G.721 and G.723 conversions. 31 | */ 32 | 33 | #include "g72x.h" 34 | 35 | static short power2[15] = {1, 2, 4, 8, 0x10, 0x20, 0x40, 0x80, 36 | 0x100, 0x200, 0x400, 0x800, 0x1000, 0x2000, 0x4000}; 37 | 38 | /* 39 | * quan() 40 | * 41 | * quantizes the input val against the table of size short integers. 42 | * It returns i if table[i - 1] <= val < table[i]. 43 | * 44 | * Using linear search for simple coding. 45 | */ 46 | static int 47 | quan( 48 | int val, 49 | short *table, 50 | int size) 51 | { 52 | int i; 53 | 54 | for (i = 0; i < size; i++) 55 | if (val < *table++) 56 | break; 57 | return (i); 58 | } 59 | 60 | /* 61 | * fmult() 62 | * 63 | * returns the integer product of the 14-bit integer "an" and 64 | * "floating point" representation (4-bit exponent, 6-bit mantessa) "srn". 65 | */ 66 | static int 67 | fmult( 68 | int an, 69 | int srn) 70 | { 71 | short anmag, anexp, anmant; 72 | short wanexp, wanmag, wanmant; 73 | short retval; 74 | 75 | anmag = (an > 0) ? an : ((-an) & 0x1FFF); 76 | anexp = quan(anmag, power2, 15) - 6; 77 | anmant = (anmag == 0) ? 32 : 78 | (anexp >= 0) ? anmag >> anexp : anmag << -anexp; 79 | wanexp = anexp + ((srn >> 6) & 0xF) - 13; 80 | 81 | wanmant = (anmant * (srn & 077) + 0x30) >> 4; 82 | retval = (wanexp >= 0) ? ((wanmant << wanexp) & 0x7FFF) : 83 | (wanmant >> -wanexp); 84 | 85 | return (((an ^ srn) < 0) ? -retval : retval); 86 | } 87 | 88 | /* 89 | * g72x_init_state() 90 | * 91 | * This routine initializes and/or resets the g72x_state structure 92 | * pointed to by 'state_ptr'. 93 | * All the initial state values are specified in the CCITT G.721 document. 94 | */ 95 | void 96 | g72x_init_state( 97 | struct g72x_state *state_ptr) 98 | { 99 | int cnta; 100 | 101 | state_ptr->yl = 34816; 102 | state_ptr->yu = 544; 103 | state_ptr->dms = 0; 104 | state_ptr->dml = 0; 105 | state_ptr->ap = 0; 106 | for (cnta = 0; cnta < 2; cnta++) { 107 | state_ptr->a[cnta] = 0; 108 | state_ptr->pk[cnta] = 0; 109 | state_ptr->sr[cnta] = 32; 110 | } 111 | for (cnta = 0; cnta < 6; cnta++) { 112 | state_ptr->b[cnta] = 0; 113 | state_ptr->dq[cnta] = 32; 114 | } 115 | state_ptr->td = 0; 116 | } 117 | 118 | /* 119 | * predictor_zero() 120 | * 121 | * computes the estimated signal from 6-zero predictor. 122 | * 123 | */ 124 | int 125 | predictor_zero( 126 | struct g72x_state *state_ptr) 127 | { 128 | int i; 129 | int sezi; 130 | 131 | sezi = fmult(state_ptr->b[0] >> 2, state_ptr->dq[0]); 132 | for (i = 1; i < 6; i++) /* ACCUM */ 133 | sezi += fmult(state_ptr->b[i] >> 2, state_ptr->dq[i]); 134 | return (sezi); 135 | } 136 | /* 137 | * predictor_pole() 138 | * 139 | * computes the estimated signal from 2-pole predictor. 140 | * 141 | */ 142 | int 143 | predictor_pole( 144 | struct g72x_state *state_ptr) 145 | { 146 | return (fmult(state_ptr->a[1] >> 2, state_ptr->sr[1]) + 147 | fmult(state_ptr->a[0] >> 2, state_ptr->sr[0])); 148 | } 149 | /* 150 | * step_size() 151 | * 152 | * computes the quantization step size of the adaptive quantizer. 153 | * 154 | */ 155 | int 156 | step_size( 157 | struct g72x_state *state_ptr) 158 | { 159 | int y; 160 | int dif; 161 | int al; 162 | 163 | if (state_ptr->ap >= 256) 164 | return (state_ptr->yu); 165 | else { 166 | y = state_ptr->yl >> 6; 167 | dif = state_ptr->yu - y; 168 | al = state_ptr->ap >> 2; 169 | if (dif > 0) 170 | y += (dif * al) >> 6; 171 | else if (dif < 0) 172 | y += (dif * al + 0x3F) >> 6; 173 | return (y); 174 | } 175 | } 176 | 177 | /* 178 | * quantize() 179 | * 180 | * Given a raw sample, 'd', of the difference signal and a 181 | * quantization step size scale factor, 'y', this routine returns the 182 | * ADPCM codeword to which that sample gets quantized. The step 183 | * size scale factor division operation is done in the log base 2 domain 184 | * as a subtraction. 185 | */ 186 | int 187 | quantize( 188 | int d, /* Raw difference signal sample */ 189 | int y, /* Step size multiplier */ 190 | short *table, /* quantization table */ 191 | int size) /* table size of short integers */ 192 | { 193 | short dqm; /* Magnitude of 'd' */ 194 | short exp; /* Integer part of base 2 log of 'd' */ 195 | short mant; /* Fractional part of base 2 log */ 196 | short dl; /* Log of magnitude of 'd' */ 197 | short dln; /* Step size scale factor normalized log */ 198 | int i; 199 | 200 | /* 201 | * LOG 202 | * 203 | * Compute base 2 log of 'd', and store in 'dl'. 204 | */ 205 | dqm = abs(d); 206 | exp = quan(dqm >> 1, power2, 15); 207 | mant = ((dqm << 7) >> exp) & 0x7F; /* Fractional portion. */ 208 | dl = (exp << 7) + mant; 209 | 210 | /* 211 | * SUBTB 212 | * 213 | * "Divide" by step size multiplier. 214 | */ 215 | dln = dl - (y >> 2); 216 | 217 | /* 218 | * QUAN 219 | * 220 | * Obtain codword i for 'd'. 221 | */ 222 | i = quan(dln, table, size); 223 | if (d < 0) /* take 1's complement of i */ 224 | return ((size << 1) + 1 - i); 225 | else if (i == 0) /* take 1's complement of 0 */ 226 | return ((size << 1) + 1); /* new in 1988 */ 227 | else 228 | return (i); 229 | } 230 | /* 231 | * reconstruct() 232 | * 233 | * Returns reconstructed difference signal 'dq' obtained from 234 | * codeword 'i' and quantization step size scale factor 'y'. 235 | * Multiplication is performed in log base 2 domain as addition. 236 | */ 237 | int 238 | reconstruct( 239 | int sign, /* 0 for non-negative value */ 240 | int dqln, /* G.72x codeword */ 241 | int y) /* Step size multiplier */ 242 | { 243 | short dql; /* Log of 'dq' magnitude */ 244 | short dex; /* Integer part of log */ 245 | short dqt; 246 | short dq; /* Reconstructed difference signal sample */ 247 | 248 | dql = dqln + (y >> 2); /* ADDA */ 249 | 250 | if (dql < 0) { 251 | return ((sign) ? -0x8000 : 0); 252 | } else { /* ANTILOG */ 253 | dex = (dql >> 7) & 15; 254 | dqt = 128 + (dql & 127); 255 | dq = (dqt << 7) >> (14 - dex); 256 | return ((sign) ? (dq - 0x8000) : dq); 257 | } 258 | } 259 | 260 | 261 | /* 262 | * update() 263 | * 264 | * updates the state variables for each output code 265 | */ 266 | void 267 | update( 268 | int code_size, /* distinguish 723_40 with others */ 269 | int y, /* quantizer step size */ 270 | int wi, /* scale factor multiplier */ 271 | int fi, /* for long/short term energies */ 272 | int dq, /* quantized prediction difference */ 273 | int sr, /* reconstructed signal */ 274 | int dqsez, /* difference from 2-pole predictor */ 275 | struct g72x_state *state_ptr) /* coder state pointer */ 276 | { 277 | int cnt; 278 | short mag, exp, mant; /* Adaptive predictor, FLOAT A */ 279 | short a2p; /* LIMC */ 280 | short a1ul; /* UPA1 */ 281 | short ua2, pks1; /* UPA2 */ 282 | short uga2a, fa1; 283 | short uga2b; 284 | char tr; /* tone/transition detector */ 285 | short ylint, thr2, dqthr; 286 | short ylfrac, thr1; 287 | short pk0; 288 | 289 | pk0 = (dqsez < 0) ? 1 : 0; /* needed in updating predictor poles */ 290 | 291 | mag = dq & 0x7FFF; /* prediction difference magnitude */ 292 | /* TRANS */ 293 | ylint = state_ptr->yl >> 15; /* exponent part of yl */ 294 | ylfrac = (state_ptr->yl >> 10) & 0x1F; /* fractional part of yl */ 295 | thr1 = (32 + ylfrac) << ylint; /* threshold */ 296 | thr2 = (ylint > 9) ? 31 << 10 : thr1; /* limit thr2 to 31 << 10 */ 297 | dqthr = (thr2 + (thr2 >> 1)) >> 1; /* dqthr = 0.75 * thr2 */ 298 | if (state_ptr->td == 0) /* signal supposed voice */ 299 | tr = 0; 300 | else if (mag <= dqthr) /* supposed data, but small mag */ 301 | tr = 0; /* treated as voice */ 302 | else /* signal is data (modem) */ 303 | tr = 1; 304 | 305 | /* 306 | * Quantizer scale factor adaptation. 307 | */ 308 | 309 | /* FUNCTW & FILTD & DELAY */ 310 | /* update non-steady state step size multiplier */ 311 | state_ptr->yu = y + ((wi - y) >> 5); 312 | 313 | /* LIMB */ 314 | if (state_ptr->yu < 544) /* 544 <= yu <= 5120 */ 315 | state_ptr->yu = 544; 316 | else if (state_ptr->yu > 5120) 317 | state_ptr->yu = 5120; 318 | 319 | /* FILTE & DELAY */ 320 | /* update steady state step size multiplier */ 321 | state_ptr->yl += state_ptr->yu + ((-state_ptr->yl) >> 6); 322 | 323 | /* 324 | * Adaptive predictor coefficients. 325 | */ 326 | if (tr == 1) { /* reset a's and b's for modem signal */ 327 | state_ptr->a[0] = 0; 328 | state_ptr->a[1] = 0; 329 | state_ptr->b[0] = 0; 330 | state_ptr->b[1] = 0; 331 | state_ptr->b[2] = 0; 332 | state_ptr->b[3] = 0; 333 | state_ptr->b[4] = 0; 334 | state_ptr->b[5] = 0; 335 | } else { /* update a's and b's */ 336 | pks1 = pk0 ^ state_ptr->pk[0]; /* UPA2 */ 337 | 338 | /* update predictor pole a[1] */ 339 | a2p = state_ptr->a[1] - (state_ptr->a[1] >> 7); 340 | if (dqsez != 0) { 341 | fa1 = (pks1) ? state_ptr->a[0] : -state_ptr->a[0]; 342 | if (fa1 < -8191) /* a2p = function of fa1 */ 343 | a2p -= 0x100; 344 | else if (fa1 > 8191) 345 | a2p += 0xFF; 346 | else 347 | a2p += fa1 >> 5; 348 | 349 | if (pk0 ^ state_ptr->pk[1]) 350 | /* LIMC */ 351 | if (a2p <= -12160) 352 | a2p = -12288; 353 | else if (a2p >= 12416) 354 | a2p = 12288; 355 | else 356 | a2p -= 0x80; 357 | else if (a2p <= -12416) 358 | a2p = -12288; 359 | else if (a2p >= 12160) 360 | a2p = 12288; 361 | else 362 | a2p += 0x80; 363 | } 364 | 365 | /* TRIGB & DELAY */ 366 | state_ptr->a[1] = a2p; 367 | 368 | /* UPA1 */ 369 | /* update predictor pole a[0] */ 370 | state_ptr->a[0] -= state_ptr->a[0] >> 8; 371 | if (dqsez != 0) 372 | if (pks1 == 0) 373 | state_ptr->a[0] += 192; 374 | else 375 | state_ptr->a[0] -= 192; 376 | 377 | /* LIMD */ 378 | a1ul = 15360 - a2p; 379 | if (state_ptr->a[0] < -a1ul) 380 | state_ptr->a[0] = -a1ul; 381 | else if (state_ptr->a[0] > a1ul) 382 | state_ptr->a[0] = a1ul; 383 | 384 | /* UPB : update predictor zeros b[6] */ 385 | for (cnt = 0; cnt < 6; cnt++) { 386 | if (code_size == 5) /* for 40Kbps G.723 */ 387 | state_ptr->b[cnt] -= state_ptr->b[cnt] >> 9; 388 | else /* for G.721 and 24Kbps G.723 */ 389 | state_ptr->b[cnt] -= state_ptr->b[cnt] >> 8; 390 | if (dq & 0x7FFF) { /* XOR */ 391 | if ((dq ^ state_ptr->dq[cnt]) >= 0) 392 | state_ptr->b[cnt] += 128; 393 | else 394 | state_ptr->b[cnt] -= 128; 395 | } 396 | } 397 | } 398 | 399 | for (cnt = 5; cnt > 0; cnt--) 400 | state_ptr->dq[cnt] = state_ptr->dq[cnt-1]; 401 | /* FLOAT A : convert dq[0] to 4-bit exp, 6-bit mantissa f.p. */ 402 | if (mag == 0) { 403 | state_ptr->dq[0] = (dq >= 0) ? 0x20 : 0xFC20; 404 | } else { 405 | exp = quan(mag, power2, 15); 406 | state_ptr->dq[0] = (dq >= 0) ? 407 | (exp << 6) + ((mag << 6) >> exp) : 408 | (exp << 6) + ((mag << 6) >> exp) - 0x400; 409 | } 410 | 411 | state_ptr->sr[1] = state_ptr->sr[0]; 412 | /* FLOAT B : convert sr to 4-bit exp., 6-bit mantissa f.p. */ 413 | if (sr == 0) { 414 | state_ptr->sr[0] = 0x20; 415 | } else if (sr > 0) { 416 | exp = quan(sr, power2, 15); 417 | state_ptr->sr[0] = (exp << 6) + ((sr << 6) >> exp); 418 | } else if (sr > -32768) { 419 | mag = -sr; 420 | exp = quan(mag, power2, 15); 421 | state_ptr->sr[0] = (exp << 6) + ((mag << 6) >> exp) - 0x400; 422 | } else 423 | state_ptr->sr[0] = 0xFC20; 424 | 425 | /* DELAY A */ 426 | state_ptr->pk[1] = state_ptr->pk[0]; 427 | state_ptr->pk[0] = pk0; 428 | 429 | /* TONE */ 430 | if (tr == 1) /* this sample has been treated as data */ 431 | state_ptr->td = 0; /* next one will be treated as voice */ 432 | else if (a2p < -11776) /* small sample-to-sample correlation */ 433 | state_ptr->td = 1; /* signal may be data */ 434 | else /* signal is voice */ 435 | state_ptr->td = 0; 436 | 437 | /* 438 | * Adaptation speed control. 439 | */ 440 | state_ptr->dms += (fi - state_ptr->dms) >> 5; /* FILTA */ 441 | state_ptr->dml += (((fi << 2) - state_ptr->dml) >> 7); /* FILTB */ 442 | 443 | if (tr == 1) 444 | state_ptr->ap = 256; 445 | else if (y < 1536) /* SUBTC */ 446 | state_ptr->ap += (0x200 - state_ptr->ap) >> 4; 447 | else if (state_ptr->td == 1) 448 | state_ptr->ap += (0x200 - state_ptr->ap) >> 4; 449 | else if (abs((state_ptr->dms << 2) - state_ptr->dml) >= 450 | (state_ptr->dml >> 3)) 451 | state_ptr->ap += (0x200 - state_ptr->ap) >> 4; 452 | else 453 | state_ptr->ap += (-state_ptr->ap) >> 4; 454 | } 455 | 456 | /* 457 | * tandem_adjust(sr, se, y, i, sign) 458 | * 459 | * At the end of ADPCM decoding, it simulates an encoder which may be receiving 460 | * the output of this decoder as a tandem process. If the output of the 461 | * simulated encoder differs from the input to this decoder, the decoder output 462 | * is adjusted by one level of A-law or u-law codes. 463 | * 464 | * Input: 465 | * sr decoder output linear PCM sample, 466 | * se predictor estimate sample, 467 | * y quantizer step size, 468 | * i decoder input code, 469 | * sign sign bit of code i 470 | * 471 | * Return: 472 | * adjusted A-law or u-law compressed sample. 473 | */ 474 | int 475 | tandem_adjust_alaw( 476 | int sr, /* decoder output linear PCM sample */ 477 | int se, /* predictor estimate sample */ 478 | int y, /* quantizer step size */ 479 | int i, /* decoder input code */ 480 | int sign, 481 | short *qtab) 482 | { 483 | unsigned char sp; /* A-law compressed 8-bit code */ 484 | short dx; /* prediction error */ 485 | char id; /* quantized prediction error */ 486 | int sd; /* adjusted A-law decoded sample value */ 487 | int im; /* biased magnitude of i */ 488 | int imx; /* biased magnitude of id */ 489 | 490 | if (sr <= -32768) 491 | sr = -1; 492 | sp = linear2alaw((sr >> 1) << 3); /* short to A-law compression */ 493 | dx = (alaw2linear(sp) >> 2) - se; /* 16-bit prediction error */ 494 | id = quantize(dx, y, qtab, sign - 1); 495 | 496 | if (id == i) { /* no adjustment on sp */ 497 | return (sp); 498 | } else { /* sp adjustment needed */ 499 | /* ADPCM codes : 8, 9, ... F, 0, 1, ... , 6, 7 */ 500 | im = i ^ sign; /* 2's complement to biased unsigned */ 501 | imx = id ^ sign; 502 | 503 | if (imx > im) { /* sp adjusted to next lower value */ 504 | if (sp & 0x80) { 505 | sd = (sp == 0xD5) ? 0x55 : 506 | ((sp ^ 0x55) - 1) ^ 0x55; 507 | } else { 508 | sd = (sp == 0x2A) ? 0x2A : 509 | ((sp ^ 0x55) + 1) ^ 0x55; 510 | } 511 | } else { /* sp adjusted to next higher value */ 512 | if (sp & 0x80) 513 | sd = (sp == 0xAA) ? 0xAA : 514 | ((sp ^ 0x55) + 1) ^ 0x55; 515 | else 516 | sd = (sp == 0x55) ? 0xD5 : 517 | ((sp ^ 0x55) - 1) ^ 0x55; 518 | } 519 | return (sd); 520 | } 521 | } 522 | 523 | int 524 | tandem_adjust_ulaw( 525 | int sr, /* decoder output linear PCM sample */ 526 | int se, /* predictor estimate sample */ 527 | int y, /* quantizer step size */ 528 | int i, /* decoder input code */ 529 | int sign, 530 | short *qtab) 531 | { 532 | unsigned char sp; /* u-law compressed 8-bit code */ 533 | short dx; /* prediction error */ 534 | char id; /* quantized prediction error */ 535 | int sd; /* adjusted u-law decoded sample value */ 536 | int im; /* biased magnitude of i */ 537 | int imx; /* biased magnitude of id */ 538 | 539 | if (sr <= -32768) 540 | sr = 0; 541 | sp = linear2ulaw(sr << 2); /* short to u-law compression */ 542 | dx = (ulaw2linear(sp) >> 2) - se; /* 16-bit prediction error */ 543 | id = quantize(dx, y, qtab, sign - 1); 544 | if (id == i) { 545 | return (sp); 546 | } else { 547 | /* ADPCM codes : 8, 9, ... F, 0, 1, ... , 6, 7 */ 548 | im = i ^ sign; /* 2's complement to biased unsigned */ 549 | imx = id ^ sign; 550 | if (imx > im) { /* sp adjusted to next lower value */ 551 | if (sp & 0x80) 552 | sd = (sp == 0xFF) ? 0x7E : sp + 1; 553 | else 554 | sd = (sp == 0) ? 0 : sp - 1; 555 | 556 | } else { /* sp adjusted to next higher value */ 557 | if (sp & 0x80) 558 | sd = (sp == 0x80) ? 0x80 : sp - 1; 559 | else 560 | sd = (sp == 0x7F) ? 0xFE : sp + 1; 561 | } 562 | return (sd); 563 | } 564 | } 565 | --------------------------------------------------------------------------------