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#include <string.h>
11#include "g711.h"
12#include "g711_interface.h"
13#include "webrtc/typedefs.h"
14
15int16_t WebRtcG711_EncodeA(void* state,
16                           int16_t* speechIn,
17                           int16_t len,
18                           int16_t* encoded) {
19  int n;
20  uint16_t tempVal, tempVal2;
21
22  // Set and discard to avoid getting warnings
23  (void)(state = NULL);
24
25  // Sanity check of input length
26  if (len < 0) {
27    return (-1);
28  }
29
30  // Loop over all samples
31  for (n = 0; n < len; n++) {
32    tempVal = (uint16_t) linear_to_alaw(speechIn[n]);
33
34#ifdef WEBRTC_ARCH_BIG_ENDIAN
35    if ((n & 0x1) == 1) {
36      encoded[n >> 1] |= ((uint16_t) tempVal);
37    } else {
38      encoded[n >> 1] = ((uint16_t) tempVal) << 8;
39    }
40#else
41    if ((n & 0x1) == 1) {
42      tempVal2 |= ((uint16_t) tempVal) << 8;
43      encoded[n >> 1] |= ((uint16_t) tempVal) << 8;
44    } else {
45      tempVal2 = ((uint16_t) tempVal);
46      encoded[n >> 1] = ((uint16_t) tempVal);
47    }
48#endif
49  }
50  return (len);
51}
52
53int16_t WebRtcG711_EncodeU(void* state,
54                           int16_t* speechIn,
55                           int16_t len,
56                           int16_t* encoded) {
57  int n;
58  uint16_t tempVal;
59
60  // Set and discard to avoid getting warnings
61  (void)(state = NULL);
62
63  // Sanity check of input length
64  if (len < 0) {
65    return (-1);
66  }
67
68  // Loop over all samples
69  for (n = 0; n < len; n++) {
70    tempVal = (uint16_t) linear_to_ulaw(speechIn[n]);
71
72#ifdef WEBRTC_ARCH_BIG_ENDIAN
73    if ((n & 0x1) == 1) {
74      encoded[n >> 1] |= ((uint16_t) tempVal);
75    } else {
76      encoded[n >> 1] = ((uint16_t) tempVal) << 8;
77    }
78#else
79    if ((n & 0x1) == 1) {
80      encoded[n >> 1] |= ((uint16_t) tempVal) << 8;
81    } else {
82      encoded[n >> 1] = ((uint16_t) tempVal);
83    }
84#endif
85  }
86  return (len);
87}
88
89int16_t WebRtcG711_DecodeA(void* state,
90                           int16_t* encoded,
91                           int16_t len,
92                           int16_t* decoded,
93                           int16_t* speechType) {
94  int n;
95  uint16_t tempVal;
96
97  // Set and discard to avoid getting warnings
98  (void)(state = NULL);
99
100  // Sanity check of input length
101  if (len < 0) {
102    return (-1);
103  }
104
105  for (n = 0; n < len; n++) {
106#ifdef WEBRTC_ARCH_BIG_ENDIAN
107    if ((n & 0x1) == 1) {
108      tempVal = ((uint16_t) encoded[n >> 1] & 0xFF);
109    } else {
110      tempVal = ((uint16_t) encoded[n >> 1] >> 8);
111    }
112#else
113    if ((n & 0x1) == 1) {
114      tempVal = (encoded[n >> 1] >> 8);
115    } else {
116      tempVal = (encoded[n >> 1] & 0xFF);
117    }
118#endif
119    decoded[n] = (int16_t) alaw_to_linear(tempVal);
120  }
121
122  *speechType = 1;
123  return (len);
124}
125
126int16_t WebRtcG711_DecodeU(void* state,
127                           int16_t* encoded,
128                           int16_t len,
129                           int16_t* decoded,
130                           int16_t* speechType) {
131  int n;
132  uint16_t tempVal;
133
134  // Set and discard to avoid getting warnings
135  (void)(state = NULL);
136
137  // Sanity check of input length
138  if (len < 0) {
139    return (-1);
140  }
141
142  for (n = 0; n < len; n++) {
143#ifdef WEBRTC_ARCH_BIG_ENDIAN
144    if ((n & 0x1) == 1) {
145      tempVal = ((uint16_t) encoded[n >> 1] & 0xFF);
146    } else {
147      tempVal = ((uint16_t) encoded[n >> 1] >> 8);
148    }
149#else
150    if ((n & 0x1) == 1) {
151      tempVal = (encoded[n >> 1] >> 8);
152    } else {
153      tempVal = (encoded[n >> 1] & 0xFF);
154    }
155#endif
156    decoded[n] = (int16_t) ulaw_to_linear(tempVal);
157  }
158
159  *speechType = 1;
160  return (len);
161}
162
163int WebRtcG711_DurationEst(void* state,
164                           const uint8_t* payload,
165                           int payload_length_bytes) {
166  (void) state;
167  (void) payload;
168  /* G.711 is one byte per sample, so we can just return the number of bytes. */
169  return payload_length_bytes;
170}
171
172int16_t WebRtcG711_Version(char* version, int16_t lenBytes) {
173  strncpy(version, "2.0.0", lenBytes);
174  return 0;
175}
176