format_g726.c 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257
  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. int 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 (%d) (%s)!\n", res, strerror(errno));
  110. return NULL;
  111. }
  112. *whennext = s->fr.samples;
  113. return &s->fr;
  114. }
  115. static int g726_write(struct ast_filestream *s, struct ast_frame *f)
  116. {
  117. int res;
  118. struct g726_desc *fs = (struct g726_desc *)s->_private;
  119. if (f->datalen % frame_size[fs->rate]) {
  120. ast_log(LOG_WARNING, "Invalid data length %d, should be multiple of %d\n",
  121. f->datalen, frame_size[fs->rate]);
  122. return -1;
  123. }
  124. if ((res = fwrite(f->data.ptr, 1, f->datalen, s->f)) != f->datalen) {
  125. ast_log(LOG_WARNING, "Bad write (%d/%d): %s\n",
  126. res, frame_size[fs->rate], strerror(errno));
  127. return -1;
  128. }
  129. return 0;
  130. }
  131. static int g726_seek(struct ast_filestream *fs, off_t sample_offset, int whence)
  132. {
  133. return -1;
  134. }
  135. static int g726_trunc(struct ast_filestream *fs)
  136. {
  137. return -1;
  138. }
  139. static off_t g726_tell(struct ast_filestream *fs)
  140. {
  141. return -1;
  142. }
  143. static struct ast_format_def f[] = {
  144. {
  145. .name = "g726-40",
  146. .exts = "g726-40",
  147. .open = g726_40_open,
  148. .rewrite = g726_40_rewrite,
  149. .write = g726_write,
  150. .seek = g726_seek,
  151. .trunc = g726_trunc,
  152. .tell = g726_tell,
  153. .read = g726_read,
  154. .buf_size = BUF_SIZE + AST_FRIENDLY_OFFSET,
  155. .desc_size = sizeof(struct g726_desc),
  156. },
  157. {
  158. .name = "g726-32",
  159. .exts = "g726-32",
  160. .open = g726_32_open,
  161. .rewrite = g726_32_rewrite,
  162. .write = g726_write,
  163. .seek = g726_seek,
  164. .trunc = g726_trunc,
  165. .tell = g726_tell,
  166. .read = g726_read,
  167. .buf_size = BUF_SIZE + AST_FRIENDLY_OFFSET,
  168. .desc_size = sizeof(struct g726_desc),
  169. },
  170. {
  171. .name = "g726-24",
  172. .exts = "g726-24",
  173. .open = g726_24_open,
  174. .rewrite = g726_24_rewrite,
  175. .write = g726_write,
  176. .seek = g726_seek,
  177. .trunc = g726_trunc,
  178. .tell = g726_tell,
  179. .read = g726_read,
  180. .buf_size = BUF_SIZE + AST_FRIENDLY_OFFSET,
  181. .desc_size = sizeof(struct g726_desc),
  182. },
  183. {
  184. .name = "g726-16",
  185. .exts = "g726-16",
  186. .open = g726_16_open,
  187. .rewrite = g726_16_rewrite,
  188. .write = g726_write,
  189. .seek = g726_seek,
  190. .trunc = g726_trunc,
  191. .tell = g726_tell,
  192. .read = g726_read,
  193. .buf_size = BUF_SIZE + AST_FRIENDLY_OFFSET,
  194. .desc_size = sizeof(struct g726_desc),
  195. },
  196. { .desc_size = 0 } /* terminator */
  197. };
  198. static int load_module(void)
  199. {
  200. int i;
  201. for (i = 0; f[i].desc_size ; i++) {
  202. f[i].format = ast_format_g726;
  203. if (ast_format_def_register(&f[i])) { /* errors are fatal */
  204. ast_log(LOG_WARNING, "Failed to register format %s.\n", f[i].name);
  205. return AST_MODULE_LOAD_FAILURE;
  206. }
  207. }
  208. return AST_MODULE_LOAD_SUCCESS;
  209. }
  210. static int unload_module(void)
  211. {
  212. int i;
  213. for (i = 0; f[i].desc_size ; i++) {
  214. if (ast_format_def_unregister(f[i].name))
  215. ast_log(LOG_WARNING, "Failed to unregister format %s.\n", f[i].name);
  216. }
  217. return(0);
  218. }
  219. AST_MODULE_INFO(ASTERISK_GPL_KEY, AST_MODFLAG_LOAD_ORDER, "Raw G.726 (16/24/32/40kbps) data",
  220. .support_level = AST_MODULE_SUPPORT_CORE,
  221. .load = load_module,
  222. .unload = unload_module,
  223. .load_pri = AST_MODPRI_APP_DEPEND
  224. );