blob: 21fa3a9d0d8f4f0725ea4d68cb73526955c1b748 [file] [log] [blame]
turaj@webrtc.org3f39c002013-09-12 18:30:261/*
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
turaj@webrtc.orged0b4fb2013-09-13 23:06:5911#include "webrtc/modules/audio_coding/main/acm2/acm_celt.h"
turaj@webrtc.org3f39c002013-09-12 18:30:2612
13#ifdef WEBRTC_CODEC_CELT
14// NOTE! Celt is not included in the open-source package. Modify this file or
15// your codec API to match the function call and name of used CELT API file.
16#include "webrtc/modules/audio_coding/codecs/celt/include/celt_interface.h"
turaj@webrtc.orged0b4fb2013-09-13 23:06:5917#include "webrtc/modules/audio_coding/main/acm2/acm_common_defs.h"
turaj@webrtc.org3f39c002013-09-12 18:30:2618#include "webrtc/system_wrappers/interface/trace.h"
19#endif
20
21namespace webrtc {
22
23#ifndef WEBRTC_CODEC_CELT
24
25ACMCELT::ACMCELT(int16_t /* codec_id */)
26 : enc_inst_ptr_(NULL),
27 sampling_freq_(0),
28 bitrate_(0),
29 channels_(1) {
30 return;
31}
32
33ACMCELT::~ACMCELT() {
34 return;
35}
36
37int16_t ACMCELT::InternalEncode(uint8_t* /* bitstream */,
38 int16_t* /* bitstream_len_byte */) {
39 return -1;
40}
41
42int16_t ACMCELT::InternalInitEncoder(WebRtcACMCodecParams* /* codec_params */) {
43 return -1;
44}
45
46ACMGenericCodec* ACMCELT::CreateInstance(void) {
47 return NULL;
48}
49
50int16_t ACMCELT::InternalCreateEncoder() {
51 return -1;
52}
53
54void ACMCELT::DestructEncoderSafe() {
55 return;
56}
57
58void ACMCELT::InternalDestructEncoderInst(void* /* ptr_inst */) {
59 return;
60}
61
62int16_t ACMCELT::SetBitRateSafe(const int32_t /*rate*/) {
63 return -1;
64}
65
66#else //===================== Actual Implementation =======================
67
68ACMCELT::ACMCELT(int16_t codec_id)
69 : enc_inst_ptr_(NULL),
70 sampling_freq_(32000), // Default sampling frequency.
71 bitrate_(64000), // Default rate.
72 channels_(1) { // Default send mono.
73 // TODO(tlegrand): remove later when ACMGenericCodec has a new constructor.
74 codec_id_ = codec_id;
75
76 return;
77}
78
79ACMCELT::~ACMCELT() {
80 if (enc_inst_ptr_ != NULL) {
81 WebRtcCelt_FreeEnc(enc_inst_ptr_);
82 enc_inst_ptr_ = NULL;
83 }
84 return;
85}
86
87int16_t ACMCELT::InternalEncode(uint8_t* bitstream,
88 int16_t* bitstream_len_byte) {
89 *bitstream_len_byte = 0;
90
91 // Call Encoder.
92 *bitstream_len_byte = WebRtcCelt_Encode(enc_inst_ptr_,
93 &in_audio_[in_audio_ix_read_],
94 bitstream);
95
96 // Increment the read index this tell the caller that how far
97 // we have gone forward in reading the audio buffer.
98 in_audio_ix_read_ += frame_len_smpl_ * channels_;
99
100 if (*bitstream_len_byte < 0) {
101 // Error reported from the encoder.
102 WEBRTC_TRACE(webrtc::kTraceError, webrtc::kTraceAudioCoding, unique_id_,
103 "InternalEncode: Encode error for Celt");
104 *bitstream_len_byte = 0;
105 return -1;
106 }
107
108 return *bitstream_len_byte;
109}
110
111int16_t ACMCELT::InternalInitEncoder(WebRtcACMCodecParams* codec_params) {
112 // Set bitrate and check that it is within the valid range.
113 int16_t status = SetBitRateSafe((codec_params->codec_inst).rate);
114 if (status < 0) {
115 return -1;
116 }
117
118 // If number of channels changed we need to re-create memory.
119 if (codec_params->codec_inst.channels != channels_) {
120 WebRtcCelt_FreeEnc(enc_inst_ptr_);
121 enc_inst_ptr_ = NULL;
122 // Store new number of channels.
123 channels_ = codec_params->codec_inst.channels;
124 if (WebRtcCelt_CreateEnc(&enc_inst_ptr_, channels_) < 0) {
125 return -1;
126 }
127 }
128
129 // Initiate encoder.
130 if (WebRtcCelt_EncoderInit(enc_inst_ptr_, channels_, bitrate_) >= 0) {
131 return 0;
132 } else {
133 return -1;
134 }
135}
136
137ACMGenericCodec* ACMCELT::CreateInstance(void) {
138 return NULL;
139}
140
141int16_t ACMCELT::InternalCreateEncoder() {
142 if (WebRtcCelt_CreateEnc(&enc_inst_ptr_, num_channels_) < 0) {
143 WEBRTC_TRACE(webrtc::kTraceError, webrtc::kTraceAudioCoding, unique_id_,
144 "InternalCreateEncoder: create encoder failed for Celt");
145 return -1;
146 }
147 channels_ = num_channels_;
148 return 0;
149}
150
151void ACMCELT::DestructEncoderSafe() {
152 encoder_exist_ = false;
153 encoder_initialized_ = false;
154 if (enc_inst_ptr_ != NULL) {
155 WebRtcCelt_FreeEnc(enc_inst_ptr_);
156 enc_inst_ptr_ = NULL;
157 }
158}
159
160void ACMCELT::InternalDestructEncoderInst(void* ptr_inst) {
161 if (ptr_inst != NULL) {
162 WebRtcCelt_FreeEnc(static_cast<CELT_encinst_t*>(ptr_inst));
163 }
164 return;
165}
166
167int16_t ACMCELT::SetBitRateSafe(const int32_t rate) {
168 // Check that rate is in the valid range.
169 if ((rate >= 48000) && (rate <= 128000)) {
170 // Store new rate.
171 bitrate_ = rate;
172
173 // Initiate encoder with new rate.
174 if (WebRtcCelt_EncoderInit(enc_inst_ptr_, channels_, bitrate_) >= 0) {
175 return 0;
176 } else {
177 WEBRTC_TRACE(webrtc::kTraceError, webrtc::kTraceAudioCoding, unique_id_,
178 "SetBitRateSafe: Failed to initiate Celt with rate %d",
179 rate);
180 return -1;
181 }
182 } else {
183 WEBRTC_TRACE(webrtc::kTraceError, webrtc::kTraceAudioCoding, unique_id_,
184 "SetBitRateSafe: Invalid rate Celt, %d", rate);
185 return -1;
186 }
187}
188
189#endif
190
191} // namespace webrtc