Update To 11.40.268.0
[platform/framework/web/crosswalk.git] / src / third_party / webrtc / modules / audio_coding / codecs / isac / main / source / isac.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  * isac.c
13  *
14  * This C file contains the functions for the ISAC API
15  *
16  */
17
18 #include "webrtc/modules/audio_coding/codecs/isac/main/interface/isac.h"
19
20 #include <math.h>
21 #include <stdio.h>
22 #include <stdlib.h>
23 #include <string.h>
24
25 #include "webrtc/common_audio/signal_processing/include/signal_processing_library.h"
26 #include "webrtc/modules/audio_coding/codecs/isac/main/source/bandwidth_estimator.h"
27 #include "webrtc/modules/audio_coding/codecs/isac/main/source/codec.h"
28 #include "webrtc/modules/audio_coding/codecs/isac/main/source/crc.h"
29 #include "webrtc/modules/audio_coding/codecs/isac/main/source/entropy_coding.h"
30 #include "webrtc/modules/audio_coding/codecs/isac/main/source/lpc_shape_swb16_tables.h"
31 #include "webrtc/modules/audio_coding/codecs/isac/main/source/os_specific_inline.h"
32 #include "webrtc/modules/audio_coding/codecs/isac/main/source/structs.h"
33
34 #define BIT_MASK_DEC_INIT 0x0001
35 #define BIT_MASK_ENC_INIT 0x0002
36
37 #define LEN_CHECK_SUM_WORD8     4
38 #define MAX_NUM_LAYERS         10
39
40
41 /****************************************************************************
42  * UpdatePayloadSizeLimit(...)
43  *
44  * Call this function to update the limit on the payload size. The limit on
45  * payload size might change i) if a user ''directly changes the limit by
46  * calling xxx_setMaxPayloadSize() or xxx_setMaxRate(), or ii) indirectly
47  * when bandwidth is changing. The latter might be the result of bandwidth
48  * adaptation, or direct change of the bottleneck in instantaneous mode.
49  *
50  * This function takes the current overall limit on payload, and translates it
51  * to the limits on lower and upper-band. If the codec is in wideband mode,
52  * then the overall limit and the limit on the lower-band is the same.
53  * Otherwise, a fraction of the limit should be allocated to lower-band
54  * leaving some room for the upper-band bit-stream. That is why an update
55  * of limit is required every time that the bandwidth is changing.
56  *
57  */
58 static void UpdatePayloadSizeLimit(ISACMainStruct* instISAC) {
59   int16_t lim30MsPayloadBytes = WEBRTC_SPL_MIN(
60                           (instISAC->maxPayloadSizeBytes),
61                           (instISAC->maxRateBytesPer30Ms));
62   int16_t lim60MsPayloadBytes = WEBRTC_SPL_MIN(
63                           (instISAC->maxPayloadSizeBytes),
64                           (instISAC->maxRateBytesPer30Ms << 1));
65
66   /* The only time that iSAC will have 60 ms
67    * frame-size is when operating in wideband, so
68    * there is no upper-band bit-stream. */
69
70   if (instISAC->bandwidthKHz == isac8kHz) {
71     /* At 8 kHz there is no upper-band bit-stream,
72      * therefore, the lower-band limit is the overall limit. */
73     instISAC->instLB.ISACencLB_obj.payloadLimitBytes60 =
74       lim60MsPayloadBytes;
75     instISAC->instLB.ISACencLB_obj.payloadLimitBytes30 =
76       lim30MsPayloadBytes;
77   } else {
78     /* When in super-wideband, we only have 30 ms frames.
79      * Do a rate allocation for the given limit. */
80     if (lim30MsPayloadBytes > 250) {
81       /* 4/5 to lower-band the rest for upper-band. */
82       instISAC->instLB.ISACencLB_obj.payloadLimitBytes30 =
83         (lim30MsPayloadBytes << 2) / 5;
84     } else if (lim30MsPayloadBytes > 200) {
85       /* For the interval of 200 to 250 the share of
86        * upper-band linearly grows from 20 to 50. */
87       instISAC->instLB.ISACencLB_obj.payloadLimitBytes30 =
88         (lim30MsPayloadBytes << 1) / 5 + 100;
89     } else {
90       /* Allocate only 20 for upper-band. */
91       instISAC->instLB.ISACencLB_obj.payloadLimitBytes30 =
92         lim30MsPayloadBytes - 20;
93     }
94     instISAC->instUB.ISACencUB_obj.maxPayloadSizeBytes =
95       lim30MsPayloadBytes;
96   }
97 }
98
99
100 /****************************************************************************
101  * UpdateBottleneck(...)
102  *
103  * This function updates the bottleneck only if the codec is operating in
104  * channel-adaptive mode. Furthermore, as the update of bottleneck might
105  * result in an update of bandwidth, therefore, the bottlenech should be
106  * updated just right before the first 10ms of a frame is pushed into encoder.
107  *
108  */
109 static void UpdateBottleneck(ISACMainStruct* instISAC) {
110   /* Read the bottleneck from bandwidth estimator for the
111    * first 10 ms audio. This way, if there is a change
112    * in bandwidth, upper and lower-band will be in sync. */
113   if ((instISAC->codingMode == 0) &&
114       (instISAC->instLB.ISACencLB_obj.buffer_index == 0) &&
115       (instISAC->instLB.ISACencLB_obj.frame_nb == 0)) {
116     int32_t bottleneck;
117     WebRtcIsac_GetUplinkBandwidth(&(instISAC->bwestimator_obj),
118                                   &bottleneck);
119
120     /* Adding hysteresis when increasing signal bandwidth. */
121     if ((instISAC->bandwidthKHz == isac8kHz)
122         && (bottleneck > 37000)
123         && (bottleneck < 41000)) {
124       bottleneck = 37000;
125     }
126
127     /* Switching from 12 kHz to 16 kHz is not allowed at this revision.
128      * If we let this happen, we have to take care of buffer_index and
129      * the last LPC vector. */
130     if ((instISAC->bandwidthKHz != isac16kHz) &&
131         (bottleneck > 46000)) {
132       bottleneck = 46000;
133     }
134
135     /* We might need a rate allocation. */
136     if (instISAC->encoderSamplingRateKHz == kIsacWideband) {
137       /* Wideband is the only choice we have here. */
138       instISAC->instLB.ISACencLB_obj.bottleneck =
139         (bottleneck > 32000) ? 32000 : bottleneck;
140       instISAC->bandwidthKHz = isac8kHz;
141     } else {
142       /* Do the rate-allocation and get the new bandwidth. */
143       enum ISACBandwidth bandwidth;
144       WebRtcIsac_RateAllocation(bottleneck,
145                                 &(instISAC->instLB.ISACencLB_obj.bottleneck),
146                                 &(instISAC->instUB.ISACencUB_obj.bottleneck),
147                                 &bandwidth);
148       if (bandwidth != isac8kHz) {
149         instISAC->instLB.ISACencLB_obj.new_framelength = 480;
150       }
151       if (bandwidth != instISAC->bandwidthKHz) {
152         /* Bandwidth is changing. */
153         instISAC->bandwidthKHz = bandwidth;
154         UpdatePayloadSizeLimit(instISAC);
155         if (bandwidth == isac12kHz) {
156           instISAC->instLB.ISACencLB_obj.buffer_index = 0;
157         }
158         /* Currently we don't let the bandwidth to switch to 16 kHz
159          * if in adaptive mode. If we let this happen, we have to take
160          * care of buffer_index and the last LPC vector. */
161       }
162     }
163   }
164 }
165
166
167 /****************************************************************************
168  * GetSendBandwidthInfo(...)
169  *
170  * This is called to get the bandwidth info. This info is the bandwidth and
171  * the jitter of 'there-to-here' channel, estimated 'here.' These info
172  * is signaled in an in-band fashion to the other side.
173  *
174  * The call to the bandwidth estimator triggers a recursive averaging which
175  * has to be synchronized between encoder & decoder, therefore, the call to
176  * BWE should be once per packet. As the BWE info is inserted into bit-stream
177  * We need a valid info right before the encodeLB function is going to
178  * generate a bit-stream. That is when lower-band buffer has already 20ms
179  * of audio, and the 3rd block of 10ms is going to be injected into encoder.
180  *
181  * Inputs:
182  *         - instISAC          : iSAC instance.
183  *
184  * Outputs:
185  *         - bandwidthIndex    : an index which has to be encoded in
186  *                               lower-band bit-stream, indicating the
187  *                               bandwidth of there-to-here channel.
188  *         - jitterInfo        : this indicates if the jitter is high
189  *                               or low and it is encoded in upper-band
190  *                               bit-stream.
191  *
192  */
193 static void GetSendBandwidthInfo(ISACMainStruct* instISAC,
194                                  int16_t* bandwidthIndex,
195                                  int16_t* jitterInfo) {
196   if ((instISAC->instLB.ISACencLB_obj.buffer_index ==
197       (FRAMESAMPLES_10ms << 1)) &&
198       (instISAC->instLB.ISACencLB_obj.frame_nb == 0)) {
199     /* Bandwidth estimation and coding. */
200     WebRtcIsac_GetDownlinkBwJitIndexImpl(&(instISAC->bwestimator_obj),
201                                          bandwidthIndex, jitterInfo,
202                                          instISAC->decoderSamplingRateKHz);
203   }
204 }
205
206
207 /****************************************************************************
208  * WebRtcIsac_AssignSize(...)
209  *
210  * This function returns the size of the ISAC instance, so that the instance
211  * can be created out side iSAC.
212  *
213  * Output:
214  *        - sizeinbytes       : number of bytes needed to allocate for the
215  *                              instance.
216  *
217  * Return value               : 0 - Ok
218  *                             -1 - Error
219  */
220 int16_t WebRtcIsac_AssignSize(int* sizeInBytes) {
221   *sizeInBytes = sizeof(ISACMainStruct) * 2 / sizeof(int16_t);
222   return 0;
223 }
224
225
226 /****************************************************************************
227  * WebRtcIsac_Assign(...)
228  *
229  * This function assigns the memory already created to the ISAC instance.
230  *
231  * Input:
232  *        - ISAC_main_inst    : address of the pointer to the coder instance.
233  *        - instISAC_Addr     : the already allocated memory, where we put the
234  *                              iSAC structure.
235  *
236  * Return value               : 0 - Ok
237  *                             -1 - Error
238  */
239 int16_t WebRtcIsac_Assign(ISACStruct** ISAC_main_inst,
240                           void* instISAC_Addr) {
241   if (instISAC_Addr != NULL) {
242     ISACMainStruct* instISAC = (ISACMainStruct*)instISAC_Addr;
243     instISAC->errorCode = 0;
244     instISAC->initFlag = 0;
245
246     /* Assign the address. */
247     *ISAC_main_inst = (ISACStruct*)instISAC_Addr;
248
249     /* Default is wideband. */
250     instISAC->encoderSamplingRateKHz = kIsacWideband;
251     instISAC->decoderSamplingRateKHz = kIsacWideband;
252     instISAC->bandwidthKHz           = isac8kHz;
253     instISAC->in_sample_rate_hz = 16000;
254     return 0;
255   } else {
256     return -1;
257   }
258 }
259
260
261 /****************************************************************************
262  * WebRtcIsac_Create(...)
263  *
264  * This function creates an ISAC instance, which will contain the state
265  * information for one coding/decoding channel.
266  *
267  * Input:
268  *        - ISAC_main_inst    : address of the pointer to the coder instance.
269  *
270  * Return value               : 0 - Ok
271  *                             -1 - Error
272  */
273 int16_t WebRtcIsac_Create(ISACStruct** ISAC_main_inst) {
274   ISACMainStruct* instISAC;
275
276   if (ISAC_main_inst != NULL) {
277     instISAC = (ISACMainStruct*)malloc(sizeof(ISACMainStruct));
278     *ISAC_main_inst = (ISACStruct*)instISAC;
279     if (*ISAC_main_inst != NULL) {
280       instISAC->errorCode = 0;
281       instISAC->initFlag = 0;
282       /* Default is wideband. */
283       instISAC->bandwidthKHz = isac8kHz;
284       instISAC->encoderSamplingRateKHz = kIsacWideband;
285       instISAC->decoderSamplingRateKHz = kIsacWideband;
286       instISAC->in_sample_rate_hz = 16000;
287       return 0;
288     } else {
289       return -1;
290     }
291   } else {
292     return -1;
293   }
294 }
295
296
297 /****************************************************************************
298  * WebRtcIsac_Free(...)
299  *
300  * This function frees the ISAC instance created at the beginning.
301  *
302  * Input:
303  *        - ISAC_main_inst    : a ISAC instance.
304  *
305  * Return value               : 0 - Ok
306  *                             -1 - Error
307  */
308 int16_t WebRtcIsac_Free(ISACStruct* ISAC_main_inst) {
309   ISACMainStruct* instISAC = (ISACMainStruct*)ISAC_main_inst;
310   free(instISAC);
311   return 0;
312 }
313
314
315 /****************************************************************************
316  * EncoderInitLb(...) - internal function for initialization of
317  *                                Lower Band
318  * EncoderInitUb(...) - internal function for initialization of
319  *                                Upper Band
320  * WebRtcIsac_EncoderInit(...) - API function
321  *
322  * This function initializes a ISAC instance prior to the encoder calls.
323  *
324  * Input:
325  *        - ISAC_main_inst    : ISAC instance.
326  *        - CodingMode        : 0 -> Bit rate and frame length are automatically
327  *                                 adjusted to available bandwidth on
328  *                                 transmission channel, applicable just to
329  *                                 wideband mode.
330  *                              1 -> User sets a frame length and a target bit
331  *                                 rate which is taken as the maximum
332  *                                 short-term average bit rate.
333  *
334  * Return value               :  0 - Ok
335  *                              -1 - Error
336  */
337 static int16_t EncoderInitLb(ISACLBStruct* instLB,
338                              int16_t codingMode,
339                              enum IsacSamplingRate sampRate) {
340   int16_t statusInit = 0;
341   int k;
342
343   /* Init stream vector to zero */
344   for (k = 0; k < STREAM_SIZE_MAX_60; k++) {
345     instLB->ISACencLB_obj.bitstr_obj.stream[k] = 0;
346   }
347
348   if ((codingMode == 1) || (sampRate == kIsacSuperWideband)) {
349     /* 30 ms frame-size if either in super-wideband or
350      * instantaneous mode (I-mode). */
351     instLB->ISACencLB_obj.new_framelength = 480;
352   } else {
353     instLB->ISACencLB_obj.new_framelength = INITIAL_FRAMESAMPLES;
354   }
355
356   WebRtcIsac_InitMasking(&instLB->ISACencLB_obj.maskfiltstr_obj);
357   WebRtcIsac_InitPreFilterbank(&instLB->ISACencLB_obj.prefiltbankstr_obj);
358   WebRtcIsac_InitPitchFilter(&instLB->ISACencLB_obj.pitchfiltstr_obj);
359   WebRtcIsac_InitPitchAnalysis(
360     &instLB->ISACencLB_obj.pitchanalysisstr_obj);
361
362   instLB->ISACencLB_obj.buffer_index = 0;
363   instLB->ISACencLB_obj.frame_nb = 0;
364   /* Default for I-mode. */
365   instLB->ISACencLB_obj.bottleneck = 32000;
366   instLB->ISACencLB_obj.current_framesamples = 0;
367   instLB->ISACencLB_obj.s2nr = 0;
368   instLB->ISACencLB_obj.payloadLimitBytes30 = STREAM_SIZE_MAX_30;
369   instLB->ISACencLB_obj.payloadLimitBytes60 = STREAM_SIZE_MAX_60;
370   instLB->ISACencLB_obj.maxPayloadBytes = STREAM_SIZE_MAX_60;
371   instLB->ISACencLB_obj.maxRateInBytes = STREAM_SIZE_MAX_30;
372   instLB->ISACencLB_obj.enforceFrameSize = 0;
373   /* Invalid value prevents getRedPayload to
374      run before encoder is called. */
375   instLB->ISACencLB_obj.lastBWIdx            = -1;
376   return statusInit;
377 }
378
379 static int16_t EncoderInitUb(ISACUBStruct* instUB,
380                              int16_t bandwidth) {
381   int16_t statusInit = 0;
382   int k;
383
384   /* Init stream vector to zero. */
385   for (k = 0; k < STREAM_SIZE_MAX_60; k++) {
386     instUB->ISACencUB_obj.bitstr_obj.stream[k] = 0;
387   }
388
389   WebRtcIsac_InitMasking(&instUB->ISACencUB_obj.maskfiltstr_obj);
390   WebRtcIsac_InitPreFilterbank(&instUB->ISACencUB_obj.prefiltbankstr_obj);
391
392   if (bandwidth == isac16kHz) {
393     instUB->ISACencUB_obj.buffer_index = LB_TOTAL_DELAY_SAMPLES;
394   } else {
395     instUB->ISACencUB_obj.buffer_index = 0;
396   }
397   /* Default for I-mode. */
398   instUB->ISACencUB_obj.bottleneck = 32000;
399   /* These store the limits for the wideband + super-wideband bit-stream. */
400   instUB->ISACencUB_obj.maxPayloadSizeBytes = STREAM_SIZE_MAX_30 << 1;
401   /* This has to be updated after each lower-band encoding to guarantee
402    * a correct payload-limitation. */
403   instUB->ISACencUB_obj.numBytesUsed = 0;
404   memset(instUB->ISACencUB_obj.data_buffer_float, 0,
405          (MAX_FRAMESAMPLES + LB_TOTAL_DELAY_SAMPLES) * sizeof(float));
406
407   memcpy(&(instUB->ISACencUB_obj.lastLPCVec),
408          WebRtcIsac_kMeanLarUb16, sizeof(double) * UB_LPC_ORDER);
409
410   return statusInit;
411 }
412
413
414 int16_t WebRtcIsac_EncoderInit(ISACStruct* ISAC_main_inst,
415                                int16_t codingMode) {
416   ISACMainStruct* instISAC = (ISACMainStruct*)ISAC_main_inst;
417   int16_t status;
418
419   if ((codingMode != 0) && (codingMode != 1)) {
420     instISAC->errorCode = ISAC_DISALLOWED_CODING_MODE;
421     return -1;
422   }
423   /* Default bottleneck. */
424   instISAC->bottleneck = MAX_ISAC_BW;
425
426   if (instISAC->encoderSamplingRateKHz == kIsacWideband) {
427     instISAC->bandwidthKHz = isac8kHz;
428     instISAC->maxPayloadSizeBytes = STREAM_SIZE_MAX_60;
429     instISAC->maxRateBytesPer30Ms = STREAM_SIZE_MAX_30;
430   } else {
431     instISAC->bandwidthKHz = isac16kHz;
432     instISAC->maxPayloadSizeBytes = STREAM_SIZE_MAX;
433     instISAC->maxRateBytesPer30Ms = STREAM_SIZE_MAX;
434   }
435
436   /* Channel-adaptive = 0; Instantaneous (Channel-independent) = 1. */
437   instISAC->codingMode = codingMode;
438
439   WebRtcIsac_InitBandwidthEstimator(&instISAC->bwestimator_obj,
440                                     instISAC->encoderSamplingRateKHz,
441                                     instISAC->decoderSamplingRateKHz);
442
443   WebRtcIsac_InitRateModel(&instISAC->rate_data_obj);
444   /* Default for I-mode. */
445   instISAC->MaxDelay = 10.0;
446
447   status = EncoderInitLb(&instISAC->instLB, codingMode,
448                          instISAC->encoderSamplingRateKHz);
449   if (status < 0) {
450     instISAC->errorCode = -status;
451     return -1;
452   }
453
454   if (instISAC->encoderSamplingRateKHz == kIsacSuperWideband) {
455     /* Initialize encoder filter-bank. */
456     memset(instISAC->analysisFBState1, 0,
457            FB_STATE_SIZE_WORD32 * sizeof(int32_t));
458     memset(instISAC->analysisFBState2, 0,
459            FB_STATE_SIZE_WORD32 * sizeof(int32_t));
460
461     status = EncoderInitUb(&(instISAC->instUB),
462                            instISAC->bandwidthKHz);
463     if (status < 0) {
464       instISAC->errorCode = -status;
465       return -1;
466     }
467   }
468   memset(instISAC->state_in_resampler, 0, sizeof(instISAC->state_in_resampler));
469   /* Initialization is successful, set the flag. */
470   instISAC->initFlag |= BIT_MASK_ENC_INIT;
471   return 0;
472 }
473
474
475 /****************************************************************************
476  * WebRtcIsac_Encode(...)
477  *
478  * This function encodes 10ms frame(s) and inserts it into a package.
479  * Input speech length has to be 160 samples (10ms). The encoder buffers those
480  * 10ms frames until it reaches the chosen Framesize (480 or 960 samples
481  * corresponding to 30 or 60 ms frames), and then proceeds to the encoding.
482  *
483  * Input:
484  *        - ISAC_main_inst    : ISAC instance.
485  *        - speechIn          : input speech vector.
486  *
487  * Output:
488  *        - encoded           : the encoded data vector
489  *
490  * Return value:
491  *                            : >0 - Length (in bytes) of coded data
492  *                            :  0 - The buffer didn't reach the chosen
493  *                                  frameSize so it keeps buffering speech
494  *                                 samples.
495  *                            : -1 - Error
496  */
497 int16_t WebRtcIsac_Encode(ISACStruct* ISAC_main_inst,
498                           const int16_t* speechIn,
499                           uint8_t* encoded) {
500   float inFrame[FRAMESAMPLES_10ms];
501   int16_t speechInLB[FRAMESAMPLES_10ms];
502   int16_t speechInUB[FRAMESAMPLES_10ms];
503   int16_t streamLenLB = 0;
504   int16_t streamLenUB = 0;
505   int16_t streamLen = 0;
506   int16_t k = 0;
507   int garbageLen = 0;
508   int32_t bottleneck = 0;
509   int16_t bottleneckIdx = 0;
510   int16_t jitterInfo = 0;
511
512   ISACMainStruct* instISAC = (ISACMainStruct*)ISAC_main_inst;
513   ISACLBStruct* instLB = &(instISAC->instLB);
514   ISACUBStruct* instUB = &(instISAC->instUB);
515   const int16_t* speech_in_ptr = speechIn;
516   int16_t resampled_buff[FRAMESAMPLES_10ms * 2];
517
518   /* Check if encoder initiated. */
519   if ((instISAC->initFlag & BIT_MASK_ENC_INIT) !=
520       BIT_MASK_ENC_INIT) {
521     instISAC->errorCode = ISAC_ENCODER_NOT_INITIATED;
522     return -1;
523   }
524
525   if (instISAC->in_sample_rate_hz == 48000) {
526     /* Samples in 10 ms @ 48 kHz. */
527     const int kNumInputSamples = FRAMESAMPLES_10ms * 3;
528     /* Samples 10 ms @ 32 kHz. */
529     const int kNumOutputSamples = FRAMESAMPLES_10ms * 2;
530     /* Resampler divide the input into blocks of 3 samples, i.e.
531      * kNumInputSamples / 3. */
532     const int kNumResamplerBlocks = FRAMESAMPLES_10ms;
533     int32_t buffer32[FRAMESAMPLES_10ms * 3 + SIZE_RESAMPLER_STATE];
534
535     /* Restore last samples from the past to the beginning of the buffer
536      * and store the last samples of current frame for the next resampling. */
537     for (k = 0; k < SIZE_RESAMPLER_STATE; k++) {
538       buffer32[k] = instISAC->state_in_resampler[k];
539       instISAC->state_in_resampler[k] = speechIn[kNumInputSamples -
540                                                  SIZE_RESAMPLER_STATE + k];
541     }
542     for (k = 0; k < kNumInputSamples; k++) {
543       buffer32[SIZE_RESAMPLER_STATE + k] = speechIn[k];
544     }
545     /* Resampling 3 samples to 2. Function divides the input in
546      * |kNumResamplerBlocks| number of 3-sample groups, and output is
547      * |kNumResamplerBlocks| number of 2-sample groups. */
548     WebRtcSpl_Resample48khzTo32khz(buffer32, buffer32, kNumResamplerBlocks);
549     WebRtcSpl_VectorBitShiftW32ToW16(resampled_buff, kNumOutputSamples,
550                                      buffer32, 15);
551     speech_in_ptr = resampled_buff;
552   }
553
554   if (instISAC->encoderSamplingRateKHz == kIsacSuperWideband) {
555     WebRtcSpl_AnalysisQMF(speech_in_ptr, SWBFRAMESAMPLES_10ms, speechInLB,
556                           speechInUB, instISAC->analysisFBState1,
557                           instISAC->analysisFBState2);
558
559     /* Convert from fixed to floating point. */
560     for (k = 0; k < FRAMESAMPLES_10ms; k++) {
561       inFrame[k] = (float)speechInLB[k];
562     }
563   } else {
564     for (k = 0; k < FRAMESAMPLES_10ms; k++) {
565       inFrame[k] = (float) speechIn[k];
566     }
567   }
568
569   /* Add some noise to avoid denormal numbers. */
570   inFrame[0] += (float)1.23455334e-3;
571   inFrame[1] -= (float)2.04324239e-3;
572   inFrame[2] += (float)1.90854954e-3;
573   inFrame[9] += (float)1.84854878e-3;
574
575   /* This function will update the bottleneck if required. */
576   UpdateBottleneck(instISAC);
577
578   /* Get the bandwith information which has to be sent to the other side. */
579   GetSendBandwidthInfo(instISAC, &bottleneckIdx, &jitterInfo);
580
581   /* Encode lower-band. */
582   streamLenLB = WebRtcIsac_EncodeLb(inFrame, &instLB->ISACencLB_obj,
583                                     instISAC->codingMode, bottleneckIdx);
584   if (streamLenLB < 0) {
585     return -1;
586   }
587
588   if (instISAC->encoderSamplingRateKHz == kIsacSuperWideband) {
589     instUB = &(instISAC->instUB);
590
591     /* Convert to float. */
592     for (k = 0; k < FRAMESAMPLES_10ms; k++) {
593       inFrame[k] = (float) speechInUB[k];
594     }
595
596     /* Add some noise to avoid denormal numbers. */
597     inFrame[0] += (float)1.23455334e-3;
598     inFrame[1] -= (float)2.04324239e-3;
599     inFrame[2] += (float)1.90854954e-3;
600     inFrame[9] += (float)1.84854878e-3;
601
602     /* Tell to upper-band the number of bytes used so far.
603      * This is for payload limitation. */
604     instUB->ISACencUB_obj.numBytesUsed = streamLenLB + 1 +
605                                          LEN_CHECK_SUM_WORD8;
606     /* Encode upper-band. */
607     switch (instISAC->bandwidthKHz) {
608       case isac12kHz: {
609         streamLenUB = WebRtcIsac_EncodeUb12(inFrame, &instUB->ISACencUB_obj,
610                                             jitterInfo);
611         break;
612       }
613       case isac16kHz: {
614         streamLenUB = WebRtcIsac_EncodeUb16(inFrame, &instUB->ISACencUB_obj,
615                                             jitterInfo);
616         break;
617       }
618       case isac8kHz: {
619         streamLenUB = 0;
620         break;
621       }
622     }
623
624     if ((streamLenUB < 0) && (streamLenUB != -ISAC_PAYLOAD_LARGER_THAN_LIMIT)) {
625       /* An error has happened but this is not the error due to a
626        * bit-stream larger than the limit. */
627       return -1;
628     }
629
630     if (streamLenLB == 0) {
631       return 0;
632     }
633
634     /* One byte is allocated for the length. According to older decoders
635        so the length bit-stream plus one byte for size and
636        LEN_CHECK_SUM_WORD8 for the checksum should be less than or equal
637        to 255. */
638     if ((streamLenUB > (255 - (LEN_CHECK_SUM_WORD8 + 1))) ||
639         (streamLenUB == -ISAC_PAYLOAD_LARGER_THAN_LIMIT)) {
640       /* We have got a too long bit-stream we skip the upper-band
641        * bit-stream for this frame. */
642       streamLenUB = 0;
643     }
644
645     memcpy(encoded, instLB->ISACencLB_obj.bitstr_obj.stream, streamLenLB);
646     streamLen = streamLenLB;
647     if (streamLenUB > 0) {
648       encoded[streamLenLB] = streamLenUB + 1 + LEN_CHECK_SUM_WORD8;
649       memcpy(&encoded[streamLenLB + 1],
650              instUB->ISACencUB_obj.bitstr_obj.stream,
651              streamLenUB);
652       streamLen += encoded[streamLenLB];
653     } else {
654       encoded[streamLenLB] = 0;
655     }
656   } else {
657     if (streamLenLB == 0) {
658       return 0;
659     }
660     memcpy(encoded, instLB->ISACencLB_obj.bitstr_obj.stream, streamLenLB);
661     streamLenUB = 0;
662     streamLen = streamLenLB;
663   }
664
665   /* Add Garbage if required. */
666   WebRtcIsac_GetUplinkBandwidth(&instISAC->bwestimator_obj, &bottleneck);
667   if (instISAC->codingMode == 0) {
668     int minBytes;
669     int limit;
670     uint8_t* ptrGarbage;
671
672     instISAC->MaxDelay = (double)WebRtcIsac_GetUplinkMaxDelay(
673                            &instISAC->bwestimator_obj);
674
675     /* Update rate model and get minimum number of bytes in this packet. */
676     minBytes = WebRtcIsac_GetMinBytes(
677         &(instISAC->rate_data_obj), streamLen,
678         instISAC->instLB.ISACencLB_obj.current_framesamples, bottleneck,
679         instISAC->MaxDelay, instISAC->bandwidthKHz);
680
681     /* Make sure MinBytes does not exceed packet size limit. */
682     if (instISAC->bandwidthKHz == isac8kHz) {
683       if (instLB->ISACencLB_obj.current_framesamples == FRAMESAMPLES) {
684         limit = instLB->ISACencLB_obj.payloadLimitBytes30;
685       } else {
686         limit = instLB->ISACencLB_obj.payloadLimitBytes60;
687       }
688     } else {
689       limit = instUB->ISACencUB_obj.maxPayloadSizeBytes;
690     }
691     minBytes = (minBytes > limit) ? limit : minBytes;
692
693     /* Make sure we don't allow more than 255 bytes of garbage data.
694      * We store the length of the garbage data in 8 bits in the bitstream,
695      * 255 is the max garbage length we can signal using 8 bits. */
696     if ((instISAC->bandwidthKHz == isac8kHz) ||
697         (streamLenUB == 0)) {
698       ptrGarbage = &encoded[streamLenLB];
699       limit = streamLen + 255;
700     } else {
701       ptrGarbage = &encoded[streamLenLB + 1 + streamLenUB];
702       limit = streamLen + (255 - encoded[streamLenLB]);
703     }
704     minBytes = (minBytes > limit) ? limit : minBytes;
705
706     garbageLen = (minBytes > streamLen) ? (minBytes - streamLen) : 0;
707
708     /* Save data for creation of multiple bit-streams. */
709     /* If bit-stream too short then add garbage at the end. */
710     if (garbageLen > 0) {
711       for (k = 0; k < garbageLen; k++) {
712         ptrGarbage[k] = (uint8_t)(rand() & 0xFF);
713       }
714       /* For a correct length of the upper-band bit-stream together
715        * with the garbage. Garbage is embeded in upper-band bit-stream.
716        * That is the only way to preserve backward compatibility. */
717       if ((instISAC->bandwidthKHz == isac8kHz) ||
718           (streamLenUB == 0)) {
719         encoded[streamLenLB] = garbageLen;
720       } else {
721         encoded[streamLenLB] += garbageLen;
722         /* Write the length of the garbage at the end of the upper-band
723          *  bit-stream, if exists. This helps for sanity check. */
724         encoded[streamLenLB + 1 + streamLenUB] = garbageLen;
725
726       }
727       streamLen += garbageLen;
728     }
729   } else {
730     /* update rate model */
731     WebRtcIsac_UpdateRateModel(
732         &instISAC->rate_data_obj, streamLen,
733         instISAC->instLB.ISACencLB_obj.current_framesamples, bottleneck);
734     garbageLen = 0;
735   }
736
737   /* Generate CRC if required. */
738   if ((instISAC->bandwidthKHz != isac8kHz) && (streamLenUB > 0)) {
739     uint32_t crc;
740
741     WebRtcIsac_GetCrc((int16_t*)(&(encoded[streamLenLB + 1])),
742                       streamLenUB + garbageLen, &crc);
743 #ifndef WEBRTC_ARCH_BIG_ENDIAN
744     for (k = 0; k < LEN_CHECK_SUM_WORD8; k++) {
745       encoded[streamLen - LEN_CHECK_SUM_WORD8 + k] = crc >> (24 - k * 8);
746     }
747 #else
748     memcpy(&encoded[streamLenLB + streamLenUB + 1], &crc, LEN_CHECK_SUM_WORD8);
749 #endif
750   }
751   return streamLen;
752 }
753
754
755 /******************************************************************************
756  * WebRtcIsac_GetNewBitStream(...)
757  *
758  * This function returns encoded data, with the recieved bwe-index in the
759  * stream. If the rate is set to a value less than bottleneck of codec
760  * the new bistream will be re-encoded with the given target rate.
761  * It should always return a complete packet, i.e. only called once
762  * even for 60 msec frames.
763  *
764  * NOTE 1! This function does not write in the ISACStruct, it is not allowed.
765  * NOTE 2! Rates larger than the bottleneck of the codec will be limited
766  *         to the current bottleneck.
767  *
768  * Input:
769  *        - ISAC_main_inst    : ISAC instance.
770  *        - bweIndex          : Index of bandwidth estimate to put in new
771  *                              bitstream
772  *        - rate              : target rate of the transcoder is bits/sec.
773  *                              Valid values are the accepted rate in iSAC,
774  *                              i.e. 10000 to 56000.
775  *
776  * Output:
777  *        - encoded           : The encoded data vector
778  *
779  * Return value               : >0 - Length (in bytes) of coded data
780  *                              -1 - Error  or called in SWB mode
781  *                                 NOTE! No error code is written to
782  *                                 the struct since it is only allowed to read
783  *                                 the struct.
784  */
785 int16_t WebRtcIsac_GetNewBitStream(ISACStruct*  ISAC_main_inst,
786                                    int16_t  bweIndex,
787                                    int16_t  jitterInfo,
788                                    int32_t  rate,
789                                    uint8_t* encoded,
790                                    int16_t  isRCU) {
791   Bitstr iSACBitStreamInst;   /* Local struct for bitstream handling */
792   int16_t streamLenLB;
793   int16_t streamLenUB;
794   int16_t totalStreamLen;
795   double gain2;
796   double gain1;
797   float scale;
798   enum ISACBandwidth bandwidthKHz;
799   double rateLB;
800   double rateUB;
801   int32_t currentBN;
802   uint32_t crc;
803 #ifndef WEBRTC_ARCH_BIG_ENDIAN
804   int16_t  k;
805 #endif
806   ISACMainStruct* instISAC = (ISACMainStruct*)ISAC_main_inst;
807
808   if ((instISAC->initFlag & BIT_MASK_ENC_INIT) !=
809       BIT_MASK_ENC_INIT) {
810     return -1;
811   }
812
813   /* Get the bottleneck of this iSAC and limit the
814    * given rate to the current bottleneck. */
815   WebRtcIsac_GetUplinkBw(ISAC_main_inst, &currentBN);
816   if (rate > currentBN) {
817     rate = currentBN;
818   }
819
820   if (WebRtcIsac_RateAllocation(rate, &rateLB, &rateUB, &bandwidthKHz) < 0) {
821     return -1;
822   }
823
824   /* Cannot transcode from 16 kHz to 12 kHz. */
825   if ((bandwidthKHz == isac12kHz) &&
826       (instISAC->bandwidthKHz == isac16kHz)) {
827     return -1;
828   }
829
830   /* A gain [dB] for the given rate. */
831   gain1 = WebRtcIsac_GetSnr(
832       rateLB, instISAC->instLB.ISACencLB_obj.current_framesamples);
833   /* The gain [dB] of this iSAC. */
834   gain2 = WebRtcIsac_GetSnr(
835       instISAC->instLB.ISACencLB_obj.bottleneck,
836       instISAC->instLB.ISACencLB_obj.current_framesamples);
837
838   /* Scale is the ratio of two gains in normal domain. */
839   scale = (float)pow(10, (gain1 - gain2) / 20.0);
840   /* Change the scale if this is a RCU bit-stream. */
841   scale = (isRCU) ? (scale * RCU_TRANSCODING_SCALE) : scale;
842
843   streamLenLB = WebRtcIsac_EncodeStoredDataLb(
844                   &instISAC->instLB.ISACencLB_obj.SaveEnc_obj,
845                   &iSACBitStreamInst, bweIndex, scale);
846
847   if (streamLenLB < 0) {
848     return -1;
849   }
850
851   /* Convert from bytes to int16_t. */
852   memcpy(encoded, iSACBitStreamInst.stream, streamLenLB);
853
854   if (bandwidthKHz == isac8kHz) {
855     return streamLenLB;
856   }
857
858   totalStreamLen = streamLenLB;
859   /* super-wideband is always at 30ms.
860    * These gains are in dB.
861    * Gain for the given rate. */
862   gain1 = WebRtcIsac_GetSnr(rateUB, FRAMESAMPLES);
863   /* Gain of this iSAC */
864   gain2 = WebRtcIsac_GetSnr(instISAC->instUB.ISACencUB_obj.bottleneck,
865                             FRAMESAMPLES);
866
867   /* Scale is the ratio of two gains in normal domain. */
868   scale = (float)pow(10, (gain1 - gain2) / 20.0);
869
870   /* Change the scale if this is a RCU bit-stream. */
871   scale = (isRCU)? (scale * RCU_TRANSCODING_SCALE_UB) : scale;
872
873   streamLenUB = WebRtcIsac_EncodeStoredDataUb(
874                   &(instISAC->instUB.ISACencUB_obj.SaveEnc_obj),
875                   &iSACBitStreamInst, jitterInfo, scale,
876                   instISAC->bandwidthKHz);
877
878   if (streamLenUB < 0) {
879     return -1;
880   }
881
882   if (streamLenUB + 1 + LEN_CHECK_SUM_WORD8 > 255) {
883     return streamLenLB;
884   }
885
886   totalStreamLen = streamLenLB + streamLenUB + 1 + LEN_CHECK_SUM_WORD8;
887   encoded[streamLenLB] = streamLenUB + 1 + LEN_CHECK_SUM_WORD8;
888
889   memcpy(&encoded[streamLenLB + 1], iSACBitStreamInst.stream,
890          streamLenUB);
891
892   WebRtcIsac_GetCrc((int16_t*)(&(encoded[streamLenLB + 1])),
893                     streamLenUB, &crc);
894 #ifndef WEBRTC_ARCH_BIG_ENDIAN
895   for (k = 0; k < LEN_CHECK_SUM_WORD8; k++) {
896     encoded[totalStreamLen - LEN_CHECK_SUM_WORD8 + k] =
897       (uint8_t)((crc >> (24 - k * 8)) & 0xFF);
898   }
899 #else
900   memcpy(&encoded[streamLenLB + streamLenUB + 1], &crc,
901          LEN_CHECK_SUM_WORD8);
902 #endif
903   return totalStreamLen;
904 }
905
906
907 /****************************************************************************
908  * DecoderInitLb(...) - internal function for initialization of
909  *                                Lower Band
910  * DecoderInitUb(...) - internal function for initialization of
911  *                                Upper Band
912  * WebRtcIsac_DecoderInit(...) - API function
913  *
914  * This function initializes a ISAC instance prior to the decoder calls.
915  *
916  * Input:
917  *        - ISAC_main_inst    : ISAC instance.
918  *
919  * Return value
920  *                            :  0 - Ok
921  *                              -1 - Error
922  */
923 static int16_t DecoderInitLb(ISACLBStruct* instISAC) {
924   int i;
925   /* Initialize stream vector to zero. */
926   for (i = 0; i < STREAM_SIZE_MAX_60; i++) {
927     instISAC->ISACdecLB_obj.bitstr_obj.stream[i] = 0;
928   }
929
930   WebRtcIsac_InitMasking(&instISAC->ISACdecLB_obj.maskfiltstr_obj);
931   WebRtcIsac_InitPostFilterbank(
932     &instISAC->ISACdecLB_obj.postfiltbankstr_obj);
933   WebRtcIsac_InitPitchFilter(&instISAC->ISACdecLB_obj.pitchfiltstr_obj);
934   return 0;
935 }
936
937 static int16_t DecoderInitUb(ISACUBStruct* instISAC) {
938   int i;
939   /* Init stream vector to zero */
940   for (i = 0; i < STREAM_SIZE_MAX_60; i++) {
941     instISAC->ISACdecUB_obj.bitstr_obj.stream[i] = 0;
942   }
943
944   WebRtcIsac_InitMasking(&instISAC->ISACdecUB_obj.maskfiltstr_obj);
945   WebRtcIsac_InitPostFilterbank(
946     &instISAC->ISACdecUB_obj.postfiltbankstr_obj);
947   return (0);
948 }
949
950 int16_t WebRtcIsac_DecoderInit(ISACStruct* ISAC_main_inst) {
951   ISACMainStruct* instISAC = (ISACMainStruct*)ISAC_main_inst;
952
953   if (DecoderInitLb(&instISAC->instLB) < 0) {
954     return -1;
955   }
956   if (instISAC->decoderSamplingRateKHz == kIsacSuperWideband) {
957     memset(instISAC->synthesisFBState1, 0,
958            FB_STATE_SIZE_WORD32 * sizeof(int32_t));
959     memset(instISAC->synthesisFBState2, 0,
960            FB_STATE_SIZE_WORD32 * sizeof(int32_t));
961
962     if (DecoderInitUb(&(instISAC->instUB)) < 0) {
963       return -1;
964     }
965   }
966   if ((instISAC->initFlag & BIT_MASK_ENC_INIT) != BIT_MASK_ENC_INIT) {
967     WebRtcIsac_InitBandwidthEstimator(&instISAC->bwestimator_obj,
968                                       instISAC->encoderSamplingRateKHz,
969                                       instISAC->decoderSamplingRateKHz);
970   }
971   instISAC->initFlag |= BIT_MASK_DEC_INIT;
972   instISAC->resetFlag_8kHz = 0;
973   return 0;
974 }
975
976
977 /****************************************************************************
978  * WebRtcIsac_UpdateBwEstimate(...)
979  *
980  * This function updates the estimate of the bandwidth.
981  *
982  * NOTE:
983  * The estimates of bandwidth is not valid if the sample rate of the far-end
984  * encoder is set to 48 kHz and send timestamps are increamented according to
985  * 48 kHz sampling rate.
986  *
987  * Input:
988  *        - ISAC_main_inst    : ISAC instance.
989  *        - encoded           : encoded ISAC frame(s).
990  *        - packet_size       : size of the packet.
991  *        - rtp_seq_number    : the RTP number of the packet.
992  *        - arr_ts            : the arrival time of the packet (from NetEq)
993  *                              in samples.
994  *
995  * Return value               :  0 - Ok
996  *                              -1 - Error
997  */
998 int16_t WebRtcIsac_UpdateBwEstimate(ISACStruct* ISAC_main_inst,
999                                     const uint8_t* encoded,
1000                                     int32_t packet_size,
1001                                     uint16_t rtp_seq_number,
1002                                     uint32_t send_ts,
1003                                     uint32_t arr_ts) {
1004   ISACMainStruct* instISAC = (ISACMainStruct*)ISAC_main_inst;
1005   Bitstr streamdata;
1006 #ifndef WEBRTC_ARCH_BIG_ENDIAN
1007   int k;
1008 #endif
1009   int16_t err;
1010
1011   /* Check if decoder initiated. */
1012   if ((instISAC->initFlag & BIT_MASK_DEC_INIT) != BIT_MASK_DEC_INIT) {
1013     instISAC->errorCode = ISAC_DECODER_NOT_INITIATED;
1014     return -1;
1015   }
1016
1017   /* Check that the size of the packet is valid, and if not return without
1018    * updating the bandwidth estimate. A valid size is at least 10 bytes. */
1019   if (packet_size < 10) {
1020     /* Return error code if the packet length is null. */
1021     instISAC->errorCode = ISAC_EMPTY_PACKET;
1022     return -1;
1023   }
1024
1025   WebRtcIsac_ResetBitstream(&(streamdata));
1026
1027 #ifndef WEBRTC_ARCH_BIG_ENDIAN
1028   for (k = 0; k < 10; k++) {
1029     uint16_t ek = ((const uint16_t*)encoded)[k >> 1];
1030     streamdata.stream[k] = (uint8_t)((ek >> ((k & 1) << 3)) & 0xff);
1031   }
1032 #else
1033   memcpy(streamdata.stream, encoded, 10);
1034 #endif
1035
1036   err = WebRtcIsac_EstimateBandwidth(&instISAC->bwestimator_obj, &streamdata,
1037                                      packet_size, rtp_seq_number, send_ts,
1038                                      arr_ts, instISAC->encoderSamplingRateKHz,
1039                                      instISAC->decoderSamplingRateKHz);
1040   if (err < 0) {
1041     /* Return error code if something went wrong. */
1042     instISAC->errorCode = -err;
1043     return -1;
1044   }
1045   return 0;
1046 }
1047
1048 static int16_t Decode(ISACStruct* ISAC_main_inst,
1049                       const uint8_t* encoded,
1050                       int16_t lenEncodedBytes,
1051                       int16_t* decoded,
1052                       int16_t* speechType,
1053                       int16_t isRCUPayload) {
1054   /* Number of samples (480 or 960), output from decoder
1055      that were actually used in the encoder/decoder
1056      (determined on the fly). */
1057   int16_t numSamplesLB;
1058   int16_t numSamplesUB;
1059   int16_t speechIdx;
1060   float outFrame[MAX_FRAMESAMPLES];
1061   int16_t outFrameLB[MAX_FRAMESAMPLES];
1062   int16_t outFrameUB[MAX_FRAMESAMPLES];
1063   int16_t numDecodedBytesLB;
1064   int16_t numDecodedBytesUB;
1065   int16_t lenEncodedLBBytes;
1066   int16_t validChecksum = 1;
1067   int16_t k;
1068   uint16_t numLayer;
1069   int16_t totSizeBytes;
1070   int16_t err;
1071
1072   ISACMainStruct* instISAC = (ISACMainStruct*)ISAC_main_inst;
1073   ISACUBDecStruct* decInstUB = &(instISAC->instUB.ISACdecUB_obj);
1074   ISACLBDecStruct* decInstLB = &(instISAC->instLB.ISACdecLB_obj);
1075
1076   /* Check if decoder initiated. */
1077   if ((instISAC->initFlag & BIT_MASK_DEC_INIT) !=
1078       BIT_MASK_DEC_INIT) {
1079     instISAC->errorCode = ISAC_DECODER_NOT_INITIATED;
1080     return -1;
1081   }
1082
1083   if (lenEncodedBytes <= 0) {
1084     /* return error code if the packet length is null. */
1085     instISAC->errorCode = ISAC_EMPTY_PACKET;
1086     return -1;
1087   }
1088
1089   /* The size of the encoded lower-band is bounded by
1090    * STREAM_SIZE_MAX. If a payload with the size larger than STREAM_SIZE_MAX
1091    * is received, it is not considered erroneous. */
1092   lenEncodedLBBytes = (lenEncodedBytes > STREAM_SIZE_MAX) ?
1093       STREAM_SIZE_MAX : lenEncodedBytes;
1094
1095   /* Copy to lower-band bit-stream structure. */
1096   memcpy(instISAC->instLB.ISACdecLB_obj.bitstr_obj.stream, encoded,
1097          lenEncodedLBBytes);
1098
1099   /* Regardless of that the current codec is setup to work in
1100    * wideband or super-wideband, the decoding of the lower-band
1101    * has to be performed. */
1102   numDecodedBytesLB = WebRtcIsac_DecodeLb(outFrame, decInstLB,
1103                                           &numSamplesLB, isRCUPayload);
1104
1105   if ((numDecodedBytesLB < 0) || (numDecodedBytesLB > lenEncodedLBBytes) ||
1106       (numSamplesLB > MAX_FRAMESAMPLES)) {
1107     instISAC->errorCode = ISAC_LENGTH_MISMATCH;
1108     return -1;
1109   }
1110
1111   /* Error Check, we accept multi-layer bit-stream This will limit number
1112    * of iterations of the while loop. Even without this the number
1113    * of iterations is limited. */
1114   numLayer = 1;
1115   totSizeBytes = numDecodedBytesLB;
1116   while (totSizeBytes != lenEncodedBytes) {
1117     if ((totSizeBytes > lenEncodedBytes) ||
1118         (encoded[totSizeBytes] == 0) ||
1119         (numLayer > MAX_NUM_LAYERS)) {
1120       instISAC->errorCode = ISAC_LENGTH_MISMATCH;
1121       return -1;
1122     }
1123     totSizeBytes += encoded[totSizeBytes];
1124     numLayer++;
1125   }
1126
1127   if (instISAC->decoderSamplingRateKHz == kIsacWideband) {
1128     for (k = 0; k < numSamplesLB; k++) {
1129       if (outFrame[k] > 32767) {
1130         decoded[k] = 32767;
1131       } else if (outFrame[k] < -32768) {
1132         decoded[k] = -32768;
1133       } else {
1134         decoded[k] = (int16_t)WebRtcIsac_lrint(outFrame[k]);
1135       }
1136     }
1137     numSamplesUB = 0;
1138   } else {
1139     uint32_t crc;
1140     /* We don't accept larger than 30ms (480 samples at lower-band)
1141      * frame-size. */
1142     for (k = 0; k < numSamplesLB; k++) {
1143       if (outFrame[k] > 32767) {
1144         outFrameLB[k] = 32767;
1145       } else if (outFrame[k] < -32768) {
1146         outFrameLB[k] = -32768;
1147       } else {
1148         outFrameLB[k] = (int16_t)WebRtcIsac_lrint(outFrame[k]);
1149       }
1150     }
1151
1152     /* Check for possible error, and if upper-band stream exists. */
1153     if (numDecodedBytesLB == lenEncodedBytes) {
1154       /* Decoding was successful. No super-wideband bit-stream exists. */
1155       numSamplesUB = numSamplesLB;
1156       memset(outFrameUB, 0, sizeof(int16_t) *  numSamplesUB);
1157
1158       /* Prepare for the potential increase of signal bandwidth. */
1159       instISAC->resetFlag_8kHz = 2;
1160     } else {
1161       /* This includes the checksum and the bytes that stores the length. */
1162       int16_t lenNextStream = encoded[numDecodedBytesLB];
1163
1164       /* Is this garbage or valid super-wideband bit-stream?
1165        * Check if checksum is valid. */
1166       if (lenNextStream <= (LEN_CHECK_SUM_WORD8 + 1)) {
1167         /* Such a small second layer cannot be super-wideband layer.
1168          * It must be a short garbage. */
1169         validChecksum = 0;
1170       } else {
1171         /* Run CRC to see if the checksum match. */
1172         WebRtcIsac_GetCrc((int16_t*)(&encoded[numDecodedBytesLB + 1]),
1173                           lenNextStream - LEN_CHECK_SUM_WORD8 - 1, &crc);
1174
1175         validChecksum = 1;
1176         for (k = 0; k < LEN_CHECK_SUM_WORD8; k++) {
1177           validChecksum &= (((crc >> (24 - k * 8)) & 0xFF) ==
1178                             encoded[numDecodedBytesLB + lenNextStream -
1179                                           LEN_CHECK_SUM_WORD8 + k]);
1180         }
1181       }
1182
1183       if (!validChecksum) {
1184         /* This is a garbage, we have received a wideband
1185          * bit-stream with garbage. */
1186         numSamplesUB = numSamplesLB;
1187         memset(outFrameUB, 0, sizeof(int16_t) * numSamplesUB);
1188       } else {
1189         /* A valid super-wideband biststream exists. */
1190         enum ISACBandwidth bandwidthKHz;
1191         int32_t maxDelayBit;
1192
1193         /* If we have super-wideband bit-stream, we cannot
1194          * have 60 ms frame-size. */
1195         if (numSamplesLB > FRAMESAMPLES) {
1196           instISAC->errorCode = ISAC_LENGTH_MISMATCH;
1197           return -1;
1198         }
1199
1200         /* The rest of the bit-stream contains the upper-band
1201          * bit-stream curently this is the only thing there,
1202          * however, we might add more layers. */
1203
1204         /* Have to exclude one byte where the length is stored
1205          * and last 'LEN_CHECK_SUM_WORD8' bytes where the
1206          * checksum is stored. */
1207         lenNextStream -= (LEN_CHECK_SUM_WORD8 + 1);
1208
1209         memcpy(decInstUB->bitstr_obj.stream,
1210                &encoded[numDecodedBytesLB + 1], lenNextStream);
1211
1212         /* Reset bit-stream object, this is the first decoding. */
1213         WebRtcIsac_ResetBitstream(&(decInstUB->bitstr_obj));
1214
1215         /* Decode jitter information. */
1216         err = WebRtcIsac_DecodeJitterInfo(&decInstUB->bitstr_obj, &maxDelayBit);
1217         if (err < 0) {
1218           instISAC->errorCode = -err;
1219           return -1;
1220         }
1221
1222         /* Update jitter info which is in the upper-band bit-stream
1223          * only if the encoder is in super-wideband. Otherwise,
1224          * the jitter info is already embedded in bandwidth index
1225          * and has been updated. */
1226         if (instISAC->encoderSamplingRateKHz == kIsacSuperWideband) {
1227           err = WebRtcIsac_UpdateUplinkJitter(
1228                   &(instISAC->bwestimator_obj), maxDelayBit);
1229           if (err < 0) {
1230             instISAC->errorCode = -err;
1231             return -1;
1232           }
1233         }
1234
1235         /* Decode bandwidth information. */
1236         err = WebRtcIsac_DecodeBandwidth(&decInstUB->bitstr_obj,
1237                                          &bandwidthKHz);
1238         if (err < 0) {
1239           instISAC->errorCode = -err;
1240           return -1;
1241         }
1242
1243         switch (bandwidthKHz) {
1244           case isac12kHz: {
1245             numDecodedBytesUB = WebRtcIsac_DecodeUb12(outFrame, decInstUB,
1246                                                       isRCUPayload);
1247
1248             /* Hang-over for transient alleviation -
1249              * wait two frames to add the upper band going up from 8 kHz. */
1250             if (instISAC->resetFlag_8kHz > 0) {
1251               if (instISAC->resetFlag_8kHz == 2) {
1252                 /* Silence first and a half frame. */
1253                 memset(outFrame, 0, MAX_FRAMESAMPLES *
1254                        sizeof(float));
1255               } else {
1256                 const float rampStep = 2.0f / MAX_FRAMESAMPLES;
1257                 float rampVal = 0;
1258                 memset(outFrame, 0, (MAX_FRAMESAMPLES >> 1) *
1259                        sizeof(float));
1260
1261                 /* Ramp up second half of second frame. */
1262                 for (k = MAX_FRAMESAMPLES / 2; k < MAX_FRAMESAMPLES; k++) {
1263                   outFrame[k] *= rampVal;
1264                   rampVal += rampStep;
1265                 }
1266               }
1267               instISAC->resetFlag_8kHz -= 1;
1268             }
1269
1270             break;
1271           }
1272           case isac16kHz: {
1273             numDecodedBytesUB = WebRtcIsac_DecodeUb16(outFrame, decInstUB,
1274                                                       isRCUPayload);
1275             break;
1276           }
1277           default:
1278             return -1;
1279         }
1280
1281         /* It might be less due to garbage. */
1282         if ((numDecodedBytesUB != lenNextStream) &&
1283             (numDecodedBytesUB != (lenNextStream -
1284                 encoded[numDecodedBytesLB + 1 + numDecodedBytesUB]))) {
1285           instISAC->errorCode = ISAC_LENGTH_MISMATCH;
1286           return -1;
1287         }
1288
1289         /* If there is no error Upper-band always decodes
1290          * 30 ms (480 samples). */
1291         numSamplesUB = FRAMESAMPLES;
1292
1293         /* Convert to W16. */
1294         for (k = 0; k < numSamplesUB; k++) {
1295           if (outFrame[k] > 32767) {
1296             outFrameUB[k] = 32767;
1297           } else if (outFrame[k] < -32768) {
1298             outFrameUB[k] = -32768;
1299           } else {
1300             outFrameUB[k] = (int16_t)WebRtcIsac_lrint(
1301                               outFrame[k]);
1302           }
1303         }
1304       }
1305     }
1306
1307     speechIdx = 0;
1308     while (speechIdx < numSamplesLB) {
1309       WebRtcSpl_SynthesisQMF(&outFrameLB[speechIdx], &outFrameUB[speechIdx],
1310                              FRAMESAMPLES_10ms, &decoded[(speechIdx << 1)],
1311                              instISAC->synthesisFBState1,
1312                              instISAC->synthesisFBState2);
1313
1314       speechIdx += FRAMESAMPLES_10ms;
1315     }
1316   }
1317   *speechType = 0;
1318   return (numSamplesLB + numSamplesUB);
1319 }
1320
1321
1322
1323
1324
1325
1326
1327 /****************************************************************************
1328  * WebRtcIsac_Decode(...)
1329  *
1330  * This function decodes a ISAC frame. Output speech length
1331  * will be a multiple of 480 samples: 480 or 960 samples,
1332  * depending on the  frameSize (30 or 60 ms).
1333  *
1334  * Input:
1335  *        - ISAC_main_inst    : ISAC instance.
1336  *        - encoded           : encoded ISAC frame(s)
1337  *        - len               : bytes in encoded vector
1338  *
1339  * Output:
1340  *        - decoded           : The decoded vector
1341  *
1342  * Return value               : >0 - number of samples in decoded vector
1343  *                              -1 - Error
1344  */
1345
1346 int16_t WebRtcIsac_Decode(ISACStruct* ISAC_main_inst,
1347                           const uint8_t* encoded,
1348                           int16_t lenEncodedBytes,
1349                           int16_t* decoded,
1350                           int16_t* speechType) {
1351   int16_t isRCUPayload = 0;
1352   return Decode(ISAC_main_inst, encoded, lenEncodedBytes, decoded,
1353                 speechType, isRCUPayload);
1354 }
1355
1356 /****************************************************************************
1357  * WebRtcIsac_DecodeRcu(...)
1358  *
1359  * This function decodes a redundant (RCU) iSAC frame. Function is called in
1360  * NetEq with a stored RCU payload in case of packet loss. Output speech length
1361  * will be a multiple of 480 samples: 480 or 960 samples,
1362  * depending on the framesize (30 or 60 ms).
1363  *
1364  * Input:
1365  *      - ISAC_main_inst     : ISAC instance.
1366  *      - encoded            : encoded ISAC RCU frame(s)
1367  *      - len                : bytes in encoded vector
1368  *
1369  * Output:
1370  *      - decoded            : The decoded vector
1371  *
1372  * Return value              : >0 - number of samples in decoded vector
1373  *                             -1 - Error
1374  */
1375
1376
1377
1378 int16_t WebRtcIsac_DecodeRcu(ISACStruct* ISAC_main_inst,
1379                              const uint8_t* encoded,
1380                              int16_t lenEncodedBytes,
1381                              int16_t* decoded,
1382                              int16_t* speechType) {
1383   int16_t isRCUPayload = 1;
1384   return Decode(ISAC_main_inst, encoded, lenEncodedBytes, decoded,
1385                 speechType, isRCUPayload);
1386 }
1387
1388
1389 /****************************************************************************
1390  * WebRtcIsac_DecodePlc(...)
1391  *
1392  * This function conducts PLC for ISAC frame(s). Output speech length
1393  * will be a multiple of 480 samples: 480 or 960 samples,
1394  * depending on the  frameSize (30 or 60 ms).
1395  *
1396  * Input:
1397  *        - ISAC_main_inst    : ISAC instance.
1398  *        - noOfLostFrames    : Number of PLC frames to produce
1399  *
1400  * Output:
1401  *        - decoded           : The decoded vector
1402  *
1403  * Return value               : >0 - number of samples in decoded PLC vector
1404  *                              -1 - Error
1405  */
1406 int16_t WebRtcIsac_DecodePlc(ISACStruct* ISAC_main_inst,
1407                              int16_t* decoded,
1408                              int16_t noOfLostFrames) {
1409   int16_t numSamples = 0;
1410   ISACMainStruct* instISAC = (ISACMainStruct*)ISAC_main_inst;
1411
1412   /* Limit number of frames to two = 60 millisecond.
1413    * Otherwise we exceed data vectors. */
1414   if (noOfLostFrames > 2) {
1415     noOfLostFrames = 2;
1416   }
1417
1418   /* Get the number of samples per frame */
1419   switch (instISAC->decoderSamplingRateKHz) {
1420     case kIsacWideband: {
1421       numSamples = 480 * noOfLostFrames;
1422       break;
1423     }
1424     case kIsacSuperWideband: {
1425       numSamples = 960 * noOfLostFrames;
1426       break;
1427     }
1428   }
1429
1430   /* Set output samples to zero. */
1431   memset(decoded, 0, numSamples * sizeof(int16_t));
1432   return numSamples;
1433 }
1434
1435
1436 /****************************************************************************
1437  * ControlLb(...) - Internal function for controlling Lower Band
1438  * ControlUb(...) - Internal function for controlling Upper Band
1439  * WebRtcIsac_Control(...) - API function
1440  *
1441  * This function sets the limit on the short-term average bit rate and the
1442  * frame length. Should be used only in Instantaneous mode.
1443  *
1444  * Input:
1445  *        - ISAC_main_inst    : ISAC instance.
1446  *        - rate              : limit on the short-term average bit rate,
1447  *                              in bits/second (between 10000 and 32000)
1448  *        - frameSize         : number of milliseconds per frame (30 or 60)
1449  *
1450  * Return value               : 0 - ok
1451  *                             -1 - Error
1452  */
1453 static int16_t ControlLb(ISACLBStruct* instISAC, double rate,
1454                          int16_t frameSize) {
1455   if ((rate >= 10000) && (rate <= 32000)) {
1456     instISAC->ISACencLB_obj.bottleneck = rate;
1457   } else {
1458     return -ISAC_DISALLOWED_BOTTLENECK;
1459   }
1460
1461   if ((frameSize == 30) || (frameSize == 60)) {
1462     instISAC->ISACencLB_obj.new_framelength = (FS / 1000) *  frameSize;
1463   } else {
1464     return -ISAC_DISALLOWED_FRAME_LENGTH;
1465   }
1466
1467   return 0;
1468 }
1469
1470 static int16_t ControlUb(ISACUBStruct* instISAC, double rate) {
1471   if ((rate >= 10000) && (rate <= 32000)) {
1472     instISAC->ISACencUB_obj.bottleneck = rate;
1473   } else {
1474     return -ISAC_DISALLOWED_BOTTLENECK;
1475   }
1476   return 0;
1477 }
1478
1479 int16_t WebRtcIsac_Control(ISACStruct* ISAC_main_inst,
1480                            int32_t bottleneckBPS,
1481                            int16_t frameSize) {
1482   ISACMainStruct* instISAC = (ISACMainStruct*)ISAC_main_inst;
1483   int16_t status;
1484   double rateLB;
1485   double rateUB;
1486   enum ISACBandwidth bandwidthKHz;
1487
1488   if (instISAC->codingMode == 0) {
1489     /* In adaptive mode. */
1490     instISAC->errorCode = ISAC_MODE_MISMATCH;
1491     return -1;
1492   }
1493
1494   /* Check if encoder initiated */
1495   if ((instISAC->initFlag & BIT_MASK_ENC_INIT) !=
1496       BIT_MASK_ENC_INIT) {
1497     instISAC->errorCode = ISAC_ENCODER_NOT_INITIATED;
1498     return -1;
1499   }
1500
1501   if (instISAC->encoderSamplingRateKHz == kIsacWideband) {
1502     /* If the sampling rate is 16kHz then bandwith should be 8kHz,
1503      * regardless of bottleneck. */
1504     bandwidthKHz = isac8kHz;
1505     rateLB = (bottleneckBPS > 32000) ? 32000 : bottleneckBPS;
1506     rateUB = 0;
1507   } else {
1508     if (WebRtcIsac_RateAllocation(bottleneckBPS, &rateLB, &rateUB,
1509                                   &bandwidthKHz) < 0) {
1510       return -1;
1511     }
1512   }
1513
1514   if ((instISAC->encoderSamplingRateKHz == kIsacSuperWideband) &&
1515       (frameSize != 30) &&
1516       (bandwidthKHz != isac8kHz)) {
1517     /* Cannot have 60 ms in super-wideband. */
1518     instISAC->errorCode = ISAC_DISALLOWED_FRAME_LENGTH;
1519     return -1;
1520   }
1521
1522   status = ControlLb(&instISAC->instLB, rateLB, frameSize);
1523   if (status < 0) {
1524     instISAC->errorCode = -status;
1525     return -1;
1526   }
1527   if (bandwidthKHz != isac8kHz) {
1528     status = ControlUb(&(instISAC->instUB), rateUB);
1529     if (status < 0) {
1530       instISAC->errorCode = -status;
1531       return -1;
1532     }
1533   }
1534
1535
1536   /* Check if bandwidth is changing from wideband to super-wideband
1537    * then we have to synch data buffer of lower & upper-band. Also
1538    * clean up the upper-band data buffer. */
1539
1540   if ((instISAC->bandwidthKHz == isac8kHz) && (bandwidthKHz != isac8kHz)) {
1541     memset(instISAC->instUB.ISACencUB_obj.data_buffer_float, 0,
1542            sizeof(float) * (MAX_FRAMESAMPLES + LB_TOTAL_DELAY_SAMPLES));
1543
1544     if (bandwidthKHz == isac12kHz) {
1545       instISAC->instUB.ISACencUB_obj.buffer_index =
1546         instISAC->instLB.ISACencLB_obj.buffer_index;
1547     } else {
1548       instISAC->instUB.ISACencUB_obj.buffer_index =
1549           LB_TOTAL_DELAY_SAMPLES + instISAC->instLB.ISACencLB_obj.buffer_index;
1550
1551       memcpy(&(instISAC->instUB.ISACencUB_obj.lastLPCVec),
1552              WebRtcIsac_kMeanLarUb16, sizeof(double) * UB_LPC_ORDER);
1553     }
1554   }
1555
1556   /* Update the payload limit if the bandwidth is changing. */
1557   if (instISAC->bandwidthKHz != bandwidthKHz) {
1558     instISAC->bandwidthKHz = bandwidthKHz;
1559     UpdatePayloadSizeLimit(instISAC);
1560   }
1561   instISAC->bottleneck = bottleneckBPS;
1562   return 0;
1563 }
1564
1565
1566 /****************************************************************************
1567  * WebRtcIsac_ControlBwe(...)
1568  *
1569  * This function sets the initial values of bottleneck and frame-size if
1570  * iSAC is used in channel-adaptive mode. Through this API, users can
1571  * enforce a frame-size for all values of bottleneck. Then iSAC will not
1572  * automatically change the frame-size.
1573  *
1574  *
1575  * Input:
1576  *        - ISAC_main_inst    : ISAC instance.
1577  *        - rateBPS           : initial value of bottleneck in bits/second
1578  *                              10000 <= rateBPS <= 32000 is accepted
1579  *                              For default bottleneck set rateBPS = 0
1580  *        - frameSizeMs       : number of milliseconds per frame (30 or 60)
1581  *        - enforceFrameSize  : 1 to enforce the given frame-size through out
1582  *                              the adaptation process, 0 to let iSAC change
1583  *                              the frame-size if required.
1584  *
1585  * Return value               : 0 - ok
1586  *                             -1 - Error
1587  */
1588 int16_t WebRtcIsac_ControlBwe(ISACStruct* ISAC_main_inst,
1589                               int32_t bottleneckBPS,
1590                               int16_t frameSizeMs,
1591                               int16_t enforceFrameSize) {
1592   ISACMainStruct* instISAC = (ISACMainStruct*)ISAC_main_inst;
1593   enum ISACBandwidth bandwidth;
1594
1595    /* Check if encoder initiated */
1596   if ((instISAC->initFlag & BIT_MASK_ENC_INIT) !=
1597       BIT_MASK_ENC_INIT) {
1598     instISAC->errorCode = ISAC_ENCODER_NOT_INITIATED;
1599     return -1;
1600   }
1601
1602   /* Check that we are in channel-adaptive mode, otherwise, return (-1) */
1603   if (instISAC->codingMode != 0) {
1604     instISAC->errorCode = ISAC_MODE_MISMATCH;
1605     return -1;
1606   }
1607   if ((frameSizeMs != 30) &&
1608       (instISAC->encoderSamplingRateKHz == kIsacSuperWideband)) {
1609     return -1;
1610   }
1611
1612   /* Set structure variable if enforceFrameSize is set. ISAC will then
1613    * keep the chosen frame size. */
1614   if (enforceFrameSize != 0) {
1615     instISAC->instLB.ISACencLB_obj.enforceFrameSize = 1;
1616   } else {
1617     instISAC->instLB.ISACencLB_obj.enforceFrameSize = 0;
1618   }
1619
1620   /* Set the initial rate. If the input value is zero then the default intial
1621    * rate is used. Otehrwise, values between 10 to 32 kbps are accepted. */
1622   if (bottleneckBPS != 0) {
1623     double rateLB;
1624     double rateUB;
1625     if (WebRtcIsac_RateAllocation(bottleneckBPS, &rateLB, &rateUB,
1626                                   &bandwidth) < 0) {
1627       return -1;
1628     }
1629     instISAC->bwestimator_obj.send_bw_avg = (float)bottleneckBPS;
1630     instISAC->bandwidthKHz = bandwidth;
1631   }
1632
1633   /* Set the initial frame-size. If 'enforceFrameSize' is set, the frame-size
1634    *  will not change */
1635   if (frameSizeMs != 0) {
1636     if ((frameSizeMs  == 30) || (frameSizeMs == 60)) {
1637       instISAC->instLB.ISACencLB_obj.new_framelength = (FS / 1000) *
1638           frameSizeMs;
1639     } else {
1640       instISAC->errorCode = ISAC_DISALLOWED_FRAME_LENGTH;
1641       return -1;
1642     }
1643   }
1644   return 0;
1645 }
1646
1647
1648 /****************************************************************************
1649  * WebRtcIsac_GetDownLinkBwIndex(...)
1650  *
1651  * This function returns index representing the Bandwidth estimate from
1652  * the other side to this side.
1653  *
1654  * Input:
1655  *        - ISAC_main_inst    : iSAC structure
1656  *
1657  * Output:
1658  *        - bweIndex         : Bandwidth estimate to transmit to other side.
1659  *
1660  */
1661 int16_t WebRtcIsac_GetDownLinkBwIndex(ISACStruct* ISAC_main_inst,
1662                                       int16_t* bweIndex,
1663                                       int16_t* jitterInfo) {
1664   ISACMainStruct* instISAC = (ISACMainStruct*)ISAC_main_inst;
1665
1666   /* Check if encoder initialized. */
1667   if ((instISAC->initFlag & BIT_MASK_DEC_INIT) !=
1668       BIT_MASK_DEC_INIT) {
1669     instISAC->errorCode = ISAC_ENCODER_NOT_INITIATED;
1670     return -1;
1671   }
1672
1673   /* Call function to get Bandwidth Estimate. */
1674   WebRtcIsac_GetDownlinkBwJitIndexImpl(&(instISAC->bwestimator_obj), bweIndex,
1675                                        jitterInfo,
1676                                        instISAC->decoderSamplingRateKHz);
1677   return 0;
1678 }
1679
1680
1681 /****************************************************************************
1682  * WebRtcIsac_UpdateUplinkBw(...)
1683  *
1684  * This function takes an index representing the Bandwidth estimate from
1685  * this side to other side and updates BWE.
1686  *
1687  * Input:
1688  *        - ISAC_main_inst    : iSAC structure
1689  *        - rateIndex         : Bandwidth estimate from other side.
1690  *
1691  * Return value               : 0 - ok
1692  *                             -1 - index out of range
1693  */
1694 int16_t WebRtcIsac_UpdateUplinkBw(ISACStruct* ISAC_main_inst,
1695                                   int16_t bweIndex) {
1696   ISACMainStruct* instISAC = (ISACMainStruct*)ISAC_main_inst;
1697   int16_t returnVal;
1698
1699   /* Check if encoder initiated. */
1700   if ((instISAC->initFlag & BIT_MASK_ENC_INIT) !=
1701       BIT_MASK_ENC_INIT) {
1702     instISAC->errorCode = ISAC_ENCODER_NOT_INITIATED;
1703     return -1;
1704   }
1705
1706   /* Call function to get Bandwidth Estimate. */
1707   returnVal = WebRtcIsac_UpdateUplinkBwImpl(
1708                 &(instISAC->bwestimator_obj), bweIndex,
1709                 instISAC->encoderSamplingRateKHz);
1710
1711   if (returnVal < 0) {
1712     instISAC->errorCode = -returnVal;
1713     return -1;
1714   } else {
1715     return 0;
1716   }
1717 }
1718
1719
1720 /****************************************************************************
1721  * WebRtcIsac_ReadBwIndex(...)
1722  *
1723  * This function returns the index of the Bandwidth estimate from the
1724  * bit-stream.
1725  *
1726  * Input:
1727  *        - encoded           : Encoded bit-stream
1728  *
1729  * Output:
1730  *        - frameLength       : Length of frame in packet (in samples)
1731  *        - bweIndex          : Bandwidth estimate in bit-stream
1732  *
1733  */
1734 int16_t WebRtcIsac_ReadBwIndex(const uint8_t* encoded,
1735                                int16_t* bweIndex) {
1736   Bitstr streamdata;
1737 #ifndef WEBRTC_ARCH_BIG_ENDIAN
1738   int k;
1739 #endif
1740   int16_t err;
1741
1742   WebRtcIsac_ResetBitstream(&(streamdata));
1743
1744 #ifndef WEBRTC_ARCH_BIG_ENDIAN
1745   for (k = 0; k < 10; k++) {
1746     int16_t ek2 = ((const int16_t*)encoded)[k >> 1];
1747     streamdata.stream[k] = (uint8_t)((ek2 >> ((k & 1) << 3)) & 0xff);
1748   }
1749 #else
1750   memcpy(streamdata.stream, encoded, 10);
1751 #endif
1752
1753   /* Decode frame length. */
1754   err = WebRtcIsac_DecodeFrameLen(&streamdata, bweIndex);
1755   if (err < 0) {
1756     return err;
1757   }
1758
1759   /* Decode BW estimation. */
1760   err = WebRtcIsac_DecodeSendBW(&streamdata, bweIndex);
1761   if (err < 0) {
1762     return err;
1763   }
1764
1765   return 0;
1766 }
1767
1768
1769 /****************************************************************************
1770  * WebRtcIsac_ReadFrameLen(...)
1771  *
1772  * This function returns the number of samples the decoder will generate if
1773  * the given payload is decoded.
1774  *
1775  * Input:
1776  *        - encoded           : Encoded bitstream
1777  *
1778  * Output:
1779  *        - frameLength       : Length of frame in packet (in samples)
1780  *
1781  */
1782 int16_t WebRtcIsac_ReadFrameLen(ISACStruct* ISAC_main_inst,
1783                                 const uint8_t* encoded,
1784                                 int16_t* frameLength) {
1785   Bitstr streamdata;
1786 #ifndef WEBRTC_ARCH_BIG_ENDIAN
1787   int k;
1788 #endif
1789   int16_t err;
1790   ISACMainStruct* instISAC;
1791
1792   WebRtcIsac_ResetBitstream(&(streamdata));
1793
1794 #ifndef WEBRTC_ARCH_BIG_ENDIAN
1795   for (k = 0; k < 10; k++) {
1796     int16_t ek2 = ((const int16_t*)encoded)[k >> 1];
1797     streamdata.stream[k] = (uint8_t)((ek2 >> ((k & 1) << 3)) & 0xff);
1798   }
1799 #else
1800   memcpy(streamdata.stream, encoded, 10);
1801 #endif
1802
1803   /* Decode frame length. */
1804   err = WebRtcIsac_DecodeFrameLen(&streamdata, frameLength);
1805   if (err < 0) {
1806     return -1;
1807   }
1808   instISAC = (ISACMainStruct*)ISAC_main_inst;
1809
1810   if (instISAC->decoderSamplingRateKHz == kIsacSuperWideband) {
1811     /* The decoded frame length indicates the number of samples in
1812      * lower-band in this case, multiply by 2 to get the total number
1813      * of samples. */
1814     *frameLength <<= 1;
1815   }
1816   return 0;
1817 }
1818
1819
1820 /*******************************************************************************
1821  * WebRtcIsac_GetNewFrameLen(...)
1822  *
1823  * This function returns the frame length (in samples) of the next packet.
1824  * In the case of channel-adaptive mode, iSAC decides on its frame length based
1825  * on the estimated bottleneck, this AOI allows a user to prepare for the next
1826  * packet (at the encoder).
1827  *
1828  * The primary usage is in CE to make the iSAC works in channel-adaptive mode
1829  *
1830  * Input:
1831  *        - ISAC_main_inst     : iSAC struct
1832  *
1833  * Return Value                : frame lenght in samples
1834  *
1835  */
1836 int16_t WebRtcIsac_GetNewFrameLen(ISACStruct* ISAC_main_inst) {
1837   ISACMainStruct* instISAC = (ISACMainStruct*)ISAC_main_inst;
1838
1839   /* Return new frame length. */
1840   if (instISAC->in_sample_rate_hz == 16000)
1841     return (instISAC->instLB.ISACencLB_obj.new_framelength);
1842   else if (instISAC->in_sample_rate_hz == 32000)
1843     return ((instISAC->instLB.ISACencLB_obj.new_framelength) * 2);
1844   else
1845     return ((instISAC->instLB.ISACencLB_obj.new_framelength) * 3);
1846 }
1847
1848
1849 /****************************************************************************
1850  * WebRtcIsac_GetErrorCode(...)
1851  *
1852  * This function can be used to check the error code of an iSAC instance.
1853  * When a function returns -1 an error code will be set for that instance.
1854  * The function below extracts the code of the last error that occurred in
1855  * the specified instance.
1856  *
1857  * Input:
1858  *        - ISAC_main_inst    : ISAC instance
1859  *
1860  * Return value               : Error code
1861  */
1862 int16_t WebRtcIsac_GetErrorCode(ISACStruct* ISAC_main_inst) {
1863  return ((ISACMainStruct*)ISAC_main_inst)->errorCode;
1864 }
1865
1866
1867 /****************************************************************************
1868  * WebRtcIsac_GetUplinkBw(...)
1869  *
1870  * This function outputs the target bottleneck of the codec. In
1871  * channel-adaptive mode, the target bottleneck is specified through an in-band
1872  * signalling retrieved by bandwidth estimator.
1873  * In channel-independent, also called instantaneous mode, the target
1874  * bottleneck is provided to the encoder by calling xxx_control(...) (if
1875  * xxx_control is never called, the default values are used.).
1876  * Note that the output is the iSAC internal operating bottleneck which might
1877  * differ slightly from the one provided through xxx_control().
1878  *
1879  * Input:
1880  *        - ISAC_main_inst    : iSAC instance
1881  *
1882  * Output:
1883  *        - *bottleneck       : bottleneck in bits/sec
1884  *
1885  * Return value               : -1 if error happens
1886  *                               0 bit-rates computed correctly.
1887  */
1888 int16_t WebRtcIsac_GetUplinkBw(ISACStruct*  ISAC_main_inst,
1889                                int32_t* bottleneck) {
1890   ISACMainStruct* instISAC = (ISACMainStruct*)ISAC_main_inst;
1891
1892   if (instISAC->codingMode == 0) {
1893     /* We are in adaptive mode then get the bottleneck from BWE. */
1894     *bottleneck = (int32_t)instISAC->bwestimator_obj.send_bw_avg;
1895   } else {
1896     *bottleneck = instISAC->bottleneck;
1897   }
1898
1899   if ((*bottleneck > 32000) && (*bottleneck < 38000)) {
1900     *bottleneck = 32000;
1901   } else if ((*bottleneck > 45000) && (*bottleneck < 50000)) {
1902     *bottleneck = 45000;
1903   } else if (*bottleneck > 56000) {
1904     *bottleneck = 56000;
1905   }
1906   return 0;
1907 }
1908
1909
1910 /******************************************************************************
1911  * WebRtcIsac_SetMaxPayloadSize(...)
1912  *
1913  * This function sets a limit for the maximum payload size of iSAC. The same
1914  * value is used both for 30 and 60 ms packets. If the encoder sampling rate
1915  * is 16 kHz the maximum payload size is between 120 and 400 bytes. If the
1916  * encoder sampling rate is 32 kHz the maximum payload size is between 120
1917  * and 600 bytes.
1918  *
1919  * ---------------
1920  * IMPORTANT NOTES
1921  * ---------------
1922  * The size of a packet is limited to the minimum of 'max-payload-size' and
1923  * 'max-rate.' For instance, let's assume the max-payload-size is set to
1924  * 170 bytes, and max-rate is set to 40 kbps. Note that a limit of 40 kbps
1925  * translates to 150 bytes for 30ms frame-size & 300 bytes for 60ms
1926  * frame-size. Then a packet with a frame-size of 30 ms is limited to 150,
1927  * i.e. min(170, 150), and a packet with 60 ms frame-size is limited to
1928  * 170 bytes, i.e. min(170, 300).
1929  *
1930  * Input:
1931  *        - ISAC_main_inst    : iSAC instance
1932  *        - maxPayloadBytes   : maximum size of the payload in bytes
1933  *                              valid values are between 100 and 400 bytes
1934  *                              if encoder sampling rate is 16 kHz. For
1935  *                              32 kHz encoder sampling rate valid values
1936  *                              are between 100 and 600 bytes.
1937  *
1938  * Return value               : 0 if successful
1939  *                             -1 if error happens
1940  */
1941 int16_t WebRtcIsac_SetMaxPayloadSize(ISACStruct* ISAC_main_inst,
1942                                      int16_t maxPayloadBytes) {
1943   ISACMainStruct* instISAC = (ISACMainStruct*)ISAC_main_inst;
1944   int16_t status = 0;
1945
1946   /* Check if encoder initiated */
1947   if ((instISAC->initFlag & BIT_MASK_ENC_INIT) !=
1948       BIT_MASK_ENC_INIT) {
1949     instISAC->errorCode = ISAC_ENCODER_NOT_INITIATED;
1950     return -1;
1951   }
1952
1953   if (instISAC->encoderSamplingRateKHz == kIsacSuperWideband) {
1954     /* Sanity check. */
1955     if (maxPayloadBytes < 120) {
1956       /* 'maxRate' is out of valid range
1957        * set to the acceptable value and return -1. */
1958       maxPayloadBytes = 120;
1959       status = -1;
1960     }
1961
1962     /* sanity check */
1963     if (maxPayloadBytes > STREAM_SIZE_MAX) {
1964       /* maxRate is out of valid range,
1965        * set to the acceptable value and return -1. */
1966       maxPayloadBytes = STREAM_SIZE_MAX;
1967       status = -1;
1968     }
1969   } else {
1970     if (maxPayloadBytes < 120) {
1971       /* Max payload-size is out of valid range
1972        * set to the acceptable value and return -1. */
1973       maxPayloadBytes = 120;
1974       status = -1;
1975     }
1976     if (maxPayloadBytes > STREAM_SIZE_MAX_60) {
1977       /* Max payload-size is out of valid range
1978        * set to the acceptable value and return -1. */
1979       maxPayloadBytes = STREAM_SIZE_MAX_60;
1980       status = -1;
1981     }
1982   }
1983   instISAC->maxPayloadSizeBytes = maxPayloadBytes;
1984   UpdatePayloadSizeLimit(instISAC);
1985   return status;
1986 }
1987
1988
1989 /******************************************************************************
1990  * WebRtcIsac_SetMaxRate(...)
1991  *
1992  * This function sets the maximum rate which the codec may not exceed for
1993  * any signal packet. The maximum rate is defined and payload-size per
1994  * frame-size in bits per second.
1995  *
1996  * The codec has a maximum rate of 53400 bits per second (200 bytes per 30
1997  * ms) if the encoder sampling rate is 16kHz, and 160 kbps (600 bytes/30 ms)
1998  * if the encoder sampling rate is 32 kHz.
1999  *
2000  * It is possible to set a maximum rate between 32000 and 53400 bits/sec
2001  * in wideband mode, and 32000 to 160000 bits/sec in super-wideband mode.
2002  *
2003  * ---------------
2004  * IMPORTANT NOTES
2005  * ---------------
2006  * The size of a packet is limited to the minimum of 'max-payload-size' and
2007  * 'max-rate.' For instance, let's assume the max-payload-size is set to
2008  * 170 bytes, and max-rate is set to 40 kbps. Note that a limit of 40 kbps
2009  * translates to 150 bytes for 30ms frame-size & 300 bytes for 60ms
2010  * frame-size. Then a packet with a frame-size of 30 ms is limited to 150,
2011  * i.e. min(170, 150), and a packet with 60 ms frame-size is limited to
2012  * 170 bytes, min(170, 300).
2013  *
2014  * Input:
2015  *        - ISAC_main_inst    : iSAC instance
2016  *        - maxRate           : maximum rate in bits per second,
2017  *                              valid values are 32000 to 53400 bits/sec in
2018  *                              wideband mode, and 32000 to 160000 bits/sec in
2019  *                              super-wideband mode.
2020  *
2021  * Return value               : 0 if successful
2022  *                             -1 if error happens
2023  */
2024 int16_t WebRtcIsac_SetMaxRate(ISACStruct* ISAC_main_inst,
2025                               int32_t maxRate) {
2026   ISACMainStruct* instISAC = (ISACMainStruct*)ISAC_main_inst;
2027   int16_t maxRateInBytesPer30Ms;
2028   int16_t status = 0;
2029
2030   /* check if encoder initiated */
2031   if ((instISAC->initFlag & BIT_MASK_ENC_INIT) != BIT_MASK_ENC_INIT) {
2032     instISAC->errorCode = ISAC_ENCODER_NOT_INITIATED;
2033     return -1;
2034   }
2035   /* Calculate maximum number of bytes per 30 msec packets for the
2036      given maximum rate. Multiply with 30/1000 to get number of
2037      bits per 30 ms, divide by 8 to get number of bytes per 30 ms:
2038      maxRateInBytes = floor((maxRate * 30/1000) / 8); */
2039   maxRateInBytesPer30Ms = (int16_t)(maxRate * 3 / 800);
2040
2041   if (instISAC->encoderSamplingRateKHz == kIsacWideband) {
2042     if (maxRate < 32000) {
2043       /* 'maxRate' is out of valid range.
2044        * Set to the acceptable value and return -1. */
2045       maxRateInBytesPer30Ms = 120;
2046       status = -1;
2047     }
2048
2049     if (maxRate > 53400) {
2050       /* 'maxRate' is out of valid range.
2051        * Set to the acceptable value and return -1. */
2052       maxRateInBytesPer30Ms = 200;
2053       status = -1;
2054     }
2055   } else {
2056     if (maxRateInBytesPer30Ms < 120) {
2057       /* 'maxRate' is out of valid range
2058        * Set to the acceptable value and return -1. */
2059       maxRateInBytesPer30Ms = 120;
2060       status = -1;
2061     }
2062
2063     if (maxRateInBytesPer30Ms > STREAM_SIZE_MAX) {
2064       /* 'maxRate' is out of valid range.
2065        * Set to the acceptable value and return -1. */
2066       maxRateInBytesPer30Ms = STREAM_SIZE_MAX;
2067       status = -1;
2068     }
2069   }
2070   instISAC->maxRateBytesPer30Ms = maxRateInBytesPer30Ms;
2071   UpdatePayloadSizeLimit(instISAC);
2072   return status;
2073 }
2074
2075
2076 /****************************************************************************
2077  * WebRtcIsac_GetRedPayload(...)
2078  *
2079  * This function populates "encoded" with the redundant payload of the recently
2080  * encodedframe. This function has to be called once that WebRtcIsac_Encode(...)
2081  * returns a positive value. Regardless of the frame-size this function will
2082  * be called only once after encoding is completed. The bit-stream is
2083  * targeted for 16000 bit/sec.
2084  *
2085  * Input:
2086  *        - ISAC_main_inst    : iSAC struct
2087  *
2088  * Output:
2089  *        - encoded           : the encoded data vector
2090  *
2091  *
2092  * Return value               : >0 - Length (in bytes) of coded data
2093  *                            : -1 - Error
2094  */
2095 int16_t WebRtcIsac_GetRedPayload(ISACStruct* ISAC_main_inst,
2096                                  uint8_t* encoded) {
2097   Bitstr iSACBitStreamInst;
2098   int16_t streamLenLB;
2099   int16_t streamLenUB;
2100   int16_t streamLen;
2101   int16_t totalLenUB;
2102   ISACMainStruct* instISAC = (ISACMainStruct*)ISAC_main_inst;
2103 #ifndef WEBRTC_ARCH_BIG_ENDIAN
2104   int k;
2105 #endif
2106
2107   if ((instISAC->initFlag & BIT_MASK_ENC_INIT) !=
2108       BIT_MASK_ENC_INIT) {
2109     instISAC->errorCode = ISAC_ENCODER_NOT_INITIATED;
2110   }
2111
2112   WebRtcIsac_ResetBitstream(&(iSACBitStreamInst));
2113
2114   streamLenLB = WebRtcIsac_EncodeStoredDataLb(
2115                   &instISAC->instLB.ISACencLB_obj.SaveEnc_obj,
2116                   &iSACBitStreamInst,
2117                   instISAC->instLB.ISACencLB_obj.lastBWIdx,
2118                   RCU_TRANSCODING_SCALE);
2119   if (streamLenLB < 0) {
2120     return -1;
2121   }
2122
2123   /* convert from bytes to int16_t. */
2124   memcpy(encoded, iSACBitStreamInst.stream, streamLenLB);
2125   streamLen = streamLenLB;
2126   if (instISAC->bandwidthKHz == isac8kHz) {
2127     return streamLenLB;
2128   }
2129
2130   streamLenUB = WebRtcIsac_GetRedPayloadUb(
2131                   &instISAC->instUB.ISACencUB_obj.SaveEnc_obj,
2132                   &iSACBitStreamInst, instISAC->bandwidthKHz);
2133   if (streamLenUB < 0) {
2134     /* An error has happened but this is not the error due to a
2135      * bit-stream larger than the limit. */
2136     return -1;
2137   }
2138
2139   /* We have one byte to write the total length of the upper-band.
2140    * The length includes the bit-stream length, check-sum and the
2141    * single byte where the length is written to. This is according to
2142    * iSAC wideband and how the "garbage" is dealt. */
2143   totalLenUB = streamLenUB + 1 + LEN_CHECK_SUM_WORD8;
2144   if (totalLenUB > 255) {
2145     streamLenUB = 0;
2146   }
2147
2148   /* Generate CRC if required. */
2149   if ((instISAC->bandwidthKHz != isac8kHz) &&
2150       (streamLenUB > 0)) {
2151     uint32_t crc;
2152     streamLen += totalLenUB;
2153     encoded[streamLenLB] = (uint8_t)totalLenUB;
2154     memcpy(&encoded[streamLenLB + 1], iSACBitStreamInst.stream,
2155            streamLenUB);
2156
2157     WebRtcIsac_GetCrc((int16_t*)(&(encoded[streamLenLB + 1])),
2158                       streamLenUB, &crc);
2159 #ifndef WEBRTC_ARCH_BIG_ENDIAN
2160     for (k = 0; k < LEN_CHECK_SUM_WORD8; k++) {
2161       encoded[streamLen - LEN_CHECK_SUM_WORD8 + k] =
2162         (uint8_t)((crc >> (24 - k * 8)) & 0xFF);
2163     }
2164 #else
2165     memcpy(&encoded[streamLenLB + streamLenUB + 1], &crc,
2166            LEN_CHECK_SUM_WORD8);
2167 #endif
2168   }
2169   return streamLen;
2170 }
2171
2172
2173 /****************************************************************************
2174  * WebRtcIsac_version(...)
2175  *
2176  * This function returns the version number.
2177  *
2178  * Output:
2179  *        - version      : Pointer to character string
2180  *
2181  */
2182 void WebRtcIsac_version(char* version) {
2183   strcpy(version, "4.3.0");
2184 }
2185
2186
2187 /******************************************************************************
2188  * WebRtcIsac_SetEncSampRate()
2189  * This function sets the sampling rate of the encoder. Initialization of the
2190  * encoder WILL NOT overwrite the sampling rate of the encoder. The default
2191  * value is 16 kHz which is set when the instance is created. The encoding-mode
2192  * and the bottleneck remain unchanged by this call, however, the maximum rate
2193  * and maximum payload-size will be reset to their default values.
2194  *
2195  * NOTE:
2196  * The maximum internal sampling rate is 32 kHz. If the encoder sample rate is
2197  * set to 48 kHz the input is expected to be at 48 kHz but will be resampled to
2198  * 32 kHz before any further processing.
2199  * This mode is created for compatibility with full-band codecs if iSAC is used
2200  * in dual-streaming. See SetDecSampleRate() for sampling rates at the decoder.
2201  *
2202  * Input:
2203  *        - ISAC_main_inst    : iSAC instance
2204  *        - sample_rate_hz    : sampling rate in Hertz, valid values are 16000,
2205  *                              32000 and 48000.
2206  *
2207  * Return value               : 0 if successful
2208  *                             -1 if failed.
2209  */
2210 int16_t WebRtcIsac_SetEncSampRate(ISACStruct* ISAC_main_inst,
2211                                   uint16_t sample_rate_hz) {
2212   ISACMainStruct* instISAC = (ISACMainStruct*)ISAC_main_inst;
2213   enum IsacSamplingRate encoder_operational_rate;
2214
2215   if ((sample_rate_hz != 16000) && (sample_rate_hz != 32000) &&
2216       (sample_rate_hz != 48000)) {
2217     /* Sampling Frequency is not supported. */
2218     instISAC->errorCode = ISAC_UNSUPPORTED_SAMPLING_FREQUENCY;
2219     return -1;
2220   }
2221   if (sample_rate_hz == 16000) {
2222     encoder_operational_rate = kIsacWideband;
2223   } else {
2224     encoder_operational_rate = kIsacSuperWideband;
2225   }
2226
2227   if ((instISAC->initFlag & BIT_MASK_ENC_INIT) !=
2228       BIT_MASK_ENC_INIT) {
2229     if (encoder_operational_rate == kIsacWideband) {
2230       instISAC->bandwidthKHz = isac8kHz;
2231     } else {
2232       instISAC->bandwidthKHz = isac16kHz;
2233     }
2234   } else {
2235     ISACUBStruct* instUB = &(instISAC->instUB);
2236     ISACLBStruct* instLB = &(instISAC->instLB);
2237     int32_t bottleneck = instISAC->bottleneck;
2238     int16_t codingMode = instISAC->codingMode;
2239     int16_t frameSizeMs = instLB->ISACencLB_obj.new_framelength /
2240         (FS / 1000);
2241
2242     if ((encoder_operational_rate == kIsacWideband) &&
2243         (instISAC->encoderSamplingRateKHz == kIsacSuperWideband)) {
2244       /* Changing from super-wideband to wideband.
2245        * we don't need to re-initialize the encoder of the lower-band. */
2246       instISAC->bandwidthKHz = isac8kHz;
2247       if (codingMode == 1) {
2248         ControlLb(instLB,
2249                   (bottleneck > 32000) ? 32000 : bottleneck, FRAMESIZE);
2250       }
2251       instISAC->maxPayloadSizeBytes = STREAM_SIZE_MAX_60;
2252       instISAC->maxRateBytesPer30Ms = STREAM_SIZE_MAX_30;
2253     } else if ((encoder_operational_rate == kIsacSuperWideband) &&
2254                (instISAC->encoderSamplingRateKHz == kIsacWideband)) {
2255       double bottleneckLB = 0;
2256       double bottleneckUB = 0;
2257       if (codingMode == 1) {
2258         WebRtcIsac_RateAllocation(bottleneck, &bottleneckLB, &bottleneckUB,
2259                                   &(instISAC->bandwidthKHz));
2260       }
2261
2262       instISAC->bandwidthKHz = isac16kHz;
2263       instISAC->maxPayloadSizeBytes = STREAM_SIZE_MAX;
2264       instISAC->maxRateBytesPer30Ms = STREAM_SIZE_MAX;
2265
2266       EncoderInitLb(instLB, codingMode, encoder_operational_rate);
2267       EncoderInitUb(instUB, instISAC->bandwidthKHz);
2268
2269       memset(instISAC->analysisFBState1, 0,
2270              FB_STATE_SIZE_WORD32 * sizeof(int32_t));
2271       memset(instISAC->analysisFBState2, 0,
2272              FB_STATE_SIZE_WORD32 * sizeof(int32_t));
2273
2274       if (codingMode == 1) {
2275         instISAC->bottleneck = bottleneck;
2276         ControlLb(instLB, bottleneckLB,
2277                   (instISAC->bandwidthKHz == isac8kHz) ? frameSizeMs:FRAMESIZE);
2278         if (instISAC->bandwidthKHz > isac8kHz) {
2279           ControlUb(instUB, bottleneckUB);
2280         }
2281       } else {
2282         instLB->ISACencLB_obj.enforceFrameSize = 0;
2283         instLB->ISACencLB_obj.new_framelength = FRAMESAMPLES;
2284       }
2285     }
2286   }
2287   instISAC->encoderSamplingRateKHz = encoder_operational_rate;
2288   instISAC->in_sample_rate_hz = sample_rate_hz;
2289   return 0;
2290 }
2291
2292
2293 /******************************************************************************
2294  * WebRtcIsac_SetDecSampRate()
2295  * This function sets the sampling rate of the decoder. Initialization of the
2296  * decoder WILL NOT overwrite the sampling rate of the encoder. The default
2297  * value is 16 kHz which is set when the instance is created.
2298  *
2299  * Input:
2300  *        - ISAC_main_inst    : iSAC instance
2301  *        - sample_rate_hz    : sampling rate in Hertz, valid values are 16000
2302  *                              and 32000.
2303  *
2304  * Return value               : 0 if successful
2305  *                             -1 if failed.
2306  */
2307 int16_t WebRtcIsac_SetDecSampRate(ISACStruct* ISAC_main_inst,
2308                                   uint16_t sample_rate_hz) {
2309   ISACMainStruct* instISAC = (ISACMainStruct*)ISAC_main_inst;
2310   enum IsacSamplingRate decoder_operational_rate;
2311
2312   if (sample_rate_hz == 16000) {
2313     decoder_operational_rate = kIsacWideband;
2314   } else if (sample_rate_hz == 32000) {
2315     decoder_operational_rate = kIsacSuperWideband;
2316   } else {
2317     /* Sampling Frequency is not supported. */
2318     instISAC->errorCode = ISAC_UNSUPPORTED_SAMPLING_FREQUENCY;
2319     return -1;
2320   }
2321
2322   if ((instISAC->decoderSamplingRateKHz == kIsacWideband) &&
2323         (decoder_operational_rate == kIsacSuperWideband)) {
2324       /* Switching from wideband to super-wideband at the decoder
2325        * we need to reset the filter-bank and initialize upper-band decoder. */
2326       memset(instISAC->synthesisFBState1, 0,
2327              FB_STATE_SIZE_WORD32 * sizeof(int32_t));
2328       memset(instISAC->synthesisFBState2, 0,
2329              FB_STATE_SIZE_WORD32 * sizeof(int32_t));
2330
2331       if (DecoderInitUb(&(instISAC->instUB)) < 0) {
2332         return -1;
2333       }
2334   }
2335   instISAC->decoderSamplingRateKHz = decoder_operational_rate;
2336   return 0;
2337 }
2338
2339
2340 /******************************************************************************
2341  * WebRtcIsac_EncSampRate()
2342  *
2343  * Input:
2344  *        - ISAC_main_inst    : iSAC instance
2345  *
2346  * Return value               : sampling rate in Hertz. The input to encoder
2347  *                              is expected to be sampled in this rate.
2348  *
2349  */
2350 uint16_t WebRtcIsac_EncSampRate(ISACStruct* ISAC_main_inst) {
2351   ISACMainStruct* instISAC = (ISACMainStruct*)ISAC_main_inst;
2352   return instISAC->in_sample_rate_hz;
2353 }
2354
2355
2356 /******************************************************************************
2357  * WebRtcIsac_DecSampRate()
2358  * Return the sampling rate of the decoded audio.
2359  *
2360  * Input:
2361  *        - ISAC_main_inst    : iSAC instance
2362  *
2363  * Return value               : sampling rate in Hertz. Decoder output is
2364  *                              sampled at this rate.
2365  *
2366  */
2367 uint16_t WebRtcIsac_DecSampRate(ISACStruct* ISAC_main_inst) {
2368   ISACMainStruct* instISAC = (ISACMainStruct*)ISAC_main_inst;
2369   return instISAC->decoderSamplingRateKHz == kIsacWideband ? 16000 : 32000;
2370 }