Checkin of validated codec used during development
[opal.git] / plugins / audio / iLBC / ilbccodec.c
blob29ee56a30296fee1780f361988cba6386408b05f
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.2 2006/07/31 09:09:19 csoutheren
24 * Checkin of validated codec used during development
26 * Revision 1.1.2.2 2006/04/08 06:09:09 rjongbloed
27 * Fix correct directory for OPAL headers
29 * Revision 1.1.2.1 2006/04/06 01:20:05 csoutheren
30 * Ported audio codec plugins from OpenH323 to OPAL
32 * Revision 1.8 2005/07/15 10:09:00 rogerhardiman
33 * Fix SF bug 1237507. Windows uses malloc.h. Linux and FreeBSD uses stdlib.h
34 * Wrap #include with _WIN32 to be consistent with malloc.h in pwlib.
36 * Revision 1.7 2004/12/20 23:18:01 csoutheren
37 * Added stdlib.h to all plugins to keep FreeBSD happy
38 * Thanks to Kevin Oberman
40 * Revision 1.6 2004/11/29 06:29:58 csoutheren
41 * Added flag to reuse RTP payload types rather than allocaing new ones for each codec
42 * variant
44 * Revision 1.5 2004/06/17 22:04:57 csoutheren
45 * Changed codec version number to be sensible rather than string $Ver$
47 * Revision 1.4 2004/04/09 12:24:19 csoutheren
48 * Renamed h323plugin.h to opalplugin.h, and modified everything else
49 * as required
51 * Revision 1.3 2004/04/04 12:43:59 csoutheren
52 * Added file headers and fixd formatting
56 #include <codec/opalplugin.h>
58 PLUGIN_CODEC_IMPLEMENT("iLBC")
60 #include <stdlib.h>
61 #ifdef _WIN32
62 #include <malloc.h>
63 #endif
65 #include "iLBC/iLBC_encode.h"
66 #include "iLBC/iLBC_decode.h"
67 #include "iLBC/iLBC_define.h"
69 #define SPEED_30MS NO_OF_BYTES_30MS*8*8000/BLOCKL_30MS
70 #define SPEED_20MS NO_OF_BYTES_20MS*8*8000/BLOCKL_20MS
72 #define SAMPLES_PER_SEC_30MS 30
75 /////////////////////////////////////////////////////////////////////////////
77 static void * create_encoder(const struct PluginCodec_Definition * codec)
79 struct iLBC_Enc_Inst_t_ * context = (struct iLBC_Enc_Inst_t_ *)malloc((unsigned)sizeof(struct iLBC_Enc_Inst_t_));
80 initEncode(context, (codec->bitsPerSec) == SPEED_30MS ? 30 : 20);
81 return context;
84 static void * create_decoder(const struct PluginCodec_Definition * codec)
86 struct iLBC_Dec_Inst_t_ * context = (struct iLBC_Dec_Inst_t_ *)malloc((unsigned)sizeof(struct iLBC_Dec_Inst_t_));
87 initDecode(context, (codec->bitsPerSec) == SPEED_30MS ? 30 : 20, 0);
88 return context;
91 static void destroy_context(const struct PluginCodec_Definition * codec, void * context)
93 free(context);
96 static int codec_encoder(const struct PluginCodec_Definition * codec,
97 void * context,
98 const void * from,
99 unsigned * fromLen,
100 void * to,
101 unsigned * toLen,
102 unsigned int * flag)
104 float block[BLOCKL_MAX];
105 int i;
107 struct iLBC_Enc_Inst_t_ * encoder = (struct iLBC_Enc_Inst_t_ *)context;
108 const short * sampleBuffer = (const short *)from;
110 if ((*fromLen)/2 != (unsigned)encoder->blockl)
111 return 0;
113 /* convert signal to float */
114 for (i = 0; i < encoder->blockl; i++)
115 block[i] = (float)sampleBuffer[i];
117 /* do the actual encoding */
118 iLBC_encode(to, block, encoder);
119 *toLen = encoder->no_of_bytes;
121 return 1;
124 static int codec_decoder(const struct PluginCodec_Definition * codec,
125 void * context,
126 const void * from,
127 unsigned * fromLen,
128 void * to,
129 unsigned * toLen,
130 unsigned int * flag)
132 int i;
133 float block[BLOCKL_MAX];
135 struct iLBC_Dec_Inst_t_ * decoder = (struct iLBC_Dec_Inst_t_ *)context;
136 short * sampleBuffer = (short *)to;
138 if (*fromLen < (unsigned)decoder->no_of_bytes)
139 return 0;
141 /* do actual decoding of block */
142 iLBC_decode(block, (unsigned char *)from, decoder, 1);
144 if (*toLen/2 != (unsigned)decoder->blockl)
145 return 0;
147 /* convert to short */
148 for (i = 0; i < decoder->blockl; i++) {
149 float tmp = block[i];
150 if (tmp < MIN_SAMPLE)
151 tmp = MIN_SAMPLE;
152 else if (tmp > MAX_SAMPLE)
153 tmp = MAX_SAMPLE;
154 sampleBuffer[i] = (short)tmp;
157 return 1;
160 /////////////////////////////////////////////////////////////////////////////
162 static struct PluginCodec_information licenseInfo = {
163 //1073187324, // timestamp = Sun 04 Jan 2004 03:35:24 AM UTC =
164 1101695533, // Mon 29 Nov 2004 12:32:13 PM EST
166 "Craig Southeren, Post Increment", // source code author
167 "1.1", // source code version
168 "craigs@postincrement.com", // source code email
169 "http://www.postincrement.com", // source code URL
170 "Copyright (C) 2004 by Post Increment, All Rights Reserved", // source code copyright
171 "MPL 1.0", // source code license
172 PluginCodec_License_MPL, // source code license
174 "iLBC (internet Low Bitrate Codec)", // codec description
175 "Global IP Sound, Inc.", // codec author
176 NULL, // codec version
177 "info@globalipsound.com", // codec email
178 "http://www.ilbcfreeware.org", // codec URL
179 "Global IP Sound AB. Portions Copyright (C) 1999-2002, All Rights Reserved", // codec copyright information
180 "Global IP Sound iLBC Freeware Public License, IETF Version, Limited Commercial Use", // codec license
181 PluginCodec_License_Freeware // codec license code
184 static const char L16Desc[] = { "L16" };
186 static const char iLBC13k3[] = { "iLBC-13k3" };
187 static const char iLBC15k2[] = { "iLBC-15k2" };
189 static const char sdpILBC[] = { "iLBC" };
191 #define EQUIVALENCE_COUNTRY_CODE 9
192 #define EQUIVALENCE_EXTENSION_CODE 0
193 #define EQUIVALENCE_MANUFACTURER_CODE 61
195 static struct PluginCodec_H323NonStandardCodecData ilbc13k3Cap =
197 NULL,
198 EQUIVALENCE_COUNTRY_CODE,
199 EQUIVALENCE_EXTENSION_CODE,
200 EQUIVALENCE_MANUFACTURER_CODE,
201 iLBC13k3, sizeof(iLBC13k3)-1,
202 NULL
205 static struct PluginCodec_H323NonStandardCodecData ilbc15k2Cap =
207 NULL,
208 EQUIVALENCE_COUNTRY_CODE,
209 EQUIVALENCE_EXTENSION_CODE,
210 EQUIVALENCE_MANUFACTURER_CODE,
211 iLBC15k2, sizeof(iLBC15k2)-1,
212 NULL
215 static struct PluginCodec_Definition iLBCCodecDefn[4] = {
218 // encoder
219 PLUGIN_CODEC_VERSION, // codec API version
220 &licenseInfo, // license information
222 PluginCodec_MediaTypeAudio | // audio codec
223 PluginCodec_InputTypeRaw | // raw input data
224 PluginCodec_OutputTypeRaw | // raw output data
225 PluginCodec_RTPTypeShared | // share RTP code
226 PluginCodec_RTPTypeDynamic, // dynamic RTP type
228 iLBC13k3, // text decription
229 L16Desc, // source format
230 iLBC13k3, // destination format
232 (void *)NULL, // user data
234 8000, // samples per second
235 SPEED_30MS, // raw bits per second
236 30000, // nanoseconds per frame
237 BLOCKL_30MS, // samples per frame
238 NO_OF_BYTES_30MS, // bytes per frame
239 1, // recommended number of frames per packet
240 1, // maximum number of frames per packe
241 0, // IANA RTP payload code
242 sdpILBC, // RTP payload name
244 create_encoder, // create codec function
245 destroy_context, // destroy codec
246 codec_encoder, // encode/decode
247 NULL, // codec controls
249 PluginCodec_H323Codec_nonStandard, // h323CapabilityType
250 &ilbc13k3Cap // h323CapabilityData
254 // decoder
255 PLUGIN_CODEC_VERSION, // codec API version
256 &licenseInfo, // license information
258 PluginCodec_MediaTypeAudio | // audio codec
259 PluginCodec_InputTypeRaw | // raw input data
260 PluginCodec_OutputTypeRaw | // raw output data
261 PluginCodec_RTPTypeShared | // share RTP code
262 PluginCodec_RTPTypeDynamic, // dynamic RTP type
264 iLBC13k3, // text decription
265 iLBC13k3, // source format
266 L16Desc, // destination format
268 (const void *)NULL, // user data
270 8000, // samples per second
271 SPEED_30MS, // raw bits per second
272 30000, // nanoseconds per frame
273 BLOCKL_30MS, // samples per frame
274 NO_OF_BYTES_30MS, // bytes per frame
275 1, // recommended number of frames per packet
276 1, // maximum number of frames per packe
277 0, // IANA RTP payload code
278 sdpILBC, // RTP payload name
280 create_decoder, // create codec function
281 destroy_context, // destroy codec
282 codec_decoder, // encode/decode
283 NULL, // codec controls
285 PluginCodec_H323Codec_nonStandard, // h323CapabilityType
286 &ilbc13k3Cap // h323CapabilityData
290 // encoder
291 PLUGIN_CODEC_VERSION, // codec API version
292 &licenseInfo, // license information
294 PluginCodec_MediaTypeAudio | // audio codec
295 PluginCodec_InputTypeRaw | // raw input data
296 PluginCodec_OutputTypeRaw | // raw output data
297 PluginCodec_RTPTypeShared | // share RTP code
298 PluginCodec_RTPTypeDynamic, // dynamic RTP type
300 iLBC15k2, // text decription
301 L16Desc, // source format
302 iLBC15k2, // destination format
304 (void *)NULL, // user data
306 8000, // samples per second
307 SPEED_20MS, // raw bits per second
308 20000, // nanoseconds per frame
309 BLOCKL_20MS, // samples per frame
310 NO_OF_BYTES_20MS, // bytes per frame
311 1, // recommended number of frames per packet
312 1, // maximum number of frames per packe
313 0, // IANA RTP payload code
314 sdpILBC, // RTP payload name
316 create_encoder, // create codec function
317 destroy_context, // destroy codec
318 codec_encoder, // encode/decode
319 NULL, // codec controls
321 PluginCodec_H323Codec_nonStandard, // h323CapabilityType
322 &ilbc15k2Cap // h323CapabilityData
326 // decoder
327 PLUGIN_CODEC_VERSION, // codec API version
328 &licenseInfo, // license information
330 PluginCodec_MediaTypeAudio | // audio codec
331 PluginCodec_InputTypeRaw | // raw input data
332 PluginCodec_OutputTypeRaw | // raw output data
333 PluginCodec_RTPTypeShared | // share RTP code
334 PluginCodec_RTPTypeDynamic, // dynamic RTP type
336 iLBC15k2, // text decription
337 iLBC15k2, // source format
338 L16Desc, // destination format
340 (void *)NULL, // user data
342 8000, // samples per second
343 SPEED_20MS, // raw bits per second
344 20000, // nanoseconds per frame
345 BLOCKL_20MS, // samples per frame
346 NO_OF_BYTES_20MS, // bytes per frame
347 1, // recommended number of frames per packet
348 1, // maximum number of frames per packe
349 0, // IANA RTP payload code
350 sdpILBC, // RTP payload name
352 create_decoder, // create codec function
353 destroy_context, // destroy codec
354 codec_decoder, // encode/decode
355 NULL, // codec controls
357 PluginCodec_H323Codec_nonStandard, // h323CapabilityType
358 &ilbc15k2Cap // h323CapabilityData
362 #define NUM_DEFNS (sizeof(iLBCCodecDefn) / sizeof(struct PluginCodec_Definition))
364 /////////////////////////////////////////////////////////////////////////////
366 PLUGIN_CODEC_DLL_API struct PluginCodec_Definition * PLUGIN_CODEC_GET_CODEC_FN(unsigned * count, unsigned version)
368 *count = NUM_DEFNS;
369 return iLBCCodecDefn;