Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(821)

Side by Side Diff: webrtc/modules/audio_coding/neteq/audio_decoder_impl.cc

Issue 1230503003: Update a ton of audio code to use size_t more correctly and in general reduce (Closed) Base URL: https://chromium.googlesource.com/external/webrtc@master
Patch Set: Resync Created 5 years, 3 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
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
(...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after
46 return 1; 46 return 1;
47 } 47 }
48 48
49 int AudioDecoderPcmU::DecodeInternal(const uint8_t* encoded, 49 int AudioDecoderPcmU::DecodeInternal(const uint8_t* encoded,
50 size_t encoded_len, 50 size_t encoded_len,
51 int sample_rate_hz, 51 int sample_rate_hz,
52 int16_t* decoded, 52 int16_t* decoded,
53 SpeechType* speech_type) { 53 SpeechType* speech_type) {
54 DCHECK_EQ(sample_rate_hz, 8000); 54 DCHECK_EQ(sample_rate_hz, 8000);
55 int16_t temp_type = 1; // Default is speech. 55 int16_t temp_type = 1; // Default is speech.
56 int16_t ret = WebRtcG711_DecodeU(encoded, static_cast<int16_t>(encoded_len), 56 size_t ret = WebRtcG711_DecodeU(encoded, encoded_len, decoded, &temp_type);
57 decoded, &temp_type);
58 *speech_type = ConvertSpeechType(temp_type); 57 *speech_type = ConvertSpeechType(temp_type);
59 return ret; 58 return static_cast<int>(ret);
60 } 59 }
61 60
62 int AudioDecoderPcmU::PacketDuration(const uint8_t* encoded, 61 int AudioDecoderPcmU::PacketDuration(const uint8_t* encoded,
63 size_t encoded_len) const { 62 size_t encoded_len) const {
64 // One encoded byte per sample per channel. 63 // One encoded byte per sample per channel.
65 return static_cast<int>(encoded_len / Channels()); 64 return static_cast<int>(encoded_len / Channels());
66 } 65 }
67 66
68 size_t AudioDecoderPcmUMultiCh::Channels() const { 67 size_t AudioDecoderPcmUMultiCh::Channels() const {
69 return channels_; 68 return channels_;
70 } 69 }
71 70
72 // PCMa 71 // PCMa
73 72
74 int AudioDecoderPcmA::Init() { 73 int AudioDecoderPcmA::Init() {
75 return 0; 74 return 0;
76 } 75 }
77 size_t AudioDecoderPcmA::Channels() const { 76 size_t AudioDecoderPcmA::Channels() const {
78 return 1; 77 return 1;
79 } 78 }
80 79
81 int AudioDecoderPcmA::DecodeInternal(const uint8_t* encoded, 80 int AudioDecoderPcmA::DecodeInternal(const uint8_t* encoded,
82 size_t encoded_len, 81 size_t encoded_len,
83 int sample_rate_hz, 82 int sample_rate_hz,
84 int16_t* decoded, 83 int16_t* decoded,
85 SpeechType* speech_type) { 84 SpeechType* speech_type) {
86 DCHECK_EQ(sample_rate_hz, 8000); 85 DCHECK_EQ(sample_rate_hz, 8000);
87 int16_t temp_type = 1; // Default is speech. 86 int16_t temp_type = 1; // Default is speech.
88 int16_t ret = WebRtcG711_DecodeA(encoded, static_cast<int16_t>(encoded_len), 87 size_t ret = WebRtcG711_DecodeA(encoded, encoded_len, decoded, &temp_type);
89 decoded, &temp_type);
90 *speech_type = ConvertSpeechType(temp_type); 88 *speech_type = ConvertSpeechType(temp_type);
91 return ret; 89 return static_cast<int>(ret);
92 } 90 }
93 91
94 int AudioDecoderPcmA::PacketDuration(const uint8_t* encoded, 92 int AudioDecoderPcmA::PacketDuration(const uint8_t* encoded,
95 size_t encoded_len) const { 93 size_t encoded_len) const {
96 // One encoded byte per sample per channel. 94 // One encoded byte per sample per channel.
97 return static_cast<int>(encoded_len / Channels()); 95 return static_cast<int>(encoded_len / Channels());
98 } 96 }
99 97
100 size_t AudioDecoderPcmAMultiCh::Channels() const { 98 size_t AudioDecoderPcmAMultiCh::Channels() const {
101 return channels_; 99 return channels_;
(...skipping 11 matching lines...) Expand all
113 } 111 }
114 112
115 int AudioDecoderPcm16B::DecodeInternal(const uint8_t* encoded, 113 int AudioDecoderPcm16B::DecodeInternal(const uint8_t* encoded,
116 size_t encoded_len, 114 size_t encoded_len,
117 int sample_rate_hz, 115 int sample_rate_hz,
118 int16_t* decoded, 116 int16_t* decoded,
119 SpeechType* speech_type) { 117 SpeechType* speech_type) {
120 DCHECK(sample_rate_hz == 8000 || sample_rate_hz == 16000 || 118 DCHECK(sample_rate_hz == 8000 || sample_rate_hz == 16000 ||
121 sample_rate_hz == 32000 || sample_rate_hz == 48000) 119 sample_rate_hz == 32000 || sample_rate_hz == 48000)
122 << "Unsupported sample rate " << sample_rate_hz; 120 << "Unsupported sample rate " << sample_rate_hz;
123 int16_t ret = 121 size_t ret = WebRtcPcm16b_Decode(encoded, encoded_len, decoded);
124 WebRtcPcm16b_Decode(encoded, static_cast<int16_t>(encoded_len), decoded);
125 *speech_type = ConvertSpeechType(1); 122 *speech_type = ConvertSpeechType(1);
126 return ret; 123 return static_cast<int>(ret);
127 } 124 }
128 125
129 int AudioDecoderPcm16B::PacketDuration(const uint8_t* encoded, 126 int AudioDecoderPcm16B::PacketDuration(const uint8_t* encoded,
130 size_t encoded_len) const { 127 size_t encoded_len) const {
131 // Two encoded byte per sample per channel. 128 // Two encoded byte per sample per channel.
132 return static_cast<int>(encoded_len / (2 * Channels())); 129 return static_cast<int>(encoded_len / (2 * Channels()));
133 } 130 }
134 131
135 AudioDecoderPcm16BMultiCh::AudioDecoderPcm16BMultiCh(int num_channels) 132 AudioDecoderPcm16BMultiCh::AudioDecoderPcm16BMultiCh(size_t num_channels)
136 : channels_(num_channels) { 133 : channels_(num_channels) {
137 DCHECK(num_channels > 0); 134 DCHECK(num_channels > 0);
138 } 135 }
139 136
140 size_t AudioDecoderPcm16BMultiCh::Channels() const { 137 size_t AudioDecoderPcm16BMultiCh::Channels() const {
141 return channels_; 138 return channels_;
142 } 139 }
143 #endif 140 #endif
144 141
145 // iLBC 142 // iLBC
(...skipping 10 matching lines...) Expand all
156 return true; 153 return true;
157 } 154 }
158 155
159 int AudioDecoderIlbc::DecodeInternal(const uint8_t* encoded, 156 int AudioDecoderIlbc::DecodeInternal(const uint8_t* encoded,
160 size_t encoded_len, 157 size_t encoded_len,
161 int sample_rate_hz, 158 int sample_rate_hz,
162 int16_t* decoded, 159 int16_t* decoded,
163 SpeechType* speech_type) { 160 SpeechType* speech_type) {
164 DCHECK_EQ(sample_rate_hz, 8000); 161 DCHECK_EQ(sample_rate_hz, 8000);
165 int16_t temp_type = 1; // Default is speech. 162 int16_t temp_type = 1; // Default is speech.
166 int ret = WebRtcIlbcfix_Decode(dec_state_, encoded, 163 int ret = WebRtcIlbcfix_Decode(dec_state_, encoded, encoded_len, decoded,
167 static_cast<int16_t>(encoded_len), decoded,
168 &temp_type); 164 &temp_type);
169 *speech_type = ConvertSpeechType(temp_type); 165 *speech_type = ConvertSpeechType(temp_type);
170 return ret; 166 return ret;
171 } 167 }
172 168
173 int AudioDecoderIlbc::DecodePlc(int num_frames, int16_t* decoded) { 169 size_t AudioDecoderIlbc::DecodePlc(size_t num_frames, int16_t* decoded) {
174 return WebRtcIlbcfix_NetEqPlc(dec_state_, decoded, num_frames); 170 return WebRtcIlbcfix_NetEqPlc(dec_state_, decoded, num_frames);
175 } 171 }
176 172
177 int AudioDecoderIlbc::Init() { 173 int AudioDecoderIlbc::Init() {
178 return WebRtcIlbcfix_Decoderinit30Ms(dec_state_); 174 return WebRtcIlbcfix_Decoderinit30Ms(dec_state_);
179 } 175 }
180 176
181 size_t AudioDecoderIlbc::Channels() const { 177 size_t AudioDecoderIlbc::Channels() const {
182 return 1; 178 return 1;
183 } 179 }
(...skipping 13 matching lines...) Expand all
197 return false; 193 return false;
198 } 194 }
199 195
200 int AudioDecoderG722::DecodeInternal(const uint8_t* encoded, 196 int AudioDecoderG722::DecodeInternal(const uint8_t* encoded,
201 size_t encoded_len, 197 size_t encoded_len,
202 int sample_rate_hz, 198 int sample_rate_hz,
203 int16_t* decoded, 199 int16_t* decoded,
204 SpeechType* speech_type) { 200 SpeechType* speech_type) {
205 DCHECK_EQ(sample_rate_hz, 16000); 201 DCHECK_EQ(sample_rate_hz, 16000);
206 int16_t temp_type = 1; // Default is speech. 202 int16_t temp_type = 1; // Default is speech.
207 int16_t ret = 203 size_t ret =
208 WebRtcG722_Decode(dec_state_, encoded, static_cast<int16_t>(encoded_len), 204 WebRtcG722_Decode(dec_state_, encoded, encoded_len, decoded, &temp_type);
209 decoded, &temp_type);
210 *speech_type = ConvertSpeechType(temp_type); 205 *speech_type = ConvertSpeechType(temp_type);
211 return ret; 206 return static_cast<int>(ret);
212 } 207 }
213 208
214 int AudioDecoderG722::Init() { 209 int AudioDecoderG722::Init() {
215 return WebRtcG722_DecoderInit(dec_state_); 210 return WebRtcG722_DecoderInit(dec_state_);
216 } 211 }
217 212
218 int AudioDecoderG722::PacketDuration(const uint8_t* encoded, 213 int AudioDecoderG722::PacketDuration(const uint8_t* encoded,
219 size_t encoded_len) const { 214 size_t encoded_len) const {
220 // 1/2 encoded byte per sample per channel. 215 // 1/2 encoded byte per sample per channel.
221 return static_cast<int>(2 * encoded_len / Channels()); 216 return static_cast<int>(2 * encoded_len / Channels());
(...skipping 17 matching lines...) Expand all
239 size_t encoded_len, 234 size_t encoded_len,
240 int sample_rate_hz, 235 int sample_rate_hz,
241 int16_t* decoded, 236 int16_t* decoded,
242 SpeechType* speech_type) { 237 SpeechType* speech_type) {
243 DCHECK_EQ(sample_rate_hz, 16000); 238 DCHECK_EQ(sample_rate_hz, 16000);
244 int16_t temp_type = 1; // Default is speech. 239 int16_t temp_type = 1; // Default is speech.
245 // De-interleave the bit-stream into two separate payloads. 240 // De-interleave the bit-stream into two separate payloads.
246 uint8_t* encoded_deinterleaved = new uint8_t[encoded_len]; 241 uint8_t* encoded_deinterleaved = new uint8_t[encoded_len];
247 SplitStereoPacket(encoded, encoded_len, encoded_deinterleaved); 242 SplitStereoPacket(encoded, encoded_len, encoded_deinterleaved);
248 // Decode left and right. 243 // Decode left and right.
249 int16_t ret = WebRtcG722_Decode(dec_state_left_, encoded_deinterleaved, 244 size_t decoded_len = WebRtcG722_Decode(dec_state_left_, encoded_deinterleaved,
250 static_cast<int16_t>(encoded_len / 2), 245 encoded_len / 2, decoded, &temp_type);
251 decoded, &temp_type); 246 size_t ret = WebRtcG722_Decode(
252 if (ret >= 0) { 247 dec_state_right_, &encoded_deinterleaved[encoded_len / 2],
253 int decoded_len = ret; 248 encoded_len / 2, &decoded[decoded_len], &temp_type);
254 ret = WebRtcG722_Decode(dec_state_right_, 249 if (ret == decoded_len) {
255 &encoded_deinterleaved[encoded_len / 2], 250 ret += decoded_len; // Return total number of samples.
256 static_cast<int16_t>(encoded_len / 2), 251 // Interleave output.
257 &decoded[decoded_len], &temp_type); 252 for (size_t k = ret / 2; k < ret; k++) {
258 if (ret == decoded_len) { 253 int16_t temp = decoded[k];
259 ret += decoded_len; // Return total number of samples. 254 memmove(&decoded[2 * k - ret + 2], &decoded[2 * k - ret + 1],
260 // Interleave output. 255 (ret - k - 1) * sizeof(int16_t));
261 for (int k = ret / 2; k < ret; k++) { 256 decoded[2 * k - ret + 1] = temp;
262 int16_t temp = decoded[k];
263 memmove(&decoded[2 * k - ret + 2], &decoded[2 * k - ret + 1],
264 (ret - k - 1) * sizeof(int16_t));
265 decoded[2 * k - ret + 1] = temp;
266 }
267 } 257 }
268 } 258 }
269 *speech_type = ConvertSpeechType(temp_type); 259 *speech_type = ConvertSpeechType(temp_type);
270 delete [] encoded_deinterleaved; 260 delete [] encoded_deinterleaved;
271 return ret; 261 return static_cast<int>(ret);
272 } 262 }
273 263
274 size_t AudioDecoderG722Stereo::Channels() const { 264 size_t AudioDecoderG722Stereo::Channels() const {
275 return 2; 265 return 2;
276 } 266 }
277 267
278 int AudioDecoderG722Stereo::Init() { 268 int AudioDecoderG722Stereo::Init() {
279 int r = WebRtcG722_DecoderInit(dec_state_left_); 269 int r = WebRtcG722_DecoderInit(dec_state_left_);
280 if (r != 0) 270 if (r != 0)
281 return r; 271 return r;
(...skipping 23 matching lines...) Expand all
305 uint8_t right_byte = encoded_deinterleaved[i + 1]; 295 uint8_t right_byte = encoded_deinterleaved[i + 1];
306 memmove(&encoded_deinterleaved[i + 1], &encoded_deinterleaved[i + 2], 296 memmove(&encoded_deinterleaved[i + 1], &encoded_deinterleaved[i + 2],
307 encoded_len - i - 2); 297 encoded_len - i - 2);
308 encoded_deinterleaved[encoded_len - 1] = right_byte; 298 encoded_deinterleaved[encoded_len - 1] = right_byte;
309 } 299 }
310 } 300 }
311 #endif 301 #endif
312 302
313 // Opus 303 // Opus
314 #ifdef WEBRTC_CODEC_OPUS 304 #ifdef WEBRTC_CODEC_OPUS
315 AudioDecoderOpus::AudioDecoderOpus(int num_channels) : channels_(num_channels) { 305 AudioDecoderOpus::AudioDecoderOpus(size_t num_channels)
306 : channels_(num_channels) {
316 DCHECK(num_channels == 1 || num_channels == 2); 307 DCHECK(num_channels == 1 || num_channels == 2);
317 WebRtcOpus_DecoderCreate(&dec_state_, static_cast<int>(channels_)); 308 WebRtcOpus_DecoderCreate(&dec_state_, static_cast<int>(channels_));
318 } 309 }
319 310
320 AudioDecoderOpus::~AudioDecoderOpus() { 311 AudioDecoderOpus::~AudioDecoderOpus() {
321 WebRtcOpus_DecoderFree(dec_state_); 312 WebRtcOpus_DecoderFree(dec_state_);
322 } 313 }
323 314
324 int AudioDecoderOpus::DecodeInternal(const uint8_t* encoded, 315 int AudioDecoderOpus::DecodeInternal(const uint8_t* encoded,
325 size_t encoded_len, 316 size_t encoded_len,
326 int sample_rate_hz, 317 int sample_rate_hz,
327 int16_t* decoded, 318 int16_t* decoded,
328 SpeechType* speech_type) { 319 SpeechType* speech_type) {
329 DCHECK_EQ(sample_rate_hz, 48000); 320 DCHECK_EQ(sample_rate_hz, 48000);
330 int16_t temp_type = 1; // Default is speech. 321 int16_t temp_type = 1; // Default is speech.
331 int ret = WebRtcOpus_Decode(dec_state_, encoded, 322 int ret = WebRtcOpus_Decode(dec_state_, encoded, encoded_len, decoded,
332 static_cast<int16_t>(encoded_len), decoded,
333 &temp_type); 323 &temp_type);
334 if (ret > 0) 324 if (ret > 0)
335 ret *= static_cast<int>(channels_); // Return total number of samples. 325 ret *= static_cast<int>(channels_); // Return total number of samples.
336 *speech_type = ConvertSpeechType(temp_type); 326 *speech_type = ConvertSpeechType(temp_type);
337 return ret; 327 return ret;
338 } 328 }
339 329
340 int AudioDecoderOpus::DecodeRedundantInternal(const uint8_t* encoded, 330 int AudioDecoderOpus::DecodeRedundantInternal(const uint8_t* encoded,
341 size_t encoded_len, 331 size_t encoded_len,
342 int sample_rate_hz, 332 int sample_rate_hz,
343 int16_t* decoded, 333 int16_t* decoded,
344 SpeechType* speech_type) { 334 SpeechType* speech_type) {
345 if (!PacketHasFec(encoded, encoded_len)) { 335 if (!PacketHasFec(encoded, encoded_len)) {
346 // This packet is a RED packet. 336 // This packet is a RED packet.
347 return DecodeInternal(encoded, encoded_len, sample_rate_hz, decoded, 337 return DecodeInternal(encoded, encoded_len, sample_rate_hz, decoded,
348 speech_type); 338 speech_type);
349 } 339 }
350 340
351 DCHECK_EQ(sample_rate_hz, 48000); 341 DCHECK_EQ(sample_rate_hz, 48000);
352 int16_t temp_type = 1; // Default is speech. 342 int16_t temp_type = 1; // Default is speech.
353 int ret = WebRtcOpus_DecodeFec(dec_state_, encoded, 343 int ret = WebRtcOpus_DecodeFec(dec_state_, encoded, encoded_len, decoded,
354 static_cast<int16_t>(encoded_len), decoded,
355 &temp_type); 344 &temp_type);
356 if (ret > 0) 345 if (ret > 0)
357 ret *= static_cast<int>(channels_); // Return total number of samples. 346 ret *= static_cast<int>(channels_); // Return total number of samples.
358 *speech_type = ConvertSpeechType(temp_type); 347 *speech_type = ConvertSpeechType(temp_type);
359 return ret; 348 return ret;
360 } 349 }
361 350
362 int AudioDecoderOpus::Init() { 351 int AudioDecoderOpus::Init() {
363 return WebRtcOpus_DecoderInit(dec_state_); 352 return WebRtcOpus_DecoderInit(dec_state_);
364 } 353 }
365 354
366 int AudioDecoderOpus::PacketDuration(const uint8_t* encoded, 355 int AudioDecoderOpus::PacketDuration(const uint8_t* encoded,
367 size_t encoded_len) const { 356 size_t encoded_len) const {
368 return WebRtcOpus_DurationEst(dec_state_, 357 return WebRtcOpus_DurationEst(dec_state_, encoded, encoded_len);
369 encoded, static_cast<int>(encoded_len));
370 } 358 }
371 359
372 int AudioDecoderOpus::PacketDurationRedundant(const uint8_t* encoded, 360 int AudioDecoderOpus::PacketDurationRedundant(const uint8_t* encoded,
373 size_t encoded_len) const { 361 size_t encoded_len) const {
374 if (!PacketHasFec(encoded, encoded_len)) { 362 if (!PacketHasFec(encoded, encoded_len)) {
375 // This packet is a RED packet. 363 // This packet is a RED packet.
376 return PacketDuration(encoded, encoded_len); 364 return PacketDuration(encoded, encoded_len);
377 } 365 }
378 366
379 return WebRtcOpus_FecDurationEst(encoded, static_cast<int>(encoded_len)); 367 return WebRtcOpus_FecDurationEst(encoded, encoded_len);
380 } 368 }
381 369
382 bool AudioDecoderOpus::PacketHasFec(const uint8_t* encoded, 370 bool AudioDecoderOpus::PacketHasFec(const uint8_t* encoded,
383 size_t encoded_len) const { 371 size_t encoded_len) const {
384 int fec; 372 int fec;
385 fec = WebRtcOpus_PacketHasFec(encoded, static_cast<int>(encoded_len)); 373 fec = WebRtcOpus_PacketHasFec(encoded, encoded_len);
386 return (fec == 1); 374 return (fec == 1);
387 } 375 }
388 376
389 size_t AudioDecoderOpus::Channels() const { 377 size_t AudioDecoderOpus::Channels() const {
390 return channels_; 378 return channels_;
391 } 379 }
392 #endif 380 #endif
393 381
394 AudioDecoderCng::AudioDecoderCng() { 382 AudioDecoderCng::AudioDecoderCng() {
395 CHECK_EQ(0, WebRtcCng_CreateDec(&dec_state_)); 383 CHECK_EQ(0, WebRtcCng_CreateDec(&dec_state_));
(...skipping 205 matching lines...) Expand 10 before | Expand all | Expand 10 after
601 case kDecoderRED: 589 case kDecoderRED:
602 case kDecoderAVT: 590 case kDecoderAVT:
603 case kDecoderArbitrary: 591 case kDecoderArbitrary:
604 default: { 592 default: {
605 return NULL; 593 return NULL;
606 } 594 }
607 } 595 }
608 } 596 }
609 597
610 } // namespace webrtc 598 } // namespace webrtc
OLDNEW
« no previous file with comments | « webrtc/modules/audio_coding/neteq/audio_decoder_impl.h ('k') | webrtc/modules/audio_coding/neteq/audio_decoder_unittest.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698