Home | History | Annotate | Download | only in acm2
      1 /*
      2  *  Copyright (c) 2012 The WebRTC project authors. All Rights Reserved.
      3  *
      4  *  Use of this source code is governed by a BSD-style license
      5  *  that can be found in the LICENSE file in the root of the source
      6  *  tree. An additional intellectual property rights grant can be found
      7  *  in the file PATENTS.  All contributing project authors may
      8  *  be found in the AUTHORS file in the root of the source tree.
      9  */
     10 #include "webrtc/modules/audio_coding/main/acm2/acm_ilbc.h"
     11 
     12 #ifdef WEBRTC_CODEC_ILBC
     13 #include "webrtc/modules/audio_coding/codecs/ilbc/interface/ilbc.h"
     14 #include "webrtc/modules/audio_coding/main/acm2/acm_common_defs.h"
     15 #include "webrtc/system_wrappers/interface/trace.h"
     16 #endif
     17 
     18 namespace webrtc {
     19 
     20 namespace acm2 {
     21 
     22 #ifndef WEBRTC_CODEC_ILBC
     23 
     24 ACMILBC::ACMILBC(int16_t /* codec_id */) : encoder_inst_ptr_(NULL) {}
     25 
     26 ACMILBC::~ACMILBC() { return; }
     27 
     28 int16_t ACMILBC::InternalEncode(uint8_t* /* bitstream */,
     29                                 int16_t* /* bitstream_len_byte */) {
     30   return -1;
     31 }
     32 
     33 int16_t ACMILBC::InternalInitEncoder(WebRtcACMCodecParams* /* codec_params */) {
     34   return -1;
     35 }
     36 
     37 ACMGenericCodec* ACMILBC::CreateInstance(void) { return NULL; }
     38 
     39 int16_t ACMILBC::InternalCreateEncoder() { return -1; }
     40 
     41 void ACMILBC::DestructEncoderSafe() { return; }
     42 
     43 void ACMILBC::InternalDestructEncoderInst(void* /* ptr_inst */) { return; }
     44 
     45 int16_t ACMILBC::SetBitRateSafe(const int32_t /* rate */) { return -1; }
     46 
     47 #else  //===================== Actual Implementation =======================
     48 
     49 ACMILBC::ACMILBC(int16_t codec_id) : encoder_inst_ptr_(NULL) {
     50   codec_id_ = codec_id;
     51   return;
     52 }
     53 
     54 ACMILBC::~ACMILBC() {
     55   if (encoder_inst_ptr_ != NULL) {
     56     WebRtcIlbcfix_EncoderFree(encoder_inst_ptr_);
     57     encoder_inst_ptr_ = NULL;
     58   }
     59   return;
     60 }
     61 
     62 int16_t ACMILBC::InternalEncode(uint8_t* bitstream,
     63                                 int16_t* bitstream_len_byte) {
     64   *bitstream_len_byte = WebRtcIlbcfix_Encode(
     65       encoder_inst_ptr_, &in_audio_[in_audio_ix_read_], frame_len_smpl_,
     66       reinterpret_cast<int16_t*>(bitstream));
     67   if (*bitstream_len_byte < 0) {
     68     WEBRTC_TRACE(webrtc::kTraceError,
     69                  webrtc::kTraceAudioCoding,
     70                  unique_id_,
     71                  "InternalEncode: error in encode for ILBC");
     72     return -1;
     73   }
     74   // increment the read index this tell the caller that how far
     75   // we have gone forward in reading the audio buffer
     76   in_audio_ix_read_ += frame_len_smpl_;
     77   return *bitstream_len_byte;
     78 }
     79 
     80 int16_t ACMILBC::InternalInitEncoder(WebRtcACMCodecParams* codec_params) {
     81   // initialize with a correct processing block length
     82   if ((160 == (codec_params->codec_inst).pacsize) ||
     83       (320 == (codec_params->codec_inst).pacsize)) {
     84     // processing block of 20ms
     85     return WebRtcIlbcfix_EncoderInit(encoder_inst_ptr_, 20);
     86   } else if ((240 == (codec_params->codec_inst).pacsize) ||
     87       (480 == (codec_params->codec_inst).pacsize)) {
     88     // processing block of 30ms
     89     return WebRtcIlbcfix_EncoderInit(encoder_inst_ptr_, 30);
     90   } else {
     91     WEBRTC_TRACE(webrtc::kTraceError, webrtc::kTraceAudioCoding, unique_id_,
     92                  "InternalInitEncoder: invalid processing block");
     93     return -1;
     94   }
     95 }
     96 
     97 ACMGenericCodec* ACMILBC::CreateInstance(void) { return NULL; }
     98 
     99 int16_t ACMILBC::InternalCreateEncoder() {
    100   if (WebRtcIlbcfix_EncoderCreate(&encoder_inst_ptr_) < 0) {
    101     WEBRTC_TRACE(webrtc::kTraceError,
    102                  webrtc::kTraceAudioCoding,
    103                  unique_id_,
    104                  "InternalCreateEncoder: cannot create instance for ILBC "
    105                  "encoder");
    106     return -1;
    107   }
    108   return 0;
    109 }
    110 
    111 void ACMILBC::DestructEncoderSafe() {
    112   encoder_initialized_ = false;
    113   encoder_exist_ = false;
    114   if (encoder_inst_ptr_ != NULL) {
    115     WebRtcIlbcfix_EncoderFree(encoder_inst_ptr_);
    116     encoder_inst_ptr_ = NULL;
    117   }
    118 }
    119 
    120 void ACMILBC::InternalDestructEncoderInst(void* ptr_inst) {
    121   if (ptr_inst != NULL) {
    122     WebRtcIlbcfix_EncoderFree(static_cast<iLBC_encinst_t_*>(ptr_inst));
    123   }
    124   return;
    125 }
    126 
    127 int16_t ACMILBC::SetBitRateSafe(const int32_t rate) {
    128   // Check that rate is valid. No need to store the value
    129   if (rate == 13300) {
    130     WebRtcIlbcfix_EncoderInit(encoder_inst_ptr_, 30);
    131   } else if (rate == 15200) {
    132     WebRtcIlbcfix_EncoderInit(encoder_inst_ptr_, 20);
    133   } else {
    134     return -1;
    135   }
    136   encoder_params_.codec_inst.rate = rate;
    137 
    138   return 0;
    139 }
    140 
    141 #endif
    142 
    143 }  // namespace acm2
    144 
    145 }  // namespace webrtc
    146