2 * Asterisk -- An open source telephony toolkit.
4 * Copyright (C) 2005, Jeff Ollie
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.
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.
19 * \brief OGG/Vorbis streams.
20 * \arg File name extension: ogg
24 #include <sys/types.h>
25 #include <netinet/in.h>
26 #include <arpa/inet.h>
34 #include <vorbis/codec.h>
35 #include <vorbis/vorbisenc.h>
44 ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
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"
53 * this is the number of samples we deal with. Samples are converted
54 * to SLINEAR so each one uses 2 bytes in the buffer.
56 #define SAMPLES_MAX 160
57 #define BUF_SIZE (2*SAMPLES_MAX)
59 #define BLOCK_SIZE 4096 /* used internally in the vorbis routines */
61 struct vorbis_desc { /* format specific parameters */
62 /* structures for handling the Ogg container */
68 /* structures for handling Vorbis audio data */
74 /*! \brief Indicates whether this filestream is set up for reading or writing. */
77 /*! \brief Indicates whether an End of Stream condition has been detected. */
82 * \brief Create a new OGG/Vorbis filestream and set it up for reading.
83 * \param f File that points to on disk storage of the OGG/Vorbis data.
84 * \return The new filestream.
86 static int ogg_vorbis_open(struct ast_filestream *s)
93 struct vorbis_desc *tmp = (struct vorbis_desc *)s->private;
97 ogg_sync_init(&tmp->oy);
99 buffer = ogg_sync_buffer(&tmp->oy, BLOCK_SIZE);
100 bytes = fread(buffer, 1, BLOCK_SIZE, s->f);
101 ogg_sync_wrote(&tmp->oy, bytes);
103 result = ogg_sync_pageout(&tmp->oy, &tmp->og);
105 if(bytes < BLOCK_SIZE) {
106 ast_log(LOG_ERROR, "Run out of data...\n");
108 ast_log(LOG_ERROR, "Input does not appear to be an Ogg bitstream.\n");
110 ogg_sync_clear(&tmp->oy);
114 ogg_stream_init(&tmp->os, ogg_page_serialno(&tmp->og));
115 vorbis_info_init(&tmp->vi);
116 vorbis_comment_init(&tmp->vc);
118 if (ogg_stream_pagein(&tmp->os, &tmp->og) < 0) {
119 ast_log(LOG_ERROR, "Error reading first page of Ogg bitstream data.\n");
121 ogg_stream_clear(&tmp->os);
122 vorbis_comment_clear(&tmp->vc);
123 vorbis_info_clear(&tmp->vi);
124 ogg_sync_clear(&tmp->oy);
128 if (ogg_stream_packetout(&tmp->os, &tmp->op) != 1) {
129 ast_log(LOG_ERROR, "Error reading initial header packet.\n");
133 if (vorbis_synthesis_headerin(&tmp->vi, &tmp->vc, &tmp->op) < 0) {
134 ast_log(LOG_ERROR, "This Ogg bitstream does not contain Vorbis audio data.\n");
138 for (i = 0; i < 2 ; ) {
140 result = ogg_sync_pageout(&tmp->oy, &tmp->og);
144 ogg_stream_pagein(&tmp->os, &tmp->og);
146 result = ogg_stream_packetout(&tmp->os,&tmp->op);
150 ast_log(LOG_ERROR, "Corrupt secondary header. Exiting.\n");
153 vorbis_synthesis_headerin(&tmp->vi, &tmp->vc, &tmp->op);
159 buffer = ogg_sync_buffer(&tmp->oy, BLOCK_SIZE);
160 bytes = fread(buffer, 1, BLOCK_SIZE, s->f);
161 if (bytes == 0 && i < 2) {
162 ast_log(LOG_ERROR, "End of file before finding all Vorbis headers!\n");
165 ogg_sync_wrote(&tmp->oy, bytes);
168 for (ptr = tmp->vc.user_comments; *ptr; ptr++)
169 ast_log(LOG_DEBUG, "OGG/Vorbis comment: %s\n", *ptr);
170 ast_log(LOG_DEBUG, "OGG/Vorbis bitstream is %d channel, %ldHz\n", tmp->vi.channels, tmp->vi.rate);
171 ast_log(LOG_DEBUG, "OGG/Vorbis file encoded by: %s\n", tmp->vc.vendor);
173 if (tmp->vi.channels != 1) {
174 ast_log(LOG_ERROR, "Only monophonic OGG/Vorbis files are currently supported!\n");
178 if (tmp->vi.rate != DEFAULT_SAMPLE_RATE) {
179 ast_log(LOG_ERROR, "Only 8000Hz OGG/Vorbis files are currently supported!\n");
180 vorbis_block_clear(&tmp->vb);
181 vorbis_dsp_clear(&tmp->vd);
185 vorbis_synthesis_init(&tmp->vd, &tmp->vi);
186 vorbis_block_init(&tmp->vd, &tmp->vb);
192 * \brief Create a new OGG/Vorbis filestream and set it up for writing.
193 * \param f File pointer that points to on-disk storage.
194 * \param comment Comment that should be embedded in the OGG/Vorbis file.
195 * \return A new filestream.
197 static int ogg_vorbis_rewrite(struct ast_filestream *s,
201 ogg_packet header_comm;
202 ogg_packet header_code;
203 struct vorbis_desc *tmp = (struct vorbis_desc *)s->private;
207 vorbis_info_init(&tmp->vi);
209 if (vorbis_encode_init_vbr(&tmp->vi, 1, DEFAULT_SAMPLE_RATE, 0.4)) {
210 ast_log(LOG_ERROR, "Unable to initialize Vorbis encoder!\n");
214 vorbis_comment_init(&tmp->vc);
215 vorbis_comment_add_tag(&tmp->vc, "ENCODER", "Asterisk PBX");
217 vorbis_comment_add_tag(&tmp->vc, "COMMENT", (char *) comment);
219 vorbis_analysis_init(&tmp->vd, &tmp->vi);
220 vorbis_block_init(&tmp->vd, &tmp->vb);
222 ogg_stream_init(&tmp->os, ast_random());
224 vorbis_analysis_headerout(&tmp->vd, &tmp->vc, &header, &header_comm,
226 ogg_stream_packetin(&tmp->os, &header);
227 ogg_stream_packetin(&tmp->os, &header_comm);
228 ogg_stream_packetin(&tmp->os, &header_code);
231 if (ogg_stream_flush(&tmp->os, &tmp->og) == 0)
233 fwrite(tmp->og.header, 1, tmp->og.header_len, s->f);
234 fwrite(tmp->og.body, 1, tmp->og.body_len, s->f);
235 if (ogg_page_eos(&tmp->og))
243 * \brief Write out any pending encoded data.
244 * \param s A OGG/Vorbis filestream.
246 static void write_stream(struct vorbis_desc *s, FILE *f)
248 while (vorbis_analysis_blockout(&s->vd, &s->vb) == 1) {
249 vorbis_analysis(&s->vb, NULL);
250 vorbis_bitrate_addblock(&s->vb);
252 while (vorbis_bitrate_flushpacket(&s->vd, &s->op)) {
253 ogg_stream_packetin(&s->os, &s->op);
255 if (ogg_stream_pageout(&s->os, &s->og) == 0) {
258 fwrite(s->og.header, 1, s->og.header_len, f);
259 fwrite(s->og.body, 1, s->og.body_len, f);
260 if (ogg_page_eos(&s->og)) {
269 * \brief Write audio data from a frame to an OGG/Vorbis filestream.
270 * \param s A OGG/Vorbis filestream.
271 * \param f An frame containing audio to be written to the filestream.
272 * \return -1 ifthere was an error, 0 on success.
274 static int ogg_vorbis_write(struct ast_filestream *fs, struct ast_frame *f)
279 struct vorbis_desc *s = (struct vorbis_desc *)fs->private;
282 ast_log(LOG_ERROR, "This stream is not set up for writing!\n");
286 if (f->frametype != AST_FRAME_VOICE) {
287 ast_log(LOG_WARNING, "Asked to write non-voice frame!\n");
290 if (f->subclass != AST_FORMAT_SLINEAR) {
291 ast_log(LOG_WARNING, "Asked to write non-SLINEAR frame (%d)!\n",
298 data = (short *) f->data;
300 buffer = vorbis_analysis_buffer(&s->vd, f->samples);
302 for (i = 0; i < f->samples; i++)
303 buffer[0][i] = (double)data[i] / 32768.0;
305 vorbis_analysis_wrote(&s->vd, f->samples);
307 write_stream(s, fs->f);
313 * \brief Close a OGG/Vorbis filestream.
314 * \param s A OGG/Vorbis filestream.
316 static void ogg_vorbis_close(struct ast_filestream *fs)
318 struct vorbis_desc *s = (struct vorbis_desc *)fs->private;
321 /* Tell the Vorbis encoder that the stream is finished
322 * and write out the rest of the data */
323 vorbis_analysis_wrote(&s->vd, 0);
324 write_stream(s, fs->f);
327 ogg_stream_clear(&s->os);
328 vorbis_block_clear(&s->vb);
329 vorbis_dsp_clear(&s->vd);
330 vorbis_comment_clear(&s->vc);
331 vorbis_info_clear(&s->vi);
334 ogg_sync_clear(&s->oy);
339 * \brief Get audio data.
340 * \param s An OGG/Vorbis filestream.
341 * \param pcm Pointer to a buffere to store audio data in.
344 static int read_samples(struct ast_filestream *fs, float ***pcm)
350 struct vorbis_desc *s = (struct vorbis_desc *)fs->private;
353 samples_in = vorbis_synthesis_pcmout(&s->vd, pcm);
354 if (samples_in > 0) {
358 /* The Vorbis decoder needs more data... */
359 /* See ifOGG has any packets in the current page for the Vorbis decoder. */
360 result = ogg_stream_packetout(&s->os, &s->op);
362 /* Yes OGG had some more packets for the Vorbis decoder. */
363 if (vorbis_synthesis(&s->vb, &s->op) == 0) {
364 vorbis_synthesis_blockin(&s->vd, &s->vb);
372 "Corrupt or missing data at this page position; continuing...\n");
374 /* No more packets left in the current page... */
377 /* No more pages left in the stream */
382 /* See ifOGG has any pages in it's internal buffers */
383 result = ogg_sync_pageout(&s->oy, &s->og);
385 /* Yes, OGG has more pages in it's internal buffers,
386 add the page to the stream state */
387 result = ogg_stream_pagein(&s->os, &s->og);
389 /* Yes, got a new,valid page */
390 if (ogg_page_eos(&s->og)) {
396 "Invalid page in the bitstream; continuing...\n");
401 "Corrupt or missing data in bitstream; continuing...\n");
403 /* No, we need to read more data from the file descrptor */
404 /* get a buffer from OGG to read the data into */
405 buffer = ogg_sync_buffer(&s->oy, BLOCK_SIZE);
406 /* read more data from the file descriptor */
407 bytes = fread(buffer, 1, BLOCK_SIZE, fs->f);
408 /* Tell OGG how many bytes we actually read into the buffer */
409 ogg_sync_wrote(&s->oy, bytes);
418 * \brief Read a frame full of audio data from the filestream.
419 * \param s The filestream.
420 * \param whennext Number of sample times to schedule the next call.
421 * \return A pointer to a frame containing audio data or NULL ifthere is no more audio data.
423 static struct ast_frame *ogg_vorbis_read(struct ast_filestream *fs,
429 double accumulator[SAMPLES_MAX];
433 struct vorbis_desc *s = (struct vorbis_desc *)fs->private;
434 short *buf = (short *)(fs->fr.data); /* SLIN data buffer */
436 fs->fr.frametype = AST_FRAME_VOICE;
437 fs->fr.subclass = AST_FORMAT_SLINEAR;
439 FR_SET_BUF(&fs->fr, fs->buf, AST_FRIENDLY_OFFSET, BUF_SIZE);
441 while (samples_out != SAMPLES_MAX) {
443 int len = SAMPLES_MAX - samples_out;
445 /* See ifVorbis decoder has some audio data for us ... */
446 samples_in = read_samples(fs, &pcm);
450 /* Got some audio data from Vorbis... */
451 /* Convert the float audio data to 16-bit signed linear */
454 if (samples_in > len)
456 for (j = 0; j < samples_in; j++)
457 accumulator[j] = 0.0;
459 for (i = 0; i < s->vi.channels; i++) {
460 float *mono = pcm[i];
461 for (j = 0; j < samples_in; j++)
462 accumulator[j] += mono[j];
465 for (j = 0; j < samples_in; j++) {
466 val = accumulator[j] * 32767.0 / s->vi.channels;
470 } else if (val < -32768) {
474 buf[samples_out + j] = val;
478 ast_log(LOG_WARNING, "Clipping in frame %ld\n", (long) (s->vd.sequence));
479 /* Tell the Vorbis decoder how many samples we actually used. */
480 vorbis_synthesis_read(&s->vd, samples_in);
481 samples_out += samples_in;
484 if (samples_out > 0) {
485 fs->fr.datalen = samples_out * 2;
486 fs->fr.samples = samples_out;
487 *whennext = samples_out;
496 * \brief Trucate an OGG/Vorbis filestream.
497 * \param s The filestream to truncate.
498 * \return 0 on success, -1 on failure.
501 static int ogg_vorbis_trunc(struct ast_filestream *s)
503 ast_log(LOG_WARNING, "Truncation is not supported on OGG/Vorbis streams!\n");
508 * \brief Seek to a specific position in an OGG/Vorbis filestream.
509 * \param s The filestream to truncate.
510 * \param sample_offset New position for the filestream, measured in 8KHz samples.
511 * \param whence Location to measure
512 * \return 0 on success, -1 on failure.
514 static int ogg_vorbis_seek(struct ast_filestream *s, off_t sample_offset, int whence)
516 ast_log(LOG_WARNING, "Seeking is not supported on OGG/Vorbis streams!\n");
520 static off_t ogg_vorbis_tell(struct ast_filestream *s)
522 ast_log(LOG_WARNING, "Telling is not supported on OGG/Vorbis streams!\n");
526 static struct ast_format_lock me = { .usecnt = -1 };
528 static const struct ast_format vorbis_f = {
529 .name = "ogg_vorbis",
531 .format = AST_FORMAT_SLINEAR,
532 .open = ogg_vorbis_open,
533 .rewrite = ogg_vorbis_rewrite,
534 .write = ogg_vorbis_write,
535 .seek = ogg_vorbis_seek,
536 .trunc = ogg_vorbis_trunc,
537 .tell = ogg_vorbis_tell,
538 .read = ogg_vorbis_read,
539 .close = ogg_vorbis_close,
540 .buf_size = BUF_SIZE + AST_FRIENDLY_OFFSET,
541 .desc_size = sizeof(struct vorbis_desc),
547 return ast_format_register(&vorbis_f);
552 return ast_format_unregister(vorbis_f.name);
560 const char *description()
562 return "OGG/Vorbis audio";
568 return ASTERISK_GPL_KEY;