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 "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 27 int16_t WebRtcIlbcfix_EncoderAssign(IlbcEncoderInstance** iLBC_encinst, 28 int16_t* ILBCENC_inst_Addr, 29 int16_t* size) { 30 *iLBC_encinst=(IlbcEncoderInstance*)ILBCENC_inst_Addr; 31 *size=sizeof(IlbcEncoder)/sizeof(int16_t); 32 if (*iLBC_encinst!=NULL) { 33 return(0); 34 } else { 35 return(-1); 36 } 37 } 38 39 int16_t WebRtcIlbcfix_DecoderAssign(IlbcDecoderInstance** iLBC_decinst, 40 int16_t* ILBCDEC_inst_Addr, 41 int16_t* size) { 42 *iLBC_decinst=(IlbcDecoderInstance*)ILBCDEC_inst_Addr; 43 *size=sizeof(IlbcDecoder)/sizeof(int16_t); 44 if (*iLBC_decinst!=NULL) { 45 return(0); 46 } else { 47 return(-1); 48 } 49 } 50 51 int16_t WebRtcIlbcfix_EncoderCreate(IlbcEncoderInstance **iLBC_encinst) { 52 *iLBC_encinst=(IlbcEncoderInstance*)malloc(sizeof(IlbcEncoder)); 53 if (*iLBC_encinst!=NULL) { 54 WebRtcSpl_Init(); 55 return(0); 56 } else { 57 return(-1); 58 } 59 } 60 61 int16_t WebRtcIlbcfix_DecoderCreate(IlbcDecoderInstance **iLBC_decinst) { 62 *iLBC_decinst=(IlbcDecoderInstance*)malloc(sizeof(IlbcDecoder)); 63 if (*iLBC_decinst!=NULL) { 64 WebRtcSpl_Init(); 65 return(0); 66 } else { 67 return(-1); 68 } 69 } 70 71 int16_t WebRtcIlbcfix_EncoderFree(IlbcEncoderInstance *iLBC_encinst) { 72 free(iLBC_encinst); 73 return(0); 74 } 75 76 int16_t WebRtcIlbcfix_DecoderFree(IlbcDecoderInstance *iLBC_decinst) { 77 free(iLBC_decinst); 78 return(0); 79 } 80 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 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 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 } 134 void WebRtcIlbcfix_DecoderInit20Ms(IlbcDecoderInstance* iLBCdec_inst) { 135 WebRtcIlbcfix_InitDecode((IlbcDecoder*) iLBCdec_inst, 20, 1); 136 } 137 void WebRtcIlbcfix_Decoderinit30Ms(IlbcDecoderInstance* iLBCdec_inst) { 138 WebRtcIlbcfix_InitDecode((IlbcDecoder*) iLBCdec_inst, 30, 1); 139 } 140 141 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 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); 188 i++; 189 } 190 /* iLBC does not support VAD/CNG yet */ 191 *speechType=1; 192 return (int)(i*((IlbcDecoder*)iLBCdec_inst)->blockl); 193 } 194 195 int WebRtcIlbcfix_Decode20Ms(IlbcDecoderInstance* iLBCdec_inst, 196 const uint8_t* encoded, 197 size_t len, 198 int16_t* decoded, 199 int16_t* speechType) 200 { 201 size_t i=0; 202 if ((len==((IlbcDecoder*)iLBCdec_inst)->no_of_bytes)|| 203 (len==2*((IlbcDecoder*)iLBCdec_inst)->no_of_bytes)|| 204 (len==3*((IlbcDecoder*)iLBCdec_inst)->no_of_bytes)) { 205 /* ok, do nothing */ 206 } else { 207 return(-1); 208 } 209 210 while ((i*((IlbcDecoder*)iLBCdec_inst)->no_of_bytes)<len) { 211 WebRtcIlbcfix_DecodeImpl( 212 &decoded[i * ((IlbcDecoder*)iLBCdec_inst)->blockl], 213 (const uint16_t*)&encoded 214 [2 * i * ((IlbcDecoder*)iLBCdec_inst)->no_of_words], 215 (IlbcDecoder*)iLBCdec_inst, 1); 216 i++; 217 } 218 /* iLBC does not support VAD/CNG yet */ 219 *speechType=1; 220 return (int)(i*((IlbcDecoder*)iLBCdec_inst)->blockl); 221 } 222 223 int WebRtcIlbcfix_Decode30Ms(IlbcDecoderInstance* iLBCdec_inst, 224 const uint8_t* encoded, 225 size_t len, 226 int16_t* decoded, 227 int16_t* speechType) 228 { 229 size_t i=0; 230 if ((len==((IlbcDecoder*)iLBCdec_inst)->no_of_bytes)|| 231 (len==2*((IlbcDecoder*)iLBCdec_inst)->no_of_bytes)|| 232 (len==3*((IlbcDecoder*)iLBCdec_inst)->no_of_bytes)) { 233 /* ok, do nothing */ 234 } else { 235 return(-1); 236 } 237 238 while ((i*((IlbcDecoder*)iLBCdec_inst)->no_of_bytes)<len) { 239 WebRtcIlbcfix_DecodeImpl( 240 &decoded[i * ((IlbcDecoder*)iLBCdec_inst)->blockl], 241 (const uint16_t*)&encoded 242 [2 * i * ((IlbcDecoder*)iLBCdec_inst)->no_of_words], 243 (IlbcDecoder*)iLBCdec_inst, 1); 244 i++; 245 } 246 /* iLBC does not support VAD/CNG yet */ 247 *speechType=1; 248 return (int)(i*((IlbcDecoder*)iLBCdec_inst)->blockl); 249 } 250 251 size_t WebRtcIlbcfix_DecodePlc(IlbcDecoderInstance* iLBCdec_inst, 252 int16_t* decoded, 253 size_t noOfLostFrames) { 254 size_t i; 255 uint16_t dummy; 256 257 for (i=0;i<noOfLostFrames;i++) { 258 /* call decoder */ 259 WebRtcIlbcfix_DecodeImpl( 260 &decoded[i * ((IlbcDecoder*)iLBCdec_inst)->blockl], &dummy, 261 (IlbcDecoder*)iLBCdec_inst, 0); 262 } 263 return (noOfLostFrames*((IlbcDecoder*)iLBCdec_inst)->blockl); 264 } 265 266 size_t WebRtcIlbcfix_NetEqPlc(IlbcDecoderInstance* iLBCdec_inst, 267 int16_t* decoded, 268 size_t noOfLostFrames) { 269 /* Two input parameters not used, but needed for function pointers in NetEQ */ 270 (void)(decoded = NULL); 271 (void)(noOfLostFrames = 0); 272 273 WebRtcSpl_MemSetW16(((IlbcDecoder*)iLBCdec_inst)->enh_buf, 0, ENH_BUFL); 274 ((IlbcDecoder*)iLBCdec_inst)->prev_enh_pl = 2; 275 276 return (0); 277 } 278 279 void WebRtcIlbcfix_version(char *version) 280 { 281 strcpy((char*)version, "1.1.1"); 282 } 283