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