home *** CD-ROM | disk | FTP | other *** search
/ ftp.muug.mb.ca / 2014.06.ftp.muug.mb.ca.tar / ftp.muug.mb.ca / pub / openh323.tar.gz / openh323.tar / openh323 / src / ilbccodec.cxx < prev    next >
C/C++ Source or Header  |  2003-06-05  |  5KB  |  188 lines

  1. /*
  2.  * ilbccodec.cxx
  3.  *
  4.  * Internet Low Bitrate Codec
  5.  *
  6.  * Open H323 Library
  7.  *
  8.  * Copyright (c) 2003 Equivalence Pty. Ltd.
  9.  *
  10.  * The contents of this file are subject to the Mozilla Public License
  11.  * Version 1.0 (the "License"); you may not use this file except in
  12.  * compliance with the License. You may obtain a copy of the License at
  13.  * http://www.mozilla.org/MPL/
  14.  *
  15.  * Software distributed under the License is distributed on an "AS IS"
  16.  * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See
  17.  * the License for the specific language governing rights and limitations
  18.  * under the License.
  19.  *
  20.  * The Original Code is Open H323 Library.
  21.  *
  22.  * The Initial Developer of the Original Code is Equivalence Pty. Ltd.
  23.  *
  24.  * Portions of this code were written with the assisance of funding from
  25.  * Vovida Networks, Inc. http://www.vovida.com.
  26.  *
  27.  * Contributor(s): ______________________________________.
  28.  *
  29.  * $Log: ilbccodec.cxx,v $
  30.  * Revision 1.1  2003/06/06 02:19:12  rjongbloed
  31.  * Added iLBC codec
  32.  *
  33.  */
  34.  
  35. #include <ptlib.h>
  36.  
  37. #ifdef __GNUC__
  38. #pragma implementation "ilbccodec.h"
  39. #endif
  40.  
  41. #include "ilbccodec.h"
  42.  
  43. #include "rtp.h"
  44.  
  45. extern "C" {
  46. #include "iLBC/iLBC_encode.h" 
  47. #include "iLBC/iLBC_decode.h" 
  48. };
  49.     
  50.  
  51. #define new PNEW
  52.  
  53.  
  54. #define H323_ILBC_13k3 OPAL_ILBC_13k3"{sw}"
  55. #define H323_ILBC_15k2 OPAL_ILBC_15k2"{sw}"
  56.  
  57. H323_REGISTER_CAPABILITY_FUNCTION(H323_iLBC_Capability_13k3, H323_ILBC_13k3, ep)
  58.   { return new H323_iLBC_Capability(ep, H323_iLBC_Capability::e_13k3); }
  59.  
  60. H323_REGISTER_CAPABILITY_FUNCTION(H323_iLBC_Capability_15k2, H323_ILBC_15k2, ep)
  61.   { return new H323_iLBC_Capability(ep, H323_iLBC_Capability::e_15k2); }
  62.  
  63.  
  64. OpalMediaFormat const OpaliLBC_13k3(OPAL_ILBC_13k3,
  65.                                     OpalMediaFormat::DefaultAudioSessionID,
  66.                                     RTP_DataFrame::DynamicBase,
  67.                                     TRUE,  // Needs jitter
  68.                                     NO_OF_BYTES_30MS*8*8000/BLOCKL_30MS,
  69.                                     NO_OF_BYTES_30MS,
  70.                                     BLOCKL_30MS,
  71.                                     OpalMediaFormat::AudioTimeUnits);
  72.  
  73. OpalMediaFormat const OpaliLBC_15k2(OPAL_ILBC_15k2,
  74.                                     OpalMediaFormat::DefaultAudioSessionID,
  75.                                     RTP_DataFrame::DynamicBase,
  76.                                     TRUE,  // Needs jitter
  77.                                     NO_OF_BYTES_20MS*8*8000/BLOCKL_20MS,
  78.                                     NO_OF_BYTES_20MS,
  79.                                     BLOCKL_20MS,
  80.                                     OpalMediaFormat::AudioTimeUnits);
  81.  
  82.  
  83. /////////////////////////////////////////////////////////////////////////////
  84.  
  85. H323_iLBC_Capability::H323_iLBC_Capability(H323EndPoint & endpoint, Speed s)
  86.   : H323NonStandardAudioCapability(7, speed == e_13k3 ? 3 : 4, endpoint,
  87.                                    (const BYTE *)(s == e_13k3 ? OPAL_ILBC_13k3 : OPAL_ILBC_15k2))
  88. {
  89.   speed = s;
  90. }
  91.  
  92.  
  93. PObject * H323_iLBC_Capability::Clone() const
  94. {
  95.   return new H323_iLBC_Capability(*this);
  96. }
  97.  
  98.  
  99. PString H323_iLBC_Capability::GetFormatName() const
  100. {
  101.   return speed == e_13k3 ? H323_ILBC_13k3 : H323_ILBC_15k2;
  102. }
  103.  
  104.  
  105. H323Codec * H323_iLBC_Capability::CreateCodec(H323Codec::Direction direction) const
  106. {
  107.   return new H323_iLBC_Codec(direction, speed);
  108. }
  109.  
  110.  
  111. /////////////////////////////////////////////////////////////////////////////
  112.  
  113. H323_iLBC_Codec::H323_iLBC_Codec(Direction dir, H323_iLBC_Capability::Speed speed)
  114.   : H323FramedAudioCodec(speed == H323_iLBC_Capability::e_13k3 ? OPAL_ILBC_13k3
  115.                                                                : OPAL_ILBC_15k2,
  116.                          dir)
  117. {
  118.   int ilbc_speed = speed == H323_iLBC_Capability::e_13k3 ? 30 : 20;
  119.  
  120.   if (dir == Encoder) {
  121.     decoder = NULL;
  122.     encoder = (struct iLBC_Enc_Inst_t_ *)malloc((unsigned)sizeof(struct iLBC_Enc_Inst_t_));
  123.     if (encoder != 0) 
  124.       initEncode(encoder, ilbc_speed); 
  125.   }
  126.   else {
  127.     encoder = NULL;
  128.     decoder = (struct iLBC_Dec_Inst_t_ *)malloc((unsigned)sizeof(struct iLBC_Dec_Inst_t_));
  129.     if (decoder != 0) 
  130.       initDecode(decoder, ilbc_speed, 1); 
  131.   }
  132.  
  133.   PTRACE(3, "Codec\tILBC " << (dir == Encoder ? "en" : "de")
  134.          << "coder created");
  135. }
  136.  
  137.  
  138. H323_iLBC_Codec::~H323_iLBC_Codec()
  139. {
  140.   if (encoder != NULL)
  141.     free(encoder);
  142.   if (decoder != NULL)
  143.     free(decoder);
  144. }
  145.  
  146.  
  147. BOOL H323_iLBC_Codec::EncodeFrame(BYTE * buffer, unsigned & length)
  148. {
  149.   float block[BLOCKL_MAX];
  150.  
  151.   /* convert signal to float */
  152.   for (int i = 0; i < encoder->blockl; i++)
  153.     block[i] = (float)sampleBuffer[i];
  154.  
  155.   /* do the actual encoding */
  156.   iLBC_encode(buffer, block, encoder);
  157.   length = encoder->no_of_bytes;
  158.  
  159.   return TRUE; 
  160. }
  161.  
  162.  
  163. BOOL H323_iLBC_Codec::DecodeFrame(const BYTE * buffer, unsigned length, unsigned &)
  164. {
  165.   if (length < (unsigned)decoder->no_of_bytes)
  166.     return FALSE;
  167.  
  168.   float block[BLOCKL_MAX];
  169.  
  170.   /* do actual decoding of block */ 
  171.   iLBC_decode(block, (unsigned char *)buffer, decoder, 1);
  172.  
  173.   /* convert to short */     
  174.   for (int i = 0; i < decoder->blockl; i++) {
  175.     float tmp = block[i];
  176.     if (tmp < MIN_SAMPLE)
  177.       tmp = MIN_SAMPLE;
  178.     else if (tmp > MAX_SAMPLE)
  179.       tmp = MAX_SAMPLE;
  180.     sampleBuffer[i] = (short)tmp;
  181.   }
  182.  
  183.   return TRUE;
  184. }
  185.  
  186.  
  187. /////////////////////////////////////////////////////////////////////////////
  188.