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