format_vox.c 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  1. /*
  2. * Asterisk -- An open source telephony toolkit.
  3. *
  4. * Copyright (C) 1999 - 2005, Digium, Inc.
  5. *
  6. * Mark Spencer <markster@digium.com>
  7. *
  8. * See http://www.asterisk.org for more information about
  9. * the Asterisk project. Please do not directly contact
  10. * any of the maintainers of this project for assistance;
  11. * the project provides a web site, mailing lists and IRC
  12. * channels for your use.
  13. *
  14. * This program is free software, distributed under the terms of
  15. * the GNU General Public License Version 2. See the LICENSE file
  16. * at the top of the source tree.
  17. */
  18. /*! \file
  19. *
  20. * \brief Flat, binary, ADPCM vox file format.
  21. * \arg File name extensions: vox
  22. *
  23. * \ingroup formats
  24. */
  25. /*** MODULEINFO
  26. <support_level>extended</support_level>
  27. ***/
  28. #include "asterisk.h"
  29. ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
  30. #include "asterisk/mod_format.h"
  31. #include "asterisk/module.h"
  32. #include "asterisk/endian.h"
  33. #define BUF_SIZE 80 /* 80 bytes, 160 samples */
  34. #define VOX_SAMPLES 160
  35. static struct ast_frame *vox_read(struct ast_filestream *s, int *whennext)
  36. {
  37. int res;
  38. /* Send a frame from the file to the appropriate channel */
  39. s->fr.frametype = AST_FRAME_VOICE;
  40. ast_format_set(&s->fr.subclass.format, AST_FORMAT_ADPCM, 0);
  41. s->fr.mallocd = 0;
  42. AST_FRAME_SET_BUFFER(&s->fr, s->buf, AST_FRIENDLY_OFFSET, BUF_SIZE);
  43. if ((res = fread(s->fr.data.ptr, 1, s->fr.datalen, s->f)) < 1) {
  44. if (res)
  45. ast_log(LOG_WARNING, "Short read (%d) (%s)!\n", res, strerror(errno));
  46. return NULL;
  47. }
  48. *whennext = s->fr.samples = res * 2;
  49. s->fr.datalen = res;
  50. return &s->fr;
  51. }
  52. static int vox_write(struct ast_filestream *s, struct ast_frame *f)
  53. {
  54. int res;
  55. if (f->frametype != AST_FRAME_VOICE) {
  56. ast_log(LOG_WARNING, "Asked to write non-voice frame!\n");
  57. return -1;
  58. }
  59. if (f->subclass.format.id != AST_FORMAT_ADPCM) {
  60. ast_log(LOG_WARNING, "Asked to write non-ADPCM frame (%s)!\n", ast_getformatname(&f->subclass.format));
  61. return -1;
  62. }
  63. if ((res = fwrite(f->data.ptr, 1, f->datalen, s->f)) != f->datalen) {
  64. ast_log(LOG_WARNING, "Bad write (%d/%d): %s\n", res, f->datalen, strerror(errno));
  65. return -1;
  66. }
  67. return 0;
  68. }
  69. static int vox_seek(struct ast_filestream *fs, off_t sample_offset, int whence)
  70. {
  71. off_t offset = 0, min = 0, cur, max, distance;
  72. if ((cur = ftello(fs->f)) < 0) {
  73. ast_log(AST_LOG_WARNING, "Unable to determine current position in g719 filestream %p: %s\n", fs, strerror(errno));
  74. return -1;
  75. }
  76. if (fseeko(fs->f, 0, SEEK_END) < 0) {
  77. ast_log(AST_LOG_WARNING, "Unable to seek to end of g719 filestream %p: %s\n", fs, strerror(errno));
  78. return -1;
  79. }
  80. if ((max = ftello(fs->f)) < 0) {
  81. ast_log(AST_LOG_WARNING, "Unable to determine max position in g719 filestream %p: %s\n", fs, strerror(errno));
  82. return -1;
  83. }
  84. /* have to fudge to frame here, so not fully to sample */
  85. distance = sample_offset/2;
  86. if (whence == SEEK_SET) {
  87. offset = distance;
  88. } else if (whence == SEEK_CUR || whence == SEEK_FORCECUR) {
  89. offset = distance + cur;
  90. } else if (whence == SEEK_END) {
  91. offset = max - distance;
  92. }
  93. if (whence != SEEK_FORCECUR) {
  94. offset = (offset > max)?max:offset;
  95. offset = (offset < min)?min:offset;
  96. }
  97. return fseeko(fs->f, offset, SEEK_SET);
  98. }
  99. static int vox_trunc(struct ast_filestream *fs)
  100. {
  101. int fd;
  102. off_t cur;
  103. if ((fd = fileno(fs->f)) < 0) {
  104. ast_log(AST_LOG_WARNING, "Unable to determine file descriptor for vox filestream %p: %s\n", fs, strerror(errno));
  105. return -1;
  106. }
  107. if ((cur = ftello(fs->f)) < 0) {
  108. ast_log(AST_LOG_WARNING, "Unable to determine current position in vox filestream %p: %s\n", fs, strerror(errno));
  109. return -1;
  110. }
  111. /* Truncate file to current length */
  112. return ftruncate(fd, cur);}
  113. static off_t vox_tell(struct ast_filestream *fs)
  114. {
  115. off_t offset;
  116. offset = ftello(fs->f) << 1;
  117. return offset;
  118. }
  119. static struct ast_format_def vox_f = {
  120. .name = "vox",
  121. .exts = "vox",
  122. .write = vox_write,
  123. .seek = vox_seek,
  124. .trunc = vox_trunc,
  125. .tell = vox_tell,
  126. .read = vox_read,
  127. .buf_size = BUF_SIZE + AST_FRIENDLY_OFFSET,
  128. };
  129. static int load_module(void)
  130. {
  131. ast_format_set(&vox_f.format, AST_FORMAT_ADPCM, 0);
  132. if (ast_format_def_register(&vox_f))
  133. return AST_MODULE_LOAD_FAILURE;
  134. return AST_MODULE_LOAD_SUCCESS;
  135. }
  136. static int unload_module(void)
  137. {
  138. return ast_format_def_unregister(vox_f.name);
  139. }
  140. AST_MODULE_INFO(ASTERISK_GPL_KEY, AST_MODFLAG_LOAD_ORDER, "Dialogic VOX (ADPCM) File Format",
  141. .load = load_module,
  142. .unload = unload_module,
  143. .load_pri = AST_MODPRI_APP_DEPEND
  144. );