format_sln.c 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275
  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,cur,max;
  67. min = 0;
  68. sample_offset <<= 1;
  69. cur = ftello(fs->f);
  70. fseeko(fs->f, 0, SEEK_END);
  71. max = ftello(fs->f);
  72. if (whence == SEEK_SET)
  73. offset = sample_offset;
  74. else if (whence == SEEK_CUR || whence == SEEK_FORCECUR)
  75. offset = sample_offset + cur;
  76. else if (whence == SEEK_END)
  77. offset = max - sample_offset;
  78. if (whence != SEEK_FORCECUR) {
  79. offset = (offset > max)?max:offset;
  80. }
  81. /* always protect against seeking past begining. */
  82. offset = (offset < min)?min:offset;
  83. return fseeko(fs->f, offset, SEEK_SET);
  84. }
  85. static int slinear_trunc(struct ast_filestream *fs)
  86. {
  87. return ftruncate(fileno(fs->f), ftello(fs->f));
  88. }
  89. static off_t slinear_tell(struct ast_filestream *fs)
  90. {
  91. return ftello(fs->f) / 2;
  92. }
  93. static int slinear_write(struct ast_filestream *fs, struct ast_frame *f){return generic_write(fs, f, AST_FORMAT_SLINEAR);}
  94. static struct ast_frame *slinear_read(struct ast_filestream *s, int *whennext){return generic_read(s, whennext, 320, AST_FORMAT_SLINEAR);}
  95. static struct ast_format_def slin_f = {
  96. .name = "sln",
  97. .exts = "sln|raw",
  98. .write = slinear_write,
  99. .seek = slinear_seek,
  100. .trunc = slinear_trunc,
  101. .tell = slinear_tell,
  102. .read = slinear_read,
  103. .buf_size = 320 + AST_FRIENDLY_OFFSET,
  104. };
  105. static int slinear12_write(struct ast_filestream *fs, struct ast_frame *f){return generic_write(fs, f, AST_FORMAT_SLINEAR12);}
  106. static struct ast_frame *slinear12_read(struct ast_filestream *s, int *whennext){return generic_read(s, whennext, 480, AST_FORMAT_SLINEAR12);}
  107. static struct ast_format_def slin12_f = {
  108. .name = "sln12",
  109. .exts = "sln12",
  110. .write = slinear12_write,
  111. .seek = slinear_seek,
  112. .trunc = slinear_trunc,
  113. .tell = slinear_tell,
  114. .read = slinear12_read,
  115. .buf_size = 480 + AST_FRIENDLY_OFFSET,
  116. };
  117. static int slinear16_write(struct ast_filestream *fs, struct ast_frame *f){return generic_write(fs, f, AST_FORMAT_SLINEAR16);}
  118. static struct ast_frame *slinear16_read(struct ast_filestream *s, int *whennext){return generic_read(s, whennext, 640, AST_FORMAT_SLINEAR16);}
  119. static struct ast_format_def slin16_f = {
  120. .name = "sln16",
  121. .exts = "sln16",
  122. .write = slinear16_write,
  123. .seek = slinear_seek,
  124. .trunc = slinear_trunc,
  125. .tell = slinear_tell,
  126. .read = slinear16_read,
  127. .buf_size = 640 + AST_FRIENDLY_OFFSET,
  128. };
  129. static int slinear24_write(struct ast_filestream *fs, struct ast_frame *f){return generic_write(fs, f, AST_FORMAT_SLINEAR24);}
  130. static struct ast_frame *slinear24_read(struct ast_filestream *s, int *whennext){return generic_read(s, whennext, 960, AST_FORMAT_SLINEAR24);}
  131. static struct ast_format_def slin24_f = {
  132. .name = "sln24",
  133. .exts = "sln24",
  134. .write = slinear24_write,
  135. .seek = slinear_seek,
  136. .trunc = slinear_trunc,
  137. .tell = slinear_tell,
  138. .read = slinear24_read,
  139. .buf_size = 960 + AST_FRIENDLY_OFFSET,
  140. };
  141. static int slinear32_write(struct ast_filestream *fs, struct ast_frame *f){return generic_write(fs, f, AST_FORMAT_SLINEAR32);}
  142. static struct ast_frame *slinear32_read(struct ast_filestream *s, int *whennext){return generic_read(s, whennext, 1280, AST_FORMAT_SLINEAR32);}
  143. static struct ast_format_def slin32_f = {
  144. .name = "sln32",
  145. .exts = "sln32",
  146. .write = slinear32_write,
  147. .seek = slinear_seek,
  148. .trunc = slinear_trunc,
  149. .tell = slinear_tell,
  150. .read = slinear32_read,
  151. .buf_size = 1280 + AST_FRIENDLY_OFFSET,
  152. };
  153. static int slinear44_write(struct ast_filestream *fs, struct ast_frame *f){return generic_write(fs, f, AST_FORMAT_SLINEAR44);}
  154. static struct ast_frame *slinear44_read(struct ast_filestream *s, int *whennext){return generic_read(s, whennext, 1764, AST_FORMAT_SLINEAR44);}
  155. static struct ast_format_def slin44_f = {
  156. .name = "sln44",
  157. .exts = "sln44",
  158. .write = slinear44_write,
  159. .seek = slinear_seek,
  160. .trunc = slinear_trunc,
  161. .tell = slinear_tell,
  162. .read = slinear44_read,
  163. .buf_size = 1764 + AST_FRIENDLY_OFFSET,
  164. };
  165. static int slinear48_write(struct ast_filestream *fs, struct ast_frame *f){return generic_write(fs, f, AST_FORMAT_SLINEAR48);}
  166. static struct ast_frame *slinear48_read(struct ast_filestream *s, int *whennext){return generic_read(s, whennext, 1920, AST_FORMAT_SLINEAR48);}
  167. static struct ast_format_def slin48_f = {
  168. .name = "sln48",
  169. .exts = "sln48",
  170. .write = slinear48_write,
  171. .seek = slinear_seek,
  172. .trunc = slinear_trunc,
  173. .tell = slinear_tell,
  174. .read = slinear48_read,
  175. .buf_size = 1920 + AST_FRIENDLY_OFFSET,
  176. };
  177. static int slinear96_write(struct ast_filestream *fs, struct ast_frame *f){return generic_write(fs, f, AST_FORMAT_SLINEAR96);}
  178. static struct ast_frame *slinear96_read(struct ast_filestream *s, int *whennext){return generic_read(s, whennext, 3840, AST_FORMAT_SLINEAR96);}
  179. static struct ast_format_def slin96_f = {
  180. .name = "sln96",
  181. .exts = "sln96",
  182. .write = slinear96_write,
  183. .seek = slinear_seek,
  184. .trunc = slinear_trunc,
  185. .tell = slinear_tell,
  186. .read = slinear96_read,
  187. .buf_size = 3840 + AST_FRIENDLY_OFFSET,
  188. };
  189. static int slinear192_write(struct ast_filestream *fs, struct ast_frame *f){return generic_write(fs, f, AST_FORMAT_SLINEAR192);}
  190. static struct ast_frame *slinear192_read(struct ast_filestream *s, int *whennext){return generic_read(s, whennext, 7680, AST_FORMAT_SLINEAR192);}
  191. static struct ast_format_def slin192_f = {
  192. .name = "sln192",
  193. .exts = "sln192",
  194. .write = slinear192_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. ast_format_set(&slin_f.format, AST_FORMAT_SLINEAR, 0);
  216. ast_format_set(&slin12_f.format, AST_FORMAT_SLINEAR12, 0);
  217. ast_format_set(&slin16_f.format, AST_FORMAT_SLINEAR16, 0);
  218. ast_format_set(&slin24_f.format, AST_FORMAT_SLINEAR24, 0);
  219. ast_format_set(&slin32_f.format, AST_FORMAT_SLINEAR32, 0);
  220. ast_format_set(&slin44_f.format, AST_FORMAT_SLINEAR44, 0);
  221. ast_format_set(&slin48_f.format, AST_FORMAT_SLINEAR48, 0);
  222. ast_format_set(&slin96_f.format, AST_FORMAT_SLINEAR96, 0);
  223. ast_format_set(&slin192_f.format, AST_FORMAT_SLINEAR192, 0);
  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. .load = load_module,
  244. .unload = unload_module,
  245. .load_pri = AST_MODPRI_APP_DEPEND
  246. );