blob: ba6c3e46c373926fbc0a9e2278bb12873d94a110 [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
Timothy Gu31117832020-12-18 22:25:57 -080019#include "modules/audio_coding/codecs/ilbc/ilbc.h"
20
niklase@google.com470e71d2011-07-07 08:21:25 +000021#include <stdlib.h>
22
Timothy Gu31117832020-12-18 22:25:57 -080023#include "modules/audio_coding/codecs/ilbc/decode.h"
Mirko Bonadei06c2aa92018-02-01 15:11:41 +010024#include "modules/audio_coding/codecs/ilbc/defines.h"
Mirko Bonadei06c2aa92018-02-01 15:11:41 +010025#include "modules/audio_coding/codecs/ilbc/encode.h"
26#include "modules/audio_coding/codecs/ilbc/init_decode.h"
Timothy Gu31117832020-12-18 22:25:57 -080027#include "modules/audio_coding/codecs/ilbc/init_encode.h"
Mirko Bonadei06c2aa92018-02-01 15:11:41 +010028#include "rtc_base/checks.h"
29
pbos@webrtc.orge728ee02014-12-17 13:43:55 +000030int16_t WebRtcIlbcfix_EncoderAssign(IlbcEncoderInstance** iLBC_encinst,
31 int16_t* ILBCENC_inst_Addr,
32 int16_t* size) {
33 *iLBC_encinst=(IlbcEncoderInstance*)ILBCENC_inst_Addr;
pbos@webrtc.orgeb544462014-12-17 15:23:29 +000034 *size=sizeof(IlbcEncoder)/sizeof(int16_t);
niklase@google.com470e71d2011-07-07 08:21:25 +000035 if (*iLBC_encinst!=NULL) {
36 return(0);
37 } else {
38 return(-1);
39 }
40}
41
pbos@webrtc.orge728ee02014-12-17 13:43:55 +000042int16_t WebRtcIlbcfix_DecoderAssign(IlbcDecoderInstance** iLBC_decinst,
43 int16_t* ILBCDEC_inst_Addr,
44 int16_t* size) {
45 *iLBC_decinst=(IlbcDecoderInstance*)ILBCDEC_inst_Addr;
pbos@webrtc.orgeb544462014-12-17 15:23:29 +000046 *size=sizeof(IlbcDecoder)/sizeof(int16_t);
niklase@google.com470e71d2011-07-07 08:21:25 +000047 if (*iLBC_decinst!=NULL) {
48 return(0);
49 } else {
50 return(-1);
51 }
52}
53
pbos@webrtc.orge728ee02014-12-17 13:43:55 +000054int16_t WebRtcIlbcfix_EncoderCreate(IlbcEncoderInstance **iLBC_encinst) {
pbos@webrtc.orgeb544462014-12-17 15:23:29 +000055 *iLBC_encinst=(IlbcEncoderInstance*)malloc(sizeof(IlbcEncoder));
niklase@google.com470e71d2011-07-07 08:21:25 +000056 if (*iLBC_encinst!=NULL) {
57 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) {
66 return(0);
67 } else {
68 return(-1);
69 }
70}
71
pbos@webrtc.orge728ee02014-12-17 13:43:55 +000072int16_t WebRtcIlbcfix_EncoderFree(IlbcEncoderInstance *iLBC_encinst) {
niklase@google.com470e71d2011-07-07 08:21:25 +000073 free(iLBC_encinst);
74 return(0);
75}
76
pbos@webrtc.orge728ee02014-12-17 13:43:55 +000077int16_t WebRtcIlbcfix_DecoderFree(IlbcDecoderInstance *iLBC_decinst) {
niklase@google.com470e71d2011-07-07 08:21:25 +000078 free(iLBC_decinst);
79 return(0);
80}
81
pbos@webrtc.orge728ee02014-12-17 13:43:55 +000082int16_t WebRtcIlbcfix_EncoderInit(IlbcEncoderInstance* iLBCenc_inst,
83 int16_t mode) {
niklase@google.com470e71d2011-07-07 08:21:25 +000084 if ((mode==20)||(mode==30)) {
pbos@webrtc.orgeb544462014-12-17 15:23:29 +000085 WebRtcIlbcfix_InitEncode((IlbcEncoder*) iLBCenc_inst, mode);
niklase@google.com470e71d2011-07-07 08:21:25 +000086 return(0);
87 } else {
88 return(-1);
89 }
90}
91
Peter Kastinga8b335c2015-06-11 18:51:20 -070092int WebRtcIlbcfix_Encode(IlbcEncoderInstance* iLBCenc_inst,
93 const int16_t* speechIn,
Peter Kastingdce40cf2015-08-24 14:52:23 -070094 size_t len,
Peter Kastinga8b335c2015-06-11 18:51:20 -070095 uint8_t* encoded) {
Peter Kastingdce40cf2015-08-24 14:52:23 -070096 size_t pos = 0;
97 size_t encpos = 0;
niklase@google.com470e71d2011-07-07 08:21:25 +000098
pbos@webrtc.orgeb544462014-12-17 15:23:29 +000099 if ((len != ((IlbcEncoder*)iLBCenc_inst)->blockl) &&
niklase@google.com470e71d2011-07-07 08:21:25 +0000100#ifdef SPLIT_10MS
101 (len != 80) &&
102#endif
pbos@webrtc.orgeb544462014-12-17 15:23:29 +0000103 (len != 2*((IlbcEncoder*)iLBCenc_inst)->blockl) &&
104 (len != 3*((IlbcEncoder*)iLBCenc_inst)->blockl))
niklase@google.com470e71d2011-07-07 08:21:25 +0000105 {
106 /* A maximum of 3 frames/packet is allowed */
107 return(-1);
108 } else {
109
110 /* call encoder */
111 while (pos<len) {
kwiberg@webrtc.orgcb858ba2014-12-08 17:11:44 +0000112 WebRtcIlbcfix_EncodeImpl((uint16_t*)&encoded[2 * encpos], &speechIn[pos],
pbos@webrtc.orgeb544462014-12-17 15:23:29 +0000113 (IlbcEncoder*)iLBCenc_inst);
niklase@google.com470e71d2011-07-07 08:21:25 +0000114#ifdef SPLIT_10MS
115 pos += 80;
pbos@webrtc.orgeb544462014-12-17 15:23:29 +0000116 if(((IlbcEncoder*)iLBCenc_inst)->section == 0)
niklase@google.com470e71d2011-07-07 08:21:25 +0000117#else
pbos@webrtc.orgeb544462014-12-17 15:23:29 +0000118 pos += ((IlbcEncoder*)iLBCenc_inst)->blockl;
niklase@google.com470e71d2011-07-07 08:21:25 +0000119#endif
pbos@webrtc.orgeb544462014-12-17 15:23:29 +0000120 encpos += ((IlbcEncoder*)iLBCenc_inst)->no_of_words;
niklase@google.com470e71d2011-07-07 08:21:25 +0000121 }
Peter Kastingdce40cf2015-08-24 14:52:23 -0700122 return (int)(encpos*2);
niklase@google.com470e71d2011-07-07 08:21:25 +0000123 }
124}
125
pbos@webrtc.orge728ee02014-12-17 13:43:55 +0000126int16_t WebRtcIlbcfix_DecoderInit(IlbcDecoderInstance* iLBCdec_inst,
127 int16_t mode) {
niklase@google.com470e71d2011-07-07 08:21:25 +0000128 if ((mode==20)||(mode==30)) {
pbos@webrtc.orgeb544462014-12-17 15:23:29 +0000129 WebRtcIlbcfix_InitDecode((IlbcDecoder*) iLBCdec_inst, mode, 1);
niklase@google.com470e71d2011-07-07 08:21:25 +0000130 return(0);
131 } else {
132 return(-1);
133 }
134}
Karl Wiberg43766482015-08-27 15:22:11 +0200135void WebRtcIlbcfix_DecoderInit20Ms(IlbcDecoderInstance* iLBCdec_inst) {
pbos@webrtc.orgeb544462014-12-17 15:23:29 +0000136 WebRtcIlbcfix_InitDecode((IlbcDecoder*) iLBCdec_inst, 20, 1);
niklase@google.com470e71d2011-07-07 08:21:25 +0000137}
Karl Wiberg43766482015-08-27 15:22:11 +0200138void 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}
141
142
Peter Kastinga8b335c2015-06-11 18:51:20 -0700143int WebRtcIlbcfix_Decode(IlbcDecoderInstance* iLBCdec_inst,
144 const uint8_t* encoded,
Peter Kastingdce40cf2015-08-24 14:52:23 -0700145 size_t len,
Peter Kastinga8b335c2015-06-11 18:51:20 -0700146 int16_t* decoded,
147 int16_t* speechType)
niklase@google.com470e71d2011-07-07 08:21:25 +0000148{
Peter Kastingdce40cf2015-08-24 14:52:23 -0700149 size_t i=0;
niklase@google.com470e71d2011-07-07 08:21:25 +0000150 /* Allow for automatic switching between the frame sizes
151 (although you do get some discontinuity) */
pbos@webrtc.orgeb544462014-12-17 15:23:29 +0000152 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)) {
niklase@google.com470e71d2011-07-07 08:21:25 +0000155 /* ok, do nothing */
156 } else {
157 /* Test if the mode has changed */
pbos@webrtc.orgeb544462014-12-17 15:23:29 +0000158 if (((IlbcDecoder*)iLBCdec_inst)->mode==20) {
niklase@google.com470e71d2011-07-07 08:21:25 +0000159 if ((len==NO_OF_BYTES_30MS)||
160 (len==2*NO_OF_BYTES_30MS)||
161 (len==3*NO_OF_BYTES_30MS)) {
pbos@webrtc.orge728ee02014-12-17 13:43:55 +0000162 WebRtcIlbcfix_InitDecode(
pbos@webrtc.orgeb544462014-12-17 15:23:29 +0000163 ((IlbcDecoder*)iLBCdec_inst), 30,
164 ((IlbcDecoder*)iLBCdec_inst)->use_enhancer);
niklase@google.com470e71d2011-07-07 08:21:25 +0000165 } 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)) {
pbos@webrtc.orge728ee02014-12-17 13:43:55 +0000173 WebRtcIlbcfix_InitDecode(
pbos@webrtc.orgeb544462014-12-17 15:23:29 +0000174 ((IlbcDecoder*)iLBCdec_inst), 20,
175 ((IlbcDecoder*)iLBCdec_inst)->use_enhancer);
niklase@google.com470e71d2011-07-07 08:21:25 +0000176 } else {
177 /* Unsupported frame length */
178 return(-1);
179 }
180 }
181 }
182
pbos@webrtc.orgeb544462014-12-17 15:23:29 +0000183 while ((i*((IlbcDecoder*)iLBCdec_inst)->no_of_bytes)<len) {
kwiberg619a2112016-08-24 02:46:44 -0700184 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;
niklase@google.com470e71d2011-07-07 08:21:25 +0000190 i++;
191 }
192 /* iLBC does not support VAD/CNG yet */
193 *speechType=1;
Peter Kastingdce40cf2015-08-24 14:52:23 -0700194 return (int)(i*((IlbcDecoder*)iLBCdec_inst)->blockl);
niklase@google.com470e71d2011-07-07 08:21:25 +0000195}
196
Peter Kastinga8b335c2015-06-11 18:51:20 -0700197int WebRtcIlbcfix_Decode20Ms(IlbcDecoderInstance* iLBCdec_inst,
198 const uint8_t* encoded,
Peter Kastingdce40cf2015-08-24 14:52:23 -0700199 size_t len,
Peter Kastinga8b335c2015-06-11 18:51:20 -0700200 int16_t* decoded,
201 int16_t* speechType)
niklase@google.com470e71d2011-07-07 08:21:25 +0000202{
Peter Kastingdce40cf2015-08-24 14:52:23 -0700203 size_t 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) {
kwiberg619a2112016-08-24 02:46:44 -0700213 if (!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],
kwiberg619a2112016-08-24 02:46:44 -0700217 (IlbcDecoder*)iLBCdec_inst, 1))
218 return -1;
niklase@google.com470e71d2011-07-07 08:21:25 +0000219 i++;
220 }
221 /* iLBC does not support VAD/CNG yet */
222 *speechType=1;
Peter Kastingdce40cf2015-08-24 14:52:23 -0700223 return (int)(i*((IlbcDecoder*)iLBCdec_inst)->blockl);
niklase@google.com470e71d2011-07-07 08:21:25 +0000224}
225
Peter Kastinga8b335c2015-06-11 18:51:20 -0700226int WebRtcIlbcfix_Decode30Ms(IlbcDecoderInstance* iLBCdec_inst,
227 const uint8_t* encoded,
Peter Kastingdce40cf2015-08-24 14:52:23 -0700228 size_t len,
Peter Kastinga8b335c2015-06-11 18:51:20 -0700229 int16_t* decoded,
230 int16_t* speechType)
niklase@google.com470e71d2011-07-07 08:21:25 +0000231{
Peter Kastingdce40cf2015-08-24 14:52:23 -0700232 size_t i=0;
pbos@webrtc.orgeb544462014-12-17 15:23:29 +0000233 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)) {
niklase@google.com470e71d2011-07-07 08:21:25 +0000236 /* ok, do nothing */
237 } else {
238 return(-1);
239 }
240
pbos@webrtc.orgeb544462014-12-17 15:23:29 +0000241 while ((i*((IlbcDecoder*)iLBCdec_inst)->no_of_bytes)<len) {
kwiberg619a2112016-08-24 02:46:44 -0700242 if (!WebRtcIlbcfix_DecodeImpl(
pbos@webrtc.orgeb544462014-12-17 15:23:29 +0000243 &decoded[i * ((IlbcDecoder*)iLBCdec_inst)->blockl],
pbos@webrtc.orge728ee02014-12-17 13:43:55 +0000244 (const uint16_t*)&encoded
jmarusic@webrtc.org71b35a42015-02-17 16:02:18 +0000245 [2 * i * ((IlbcDecoder*)iLBCdec_inst)->no_of_words],
kwiberg619a2112016-08-24 02:46:44 -0700246 (IlbcDecoder*)iLBCdec_inst, 1))
247 return -1;
niklase@google.com470e71d2011-07-07 08:21:25 +0000248 i++;
249 }
250 /* iLBC does not support VAD/CNG yet */
251 *speechType=1;
Peter Kastingdce40cf2015-08-24 14:52:23 -0700252 return (int)(i*((IlbcDecoder*)iLBCdec_inst)->blockl);
niklase@google.com470e71d2011-07-07 08:21:25 +0000253}
254
Peter Kastingdce40cf2015-08-24 14:52:23 -0700255size_t WebRtcIlbcfix_DecodePlc(IlbcDecoderInstance* iLBCdec_inst,
256 int16_t* decoded,
257 size_t noOfLostFrames) {
258 size_t i;
pbos@webrtc.org0946a562013-04-09 00:28:06 +0000259 uint16_t dummy;
niklase@google.com470e71d2011-07-07 08:21:25 +0000260
261 for (i=0;i<noOfLostFrames;i++) {
kwiberg619a2112016-08-24 02:46:44 -0700262 // PLC decoding shouldn't fail, because there is no external input data
263 // that can be bad.
Garth Judge73f481c2020-11-10 09:44:22 -0800264 int result = WebRtcIlbcfix_DecodeImpl(
pbos@webrtc.orgeb544462014-12-17 15:23:29 +0000265 &decoded[i * ((IlbcDecoder*)iLBCdec_inst)->blockl], &dummy,
Garth Judge73f481c2020-11-10 09:44:22 -0800266 (IlbcDecoder*)iLBCdec_inst, 0);
267 RTC_CHECK_EQ(result, 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}