2 * Copyright (c) 2012 The WebRTC project authors. All Rights Reserved.
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.
11 #include "webrtc/modules/audio_coding/main/acm2/acm_g7221.h"
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"
21 // The API in the header file should match the one below.
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);
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);
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);
44 // int16_t WebRtcG7221_Encode16(G722_1_16_encinst_t_* enc_inst,
48 // int16_t WebRtcG7221_Encode24(G722_1_24_encinst_t_* enc_inst,
52 // int16_t WebRtcG7221_Encode32(G722_1_32_encinst_t_* enc_inst,
57 // int16_t WebRtcG7221_Decode16(G722_1_16_decinst_t_* dec_inst,
58 // int16_t* bitstream,
61 // int16_t WebRtcG7221_Decode24(G722_1_24_decinst_t_* dec_inst,
62 // int16_t* bitstream,
65 // int16_t WebRtcG7221_Decode32(G722_1_32_decinst_t_* dec_inst,
66 // int16_t* bitstream,
70 // int16_t WebRtcG7221_DecodePlc16(G722_1_16_decinst_t_* dec_inst,
72 // int16_t nr_lost_frames);
73 // int16_t WebRtcG7221_DecodePlc24(G722_1_24_decinst_t_* dec_inst,
75 // int16_t nr_lost_frames);
76 // int16_t WebRtcG7221_DecodePlc32(G722_1_32_decinst_t_* dec_inst,
78 // int16_t nr_lost_frames);
85 #ifndef WEBRTC_CODEC_G722_1
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) {
100 ACMG722_1::~ACMG722_1() { return; }
102 int16_t ACMG722_1::InternalEncode(uint8_t* /* bitstream */,
103 int16_t* /* bitstream_len_byte */) {
107 int16_t ACMG722_1::InternalInitEncoder(
108 WebRtcACMCodecParams* /* codec_params */) {
112 ACMGenericCodec* ACMG722_1::CreateInstance(void) { return NULL; }
114 int16_t ACMG722_1::InternalCreateEncoder() { return -1; }
116 void ACMG722_1::DestructEncoderSafe() { return; }
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;
136 operational_rate_ = -1;
141 ACMG722_1::~ACMG722_1() {
142 if (encoder_inst_ptr_ != NULL) {
143 delete encoder_inst_ptr_;
144 encoder_inst_ptr_ = NULL;
146 if (encoder_inst_ptr_right_ != NULL) {
147 delete encoder_inst_ptr_right_;
148 encoder_inst_ptr_right_ = NULL;
151 switch (operational_rate_) {
153 encoder_inst16_ptr_ = NULL;
154 encoder_inst16_ptr_right_ = NULL;
158 encoder_inst24_ptr_ = NULL;
159 encoder_inst24_ptr_right_ = NULL;
163 encoder_inst32_ptr_ = NULL;
164 encoder_inst32_ptr_right_ = NULL;
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];
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];
188 memcpy(left_channel, &in_audio_[in_audio_ix_read_], 320);
191 switch (operational_rate_) {
193 len_in_bytes = WebRtcG7221_Encode16(encoder_inst16_ptr_, left_channel,
195 if (num_channels_ == 2) {
196 len_in_bytes += WebRtcG7221_Encode16(encoder_inst16_ptr_right_,
198 &out_bits[len_in_bytes / 2]);
203 len_in_bytes = WebRtcG7221_Encode24(encoder_inst24_ptr_, left_channel,
205 if (num_channels_ == 2) {
206 len_in_bytes += WebRtcG7221_Encode24(encoder_inst24_ptr_right_,
208 &out_bits[len_in_bytes / 2]);
213 len_in_bytes = WebRtcG7221_Encode32(encoder_inst32_ptr_, left_channel,
215 if (num_channels_ == 2) {
216 len_in_bytes += WebRtcG7221_Encode32(encoder_inst32_ptr_right_,
218 &out_bits[len_in_bytes / 2]);
223 WEBRTC_TRACE(webrtc::kTraceError, webrtc::kTraceAudioCoding, unique_id_,
224 "InternalInitEncode: Wrong rate for G722_1.");
228 memcpy(bitstream, out_bits, len_in_bytes);
229 *bitstream_len_byte = len_in_bytes;
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;
237 int16_t ACMG722_1::InternalInitEncoder(WebRtcACMCodecParams* codec_params) {
240 switch (operational_rate_) {
242 ret = WebRtcG7221_EncoderInit16(encoder_inst16_ptr_right_);
246 return WebRtcG7221_EncoderInit16(encoder_inst16_ptr_);
249 ret = WebRtcG7221_EncoderInit24(encoder_inst24_ptr_right_);
253 return WebRtcG7221_EncoderInit24(encoder_inst24_ptr_);
256 ret = WebRtcG7221_EncoderInit32(encoder_inst32_ptr_right_);
260 return WebRtcG7221_EncoderInit32(encoder_inst32_ptr_);
263 WEBRTC_TRACE(webrtc::kTraceError, webrtc::kTraceAudioCoding,
264 unique_id_, "InternalInitEncoder: Wrong rate for G722_1.");
270 ACMGenericCodec* ACMG722_1::CreateInstance(void) { return NULL; }
272 int16_t ACMG722_1::InternalCreateEncoder() {
273 if ((encoder_inst_ptr_ == NULL) || (encoder_inst_ptr_right_ == NULL)) {
276 switch (operational_rate_) {
278 WebRtcG7221_CreateEnc16(&encoder_inst16_ptr_);
279 WebRtcG7221_CreateEnc16(&encoder_inst16_ptr_right_);
283 WebRtcG7221_CreateEnc24(&encoder_inst24_ptr_);
284 WebRtcG7221_CreateEnc24(&encoder_inst24_ptr_right_);
288 WebRtcG7221_CreateEnc32(&encoder_inst32_ptr_);
289 WebRtcG7221_CreateEnc32(&encoder_inst32_ptr_right_);
293 WEBRTC_TRACE(webrtc::kTraceError, webrtc::kTraceAudioCoding, unique_id_,
294 "InternalCreateEncoder: Wrong rate for G722_1.");
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;
308 if (encoder_inst_ptr_right_ != NULL) {
309 delete encoder_inst_ptr_right_;
310 encoder_inst_ptr_right_ = NULL;
312 encoder_inst16_ptr_ = NULL;
313 encoder_inst24_ptr_ = NULL;
314 encoder_inst32_ptr_ = NULL;
321 } // namespace webrtc