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
14#include <stdlib.h>
15
16#include "typedefs.h"
17
18#ifdef WEBRTC_ARCH_BIG_ENDIAN
19#include "signal_processing_library.h"
20#endif
21
22#define HIGHEND 0xFF00
23#define LOWEND    0xFF
24
25
26
27/* Encoder with int16_t Output */
28int16_t WebRtcPcm16b_EncodeW16(int16_t *speechIn16b,
29                               int16_t len,
30                               int16_t *speechOut16b)
31{
32#ifdef WEBRTC_ARCH_BIG_ENDIAN
33    WEBRTC_SPL_MEMCPY_W16(speechOut16b, speechIn16b, len);
34#else
35    int i;
36    for (i=0;i<len;i++) {
37        speechOut16b[i]=(((uint16_t)speechIn16b[i])>>8)|((((uint16_t)speechIn16b[i])<<8)&0xFF00);
38    }
39#endif
40    return(len<<1);
41}
42
43
44/* Encoder with char Output (old version) */
45int16_t WebRtcPcm16b_Encode(int16_t *speech16b,
46                            int16_t len,
47                            unsigned char *speech8b)
48{
49    int16_t samples=len*2;
50    int16_t pos;
51    int16_t short1;
52    int16_t short2;
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
64/* 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)
70{
71#ifdef WEBRTC_ARCH_BIG_ENDIAN
72    WEBRTC_SPL_MEMCPY_W8(speechOut16b, speechIn16b, ((len*sizeof(int16_t)+1)>>1));
73#else
74    int i;
75    int samples=len>>1;
76
77    for (i=0;i<samples;i++) {
78        speechOut16b[i]=(((uint16_t)speechIn16b[i])>>8)|(((uint16_t)(speechIn16b[i]&0xFF))<<8);
79    }
80#endif
81
82    *speechType=1;
83
84    // Avoid warning.
85    (void)(inst = NULL);
86
87    return(len>>1);
88}
89
90/* "old" version of the decoder that uses char as input (not used in NetEq any more) */
91int16_t WebRtcPcm16b_Decode(unsigned char *speech8b,
92                            int16_t len,
93                            int16_t *speech16b)
94{
95    int16_t samples=len>>1;
96    int16_t pos;
97    int16_t shortval;
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}
106