format_sln.c 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291
  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. size_t 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 of %s data (expected %d bytes, read %zu): %s\n",
  39. ast_format_get_name(s->fr.subclass.format), s->fr.datalen, res,
  40. strerror(errno));
  41. }
  42. return NULL;
  43. }
  44. *whennext = s->fr.samples = res/2;
  45. s->fr.datalen = res;
  46. return &s->fr;
  47. }
  48. static int slinear_write(struct ast_filestream *fs, struct ast_frame *f)
  49. {
  50. int res;
  51. /* Don't try to write an interpolated frame */
  52. if (f->datalen == 0) {
  53. return 0;
  54. }
  55. if ((res = fwrite(f->data.ptr, 1, f->datalen, fs->f)) != f->datalen) {
  56. ast_log(LOG_WARNING, "Bad write (%d/%d): %s\n", res, f->datalen, strerror(errno));
  57. return -1;
  58. }
  59. return 0;
  60. }
  61. static int slinear_seek(struct ast_filestream *fs, off_t sample_offset, int whence)
  62. {
  63. off_t offset=0, min = 0, cur, max;
  64. sample_offset <<= 1;
  65. if ((cur = ftello(fs->f)) < 0) {
  66. ast_log(AST_LOG_WARNING, "Unable to determine current position in sln filestream %p: %s\n", fs, strerror(errno));
  67. return -1;
  68. }
  69. if (fseeko(fs->f, 0, SEEK_END) < 0) {
  70. ast_log(AST_LOG_WARNING, "Unable to seek to end of sln filestream %p: %s\n", fs, strerror(errno));
  71. return -1;
  72. }
  73. if ((max = ftello(fs->f)) < 0) {
  74. ast_log(AST_LOG_WARNING, "Unable to determine max position in sln filestream %p: %s\n", fs, strerror(errno));
  75. return -1;
  76. }
  77. if (whence == SEEK_SET)
  78. offset = sample_offset;
  79. else if (whence == SEEK_CUR || whence == SEEK_FORCECUR)
  80. offset = sample_offset + cur;
  81. else if (whence == SEEK_END)
  82. offset = max - sample_offset;
  83. if (whence != SEEK_FORCECUR) {
  84. offset = (offset > max)?max:offset;
  85. }
  86. /* always protect against seeking past begining. */
  87. offset = (offset < min)?min:offset;
  88. return fseeko(fs->f, offset, SEEK_SET);
  89. }
  90. static int slinear_trunc(struct ast_filestream *fs)
  91. {
  92. int fd;
  93. off_t cur;
  94. if ((fd = fileno(fs->f)) < 0) {
  95. ast_log(AST_LOG_WARNING, "Unable to determine file descriptor for sln filestream %p: %s\n", fs, strerror(errno));
  96. return -1;
  97. }
  98. if ((cur = ftello(fs->f)) < 0) {
  99. ast_log(AST_LOG_WARNING, "Unable to determine current position in sln filestream %p: %s\n", fs, strerror(errno));
  100. return -1;
  101. }
  102. /* Truncate file to current length */
  103. return ftruncate(fd, cur);
  104. }
  105. static off_t slinear_tell(struct ast_filestream *fs)
  106. {
  107. return ftello(fs->f) / 2;
  108. }
  109. static struct ast_frame *slinear_read(struct ast_filestream *s, int *whennext){return generic_read(s, whennext, 320);}
  110. static struct ast_format_def slin_f = {
  111. .name = "sln",
  112. .exts = "sln|raw",
  113. .write = slinear_write,
  114. .seek = slinear_seek,
  115. .trunc = slinear_trunc,
  116. .tell = slinear_tell,
  117. .read = slinear_read,
  118. .buf_size = 320 + AST_FRIENDLY_OFFSET,
  119. };
  120. static struct ast_frame *slinear12_read(struct ast_filestream *s, int *whennext){return generic_read(s, whennext, 480);}
  121. static struct ast_format_def slin12_f = {
  122. .name = "sln12",
  123. .exts = "sln12",
  124. .write = slinear_write,
  125. .seek = slinear_seek,
  126. .trunc = slinear_trunc,
  127. .tell = slinear_tell,
  128. .read = slinear12_read,
  129. .buf_size = 480 + AST_FRIENDLY_OFFSET,
  130. };
  131. static struct ast_frame *slinear16_read(struct ast_filestream *s, int *whennext){return generic_read(s, whennext, 640);}
  132. static struct ast_format_def slin16_f = {
  133. .name = "sln16",
  134. .exts = "sln16",
  135. .write = slinear_write,
  136. .seek = slinear_seek,
  137. .trunc = slinear_trunc,
  138. .tell = slinear_tell,
  139. .read = slinear16_read,
  140. .buf_size = 640 + AST_FRIENDLY_OFFSET,
  141. };
  142. static struct ast_frame *slinear24_read(struct ast_filestream *s, int *whennext){return generic_read(s, whennext, 960);}
  143. static struct ast_format_def slin24_f = {
  144. .name = "sln24",
  145. .exts = "sln24",
  146. .write = slinear_write,
  147. .seek = slinear_seek,
  148. .trunc = slinear_trunc,
  149. .tell = slinear_tell,
  150. .read = slinear24_read,
  151. .buf_size = 960 + AST_FRIENDLY_OFFSET,
  152. };
  153. static struct ast_frame *slinear32_read(struct ast_filestream *s, int *whennext){return generic_read(s, whennext, 1280);}
  154. static struct ast_format_def slin32_f = {
  155. .name = "sln32",
  156. .exts = "sln32",
  157. .write = slinear_write,
  158. .seek = slinear_seek,
  159. .trunc = slinear_trunc,
  160. .tell = slinear_tell,
  161. .read = slinear32_read,
  162. .buf_size = 1280 + AST_FRIENDLY_OFFSET,
  163. };
  164. static struct ast_frame *slinear44_read(struct ast_filestream *s, int *whennext){return generic_read(s, whennext, 1764);}
  165. static struct ast_format_def slin44_f = {
  166. .name = "sln44",
  167. .exts = "sln44",
  168. .write = slinear_write,
  169. .seek = slinear_seek,
  170. .trunc = slinear_trunc,
  171. .tell = slinear_tell,
  172. .read = slinear44_read,
  173. .buf_size = 1764 + AST_FRIENDLY_OFFSET,
  174. };
  175. static struct ast_frame *slinear48_read(struct ast_filestream *s, int *whennext){return generic_read(s, whennext, 1920);}
  176. static struct ast_format_def slin48_f = {
  177. .name = "sln48",
  178. .exts = "sln48",
  179. .write = slinear_write,
  180. .seek = slinear_seek,
  181. .trunc = slinear_trunc,
  182. .tell = slinear_tell,
  183. .read = slinear48_read,
  184. .buf_size = 1920 + AST_FRIENDLY_OFFSET,
  185. };
  186. static struct ast_frame *slinear96_read(struct ast_filestream *s, int *whennext){return generic_read(s, whennext, 3840);}
  187. static struct ast_format_def slin96_f = {
  188. .name = "sln96",
  189. .exts = "sln96",
  190. .write = slinear_write,
  191. .seek = slinear_seek,
  192. .trunc = slinear_trunc,
  193. .tell = slinear_tell,
  194. .read = slinear96_read,
  195. .buf_size = 3840 + AST_FRIENDLY_OFFSET,
  196. };
  197. static struct ast_frame *slinear192_read(struct ast_filestream *s, int *whennext){return generic_read(s, whennext, 7680);}
  198. static struct ast_format_def slin192_f = {
  199. .name = "sln192",
  200. .exts = "sln192",
  201. .write = slinear_write,
  202. .seek = slinear_seek,
  203. .trunc = slinear_trunc,
  204. .tell = slinear_tell,
  205. .read = slinear192_read,
  206. .buf_size = 7680 + AST_FRIENDLY_OFFSET,
  207. };
  208. static struct ast_format_def *slin_list[] = {
  209. &slin_f,
  210. &slin12_f,
  211. &slin16_f,
  212. &slin24_f,
  213. &slin32_f,
  214. &slin44_f,
  215. &slin48_f,
  216. &slin96_f,
  217. &slin192_f,
  218. };
  219. static int unload_module(void)
  220. {
  221. int res = 0;
  222. int i = 0;
  223. for (i = 0; i < ARRAY_LEN(slin_list); i++) {
  224. if (ast_format_def_unregister(slin_list[i]->name)) {
  225. res = -1;
  226. }
  227. }
  228. return res;
  229. }
  230. static int load_module(void)
  231. {
  232. int i;
  233. slin_f.format = ast_format_slin;
  234. slin12_f.format = ast_format_slin12;
  235. slin16_f.format = ast_format_slin16;
  236. slin24_f.format = ast_format_slin24;
  237. slin32_f.format = ast_format_slin32;
  238. slin44_f.format = ast_format_slin44;
  239. slin48_f.format = ast_format_slin48;
  240. slin96_f.format = ast_format_slin96;
  241. slin192_f.format = ast_format_slin192;
  242. for (i = 0; i < ARRAY_LEN(slin_list); i++) {
  243. if (ast_format_def_register(slin_list[i])) {
  244. unload_module();
  245. return AST_MODULE_LOAD_DECLINE;
  246. }
  247. }
  248. return AST_MODULE_LOAD_SUCCESS;
  249. }
  250. AST_MODULE_INFO(ASTERISK_GPL_KEY, AST_MODFLAG_LOAD_ORDER, "Raw Signed Linear Audio support (SLN) 8khz-192khz",
  251. .support_level = AST_MODULE_SUPPORT_CORE,
  252. .load = load_module,
  253. .unload = unload_module,
  254. .load_pri = AST_MODPRI_APP_DEPEND
  255. );