summaryrefslogtreecommitdiffstats
path: root/kopete/protocols/jabber/jingle/libjingle/talk/third_party/mediastreamer/msspeexdec.c
blob: 3a94de0f4917a5908d16ec73e25cc86eeb832aab (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
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
/*
  The mediastreamer library aims at providing modular media processing and I/O
	for linphone, but also for any telephony application.
  Copyright (C) 2001  Simon MORLAT simon.morlat@linphone.org
  										
  This library is free software; you can redistribute it and/or
  modify it under the terms of the GNU Lesser General Public
  License as published by the Free Software Foundation; either
  version 2.1 of the License, or (at your option) any later version.

  This library 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
  Lesser General Public License for more details.

  You should have received a copy of the GNU Lesser General Public
  License along with this library; if not, write to the Free Software
  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02111-1307  USA
*/

#include <config.h>

#ifdef HAVE_SPEEX

#include "msspeexdec.h"

#ifdef HAVE_GLIB
#include <gmodule.h>
#endif

extern MSFilter * ms_speex_enc_new();

MSCodecInfo speex_info=
{
	{
		"Speex codec",
		0,
		MS_FILTER_AUDIO_CODEC,
		ms_speex_dec_new,
		"A high quality variable bit-rate codec from Jean Marc Valin and David Rowe."
	},
	ms_speex_enc_new,
	ms_speex_dec_new,
	0,		/*frame size */
	0,
	8000, /*minimal bitrate */
	-1,	/* sampling frequency */
	110,		/* payload type */
	"speex",
	1,
	1
};



void ms_speex_codec_init()
{

	ms_filter_register(MS_FILTER_INFO(&speex_info));
	/* //ms_filter_register(MS_FILTER_INFO(&speex_lbr_info)); */
}

#ifdef HAVE_GLIB
gchar * g_module_check_init(GModule *module)
{
	ms_speex_codec_init();
	
	return NULL;
}
#else
gchar * g_module_check_init()
{
	ms_speex_codec_init();
	
	return NULL;
}
#endif

static MSSpeexDecClass * ms_speex_dec_class=NULL;
/* //static MSSpeexDecClass * ms_speexnb_dec_class=NULL; */

MSFilter * ms_speex_dec_new()
{
	MSSpeexDec *obj=g_new(MSSpeexDec,1);
	
	if (ms_speex_dec_class==NULL){
		ms_speex_dec_class=g_new(MSSpeexDecClass,1);
		ms_speex_dec_class_init(ms_speex_dec_class);
	}
	MS_FILTER(obj)->klass=MS_FILTER_CLASS(ms_speex_dec_class);

	ms_speex_dec_init(obj);
	return MS_FILTER(obj);
}

void ms_speex_dec_init(MSSpeexDec *obj)
{
	ms_filter_init(MS_FILTER(obj));
	obj->initialized=0;
	MS_FILTER(obj)->outfifos=obj->outf;
	MS_FILTER(obj)->inqueues=obj->inq;
	obj->outf[0]=NULL;
	obj->inq[0]=NULL;
	obj->frequency=8000; /*default value */
	
}

void ms_speex_dec_init_core(MSSpeexDec *obj,const SpeexMode *mode)
{
	int pf=1;
	
	obj->speex_state=speex_decoder_init(mode);
	speex_bits_init(&obj->bits);
	/* enable the perceptual post filter */
	speex_decoder_ctl(obj->speex_state,SPEEX_SET_PF, &pf);
	
	speex_mode_query(mode, SPEEX_MODE_FRAME_SIZE, &obj->frame_size);
	
	obj->initialized=1;
}

int ms_speex_dec_set_property(MSSpeexDec *obj, MSFilterProperty prop, int *value)
{
	if (obj->initialized){
		/* we are called when speex is running !! forbid that! */
		ms_warning("ms_speex_dec_set_property: cannot call this function when running!");
		return -1;
	}
	switch(prop){
		case MS_FILTER_PROPERTY_FREQ:
			obj->frequency=value[0];
		break;
	}
	return 0;
}

void ms_speex_dec_setup(MSSpeexDec *obj)
{
	const SpeexMode *mode;
	g_message("Speex decoder setup: freq=%i",obj->frequency);
	if ( obj->frequency< 16000) mode=&speex_nb_mode;
	else mode=&speex_wb_mode;
	ms_speex_dec_init_core(obj,mode);
}

void ms_speex_dec_unsetup(MSSpeexDec *obj)
{
	ms_speex_dec_uninit_core(obj);
}

void ms_speex_dec_class_init(MSSpeexDecClass *klass)
{
	gint frame_size=0;
	
	ms_filter_class_init(MS_FILTER_CLASS(klass));
	/* use the largest frame size to configure fifos */
	speex_mode_query(&speex_wb_mode, SPEEX_MODE_FRAME_SIZE, &frame_size);
	MS_FILTER_CLASS(klass)->process=(MSFilterProcessFunc)ms_speex_dec_process;
	MS_FILTER_CLASS(klass)->setup=(MSFilterSetupFunc)ms_speex_dec_setup;
	MS_FILTER_CLASS(klass)->unsetup=(MSFilterSetupFunc)ms_speex_dec_unsetup;
	MS_FILTER_CLASS(klass)->destroy=(MSFilterDestroyFunc)ms_speex_dec_destroy;
	MS_FILTER_CLASS(klass)->set_property=(MSFilterPropertyFunc)ms_speex_dec_set_property;
	ms_filter_class_set_name(MS_FILTER_CLASS(klass),"SpeexDecoder");
	MS_FILTER_CLASS(klass)->info=(MSFilterInfo*)&speex_info;
	MS_FILTER_CLASS(klass)->max_foutputs=1;
	MS_FILTER_CLASS(klass)->max_qinputs=1;
	MS_FILTER_CLASS(klass)->w_maxgran=frame_size*2;
	ms_trace("ms_speex_dec_class_init: w_maxgran is %i.",MS_FILTER_CLASS(klass)->w_maxgran);
}

void ms_speex_dec_uninit_core(MSSpeexDec *obj)
{
	speex_decoder_destroy(obj->speex_state);
	obj->initialized=0;
}

void ms_speex_dec_uninit(MSSpeexDec *obj)
{
	
}

void ms_speex_dec_destroy(MSSpeexDec *obj)
{
	ms_speex_dec_uninit(obj);
	g_free(obj);
}

void ms_speex_dec_process(MSSpeexDec *obj)
{
	MSFifo *outf=obj->outf[0];
	MSQueue *inq=obj->inq[0];
	gint16 *output;
	gint gran=obj->frame_size*2;
	gint i;
	MSMessage *m;
	
	g_return_if_fail(inq!=NULL);
	g_return_if_fail(outf!=NULL);
	
	m=ms_queue_get(inq);
	g_return_if_fail(m!=NULL);
	speex_bits_reset(&obj->bits);
	ms_fifo_get_write_ptr(outf,gran,(void**)&output);
	g_return_if_fail(output!=NULL);
	if (m->data!=NULL){
		
		speex_bits_read_from(&obj->bits,m->data,m->size);
		/* decode */
		speex_decode_int(obj->speex_state,&obj->bits,(short*)output);
	}else{
		/* we have a missing packet */
		speex_decode_int(obj->speex_state,NULL,(short*)output);
	}
	ms_message_destroy(m);
	
}

#endif  /* HAVE_SPEEX */