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
This commit is contained in:
kwiberg@webrtc.org
2014-12-08 17:11:44 +00:00
parent ee43263a50
commit cb858ba397
10 changed files with 171 additions and 29 deletions

View File

@@ -198,6 +198,8 @@ config("ilbc_config") {
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",

View File

@@ -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

View File

@@ -86,8 +86,10 @@ int16_t WebRtcIlbcfix_EncoderInit(iLBC_encinst_t *iLBCenc_inst, int16_t mode)
}
}
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 @@ int16_t WebRtcIlbcfix_Encode(iLBC_encinst_t *iLBCenc_inst, const int16_t *speech
/* 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)

View File

@@ -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',

View File

@@ -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_

View File

@@ -138,7 +138,7 @@ extern "C" {
int16_t WebRtcIlbcfix_Encode(iLBC_encinst_t *iLBCenc_inst,
const int16_t *speechIn,
int16_t len,
int16_t *encoded);
uint8_t* encoded);
/****************************************************************************
* WebRtcIlbcfix_DecoderInit(...)

View File

@@ -163,7 +163,8 @@ int main(int argc, char* argv[])
/* 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 */

View File

@@ -61,7 +61,7 @@ int16_t ACMILBC::InternalEncode(uint8_t* bitstream,
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,

View File

@@ -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 @@ class AudioDecoderIlbcTest : public AudioDecoderTest {
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 @@ class AudioDecoderIlbcTest : public AudioDecoderTest {
// 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 {

View File

@@ -1621,7 +1621,8 @@ int NetEQTest_encode(int coder, int16_t *indata, int frameLen, unsigned char * e
#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