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