bvcodec.c 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225
  1. #include "precompile.h"
  2. #pragma warning(disable: 4005)
  3. #include "bvcodec.h"
  4. #include "broadvoice.h"
  5. #define NB_CLOCK 8000
  6. #define WB_CLOCK 16000
  7. static codec_factory_t g_bv16_factory;
  8. typedef struct bv16_codec_t
  9. {
  10. codec_t base;
  11. bv16_encode_state_t *encoder;
  12. bv16_decode_state_t *decoder;
  13. } bv16_codec_t;
  14. static codec_t* bv16_codec_create(apr_pool_t *pool, int clock, int ptime)
  15. {
  16. bv16_codec_t *codec;
  17. if (clock != NB_CLOCK)
  18. return NULL;
  19. codec = apr_palloc(pool, sizeof(bv16_codec_t));
  20. codec->base.pool = pool;
  21. codec->base.ptime = ptime;
  22. codec->base.clock = clock;
  23. codec->base.factory = &g_bv16_factory;
  24. codec->base.kvp_table = apr_table_make(pool, 3);
  25. codec->encoder = bv16_encode_init(NULL);
  26. codec->decoder = bv16_decode_init(NULL);
  27. return &codec->base;
  28. }
  29. static void bv16_codec_destroy(codec_t *c)
  30. {
  31. bv16_codec_t *codec = (bv16_codec_t *)c;
  32. bv16_encode_free(codec->encoder);
  33. bv16_decode_free(codec->decoder);
  34. }
  35. static apr_status_t bv16_codec_set_param(codec_t *codec,
  36. const char *key,
  37. const char *value)
  38. {
  39. return APR_ENOTIMPL;
  40. }
  41. static apr_status_t bv16_codec_get_param(codec_t *codec,
  42. const char *key,
  43. char *value)
  44. {
  45. return APR_ENOTIMPL;
  46. }
  47. static apr_status_t bv16_codec_init(codec_t *codec)
  48. {
  49. return APR_SUCCESS;
  50. }
  51. static apr_status_t bv16_codec_term(codec_t *codec)
  52. {
  53. return APR_SUCCESS;
  54. }
  55. static apr_status_t bv16_codec_encode(codec_t *c,
  56. const void *inbuf,
  57. int insize,
  58. void *outbuf,
  59. int *outsize)
  60. {
  61. bv16_codec_t *codec = (bv16_codec_t *)c;
  62. if (insize) {
  63. *outsize = bv16_encode(codec->encoder, outbuf, inbuf, insize/2);
  64. } else {
  65. *outsize = 0;
  66. }
  67. return APR_SUCCESS;
  68. }
  69. static apr_status_t bv16_codec_decode(codec_t *c,
  70. const void *inbuf,
  71. int insize,
  72. void *outbuf,
  73. int *outsize)
  74. {
  75. bv16_codec_t *codec = (bv16_codec_t *)c;
  76. if (insize) {
  77. *outsize = 2 * bv16_decode(codec->decoder, outbuf, inbuf, insize);
  78. } else {
  79. *outsize = 0;
  80. }
  81. return APR_SUCCESS;
  82. }
  83. static codec_factory_t g_bv32_factory;
  84. typedef struct bv32_codec_t
  85. {
  86. codec_t base;
  87. bv32_encode_state_t *encoder;
  88. bv32_decode_state_t *decoder;
  89. } bv32_codec_t;
  90. static codec_t* bv32_codec_create(apr_pool_t *pool, int clock, int ptime)
  91. {
  92. bv32_codec_t *codec;
  93. apr_status_t status;
  94. apr_pool_t *subpool;
  95. if (clock != WB_CLOCK)
  96. return NULL;
  97. status = apr_pool_create(&subpool, pool);
  98. if (status != APR_SUCCESS)
  99. return NULL;
  100. codec = apr_palloc(pool, sizeof(bv32_codec_t));
  101. codec->base.pool = subpool;
  102. codec->base.ptime = ptime;
  103. codec->base.clock = clock;
  104. codec->base.factory = &g_bv16_factory;
  105. codec->base.kvp_table = apr_table_make(subpool, 3);
  106. codec->encoder = bv32_encode_init(NULL);
  107. codec->decoder = bv32_decode_init(NULL);
  108. return &codec->base;
  109. }
  110. static void bv32_codec_destroy(codec_t *c)
  111. {
  112. bv32_codec_t *codec = (bv32_codec_t *)c;
  113. bv32_encode_free(codec->encoder);
  114. bv32_decode_free(codec->decoder);
  115. apr_pool_destroy(codec->base.pool);
  116. }
  117. static apr_status_t bv32_codec_set_param(codec_t *codec,
  118. const char *key,
  119. const char *value)
  120. {
  121. return APR_ENOTIMPL;
  122. }
  123. static apr_status_t bv32_codec_get_param(codec_t *codec,
  124. const char *key,
  125. char *value)
  126. {
  127. return APR_ENOTIMPL;
  128. }
  129. static apr_status_t bv32_codec_init(codec_t *codec)
  130. {
  131. return APR_SUCCESS;
  132. }
  133. static apr_status_t bv32_codec_term(codec_t *codec)
  134. {
  135. return APR_SUCCESS;
  136. }
  137. static apr_status_t bv32_codec_encode(codec_t *c,
  138. const void *inbuf,
  139. int insize,
  140. void *outbuf,
  141. int *outsize)
  142. {
  143. bv32_codec_t *codec = (bv32_codec_t *)c;
  144. if (insize) {
  145. *outsize = bv32_encode(codec->encoder, outbuf, inbuf, insize/2);
  146. } else {
  147. *outsize = 0;
  148. }
  149. return APR_SUCCESS;
  150. }
  151. static apr_status_t bv32_codec_decode(codec_t *c,
  152. const void *inbuf,
  153. int insize,
  154. void *outbuf,
  155. int *outsize)
  156. {
  157. bv32_codec_t *codec = (bv32_codec_t *)c;
  158. if (insize) {
  159. *outsize = 2 * bv32_decode(codec->decoder, outbuf, inbuf, insize);
  160. } else {
  161. *outsize = 0;
  162. }
  163. return APR_SUCCESS;
  164. }
  165. apr_status_t bv_codec_factory_init()
  166. {
  167. apr_status_t status;
  168. g_bv16_factory.name = "BV16";
  169. g_bv16_factory.create = &bv16_codec_create;
  170. g_bv16_factory.destroy = &bv16_codec_destroy;
  171. g_bv16_factory.set_param = &bv16_codec_set_param;
  172. g_bv16_factory.get_param = &bv16_codec_get_param;
  173. g_bv16_factory.init = &bv16_codec_init;
  174. g_bv16_factory.term = &bv16_codec_term;
  175. g_bv16_factory.encode = &bv16_codec_encode;
  176. g_bv16_factory.decode = &bv16_codec_decode;
  177. status = codec_factory_register(&g_bv16_factory);
  178. if (status != APR_SUCCESS)
  179. return status;
  180. g_bv32_factory.name = "BV32";
  181. g_bv32_factory.create = &bv32_codec_create;
  182. g_bv32_factory.destroy = &bv32_codec_destroy;
  183. g_bv32_factory.set_param = &bv32_codec_set_param;
  184. g_bv32_factory.get_param = &bv32_codec_get_param;
  185. g_bv32_factory.init = &bv32_codec_init;
  186. g_bv32_factory.term = &bv32_codec_term;
  187. g_bv32_factory.encode = &bv32_codec_encode;
  188. g_bv32_factory.decode = &bv32_codec_decode;
  189. status = codec_factory_register(&g_bv32_factory);
  190. if (status != APR_SUCCESS)
  191. return status;
  192. return status;
  193. }
  194. apr_status_t bv_codec_factory_term()
  195. {
  196. apr_status_t status;
  197. status = codec_factory_unregister(&g_bv16_factory);
  198. if (status != APR_SUCCESS)
  199. return status;
  200. status = codec_factory_unregister(&g_bv32_factory);
  201. if (status != APR_SUCCESS)
  202. return status;
  203. return status;
  204. }