12024
|
1 /*
|
|
2 The mediastreamer library aims at providing modular media processing and I/O
|
|
3 for linphone, but also for any telephony application.
|
|
4 Copyright (C) 2001 Simon MORLAT simon.morlat@linphone.org
|
|
5
|
|
6 This library is free software; you can redistribute it and/or
|
|
7 modify it under the terms of the GNU Lesser General Public
|
|
8 License as published by the Free Software Foundation; either
|
|
9 version 2.1 of the License, or (at your option) any later version.
|
|
10
|
|
11 This library is distributed in the hope that it will be useful,
|
|
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
14 Lesser General Public License for more details.
|
|
15
|
|
16 You should have received a copy of the GNU Lesser General Public
|
|
17 License along with this library; if not, write to the Free Software
|
|
18 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
|
|
19 */
|
|
20
|
|
21
|
|
22 #include "msspeexdec.h"
|
|
23
|
|
24 #ifdef HAVE_GLIB
|
|
25 #include <gmodule.h>
|
|
26 #endif
|
|
27
|
|
28 extern MSFilter * ms_speex_enc_new();
|
|
29
|
|
30 MSCodecInfo speex_info=
|
|
31 {
|
|
32 {
|
|
33 "Speex codec",
|
|
34 0,
|
|
35 MS_FILTER_AUDIO_CODEC,
|
|
36 ms_speex_dec_new,
|
|
37 "A high quality variable bit-rate codec from Jean Marc Valin and David Rowe."
|
|
38 },
|
|
39 ms_speex_enc_new,
|
|
40 ms_speex_dec_new,
|
|
41 0, /*frame size */
|
|
42 0,
|
|
43 8000, /*minimal bitrate */
|
|
44 -1, /* sampling frequency */
|
|
45 110, /* payload type */
|
|
46 "speex",
|
|
47 1,
|
|
48 1
|
|
49 };
|
|
50
|
|
51
|
|
52
|
|
53 void ms_speex_codec_init()
|
|
54 {
|
|
55
|
|
56 ms_filter_register(MS_FILTER_INFO(&speex_info));
|
|
57 //ms_filter_register(MS_FILTER_INFO(&speex_lbr_info));
|
|
58 }
|
|
59
|
|
60 #ifdef HAVE_GLIB
|
|
61 gchar * g_module_check_init(GModule *module)
|
|
62 {
|
|
63 ms_speex_codec_init();
|
|
64
|
|
65 return NULL;
|
|
66 }
|
|
67 #else
|
|
68 gchar * g_module_check_init()
|
|
69 {
|
|
70 ms_speex_codec_init();
|
|
71
|
|
72 return NULL;
|
|
73 }
|
|
74 #endif
|
|
75
|
|
76 static MSSpeexDecClass * ms_speex_dec_class=NULL;
|
|
77 //static MSSpeexDecClass * ms_speexnb_dec_class=NULL;
|
|
78
|
|
79 MSFilter * ms_speex_dec_new()
|
|
80 {
|
|
81 MSSpeexDec *obj=g_new(MSSpeexDec,1);
|
|
82
|
|
83 if (ms_speex_dec_class==NULL){
|
|
84 ms_speex_dec_class=g_new(MSSpeexDecClass,1);
|
|
85 ms_speex_dec_class_init(ms_speex_dec_class);
|
|
86 }
|
|
87 MS_FILTER(obj)->klass=MS_FILTER_CLASS(ms_speex_dec_class);
|
|
88
|
|
89 ms_speex_dec_init(obj);
|
|
90 return MS_FILTER(obj);
|
|
91 }
|
|
92
|
|
93 void ms_speex_dec_init(MSSpeexDec *obj)
|
|
94 {
|
|
95 ms_filter_init(MS_FILTER(obj));
|
|
96 obj->initialized=0;
|
|
97 MS_FILTER(obj)->outfifos=obj->outf;
|
|
98 MS_FILTER(obj)->inqueues=obj->inq;
|
|
99 obj->outf[0]=NULL;
|
|
100 obj->inq[0]=NULL;
|
|
101 obj->frequency=8000; /*default value */
|
|
102
|
|
103 }
|
|
104
|
|
105 void ms_speex_dec_init_core(MSSpeexDec *obj,const SpeexMode *mode)
|
|
106 {
|
|
107 int pf=1;
|
|
108
|
|
109 obj->speex_state=speex_decoder_init(mode);
|
|
110 speex_bits_init(&obj->bits);
|
|
111 /* enable the perceptual post filter */
|
|
112 speex_decoder_ctl(obj->speex_state,SPEEX_SET_PF, &pf);
|
|
113
|
|
114 speex_mode_query(mode, SPEEX_MODE_FRAME_SIZE, &obj->frame_size);
|
|
115
|
|
116 obj->initialized=1;
|
|
117 }
|
|
118
|
|
119 int ms_speex_dec_set_property(MSSpeexDec *obj, MSFilterProperty prop, int *value)
|
|
120 {
|
|
121 if (obj->initialized){
|
|
122 /* we are called when speex is running !! forbid that! */
|
|
123 ms_warning("ms_speex_dec_set_property: cannot call this function when running!");
|
|
124 return -1;
|
|
125 }
|
|
126 switch(prop){
|
|
127 case MS_FILTER_PROPERTY_FREQ:
|
|
128 obj->frequency=value[0];
|
|
129 break;
|
|
130 }
|
|
131 return 0;
|
|
132 }
|
|
133
|
|
134 void ms_speex_dec_setup(MSSpeexDec *obj)
|
|
135 {
|
|
136 const SpeexMode *mode;
|
|
137 g_message("Speex decoder setup: freq=%i",obj->frequency);
|
|
138 if ( obj->frequency< 16000) mode=&speex_nb_mode;
|
|
139 else mode=&speex_wb_mode;
|
|
140 ms_speex_dec_init_core(obj,mode);
|
|
141 }
|
|
142
|
|
143 void ms_speex_dec_unsetup(MSSpeexDec *obj)
|
|
144 {
|
|
145 ms_speex_dec_uninit_core(obj);
|
|
146 }
|
|
147
|
|
148 void ms_speex_dec_class_init(MSSpeexDecClass *klass)
|
|
149 {
|
|
150 gint frame_size=0;
|
|
151
|
|
152 ms_filter_class_init(MS_FILTER_CLASS(klass));
|
|
153 /* use the largest frame size to configure fifos */
|
|
154 speex_mode_query(&speex_wb_mode, SPEEX_MODE_FRAME_SIZE, &frame_size);
|
|
155 MS_FILTER_CLASS(klass)->process=(MSFilterProcessFunc)ms_speex_dec_process;
|
|
156 MS_FILTER_CLASS(klass)->setup=(MSFilterSetupFunc)ms_speex_dec_setup;
|
|
157 MS_FILTER_CLASS(klass)->unsetup=(MSFilterSetupFunc)ms_speex_dec_unsetup;
|
|
158 MS_FILTER_CLASS(klass)->destroy=(MSFilterDestroyFunc)ms_speex_dec_destroy;
|
|
159 MS_FILTER_CLASS(klass)->set_property=(MSFilterPropertyFunc)ms_speex_dec_set_property;
|
|
160 ms_filter_class_set_name(MS_FILTER_CLASS(klass),"SpeexDecoder");
|
|
161 MS_FILTER_CLASS(klass)->info=(MSFilterInfo*)&speex_info;
|
|
162 MS_FILTER_CLASS(klass)->max_foutputs=1;
|
|
163 MS_FILTER_CLASS(klass)->max_qinputs=1;
|
|
164 MS_FILTER_CLASS(klass)->w_maxgran=frame_size*2;
|
|
165 ms_trace("ms_speex_dec_class_init: w_maxgran is %i.",MS_FILTER_CLASS(klass)->w_maxgran);
|
|
166 }
|
|
167
|
|
168 void ms_speex_dec_uninit_core(MSSpeexDec *obj)
|
|
169 {
|
|
170 speex_decoder_destroy(obj->speex_state);
|
|
171 obj->initialized=0;
|
|
172 }
|
|
173
|
|
174 void ms_speex_dec_uninit(MSSpeexDec *obj)
|
|
175 {
|
|
176
|
|
177 }
|
|
178
|
|
179 void ms_speex_dec_destroy(MSSpeexDec *obj)
|
|
180 {
|
|
181 ms_speex_dec_uninit(obj);
|
|
182 g_free(obj);
|
|
183 }
|
|
184
|
|
185 void ms_speex_dec_process(MSSpeexDec *obj)
|
|
186 {
|
|
187 MSFifo *outf=obj->outf[0];
|
|
188 MSQueue *inq=obj->inq[0];
|
|
189 gint16 *output;
|
|
190 gint gran=obj->frame_size*2;
|
|
191 gint i;
|
|
192 MSMessage *m;
|
|
193
|
|
194 g_return_if_fail(inq!=NULL);
|
|
195 g_return_if_fail(outf!=NULL);
|
|
196
|
|
197 m=ms_queue_get(inq);
|
|
198 g_return_if_fail(m!=NULL);
|
|
199 speex_bits_reset(&obj->bits);
|
|
200 ms_fifo_get_write_ptr(outf,gran,(void**)&output);
|
|
201 g_return_if_fail(output!=NULL);
|
|
202 if (m->data!=NULL){
|
|
203
|
|
204 speex_bits_read_from(&obj->bits,m->data,m->size);
|
|
205 /* decode */
|
|
206 speex_decode_int(obj->speex_state,&obj->bits,(short*)output);
|
|
207 }else{
|
|
208 /* we have a missing packet */
|
|
209 speex_decode_int(obj->speex_state,NULL,(short*)output);
|
|
210 }
|
|
211 ms_message_destroy(m);
|
|
212
|
|
213 }
|