Merged revisions 284701 via svnmerge from
[asterisk/asterisk.git] / formats / format_g726.c
1 /*
2  * Asterisk -- An open source telephony toolkit.
3  *
4  * Copyright (c) 2004 - 2005, inAccess Networks
5  *
6  * Michael Manousos <manousos@inaccessnetworks.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 Headerless G.726 (16/24/32/40kbps) data format for Asterisk.
22  * 
23  * File name extensions:
24  * \arg 40 kbps: g726-40
25  * \arg 32 kbps: g726-32
26  * \arg 24 kbps: g726-24
27  * \arg 16 kbps: g726-16
28  * \ingroup formats
29  */
30  
31 #include "asterisk.h"
32
33 ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
34
35 #include "asterisk/mod_format.h"
36 #include "asterisk/module.h"
37 #include "asterisk/endian.h"
38
39 #define RATE_40         0
40 #define RATE_32         1
41 #define RATE_24         2
42 #define RATE_16         3
43
44 /* We can only read/write chunks of FRAME_TIME ms G.726 data */
45 #define FRAME_TIME      10      /* 10 ms size */
46
47 #define BUF_SIZE        (5*FRAME_TIME)  /* max frame size in bytes ? */
48 /* Frame sizes in bytes */
49 static int frame_size[4] = { 
50                 FRAME_TIME * 5,
51                 FRAME_TIME * 4,
52                 FRAME_TIME * 3,
53                 FRAME_TIME * 2
54 };
55
56 struct g726_desc  {
57         int rate;       /* RATE_* defines */
58 };
59
60 /*
61  * Rate dependant format functions (open, rewrite)
62  */
63 static int g726_open(struct ast_filestream *tmp, int rate)
64 {
65         struct g726_desc *s = (struct g726_desc *)tmp->_private;
66         s->rate = rate;
67         ast_debug(1, "Created filestream G.726-%dk.\n", 40 - s->rate * 8);
68         return 0;
69 }
70
71 static int g726_40_open(struct ast_filestream *s)
72 {
73         return g726_open(s, RATE_40);
74 }
75
76 static int g726_32_open(struct ast_filestream *s)
77 {
78         return g726_open(s, RATE_32);
79 }
80
81 static int g726_24_open(struct ast_filestream *s)
82 {
83         return g726_open(s, RATE_24);
84 }
85
86 static int g726_16_open(struct ast_filestream *s)
87 {
88         return g726_open(s, RATE_16);
89 }
90
91 static int g726_40_rewrite(struct ast_filestream *s, const char *comment)
92 {
93         return g726_open(s, RATE_40);
94 }
95
96 static int g726_32_rewrite(struct ast_filestream *s, const char *comment)
97 {
98         return g726_open(s, RATE_32);
99 }
100
101 static int g726_24_rewrite(struct ast_filestream *s, const char *comment)
102 {
103         return g726_open(s, RATE_24);
104 }
105
106 static int g726_16_rewrite(struct ast_filestream *s, const char *comment)
107 {
108         return g726_open(s, RATE_16);
109 }
110
111 /*
112  * Rate independent format functions (read, write)
113  */
114
115 static struct ast_frame *g726_read(struct ast_filestream *s, int *whennext)
116 {
117         int res;
118         struct g726_desc *fs = (struct g726_desc *)s->_private;
119
120         /* Send a frame from the file to the appropriate channel */
121         s->fr.frametype = AST_FRAME_VOICE;
122         s->fr.subclass.codec = AST_FORMAT_G726;
123         s->fr.mallocd = 0;
124         AST_FRAME_SET_BUFFER(&s->fr, s->buf, AST_FRIENDLY_OFFSET, frame_size[fs->rate]);
125         s->fr.samples = 8 * FRAME_TIME;
126         if ((res = fread(s->fr.data.ptr, 1, s->fr.datalen, s->f)) != s->fr.datalen) {
127                 if (res)
128                         ast_log(LOG_WARNING, "Short read (%d) (%s)!\n", res, strerror(errno));
129                 return NULL;
130         }
131         *whennext = s->fr.samples;
132         return &s->fr;
133 }
134
135 static int g726_write(struct ast_filestream *s, struct ast_frame *f)
136 {
137         int res;
138         struct g726_desc *fs = (struct g726_desc *)s->_private;
139
140         if (f->frametype != AST_FRAME_VOICE) {
141                 ast_log(LOG_WARNING, "Asked to write non-voice frame!\n");
142                 return -1;
143         }
144         if (f->subclass.codec != AST_FORMAT_G726) {
145                 ast_log(LOG_WARNING, "Asked to write non-G726 frame (%s)!\n", 
146                                                 ast_getformatname(f->subclass.codec));
147                 return -1;
148         }
149         if (f->datalen % frame_size[fs->rate]) {
150                 ast_log(LOG_WARNING, "Invalid data length %d, should be multiple of %d\n", 
151                                                 f->datalen, frame_size[fs->rate]);
152                 return -1;
153         }
154         if ((res = fwrite(f->data.ptr, 1, f->datalen, s->f)) != f->datalen) {
155                 ast_log(LOG_WARNING, "Bad write (%d/%d): %s\n", 
156                                 res, frame_size[fs->rate], strerror(errno));
157                         return -1;
158         }
159         return 0;
160 }
161
162 static int g726_seek(struct ast_filestream *fs, off_t sample_offset, int whence)
163 {
164         return -1;
165 }
166
167 static int g726_trunc(struct ast_filestream *fs)
168 {
169         return -1;
170 }
171
172 static off_t g726_tell(struct ast_filestream *fs)
173 {
174         return -1;
175 }
176
177 static const struct ast_format f[] = {
178         {
179                 .name = "g726-40",
180                 .exts = "g726-40",
181                 .format = AST_FORMAT_G726,
182                 .open = g726_40_open,
183                 .rewrite = g726_40_rewrite,
184                 .write = g726_write,
185                 .seek = g726_seek,
186                 .trunc = g726_trunc,
187                 .tell = g726_tell,
188                 .read = g726_read,
189                 .buf_size = BUF_SIZE + AST_FRIENDLY_OFFSET,
190                 .desc_size = sizeof(struct g726_desc),
191         },
192         {
193                 .name = "g726-32",
194                 .exts = "g726-32",
195                 .format = AST_FORMAT_G726,
196                 .open = g726_32_open,
197                 .rewrite = g726_32_rewrite,
198                 .write = g726_write,
199                 .seek = g726_seek,
200                 .trunc = g726_trunc,
201                 .tell = g726_tell,
202                 .read = g726_read,
203                 .buf_size = BUF_SIZE + AST_FRIENDLY_OFFSET,
204                 .desc_size = sizeof(struct g726_desc),
205         },
206         {
207                 .name = "g726-24",
208                 .exts = "g726-24",
209                 .format = AST_FORMAT_G726,
210                 .open = g726_24_open,
211                 .rewrite = g726_24_rewrite,
212                 .write = g726_write,
213                 .seek = g726_seek,
214                 .trunc = g726_trunc,
215                 .tell = g726_tell,
216                 .read = g726_read,
217                 .buf_size = BUF_SIZE + AST_FRIENDLY_OFFSET,
218                 .desc_size = sizeof(struct g726_desc),
219         },
220         {
221                 .name = "g726-16",
222                 .exts = "g726-16",
223                 .format = AST_FORMAT_G726,
224                 .open = g726_16_open,
225                 .rewrite = g726_16_rewrite,
226                 .write = g726_write,
227                 .seek = g726_seek,
228                 .trunc = g726_trunc,
229                 .tell = g726_tell,
230                 .read = g726_read,
231                 .buf_size = BUF_SIZE + AST_FRIENDLY_OFFSET,
232                 .desc_size = sizeof(struct g726_desc),
233         },
234         {       .format = 0 }   /* terminator */
235 };
236
237 static int load_module(void)
238 {
239         int i;
240
241         for (i = 0; f[i].format ; i++) {
242                 if (ast_format_register(&f[i])) {       /* errors are fatal */
243                         ast_log(LOG_WARNING, "Failed to register format %s.\n", f[i].name);
244                         return AST_MODULE_LOAD_FAILURE;
245                 }
246         }
247         return AST_MODULE_LOAD_SUCCESS;
248 }
249
250 static int unload_module(void)
251 {
252         int i;
253
254         for (i = 0; f[i].format ; i++) {
255                 if (ast_format_unregister(f[i].name))
256                         ast_log(LOG_WARNING, "Failed to unregister format %s.\n", f[i].name);
257         }
258         return(0);
259 }
260
261 AST_MODULE_INFO(ASTERISK_GPL_KEY, AST_MODFLAG_LOAD_ORDER, "Raw G.726 (16/24/32/40kbps) data",
262         .load = load_module,
263         .unload = unload_module,
264         .load_pri = AST_MODPRI_APP_DEPEND
265 );