00001
00026 #include <stdio.h>
00027 #include <stdlib.h>
00028 #include <unistd.h>
00029 #include <string.h>
00030 #include <errno.h>
00031 #include "alaw.h"
00032
00033 #define SIGN_BIT (0x80)
00034 #define QUANT_MASK (0xf)
00035 #define NSEGS (8)
00036 #define SEG_SHIFT (4)
00037 #define SEG_MASK (0x70)
00038
00039 static const short alaw_seg_end[8] = {0xFF, 0x1FF, 0x3FF, 0x7FF,
00040 0xFFF, 0x1FFF, 0x3FFF, 0x7FFF};
00041
00042 inline int Alaw::search(int val, const short *table, int size)
00043 {
00044 int i;
00045
00046 for (i = 0; i < size; i++) {
00047 if (val <= *table++)
00048 return (i);
00049 }
00050 return (size);
00051 }
00052
00053
00054
00055
00056
00057
00058
00059
00060
00061
00062
00063
00064
00065
00066
00067
00068
00069
00070
00071
00072 inline unsigned char Alaw::linear2alaw(int pcm_val)
00073 {
00074 int mask;
00075 int seg;
00076 unsigned char aval;
00077
00078 if (pcm_val >= 0) {
00079 mask = 0xD5;
00080 } else {
00081 mask = 0x55;
00082 pcm_val = -pcm_val - 8;
00083 }
00084
00085
00086 seg = search(pcm_val, alaw_seg_end, NSEGS);
00087
00088
00089
00090 if (seg >= 8)
00091 return (unsigned char)(0x7F ^ mask);
00092 else {
00093 aval = (unsigned char)(seg << SEG_SHIFT);
00094 if (seg < 2)
00095 aval |= (pcm_val >> 4) & QUANT_MASK;
00096 else
00097 aval |= (pcm_val >> (seg + 3)) & QUANT_MASK;
00098 return (unsigned char)(aval ^ mask);
00099 }
00100 }
00101
00102
00103 void Alaw::conv_u8bit_alaw(unsigned char *src_ptr, unsigned char *dst_ptr, size_t size)
00104 {
00105 unsigned int pcm;
00106
00107 while (size-- > 0) {
00108 pcm = ((*src_ptr++) ^ 0x80) << 8;
00109 *dst_ptr++ = linear2alaw((signed short)(pcm));
00110 }
00111 }
00112
00113 void Alaw::conv_s8bit_alaw(unsigned char *src_ptr, unsigned char *dst_ptr, size_t size)
00114 {
00115 unsigned int pcm;
00116
00117 while (size-- > 0) {
00118 pcm = *src_ptr++ << 8;
00119 *dst_ptr++ = Alaw::linear2alaw((signed short)(pcm));
00120 }
00121 }
00122
00123 void Alaw::conv_s16bit_alaw(unsigned short *src_ptr, unsigned char *dst_ptr, size_t size)
00124 {
00125 while (size-- > 0)
00126 *dst_ptr++ = Alaw::linear2alaw((signed short)(*src_ptr++));
00127 }
00128
00129
00130
00131
00132
00133
00134
00135
00136
00137 void Alaw::conv_u16bit_alaw(unsigned short *src_ptr, unsigned char *dst_ptr, size_t size)
00138 {
00139 while (size-- > 0)
00140 *dst_ptr++ = Alaw::linear2alaw((signed short)((*src_ptr++) ^ 0x8000));
00141 }
00142
00143
00144
00145
00146
00147
00148
00149
00150
00151
00152
00153
00154
00155
00156
00157
00158
00159
00160
00161
00162
00163
00164
00165
00166
00167
00168
00169
00170
00171
00172
00173
00174
00175
00176
00177
00178
00179
00180
00181
00182
00183
00184
00185
00186
00187
00188
00189