format_g726.c 6.9 KB

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