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