blob: e41c095f82ead8a27b66be54251150500c5c5f18 [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.orgeb544462014-12-17 15:23:29 +000031 *size=sizeof(IlbcEncoder)/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.orgeb544462014-12-17 15:23:29 +000043 *size=sizeof(IlbcDecoder)/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) {
pbos@webrtc.orgeb544462014-12-17 15:23:29 +000052 *iLBC_encinst=(IlbcEncoderInstance*)malloc(sizeof(IlbcEncoder));
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) {
pbos@webrtc.orgeb544462014-12-17 15:23:29 +000062 *iLBC_decinst=(IlbcDecoderInstance*)malloc(sizeof(IlbcDecoder));
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)) {
pbos@webrtc.orgeb544462014-12-17 15:23:29 +000084 WebRtcIlbcfix_InitEncode((IlbcEncoder*) iLBCenc_inst, mode);
niklase@google.com470e71d2011-07-07 08:21:25 +000085 return(0);
86 } else {
87 return(-1);
88 }
89}
90
Peter Kasting83ad33a2015-06-09 17:19:57 -070091int WebRtcIlbcfix_Encode(IlbcEncoderInstance* iLBCenc_inst,
92 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
pbos@webrtc.orgeb544462014-12-17 15:23:29 +000098 if ((len != ((IlbcEncoder*)iLBCenc_inst)->blockl) &&
niklase@google.com470e71d2011-07-07 08:21:25 +000099#ifdef SPLIT_10MS
100 (len != 80) &&
101#endif
pbos@webrtc.orgeb544462014-12-17 15:23:29 +0000102 (len != 2*((IlbcEncoder*)iLBCenc_inst)->blockl) &&
103 (len != 3*((IlbcEncoder*)iLBCenc_inst)->blockl))
niklase@google.com470e71d2011-07-07 08:21:25 +0000104 {
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],
pbos@webrtc.orgeb544462014-12-17 15:23:29 +0000112 (IlbcEncoder*)iLBCenc_inst);
niklase@google.com470e71d2011-07-07 08:21:25 +0000113#ifdef SPLIT_10MS
114 pos += 80;
pbos@webrtc.orgeb544462014-12-17 15:23:29 +0000115 if(((IlbcEncoder*)iLBCenc_inst)->section == 0)
niklase@google.com470e71d2011-07-07 08:21:25 +0000116#else
pbos@webrtc.orgeb544462014-12-17 15:23:29 +0000117 pos += ((IlbcEncoder*)iLBCenc_inst)->blockl;
niklase@google.com470e71d2011-07-07 08:21:25 +0000118#endif
pbos@webrtc.orgeb544462014-12-17 15:23:29 +0000119 encpos += ((IlbcEncoder*)iLBCenc_inst)->no_of_words;
niklase@google.com470e71d2011-07-07 08:21:25 +0000120 }
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)) {
pbos@webrtc.orgeb544462014-12-17 15:23:29 +0000128 WebRtcIlbcfix_InitDecode((IlbcDecoder*) iLBCdec_inst, mode, 1);
niklase@google.com470e71d2011-07-07 08:21:25 +0000129 return(0);
130 } else {
131 return(-1);
132 }
133}
pbos@webrtc.orge728ee02014-12-17 13:43:55 +0000134int16_t WebRtcIlbcfix_DecoderInit20Ms(IlbcDecoderInstance *iLBCdec_inst) {
pbos@webrtc.orgeb544462014-12-17 15:23:29 +0000135 WebRtcIlbcfix_InitDecode((IlbcDecoder*) iLBCdec_inst, 20, 1);
niklase@google.com470e71d2011-07-07 08:21:25 +0000136 return(0);
137}
pbos@webrtc.orge728ee02014-12-17 13:43:55 +0000138int16_t WebRtcIlbcfix_Decoderinit30Ms(IlbcDecoderInstance *iLBCdec_inst) {
pbos@webrtc.orgeb544462014-12-17 15:23:29 +0000139 WebRtcIlbcfix_InitDecode((IlbcDecoder*) iLBCdec_inst, 30, 1);
niklase@google.com470e71d2011-07-07 08:21:25 +0000140 return(0);
141}
142
143
Peter Kasting83ad33a2015-06-09 17:19:57 -0700144int WebRtcIlbcfix_Decode(IlbcDecoderInstance* iLBCdec_inst,
145 const uint8_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) */
pbos@webrtc.orgeb544462014-12-17 15:23:29 +0000153 if ((len==((IlbcDecoder*)iLBCdec_inst)->no_of_bytes)||
154 (len==2*((IlbcDecoder*)iLBCdec_inst)->no_of_bytes)||
155 (len==3*((IlbcDecoder*)iLBCdec_inst)->no_of_bytes)) {
niklase@google.com470e71d2011-07-07 08:21:25 +0000156 /* ok, do nothing */
157 } else {
158 /* Test if the mode has changed */
pbos@webrtc.orgeb544462014-12-17 15:23:29 +0000159 if (((IlbcDecoder*)iLBCdec_inst)->mode==20) {
niklase@google.com470e71d2011-07-07 08:21:25 +0000160 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(
pbos@webrtc.orgeb544462014-12-17 15:23:29 +0000164 ((IlbcDecoder*)iLBCdec_inst), 30,
165 ((IlbcDecoder*)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(
pbos@webrtc.orgeb544462014-12-17 15:23:29 +0000175 ((IlbcDecoder*)iLBCdec_inst), 20,
176 ((IlbcDecoder*)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
pbos@webrtc.orgeb544462014-12-17 15:23:29 +0000184 while ((i*((IlbcDecoder*)iLBCdec_inst)->no_of_bytes)<len) {
pbos@webrtc.orge728ee02014-12-17 13:43:55 +0000185 WebRtcIlbcfix_DecodeImpl(
pbos@webrtc.orgeb544462014-12-17 15:23:29 +0000186 &decoded[i * ((IlbcDecoder*)iLBCdec_inst)->blockl],
pbos@webrtc.orge728ee02014-12-17 13:43:55 +0000187 (const uint16_t*)&encoded
jmarusic@webrtc.org71b35a42015-02-17 16:02:18 +0000188 [2 * i * ((IlbcDecoder*)iLBCdec_inst)->no_of_words],
pbos@webrtc.orgeb544462014-12-17 15:23:29 +0000189 (IlbcDecoder*)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;
pbos@webrtc.orgeb544462014-12-17 15:23:29 +0000194 return(i*((IlbcDecoder*)iLBCdec_inst)->blockl);
niklase@google.com470e71d2011-07-07 08:21:25 +0000195}
196
Peter Kasting83ad33a2015-06-09 17:19:57 -0700197int WebRtcIlbcfix_Decode20Ms(IlbcDecoderInstance* iLBCdec_inst,
198 const uint8_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;
pbos@webrtc.orgeb544462014-12-17 15:23:29 +0000204 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)) {
niklase@google.com470e71d2011-07-07 08:21:25 +0000207 /* ok, do nothing */
208 } else {
209 return(-1);
210 }
211
pbos@webrtc.orgeb544462014-12-17 15:23:29 +0000212 while ((i*((IlbcDecoder*)iLBCdec_inst)->no_of_bytes)<len) {
pbos@webrtc.orge728ee02014-12-17 13:43:55 +0000213 WebRtcIlbcfix_DecodeImpl(
pbos@webrtc.orgeb544462014-12-17 15:23:29 +0000214 &decoded[i * ((IlbcDecoder*)iLBCdec_inst)->blockl],
pbos@webrtc.orge728ee02014-12-17 13:43:55 +0000215 (const uint16_t*)&encoded
jmarusic@webrtc.org71b35a42015-02-17 16:02:18 +0000216 [2 * i * ((IlbcDecoder*)iLBCdec_inst)->no_of_words],
pbos@webrtc.orgeb544462014-12-17 15:23:29 +0000217 (IlbcDecoder*)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;
pbos@webrtc.orgeb544462014-12-17 15:23:29 +0000222 return(i*((IlbcDecoder*)iLBCdec_inst)->blockl);
niklase@google.com470e71d2011-07-07 08:21:25 +0000223}
224
Peter Kasting83ad33a2015-06-09 17:19:57 -0700225int WebRtcIlbcfix_Decode30Ms(IlbcDecoderInstance* iLBCdec_inst,
226 const uint8_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;
pbos@webrtc.orgeb544462014-12-17 15:23:29 +0000232 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)) {
niklase@google.com470e71d2011-07-07 08:21:25 +0000235 /* ok, do nothing */
236 } else {
237 return(-1);
238 }
239
pbos@webrtc.orgeb544462014-12-17 15:23:29 +0000240 while ((i*((IlbcDecoder*)iLBCdec_inst)->no_of_bytes)<len) {
pbos@webrtc.orge728ee02014-12-17 13:43:55 +0000241 WebRtcIlbcfix_DecodeImpl(
pbos@webrtc.orgeb544462014-12-17 15:23:29 +0000242 &decoded[i * ((IlbcDecoder*)iLBCdec_inst)->blockl],
pbos@webrtc.orge728ee02014-12-17 13:43:55 +0000243 (const uint16_t*)&encoded
jmarusic@webrtc.org71b35a42015-02-17 16:02:18 +0000244 [2 * i * ((IlbcDecoder*)iLBCdec_inst)->no_of_words],
pbos@webrtc.orgeb544462014-12-17 15:23:29 +0000245 (IlbcDecoder*)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;
pbos@webrtc.orgeb544462014-12-17 15:23:29 +0000250 return(i*((IlbcDecoder*)iLBCdec_inst)->blockl);
niklase@google.com470e71d2011-07-07 08:21:25 +0000251}
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(
pbos@webrtc.orgeb544462014-12-17 15:23:29 +0000262 &decoded[i * ((IlbcDecoder*)iLBCdec_inst)->blockl], &dummy,
263 (IlbcDecoder*)iLBCdec_inst, 0);
niklase@google.com470e71d2011-07-07 08:21:25 +0000264 }
pbos@webrtc.orgeb544462014-12-17 15:23:29 +0000265 return (noOfLostFrames*((IlbcDecoder*)iLBCdec_inst)->blockl);
niklase@google.com470e71d2011-07-07 08:21:25 +0000266}
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
pbos@webrtc.orgeb544462014-12-17 15:23:29 +0000275 WebRtcSpl_MemSetW16(((IlbcDecoder*)iLBCdec_inst)->enh_buf, 0, ENH_BUFL);
276 ((IlbcDecoder*)iLBCdec_inst)->prev_enh_pl = 2;
niklase@google.com470e71d2011-07-07 08:21:25 +0000277
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}