format_g726.c 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265
  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. #include "asterisk.h"
  30. ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
  31. #include "asterisk/mod_format.h"
  32. #include "asterisk/module.h"
  33. #include "asterisk/endian.h"
  34. #define RATE_40 0
  35. #define RATE_32 1
  36. #define RATE_24 2
  37. #define RATE_16 3
  38. /* We can only read/write chunks of FRAME_TIME ms G.726 data */
  39. #define FRAME_TIME 10 /* 10 ms size */
  40. #define BUF_SIZE (5*FRAME_TIME) /* max frame size in bytes ? */
  41. /* Frame sizes in bytes */
  42. static int frame_size[4] = {
  43. FRAME_TIME * 5,
  44. FRAME_TIME * 4,
  45. FRAME_TIME * 3,
  46. FRAME_TIME * 2
  47. };
  48. struct g726_desc {
  49. int rate; /* RATE_* defines */
  50. };
  51. /*
  52. * Rate dependant format functions (open, rewrite)
  53. */
  54. static int g726_open(struct ast_filestream *tmp, int rate)
  55. {
  56. struct g726_desc *s = (struct g726_desc *)tmp->_private;
  57. s->rate = rate;
  58. ast_debug(1, "Created filestream G.726-%dk.\n", 40 - s->rate * 8);
  59. return 0;
  60. }
  61. static int g726_40_open(struct ast_filestream *s)
  62. {
  63. return g726_open(s, RATE_40);
  64. }
  65. static int g726_32_open(struct ast_filestream *s)
  66. {
  67. return g726_open(s, RATE_32);
  68. }
  69. static int g726_24_open(struct ast_filestream *s)
  70. {
  71. return g726_open(s, RATE_24);
  72. }
  73. static int g726_16_open(struct ast_filestream *s)
  74. {
  75. return g726_open(s, RATE_16);
  76. }
  77. static int g726_40_rewrite(struct ast_filestream *s, const char *comment)
  78. {
  79. return g726_open(s, RATE_40);
  80. }
  81. static int g726_32_rewrite(struct ast_filestream *s, const char *comment)
  82. {
  83. return g726_open(s, RATE_32);
  84. }
  85. static int g726_24_rewrite(struct ast_filestream *s, const char *comment)
  86. {
  87. return g726_open(s, RATE_24);
  88. }
  89. static int g726_16_rewrite(struct ast_filestream *s, const char *comment)
  90. {
  91. return g726_open(s, RATE_16);
  92. }
  93. /*
  94. * Rate independent format functions (read, write)
  95. */
  96. static struct ast_frame *g726_read(struct ast_filestream *s, int *whennext)
  97. {
  98. int res;
  99. struct g726_desc *fs = (struct g726_desc *)s->_private;
  100. /* Send a frame from the file to the appropriate channel */
  101. s->fr.frametype = AST_FRAME_VOICE;
  102. s->fr.subclass = AST_FORMAT_G726;
  103. s->fr.mallocd = 0;
  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, 1, s->fr.datalen, s->f)) != s->fr.datalen) {
  107. if (res)
  108. ast_log(LOG_WARNING, "Short read (%d) (%s)!\n", res, strerror(errno));
  109. return NULL;
  110. }
  111. *whennext = s->fr.samples;
  112. return &s->fr;
  113. }
  114. static int g726_write(struct ast_filestream *s, struct ast_frame *f)
  115. {
  116. int res;
  117. struct g726_desc *fs = (struct g726_desc *)s->_private;
  118. if (f->frametype != AST_FRAME_VOICE) {
  119. ast_log(LOG_WARNING, "Asked to write non-voice frame!\n");
  120. return -1;
  121. }
  122. if (f->subclass != AST_FORMAT_G726) {
  123. ast_log(LOG_WARNING, "Asked to write non-G726 frame (%d)!\n",
  124. f->subclass);
  125. return -1;
  126. }
  127. if (f->datalen % frame_size[fs->rate]) {
  128. ast_log(LOG_WARNING, "Invalid data length %d, should be multiple of %d\n",
  129. f->datalen, frame_size[fs->rate]);
  130. return -1;
  131. }
  132. if ((res = fwrite(f->data, 1, f->datalen, s->f)) != f->datalen) {
  133. ast_log(LOG_WARNING, "Bad write (%d/%d): %s\n",
  134. res, frame_size[fs->rate], strerror(errno));
  135. return -1;
  136. }
  137. return 0;
  138. }
  139. static int g726_seek(struct ast_filestream *fs, off_t sample_offset, int whence)
  140. {
  141. return -1;
  142. }
  143. static int g726_trunc(struct ast_filestream *fs)
  144. {
  145. return -1;
  146. }
  147. static off_t g726_tell(struct ast_filestream *fs)
  148. {
  149. return -1;
  150. }
  151. static const struct ast_format f[] = {
  152. {
  153. .name = "g726-40",
  154. .exts = "g726-40",
  155. .format = AST_FORMAT_G726,
  156. .open = g726_40_open,
  157. .rewrite = g726_40_rewrite,
  158. .write = g726_write,
  159. .seek = g726_seek,
  160. .trunc = g726_trunc,
  161. .tell = g726_tell,
  162. .read = g726_read,
  163. .buf_size = BUF_SIZE + AST_FRIENDLY_OFFSET,
  164. .desc_size = sizeof(struct g726_desc),
  165. },
  166. {
  167. .name = "g726-32",
  168. .exts = "g726-32",
  169. .format = AST_FORMAT_G726,
  170. .open = g726_32_open,
  171. .rewrite = g726_32_rewrite,
  172. .write = g726_write,
  173. .seek = g726_seek,
  174. .trunc = g726_trunc,
  175. .tell = g726_tell,
  176. .read = g726_read,
  177. .buf_size = BUF_SIZE + AST_FRIENDLY_OFFSET,
  178. .desc_size = sizeof(struct g726_desc),
  179. },
  180. {
  181. .name = "g726-24",
  182. .exts = "g726-24",
  183. .format = AST_FORMAT_G726,
  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. .format = AST_FORMAT_G726,
  198. .open = g726_16_open,
  199. .rewrite = g726_16_rewrite,
  200. .write = g726_write,
  201. .seek = g726_seek,
  202. .trunc = g726_trunc,
  203. .tell = g726_tell,
  204. .read = g726_read,
  205. .buf_size = BUF_SIZE + AST_FRIENDLY_OFFSET,
  206. .desc_size = sizeof(struct g726_desc),
  207. },
  208. { .format = 0 } /* terminator */
  209. };
  210. static int load_module(void)
  211. {
  212. int i;
  213. for (i = 0; f[i].format ; i++) {
  214. if (ast_format_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].format ; i++) {
  225. if (ast_format_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_FIRST, "Raw G.726 (16/24/32/40kbps) data",
  231. .load = load_module,
  232. .unload = unload_module,
  233. );