123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234 |
- #include "precompile.h"
- #include "g711codec.h"
- #include <spandsp.h>
- static codec_factory_t g_g711alaw_factory;
- static codec_t* g711alaw_codec_create(apr_pool_t *pool, int clock, int ptime)
- {
- codec_t *codec;
- codec = apr_palloc(pool, sizeof(codec_t));
- codec->pool = pool;
- codec->ptime = ptime;
- codec->clock = clock;
- codec->factory = &g_g711alaw_factory;
- codec->kvp_table = apr_table_make(pool, 3);
- return codec;
- }
- static void g711alaw_codec_destroy(codec_t *codec)
- {
-
- }
- static apr_status_t g711alaw_codec_set_param(codec_t *codec,
- const char *key,
- const char *value)
- {
- return APR_ENOTIMPL;
- }
- static apr_status_t g711alaw_codec_get_param(codec_t *codec,
- const char *key,
- char *value)
- {
- return APR_ENOTIMPL;
- }
- static apr_status_t g711alaw_codec_init(codec_t *codec)
- {
- return APR_SUCCESS;
- }
- static apr_status_t g711alaw_codec_term(codec_t *codec)
- {
- return APR_SUCCESS;
- }
- static apr_status_t g711alaw_codec_encode(codec_t *codec,
- const void *inbuf,
- int insize,
- void *outbuf,
- int *outsize)
- {
- if (insize) {
- int i;
- int samples = insize >> 1;
- for (i = 0; i < samples; ++i) {
- *((unsigned char*)outbuf+i) = linear_to_alaw(*((short*)inbuf+i));
- }
- *outsize = samples;
- } else {
- *outsize = 0;
- }
- return APR_SUCCESS;
- }
- static apr_status_t g711alaw_codec_decode(codec_t *codec,
- const void *inbuf,
- int insize,
- void *outbuf,
- int *outsize)
- {
- if (insize) {
- int i;
- int samples = insize;
- if (*outsize < 2*samples)
- return APR_EGENERAL;
- for (i = 0; i < samples; ++i) {
- *((short*)outbuf+i) = alaw_to_linear(*((unsigned char*)inbuf+i));
- }
- *outsize = 2*samples;
- } else {
- *outsize = 0;
- }
- return APR_SUCCESS;
- }
- apr_status_t pcma_codec_decode(
- const void *inbuf,
- int insize,
- void *outbuf,
- int *outsize)
- {
- g711alaw_codec_decode(NULL, inbuf, insize, outbuf, outsize);
- return APR_SUCCESS;
- }
- static codec_factory_t g_g711ulaw_factory;
- static codec_t* g711ulaw_codec_create(apr_pool_t *pool, int clock, int ptime)
- {
- codec_t *codec;
- apr_status_t status;
- apr_pool_t *subpool;
- status = apr_pool_create(&subpool, pool);
- if (status != APR_SUCCESS)
- return NULL;
- codec = apr_palloc(pool, sizeof(codec_t));
- codec->pool = subpool;
- codec->ptime = ptime;
- codec->clock = clock;
- codec->factory = &g_g711ulaw_factory;
- codec->kvp_table = apr_table_make(subpool, 3);
- return codec;
- }
- static void g711ulaw_codec_destroy(codec_t *codec)
- {
- apr_pool_destroy(codec->pool);
- }
- static apr_status_t g711ulaw_codec_set_param(codec_t *codec,
- const char *key,
- const char *value)
- {
- return APR_ENOTIMPL;
- }
- static apr_status_t g711ulaw_codec_get_param(codec_t *codec,
- const char *key,
- char *value)
- {
- return APR_ENOTIMPL;
- }
- static apr_status_t g711ulaw_codec_init(codec_t *codec)
- {
- return APR_SUCCESS;
- }
- static apr_status_t g711ulaw_codec_term(codec_t *codec)
- {
- return APR_SUCCESS;
- }
- static apr_status_t g711ulaw_codec_encode(codec_t *codec,
- const void *inbuf,
- int insize,
- void *outbuf,
- int *outsize)
- {
- if (insize) {
- int i;
- int samples = insize >> 1;
- if (*outsize < samples)
- return APR_EGENERAL;
- for (i = 0; i < samples; ++i) {
- *((unsigned char*)outbuf+i) = linear_to_ulaw(*((short*)inbuf+i));
- }
- *outsize = samples;
- } else {
- *outsize = 0;
- }
- return APR_SUCCESS;
- }
- static apr_status_t g711ulaw_codec_decode(codec_t *codec,
- const void *inbuf,
- int insize,
- void *outbuf,
- int *outsize)
- {
- if (insize) {
- int i;
- int samples = insize;
- if (*outsize < 2*samples)
- return APR_EGENERAL;
- for (i = 0; i < samples; ++i) {
- *((short*)outbuf+i) = ulaw_to_linear(*((unsigned char*)inbuf+i));
- }
- *outsize = 2*samples;
- } else {
- *outsize = 0;
- }
- return APR_SUCCESS;
- }
- apr_status_t pcmu_codec_decode(const void *inbuf,int insize,void *outbuf,int *outsize)
- {
- g711ulaw_codec_decode(NULL, inbuf, insize, outbuf, outsize);
- return APR_SUCCESS;
- }
- apr_status_t g711_codec_factory_init()
- {
- apr_status_t status;
- g_g711alaw_factory.name = "PCMA";
- g_g711alaw_factory.create = &g711alaw_codec_create;
- g_g711alaw_factory.destroy = &g711alaw_codec_destroy;
- g_g711alaw_factory.set_param = &g711alaw_codec_set_param;
- g_g711alaw_factory.get_param = &g711alaw_codec_get_param;
- g_g711alaw_factory.init = &g711alaw_codec_init;
- g_g711alaw_factory.term = &g711alaw_codec_term;
- g_g711alaw_factory.encode = &g711alaw_codec_encode;
- g_g711alaw_factory.decode = &g711alaw_codec_decode;
- status = codec_factory_register(&g_g711alaw_factory);
- if (status != APR_SUCCESS)
- return status;
- g_g711ulaw_factory.name = "PCMU";
- g_g711ulaw_factory.create = &g711ulaw_codec_create;
- g_g711ulaw_factory.destroy = &g711ulaw_codec_destroy;
- g_g711ulaw_factory.set_param = &g711ulaw_codec_set_param;
- g_g711ulaw_factory.get_param = &g711ulaw_codec_get_param;
- g_g711ulaw_factory.init = &g711ulaw_codec_init;
- g_g711ulaw_factory.term = &g711ulaw_codec_term;
- g_g711ulaw_factory.encode = &g711ulaw_codec_encode;
- g_g711ulaw_factory.decode = &g711ulaw_codec_decode;
- status = codec_factory_register(&g_g711ulaw_factory);
- if (status != APR_SUCCESS)
- return status;
- return status;
- }
- apr_status_t g711_codec_factory_term()
- {
- apr_status_t status;
- status = codec_factory_unregister(&g_g711alaw_factory);
- if (status != APR_SUCCESS)
- return status;
- status = codec_factory_unregister(&g_g711ulaw_factory);
- if (status != APR_SUCCESS)
- return status;
- return status;
- }
|