OLD | NEW |
1 /* | 1 /* |
2 * Copyright (c) 2012 The WebRTC project authors. All Rights Reserved. | 2 * Copyright (c) 2012 The WebRTC project authors. All Rights Reserved. |
3 * | 3 * |
4 * Use of this source code is governed by a BSD-style license | 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 | 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 | 6 * tree. An additional intellectual property rights grant can be found |
7 * in the file PATENTS. All contributing project authors may | 7 * in the file PATENTS. All contributing project authors may |
8 * be found in the AUTHORS file in the root of the source tree. | 8 * be found in the AUTHORS file in the root of the source tree. |
9 */ | 9 */ |
10 | 10 |
11 #include "webrtc/modules/audio_coding/neteq/comfort_noise.h" | 11 #include "webrtc/modules/audio_coding/neteq/comfort_noise.h" |
12 | 12 |
13 #include <assert.h> | 13 #include <assert.h> |
14 | 14 |
15 #include "webrtc/base/logging.h" | 15 #include "webrtc/base/logging.h" |
16 #include "webrtc/modules/audio_coding/codecs/audio_decoder.h" | 16 #include "webrtc/modules/audio_coding/codecs/audio_decoder.h" |
17 #include "webrtc/modules/audio_coding/codecs/cng/webrtc_cng.h" | |
18 #include "webrtc/modules/audio_coding/neteq/decoder_database.h" | 17 #include "webrtc/modules/audio_coding/neteq/decoder_database.h" |
19 #include "webrtc/modules/audio_coding/neteq/dsp_helper.h" | 18 #include "webrtc/modules/audio_coding/neteq/dsp_helper.h" |
20 #include "webrtc/modules/audio_coding/neteq/sync_buffer.h" | 19 #include "webrtc/modules/audio_coding/neteq/sync_buffer.h" |
21 | 20 |
22 namespace webrtc { | 21 namespace webrtc { |
23 | 22 |
24 void ComfortNoise::Reset() { | 23 void ComfortNoise::Reset() { |
25 first_call_ = true; | 24 first_call_ = true; |
26 internal_error_code_ = 0; | |
27 } | 25 } |
28 | 26 |
29 int ComfortNoise::UpdateParameters(Packet* packet) { | 27 int ComfortNoise::UpdateParameters(Packet* packet) { |
30 assert(packet); // Existence is verified by caller. | 28 assert(packet); // Existence is verified by caller. |
31 // Get comfort noise decoder. | 29 // Get comfort noise decoder. |
32 AudioDecoder* cng_decoder = decoder_database_->GetDecoder( | 30 if (decoder_database_->SetActiveCngDecoder(packet->header.payloadType) |
33 packet->header.payloadType); | 31 != kOK) { |
34 if (!cng_decoder) { | |
35 delete [] packet->payload; | 32 delete [] packet->payload; |
36 delete packet; | 33 delete packet; |
37 return kUnknownPayloadType; | 34 return kUnknownPayloadType; |
38 } | 35 } |
39 decoder_database_->SetActiveCngDecoder(packet->header.payloadType); | 36 ComfortNoiseDecoder* cng_decoder = decoder_database_->GetActiveCngDecoder(); |
40 CNG_dec_inst* cng_inst = cng_decoder->CngDecoderInstance(); | 37 RTC_DCHECK(cng_decoder); |
41 int16_t ret = WebRtcCng_UpdateSid(cng_inst, | 38 cng_decoder->UpdateSid(rtc::ArrayView<const uint8_t>( |
42 packet->payload, | 39 packet->payload, packet->payload_length)); |
43 packet->payload_length); | |
44 delete [] packet->payload; | 40 delete [] packet->payload; |
45 delete packet; | 41 delete packet; |
46 if (ret < 0) { | |
47 internal_error_code_ = WebRtcCng_GetErrorCodeDec(cng_inst); | |
48 LOG(LS_ERROR) << "WebRtcCng_UpdateSid produced " << internal_error_code_; | |
49 return kInternalError; | |
50 } | |
51 return kOK; | 42 return kOK; |
52 } | 43 } |
53 | 44 |
54 int ComfortNoise::Generate(size_t requested_length, | 45 int ComfortNoise::Generate(size_t requested_length, |
55 AudioMultiVector* output) { | 46 AudioMultiVector* output) { |
56 // TODO(hlundin): Change to an enumerator and skip assert. | 47 // TODO(hlundin): Change to an enumerator and skip assert. |
57 assert(fs_hz_ == 8000 || fs_hz_ == 16000 || fs_hz_ == 32000 || | 48 assert(fs_hz_ == 8000 || fs_hz_ == 16000 || fs_hz_ == 32000 || |
58 fs_hz_ == 48000); | 49 fs_hz_ == 48000); |
59 // Not adapted for multi-channel yet. | 50 // Not adapted for multi-channel yet. |
60 if (output->Channels() != 1) { | 51 if (output->Channels() != 1) { |
61 LOG(LS_ERROR) << "No multi-channel support"; | 52 LOG(LS_ERROR) << "No multi-channel support"; |
62 return kMultiChannelNotSupported; | 53 return kMultiChannelNotSupported; |
63 } | 54 } |
64 | 55 |
65 size_t number_of_samples = requested_length; | 56 size_t number_of_samples = requested_length; |
66 int16_t new_period = 0; | 57 bool new_period = false; |
67 if (first_call_) { | 58 if (first_call_) { |
68 // Generate noise and overlap slightly with old data. | 59 // Generate noise and overlap slightly with old data. |
69 number_of_samples = requested_length + overlap_length_; | 60 number_of_samples = requested_length + overlap_length_; |
70 new_period = 1; | 61 new_period = true; |
71 } | 62 } |
72 output->AssertSize(number_of_samples); | 63 output->AssertSize(number_of_samples); |
73 // Get the decoder from the database. | 64 // Get the decoder from the database. |
74 AudioDecoder* cng_decoder = decoder_database_->GetActiveCngDecoder(); | 65 ComfortNoiseDecoder* cng_decoder = decoder_database_->GetActiveCngDecoder(); |
75 if (!cng_decoder) { | 66 if (!cng_decoder) { |
76 LOG(LS_ERROR) << "Unknwown payload type"; | 67 LOG(LS_ERROR) << "Unknwown payload type"; |
77 return kUnknownPayloadType; | 68 return kUnknownPayloadType; |
78 } | 69 } |
79 CNG_dec_inst* cng_inst = cng_decoder->CngDecoderInstance(); | |
80 // The expression &(*output)[0][0] is a pointer to the first element in | 70 // The expression &(*output)[0][0] is a pointer to the first element in |
81 // the first channel. | 71 // the first channel. |
82 if (WebRtcCng_Generate(cng_inst, &(*output)[0][0], number_of_samples, | 72 if (!cng_decoder->Generate( |
83 new_period) < 0) { | 73 rtc::ArrayView<int16_t>(&(*output)[0][0], number_of_samples), |
| 74 new_period)) { |
84 // Error returned. | 75 // Error returned. |
85 output->Zeros(requested_length); | 76 output->Zeros(requested_length); |
86 internal_error_code_ = WebRtcCng_GetErrorCodeDec(cng_inst); | 77 LOG(LS_ERROR) << |
87 LOG(LS_ERROR) << "WebRtcCng_Generate produced " << internal_error_code_; | 78 "ComfortNoiseDecoder::Genererate failed to generate comfort noise"; |
88 return kInternalError; | 79 return kInternalError; |
89 } | 80 } |
90 | 81 |
91 if (first_call_) { | 82 if (first_call_) { |
92 // Set tapering window parameters. Values are in Q15. | 83 // Set tapering window parameters. Values are in Q15. |
93 int16_t muting_window; // Mixing factor for overlap data. | 84 int16_t muting_window; // Mixing factor for overlap data. |
94 int16_t muting_window_increment; // Mixing factor increment (negative). | 85 int16_t muting_window_increment; // Mixing factor increment (negative). |
95 int16_t unmuting_window; // Mixing factor for comfort noise. | 86 int16_t unmuting_window; // Mixing factor for comfort noise. |
96 int16_t unmuting_window_increment; // Mixing factor increment. | 87 int16_t unmuting_window_increment; // Mixing factor increment. |
97 if (fs_hz_ == 8000) { | 88 if (fs_hz_ == 8000) { |
(...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
130 } | 121 } |
131 // Remove |overlap_length_| samples from the front of |output| since they | 122 // Remove |overlap_length_| samples from the front of |output| since they |
132 // were mixed into |sync_buffer_| above. | 123 // were mixed into |sync_buffer_| above. |
133 output->PopFront(overlap_length_); | 124 output->PopFront(overlap_length_); |
134 } | 125 } |
135 first_call_ = false; | 126 first_call_ = false; |
136 return kOK; | 127 return kOK; |
137 } | 128 } |
138 | 129 |
139 } // namespace webrtc | 130 } // namespace webrtc |
OLD | NEW |