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