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_g7221c.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: 16.0 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_g7221c.h"
12
13#include "webrtc/modules/audio_coding/main/source/acm_codec_database.h"
14#include "webrtc/modules/audio_coding/main/acm2/acm_common_defs.h"
15#include "webrtc/modules/audio_coding/main/source/acm_neteq.h"
16#include "webrtc/modules/audio_coding/neteq/interface/webrtc_neteq.h"
17#include "webrtc/modules/audio_coding/neteq/interface/webrtc_neteq_help_macros.h"
18#include "webrtc/system_wrappers/interface/trace.h"
19
20#ifdef WEBRTC_CODEC_G722_1C
21// NOTE! G.722.1C is not included in the open-source package. The following
22// interface file is needed:
23//
24// /modules/audio_coding/codecs/g7221c/main/interface/g7221c_interface.h
25//
26// The API in the header file should match the one below.
27//
28
29// int16_t WebRtcG7221C_CreateEnc24(G722_1C_24_encinst_t_** enc_inst);
30// int16_t WebRtcG7221C_CreateEnc32(G722_1C_32_encinst_t_** enc_inst);
31// int16_t WebRtcG7221C_CreateEnc48(G722_1C_48_encinst_t_** enc_inst);
32// int16_t WebRtcG7221C_CreateDec24(G722_1C_24_decinst_t_** dec_inst);
33// int16_t WebRtcG7221C_CreateDec32(G722_1C_32_decinst_t_** dec_inst);
34// int16_t WebRtcG7221C_CreateDec48(G722_1C_48_decinst_t_** dec_inst);
35//
36// int16_t WebRtcG7221C_FreeEnc24(G722_1C_24_encinst_t_** enc_inst);
37// int16_t WebRtcG7221C_FreeEnc32(G722_1C_32_encinst_t_** enc_inst);
38// int16_t WebRtcG7221C_FreeEnc48(G722_1C_48_encinst_t_** enc_inst);
39// int16_t WebRtcG7221C_FreeDec24(G722_1C_24_decinst_t_** dec_inst);
40// int16_t WebRtcG7221C_FreeDec32(G722_1C_32_decinst_t_** dec_inst);
41// int16_t WebRtcG7221C_FreeDec48(G722_1C_48_decinst_t_** dec_inst);
42//
43// int16_t WebRtcG7221C_EncoderInit24(G722_1C_24_encinst_t_* enc_inst);
44// int16_t WebRtcG7221C_EncoderInit32(G722_1C_32_encinst_t_* enc_inst);
45// int16_t WebRtcG7221C_EncoderInit48(G722_1C_48_encinst_t_* enc_inst);
46// int16_t WebRtcG7221C_DecoderInit24(G722_1C_24_decinst_t_* dec_inst);
47// int16_t WebRtcG7221C_DecoderInit32(G722_1C_32_decinst_t_* dec_inst);
48// int16_t WebRtcG7221C_DecoderInit48(G722_1C_48_decinst_t_* dec_inst);
49//
50// int16_t WebRtcG7221C_Encode24(G722_1C_24_encinst_t_* enc_inst,
51//                               int16_t* input,
52//                               int16_t len,
53//                               int16_t* output);
54// int16_t WebRtcG7221C_Encode32(G722_1C_32_encinst_t_* enc_inst,
55//                               int16_t* input,
56//                               int16_t len,
57//                               int16_t* output);
58// int16_t WebRtcG7221C_Encode48(G722_1C_48_encinst_t_* enc_inst,
59//                               int16_t* input,
60//                               int16_t len,
61//                               int16_t* output);
62//
63// int16_t WebRtcG7221C_Decode24(G722_1C_24_decinst_t_* dec_inst,
64//                               int16_t* bitstream,
65//                               int16_t len,
66//                               int16_t* output);
67// int16_t WebRtcG7221C_Decode32(G722_1C_32_decinst_t_* dec_inst,
68//                               int16_t* bitstream,
69//                               int16_t len,
70//                               int16_t* output);
71// int16_t WebRtcG7221C_Decode48(G722_1C_48_decinst_t_* dec_inst,
72//                               int16_t* bitstream,
73//                               int16_t len,
74//                               int16_t* output);
75//
76// int16_t WebRtcG7221C_DecodePlc24(G722_1C_24_decinst_t_* dec_inst,
77//                                  int16_t* output,
78//                                  int16_t nr_lost_frames);
79// int16_t WebRtcG7221C_DecodePlc32(G722_1C_32_decinst_t_* dec_inst,
80//                                  int16_t* output,
81//                                  int16_t nr_lost_frames);
82// int16_t WebRtcG7221C_DecodePlc48(G722_1C_48_decinst_t_* dec_inst,
83//                                  int16_t* output,
84//                                  int16_t nr_lost_frames);
85#include "g7221c_interface.h"
86#endif
87
88namespace webrtc {
89
90namespace acm1 {
91
92#ifndef WEBRTC_CODEC_G722_1C
93
94ACMG722_1C::ACMG722_1C(int16_t /* codec_id */)
95    : operational_rate_(-1),
96      encoder_inst_ptr_(NULL),
97      encoder_inst_ptr_right_(NULL),
98      decoder_inst_ptr_(NULL),
99      encoder_inst24_ptr_(NULL),
100      encoder_inst24_ptr_right_(NULL),
101      encoder_inst32_ptr_(NULL),
102      encoder_inst32_ptr_right_(NULL),
103      encoder_inst48_ptr_(NULL),
104      encoder_inst48_ptr_right_(NULL),
105      decoder_inst24_ptr_(NULL),
106      decoder_inst32_ptr_(NULL),
107      decoder_inst48_ptr_(NULL) {
108  return;
109}
110
111ACMG722_1C::~ACMG722_1C() {
112  return;
113}
114
115int16_t ACMG722_1C::InternalEncode(
116    uint8_t* /* bitstream */,
117    int16_t* /* bitstream_len_byte */) {
118  return -1;
119}
120
121int16_t ACMG722_1C::DecodeSafe(uint8_t* /* bitstream */,
122                               int16_t /* bitstream_len_byte */,
123                               int16_t* /* audio */,
124                               int16_t* /* audio_samples */,
125                               int8_t* /* speech_type */) {
126  return -1;
127}
128
129int16_t ACMG722_1C::InternalInitEncoder(
130    WebRtcACMCodecParams* /* codec_params */) {
131  return -1;
132}
133
134int16_t ACMG722_1C::InternalInitDecoder(
135    WebRtcACMCodecParams* /* codec_params */) {
136  return -1;
137}
138
139int32_t ACMG722_1C::CodecDef(WebRtcNetEQ_CodecDef& /* codec_def */,
140                             const CodecInst& /* codec_inst */) {
141  return -1;
142}
143
144ACMGenericCodec* ACMG722_1C::CreateInstance(void) {
145  return NULL;
146}
147
148int16_t ACMG722_1C::InternalCreateEncoder() {
149  return -1;
150}
151
152void ACMG722_1C::DestructEncoderSafe() {
153  return;
154}
155
156int16_t ACMG722_1C::InternalCreateDecoder() {
157  return -1;
158}
159
160void ACMG722_1C::DestructDecoderSafe() {
161  return;
162}
163
164void ACMG722_1C::InternalDestructEncoderInst(void* /* ptr_inst */) {
165  return;
166}
167
168#else     //===================== Actual Implementation =======================
169ACMG722_1C::ACMG722_1C(int16_t codec_id)
170    : encoder_inst_ptr_(NULL),
171      encoder_inst_ptr_right_(NULL),
172      decoder_inst_ptr_(NULL),
173      encoder_inst24_ptr_(NULL),
174      encoder_inst24_ptr_right_(NULL),
175      encoder_inst32_ptr_(NULL),
176      encoder_inst32_ptr_right_(NULL),
177      encoder_inst48_ptr_(NULL),
178      encoder_inst48_ptr_right_(NULL),
179      decoder_inst24_ptr_(NULL),
180      decoder_inst32_ptr_(NULL),
181      decoder_inst48_ptr_(NULL) {
182  codec_id_ = codec_id;
183  if (codec_id_ == ACMCodecDB::kG722_1C_24) {
184    operational_rate_ = 24000;
185  } else if (codec_id_ == ACMCodecDB::kG722_1C_32) {
186    operational_rate_ = 32000;
187  } else if (codec_id_ == ACMCodecDB::kG722_1C_48) {
188    operational_rate_ = 48000;
189  } else {
190    WEBRTC_TRACE(webrtc::kTraceError, webrtc::kTraceAudioCoding, unique_id_,
191                 "Wrong codec id for G722_1c.");
192    operational_rate_ = -1;
193  }
194  return;
195}
196
197ACMG722_1C::~ACMG722_1C() {
198  if (encoder_inst_ptr_ != NULL) {
199    delete encoder_inst_ptr_;
200    encoder_inst_ptr_ = NULL;
201  }
202  if (encoder_inst_ptr_right_ != NULL) {
203    delete encoder_inst_ptr_right_;
204    encoder_inst_ptr_right_ = NULL;
205  }
206  if (decoder_inst_ptr_ != NULL) {
207    delete decoder_inst_ptr_;
208    decoder_inst_ptr_ = NULL;
209  }
210
211  switch (operational_rate_) {
212    case 24000: {
213      encoder_inst24_ptr_ = NULL;
214      encoder_inst24_ptr_right_ = NULL;
215      decoder_inst24_ptr_ = NULL;
216      break;
217    }
218    case 32000: {
219      encoder_inst32_ptr_ = NULL;
220      encoder_inst32_ptr_right_ = NULL;
221      decoder_inst32_ptr_ = NULL;
222      break;
223    }
224    case 48000: {
225      encoder_inst48_ptr_ = NULL;
226      encoder_inst48_ptr_right_ = NULL;
227      decoder_inst48_ptr_ = NULL;
228      break;
229    }
230    default: {
231      WEBRTC_TRACE(webrtc::kTraceError, webrtc::kTraceAudioCoding, unique_id_,
232                   "Wrong rate for G722_1c.");
233      break;
234    }
235  }
236  return;
237}
238
239int16_t ACMG722_1C::InternalEncode(uint8_t* bitstream,
240                                   int16_t* bitstream_len_byte) {
241  int16_t left_channel[640];
242  int16_t right_channel[640];
243  int16_t len_in_bytes;
244  int16_t out_bits[240];
245
246  // If stereo, split input signal in left and right channel before encoding
247  if (num_channels_ == 2) {
248    for (int i = 0, j = 0; i < frame_len_smpl_ * 2; i += 2, j++) {
249      left_channel[j] = in_audio_[in_audio_ix_read_ + i];
250      right_channel[j] = in_audio_[in_audio_ix_read_ + i + 1];
251    }
252  } else {
253    memcpy(left_channel, &in_audio_[in_audio_ix_read_], 640);
254  }
255
256  switch (operational_rate_) {
257    case 24000: {
258      len_in_bytes = WebRtcG7221C_Encode24(encoder_inst24_ptr_, left_channel,
259                                           640, &out_bits[0]);
260      if (num_channels_ == 2) {
261        len_in_bytes += WebRtcG7221C_Encode24(encoder_inst24_ptr_right_,
262                                              right_channel, 640,
263                                              &out_bits[len_in_bytes / 2]);
264      }
265      break;
266    }
267    case 32000: {
268      len_in_bytes = WebRtcG7221C_Encode32(encoder_inst32_ptr_, left_channel,
269                                           640, &out_bits[0]);
270      if (num_channels_ == 2) {
271        len_in_bytes += WebRtcG7221C_Encode32(encoder_inst32_ptr_right_,
272                                              right_channel, 640,
273                                              &out_bits[len_in_bytes / 2]);
274      }
275      break;
276    }
277    case 48000: {
278      len_in_bytes = WebRtcG7221C_Encode48(encoder_inst48_ptr_, left_channel,
279                                           640, &out_bits[0]);
280      if (num_channels_ == 2) {
281        len_in_bytes += WebRtcG7221C_Encode48(encoder_inst48_ptr_right_,
282                                              right_channel, 640,
283                                              &out_bits[len_in_bytes / 2]);
284      }
285      break;
286    }
287    default: {
288      WEBRTC_TRACE(webrtc::kTraceError, webrtc::kTraceAudioCoding, unique_id_,
289                   "InternalEncode: Wrong rate for G722_1c.");
290      return -1;
291    }
292  }
293
294  memcpy(bitstream, out_bits, len_in_bytes);
295  *bitstream_len_byte = len_in_bytes;
296
297  // increment the read index this tell the caller that how far
298  // we have gone forward in reading the audio buffer
299  in_audio_ix_read_ += 640 * num_channels_;
300
301  return *bitstream_len_byte;
302}
303
304int16_t ACMG722_1C::DecodeSafe(uint8_t* /* bitstream */,
305                               int16_t /* bitstream_len_byte */,
306                               int16_t* /* audio */,
307                               int16_t* /* audio_samples */,
308                               int8_t* /* speech_type */) {
309  return 0;
310}
311
312int16_t ACMG722_1C::InternalInitEncoder(
313    WebRtcACMCodecParams* codec_params) {
314  int16_t ret;
315
316  switch (operational_rate_) {
317    case 24000: {
318      ret = WebRtcG7221C_EncoderInit24(encoder_inst24_ptr_right_);
319      if (ret < 0) {
320        return ret;
321      }
322      return WebRtcG7221C_EncoderInit24(encoder_inst24_ptr_);
323    }
324    case 32000: {
325      ret = WebRtcG7221C_EncoderInit32(encoder_inst32_ptr_right_);
326      if (ret < 0) {
327        return ret;
328      }
329      return WebRtcG7221C_EncoderInit32(encoder_inst32_ptr_);
330    }
331    case 48000: {
332      ret = WebRtcG7221C_EncoderInit48(encoder_inst48_ptr_right_);
333      if (ret < 0) {
334        return ret;
335      }
336      return WebRtcG7221C_EncoderInit48(encoder_inst48_ptr_);
337    }
338    default: {
339      WEBRTC_TRACE(webrtc::kTraceError, webrtc::kTraceAudioCoding, unique_id_,
340                   "InternalInitEncode: Wrong rate for G722_1c.");
341      return -1;
342    }
343  }
344}
345
346int16_t ACMG722_1C::InternalInitDecoder(
347    WebRtcACMCodecParams* /* codec_params */) {
348  switch (operational_rate_) {
349    case 24000: {
350      return WebRtcG7221C_DecoderInit24(decoder_inst24_ptr_);
351    }
352    case 32000: {
353      return WebRtcG7221C_DecoderInit32(decoder_inst32_ptr_);
354    }
355    case 48000: {
356      return WebRtcG7221C_DecoderInit48(decoder_inst48_ptr_);
357    }
358    default: {
359      WEBRTC_TRACE(webrtc::kTraceError, webrtc::kTraceAudioCoding, unique_id_,
360                   "InternalInitDecoder: Wrong rate for G722_1c.");
361      return -1;
362    }
363  }
364}
365
366int32_t ACMG722_1C::CodecDef(WebRtcNetEQ_CodecDef& codec_def,
367                             const CodecInst& codec_inst) {
368  if (!decoder_initialized_) {
369    WEBRTC_TRACE(webrtc::kTraceError, webrtc::kTraceAudioCoding, unique_id_,
370                 "CodeDef: decoder not initialized for G722_1c");
371    return -1;
372  }
373  // NetEq has an array of pointers to WebRtcNetEQ_CodecDef.
374  // get an entry of that array (neteq wrapper will allocate memory)
375  // by calling "netEq->CodecDef", where "NETEQ_CODEC_G722_1_XX" would
376  // be the index of the entry.
377  // Fill up the given structure by calling
378  // "SET_CODEC_PAR" & "SET_G722_1_XX_FUNCTION."
379  // Then return the structure back to NetEQ to add the codec to it's
380  // database.
381  switch (operational_rate_) {
382    case 24000: {
383      SET_CODEC_PAR((codec_def), kDecoderG722_1C_24, codec_inst.pltype,
384                    decoder_inst24_ptr_, 32000);
385      SET_G722_1C_24_FUNCTIONS((codec_def));
386      break;
387    }
388    case 32000: {
389      SET_CODEC_PAR((codec_def), kDecoderG722_1C_32, codec_inst.pltype,
390                    decoder_inst32_ptr_, 32000);
391      SET_G722_1C_32_FUNCTIONS((codec_def));
392      break;
393    }
394    case 48000: {
395      SET_CODEC_PAR((codec_def), kDecoderG722_1C_32, codec_inst.pltype,
396                    decoder_inst48_ptr_, 32000);
397      SET_G722_1C_48_FUNCTIONS((codec_def));
398      break;
399    }
400    default: {
401      WEBRTC_TRACE(webrtc::kTraceError, webrtc::kTraceAudioCoding, unique_id_,
402                   "CodeDef: Wrong rate for G722_1c.");
403      return -1;
404    }
405  }
406  return 0;
407}
408
409ACMGenericCodec*
410ACMG722_1C::CreateInstance(void) {
411  return NULL;
412}
413
414int16_t ACMG722_1C::InternalCreateEncoder() {
415  if ((encoder_inst_ptr_ == NULL) || (encoder_inst_ptr_right_ == NULL)) {
416    return -1;
417  }
418  switch (operational_rate_) {
419    case 24000: {
420      WebRtcG7221C_CreateEnc24(&encoder_inst24_ptr_);
421      WebRtcG7221C_CreateEnc24(&encoder_inst24_ptr_right_);
422      break;
423    }
424    case 32000: {
425      WebRtcG7221C_CreateEnc32(&encoder_inst32_ptr_);
426      WebRtcG7221C_CreateEnc32(&encoder_inst32_ptr_right_);
427      break;
428    }
429    case 48000: {
430      WebRtcG7221C_CreateEnc48(&encoder_inst48_ptr_);
431      WebRtcG7221C_CreateEnc48(&encoder_inst48_ptr_right_);
432      break;
433    }
434    default: {
435      WEBRTC_TRACE(webrtc::kTraceError, webrtc::kTraceAudioCoding, unique_id_,
436                   "InternalCreateEncoder: Wrong rate for G722_1c.");
437      return -1;
438    }
439  }
440  return 0;
441}
442
443void ACMG722_1C::DestructEncoderSafe() {
444  encoder_exist_ = false;
445  encoder_initialized_ = false;
446  if (encoder_inst_ptr_ != NULL) {
447    delete encoder_inst_ptr_;
448    encoder_inst_ptr_ = NULL;
449  }
450  if (encoder_inst_ptr_right_ != NULL) {
451    delete encoder_inst_ptr_right_;
452    encoder_inst_ptr_right_ = NULL;
453  }
454  encoder_inst24_ptr_ = NULL;
455  encoder_inst32_ptr_ = NULL;
456  encoder_inst48_ptr_ = NULL;
457}
458
459int16_t ACMG722_1C::InternalCreateDecoder() {
460  if (decoder_inst_ptr_ == NULL) {
461    WEBRTC_TRACE(webrtc::kTraceError, webrtc::kTraceAudioCoding, unique_id_,
462                 "InternalCreateEncoder: cannot create decoder");
463    return -1;
464  }
465  switch (operational_rate_) {
466    case 24000: {
467      WebRtcG7221C_CreateDec24(&decoder_inst24_ptr_);
468      break;
469    }
470    case 32000: {
471      WebRtcG7221C_CreateDec32(&decoder_inst32_ptr_);
472      break;
473    }
474    case 48000: {
475      WebRtcG7221C_CreateDec48(&decoder_inst48_ptr_);
476      break;
477    }
478    default: {
479      WEBRTC_TRACE(webrtc::kTraceError, webrtc::kTraceAudioCoding, unique_id_,
480                   "InternalCreateEncoder: Wrong rate for G722_1c.");
481      return -1;
482    }
483  }
484  return 0;
485}
486
487void ACMG722_1C::DestructDecoderSafe() {
488  decoder_exist_ = false;
489  decoder_initialized_ = false;
490  if (decoder_inst_ptr_ != NULL) {
491    delete decoder_inst_ptr_;
492    decoder_inst_ptr_ = NULL;
493  }
494  decoder_inst24_ptr_ = NULL;
495  decoder_inst32_ptr_ = NULL;
496  decoder_inst48_ptr_ = NULL;
497}
498
499void ACMG722_1C::InternalDestructEncoderInst(void* ptr_inst) {
500  if (ptr_inst != NULL) {
501    delete ptr_inst;
502  }
503  return;
504}
505
506#endif
507
508}  // namespace acm1
509
510}  // namespace webrtc
Note: See TracBrowser for help on using the repository browser.