-
Notifications
You must be signed in to change notification settings - Fork 3
/
ilbc.c
354 lines (316 loc) · 8.57 KB
/
ilbc.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
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
/*
ilbc.c
Copyright (C) 2015 Belledonne Communications, Grenoble, France
This program 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 2
of the License, or (at your option) any later version.
This program 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 this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*/
#include "ilbc.h"
#include "mediastreamer2/msfilter.h"
#include "mediastreamer2/mscodecutils.h"
#include "mediastreamer2/msticker.h"
#ifdef HAVE_ms_bufferizer_fill_current_metas
#define ms_bufferizer_fill_current_metas(b,m) ms_bufferizer_fill_current_metas(b,m)
#else
#define ms_bufferizer_fill_current_metas(b,m)
#endif
#define BLOCKL_20MS 160
#define BLOCKL_30MS 240
#define BLOCKL_MAX 240
#define NO_OF_BYTES_20MS 38
#define NO_OF_BYTES_30MS 50
typedef struct EncState{
int nsamples;
int nbytes;
int ms_per_frame;
int ptime;
uint32_t ts;
MSBufferizer *bufferizer;
IlbcEncoderInstance *ilbc_enc;
}EncState;
static void enc_init(MSFilter *f){
EncState *s=ms_new0(EncState,1);
#ifdef USE_20MS_FRAMES
s->nsamples=BLOCKL_20MS;
s->nbytes=NO_OF_BYTES_20MS;
s->ms_per_frame=20;
#else
s->nsamples=BLOCKL_30MS;
s->nbytes=NO_OF_BYTES_30MS;
s->ms_per_frame=30;
#endif
s->ts = 0;
s->bufferizer=ms_bufferizer_new();
WebRtcIlbcfix_EncoderCreate(&s->ilbc_enc);
f->data=s;
}
static void enc_uninit(MSFilter *f){
EncState *s=(EncState*)f->data;
ms_bufferizer_destroy(s->bufferizer);
WebRtcIlbcfix_EncoderFree(s->ilbc_enc);
ms_free(f->data);
}
static void enc_preprocess(MSFilter *f){
EncState *s=(EncState*)f->data;
WebRtcIlbcfix_EncoderInit(s->ilbc_enc, s->ms_per_frame);
}
static int enc_add_fmtp(MSFilter *f, void *arg){
char buf[64];
const char *fmtp=(const char *)arg;
EncState *s=(EncState*)f->data;
memset(buf, '\0', sizeof(buf));
if (fmtp_get_value(fmtp, "mode", buf, sizeof(buf))){
if (buf[0]=='\0'){
ms_warning("unsupported fmtp parameter (%s)!", fmtp);
return 0;
}
ms_message("iLBC encoder got mode=%s",buf);
if (strstr(buf,"20")!=NULL){
s->nsamples=BLOCKL_20MS;
s->nbytes=NO_OF_BYTES_20MS;
s->ms_per_frame=20;
}else if (strstr(buf,"30")!=NULL){
s->nsamples=BLOCKL_30MS;
s->nbytes=NO_OF_BYTES_30MS;
s->ms_per_frame=30;
}
}
if (fmtp_get_value(fmtp,"ptime",buf,sizeof(buf))){
int ptime;
if (buf[0]=='\0'){
ms_warning("unsupported fmtp parameter (%s)!", fmtp);
return 0;
}
ms_message("iLBC encoder got ptime=%s",buf);
ptime=atoi(buf);
if (ptime>=20 && ptime<=140){
s->ptime=ptime;
}
}
return 0;
}
static int enc_add_attr(MSFilter *f, void *arg){
const char *fmtp=(const char *)arg;
EncState *s=(EncState*)f->data;
if (strstr(fmtp,"ptime:20")!=NULL){
s->ptime=20;
}else if (strstr(fmtp,"ptime:30")!=NULL){
s->ptime=30;
}else if (strstr(fmtp,"ptime:40")!=NULL){
s->ptime=40;
}else if (strstr(fmtp,"ptime:60")!=NULL){
s->ptime=60;
}else if (strstr(fmtp,"ptime:80")!=NULL){
s->ptime=80;
}else if (strstr(fmtp,"ptime:90")!=NULL){
s->ptime=90;
}else if (strstr(fmtp,"ptime:100")!=NULL){
s->ptime=100;
}else if (strstr(fmtp,"ptime:120")!=NULL){
s->ptime=120;
}else if (strstr(fmtp,"ptime:140")!=NULL){
s->ptime=140;
}
return 0;
}
static void enc_process(MSFilter *f){
EncState *s=(EncState*)f->data;
mblk_t *im,*om;
int size=s->nsamples*2;
int16_t samples[BLOCKL_MAX * 7]; /* BLOCKL_MAX * 7 is the largest size for ptime == 140 */
int16_t *samples_ptr;
int frame_per_packet=1;
int k;
if (s->ptime>=20 && s->ms_per_frame>0 && s->ptime%s->ms_per_frame==0)
{
frame_per_packet = s->ptime/s->ms_per_frame;
}
if (frame_per_packet<=0)
frame_per_packet=1;
if (frame_per_packet>7) /* 7*20 == 140 ms max */
frame_per_packet=7;
while((im=ms_queue_get(f->inputs[0]))!=NULL){
ms_bufferizer_put(s->bufferizer,im);
}
while(ms_bufferizer_read(s->bufferizer,(uint8_t*)samples,size*frame_per_packet)==(size*frame_per_packet)){
om=allocb(s->nbytes*frame_per_packet,0);
for (k=0;k<frame_per_packet;k++) {
samples_ptr = samples + k * s->nsamples;
WebRtcIlbcfix_Encode(s->ilbc_enc, samples_ptr, s->nsamples, om->b_wptr);
om->b_wptr+=s->nbytes;
}
s->ts+=s->nsamples*frame_per_packet;
mblk_set_timestamp_info(om,s->ts);
ms_bufferizer_fill_current_metas(s->bufferizer,om);
ms_queue_put(f->outputs[0],om);
}
}
static MSFilterMethod enc_methods[]={
{ MS_FILTER_ADD_FMTP, enc_add_fmtp },
{ MS_FILTER_ADD_ATTR, enc_add_attr},
{ 0 , NULL }
};
#ifdef _MSC_VER
MSFilterDesc ms_webrtc_ilbc_enc_desc={
MS_FILTER_PLUGIN_ID,
"MSWebRtcIlbcEnc",
"WebRtc's iLBC encoder",
MS_FILTER_ENCODER,
"iLBC",
1,
1,
enc_init,
enc_preprocess,
enc_process,
NULL,
enc_uninit,
enc_methods
};
#else
MSFilterDesc ms_webrtc_ilbc_enc_desc={
.id=MS_FILTER_PLUGIN_ID,
.name="MSWebRtcIlbcEnc",
.text="WebRtc's iLBC encoder",
.category=MS_FILTER_ENCODER,
.enc_fmt="iLBC",
.ninputs=1,
.noutputs=1,
.init=enc_init,
.preprocess=enc_preprocess,
.process=enc_process,
.uninit=enc_uninit,
.methods=enc_methods
};
#endif
typedef struct DecState{
int nsamples;
int nbytes;
int ms_per_frame;
IlbcDecoderInstance *ilbc_dec;
bool_t ready;
MSConcealerContext *plcctx;
}DecState;
static void dec_init(MSFilter *f){
DecState *s=ms_new0(DecState,1);
s->nsamples=0;
s->nbytes=0;
s->ms_per_frame=0;
WebRtcIlbcfix_DecoderCreate(&s->ilbc_dec);
s->ready=FALSE;
s->plcctx=ms_concealer_context_new(200);
f->data=s;
}
static void dec_uninit(MSFilter *f){
DecState *s=(DecState*)f->data;
WebRtcIlbcfix_DecoderFree(s->ilbc_dec);
if (s->plcctx) ms_concealer_context_destroy(s->plcctx);
ms_free(s);
}
static void dec_process(MSFilter *f){
DecState *s=(DecState*)f->data;
mblk_t *im,*om;
int nbytes;
while ((im=ms_queue_get(f->inputs[0]))!=NULL){
nbytes=msgdsize(im);
if (nbytes==0 || (nbytes%NO_OF_BYTES_20MS!=0 && nbytes%NO_OF_BYTES_30MS!=0)){
freemsg(im);
continue;
}
if (nbytes%NO_OF_BYTES_20MS==0 && s->nbytes!=NO_OF_BYTES_20MS) {
/* not yet configured, or misconfigured */
s->ms_per_frame=20;
s->nbytes=NO_OF_BYTES_20MS;
s->nsamples=BLOCKL_20MS;
s->ready=TRUE;
WebRtcIlbcfix_DecoderInit(s->ilbc_dec, s->ms_per_frame);
}
else if (nbytes%NO_OF_BYTES_30MS==0 && s->nbytes!=NO_OF_BYTES_30MS) {
/* not yet configured, or misconfigured */
s->ms_per_frame=30;
s->nbytes=NO_OF_BYTES_30MS;
s->nsamples=BLOCKL_30MS;
s->ready=TRUE;
WebRtcIlbcfix_DecoderInit(s->ilbc_dec, s->ms_per_frame);
}
if (s->nbytes>0 && nbytes>=s->nbytes){
int frame_per_packet = nbytes/s->nbytes;
int k;
int plctime;
for (k=0;k<frame_per_packet;k++) {
int16_t speech_type;
om=allocb(s->nsamples*2,0);
WebRtcIlbcfix_Decode(s->ilbc_dec, im->b_rptr+k*s->nbytes, s->nbytes, (int16_t *)om->b_wptr, &speech_type);
om->b_wptr += s->nsamples * 2;
mblk_meta_copy(im,om);
ms_queue_put(f->outputs[0],om);
}
if (s->plcctx){
plctime=ms_concealer_inc_sample_time(s->plcctx,f->ticker->time,frame_per_packet*s->ms_per_frame,1);
if (plctime>0){
ms_warning("ilbc: did plc during %i ms",plctime);
}
}
}else{
ms_warning("bad iLBC frame !");
}
freemsg(im);
}
if (s->plcctx && s->ready && ms_concealer_context_is_concealement_required(s->plcctx,f->ticker->time)){
om=allocb(s->nsamples*2,0);
WebRtcIlbcfix_DecodePlc(s->ilbc_dec, (int16_t *)om->b_wptr, 1);
om->b_wptr += s->nsamples * 2;
mblk_set_plc_flag(om,TRUE);
ms_queue_put(f->outputs[0],om);
ms_concealer_inc_sample_time(s->plcctx,f->ticker->time,s->ms_per_frame,0);
}
}
static int dec_have_plc(MSFilter *f, void *arg){
DecState *s=(DecState*)f->data;
*((int *)arg) = (s->plcctx!=NULL);
return 0;
}
static MSFilterMethod dec_methods[]={
{ MS_DECODER_HAVE_PLC, dec_have_plc },
{ 0 , NULL }
};
#ifdef _MSC_VER
MSFilterDesc ms_webrtc_ilbc_dec_desc={
MS_FILTER_PLUGIN_ID,
"MSWebRtcIlbcDec",
"WebRtc's iLBC decoder",
MS_FILTER_DECODER,
"iLBC",
1,
1,
dec_init,
NULL,
dec_process,
NULL,
dec_uninit,
dec_methods,
MS_FILTER_IS_PUMP
};
#else
MSFilterDesc ms_webrtc_ilbc_dec_desc={
.id=MS_FILTER_PLUGIN_ID,
.name="MSWebRtcIlbcDec",
.text="WebRtc's iLBC decoder",
.category=MS_FILTER_DECODER,
.enc_fmt="iLBC",
.ninputs=1,
.noutputs=1,
.init=dec_init,
.process=dec_process,
.uninit=dec_uninit,
.methods=dec_methods,
.flags=MS_FILTER_IS_PUMP
};
#endif