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 #include "asterisk.h"
00027
00028 ASTERISK_FILE_VERSION(__FILE__, "$Revision: 125386 $")
00029
00030 #include "asterisk/module.h"
00031 #include "asterisk/config.h"
00032 #include "asterisk/translate.h"
00033 #include "asterisk/alaw.h"
00034 #include "asterisk/utils.h"
00035
00036 #define BUFFER_SAMPLES 8096
00037
00038
00039
00040 #include "slin_ulaw_ex.h"
00041 #include "ulaw_slin_ex.h"
00042
00043
00044 static int alawtolin_framein(struct ast_trans_pvt *pvt, struct ast_frame *f)
00045 {
00046 int i = f->samples;
00047 unsigned char *src = f->data.ptr;
00048 int16_t *dst = pvt->outbuf.i16 + pvt->samples;
00049
00050 pvt->samples += i;
00051 pvt->datalen += i * 2;
00052
00053 while (i--)
00054 *dst++ = AST_ALAW(*src++);
00055
00056 return 0;
00057 }
00058
00059
00060 static int lintoalaw_framein(struct ast_trans_pvt *pvt, struct ast_frame *f)
00061 {
00062 int i = f->samples;
00063 char *dst = pvt->outbuf.c + pvt->samples;
00064 int16_t *src = f->data.ptr;
00065
00066 pvt->samples += i;
00067 pvt->datalen += i;
00068
00069 while (i--)
00070 *dst++ = AST_LIN2A(*src++);
00071
00072 return 0;
00073 }
00074
00075
00076 static struct ast_frame *alawtolin_sample(void)
00077 {
00078 static struct ast_frame f;
00079 f.frametype = AST_FRAME_VOICE;
00080 f.subclass = AST_FORMAT_ALAW;
00081 f.datalen = sizeof(ulaw_slin_ex);
00082 f.samples = sizeof(ulaw_slin_ex);
00083 f.mallocd = 0;
00084 f.offset = 0;
00085 f.src = __PRETTY_FUNCTION__;
00086 f.data.ptr = ulaw_slin_ex;
00087 return &f;
00088 }
00089
00090
00091 static struct ast_frame *lintoalaw_sample(void)
00092 {
00093 static struct ast_frame f;
00094 f.frametype = AST_FRAME_VOICE;
00095 f.subclass = AST_FORMAT_SLINEAR;
00096 f.datalen = sizeof(slin_ulaw_ex);
00097 f.samples = sizeof(slin_ulaw_ex) / 2;
00098 f.mallocd = 0;
00099 f.offset = 0;
00100 f.src = __PRETTY_FUNCTION__;
00101 f.data.ptr = slin_ulaw_ex;
00102 return &f;
00103 }
00104
00105 static struct ast_translator alawtolin = {
00106 .name = "alawtolin",
00107 .srcfmt = AST_FORMAT_ALAW,
00108 .dstfmt = AST_FORMAT_SLINEAR,
00109 .framein = alawtolin_framein,
00110 .sample = alawtolin_sample,
00111 .buffer_samples = BUFFER_SAMPLES,
00112 .buf_size = BUFFER_SAMPLES * 2,
00113 .plc_samples = 160,
00114 };
00115
00116 static struct ast_translator lintoalaw = {
00117 "lintoalaw",
00118 .srcfmt = AST_FORMAT_SLINEAR,
00119 .dstfmt = AST_FORMAT_ALAW,
00120 .framein = lintoalaw_framein,
00121 .sample = lintoalaw_sample,
00122 .buffer_samples = BUFFER_SAMPLES,
00123 .buf_size = BUFFER_SAMPLES,
00124 };
00125
00126 static int parse_config(int reload)
00127 {
00128 struct ast_variable *var;
00129 struct ast_flags config_flags = { reload ? CONFIG_FLAG_FILEUNCHANGED : 0 };
00130 struct ast_config *cfg = ast_config_load("codecs.conf", config_flags);
00131 if (cfg == NULL)
00132 return 0;
00133 if (cfg == CONFIG_STATUS_FILEUNCHANGED)
00134 return 0;
00135 for (var = ast_variable_browse(cfg, "plc"); var; var = var->next) {
00136 if (!strcasecmp(var->name, "genericplc")) {
00137 alawtolin.useplc = ast_true(var->value) ? 1 : 0;
00138 ast_verb(3, "codec_alaw: %susing generic PLC\n", alawtolin.useplc ? "" : "not ");
00139 }
00140 }
00141 ast_config_destroy(cfg);
00142 return 0;
00143 }
00144
00145
00146
00147 static int reload(void)
00148 {
00149 if (parse_config(1))
00150 return AST_MODULE_LOAD_DECLINE;
00151 return AST_MODULE_LOAD_SUCCESS;
00152 }
00153
00154 static int unload_module(void)
00155 {
00156 int res;
00157
00158 res = ast_unregister_translator(&lintoalaw);
00159 res |= ast_unregister_translator(&alawtolin);
00160
00161 return res;
00162 }
00163
00164 static int load_module(void)
00165 {
00166 int res;
00167
00168 if (parse_config(0))
00169 return AST_MODULE_LOAD_DECLINE;
00170 res = ast_register_translator(&alawtolin);
00171 if (!res)
00172 res = ast_register_translator(&lintoalaw);
00173 else
00174 ast_unregister_translator(&alawtolin);
00175 if (res)
00176 return AST_MODULE_LOAD_FAILURE;
00177 return AST_MODULE_LOAD_SUCCESS;
00178 }
00179
00180 AST_MODULE_INFO(ASTERISK_GPL_KEY, AST_MODFLAG_DEFAULT, "A-law Coder/Decoder",
00181 .load = load_module,
00182 .unload = unload_module,
00183 .reload = reload,
00184 );