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