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/ulaw.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 ulawtolin_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
00054 while (i--)
00055 *dst++ = AST_MULAW(*src++);
00056
00057 return 0;
00058 }
00059
00060
00061 static int lintoulaw_framein(struct ast_trans_pvt *pvt, struct ast_frame *f)
00062 {
00063 int i = f->samples;
00064 char *dst = pvt->outbuf.c + pvt->samples;
00065 int16_t *src = f->data.ptr;
00066
00067 pvt->samples += i;
00068 pvt->datalen += i;
00069
00070 while (i--)
00071 *dst++ = AST_LIN2MU(*src++);
00072
00073 return 0;
00074 }
00075
00076
00077 static struct ast_frame *ulawtolin_sample(void)
00078 {
00079 static struct ast_frame f;
00080 f.frametype = AST_FRAME_VOICE;
00081 f.subclass = AST_FORMAT_ULAW;
00082 f.datalen = sizeof(ulaw_slin_ex);
00083 f.samples = sizeof(ulaw_slin_ex);
00084 f.mallocd = 0;
00085 f.offset = 0;
00086 f.src = __PRETTY_FUNCTION__;
00087 f.data.ptr = ulaw_slin_ex;
00088 return &f;
00089 }
00090
00091
00092
00093
00094
00095 static struct ast_frame *lintoulaw_sample(void)
00096 {
00097 static struct ast_frame f;
00098 f.frametype = AST_FRAME_VOICE;
00099 f.subclass = AST_FORMAT_SLINEAR;
00100 f.datalen = sizeof(slin_ulaw_ex);
00101
00102 f.samples = sizeof(slin_ulaw_ex) / 2;
00103 f.mallocd = 0;
00104 f.offset = 0;
00105 f.src = __PRETTY_FUNCTION__;
00106 f.data.ptr = slin_ulaw_ex;
00107 return &f;
00108 }
00109
00110
00111
00112
00113
00114 static struct ast_translator ulawtolin = {
00115 .name = "ulawtolin",
00116 .srcfmt = AST_FORMAT_ULAW,
00117 .dstfmt = AST_FORMAT_SLINEAR,
00118 .framein = ulawtolin_framein,
00119 .sample = ulawtolin_sample,
00120 .buffer_samples = BUFFER_SAMPLES,
00121 .buf_size = BUFFER_SAMPLES * 2,
00122 .plc_samples = 160,
00123 };
00124
00125
00126
00127
00128
00129 static struct ast_translator lintoulaw = {
00130 .name = "lintoulaw",
00131 .srcfmt = AST_FORMAT_SLINEAR,
00132 .dstfmt = AST_FORMAT_ULAW,
00133 .framein = lintoulaw_framein,
00134 .sample = lintoulaw_sample,
00135 .buf_size = BUFFER_SAMPLES,
00136 .buffer_samples = BUFFER_SAMPLES,
00137 };
00138
00139 static int parse_config(int reload)
00140 {
00141 struct ast_variable *var;
00142 struct ast_flags config_flags = { reload ? CONFIG_FLAG_FILEUNCHANGED : 0 };
00143 struct ast_config *cfg = ast_config_load("codecs.conf", config_flags);
00144 if (cfg == NULL)
00145 return 0;
00146 if (cfg == CONFIG_STATUS_FILEUNCHANGED)
00147 return 0;
00148 for (var = ast_variable_browse(cfg, "plc"); var; var = var->next) {
00149 if (!strcasecmp(var->name, "genericplc")) {
00150 ulawtolin.useplc = ast_true(var->value) ? 1 : 0;
00151 ast_verb(3, "codec_ulaw: %susing generic PLC\n", ulawtolin.useplc ? "" : "not ");
00152 }
00153 }
00154 ast_config_destroy(cfg);
00155 return 0;
00156 }
00157
00158 static int reload(void)
00159 {
00160 if (parse_config(1))
00161 return AST_MODULE_LOAD_DECLINE;
00162 return AST_MODULE_LOAD_SUCCESS;
00163 }
00164
00165 static int unload_module(void)
00166 {
00167 int res;
00168
00169 res = ast_unregister_translator(&lintoulaw);
00170 res |= ast_unregister_translator(&ulawtolin);
00171
00172 return res;
00173 }
00174
00175 static int load_module(void)
00176 {
00177 int res;
00178
00179 if (parse_config(0))
00180 return AST_MODULE_LOAD_DECLINE;
00181 res = ast_register_translator(&ulawtolin);
00182 if (!res)
00183 res = ast_register_translator(&lintoulaw);
00184 else
00185 ast_unregister_translator(&ulawtolin);
00186 if (res)
00187 return AST_MODULE_LOAD_FAILURE;
00188 return AST_MODULE_LOAD_SUCCESS;
00189 }
00190
00191 AST_MODULE_INFO(ASTERISK_GPL_KEY, AST_MODFLAG_DEFAULT, "mu-Law Coder/Decoder",
00192 .load = load_module,
00193 .unload = unload_module,
00194 .reload = reload,
00195 );