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> |
bjornv@webrtc.org | d32c438 | 2014-08-25 11:19:05 +0000 | [diff] [blame] | 15 | #ifdef WEBRTC_ARCH_BIG_ENDIAN |
| 16 | #include <string.h> |
| 17 | #endif |
turajs@google.com | 74c640a | 2011-08-30 20:44:24 +0000 | [diff] [blame] | 18 | |
andresp@webrtc.org | 262e676 | 2014-09-04 13:28:48 +0000 | [diff] [blame] | 19 | #include "webrtc/typedefs.h" |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 20 | |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 21 | #define HIGHEND 0xFF00 |
| 22 | #define LOWEND 0xFF |
| 23 | |
| 24 | |
| 25 | |
pbos@webrtc.org | 0946a56 | 2013-04-09 00:28:06 +0000 | [diff] [blame] | 26 | /* Encoder with int16_t Output */ |
bjornv@webrtc.org | 4a616be | 2014-08-25 10:32:22 +0000 | [diff] [blame] | 27 | int16_t WebRtcPcm16b_EncodeW16(const int16_t* speechIn16b, |
bjornv@webrtc.org | d32c438 | 2014-08-25 11:19:05 +0000 | [diff] [blame] | 28 | int16_t length_samples, |
bjornv@webrtc.org | 4a616be | 2014-08-25 10:32:22 +0000 | [diff] [blame] | 29 | int16_t* speechOut16b) |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 30 | { |
andrew@webrtc.org | 621df67 | 2013-10-22 10:27:23 +0000 | [diff] [blame] | 31 | #ifdef WEBRTC_ARCH_BIG_ENDIAN |
bjornv@webrtc.org | d32c438 | 2014-08-25 11:19:05 +0000 | [diff] [blame] | 32 | memcpy(speechOut16b, speechIn16b, length_samples * sizeof(int16_t)); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 33 | #else |
| 34 | int i; |
bjornv@webrtc.org | d32c438 | 2014-08-25 11:19:05 +0000 | [diff] [blame] | 35 | for (i = 0; i < length_samples; i++) { |
pbos@webrtc.org | 0946a56 | 2013-04-09 00:28:06 +0000 | [diff] [blame] | 36 | 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] | 37 | } |
| 38 | #endif |
bjornv@webrtc.org | d32c438 | 2014-08-25 11:19:05 +0000 | [diff] [blame] | 39 | return length_samples << 1; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 40 | } |
| 41 | |
| 42 | |
| 43 | /* Encoder with char Output (old version) */ |
henrik.lundin@webrtc.org | fcbe36a | 2014-12-08 18:26:49 +0000 | [diff] [blame^] | 44 | int16_t WebRtcPcm16b_Encode(const int16_t *speech16b, |
pbos@webrtc.org | 0946a56 | 2013-04-09 00:28:06 +0000 | [diff] [blame] | 45 | int16_t len, |
| 46 | unsigned char *speech8b) |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 47 | { |
pbos@webrtc.org | 0946a56 | 2013-04-09 00:28:06 +0000 | [diff] [blame] | 48 | int16_t samples=len*2; |
| 49 | int16_t pos; |
| 50 | int16_t short1; |
| 51 | int16_t short2; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 52 | for (pos=0;pos<len;pos++) { |
| 53 | short1=HIGHEND & speech16b[pos]; |
| 54 | short2=LOWEND & speech16b[pos]; |
| 55 | short1=short1>>8; |
| 56 | speech8b[pos*2]=(unsigned char) short1; |
| 57 | speech8b[pos*2+1]=(unsigned char) short2; |
| 58 | } |
| 59 | return(samples); |
| 60 | } |
| 61 | |
| 62 | |
pbos@webrtc.org | 0946a56 | 2013-04-09 00:28:06 +0000 | [diff] [blame] | 63 | /* Decoder with int16_t Input instead of char when the int16_t Encoder is used */ |
kwiberg@webrtc.org | 6de75ca | 2014-11-04 13:29:24 +0000 | [diff] [blame] | 64 | int16_t WebRtcPcm16b_DecodeW16(int16_t *speechIn16b, |
bjornv@webrtc.org | d32c438 | 2014-08-25 11:19:05 +0000 | [diff] [blame] | 65 | int16_t length_bytes, |
pbos@webrtc.org | 0946a56 | 2013-04-09 00:28:06 +0000 | [diff] [blame] | 66 | int16_t *speechOut16b, |
| 67 | int16_t* speechType) |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 68 | { |
andrew@webrtc.org | 621df67 | 2013-10-22 10:27:23 +0000 | [diff] [blame] | 69 | #ifdef WEBRTC_ARCH_BIG_ENDIAN |
bjornv@webrtc.org | d32c438 | 2014-08-25 11:19:05 +0000 | [diff] [blame] | 70 | memcpy(speechOut16b, speechIn16b, length_bytes); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 71 | #else |
| 72 | int i; |
bjornv@webrtc.org | d32c438 | 2014-08-25 11:19:05 +0000 | [diff] [blame] | 73 | int samples = length_bytes >> 1; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 74 | |
| 75 | for (i=0;i<samples;i++) { |
pbos@webrtc.org | 0946a56 | 2013-04-09 00:28:06 +0000 | [diff] [blame] | 76 | 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] | 77 | } |
| 78 | #endif |
| 79 | |
| 80 | *speechType=1; |
tommi@webrtc.org | c7d5f62 | 2011-08-31 12:11:24 +0000 | [diff] [blame] | 81 | |
bjornv@webrtc.org | d32c438 | 2014-08-25 11:19:05 +0000 | [diff] [blame] | 82 | return length_bytes >> 1; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 83 | } |
| 84 | |
| 85 | /* "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] | 86 | int16_t WebRtcPcm16b_Decode(unsigned char *speech8b, |
| 87 | int16_t len, |
| 88 | int16_t *speech16b) |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 89 | { |
pbos@webrtc.org | 0946a56 | 2013-04-09 00:28:06 +0000 | [diff] [blame] | 90 | int16_t samples=len>>1; |
| 91 | int16_t pos; |
| 92 | int16_t shortval; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 93 | for (pos=0;pos<samples;pos++) { |
| 94 | shortval=((unsigned short) speech8b[pos*2]); |
| 95 | shortval=(shortval<<8)&HIGHEND; |
| 96 | shortval=shortval|(((unsigned short) speech8b[pos*2+1])&LOWEND); |
| 97 | speech16b[pos]=shortval; |
| 98 | } |
| 99 | return(samples); |
| 100 | } |