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 * isacfix.c
13 *
14 * This C file contains the functions for the ISAC API
15 *
16 */
17
18 #include "modules/audio_coding/codecs/isac/fix/include/isacfix.h"
19
20 #include <stdlib.h>
21
22 #include "rtc_base/checks.h"
23 #include "modules/audio_coding/codecs/isac/fix/source/bandwidth_estimator.h"
24 #include "modules/audio_coding/codecs/isac/fix/source/codec.h"
25 #include "modules/audio_coding/codecs/isac/fix/source/entropy_coding.h"
26 #include "modules/audio_coding/codecs/isac/fix/source/filterbank_internal.h"
27 #include "modules/audio_coding/codecs/isac/fix/source/lpc_masking_model.h"
28 #include "modules/audio_coding/codecs/isac/fix/source/structs.h"
29 #include "system_wrappers/include/cpu_features_wrapper.h"
30
31 // Declare function pointers.
32 FilterMaLoopFix WebRtcIsacfix_FilterMaLoopFix;
33 Spec2Time WebRtcIsacfix_Spec2Time;
34 Time2Spec WebRtcIsacfix_Time2Spec;
35 MatrixProduct1 WebRtcIsacfix_MatrixProduct1;
36 MatrixProduct2 WebRtcIsacfix_MatrixProduct2;
37
38 /* This method assumes that |stream_size_bytes| is in valid range,
39 * i.e. >= 0 && <= STREAM_MAXW16_60MS
40 */
InitializeDecoderBitstream(size_t stream_size_bytes,Bitstr_dec * bitstream)41 static void InitializeDecoderBitstream(size_t stream_size_bytes,
42 Bitstr_dec* bitstream) {
43 bitstream->W_upper = 0xFFFFFFFF;
44 bitstream->streamval = 0;
45 bitstream->stream_index = 0;
46 bitstream->full = 1;
47 bitstream->stream_size = (stream_size_bytes + 1) >> 1;
48 memset(bitstream->stream, 0, sizeof(bitstream->stream));
49 }
50
51 /**************************************************************************
52 * WebRtcIsacfix_AssignSize(...)
53 *
54 * Functions used when malloc is not allowed
55 * Returns number of bytes needed to allocate for iSAC struct.
56 *
57 */
58
WebRtcIsacfix_AssignSize(int * sizeinbytes)59 int16_t WebRtcIsacfix_AssignSize(int *sizeinbytes) {
60 *sizeinbytes=sizeof(ISACFIX_SubStruct)*2/sizeof(int16_t);
61 return(0);
62 }
63
64 /***************************************************************************
65 * WebRtcIsacfix_Assign(...)
66 *
67 * Functions used when malloc is not allowed
68 * Place struct at given address
69 *
70 * If successful, Return 0, else Return -1
71 */
72
WebRtcIsacfix_Assign(ISACFIX_MainStruct ** inst,void * ISACFIX_inst_Addr)73 int16_t WebRtcIsacfix_Assign(ISACFIX_MainStruct **inst, void *ISACFIX_inst_Addr) {
74 if (ISACFIX_inst_Addr!=NULL) {
75 ISACFIX_SubStruct* self = ISACFIX_inst_Addr;
76 *inst = (ISACFIX_MainStruct*)self;
77 self->errorcode = 0;
78 self->initflag = 0;
79 self->ISACenc_obj.SaveEnc_ptr = NULL;
80 WebRtcIsacfix_InitBandwidthEstimator(&self->bwestimator_obj);
81 return(0);
82 } else {
83 return(-1);
84 }
85 }
86
87
88 #ifndef ISACFIX_NO_DYNAMIC_MEM
89
90 /****************************************************************************
91 * WebRtcIsacfix_Create(...)
92 *
93 * This function creates a ISAC instance, which will contain the state
94 * information for one coding/decoding channel.
95 *
96 * Input:
97 * - *ISAC_main_inst : a pointer to the coder instance.
98 *
99 * Return value : 0 - Ok
100 * -1 - Error
101 */
102
WebRtcIsacfix_Create(ISACFIX_MainStruct ** ISAC_main_inst)103 int16_t WebRtcIsacfix_Create(ISACFIX_MainStruct **ISAC_main_inst)
104 {
105 ISACFIX_SubStruct *tempo;
106 tempo = malloc(1 * sizeof(ISACFIX_SubStruct));
107 *ISAC_main_inst = (ISACFIX_MainStruct *)tempo;
108 if (*ISAC_main_inst!=NULL) {
109 (*(ISACFIX_SubStruct**)ISAC_main_inst)->errorcode = 0;
110 (*(ISACFIX_SubStruct**)ISAC_main_inst)->initflag = 0;
111 (*(ISACFIX_SubStruct**)ISAC_main_inst)->ISACenc_obj.SaveEnc_ptr = NULL;
112 WebRtcSpl_Init();
113 WebRtcIsacfix_InitBandwidthEstimator(&tempo->bwestimator_obj);
114 return(0);
115 } else {
116 return(-1);
117 }
118 }
119
120
121 /****************************************************************************
122 * WebRtcIsacfix_CreateInternal(...)
123 *
124 * This function creates the memory that is used to store data in the encoder
125 *
126 * Input:
127 * - *ISAC_main_inst : a pointer to the coder instance.
128 *
129 * Return value : 0 - Ok
130 * -1 - Error
131 */
132
WebRtcIsacfix_CreateInternal(ISACFIX_MainStruct * ISAC_main_inst)133 int16_t WebRtcIsacfix_CreateInternal(ISACFIX_MainStruct *ISAC_main_inst)
134 {
135 ISACFIX_SubStruct *ISAC_inst;
136
137 /* typecast pointer to real structure */
138 ISAC_inst = (ISACFIX_SubStruct *)ISAC_main_inst;
139
140 /* Allocate memory for storing encoder data */
141 ISAC_inst->ISACenc_obj.SaveEnc_ptr = malloc(1 * sizeof(IsacSaveEncoderData));
142
143 if (ISAC_inst->ISACenc_obj.SaveEnc_ptr!=NULL) {
144 return(0);
145 } else {
146 return(-1);
147 }
148 }
149
150
151 #endif
152
153
154
155 /****************************************************************************
156 * WebRtcIsacfix_Free(...)
157 *
158 * This function frees the ISAC instance created at the beginning.
159 *
160 * Input:
161 * - ISAC_main_inst : a ISAC instance.
162 *
163 * Return value : 0 - Ok
164 * -1 - Error
165 */
166
WebRtcIsacfix_Free(ISACFIX_MainStruct * ISAC_main_inst)167 int16_t WebRtcIsacfix_Free(ISACFIX_MainStruct *ISAC_main_inst)
168 {
169 free(ISAC_main_inst);
170 return(0);
171 }
172
173 /****************************************************************************
174 * WebRtcIsacfix_FreeInternal(...)
175 *
176 * This function frees the internal memory for storing encoder data.
177 *
178 * Input:
179 * - ISAC_main_inst : a ISAC instance.
180 *
181 * Return value : 0 - Ok
182 * -1 - Error
183 */
184
WebRtcIsacfix_FreeInternal(ISACFIX_MainStruct * ISAC_main_inst)185 int16_t WebRtcIsacfix_FreeInternal(ISACFIX_MainStruct *ISAC_main_inst)
186 {
187 ISACFIX_SubStruct *ISAC_inst;
188
189 /* typecast pointer to real structure */
190 ISAC_inst = (ISACFIX_SubStruct *)ISAC_main_inst;
191
192 /* Release memory */
193 free(ISAC_inst->ISACenc_obj.SaveEnc_ptr);
194
195 return(0);
196 }
197
198 /****************************************************************************
199 * WebRtcIsacfix_InitNeon(...)
200 *
201 * This function initializes function pointers for ARM Neon platform.
202 */
203
204 #if defined(WEBRTC_HAS_NEON)
WebRtcIsacfix_InitNeon(void)205 static void WebRtcIsacfix_InitNeon(void) {
206 WebRtcIsacfix_AutocorrFix = WebRtcIsacfix_AutocorrNeon;
207 WebRtcIsacfix_FilterMaLoopFix = WebRtcIsacfix_FilterMaLoopNeon;
208 WebRtcIsacfix_Spec2Time = WebRtcIsacfix_Spec2TimeNeon;
209 WebRtcIsacfix_Time2Spec = WebRtcIsacfix_Time2SpecNeon;
210 WebRtcIsacfix_AllpassFilter2FixDec16 =
211 WebRtcIsacfix_AllpassFilter2FixDec16Neon;
212 WebRtcIsacfix_MatrixProduct1 = WebRtcIsacfix_MatrixProduct1Neon;
213 WebRtcIsacfix_MatrixProduct2 = WebRtcIsacfix_MatrixProduct2Neon;
214 }
215 #endif
216
217 /****************************************************************************
218 * WebRtcIsacfix_InitMIPS(...)
219 *
220 * This function initializes function pointers for MIPS platform.
221 */
222
223 #if defined(MIPS32_LE)
WebRtcIsacfix_InitMIPS(void)224 static void WebRtcIsacfix_InitMIPS(void) {
225 WebRtcIsacfix_AutocorrFix = WebRtcIsacfix_AutocorrMIPS;
226 WebRtcIsacfix_FilterMaLoopFix = WebRtcIsacfix_FilterMaLoopMIPS;
227 WebRtcIsacfix_Spec2Time = WebRtcIsacfix_Spec2TimeMIPS;
228 WebRtcIsacfix_Time2Spec = WebRtcIsacfix_Time2SpecMIPS;
229 WebRtcIsacfix_MatrixProduct1 = WebRtcIsacfix_MatrixProduct1MIPS;
230 WebRtcIsacfix_MatrixProduct2 = WebRtcIsacfix_MatrixProduct2MIPS;
231 #if defined(MIPS_DSP_R1_LE)
232 WebRtcIsacfix_AllpassFilter2FixDec16 =
233 WebRtcIsacfix_AllpassFilter2FixDec16MIPS;
234 WebRtcIsacfix_HighpassFilterFixDec32 =
235 WebRtcIsacfix_HighpassFilterFixDec32MIPS;
236 #endif
237 #if defined(MIPS_DSP_R2_LE)
238 WebRtcIsacfix_CalculateResidualEnergy =
239 WebRtcIsacfix_CalculateResidualEnergyMIPS;
240 #endif
241 }
242 #endif
243
InitFunctionPointers(void)244 static void InitFunctionPointers(void) {
245 WebRtcIsacfix_AutocorrFix = WebRtcIsacfix_AutocorrC;
246 WebRtcIsacfix_FilterMaLoopFix = WebRtcIsacfix_FilterMaLoopC;
247 WebRtcIsacfix_CalculateResidualEnergy =
248 WebRtcIsacfix_CalculateResidualEnergyC;
249 WebRtcIsacfix_AllpassFilter2FixDec16 = WebRtcIsacfix_AllpassFilter2FixDec16C;
250 WebRtcIsacfix_HighpassFilterFixDec32 = WebRtcIsacfix_HighpassFilterFixDec32C;
251 WebRtcIsacfix_Time2Spec = WebRtcIsacfix_Time2SpecC;
252 WebRtcIsacfix_Spec2Time = WebRtcIsacfix_Spec2TimeC;
253 WebRtcIsacfix_MatrixProduct1 = WebRtcIsacfix_MatrixProduct1C;
254 WebRtcIsacfix_MatrixProduct2 = WebRtcIsacfix_MatrixProduct2C;
255
256 #if defined(WEBRTC_HAS_NEON)
257 WebRtcIsacfix_InitNeon();
258 #endif
259
260 #if defined(MIPS32_LE)
261 WebRtcIsacfix_InitMIPS();
262 #endif
263 }
264
265 /****************************************************************************
266 * WebRtcIsacfix_EncoderInit(...)
267 *
268 * This function initializes a ISAC instance prior to the encoder calls.
269 *
270 * Input:
271 * - ISAC_main_inst : ISAC instance.
272 * - CodingMode : 0 -> Bit rate and frame length are automatically
273 * adjusted to available bandwidth on
274 * transmission channel.
275 * 1 -> User sets a frame length and a target bit
276 * rate which is taken as the maximum short-term
277 * average bit rate.
278 *
279 * Return value : 0 - Ok
280 * -1 - Error
281 */
282
WebRtcIsacfix_EncoderInit(ISACFIX_MainStruct * ISAC_main_inst,int16_t CodingMode)283 int16_t WebRtcIsacfix_EncoderInit(ISACFIX_MainStruct *ISAC_main_inst,
284 int16_t CodingMode)
285 {
286 int k;
287 int16_t statusInit;
288 ISACFIX_SubStruct *ISAC_inst;
289
290 statusInit = 0;
291 /* typecast pointer to rela structure */
292 ISAC_inst = (ISACFIX_SubStruct *)ISAC_main_inst;
293
294 /* flag encoder init */
295 ISAC_inst->initflag |= 2;
296
297 if (CodingMode == 0)
298 /* Adaptive mode */
299 ISAC_inst->ISACenc_obj.new_framelength = INITIAL_FRAMESAMPLES;
300 else if (CodingMode == 1)
301 /* Instantaneous mode */
302 ISAC_inst->ISACenc_obj.new_framelength = 480; /* default for I-mode */
303 else {
304 ISAC_inst->errorcode = ISAC_DISALLOWED_CODING_MODE;
305 statusInit = -1;
306 }
307
308 ISAC_inst->CodingMode = CodingMode;
309
310 WebRtcIsacfix_InitMaskingEnc(&ISAC_inst->ISACenc_obj.maskfiltstr_obj);
311 WebRtcIsacfix_InitPreFilterbank(&ISAC_inst->ISACenc_obj.prefiltbankstr_obj);
312 WebRtcIsacfix_InitPitchFilter(&ISAC_inst->ISACenc_obj.pitchfiltstr_obj);
313 WebRtcIsacfix_InitPitchAnalysis(&ISAC_inst->ISACenc_obj.pitchanalysisstr_obj);
314
315 WebRtcIsacfix_InitRateModel(&ISAC_inst->ISACenc_obj.rate_data_obj);
316
317
318 ISAC_inst->ISACenc_obj.buffer_index = 0;
319 ISAC_inst->ISACenc_obj.frame_nb = 0;
320 ISAC_inst->ISACenc_obj.BottleNeck = 32000; /* default for I-mode */
321 ISAC_inst->ISACenc_obj.MaxDelay = 10; /* default for I-mode */
322 ISAC_inst->ISACenc_obj.current_framesamples = 0;
323 ISAC_inst->ISACenc_obj.s2nr = 0;
324 ISAC_inst->ISACenc_obj.MaxBits = 0;
325 ISAC_inst->ISACenc_obj.bitstr_seed = 4447;
326 ISAC_inst->ISACenc_obj.payloadLimitBytes30 = STREAM_MAXW16_30MS << 1;
327 ISAC_inst->ISACenc_obj.payloadLimitBytes60 = STREAM_MAXW16_60MS << 1;
328 ISAC_inst->ISACenc_obj.maxPayloadBytes = STREAM_MAXW16_60MS << 1;
329 ISAC_inst->ISACenc_obj.maxRateInBytes = STREAM_MAXW16_30MS << 1;
330 ISAC_inst->ISACenc_obj.enforceFrameSize = 0;
331
332 /* Init the bistream data area to zero */
333 for (k=0; k<STREAM_MAXW16_60MS; k++){
334 ISAC_inst->ISACenc_obj.bitstr_obj.stream[k] = 0;
335 }
336
337 #ifdef WEBRTC_ISAC_FIX_NB_CALLS_ENABLED
338 WebRtcIsacfix_InitPostFilterbank(&ISAC_inst->ISACenc_obj.interpolatorstr_obj);
339 #endif
340
341 InitFunctionPointers();
342
343 return statusInit;
344 }
345
346 /* Read the given number of bytes of big-endian 16-bit integers from |src| and
347 write them to |dest| in host endian. If |nbytes| is odd, the number of
348 output elements is rounded up, and the least significant byte of the last
349 element is set to 0. */
read_be16(const uint8_t * src,size_t nbytes,uint16_t * dest)350 static void read_be16(const uint8_t* src, size_t nbytes, uint16_t* dest) {
351 size_t i;
352 for (i = 0; i < nbytes / 2; ++i)
353 dest[i] = src[2 * i] << 8 | src[2 * i + 1];
354 if (nbytes % 2 == 1)
355 dest[nbytes / 2] = src[nbytes - 1] << 8;
356 }
357
358 /* Read the given number of bytes of host-endian 16-bit integers from |src| and
359 write them to |dest| in big endian. If |nbytes| is odd, the number of source
360 elements is rounded up (but only the most significant byte of the last
361 element is used), and the number of output bytes written will be
362 nbytes + 1. */
write_be16(const uint16_t * src,size_t nbytes,uint8_t * dest)363 static void write_be16(const uint16_t* src, size_t nbytes, uint8_t* dest) {
364 size_t i;
365 for (i = 0; i < nbytes / 2; ++i) {
366 dest[2 * i] = src[i] >> 8;
367 dest[2 * i + 1] = src[i];
368 }
369 if (nbytes % 2 == 1) {
370 dest[nbytes - 1] = src[nbytes / 2] >> 8;
371 dest[nbytes] = 0;
372 }
373 }
374
375 /****************************************************************************
376 * WebRtcIsacfix_Encode(...)
377 *
378 * This function encodes 10ms frame(s) and inserts it into a package.
379 * Input speech length has to be 160 samples (10ms). The encoder buffers those
380 * 10ms frames until it reaches the chosen Framesize (480 or 960 samples
381 * corresponding to 30 or 60 ms frames), and then proceeds to the encoding.
382 *
383 * Input:
384 * - ISAC_main_inst : ISAC instance.
385 * - speechIn : input speech vector.
386 *
387 * Output:
388 * - encoded : the encoded data vector
389 *
390 * Return value:
391 * : >0 - Length (in bytes) of coded data
392 * : 0 - The buffer didn't reach the chosen framesize
393 * so it keeps buffering speech samples.
394 * : -1 - Error
395 */
396
WebRtcIsacfix_Encode(ISACFIX_MainStruct * ISAC_main_inst,const int16_t * speechIn,uint8_t * encoded)397 int WebRtcIsacfix_Encode(ISACFIX_MainStruct *ISAC_main_inst,
398 const int16_t *speechIn,
399 uint8_t* encoded)
400 {
401 ISACFIX_SubStruct *ISAC_inst;
402 int stream_len;
403
404 /* typecast pointer to rela structure */
405 ISAC_inst = (ISACFIX_SubStruct *)ISAC_main_inst;
406
407
408 /* check if encoder initiated */
409 if ((ISAC_inst->initflag & 2) != 2) {
410 ISAC_inst->errorcode = ISAC_ENCODER_NOT_INITIATED;
411 return (-1);
412 }
413
414 stream_len = WebRtcIsacfix_EncodeImpl((int16_t*)speechIn,
415 &ISAC_inst->ISACenc_obj,
416 &ISAC_inst->bwestimator_obj,
417 ISAC_inst->CodingMode);
418 if (stream_len<0) {
419 ISAC_inst->errorcode = -(int16_t)stream_len;
420 return -1;
421 }
422
423 write_be16(ISAC_inst->ISACenc_obj.bitstr_obj.stream, (size_t)stream_len,
424 encoded);
425 return stream_len;
426
427 }
428
429
430
431
432 /****************************************************************************
433 * WebRtcIsacfix_EncodeNb(...)
434 *
435 * This function encodes 10ms narrow band (8 kHz sampling) frame(s) and inserts
436 * it into a package. Input speech length has to be 80 samples (10ms). The encoder
437 * interpolates into wide-band (16 kHz sampling) buffers those
438 * 10ms frames until it reaches the chosen Framesize (480 or 960 wide-band samples
439 * corresponding to 30 or 60 ms frames), and then proceeds to the encoding.
440 *
441 * The function is enabled if WEBRTC_ISAC_FIX_NB_CALLS_ENABLED is defined
442 *
443 * Input:
444 * - ISAC_main_inst : ISAC instance.
445 * - speechIn : input speech vector.
446 *
447 * Output:
448 * - encoded : the encoded data vector
449 *
450 * Return value:
451 * : >0 - Length (in bytes) of coded data
452 * : 0 - The buffer didn't reach the chosen framesize
453 * so it keeps buffering speech samples.
454 * : -1 - Error
455 */
456 #ifdef WEBRTC_ISAC_FIX_NB_CALLS_ENABLED
WebRtcIsacfix_EncodeNb(ISACFIX_MainStruct * ISAC_main_inst,const int16_t * speechIn,int16_t * encoded)457 int16_t WebRtcIsacfix_EncodeNb(ISACFIX_MainStruct *ISAC_main_inst,
458 const int16_t *speechIn,
459 int16_t *encoded)
460 {
461 ISACFIX_SubStruct *ISAC_inst;
462 int16_t stream_len;
463 int16_t speechInWB[FRAMESAMPLES_10ms];
464 int16_t Vector_Word16_1[FRAMESAMPLES_10ms/2];
465 int16_t Vector_Word16_2[FRAMESAMPLES_10ms/2];
466
467 int k;
468
469
470 /* typecast pointer to rela structure */
471 ISAC_inst = (ISACFIX_SubStruct *)ISAC_main_inst;
472
473
474 /* check if encoder initiated */
475 if ((ISAC_inst->initflag & 2) != 2) {
476 ISAC_inst->errorcode = ISAC_ENCODER_NOT_INITIATED;
477 return (-1);
478 }
479
480
481 /* Oversample to WB */
482
483 /* Form polyphase signals, and compensate for DC offset */
484 for (k=0;k<FRAMESAMPLES_10ms/2;k++) {
485 Vector_Word16_1[k] = speechIn[k] + 1;
486 Vector_Word16_2[k] = speechIn[k];
487 }
488 WebRtcIsacfix_FilterAndCombine2(Vector_Word16_1, Vector_Word16_2, speechInWB, &ISAC_inst->ISACenc_obj.interpolatorstr_obj, FRAMESAMPLES_10ms);
489
490
491 /* Encode WB signal */
492 stream_len = WebRtcIsacfix_EncodeImpl((int16_t*)speechInWB,
493 &ISAC_inst->ISACenc_obj,
494 &ISAC_inst->bwestimator_obj,
495 ISAC_inst->CodingMode);
496 if (stream_len<0) {
497 ISAC_inst->errorcode = - stream_len;
498 return -1;
499 }
500
501 write_be16(ISAC_inst->ISACenc_obj.bitstr_obj.stream,
502 stream_len,
503 (uint8_t*)encoded);
504 return stream_len;
505 }
506 #endif /* WEBRTC_ISAC_FIX_NB_CALLS_ENABLED */
507
508
509 /****************************************************************************
510 * WebRtcIsacfix_GetNewBitStream(...)
511 *
512 * This function returns encoded data, with the recieved bwe-index in the
513 * stream. It should always return a complete packet, i.e. only called once
514 * even for 60 msec frames
515 *
516 * Input:
517 * - ISAC_main_inst : ISAC instance.
518 * - bweIndex : index of bandwidth estimate to put in new bitstream
519 *
520 * Output:
521 * - encoded : the encoded data vector
522 *
523 * Return value:
524 * : >0 - Length (in bytes) of coded data
525 * : -1 - Error
526 */
527
WebRtcIsacfix_GetNewBitStream(ISACFIX_MainStruct * ISAC_main_inst,int16_t bweIndex,float scale,uint8_t * encoded)528 int16_t WebRtcIsacfix_GetNewBitStream(ISACFIX_MainStruct *ISAC_main_inst,
529 int16_t bweIndex,
530 float scale,
531 uint8_t* encoded)
532 {
533 ISACFIX_SubStruct *ISAC_inst;
534 int16_t stream_len;
535
536 /* typecast pointer to rela structure */
537 ISAC_inst = (ISACFIX_SubStruct *)ISAC_main_inst;
538
539
540 /* check if encoder initiated */
541 if ((ISAC_inst->initflag & 2) != 2) {
542 ISAC_inst->errorcode = ISAC_ENCODER_NOT_INITIATED;
543 return (-1);
544 }
545
546 stream_len = WebRtcIsacfix_EncodeStoredData(&ISAC_inst->ISACenc_obj,
547 bweIndex,
548 scale);
549 if (stream_len<0) {
550 ISAC_inst->errorcode = - stream_len;
551 return -1;
552 }
553
554 write_be16(ISAC_inst->ISACenc_obj.bitstr_obj.stream, stream_len, encoded);
555 return stream_len;
556 }
557
558
559
560 /****************************************************************************
561 * WebRtcIsacfix_DecoderInit(...)
562 *
563 * This function initializes a ISAC instance prior to the decoder calls.
564 *
565 * Input:
566 * - ISAC_main_inst : ISAC instance.
567 */
568
WebRtcIsacfix_DecoderInit(ISACFIX_MainStruct * ISAC_main_inst)569 void WebRtcIsacfix_DecoderInit(ISACFIX_MainStruct *ISAC_main_inst)
570 {
571 ISACFIX_SubStruct *ISAC_inst;
572
573 InitFunctionPointers();
574
575 /* typecast pointer to real structure */
576 ISAC_inst = (ISACFIX_SubStruct *)ISAC_main_inst;
577
578 /* flag decoder init */
579 ISAC_inst->initflag |= 1;
580
581 WebRtcIsacfix_InitMaskingDec(&ISAC_inst->ISACdec_obj.maskfiltstr_obj);
582 WebRtcIsacfix_InitPostFilterbank(&ISAC_inst->ISACdec_obj.postfiltbankstr_obj);
583 WebRtcIsacfix_InitPitchFilter(&ISAC_inst->ISACdec_obj.pitchfiltstr_obj);
584
585 /* TS */
586 WebRtcIsacfix_InitPlc( &ISAC_inst->ISACdec_obj.plcstr_obj );
587
588
589 #ifdef WEBRTC_ISAC_FIX_NB_CALLS_ENABLED
590 WebRtcIsacfix_InitPreFilterbank(&ISAC_inst->ISACdec_obj.decimatorstr_obj);
591 #endif
592 }
593
594
595 /****************************************************************************
596 * WebRtcIsacfix_UpdateBwEstimate1(...)
597 *
598 * This function updates the estimate of the bandwidth.
599 *
600 * Input:
601 * - ISAC_main_inst : ISAC instance.
602 * - encoded : encoded ISAC frame(s).
603 * - packet_size : size of the packet.
604 * - rtp_seq_number : the RTP number of the packet.
605 * - arr_ts : the arrival time of the packet (from NetEq)
606 * in samples.
607 *
608 * Return value : 0 - Ok
609 * -1 - Error
610 */
611
WebRtcIsacfix_UpdateBwEstimate1(ISACFIX_MainStruct * ISAC_main_inst,const uint8_t * encoded,size_t packet_size,uint16_t rtp_seq_number,uint32_t arr_ts)612 int16_t WebRtcIsacfix_UpdateBwEstimate1(ISACFIX_MainStruct *ISAC_main_inst,
613 const uint8_t* encoded,
614 size_t packet_size,
615 uint16_t rtp_seq_number,
616 uint32_t arr_ts)
617 {
618 ISACFIX_SubStruct *ISAC_inst;
619 Bitstr_dec streamdata;
620 int16_t err;
621 const size_t kRequiredEncodedLenBytes = 10;
622
623 /* typecast pointer to real structure */
624 ISAC_inst = (ISACFIX_SubStruct *)ISAC_main_inst;
625
626 /* Sanity check of packet length */
627 if (packet_size == 0) {
628 /* return error code if the packet length is null or less */
629 ISAC_inst->errorcode = ISAC_EMPTY_PACKET;
630 return -1;
631 } else if (packet_size > (STREAM_MAXW16<<1)) {
632 /* return error code if length of stream is too long */
633 ISAC_inst->errorcode = ISAC_LENGTH_MISMATCH;
634 return -1;
635 }
636
637 /* check if decoder initiated */
638 if ((ISAC_inst->initflag & 1) != 1) {
639 ISAC_inst->errorcode = ISAC_DECODER_NOT_INITIATED;
640 return (-1);
641 }
642
643 InitializeDecoderBitstream(packet_size, &streamdata);
644
645 read_be16(encoded, kRequiredEncodedLenBytes, streamdata.stream);
646
647 err = WebRtcIsacfix_EstimateBandwidth(&ISAC_inst->bwestimator_obj,
648 &streamdata,
649 packet_size,
650 rtp_seq_number,
651 0,
652 arr_ts);
653
654
655 if (err < 0)
656 {
657 /* return error code if something went wrong */
658 ISAC_inst->errorcode = -err;
659 return -1;
660 }
661
662
663 return 0;
664 }
665
666 /****************************************************************************
667 * WebRtcIsacfix_UpdateBwEstimate(...)
668 *
669 * This function updates the estimate of the bandwidth.
670 *
671 * Input:
672 * - ISAC_main_inst : ISAC instance.
673 * - encoded : encoded ISAC frame(s).
674 * - packet_size : size of the packet.
675 * - rtp_seq_number : the RTP number of the packet.
676 * - send_ts : Send Time Stamp from RTP header
677 * - arr_ts : the arrival time of the packet (from NetEq)
678 * in samples.
679 *
680 * Return value : 0 - Ok
681 * -1 - Error
682 */
683
WebRtcIsacfix_UpdateBwEstimate(ISACFIX_MainStruct * ISAC_main_inst,const uint8_t * encoded,size_t packet_size,uint16_t rtp_seq_number,uint32_t send_ts,uint32_t arr_ts)684 int16_t WebRtcIsacfix_UpdateBwEstimate(ISACFIX_MainStruct *ISAC_main_inst,
685 const uint8_t* encoded,
686 size_t packet_size,
687 uint16_t rtp_seq_number,
688 uint32_t send_ts,
689 uint32_t arr_ts)
690 {
691 ISACFIX_SubStruct *ISAC_inst;
692 Bitstr_dec streamdata;
693 int16_t err;
694 const size_t kRequiredEncodedLenBytes = 10;
695
696 /* typecast pointer to real structure */
697 ISAC_inst = (ISACFIX_SubStruct *)ISAC_main_inst;
698
699 /* Sanity check of packet length */
700 if (packet_size == 0) {
701 /* return error code if the packet length is null or less */
702 ISAC_inst->errorcode = ISAC_EMPTY_PACKET;
703 return -1;
704 } else if (packet_size < kRequiredEncodedLenBytes) {
705 ISAC_inst->errorcode = ISAC_PACKET_TOO_SHORT;
706 return -1;
707 } else if (packet_size > (STREAM_MAXW16<<1)) {
708 /* return error code if length of stream is too long */
709 ISAC_inst->errorcode = ISAC_LENGTH_MISMATCH;
710 return -1;
711 }
712
713 /* check if decoder initiated */
714 if ((ISAC_inst->initflag & 1) != 1) {
715 ISAC_inst->errorcode = ISAC_DECODER_NOT_INITIATED;
716 return (-1);
717 }
718
719 InitializeDecoderBitstream(packet_size, &streamdata);
720
721 read_be16(encoded, kRequiredEncodedLenBytes, streamdata.stream);
722
723 err = WebRtcIsacfix_EstimateBandwidth(&ISAC_inst->bwestimator_obj,
724 &streamdata,
725 packet_size,
726 rtp_seq_number,
727 send_ts,
728 arr_ts);
729
730 if (err < 0)
731 {
732 /* return error code if something went wrong */
733 ISAC_inst->errorcode = -err;
734 return -1;
735 }
736
737
738 return 0;
739 }
740
741 /****************************************************************************
742 * WebRtcIsacfix_Decode(...)
743 *
744 * This function decodes a ISAC frame. Output speech length
745 * will be a multiple of 480 samples: 480 or 960 samples,
746 * depending on the framesize (30 or 60 ms).
747 *
748 * Input:
749 * - ISAC_main_inst : ISAC instance.
750 * - encoded : encoded ISAC frame(s)
751 * - len : bytes in encoded vector
752 *
753 * Output:
754 * - decoded : The decoded vector
755 *
756 * Return value : >0 - number of samples in decoded vector
757 * -1 - Error
758 */
759
760
WebRtcIsacfix_Decode(ISACFIX_MainStruct * ISAC_main_inst,const uint8_t * encoded,size_t len,int16_t * decoded,int16_t * speechType)761 int WebRtcIsacfix_Decode(ISACFIX_MainStruct* ISAC_main_inst,
762 const uint8_t* encoded,
763 size_t len,
764 int16_t* decoded,
765 int16_t* speechType)
766 {
767 ISACFIX_SubStruct *ISAC_inst;
768 /* number of samples (480 or 960), output from decoder */
769 /* that were actually used in the encoder/decoder (determined on the fly) */
770 size_t number_of_samples;
771 int declen_int = 0;
772 size_t declen;
773
774 /* typecast pointer to real structure */
775 ISAC_inst = (ISACFIX_SubStruct *)ISAC_main_inst;
776
777 /* check if decoder initiated */
778 if ((ISAC_inst->initflag & 1) != 1) {
779 ISAC_inst->errorcode = ISAC_DECODER_NOT_INITIATED;
780 return (-1);
781 }
782
783 /* Sanity check of packet length */
784 if (len == 0) {
785 /* return error code if the packet length is null or less */
786 ISAC_inst->errorcode = ISAC_EMPTY_PACKET;
787 return -1;
788 } else if (len > (STREAM_MAXW16<<1)) {
789 /* return error code if length of stream is too long */
790 ISAC_inst->errorcode = ISAC_LENGTH_MISMATCH;
791 return -1;
792 }
793
794 InitializeDecoderBitstream(len, &ISAC_inst->ISACdec_obj.bitstr_obj);
795
796 read_be16(encoded, len, ISAC_inst->ISACdec_obj.bitstr_obj.stream);
797
798 /* added for NetEq purposes (VAD/DTX related) */
799 *speechType=1;
800
801 declen_int = WebRtcIsacfix_DecodeImpl(decoded, &ISAC_inst->ISACdec_obj,
802 &number_of_samples);
803 if (declen_int < 0) {
804 /* Some error inside the decoder */
805 ISAC_inst->errorcode = -(int16_t)declen_int;
806 memset(decoded, 0, sizeof(int16_t) * MAX_FRAMESAMPLES);
807 return -1;
808 }
809 declen = (size_t)declen_int;
810
811 /* error check */
812
813 if (declen & 1) {
814 if (len != declen &&
815 len != declen +
816 ((ISAC_inst->ISACdec_obj.bitstr_obj.stream[declen >> 1]) & 0xFF)) {
817 ISAC_inst->errorcode = ISAC_LENGTH_MISMATCH;
818 memset(decoded, 0, sizeof(int16_t) * number_of_samples);
819 return -1;
820 }
821 } else {
822 if (len != declen &&
823 len != declen +
824 ((ISAC_inst->ISACdec_obj.bitstr_obj.stream[declen >> 1]) >> 8)) {
825 ISAC_inst->errorcode = ISAC_LENGTH_MISMATCH;
826 memset(decoded, 0, sizeof(int16_t) * number_of_samples);
827 return -1;
828 }
829 }
830
831 return (int)number_of_samples;
832 }
833
834
835
836
837
838 /****************************************************************************
839 * WebRtcIsacfix_DecodeNb(...)
840 *
841 * This function decodes a ISAC frame in narrow-band (8 kHz sampling).
842 * Output speech length will be a multiple of 240 samples: 240 or 480 samples,
843 * depending on the framesize (30 or 60 ms).
844 *
845 * The function is enabled if WEBRTC_ISAC_FIX_NB_CALLS_ENABLED is defined
846 *
847 * Input:
848 * - ISAC_main_inst : ISAC instance.
849 * - encoded : encoded ISAC frame(s)
850 * - len : bytes in encoded vector
851 *
852 * Output:
853 * - decoded : The decoded vector
854 *
855 * Return value : >0 - number of samples in decoded vector
856 * -1 - Error
857 */
858
859 #ifdef WEBRTC_ISAC_FIX_NB_CALLS_ENABLED
WebRtcIsacfix_DecodeNb(ISACFIX_MainStruct * ISAC_main_inst,const uint16_t * encoded,size_t len,int16_t * decoded,int16_t * speechType)860 int WebRtcIsacfix_DecodeNb(ISACFIX_MainStruct* ISAC_main_inst,
861 const uint16_t* encoded,
862 size_t len,
863 int16_t* decoded,
864 int16_t* speechType)
865 {
866 ISACFIX_SubStruct *ISAC_inst;
867 /* twice the number of samples (480 or 960), output from decoder */
868 /* that were actually used in the encoder/decoder (determined on the fly) */
869 size_t number_of_samples;
870 int declen_int = 0;
871 size_t declen;
872 int16_t dummy[FRAMESAMPLES/2];
873
874
875 /* typecast pointer to real structure */
876 ISAC_inst = (ISACFIX_SubStruct *)ISAC_main_inst;
877
878 /* check if decoder initiated */
879 if ((ISAC_inst->initflag & 1) != 1) {
880 ISAC_inst->errorcode = ISAC_DECODER_NOT_INITIATED;
881 return (-1);
882 }
883
884 if (len == 0) {
885 /* return error code if the packet length is null or less */
886 ISAC_inst->errorcode = ISAC_EMPTY_PACKET;
887 return -1;
888 } else if (len > (STREAM_MAXW16<<1)) {
889 /* return error code if length of stream is too long */
890 ISAC_inst->errorcode = ISAC_LENGTH_MISMATCH;
891 return -1;
892 }
893
894 InitializeDecoderBitstream(len, &ISAC_inst->ISACdec_obj.bitstr_obj);
895
896 read_be16(encoded, len, ISAC_inst->ISACdec_obj.bitstr_obj.stream);
897
898 /* added for NetEq purposes (VAD/DTX related) */
899 *speechType=1;
900
901 declen_int = WebRtcIsacfix_DecodeImpl(decoded, &ISAC_inst->ISACdec_obj,
902 &number_of_samples);
903 if (declen_int < 0) {
904 /* Some error inside the decoder */
905 ISAC_inst->errorcode = -(int16_t)declen_int;
906 memset(decoded, 0, sizeof(int16_t) * FRAMESAMPLES);
907 return -1;
908 }
909 declen = (size_t)declen_int;
910
911 /* error check */
912
913 if (declen & 1) {
914 if (len != declen &&
915 len != declen +
916 ((ISAC_inst->ISACdec_obj.bitstr_obj.stream[declen >> 1]) & 0xFF)) {
917 ISAC_inst->errorcode = ISAC_LENGTH_MISMATCH;
918 memset(decoded, 0, sizeof(int16_t) * number_of_samples);
919 return -1;
920 }
921 } else {
922 if (len != declen &&
923 len != declen +
924 ((ISAC_inst->ISACdec_obj.bitstr_obj.stream[declen >>1]) >> 8)) {
925 ISAC_inst->errorcode = ISAC_LENGTH_MISMATCH;
926 memset(decoded, 0, sizeof(int16_t) * number_of_samples);
927 return -1;
928 }
929 }
930
931 WebRtcIsacfix_SplitAndFilter2(decoded, decoded, dummy, &ISAC_inst->ISACdec_obj.decimatorstr_obj);
932
933 if (number_of_samples>FRAMESAMPLES) {
934 WebRtcIsacfix_SplitAndFilter2(decoded + FRAMESAMPLES, decoded + FRAMESAMPLES/2,
935 dummy, &ISAC_inst->ISACdec_obj.decimatorstr_obj);
936 }
937
938 return (int)(number_of_samples / 2);
939 }
940 #endif /* WEBRTC_ISAC_FIX_NB_CALLS_ENABLED */
941
942
943 /****************************************************************************
944 * WebRtcIsacfix_DecodePlcNb(...)
945 *
946 * This function conducts PLC for ISAC frame(s) in narrow-band (8kHz sampling).
947 * Output speech length will be "240*noOfLostFrames" samples
948 * that is equevalent of "30*noOfLostFrames" millisecond.
949 *
950 * The function is enabled if WEBRTC_ISAC_FIX_NB_CALLS_ENABLED is defined
951 *
952 * Input:
953 * - ISAC_main_inst : ISAC instance.
954 * - noOfLostFrames : Number of PLC frames (240 sample=30ms) to produce
955 *
956 * Output:
957 * - decoded : The decoded vector
958 *
959 * Return value : Number of samples in decoded PLC vector
960 */
961
962 #ifdef WEBRTC_ISAC_FIX_NB_CALLS_ENABLED
WebRtcIsacfix_DecodePlcNb(ISACFIX_MainStruct * ISAC_main_inst,int16_t * decoded,size_t noOfLostFrames)963 size_t WebRtcIsacfix_DecodePlcNb(ISACFIX_MainStruct* ISAC_main_inst,
964 int16_t* decoded,
965 size_t noOfLostFrames )
966 {
967 size_t no_of_samples, declen, k;
968 int16_t outframeNB[FRAMESAMPLES];
969 int16_t outframeWB[FRAMESAMPLES];
970 int16_t dummy[FRAMESAMPLES/2];
971
972
973 ISACFIX_SubStruct *ISAC_inst;
974 /* typecast pointer to real structure */
975 ISAC_inst = (ISACFIX_SubStruct *)ISAC_main_inst;
976
977 /* Limit number of frames to two = 60 msec. Otherwise we exceed data vectors */
978 if (noOfLostFrames > 2){
979 noOfLostFrames = 2;
980 }
981
982 k = 0;
983 declen = 0;
984 while( noOfLostFrames > 0 )
985 {
986 WebRtcIsacfix_DecodePlcImpl(outframeWB, &ISAC_inst->ISACdec_obj,
987 &no_of_samples);
988
989 WebRtcIsacfix_SplitAndFilter2(outframeWB, &(outframeNB[k*240]), dummy, &ISAC_inst->ISACdec_obj.decimatorstr_obj);
990
991 declen += no_of_samples;
992 noOfLostFrames--;
993 k++;
994 }
995
996 declen>>=1;
997
998 for (k=0;k<declen;k++) {
999 decoded[k] = outframeNB[k];
1000 }
1001
1002 return declen;
1003 }
1004 #endif /* WEBRTC_ISAC_FIX_NB_CALLS_ENABLED */
1005
1006
1007
1008
1009 /****************************************************************************
1010 * WebRtcIsacfix_DecodePlc(...)
1011 *
1012 * This function conducts PLC for ISAC frame(s) in wide-band (16kHz sampling).
1013 * Output speech length will be "480*noOfLostFrames" samples
1014 * that is equevalent of "30*noOfLostFrames" millisecond.
1015 *
1016 * Input:
1017 * - ISAC_main_inst : ISAC instance.
1018 * - noOfLostFrames : Number of PLC frames (480sample = 30ms)
1019 * to produce
1020 *
1021 * Output:
1022 * - decoded : The decoded vector
1023 *
1024 * Return value : Number of samples in decoded PLC vector
1025 */
1026
WebRtcIsacfix_DecodePlc(ISACFIX_MainStruct * ISAC_main_inst,int16_t * decoded,size_t noOfLostFrames)1027 size_t WebRtcIsacfix_DecodePlc(ISACFIX_MainStruct* ISAC_main_inst,
1028 int16_t* decoded,
1029 size_t noOfLostFrames)
1030 {
1031
1032 size_t no_of_samples, declen, k;
1033 int16_t outframe16[MAX_FRAMESAMPLES];
1034
1035 ISACFIX_SubStruct *ISAC_inst;
1036 /* typecast pointer to real structure */
1037 ISAC_inst = (ISACFIX_SubStruct *)ISAC_main_inst;
1038
1039 /* Limit number of frames to two = 60 msec. Otherwise we exceed data vectors */
1040 if (noOfLostFrames > 2) {
1041 noOfLostFrames = 2;
1042 }
1043 k = 0;
1044 declen = 0;
1045 while( noOfLostFrames > 0 )
1046 {
1047 WebRtcIsacfix_DecodePlcImpl(&(outframe16[k*480]), &ISAC_inst->ISACdec_obj,
1048 &no_of_samples);
1049 declen += no_of_samples;
1050 noOfLostFrames--;
1051 k++;
1052 }
1053
1054 for (k=0;k<declen;k++) {
1055 decoded[k] = outframe16[k];
1056 }
1057
1058 return declen;
1059 }
1060
1061
1062 /****************************************************************************
1063 * WebRtcIsacfix_Control(...)
1064 *
1065 * This function sets the limit on the short-term average bit rate and the
1066 * frame length. Should be used only in Instantaneous mode.
1067 *
1068 * Input:
1069 * - ISAC_main_inst : ISAC instance.
1070 * - rate : limit on the short-term average bit rate,
1071 * in bits/second (between 10000 and 32000)
1072 * - framesize : number of milliseconds per frame (30 or 60)
1073 *
1074 * Return value : 0 - ok
1075 * -1 - Error
1076 */
1077
WebRtcIsacfix_Control(ISACFIX_MainStruct * ISAC_main_inst,int16_t rate,int framesize)1078 int16_t WebRtcIsacfix_Control(ISACFIX_MainStruct *ISAC_main_inst,
1079 int16_t rate,
1080 int framesize)
1081 {
1082 ISACFIX_SubStruct *ISAC_inst;
1083 /* typecast pointer to real structure */
1084 ISAC_inst = (ISACFIX_SubStruct *)ISAC_main_inst;
1085
1086 if (ISAC_inst->CodingMode == 0)
1087 {
1088 /* in adaptive mode */
1089 ISAC_inst->errorcode = ISAC_MODE_MISMATCH;
1090 return -1;
1091 }
1092
1093
1094 if (rate >= 10000 && rate <= 32000)
1095 ISAC_inst->ISACenc_obj.BottleNeck = rate;
1096 else {
1097 ISAC_inst->errorcode = ISAC_DISALLOWED_BOTTLENECK;
1098 return -1;
1099 }
1100
1101
1102
1103 if (framesize == 30 || framesize == 60)
1104 ISAC_inst->ISACenc_obj.new_framelength = (int16_t)((FS/1000) * framesize);
1105 else {
1106 ISAC_inst->errorcode = ISAC_DISALLOWED_FRAME_LENGTH;
1107 return -1;
1108 }
1109
1110 return 0;
1111 }
1112
WebRtcIsacfix_SetInitialBweBottleneck(ISACFIX_MainStruct * ISAC_main_inst,int bottleneck_bits_per_second)1113 void WebRtcIsacfix_SetInitialBweBottleneck(ISACFIX_MainStruct* ISAC_main_inst,
1114 int bottleneck_bits_per_second) {
1115 ISACFIX_SubStruct* inst = (ISACFIX_SubStruct*)ISAC_main_inst;
1116 RTC_DCHECK_GE(bottleneck_bits_per_second, 10000);
1117 RTC_DCHECK_LE(bottleneck_bits_per_second, 32000);
1118 inst->bwestimator_obj.sendBwAvg = ((uint32_t)bottleneck_bits_per_second) << 7;
1119 }
1120
1121 /****************************************************************************
1122 * WebRtcIsacfix_ControlBwe(...)
1123 *
1124 * This function sets the initial values of bottleneck and frame-size if
1125 * iSAC is used in channel-adaptive mode. Through this API, users can
1126 * enforce a frame-size for all values of bottleneck. Then iSAC will not
1127 * automatically change the frame-size.
1128 *
1129 *
1130 * Input:
1131 * - ISAC_main_inst : ISAC instance.
1132 * - rateBPS : initial value of bottleneck in bits/second
1133 * 10000 <= rateBPS <= 32000 is accepted
1134 * For default bottleneck set rateBPS = 0
1135 * - frameSizeMs : number of milliseconds per frame (30 or 60)
1136 * - enforceFrameSize : 1 to enforce the given frame-size through out
1137 * the adaptation process, 0 to let iSAC change
1138 * the frame-size if required.
1139 *
1140 * Return value : 0 - ok
1141 * -1 - Error
1142 */
1143
WebRtcIsacfix_ControlBwe(ISACFIX_MainStruct * ISAC_main_inst,int16_t rateBPS,int frameSizeMs,int16_t enforceFrameSize)1144 int16_t WebRtcIsacfix_ControlBwe(ISACFIX_MainStruct *ISAC_main_inst,
1145 int16_t rateBPS,
1146 int frameSizeMs,
1147 int16_t enforceFrameSize)
1148 {
1149 ISACFIX_SubStruct *ISAC_inst;
1150 /* Typecast pointer to real structure */
1151 ISAC_inst = (ISACFIX_SubStruct *)ISAC_main_inst;
1152
1153 /* check if encoder initiated */
1154 if ((ISAC_inst->initflag & 2) != 2) {
1155 ISAC_inst->errorcode = ISAC_ENCODER_NOT_INITIATED;
1156 return (-1);
1157 }
1158
1159 /* Check that we are in channel-adaptive mode, otherwise, return -1 */
1160 if (ISAC_inst->CodingMode != 0) {
1161 ISAC_inst->errorcode = ISAC_MODE_MISMATCH;
1162 return (-1);
1163 }
1164
1165 /* Set struct variable if enforceFrameSize is set. ISAC will then keep the */
1166 /* chosen frame size. */
1167 ISAC_inst->ISACenc_obj.enforceFrameSize = (enforceFrameSize != 0)? 1:0;
1168
1169 /* Set initial rate, if value between 10000 and 32000, */
1170 /* if rateBPS is 0, keep the default initial bottleneck value (15000) */
1171 if ((rateBPS >= 10000) && (rateBPS <= 32000)) {
1172 ISAC_inst->bwestimator_obj.sendBwAvg = (((uint32_t)rateBPS) << 7);
1173 } else if (rateBPS != 0) {
1174 ISAC_inst->errorcode = ISAC_DISALLOWED_BOTTLENECK;
1175 return -1;
1176 }
1177
1178 /* Set initial framesize. If enforceFrameSize is set the frame size will not change */
1179 if ((frameSizeMs == 30) || (frameSizeMs == 60)) {
1180 ISAC_inst->ISACenc_obj.new_framelength = (int16_t)((FS/1000) * frameSizeMs);
1181 } else {
1182 ISAC_inst->errorcode = ISAC_DISALLOWED_FRAME_LENGTH;
1183 return -1;
1184 }
1185
1186 return 0;
1187 }
1188
1189
1190
1191
1192
1193 /****************************************************************************
1194 * WebRtcIsacfix_GetDownLinkBwIndex(...)
1195 *
1196 * This function returns index representing the Bandwidth estimate from
1197 * other side to this side.
1198 *
1199 * Input:
1200 * - ISAC_main_inst: iSAC struct
1201 *
1202 * Output:
1203 * - rateIndex : Bandwidth estimate to transmit to other side.
1204 *
1205 */
1206
WebRtcIsacfix_GetDownLinkBwIndex(ISACFIX_MainStruct * ISAC_main_inst,int16_t * rateIndex)1207 int16_t WebRtcIsacfix_GetDownLinkBwIndex(ISACFIX_MainStruct* ISAC_main_inst,
1208 int16_t* rateIndex)
1209 {
1210 ISACFIX_SubStruct *ISAC_inst;
1211
1212 /* typecast pointer to real structure */
1213 ISAC_inst = (ISACFIX_SubStruct *)ISAC_main_inst;
1214
1215 /* Call function to get Bandwidth Estimate */
1216 *rateIndex = WebRtcIsacfix_GetDownlinkBwIndexImpl(&ISAC_inst->bwestimator_obj);
1217
1218 return 0;
1219 }
1220
1221
1222 /****************************************************************************
1223 * WebRtcIsacfix_UpdateUplinkBw(...)
1224 *
1225 * This function takes an index representing the Bandwidth estimate from
1226 * this side to other side and updates BWE.
1227 *
1228 * Input:
1229 * - ISAC_main_inst: iSAC struct
1230 * - rateIndex : Bandwidth estimate from other side.
1231 *
1232 */
1233
WebRtcIsacfix_UpdateUplinkBw(ISACFIX_MainStruct * ISAC_main_inst,int16_t rateIndex)1234 int16_t WebRtcIsacfix_UpdateUplinkBw(ISACFIX_MainStruct* ISAC_main_inst,
1235 int16_t rateIndex)
1236 {
1237 int16_t err = 0;
1238 ISACFIX_SubStruct *ISAC_inst;
1239
1240 /* typecast pointer to real structure */
1241 ISAC_inst = (ISACFIX_SubStruct *)ISAC_main_inst;
1242
1243 /* Call function to update BWE with received Bandwidth Estimate */
1244 err = WebRtcIsacfix_UpdateUplinkBwRec(&ISAC_inst->bwestimator_obj, rateIndex);
1245 if (err < 0) {
1246 ISAC_inst->errorcode = -err;
1247 return (-1);
1248 }
1249
1250 return 0;
1251 }
1252
1253 /****************************************************************************
1254 * WebRtcIsacfix_ReadFrameLen(...)
1255 *
1256 * This function returns the length of the frame represented in the packet.
1257 *
1258 * Input:
1259 * - encoded : Encoded bitstream
1260 *
1261 * Output:
1262 * - frameLength : Length of frame in packet (in samples)
1263 *
1264 */
1265
WebRtcIsacfix_ReadFrameLen(const uint8_t * encoded,size_t encoded_len_bytes,size_t * frameLength)1266 int16_t WebRtcIsacfix_ReadFrameLen(const uint8_t* encoded,
1267 size_t encoded_len_bytes,
1268 size_t* frameLength)
1269 {
1270 Bitstr_dec streamdata;
1271 int16_t err;
1272 const size_t kRequiredEncodedLenBytes = 10;
1273
1274 if (encoded_len_bytes < kRequiredEncodedLenBytes) {
1275 return -1;
1276 }
1277
1278 InitializeDecoderBitstream(encoded_len_bytes, &streamdata);
1279
1280 read_be16(encoded, kRequiredEncodedLenBytes, streamdata.stream);
1281
1282 /* decode frame length */
1283 err = WebRtcIsacfix_DecodeFrameLen(&streamdata, frameLength);
1284 if (err<0) // error check
1285 return err;
1286
1287 return 0;
1288 }
1289
1290
1291 /****************************************************************************
1292 * WebRtcIsacfix_ReadBwIndex(...)
1293 *
1294 * This function returns the index of the Bandwidth estimate from the bitstream.
1295 *
1296 * Input:
1297 * - encoded : Encoded bitstream
1298 *
1299 * Output:
1300 * - frameLength : Length of frame in packet (in samples)
1301 * - rateIndex : Bandwidth estimate in bitstream
1302 *
1303 */
1304
WebRtcIsacfix_ReadBwIndex(const uint8_t * encoded,size_t encoded_len_bytes,int16_t * rateIndex)1305 int16_t WebRtcIsacfix_ReadBwIndex(const uint8_t* encoded,
1306 size_t encoded_len_bytes,
1307 int16_t* rateIndex)
1308 {
1309 Bitstr_dec streamdata;
1310 int16_t err;
1311 const size_t kRequiredEncodedLenBytes = 10;
1312
1313 if (encoded_len_bytes < kRequiredEncodedLenBytes) {
1314 return -1;
1315 }
1316
1317 InitializeDecoderBitstream(encoded_len_bytes, &streamdata);
1318
1319 read_be16(encoded, kRequiredEncodedLenBytes, streamdata.stream);
1320
1321 /* decode frame length, needed to get to the rateIndex in the bitstream */
1322 size_t frameLength;
1323 err = WebRtcIsacfix_DecodeFrameLen(&streamdata, &frameLength);
1324 if (err<0) // error check
1325 return err;
1326
1327 /* decode BW estimation */
1328 err = WebRtcIsacfix_DecodeSendBandwidth(&streamdata, rateIndex);
1329 if (err<0) // error check
1330 return err;
1331
1332 return 0;
1333 }
1334
1335
1336
1337
1338 /****************************************************************************
1339 * WebRtcIsacfix_GetErrorCode(...)
1340 *
1341 * This function can be used to check the error code of an iSAC instance. When
1342 * a function returns -1 a error code will be set for that instance. The
1343 * function below extract the code of the last error that occured in the
1344 * specified instance.
1345 *
1346 * Input:
1347 * - ISAC_main_inst : ISAC instance
1348 *
1349 * Return value : Error code
1350 */
1351
WebRtcIsacfix_GetErrorCode(ISACFIX_MainStruct * ISAC_main_inst)1352 int16_t WebRtcIsacfix_GetErrorCode(ISACFIX_MainStruct *ISAC_main_inst)
1353 {
1354 ISACFIX_SubStruct *ISAC_inst;
1355 /* typecast pointer to real structure */
1356 ISAC_inst = (ISACFIX_SubStruct *)ISAC_main_inst;
1357
1358 return ISAC_inst->errorcode;
1359 }
1360
1361
1362
1363 /****************************************************************************
1364 * WebRtcIsacfix_GetUplinkBw(...)
1365 *
1366 * This function returns the inst quantized iSAC send bitrate
1367 *
1368 * Input:
1369 * - ISAC_main_inst : iSAC instance
1370 *
1371 * Return value : bitrate
1372 */
1373
WebRtcIsacfix_GetUplinkBw(ISACFIX_MainStruct * ISAC_main_inst)1374 int32_t WebRtcIsacfix_GetUplinkBw(ISACFIX_MainStruct *ISAC_main_inst)
1375 {
1376 ISACFIX_SubStruct *ISAC_inst = (ISACFIX_SubStruct *)ISAC_main_inst;
1377 BwEstimatorstr * bw = (BwEstimatorstr*)&(ISAC_inst->bwestimator_obj);
1378
1379 return (int32_t) WebRtcIsacfix_GetUplinkBandwidth(bw);
1380 }
1381
1382 /****************************************************************************
1383 * WebRtcIsacfix_GetNewFrameLen(...)
1384 *
1385 * This function return the next frame length (in samples) of iSAC.
1386 *
1387 * Input:
1388 * - ISAC_main_inst : iSAC instance
1389 *
1390 * Return value : frame lenght in samples
1391 */
1392
WebRtcIsacfix_GetNewFrameLen(ISACFIX_MainStruct * ISAC_main_inst)1393 int16_t WebRtcIsacfix_GetNewFrameLen(ISACFIX_MainStruct *ISAC_main_inst)
1394 {
1395 ISACFIX_SubStruct *ISAC_inst = (ISACFIX_SubStruct *)ISAC_main_inst;
1396 return ISAC_inst->ISACenc_obj.new_framelength;
1397 }
1398
1399
1400 /****************************************************************************
1401 * WebRtcIsacfix_SetMaxPayloadSize(...)
1402 *
1403 * This function sets a limit for the maximum payload size of iSAC. The same
1404 * value is used both for 30 and 60 msec packets.
1405 * The absolute max will be valid until next time the function is called.
1406 * NOTE! This function may override the function WebRtcIsacfix_SetMaxRate()
1407 *
1408 * Input:
1409 * - ISAC_main_inst : iSAC instance
1410 * - maxPayloadBytes : maximum size of the payload in bytes
1411 * valid values are between 100 and 400 bytes
1412 *
1413 *
1414 * Return value : 0 if sucessful
1415 * -1 if error happens
1416 */
1417
WebRtcIsacfix_SetMaxPayloadSize(ISACFIX_MainStruct * ISAC_main_inst,int16_t maxPayloadBytes)1418 int16_t WebRtcIsacfix_SetMaxPayloadSize(ISACFIX_MainStruct *ISAC_main_inst,
1419 int16_t maxPayloadBytes)
1420 {
1421 ISACFIX_SubStruct *ISAC_inst;
1422
1423 /* typecast pointer to real structure */
1424 ISAC_inst = (ISACFIX_SubStruct *)ISAC_main_inst;
1425
1426 if((maxPayloadBytes < 100) || (maxPayloadBytes > 400))
1427 {
1428 /* maxPayloadBytes is out of valid range */
1429 return -1;
1430 }
1431 else
1432 {
1433 /* Set new absolute max, which will not change unless this function
1434 is called again with a new value */
1435 ISAC_inst->ISACenc_obj.maxPayloadBytes = maxPayloadBytes;
1436
1437 /* Set new maximum values for 30 and 60 msec packets */
1438 if (maxPayloadBytes < ISAC_inst->ISACenc_obj.maxRateInBytes) {
1439 ISAC_inst->ISACenc_obj.payloadLimitBytes30 = maxPayloadBytes;
1440 } else {
1441 ISAC_inst->ISACenc_obj.payloadLimitBytes30 = ISAC_inst->ISACenc_obj.maxRateInBytes;
1442 }
1443
1444 if ( maxPayloadBytes < (ISAC_inst->ISACenc_obj.maxRateInBytes << 1)) {
1445 ISAC_inst->ISACenc_obj.payloadLimitBytes60 = maxPayloadBytes;
1446 } else {
1447 ISAC_inst->ISACenc_obj.payloadLimitBytes60 = (ISAC_inst->ISACenc_obj.maxRateInBytes << 1);
1448 }
1449 }
1450 return 0;
1451 }
1452
1453
1454 /****************************************************************************
1455 * WebRtcIsacfix_SetMaxRate(...)
1456 *
1457 * This function sets the maximum rate which the codec may not exceed for a
1458 * singel packet. The maximum rate is set in bits per second.
1459 * The codec has an absolute maximum rate of 53400 bits per second (200 bytes
1460 * per 30 msec).
1461 * It is possible to set a maximum rate between 32000 and 53400 bits per second.
1462 *
1463 * The rate limit is valid until next time the function is called.
1464 *
1465 * NOTE! Packet size will never go above the value set if calling
1466 * WebRtcIsacfix_SetMaxPayloadSize() (default max packet size is 400 bytes).
1467 *
1468 * Input:
1469 * - ISAC_main_inst : iSAC instance
1470 * - maxRateInBytes : maximum rate in bits per second,
1471 * valid values are 32000 to 53400 bits
1472 *
1473 * Return value : 0 if sucessful
1474 * -1 if error happens
1475 */
1476
WebRtcIsacfix_SetMaxRate(ISACFIX_MainStruct * ISAC_main_inst,int32_t maxRate)1477 int16_t WebRtcIsacfix_SetMaxRate(ISACFIX_MainStruct *ISAC_main_inst,
1478 int32_t maxRate)
1479 {
1480 ISACFIX_SubStruct *ISAC_inst;
1481 int16_t maxRateInBytes;
1482
1483 /* typecast pointer to real structure */
1484 ISAC_inst = (ISACFIX_SubStruct *)ISAC_main_inst;
1485
1486 if((maxRate < 32000) || (maxRate > 53400))
1487 {
1488 /* maxRate is out of valid range */
1489 return -1;
1490 }
1491 else
1492 {
1493 /* Calculate maximum number of bytes per 30 msec packets for the given
1494 maximum rate. Multiply with 30/1000 to get number of bits per 30 msec,
1495 divide by 8 to get number of bytes per 30 msec:
1496 maxRateInBytes = floor((maxRate * 30/1000) / 8); */
1497 maxRateInBytes = (int16_t)( WebRtcSpl_DivW32W16ResW16(WEBRTC_SPL_MUL(maxRate, 3), 800) );
1498
1499 /* Store the value for usage in the WebRtcIsacfix_SetMaxPayloadSize-function */
1500 ISAC_inst->ISACenc_obj.maxRateInBytes = maxRateInBytes;
1501
1502 /* For 30 msec packets: if the new limit is below the maximum
1503 payload size, set a new limit */
1504 if (maxRateInBytes < ISAC_inst->ISACenc_obj.maxPayloadBytes) {
1505 ISAC_inst->ISACenc_obj.payloadLimitBytes30 = maxRateInBytes;
1506 } else {
1507 ISAC_inst->ISACenc_obj.payloadLimitBytes30 = ISAC_inst->ISACenc_obj.maxPayloadBytes;
1508 }
1509
1510 /* For 60 msec packets: if the new limit (times 2) is below the
1511 maximum payload size, set a new limit */
1512 if ( (maxRateInBytes << 1) < ISAC_inst->ISACenc_obj.maxPayloadBytes) {
1513 ISAC_inst->ISACenc_obj.payloadLimitBytes60 = (maxRateInBytes << 1);
1514 } else {
1515 ISAC_inst->ISACenc_obj.payloadLimitBytes60 = ISAC_inst->ISACenc_obj.maxPayloadBytes;
1516 }
1517 }
1518
1519 return 0;
1520 }
1521
1522
1523
1524 /****************************************************************************
1525 * WebRtcIsacfix_version(...)
1526 *
1527 * This function returns the version number.
1528 *
1529 * Output:
1530 * - version : Pointer to character string
1531 *
1532 */
1533
WebRtcIsacfix_version(char * version)1534 void WebRtcIsacfix_version(char *version)
1535 {
1536 strcpy(version, "3.6.0");
1537 }
1538
WebRtcIsacfix_GetBandwidthInfo(ISACFIX_MainStruct * ISAC_main_inst,IsacBandwidthInfo * bwinfo)1539 void WebRtcIsacfix_GetBandwidthInfo(ISACFIX_MainStruct* ISAC_main_inst,
1540 IsacBandwidthInfo* bwinfo) {
1541 ISACFIX_SubStruct* inst = (ISACFIX_SubStruct*)ISAC_main_inst;
1542 RTC_DCHECK_NE(0, inst->initflag & 1); // Decoder initialized.
1543 WebRtcIsacfixBw_GetBandwidthInfo(&inst->bwestimator_obj, bwinfo);
1544 }
1545
WebRtcIsacfix_SetBandwidthInfo(ISACFIX_MainStruct * ISAC_main_inst,const IsacBandwidthInfo * bwinfo)1546 void WebRtcIsacfix_SetBandwidthInfo(ISACFIX_MainStruct* ISAC_main_inst,
1547 const IsacBandwidthInfo* bwinfo) {
1548 ISACFIX_SubStruct* inst = (ISACFIX_SubStruct*)ISAC_main_inst;
1549 RTC_DCHECK_NE(0, inst->initflag & 2); // Encoder initialized.
1550 WebRtcIsacfixBw_SetBandwidthInfo(&inst->bwestimator_obj, bwinfo);
1551 }
1552