aboutsummaryrefslogtreecommitdiffstats
path: root/src/codec_efr.c
blob: 9804bd9d88b45dff36b3291616017fd0e3cbb695 (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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
/* EFR (GSM 06.60) codec */

/*
 * This file is part of gapk (GSM Audio Pocket Knife).
 *
 * gapk is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * gapk is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with gapk.  If not, see <http://www.gnu.org/licenses/>.
 */

#include <osmocom/gapk/codecs.h>
#include <osmocom/gapk/benchmark.h>
#include <osmocom/gapk/bench.h>

#include "config.h"


#ifdef HAVE_OPENCORE_AMRNB

#include <talloc.h>
#include <assert.h>

#include <opencore-amrnb/interf_dec.h>
#include <opencore-amrnb/interf_enc.h>

/* Internal root talloc context */
extern TALLOC_CTX *gapk_root_ctx;

struct codec_efr_state {
	void *encoder;
	void *decoder;
};


static void *
codec_efr_init(void)
{
	struct codec_efr_state *st;

	st = talloc_zero(gapk_root_ctx, struct codec_efr_state);
	if (!st)
		return NULL;

	st->encoder = Encoder_Interface_init(0);
	st->decoder = Decoder_Interface_init();

	return (void *)st;
}

static void
codec_efr_exit(void *state)
{
	struct codec_efr_state *st = state;

	Decoder_Interface_exit(st->decoder);
	Encoder_Interface_exit(st->encoder);

	talloc_free(st);

	return;
}

static int
codec_efr_encode(void *state, uint8_t *cod, const uint8_t *pcm, unsigned int pcm_len)
{
	struct codec_efr_state *st = state;
	int rv;

	assert(pcm_len == PCM_CANON_LEN);
	BENCHMARK_START;
	rv = Encoder_Interface_Encode(
		st->encoder,
		MR122,
		(const short*) pcm,
		(unsigned char*) cod,
		1
	);
	BENCHMARK_STOP(CODEC_EFR, 1);

	if (rv != 32)
		return -1;

	return 32;
}

static int
codec_efr_decode(void *state, uint8_t *pcm, const uint8_t *cod, unsigned int cod_len)
{
	struct codec_efr_state *st = state;

	assert(cod_len == 32);
	BENCHMARK_START;
	Decoder_Interface_Decode(
		st->decoder,
		(const unsigned char*) cod,
		(short *) pcm,
		0
	);
	BENCHMARK_STOP(CODEC_EFR, 0);

	return PCM_CANON_LEN;
}

#endif /* HAVE_OPENCORE_AMRNB */


const struct osmo_gapk_codec_desc codec_efr_desc = {
	.type = CODEC_EFR,
	.name = "efr",
	.description = "GSM 06.60 Enhanced Full Rate codec",
	.canon_frame_len = EFR_CANON_LEN,
#ifdef HAVE_OPENCORE_AMRNB
	.codec_enc_format_type = FMT_AMR_EFR,
	.codec_dec_format_type = FMT_AMR_EFR,
	.codec_init = codec_efr_init,
	.codec_exit = codec_efr_exit,
	.codec_encode = codec_efr_encode,
	.codec_decode = codec_efr_decode,
#endif
};