format_sln.c 9.4 KB

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