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 #include "asterisk.h"
00030
00031 ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
00032
00033 #include <fcntl.h>
00034 #include <netinet/in.h>
00035 #include <stdio.h>
00036 #include <stdlib.h>
00037 #include <string.h>
00038 #include <unistd.h>
00039
00040 #include "asterisk/lock.h"
00041 #include "asterisk/logger.h"
00042 #include "asterisk/linkedlists.h"
00043 #include "asterisk/module.h"
00044 #include "asterisk/config.h"
00045 #include "asterisk/options.h"
00046 #include "asterisk/translate.h"
00047 #include "asterisk/channel.h"
00048 #include "asterisk/utils.h"
00049
00050 #define BUFFER_SAMPLES 8096
00051 #define BUF_SHIFT 5
00052
00053
00054
00055 #include "g722/g722.h"
00056 #include "slin_g722_ex.h"
00057 #include "g722_slin_ex.h"
00058
00059 struct g722_encoder_pvt {
00060 g722_encode_state_t g722;
00061 };
00062
00063 struct g722_decoder_pvt {
00064 g722_decode_state_t g722;
00065 };
00066
00067
00068 static int lintog722_new(struct ast_trans_pvt *pvt)
00069 {
00070 struct g722_encoder_pvt *tmp = pvt->pvt;
00071
00072 g722_encode_init(&tmp->g722, 64000, G722_SAMPLE_RATE_8000);
00073
00074 return 0;
00075 }
00076
00077
00078 static int g722tolin_new(struct ast_trans_pvt *pvt)
00079 {
00080 struct g722_decoder_pvt *tmp = pvt->pvt;
00081
00082 g722_decode_init(&tmp->g722, 64000, G722_SAMPLE_RATE_8000);
00083
00084 return 0;
00085 }
00086
00087 static int g722tolin_framein(struct ast_trans_pvt *pvt, struct ast_frame *f)
00088 {
00089 struct g722_decoder_pvt *tmp = pvt->pvt;
00090 unsigned char *src = f->data;
00091 int16_t *dst = (int16_t *) pvt->outbuf + pvt->samples;
00092
00093 g722_decode(&tmp->g722, dst, src, f->samples);
00094 pvt->samples += f->samples;
00095 pvt->datalen += 2 * f->samples;
00096
00097 return 0;
00098 }
00099
00100 static int lintog722_framein(struct ast_trans_pvt *pvt, struct ast_frame *f)
00101 {
00102 struct g722_encoder_pvt *tmp = pvt->pvt;
00103 int16_t *src = f->data;
00104
00105 g722_encode(&tmp->g722, (uint8_t*)(&pvt->outbuf[pvt->datalen]), src, f->samples);
00106
00107
00108 pvt->samples += f->samples;
00109 pvt->datalen += f->samples;
00110
00111 return 0;
00112 }
00113
00114 static struct ast_frame *g722tolin_sample(void)
00115 {
00116 static struct ast_frame f = {
00117 .frametype = AST_FRAME_VOICE,
00118 .subclass = AST_FORMAT_G722,
00119 .datalen = sizeof(g722_slin_ex),
00120 .samples = sizeof(g722_slin_ex),
00121 .src = __PRETTY_FUNCTION__,
00122 .data = g722_slin_ex,
00123 };
00124
00125 return &f;
00126 }
00127
00128 static struct ast_frame *lintog722_sample (void)
00129 {
00130 static struct ast_frame f = {
00131 .frametype = AST_FRAME_VOICE,
00132 .subclass = AST_FORMAT_SLINEAR,
00133 .datalen = sizeof(slin_g722_ex),
00134 .samples = sizeof(slin_g722_ex),
00135 .src = __PRETTY_FUNCTION__,
00136 .data = slin_g722_ex,
00137 };
00138
00139 return &f;
00140 }
00141
00142 static struct ast_translator g722tolin = {
00143 .name = "g722tolin",
00144 .srcfmt = AST_FORMAT_G722,
00145 .dstfmt = AST_FORMAT_SLINEAR,
00146 .newpvt = g722tolin_new,
00147 .framein = g722tolin_framein,
00148 .sample = g722tolin_sample,
00149 .desc_size = sizeof(struct g722_decoder_pvt),
00150 .buffer_samples = BUFFER_SAMPLES,
00151 .buf_size = BUFFER_SAMPLES,
00152 .plc_samples = 160,
00153 };
00154
00155 static struct ast_translator lintog722 = {
00156 .name = "lintog722",
00157 .srcfmt = AST_FORMAT_SLINEAR,
00158 .dstfmt = AST_FORMAT_G722,
00159 .newpvt = lintog722_new,
00160 .framein = lintog722_framein,
00161 .sample = lintog722_sample,
00162 .desc_size = sizeof(struct g722_encoder_pvt),
00163 .buffer_samples = BUFFER_SAMPLES,
00164 .buf_size = BUFFER_SAMPLES,
00165 };
00166
00167 static void parse_config(void)
00168 {
00169 struct ast_variable *var;
00170 struct ast_config *cfg = ast_config_load("codecs.conf");
00171
00172 if (!cfg)
00173 return;
00174 for (var = ast_variable_browse(cfg, "plc"); var; var = var->next) {
00175 if (!strcasecmp(var->name, "genericplc")) {
00176 g722tolin.useplc = ast_true(var->value) ? 1 : 0;
00177 if (option_verbose > 2)
00178 ast_verbose(VERBOSE_PREFIX_3 "codec_g722: %susing generic PLC\n",
00179 g722tolin.useplc ? "" : "not ");
00180 }
00181 }
00182 ast_config_destroy(cfg);
00183 }
00184
00185 static int reload(void)
00186 {
00187 parse_config();
00188
00189 return 0;
00190 }
00191
00192 static int unload_module(void)
00193 {
00194 int res = 0;
00195
00196 res |= ast_unregister_translator(&g722tolin);
00197 res |= ast_unregister_translator(&lintog722);
00198
00199 return res;
00200 }
00201
00202 static int load_module(void)
00203 {
00204 int res = 0;
00205
00206
00207 parse_config();
00208
00209 res |= ast_register_translator(&g722tolin);
00210 res |= ast_register_translator(&lintog722);
00211
00212 if (res)
00213 unload_module();
00214
00215 return res;
00216 }
00217
00218 AST_MODULE_INFO(ASTERISK_GPL_KEY, AST_MODFLAG_DEFAULT, "ITU G.722-64kbps G722 Transcoder",
00219 .load = load_module,
00220 .unload = unload_module,
00221 .reload = reload,
00222 );