| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
#include "asterisk.h" |
| 31 |
|
| 32 |
#include "asterisk/alaw.h" |
| 33 |
#include "asterisk/logger.h" |
| 34 |
|
| 35 |
#ifndef G711_NEW_ALGORITHM |
| 36 |
#define AMI_MASK 0x55 |
| 37 |
|
| 38 |
static inline unsigned char linear2alaw(short int linear) |
| 39 |
{ |
| 40 |
int mask; |
| 41 |
int seg; |
| 42 |
int pcm_val; |
| 43 |
static int seg_end[8] = |
| 44 |
{ |
| 45 |
0xFF, 0x1FF, 0x3FF, 0x7FF, 0xFFF, 0x1FFF, 0x3FFF, 0x7FFF |
| 46 |
}; |
| 47 |
|
| 48 |
pcm_val = linear; |
| 49 |
if (pcm_val >= 0) { |
| 50 |
|
| 51 |
mask = AMI_MASK | 0x80; |
| 52 |
} else { |
| 53 |
|
| 54 |
mask = AMI_MASK; |
| 55 |
pcm_val = -pcm_val; |
| 56 |
} |
| 57 |
|
| 58 |
|
| 59 |
for (seg = 0; seg < 8; seg++) { |
| 60 |
if (pcm_val <= seg_end[seg]) { |
| 61 |
break; |
| 62 |
} |
| 63 |
} |
| 64 |
|
| 65 |
return ((seg << 4) | ((pcm_val >> ((seg) ? (seg + 3) : 4)) & 0x0F)) ^ mask; |
| 66 |
} |
| 67 |
#else |
| 68 |
static unsigned char linear2alaw(short sample, int full_coding) |
| 69 |
{ |
| 70 |
static const unsigned exp_lut[128] = { |
| 71 |
1,1,2,2,3,3,3,3, |
| 72 |
4,4,4,4,4,4,4,4, |
| 73 |
5,5,5,5,5,5,5,5, |
| 74 |
5,5,5,5,5,5,5,5, |
| 75 |
6,6,6,6,6,6,6,6, |
| 76 |
6,6,6,6,6,6,6,6, |
| 77 |
6,6,6,6,6,6,6,6, |
| 78 |
6,6,6,6,6,6,6,6, |
| 79 |
7,7,7,7,7,7,7,7, |
| 80 |
7,7,7,7,7,7,7,7, |
| 81 |
7,7,7,7,7,7,7,7, |
| 82 |
7,7,7,7,7,7,7,7, |
| 83 |
7,7,7,7,7,7,7,7, |
| 84 |
7,7,7,7,7,7,7,7, |
| 85 |
7,7,7,7,7,7,7,7, |
| 86 |
7,7,7,7,7,7,7,7 }; |
| 87 |
unsigned sign, exponent, mantissa, mag; |
| 88 |
unsigned char alawbyte; |
| 89 |
|
| 90 |
ast_alaw_get_sign_mag(sample, &sign, &mag); |
| 91 |
if (mag > 32767) |
| 92 |
mag = 32767; |
| 93 |
|
| 94 |
exponent = exp_lut[(mag >> 8) & 0x7f]; |
| 95 |
mantissa = (mag >> (exponent + 3)) & 0x0f; |
| 96 |
if (mag < 0x100) |
| 97 |
exponent = 0; |
| 98 |
|
| 99 |
if (full_coding) { |
| 100 |
|
| 101 |
alawbyte = (unsigned char)(sign | (exponent << 4) | mantissa); |
| 102 |
alawbyte ^= AST_ALAW_AMI_MASK; |
| 103 |
} else { |
| 104 |
|
| 105 |
alawbyte = (exponent << 4) | mantissa; |
| 106 |
} |
| 107 |
return alawbyte; |
| 108 |
} |
| 109 |
#endif |
| 110 |
|
| 111 |
#ifndef G711_NEW_ALGORITHM |
| 112 |
static inline short int alaw2linear (unsigned char alaw) |
| 113 |
{ |
| 114 |
int i; |
| 115 |
int seg; |
| 116 |
|
| 117 |
alaw ^= AMI_MASK; |
| 118 |
i = ((alaw & 0x0F) << 4) + 8 ; |
| 119 |
seg = (((int) alaw & 0x70) >> 4); |
| 120 |
if (seg) { |
| 121 |
i = (i + 0x100) << (seg - 1); |
| 122 |
} |
| 123 |
return (short int) ((alaw & 0x80) ? i : -i); |
| 124 |
} |
| 125 |
#else |
| 126 |
static inline short alaw2linear(unsigned char alawbyte) |
| 127 |
{ |
| 128 |
unsigned exponent, mantissa; |
| 129 |
short sample; |
| 130 |
|
| 131 |
alawbyte ^= AST_ALAW_AMI_MASK; |
| 132 |
exponent = (alawbyte & 0x70) >> 4; |
| 133 |
mantissa = alawbyte & 0x0f; |
| 134 |
sample = (mantissa << 4) + 8 ; |
| 135 |
if (exponent) |
| 136 |
sample = (sample + 0x100) << (exponent - 1); |
| 137 |
if (!(alawbyte & 0x80)) |
| 138 |
sample = -sample; |
| 139 |
return sample; |
| 140 |
} |
| 141 |
#endif |
| 142 |
|
| 143 |
|
| 144 |
|
| 145 |
#ifndef G711_NEW_ALGORITHM |
| 146 |
unsigned char __ast_lin2a[8192]; |
| 147 |
#else |
| 148 |
unsigned char __ast_lin2a[AST_ALAW_TAB_SIZE]; |
| 149 |
#endif |
| 150 |
short __ast_alaw[256]; |
| 151 |
|
| 152 |
void ast_alaw_init(void) |
| 153 |
{ |
| 154 |
int i; |
| 155 |
|
| 156 |
|
| 157 |
|
| 158 |
#ifndef G711_NEW_ALGORITHM |
| 159 |
for (i = 0; i < 256; i++) { |
| 160 |
__ast_alaw[i] = alaw2linear(i); |
| 161 |
} |
| 162 |
|
| 163 |
for (i = -32768; i < 32768; i++) { |
| 164 |
__ast_lin2a[((unsigned short)i) >> 3] = linear2alaw(i); |
| 165 |
} |
| 166 |
#else |
| 167 |
for (i = 0; i < 256; i++) { |
| 168 |
__ast_alaw[i] = alaw2linear(i); |
| 169 |
} |
| 170 |
|
| 171 |
for (i = 0; i <= 32768; i += AST_ALAW_STEP) { |
| 172 |
AST_LIN2A_LOOKUP(i) = linear2alaw(i, 0 ); |
| 173 |
} |
| 174 |
#endif |
| 175 |
|
| 176 |
#ifdef TEST_CODING_TABLES |
| 177 |
for (i = -32768; i < 32768; ++i) { |
| 178 |
#ifndef G711_NEW_ALGORITHM |
| 179 |
unsigned char e1 = linear2alaw(i); |
| 180 |
#else |
| 181 |
unsigned char e1 = linear2alaw(i, 1); |
| 182 |
#endif |
| 183 |
short d1 = alaw2linear(e1); |
| 184 |
unsigned char e2 = AST_LIN2A(i); |
| 185 |
short d2 = alaw2linear(e2); |
| 186 |
short d3 = AST_ALAW(e1); |
| 187 |
|
| 188 |
if (e1 != e2 || d1 != d3 || d2 != d3) { |
| 189 |
ast_log(LOG_WARNING, "a-Law coding tables test failed on %d: e1=%u, e2=%u, d1=%d, d2=%d\n", |
| 190 |
i, (unsigned)e1, (unsigned)e2, (int)d1, (int)d2); |
| 191 |
} |
| 192 |
} |
| 193 |
ast_log(LOG_NOTICE, "a-Law coding tables test complete.\n"); |
| 194 |
#endif |
| 195 |
|
| 196 |
#ifdef TEST_TANDEM_TRANSCODING |
| 197 |
|
| 198 |
for (i = -32768; i < 32768; ++i) { |
| 199 |
unsigned char e1 = AST_LIN2A(i); |
| 200 |
short d1 = AST_ALAW(e1); |
| 201 |
unsigned char e2 = AST_LIN2A(d1); |
| 202 |
short d2 = AST_ALAW(e2); |
| 203 |
unsigned char e3 = AST_LIN2A(d2); |
| 204 |
short d3 = AST_ALAW(e3); |
| 205 |
|
| 206 |
if (e1 != e2 || e2 != e3 || d1 != d2 || d2 != d3) { |
| 207 |
ast_log(LOG_WARNING, "a-Law tandem transcoding test failed on %d: e1=%u, e2=%u, d1=%d, d2=%d, d3=%d\n", |
| 208 |
i, (unsigned)e1, (unsigned)e2, (int)d1, (int)d2, (int)d3); |
| 209 |
} |
| 210 |
} |
| 211 |
ast_log(LOG_NOTICE, "a-Law tandem transcoding test complete.\n"); |
| 212 |
#endif |
| 213 |
|
| 214 |
} |