codec_ilbc.c

00001 /*
00002  * iaxclient: a cross-platform IAX softphone library
00003  *
00004  * Copyrights:
00005  * Copyright (C) 2003-2006, Horizon Wimba, Inc.
00006  * Copyright (C) 2007, Wimba, Inc.
00007  *
00008  * Contributors:
00009  * Steve Kann <stevek@stevek.com>
00010  *
00011  * This program is free software, distributed under the terms of
00012  * the GNU Lesser (Library) General Public License.
00013  */
00014 
00015 #include "codec_ilbc.h"
00016 #include "iaxclient_lib.h"
00017 #include "iLBC/iLBC_encode.h"
00018 #include "iLBC/iLBC_decode.h"
00019 
00020 
00021 static void destroy ( struct iaxc_audio_codec *c) {
00022     free(c->encstate);
00023     free(c->decstate);
00024     free(c);
00025 }
00026 
00027 
00028 static int decode ( struct iaxc_audio_codec *c,
00029     int *inlen, char *in, int *outlen, short *out ) {
00030 
00031     float fbuf[240];
00032     int i;
00033 
00034     if(*inlen == 0) {
00035         //fprintf(stderr, "ILBC Interpolate\n");
00036         iLBC_decode(fbuf, NULL, c->decstate, 0);
00037         for(i=0;i<240;i++)
00038             out[i] = fbuf[i];
00039         *outlen -= 240;
00040         return 0;
00041     }
00042 
00043 
00044     /* need to decode minimum of 33 bytes to 160 byte output */
00045     if( (*inlen < 50) || (*outlen < 240) ) {
00046         fprintf(stderr, "codec_ilbc: inlen = %d outlen= %d\n",*inlen,*outlen);
00047         return -1;
00048     }
00049 
00050     while( (*inlen >= 50) && (*outlen >= 240) ) {
00051         iLBC_decode(fbuf, in, c->decstate, 1);
00052         for(i=0;i<240;i++)
00053             out[i] = fbuf[i];
00054 
00055         out += 240;
00056         *outlen -= 240;
00057         in += 50;
00058         *inlen -= 50;
00059     }
00060 
00061     return 0;
00062 }
00063 
00064 static int encode ( struct iaxc_audio_codec *c,
00065     int *inlen, short *in, int *outlen, char *out ) {
00066 
00067     float fbuf[240];
00068     int i;
00069 
00070     while( (*inlen >= 240) && (*outlen >= 50) ) {
00071 
00072         for(i=0;i<240;i++)
00073             fbuf[i] = in[i];
00074 
00075         iLBC_encode(out,fbuf, c->encstate);
00076 
00077         out += 50;
00078         *outlen -= 50;
00079         in += 240;
00080         *inlen -= 240;
00081     }
00082 
00083     return 0;
00084 }
00085 
00086 struct iaxc_audio_codec *codec_audio_ilbc_new() {
00087   struct iaxc_audio_codec *c = calloc(sizeof(struct iaxc_audio_codec),1);
00088 
00089 
00090   if(!c) return c;
00091 
00092   strcpy(c->name,"iLBC");
00093   c->format = IAXC_FORMAT_ILBC;
00094   c->encode = encode;
00095   c->decode = decode;
00096   c->destroy = destroy;
00097 
00098   c->minimum_frame_size = 240;
00099 
00100   c->encstate = calloc(sizeof(iLBC_Enc_Inst_t),1);
00101   c->decstate = calloc(sizeof(iLBC_Dec_Inst_t),1);
00102 
00103   /* leaks a bit on no-memory */
00104   if(!(c->encstate && c->decstate))
00105       return NULL;
00106 
00107   /* the 30 parameters are used for the latest iLBC sources, in
00108    * http://www.ietf.org/internet-drafts/draft-ietf-avt-ilbc-codec-05.txt
00109    * as used in asterisk-CVS as of 14 Oct 2004 */
00110   initEncode(c->encstate, 30);
00111   initDecode(c->decstate, 30, 1); /* use enhancer */
00112 
00113   return c;
00114 }
00115 

Generated on Mon Sep 24 15:43:29 2007 for IAXClient by  doxygen 1.5.3