2011-07-07 08:21:25 +00:00
|
|
|
/*
|
2012-05-16 07:11:53 +00:00
|
|
|
* Copyright (c) 2012 The WebRTC project authors. All Rights Reserved.
|
2011-07-07 08:21:25 +00:00
|
|
|
*
|
|
|
|
|
* 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.
|
|
|
|
|
*/
|
|
|
|
|
|
2012-10-18 10:00:52 +00:00
|
|
|
#include "acm_opus.h"
|
|
|
|
|
|
|
|
|
|
#include "acm_codec_database.h"
|
2011-07-07 08:21:25 +00:00
|
|
|
#include "acm_common_defs.h"
|
|
|
|
|
#include "acm_neteq.h"
|
|
|
|
|
#include "trace.h"
|
|
|
|
|
#include "webrtc_neteq.h"
|
|
|
|
|
#include "webrtc_neteq_help_macros.h"
|
|
|
|
|
|
|
|
|
|
#ifdef WEBRTC_CODEC_OPUS
|
2012-10-19 07:13:52 +00:00
|
|
|
#include "modules/audio_coding/codecs/opus/interface/opus_interface.h"
|
2011-07-07 08:21:25 +00:00
|
|
|
#endif
|
|
|
|
|
|
2012-10-18 10:00:52 +00:00
|
|
|
namespace webrtc {
|
2011-07-07 08:21:25 +00:00
|
|
|
|
|
|
|
|
#ifndef WEBRTC_CODEC_OPUS
|
|
|
|
|
|
2012-10-18 10:00:52 +00:00
|
|
|
ACMOpus::ACMOpus(int16_t /* codecID */)
|
2012-01-19 13:22:22 +00:00
|
|
|
: _encoderInstPtr(NULL),
|
|
|
|
|
_decoderInstPtr(NULL),
|
2012-10-18 10:00:52 +00:00
|
|
|
_sampleFreq(0),
|
|
|
|
|
_bitrate(0) {
|
2012-01-19 13:22:22 +00:00
|
|
|
return;
|
2011-07-07 08:21:25 +00:00
|
|
|
}
|
|
|
|
|
|
2012-10-18 10:00:52 +00:00
|
|
|
ACMOpus::~ACMOpus() {
|
|
|
|
|
return;
|
2011-07-07 08:21:25 +00:00
|
|
|
}
|
|
|
|
|
|
2012-10-18 10:00:52 +00:00
|
|
|
int16_t ACMOpus::InternalEncode(uint8_t* /* bitStream */,
|
|
|
|
|
int16_t* /* bitStreamLenByte */) {
|
|
|
|
|
return -1;
|
2011-07-07 08:21:25 +00:00
|
|
|
}
|
|
|
|
|
|
2012-10-18 10:00:52 +00:00
|
|
|
int16_t ACMOpus::DecodeSafe(uint8_t* /* bitStream */,
|
|
|
|
|
int16_t /* bitStreamLenByte */,
|
|
|
|
|
int16_t* /* audio */,
|
|
|
|
|
int16_t* /* audioSamples */,
|
|
|
|
|
int8_t* /* speechType */) {
|
|
|
|
|
return -1;
|
2011-07-07 08:21:25 +00:00
|
|
|
}
|
|
|
|
|
|
2012-10-18 10:00:52 +00:00
|
|
|
int16_t ACMOpus::InternalInitEncoder(WebRtcACMCodecParams* /* codecParams */) {
|
|
|
|
|
return -1;
|
2011-07-07 08:21:25 +00:00
|
|
|
}
|
|
|
|
|
|
2012-10-18 10:00:52 +00:00
|
|
|
int16_t ACMOpus::InternalInitDecoder(WebRtcACMCodecParams* /* codecParams */) {
|
|
|
|
|
return -1;
|
2011-07-07 08:21:25 +00:00
|
|
|
}
|
|
|
|
|
|
2012-10-18 10:00:52 +00:00
|
|
|
int32_t ACMOpus::CodecDef(WebRtcNetEQ_CodecDef& /* codecDef */,
|
|
|
|
|
const CodecInst& /* codecInst */) {
|
|
|
|
|
return -1;
|
2011-07-07 08:21:25 +00:00
|
|
|
}
|
|
|
|
|
|
2012-10-18 10:00:52 +00:00
|
|
|
ACMGenericCodec* ACMOpus::CreateInstance(void) {
|
|
|
|
|
return NULL;
|
2011-07-07 08:21:25 +00:00
|
|
|
}
|
|
|
|
|
|
2012-10-18 10:00:52 +00:00
|
|
|
int16_t ACMOpus::InternalCreateEncoder() {
|
|
|
|
|
return -1;
|
2011-07-07 08:21:25 +00:00
|
|
|
}
|
|
|
|
|
|
2012-10-18 10:00:52 +00:00
|
|
|
void ACMOpus::DestructEncoderSafe() {
|
|
|
|
|
return;
|
2011-07-07 08:21:25 +00:00
|
|
|
}
|
|
|
|
|
|
2012-10-18 10:00:52 +00:00
|
|
|
int16_t ACMOpus::InternalCreateDecoder() {
|
|
|
|
|
return -1;
|
2011-07-07 08:21:25 +00:00
|
|
|
}
|
|
|
|
|
|
2012-10-18 10:00:52 +00:00
|
|
|
void ACMOpus::DestructDecoderSafe() {
|
|
|
|
|
return;
|
2011-07-07 08:21:25 +00:00
|
|
|
}
|
|
|
|
|
|
2012-10-18 10:00:52 +00:00
|
|
|
void ACMOpus::InternalDestructEncoderInst(void* /* ptrInst */) {
|
|
|
|
|
return;
|
2011-07-07 08:21:25 +00:00
|
|
|
}
|
|
|
|
|
|
2012-10-18 10:00:52 +00:00
|
|
|
int16_t ACMOpus::SetBitRateSafe(const int32_t /*rate*/) {
|
|
|
|
|
return -1;
|
2011-07-07 08:21:25 +00:00
|
|
|
}
|
|
|
|
|
|
2012-10-18 10:00:52 +00:00
|
|
|
#else //===================== Actual Implementation =======================
|
|
|
|
|
|
|
|
|
|
ACMOpus::ACMOpus(int16_t codecID)
|
2012-01-19 13:22:22 +00:00
|
|
|
: _encoderInstPtr(NULL),
|
|
|
|
|
_decoderInstPtr(NULL),
|
2012-10-18 10:00:52 +00:00
|
|
|
_sampleFreq(32000), // Default sampling frequency.
|
|
|
|
|
_bitrate(20000) { // Default bit-rate.
|
2012-01-19 13:22:22 +00:00
|
|
|
_codecID = codecID;
|
2012-10-18 10:00:52 +00:00
|
|
|
// Opus has internal DTX, but we dont use it for now.
|
2012-01-19 13:22:22 +00:00
|
|
|
_hasInternalDTX = false;
|
2011-07-07 08:21:25 +00:00
|
|
|
|
2012-10-18 10:00:52 +00:00
|
|
|
if (_codecID != ACMCodecDB::kOpus) {
|
|
|
|
|
WEBRTC_TRACE(webrtc::kTraceError, webrtc::kTraceAudioCoding, _uniqueID,
|
|
|
|
|
"Wrong codec id for Opus.");
|
|
|
|
|
_sampleFreq = -1;
|
|
|
|
|
_bitrate = -1;
|
|
|
|
|
}
|
2012-01-19 13:22:22 +00:00
|
|
|
return;
|
2011-07-07 08:21:25 +00:00
|
|
|
}
|
|
|
|
|
|
2012-10-18 10:00:52 +00:00
|
|
|
ACMOpus::~ACMOpus() {
|
|
|
|
|
if (_encoderInstPtr != NULL) {
|
|
|
|
|
WebRtcOpus_EncoderFree(_encoderInstPtr);
|
|
|
|
|
_encoderInstPtr = NULL;
|
|
|
|
|
}
|
|
|
|
|
if (_decoderInstPtr != NULL) {
|
|
|
|
|
WebRtcOpus_DecoderFree(_decoderInstPtr);
|
|
|
|
|
_decoderInstPtr = NULL;
|
|
|
|
|
}
|
|
|
|
|
return;
|
2011-07-07 08:21:25 +00:00
|
|
|
}
|
|
|
|
|
|
2012-10-18 10:00:52 +00:00
|
|
|
int16_t ACMOpus::InternalEncode(uint8_t* bitStream, int16_t* bitStreamLenByte) {
|
|
|
|
|
// Call Encoder.
|
|
|
|
|
*bitStreamLenByte = WebRtcOpus_Encode(_encoderInstPtr,
|
|
|
|
|
&_inAudio[_inAudioIxRead],
|
|
|
|
|
_frameLenSmpl,
|
|
|
|
|
MAX_PAYLOAD_SIZE_BYTE,
|
|
|
|
|
bitStream);
|
|
|
|
|
// Check for error reported from encoder.
|
|
|
|
|
if (*bitStreamLenByte < 0) {
|
|
|
|
|
WEBRTC_TRACE(webrtc::kTraceError, webrtc::kTraceAudioCoding, _uniqueID,
|
|
|
|
|
"InternalEncode: Encode error for Opus");
|
2011-07-07 08:21:25 +00:00
|
|
|
*bitStreamLenByte = 0;
|
2012-10-18 10:00:52 +00:00
|
|
|
return -1;
|
|
|
|
|
}
|
2011-07-07 08:21:25 +00:00
|
|
|
|
2012-10-18 10:00:52 +00:00
|
|
|
// Increment the read index. This tells the caller how far
|
|
|
|
|
// we have gone forward in reading the audio buffer.
|
|
|
|
|
_inAudioIxRead += _frameLenSmpl;
|
2011-07-07 08:21:25 +00:00
|
|
|
|
2012-10-18 10:00:52 +00:00
|
|
|
return *bitStreamLenByte;
|
2011-07-07 08:21:25 +00:00
|
|
|
}
|
|
|
|
|
|
2012-10-18 10:00:52 +00:00
|
|
|
int16_t ACMOpus::DecodeSafe(uint8_t* bitStream, int16_t bitStreamLenByte,
|
|
|
|
|
int16_t* audio, int16_t* audioSamples,
|
|
|
|
|
int8_t* speechType) {
|
|
|
|
|
return 0;
|
2011-07-07 08:21:25 +00:00
|
|
|
}
|
|
|
|
|
|
2012-10-18 10:00:52 +00:00
|
|
|
int16_t ACMOpus::InternalInitEncoder(WebRtcACMCodecParams* codecParams) {
|
|
|
|
|
int16_t ret;
|
|
|
|
|
if (_encoderInstPtr != NULL) {
|
|
|
|
|
WebRtcOpus_EncoderFree(_encoderInstPtr);
|
|
|
|
|
_encoderInstPtr = NULL;
|
|
|
|
|
}
|
|
|
|
|
ret = WebRtcOpus_EncoderCreate(&_encoderInstPtr,
|
|
|
|
|
codecParams->codecInstant.channels);
|
|
|
|
|
if (ret < 0) {
|
|
|
|
|
WEBRTC_TRACE(webrtc::kTraceError, webrtc::kTraceAudioCoding, _uniqueID,
|
|
|
|
|
"Encoder creation failed for Opus");
|
|
|
|
|
return ret;
|
|
|
|
|
}
|
|
|
|
|
ret = WebRtcOpus_SetBitRate(_encoderInstPtr, codecParams->codecInstant.rate);
|
|
|
|
|
if (ret < 0) {
|
|
|
|
|
WEBRTC_TRACE(webrtc::kTraceError, webrtc::kTraceAudioCoding, _uniqueID,
|
|
|
|
|
"Setting initial bitrate failed for Opus");
|
|
|
|
|
return ret;
|
|
|
|
|
}
|
|
|
|
|
return 0;
|
2011-07-07 08:21:25 +00:00
|
|
|
}
|
|
|
|
|
|
2012-10-18 10:00:52 +00:00
|
|
|
int16_t ACMOpus::InternalInitDecoder(WebRtcACMCodecParams* codecParams) {
|
|
|
|
|
if (_decoderInstPtr != NULL) {
|
|
|
|
|
WebRtcOpus_DecoderFree(_decoderInstPtr);
|
|
|
|
|
_decoderInstPtr = NULL;
|
|
|
|
|
}
|
|
|
|
|
if (WebRtcOpus_DecoderCreate(&_decoderInstPtr,
|
|
|
|
|
codecParams->codecInstant.channels) < 0) {
|
|
|
|
|
return -1;
|
|
|
|
|
}
|
|
|
|
|
return WebRtcOpus_DecoderInit(_decoderInstPtr);
|
2011-07-07 08:21:25 +00:00
|
|
|
}
|
|
|
|
|
|
2012-10-18 10:00:52 +00:00
|
|
|
int32_t ACMOpus::CodecDef(WebRtcNetEQ_CodecDef& codecDef,
|
|
|
|
|
const CodecInst& codecInst) {
|
|
|
|
|
if (!_decoderInitialized) {
|
|
|
|
|
WEBRTC_TRACE(webrtc::kTraceError, webrtc::kTraceAudioCoding, _uniqueID,
|
|
|
|
|
"CodeDef: Decoder uninitialized for Opus");
|
|
|
|
|
return -1;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Fill up the structure by calling "SET_CODEC_PAR" & "SET_OPUS_FUNCTION."
|
|
|
|
|
// Then call NetEQ to add the codec to its database.
|
|
|
|
|
// TODO(tlegrand): Decoder is registered in NetEQ as a 32 kHz decoder, which
|
|
|
|
|
// is true until we have a full 48 kHz system, and remove the downsampling
|
|
|
|
|
// in the Opus decoder wrapper.
|
|
|
|
|
SET_CODEC_PAR((codecDef), kDecoderOpus, codecInst.pltype, _decoderInstPtr,
|
|
|
|
|
32000);
|
|
|
|
|
SET_OPUS_FUNCTIONS((codecDef));
|
|
|
|
|
return 0;
|
2011-07-07 08:21:25 +00:00
|
|
|
}
|
|
|
|
|
|
2012-10-18 10:00:52 +00:00
|
|
|
ACMGenericCodec* ACMOpus::CreateInstance(void) {
|
|
|
|
|
return NULL;
|
2011-07-07 08:21:25 +00:00
|
|
|
}
|
|
|
|
|
|
2012-10-18 10:00:52 +00:00
|
|
|
int16_t ACMOpus::InternalCreateEncoder() {
|
|
|
|
|
// Real encoder will be created in InternalInitEncoder.
|
|
|
|
|
return 0;
|
2011-07-07 08:21:25 +00:00
|
|
|
}
|
|
|
|
|
|
2012-10-18 10:00:52 +00:00
|
|
|
void ACMOpus::DestructEncoderSafe() {
|
|
|
|
|
if (_encoderInstPtr) {
|
|
|
|
|
WebRtcOpus_EncoderFree(_encoderInstPtr);
|
|
|
|
|
_encoderInstPtr = NULL;
|
|
|
|
|
}
|
2011-07-07 08:21:25 +00:00
|
|
|
}
|
|
|
|
|
|
2012-10-18 10:00:52 +00:00
|
|
|
int16_t ACMOpus::InternalCreateDecoder() {
|
|
|
|
|
// Real decoder will be created in InternalInitDecoder
|
|
|
|
|
return 0;
|
2011-07-07 08:21:25 +00:00
|
|
|
}
|
|
|
|
|
|
2012-10-18 10:00:52 +00:00
|
|
|
void ACMOpus::DestructDecoderSafe() {
|
|
|
|
|
_decoderInitialized = false;
|
|
|
|
|
if (_decoderInstPtr) {
|
|
|
|
|
WebRtcOpus_DecoderFree(_decoderInstPtr);
|
|
|
|
|
_decoderInstPtr = NULL;
|
|
|
|
|
}
|
|
|
|
|
}
|
2011-07-07 08:21:25 +00:00
|
|
|
|
2012-10-18 10:00:52 +00:00
|
|
|
void ACMOpus::InternalDestructEncoderInst(void* ptrInst) {
|
|
|
|
|
if (ptrInst != NULL) {
|
|
|
|
|
WebRtcOpus_EncoderFree((OpusEncInst*) ptrInst);
|
|
|
|
|
}
|
|
|
|
|
return;
|
2011-07-07 08:21:25 +00:00
|
|
|
}
|
|
|
|
|
|
2012-10-18 10:00:52 +00:00
|
|
|
int16_t ACMOpus::SetBitRateSafe(const int32_t rate) {
|
|
|
|
|
if (rate < 6000 || rate > 510000) {
|
|
|
|
|
WEBRTC_TRACE(webrtc::kTraceError, webrtc::kTraceAudioCoding, _uniqueID,
|
|
|
|
|
"SetBitRateSafe: Invalid rate Opus");
|
|
|
|
|
return -1;
|
|
|
|
|
}
|
2011-07-07 08:21:25 +00:00
|
|
|
|
2012-10-18 10:00:52 +00:00
|
|
|
_bitrate = rate;
|
2011-07-07 08:21:25 +00:00
|
|
|
|
2012-10-18 10:00:52 +00:00
|
|
|
// Ask the encoder for the new rate.
|
|
|
|
|
if (WebRtcOpus_SetBitRate(_encoderInstPtr, _bitrate) >= 0) {
|
|
|
|
|
_encoderParams.codecInstant.rate = _bitrate;
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return -1;
|
2011-07-07 08:21:25 +00:00
|
|
|
}
|
|
|
|
|
|
2012-10-18 10:00:52 +00:00
|
|
|
#endif // WEBRTC_CODEC_OPUS
|
2011-07-07 08:21:25 +00:00
|
|
|
|
2012-10-18 10:00:52 +00:00
|
|
|
} // namespace webrtc
|