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
00026
00027
00028
00029
00030 #include "asterisk.h"
00031
00032 ASTERISK_FILE_VERSION(__FILE__, "$Revision: 267539 $")
00033
00034 #include <fcntl.h>
00035 #include <netinet/in.h>
00036 #include <stdio.h>
00037 #include <stdlib.h>
00038 #include <string.h>
00039 #include <unistd.h>
00040
00041 #include "asterisk/lock.h"
00042 #include "asterisk/logger.h"
00043 #include "asterisk/linkedlists.h"
00044 #include "asterisk/module.h"
00045 #include "asterisk/config.h"
00046 #include "asterisk/options.h"
00047 #include "asterisk/translate.h"
00048 #include "asterisk/channel.h"
00049 #include "asterisk/utils.h"
00050
00051
00052
00053
00054 #define BUFFER_SAMPLES 8096
00055
00056
00057
00058 #include "slin_adpcm_ex.h"
00059 #include "adpcm_slin_ex.h"
00060
00061
00062
00063
00064
00065 static int indsft[8] = { -1, -1, -1, -1, 2, 4, 6, 8 };
00066
00067
00068
00069
00070
00071 static int stpsz[49] = {
00072 16, 17, 19, 21, 23, 25, 28, 31, 34, 37, 41, 45, 50, 55, 60, 66, 73,
00073 80, 88, 97, 107, 118, 130, 143, 157, 173, 190, 209, 230, 253, 279,
00074 307, 337, 371, 408, 449, 494, 544, 598, 658, 724, 796, 876, 963,
00075 1060, 1166, 1282, 1411, 1552
00076 };
00077
00078
00079
00080
00081
00082 struct adpcm_state {
00083 int ssindex;
00084 int signal;
00085 int zero_count;
00086 int next_flag;
00087 };
00088
00089
00090
00091
00092
00093
00094
00095
00096
00097
00098
00099
00100 static inline short decode(int encoded, struct adpcm_state *state)
00101 {
00102 int diff;
00103 int step;
00104 int sign;
00105
00106 step = stpsz[state->ssindex];
00107
00108 sign = encoded & 0x08;
00109 encoded &= 0x07;
00110 #ifdef NOT_BLI
00111 diff = (((encoded << 1) + 1) * step) >> 3;
00112 #else
00113 diff = step >> 3;
00114 if (encoded & 4)
00115 diff += step;
00116 if (encoded & 2)
00117 diff += step >> 1;
00118 if (encoded & 1)
00119 diff += step >> 2;
00120 if ((encoded >> 1) & step & 0x1)
00121 diff++;
00122 #endif
00123 if (sign)
00124 diff = -diff;
00125
00126 if (state->next_flag & 0x1)
00127 state->signal -= 8;
00128 else if (state->next_flag & 0x2)
00129 state->signal += 8;
00130
00131 state->signal += diff;
00132
00133 if (state->signal > 2047)
00134 state->signal = 2047;
00135 else if (state->signal < -2047)
00136 state->signal = -2047;
00137
00138 state->next_flag = 0;
00139
00140 #ifdef AUTO_RETURN
00141 if (encoded)
00142 state->zero_count = 0;
00143 else if (++(state->zero_count) == 24) {
00144 state->zero_count = 0;
00145 if (state->signal > 0)
00146 state->next_flag = 0x1;
00147 else if (state->signal < 0)
00148 state->next_flag = 0x2;
00149 }
00150 #endif
00151
00152 state->ssindex += indsft[encoded];
00153 if (state->ssindex < 0)
00154 state->ssindex = 0;
00155 else if (state->ssindex > 48)
00156 state->ssindex = 48;
00157
00158 return state->signal << 4;
00159 }
00160
00161
00162
00163
00164
00165
00166
00167
00168
00169
00170
00171
00172
00173 static inline int adpcm(short csig, struct adpcm_state *state)
00174 {
00175 int diff;
00176 int step;
00177 int encoded;
00178
00179
00180
00181
00182 csig >>= 4;
00183
00184 step = stpsz[state->ssindex];
00185 diff = csig - state->signal;
00186
00187 #ifdef NOT_BLI
00188 if (diff < 0) {
00189 encoded = (-diff << 2) / step;
00190 if (encoded > 7)
00191 encoded = 7;
00192 encoded |= 0x08;
00193 } else {
00194 encoded = (diff << 2) / step;
00195 if (encoded > 7)
00196 encoded = 7;
00197 }
00198 #else
00199 if (diff < 0) {
00200 encoded = 8;
00201 diff = -diff;
00202 } else
00203 encoded = 0;
00204 if (diff >= step) {
00205 encoded |= 4;
00206 diff -= step;
00207 }
00208 step >>= 1;
00209 if (diff >= step) {
00210 encoded |= 2;
00211 diff -= step;
00212 }
00213 step >>= 1;
00214 if (diff >= step)
00215 encoded |= 1;
00216 #endif
00217
00218
00219 decode(encoded, state);
00220
00221 return encoded;
00222 }
00223
00224
00225
00226
00227 struct adpcm_encoder_pvt {
00228 struct adpcm_state state;
00229 int16_t inbuf[BUFFER_SAMPLES];
00230 };
00231
00232
00233 struct adpcm_decoder_pvt {
00234 struct adpcm_state state;
00235 };
00236
00237
00238 static int adpcmtolin_framein(struct ast_trans_pvt *pvt, struct ast_frame *f)
00239 {
00240 struct adpcm_decoder_pvt *tmp = pvt->pvt;
00241 int x = f->datalen;
00242 unsigned char *src = f->data;
00243 int16_t *dst = (int16_t *)pvt->outbuf + pvt->samples;
00244
00245 while (x--) {
00246 *dst++ = decode((*src >> 4) & 0xf, &tmp->state);
00247 *dst++ = decode(*src++ & 0x0f, &tmp->state);
00248 }
00249 pvt->samples += f->samples;
00250 pvt->datalen += 2*f->samples;
00251 return 0;
00252 }
00253
00254
00255 static int lintoadpcm_framein(struct ast_trans_pvt *pvt, struct ast_frame *f)
00256 {
00257 struct adpcm_encoder_pvt *tmp = pvt->pvt;
00258
00259 memcpy(&tmp->inbuf[pvt->samples], f->data, f->datalen);
00260 pvt->samples += f->samples;
00261 return 0;
00262 }
00263
00264
00265 static struct ast_frame *lintoadpcm_frameout(struct ast_trans_pvt *pvt)
00266 {
00267 struct adpcm_encoder_pvt *tmp = pvt->pvt;
00268 struct ast_frame *f;
00269 int i;
00270 int samples = pvt->samples;
00271
00272 if (samples < 2)
00273 return NULL;
00274
00275 pvt->samples &= ~1;
00276
00277 for (i = 0; i < pvt->samples; i += 2) {
00278 pvt->outbuf[i/2] =
00279 (adpcm(tmp->inbuf[i ], &tmp->state) << 4) |
00280 (adpcm(tmp->inbuf[i+1], &tmp->state) );
00281 };
00282
00283 f = ast_trans_frameout(pvt, pvt->samples/2, 0);
00284
00285
00286
00287
00288
00289
00290 if (samples & 1) {
00291 tmp->inbuf[0] = tmp->inbuf[samples - 1];
00292 pvt->samples = 1;
00293 }
00294 return f;
00295 }
00296
00297
00298
00299 static struct ast_frame *adpcmtolin_sample(void)
00300 {
00301 static struct ast_frame f;
00302 f.frametype = AST_FRAME_VOICE;
00303 f.subclass = AST_FORMAT_ADPCM;
00304 f.datalen = sizeof(adpcm_slin_ex);
00305 f.samples = sizeof(adpcm_slin_ex) * 2;
00306 f.mallocd = 0;
00307 f.offset = 0;
00308 f.src = __PRETTY_FUNCTION__;
00309 f.data = adpcm_slin_ex;
00310 return &f;
00311 }
00312
00313
00314 static struct ast_frame *lintoadpcm_sample(void)
00315 {
00316 static struct ast_frame f;
00317 f.frametype = AST_FRAME_VOICE;
00318 f.subclass = AST_FORMAT_SLINEAR;
00319 f.datalen = sizeof(slin_adpcm_ex);
00320
00321 f.samples = sizeof(slin_adpcm_ex) / 2;
00322 f.mallocd = 0;
00323 f.offset = 0;
00324 f.src = __PRETTY_FUNCTION__;
00325 f.data = slin_adpcm_ex;
00326 return &f;
00327 }
00328
00329 static struct ast_translator adpcmtolin = {
00330 .name = "adpcmtolin",
00331 .srcfmt = AST_FORMAT_ADPCM,
00332 .dstfmt = AST_FORMAT_SLINEAR,
00333 .framein = adpcmtolin_framein,
00334 .sample = adpcmtolin_sample,
00335 .desc_size = sizeof(struct adpcm_decoder_pvt),
00336 .buffer_samples = BUFFER_SAMPLES,
00337 .buf_size = BUFFER_SAMPLES * 2,
00338 };
00339
00340 static struct ast_translator lintoadpcm = {
00341 .name = "lintoadpcm",
00342 .srcfmt = AST_FORMAT_SLINEAR,
00343 .dstfmt = AST_FORMAT_ADPCM,
00344 .framein = lintoadpcm_framein,
00345 .frameout = lintoadpcm_frameout,
00346 .sample = lintoadpcm_sample,
00347 .desc_size = sizeof (struct adpcm_encoder_pvt),
00348 .buffer_samples = BUFFER_SAMPLES,
00349 .buf_size = BUFFER_SAMPLES/ 2,
00350 };
00351
00352
00353 static int reload(void)
00354 {
00355 return 0;
00356 }
00357
00358 static int unload_module(void)
00359 {
00360 int res;
00361
00362 res = ast_unregister_translator(&lintoadpcm);
00363 res |= ast_unregister_translator(&adpcmtolin);
00364
00365 return res;
00366 }
00367
00368 static int load_module(void)
00369 {
00370 int res;
00371
00372 res = ast_register_translator(&adpcmtolin);
00373 if (!res)
00374 res = ast_register_translator(&lintoadpcm);
00375 else
00376 ast_unregister_translator(&adpcmtolin);
00377
00378 return res;
00379 }
00380
00381 AST_MODULE_INFO(ASTERISK_GPL_KEY, AST_MODFLAG_DEFAULT, "Adaptive Differential PCM Coder/Decoder",
00382 .load = load_module,
00383 .unload = unload_module,
00384 .reload = reload,
00385 );