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 "ilbc.h"
20 #include "defines.h"
21 #include "init_encode.h"
22 #include "encode.h"
23 #include "init_decode.h"
24 #include "decode.h"
25 #include "rtc_base/checks.h"
26 #include <stdlib.h>
27 
WebRtcIlbcfix_EncoderAssign(IlbcEncoderInstance ** iLBC_encinst,int16_t * ILBCENC_inst_Addr,int16_t * size)28 int16_t WebRtcIlbcfix_EncoderAssign(IlbcEncoderInstance** iLBC_encinst,
29                                     int16_t* ILBCENC_inst_Addr,
30                                     int16_t* size) {
31   *iLBC_encinst=(IlbcEncoderInstance*)ILBCENC_inst_Addr;
32   *size=sizeof(IlbcEncoder)/sizeof(int16_t);
33   if (*iLBC_encinst!=NULL) {
34     return(0);
35   } else {
36     return(-1);
37   }
38 }
39 
WebRtcIlbcfix_DecoderAssign(IlbcDecoderInstance ** iLBC_decinst,int16_t * ILBCDEC_inst_Addr,int16_t * size)40 int16_t WebRtcIlbcfix_DecoderAssign(IlbcDecoderInstance** iLBC_decinst,
41                                     int16_t* ILBCDEC_inst_Addr,
42                                     int16_t* size) {
43   *iLBC_decinst=(IlbcDecoderInstance*)ILBCDEC_inst_Addr;
44   *size=sizeof(IlbcDecoder)/sizeof(int16_t);
45   if (*iLBC_decinst!=NULL) {
46     return(0);
47   } else {
48     return(-1);
49   }
50 }
51 
WebRtcIlbcfix_EncoderCreate(IlbcEncoderInstance ** iLBC_encinst)52 int16_t WebRtcIlbcfix_EncoderCreate(IlbcEncoderInstance **iLBC_encinst) {
53   *iLBC_encinst=(IlbcEncoderInstance*)malloc(sizeof(IlbcEncoder));
54   if (*iLBC_encinst!=NULL) {
55     WebRtcSpl_Init();
56     return(0);
57   } else {
58     return(-1);
59   }
60 }
61 
WebRtcIlbcfix_DecoderCreate(IlbcDecoderInstance ** iLBC_decinst)62 int16_t WebRtcIlbcfix_DecoderCreate(IlbcDecoderInstance **iLBC_decinst) {
63   *iLBC_decinst=(IlbcDecoderInstance*)malloc(sizeof(IlbcDecoder));
64   if (*iLBC_decinst!=NULL) {
65     WebRtcSpl_Init();
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     RTC_CHECK(WebRtcIlbcfix_DecodeImpl(
265         &decoded[i * ((IlbcDecoder*)iLBCdec_inst)->blockl], &dummy,
266         (IlbcDecoder*)iLBCdec_inst, 0));
267   }
268   return (noOfLostFrames*((IlbcDecoder*)iLBCdec_inst)->blockl);
269 }
270 
WebRtcIlbcfix_NetEqPlc(IlbcDecoderInstance * iLBCdec_inst,int16_t * decoded,size_t noOfLostFrames)271 size_t WebRtcIlbcfix_NetEqPlc(IlbcDecoderInstance* iLBCdec_inst,
272                               int16_t* decoded,
273                               size_t noOfLostFrames) {
274   /* Two input parameters not used, but needed for function pointers in NetEQ */
275   (void)(decoded = NULL);
276   (void)(noOfLostFrames = 0);
277 
278   WebRtcSpl_MemSetW16(((IlbcDecoder*)iLBCdec_inst)->enh_buf, 0, ENH_BUFL);
279   ((IlbcDecoder*)iLBCdec_inst)->prev_enh_pl = 2;
280 
281   return (0);
282 }
283 
WebRtcIlbcfix_version(char * version)284 void WebRtcIlbcfix_version(char *version)
285 {
286   strcpy((char*)version, "1.1.1");
287 }
288