diff options
Diffstat (limited to 'modules/g7221')
-rw-r--r-- | modules/g7221/decode.c | 67 | ||||
-rw-r--r-- | modules/g7221/encode.c | 68 | ||||
-rw-r--r-- | modules/g7221/g7221.c | 49 | ||||
-rw-r--r-- | modules/g7221/g7221.h | 29 | ||||
-rw-r--r-- | modules/g7221/module.mk | 14 | ||||
-rw-r--r-- | modules/g7221/sdp.c | 54 |
6 files changed, 281 insertions, 0 deletions
diff --git a/modules/g7221/decode.c b/modules/g7221/decode.c new file mode 100644 index 0000000..6977a12 --- /dev/null +++ b/modules/g7221/decode.c @@ -0,0 +1,67 @@ +/** + * @file g7221/decode.c G.722.1 Decode + * + * Copyright (C) 2010 Creytiv.com + */ + +#include <re.h> +#include <baresip.h> +#include <g722_1.h> +#include "g7221.h" + + +struct audec_state { + g722_1_decode_state_t dec; +}; + + +int g7221_decode_update(struct audec_state **adsp, const struct aucodec *ac, + const char *fmtp) +{ + const struct g7221_aucodec *g7221 = (struct g7221_aucodec *)ac; + struct audec_state *ads; + (void)fmtp; + + if (!adsp || !ac) + return EINVAL; + + ads = *adsp; + + if (ads) + return 0; + + ads = mem_alloc(sizeof(*ads), NULL); + if (!ads) + return ENOMEM; + + if (!g722_1_decode_init(&ads->dec, g7221->bitrate, ac->srate)) { + mem_deref(ads); + return EPROTO; + } + + *adsp = ads; + + return 0; +} + + +int g7221_decode(struct audec_state *ads, int16_t *sampv, size_t *sampc, + const uint8_t *buf, size_t len) +{ + size_t framec; + + if (!ads || !sampv || !sampc || !buf) + return EINVAL; + + framec = len / ads->dec.bytes_per_frame; + + if (len != ads->dec.bytes_per_frame * framec) + return EPROTO; + + if (*sampc < ads->dec.frame_size * framec) + return ENOMEM; + + *sampc = g722_1_decode(&ads->dec, sampv, buf, (int)len); + + return 0; +} diff --git a/modules/g7221/encode.c b/modules/g7221/encode.c new file mode 100644 index 0000000..8bb5b2b --- /dev/null +++ b/modules/g7221/encode.c @@ -0,0 +1,68 @@ +/** + * @file g7221/encode.c G.722.1 Encode + * + * Copyright (C) 2010 Creytiv.com + */ + +#include <re.h> +#include <baresip.h> +#include <g722_1.h> +#include "g7221.h" + + +struct auenc_state { + g722_1_encode_state_t enc; +}; + + +int g7221_encode_update(struct auenc_state **aesp, const struct aucodec *ac, + struct auenc_param *prm, const char *fmtp) +{ + const struct g7221_aucodec *g7221 = (struct g7221_aucodec *)ac; + struct auenc_state *aes; + (void)prm; + (void)fmtp; + + if (!aesp || !ac) + return EINVAL; + + aes = *aesp; + + if (aes) + return 0; + + aes = mem_alloc(sizeof(*aes), NULL); + if (!aes) + return ENOMEM; + + if (!g722_1_encode_init(&aes->enc, g7221->bitrate, ac->srate)) { + mem_deref(aes); + return EPROTO; + } + + *aesp = aes; + + return 0; +} + + +int g7221_encode(struct auenc_state *aes, uint8_t *buf, size_t *len, + const int16_t *sampv, size_t sampc) +{ + size_t framec; + + if (!aes || !buf || !len || !sampv) + return EINVAL; + + framec = sampc / aes->enc.frame_size; + + if (sampc != aes->enc.frame_size * framec) + return EPROTO; + + if (*len < aes->enc.bytes_per_frame * framec) + return ENOMEM; + + *len = g722_1_encode(&aes->enc, buf, sampv, (int)sampc); + + return 0; +} diff --git a/modules/g7221/g7221.c b/modules/g7221/g7221.c new file mode 100644 index 0000000..a224f46 --- /dev/null +++ b/modules/g7221/g7221.c @@ -0,0 +1,49 @@ +/** + * @file g7221.c G.722.1 Video Codec + * + * Copyright (C) 2010 Creytiv.com + */ + +#include <re.h> +#include <baresip.h> +#include "g7221.h" + + +static struct g7221_aucodec g7221 = { + .ac = { + .name = "G7221", + .srate = 16000, + .ch = 1, + .encupdh = g7221_encode_update, + .ench = g7221_encode, + .decupdh = g7221_decode_update, + .dech = g7221_decode, + .fmtp_ench = g7221_fmtp_enc, + .fmtp_cmph = g7221_fmtp_cmp, + }, + .bitrate = 32000, +}; + + +static int module_init(void) +{ + aucodec_register((struct aucodec *)&g7221); + + return 0; +} + + +static int module_close(void) +{ + aucodec_unregister((struct aucodec *)&g7221); + + return 0; +} + + +EXPORT_SYM const struct mod_export DECL_EXPORTS(g7221) = { + "g7221", + "audio codec", + module_init, + module_close, +}; diff --git a/modules/g7221/g7221.h b/modules/g7221/g7221.h new file mode 100644 index 0000000..635fc01 --- /dev/null +++ b/modules/g7221/g7221.h @@ -0,0 +1,29 @@ +/** + * @file g7221.h Private G.722.1 Interface + * + * Copyright (C) 2010 Creytiv.com + */ + +struct g7221_aucodec { + struct aucodec ac; + uint32_t bitrate; +}; + +/* Encode */ +int g7221_encode_update(struct auenc_state **aesp, const struct aucodec *ac, + struct auenc_param *prm, const char *fmtp); +int g7221_encode(struct auenc_state *aes, uint8_t *buf, size_t *len, + const int16_t *sampv, size_t sampc); + + +/* Decode */ +int g7221_decode_update(struct audec_state **adsp, const struct aucodec *ac, + const char *fmtp); +int g7221_decode(struct audec_state *ads, int16_t *sampv, size_t *sampc, + const uint8_t *buf, size_t len); + + +/* SDP */ +int g7221_fmtp_enc(struct mbuf *mb, const struct sdp_format *fmt, + bool offer, void *arg); +bool g7221_fmtp_cmp(const char *lfmtp, const char *rfmtp, void *arg); diff --git a/modules/g7221/module.mk b/modules/g7221/module.mk new file mode 100644 index 0000000..e0471a7 --- /dev/null +++ b/modules/g7221/module.mk @@ -0,0 +1,14 @@ +# +# module.mk +# +# Copyright (C) 2010 Creytiv.com +# + +MOD := g7221 +$(MOD)_SRCS += decode.c +$(MOD)_SRCS += encode.c +$(MOD)_SRCS += g7221.c +$(MOD)_SRCS += sdp.c +$(MOD)_LFLAGS += -lg722_1 + +include mk/mod.mk diff --git a/modules/g7221/sdp.c b/modules/g7221/sdp.c new file mode 100644 index 0000000..b46351e --- /dev/null +++ b/modules/g7221/sdp.c @@ -0,0 +1,54 @@ +/** + * @file g7221/sdp.c H.264 SDP Functions + * + * Copyright (C) 2010 Creytiv.com + */ + +#include <re.h> +#include <baresip.h> +#include "g7221.h" + + +static uint32_t g7221_bitrate(const char *fmtp) +{ + struct pl pl, bitrate; + + if (!fmtp) + return 0; + + pl_set_str(&pl, fmtp); + + if (fmt_param_get(&pl, "bitrate", &bitrate)) + return pl_u32(&bitrate); + + return 0; +} + + +int g7221_fmtp_enc(struct mbuf *mb, const struct sdp_format *fmt, + bool offer, void *arg) +{ + const struct g7221_aucodec *g7221 = arg; + (void)offer; + + if (!mb || !fmt || !g7221) + return 0; + + return mbuf_printf(mb, "a=fmtp:%s bitrate=%u\r\n", + fmt->id, g7221->bitrate); +} + + +bool g7221_fmtp_cmp(const char *lfmtp, const char *rfmtp, void *arg) +{ + const struct g7221_aucodec *g7221 = arg; + (void)lfmtp; + + if (!g7221) + return false; + + if (g7221->bitrate != g7221_bitrate(rfmtp)) + return false; + + return true; +} |