1a73cdfbbf977bae3ae589aa9172454e488dea20
[asterisk/asterisk.git] / formats / format_sln.c
1 /*
2  * Asterisk -- An open source telephony toolkit.
3  *
4  * Copyright (C) 1999 - 2005, Anthony Minessale
5  * Anthony Minessale (anthmct@yahoo.com)
6  *
7  * See http://www.asterisk.org for more information about
8  * the Asterisk project. Please do not directly contact
9  * any of the maintainers of this project for assistance;
10  * the project provides a web site, mailing lists and IRC
11  * channels for your use.
12  *
13  * This program is free software, distributed under the terms of
14  * the GNU General Public License Version 2. See the LICENSE file
15  * at the top of the source tree.
16  */
17
18 /*! \file
19  *
20  * \brief RAW SLINEAR Formats
21  * \ingroup formats
22  */
23
24 /*** MODULEINFO
25         <support_level>core</support_level>
26  ***/
27  
28 #include "asterisk.h"
29
30 ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
31
32 #include "asterisk/mod_format.h"
33 #include "asterisk/module.h"
34 #include "asterisk/endian.h"
35
36 static struct ast_frame *generic_read(struct ast_filestream *s, int *whennext, unsigned int buf_size, enum ast_format_id id)
37 {
38         int res;
39         /* Send a frame from the file to the appropriate channel */
40
41         s->fr.frametype = AST_FRAME_VOICE;
42         ast_format_set(&s->fr.subclass.format, id, 0);
43         s->fr.mallocd = 0;
44         AST_FRAME_SET_BUFFER(&s->fr, s->buf, AST_FRIENDLY_OFFSET, buf_size);
45         if ((res = fread(s->fr.data.ptr, 1, s->fr.datalen, s->f)) < 1) {
46                 if (res)
47                         ast_log(LOG_WARNING, "Short read (%d) (%s)!\n", res, strerror(errno));
48                 return NULL;
49         }
50         *whennext = s->fr.samples = res/2;
51         s->fr.datalen = res;
52         return &s->fr;
53 }
54
55 static int generic_write(struct ast_filestream *fs, struct ast_frame *f, enum ast_format_id id)
56 {
57         int res;
58         if (f->frametype != AST_FRAME_VOICE) {
59                 ast_log(LOG_WARNING, "Asked to write non-voice frame!\n");
60                 return -1;
61         }
62         if (f->subclass.format.id != id) {
63                 ast_log(LOG_WARNING, "Asked to write non-slinear frame (%s)!\n", ast_getformatname(&f->subclass.format));
64                 return -1;
65         }
66         if ((res = fwrite(f->data.ptr, 1, f->datalen, fs->f)) != f->datalen) {
67                         ast_log(LOG_WARNING, "Bad write (%d/%d): %s\n", res, f->datalen, strerror(errno));
68                         return -1;
69         }
70         return 0;
71 }
72
73 static int slinear_seek(struct ast_filestream *fs, off_t sample_offset, int whence)
74 {
75         off_t offset=0,min,cur,max;
76
77         min = 0;
78         sample_offset <<= 1;
79         cur = ftello(fs->f);
80         fseeko(fs->f, 0, SEEK_END);
81         max = ftello(fs->f);
82         if (whence == SEEK_SET)
83                 offset = sample_offset;
84         else if (whence == SEEK_CUR || whence == SEEK_FORCECUR)
85                 offset = sample_offset + cur;
86         else if (whence == SEEK_END)
87                 offset = max - sample_offset;
88         if (whence != SEEK_FORCECUR) {
89                 offset = (offset > max)?max:offset;
90         }
91         /* always protect against seeking past begining. */
92         offset = (offset < min)?min:offset;
93         return fseeko(fs->f, offset, SEEK_SET);
94 }
95
96 static int slinear_trunc(struct ast_filestream *fs)
97 {
98         return ftruncate(fileno(fs->f), ftello(fs->f));
99 }
100
101 static off_t slinear_tell(struct ast_filestream *fs)
102 {
103         return ftello(fs->f) / 2;
104 }
105
106 static int slinear_write(struct ast_filestream *fs, struct ast_frame *f){return generic_write(fs, f, AST_FORMAT_SLINEAR);}
107 static struct ast_frame *slinear_read(struct ast_filestream *s, int *whennext){return generic_read(s, whennext, 320, AST_FORMAT_SLINEAR);}
108 static struct ast_format_def slin_f = {
109         .name = "sln",
110         .exts = "sln|raw",
111         .write = slinear_write,
112         .seek = slinear_seek,
113         .trunc = slinear_trunc,
114         .tell = slinear_tell,
115         .read = slinear_read,
116         .buf_size = 320 + AST_FRIENDLY_OFFSET,
117 };
118
119 static int slinear12_write(struct ast_filestream *fs, struct ast_frame *f){return generic_write(fs, f, AST_FORMAT_SLINEAR12);}
120 static struct ast_frame *slinear12_read(struct ast_filestream *s, int *whennext){return generic_read(s, whennext, 480, AST_FORMAT_SLINEAR12);}
121 static struct ast_format_def slin12_f = {
122         .name = "sln12",
123         .exts = "sln12",
124         .write = slinear12_write,
125         .seek = slinear_seek,
126         .trunc = slinear_trunc,
127         .tell = slinear_tell,
128         .read = slinear12_read,
129         .buf_size = 480 + AST_FRIENDLY_OFFSET,
130 };
131
132 static int slinear16_write(struct ast_filestream *fs, struct ast_frame *f){return generic_write(fs, f, AST_FORMAT_SLINEAR16);}
133 static struct ast_frame *slinear16_read(struct ast_filestream *s, int *whennext){return generic_read(s, whennext, 640, AST_FORMAT_SLINEAR16);}
134 static struct ast_format_def slin16_f = {
135         .name = "sln16",
136         .exts = "sln16",
137         .write = slinear16_write,
138         .seek = slinear_seek,
139         .trunc = slinear_trunc,
140         .tell = slinear_tell,
141         .read = slinear16_read,
142         .buf_size = 640 + AST_FRIENDLY_OFFSET,
143 };
144
145 static int slinear24_write(struct ast_filestream *fs, struct ast_frame *f){return generic_write(fs, f, AST_FORMAT_SLINEAR24);}
146 static struct ast_frame *slinear24_read(struct ast_filestream *s, int *whennext){return generic_read(s, whennext, 960, AST_FORMAT_SLINEAR24);}
147 static struct ast_format_def slin24_f = {
148         .name = "sln24",
149         .exts = "sln24",
150         .write = slinear24_write,
151         .seek = slinear_seek,
152         .trunc = slinear_trunc,
153         .tell = slinear_tell,
154         .read = slinear24_read,
155         .buf_size = 960 + AST_FRIENDLY_OFFSET,
156 };
157
158 static int slinear32_write(struct ast_filestream *fs, struct ast_frame *f){return generic_write(fs, f, AST_FORMAT_SLINEAR32);}
159 static struct ast_frame *slinear32_read(struct ast_filestream *s, int *whennext){return generic_read(s, whennext, 1280, AST_FORMAT_SLINEAR32);}
160 static struct ast_format_def slin32_f = {
161         .name = "sln32",
162         .exts = "sln32",
163         .write = slinear32_write,
164         .seek = slinear_seek,
165         .trunc = slinear_trunc,
166         .tell = slinear_tell,
167         .read = slinear32_read,
168         .buf_size = 1280 + AST_FRIENDLY_OFFSET,
169 };
170
171 static int slinear44_write(struct ast_filestream *fs, struct ast_frame *f){return generic_write(fs, f, AST_FORMAT_SLINEAR44);}
172 static struct ast_frame *slinear44_read(struct ast_filestream *s, int *whennext){return generic_read(s, whennext, 1764, AST_FORMAT_SLINEAR44);}
173 static struct ast_format_def slin44_f = {
174         .name = "sln44",
175         .exts = "sln44",
176         .write = slinear44_write,
177         .seek = slinear_seek,
178         .trunc = slinear_trunc,
179         .tell = slinear_tell,
180         .read = slinear44_read,
181         .buf_size = 1764 + AST_FRIENDLY_OFFSET,
182 };
183
184 static int slinear48_write(struct ast_filestream *fs, struct ast_frame *f){return generic_write(fs, f, AST_FORMAT_SLINEAR48);}
185 static struct ast_frame *slinear48_read(struct ast_filestream *s, int *whennext){return generic_read(s, whennext, 1920, AST_FORMAT_SLINEAR48);}
186 static struct ast_format_def slin48_f = {
187         .name = "sln48",
188         .exts = "sln48",
189         .write = slinear48_write,
190         .seek = slinear_seek,
191         .trunc = slinear_trunc,
192         .tell = slinear_tell,
193         .read = slinear48_read,
194         .buf_size = 1920 + AST_FRIENDLY_OFFSET,
195 };
196
197 static int slinear96_write(struct ast_filestream *fs, struct ast_frame *f){return generic_write(fs, f, AST_FORMAT_SLINEAR96);}
198 static struct ast_frame *slinear96_read(struct ast_filestream *s, int *whennext){return generic_read(s, whennext, 3840, AST_FORMAT_SLINEAR96);}
199 static struct ast_format_def slin96_f = {
200         .name = "sln96",
201         .exts = "sln96",
202         .write = slinear96_write,
203         .seek = slinear_seek,
204         .trunc = slinear_trunc,
205         .tell = slinear_tell,
206         .read = slinear96_read,
207         .buf_size = 3840 + AST_FRIENDLY_OFFSET,
208 };
209
210 static int slinear192_write(struct ast_filestream *fs, struct ast_frame *f){return generic_write(fs, f, AST_FORMAT_SLINEAR192);}
211 static struct ast_frame *slinear192_read(struct ast_filestream *s, int *whennext){return generic_read(s, whennext, 7680, AST_FORMAT_SLINEAR192);}
212 static struct ast_format_def slin192_f = {
213         .name = "sln192",
214         .exts = "sln192",
215         .write = slinear192_write,
216         .seek = slinear_seek,
217         .trunc = slinear_trunc,
218         .tell = slinear_tell,
219         .read = slinear192_read,
220         .buf_size = 7680 + AST_FRIENDLY_OFFSET,
221 };
222
223 static struct ast_format_def *slin_list[] = {
224         &slin_f,
225         &slin12_f,
226         &slin16_f,
227         &slin24_f,
228         &slin32_f,
229         &slin44_f,
230         &slin48_f,
231         &slin96_f,
232         &slin192_f,
233 };
234
235 static int load_module(void)
236 {
237         int i;
238         ast_format_set(&slin_f.format, AST_FORMAT_SLINEAR, 0);
239         ast_format_set(&slin12_f.format, AST_FORMAT_SLINEAR12, 0);
240         ast_format_set(&slin16_f.format, AST_FORMAT_SLINEAR16, 0);
241         ast_format_set(&slin24_f.format, AST_FORMAT_SLINEAR24, 0);
242         ast_format_set(&slin32_f.format, AST_FORMAT_SLINEAR32, 0);
243         ast_format_set(&slin44_f.format, AST_FORMAT_SLINEAR44, 0);
244         ast_format_set(&slin48_f.format, AST_FORMAT_SLINEAR48, 0);
245         ast_format_set(&slin96_f.format, AST_FORMAT_SLINEAR96, 0);
246         ast_format_set(&slin192_f.format, AST_FORMAT_SLINEAR192, 0);
247
248         for (i = 0; i < ARRAY_LEN(slin_list); i++) {
249                 if (ast_format_def_register(slin_list[i])) {
250                         return AST_MODULE_LOAD_FAILURE;
251                 }
252         }
253
254         return AST_MODULE_LOAD_SUCCESS;
255 }
256
257 static int unload_module(void)
258 {
259         int res = 0;
260         int i = 0;
261
262         for (i = 0; i < ARRAY_LEN(slin_list); i++) {
263                 if (ast_format_def_unregister(slin_list[i]->name)) {
264                         res |= AST_MODULE_LOAD_FAILURE;
265                 }
266         }
267         return res;
268 }
269
270 AST_MODULE_INFO(ASTERISK_GPL_KEY, AST_MODFLAG_LOAD_ORDER, "Raw Signed Linear Audio support (SLN) 8khz-192khz",
271         .load = load_module,
272         .unload = unload_module,
273         .load_pri = AST_MODPRI_APP_DEPEND
274 );