msspeex.c 16.8 KB
Newer Older
aymeric's avatar
aymeric committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
/*
mediastreamer2 library - modular sound and video processing and streaming
Copyright (C) 2006  Simon MORLAT (simon.morlat@linphone.org)

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., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
*/

#include "mediastreamer2/msfilter.h"
21
#include "mediastreamer2/msticker.h"
aymeric's avatar
aymeric committed
22 23 24

#include <speex/speex.h>

25 26 27 28
#ifdef ANDROID
#include "cpu-features.h"
#endif

aymeric's avatar
aymeric committed
29 30 31 32
#ifdef WIN32
#include <malloc.h> /* for alloca */
#endif

aymeric's avatar
aymeric committed
33
typedef struct SpeexEncState{
aymeric's avatar
aymeric committed
34 35
	int rate;
	int bitrate;
36 37
	int maxbitrate; /*ip bitrate*/
	int ip_bitrate; /*effective ip bitrate */
aymeric's avatar
aymeric committed
38 39 40 41 42 43 44 45
	int ptime;
	int vbr;
	int cng;
	int mode;
	int frame_size;
	void *state;
	uint32_t ts;
	MSBufferizer *bufferizer;
aymeric's avatar
aymeric committed
46
} SpeexEncState;
aymeric's avatar
aymeric committed
47 48

static void enc_init(MSFilter *f){
49
	SpeexEncState *s=ms_new0(SpeexEncState,1);
50
#ifdef SPEEX_LIB_SET_CPU_FEATURES
51
	int cpuFeatures = 0;
52
#endif
aymeric's avatar
aymeric committed
53 54 55
	s->rate=8000;
	s->bitrate=-1;
	s->maxbitrate=-1;
56
	s->ip_bitrate=-1;
57
	s->ptime=20;
aymeric's avatar
aymeric committed
58 59
	s->mode=-1;
	s->vbr=0;
aymeric's avatar
aymeric committed
60 61 62 63 64 65
	s->cng=0;
	s->frame_size=0;
	s->state=0;
	s->ts=0;
	s->bufferizer=ms_bufferizer_new();
	f->data=s;
66 67 68

#ifdef SPEEX_LIB_SET_CPU_FEATURES
#ifdef __ARM_NEON__
69 70 71 72 73 74 75 76
	#ifdef ANDROID
	if (android_getCpuFamily() == ANDROID_CPU_FAMILY_ARM
		&& (android_getCpuFeatures() & ANDROID_CPU_ARM_FEATURE_NEON) != 0) {
		cpuFeatures = SPEEX_LIB_CPU_FEATURE_NEON;
	}
	#else
	cpuFeatures = SPEEX_LIB_CPU_FEATURE_NEON;
	#endif
77
#endif
78 79
	ms_message("speex_lib_ctl init with neon ? %d", (cpuFeatures == SPEEX_LIB_CPU_FEATURE_NEON));
	speex_lib_ctl(SPEEX_LIB_SET_CPU_FEATURES, &cpuFeatures);
80
#else
81
	ms_message("speex_lib_ctl does not support SPEEX_LIB_CPU_FEATURE_NEON");
82
#endif
aymeric's avatar
aymeric committed
83 84 85
}

static void enc_uninit(MSFilter *f){
aymeric's avatar
aymeric committed
86
	SpeexEncState *s=(SpeexEncState*)f->data;
aymeric's avatar
aymeric committed
87 88 89 90 91 92 93 94
	if (s==NULL)
		return;
	ms_bufferizer_destroy(s->bufferizer);
	if (s->state!=NULL)
		speex_encoder_destroy(s->state);
	ms_free(s);
}

95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111
static void apply_max_bitrate(SpeexEncState *s){
	int pps=1000/s->ptime;

	if (s->maxbitrate>0){
		/* convert from network bitrate to codec bitrate:*/
		/* ((nbr/(pps*8)) -20-12-8)*pps*8*/
		int cbr=(int)( ((((float)s->maxbitrate)/(pps*8))-20-12-8)*pps*8);
		ms_message("Setting maxbitrate=%i to speex encoder.",cbr);
		if (speex_encoder_ctl(s->state,SPEEX_SET_BITRATE,&cbr)!=0){
			ms_error("Could not set maxbitrate %i to speex encoder.",s->bitrate);
		}
	}
	if (speex_encoder_ctl(s->state,SPEEX_GET_BITRATE,&s->bitrate)!=0){
			ms_error("Could not get bitrate %i to speex encoder.",s->bitrate);
	}else{
		/*convert from codec bitrate to network bitrate */
		s->ip_bitrate=( (s->bitrate/(pps*8))+20+12+8)*8*pps;
112
		ms_message("Using bitrate %i for speex encoder, ip bitrate is %i",s->bitrate,s->ip_bitrate);
113 114 115
	}
}

aymeric's avatar
aymeric committed
116
static void enc_preprocess(MSFilter *f){
aymeric's avatar
aymeric committed
117
	SpeexEncState *s=(SpeexEncState*)f->data;
aymeric's avatar
aymeric committed
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
	const SpeexMode *mode=NULL;
	int _mode=0;

	switch(s->rate){
		case 8000:
	        _mode = SPEEX_MODEID_NB;    /* rate = 8000Hz */
			break;
		case 16000:
	        _mode = SPEEX_MODEID_WB;    /* rate = 16000Hz */
			break;
			/* should be supported in the future */
		case 32000:
	        _mode = SPEEX_MODEID_UWB;   /* rate = 32000Hz */
			break;
		default:
			ms_error("Unsupported rate for speex encoder (back to default rate=8000).");
			s->rate=8000;
	}
	/* warning: speex_lib_get_mode() is not available on speex<1.1.12 */
	mode = speex_lib_get_mode(_mode);

	if (mode==NULL)
		return;
	s->state=speex_encoder_init(mode);

	if (s->vbr==1)
	{
		if (speex_encoder_ctl(s->state,SPEEX_SET_VBR,&s->vbr)!=0){
			ms_error("Could not set vbr mode to speex encoder.");
		}
		/* implicit VAD */
		speex_encoder_ctl (s->state, SPEEX_SET_DTX, &s->vbr);
	}
	else if (s->vbr==2)
	{
		int vad=1;
		/* VAD */
		speex_encoder_ctl (s->state, SPEEX_SET_VAD, &vad);
aymeric's avatar
aymeric committed
156
		speex_encoder_ctl (s->state, SPEEX_SET_DTX, &vad);
aymeric's avatar
aymeric committed
157 158 159 160 161 162 163
	}
	else if (s->cng==1)
	{
		speex_encoder_ctl (s->state, SPEEX_SET_VAD, &s->cng);
	}

	if (s->rate==8000){
164 165 166 167 168 169 170 171 172 173 174 175
		//+------+---------------+-------------+
		//| mode | Speex quality |   bit-rate  |
		//+------+---------------+-------------+
		//|   1  |       0       | 2.15 kbit/s |
		//|   2  |       2       | 5.95 kbit/s |
		//|   3  |     3 or 4    | 8.00 kbit/s |
		//|   4  |     5 or 6    | 11.0 kbit/s |
		//|   5  |     7 or 8    | 15.0 kbit/s |
		//|   6  |       9       | 18.2 kbit/s |
		//|   7  |      10       | 24.6 kbit/s |
		//|   8  |       1       | 3.95 kbit/s |
		//+------+---------------+-------------+
aymeric's avatar
aymeric committed
176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198
		if (s->mode<=0 || s->mode>8)
			s->mode = 3; /* default mode */

		if (s->mode==1)
			s->bitrate = 2150;
		else if (s->mode==2)
			s->bitrate = 5950;
		else if (s->mode==3)
			s->bitrate = 8000;
		else if (s->mode==4)
			s->bitrate = 11000;
		else if (s->mode==5)
			s->bitrate = 15000;
		else if (s->mode==6)
			s->bitrate = 18200;
		else if (s->mode==7)
			s->bitrate = 24600;
		else if (s->mode==8)
			s->bitrate = 3950;

		if (s->bitrate!=-1){
			if (speex_encoder_ctl(s->state,SPEEX_SET_BITRATE,&s->bitrate)!=0){
				ms_error("Could not set bitrate %i to speex encoder.",s->bitrate);
aymeric's avatar
aymeric committed
199 200 201
			}
		}
	}
aymeric's avatar
aymeric committed
202
	else if (s->rate==16000 || s->rate==32000){
203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218
		//+------+---------------+-------------------+------------------------+
		//| mode | Speex quality | wideband bit-rate |     ultra wideband     |
		//|      |               |                   |        bit-rate        |
		//+------+---------------+-------------------+------------------------+
		//|   0  |       0       |    3.95 kbit/s    |       5.75 kbit/s      |
		//|   1  |       1       |    5.75 kbit/s    |       7.55 kbit/s      |
		//|   2  |       2       |    7.75 kbit/s    |       9.55 kbit/s      |
		//|   3  |       3       |    9.80 kbit/s    |       11.6 kbit/s      |
		//|   4  |       4       |    12.8 kbit/s    |       14.6 kbit/s      |
		//|   5  |       5       |    16.8 kbit/s    |       18.6 kbit/s      |
		//|   6  |       6       |    20.6 kbit/s    |       22.4 kbit/s      |
		//|   7  |       7       |    23.8 kbit/s    |       25.6 kbit/s      |
		//|   8  |       8       |    27.8 kbit/s    |       29.6 kbit/s      |
		//|   9  |       9       |    34.2 kbit/s    |       36.0 kbit/s      |
		//|  10  |       10      |    42.2 kbit/s    |       44.0 kbit/s      |
		//+------+---------------+-------------------+------------------------+
aymeric's avatar
aymeric committed
219 220 221 222 223 224
		int q=0;
		if (s->mode<0 || s->mode>10)
			s->mode = 8; /* default mode */
		q=s->mode;
		if (speex_encoder_ctl(s->state,SPEEX_SET_QUALITY,&q)!=0){
			ms_error("Could not set quality %i to speex encoder.",q);
aymeric's avatar
aymeric committed
225 226
		}
	}
227
	apply_max_bitrate(s);
aymeric's avatar
aymeric committed
228 229 230 231 232 233


	speex_mode_query(mode,SPEEX_MODE_FRAME_SIZE,&s->frame_size);
}

static void enc_process(MSFilter *f){
aymeric's avatar
aymeric committed
234
	SpeexEncState *s=(SpeexEncState*)f->data;
aymeric's avatar
aymeric committed
235 236 237 238 239 240 241 242
	mblk_t *im;
	int nbytes;
	uint8_t *buf;
	int frame_per_packet=1;

	if (s->frame_size<=0)
		return;

243 244
	ms_filter_lock(f);

aymeric's avatar
aymeric committed
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
	if (s->ptime>=20)
	{
		frame_per_packet = s->ptime/20;
	}

	if (frame_per_packet<=0)
		frame_per_packet=1;
	if (frame_per_packet>7) /* 7*20 == 140 ms max */
		frame_per_packet=7;

	nbytes=s->frame_size*2;
	buf=(uint8_t*)alloca(nbytes*frame_per_packet);

	while((im=ms_queue_get(f->inputs[0]))!=NULL){
		ms_bufferizer_put(s->bufferizer,im);
	}
	while(ms_bufferizer_read(s->bufferizer,buf,nbytes*frame_per_packet)==nbytes*frame_per_packet){
		mblk_t *om=allocb(nbytes*frame_per_packet,0);//too large...
		int k;
		SpeexBits bits;
		speex_bits_init(&bits);
		for (k=0;k<frame_per_packet;k++)
		{
			speex_encode_int(s->state,(int16_t*)(buf + (k*s->frame_size*2)),&bits);
			s->ts+=s->frame_size;
		}
		speex_bits_insert_terminator(&bits);
		k=speex_bits_write(&bits, (char*)om->b_wptr, nbytes*frame_per_packet);
		om->b_wptr+=k;

		mblk_set_timestamp_info(om,s->ts-s->frame_size);
		ms_queue_put(f->outputs[0],om);
		speex_bits_destroy(&bits);
	}
279
	ms_filter_unlock(f);
aymeric's avatar
aymeric committed
280 281 282
}

static void enc_postprocess(MSFilter *f){
aymeric's avatar
aymeric committed
283
	SpeexEncState *s=(SpeexEncState*)f->data;
aymeric's avatar
aymeric committed
284 285 286 287 288
	speex_encoder_destroy(s->state);
	s->state=NULL;
}

static int enc_set_sr(MSFilter *f, void *arg){
aymeric's avatar
aymeric committed
289
	SpeexEncState *s=(SpeexEncState*)f->data;
aymeric's avatar
aymeric committed
290 291 292 293
	s->rate=((int*)arg)[0];
	return 0;
}

294 295 296 297 298 299
static int enc_get_sr(MSFilter *f, void *arg){
	SpeexEncState *s=(SpeexEncState*)f->data;
	((int*)arg)[0]=s->rate;
	return 0;
}

aymeric's avatar
aymeric committed
300
static int enc_set_br(MSFilter *f, void *arg){
aymeric's avatar
aymeric committed
301
	SpeexEncState *s=(SpeexEncState*)f->data;
302
	ms_filter_lock(f);
aymeric's avatar
aymeric committed
303
	s->maxbitrate=((int*)arg)[0];
304 305 306 307 308 309 310 311
	if (s->state) apply_max_bitrate(s);
	ms_filter_unlock(f);
	return 0;
}

static int enc_get_br(MSFilter *f, void *arg){
	SpeexEncState *s=(SpeexEncState*)f->data;
	((int*)arg)[0]=s->ip_bitrate;
aymeric's avatar
aymeric committed
312 313 314
	return 0;
}

315 316 317 318 319
static int enc_set_ptime(MSFilter *f, void *arg){
	SpeexEncState *s=(SpeexEncState*)f->data;
	s->ptime=*(int*)arg;
	/*if the ptime is not a mulptiple of 20, go to the next multiple*/
	if (s->ptime%20)
320
		s->ptime = s->ptime - s->ptime%20 + 20;
321 322 323 324 325 326 327 328 329 330
	ms_message("MSSpeexEnc: got ptime=%i",s->ptime);
	return 0;
}

static int enc_get_ptime(MSFilter *f, void *arg){
	SpeexEncState *s=(SpeexEncState*)f->data;
	*(int*)arg=s->ptime;
	return 0;
}

aymeric's avatar
aymeric committed
331 332 333
static int enc_add_fmtp(MSFilter *f, void *arg){
	char buf[64];
	const char *fmtp=(const char *)arg;
aymeric's avatar
aymeric committed
334
	SpeexEncState *s=(SpeexEncState*)f->data;
aymeric's avatar
aymeric committed
335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357

	memset(buf, '\0', sizeof(buf));
	fmtp_get_value(fmtp, "vbr", buf, sizeof(buf));
	if (buf[0]=='\0'){
	}
	else if (strstr(buf,"off")!=NULL){
		s->vbr=0;
	}
	else if (strstr(buf,"on")!=NULL){
		s->vbr=1;
	}
	else if (strstr(buf,"vad")!=NULL){
		s->vbr=2;
	}

	memset(buf, '\0', sizeof(buf));
	fmtp_get_value(fmtp, "cng", buf, sizeof(buf));
	if (buf[0]=='\0'){
	}
	else if (strstr(buf,"off")!=NULL){
		s->cng=0;
	}
	else if (strstr(buf,"on")!=NULL){
aymeric's avatar
aymeric committed
358
		s->cng=1;
aymeric's avatar
aymeric committed
359 360 361 362
	}

	memset(buf, '\0', sizeof(buf));
	fmtp_get_value(fmtp, "mode", buf, sizeof(buf));
aymeric's avatar
aymeric committed
363 364 365 366
	if (buf[0]=='\0' || buf[1]=='\0'){
	}
	else if (buf[0]=='0' || (buf[0]=='"' && buf[1]=='0')){
		s->mode=0;
aymeric's avatar
aymeric committed
367
	}
aymeric's avatar
aymeric committed
368 369 370 371 372
	else if (buf[0]=='"' && atoi(buf+1)>=0){
		s->mode=atoi(buf+1);
	}
	else if (buf[0]!='"' && atoi(buf)>=0){
		s->mode=atoi(buf);
aymeric's avatar
aymeric committed
373 374
	}
	else {
Simon Morlat's avatar
Simon Morlat committed
375
		s->mode = -1; /* default mode */
aymeric's avatar
aymeric committed
376
	}
377 378
	memset(buf, '\0', sizeof(buf));
	if (fmtp_get_value(fmtp,"ptime",buf,sizeof(buf))){
379 380
		int val=atoi(buf);
		enc_set_ptime(f,&val);
381
	}
382

aymeric's avatar
aymeric committed
383 384 385 386 387
	return 0;
}

static int enc_add_attr(MSFilter *f, void *arg){
	const char *fmtp=(const char *)arg;
aymeric's avatar
aymeric committed
388
	SpeexEncState *s=(SpeexEncState*)f->data;
aymeric's avatar
aymeric committed
389 390 391 392 393
	if (strstr(fmtp,"ptime:10")!=NULL){
		s->ptime=20;
	}else if (strstr(fmtp,"ptime:20")!=NULL){
		s->ptime=20;
	}else if (strstr(fmtp,"ptime:30")!=NULL){
aymeric's avatar
aymeric committed
394
		s->ptime=40;
aymeric's avatar
aymeric committed
395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422
	}else if (strstr(fmtp,"ptime:40")!=NULL){
		s->ptime=40;
	}else if (strstr(fmtp,"ptime:50")!=NULL){
		s->ptime=60;
	}else if (strstr(fmtp,"ptime:60")!=NULL){
		s->ptime=60;
	}else if (strstr(fmtp,"ptime:70")!=NULL){
		s->ptime=80;
	}else if (strstr(fmtp,"ptime:80")!=NULL){
		s->ptime=80;
	}else if (strstr(fmtp,"ptime:90")!=NULL){
		s->ptime=100; /* not allowed */
	}else if (strstr(fmtp,"ptime:100")!=NULL){
		s->ptime=100;
	}else if (strstr(fmtp,"ptime:110")!=NULL){
		s->ptime=120;
	}else if (strstr(fmtp,"ptime:120")!=NULL){
		s->ptime=120;
	}else if (strstr(fmtp,"ptime:130")!=NULL){
		s->ptime=140;
	}else if (strstr(fmtp,"ptime:140")!=NULL){
		s->ptime=140;
	}
	return 0;
}

static MSFilterMethod enc_methods[]={
	{	MS_FILTER_SET_SAMPLE_RATE	,	enc_set_sr	},
423
	{	MS_FILTER_GET_SAMPLE_RATE	,	enc_get_sr	},
aymeric's avatar
aymeric committed
424
	{	MS_FILTER_SET_BITRATE		,	enc_set_br	},
425
	{	MS_FILTER_GET_BITRATE		,	enc_get_br	},
426 427 428 429
	{	MS_FILTER_ADD_FMTP		,	enc_add_fmtp 	},
	{	MS_FILTER_ADD_ATTR		,	enc_add_attr	},
	{	MS_AUDIO_ENCODER_SET_PTIME	,	enc_set_ptime	},
	{	MS_AUDIO_ENCODER_GET_PTIME	,	enc_get_ptime	},
aymeric's avatar
aymeric committed
430 431 432 433 434 435 436 437
	{	0				,	NULL		}
};

#ifdef _MSC_VER

MSFilterDesc ms_speex_enc_desc={
	MS_SPEEX_ENC_ID,
	"MSSpeexEnc",
438
	N_("The free and wonderful speex codec"),
aymeric's avatar
aymeric committed
439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455
	MS_FILTER_ENCODER,
	"speex",
	1,
	1,
	enc_init,
	enc_preprocess,
	enc_process,
	enc_postprocess,
	enc_uninit,
	enc_methods
};

#else

MSFilterDesc ms_speex_enc_desc={
	.id=MS_SPEEX_ENC_ID,
	.name="MSSpeexEnc",
456
	.text=N_("The free and wonderful speex codec"),
aymeric's avatar
aymeric committed
457 458 459 460 461 462 463 464 465 466 467 468 469 470
	.category=MS_FILTER_ENCODER,
	.enc_fmt="speex",
	.ninputs=1,
	.noutputs=1,
	.init=enc_init,
	.preprocess=enc_preprocess,
	.postprocess=enc_postprocess,
	.process=enc_process,
	.uninit=enc_uninit,
	.methods=enc_methods
};

#endif

471 472
static const int plc_max=10;/*10 frames of plc, no more*/

aymeric's avatar
aymeric committed
473 474 475 476
typedef struct DecState{
	int rate;
	int penh;
	int frsz;
477
	uint64_t sample_time;
aymeric's avatar
aymeric committed
478
	void *state;
479
	int plc_count;
Simon Morlat's avatar
Simon Morlat committed
480
	bool_t plc;
aymeric's avatar
aymeric committed
481 482 483
} DecState;

static void dec_init(MSFilter *f){
484
	DecState *s=ms_new0(DecState,1);
aymeric's avatar
aymeric committed
485 486 487 488
	s->rate=8000;
	s->frsz=0;
	s->state=NULL;
	s->penh=1;
489
	s->sample_time=0;
Simon Morlat's avatar
Simon Morlat committed
490
	s->plc=1;
491
	s->plc_count=0;
aymeric's avatar
aymeric committed
492 493 494 495 496
	f->data=s;
}

static void dec_uninit(MSFilter *f){
	DecState *s=(DecState*)f->data;
497
	if (s->state!=NULL)
aymeric's avatar
aymeric committed
498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526
		speex_decoder_destroy(s->state);
	ms_free(s);
}

static void dec_preprocess(MSFilter *f){
	DecState *s=(DecState*)f->data;
	const SpeexMode *mode=NULL;
	int modeid;
	switch(s->rate){
		case 8000:
	        modeid = SPEEX_MODEID_NB;    /* rate = 8000Hz */
			break;
		case 16000:
	        modeid = SPEEX_MODEID_WB;    /* rate = 16000Hz */
			break;
			/* should be supported in the future */
		case 32000:
	        modeid = SPEEX_MODEID_UWB;   /* rate = 32000Hz */
			break;
		default:
			ms_error("Unsupported rate for speex decoder (back to default rate=8000).");
			modeid=SPEEX_MODEID_NB;
	}
	/* warning: speex_lib_get_mode() is not available on speex<1.1.12 */
	mode = speex_lib_get_mode(modeid);
	s->state=speex_decoder_init(mode);
	speex_mode_query(mode,SPEEX_MODE_FRAME_SIZE,&s->frsz);
	if (s->penh==1)
		speex_decoder_ctl (s->state, SPEEX_SET_ENH, &s->penh);
527
	s->sample_time=0;
aymeric's avatar
aymeric committed
528 529 530 531 532 533 534 535
}

static void dec_postprocess(MSFilter *f){
	DecState *s=(DecState*)f->data;
	speex_decoder_destroy(s->state);
	s->state=NULL;
}

536 537 538 539 540 541
static int dec_get_sr(MSFilter *f, void *arg){
	DecState *s=(DecState*)f->data;
	((int*)arg)[0]=s->rate;
	return 0;
}

aymeric's avatar
aymeric committed
542 543 544 545 546 547
static int dec_set_sr(MSFilter *f, void *arg){
	DecState *s=(DecState*)f->data;
	s->rate=((int*)arg)[0];
	return 0;
}

548
static int dec_add_fmtp(MSFilter *f, void *arg){
Simon Morlat's avatar
Simon Morlat committed
549 550 551 552 553 554 555 556 557
	DecState *s=(DecState*)f->data;
	const char *fmtp=(const char *)arg;
	char buf[32];
	if (fmtp_get_value(fmtp, "plc", buf, sizeof(buf))){
		s->plc=atoi(buf);
	}
	return 0;
}

aymeric's avatar
aymeric committed
558 559 560 561
static void dec_process(MSFilter *f){
	DecState *s=(DecState*)f->data;
	mblk_t *im;
	mblk_t *om;
562
	int err=-2;
aymeric's avatar
aymeric committed
563 564
	SpeexBits bits;
	int bytes=s->frsz*2;
565
	bool_t bits_initd=FALSE;
566

aymeric's avatar
aymeric committed
567
	while((im=ms_queue_get(f->inputs[0]))!=NULL){
568
		int rem_bits=(im->b_wptr-im->b_rptr)*8;
569

570 571 572 573
		if (!bits_initd) {
			speex_bits_init(&bits);
			bits_initd=TRUE;
		}else speex_bits_reset(&bits);
574

aymeric's avatar
aymeric committed
575
		speex_bits_read_from(&bits,(char*)im->b_rptr,im->b_wptr-im->b_rptr);
576

577 578 579
		/* support for multiple frame  in one RTP packet */
 		do{
			om=allocb(bytes,0);
580
			mblk_meta_copy(im, om);
581
			err=speex_decode_int(s->state,&bits,(int16_t*)om->b_wptr);
582
			om->b_wptr+=bytes;
583

584 585
			if (err==0){
				ms_queue_put(f->outputs[0],om);
586
				if (s->sample_time==0) s->sample_time=f->ticker->time;
587
				s->sample_time+=20;
588
				if (s->plc_count>0){
589
					// ms_warning("Did speex packet loss concealment during %i ms",s->plc_count*20);
590 591
					s->plc_count=0;
				}
592

593 594 595 596 597 598 599 600
			}else {
				if (err==-1)
					ms_warning("speex end of stream");
				else if (err==-2)
					ms_warning("speex corrupted stream");
				freemsg(om);
			}
		}while((rem_bits= speex_bits_remaining(&bits))>10);
aymeric's avatar
aymeric committed
601 602
		freemsg(im);
	}
603
	if (s->plc && s->sample_time!=0 && f->ticker->time>=s->sample_time){
604 605 606 607 608
		/* we should output a frame but no packet were decoded
		 thus do packet loss concealment*/
		om=allocb(bytes,0);
		err=speex_decode_int(s->state,NULL,(int16_t*)om->b_wptr);
		om->b_wptr+=bytes;
609
		mblk_set_plc_flag(om, 1);
610
		ms_queue_put(f->outputs[0],om);
611

612
		s->sample_time+=20;
613 614 615 616
		s->plc_count++;
		if (s->plc_count>=plc_max){
			s->sample_time=0;
		}
617 618
	}
	if (bits_initd) speex_bits_destroy(&bits);
aymeric's avatar
aymeric committed
619 620
}

Yann Diorcet's avatar
Yann Diorcet committed
621 622 623 624 625 626
static int dec_have_plc(MSFilter *f, void *arg)
{
	*((int *)arg) = 1;
	return 0;
}

aymeric's avatar
aymeric committed
627 628
static MSFilterMethod dec_methods[]={
	{	MS_FILTER_SET_SAMPLE_RATE	,	dec_set_sr	},
629
	{	MS_FILTER_GET_SAMPLE_RATE	,	dec_get_sr	},
Yann Diorcet's avatar
Yann Diorcet committed
630 631
	{	MS_FILTER_ADD_FMTP		,	dec_add_fmtp	},
	{ 	MS_DECODER_HAVE_PLC		, 	dec_have_plc	},
aymeric's avatar
aymeric committed
632 633 634 635 636 637 638 639
	{	0				,	NULL		}
};

#ifdef _MSC_VER

MSFilterDesc ms_speex_dec_desc={
	MS_SPEEX_DEC_ID,
	"MSSpeexDec",
640
	N_("The free and wonderful speex codec"),
aymeric's avatar
aymeric committed
641 642 643 644 645 646 647 648 649
	MS_FILTER_DECODER,
	"speex",
	1,
	1,
	dec_init,
	dec_preprocess,
	dec_process,
	dec_postprocess,
	dec_uninit,
650 651
	dec_methods,
	MS_FILTER_IS_PUMP
aymeric's avatar
aymeric committed
652 653 654 655 656 657 658
};

#else

MSFilterDesc ms_speex_dec_desc={
	.id=MS_SPEEX_DEC_ID,
	.name="MSSpeexDec",
659
	.text=N_("The free and wonderful speex codec"),
aymeric's avatar
aymeric committed
660 661 662 663 664 665 666 667 668
	.category=MS_FILTER_DECODER,
	.enc_fmt="speex",
	.ninputs=1,
	.noutputs=1,
	.init=dec_init,
	.preprocess=dec_preprocess,
	.postprocess=dec_postprocess,
	.process=dec_process,
	.uninit=dec_uninit,
669 670
	.methods=dec_methods,
	.flags=MS_FILTER_IS_PUMP
aymeric's avatar
aymeric committed
671 672 673 674 675 676
};

#endif

MS_FILTER_DESC_EXPORT(ms_speex_dec_desc)
MS_FILTER_DESC_EXPORT(ms_speex_enc_desc)
677