Wed Apr 6 11:29:44 2011

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: 267492 $")
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 };
00146 
00147 static struct ast_translator lintog722 = {
00148    .name = "lintog722",
00149    .srcfmt = AST_FORMAT_SLINEAR,
00150    .dstfmt = AST_FORMAT_G722,
00151    .newpvt = lintog722_new,   /* same for both directions */
00152    .framein = lintog722_framein,
00153    .sample = slin8_sample,
00154    .desc_size = sizeof(struct g722_encoder_pvt),
00155    .buffer_samples = BUFFER_SAMPLES * 2,
00156    .buf_size = BUFFER_SAMPLES,
00157 };
00158 
00159 static struct ast_translator g722tolin16 = {
00160    .name = "g722tolin16",
00161    .srcfmt = AST_FORMAT_G722,
00162    .dstfmt = AST_FORMAT_SLINEAR16,
00163    .newpvt = g722tolin16_new, /* same for both directions */
00164    .framein = g722tolin_framein,
00165    .sample = g722_sample,
00166    .desc_size = sizeof(struct g722_decoder_pvt),
00167    .buffer_samples = BUFFER_SAMPLES / sizeof(int16_t),
00168    .buf_size = BUFFER_SAMPLES,
00169 };
00170 
00171 static struct ast_translator lin16tog722 = {
00172    .name = "lin16tog722",
00173    .srcfmt = AST_FORMAT_SLINEAR16,
00174    .dstfmt = AST_FORMAT_G722,
00175    .newpvt = lin16tog722_new, /* same for both directions */
00176    .framein = lintog722_framein,
00177    .sample = slin16_sample,
00178    .desc_size = sizeof(struct g722_encoder_pvt),
00179    .buffer_samples = BUFFER_SAMPLES * 2,
00180    .buf_size = BUFFER_SAMPLES,
00181 };
00182 
00183 static int reload(void)
00184 {
00185    return AST_MODULE_LOAD_SUCCESS;
00186 }
00187 
00188 static int unload_module(void)
00189 {
00190    int res = 0;
00191 
00192    res |= ast_unregister_translator(&g722tolin);
00193    res |= ast_unregister_translator(&lintog722);
00194    res |= ast_unregister_translator(&g722tolin16);
00195    res |= ast_unregister_translator(&lin16tog722);
00196 
00197    return res;
00198 }
00199 
00200 static int load_module(void)
00201 {
00202    int res = 0;
00203 
00204    res |= ast_register_translator(&g722tolin);
00205    res |= ast_register_translator(&lintog722);
00206    res |= ast_register_translator(&g722tolin16);
00207    res |= ast_register_translator(&lin16tog722);
00208 
00209    if (res) {
00210       unload_module();
00211       return AST_MODULE_LOAD_FAILURE;
00212    }  
00213 
00214    return AST_MODULE_LOAD_SUCCESS;
00215 }
00216 
00217 AST_MODULE_INFO(ASTERISK_GPL_KEY, AST_MODFLAG_DEFAULT, "ITU G.722-64kbps G722 Transcoder",
00218       .load = load_module,
00219       .unload = unload_module,
00220       .reload = reload,
00221           );

Generated on Wed Apr 6 11:29:44 2011 for Asterisk - The Open Source Telephony Project by  doxygen 1.4.7