4ca897820d4e44ba9b1cc8c8648536a569486f89
[asterisk/asterisk.git] / codecs / codec_alaw.c
1 /*
2  * Asterisk -- An open source telephony toolkit.
3  *
4  * Copyright (C) 1999 - 2005, Digium, Inc.
5  *
6  * Mark Spencer <markster@digium.com>
7  *
8  * See http://www.asterisk.org for more information about
9  * the Asterisk project. Please do not directly contact
10  * any of the maintainers of this project for assistance;
11  * the project provides a web site, mailing lists and IRC
12  * channels for your use.
13  *
14  * This program is free software, distributed under the terms of
15  * the GNU General Public License Version 2. See the LICENSE file
16  * at the top of the source tree.
17  */
18
19 /*! \file
20  *
21  * \brief codec_alaw.c - translate between signed linear and alaw
22  * 
23  * \ingroup codecs
24  */
25
26 #include "asterisk.h"
27
28 ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
29
30 #include "asterisk/module.h"
31 #include "asterisk/config.h"
32 #include "asterisk/translate.h"
33 #include "asterisk/alaw.h"
34 #include "asterisk/utils.h"
35
36 #define BUFFER_SAMPLES   8096   /* size for the translation buffers */
37
38 /* Sample frame data */
39 #include "asterisk/slin.h"
40 #include "ex_alaw.h"
41
42 /*! \brief decode frame into lin and fill output buffer. */
43 static int alawtolin_framein(struct ast_trans_pvt *pvt, struct ast_frame *f)
44 {
45         int i = f->samples;
46         unsigned char *src = f->data.ptr;
47         int16_t *dst = pvt->outbuf.i16 + pvt->samples;
48
49         pvt->samples += i;
50         pvt->datalen += i * 2;  /* 2 bytes/sample */
51         
52         while (i--)
53                 *dst++ = AST_ALAW(*src++);
54
55         return 0;
56 }
57
58 /*! \brief convert and store input samples in output buffer */
59 static int lintoalaw_framein(struct ast_trans_pvt *pvt, struct ast_frame *f)
60 {
61         int i = f->samples;
62         char *dst = pvt->outbuf.c + pvt->samples;
63         int16_t *src = f->data.ptr;
64
65         pvt->samples += i;
66         pvt->datalen += i;      /* 1 byte/sample */
67
68         while (i--) 
69                 *dst++ = AST_LIN2A(*src++);
70
71         return 0;
72 }
73
74 static struct ast_translator alawtolin = {
75         .name = "alawtolin",
76         .srcfmt = AST_FORMAT_ALAW,
77         .dstfmt = AST_FORMAT_SLINEAR,
78         .framein = alawtolin_framein,
79         .sample = alaw_sample,
80         .buffer_samples = BUFFER_SAMPLES,
81         .buf_size = BUFFER_SAMPLES * 2,
82         .plc_samples = 160,
83 };
84
85 static struct ast_translator lintoalaw = {
86         "lintoalaw",
87         .srcfmt = AST_FORMAT_SLINEAR,
88         .dstfmt = AST_FORMAT_ALAW,
89         .framein = lintoalaw_framein,
90         .sample = slin8_sample,
91         .buffer_samples = BUFFER_SAMPLES,
92         .buf_size = BUFFER_SAMPLES,
93 };
94
95 static int parse_config(int reload)
96 {
97         struct ast_variable *var;
98         struct ast_flags config_flags = { reload ? CONFIG_FLAG_FILEUNCHANGED : 0 };
99         struct ast_config *cfg = ast_config_load("codecs.conf", config_flags);
100         if (cfg == CONFIG_STATUS_FILEMISSING || cfg == CONFIG_STATUS_FILEUNCHANGED || cfg == CONFIG_STATUS_FILEINVALID)
101                 return 0;
102         for (var = ast_variable_browse(cfg, "plc"); var; var = var->next) {
103                 if (!strcasecmp(var->name, "genericplc")) {
104                         alawtolin.useplc = ast_true(var->value) ? 1 : 0;
105                         ast_verb(3, "codec_alaw: %susing generic PLC\n", alawtolin.useplc ? "" : "not ");
106                 }
107         }
108         ast_config_destroy(cfg);
109         return 0;
110 }
111
112 /*! \brief standard module stuff */
113
114 static int reload(void)
115 {
116         if (parse_config(1))
117                 return AST_MODULE_LOAD_DECLINE;
118         return AST_MODULE_LOAD_SUCCESS;
119 }
120
121 static int unload_module(void)
122 {
123         int res;
124
125         res = ast_unregister_translator(&lintoalaw);
126         res |= ast_unregister_translator(&alawtolin);
127
128         return res;
129 }
130
131 static int load_module(void)
132 {
133         int res;
134
135         if (parse_config(0))
136                 return AST_MODULE_LOAD_DECLINE;
137         res = ast_register_translator(&alawtolin);
138         if (!res)
139                 res = ast_register_translator(&lintoalaw);
140         else
141                 ast_unregister_translator(&alawtolin);
142         if (res)
143                 return AST_MODULE_LOAD_FAILURE;
144         return AST_MODULE_LOAD_SUCCESS;
145 }
146
147 AST_MODULE_INFO(ASTERISK_GPL_KEY, AST_MODFLAG_DEFAULT, "A-law Coder/Decoder",
148                 .load = load_module,
149                 .unload = unload_module,
150                 .reload = reload,
151                );