format_sln.c 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282
  1. /*
  2. * Asterisk -- An open source telephony toolkit.
  3. *
  4. * Copyright (C) 1999 - 2005, Anthony Minessale
  5. * Anthony Minessale (anthmct@yahoo.com)
  6. *
  7. * See http://www.asterisk.org for more information about
  8. * the Asterisk project. Please do not directly contact
  9. * any of the maintainers of this project for assistance;
  10. * the project provides a web site, mailing lists and IRC
  11. * channels for your use.
  12. *
  13. * This program is free software, distributed under the terms of
  14. * the GNU General Public License Version 2. See the LICENSE file
  15. * at the top of the source tree.
  16. */
  17. /*! \file
  18. *
  19. * \brief RAW SLINEAR Formats
  20. * \ingroup formats
  21. */
  22. /*** MODULEINFO
  23. <support_level>core</support_level>
  24. ***/
  25. #include "asterisk.h"
  26. ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
  27. #include "asterisk/mod_format.h"
  28. #include "asterisk/module.h"
  29. #include "asterisk/endian.h"
  30. #include "asterisk/format_cache.h"
  31. static struct ast_frame *generic_read(struct ast_filestream *s, int *whennext, unsigned int buf_size)
  32. {
  33. int res;
  34. /* Send a frame from the file to the appropriate channel */
  35. AST_FRAME_SET_BUFFER(&s->fr, s->buf, AST_FRIENDLY_OFFSET, buf_size);
  36. if ((res = fread(s->fr.data.ptr, 1, s->fr.datalen, s->f)) < 1) {
  37. if (res)
  38. ast_log(LOG_WARNING, "Short read (%d) (%s)!\n", res, strerror(errno));
  39. return NULL;
  40. }
  41. *whennext = s->fr.samples = res/2;
  42. s->fr.datalen = res;
  43. return &s->fr;
  44. }
  45. static int slinear_write(struct ast_filestream *fs, struct ast_frame *f)
  46. {
  47. int res;
  48. if ((res = fwrite(f->data.ptr, 1, f->datalen, fs->f)) != f->datalen) {
  49. ast_log(LOG_WARNING, "Bad write (%d/%d): %s\n", res, f->datalen, strerror(errno));
  50. return -1;
  51. }
  52. return 0;
  53. }
  54. static int slinear_seek(struct ast_filestream *fs, off_t sample_offset, int whence)
  55. {
  56. off_t offset=0, min = 0, cur, max;
  57. sample_offset <<= 1;
  58. if ((cur = ftello(fs->f)) < 0) {
  59. ast_log(AST_LOG_WARNING, "Unable to determine current position in sln filestream %p: %s\n", fs, strerror(errno));
  60. return -1;
  61. }
  62. if (fseeko(fs->f, 0, SEEK_END) < 0) {
  63. ast_log(AST_LOG_WARNING, "Unable to seek to end of sln filestream %p: %s\n", fs, strerror(errno));
  64. return -1;
  65. }
  66. if ((max = ftello(fs->f)) < 0) {
  67. ast_log(AST_LOG_WARNING, "Unable to determine max position in sln filestream %p: %s\n", fs, strerror(errno));
  68. return -1;
  69. }
  70. if (whence == SEEK_SET)
  71. offset = sample_offset;
  72. else if (whence == SEEK_CUR || whence == SEEK_FORCECUR)
  73. offset = sample_offset + cur;
  74. else if (whence == SEEK_END)
  75. offset = max - sample_offset;
  76. if (whence != SEEK_FORCECUR) {
  77. offset = (offset > max)?max:offset;
  78. }
  79. /* always protect against seeking past begining. */
  80. offset = (offset < min)?min:offset;
  81. return fseeko(fs->f, offset, SEEK_SET);
  82. }
  83. static int slinear_trunc(struct ast_filestream *fs)
  84. {
  85. int fd;
  86. off_t cur;
  87. if ((fd = fileno(fs->f)) < 0) {
  88. ast_log(AST_LOG_WARNING, "Unable to determine file descriptor for sln filestream %p: %s\n", fs, strerror(errno));
  89. return -1;
  90. }
  91. if ((cur = ftello(fs->f)) < 0) {
  92. ast_log(AST_LOG_WARNING, "Unable to determine current position in sln filestream %p: %s\n", fs, strerror(errno));
  93. return -1;
  94. }
  95. /* Truncate file to current length */
  96. return ftruncate(fd, cur);
  97. }
  98. static off_t slinear_tell(struct ast_filestream *fs)
  99. {
  100. return ftello(fs->f) / 2;
  101. }
  102. static struct ast_frame *slinear_read(struct ast_filestream *s, int *whennext){return generic_read(s, whennext, 320);}
  103. static struct ast_format_def slin_f = {
  104. .name = "sln",
  105. .exts = "sln|raw",
  106. .write = slinear_write,
  107. .seek = slinear_seek,
  108. .trunc = slinear_trunc,
  109. .tell = slinear_tell,
  110. .read = slinear_read,
  111. .buf_size = 320 + AST_FRIENDLY_OFFSET,
  112. };
  113. static struct ast_frame *slinear12_read(struct ast_filestream *s, int *whennext){return generic_read(s, whennext, 480);}
  114. static struct ast_format_def slin12_f = {
  115. .name = "sln12",
  116. .exts = "sln12",
  117. .write = slinear_write,
  118. .seek = slinear_seek,
  119. .trunc = slinear_trunc,
  120. .tell = slinear_tell,
  121. .read = slinear12_read,
  122. .buf_size = 480 + AST_FRIENDLY_OFFSET,
  123. };
  124. static struct ast_frame *slinear16_read(struct ast_filestream *s, int *whennext){return generic_read(s, whennext, 640);}
  125. static struct ast_format_def slin16_f = {
  126. .name = "sln16",
  127. .exts = "sln16",
  128. .write = slinear_write,
  129. .seek = slinear_seek,
  130. .trunc = slinear_trunc,
  131. .tell = slinear_tell,
  132. .read = slinear16_read,
  133. .buf_size = 640 + AST_FRIENDLY_OFFSET,
  134. };
  135. static struct ast_frame *slinear24_read(struct ast_filestream *s, int *whennext){return generic_read(s, whennext, 960);}
  136. static struct ast_format_def slin24_f = {
  137. .name = "sln24",
  138. .exts = "sln24",
  139. .write = slinear_write,
  140. .seek = slinear_seek,
  141. .trunc = slinear_trunc,
  142. .tell = slinear_tell,
  143. .read = slinear24_read,
  144. .buf_size = 960 + AST_FRIENDLY_OFFSET,
  145. };
  146. static struct ast_frame *slinear32_read(struct ast_filestream *s, int *whennext){return generic_read(s, whennext, 1280);}
  147. static struct ast_format_def slin32_f = {
  148. .name = "sln32",
  149. .exts = "sln32",
  150. .write = slinear_write,
  151. .seek = slinear_seek,
  152. .trunc = slinear_trunc,
  153. .tell = slinear_tell,
  154. .read = slinear32_read,
  155. .buf_size = 1280 + AST_FRIENDLY_OFFSET,
  156. };
  157. static struct ast_frame *slinear44_read(struct ast_filestream *s, int *whennext){return generic_read(s, whennext, 1764);}
  158. static struct ast_format_def slin44_f = {
  159. .name = "sln44",
  160. .exts = "sln44",
  161. .write = slinear_write,
  162. .seek = slinear_seek,
  163. .trunc = slinear_trunc,
  164. .tell = slinear_tell,
  165. .read = slinear44_read,
  166. .buf_size = 1764 + AST_FRIENDLY_OFFSET,
  167. };
  168. static struct ast_frame *slinear48_read(struct ast_filestream *s, int *whennext){return generic_read(s, whennext, 1920);}
  169. static struct ast_format_def slin48_f = {
  170. .name = "sln48",
  171. .exts = "sln48",
  172. .write = slinear_write,
  173. .seek = slinear_seek,
  174. .trunc = slinear_trunc,
  175. .tell = slinear_tell,
  176. .read = slinear48_read,
  177. .buf_size = 1920 + AST_FRIENDLY_OFFSET,
  178. };
  179. static struct ast_frame *slinear96_read(struct ast_filestream *s, int *whennext){return generic_read(s, whennext, 3840);}
  180. static struct ast_format_def slin96_f = {
  181. .name = "sln96",
  182. .exts = "sln96",
  183. .write = slinear_write,
  184. .seek = slinear_seek,
  185. .trunc = slinear_trunc,
  186. .tell = slinear_tell,
  187. .read = slinear96_read,
  188. .buf_size = 3840 + AST_FRIENDLY_OFFSET,
  189. };
  190. static struct ast_frame *slinear192_read(struct ast_filestream *s, int *whennext){return generic_read(s, whennext, 7680);}
  191. static struct ast_format_def slin192_f = {
  192. .name = "sln192",
  193. .exts = "sln192",
  194. .write = slinear_write,
  195. .seek = slinear_seek,
  196. .trunc = slinear_trunc,
  197. .tell = slinear_tell,
  198. .read = slinear192_read,
  199. .buf_size = 7680 + AST_FRIENDLY_OFFSET,
  200. };
  201. static struct ast_format_def *slin_list[] = {
  202. &slin_f,
  203. &slin12_f,
  204. &slin16_f,
  205. &slin24_f,
  206. &slin32_f,
  207. &slin44_f,
  208. &slin48_f,
  209. &slin96_f,
  210. &slin192_f,
  211. };
  212. static int load_module(void)
  213. {
  214. int i;
  215. slin_f.format = ast_format_slin;
  216. slin12_f.format = ast_format_slin12;
  217. slin16_f.format = ast_format_slin16;
  218. slin24_f.format = ast_format_slin24;
  219. slin32_f.format = ast_format_slin32;
  220. slin44_f.format = ast_format_slin44;
  221. slin48_f.format = ast_format_slin48;
  222. slin96_f.format = ast_format_slin96;
  223. slin192_f.format = ast_format_slin192;
  224. for (i = 0; i < ARRAY_LEN(slin_list); i++) {
  225. if (ast_format_def_register(slin_list[i])) {
  226. return AST_MODULE_LOAD_FAILURE;
  227. }
  228. }
  229. return AST_MODULE_LOAD_SUCCESS;
  230. }
  231. static int unload_module(void)
  232. {
  233. int res = 0;
  234. int i = 0;
  235. for (i = 0; i < ARRAY_LEN(slin_list); i++) {
  236. if (ast_format_def_unregister(slin_list[i]->name)) {
  237. res |= AST_MODULE_LOAD_FAILURE;
  238. }
  239. }
  240. return res;
  241. }
  242. AST_MODULE_INFO(ASTERISK_GPL_KEY, AST_MODFLAG_LOAD_ORDER, "Raw Signed Linear Audio support (SLN) 8khz-192khz",
  243. .support_level = AST_MODULE_SUPPORT_CORE,
  244. .load = load_module,
  245. .unload = unload_module,
  246. .load_pri = AST_MODPRI_APP_DEPEND
  247. );