niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (c) 2011 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 | |
| 11 | |
| 12 | #include "pcm16b.h" |
| 13 | |
turajs@google.com | 74c640a | 2011-08-30 20:44:24 +0000 | [diff] [blame] | 14 | #include <stdlib.h> |
| 15 | |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 16 | #include "typedefs.h" |
| 17 | |
| 18 | #ifdef WEBRTC_BIG_ENDIAN |
| 19 | #include "signal_processing_library.h" |
| 20 | #endif |
| 21 | |
| 22 | #define HIGHEND 0xFF00 |
| 23 | #define LOWEND 0xFF |
| 24 | |
| 25 | |
| 26 | |
pbos@webrtc.org | 0946a56 | 2013-04-09 00:28:06 +0000 | [diff] [blame] | 27 | /* Encoder with int16_t Output */ |
| 28 | int16_t WebRtcPcm16b_EncodeW16(int16_t *speechIn16b, |
| 29 | int16_t len, |
| 30 | int16_t *speechOut16b) |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 31 | { |
| 32 | #ifdef WEBRTC_BIG_ENDIAN |
| 33 | WEBRTC_SPL_MEMCPY_W16(speechOut16b, speechIn16b, len); |
| 34 | #else |
| 35 | int i; |
| 36 | for (i=0;i<len;i++) { |
pbos@webrtc.org | 0946a56 | 2013-04-09 00:28:06 +0000 | [diff] [blame] | 37 | speechOut16b[i]=(((uint16_t)speechIn16b[i])>>8)|((((uint16_t)speechIn16b[i])<<8)&0xFF00); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 38 | } |
| 39 | #endif |
| 40 | return(len<<1); |
| 41 | } |
| 42 | |
| 43 | |
| 44 | /* Encoder with char Output (old version) */ |
pbos@webrtc.org | 0946a56 | 2013-04-09 00:28:06 +0000 | [diff] [blame] | 45 | int16_t WebRtcPcm16b_Encode(int16_t *speech16b, |
| 46 | int16_t len, |
| 47 | unsigned char *speech8b) |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 48 | { |
pbos@webrtc.org | 0946a56 | 2013-04-09 00:28:06 +0000 | [diff] [blame] | 49 | int16_t samples=len*2; |
| 50 | int16_t pos; |
| 51 | int16_t short1; |
| 52 | int16_t short2; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 53 | for (pos=0;pos<len;pos++) { |
| 54 | short1=HIGHEND & speech16b[pos]; |
| 55 | short2=LOWEND & speech16b[pos]; |
| 56 | short1=short1>>8; |
| 57 | speech8b[pos*2]=(unsigned char) short1; |
| 58 | speech8b[pos*2+1]=(unsigned char) short2; |
| 59 | } |
| 60 | return(samples); |
| 61 | } |
| 62 | |
| 63 | |
pbos@webrtc.org | 0946a56 | 2013-04-09 00:28:06 +0000 | [diff] [blame] | 64 | /* Decoder with int16_t Input instead of char when the int16_t Encoder is used */ |
| 65 | int16_t WebRtcPcm16b_DecodeW16(void *inst, |
| 66 | int16_t *speechIn16b, |
| 67 | int16_t len, |
| 68 | int16_t *speechOut16b, |
| 69 | int16_t* speechType) |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 70 | { |
| 71 | #ifdef WEBRTC_BIG_ENDIAN |
pbos@webrtc.org | 0946a56 | 2013-04-09 00:28:06 +0000 | [diff] [blame] | 72 | WEBRTC_SPL_MEMCPY_W8(speechOut16b, speechIn16b, ((len*sizeof(int16_t)+1)>>1)); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 73 | #else |
| 74 | int i; |
| 75 | int samples=len>>1; |
| 76 | |
| 77 | for (i=0;i<samples;i++) { |
pbos@webrtc.org | 0946a56 | 2013-04-09 00:28:06 +0000 | [diff] [blame] | 78 | speechOut16b[i]=(((uint16_t)speechIn16b[i])>>8)|(((uint16_t)(speechIn16b[i]&0xFF))<<8); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 79 | } |
| 80 | #endif |
| 81 | |
| 82 | *speechType=1; |
tommi@webrtc.org | c7d5f62 | 2011-08-31 12:11:24 +0000 | [diff] [blame] | 83 | |
| 84 | // Avoid warning. |
| 85 | (void)(inst = NULL); |
| 86 | |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 87 | return(len>>1); |
| 88 | } |
| 89 | |
| 90 | /* "old" version of the decoder that uses char as input (not used in NetEq any more) */ |
pbos@webrtc.org | 0946a56 | 2013-04-09 00:28:06 +0000 | [diff] [blame] | 91 | int16_t WebRtcPcm16b_Decode(unsigned char *speech8b, |
| 92 | int16_t len, |
| 93 | int16_t *speech16b) |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 94 | { |
pbos@webrtc.org | 0946a56 | 2013-04-09 00:28:06 +0000 | [diff] [blame] | 95 | int16_t samples=len>>1; |
| 96 | int16_t pos; |
| 97 | int16_t shortval; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 98 | for (pos=0;pos<samples;pos++) { |
| 99 | shortval=((unsigned short) speech8b[pos*2]); |
| 100 | shortval=(shortval<<8)&HIGHEND; |
| 101 | shortval=shortval|(((unsigned short) speech8b[pos*2+1])&LOWEND); |
| 102 | speech16b[pos]=shortval; |
| 103 | } |
| 104 | return(samples); |
| 105 | } |