Make an AudioEncoder subclass for iLBC

BUG=3926
R=henrik.lundin@webrtc.org, kjellander@google.com
TBR=kjellander@webrtc.org

Review URL: https://webrtc-codereview.appspot.com/32649005

git-svn-id: http://webrtc.googlecode.com/svn/trunk@7828 4adac7df-926f-26a2-2b94-8c16560cd09d
diff --git a/webrtc/modules/audio_coding/BUILD.gn b/webrtc/modules/audio_coding/BUILD.gn
index 0c087ec..184d37e 100644
--- a/webrtc/modules/audio_coding/BUILD.gn
+++ b/webrtc/modules/audio_coding/BUILD.gn
@@ -198,6 +198,8 @@
 
 source_set("ilbc") {
   sources = [
+    "codecs/ilbc/audio_encoder_ilbc.cc",
+    "codecs/ilbc/include/audio_encoder_ilbc.h",
     "codecs/ilbc/abs_quant.c",
     "codecs/ilbc/abs_quant.h",
     "codecs/ilbc/abs_quant_loop.c",
diff --git a/webrtc/modules/audio_coding/codecs/ilbc/audio_encoder_ilbc.cc b/webrtc/modules/audio_coding/codecs/ilbc/audio_encoder_ilbc.cc
new file mode 100644
index 0000000..cf1d632
--- /dev/null
+++ b/webrtc/modules/audio_coding/codecs/ilbc/audio_encoder_ilbc.cc
@@ -0,0 +1,95 @@
+/*
+ *  Copyright (c) 2014 The WebRTC project authors. All Rights Reserved.
+ *
+ *  Use of this source code is governed by a BSD-style license
+ *  that can be found in the LICENSE file in the root of the source
+ *  tree. An additional intellectual property rights grant can be found
+ *  in the file PATENTS.  All contributing project authors may
+ *  be found in the AUTHORS file in the root of the source tree.
+ */
+
+#include "webrtc/modules/audio_coding/codecs/ilbc/interface/audio_encoder_ilbc.h"
+
+#include <cstring>
+#include <limits>
+#include "webrtc/base/checks.h"
+#include "webrtc/modules/audio_coding/codecs/ilbc/interface/ilbc.h"
+
+namespace webrtc {
+
+namespace {
+
+const int kSampleRateHz = 8000;
+
+}  // namespace
+
+AudioEncoderIlbc::AudioEncoderIlbc(const Config& config)
+    : payload_type_(config.payload_type),
+      num_10ms_frames_per_packet_(config.frame_size_ms / 10),
+      num_10ms_frames_buffered_(0) {
+  CHECK(config.frame_size_ms == 20 || config.frame_size_ms == 30)
+      << "Frame size must be 20 or 30 ms.";
+  DCHECK_LE(kSampleRateHz / 100 * num_10ms_frames_per_packet_,
+            kMaxSamplesPerPacket);
+  CHECK_EQ(0, WebRtcIlbcfix_EncoderCreate(&encoder_));
+  CHECK_EQ(0, WebRtcIlbcfix_EncoderInit(encoder_, config.frame_size_ms));
+}
+
+AudioEncoderIlbc::~AudioEncoderIlbc() {
+  CHECK_EQ(0, WebRtcIlbcfix_EncoderFree(encoder_));
+}
+
+int AudioEncoderIlbc::sample_rate_hz() const {
+  return kSampleRateHz;
+}
+int AudioEncoderIlbc::num_channels() const {
+  return 1;
+}
+int AudioEncoderIlbc::Num10MsFramesInNextPacket() const {
+  return num_10ms_frames_per_packet_;
+}
+
+bool AudioEncoderIlbc::EncodeInternal(uint32_t timestamp,
+                                      const int16_t* audio,
+                                      size_t max_encoded_bytes,
+                                      uint8_t* encoded,
+                                      size_t* encoded_bytes,
+                                      EncodedInfo* info) {
+  const size_t expected_output_len ATTRIBUTE_UNUSED =
+      num_10ms_frames_per_packet_ == 2 ? 38 : 50;
+  DCHECK_GE(max_encoded_bytes, expected_output_len);
+
+  // Save timestamp if starting a new packet.
+  if (num_10ms_frames_buffered_ == 0)
+    first_timestamp_in_buffer_ = timestamp;
+
+  // Buffer input.
+  std::memcpy(input_buffer_ + kSampleRateHz / 100 * num_10ms_frames_buffered_,
+              audio,
+              kSampleRateHz / 100 * sizeof(audio[0]));
+
+  // If we don't yet have enough buffered input for a whole packet, we're done
+  // for now.
+  if (++num_10ms_frames_buffered_ < num_10ms_frames_per_packet_) {
+    *encoded_bytes = 0;
+    return true;
+  }
+
+  // Encode buffered input.
+  DCHECK_EQ(num_10ms_frames_buffered_, num_10ms_frames_per_packet_);
+  num_10ms_frames_buffered_ = 0;
+  const int output_len = WebRtcIlbcfix_Encode(
+      encoder_,
+      input_buffer_,
+      kSampleRateHz / 100 * num_10ms_frames_per_packet_,
+      encoded);
+  if (output_len == -1)
+    return false;  // Encoding error.
+  DCHECK_EQ(output_len, static_cast<int>(expected_output_len));
+  *encoded_bytes = output_len;
+  info->encoded_timestamp = first_timestamp_in_buffer_;
+  info->payload_type = payload_type_;
+  return true;
+}
+
+}  // namespace webrtc
diff --git a/webrtc/modules/audio_coding/codecs/ilbc/ilbc.c b/webrtc/modules/audio_coding/codecs/ilbc/ilbc.c
index 21d159f..97b3174 100644
--- a/webrtc/modules/audio_coding/codecs/ilbc/ilbc.c
+++ b/webrtc/modules/audio_coding/codecs/ilbc/ilbc.c
@@ -86,8 +86,10 @@
   }
 }
 
-int16_t WebRtcIlbcfix_Encode(iLBC_encinst_t *iLBCenc_inst, const int16_t *speechIn, int16_t len, int16_t *encoded) {
-
+int16_t WebRtcIlbcfix_Encode(iLBC_encinst_t* iLBCenc_inst,
+                             const int16_t* speechIn,
+                             int16_t len,
+                             uint8_t* encoded) {
   int16_t pos = 0;
   int16_t encpos = 0;
 
@@ -104,7 +106,8 @@
 
     /* call encoder */
     while (pos<len) {
-      WebRtcIlbcfix_EncodeImpl((uint16_t*) &encoded[encpos], &speechIn[pos], (iLBC_Enc_Inst_t*) iLBCenc_inst);
+      WebRtcIlbcfix_EncodeImpl((uint16_t*)&encoded[2 * encpos], &speechIn[pos],
+                               (iLBC_Enc_Inst_t*)iLBCenc_inst);
 #ifdef SPLIT_10MS
       pos += 80;
       if(((iLBC_Enc_Inst_t*)iLBCenc_inst)->section == 0)
diff --git a/webrtc/modules/audio_coding/codecs/ilbc/ilbc.gypi b/webrtc/modules/audio_coding/codecs/ilbc/ilbc.gypi
index ec3284f..dcee4be 100644
--- a/webrtc/modules/audio_coding/codecs/ilbc/ilbc.gypi
+++ b/webrtc/modules/audio_coding/codecs/ilbc/ilbc.gypi
@@ -25,9 +25,11 @@
         ],
       },
       'sources': [
+        'interface/audio_encoder_ilbc.h',
         'interface/ilbc.h',
         'abs_quant.c',
         'abs_quant_loop.c',
+        'audio_encoder_ilbc.cc',
         'augmented_cb_corr.c',
         'bw_expand.c',
         'cb_construct.c',
diff --git a/webrtc/modules/audio_coding/codecs/ilbc/interface/audio_encoder_ilbc.h b/webrtc/modules/audio_coding/codecs/ilbc/interface/audio_encoder_ilbc.h
new file mode 100644
index 0000000..199ef2b
--- /dev/null
+++ b/webrtc/modules/audio_coding/codecs/ilbc/interface/audio_encoder_ilbc.h
@@ -0,0 +1,55 @@
+/*
+ *  Copyright (c) 2014 The WebRTC project authors. All Rights Reserved.
+ *
+ *  Use of this source code is governed by a BSD-style license
+ *  that can be found in the LICENSE file in the root of the source
+ *  tree. An additional intellectual property rights grant can be found
+ *  in the file PATENTS.  All contributing project authors may
+ *  be found in the AUTHORS file in the root of the source tree.
+ */
+
+#ifndef WEBRTC_MODULES_AUDIO_CODING_CODECS_ILBC_INTERFACE_AUDIO_ENCODER_ILBC_H_
+#define WEBRTC_MODULES_AUDIO_CODING_CODECS_ILBC_INTERFACE_AUDIO_ENCODER_ILBC_H_
+
+#include "webrtc/modules/audio_coding/codecs/audio_encoder.h"
+#include "webrtc/modules/audio_coding/codecs/ilbc/interface/ilbc.h"
+#include "webrtc/system_wrappers/interface/scoped_ptr.h"
+
+namespace webrtc {
+
+class AudioEncoderIlbc : public AudioEncoder {
+ public:
+  struct Config {
+    Config() : payload_type(102), frame_size_ms(30) {}
+
+    int payload_type;
+    int frame_size_ms;
+  };
+
+  explicit AudioEncoderIlbc(const Config& config);
+  virtual ~AudioEncoderIlbc();
+
+  virtual int sample_rate_hz() const OVERRIDE;
+  virtual int num_channels() const OVERRIDE;
+  virtual int Num10MsFramesInNextPacket() const OVERRIDE;
+
+ protected:
+  virtual bool EncodeInternal(uint32_t timestamp,
+                              const int16_t* audio,
+                              size_t max_encoded_bytes,
+                              uint8_t* encoded,
+                              size_t* encoded_bytes,
+                              EncodedInfo* info) OVERRIDE;
+
+ private:
+  static const int kMaxSamplesPerPacket = 240;
+  const int payload_type_;
+  const int num_10ms_frames_per_packet_;
+  int num_10ms_frames_buffered_;
+  uint32_t first_timestamp_in_buffer_;
+  int16_t input_buffer_[kMaxSamplesPerPacket];
+  iLBC_encinst_t* encoder_;
+};
+
+}  // namespace webrtc
+#endif  // WEBRTC_MODULES_AUDIO_CODING_CODECS_ILBC_INTERFACE_AUDIO_ENCODER_ILBC_H_
diff --git a/webrtc/modules/audio_coding/codecs/ilbc/interface/ilbc.h b/webrtc/modules/audio_coding/codecs/ilbc/interface/ilbc.h
index ccb3f5e..8e56008 100644
--- a/webrtc/modules/audio_coding/codecs/ilbc/interface/ilbc.h
+++ b/webrtc/modules/audio_coding/codecs/ilbc/interface/ilbc.h
@@ -138,7 +138,7 @@
   int16_t WebRtcIlbcfix_Encode(iLBC_encinst_t *iLBCenc_inst,
                                const int16_t *speechIn,
                                int16_t len,
-                               int16_t *encoded);
+                               uint8_t* encoded);
 
   /****************************************************************************
    * WebRtcIlbcfix_DecoderInit(...)
diff --git a/webrtc/modules/audio_coding/codecs/ilbc/test/iLBC_test.c b/webrtc/modules/audio_coding/codecs/ilbc/test/iLBC_test.c
index 4b86b91..4c1b5cb 100644
--- a/webrtc/modules/audio_coding/codecs/ilbc/test/iLBC_test.c
+++ b/webrtc/modules/audio_coding/codecs/ilbc/test/iLBC_test.c
@@ -163,7 +163,8 @@
     /* encoding */
 
     fprintf(stderr, "--- Encoding block %i --- ",blockcount);
-    len=WebRtcIlbcfix_Encode(Enc_Inst, data, (int16_t)frameLen, encoded_data);
+    len=WebRtcIlbcfix_Encode(Enc_Inst, data, (int16_t)frameLen,
+                             (uint8_t*)encoded_data);
     fprintf(stderr, "\r");
 
     /* write byte file */
diff --git a/webrtc/modules/audio_coding/main/acm2/acm_ilbc.cc b/webrtc/modules/audio_coding/main/acm2/acm_ilbc.cc
index d23ec6e..94d655f 100644
--- a/webrtc/modules/audio_coding/main/acm2/acm_ilbc.cc
+++ b/webrtc/modules/audio_coding/main/acm2/acm_ilbc.cc
@@ -61,7 +61,7 @@
                                 int16_t* bitstream_len_byte) {
   *bitstream_len_byte = WebRtcIlbcfix_Encode(
       encoder_inst_ptr_, &in_audio_[in_audio_ix_read_], frame_len_smpl_,
-      reinterpret_cast<int16_t*>(bitstream));
+      bitstream);
   if (*bitstream_len_byte < 0) {
     WEBRTC_TRACE(webrtc::kTraceError,
                  webrtc::kTraceAudioCoding,
diff --git a/webrtc/modules/audio_coding/neteq/audio_decoder_unittest.cc b/webrtc/modules/audio_coding/neteq/audio_decoder_unittest.cc
index bbcf9ed..2e8118d 100644
--- a/webrtc/modules/audio_coding/neteq/audio_decoder_unittest.cc
+++ b/webrtc/modules/audio_coding/neteq/audio_decoder_unittest.cc
@@ -23,7 +23,7 @@
 #include "webrtc/modules/audio_coding/codecs/g711/include/g711_interface.h"
 #include "webrtc/modules/audio_coding/codecs/g711/include/audio_encoder_pcm.h"
 #include "webrtc/modules/audio_coding/codecs/g722/include/audio_encoder_g722.h"
-#include "webrtc/modules/audio_coding/codecs/ilbc/interface/ilbc.h"
+#include "webrtc/modules/audio_coding/codecs/ilbc/interface/audio_encoder_ilbc.h"
 #include "webrtc/modules/audio_coding/codecs/isac/fix/interface/isacfix.h"
 #include "webrtc/modules/audio_coding/codecs/isac/main/interface/isac.h"
 #include "webrtc/modules/audio_coding/codecs/opus/interface/audio_encoder_opus.h"
@@ -324,25 +324,10 @@
     data_length_ = 10 * frame_size_;
     decoder_ = new AudioDecoderIlbc;
     assert(decoder_);
-    WebRtcIlbcfix_EncoderCreate(&encoder_);
-  }
-
-  ~AudioDecoderIlbcTest() {
-    WebRtcIlbcfix_EncoderFree(encoder_);
-  }
-
-  virtual void InitEncoder() {
-    ASSERT_EQ(0, WebRtcIlbcfix_EncoderInit(encoder_, 30));  // 30 ms.
-  }
-
-  virtual int EncodeFrame(const int16_t* input, size_t input_len_samples,
-                          uint8_t* output) {
-    int enc_len_bytes =
-        WebRtcIlbcfix_Encode(encoder_, input,
-                             static_cast<int>(input_len_samples),
-                             reinterpret_cast<int16_t*>(output));
-    EXPECT_EQ(50, enc_len_bytes);
-    return enc_len_bytes;
+    AudioEncoderIlbc::Config config;
+    config.frame_size_ms = 30;
+    config.payload_type = payload_type_;
+    audio_encoder_.reset(new AudioEncoderIlbc(config));
   }
 
   // Overload the default test since iLBC's function WebRtcIlbcfix_NetEqPlc does
@@ -362,8 +347,6 @@
     // Simply call DecodePlc and verify that we get 0 as return value.
     EXPECT_EQ(0, decoder_->DecodePlc(1, output.get()));
   }
-
-  iLBC_encinst_t* encoder_;
 };
 
 class AudioDecoderIsacFloatTest : public AudioDecoderTest {
diff --git a/webrtc/modules/audio_coding/neteq/test/RTPencode.cc b/webrtc/modules/audio_coding/neteq/test/RTPencode.cc
index efd0069..49d66e9 100644
--- a/webrtc/modules/audio_coding/neteq/test/RTPencode.cc
+++ b/webrtc/modules/audio_coding/neteq/test/RTPencode.cc
@@ -1621,7 +1621,8 @@
 #endif
 #ifdef CODEC_ILBC
         else if (coder==webrtc::kDecoderILBC) { /*iLBC */
-            cdlen=WebRtcIlbcfix_Encode(iLBCenc_inst[k], indata,frameLen,(int16_t*)encoded);
+            cdlen = WebRtcIlbcfix_Encode(iLBCenc_inst[k], indata,
+                                         frameLen, encoded);
         }
 #endif
 #if (defined(CODEC_ISAC) || defined(NETEQ_ISACFIX_CODEC)) // TODO(hlundin): remove all NETEQ_ISACFIX_CODEC