From 648f5d6dc7598543e4f980cff8cca60234a7d83d Mon Sep 17 00:00:00 2001 From: "kwiberg@webrtc.org" Date: Tue, 10 Feb 2015 09:18:28 +0000 Subject: [PATCH] pcm16b: Make input arrays const and use uint8_t[] for byte arrays There were both uint8 and uint16 versions of the pcm16b encode and decode functions; this patch removes the latter. BUG=909 R=henrik.lundin@webrtc.org Review URL: https://webrtc-codereview.appspot.com/34139004 Cr-Commit-Position: refs/heads/master@{#8309} git-svn-id: http://webrtc.googlecode.com/svn/trunk@8309 4adac7df-926f-26a2-2b94-8c16560cd09d --- .../codecs/pcm16b/include/pcm16b.h | 64 +++---------- .../audio_coding/codecs/pcm16b/pcm16b.c | 96 +++---------------- .../audio_coding/neteq/audio_decoder_impl.cc | 8 +- .../neteq/mock/mock_external_decoder_pcm16b.h | 8 +- .../audio_coding/neteq/neteq_unittest.cc | 6 +- .../audio_coding/neteq/test/RTPencode.cc | 2 +- .../neteq/tools/constant_pcm_packet_source.cc | 7 +- .../neteq/tools/constant_pcm_packet_source.h | 2 +- 8 files changed, 40 insertions(+), 153 deletions(-) diff --git a/webrtc/modules/audio_coding/codecs/pcm16b/include/pcm16b.h b/webrtc/modules/audio_coding/codecs/pcm16b/include/pcm16b.h index 52bc9cc12..49b461476 100644 --- a/webrtc/modules/audio_coding/codecs/pcm16b/include/pcm16b.h +++ b/webrtc/modules/audio_coding/codecs/pcm16b/include/pcm16b.h @@ -20,63 +20,24 @@ extern "C" { #endif -/**************************************************************************** - * WebRtcPcm16b_EncodeW16(...) - * - * "Encode" a sample vector to 16 bit linear (Encoded standard is big endian) - * - * Input: - * - speechIn16b : Input speech vector - * - length_samples : Number of samples in speech vector - * - * Output: - * - speechOut16b : Encoded data vector (big endian 16 bit) - * - * Returned value : Size in bytes of speechOut16b - */ - -int16_t WebRtcPcm16b_EncodeW16(const int16_t* speechIn16b, - int16_t length_samples, - int16_t* speechOut16b); - /**************************************************************************** * WebRtcPcm16b_Encode(...) * * "Encode" a sample vector to 16 bit linear (Encoded standard is big endian) * * Input: - * - speech16b : Input speech vector - * - len : Number of samples in speech vector + * - speech : Input speech vector + * - len : Number of samples in speech vector * * Output: - * - speech8b : Encoded data vector (big endian 16 bit) + * - encoded : Encoded data vector (big endian 16 bit) * - * Returned value : Size in bytes of speech8b + * Returned value : Size in bytes of encoded */ -int16_t WebRtcPcm16b_Encode(const int16_t* speech16b, +int16_t WebRtcPcm16b_Encode(const int16_t* speech, int16_t len, - unsigned char* speech8b); - -/**************************************************************************** - * WebRtcPcm16b_DecodeW16(...) - * - * "Decode" a vector to 16 bit linear (Encoded standard is big endian) - * - * Input: - * - speechIn16b : Encoded data vector (big endian 16 bit) - * - length_bytes : Number of bytes in speechIn16b - * - * Output: - * - speechOut16b : Decoded speech vector - * - * Returned value : Samples in speechOut16b - */ - -int16_t WebRtcPcm16b_DecodeW16(int16_t *speechIn16b, - int16_t length_bytes, - int16_t *speechOut16b, - int16_t* speechType); + uint8_t* encoded); /**************************************************************************** * WebRtcPcm16b_Decode(...) @@ -84,19 +45,18 @@ int16_t WebRtcPcm16b_DecodeW16(int16_t *speechIn16b, * "Decode" a vector to 16 bit linear (Encoded standard is big endian) * * Input: - * - speech8b : Encoded data vector (big endian 16 bit) - * - len : Number of bytes in speech8b + * - encoded : Encoded data vector (big endian 16 bit) + * - len : Number of bytes in encoded * * Output: - * - speech16b : Decoded speech vector + * - speech : Decoded speech vector * - * Returned value : Samples in speech16b + * Returned value : Samples in speech */ - -int16_t WebRtcPcm16b_Decode(unsigned char *speech8b, +int16_t WebRtcPcm16b_Decode(const uint8_t* encoded, int16_t len, - int16_t *speech16b); + int16_t* speech); #ifdef __cplusplus } diff --git a/webrtc/modules/audio_coding/codecs/pcm16b/pcm16b.c b/webrtc/modules/audio_coding/codecs/pcm16b/pcm16b.c index bc7a4747a..b6de0b5e6 100644 --- a/webrtc/modules/audio_coding/codecs/pcm16b/pcm16b.c +++ b/webrtc/modules/audio_coding/codecs/pcm16b/pcm16b.c @@ -8,93 +8,27 @@ * be found in the AUTHORS file in the root of the source tree. */ - #include "pcm16b.h" -#include -#ifdef WEBRTC_ARCH_BIG_ENDIAN -#include -#endif - #include "webrtc/typedefs.h" -#define HIGHEND 0xFF00 -#define LOWEND 0xFF - - - -/* Encoder with int16_t Output */ -int16_t WebRtcPcm16b_EncodeW16(const int16_t* speechIn16b, - int16_t length_samples, - int16_t* speechOut16b) -{ -#ifdef WEBRTC_ARCH_BIG_ENDIAN - memcpy(speechOut16b, speechIn16b, length_samples * sizeof(int16_t)); -#else - int i; - for (i = 0; i < length_samples; i++) { - speechOut16b[i]=(((uint16_t)speechIn16b[i])>>8)|((((uint16_t)speechIn16b[i])<<8)&0xFF00); - } -#endif - return length_samples << 1; -} - - -/* Encoder with char Output (old version) */ -int16_t WebRtcPcm16b_Encode(const int16_t *speech16b, +int16_t WebRtcPcm16b_Encode(const int16_t* speech, int16_t len, - unsigned char *speech8b) -{ - int16_t samples=len*2; - int16_t pos; - int16_t short1; - int16_t short2; - for (pos=0;pos>8; - speech8b[pos*2]=(unsigned char) short1; - speech8b[pos*2+1]=(unsigned char) short2; - } - return(samples); + uint8_t* encoded) { + int i; + for (i = 0; i < len; ++i) { + uint16_t s = speech[i]; + encoded[2 * i] = s >> 8; + encoded[2 * i + 1] = s; + } + return 2 * len; } - -/* Decoder with int16_t Input instead of char when the int16_t Encoder is used */ -int16_t WebRtcPcm16b_DecodeW16(int16_t *speechIn16b, - int16_t length_bytes, - int16_t *speechOut16b, - int16_t* speechType) -{ -#ifdef WEBRTC_ARCH_BIG_ENDIAN - memcpy(speechOut16b, speechIn16b, length_bytes); -#else - int i; - int samples = length_bytes >> 1; - - for (i=0;i>8)|(((uint16_t)(speechIn16b[i]&0xFF))<<8); - } -#endif - - *speechType=1; - - return length_bytes >> 1; -} - -/* "old" version of the decoder that uses char as input (not used in NetEq any more) */ -int16_t WebRtcPcm16b_Decode(unsigned char *speech8b, +int16_t WebRtcPcm16b_Decode(const uint8_t* encoded, int16_t len, - int16_t *speech16b) -{ - int16_t samples=len>>1; - int16_t pos; - int16_t shortval; - for (pos=0;pos(const_cast(encoded)), - static_cast(encoded_len), decoded, &temp_type); - *speech_type = ConvertSpeechType(temp_type); + int16_t ret = + WebRtcPcm16b_Decode(encoded, static_cast(encoded_len), decoded); + *speech_type = ConvertSpeechType(1); return ret; } diff --git a/webrtc/modules/audio_coding/neteq/mock/mock_external_decoder_pcm16b.h b/webrtc/modules/audio_coding/neteq/mock/mock_external_decoder_pcm16b.h index 68b4c1182..974976921 100644 --- a/webrtc/modules/audio_coding/neteq/mock/mock_external_decoder_pcm16b.h +++ b/webrtc/modules/audio_coding/neteq/mock/mock_external_decoder_pcm16b.h @@ -31,11 +31,9 @@ class ExternalPcm16B : public AudioDecoder { virtual int Decode(const uint8_t* encoded, size_t encoded_len, int16_t* decoded, SpeechType* speech_type) { - int16_t temp_type; - int16_t ret = WebRtcPcm16b_DecodeW16( - reinterpret_cast(const_cast(encoded)), - static_cast(encoded_len), decoded, &temp_type); - *speech_type = ConvertSpeechType(temp_type); + int16_t ret = WebRtcPcm16b_Decode( + encoded, static_cast(encoded_len), decoded); + *speech_type = ConvertSpeechType(1); return ret; } diff --git a/webrtc/modules/audio_coding/neteq/neteq_unittest.cc b/webrtc/modules/audio_coding/neteq/neteq_unittest.cc index f1e64282a..498cde359 100644 --- a/webrtc/modules/audio_coding/neteq/neteq_unittest.cc +++ b/webrtc/modules/audio_coding/neteq/neteq_unittest.cc @@ -912,10 +912,8 @@ class NetEqBgnTest : public NetEqDecodingTest { uint32_t receive_timestamp = 0; for (int n = 0; n < 10; ++n) { // Insert few packets and get audio. - int16_t enc_len_bytes = - WebRtcPcm16b_EncodeW16(input.GetNextBlock(), - expected_samples_per_channel, - reinterpret_cast(payload)); + int16_t enc_len_bytes = WebRtcPcm16b_Encode( + input.GetNextBlock(), expected_samples_per_channel, payload); ASSERT_EQ(enc_len_bytes, expected_samples_per_channel * 2); number_channels = 0; diff --git a/webrtc/modules/audio_coding/neteq/test/RTPencode.cc b/webrtc/modules/audio_coding/neteq/test/RTPencode.cc index 9aa207d54..e6d8f2e21 100644 --- a/webrtc/modules/audio_coding/neteq/test/RTPencode.cc +++ b/webrtc/modules/audio_coding/neteq/test/RTPencode.cc @@ -1569,7 +1569,7 @@ int NetEQTest_encode(int coder, int16_t *indata, int frameLen, unsigned char * e #ifdef CODEC_PCM16B else if ((coder==webrtc::kDecoderPCM16B)||(coder==webrtc::kDecoderPCM16Bwb)|| (coder==webrtc::kDecoderPCM16Bswb32kHz)||(coder==webrtc::kDecoderPCM16Bswb48kHz)) { /*pcm16b (8kHz, 16kHz, 32kHz or 48kHz) */ - cdlen = WebRtcPcm16b_EncodeW16(indata, frameLen, (int16_t*) encoded); + cdlen = WebRtcPcm16b_Encode(indata, frameLen, encoded); } #endif #ifdef CODEC_G722 diff --git a/webrtc/modules/audio_coding/neteq/tools/constant_pcm_packet_source.cc b/webrtc/modules/audio_coding/neteq/tools/constant_pcm_packet_source.cc index a92f41255..3942c13bb 100644 --- a/webrtc/modules/audio_coding/neteq/tools/constant_pcm_packet_source.cc +++ b/webrtc/modules/audio_coding/neteq/tools/constant_pcm_packet_source.cc @@ -31,7 +31,7 @@ ConstantPcmPacketSource::ConstantPcmPacketSource(size_t payload_len_samples, seq_number_(0), timestamp_(0), payload_ssrc_(0xABCD1234) { - int encoded_len = WebRtcPcm16b_EncodeW16(&sample_value, 1, &encoded_sample_); + int encoded_len = WebRtcPcm16b_Encode(&sample_value, 1, encoded_sample_); CHECK_EQ(encoded_len, 2); } @@ -39,9 +39,8 @@ Packet* ConstantPcmPacketSource::NextPacket() { CHECK_GT(packet_len_bytes_, kHeaderLenBytes); uint8_t* packet_memory = new uint8_t[packet_len_bytes_]; // Fill the payload part of the packet memory with the pre-encoded value. - std::fill_n(reinterpret_cast(packet_memory + kHeaderLenBytes), - payload_len_samples_, - encoded_sample_); + for (unsigned i = 0; i < 2 * payload_len_samples_; ++i) + packet_memory[kHeaderLenBytes + i] = encoded_sample_[i % 2]; WriteHeader(packet_memory); // |packet| assumes ownership of |packet_memory|. Packet* packet = diff --git a/webrtc/modules/audio_coding/neteq/tools/constant_pcm_packet_source.h b/webrtc/modules/audio_coding/neteq/tools/constant_pcm_packet_source.h index 8a4bbf172..fcf4f1320 100644 --- a/webrtc/modules/audio_coding/neteq/tools/constant_pcm_packet_source.h +++ b/webrtc/modules/audio_coding/neteq/tools/constant_pcm_packet_source.h @@ -41,7 +41,7 @@ class ConstantPcmPacketSource : public PacketSource { const size_t kHeaderLenBytes = 12; const size_t payload_len_samples_; const size_t packet_len_bytes_; - int16_t encoded_sample_; + uint8_t encoded_sample_[2]; const int samples_per_ms_; double next_arrival_time_ms_; const int payload_type_;