blob: 04814b7673462faa1bd07fd5eae55c811989df30 [file] [log] [blame]
niklase@google.com470e71d2011-07-07 08:21:25 +00001/*
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.com74c640a2011-08-30 20:44:24 +000014#include <stdlib.h>
15
niklase@google.com470e71d2011-07-07 08:21:25 +000016#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.org0946a562013-04-09 00:28:06 +000027/* Encoder with int16_t Output */
28int16_t WebRtcPcm16b_EncodeW16(int16_t *speechIn16b,
29 int16_t len,
30 int16_t *speechOut16b)
niklase@google.com470e71d2011-07-07 08:21:25 +000031{
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.org0946a562013-04-09 00:28:06 +000037 speechOut16b[i]=(((uint16_t)speechIn16b[i])>>8)|((((uint16_t)speechIn16b[i])<<8)&0xFF00);
niklase@google.com470e71d2011-07-07 08:21:25 +000038 }
39#endif
40 return(len<<1);
41}
42
43
44/* Encoder with char Output (old version) */
pbos@webrtc.org0946a562013-04-09 00:28:06 +000045int16_t WebRtcPcm16b_Encode(int16_t *speech16b,
46 int16_t len,
47 unsigned char *speech8b)
niklase@google.com470e71d2011-07-07 08:21:25 +000048{
pbos@webrtc.org0946a562013-04-09 00:28:06 +000049 int16_t samples=len*2;
50 int16_t pos;
51 int16_t short1;
52 int16_t short2;
niklase@google.com470e71d2011-07-07 08:21:25 +000053 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.org0946a562013-04-09 00:28:06 +000064/* Decoder with int16_t Input instead of char when the int16_t Encoder is used */
65int16_t WebRtcPcm16b_DecodeW16(void *inst,
66 int16_t *speechIn16b,
67 int16_t len,
68 int16_t *speechOut16b,
69 int16_t* speechType)
niklase@google.com470e71d2011-07-07 08:21:25 +000070{
71#ifdef WEBRTC_BIG_ENDIAN
pbos@webrtc.org0946a562013-04-09 00:28:06 +000072 WEBRTC_SPL_MEMCPY_W8(speechOut16b, speechIn16b, ((len*sizeof(int16_t)+1)>>1));
niklase@google.com470e71d2011-07-07 08:21:25 +000073#else
74 int i;
75 int samples=len>>1;
76
77 for (i=0;i<samples;i++) {
pbos@webrtc.org0946a562013-04-09 00:28:06 +000078 speechOut16b[i]=(((uint16_t)speechIn16b[i])>>8)|(((uint16_t)(speechIn16b[i]&0xFF))<<8);
niklase@google.com470e71d2011-07-07 08:21:25 +000079 }
80#endif
81
82 *speechType=1;
tommi@webrtc.orgc7d5f622011-08-31 12:11:24 +000083
84 // Avoid warning.
85 (void)(inst = NULL);
86
niklase@google.com470e71d2011-07-07 08:21:25 +000087 return(len>>1);
88}
89
90/* "old" version of the decoder that uses char as input (not used in NetEq any more) */
pbos@webrtc.org0946a562013-04-09 00:28:06 +000091int16_t WebRtcPcm16b_Decode(unsigned char *speech8b,
92 int16_t len,
93 int16_t *speech16b)
niklase@google.com470e71d2011-07-07 08:21:25 +000094{
pbos@webrtc.org0946a562013-04-09 00:28:06 +000095 int16_t samples=len>>1;
96 int16_t pos;
97 int16_t shortval;
niklase@google.com470e71d2011-07-07 08:21:25 +000098 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}