summaryrefslogtreecommitdiff
path: root/modules/g7221/encode.c
blob: 8dec82f74acdbc1e7e0af73fac69ff6686aebcc8 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
/**
 * @file g7221/encode.c G.722.1 Encode
 *
 * Copyright (C) 2010 Creytiv.com
 */

#include <re.h>
#include <baresip.h>

#define G722_1_EXPOSE_INTERNAL_STRUCTURES

#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,
		 int fmt, const void *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;
}