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 /* the order of these dependencies is important... it also specifies
25 the link order of the libraries during linking
29 <depend>vorbis</depend>
35 ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
37 #include <sys/types.h>
38 #include <netinet/in.h>
39 #include <arpa/inet.h>
47 #include <vorbis/codec.h>
48 #include <vorbis/vorbisenc.h>
55 #include "asterisk/lock.h"
56 #include "asterisk/channel.h"
57 #include "asterisk/file.h"
58 #include "asterisk/logger.h"
59 #include "asterisk/module.h"
60 #include "asterisk/options.h"
63 * this is the number of samples we deal with. Samples are converted
64 * to SLINEAR so each one uses 2 bytes in the buffer.
66 #define SAMPLES_MAX 160
67 #define BUF_SIZE (2*SAMPLES_MAX)
69 #define BLOCK_SIZE 4096 /* used internally in the vorbis routines */
71 struct vorbis_desc { /* format specific parameters */
72 /* structures for handling the Ogg container */
78 /* structures for handling Vorbis audio data */
84 /*! \brief Indicates whether this filestream is set up for reading or writing. */
87 /*! \brief Indicates whether an End of Stream condition has been detected. */
92 * \brief Create a new OGG/Vorbis filestream and set it up for reading.
93 * \param s File that points to on disk storage of the OGG/Vorbis data.
94 * \return The new filestream.
96 static int ogg_vorbis_open(struct ast_filestream *s)
103 struct vorbis_desc *tmp = (struct vorbis_desc *)s->private;
107 ogg_sync_init(&tmp->oy);
109 buffer = ogg_sync_buffer(&tmp->oy, BLOCK_SIZE);
110 bytes = fread(buffer, 1, BLOCK_SIZE, s->f);
111 ogg_sync_wrote(&tmp->oy, bytes);
113 result = ogg_sync_pageout(&tmp->oy, &tmp->og);
115 if(bytes < BLOCK_SIZE) {
116 ast_log(LOG_ERROR, "Run out of data...\n");
118 ast_log(LOG_ERROR, "Input does not appear to be an Ogg bitstream.\n");
120 ogg_sync_clear(&tmp->oy);
124 ogg_stream_init(&tmp->os, ogg_page_serialno(&tmp->og));
125 vorbis_info_init(&tmp->vi);
126 vorbis_comment_init(&tmp->vc);
128 if (ogg_stream_pagein(&tmp->os, &tmp->og) < 0) {
129 ast_log(LOG_ERROR, "Error reading first page of Ogg bitstream data.\n");
131 ogg_stream_clear(&tmp->os);
132 vorbis_comment_clear(&tmp->vc);
133 vorbis_info_clear(&tmp->vi);
134 ogg_sync_clear(&tmp->oy);
138 if (ogg_stream_packetout(&tmp->os, &tmp->op) != 1) {
139 ast_log(LOG_ERROR, "Error reading initial header packet.\n");
143 if (vorbis_synthesis_headerin(&tmp->vi, &tmp->vc, &tmp->op) < 0) {
144 ast_log(LOG_ERROR, "This Ogg bitstream does not contain Vorbis audio data.\n");
148 for (i = 0; i < 2 ; ) {
150 result = ogg_sync_pageout(&tmp->oy, &tmp->og);
154 ogg_stream_pagein(&tmp->os, &tmp->og);
156 result = ogg_stream_packetout(&tmp->os,&tmp->op);
160 ast_log(LOG_ERROR, "Corrupt secondary header. Exiting.\n");
163 vorbis_synthesis_headerin(&tmp->vi, &tmp->vc, &tmp->op);
169 buffer = ogg_sync_buffer(&tmp->oy, BLOCK_SIZE);
170 bytes = fread(buffer, 1, BLOCK_SIZE, s->f);
171 if (bytes == 0 && i < 2) {
172 ast_log(LOG_ERROR, "End of file before finding all Vorbis headers!\n");
175 ogg_sync_wrote(&tmp->oy, bytes);
178 for (ptr = tmp->vc.user_comments; *ptr; ptr++)
179 ast_debug(1, "OGG/Vorbis comment: %s\n", *ptr);
180 ast_debug(1, "OGG/Vorbis bitstream is %d channel, %ldHz\n", tmp->vi.channels, tmp->vi.rate);
181 ast_debug(1, "OGG/Vorbis file encoded by: %s\n", tmp->vc.vendor);
183 if (tmp->vi.channels != 1) {
184 ast_log(LOG_ERROR, "Only monophonic OGG/Vorbis files are currently supported!\n");
188 if (tmp->vi.rate != DEFAULT_SAMPLE_RATE) {
189 ast_log(LOG_ERROR, "Only 8000Hz OGG/Vorbis files are currently supported!\n");
190 vorbis_block_clear(&tmp->vb);
191 vorbis_dsp_clear(&tmp->vd);
195 vorbis_synthesis_init(&tmp->vd, &tmp->vi);
196 vorbis_block_init(&tmp->vd, &tmp->vb);
202 * \brief Create a new OGG/Vorbis filestream and set it up for writing.
203 * \param s File pointer that points to on-disk storage.
204 * \param comment Comment that should be embedded in the OGG/Vorbis file.
205 * \return A new filestream.
207 static int ogg_vorbis_rewrite(struct ast_filestream *s,
211 ogg_packet header_comm;
212 ogg_packet header_code;
213 struct vorbis_desc *tmp = (struct vorbis_desc *)s->private;
217 vorbis_info_init(&tmp->vi);
219 if (vorbis_encode_init_vbr(&tmp->vi, 1, DEFAULT_SAMPLE_RATE, 0.4)) {
220 ast_log(LOG_ERROR, "Unable to initialize Vorbis encoder!\n");
224 vorbis_comment_init(&tmp->vc);
225 vorbis_comment_add_tag(&tmp->vc, "ENCODER", "Asterisk PBX");
227 vorbis_comment_add_tag(&tmp->vc, "COMMENT", (char *) comment);
229 vorbis_analysis_init(&tmp->vd, &tmp->vi);
230 vorbis_block_init(&tmp->vd, &tmp->vb);
232 ogg_stream_init(&tmp->os, ast_random());
234 vorbis_analysis_headerout(&tmp->vd, &tmp->vc, &header, &header_comm,
236 ogg_stream_packetin(&tmp->os, &header);
237 ogg_stream_packetin(&tmp->os, &header_comm);
238 ogg_stream_packetin(&tmp->os, &header_code);
241 if (ogg_stream_flush(&tmp->os, &tmp->og) == 0)
243 fwrite(tmp->og.header, 1, tmp->og.header_len, s->f);
244 fwrite(tmp->og.body, 1, tmp->og.body_len, s->f);
245 if (ogg_page_eos(&tmp->og))
253 * \brief Write out any pending encoded data.
254 * \param s An OGG/Vorbis filestream.
255 * \param f The file to write to.
257 static void write_stream(struct vorbis_desc *s, FILE *f)
259 while (vorbis_analysis_blockout(&s->vd, &s->vb) == 1) {
260 vorbis_analysis(&s->vb, NULL);
261 vorbis_bitrate_addblock(&s->vb);
263 while (vorbis_bitrate_flushpacket(&s->vd, &s->op)) {
264 ogg_stream_packetin(&s->os, &s->op);
266 if (ogg_stream_pageout(&s->os, &s->og) == 0) {
269 fwrite(s->og.header, 1, s->og.header_len, f);
270 fwrite(s->og.body, 1, s->og.body_len, f);
271 if (ogg_page_eos(&s->og)) {
280 * \brief Write audio data from a frame to an OGG/Vorbis filestream.
281 * \param fs An OGG/Vorbis filestream.
282 * \param f A frame containing audio to be written to the filestream.
283 * \return -1 if there was an error, 0 on success.
285 static int ogg_vorbis_write(struct ast_filestream *fs, struct ast_frame *f)
290 struct vorbis_desc *s = (struct vorbis_desc *)fs->private;
293 ast_log(LOG_ERROR, "This stream is not set up for writing!\n");
297 if (f->frametype != AST_FRAME_VOICE) {
298 ast_log(LOG_WARNING, "Asked to write non-voice frame!\n");
301 if (f->subclass != AST_FORMAT_SLINEAR) {
302 ast_log(LOG_WARNING, "Asked to write non-SLINEAR frame (%d)!\n",
309 data = (short *) f->data;
311 buffer = vorbis_analysis_buffer(&s->vd, f->samples);
313 for (i = 0; i < f->samples; i++)
314 buffer[0][i] = (double)data[i] / 32768.0;
316 vorbis_analysis_wrote(&s->vd, f->samples);
318 write_stream(s, fs->f);
324 * \brief Close a OGG/Vorbis filestream.
325 * \param fs A OGG/Vorbis filestream.
327 static void ogg_vorbis_close(struct ast_filestream *fs)
329 struct vorbis_desc *s = (struct vorbis_desc *)fs->private;
332 /* Tell the Vorbis encoder that the stream is finished
333 * and write out the rest of the data */
334 vorbis_analysis_wrote(&s->vd, 0);
335 write_stream(s, fs->f);
338 ogg_stream_clear(&s->os);
339 vorbis_block_clear(&s->vb);
340 vorbis_dsp_clear(&s->vd);
341 vorbis_comment_clear(&s->vc);
342 vorbis_info_clear(&s->vi);
345 ogg_sync_clear(&s->oy);
350 * \brief Get audio data.
351 * \param fs An OGG/Vorbis filestream.
352 * \param pcm Pointer to a buffere to store audio data in.
355 static int read_samples(struct ast_filestream *fs, float ***pcm)
361 struct vorbis_desc *s = (struct vorbis_desc *)fs->private;
364 samples_in = vorbis_synthesis_pcmout(&s->vd, pcm);
365 if (samples_in > 0) {
369 /* The Vorbis decoder needs more data... */
370 /* See ifOGG has any packets in the current page for the Vorbis decoder. */
371 result = ogg_stream_packetout(&s->os, &s->op);
373 /* Yes OGG had some more packets for the Vorbis decoder. */
374 if (vorbis_synthesis(&s->vb, &s->op) == 0) {
375 vorbis_synthesis_blockin(&s->vd, &s->vb);
383 "Corrupt or missing data at this page position; continuing...\n");
385 /* No more packets left in the current page... */
388 /* No more pages left in the stream */
393 /* See ifOGG has any pages in it's internal buffers */
394 result = ogg_sync_pageout(&s->oy, &s->og);
396 /* Yes, OGG has more pages in it's internal buffers,
397 add the page to the stream state */
398 result = ogg_stream_pagein(&s->os, &s->og);
400 /* Yes, got a new,valid page */
401 if (ogg_page_eos(&s->og)) {
407 "Invalid page in the bitstream; continuing...\n");
412 "Corrupt or missing data in bitstream; continuing...\n");
414 /* No, we need to read more data from the file descrptor */
415 /* get a buffer from OGG to read the data into */
416 buffer = ogg_sync_buffer(&s->oy, BLOCK_SIZE);
417 /* read more data from the file descriptor */
418 bytes = fread(buffer, 1, BLOCK_SIZE, fs->f);
419 /* Tell OGG how many bytes we actually read into the buffer */
420 ogg_sync_wrote(&s->oy, bytes);
429 * \brief Read a frame full of audio data from the filestream.
430 * \param fs The filestream.
431 * \param whennext Number of sample times to schedule the next call.
432 * \return A pointer to a frame containing audio data or NULL ifthere is no more audio data.
434 static struct ast_frame *ogg_vorbis_read(struct ast_filestream *fs,
440 double accumulator[SAMPLES_MAX];
444 struct vorbis_desc *s = (struct vorbis_desc *)fs->private;
445 short *buf; /* SLIN data buffer */
447 fs->fr.frametype = AST_FRAME_VOICE;
448 fs->fr.subclass = AST_FORMAT_SLINEAR;
450 AST_FRAME_SET_BUFFER(&fs->fr, fs->buf, AST_FRIENDLY_OFFSET, BUF_SIZE);
451 buf = (short *)(fs->fr.data); /* SLIN data buffer */
453 while (samples_out != SAMPLES_MAX) {
455 int len = SAMPLES_MAX - samples_out;
457 /* See ifVorbis decoder has some audio data for us ... */
458 samples_in = read_samples(fs, &pcm);
462 /* Got some audio data from Vorbis... */
463 /* Convert the float audio data to 16-bit signed linear */
466 if (samples_in > len)
468 for (j = 0; j < samples_in; j++)
469 accumulator[j] = 0.0;
471 for (i = 0; i < s->vi.channels; i++) {
472 float *mono = pcm[i];
473 for (j = 0; j < samples_in; j++)
474 accumulator[j] += mono[j];
477 for (j = 0; j < samples_in; j++) {
478 val = accumulator[j] * 32767.0 / s->vi.channels;
482 } else if (val < -32768) {
486 buf[samples_out + j] = val;
490 ast_log(LOG_WARNING, "Clipping in frame %ld\n", (long) (s->vd.sequence));
491 /* Tell the Vorbis decoder how many samples we actually used. */
492 vorbis_synthesis_read(&s->vd, samples_in);
493 samples_out += samples_in;
496 if (samples_out > 0) {
497 fs->fr.datalen = samples_out * 2;
498 fs->fr.samples = samples_out;
499 *whennext = samples_out;
508 * \brief Trucate an OGG/Vorbis filestream.
509 * \param s The filestream to truncate.
510 * \return 0 on success, -1 on failure.
513 static int ogg_vorbis_trunc(struct ast_filestream *s)
515 ast_log(LOG_WARNING, "Truncation is not supported on OGG/Vorbis streams!\n");
520 * \brief Seek to a specific position in an OGG/Vorbis filestream.
521 * \param s The filestream to truncate.
522 * \param sample_offset New position for the filestream, measured in 8KHz samples.
523 * \param whence Location to measure
524 * \return 0 on success, -1 on failure.
526 static int ogg_vorbis_seek(struct ast_filestream *s, off_t sample_offset, int whence)
528 ast_log(LOG_WARNING, "Seeking is not supported on OGG/Vorbis streams!\n");
532 static off_t ogg_vorbis_tell(struct ast_filestream *s)
534 ast_log(LOG_WARNING, "Telling is not supported on OGG/Vorbis streams!\n");
538 static const struct ast_format vorbis_f = {
539 .name = "ogg_vorbis",
541 .format = AST_FORMAT_SLINEAR,
542 .open = ogg_vorbis_open,
543 .rewrite = ogg_vorbis_rewrite,
544 .write = ogg_vorbis_write,
545 .seek = ogg_vorbis_seek,
546 .trunc = ogg_vorbis_trunc,
547 .tell = ogg_vorbis_tell,
548 .read = ogg_vorbis_read,
549 .close = ogg_vorbis_close,
550 .buf_size = BUF_SIZE + AST_FRIENDLY_OFFSET,
551 .desc_size = sizeof(struct vorbis_desc),
554 static int load_module(void)
556 return ast_format_register(&vorbis_f);
559 static int unload_module(void)
561 return ast_format_unregister(vorbis_f.name);
564 AST_MODULE_INFO_STANDARD(ASTERISK_GPL_KEY, "OGG/Vorbis audio");