1 /*
2 * Copyright (c) 2012 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
13 iLBC Speech Coder ANSI-C Source Code
14
15 iLBCInterface.c
16
17 ******************************************************************/
18
19 #include "modules/audio_coding/codecs/ilbc/ilbc.h"
20
21 #include <stdlib.h>
22
23 #include "modules/audio_coding/codecs/ilbc/decode.h"
24 #include "modules/audio_coding/codecs/ilbc/defines.h"
25 #include "modules/audio_coding/codecs/ilbc/encode.h"
26 #include "modules/audio_coding/codecs/ilbc/init_decode.h"
27 #include "modules/audio_coding/codecs/ilbc/init_encode.h"
28 #include "rtc_base/checks.h"
29
WebRtcIlbcfix_EncoderAssign(IlbcEncoderInstance ** iLBC_encinst,int16_t * ILBCENC_inst_Addr,int16_t * size)30 int16_t WebRtcIlbcfix_EncoderAssign(IlbcEncoderInstance** iLBC_encinst,
31 int16_t* ILBCENC_inst_Addr,
32 int16_t* size) {
33 *iLBC_encinst=(IlbcEncoderInstance*)ILBCENC_inst_Addr;
34 *size=sizeof(IlbcEncoder)/sizeof(int16_t);
35 if (*iLBC_encinst!=NULL) {
36 return(0);
37 } else {
38 return(-1);
39 }
40 }
41
WebRtcIlbcfix_DecoderAssign(IlbcDecoderInstance ** iLBC_decinst,int16_t * ILBCDEC_inst_Addr,int16_t * size)42 int16_t WebRtcIlbcfix_DecoderAssign(IlbcDecoderInstance** iLBC_decinst,
43 int16_t* ILBCDEC_inst_Addr,
44 int16_t* size) {
45 *iLBC_decinst=(IlbcDecoderInstance*)ILBCDEC_inst_Addr;
46 *size=sizeof(IlbcDecoder)/sizeof(int16_t);
47 if (*iLBC_decinst!=NULL) {
48 return(0);
49 } else {
50 return(-1);
51 }
52 }
53
WebRtcIlbcfix_EncoderCreate(IlbcEncoderInstance ** iLBC_encinst)54 int16_t WebRtcIlbcfix_EncoderCreate(IlbcEncoderInstance **iLBC_encinst) {
55 *iLBC_encinst=(IlbcEncoderInstance*)malloc(sizeof(IlbcEncoder));
56 if (*iLBC_encinst!=NULL) {
57 return(0);
58 } else {
59 return(-1);
60 }
61 }
62
WebRtcIlbcfix_DecoderCreate(IlbcDecoderInstance ** iLBC_decinst)63 int16_t WebRtcIlbcfix_DecoderCreate(IlbcDecoderInstance **iLBC_decinst) {
64 *iLBC_decinst=(IlbcDecoderInstance*)malloc(sizeof(IlbcDecoder));
65 if (*iLBC_decinst!=NULL) {
66 return(0);
67 } else {
68 return(-1);
69 }
70 }
71
WebRtcIlbcfix_EncoderFree(IlbcEncoderInstance * iLBC_encinst)72 int16_t WebRtcIlbcfix_EncoderFree(IlbcEncoderInstance *iLBC_encinst) {
73 free(iLBC_encinst);
74 return(0);
75 }
76
WebRtcIlbcfix_DecoderFree(IlbcDecoderInstance * iLBC_decinst)77 int16_t WebRtcIlbcfix_DecoderFree(IlbcDecoderInstance *iLBC_decinst) {
78 free(iLBC_decinst);
79 return(0);
80 }
81
WebRtcIlbcfix_EncoderInit(IlbcEncoderInstance * iLBCenc_inst,int16_t mode)82 int16_t WebRtcIlbcfix_EncoderInit(IlbcEncoderInstance* iLBCenc_inst,
83 int16_t mode) {
84 if ((mode==20)||(mode==30)) {
85 WebRtcIlbcfix_InitEncode((IlbcEncoder*) iLBCenc_inst, mode);
86 return(0);
87 } else {
88 return(-1);
89 }
90 }
91
WebRtcIlbcfix_Encode(IlbcEncoderInstance * iLBCenc_inst,const int16_t * speechIn,size_t len,uint8_t * encoded)92 int WebRtcIlbcfix_Encode(IlbcEncoderInstance* iLBCenc_inst,
93 const int16_t* speechIn,
94 size_t len,
95 uint8_t* encoded) {
96 size_t pos = 0;
97 size_t encpos = 0;
98
99 if ((len != ((IlbcEncoder*)iLBCenc_inst)->blockl) &&
100 #ifdef SPLIT_10MS
101 (len != 80) &&
102 #endif
103 (len != 2*((IlbcEncoder*)iLBCenc_inst)->blockl) &&
104 (len != 3*((IlbcEncoder*)iLBCenc_inst)->blockl))
105 {
106 /* A maximum of 3 frames/packet is allowed */
107 return(-1);
108 } else {
109
110 /* call encoder */
111 while (pos<len) {
112 WebRtcIlbcfix_EncodeImpl((uint16_t*)&encoded[2 * encpos], &speechIn[pos],
113 (IlbcEncoder*)iLBCenc_inst);
114 #ifdef SPLIT_10MS
115 pos += 80;
116 if(((IlbcEncoder*)iLBCenc_inst)->section == 0)
117 #else
118 pos += ((IlbcEncoder*)iLBCenc_inst)->blockl;
119 #endif
120 encpos += ((IlbcEncoder*)iLBCenc_inst)->no_of_words;
121 }
122 return (int)(encpos*2);
123 }
124 }
125
WebRtcIlbcfix_DecoderInit(IlbcDecoderInstance * iLBCdec_inst,int16_t mode)126 int16_t WebRtcIlbcfix_DecoderInit(IlbcDecoderInstance* iLBCdec_inst,
127 int16_t mode) {
128 if ((mode==20)||(mode==30)) {
129 WebRtcIlbcfix_InitDecode((IlbcDecoder*) iLBCdec_inst, mode, 1);
130 return(0);
131 } else {
132 return(-1);
133 }
134 }
WebRtcIlbcfix_DecoderInit20Ms(IlbcDecoderInstance * iLBCdec_inst)135 void WebRtcIlbcfix_DecoderInit20Ms(IlbcDecoderInstance* iLBCdec_inst) {
136 WebRtcIlbcfix_InitDecode((IlbcDecoder*) iLBCdec_inst, 20, 1);
137 }
WebRtcIlbcfix_Decoderinit30Ms(IlbcDecoderInstance * iLBCdec_inst)138 void WebRtcIlbcfix_Decoderinit30Ms(IlbcDecoderInstance* iLBCdec_inst) {
139 WebRtcIlbcfix_InitDecode((IlbcDecoder*) iLBCdec_inst, 30, 1);
140 }
141
142
WebRtcIlbcfix_Decode(IlbcDecoderInstance * iLBCdec_inst,const uint8_t * encoded,size_t len,int16_t * decoded,int16_t * speechType)143 int WebRtcIlbcfix_Decode(IlbcDecoderInstance* iLBCdec_inst,
144 const uint8_t* encoded,
145 size_t len,
146 int16_t* decoded,
147 int16_t* speechType)
148 {
149 size_t i=0;
150 /* Allow for automatic switching between the frame sizes
151 (although you do get some discontinuity) */
152 if ((len==((IlbcDecoder*)iLBCdec_inst)->no_of_bytes)||
153 (len==2*((IlbcDecoder*)iLBCdec_inst)->no_of_bytes)||
154 (len==3*((IlbcDecoder*)iLBCdec_inst)->no_of_bytes)) {
155 /* ok, do nothing */
156 } else {
157 /* Test if the mode has changed */
158 if (((IlbcDecoder*)iLBCdec_inst)->mode==20) {
159 if ((len==NO_OF_BYTES_30MS)||
160 (len==2*NO_OF_BYTES_30MS)||
161 (len==3*NO_OF_BYTES_30MS)) {
162 WebRtcIlbcfix_InitDecode(
163 ((IlbcDecoder*)iLBCdec_inst), 30,
164 ((IlbcDecoder*)iLBCdec_inst)->use_enhancer);
165 } else {
166 /* Unsupported frame length */
167 return(-1);
168 }
169 } else {
170 if ((len==NO_OF_BYTES_20MS)||
171 (len==2*NO_OF_BYTES_20MS)||
172 (len==3*NO_OF_BYTES_20MS)) {
173 WebRtcIlbcfix_InitDecode(
174 ((IlbcDecoder*)iLBCdec_inst), 20,
175 ((IlbcDecoder*)iLBCdec_inst)->use_enhancer);
176 } else {
177 /* Unsupported frame length */
178 return(-1);
179 }
180 }
181 }
182
183 while ((i*((IlbcDecoder*)iLBCdec_inst)->no_of_bytes)<len) {
184 if (WebRtcIlbcfix_DecodeImpl(
185 &decoded[i * ((IlbcDecoder*)iLBCdec_inst)->blockl],
186 (const uint16_t*)&encoded
187 [2 * i * ((IlbcDecoder*)iLBCdec_inst)->no_of_words],
188 (IlbcDecoder*)iLBCdec_inst, 1) == -1)
189 return -1;
190 i++;
191 }
192 /* iLBC does not support VAD/CNG yet */
193 *speechType=1;
194 return (int)(i*((IlbcDecoder*)iLBCdec_inst)->blockl);
195 }
196
WebRtcIlbcfix_Decode20Ms(IlbcDecoderInstance * iLBCdec_inst,const uint8_t * encoded,size_t len,int16_t * decoded,int16_t * speechType)197 int WebRtcIlbcfix_Decode20Ms(IlbcDecoderInstance* iLBCdec_inst,
198 const uint8_t* encoded,
199 size_t len,
200 int16_t* decoded,
201 int16_t* speechType)
202 {
203 size_t i=0;
204 if ((len==((IlbcDecoder*)iLBCdec_inst)->no_of_bytes)||
205 (len==2*((IlbcDecoder*)iLBCdec_inst)->no_of_bytes)||
206 (len==3*((IlbcDecoder*)iLBCdec_inst)->no_of_bytes)) {
207 /* ok, do nothing */
208 } else {
209 return(-1);
210 }
211
212 while ((i*((IlbcDecoder*)iLBCdec_inst)->no_of_bytes)<len) {
213 if (!WebRtcIlbcfix_DecodeImpl(
214 &decoded[i * ((IlbcDecoder*)iLBCdec_inst)->blockl],
215 (const uint16_t*)&encoded
216 [2 * i * ((IlbcDecoder*)iLBCdec_inst)->no_of_words],
217 (IlbcDecoder*)iLBCdec_inst, 1))
218 return -1;
219 i++;
220 }
221 /* iLBC does not support VAD/CNG yet */
222 *speechType=1;
223 return (int)(i*((IlbcDecoder*)iLBCdec_inst)->blockl);
224 }
225
WebRtcIlbcfix_Decode30Ms(IlbcDecoderInstance * iLBCdec_inst,const uint8_t * encoded,size_t len,int16_t * decoded,int16_t * speechType)226 int WebRtcIlbcfix_Decode30Ms(IlbcDecoderInstance* iLBCdec_inst,
227 const uint8_t* encoded,
228 size_t len,
229 int16_t* decoded,
230 int16_t* speechType)
231 {
232 size_t i=0;
233 if ((len==((IlbcDecoder*)iLBCdec_inst)->no_of_bytes)||
234 (len==2*((IlbcDecoder*)iLBCdec_inst)->no_of_bytes)||
235 (len==3*((IlbcDecoder*)iLBCdec_inst)->no_of_bytes)) {
236 /* ok, do nothing */
237 } else {
238 return(-1);
239 }
240
241 while ((i*((IlbcDecoder*)iLBCdec_inst)->no_of_bytes)<len) {
242 if (!WebRtcIlbcfix_DecodeImpl(
243 &decoded[i * ((IlbcDecoder*)iLBCdec_inst)->blockl],
244 (const uint16_t*)&encoded
245 [2 * i * ((IlbcDecoder*)iLBCdec_inst)->no_of_words],
246 (IlbcDecoder*)iLBCdec_inst, 1))
247 return -1;
248 i++;
249 }
250 /* iLBC does not support VAD/CNG yet */
251 *speechType=1;
252 return (int)(i*((IlbcDecoder*)iLBCdec_inst)->blockl);
253 }
254
WebRtcIlbcfix_DecodePlc(IlbcDecoderInstance * iLBCdec_inst,int16_t * decoded,size_t noOfLostFrames)255 size_t WebRtcIlbcfix_DecodePlc(IlbcDecoderInstance* iLBCdec_inst,
256 int16_t* decoded,
257 size_t noOfLostFrames) {
258 size_t i;
259 uint16_t dummy;
260
261 for (i=0;i<noOfLostFrames;i++) {
262 // PLC decoding shouldn't fail, because there is no external input data
263 // that can be bad.
264 int result = WebRtcIlbcfix_DecodeImpl(
265 &decoded[i * ((IlbcDecoder*)iLBCdec_inst)->blockl], &dummy,
266 (IlbcDecoder*)iLBCdec_inst, 0);
267 RTC_CHECK_EQ(result, 0);
268 }
269 return (noOfLostFrames*((IlbcDecoder*)iLBCdec_inst)->blockl);
270 }
271
WebRtcIlbcfix_NetEqPlc(IlbcDecoderInstance * iLBCdec_inst,int16_t * decoded,size_t noOfLostFrames)272 size_t WebRtcIlbcfix_NetEqPlc(IlbcDecoderInstance* iLBCdec_inst,
273 int16_t* decoded,
274 size_t noOfLostFrames) {
275 /* Two input parameters not used, but needed for function pointers in NetEQ */
276 (void)(decoded = NULL);
277 (void)(noOfLostFrames = 0);
278
279 WebRtcSpl_MemSetW16(((IlbcDecoder*)iLBCdec_inst)->enh_buf, 0, ENH_BUFL);
280 ((IlbcDecoder*)iLBCdec_inst)->prev_enh_pl = 2;
281
282 return (0);
283 }
284
WebRtcIlbcfix_version(char * version)285 void WebRtcIlbcfix_version(char *version)
286 {
287 strcpy((char*)version, "1.1.1");
288 }
289