diff options
Diffstat (limited to 'main/alaw.c')
-rw-r--r-- | main/alaw.c | 64 |
1 files changed, 33 insertions, 31 deletions
diff --git a/main/alaw.c b/main/alaw.c index 8d3595206..367de0b77 100644 --- a/main/alaw.c +++ b/main/alaw.c @@ -34,31 +34,32 @@ ASTERISK_FILE_VERSION(__FILE__, "$Revision$") static inline unsigned char linear2alaw(short int linear) { - int mask; - int seg; - int pcm_val; - static int seg_end[8] = + int mask; + int seg; + int pcm_val; + static int seg_end[8] = { 0xFF, 0x1FF, 0x3FF, 0x7FF, 0xFFF, 0x1FFF, 0x3FFF, 0x7FFF }; - - pcm_val = linear; - if (pcm_val >= 0) { - /* Sign (7th) bit = 1 */ - mask = AMI_MASK | 0x80; - } else { - /* Sign bit = 0 */ - mask = AMI_MASK; - pcm_val = -pcm_val; - } - - /* Convert the scaled magnitude to segment number. */ - for (seg = 0; seg < 8; seg++) { - if (pcm_val <= seg_end[seg]) - break; - } - /* Combine the sign, segment, and quantization bits. */ - return ((seg << 4) | ((pcm_val >> ((seg) ? (seg + 3) : 4)) & 0x0F)) ^ mask; + + pcm_val = linear; + if (pcm_val >= 0) { + /* Sign (7th) bit = 1 */ + mask = AMI_MASK | 0x80; + } else { + /* Sign bit = 0 */ + mask = AMI_MASK; + pcm_val = -pcm_val; + } + + /* Convert the scaled magnitude to segment number. */ + for (seg = 0; seg < 8; seg++) { + if (pcm_val <= seg_end[seg]) { + break; + } + } + /* Combine the sign, segment, and quantization bits. */ + return ((seg << 4) | ((pcm_val >> ((seg) ? (seg + 3) : 4)) & 0x0F)) ^ mask; } #else static unsigned char linear2alaw(short sample, int full_coding) @@ -107,15 +108,16 @@ static unsigned char linear2alaw(short sample, int full_coding) #ifndef G711_NEW_ALGORITHM static inline short int alaw2linear (unsigned char alaw) { - int i; - int seg; - - alaw ^= AMI_MASK; - i = ((alaw & 0x0F) << 4) + 8 /* rounding error */; - seg = (((int) alaw & 0x70) >> 4); - if (seg) - i = (i + 0x100) << (seg - 1); - return (short int) ((alaw & 0x80) ? i : -i); + int i; + int seg; + + alaw ^= AMI_MASK; + i = ((alaw & 0x0F) << 4) + 8 /* rounding error */; + seg = (((int) alaw & 0x70) >> 4); + if (seg) { + i = (i + 0x100) << (seg - 1); + } + return (short int) ((alaw & 0x80) ? i : -i); } #else static inline short alaw2linear(unsigned char alawbyte) |