format_g726.c 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289
  1. /*
  2. * Asterisk -- An open source telephony toolkit.
  3. *
  4. * Copyright (c) 2004 - 2005, inAccess Networks
  5. *
  6. * Michael Manousos <manousos@inaccessnetworks.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 Headerless G.726 (16/24/32/40kbps) data format for Asterisk.
  21. *
  22. * File name extensions:
  23. * \arg 40 kbps: g726-40
  24. * \arg 32 kbps: g726-32
  25. * \arg 24 kbps: g726-24
  26. * \arg 16 kbps: g726-16
  27. * \ingroup formats
  28. */
  29. /*** MODULEINFO
  30. <support_level>core</support_level>
  31. ***/
  32. #include "asterisk.h"
  33. #include "asterisk/mod_format.h"
  34. #include "asterisk/module.h"
  35. #include "asterisk/endian.h"
  36. #include "asterisk/format_cache.h"
  37. #define RATE_40 0
  38. #define RATE_32 1
  39. #define RATE_24 2
  40. #define RATE_16 3
  41. /* We can only read/write chunks of FRAME_TIME ms G.726 data */
  42. #define FRAME_TIME 10 /* 10 ms size */
  43. #define BUF_SIZE (5*FRAME_TIME) /* max frame size in bytes ? */
  44. /* Frame sizes in bytes */
  45. static int frame_size[4] = {
  46. FRAME_TIME * 5,
  47. FRAME_TIME * 4,
  48. FRAME_TIME * 3,
  49. FRAME_TIME * 2
  50. };
  51. struct g726_desc {
  52. int rate; /* RATE_* defines */
  53. };
  54. /*
  55. * Rate dependant format functions (open, rewrite)
  56. */
  57. static int g726_open(struct ast_filestream *tmp, int rate)
  58. {
  59. struct g726_desc *s = (struct g726_desc *)tmp->_private;
  60. s->rate = rate;
  61. ast_debug(1, "Created filestream G.726-%dk.\n", 40 - s->rate * 8);
  62. return 0;
  63. }
  64. static int g726_40_open(struct ast_filestream *s)
  65. {
  66. return g726_open(s, RATE_40);
  67. }
  68. static int g726_32_open(struct ast_filestream *s)
  69. {
  70. return g726_open(s, RATE_32);
  71. }
  72. static int g726_24_open(struct ast_filestream *s)
  73. {
  74. return g726_open(s, RATE_24);
  75. }
  76. static int g726_16_open(struct ast_filestream *s)
  77. {
  78. return g726_open(s, RATE_16);
  79. }
  80. static int g726_40_rewrite(struct ast_filestream *s, const char *comment)
  81. {
  82. return g726_open(s, RATE_40);
  83. }
  84. static int g726_32_rewrite(struct ast_filestream *s, const char *comment)
  85. {
  86. return g726_open(s, RATE_32);
  87. }
  88. static int g726_24_rewrite(struct ast_filestream *s, const char *comment)
  89. {
  90. return g726_open(s, RATE_24);
  91. }
  92. static int g726_16_rewrite(struct ast_filestream *s, const char *comment)
  93. {
  94. return g726_open(s, RATE_16);
  95. }
  96. /*
  97. * Rate independent format functions (read, write)
  98. */
  99. static struct ast_frame *g726_read(struct ast_filestream *s, int *whennext)
  100. {
  101. size_t res;
  102. struct g726_desc *fs = (struct g726_desc *)s->_private;
  103. /* Send a frame from the file to the appropriate channel */
  104. AST_FRAME_SET_BUFFER(&s->fr, s->buf, AST_FRIENDLY_OFFSET, frame_size[fs->rate]);
  105. s->fr.samples = 8 * FRAME_TIME;
  106. if ((res = fread(s->fr.data.ptr, 1, s->fr.datalen, s->f)) != s->fr.datalen) {
  107. if (res) {
  108. ast_log(LOG_WARNING, "Short read of %s data (expected %d bytes, read %zu): %s\n",
  109. ast_format_get_name(s->fr.subclass.format), s->fr.datalen, res,
  110. strerror(errno));
  111. }
  112. return NULL;
  113. }
  114. *whennext = s->fr.samples;
  115. return &s->fr;
  116. }
  117. static int g726_write(struct ast_filestream *s, struct ast_frame *f)
  118. {
  119. int res;
  120. struct g726_desc *fs = (struct g726_desc *)s->_private;
  121. if (f->datalen % frame_size[fs->rate]) {
  122. ast_log(LOG_WARNING, "Invalid data length %d, should be multiple of %d\n",
  123. f->datalen, frame_size[fs->rate]);
  124. return -1;
  125. }
  126. if ((res = fwrite(f->data.ptr, 1, f->datalen, s->f)) != f->datalen) {
  127. ast_log(LOG_WARNING, "Bad write (%d/%d): %s\n",
  128. res, frame_size[fs->rate], strerror(errno));
  129. return -1;
  130. }
  131. return 0;
  132. }
  133. static int g726_seek(struct ast_filestream *fs, off_t sample_offset, int whence)
  134. {
  135. off_t offset = 0, min = 0, cur, max, distance;
  136. if ((cur = ftello(fs->f)) < 0) {
  137. ast_log(AST_LOG_WARNING, "Unable to determine current position in g726 filestream %p: %s\n", fs, strerror(errno));
  138. return -1;
  139. }
  140. if (fseeko(fs->f, 0, SEEK_END) < 0) {
  141. ast_log(AST_LOG_WARNING, "Unable to seek to end of g726 filestream %p: %s\n", fs, strerror(errno));
  142. return -1;
  143. }
  144. if ((max = ftello(fs->f)) < 0) {
  145. ast_log(AST_LOG_WARNING, "Unable to determine max position in g726 filestream %p: %s\n", fs, strerror(errno));
  146. return -1;
  147. }
  148. /* have to fudge to frame here, so not fully to sample */
  149. distance = sample_offset / 2;
  150. if (whence == SEEK_SET) {
  151. offset = distance;
  152. } else if (whence == SEEK_CUR || whence == SEEK_FORCECUR) {
  153. offset = distance + cur;
  154. } else if (whence == SEEK_END) {
  155. offset = max - distance;
  156. }
  157. if (whence != SEEK_FORCECUR) {
  158. offset = offset > max ? max : offset;
  159. offset = offset < min ? min : offset;
  160. }
  161. return fseeko(fs->f, offset, SEEK_SET);
  162. }
  163. static int g726_trunc(struct ast_filestream *fs)
  164. {
  165. return -1;
  166. }
  167. static off_t g726_tell(struct ast_filestream *fs)
  168. {
  169. return ftello(fs->f) << 1;
  170. }
  171. static struct ast_format_def f_def[] = {
  172. {
  173. .name = "g726-40",
  174. .exts = "g726-40",
  175. .open = g726_40_open,
  176. .rewrite = g726_40_rewrite,
  177. .write = g726_write,
  178. .seek = g726_seek,
  179. .trunc = g726_trunc,
  180. .tell = g726_tell,
  181. .read = g726_read,
  182. .buf_size = BUF_SIZE + AST_FRIENDLY_OFFSET,
  183. .desc_size = sizeof(struct g726_desc),
  184. },
  185. {
  186. .name = "g726-32",
  187. .exts = "g726-32",
  188. .open = g726_32_open,
  189. .rewrite = g726_32_rewrite,
  190. .write = g726_write,
  191. .seek = g726_seek,
  192. .trunc = g726_trunc,
  193. .tell = g726_tell,
  194. .read = g726_read,
  195. .buf_size = BUF_SIZE + AST_FRIENDLY_OFFSET,
  196. .desc_size = sizeof(struct g726_desc),
  197. },
  198. {
  199. .name = "g726-24",
  200. .exts = "g726-24",
  201. .open = g726_24_open,
  202. .rewrite = g726_24_rewrite,
  203. .write = g726_write,
  204. .seek = g726_seek,
  205. .trunc = g726_trunc,
  206. .tell = g726_tell,
  207. .read = g726_read,
  208. .buf_size = BUF_SIZE + AST_FRIENDLY_OFFSET,
  209. .desc_size = sizeof(struct g726_desc),
  210. },
  211. {
  212. .name = "g726-16",
  213. .exts = "g726-16",
  214. .open = g726_16_open,
  215. .rewrite = g726_16_rewrite,
  216. .write = g726_write,
  217. .seek = g726_seek,
  218. .trunc = g726_trunc,
  219. .tell = g726_tell,
  220. .read = g726_read,
  221. .buf_size = BUF_SIZE + AST_FRIENDLY_OFFSET,
  222. .desc_size = sizeof(struct g726_desc),
  223. },
  224. { .desc_size = 0 } /* terminator */
  225. };
  226. static int unload_module(void)
  227. {
  228. int i;
  229. for (i = 0; f_def[i].desc_size ; i++) {
  230. if (ast_format_def_unregister(f_def[i].name))
  231. ast_log(LOG_WARNING, "Failed to unregister format %s.\n", f_def[i].name);
  232. }
  233. return(0);
  234. }
  235. static int load_module(void)
  236. {
  237. int i;
  238. for (i = 0; f_def[i].desc_size ; i++) {
  239. f_def[i].format = ast_format_g726;
  240. if (ast_format_def_register(&f_def[i])) { /* errors are fatal */
  241. ast_log(LOG_WARNING, "Failed to register format %s.\n", f_def[i].name);
  242. unload_module();
  243. return AST_MODULE_LOAD_DECLINE;
  244. }
  245. }
  246. return AST_MODULE_LOAD_SUCCESS;
  247. }
  248. AST_MODULE_INFO(ASTERISK_GPL_KEY, AST_MODFLAG_LOAD_ORDER, "Raw G.726 (16/24/32/40kbps) data",
  249. .support_level = AST_MODULE_SUPPORT_CORE,
  250. .load = load_module,
  251. .unload = unload_module,
  252. .load_pri = AST_MODPRI_APP_DEPEND
  253. );