From 1c6239a3b622fd886d1a2d78cb716b4745446a51 Mon Sep 17 00:00:00 2001 From: "kwiberg@webrtc.org" Date: Mon, 9 Feb 2015 12:55:48 +0000 Subject: [PATCH] G711: Make input arrays const and use uint8_t[] for byte arrays BUG=909 R=henrik.lundin@webrtc.org Review URL: https://webrtc-codereview.appspot.com/39809004 Cr-Commit-Position: refs/heads/master@{#8294} git-svn-id: http://webrtc.googlecode.com/svn/trunk@8294 4adac7df-926f-26a2-2b94-8c16560cd09d --- .../codecs/g711/audio_encoder_pcm.cc | 8 +- .../audio_coding/codecs/g711/g711_interface.c | 128 +++--------------- .../codecs/g711/include/g711_interface.h | 12 +- .../audio_coding/codecs/g711/test/testG711.cc | 2 +- .../audio_coding/main/acm2/acm_pcma.cc | 6 +- .../audio_coding/main/acm2/acm_pcmu.cc | 6 +- .../audio_coding/neteq/audio_decoder_impl.cc | 10 +- .../audio_coding/neteq/test/RTPencode.cc | 4 +- 8 files changed, 39 insertions(+), 137 deletions(-) diff --git a/webrtc/modules/audio_coding/codecs/g711/audio_encoder_pcm.cc b/webrtc/modules/audio_coding/codecs/g711/audio_encoder_pcm.cc index c2f642471..3dd880021 100644 --- a/webrtc/modules/audio_coding/codecs/g711/audio_encoder_pcm.cc +++ b/webrtc/modules/audio_coding/codecs/g711/audio_encoder_pcm.cc @@ -90,17 +90,13 @@ bool AudioEncoderPcm::EncodeInternal(uint32_t rtp_timestamp, int16_t AudioEncoderPcmA::EncodeCall(const int16_t* audio, size_t input_len, uint8_t* encoded) { - return WebRtcG711_EncodeA(const_cast(audio), - static_cast(input_len), - reinterpret_cast(encoded)); + return WebRtcG711_EncodeA(audio, static_cast(input_len), encoded); } int16_t AudioEncoderPcmU::EncodeCall(const int16_t* audio, size_t input_len, uint8_t* encoded) { - return WebRtcG711_EncodeU(const_cast(audio), - static_cast(input_len), - reinterpret_cast(encoded)); + return WebRtcG711_EncodeU(audio, static_cast(input_len), encoded); } } // namespace webrtc diff --git a/webrtc/modules/audio_coding/codecs/g711/g711_interface.c b/webrtc/modules/audio_coding/codecs/g711/g711_interface.c index ec726c51a..809a70e88 100644 --- a/webrtc/modules/audio_coding/codecs/g711/g711_interface.c +++ b/webrtc/modules/audio_coding/codecs/g711/g711_interface.c @@ -12,136 +12,44 @@ #include "g711_interface.h" #include "webrtc/typedefs.h" -int16_t WebRtcG711_EncodeA(int16_t* speechIn, +int16_t WebRtcG711_EncodeA(const int16_t* speechIn, int16_t len, - int16_t* encoded) { + uint8_t* encoded) { int n; - uint16_t tempVal, tempVal2; - - // Sanity check of input length - if (len < 0) { - return (-1); - } - - // Loop over all samples - for (n = 0; n < len; n++) { - tempVal = (uint16_t) linear_to_alaw(speechIn[n]); - -#ifdef WEBRTC_ARCH_BIG_ENDIAN - if ((n & 0x1) == 1) { - encoded[n >> 1] |= ((uint16_t) tempVal); - } else { - encoded[n >> 1] = ((uint16_t) tempVal) << 8; - } -#else - if ((n & 0x1) == 1) { - tempVal2 |= ((uint16_t) tempVal) << 8; - encoded[n >> 1] |= ((uint16_t) tempVal) << 8; - } else { - tempVal2 = ((uint16_t) tempVal); - encoded[n >> 1] = ((uint16_t) tempVal); - } -#endif - } - return (len); + for (n = 0; n < len; n++) + encoded[n] = linear_to_alaw(speechIn[n]); + return len; } -int16_t WebRtcG711_EncodeU(int16_t* speechIn, +int16_t WebRtcG711_EncodeU(const int16_t* speechIn, int16_t len, - int16_t* encoded) { + uint8_t* encoded) { int n; - uint16_t tempVal; - - // Sanity check of input length - if (len < 0) { - return (-1); - } - - // Loop over all samples - for (n = 0; n < len; n++) { - tempVal = (uint16_t) linear_to_ulaw(speechIn[n]); - -#ifdef WEBRTC_ARCH_BIG_ENDIAN - if ((n & 0x1) == 1) { - encoded[n >> 1] |= ((uint16_t) tempVal); - } else { - encoded[n >> 1] = ((uint16_t) tempVal) << 8; - } -#else - if ((n & 0x1) == 1) { - encoded[n >> 1] |= ((uint16_t) tempVal) << 8; - } else { - encoded[n >> 1] = ((uint16_t) tempVal); - } -#endif - } - return (len); + for (n = 0; n < len; n++) + encoded[n] = linear_to_ulaw(speechIn[n]); + return len; } -int16_t WebRtcG711_DecodeA(int16_t* encoded, +int16_t WebRtcG711_DecodeA(const uint8_t* encoded, int16_t len, int16_t* decoded, int16_t* speechType) { int n; - uint16_t tempVal; - - // Sanity check of input length - if (len < 0) { - return (-1); - } - - for (n = 0; n < len; n++) { -#ifdef WEBRTC_ARCH_BIG_ENDIAN - if ((n & 0x1) == 1) { - tempVal = ((uint16_t) encoded[n >> 1] & 0xFF); - } else { - tempVal = ((uint16_t) encoded[n >> 1] >> 8); - } -#else - if ((n & 0x1) == 1) { - tempVal = (encoded[n >> 1] >> 8); - } else { - tempVal = (encoded[n >> 1] & 0xFF); - } -#endif - decoded[n] = (int16_t) alaw_to_linear(tempVal); - } - + for (n = 0; n < len; n++) + decoded[n] = alaw_to_linear(encoded[n]); *speechType = 1; - return (len); + return len; } -int16_t WebRtcG711_DecodeU(int16_t* encoded, +int16_t WebRtcG711_DecodeU(const uint8_t* encoded, int16_t len, int16_t* decoded, int16_t* speechType) { int n; - uint16_t tempVal; - - // Sanity check of input length - if (len < 0) { - return (-1); - } - - for (n = 0; n < len; n++) { -#ifdef WEBRTC_ARCH_BIG_ENDIAN - if ((n & 0x1) == 1) { - tempVal = ((uint16_t) encoded[n >> 1] & 0xFF); - } else { - tempVal = ((uint16_t) encoded[n >> 1] >> 8); - } -#else - if ((n & 0x1) == 1) { - tempVal = (encoded[n >> 1] >> 8); - } else { - tempVal = (encoded[n >> 1] & 0xFF); - } -#endif - decoded[n] = (int16_t) ulaw_to_linear(tempVal); - } - + for (n = 0; n < len; n++) + decoded[n] = ulaw_to_linear(encoded[n]); *speechType = 1; - return (len); + return len; } int WebRtcG711_DurationEst(const uint8_t* payload, diff --git a/webrtc/modules/audio_coding/codecs/g711/include/g711_interface.h b/webrtc/modules/audio_coding/codecs/g711/include/g711_interface.h index 545ca3e83..aa3894da3 100644 --- a/webrtc/modules/audio_coding/codecs/g711/include/g711_interface.h +++ b/webrtc/modules/audio_coding/codecs/g711/include/g711_interface.h @@ -38,9 +38,9 @@ extern "C" { * -1 - Error */ -int16_t WebRtcG711_EncodeA(int16_t* speechIn, +int16_t WebRtcG711_EncodeA(const int16_t* speechIn, int16_t len, - int16_t* encoded); + uint8_t* encoded); /**************************************************************************** * WebRtcG711_EncodeU(...) @@ -59,9 +59,9 @@ int16_t WebRtcG711_EncodeA(int16_t* speechIn, * -1 - Error */ -int16_t WebRtcG711_EncodeU(int16_t* speechIn, +int16_t WebRtcG711_EncodeU(const int16_t* speechIn, int16_t len, - int16_t* encoded); + uint8_t* encoded); /**************************************************************************** * WebRtcG711_DecodeA(...) @@ -82,7 +82,7 @@ int16_t WebRtcG711_EncodeU(int16_t* speechIn, * -1 - Error */ -int16_t WebRtcG711_DecodeA(int16_t* encoded, +int16_t WebRtcG711_DecodeA(const uint8_t* encoded, int16_t len, int16_t* decoded, int16_t* speechType); @@ -106,7 +106,7 @@ int16_t WebRtcG711_DecodeA(int16_t* encoded, * -1 - Error */ -int16_t WebRtcG711_DecodeU(int16_t* encoded, +int16_t WebRtcG711_DecodeU(const uint8_t* encoded, int16_t len, int16_t* decoded, int16_t* speechType); diff --git a/webrtc/modules/audio_coding/codecs/g711/test/testG711.cc b/webrtc/modules/audio_coding/codecs/g711/test/testG711.cc index 76950fa3b..e891810ca 100644 --- a/webrtc/modules/audio_coding/codecs/g711/test/testG711.cc +++ b/webrtc/modules/audio_coding/codecs/g711/test/testG711.cc @@ -57,7 +57,7 @@ int main(int argc, char* argv[]) { int16_t stream_len = 0; int16_t shortdata[480]; int16_t decoded[480]; - int16_t streamdata[500]; + uint8_t streamdata[1000]; int16_t speechType[1]; char law[2]; char versionNumber[40]; diff --git a/webrtc/modules/audio_coding/main/acm2/acm_pcma.cc b/webrtc/modules/audio_coding/main/acm2/acm_pcma.cc index 008b1cf39..56742108f 100644 --- a/webrtc/modules/audio_coding/main/acm2/acm_pcma.cc +++ b/webrtc/modules/audio_coding/main/acm2/acm_pcma.cc @@ -29,9 +29,9 @@ ACMPCMA::~ACMPCMA() { return; } int16_t ACMPCMA::InternalEncode(uint8_t* bitstream, int16_t* bitstream_len_byte) { - *bitstream_len_byte = WebRtcG711_EncodeA( - &in_audio_[in_audio_ix_read_], frame_len_smpl_ * num_channels_, - reinterpret_cast(bitstream)); + *bitstream_len_byte = + WebRtcG711_EncodeA(&in_audio_[in_audio_ix_read_], + frame_len_smpl_ * num_channels_, bitstream); // Increment the read index this tell the caller that how far // we have gone forward in reading the audio buffer. in_audio_ix_read_ += frame_len_smpl_ * num_channels_; diff --git a/webrtc/modules/audio_coding/main/acm2/acm_pcmu.cc b/webrtc/modules/audio_coding/main/acm2/acm_pcmu.cc index 7ccc18416..4c50696c0 100644 --- a/webrtc/modules/audio_coding/main/acm2/acm_pcmu.cc +++ b/webrtc/modules/audio_coding/main/acm2/acm_pcmu.cc @@ -29,9 +29,9 @@ ACMPCMU::~ACMPCMU() {} int16_t ACMPCMU::InternalEncode(uint8_t* bitstream, int16_t* bitstream_len_byte) { - *bitstream_len_byte = WebRtcG711_EncodeU( - &in_audio_[in_audio_ix_read_], frame_len_smpl_ * num_channels_, - reinterpret_cast(bitstream)); + *bitstream_len_byte = + WebRtcG711_EncodeU(&in_audio_[in_audio_ix_read_], + frame_len_smpl_ * num_channels_, bitstream); // Increment the read index this tell the caller that how far // we have gone forward in reading the audio buffer. diff --git a/webrtc/modules/audio_coding/neteq/audio_decoder_impl.cc b/webrtc/modules/audio_coding/neteq/audio_decoder_impl.cc index 9ea242995..6050585a7 100644 --- a/webrtc/modules/audio_coding/neteq/audio_decoder_impl.cc +++ b/webrtc/modules/audio_coding/neteq/audio_decoder_impl.cc @@ -41,9 +41,8 @@ namespace webrtc { int AudioDecoderPcmU::Decode(const uint8_t* encoded, size_t encoded_len, int16_t* decoded, SpeechType* speech_type) { int16_t temp_type = 1; // Default is speech. - int16_t ret = WebRtcG711_DecodeU( - reinterpret_cast(const_cast(encoded)), - static_cast(encoded_len), decoded, &temp_type); + int16_t ret = WebRtcG711_DecodeU(encoded, static_cast(encoded_len), + decoded, &temp_type); *speech_type = ConvertSpeechType(temp_type); return ret; } @@ -58,9 +57,8 @@ int AudioDecoderPcmU::PacketDuration(const uint8_t* encoded, int AudioDecoderPcmA::Decode(const uint8_t* encoded, size_t encoded_len, int16_t* decoded, SpeechType* speech_type) { int16_t temp_type = 1; // Default is speech. - int16_t ret = WebRtcG711_DecodeA( - reinterpret_cast(const_cast(encoded)), - static_cast(encoded_len), decoded, &temp_type); + int16_t ret = WebRtcG711_DecodeA(encoded, static_cast(encoded_len), + decoded, &temp_type); *speech_type = ConvertSpeechType(temp_type); return ret; } diff --git a/webrtc/modules/audio_coding/neteq/test/RTPencode.cc b/webrtc/modules/audio_coding/neteq/test/RTPencode.cc index 543ed11a8..9aa207d54 100644 --- a/webrtc/modules/audio_coding/neteq/test/RTPencode.cc +++ b/webrtc/modules/audio_coding/neteq/test/RTPencode.cc @@ -1558,12 +1558,12 @@ int NetEQTest_encode(int coder, int16_t *indata, int frameLen, unsigned char * e /* Encode with the selected coder type */ if (coder==webrtc::kDecoderPCMu) { /*g711 u-law */ #ifdef CODEC_G711 - cdlen = WebRtcG711_EncodeU(indata, frameLen, (int16_t*) encoded); + cdlen = WebRtcG711_EncodeU(indata, frameLen, encoded); #endif } else if (coder==webrtc::kDecoderPCMa) { /*g711 A-law */ #ifdef CODEC_G711 - cdlen = WebRtcG711_EncodeA(indata, frameLen, (int16_t*) encoded); + cdlen = WebRtcG711_EncodeA(indata, frameLen, encoded); } #endif #ifdef CODEC_PCM16B