2 * Asterisk -- An open source telephony toolkit.
4 * Anthony Minessale <anthmct@yahoo.com>
6 * Derived from other asterisk sound formats by
7 * Mark Spencer <markster@linux-support.net>
9 * Thanks to mpglib from http://www.mpg123.org/
10 * and Chris Stenton [jacs@gnome.co.uk]
11 * for coding the ability to play stereo and non-8khz files
13 * See http://www.asterisk.org for more information about
14 * the Asterisk project. Please do not directly contact
15 * any of the maintainers of this project for assistance;
16 * the project provides a web site, mailing lists and IRC
17 * channels for your use.
19 * This program is free software, distributed under the terms of
20 * the GNU General Public License Version 2. See the LICENSE file
21 * at the top of the source tree.
26 * \brief MP3 Format Handler
31 <defaultenabled>no</defaultenabled>
32 <support_level>extended</support_level>
37 ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
39 #include "mp3/mpg123.h"
40 #include "mp3/mpglib.h"
42 #include "asterisk/module.h"
43 #include "asterisk/mod_format.h"
44 #include "asterisk/logger.h"
46 #define MP3_BUFLEN 320
47 #define MP3_SCACHE 16384
48 #define MP3_DCACHE 8192
51 /*! state for the mp3 decoder */
53 /*! buffer to hold mp3 data after read from disk */
54 char sbuf[MP3_SCACHE];
55 /*! buffer for slinear audio after being decoded out of sbuf */
56 char dbuf[MP3_DCACHE];
57 /*! how much data has been written to the output buffer in the ast_filestream */
59 /*! how much data has been written to sbuf */
61 /*! how much data is left to be read out of dbuf, starting at dbufoffset */
63 /*! current offset for reading data out of dbuf */
69 static const char name[] = "mp3";
74 #define GAIN -4 /* 2^GAIN is the multiple to increase the volume by */
76 #if __BYTE_ORDER == __LITTLE_ENDIAN
82 #if __BYTE_ORDER == __BIG_ENDIAN
84 (((((b) ) & 0xFF) << 24) | \
85 ((((b) >> 8) & 0xFF) << 16) | \
86 ((((b) >> 16) & 0xFF) << 8) | \
87 ((((b) >> 24) & 0xFF) ))
89 (((((b) ) & 0xFF) << 8) | \
90 ((((b) >> 8) & 0xFF) ))
91 #define ltohl(b) htoll(b)
92 #define ltohs(b) htols(b)
94 #error "Endianess not defined"
99 static int mp3_open(struct ast_filestream *s)
101 struct mp3_private *p = s->_private;
102 InitMP3(&p->mp, OUTSCALE);
107 static void mp3_close(struct ast_filestream *s)
109 struct mp3_private *p = s->_private;
115 static int mp3_squeue(struct ast_filestream *s)
117 struct mp3_private *p = s->_private;
121 p->sbuflen = fread(p->sbuf, 1, MP3_SCACHE, s->f);
123 ast_log(LOG_WARNING, "Short read (%d) (%s)!\n", p->sbuflen, strerror(errno));
126 res = decodeMP3(&p->mp,p->sbuf,p->sbuflen,p->dbuf,MP3_DCACHE,&p->dbuflen);
129 p->sbuflen -= p->dbuflen;
134 static int mp3_dqueue(struct ast_filestream *s)
136 struct mp3_private *p = s->_private;
139 if((res = decodeMP3(&p->mp,NULL,0,p->dbuf,MP3_DCACHE,&p->dbuflen)) == MP3_OK) {
140 p->sbuflen -= p->dbuflen;
146 static int mp3_queue(struct ast_filestream *s)
148 struct mp3_private *p = s->_private;
149 int res = 0, bytes = 0;
153 InitMP3(&p->mp, OUTSCALE);
154 fseek(s->f, 0, SEEK_SET);
155 p->sbuflen = p->dbuflen = p->offset = 0;
156 while(p->offset < p->seek) {
159 while(p->offset < p->seek && ((res = mp3_dqueue(s))) == MP3_OK) {
160 for(bytes = 0 ; bytes < p->dbuflen ; bytes++) {
163 if(p->offset >= p->seek)
174 if(p->dbuflen == 0) {
180 if(! p->sbuflen || res != MP3_OK) {
190 static struct ast_frame *mp3_read(struct ast_filestream *s, int *whennext)
193 struct mp3_private *p = s->_private;
197 /* Pre-populate the buffer that holds audio to be returned (dbuf) */
203 /* Read out what's waiting in dbuf */
204 for (p->buflen = 0; p->buflen < MP3_BUFLEN && p->buflen < p->dbuflen; p->buflen++) {
205 s->buf[p->buflen + AST_FRIENDLY_OFFSET] = p->dbuf[p->buflen + p->dbufoffset];
207 p->dbufoffset += p->buflen;
208 p->dbuflen -= p->buflen;
211 if (p->buflen < MP3_BUFLEN) {
212 /* dbuf didn't have enough, so reset dbuf, fill it back up and continue */
213 p->dbuflen = p->dbufoffset = 0;
219 /* Make sure dbuf has enough to complete this read attempt */
220 if (p->dbuflen >= (MP3_BUFLEN - p->buflen)) {
221 for (save = p->buflen; p->buflen < MP3_BUFLEN; p->buflen++) {
222 s->buf[p->buflen + AST_FRIENDLY_OFFSET] = p->dbuf[(p->buflen - save) + p->dbufoffset];
224 p->dbufoffset += (MP3_BUFLEN - save);
225 p->dbuflen -= (MP3_BUFLEN - save);
230 p->offset += p->buflen;
231 delay = p->buflen / 2;
232 s->fr.frametype = AST_FRAME_VOICE;
233 ast_format_set(&s->fr.subclass.format, AST_FORMAT_SLINEAR, 0);
234 AST_FRAME_SET_BUFFER(&s->fr, s->buf, AST_FRIENDLY_OFFSET, p->buflen);
236 s->fr.samples = delay;
242 static int mp3_write(struct ast_filestream *fs, struct ast_frame *f)
244 ast_log(LOG_ERROR,"I Can't write MP3 only read them.\n");
250 static int mp3_seek(struct ast_filestream *s, off_t sample_offset, int whence)
252 struct mp3_private *p = s->_private;
254 long offset=0,samples;
255 samples = sample_offset * 2;
258 fseek(s->f, 0, SEEK_END);
259 max = ftell(s->f) * 100;
262 if (whence == SEEK_SET)
263 offset = samples + min;
264 else if (whence == SEEK_CUR || whence == SEEK_FORCECUR)
265 offset = samples + cur;
266 else if (whence == SEEK_END)
267 offset = max - samples;
268 if (whence != SEEK_FORCECUR) {
269 offset = (offset > max)?max:offset;
273 return fseek(s->f, offset, SEEK_SET);
277 static int mp3_rewrite(struct ast_filestream *s, const char *comment)
279 ast_log(LOG_ERROR,"I Can't write MP3 only read them.\n");
283 static int mp3_trunc(struct ast_filestream *s)
286 ast_log(LOG_ERROR,"I Can't write MP3 only read them.\n");
290 static off_t mp3_tell(struct ast_filestream *s)
292 struct mp3_private *p = s->_private;
297 static char *mp3_getcomment(struct ast_filestream *s)
302 static struct ast_format_def mp3_f = {
307 .rewrite = mp3_rewrite,
313 .getcomment = mp3_getcomment,
314 .buf_size = MP3_BUFLEN + AST_FRIENDLY_OFFSET,
315 .desc_size = sizeof(struct mp3_private),
319 static int load_module(void)
321 ast_format_set(&mp3_f.format, AST_FORMAT_SLINEAR, 0);
323 return ast_format_def_register(&mp3_f);
326 static int unload_module(void)
328 return ast_format_def_unregister(name);
331 AST_MODULE_INFO_STANDARD(ASTERISK_GPL_KEY, "MP3 format [Any rate but 8000hz mono is optimal]");