format_ogg_vorbis.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454
  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. /*! \file
  17. *
  18. * \brief OGG/Vorbis streams.
  19. * \arg File name extension: ogg
  20. * \ingroup formats
  21. */
  22. /* the order of these dependencies is important... it also specifies
  23. the link order of the libraries during linking
  24. */
  25. /*** MODULEINFO
  26. <depend>vorbis</depend>
  27. <depend>ogg</depend>
  28. <support_level>core</support_level>
  29. ***/
  30. #include "asterisk.h"
  31. ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
  32. #include <vorbis/codec.h>
  33. #include <vorbis/vorbisenc.h>
  34. #include <vorbis/vorbisfile.h>
  35. #ifdef _WIN32
  36. #include <io.h>
  37. #endif
  38. #include "asterisk/mod_format.h"
  39. #include "asterisk/module.h"
  40. /*
  41. * this is the number of samples we deal with. Samples are converted
  42. * to SLINEAR so each one uses 2 bytes in the buffer.
  43. */
  44. #define SAMPLES_MAX 512
  45. #define BUF_SIZE (2*SAMPLES_MAX)
  46. #define BLOCK_SIZE 4096 /* used internally in the vorbis routines */
  47. struct ogg_vorbis_desc { /* format specific parameters */
  48. /* OggVorbis_File structure for libvorbisfile interface */
  49. OggVorbis_File ov_f;
  50. /* structures for handling the Ogg container */
  51. ogg_stream_state os;
  52. ogg_page og;
  53. ogg_packet op;
  54. /* structures for handling Vorbis audio data */
  55. vorbis_info vi;
  56. vorbis_comment vc;
  57. vorbis_dsp_state vd;
  58. vorbis_block vb;
  59. /*! \brief Indicates whether this filestream is set up for reading or writing. */
  60. int writing;
  61. /*! \brief Stores the current pcm position to support tell() on writing mode. */
  62. off_t writing_pcm_pos;
  63. /*! \brief Indicates whether an End of Stream condition has been detected. */
  64. int eos;
  65. };
  66. #if !defined(HAVE_VORBIS_OPEN_CALLBACKS)
  67. /*
  68. * Declared for backward compatibility with vorbisfile v1.1.2.
  69. * Code taken from vorbisfile.h v1.2.0.
  70. */
  71. static int _ov_header_fseek_wrap(FILE *f, ogg_int64_t off, int whence)
  72. {
  73. if (f == NULL) {
  74. return -1;
  75. }
  76. return fseek(f, off, whence);
  77. }
  78. static ov_callbacks OV_CALLBACKS_NOCLOSE = {
  79. (size_t (*)(void *, size_t, size_t, void *)) fread,
  80. (int (*)(void *, ogg_int64_t, int)) _ov_header_fseek_wrap,
  81. (int (*)(void *)) NULL,
  82. (long (*)(void *)) ftell
  83. };
  84. #endif /* !defined(HAVE_VORBIS_OPEN_CALLBACKS) */
  85. /*!
  86. * \brief Create a new OGG/Vorbis filestream and set it up for reading.
  87. * \param s File that points to on disk storage of the OGG/Vorbis data.
  88. * \return The new filestream.
  89. */
  90. static int ogg_vorbis_open(struct ast_filestream *s)
  91. {
  92. int result;
  93. struct ogg_vorbis_desc *desc = (struct ogg_vorbis_desc *) s->_private;
  94. /* initialize private description block */
  95. memset(desc, 0, sizeof(struct ogg_vorbis_desc));
  96. desc->writing = 0;
  97. /* actually open file */
  98. result = ov_open_callbacks(s->f, &desc->ov_f, NULL, 0, OV_CALLBACKS_NOCLOSE);
  99. if (result != 0) {
  100. ast_log(LOG_ERROR, "Error opening Ogg/Vorbis file stream.\n");
  101. return -1;
  102. }
  103. /* check stream(s) type */
  104. if (desc->ov_f.vi->channels != 1) {
  105. ast_log(LOG_ERROR, "Only monophonic OGG/Vorbis files are currently supported!\n");
  106. ov_clear(&desc->ov_f);
  107. return -1;
  108. }
  109. if (desc->ov_f.vi->rate != DEFAULT_SAMPLE_RATE) {
  110. ast_log(LOG_ERROR, "Only 8000Hz OGG/Vorbis files are currently supported!\n");
  111. ov_clear(&desc->ov_f);
  112. return -1;
  113. }
  114. return 0;
  115. }
  116. /*!
  117. * \brief Create a new OGG/Vorbis filestream and set it up for writing.
  118. * \param s File pointer that points to on-disk storage.
  119. * \param comment Comment that should be embedded in the OGG/Vorbis file.
  120. * \return A new filestream.
  121. */
  122. static int ogg_vorbis_rewrite(struct ast_filestream *s,
  123. const char *comment)
  124. {
  125. ogg_packet header;
  126. ogg_packet header_comm;
  127. ogg_packet header_code;
  128. struct ogg_vorbis_desc *tmp = (struct ogg_vorbis_desc *) s->_private;
  129. tmp->writing = 1;
  130. tmp->writing_pcm_pos = 0;
  131. vorbis_info_init(&tmp->vi);
  132. if (vorbis_encode_init_vbr(&tmp->vi, 1, DEFAULT_SAMPLE_RATE, 0.4)) {
  133. ast_log(LOG_ERROR, "Unable to initialize Vorbis encoder!\n");
  134. return -1;
  135. }
  136. vorbis_comment_init(&tmp->vc);
  137. vorbis_comment_add_tag(&tmp->vc, "ENCODER", "Asterisk PBX");
  138. if (comment)
  139. vorbis_comment_add_tag(&tmp->vc, "COMMENT", (char *) comment);
  140. vorbis_analysis_init(&tmp->vd, &tmp->vi);
  141. vorbis_block_init(&tmp->vd, &tmp->vb);
  142. ogg_stream_init(&tmp->os, ast_random());
  143. vorbis_analysis_headerout(&tmp->vd, &tmp->vc, &header, &header_comm,
  144. &header_code);
  145. ogg_stream_packetin(&tmp->os, &header);
  146. ogg_stream_packetin(&tmp->os, &header_comm);
  147. ogg_stream_packetin(&tmp->os, &header_code);
  148. while (!tmp->eos) {
  149. if (ogg_stream_flush(&tmp->os, &tmp->og) == 0)
  150. break;
  151. if (!fwrite(tmp->og.header, 1, tmp->og.header_len, s->f)) {
  152. ast_log(LOG_WARNING, "fwrite() failed: %s\n", strerror(errno));
  153. }
  154. if (!fwrite(tmp->og.body, 1, tmp->og.body_len, s->f)) {
  155. ast_log(LOG_WARNING, "fwrite() failed: %s\n", strerror(errno));
  156. }
  157. if (ogg_page_eos(&tmp->og))
  158. tmp->eos = 1;
  159. }
  160. return 0;
  161. }
  162. /*!
  163. * \brief Write out any pending encoded data.
  164. * \param s An OGG/Vorbis filestream.
  165. * \param f The file to write to.
  166. */
  167. static void write_stream(struct ogg_vorbis_desc *s, FILE *f)
  168. {
  169. while (vorbis_analysis_blockout(&s->vd, &s->vb) == 1) {
  170. vorbis_analysis(&s->vb, NULL);
  171. vorbis_bitrate_addblock(&s->vb);
  172. while (vorbis_bitrate_flushpacket(&s->vd, &s->op)) {
  173. ogg_stream_packetin(&s->os, &s->op);
  174. while (!s->eos) {
  175. if (ogg_stream_pageout(&s->os, &s->og) == 0) {
  176. break;
  177. }
  178. if (!fwrite(s->og.header, 1, s->og.header_len, f)) {
  179. ast_log(LOG_WARNING, "fwrite() failed: %s\n", strerror(errno));
  180. }
  181. if (!fwrite(s->og.body, 1, s->og.body_len, f)) {
  182. ast_log(LOG_WARNING, "fwrite() failed: %s\n", strerror(errno));
  183. }
  184. if (ogg_page_eos(&s->og)) {
  185. s->eos = 1;
  186. }
  187. }
  188. }
  189. }
  190. }
  191. /*!
  192. * \brief Write audio data from a frame to an OGG/Vorbis filestream.
  193. * \param fs An OGG/Vorbis filestream.
  194. * \param f A frame containing audio to be written to the filestream.
  195. * \return -1 if there was an error, 0 on success.
  196. */
  197. static int ogg_vorbis_write(struct ast_filestream *fs, struct ast_frame *f)
  198. {
  199. int i;
  200. float **buffer;
  201. short *data;
  202. struct ogg_vorbis_desc *s = (struct ogg_vorbis_desc *) fs->_private;
  203. if (!s->writing) {
  204. ast_log(LOG_ERROR, "This stream is not set up for writing!\n");
  205. return -1;
  206. }
  207. if (f->frametype != AST_FRAME_VOICE) {
  208. ast_log(LOG_WARNING, "Asked to write non-voice frame!\n");
  209. return -1;
  210. }
  211. if (f->subclass.format.id != AST_FORMAT_SLINEAR) {
  212. ast_log(LOG_WARNING, "Asked to write non-SLINEAR frame (%s)!\n",
  213. ast_getformatname(&f->subclass.format));
  214. return -1;
  215. }
  216. if (!f->datalen)
  217. return -1;
  218. data = (short *) f->data.ptr;
  219. buffer = vorbis_analysis_buffer(&s->vd, f->samples);
  220. for (i = 0; i < f->samples; i++)
  221. buffer[0][i] = (double)data[i] / 32768.0;
  222. vorbis_analysis_wrote(&s->vd, f->samples);
  223. write_stream(s, fs->f);
  224. s->writing_pcm_pos += f->samples;
  225. return 0;
  226. }
  227. /*!
  228. * \brief Close a OGG/Vorbis filestream.
  229. * \param fs A OGG/Vorbis filestream.
  230. */
  231. static void ogg_vorbis_close(struct ast_filestream *fs)
  232. {
  233. struct ogg_vorbis_desc *s = (struct ogg_vorbis_desc *) fs->_private;
  234. if (s->writing) {
  235. /* Tell the Vorbis encoder that the stream is finished
  236. * and write out the rest of the data */
  237. vorbis_analysis_wrote(&s->vd, 0);
  238. write_stream(s, fs->f);
  239. } else {
  240. /* clear OggVorbis_File handle */
  241. ov_clear(&s->ov_f);
  242. }
  243. }
  244. /*!
  245. * \brief Read a frame full of audio data from the filestream.
  246. * \param fs The filestream.
  247. * \param whennext Number of sample times to schedule the next call.
  248. * \return A pointer to a frame containing audio data or NULL ifthere is no more audio data.
  249. */
  250. static struct ast_frame *ogg_vorbis_read(struct ast_filestream *fs,
  251. int *whennext)
  252. {
  253. struct ogg_vorbis_desc *desc = (struct ogg_vorbis_desc *) fs->_private;
  254. int current_bitstream = -10;
  255. char *out_buf;
  256. long bytes_read;
  257. if (desc->writing) {
  258. ast_log(LOG_WARNING, "Reading is not suport on OGG/Vorbis on write files.\n");
  259. return NULL;
  260. }
  261. /* initialize frame */
  262. fs->fr.frametype = AST_FRAME_VOICE;
  263. ast_format_set(&fs->fr.subclass.format, AST_FORMAT_SLINEAR, 0);
  264. fs->fr.mallocd = 0;
  265. AST_FRAME_SET_BUFFER(&fs->fr, fs->buf, AST_FRIENDLY_OFFSET, BUF_SIZE);
  266. out_buf = (char *) (fs->fr.data.ptr); /* SLIN data buffer */
  267. /* read samples from OV interface */
  268. bytes_read = ov_read(
  269. &desc->ov_f,
  270. out_buf, /* Buffer to write data */
  271. BUF_SIZE, /* Size of buffer */
  272. (__BYTE_ORDER == __BIG_ENDIAN), /* Endianes (0 for little) */
  273. 2, /* 1 = 8bit, 2 = 16bit */
  274. 1, /* 0 = unsigned, 1 = signed */
  275. &current_bitstream /* Returns the current bitstream section */
  276. );
  277. /* check returned data */
  278. if (bytes_read <= 0) {
  279. /* End of stream */
  280. return NULL;
  281. }
  282. /* Return decoded bytes */
  283. fs->fr.datalen = bytes_read;
  284. fs->fr.samples = bytes_read / 2;
  285. *whennext = fs->fr.samples;
  286. return &fs->fr;
  287. }
  288. /*!
  289. * \brief Trucate an OGG/Vorbis filestream.
  290. * \param s The filestream to truncate.
  291. * \return 0 on success, -1 on failure.
  292. */
  293. static int ogg_vorbis_trunc(struct ast_filestream *fs)
  294. {
  295. ast_log(LOG_WARNING, "Truncation is not supported on OGG/Vorbis streams!\n");
  296. return -1;
  297. }
  298. /*!
  299. * \brief Tell the current position in OGG/Vorbis filestream measured in pcms.
  300. * \param s The filestream to take action on.
  301. * \return 0 or greater with the position measured in samples, or -1 for false.
  302. */
  303. static off_t ogg_vorbis_tell(struct ast_filestream *fs)
  304. {
  305. off_t pos;
  306. struct ogg_vorbis_desc *desc = (struct ogg_vorbis_desc *) fs->_private;
  307. if (desc->writing) {
  308. return desc->writing_pcm_pos;
  309. }
  310. if ((pos = ov_pcm_tell(&desc->ov_f)) < 0) {
  311. return -1;
  312. }
  313. return pos;
  314. }
  315. /*!
  316. * \brief Seek to a specific position in an OGG/Vorbis filestream.
  317. * \param s The filestream to take action on.
  318. * \param sample_offset New position for the filestream, measured in 8KHz samples.
  319. * \param whence Location to measure
  320. * \return 0 on success, -1 on failure.
  321. */
  322. static int ogg_vorbis_seek(struct ast_filestream *fs, off_t sample_offset, int whence)
  323. {
  324. int seek_result = -1;
  325. off_t relative_pcm_pos;
  326. struct ogg_vorbis_desc *desc = (struct ogg_vorbis_desc *) fs->_private;
  327. if (desc->writing) {
  328. ast_log(LOG_WARNING, "Seeking is not supported on OGG/Vorbis streams in writing mode!\n");
  329. return -1;
  330. }
  331. /* ov_pcm_seek support seeking only from begining (SEEK_SET), the rest must be emulated */
  332. switch (whence) {
  333. case SEEK_SET:
  334. seek_result = ov_pcm_seek(&desc->ov_f, sample_offset);
  335. break;
  336. case SEEK_CUR:
  337. if ((relative_pcm_pos = ogg_vorbis_tell(fs)) < 0) {
  338. seek_result = -1;
  339. break;
  340. }
  341. seek_result = ov_pcm_seek(&desc->ov_f, relative_pcm_pos + sample_offset);
  342. break;
  343. case SEEK_END:
  344. if ((relative_pcm_pos = ov_pcm_total(&desc->ov_f, -1)) < 0) {
  345. seek_result = -1;
  346. break;
  347. }
  348. seek_result = ov_pcm_seek(&desc->ov_f, relative_pcm_pos - sample_offset);
  349. break;
  350. default:
  351. ast_log(LOG_WARNING, "Unknown *whence* to seek on OGG/Vorbis streams!\n");
  352. break;
  353. }
  354. /* normalize error value to -1,0 */
  355. return (seek_result == 0) ? 0 : -1;
  356. }
  357. static struct ast_format_def vorbis_f = {
  358. .name = "ogg_vorbis",
  359. .exts = "ogg",
  360. .open = ogg_vorbis_open,
  361. .rewrite = ogg_vorbis_rewrite,
  362. .write = ogg_vorbis_write,
  363. .seek = ogg_vorbis_seek,
  364. .trunc = ogg_vorbis_trunc,
  365. .tell = ogg_vorbis_tell,
  366. .read = ogg_vorbis_read,
  367. .close = ogg_vorbis_close,
  368. .buf_size = BUF_SIZE + AST_FRIENDLY_OFFSET,
  369. .desc_size = sizeof(struct ogg_vorbis_desc),
  370. };
  371. static int load_module(void)
  372. {
  373. ast_format_set(&vorbis_f.format, AST_FORMAT_SLINEAR, 0);
  374. if (ast_format_def_register(&vorbis_f))
  375. return AST_MODULE_LOAD_FAILURE;
  376. return AST_MODULE_LOAD_SUCCESS;
  377. }
  378. static int unload_module(void)
  379. {
  380. return ast_format_def_unregister(vorbis_f.name);
  381. }
  382. AST_MODULE_INFO(ASTERISK_GPL_KEY, AST_MODFLAG_LOAD_ORDER, "OGG/Vorbis audio",
  383. .load = load_module,
  384. .unload = unload_module,
  385. .load_pri = AST_MODPRI_APP_DEPEND
  386. );