Fixed DevStudio 2005 build.
[opal.git] / plugins / audio / iLBC / ilbccodec.c
bloba70c178fed55c23857f7dfedfd5b52f3fe85c461
1 /*
2 * iLBC Plugin codec for OpenH323/OPAL
4 * Copyright (C) 2004 Post Increment, All Rights Reserved
6 * The contents of this file are subject to the Mozilla Public License
7 * Version 1.0 (the "License"); you may not use this file except in
8 * compliance with the License. You may obtain a copy of the License at
9 * http://www.mozilla.org/MPL/
11 * Software distributed under the License is distributed on an "AS IS"
12 * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See
13 * the License for the specific language governing rights and limitations
14 * under the License.
16 * The Original Code is Open H323 Library.
18 * The Initial Developer of the Original Code is Post Increment
20 * Contributor(s): ______________________________________.
22 * $Log$
23 * Revision 1.5 2007/09/05 07:49:07 csoutheren
24 * Set output buffers correctly and check input buffers correctly
26 * Revision 1.4 2007/09/05 06:22:32 rjongbloed
27 * Update iLBC plug in to latest API so get FMTP parameter for mode.
29 * Revision 1.3 2006/08/28 01:21:54 csoutheren
30 * Disable 15k for SIP
32 * Revision 1.2 2006/07/31 09:09:19 csoutheren
33 * Checkin of validated codec used during development
35 * Revision 1.1.2.2 2006/04/08 06:09:09 rjongbloed
36 * Fix correct directory for OPAL headers
38 * Revision 1.1.2.1 2006/04/06 01:20:05 csoutheren
39 * Ported audio codec plugins from OpenH323 to OPAL
41 * Revision 1.8 2005/07/15 10:09:00 rogerhardiman
42 * Fix SF bug 1237507. Windows uses malloc.h. Linux and FreeBSD uses stdlib.h
43 * Wrap #include with _WIN32 to be consistent with malloc.h in pwlib.
45 * Revision 1.7 2004/12/20 23:18:01 csoutheren
46 * Added stdlib.h to all plugins to keep FreeBSD happy
47 * Thanks to Kevin Oberman
49 * Revision 1.6 2004/11/29 06:29:58 csoutheren
50 * Added flag to reuse RTP payload types rather than allocaing new ones for each codec
51 * variant
53 * Revision 1.5 2004/06/17 22:04:57 csoutheren
54 * Changed codec version number to be sensible rather than string $Ver$
56 * Revision 1.4 2004/04/09 12:24:19 csoutheren
57 * Renamed h323plugin.h to opalplugin.h, and modified everything else
58 * as required
60 * Revision 1.3 2004/04/04 12:43:59 csoutheren
61 * Added file headers and fixd formatting
65 #include <codec/opalplugin.h>
67 #include <stdlib.h>
68 #ifdef _WIN32
69 #define _CRT_SECURE_NO_DEPRECATE
70 #include <malloc.h>
71 #define STRCMPI _strcmpi
72 #else
73 #define STRCMPI strcasecmp
74 #endif
76 #include "iLBC/iLBC_encode.h"
77 #include "iLBC/iLBC_decode.h"
78 #include "iLBC/iLBC_define.h"
80 #define SPEED_30MS NO_OF_BYTES_30MS*8*8000/BLOCKL_30MS
81 #define SPEED_20MS NO_OF_BYTES_20MS*8*8000/BLOCKL_20MS
84 /////////////////////////////////////////////////////////////////////////////
86 static void * create_encoder(const struct PluginCodec_Definition * codec)
88 struct iLBC_Enc_Inst_t_ * context = (struct iLBC_Enc_Inst_t_ *)malloc((unsigned)sizeof(struct iLBC_Enc_Inst_t_));
89 initEncode(context, (codec->bitsPerSec) == SPEED_30MS ? 30 : 20);
90 return context;
93 static void * create_decoder(const struct PluginCodec_Definition * codec)
95 struct iLBC_Dec_Inst_t_ * context = (struct iLBC_Dec_Inst_t_ *)malloc((unsigned)sizeof(struct iLBC_Dec_Inst_t_));
96 initDecode(context, (codec->bitsPerSec) == SPEED_30MS ? 30 : 20, 0);
97 return context;
100 static void destroy_context(const struct PluginCodec_Definition * codec, void * context)
102 free(context);
105 static int codec_encoder(const struct PluginCodec_Definition * codec,
106 void * context,
107 const void * from,
108 unsigned * fromLen,
109 void * to,
110 unsigned * toLen,
111 unsigned int * flag)
113 float block[BLOCKL_MAX];
114 int i;
116 struct iLBC_Enc_Inst_t_ * encoder = (struct iLBC_Enc_Inst_t_ *)context;
117 const short * sampleBuffer = (const short *)from;
119 if ((*fromLen)/2 < (unsigned)encoder->blockl)
120 return 0;
122 /* convert signal to float */
123 for (i = 0; i < encoder->blockl; i++)
124 block[i] = (float)sampleBuffer[i];
126 /* do the actual encoding */
127 iLBC_encode(to, block, encoder);
129 // set output length
130 *toLen = encoder->no_of_bytes;
132 return 1;
135 static int codec_decoder(const struct PluginCodec_Definition * codec,
136 void * context,
137 const void * from,
138 unsigned * fromLen,
139 void * to,
140 unsigned * toLen,
141 unsigned int * flag)
143 int i;
144 float block[BLOCKL_MAX];
146 struct iLBC_Dec_Inst_t_ * decoder = (struct iLBC_Dec_Inst_t_ *)context;
147 short * sampleBuffer = (short *)to;
149 if (*fromLen < (unsigned)decoder->no_of_bytes)
150 return 0;
152 /* do actual decoding of block */
153 iLBC_decode(block, (unsigned char *)from, decoder, 1);
155 if (*toLen < (unsigned)decoder->blockl*2)
156 return 0;
158 /* convert to short */
159 for (i = 0; i < decoder->blockl; i++) {
160 float tmp = block[i];
161 if (tmp < MIN_SAMPLE)
162 tmp = MIN_SAMPLE;
163 else if (tmp > MAX_SAMPLE)
164 tmp = MAX_SAMPLE;
165 sampleBuffer[i] = (short)tmp;
168 *toLen = decoder->blockl*2;
170 return 1;
173 static int valid_for_h323(
174 const struct PluginCodec_Definition * codec,
175 void * context ,
176 const char * key,
177 void * parm ,
178 unsigned * parmLen)
180 if (parmLen == NULL || parm == NULL || *parmLen != sizeof(char *))
181 return 0;
183 return (STRCMPI((const char *)parm, "h.323") == 0 ||
184 STRCMPI((const char *)parm, "h323") == 0) ? 1 : 0;
188 static int valid_for_sip_or_h323(
189 const struct PluginCodec_Definition * codec,
190 void * context ,
191 const char * key,
192 void * parm ,
193 unsigned * parmLen)
195 if (parmLen == NULL || parm == NULL || *parmLen != sizeof(char *))
196 return 0;
198 return (STRCMPI((const char *)parm, "sip") == 0 ||
199 STRCMPI((const char *)parm, "h.323") == 0 ||
200 STRCMPI((const char *)parm, "h323") == 0) ? 1 : 0;
203 static struct PluginCodec_Option const PreferredMode =
204 { PluginCodec_IntegerOption, "Preferred Mode", 0, PluginCodec_NoMerge, "30", "mode", NULL, 0, "0", "30" };
206 static struct PluginCodec_Option const * const OptionTable[] = {
207 &PreferredMode,
208 NULL
211 static int get_codec_options(const struct PluginCodec_Definition * defn,
212 void * context,
213 const char * name,
214 void * parm,
215 unsigned * parmLen)
217 if (parm == NULL || parmLen == NULL || *parmLen != sizeof(struct PluginCodec_Option **))
218 return 0;
220 *(struct PluginCodec_Option const * const * *)parm = OptionTable;
221 return 1;
225 static int set_codec_options(const struct PluginCodec_Definition * defn,
226 void * context,
227 const char * name,
228 void * parm,
229 unsigned * parmLen)
231 const char * const * option;
232 struct iLBC_Enc_Inst_t_ * encoder;
234 if (context == NULL || parm == NULL || parmLen == NULL || *parmLen != sizeof(const char **))
235 return 0;
237 encoder = (struct iLBC_Enc_Inst_t_ *)context;
239 for (option = (const char * const *)parm; *option != NULL; option += 2) {
240 if (STRCMPI(option[0], "Preferred Mode") == 0)
241 initEncode(context, atoi(option[1]) == SPEED_30MS ? 30 : 20);
244 return 1;
248 static struct PluginCodec_ControlDefn h323CoderControls[] = {
249 { "valid_for_protocol", valid_for_h323 },
250 { "get_codec_options", get_codec_options },
251 { "set_codec_options", set_codec_options },
252 { NULL }
255 static struct PluginCodec_ControlDefn h323AndSIPCoderControls[] = {
256 { "valid_for_protocol", valid_for_sip_or_h323 },
257 { "get_codec_options", get_codec_options },
258 { "set_codec_options", set_codec_options },
259 { NULL }
263 /////////////////////////////////////////////////////////////////////////////
265 static struct PluginCodec_information licenseInfo = {
266 //1073187324, // timestamp = Sun 04 Jan 2004 03:35:24 AM UTC =
267 1101695533, // Mon 29 Nov 2004 12:32:13 PM EST
269 "Craig Southeren, Post Increment", // source code author
270 "1.1", // source code version
271 "craigs@postincrement.com", // source code email
272 "http://www.postincrement.com", // source code URL
273 "Copyright (C) 2004 by Post Increment, All Rights Reserved", // source code copyright
274 "MPL 1.0", // source code license
275 PluginCodec_License_MPL, // source code license
277 "iLBC (internet Low Bitrate Codec)", // codec description
278 "Global IP Sound, Inc.", // codec author
279 NULL, // codec version
280 "info@globalipsound.com", // codec email
281 "http://www.ilbcfreeware.org", // codec URL
282 "Global IP Sound AB. Portions Copyright (C) 1999-2002, All Rights Reserved", // codec copyright information
283 "Global IP Sound iLBC Freeware Public License, IETF Version, Limited Commercial Use", // codec license
284 PluginCodec_License_Freeware // codec license code
287 static const char L16Desc[] = { "L16" };
289 static const char iLBC13k3[] = { "iLBC-13k3" };
290 static const char iLBC15k2[] = { "iLBC-15k2" };
292 static const char sdpILBC[] = { "iLBC" };
294 #define EQUIVALENCE_COUNTRY_CODE 9
295 #define EQUIVALENCE_EXTENSION_CODE 0
296 #define EQUIVALENCE_MANUFACTURER_CODE 61
298 static struct PluginCodec_H323NonStandardCodecData ilbc13k3Cap =
300 NULL,
301 EQUIVALENCE_COUNTRY_CODE,
302 EQUIVALENCE_EXTENSION_CODE,
303 EQUIVALENCE_MANUFACTURER_CODE,
304 iLBC13k3, sizeof(iLBC13k3)-1,
305 NULL
308 static struct PluginCodec_H323NonStandardCodecData ilbc15k2Cap =
310 NULL,
311 EQUIVALENCE_COUNTRY_CODE,
312 EQUIVALENCE_EXTENSION_CODE,
313 EQUIVALENCE_MANUFACTURER_CODE,
314 iLBC15k2, sizeof(iLBC15k2)-1,
315 NULL
318 static struct PluginCodec_Definition iLBCCodecDefn[4] = {
321 // encoder
322 PLUGIN_CODEC_VERSION_OPTIONS, // codec API version
323 &licenseInfo, // license information
325 PluginCodec_MediaTypeAudio | // audio codec
326 PluginCodec_InputTypeRaw | // raw input data
327 PluginCodec_OutputTypeRaw | // raw output data
328 PluginCodec_RTPTypeShared | // share RTP code
329 PluginCodec_RTPTypeDynamic, // dynamic RTP type
331 iLBC13k3, // text decription
332 L16Desc, // source format
333 iLBC13k3, // destination format
335 (void *)NULL, // user data
337 8000, // samples per second
338 SPEED_30MS, // raw bits per second
339 30000, // nanoseconds per frame
340 BLOCKL_30MS, // samples per frame
341 NO_OF_BYTES_30MS, // bytes per frame
342 1, // recommended number of frames per packet
343 1, // maximum number of frames per packe
344 0, // IANA RTP payload code
345 sdpILBC, // RTP payload name
347 create_encoder, // create codec function
348 destroy_context, // destroy codec
349 codec_encoder, // encode/decode
350 h323AndSIPCoderControls, // codec controls
352 PluginCodec_H323Codec_nonStandard, // h323CapabilityType
353 &ilbc13k3Cap // h323CapabilityData
357 // decoder
358 PLUGIN_CODEC_VERSION_OPTIONS, // codec API version
359 &licenseInfo, // license information
361 PluginCodec_MediaTypeAudio | // audio codec
362 PluginCodec_InputTypeRaw | // raw input data
363 PluginCodec_OutputTypeRaw | // raw output data
364 PluginCodec_RTPTypeShared | // share RTP code
365 PluginCodec_RTPTypeDynamic, // dynamic RTP type
367 iLBC13k3, // text decription
368 iLBC13k3, // source format
369 L16Desc, // destination format
371 (const void *)NULL, // user data
373 8000, // samples per second
374 SPEED_30MS, // raw bits per second
375 30000, // nanoseconds per frame
376 BLOCKL_30MS, // samples per frame
377 NO_OF_BYTES_30MS, // bytes per frame
378 1, // recommended number of frames per packet
379 1, // maximum number of frames per packe
380 0, // IANA RTP payload code
381 sdpILBC, // RTP payload name
383 create_decoder, // create codec function
384 destroy_context, // destroy codec
385 codec_decoder, // encode/decode
386 h323AndSIPCoderControls, // codec controls
388 PluginCodec_H323Codec_nonStandard, // h323CapabilityType
389 &ilbc13k3Cap // h323CapabilityData
393 // encoder
394 PLUGIN_CODEC_VERSION_OPTIONS, // codec API version
395 &licenseInfo, // license information
397 PluginCodec_MediaTypeAudio | // audio codec
398 PluginCodec_InputTypeRaw | // raw input data
399 PluginCodec_OutputTypeRaw | // raw output data
400 PluginCodec_RTPTypeShared | // share RTP code
401 PluginCodec_RTPTypeDynamic, // dynamic RTP type
403 iLBC15k2, // text decription
404 L16Desc, // source format
405 iLBC15k2, // destination format
407 (void *)NULL, // user data
409 8000, // samples per second
410 SPEED_20MS, // raw bits per second
411 20000, // nanoseconds per frame
412 BLOCKL_20MS, // samples per frame
413 NO_OF_BYTES_20MS, // bytes per frame
414 1, // recommended number of frames per packet
415 1, // maximum number of frames per packe
416 0, // IANA RTP payload code
417 sdpILBC, // RTP payload name
419 create_encoder, // create codec function
420 destroy_context, // destroy codec
421 codec_encoder, // encode/decode
422 h323CoderControls, // codec controls
424 PluginCodec_H323Codec_nonStandard, // h323CapabilityType
425 &ilbc15k2Cap // h323CapabilityData
429 // decoder
430 PLUGIN_CODEC_VERSION_OPTIONS, // codec API version
431 &licenseInfo, // license information
433 PluginCodec_MediaTypeAudio | // audio codec
434 PluginCodec_InputTypeRaw | // raw input data
435 PluginCodec_OutputTypeRaw | // raw output data
436 PluginCodec_RTPTypeShared | // share RTP code
437 PluginCodec_RTPTypeDynamic, // dynamic RTP type
439 iLBC15k2, // text decription
440 iLBC15k2, // source format
441 L16Desc, // destination format
443 (void *)NULL, // user data
445 8000, // samples per second
446 SPEED_20MS, // raw bits per second
447 20000, // nanoseconds per frame
448 BLOCKL_20MS, // samples per frame
449 NO_OF_BYTES_20MS, // bytes per frame
450 1, // recommended number of frames per packet
451 1, // maximum number of frames per packe
452 0, // IANA RTP payload code
453 sdpILBC, // RTP payload name
455 create_decoder, // create codec function
456 destroy_context, // destroy codec
457 codec_decoder, // encode/decode
458 h323CoderControls, // codec controls
460 PluginCodec_H323Codec_nonStandard, // h323CapabilityType
461 &ilbc15k2Cap // h323CapabilityData
466 /////////////////////////////////////////////////////////////////////////////
468 PLUGIN_CODEC_IMPLEMENT_ALL("iLBC", iLBCCodecDefn, PLUGIN_CODEC_VERSION_OPTIONS)