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