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 
     11 #include "webrtc/modules/audio_coding/main/acm2/acm_g7221.h"
     12 
     13 #ifdef WEBRTC_CODEC_G722_1
     14 // NOTE! G.722.1 is not included in the open-source package. The following
     15 // interface file is needed:
     16 #include "webrtc/modules/audio_coding/main/codecs/g7221/interface/g7221_interface.h"
     17 #include "webrtc/modules/audio_coding/main/acm2/acm_codec_database.h"
     18 #include "webrtc/modules/audio_coding/main/acm2/acm_common_defs.h"
     19 #include "webrtc/system_wrappers/interface/trace.h"
     20 
     21 // The API in the header file should match the one below.
     22 //
     23 // int16_t WebRtcG7221_CreateEnc16(G722_1_16_encinst_t_** enc_inst);
     24 // int16_t WebRtcG7221_CreateEnc24(G722_1_24_encinst_t_** enc_inst);
     25 // int16_t WebRtcG7221_CreateEnc32(G722_1_32_encinst_t_** enc_inst);
     26 // int16_t WebRtcG7221_CreateDec16(G722_1_16_decinst_t_** dec_inst);
     27 // int16_t WebRtcG7221_CreateDec24(G722_1_24_decinst_t_** dec_inst);
     28 // int16_t WebRtcG7221_CreateDec32(G722_1_32_decinst_t_** dec_inst);
     29 //
     30 // int16_t WebRtcG7221_FreeEnc16(G722_1_16_encinst_t_** enc_inst);
     31 // int16_t WebRtcG7221_FreeEnc24(G722_1_24_encinst_t_** enc_inst);
     32 // int16_t WebRtcG7221_FreeEnc32(G722_1_32_encinst_t_** enc_inst);
     33 // int16_t WebRtcG7221_FreeDec16(G722_1_16_decinst_t_** dec_inst);
     34 // int16_t WebRtcG7221_FreeDec24(G722_1_24_decinst_t_** dec_inst);
     35 // int16_t WebRtcG7221_FreeDec32(G722_1_32_decinst_t_** dec_inst);
     36 //
     37 // int16_t WebRtcG7221_EncoderInit16(G722_1_16_encinst_t_* enc_inst);
     38 // int16_t WebRtcG7221_EncoderInit24(G722_1_24_encinst_t_* enc_inst);
     39 // int16_t WebRtcG7221_EncoderInit32(G722_1_32_encinst_t_* enc_inst);
     40 // int16_t WebRtcG7221_DecoderInit16(G722_1_16_decinst_t_* dec_inst);
     41 // int16_t WebRtcG7221_DecoderInit24(G722_1_24_decinst_t_* dec_inst);
     42 // int16_t WebRtcG7221_DecoderInit32(G722_1_32_decinst_t_* dec_inst);
     43 //
     44 // int16_t WebRtcG7221_Encode16(G722_1_16_encinst_t_* enc_inst,
     45 //                              int16_t* input,
     46 //                              int16_t len,
     47 //                              int16_t* output);
     48 // int16_t WebRtcG7221_Encode24(G722_1_24_encinst_t_* enc_inst,
     49 //                              int16_t* input,
     50 //                              int16_t len,
     51 //                              int16_t* output);
     52 // int16_t WebRtcG7221_Encode32(G722_1_32_encinst_t_* enc_inst,
     53 //                              int16_t* input,
     54 //                              int16_t len,
     55 //                              int16_t* output);
     56 //
     57 // int16_t WebRtcG7221_Decode16(G722_1_16_decinst_t_* dec_inst,
     58 //                              int16_t* bitstream,
     59 //                              int16_t len,
     60 //                              int16_t* output);
     61 // int16_t WebRtcG7221_Decode24(G722_1_24_decinst_t_* dec_inst,
     62 //                              int16_t* bitstream,
     63 //                              int16_t len,
     64 //                              int16_t* output);
     65 // int16_t WebRtcG7221_Decode32(G722_1_32_decinst_t_* dec_inst,
     66 //                              int16_t* bitstream,
     67 //                              int16_t len,
     68 //                              int16_t* output);
     69 //
     70 // int16_t WebRtcG7221_DecodePlc16(G722_1_16_decinst_t_* dec_inst,
     71 //                                 int16_t* output,
     72 //                                 int16_t nr_lost_frames);
     73 // int16_t WebRtcG7221_DecodePlc24(G722_1_24_decinst_t_* dec_inst,
     74 //                                 int16_t* output,
     75 //                                 int16_t nr_lost_frames);
     76 // int16_t WebRtcG7221_DecodePlc32(G722_1_32_decinst_t_* dec_inst,
     77 //                                 int16_t* output,
     78 //                                 int16_t nr_lost_frames);
     79 #endif
     80 
     81 namespace webrtc {
     82 
     83 namespace acm2 {
     84 
     85 #ifndef WEBRTC_CODEC_G722_1
     86 
     87 ACMG722_1::ACMG722_1(int16_t /* codec_id */)
     88     : operational_rate_(-1),
     89       encoder_inst_ptr_(NULL),
     90       encoder_inst_ptr_right_(NULL),
     91       encoder_inst16_ptr_(NULL),
     92       encoder_inst16_ptr_right_(NULL),
     93       encoder_inst24_ptr_(NULL),
     94       encoder_inst24_ptr_right_(NULL),
     95       encoder_inst32_ptr_(NULL),
     96       encoder_inst32_ptr_right_(NULL) {
     97   return;
     98 }
     99 
    100 ACMG722_1::~ACMG722_1() { return; }
    101 
    102 int16_t ACMG722_1::InternalEncode(uint8_t* /* bitstream */,
    103                                   int16_t* /* bitstream_len_byte */) {
    104   return -1;
    105 }
    106 
    107 int16_t ACMG722_1::InternalInitEncoder(
    108     WebRtcACMCodecParams* /* codec_params */) {
    109   return -1;
    110 }
    111 
    112 ACMGenericCodec* ACMG722_1::CreateInstance(void) { return NULL; }
    113 
    114 int16_t ACMG722_1::InternalCreateEncoder() { return -1; }
    115 
    116 void ACMG722_1::DestructEncoderSafe() { return; }
    117 
    118 #else  //===================== Actual Implementation =======================
    119 ACMG722_1::ACMG722_1(int16_t codec_id)
    120     : encoder_inst_ptr_(NULL),
    121       encoder_inst_ptr_right_(NULL),
    122       encoder_inst16_ptr_(NULL),
    123       encoder_inst16_ptr_right_(NULL),
    124       encoder_inst24_ptr_(NULL),
    125       encoder_inst24_ptr_right_(NULL),
    126       encoder_inst32_ptr_(NULL),
    127       encoder_inst32_ptr_right_(NULL) {
    128   codec_id_ = codec_id;
    129   if (codec_id_ == ACMCodecDB::kG722_1_16) {
    130     operational_rate_ = 16000;
    131   } else if (codec_id_ == ACMCodecDB::kG722_1_24) {
    132     operational_rate_ = 24000;
    133   } else if (codec_id_ == ACMCodecDB::kG722_1_32) {
    134     operational_rate_ = 32000;
    135   } else {
    136     operational_rate_ = -1;
    137   }
    138   return;
    139 }
    140 
    141 ACMG722_1::~ACMG722_1() {
    142   if (encoder_inst_ptr_ != NULL) {
    143     delete encoder_inst_ptr_;
    144     encoder_inst_ptr_ = NULL;
    145   }
    146   if (encoder_inst_ptr_right_ != NULL) {
    147     delete encoder_inst_ptr_right_;
    148     encoder_inst_ptr_right_ = NULL;
    149   }
    150 
    151   switch (operational_rate_) {
    152     case 16000: {
    153       encoder_inst16_ptr_ = NULL;
    154       encoder_inst16_ptr_right_ = NULL;
    155       break;
    156     }
    157     case 24000: {
    158       encoder_inst24_ptr_ = NULL;
    159       encoder_inst24_ptr_right_ = NULL;
    160       break;
    161     }
    162     case 32000: {
    163       encoder_inst32_ptr_ = NULL;
    164       encoder_inst32_ptr_right_ = NULL;
    165       break;
    166     }
    167     default: {
    168       break;
    169     }
    170   }
    171   return;
    172 }
    173 
    174 int16_t ACMG722_1::InternalEncode(uint8_t* bitstream,
    175                                   int16_t* bitstream_len_byte) {
    176   int16_t left_channel[320];
    177   int16_t right_channel[320];
    178   int16_t len_in_bytes;
    179   int16_t out_bits[160];
    180 
    181   // If stereo, split input signal in left and right channel before encoding
    182   if (num_channels_ == 2) {
    183     for (int i = 0, j = 0; i < frame_len_smpl_ * 2; i += 2, j++) {
    184       left_channel[j] = in_audio_[in_audio_ix_read_ + i];
    185       right_channel[j] = in_audio_[in_audio_ix_read_ + i + 1];
    186     }
    187   } else {
    188     memcpy(left_channel, &in_audio_[in_audio_ix_read_], 320);
    189   }
    190 
    191   switch (operational_rate_) {
    192     case 16000: {
    193       len_in_bytes = WebRtcG7221_Encode16(encoder_inst16_ptr_, left_channel,
    194                                                320, &out_bits[0]);
    195       if (num_channels_ == 2) {
    196         len_in_bytes += WebRtcG7221_Encode16(encoder_inst16_ptr_right_,
    197                                              right_channel, 320,
    198                                              &out_bits[len_in_bytes / 2]);
    199       }
    200       break;
    201     }
    202     case 24000: {
    203       len_in_bytes = WebRtcG7221_Encode24(encoder_inst24_ptr_, left_channel,
    204                                           320, &out_bits[0]);
    205       if (num_channels_ == 2) {
    206         len_in_bytes += WebRtcG7221_Encode24(encoder_inst24_ptr_right_,
    207                                              right_channel, 320,
    208                                              &out_bits[len_in_bytes / 2]);
    209       }
    210       break;
    211     }
    212     case 32000: {
    213       len_in_bytes = WebRtcG7221_Encode32(encoder_inst32_ptr_, left_channel,
    214                                           320, &out_bits[0]);
    215       if (num_channels_ == 2) {
    216         len_in_bytes += WebRtcG7221_Encode32(encoder_inst32_ptr_right_,
    217                                              right_channel, 320,
    218                                              &out_bits[len_in_bytes / 2]);
    219       }
    220       break;
    221     }
    222     default: {
    223       WEBRTC_TRACE(webrtc::kTraceError, webrtc::kTraceAudioCoding, unique_id_,
    224                    "InternalInitEncode: Wrong rate for G722_1.");
    225       return -1;
    226     }
    227   }
    228   memcpy(bitstream, out_bits, len_in_bytes);
    229   *bitstream_len_byte = len_in_bytes;
    230 
    231   // increment the read index this tell the caller that how far
    232   // we have gone forward in reading the audio buffer
    233   in_audio_ix_read_ += 320 * num_channels_;
    234   return *bitstream_len_byte;
    235 }
    236 
    237 int16_t ACMG722_1::InternalInitEncoder(WebRtcACMCodecParams* codec_params) {
    238   int16_t ret;
    239 
    240   switch (operational_rate_) {
    241     case 16000: {
    242       ret = WebRtcG7221_EncoderInit16(encoder_inst16_ptr_right_);
    243       if (ret < 0) {
    244         return ret;
    245       }
    246       return WebRtcG7221_EncoderInit16(encoder_inst16_ptr_);
    247     }
    248     case 24000: {
    249       ret = WebRtcG7221_EncoderInit24(encoder_inst24_ptr_right_);
    250       if (ret < 0) {
    251         return ret;
    252       }
    253       return WebRtcG7221_EncoderInit24(encoder_inst24_ptr_);
    254     }
    255     case 32000: {
    256       ret = WebRtcG7221_EncoderInit32(encoder_inst32_ptr_right_);
    257       if (ret < 0) {
    258         return ret;
    259       }
    260       return WebRtcG7221_EncoderInit32(encoder_inst32_ptr_);
    261     }
    262     default: {
    263       WEBRTC_TRACE(webrtc::kTraceError, webrtc::kTraceAudioCoding,
    264                    unique_id_, "InternalInitEncoder: Wrong rate for G722_1.");
    265       return -1;
    266     }
    267   }
    268 }
    269 
    270 ACMGenericCodec* ACMG722_1::CreateInstance(void) { return NULL; }
    271 
    272 int16_t ACMG722_1::InternalCreateEncoder() {
    273   if ((encoder_inst_ptr_ == NULL) || (encoder_inst_ptr_right_ == NULL)) {
    274     return -1;
    275   }
    276   switch (operational_rate_) {
    277     case 16000: {
    278       WebRtcG7221_CreateEnc16(&encoder_inst16_ptr_);
    279       WebRtcG7221_CreateEnc16(&encoder_inst16_ptr_right_);
    280       break;
    281     }
    282     case 24000: {
    283       WebRtcG7221_CreateEnc24(&encoder_inst24_ptr_);
    284       WebRtcG7221_CreateEnc24(&encoder_inst24_ptr_right_);
    285       break;
    286     }
    287     case 32000: {
    288       WebRtcG7221_CreateEnc32(&encoder_inst32_ptr_);
    289       WebRtcG7221_CreateEnc32(&encoder_inst32_ptr_right_);
    290       break;
    291     }
    292     default: {
    293       WEBRTC_TRACE(webrtc::kTraceError, webrtc::kTraceAudioCoding, unique_id_,
    294                    "InternalCreateEncoder: Wrong rate for G722_1.");
    295       return -1;
    296     }
    297   }
    298   return 0;
    299 }
    300 
    301 void ACMG722_1::DestructEncoderSafe() {
    302   encoder_exist_ = false;
    303   encoder_initialized_ = false;
    304   if (encoder_inst_ptr_ != NULL) {
    305     delete encoder_inst_ptr_;
    306     encoder_inst_ptr_ = NULL;
    307   }
    308   if (encoder_inst_ptr_right_ != NULL) {
    309     delete encoder_inst_ptr_right_;
    310     encoder_inst_ptr_right_ = NULL;
    311   }
    312   encoder_inst16_ptr_ = NULL;
    313   encoder_inst24_ptr_ = NULL;
    314   encoder_inst32_ptr_ = NULL;
    315 }
    316 
    317 #endif
    318 
    319 }  // namespace acm2
    320 
    321 }  // namespace webrtc
    322