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