Fix compilation on Snow Leopard/FreeBSD for pbx_spool.c
[asterisk/asterisk.git] / formats / format_ogg_vorbis.c
1 /*
2  * Asterisk -- An open source telephony toolkit.
3  *
4  * Copyright (C) 2005, Jeff Ollie
5  *
6  * See http://www.asterisk.org for more information about
7  * the Asterisk project. Please do not directly contact
8  * any of the maintainers of this project for assistance;
9  * the project provides a web site, mailing lists and IRC
10  * channels for your use.
11  *
12  * This program is free software, distributed under the terms of
13  * the GNU General Public License Version 2. See the LICENSE file
14  * at the top of the source tree.
15  */
16
17 /*! \file
18  *
19  * \brief OGG/Vorbis streams.
20  * \arg File name extension: ogg
21  * \ingroup formats
22  */
23
24 /* the order of these dependencies is important... it also specifies
25    the link order of the libraries during linking
26 */
27
28 /*** MODULEINFO
29         <depend>vorbis</depend>
30         <depend>ogg</depend>
31         <support_level>core</support_level>
32  ***/
33
34 #include "asterisk.h"
35
36 ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
37
38 #include <vorbis/codec.h>
39 #include <vorbis/vorbisenc.h>
40
41 #ifdef _WIN32
42 #include <io.h>
43 #endif
44
45 #include "asterisk/mod_format.h"
46 #include "asterisk/module.h"
47
48 /*
49  * this is the number of samples we deal with. Samples are converted
50  * to SLINEAR so each one uses 2 bytes in the buffer.
51  */
52 #define SAMPLES_MAX 160
53 #define BUF_SIZE        (2*SAMPLES_MAX)
54
55 #define BLOCK_SIZE 4096         /* used internally in the vorbis routines */
56
57 struct vorbis_desc {    /* format specific parameters */
58         /* structures for handling the Ogg container */
59         ogg_sync_state oy;
60         ogg_stream_state os;
61         ogg_page og;
62         ogg_packet op;
63         
64         /* structures for handling Vorbis audio data */
65         vorbis_info vi;
66         vorbis_comment vc;
67         vorbis_dsp_state vd;
68         vorbis_block vb;
69         
70         /*! \brief Indicates whether this filestream is set up for reading or writing. */
71         int writing;
72         
73         /*! \brief Indicates whether an End of Stream condition has been detected. */
74         int eos;
75 };
76
77 /*!
78  * \brief Create a new OGG/Vorbis filestream and set it up for reading.
79  * \param s File that points to on disk storage of the OGG/Vorbis data.
80  * \return The new filestream.
81  */
82 static int ogg_vorbis_open(struct ast_filestream *s)
83 {
84         int i;
85         int bytes;
86         int result;
87         char **ptr;
88         char *buffer;
89         struct vorbis_desc *tmp = (struct vorbis_desc *)s->_private;
90
91         tmp->writing = 0;
92
93         ogg_sync_init(&tmp->oy);
94
95         buffer = ogg_sync_buffer(&tmp->oy, BLOCK_SIZE);
96         bytes = fread(buffer, 1, BLOCK_SIZE, s->f);
97         ogg_sync_wrote(&tmp->oy, bytes);
98
99         result = ogg_sync_pageout(&tmp->oy, &tmp->og);
100         if (result != 1) {
101                 if(bytes < BLOCK_SIZE) {
102                         ast_log(LOG_ERROR, "Run out of data...\n");
103                 } else {
104                         ast_log(LOG_ERROR, "Input does not appear to be an Ogg bitstream.\n");
105                 }
106                 ogg_sync_clear(&tmp->oy);
107                 return -1;
108         }
109         
110         ogg_stream_init(&tmp->os, ogg_page_serialno(&tmp->og));
111         vorbis_info_init(&tmp->vi);
112         vorbis_comment_init(&tmp->vc);
113
114         if (ogg_stream_pagein(&tmp->os, &tmp->og) < 0) { 
115                 ast_log(LOG_ERROR, "Error reading first page of Ogg bitstream data.\n");
116 error:
117                 ogg_stream_clear(&tmp->os);
118                 vorbis_comment_clear(&tmp->vc);
119                 vorbis_info_clear(&tmp->vi);
120                 ogg_sync_clear(&tmp->oy);
121                 return -1;
122         }
123         
124         if (ogg_stream_packetout(&tmp->os, &tmp->op) != 1) { 
125                 ast_log(LOG_ERROR, "Error reading initial header packet.\n");
126                 goto error;
127         }
128         
129         if (vorbis_synthesis_headerin(&tmp->vi, &tmp->vc, &tmp->op) < 0) { 
130                 ast_log(LOG_ERROR, "This Ogg bitstream does not contain Vorbis audio data.\n");
131                 goto error;
132         }
133         
134         for (i = 0; i < 2 ; ) {
135                 while (i < 2) {
136                         result = ogg_sync_pageout(&tmp->oy, &tmp->og);
137                         if (result == 0)
138                                 break;
139                         if (result == 1) {
140                                 ogg_stream_pagein(&tmp->os, &tmp->og);
141                                 while(i < 2) {
142                                         result = ogg_stream_packetout(&tmp->os,&tmp->op);
143                                         if(result == 0)
144                                                 break;
145                                         if(result < 0) {
146                                                 ast_log(LOG_ERROR, "Corrupt secondary header.  Exiting.\n");
147                                                 goto error;
148                                         }
149                                         vorbis_synthesis_headerin(&tmp->vi, &tmp->vc, &tmp->op);
150                                         i++;
151                                 }
152                         }
153                 }
154
155                 buffer = ogg_sync_buffer(&tmp->oy, BLOCK_SIZE);
156                 bytes = fread(buffer, 1, BLOCK_SIZE, s->f);
157                 if (bytes == 0 && i < 2) {
158                         ast_log(LOG_ERROR, "End of file before finding all Vorbis headers!\n");
159                         goto error;
160                 }
161                 ogg_sync_wrote(&tmp->oy, bytes);
162         }
163         
164         for (ptr = tmp->vc.user_comments; *ptr; ptr++)
165                 ast_debug(1, "OGG/Vorbis comment: %s\n", *ptr);
166                 ast_debug(1, "OGG/Vorbis bitstream is %d channel, %ldHz\n", tmp->vi.channels, tmp->vi.rate);
167                 ast_debug(1, "OGG/Vorbis file encoded by: %s\n", tmp->vc.vendor);
168
169         if (tmp->vi.channels != 1) {
170                 ast_log(LOG_ERROR, "Only monophonic OGG/Vorbis files are currently supported!\n");
171                 goto error;
172         }
173         
174         if (tmp->vi.rate != DEFAULT_SAMPLE_RATE) {
175                 ast_log(LOG_ERROR, "Only 8000Hz OGG/Vorbis files are currently supported!\n");
176                 vorbis_block_clear(&tmp->vb);
177                 vorbis_dsp_clear(&tmp->vd);
178                 goto error;
179         }
180         
181         vorbis_synthesis_init(&tmp->vd, &tmp->vi);
182         vorbis_block_init(&tmp->vd, &tmp->vb);
183
184         return 0;
185 }
186
187 /*!
188  * \brief Create a new OGG/Vorbis filestream and set it up for writing.
189  * \param s File pointer that points to on-disk storage.
190  * \param comment Comment that should be embedded in the OGG/Vorbis file.
191  * \return A new filestream.
192  */
193 static int ogg_vorbis_rewrite(struct ast_filestream *s,
194                                                  const char *comment)
195 {
196         ogg_packet header;
197         ogg_packet header_comm;
198         ogg_packet header_code;
199         struct vorbis_desc *tmp = (struct vorbis_desc *)s->_private;
200
201         tmp->writing = 1;
202
203         vorbis_info_init(&tmp->vi);
204
205         if (vorbis_encode_init_vbr(&tmp->vi, 1, DEFAULT_SAMPLE_RATE, 0.4)) {
206                 ast_log(LOG_ERROR, "Unable to initialize Vorbis encoder!\n");
207                 return -1;
208         }
209
210         vorbis_comment_init(&tmp->vc);
211         vorbis_comment_add_tag(&tmp->vc, "ENCODER", "Asterisk PBX");
212         if (comment)
213                 vorbis_comment_add_tag(&tmp->vc, "COMMENT", (char *) comment);
214
215         vorbis_analysis_init(&tmp->vd, &tmp->vi);
216         vorbis_block_init(&tmp->vd, &tmp->vb);
217
218         ogg_stream_init(&tmp->os, ast_random());
219
220         vorbis_analysis_headerout(&tmp->vd, &tmp->vc, &header, &header_comm,
221                                   &header_code);
222         ogg_stream_packetin(&tmp->os, &header);
223         ogg_stream_packetin(&tmp->os, &header_comm);
224         ogg_stream_packetin(&tmp->os, &header_code);
225
226         while (!tmp->eos) {
227                 if (ogg_stream_flush(&tmp->os, &tmp->og) == 0)
228                         break;
229                 if (!fwrite(tmp->og.header, 1, tmp->og.header_len, s->f)) {
230                         ast_log(LOG_WARNING, "fwrite() failed: %s\n", strerror(errno));
231                 }
232                 if (!fwrite(tmp->og.body, 1, tmp->og.body_len, s->f)) {
233                         ast_log(LOG_WARNING, "fwrite() failed: %s\n", strerror(errno));
234                 }
235                 if (ogg_page_eos(&tmp->og))
236                         tmp->eos = 1;
237         }
238
239         return 0;
240 }
241
242 /*!
243  * \brief Write out any pending encoded data.
244  * \param s An OGG/Vorbis filestream.
245  * \param f The file to write to.
246  */
247 static void write_stream(struct vorbis_desc *s, FILE *f)
248 {
249         while (vorbis_analysis_blockout(&s->vd, &s->vb) == 1) {
250                 vorbis_analysis(&s->vb, NULL);
251                 vorbis_bitrate_addblock(&s->vb);
252
253                 while (vorbis_bitrate_flushpacket(&s->vd, &s->op)) {
254                         ogg_stream_packetin(&s->os, &s->op);
255                         while (!s->eos) {
256                                 if (ogg_stream_pageout(&s->os, &s->og) == 0) {
257                                         break;
258                                 }
259                                 if (!fwrite(s->og.header, 1, s->og.header_len, f)) {
260                                 ast_log(LOG_WARNING, "fwrite() failed: %s\n", strerror(errno));
261                                 }
262                                 if (!fwrite(s->og.body, 1, s->og.body_len, f)) {
263                                         ast_log(LOG_WARNING, "fwrite() failed: %s\n", strerror(errno));
264                                 }
265                                 if (ogg_page_eos(&s->og)) {
266                                         s->eos = 1;
267                                 }
268                         }
269                 }
270         }
271 }
272
273 /*!
274  * \brief Write audio data from a frame to an OGG/Vorbis filestream.
275  * \param fs An OGG/Vorbis filestream.
276  * \param f A frame containing audio to be written to the filestream.
277  * \return -1 if there was an error, 0 on success.
278  */
279 static int ogg_vorbis_write(struct ast_filestream *fs, struct ast_frame *f)
280 {
281         int i;
282         float **buffer;
283         short *data;
284         struct vorbis_desc *s = (struct vorbis_desc *)fs->_private;
285
286         if (!s->writing) {
287                 ast_log(LOG_ERROR, "This stream is not set up for writing!\n");
288                 return -1;
289         }
290
291         if (f->frametype != AST_FRAME_VOICE) {
292                 ast_log(LOG_WARNING, "Asked to write non-voice frame!\n");
293                 return -1;
294         }
295         if (f->subclass.format.id != AST_FORMAT_SLINEAR) {
296                 ast_log(LOG_WARNING, "Asked to write non-SLINEAR frame (%s)!\n",
297                         ast_getformatname(&f->subclass.format));
298                 return -1;
299         }
300         if (!f->datalen)
301                 return -1;
302
303         data = (short *) f->data.ptr;
304
305         buffer = vorbis_analysis_buffer(&s->vd, f->samples);
306
307         for (i = 0; i < f->samples; i++)
308                 buffer[0][i] = (double)data[i] / 32768.0;
309
310         vorbis_analysis_wrote(&s->vd, f->samples);
311
312         write_stream(s, fs->f);
313
314         return 0;
315 }
316
317 /*!
318  * \brief Close a OGG/Vorbis filestream.
319  * \param fs A OGG/Vorbis filestream.
320  */
321 static void ogg_vorbis_close(struct ast_filestream *fs)
322 {
323         struct vorbis_desc *s = (struct vorbis_desc *)fs->_private;
324
325         if (s->writing) {
326                 /* Tell the Vorbis encoder that the stream is finished
327                  * and write out the rest of the data */
328                 vorbis_analysis_wrote(&s->vd, 0);
329                 write_stream(s, fs->f);
330         }
331
332         ogg_stream_clear(&s->os);
333         vorbis_block_clear(&s->vb);
334         vorbis_dsp_clear(&s->vd);
335         vorbis_comment_clear(&s->vc);
336         vorbis_info_clear(&s->vi);
337
338         if (s->writing) {
339                 ogg_sync_clear(&s->oy);
340         }
341 }
342
343 /*!
344  * \brief Get audio data.
345  * \param fs An OGG/Vorbis filestream.
346  * \param pcm Pointer to a buffere to store audio data in.
347  */
348
349 static int read_samples(struct ast_filestream *fs, float ***pcm)
350 {
351         int samples_in;
352         int result;
353         char *buffer;
354         int bytes;
355         struct vorbis_desc *s = (struct vorbis_desc *)fs->_private;
356
357         while (1) {
358                 samples_in = vorbis_synthesis_pcmout(&s->vd, pcm);
359                 if (samples_in > 0) {
360                         return samples_in;
361                 }
362
363                 /* The Vorbis decoder needs more data... */
364                 /* See ifOGG has any packets in the current page for the Vorbis decoder. */
365                 result = ogg_stream_packetout(&s->os, &s->op);
366                 if (result > 0) {
367                         /* Yes OGG had some more packets for the Vorbis decoder. */
368                         if (vorbis_synthesis(&s->vb, &s->op) == 0) {
369                                 vorbis_synthesis_blockin(&s->vd, &s->vb);
370                         }
371
372                         continue;
373                 }
374
375                 if (result < 0)
376                         ast_log(LOG_WARNING,
377                                         "Corrupt or missing data at this page position; continuing...\n");
378
379                 /* No more packets left in the current page... */
380
381                 if (s->eos) {
382                         /* No more pages left in the stream */
383                         return -1;
384                 }
385
386                 while (!s->eos) {
387                         /* See ifOGG has any pages in it's internal buffers */
388                         result = ogg_sync_pageout(&s->oy, &s->og);
389                         if (result > 0) {
390                                 /* Yes, OGG has more pages in it's internal buffers,
391                                    add the page to the stream state */
392                                 result = ogg_stream_pagein(&s->os, &s->og);
393                                 if (result == 0) {
394                                         /* Yes, got a new,valid page */
395                                         if (ogg_page_eos(&s->og)) {
396                                                 s->eos = 1;
397                                         }
398                                         break;
399                                 }
400                                 ast_log(LOG_WARNING,
401                                                 "Invalid page in the bitstream; continuing...\n");
402                         }
403
404                         if (result < 0)
405                                 ast_log(LOG_WARNING,
406                                                 "Corrupt or missing data in bitstream; continuing...\n");
407
408                         /* No, we need to read more data from the file descrptor */
409                         /* get a buffer from OGG to read the data into */
410                         buffer = ogg_sync_buffer(&s->oy, BLOCK_SIZE);
411                         /* read more data from the file descriptor */
412                         bytes = fread(buffer, 1, BLOCK_SIZE, fs->f);
413                         /* Tell OGG how many bytes we actually read into the buffer */
414                         ogg_sync_wrote(&s->oy, bytes);
415                         if (bytes == 0) {
416                                 s->eos = 1;
417                         }
418                 }
419         }
420 }
421
422 /*!
423  * \brief Read a frame full of audio data from the filestream.
424  * \param fs The filestream.
425  * \param whennext Number of sample times to schedule the next call.
426  * \return A pointer to a frame containing audio data or NULL ifthere is no more audio data.
427  */
428 static struct ast_frame *ogg_vorbis_read(struct ast_filestream *fs,
429                                          int *whennext)
430 {
431         int clipflag = 0;
432         int i;
433         int j;
434         double accumulator[SAMPLES_MAX];
435         int val;
436         int samples_in;
437         int samples_out = 0;
438         struct vorbis_desc *s = (struct vorbis_desc *)fs->_private;
439         short *buf;     /* SLIN data buffer */
440
441         fs->fr.frametype = AST_FRAME_VOICE;
442         ast_format_set(&fs->fr.subclass.format, AST_FORMAT_SLINEAR, 0);
443         fs->fr.mallocd = 0;
444         AST_FRAME_SET_BUFFER(&fs->fr, fs->buf, AST_FRIENDLY_OFFSET, BUF_SIZE);
445         buf = (short *)(fs->fr.data.ptr);       /* SLIN data buffer */
446
447         while (samples_out != SAMPLES_MAX) {
448                 float **pcm;
449                 int len = SAMPLES_MAX - samples_out;
450
451                 /* See ifVorbis decoder has some audio data for us ... */
452                 samples_in = read_samples(fs, &pcm);
453                 if (samples_in <= 0)
454                         break;
455
456                 /* Got some audio data from Vorbis... */
457                 /* Convert the float audio data to 16-bit signed linear */
458
459                 clipflag = 0;
460                 if (samples_in > len)
461                         samples_in = len;
462                 for (j = 0; j < samples_in; j++)
463                         accumulator[j] = 0.0;
464
465                 for (i = 0; i < s->vi.channels; i++) {
466                         float *mono = pcm[i];
467                         for (j = 0; j < samples_in; j++)
468                                 accumulator[j] += mono[j];
469                 }
470
471                 for (j = 0; j < samples_in; j++) {
472                         val = accumulator[j] * 32767.0 / s->vi.channels;
473                         if (val > 32767) {
474                                 val = 32767;
475                                 clipflag = 1;
476                         } else if (val < -32768) {
477                                 val = -32768;
478                                 clipflag = 1;
479                         }
480                         buf[samples_out + j] = val;
481                 }
482
483                 if (clipflag)
484                         ast_log(LOG_WARNING, "Clipping in frame %ld\n", (long) (s->vd.sequence));
485                 /* Tell the Vorbis decoder how many samples we actually used. */
486                 vorbis_synthesis_read(&s->vd, samples_in);
487                 samples_out += samples_in;
488         }
489
490         if (samples_out > 0) {
491                 fs->fr.datalen = samples_out * 2;
492                 fs->fr.samples = samples_out;
493                 *whennext = samples_out;
494
495                 return &fs->fr;
496         } else {
497                 return NULL;
498         }
499 }
500
501 /*!
502  * \brief Trucate an OGG/Vorbis filestream.
503  * \param s The filestream to truncate.
504  * \return 0 on success, -1 on failure.
505  */
506
507 static int ogg_vorbis_trunc(struct ast_filestream *s)
508 {
509         ast_log(LOG_WARNING, "Truncation is not supported on OGG/Vorbis streams!\n");
510         return -1;
511 }
512
513 /*!
514  * \brief Seek to a specific position in an OGG/Vorbis filestream.
515  * \param s The filestream to truncate.
516  * \param sample_offset New position for the filestream, measured in 8KHz samples.
517  * \param whence Location to measure 
518  * \return 0 on success, -1 on failure.
519  */
520 static int ogg_vorbis_seek(struct ast_filestream *s, off_t sample_offset, int whence)
521 {
522         ast_log(LOG_WARNING, "Seeking is not supported on OGG/Vorbis streams!\n");
523         return -1;
524 }
525
526 static off_t ogg_vorbis_tell(struct ast_filestream *s)
527 {
528         ast_log(LOG_WARNING, "Telling is not supported on OGG/Vorbis streams!\n");
529         return -1;
530 }
531
532 static struct ast_format_def vorbis_f = {
533         .name = "ogg_vorbis",
534         .exts = "ogg",
535         .open = ogg_vorbis_open,
536         .rewrite = ogg_vorbis_rewrite,
537         .write = ogg_vorbis_write,
538         .seek = ogg_vorbis_seek,
539         .trunc = ogg_vorbis_trunc,
540         .tell = ogg_vorbis_tell,
541         .read = ogg_vorbis_read,
542         .close = ogg_vorbis_close,
543         .buf_size = BUF_SIZE + AST_FRIENDLY_OFFSET,
544         .desc_size = sizeof(struct vorbis_desc),
545 };
546
547 static int load_module(void)
548 {
549         ast_format_set(&vorbis_f.format, AST_FORMAT_SLINEAR, 0);
550         if (ast_format_def_register(&vorbis_f))
551                 return AST_MODULE_LOAD_FAILURE;
552         return AST_MODULE_LOAD_SUCCESS;
553 }
554
555 static int unload_module(void)
556 {
557         return ast_format_def_unregister(vorbis_f.name);
558 }
559
560 AST_MODULE_INFO(ASTERISK_GPL_KEY, AST_MODFLAG_LOAD_ORDER, "OGG/Vorbis audio",
561         .load = load_module,
562         .unload = unload_module,
563         .load_pri = AST_MODPRI_APP_DEPEND
564 );