Note: We no longer publish the latest version of our code here. We primarily use a kumc-bmi github organization. The heron ETL repository, in particular, is not public. Peers in the informatics community should see MultiSiteDev for details on requesting access.

source: webrtc/webrtc/modules/audio_coding/main/source/acm_g722.cc @ 0:4bda6873e34c

pub_scrub_3792 tip
Last change on this file since 0:4bda6873e34c was 0:4bda6873e34c, checked in by Michael Prittie <mprittie@…>, 6 years ago

Scrubbed password for publication.

File size: 10.7 KB
Line 
1/*
2 *  Copyright (c) 2012 The WebRTC project authors. All Rights Reserved.
3 *
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.
9 */
10
11#include "webrtc/modules/audio_coding/main/source/acm_g722.h"
12
13#include "webrtc/modules/audio_coding/codecs/g722/include/g722_interface.h"
14#include "webrtc/modules/audio_coding/main/source/acm_codec_database.h"
15#include "webrtc/modules/audio_coding/main/acm2/acm_common_defs.h"
16#include "webrtc/modules/audio_coding/main/source/acm_neteq.h"
17#include "webrtc/modules/audio_coding/neteq/interface/webrtc_neteq.h"
18#include "webrtc/modules/audio_coding/neteq/interface/webrtc_neteq_help_macros.h"
19#include "webrtc/system_wrappers/interface/trace.h"
20
21namespace webrtc {
22
23namespace acm1 {
24
25#ifndef WEBRTC_CODEC_G722
26
27ACMG722::ACMG722(int16_t /* codec_id */)
28    : ptr_enc_str_(NULL),
29      ptr_dec_str_(NULL),
30      encoder_inst_ptr_(NULL),
31      encoder_inst_ptr_right_(NULL),
32      decoder_inst_ptr_(NULL) {}
33
34ACMG722::~ACMG722() {}
35
36int32_t ACMG722::Add10MsDataSafe(
37    const uint32_t /* timestamp */,
38    const int16_t* /* data */,
39    const uint16_t /* length_smpl */,
40    const uint8_t /* audio_channel */) {
41  return -1;
42}
43
44int16_t ACMG722::InternalEncode(
45    uint8_t* /* bitstream */,
46    int16_t* /* bitstream_len_byte */) {
47  return -1;
48}
49
50int16_t ACMG722::DecodeSafe(uint8_t* /* bitstream */,
51                            int16_t /* bitstream_len_byte */,
52                            int16_t* /* audio */,
53                            int16_t* /* audio_samples */,
54                            int8_t* /* speech_type */) {
55  return -1;
56}
57
58int16_t ACMG722::InternalInitEncoder(
59    WebRtcACMCodecParams* /* codec_params */) {
60  return -1;
61}
62
63int16_t ACMG722::InternalInitDecoder(
64    WebRtcACMCodecParams* /* codec_params */) {
65  return -1;
66}
67
68int32_t ACMG722::CodecDef(WebRtcNetEQ_CodecDef& /* codec_def */,
69                          const CodecInst& /* codec_inst */) {
70  return -1;
71}
72
73ACMGenericCodec* ACMG722::CreateInstance(void) {
74  return NULL;
75}
76
77int16_t ACMG722::InternalCreateEncoder() {
78  return -1;
79}
80
81void ACMG722::DestructEncoderSafe() {
82  return;
83}
84
85int16_t ACMG722::InternalCreateDecoder() {
86  return -1;
87}
88
89void ACMG722::DestructDecoderSafe() {
90  return;
91}
92
93void ACMG722::InternalDestructEncoderInst(void* /* ptr_inst */) {
94  return;
95}
96
97void ACMG722::SplitStereoPacket(uint8_t* /*payload*/,
98                                int32_t* /*payload_length*/) {}
99
100#else     //===================== Actual Implementation =======================
101
102// Encoder and decoder memory
103struct ACMG722EncStr {
104  G722EncInst* inst;  // instance for left channel in case of stereo
105  G722EncInst* inst_right;  // instance for right channel in case of stereo
106};
107struct ACMG722DecStr {
108  G722DecInst* inst;  // instance for left channel in case of stereo
109  G722DecInst* inst_right;  // instance for right channel in case of stereo
110};
111
112ACMG722::ACMG722(int16_t codec_id)
113    : encoder_inst_ptr_(NULL),
114      encoder_inst_ptr_right_(NULL),
115      decoder_inst_ptr_(NULL) {
116  // Encoder
117  ptr_enc_str_ = new ACMG722EncStr;
118  if (ptr_enc_str_ != NULL) {
119    ptr_enc_str_->inst = NULL;
120    ptr_enc_str_->inst_right = NULL;
121  }
122  // Decoder
123  ptr_dec_str_ = new ACMG722DecStr;
124  if (ptr_dec_str_ != NULL) {
125    ptr_dec_str_->inst = NULL;
126    ptr_dec_str_->inst_right = NULL;  // Not used
127  }
128  codec_id_ = codec_id;
129  return;
130}
131
132ACMG722::~ACMG722() {
133  // Encoder
134  if (ptr_enc_str_ != NULL) {
135    if (ptr_enc_str_->inst != NULL) {
136      WebRtcG722_FreeEncoder(ptr_enc_str_->inst);
137      ptr_enc_str_->inst = NULL;
138    }
139    if (ptr_enc_str_->inst_right != NULL) {
140      WebRtcG722_FreeEncoder(ptr_enc_str_->inst_right);
141      ptr_enc_str_->inst_right = NULL;
142    }
143    delete ptr_enc_str_;
144    ptr_enc_str_ = NULL;
145  }
146  // Decoder
147  if (ptr_dec_str_ != NULL) {
148    if (ptr_dec_str_->inst != NULL) {
149      WebRtcG722_FreeDecoder(ptr_dec_str_->inst);
150      ptr_dec_str_->inst = NULL;
151    }
152    if (ptr_dec_str_->inst_right != NULL) {
153      WebRtcG722_FreeDecoder(ptr_dec_str_->inst_right);
154      ptr_dec_str_->inst_right = NULL;
155    }
156    delete ptr_dec_str_;
157    ptr_dec_str_ = NULL;
158  }
159  return;
160}
161
162int32_t ACMG722::Add10MsDataSafe(const uint32_t timestamp,
163                                 const int16_t* data,
164                                 const uint16_t length_smpl,
165                                 const uint8_t audio_channel) {
166  return ACMGenericCodec::Add10MsDataSafe((timestamp >> 1), data, length_smpl,
167                                          audio_channel);
168}
169
170int16_t ACMG722::InternalEncode(uint8_t* bitstream,
171                                int16_t* bitstream_len_byte) {
172  // If stereo, split input signal in left and right channel before encoding
173  if (num_channels_ == 2) {
174    int16_t left_channel[960];
175    int16_t right_channel[960];
176    uint8_t out_left[480];
177    uint8_t out_right[480];
178    int16_t len_in_bytes;
179    for (int i = 0, j = 0; i < frame_len_smpl_ * 2; i += 2, j++) {
180      left_channel[j] = in_audio_[in_audio_ix_read_ + i];
181      right_channel[j] = in_audio_[in_audio_ix_read_ + i + 1];
182    }
183    len_in_bytes = WebRtcG722_Encode(encoder_inst_ptr_, left_channel,
184                                     frame_len_smpl_,
185                                     (int16_t*)out_left);
186    len_in_bytes += WebRtcG722_Encode(encoder_inst_ptr_right_, right_channel,
187                                      frame_len_smpl_,
188                                      (int16_t*)out_right);
189    *bitstream_len_byte = len_in_bytes;
190
191    // Interleave the 4 bits per sample from left and right channel
192    for (int i = 0, j = 0; i < len_in_bytes; i += 2, j++) {
193      bitstream[i] = (out_left[j] & 0xF0) + (out_right[j] >> 4);
194      bitstream[i + 1] = ((out_left[j] & 0x0F) << 4) + (out_right[j] & 0x0F);
195    }
196  } else {
197    *bitstream_len_byte = WebRtcG722_Encode(encoder_inst_ptr_,
198                                            &in_audio_[in_audio_ix_read_],
199                                            frame_len_smpl_,
200                                            (int16_t*)bitstream);
201  }
202
203  // increment the read index this tell the caller how far
204  // we have gone forward in reading the audio buffer
205  in_audio_ix_read_ += frame_len_smpl_ * num_channels_;
206  return *bitstream_len_byte;
207}
208
209int16_t ACMG722::DecodeSafe(uint8_t* /* bitstream */,
210                            int16_t /* bitstream_len_byte */,
211                            int16_t* /* audio */,
212                            int16_t* /* audio_samples */,
213                            int8_t* /* speech_type */) {
214  return 0;
215}
216
217int16_t ACMG722::InternalInitEncoder(WebRtcACMCodecParams* codec_params) {
218  if (codec_params->codec_inst.channels == 2) {
219    // Create codec struct for right channel
220    if (ptr_enc_str_->inst_right == NULL) {
221      WebRtcG722_CreateEncoder(&ptr_enc_str_->inst_right);
222      if (ptr_enc_str_->inst_right == NULL) {
223        return -1;
224      }
225    }
226    encoder_inst_ptr_right_ = ptr_enc_str_->inst_right;
227    if (WebRtcG722_EncoderInit(encoder_inst_ptr_right_) < 0) {
228      return -1;
229    }
230  }
231
232  return WebRtcG722_EncoderInit(encoder_inst_ptr_);
233}
234
235int16_t ACMG722::InternalInitDecoder(
236    WebRtcACMCodecParams* /* codec_params */) {
237  return WebRtcG722_DecoderInit(decoder_inst_ptr_);
238}
239
240int32_t ACMG722::CodecDef(WebRtcNetEQ_CodecDef& codec_def,
241                          const CodecInst& codec_inst) {
242  if (!decoder_initialized_) {
243    // TODO(turajs): log error
244    return -1;
245  }
246  // Fill up the structure by calling
247  // "SET_CODEC_PAR" & "SET_G722_FUNCTION."
248  // Then call NetEQ to add the codec to it's
249  // database.
250  if (codec_inst.channels == 1) {
251    SET_CODEC_PAR(codec_def, kDecoderG722, codec_inst.pltype, decoder_inst_ptr_,
252                  16000);
253  } else {
254    SET_CODEC_PAR(codec_def, kDecoderG722_2ch, codec_inst.pltype,
255                  decoder_inst_ptr_, 16000);
256  }
257  SET_G722_FUNCTIONS(codec_def);
258  return 0;
259}
260
261ACMGenericCodec* ACMG722::CreateInstance(void) {
262  return NULL;
263}
264
265int16_t ACMG722::InternalCreateEncoder() {
266  if (ptr_enc_str_ == NULL) {
267    // this structure must be created at the costructor
268    // if it is still NULL then there is a probelm and
269    // we dont continue
270    return -1;
271  }
272  WebRtcG722_CreateEncoder(&ptr_enc_str_->inst);
273  if (ptr_enc_str_->inst == NULL) {
274    return -1;
275  }
276  encoder_inst_ptr_ = ptr_enc_str_->inst;
277  return 0;
278}
279
280void ACMG722::DestructEncoderSafe() {
281  if (ptr_enc_str_ != NULL) {
282    if (ptr_enc_str_->inst != NULL) {
283      WebRtcG722_FreeEncoder(ptr_enc_str_->inst);
284      ptr_enc_str_->inst = NULL;
285    }
286  }
287  encoder_exist_ = false;
288  encoder_initialized_ = false;
289}
290
291int16_t ACMG722::InternalCreateDecoder() {
292  if (ptr_dec_str_ == NULL) {
293    // this structure must be created at the costructor
294    // if it is still NULL then there is a probelm and
295    // we dont continue
296    return -1;
297  }
298
299  WebRtcG722_CreateDecoder(&ptr_dec_str_->inst);
300  if (ptr_dec_str_->inst == NULL) {
301    return -1;
302  }
303  decoder_inst_ptr_ = ptr_dec_str_->inst;
304  return 0;
305}
306
307void ACMG722::DestructDecoderSafe() {
308  decoder_exist_ = false;
309  decoder_initialized_ = false;
310  if (ptr_dec_str_ != NULL) {
311    if (ptr_dec_str_->inst != NULL) {
312      WebRtcG722_FreeDecoder(ptr_dec_str_->inst);
313      ptr_dec_str_->inst = NULL;
314    }
315  }
316}
317
318void ACMG722::InternalDestructEncoderInst(void* ptr_inst) {
319  if (ptr_inst != NULL) {
320    WebRtcG722_FreeEncoder(static_cast<G722EncInst*>(ptr_inst));
321  }
322  return;
323}
324
325// Split the stereo packet and place left and right channel after each other
326// in the payload vector.
327void ACMG722::SplitStereoPacket(uint8_t* payload, int32_t* payload_length) {
328  uint8_t right_byte;
329
330  // Check for valid inputs.
331  assert(payload != NULL);
332  assert(*payload_length > 0);
333
334  // Regroup the 4 bits/sample so to |l1 l2| |r1 r2| |l3 l4| |r3 r4| ...,
335  // where "lx" is 4 bits representing left sample number x, and "rx" right
336  // sample. Two samples fits in one byte, represented with |...|.
337  for (int i = 0; i < *payload_length; i += 2) {
338    right_byte = ((payload[i] & 0x0F) << 4) + (payload[i + 1] & 0x0F);
339    payload[i] = (payload[i] & 0xF0) + (payload[i + 1] >> 4);
340    payload[i + 1] = right_byte;
341  }
342
343  // Move one byte representing right channel each loop, and place it at the
344  // end of the bytestream vector. After looping the data is reordered to:
345  // |l1 l2| |l3 l4| ... |l(N-1) lN| |r1 r2| |r3 r4| ... |r(N-1) r(N)|,
346  // where N is the total number of samples.
347  for (int i = 0; i < *payload_length / 2; i++) {
348    right_byte = payload[i + 1];
349    memmove(&payload[i + 1], &payload[i + 2], *payload_length - i - 2);
350    payload[*payload_length - 1] = right_byte;
351  }
352}
353
354#endif
355
356}  // namespace acm1
357
358}  // namespace webrtc
Note: See TracBrowser for help on using the repository browser.