Skip to content
Snippets Groups Projects
codec_alaw.c 4.26 KiB
Newer Older
  • Learn to ignore specific revisions
  • /*
     * Asterisk -- An open source telephony toolkit.
     *
     * Copyright (C) 1999 - 2005, Digium, Inc.
    
    Mark Spencer's avatar
    Mark Spencer committed
     *
    
     * Mark Spencer <markster@digium.com>
    
    Mark Spencer's avatar
    Mark Spencer committed
     *
    
     * See http://www.asterisk.org for more information about
     * the Asterisk project. Please do not directly contact
     * any of the maintainers of this project for assistance;
     * the project provides a web site, mailing lists and IRC
     * channels for your use.
    
    Mark Spencer's avatar
    Mark Spencer committed
     *
     * This program is free software, distributed under the terms of
    
     * the GNU General Public License Version 2. See the LICENSE file
     * at the top of the source tree.
     */
    
    
     * \brief codec_alaw.c - translate between signed linear and alaw
    
    Russell Bryant's avatar
    Russell Bryant committed
     * \ingroup codecs
    
    Kevin P. Fleming's avatar
    Kevin P. Fleming committed
    #include <fcntl.h>
    #include <netinet/in.h>
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    #include <unistd.h>
    
    #include "asterisk.h"
    
    ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
    
    
    #include "asterisk/lock.h"
    #include "asterisk/logger.h"
    #include "asterisk/module.h"
    #include "asterisk/config.h"
    #include "asterisk/options.h"
    #include "asterisk/translate.h"
    #include "asterisk/channel.h"
    #include "asterisk/alaw.h"
    
    #include "asterisk/utils.h"
    
    #define BUFFER_SAMPLES   8096	/* size for the translation buffers */
    
    Mark Spencer's avatar
    Mark Spencer committed
    /* Sample frame data (Mu data is okay) */
    
    #include "slin_ulaw_ex.h"
    #include "ulaw_slin_ex.h"
    
    
    /*! \brief decode frame into lin and fill output buffer. */
    static int alawtolin_framein(struct ast_trans_pvt *pvt, struct ast_frame *f)
    
    Mark Spencer's avatar
    Mark Spencer committed
    {
    
    	int16_t *dst = (int16_t *)pvt->outbuf + pvt->samples;
    
    
    	pvt->samples += i;
    	pvt->datalen += i * 2;	/* 2 bytes/sample */
    
    	while (i--)
    		*dst++ = AST_ALAW(*src++);
    
    /*! \brief convert and store input samples in output buffer */
    static int lintoalaw_framein(struct ast_trans_pvt *pvt, struct ast_frame *f)
    
    Mark Spencer's avatar
    Mark Spencer committed
    {
    
    	char *dst = pvt->outbuf + pvt->samples;
    	int16_t *src = f->data;
    
    
    	pvt->samples += i;
    	pvt->datalen += i;	/* 1 byte/sample */
    
    	while (i--) 
    		*dst++ = AST_LIN2A(*src++);
    
    
    static struct ast_frame *alawtolin_sample(void)
    
    Mark Spencer's avatar
    Mark Spencer committed
    {
    
    	static struct ast_frame f;
    	f.frametype = AST_FRAME_VOICE;
    	f.subclass = AST_FORMAT_ALAW;
    	f.datalen = sizeof(ulaw_slin_ex);
    	f.samples = sizeof(ulaw_slin_ex);
    	f.mallocd = 0;
    	f.offset = 0;
    	f.src = __PRETTY_FUNCTION__;
    	f.data = ulaw_slin_ex;
    	return &f;
    
    static struct ast_frame *lintoalaw_sample(void)
    
    Mark Spencer's avatar
    Mark Spencer committed
    {
    
    	static struct ast_frame f;
    	f.frametype = AST_FRAME_VOICE;
    	f.subclass = AST_FORMAT_SLINEAR;
    	f.datalen = sizeof(slin_ulaw_ex);
    	f.samples = sizeof(slin_ulaw_ex) / 2;
    	f.mallocd = 0;
    	f.offset = 0;
    	f.src = __PRETTY_FUNCTION__;
    	f.data = slin_ulaw_ex;
    	return &f;
    
    Mark Spencer's avatar
    Mark Spencer committed
    }
    
    static struct ast_translator alawtolin = {
    
    	.name = "alawtolin",
    	.srcfmt = AST_FORMAT_ALAW,
    	.dstfmt = AST_FORMAT_SLINEAR,
    	.framein = alawtolin_framein,
    	.sample = alawtolin_sample,
    	.buffer_samples = BUFFER_SAMPLES,
    	.buf_size = BUFFER_SAMPLES * 2,
    	.plc_samples = 160,
    
    Mark Spencer's avatar
    Mark Spencer committed
    };
    
    static struct ast_translator lintoalaw = {
    
    	.srcfmt = AST_FORMAT_SLINEAR,
    	.dstfmt = AST_FORMAT_ALAW,
    	.framein = lintoalaw_framein,
    	.sample = lintoalaw_sample,
    	.buffer_samples = BUFFER_SAMPLES,
    	.buf_size = BUFFER_SAMPLES,
    
    static void parse_config(void)
    
    	struct ast_variable *var;
    
    	struct ast_config *cfg = ast_config_load("codecs.conf");
    	if (!cfg)
    		return;
    	for (var = ast_variable_browse(cfg, "plc"); var; var = var->next) {
    		if (!strcasecmp(var->name, "genericplc")) {
    			alawtolin.useplc = ast_true(var->value) ? 1 : 0;
    			if (option_verbose > 2)
    				ast_verbose(VERBOSE_PREFIX_3 "codec_alaw: %susing generic PLC\n", alawtolin.useplc ? "" : "not ");
    
    static int reload(void *mod)
    
    	parse_config();
    	return 0;
    
    static int unload_module(void *mod)
    
    Mark Spencer's avatar
    Mark Spencer committed
    {
    
    	int res;
    	res = ast_unregister_translator(&lintoalaw);
    
    	res |= ast_unregister_translator(&alawtolin);
    
    static int load_module(void *mod)
    
    Mark Spencer's avatar
    Mark Spencer committed
    {
    
    	int res;
    	parse_config();
    
    	res = ast_register_translator(&alawtolin, mod);
    
    		res = ast_register_translator(&lintoalaw, mod);
    
    	else
    		ast_unregister_translator(&alawtolin);
    	return res;
    
    static const char *description(void)
    
    Mark Spencer's avatar
    Mark Spencer committed
    {
    
    static const char *key(void)
    
    Mark Spencer's avatar
    Mark Spencer committed
    {
    
    	return ASTERISK_GPL_KEY;
    
    Mark Spencer's avatar
    Mark Spencer committed
    }
    
    
    STD_MOD(MOD_1, reload, NULL, NULL);