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