-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathg729_if_process.c
302 lines (261 loc) · 7.62 KB
/
g729_if_process.c
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
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
/*
* g729_if_process.c
*
* ============================================================================
* Copyright (c) Texas Instruments Inc 2009
*
* Use of this software is controlled by the terms and conditions found in the
* license agreement under which this software has been supplied or provided.
* ============================================================================
*/
#define G729AB_DSPCODEC_INTERNAL
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <xdc/std.h>
#include <ti/sdo/ce/Engine.h>
#include <ti/sdo/dmai/Pause.h>
#include <ti/sdo/dmai/Sound.h>
#include <ti/sdo/dmai/Buffer.h>
#include <ti/sdo/dmai/Loader.h>
#include <ti/sdo/dmai/Rendezvous.h>
#include <ti/sdo/dmai/ce/Sdec1.h>
#include "g729_if_dec.h"
#include "g729_if_enc.h"
#include "Senc1.h"
static short g729_suda_Flen[16] = {
11, 3, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1
};
struct g729_decoder_state {
Engine_Handle hEngine;
Sdec1_Handle hSd1;
SPHDEC1_Params decParams;
SPHDEC1_DynamicParams decDynParams;
Buffer_Handle hInBuf;
Buffer_Handle hOutBuf;
};
/*
* deocder interface initialization
*/
void *
G729_Decoder_Interface_init(void)
{
Buffer_Attrs bAttrs = Buffer_Attrs_DEFAULT;
struct g729_decoder_state *state =
(struct g729_decoder_state *) malloc(sizeof(struct g729_decoder_state));
CERuntime_init();
Dmai_init();
if (state == NULL) {
fprintf(stderr,
"Error to malloc memory for G729AB decoder state variable\n");
return (void *) state;
}
fprintf(stderr, "Engine opening in G729AB decoder init......\n");
if ((state->hEngine = Engine_open("encodedecode", NULL, NULL)) == NULL) {
fprintf(stderr, "Engine open error in G729AB decoder init\n");
free(state);
state = NULL;
return (void *) state;
}
fprintf(stderr, "Engine opened in G729AB decoder init......\n");
state->decParams = Sdec1_Params_DEFAULT;
state->decDynParams = Sdec1_DynamicParams_DEFAULT;
if ((state->hSd1 =
Sdec1_create(state->hEngine, "g729dec", &(state->decParams),
&(state->decDynParams)))
== NULL) {
fprintf(stderr, "Create G729AB decoder handle error\n");
Engine_close(state->hEngine);
free(state);
state = NULL;
return (void *) state;
}
state->hInBuf =
Buffer_create(Sdec1_getInBufSize(state->hSd1), &bAttrs);
state->hOutBuf =
Buffer_create(Sdec1_getOutBufSize(state->hSd1) * 2, &bAttrs);
if ((state->hInBuf == NULL) || (state->hOutBuf == NULL)) {
fprintf(stderr,
"Failed to allocate buffer for G729AB decoder input and output\n");
if (state->hInBuf) {
Buffer_delete(state->hInBuf);
}
if (state->hOutBuf) {
Buffer_delete(state->hOutBuf);
}
if (state->hSd1) {
Sdec1_delete(state->hSd1);
}
if (state->hEngine) {
Engine_close(state->hEngine);
}
free(state);
state = NULL;
return (void *) state;
}
return (void *) state;
}
void
G729_Decoder_Interface_exit(void *s)
{
struct g729_decoder_state *state = (struct g729_decoder_state *) s;
if (state->hInBuf) {
Buffer_delete(state->hInBuf);
}
if (state->hOutBuf) {
Buffer_delete(state->hOutBuf);
}
if (state->hSd1) {
Sdec1_delete(state->hSd1);
}
if (state->hEngine) {
Engine_close(state->hEngine);
}
free(state);
}
void
G729_Decoder_Interface_Decode(void *s, const unsigned char *in, short *out,
int bfi)
{
struct g729_decoder_state *state = (struct g729_decoder_state *) s;
int mode = (int) ((in[0] >> 3) & 0x0f);
int len = g729_suda_Flen[mode];
unsigned char *pIn =
(unsigned char *) Buffer_getUserPtr(state->hInBuf);
unsigned char *pOut =
(unsigned char *) Buffer_getUserPtr(state->hOutBuf);
memcpy(pIn, in, len);
Buffer_setNumBytesUsed(state->hInBuf, len);
if (Sdec1_process(state->hSd1, state->hInBuf, state->hOutBuf) < 0) {
fprintf(stderr, "G729AB Failed to decode speech buffer\n");
}
memcpy((unsigned char *) out, pOut, 80 * 2);
}
struct g729_encoder_state {
Engine_Handle hEngine;
Senc1_Handle hSe1;
SPHENC1_Params encParams;
SPHENC1_DynamicParams encDynParams;
Buffer_Handle hInBuf;
Buffer_Handle hOutBuf;
};
/*
* deocder interface initialization
*/
void *
G729_Encoder_Interface_init(int dtx)
{
Buffer_Attrs bAttrs = Buffer_Attrs_DEFAULT;
struct g729_encoder_state *state =
(struct g729_encoder_state *) malloc(sizeof(struct g729_encoder_state));
CERuntime_init();
Dmai_init();
fprintf(stderr, "Encoder_Interface_init init..................\n");
if (state == NULL) {
fprintf(stderr,
"Error to malloc memory for G729AB encoder state variable\n");
return (void *) state;
}
if ((state->hEngine = Engine_open("encodedecode", NULL, NULL)) == NULL) {
fprintf(stderr, "Engine open error in G729AB encoder init\n");
free(state);
state = NULL;
return (void *) state;
}
fprintf(stderr,
"Engine opened in G729AB encoder init..................\n");
state->encParams = Senc1_Params_DEFAULT;
state->encDynParams = Senc1_DynamicParams_DEFAULT;
state->encParams.vadSelection = dtx;
state->encDynParams.vadFlag = dtx;
if ((state->hSe1 =
Senc1_create(state->hEngine, "g729enc", &(state->encParams),
&(state->encDynParams)))
== NULL) {
fprintf(stderr, "Create G729AB encoder handle error\n");
Engine_close(state->hEngine);
free(state);
state = NULL;
return (void *) state;
}
state->hInBuf =
Buffer_create(Senc1_getInBufSize(state->hSe1) * 2, &bAttrs);
state->hOutBuf =
Buffer_create(Senc1_getOutBufSize(state->hSe1), &bAttrs);
if ((state->hInBuf == NULL) || (state->hOutBuf == NULL)) {
fprintf(stderr,
"Failed to allocate buffer for G729AB encoder input and output\n");
if (state->hInBuf) {
Buffer_delete(state->hInBuf);
}
if (state->hOutBuf) {
Buffer_delete(state->hOutBuf);
}
if (state->hSe1) {
Senc1_delete(state->hSe1);
}
if (state->hEngine) {
Engine_close(state->hEngine);
}
free(state);
state = NULL;
return (void *) state;
}
return (void *) state;
}
void
G729_Encoder_Interface_exit(void *s)
{
struct g729_encoder_state *state = (struct g729_encoder_state *) s;
if (state->hInBuf) {
Buffer_delete(state->hInBuf);
}
if (state->hOutBuf) {
Buffer_delete(state->hOutBuf);
}
if (state->hSe1) {
Senc1_delete(state->hSe1);
}
if (state->hEngine) {
Engine_close(state->hEngine);
}
free(state);
}
void
G729_Encoder_Interface_ctrl(void *s, int dtx)
{
struct g729_encoder_state *state = (struct g729_encoder_state *) s;
SPHENC1_Status encStatus;
XDAS_Int32 status;
SPHENC1_Handle hEncode;
if (state == NULL)
return;
state->encDynParams.vadFlag = dtx;
encStatus.size = sizeof(SPHENC1_Status);
encStatus.data.buf = NULL;
hEncode = Senc1_getVisaHandle(state->hSe1);
status =
SPHENC1_control(hEncode, XDM_SETPARAMS, &(state->encDynParams),
&encStatus);
if (status != SPHENC1_EOK) {
fprintf(stderr, "G729AB encoder error to set parameters\n");
}
}
int
G729_Encoder_Interface_Encode(void *s, const short *speech, unsigned char *out)
{
struct g729_encoder_state *state = (struct g729_encoder_state *) s;
unsigned char *pIn =
(unsigned char *) Buffer_getUserPtr(state->hInBuf);
unsigned char *pOut =
(unsigned char *) Buffer_getUserPtr(state->hOutBuf);
int len;
memcpy(pIn, (unsigned char *) speech, 80 * 2);
Buffer_setNumBytesUsed(state->hInBuf, 80 * 2);
if (Senc1_process(state->hSe1, state->hInBuf, state->hOutBuf) < 0) {
fprintf(stderr, "G729AB failed to encode one frame of speech\n");
}
len = Buffer_getNumBytesUsed(state->hOutBuf);
memcpy(out, pOut, len);
return len;
}