blob: 5af52bb49df2e42ec73684c5232bcb7b2ed961c9 [file] [log] [blame]
niklase@google.com470e71d2011-07-07 08:21:25 +00001/*
leozwang@webrtc.org91b359e2012-02-28 17:26:14 +00002 * Copyright (c) 2012 The WebRTC project authors. All Rights Reserved.
niklase@google.com470e71d2011-07-07 08:21:25 +00003 *
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 <stdlib.h>
26
pbos@webrtc.orge728ee02014-12-17 13:43:55 +000027int16_t WebRtcIlbcfix_EncoderAssign(IlbcEncoderInstance** iLBC_encinst,
28 int16_t* ILBCENC_inst_Addr,
29 int16_t* size) {
30 *iLBC_encinst=(IlbcEncoderInstance*)ILBCENC_inst_Addr;
pbos@webrtc.org0946a562013-04-09 00:28:06 +000031 *size=sizeof(iLBC_Enc_Inst_t)/sizeof(int16_t);
niklase@google.com470e71d2011-07-07 08:21:25 +000032 if (*iLBC_encinst!=NULL) {
33 return(0);
34 } else {
35 return(-1);
36 }
37}
38
pbos@webrtc.orge728ee02014-12-17 13:43:55 +000039int16_t WebRtcIlbcfix_DecoderAssign(IlbcDecoderInstance** iLBC_decinst,
40 int16_t* ILBCDEC_inst_Addr,
41 int16_t* size) {
42 *iLBC_decinst=(IlbcDecoderInstance*)ILBCDEC_inst_Addr;
pbos@webrtc.org0946a562013-04-09 00:28:06 +000043 *size=sizeof(iLBC_Dec_Inst_t)/sizeof(int16_t);
niklase@google.com470e71d2011-07-07 08:21:25 +000044 if (*iLBC_decinst!=NULL) {
45 return(0);
46 } else {
47 return(-1);
48 }
49}
50
pbos@webrtc.orge728ee02014-12-17 13:43:55 +000051int16_t WebRtcIlbcfix_EncoderCreate(IlbcEncoderInstance **iLBC_encinst) {
52 *iLBC_encinst=(IlbcEncoderInstance*)malloc(sizeof(iLBC_Enc_Inst_t));
niklase@google.com470e71d2011-07-07 08:21:25 +000053 if (*iLBC_encinst!=NULL) {
kma@webrtc.orgac4d70d2012-10-05 00:19:01 +000054 WebRtcSpl_Init();
niklase@google.com470e71d2011-07-07 08:21:25 +000055 return(0);
56 } else {
57 return(-1);
58 }
59}
60
pbos@webrtc.orge728ee02014-12-17 13:43:55 +000061int16_t WebRtcIlbcfix_DecoderCreate(IlbcDecoderInstance **iLBC_decinst) {
62 *iLBC_decinst=(IlbcDecoderInstance*)malloc(sizeof(iLBC_Dec_Inst_t));
niklase@google.com470e71d2011-07-07 08:21:25 +000063 if (*iLBC_decinst!=NULL) {
kma@webrtc.orgac4d70d2012-10-05 00:19:01 +000064 WebRtcSpl_Init();
niklase@google.com470e71d2011-07-07 08:21:25 +000065 return(0);
66 } else {
67 return(-1);
68 }
69}
70
pbos@webrtc.orge728ee02014-12-17 13:43:55 +000071int16_t WebRtcIlbcfix_EncoderFree(IlbcEncoderInstance *iLBC_encinst) {
niklase@google.com470e71d2011-07-07 08:21:25 +000072 free(iLBC_encinst);
73 return(0);
74}
75
pbos@webrtc.orge728ee02014-12-17 13:43:55 +000076int16_t WebRtcIlbcfix_DecoderFree(IlbcDecoderInstance *iLBC_decinst) {
niklase@google.com470e71d2011-07-07 08:21:25 +000077 free(iLBC_decinst);
78 return(0);
79}
80
pbos@webrtc.orge728ee02014-12-17 13:43:55 +000081int16_t WebRtcIlbcfix_EncoderInit(IlbcEncoderInstance* iLBCenc_inst,
82 int16_t mode) {
niklase@google.com470e71d2011-07-07 08:21:25 +000083 if ((mode==20)||(mode==30)) {
84 WebRtcIlbcfix_InitEncode((iLBC_Enc_Inst_t*) iLBCenc_inst, mode);
85 return(0);
86 } else {
87 return(-1);
88 }
89}
90
pbos@webrtc.orge728ee02014-12-17 13:43:55 +000091int16_t WebRtcIlbcfix_Encode(IlbcEncoderInstance* iLBCenc_inst,
kwiberg@webrtc.orgcb858ba2014-12-08 17:11:44 +000092 const int16_t* speechIn,
93 int16_t len,
94 uint8_t* encoded) {
pbos@webrtc.org0946a562013-04-09 00:28:06 +000095 int16_t pos = 0;
96 int16_t encpos = 0;
niklase@google.com470e71d2011-07-07 08:21:25 +000097
98 if ((len != ((iLBC_Enc_Inst_t*)iLBCenc_inst)->blockl) &&
99#ifdef SPLIT_10MS
100 (len != 80) &&
101#endif
102 (len != 2*((iLBC_Enc_Inst_t*)iLBCenc_inst)->blockl) &&
103 (len != 3*((iLBC_Enc_Inst_t*)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) {
kwiberg@webrtc.orgcb858ba2014-12-08 17:11:44 +0000111 WebRtcIlbcfix_EncodeImpl((uint16_t*)&encoded[2 * encpos], &speechIn[pos],
112 (iLBC_Enc_Inst_t*)iLBCenc_inst);
niklase@google.com470e71d2011-07-07 08:21:25 +0000113#ifdef SPLIT_10MS
114 pos += 80;
115 if(((iLBC_Enc_Inst_t*)iLBCenc_inst)->section == 0)
116#else
117 pos += ((iLBC_Enc_Inst_t*)iLBCenc_inst)->blockl;
118#endif
119 encpos += ((iLBC_Enc_Inst_t*)iLBCenc_inst)->no_of_words;
120 }
121 return (encpos*2);
122 }
123}
124
pbos@webrtc.orge728ee02014-12-17 13:43:55 +0000125int16_t WebRtcIlbcfix_DecoderInit(IlbcDecoderInstance* iLBCdec_inst,
126 int16_t mode) {
niklase@google.com470e71d2011-07-07 08:21:25 +0000127 if ((mode==20)||(mode==30)) {
128 WebRtcIlbcfix_InitDecode((iLBC_Dec_Inst_t*) iLBCdec_inst, mode, 1);
129 return(0);
130 } else {
131 return(-1);
132 }
133}
pbos@webrtc.orge728ee02014-12-17 13:43:55 +0000134int16_t WebRtcIlbcfix_DecoderInit20Ms(IlbcDecoderInstance *iLBCdec_inst) {
niklase@google.com470e71d2011-07-07 08:21:25 +0000135 WebRtcIlbcfix_InitDecode((iLBC_Dec_Inst_t*) iLBCdec_inst, 20, 1);
136 return(0);
137}
pbos@webrtc.orge728ee02014-12-17 13:43:55 +0000138int16_t WebRtcIlbcfix_Decoderinit30Ms(IlbcDecoderInstance *iLBCdec_inst) {
niklase@google.com470e71d2011-07-07 08:21:25 +0000139 WebRtcIlbcfix_InitDecode((iLBC_Dec_Inst_t*) iLBCdec_inst, 30, 1);
140 return(0);
141}
142
143
pbos@webrtc.orge728ee02014-12-17 13:43:55 +0000144int16_t WebRtcIlbcfix_Decode(IlbcDecoderInstance *iLBCdec_inst,
pbos@webrtc.org0946a562013-04-09 00:28:06 +0000145 const int16_t *encoded,
146 int16_t len,
147 int16_t *decoded,
148 int16_t *speechType)
niklase@google.com470e71d2011-07-07 08:21:25 +0000149{
150 int i=0;
151 /* Allow for automatic switching between the frame sizes
152 (although you do get some discontinuity) */
153 if ((len==((iLBC_Dec_Inst_t*)iLBCdec_inst)->no_of_bytes)||
154 (len==2*((iLBC_Dec_Inst_t*)iLBCdec_inst)->no_of_bytes)||
155 (len==3*((iLBC_Dec_Inst_t*)iLBCdec_inst)->no_of_bytes)) {
156 /* ok, do nothing */
157 } else {
158 /* Test if the mode has changed */
159 if (((iLBC_Dec_Inst_t*)iLBCdec_inst)->mode==20) {
160 if ((len==NO_OF_BYTES_30MS)||
161 (len==2*NO_OF_BYTES_30MS)||
162 (len==3*NO_OF_BYTES_30MS)) {
pbos@webrtc.orge728ee02014-12-17 13:43:55 +0000163 WebRtcIlbcfix_InitDecode(
164 ((iLBC_Dec_Inst_t*)iLBCdec_inst), 30,
165 ((iLBC_Dec_Inst_t*)iLBCdec_inst)->use_enhancer);
niklase@google.com470e71d2011-07-07 08:21:25 +0000166 } else {
167 /* Unsupported frame length */
168 return(-1);
169 }
170 } else {
171 if ((len==NO_OF_BYTES_20MS)||
172 (len==2*NO_OF_BYTES_20MS)||
173 (len==3*NO_OF_BYTES_20MS)) {
pbos@webrtc.orge728ee02014-12-17 13:43:55 +0000174 WebRtcIlbcfix_InitDecode(
175 ((iLBC_Dec_Inst_t*)iLBCdec_inst), 20,
176 ((iLBC_Dec_Inst_t*)iLBCdec_inst)->use_enhancer);
niklase@google.com470e71d2011-07-07 08:21:25 +0000177 } else {
178 /* Unsupported frame length */
179 return(-1);
180 }
181 }
182 }
183
184 while ((i*((iLBC_Dec_Inst_t*)iLBCdec_inst)->no_of_bytes)<len) {
pbos@webrtc.orge728ee02014-12-17 13:43:55 +0000185 WebRtcIlbcfix_DecodeImpl(
186 &decoded[i * ((iLBC_Dec_Inst_t*)iLBCdec_inst)->blockl],
187 (const uint16_t*)&encoded
188 [i * ((iLBC_Dec_Inst_t*)iLBCdec_inst)->no_of_words],
189 (iLBC_Dec_Inst_t*)iLBCdec_inst, 1);
niklase@google.com470e71d2011-07-07 08:21:25 +0000190 i++;
191 }
192 /* iLBC does not support VAD/CNG yet */
193 *speechType=1;
194 return(i*((iLBC_Dec_Inst_t*)iLBCdec_inst)->blockl);
195}
196
pbos@webrtc.orge728ee02014-12-17 13:43:55 +0000197int16_t WebRtcIlbcfix_Decode20Ms(IlbcDecoderInstance *iLBCdec_inst,
pbos@webrtc.org0946a562013-04-09 00:28:06 +0000198 const int16_t *encoded,
199 int16_t len,
200 int16_t *decoded,
201 int16_t *speechType)
niklase@google.com470e71d2011-07-07 08:21:25 +0000202{
203 int i=0;
204 if ((len==((iLBC_Dec_Inst_t*)iLBCdec_inst)->no_of_bytes)||
205 (len==2*((iLBC_Dec_Inst_t*)iLBCdec_inst)->no_of_bytes)||
206 (len==3*((iLBC_Dec_Inst_t*)iLBCdec_inst)->no_of_bytes)) {
207 /* ok, do nothing */
208 } else {
209 return(-1);
210 }
211
212 while ((i*((iLBC_Dec_Inst_t*)iLBCdec_inst)->no_of_bytes)<len) {
pbos@webrtc.orge728ee02014-12-17 13:43:55 +0000213 WebRtcIlbcfix_DecodeImpl(
214 &decoded[i * ((iLBC_Dec_Inst_t*)iLBCdec_inst)->blockl],
215 (const uint16_t*)&encoded
216 [i * ((iLBC_Dec_Inst_t*)iLBCdec_inst)->no_of_words],
217 (iLBC_Dec_Inst_t*)iLBCdec_inst, 1);
niklase@google.com470e71d2011-07-07 08:21:25 +0000218 i++;
219 }
220 /* iLBC does not support VAD/CNG yet */
221 *speechType=1;
222 return(i*((iLBC_Dec_Inst_t*)iLBCdec_inst)->blockl);
223}
224
pbos@webrtc.orge728ee02014-12-17 13:43:55 +0000225int16_t WebRtcIlbcfix_Decode30Ms(IlbcDecoderInstance *iLBCdec_inst,
pbos@webrtc.org0946a562013-04-09 00:28:06 +0000226 const int16_t *encoded,
227 int16_t len,
228 int16_t *decoded,
229 int16_t *speechType)
niklase@google.com470e71d2011-07-07 08:21:25 +0000230{
231 int i=0;
232 if ((len==((iLBC_Dec_Inst_t*)iLBCdec_inst)->no_of_bytes)||
233 (len==2*((iLBC_Dec_Inst_t*)iLBCdec_inst)->no_of_bytes)||
234 (len==3*((iLBC_Dec_Inst_t*)iLBCdec_inst)->no_of_bytes)) {
235 /* ok, do nothing */
236 } else {
237 return(-1);
238 }
239
240 while ((i*((iLBC_Dec_Inst_t*)iLBCdec_inst)->no_of_bytes)<len) {
pbos@webrtc.orge728ee02014-12-17 13:43:55 +0000241 WebRtcIlbcfix_DecodeImpl(
242 &decoded[i * ((iLBC_Dec_Inst_t*)iLBCdec_inst)->blockl],
243 (const uint16_t*)&encoded
244 [i * ((iLBC_Dec_Inst_t*)iLBCdec_inst)->no_of_words],
245 (iLBC_Dec_Inst_t*)iLBCdec_inst, 1);
niklase@google.com470e71d2011-07-07 08:21:25 +0000246 i++;
247 }
248 /* iLBC does not support VAD/CNG yet */
249 *speechType=1;
250 return(i*((iLBC_Dec_Inst_t*)iLBCdec_inst)->blockl);
251}
252
pbos@webrtc.orge728ee02014-12-17 13:43:55 +0000253int16_t WebRtcIlbcfix_DecodePlc(IlbcDecoderInstance* iLBCdec_inst,
254 int16_t* decoded,
255 int16_t noOfLostFrames) {
niklase@google.com470e71d2011-07-07 08:21:25 +0000256 int i;
pbos@webrtc.org0946a562013-04-09 00:28:06 +0000257 uint16_t dummy;
niklase@google.com470e71d2011-07-07 08:21:25 +0000258
259 for (i=0;i<noOfLostFrames;i++) {
260 /* call decoder */
pbos@webrtc.orge728ee02014-12-17 13:43:55 +0000261 WebRtcIlbcfix_DecodeImpl(
262 &decoded[i * ((iLBC_Dec_Inst_t*)iLBCdec_inst)->blockl], &dummy,
263 (iLBC_Dec_Inst_t*)iLBCdec_inst, 0);
niklase@google.com470e71d2011-07-07 08:21:25 +0000264 }
265 return (noOfLostFrames*((iLBC_Dec_Inst_t*)iLBCdec_inst)->blockl);
266}
267
pbos@webrtc.orge728ee02014-12-17 13:43:55 +0000268int16_t WebRtcIlbcfix_NetEqPlc(IlbcDecoderInstance* iLBCdec_inst,
269 int16_t* decoded,
270 int16_t noOfLostFrames) {
niklase@google.com470e71d2011-07-07 08:21:25 +0000271 /* Two input parameters not used, but needed for function pointers in NetEQ */
andrew@webrtc.org9562a362011-08-31 18:50:12 +0000272 (void)(decoded = NULL);
273 (void)(noOfLostFrames = 0);
niklase@google.com470e71d2011-07-07 08:21:25 +0000274
275 WebRtcSpl_MemSetW16(((iLBC_Dec_Inst_t*)iLBCdec_inst)->enh_buf, 0, ENH_BUFL);
276 ((iLBC_Dec_Inst_t*)iLBCdec_inst)->prev_enh_pl = 2;
277
278 return (0);
279}
280
leozwang@webrtc.org91b359e2012-02-28 17:26:14 +0000281void WebRtcIlbcfix_version(char *version)
niklase@google.com470e71d2011-07-07 08:21:25 +0000282{
tina.legrand@webrtc.orga41b4ce2011-08-29 08:19:30 +0000283 strcpy((char*)version, "1.1.1");
niklase@google.com470e71d2011-07-07 08:21:25 +0000284}