Mon Sep 20 2010 00:22:30

Asterisk developer's documentation


codec_g722.c

Go to the documentation of this file.
00001 /*
00002  * Asterisk -- An open source telephony toolkit.
00003  *
00004  * Copyright (C) 1999 - 2008, Digium, Inc.
00005  *
00006  * Matthew Fredrickson <creslin@digium.com>
00007  * Russell Bryant <russell@digium.com>
00008  *
00009  * Special thanks to Steve Underwood for the implementation
00010  * and for doing the 8khz<->g.722 direct translation code.
00011  *
00012  * See http://www.asterisk.org for more information about
00013  * the Asterisk project. Please do not directly contact
00014  * any of the maintainers of this project for assistance;
00015  * the project provides a web site, mailing lists and IRC
00016  * channels for your use.
00017  *
00018  * This program is free software, distributed under the terms of
00019  * the GNU General Public License Version 2. See the LICENSE file
00020  * at the top of the source tree.
00021  */
00022 
00023 /*! \file
00024  *
00025  * \brief codec_g722.c - translate between signed linear and ITU G.722-64kbps
00026  *
00027  * \author Matthew Fredrickson <creslin@digium.com>
00028  * \author Russell Bryant <russell@digium.com>
00029  *
00030  * \arg http://soft-switch.org/downloads/non-gpl-bits.tgz
00031  * \arg http://lists.digium.com/pipermail/asterisk-dev/2006-September/022866.html
00032  *
00033  * \ingroup codecs
00034  */
00035 
00036 #include "asterisk.h"
00037 
00038 ASTERISK_FILE_VERSION(__FILE__, "$Revision: 150729 $")
00039 
00040 #include "asterisk/linkedlists.h"
00041 #include "asterisk/module.h"
00042 #include "asterisk/config.h"
00043 #include "asterisk/translate.h"
00044 #include "asterisk/utils.h"
00045 
00046 #define BUFFER_SAMPLES   8096 /* size for the translation buffers */
00047 #define BUF_SHIFT 5
00048 
00049 #include "g722/g722.h"
00050 
00051 /* Sample frame data */
00052 #include "asterisk/slin.h"
00053 #include "ex_g722.h"
00054 
00055 struct g722_encoder_pvt {
00056    g722_encode_state_t g722;
00057 };
00058 
00059 struct g722_decoder_pvt {
00060    g722_decode_state_t g722;
00061 };
00062 
00063 /*! \brief init a new instance of g722_encoder_pvt. */
00064 static int lintog722_new(struct ast_trans_pvt *pvt)
00065 {
00066    struct g722_encoder_pvt *tmp = pvt->pvt;
00067 
00068    g722_encode_init(&tmp->g722, 64000, G722_SAMPLE_RATE_8000);
00069 
00070    return 0;
00071 }
00072 
00073 static int lin16tog722_new(struct ast_trans_pvt *pvt)
00074 {
00075    struct g722_encoder_pvt *tmp = pvt->pvt;
00076 
00077    g722_encode_init(&tmp->g722, 64000, 0);
00078 
00079    return 0;
00080 }
00081 
00082 /*! \brief init a new instance of g722_encoder_pvt. */
00083 static int g722tolin_new(struct ast_trans_pvt *pvt)
00084 {
00085    struct g722_decoder_pvt *tmp = pvt->pvt;
00086 
00087    g722_decode_init(&tmp->g722, 64000, G722_SAMPLE_RATE_8000);
00088 
00089    return 0;
00090 }
00091 
00092 static int g722tolin16_new(struct ast_trans_pvt *pvt)
00093 {
00094    struct g722_decoder_pvt *tmp = pvt->pvt;
00095 
00096    g722_decode_init(&tmp->g722, 64000, 0);
00097 
00098    return 0;
00099 }
00100 
00101 static int g722tolin_framein(struct ast_trans_pvt *pvt, struct ast_frame *f)
00102 {
00103    struct g722_decoder_pvt *tmp = pvt->pvt;
00104    int out_samples;
00105    int in_samples;
00106 
00107    /* g722_decode expects the samples to be in the invalid samples / 2 format */
00108    in_samples = f->samples / 2;
00109 
00110    out_samples = g722_decode(&tmp->g722, &pvt->outbuf.i16[pvt->samples * sizeof(int16_t)], 
00111       (uint8_t *) f->data.ptr, in_samples);
00112 
00113    pvt->samples += out_samples;
00114 
00115    pvt->datalen += (out_samples * sizeof(int16_t));
00116 
00117    return 0;
00118 }
00119 
00120 static int lintog722_framein(struct ast_trans_pvt *pvt, struct ast_frame *f)
00121 {
00122    struct g722_encoder_pvt *tmp = pvt->pvt;
00123    int outlen;
00124 
00125    outlen = g722_encode(&tmp->g722, (&pvt->outbuf.ui8[pvt->datalen]), 
00126       (int16_t *) f->data.ptr, f->samples);
00127 
00128    pvt->samples += outlen * 2;
00129 
00130    pvt->datalen += outlen;
00131 
00132    return 0;
00133 }
00134 
00135 static struct ast_translator g722tolin = {
00136    .name = "g722tolin",
00137    .srcfmt = AST_FORMAT_G722,
00138    .dstfmt = AST_FORMAT_SLINEAR,
00139    .newpvt = g722tolin_new,   /* same for both directions */
00140    .framein = g722tolin_framein,
00141    .sample = g722_sample,
00142    .desc_size = sizeof(struct g722_decoder_pvt),
00143    .buffer_samples = BUFFER_SAMPLES / sizeof(int16_t),
00144    .buf_size = BUFFER_SAMPLES,
00145    .plc_samples = 160,
00146 };
00147 
00148 static struct ast_translator lintog722 = {
00149    .name = "lintog722",
00150    .srcfmt = AST_FORMAT_SLINEAR,
00151    .dstfmt = AST_FORMAT_G722,
00152    .newpvt = lintog722_new,   /* same for both directions */
00153    .framein = lintog722_framein,
00154    .sample = slin8_sample,
00155    .desc_size = sizeof(struct g722_encoder_pvt),
00156    .buffer_samples = BUFFER_SAMPLES * 2,
00157    .buf_size = BUFFER_SAMPLES,
00158 };
00159 
00160 static struct ast_translator g722tolin16 = {
00161    .name = "g722tolin16",
00162    .srcfmt = AST_FORMAT_G722,
00163    .dstfmt = AST_FORMAT_SLINEAR16,
00164    .newpvt = g722tolin16_new, /* same for both directions */
00165    .framein = g722tolin_framein,
00166    .sample = g722_sample,
00167    .desc_size = sizeof(struct g722_decoder_pvt),
00168    .buffer_samples = BUFFER_SAMPLES / sizeof(int16_t),
00169    .buf_size = BUFFER_SAMPLES,
00170    .plc_samples = 160,
00171 };
00172 
00173 static struct ast_translator lin16tog722 = {
00174    .name = "lin16tog722",
00175    .srcfmt = AST_FORMAT_SLINEAR16,
00176    .dstfmt = AST_FORMAT_G722,
00177    .newpvt = lin16tog722_new, /* same for both directions */
00178    .framein = lintog722_framein,
00179    .sample = slin16_sample,
00180    .desc_size = sizeof(struct g722_encoder_pvt),
00181    .buffer_samples = BUFFER_SAMPLES * 2,
00182    .buf_size = BUFFER_SAMPLES,
00183 };
00184 
00185 static int parse_config(int reload)
00186 {
00187    struct ast_variable *var;
00188    struct ast_flags config_flags = { reload ? CONFIG_FLAG_FILEUNCHANGED : 0 };
00189    struct ast_config *cfg = ast_config_load("codecs.conf", config_flags);
00190 
00191    if (cfg == CONFIG_STATUS_FILEMISSING || cfg == CONFIG_STATUS_FILEUNCHANGED || cfg == CONFIG_STATUS_FILEINVALID)
00192       return 0;
00193    for (var = ast_variable_browse(cfg, "plc"); var; var = var->next) {
00194       if (!strcasecmp(var->name, "genericplc")) {
00195          g722tolin.useplc = ast_true(var->value) ? 1 : 0;
00196          ast_verb(3, "codec_g722: %susing generic PLC\n",
00197                g722tolin.useplc ? "" : "not ");
00198       }
00199    }
00200    ast_config_destroy(cfg);
00201    return 0;
00202 }
00203 
00204 static int reload(void)
00205 {
00206    if (parse_config(1))
00207       return AST_MODULE_LOAD_DECLINE;
00208    return AST_MODULE_LOAD_SUCCESS;
00209 }
00210 
00211 static int unload_module(void)
00212 {
00213    int res = 0;
00214 
00215    res |= ast_unregister_translator(&g722tolin);
00216    res |= ast_unregister_translator(&lintog722);
00217    res |= ast_unregister_translator(&g722tolin16);
00218    res |= ast_unregister_translator(&lin16tog722);
00219 
00220    return res;
00221 }
00222 
00223 static int load_module(void)
00224 {
00225    int res = 0;
00226 
00227    if (parse_config(0))
00228       return AST_MODULE_LOAD_DECLINE;
00229 
00230    res |= ast_register_translator(&g722tolin);
00231    res |= ast_register_translator(&lintog722);
00232    res |= ast_register_translator(&g722tolin16);
00233    res |= ast_register_translator(&lin16tog722);
00234 
00235    if (res) {
00236       unload_module();
00237       return AST_MODULE_LOAD_FAILURE;
00238    }  
00239 
00240    return AST_MODULE_LOAD_SUCCESS;
00241 }
00242 
00243 AST_MODULE_INFO(ASTERISK_GPL_KEY, AST_MODFLAG_DEFAULT, "ITU G.722-64kbps G722 Transcoder",
00244       .load = load_module,
00245       .unload = unload_module,
00246       .reload = reload,
00247           );