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