Wed Jan 27 20:02:09 2016

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 /*** MODULEINFO
00037    <support_level>core</support_level>
00038  ***/
00039 
00040 #include "asterisk.h"
00041 
00042 ASTERISK_FILE_VERSION(__FILE__, "$Revision: 328209 $")
00043 
00044 #include "asterisk/linkedlists.h"
00045 #include "asterisk/module.h"
00046 #include "asterisk/config.h"
00047 #include "asterisk/translate.h"
00048 #include "asterisk/utils.h"
00049 
00050 #define BUFFER_SAMPLES   8096 /* size for the translation buffers */
00051 #define BUF_SHIFT 5
00052 
00053 #include "g722/g722.h"
00054 
00055 /* Sample frame data */
00056 #include "asterisk/slin.h"
00057 #include "ex_g722.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 /*! \brief init a new instance of g722_encoder_pvt. */
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 static int lin16tog722_new(struct ast_trans_pvt *pvt)
00078 {
00079    struct g722_encoder_pvt *tmp = pvt->pvt;
00080 
00081    g722_encode_init(&tmp->g722, 64000, 0);
00082 
00083    return 0;
00084 }
00085 
00086 /*! \brief init a new instance of g722_encoder_pvt. */
00087 static int g722tolin_new(struct ast_trans_pvt *pvt)
00088 {
00089    struct g722_decoder_pvt *tmp = pvt->pvt;
00090 
00091    g722_decode_init(&tmp->g722, 64000, G722_SAMPLE_RATE_8000);
00092 
00093    return 0;
00094 }
00095 
00096 static int g722tolin16_new(struct ast_trans_pvt *pvt)
00097 {
00098    struct g722_decoder_pvt *tmp = pvt->pvt;
00099 
00100    g722_decode_init(&tmp->g722, 64000, 0);
00101 
00102    return 0;
00103 }
00104 
00105 static int g722tolin_framein(struct ast_trans_pvt *pvt, struct ast_frame *f)
00106 {
00107    struct g722_decoder_pvt *tmp = pvt->pvt;
00108    int out_samples;
00109    int in_samples;
00110 
00111    /* g722_decode expects the samples to be in the invalid samples / 2 format */
00112    in_samples = f->samples / 2;
00113 
00114    out_samples = g722_decode(&tmp->g722, &pvt->outbuf.i16[pvt->samples * sizeof(int16_t)], 
00115       (uint8_t *) f->data.ptr, in_samples);
00116 
00117    pvt->samples += out_samples;
00118 
00119    pvt->datalen += (out_samples * sizeof(int16_t));
00120 
00121    return 0;
00122 }
00123 
00124 static int lintog722_framein(struct ast_trans_pvt *pvt, struct ast_frame *f)
00125 {
00126    struct g722_encoder_pvt *tmp = pvt->pvt;
00127    int outlen;
00128 
00129    outlen = g722_encode(&tmp->g722, (&pvt->outbuf.ui8[pvt->datalen]), 
00130       (int16_t *) f->data.ptr, f->samples);
00131 
00132    pvt->samples += outlen * 2;
00133 
00134    pvt->datalen += outlen;
00135 
00136    return 0;
00137 }
00138 
00139 static struct ast_translator g722tolin = {
00140    .name = "g722tolin",
00141    .srcfmt = AST_FORMAT_G722,
00142    .dstfmt = AST_FORMAT_SLINEAR,
00143    .newpvt = g722tolin_new,   /* same for both directions */
00144    .framein = g722tolin_framein,
00145    .sample = g722_sample,
00146    .desc_size = sizeof(struct g722_decoder_pvt),
00147    .buffer_samples = BUFFER_SAMPLES / sizeof(int16_t),
00148    .buf_size = BUFFER_SAMPLES,
00149 };
00150 
00151 static struct ast_translator lintog722 = {
00152    .name = "lintog722",
00153    .srcfmt = AST_FORMAT_SLINEAR,
00154    .dstfmt = AST_FORMAT_G722,
00155    .newpvt = lintog722_new,   /* same for both directions */
00156    .framein = lintog722_framein,
00157    .sample = slin8_sample,
00158    .desc_size = sizeof(struct g722_encoder_pvt),
00159    .buffer_samples = BUFFER_SAMPLES * 2,
00160    .buf_size = BUFFER_SAMPLES,
00161 };
00162 
00163 static struct ast_translator g722tolin16 = {
00164    .name = "g722tolin16",
00165    .srcfmt = AST_FORMAT_G722,
00166    .dstfmt = AST_FORMAT_SLINEAR16,
00167    .newpvt = g722tolin16_new, /* same for both directions */
00168    .framein = g722tolin_framein,
00169    .sample = g722_sample,
00170    .desc_size = sizeof(struct g722_decoder_pvt),
00171    .buffer_samples = BUFFER_SAMPLES / sizeof(int16_t),
00172    .buf_size = BUFFER_SAMPLES,
00173 };
00174 
00175 static struct ast_translator lin16tog722 = {
00176    .name = "lin16tog722",
00177    .srcfmt = AST_FORMAT_SLINEAR16,
00178    .dstfmt = AST_FORMAT_G722,
00179    .newpvt = lin16tog722_new, /* same for both directions */
00180    .framein = lintog722_framein,
00181    .sample = slin16_sample,
00182    .desc_size = sizeof(struct g722_encoder_pvt),
00183    .buffer_samples = BUFFER_SAMPLES * 2,
00184    .buf_size = BUFFER_SAMPLES,
00185 };
00186 
00187 static int reload(void)
00188 {
00189    return AST_MODULE_LOAD_SUCCESS;
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    res |= ast_unregister_translator(&g722tolin16);
00199    res |= ast_unregister_translator(&lin16tog722);
00200 
00201    return res;
00202 }
00203 
00204 static int load_module(void)
00205 {
00206    int res = 0;
00207 
00208    res |= ast_register_translator(&g722tolin);
00209    res |= ast_register_translator(&lintog722);
00210    res |= ast_register_translator(&g722tolin16);
00211    res |= ast_register_translator(&lin16tog722);
00212 
00213    if (res) {
00214       unload_module();
00215       return AST_MODULE_LOAD_FAILURE;
00216    }  
00217 
00218    return AST_MODULE_LOAD_SUCCESS;
00219 }
00220 
00221 AST_MODULE_INFO(ASTERISK_GPL_KEY, AST_MODFLAG_DEFAULT, "ITU G.722-64kbps G722 Transcoder",
00222       .load = load_module,
00223       .unload = unload_module,
00224       .reload = reload,
00225           );

Generated on 27 Jan 2016 for Asterisk - The Open Source Telephony Project by  doxygen 1.6.1