G711.cpp
Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00032 #include "common.h"
00033 #include "G711.h"
00034
00035
00036
00037
00038
00039
00040
00041 uint8_t G711::ALawEncode(int16_t pcm16)
00042 {
00043 int p = pcm16;
00044 unsigned a;
00045 if(p<0)
00046 {
00047
00048
00049
00050 p = ~p;
00051 a = 0x00;
00052 }
00053 else
00054 {
00055
00056 a = 0x80;
00057 }
00058
00059
00060 p >>= 4;
00061 if(p>=0x20)
00062 {
00063 if(p>=0x100)
00064 {
00065 p >>= 4;
00066 a += 0x40;
00067 }
00068 if(p>=0x40)
00069 {
00070 p >>= 2;
00071 a += 0x20;
00072 }
00073 if(p>=0x20)
00074 {
00075 p >>= 1;
00076 a += 0x10;
00077 }
00078 }
00079
00080
00081 a += p;
00082
00083 return a^0x55;
00084 }
00085
00086
00087 int G711::ALawDecode(uint8_t alaw)
00088 {
00089 alaw ^= 0x55;
00090
00091 unsigned sign = alaw&0x80;
00092
00093 int linear = alaw&0x1f;
00094 linear <<= 4;
00095 linear += 8;
00096
00097 alaw &= 0x7f;
00098 if(alaw>=0x20)
00099 {
00100 linear |= 0x100;
00101 unsigned shift = (alaw>>4)-1;
00102 linear <<= shift;
00103 }
00104
00105 if(!sign)
00106 return -linear;
00107 else
00108 return linear;
00109 }
00110
00111
00112 uint8_t G711::ULawEncode(int16_t pcm16)
00113 {
00114 int p = pcm16;
00115 unsigned u;
00116
00117 if(p<0)
00118 {
00119
00120
00121
00122 p = ~p;
00123 u = 0x80^0x10^0xff;
00124 }
00125 else
00126 {
00127
00128 u = 0x00^0x10^0xff;
00129 }
00130
00131 p += 0x84;
00132
00133 if(p>0x7f00)
00134 p = 0x7f00;
00135
00136
00137 p >>= 3;
00138 if(p>=0x100)
00139 {
00140 p >>= 4;
00141 u ^= 0x40;
00142 }
00143 if(p>=0x40)
00144 {
00145 p >>= 2;
00146 u ^= 0x20;
00147 }
00148 if(p>=0x20)
00149 {
00150 p >>= 1;
00151 u ^= 0x10;
00152 }
00153
00154
00155 u ^= p;
00156
00157 return u;
00158 }
00159
00160
00161 int G711::ULawDecode(uint8_t ulaw)
00162 {
00163 ulaw ^= 0xff;
00164
00165 int linear = ulaw&0x0f;
00166 linear <<= 3;
00167 linear |= 0x84;
00168
00169 unsigned shift = ulaw>>4;
00170 shift &= 7;
00171 linear <<= shift;
00172
00173 linear -= 0x84;
00174
00175 if(ulaw&0x80)
00176 return -linear;
00177 else
00178 return linear;
00179 }
00180
00181
00182 uint8_t G711::ALawToULaw(uint8_t alaw)
00183 {
00184 uint8_t sign=alaw&0x80;
00185 alaw ^= sign;
00186 alaw ^= 0x55;
00187 unsigned ulaw;
00188 if(alaw<45)
00189 {
00190 if(alaw<24)
00191 ulaw = (alaw<8) ? (alaw<<1)+1 : alaw+8;
00192 else
00193 ulaw = (alaw<32) ? (alaw>>1)+20 : alaw+4;
00194 }
00195 else
00196 {
00197 if(alaw<63)
00198 ulaw = (alaw<47) ? alaw+3 : alaw+2;
00199 else
00200 ulaw = (alaw<79) ? alaw+1 : alaw;
00201 }
00202 ulaw ^= sign;
00203 return ulaw^0x7f;
00204 }
00205
00206
00207 uint8_t G711::ULawToALaw(uint8_t ulaw)
00208 {
00209 uint8_t sign=ulaw&0x80;
00210 ulaw ^= sign;
00211 ulaw ^= 0x7f;
00212 unsigned alaw;
00213 if(ulaw<48)
00214 {
00215 if(ulaw<=32)
00216 alaw = (ulaw<=15) ? ulaw>>1 : ulaw-8;
00217 else
00218 alaw = (ulaw<=35) ? (ulaw<<1)-40 : ulaw-4;
00219 }
00220 else
00221 {
00222 if(ulaw<=63)
00223 alaw = (ulaw==48) ? ulaw-3 : ulaw-2;
00224 else
00225 alaw = (ulaw<=79) ? ulaw-1 : ulaw;
00226 }
00227 alaw ^= sign;
00228 return alaw^0x55;
00229 }
00230
00231
00232 unsigned G711::ALawEncode(uint8_t* dst, int16_t* src, size_t srcSize)
00233 {
00234 srcSize >>= 1;
00235 uint8_t* end = dst+srcSize;
00236 while(dst<end)
00237 *dst++ = ALawEncode(*src++);
00238 return srcSize;
00239 }
00240
00241
00242 unsigned G711::ALawDecode(int16_t* dst, const uint8_t* src, size_t srcSize)
00243 {
00244 int16_t* end = dst+srcSize;
00245 while(dst<end)
00246 *dst++ = ALawDecode(*src++);
00247 return srcSize<<1;
00248 }
00249
00250
00251 unsigned G711::ULawEncode(uint8_t* dst, int16_t* src, size_t srcSize)
00252 {
00253 srcSize >>= 1;
00254 uint8_t* end = dst+srcSize;
00255 while(dst<end)
00256 *dst++ = ULawEncode(*src++);
00257 return srcSize;
00258 }
00259
00260
00261 unsigned G711::ULawDecode(int16_t* dst, const uint8_t* src, size_t srcSize)
00262 {
00263 int16_t* end = dst+srcSize;
00264 while(dst<end)
00265 *dst++ = ULawDecode(*src++);
00266 return srcSize<<1;
00267 }
00268
00269
00270 unsigned G711::ALawToULaw(uint8_t* dst, const uint8_t* src, size_t srcSize)
00271 {
00272 uint8_t* end = dst+srcSize;
00273 while(dst<end)
00274 *dst++ = ALawToULaw(*src++);
00275 return srcSize;
00276 }
00277
00278
00279 unsigned G711::ULawToALaw(uint8_t* dst, const uint8_t* src, size_t srcSize)
00280 {
00281 uint8_t* end = dst+srcSize;
00282 while(dst<end)
00283 *dst++ = ULawToALaw(*src++);
00284 return srcSize;
00285 }