blob: 21fc3f8ad0d5e3df1cce74909941594a6310af74 [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
niklase@google.com470e71d2011-07-07 08:21:25 +000019#include <stdlib.h>
20
Mirko Bonadei06c2aa92018-02-01 15:11:41 +010021#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
pbos@webrtc.orge728ee02014-12-17 13:43:55 +000029int16_t WebRtcIlbcfix_EncoderAssign(IlbcEncoderInstance** iLBC_encinst,
30 int16_t* ILBCENC_inst_Addr,
31 int16_t* size) {
32 *iLBC_encinst=(IlbcEncoderInstance*)ILBCENC_inst_Addr;
pbos@webrtc.orgeb544462014-12-17 15:23:29 +000033 *size=sizeof(IlbcEncoder)/sizeof(int16_t);
niklase@google.com470e71d2011-07-07 08:21:25 +000034 if (*iLBC_encinst!=NULL) {
35 return(0);
36 } else {
37 return(-1);
38 }
39}
40
pbos@webrtc.orge728ee02014-12-17 13:43:55 +000041int16_t WebRtcIlbcfix_DecoderAssign(IlbcDecoderInstance** iLBC_decinst,
42 int16_t* ILBCDEC_inst_Addr,
43 int16_t* size) {
44 *iLBC_decinst=(IlbcDecoderInstance*)ILBCDEC_inst_Addr;
pbos@webrtc.orgeb544462014-12-17 15:23:29 +000045 *size=sizeof(IlbcDecoder)/sizeof(int16_t);
niklase@google.com470e71d2011-07-07 08:21:25 +000046 if (*iLBC_decinst!=NULL) {
47 return(0);
48 } else {
49 return(-1);
50 }
51}
52
pbos@webrtc.orge728ee02014-12-17 13:43:55 +000053int16_t WebRtcIlbcfix_EncoderCreate(IlbcEncoderInstance **iLBC_encinst) {
pbos@webrtc.orgeb544462014-12-17 15:23:29 +000054 *iLBC_encinst=(IlbcEncoderInstance*)malloc(sizeof(IlbcEncoder));
niklase@google.com470e71d2011-07-07 08:21:25 +000055 if (*iLBC_encinst!=NULL) {
kma@webrtc.orgac4d70d2012-10-05 00:19:01 +000056 WebRtcSpl_Init();
niklase@google.com470e71d2011-07-07 08:21:25 +000057 return(0);
58 } else {
59 return(-1);
60 }
61}
62
pbos@webrtc.orge728ee02014-12-17 13:43:55 +000063int16_t WebRtcIlbcfix_DecoderCreate(IlbcDecoderInstance **iLBC_decinst) {
pbos@webrtc.orgeb544462014-12-17 15:23:29 +000064 *iLBC_decinst=(IlbcDecoderInstance*)malloc(sizeof(IlbcDecoder));
niklase@google.com470e71d2011-07-07 08:21:25 +000065 if (*iLBC_decinst!=NULL) {
kma@webrtc.orgac4d70d2012-10-05 00:19:01 +000066 WebRtcSpl_Init();
niklase@google.com470e71d2011-07-07 08:21:25 +000067 return(0);
68 } else {
69 return(-1);
70 }
71}
72
pbos@webrtc.orge728ee02014-12-17 13:43:55 +000073int16_t WebRtcIlbcfix_EncoderFree(IlbcEncoderInstance *iLBC_encinst) {
niklase@google.com470e71d2011-07-07 08:21:25 +000074 free(iLBC_encinst);
75 return(0);
76}
77
pbos@webrtc.orge728ee02014-12-17 13:43:55 +000078int16_t WebRtcIlbcfix_DecoderFree(IlbcDecoderInstance *iLBC_decinst) {
niklase@google.com470e71d2011-07-07 08:21:25 +000079 free(iLBC_decinst);
80 return(0);
81}
82
pbos@webrtc.orge728ee02014-12-17 13:43:55 +000083int16_t WebRtcIlbcfix_EncoderInit(IlbcEncoderInstance* iLBCenc_inst,
84 int16_t mode) {
niklase@google.com470e71d2011-07-07 08:21:25 +000085 if ((mode==20)||(mode==30)) {
pbos@webrtc.orgeb544462014-12-17 15:23:29 +000086 WebRtcIlbcfix_InitEncode((IlbcEncoder*) iLBCenc_inst, mode);
niklase@google.com470e71d2011-07-07 08:21:25 +000087 return(0);
88 } else {
89 return(-1);
90 }
91}
92
Peter Kastinga8b335c2015-06-11 18:51:20 -070093int WebRtcIlbcfix_Encode(IlbcEncoderInstance* iLBCenc_inst,
94 const int16_t* speechIn,
Peter Kastingdce40cf2015-08-24 14:52:23 -070095 size_t len,
Peter Kastinga8b335c2015-06-11 18:51:20 -070096 uint8_t* encoded) {
Peter Kastingdce40cf2015-08-24 14:52:23 -070097 size_t pos = 0;
98 size_t encpos = 0;
niklase@google.com470e71d2011-07-07 08:21:25 +000099
pbos@webrtc.orgeb544462014-12-17 15:23:29 +0000100 if ((len != ((IlbcEncoder*)iLBCenc_inst)->blockl) &&
niklase@google.com470e71d2011-07-07 08:21:25 +0000101#ifdef SPLIT_10MS
102 (len != 80) &&
103#endif
pbos@webrtc.orgeb544462014-12-17 15:23:29 +0000104 (len != 2*((IlbcEncoder*)iLBCenc_inst)->blockl) &&
105 (len != 3*((IlbcEncoder*)iLBCenc_inst)->blockl))
niklase@google.com470e71d2011-07-07 08:21:25 +0000106 {
107 /* A maximum of 3 frames/packet is allowed */
108 return(-1);
109 } else {
110
111 /* call encoder */
112 while (pos<len) {
kwiberg@webrtc.orgcb858ba2014-12-08 17:11:44 +0000113 WebRtcIlbcfix_EncodeImpl((uint16_t*)&encoded[2 * encpos], &speechIn[pos],
pbos@webrtc.orgeb544462014-12-17 15:23:29 +0000114 (IlbcEncoder*)iLBCenc_inst);
niklase@google.com470e71d2011-07-07 08:21:25 +0000115#ifdef SPLIT_10MS
116 pos += 80;
pbos@webrtc.orgeb544462014-12-17 15:23:29 +0000117 if(((IlbcEncoder*)iLBCenc_inst)->section == 0)
niklase@google.com470e71d2011-07-07 08:21:25 +0000118#else
pbos@webrtc.orgeb544462014-12-17 15:23:29 +0000119 pos += ((IlbcEncoder*)iLBCenc_inst)->blockl;
niklase@google.com470e71d2011-07-07 08:21:25 +0000120#endif
pbos@webrtc.orgeb544462014-12-17 15:23:29 +0000121 encpos += ((IlbcEncoder*)iLBCenc_inst)->no_of_words;
niklase@google.com470e71d2011-07-07 08:21:25 +0000122 }
Peter Kastingdce40cf2015-08-24 14:52:23 -0700123 return (int)(encpos*2);
niklase@google.com470e71d2011-07-07 08:21:25 +0000124 }
125}
126
pbos@webrtc.orge728ee02014-12-17 13:43:55 +0000127int16_t WebRtcIlbcfix_DecoderInit(IlbcDecoderInstance* iLBCdec_inst,
128 int16_t mode) {
niklase@google.com470e71d2011-07-07 08:21:25 +0000129 if ((mode==20)||(mode==30)) {
pbos@webrtc.orgeb544462014-12-17 15:23:29 +0000130 WebRtcIlbcfix_InitDecode((IlbcDecoder*) iLBCdec_inst, mode, 1);
niklase@google.com470e71d2011-07-07 08:21:25 +0000131 return(0);
132 } else {
133 return(-1);
134 }
135}
Karl Wiberg43766482015-08-27 15:22:11 +0200136void WebRtcIlbcfix_DecoderInit20Ms(IlbcDecoderInstance* iLBCdec_inst) {
pbos@webrtc.orgeb544462014-12-17 15:23:29 +0000137 WebRtcIlbcfix_InitDecode((IlbcDecoder*) iLBCdec_inst, 20, 1);
niklase@google.com470e71d2011-07-07 08:21:25 +0000138}
Karl Wiberg43766482015-08-27 15:22:11 +0200139void WebRtcIlbcfix_Decoderinit30Ms(IlbcDecoderInstance* iLBCdec_inst) {
pbos@webrtc.orgeb544462014-12-17 15:23:29 +0000140 WebRtcIlbcfix_InitDecode((IlbcDecoder*) iLBCdec_inst, 30, 1);
niklase@google.com470e71d2011-07-07 08:21:25 +0000141}
142
143
Peter Kastinga8b335c2015-06-11 18:51:20 -0700144int WebRtcIlbcfix_Decode(IlbcDecoderInstance* iLBCdec_inst,
145 const uint8_t* encoded,
Peter Kastingdce40cf2015-08-24 14:52:23 -0700146 size_t len,
Peter Kastinga8b335c2015-06-11 18:51:20 -0700147 int16_t* decoded,
148 int16_t* speechType)
niklase@google.com470e71d2011-07-07 08:21:25 +0000149{
Peter Kastingdce40cf2015-08-24 14:52:23 -0700150 size_t i=0;
niklase@google.com470e71d2011-07-07 08:21:25 +0000151 /* 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) {
kwiberg619a2112016-08-24 02:46:44 -0700185 if (WebRtcIlbcfix_DecodeImpl(
186 &decoded[i * ((IlbcDecoder*)iLBCdec_inst)->blockl],
187 (const uint16_t*)&encoded
188 [2 * i * ((IlbcDecoder*)iLBCdec_inst)->no_of_words],
189 (IlbcDecoder*)iLBCdec_inst, 1) == -1)
190 return -1;
niklase@google.com470e71d2011-07-07 08:21:25 +0000191 i++;
192 }
193 /* iLBC does not support VAD/CNG yet */
194 *speechType=1;
Peter Kastingdce40cf2015-08-24 14:52:23 -0700195 return (int)(i*((IlbcDecoder*)iLBCdec_inst)->blockl);
niklase@google.com470e71d2011-07-07 08:21:25 +0000196}
197
Peter Kastinga8b335c2015-06-11 18:51:20 -0700198int WebRtcIlbcfix_Decode20Ms(IlbcDecoderInstance* iLBCdec_inst,
199 const uint8_t* encoded,
Peter Kastingdce40cf2015-08-24 14:52:23 -0700200 size_t len,
Peter Kastinga8b335c2015-06-11 18:51:20 -0700201 int16_t* decoded,
202 int16_t* speechType)
niklase@google.com470e71d2011-07-07 08:21:25 +0000203{
Peter Kastingdce40cf2015-08-24 14:52:23 -0700204 size_t i=0;
pbos@webrtc.orgeb544462014-12-17 15:23:29 +0000205 if ((len==((IlbcDecoder*)iLBCdec_inst)->no_of_bytes)||
206 (len==2*((IlbcDecoder*)iLBCdec_inst)->no_of_bytes)||
207 (len==3*((IlbcDecoder*)iLBCdec_inst)->no_of_bytes)) {
niklase@google.com470e71d2011-07-07 08:21:25 +0000208 /* ok, do nothing */
209 } else {
210 return(-1);
211 }
212
pbos@webrtc.orgeb544462014-12-17 15:23:29 +0000213 while ((i*((IlbcDecoder*)iLBCdec_inst)->no_of_bytes)<len) {
kwiberg619a2112016-08-24 02:46:44 -0700214 if (!WebRtcIlbcfix_DecodeImpl(
pbos@webrtc.orgeb544462014-12-17 15:23:29 +0000215 &decoded[i * ((IlbcDecoder*)iLBCdec_inst)->blockl],
pbos@webrtc.orge728ee02014-12-17 13:43:55 +0000216 (const uint16_t*)&encoded
jmarusic@webrtc.org71b35a42015-02-17 16:02:18 +0000217 [2 * i * ((IlbcDecoder*)iLBCdec_inst)->no_of_words],
kwiberg619a2112016-08-24 02:46:44 -0700218 (IlbcDecoder*)iLBCdec_inst, 1))
219 return -1;
niklase@google.com470e71d2011-07-07 08:21:25 +0000220 i++;
221 }
222 /* iLBC does not support VAD/CNG yet */
223 *speechType=1;
Peter Kastingdce40cf2015-08-24 14:52:23 -0700224 return (int)(i*((IlbcDecoder*)iLBCdec_inst)->blockl);
niklase@google.com470e71d2011-07-07 08:21:25 +0000225}
226
Peter Kastinga8b335c2015-06-11 18:51:20 -0700227int WebRtcIlbcfix_Decode30Ms(IlbcDecoderInstance* iLBCdec_inst,
228 const uint8_t* encoded,
Peter Kastingdce40cf2015-08-24 14:52:23 -0700229 size_t len,
Peter Kastinga8b335c2015-06-11 18:51:20 -0700230 int16_t* decoded,
231 int16_t* speechType)
niklase@google.com470e71d2011-07-07 08:21:25 +0000232{
Peter Kastingdce40cf2015-08-24 14:52:23 -0700233 size_t i=0;
pbos@webrtc.orgeb544462014-12-17 15:23:29 +0000234 if ((len==((IlbcDecoder*)iLBCdec_inst)->no_of_bytes)||
235 (len==2*((IlbcDecoder*)iLBCdec_inst)->no_of_bytes)||
236 (len==3*((IlbcDecoder*)iLBCdec_inst)->no_of_bytes)) {
niklase@google.com470e71d2011-07-07 08:21:25 +0000237 /* ok, do nothing */
238 } else {
239 return(-1);
240 }
241
pbos@webrtc.orgeb544462014-12-17 15:23:29 +0000242 while ((i*((IlbcDecoder*)iLBCdec_inst)->no_of_bytes)<len) {
kwiberg619a2112016-08-24 02:46:44 -0700243 if (!WebRtcIlbcfix_DecodeImpl(
pbos@webrtc.orgeb544462014-12-17 15:23:29 +0000244 &decoded[i * ((IlbcDecoder*)iLBCdec_inst)->blockl],
pbos@webrtc.orge728ee02014-12-17 13:43:55 +0000245 (const uint16_t*)&encoded
jmarusic@webrtc.org71b35a42015-02-17 16:02:18 +0000246 [2 * i * ((IlbcDecoder*)iLBCdec_inst)->no_of_words],
kwiberg619a2112016-08-24 02:46:44 -0700247 (IlbcDecoder*)iLBCdec_inst, 1))
248 return -1;
niklase@google.com470e71d2011-07-07 08:21:25 +0000249 i++;
250 }
251 /* iLBC does not support VAD/CNG yet */
252 *speechType=1;
Peter Kastingdce40cf2015-08-24 14:52:23 -0700253 return (int)(i*((IlbcDecoder*)iLBCdec_inst)->blockl);
niklase@google.com470e71d2011-07-07 08:21:25 +0000254}
255
Peter Kastingdce40cf2015-08-24 14:52:23 -0700256size_t WebRtcIlbcfix_DecodePlc(IlbcDecoderInstance* iLBCdec_inst,
257 int16_t* decoded,
258 size_t noOfLostFrames) {
259 size_t i;
pbos@webrtc.org0946a562013-04-09 00:28:06 +0000260 uint16_t dummy;
niklase@google.com470e71d2011-07-07 08:21:25 +0000261
262 for (i=0;i<noOfLostFrames;i++) {
kwiberg619a2112016-08-24 02:46:44 -0700263 // PLC decoding shouldn't fail, because there is no external input data
264 // that can be bad.
265 RTC_CHECK(WebRtcIlbcfix_DecodeImpl(
pbos@webrtc.orgeb544462014-12-17 15:23:29 +0000266 &decoded[i * ((IlbcDecoder*)iLBCdec_inst)->blockl], &dummy,
kwiberg619a2112016-08-24 02:46:44 -0700267 (IlbcDecoder*)iLBCdec_inst, 0));
niklase@google.com470e71d2011-07-07 08:21:25 +0000268 }
pbos@webrtc.orgeb544462014-12-17 15:23:29 +0000269 return (noOfLostFrames*((IlbcDecoder*)iLBCdec_inst)->blockl);
niklase@google.com470e71d2011-07-07 08:21:25 +0000270}
271
Peter Kastingdce40cf2015-08-24 14:52:23 -0700272size_t WebRtcIlbcfix_NetEqPlc(IlbcDecoderInstance* iLBCdec_inst,
273 int16_t* decoded,
274 size_t noOfLostFrames) {
niklase@google.com470e71d2011-07-07 08:21:25 +0000275 /* Two input parameters not used, but needed for function pointers in NetEQ */
andrew@webrtc.org9562a362011-08-31 18:50:12 +0000276 (void)(decoded = NULL);
277 (void)(noOfLostFrames = 0);
niklase@google.com470e71d2011-07-07 08:21:25 +0000278
pbos@webrtc.orgeb544462014-12-17 15:23:29 +0000279 WebRtcSpl_MemSetW16(((IlbcDecoder*)iLBCdec_inst)->enh_buf, 0, ENH_BUFL);
280 ((IlbcDecoder*)iLBCdec_inst)->prev_enh_pl = 2;
niklase@google.com470e71d2011-07-07 08:21:25 +0000281
282 return (0);
283}
284
leozwang@webrtc.org91b359e2012-02-28 17:26:14 +0000285void WebRtcIlbcfix_version(char *version)
niklase@google.com470e71d2011-07-07 08:21:25 +0000286{
tina.legrand@webrtc.orga41b4ce2011-08-29 08:19:30 +0000287 strcpy((char*)version, "1.1.1");
niklase@google.com470e71d2011-07-07 08:21:25 +0000288}