format_g726.c 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267
  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. #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. int res;
  102. struct g726_desc *fs = (struct g726_desc *)s->_private;
  103. /* Send a frame from the file to the appropriate channel */
  104. s->fr.frametype = AST_FRAME_VOICE;
  105. ast_format_set(&s->fr.subclass.format, AST_FORMAT_G726, 0);
  106. s->fr.mallocd = 0;
  107. AST_FRAME_SET_BUFFER(&s->fr, s->buf, AST_FRIENDLY_OFFSET, frame_size[fs->rate]);
  108. s->fr.samples = 8 * FRAME_TIME;
  109. if ((res = fread(s->fr.data.ptr, 1, s->fr.datalen, s->f)) != s->fr.datalen) {
  110. if (res)
  111. ast_log(LOG_WARNING, "Short read (%d) (%s)!\n", res, strerror(errno));
  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->frametype != AST_FRAME_VOICE) {
  122. ast_log(LOG_WARNING, "Asked to write non-voice frame!\n");
  123. return -1;
  124. }
  125. if (f->subclass.format.id != AST_FORMAT_G726) {
  126. ast_log(LOG_WARNING, "Asked to write non-G726 frame (%s)!\n",
  127. ast_getformatname(&f->subclass.format));
  128. return -1;
  129. }
  130. if (f->datalen % frame_size[fs->rate]) {
  131. ast_log(LOG_WARNING, "Invalid data length %d, should be multiple of %d\n",
  132. f->datalen, frame_size[fs->rate]);
  133. return -1;
  134. }
  135. if ((res = fwrite(f->data.ptr, 1, f->datalen, s->f)) != f->datalen) {
  136. ast_log(LOG_WARNING, "Bad write (%d/%d): %s\n",
  137. res, frame_size[fs->rate], strerror(errno));
  138. return -1;
  139. }
  140. return 0;
  141. }
  142. static int g726_seek(struct ast_filestream *fs, off_t sample_offset, int whence)
  143. {
  144. return -1;
  145. }
  146. static int g726_trunc(struct ast_filestream *fs)
  147. {
  148. return -1;
  149. }
  150. static off_t g726_tell(struct ast_filestream *fs)
  151. {
  152. return -1;
  153. }
  154. static struct ast_format_def f[] = {
  155. {
  156. .name = "g726-40",
  157. .exts = "g726-40",
  158. .open = g726_40_open,
  159. .rewrite = g726_40_rewrite,
  160. .write = g726_write,
  161. .seek = g726_seek,
  162. .trunc = g726_trunc,
  163. .tell = g726_tell,
  164. .read = g726_read,
  165. .buf_size = BUF_SIZE + AST_FRIENDLY_OFFSET,
  166. .desc_size = sizeof(struct g726_desc),
  167. },
  168. {
  169. .name = "g726-32",
  170. .exts = "g726-32",
  171. .open = g726_32_open,
  172. .rewrite = g726_32_rewrite,
  173. .write = g726_write,
  174. .seek = g726_seek,
  175. .trunc = g726_trunc,
  176. .tell = g726_tell,
  177. .read = g726_read,
  178. .buf_size = BUF_SIZE + AST_FRIENDLY_OFFSET,
  179. .desc_size = sizeof(struct g726_desc),
  180. },
  181. {
  182. .name = "g726-24",
  183. .exts = "g726-24",
  184. .open = g726_24_open,
  185. .rewrite = g726_24_rewrite,
  186. .write = g726_write,
  187. .seek = g726_seek,
  188. .trunc = g726_trunc,
  189. .tell = g726_tell,
  190. .read = g726_read,
  191. .buf_size = BUF_SIZE + AST_FRIENDLY_OFFSET,
  192. .desc_size = sizeof(struct g726_desc),
  193. },
  194. {
  195. .name = "g726-16",
  196. .exts = "g726-16",
  197. .open = g726_16_open,
  198. .rewrite = g726_16_rewrite,
  199. .write = g726_write,
  200. .seek = g726_seek,
  201. .trunc = g726_trunc,
  202. .tell = g726_tell,
  203. .read = g726_read,
  204. .buf_size = BUF_SIZE + AST_FRIENDLY_OFFSET,
  205. .desc_size = sizeof(struct g726_desc),
  206. },
  207. { .desc_size = 0 } /* terminator */
  208. };
  209. static int load_module(void)
  210. {
  211. int i;
  212. for (i = 0; f[i].desc_size ; i++) {
  213. ast_format_set(&f[i].format, AST_FORMAT_G726, 0);
  214. if (ast_format_def_register(&f[i])) { /* errors are fatal */
  215. ast_log(LOG_WARNING, "Failed to register format %s.\n", f[i].name);
  216. return AST_MODULE_LOAD_FAILURE;
  217. }
  218. }
  219. return AST_MODULE_LOAD_SUCCESS;
  220. }
  221. static int unload_module(void)
  222. {
  223. int i;
  224. for (i = 0; f[i].desc_size ; i++) {
  225. if (ast_format_def_unregister(f[i].name))
  226. ast_log(LOG_WARNING, "Failed to unregister format %s.\n", f[i].name);
  227. }
  228. return(0);
  229. }
  230. AST_MODULE_INFO(ASTERISK_GPL_KEY, AST_MODFLAG_LOAD_ORDER, "Raw G.726 (16/24/32/40kbps) data",
  231. .load = load_module,
  232. .unload = unload_module,
  233. .load_pri = AST_MODPRI_APP_DEPEND
  234. );