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:
@ -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
|
||||
@ -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)
|
||||
|
||||
@ -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',
|
||||
|
||||
@ -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_
|
||||
@ -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(...)
|
||||
|
||||
@ -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 */
|
||||
|
||||
Reference in New Issue
Block a user