2 * Copyright (c) 2012 The WebRTC project authors. All Rights Reserved.
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.
14 * This C file contains the functions for the ISAC API
18 #include "webrtc/modules/audio_coding/codecs/isac/main/interface/isac.h"
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"
34 #define BIT_MASK_DEC_INIT 0x0001
35 #define BIT_MASK_ENC_INIT 0x0002
37 #define LEN_CHECK_SUM_WORD8 4
38 #define MAX_NUM_LAYERS 10
41 /****************************************************************************
42 * UpdatePayloadSizeLimit(...)
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.
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.
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));
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. */
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 =
75 instISAC->instLB.ISACencLB_obj.payloadLimitBytes30 =
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;
90 /* Allocate only 20 for upper-band. */
91 instISAC->instLB.ISACencLB_obj.payloadLimitBytes30 =
92 lim30MsPayloadBytes - 20;
94 instISAC->instUB.ISACencUB_obj.maxPayloadSizeBytes =
100 /****************************************************************************
101 * UpdateBottleneck(...)
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.
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)) {
117 WebRtcIsac_GetUplinkBandwidth(&(instISAC->bwestimator_obj),
120 /* Adding hysteresis when increasing signal bandwidth. */
121 if ((instISAC->bandwidthKHz == isac8kHz)
122 && (bottleneck > 37000)
123 && (bottleneck < 41000)) {
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)) {
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;
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),
148 if (bandwidth != isac8kHz) {
149 instISAC->instLB.ISACencLB_obj.new_framelength = 480;
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;
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. */
167 /****************************************************************************
168 * GetSendBandwidthInfo(...)
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.
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.
182 * - instISAC : iSAC instance.
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
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);
207 /****************************************************************************
208 * WebRtcIsac_AssignSize(...)
210 * This function returns the size of the ISAC instance, so that the instance
211 * can be created out side iSAC.
214 * - sizeinbytes : number of bytes needed to allocate for the
217 * Return value : 0 - Ok
220 int16_t WebRtcIsac_AssignSize(int* sizeInBytes) {
221 *sizeInBytes = sizeof(ISACMainStruct) * 2 / sizeof(int16_t);
226 /****************************************************************************
227 * WebRtcIsac_Assign(...)
229 * This function assigns the memory already created to the ISAC instance.
232 * - ISAC_main_inst : address of the pointer to the coder instance.
233 * - instISAC_Addr : the already allocated memory, where we put the
236 * Return value : 0 - Ok
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;
246 /* Assign the address. */
247 *ISAC_main_inst = (ISACStruct*)instISAC_Addr;
249 /* Default is wideband. */
250 instISAC->encoderSamplingRateKHz = kIsacWideband;
251 instISAC->decoderSamplingRateKHz = kIsacWideband;
252 instISAC->bandwidthKHz = isac8kHz;
253 instISAC->in_sample_rate_hz = 16000;
261 /****************************************************************************
262 * WebRtcIsac_Create(...)
264 * This function creates an ISAC instance, which will contain the state
265 * information for one coding/decoding channel.
268 * - ISAC_main_inst : address of the pointer to the coder instance.
270 * Return value : 0 - Ok
273 int16_t WebRtcIsac_Create(ISACStruct** ISAC_main_inst) {
274 ISACMainStruct* instISAC;
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;
297 /****************************************************************************
298 * WebRtcIsac_Free(...)
300 * This function frees the ISAC instance created at the beginning.
303 * - ISAC_main_inst : a ISAC instance.
305 * Return value : 0 - Ok
308 int16_t WebRtcIsac_Free(ISACStruct* ISAC_main_inst) {
309 ISACMainStruct* instISAC = (ISACMainStruct*)ISAC_main_inst;
315 /****************************************************************************
316 * EncoderInitLb(...) - internal function for initialization of
318 * EncoderInitUb(...) - internal function for initialization of
320 * WebRtcIsac_EncoderInit(...) - API function
322 * This function initializes a ISAC instance prior to the encoder calls.
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
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.
334 * Return value : 0 - Ok
337 static int16_t EncoderInitLb(ISACLBStruct* instLB,
339 enum IsacSamplingRate sampRate) {
340 int16_t statusInit = 0;
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;
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;
353 instLB->ISACencLB_obj.new_framelength = INITIAL_FRAMESAMPLES;
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);
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;
379 static int16_t EncoderInitUb(ISACUBStruct* instUB,
381 int16_t statusInit = 0;
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;
389 WebRtcIsac_InitMasking(&instUB->ISACencUB_obj.maskfiltstr_obj);
390 WebRtcIsac_InitPreFilterbank(&instUB->ISACencUB_obj.prefiltbankstr_obj);
392 if (bandwidth == isac16kHz) {
393 instUB->ISACencUB_obj.buffer_index = LB_TOTAL_DELAY_SAMPLES;
395 instUB->ISACencUB_obj.buffer_index = 0;
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));
407 memcpy(&(instUB->ISACencUB_obj.lastLPCVec),
408 WebRtcIsac_kMeanLarUb16, sizeof(double) * UB_LPC_ORDER);
414 int16_t WebRtcIsac_EncoderInit(ISACStruct* ISAC_main_inst,
415 int16_t codingMode) {
416 ISACMainStruct* instISAC = (ISACMainStruct*)ISAC_main_inst;
419 if ((codingMode != 0) && (codingMode != 1)) {
420 instISAC->errorCode = ISAC_DISALLOWED_CODING_MODE;
423 /* Default bottleneck. */
424 instISAC->bottleneck = MAX_ISAC_BW;
426 if (instISAC->encoderSamplingRateKHz == kIsacWideband) {
427 instISAC->bandwidthKHz = isac8kHz;
428 instISAC->maxPayloadSizeBytes = STREAM_SIZE_MAX_60;
429 instISAC->maxRateBytesPer30Ms = STREAM_SIZE_MAX_30;
431 instISAC->bandwidthKHz = isac16kHz;
432 instISAC->maxPayloadSizeBytes = STREAM_SIZE_MAX;
433 instISAC->maxRateBytesPer30Ms = STREAM_SIZE_MAX;
436 /* Channel-adaptive = 0; Instantaneous (Channel-independent) = 1. */
437 instISAC->codingMode = codingMode;
439 WebRtcIsac_InitBandwidthEstimator(&instISAC->bwestimator_obj,
440 instISAC->encoderSamplingRateKHz,
441 instISAC->decoderSamplingRateKHz);
443 WebRtcIsac_InitRateModel(&instISAC->rate_data_obj);
444 /* Default for I-mode. */
445 instISAC->MaxDelay = 10.0;
447 status = EncoderInitLb(&instISAC->instLB, codingMode,
448 instISAC->encoderSamplingRateKHz);
450 instISAC->errorCode = -status;
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));
461 status = EncoderInitUb(&(instISAC->instUB),
462 instISAC->bandwidthKHz);
464 instISAC->errorCode = -status;
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;
475 /****************************************************************************
476 * WebRtcIsac_Encode(...)
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.
484 * - ISAC_main_inst : ISAC instance.
485 * - speechIn : input speech vector.
488 * - encoded : the encoded data vector
491 * : >0 - Length (in bytes) of coded data
492 * : 0 - The buffer didn't reach the chosen
493 * frameSize so it keeps buffering speech
497 int16_t WebRtcIsac_Encode(ISACStruct* ISAC_main_inst,
498 const int16_t* speechIn,
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;
508 int32_t bottleneck = 0;
509 int16_t bottleneckIdx = 0;
510 int16_t jitterInfo = 0;
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];
518 /* Check if encoder initiated. */
519 if ((instISAC->initFlag & BIT_MASK_ENC_INIT) !=
521 instISAC->errorCode = ISAC_ENCODER_NOT_INITIATED;
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];
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];
542 for (k = 0; k < kNumInputSamples; k++) {
543 buffer32[SIZE_RESAMPLER_STATE + k] = speechIn[k];
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,
551 speech_in_ptr = resampled_buff;
554 if (instISAC->encoderSamplingRateKHz == kIsacSuperWideband) {
555 WebRtcSpl_AnalysisQMF(speech_in_ptr, SWBFRAMESAMPLES_10ms, speechInLB,
556 speechInUB, instISAC->analysisFBState1,
557 instISAC->analysisFBState2);
559 /* Convert from fixed to floating point. */
560 for (k = 0; k < FRAMESAMPLES_10ms; k++) {
561 inFrame[k] = (float)speechInLB[k];
564 for (k = 0; k < FRAMESAMPLES_10ms; k++) {
565 inFrame[k] = (float) speechIn[k];
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;
575 /* This function will update the bottleneck if required. */
576 UpdateBottleneck(instISAC);
578 /* Get the bandwith information which has to be sent to the other side. */
579 GetSendBandwidthInfo(instISAC, &bottleneckIdx, &jitterInfo);
581 /* Encode lower-band. */
582 streamLenLB = WebRtcIsac_EncodeLb(inFrame, &instLB->ISACencLB_obj,
583 instISAC->codingMode, bottleneckIdx);
584 if (streamLenLB < 0) {
588 if (instISAC->encoderSamplingRateKHz == kIsacSuperWideband) {
589 instUB = &(instISAC->instUB);
591 /* Convert to float. */
592 for (k = 0; k < FRAMESAMPLES_10ms; k++) {
593 inFrame[k] = (float) speechInUB[k];
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;
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 +
606 /* Encode upper-band. */
607 switch (instISAC->bandwidthKHz) {
609 streamLenUB = WebRtcIsac_EncodeUb12(inFrame, &instUB->ISACencUB_obj,
614 streamLenUB = WebRtcIsac_EncodeUb16(inFrame, &instUB->ISACencUB_obj,
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. */
630 if (streamLenLB == 0) {
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
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. */
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,
652 streamLen += encoded[streamLenLB];
654 encoded[streamLenLB] = 0;
657 if (streamLenLB == 0) {
660 memcpy(encoded, instLB->ISACencLB_obj.bitstr_obj.stream, streamLenLB);
662 streamLen = streamLenLB;
665 /* Add Garbage if required. */
666 WebRtcIsac_GetUplinkBandwidth(&instISAC->bwestimator_obj, &bottleneck);
667 if (instISAC->codingMode == 0) {
672 instISAC->MaxDelay = (double)WebRtcIsac_GetUplinkMaxDelay(
673 &instISAC->bwestimator_obj);
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);
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;
686 limit = instLB->ISACencLB_obj.payloadLimitBytes60;
689 limit = instUB->ISACencUB_obj.maxPayloadSizeBytes;
691 minBytes = (minBytes > limit) ? limit : minBytes;
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;
701 ptrGarbage = &encoded[streamLenLB + 1 + streamLenUB];
702 limit = streamLen + (255 - encoded[streamLenLB]);
704 minBytes = (minBytes > limit) ? limit : minBytes;
706 garbageLen = (minBytes > streamLen) ? (minBytes - streamLen) : 0;
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);
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;
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;
727 streamLen += garbageLen;
730 /* update rate model */
731 WebRtcIsac_UpdateRateModel(
732 &instISAC->rate_data_obj, streamLen,
733 instISAC->instLB.ISACencLB_obj.current_framesamples, bottleneck);
737 /* Generate CRC if required. */
738 if ((instISAC->bandwidthKHz != isac8kHz) && (streamLenUB > 0)) {
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);
748 memcpy(&encoded[streamLenLB + streamLenUB + 1], &crc, LEN_CHECK_SUM_WORD8);
755 /******************************************************************************
756 * WebRtcIsac_GetNewBitStream(...)
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.
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.
769 * - ISAC_main_inst : ISAC instance.
770 * - bweIndex : Index of bandwidth estimate to put in new
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.
777 * - encoded : The encoded data vector
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
785 int16_t WebRtcIsac_GetNewBitStream(ISACStruct* ISAC_main_inst,
791 Bitstr iSACBitStreamInst; /* Local struct for bitstream handling */
794 int16_t totalStreamLen;
798 enum ISACBandwidth bandwidthKHz;
803 #ifndef WEBRTC_ARCH_BIG_ENDIAN
806 ISACMainStruct* instISAC = (ISACMainStruct*)ISAC_main_inst;
808 if ((instISAC->initFlag & BIT_MASK_ENC_INIT) !=
813 /* Get the bottleneck of this iSAC and limit the
814 * given rate to the current bottleneck. */
815 WebRtcIsac_GetUplinkBw(ISAC_main_inst, ¤tBN);
816 if (rate > currentBN) {
820 if (WebRtcIsac_RateAllocation(rate, &rateLB, &rateUB, &bandwidthKHz) < 0) {
824 /* Cannot transcode from 16 kHz to 12 kHz. */
825 if ((bandwidthKHz == isac12kHz) &&
826 (instISAC->bandwidthKHz == isac16kHz)) {
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);
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;
843 streamLenLB = WebRtcIsac_EncodeStoredDataLb(
844 &instISAC->instLB.ISACencLB_obj.SaveEnc_obj,
845 &iSACBitStreamInst, bweIndex, scale);
847 if (streamLenLB < 0) {
851 /* Convert from bytes to int16_t. */
852 memcpy(encoded, iSACBitStreamInst.stream, streamLenLB);
854 if (bandwidthKHz == isac8kHz) {
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,
867 /* Scale is the ratio of two gains in normal domain. */
868 scale = (float)pow(10, (gain1 - gain2) / 20.0);
870 /* Change the scale if this is a RCU bit-stream. */
871 scale = (isRCU)? (scale * RCU_TRANSCODING_SCALE_UB) : scale;
873 streamLenUB = WebRtcIsac_EncodeStoredDataUb(
874 &(instISAC->instUB.ISACencUB_obj.SaveEnc_obj),
875 &iSACBitStreamInst, jitterInfo, scale,
876 instISAC->bandwidthKHz);
878 if (streamLenUB < 0) {
882 if (streamLenUB + 1 + LEN_CHECK_SUM_WORD8 > 255) {
886 totalStreamLen = streamLenLB + streamLenUB + 1 + LEN_CHECK_SUM_WORD8;
887 encoded[streamLenLB] = streamLenUB + 1 + LEN_CHECK_SUM_WORD8;
889 memcpy(&encoded[streamLenLB + 1], iSACBitStreamInst.stream,
892 WebRtcIsac_GetCrc((int16_t*)(&(encoded[streamLenLB + 1])),
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);
900 memcpy(&encoded[streamLenLB + streamLenUB + 1], &crc,
901 LEN_CHECK_SUM_WORD8);
903 return totalStreamLen;
907 /****************************************************************************
908 * DecoderInitLb(...) - internal function for initialization of
910 * DecoderInitUb(...) - internal function for initialization of
912 * WebRtcIsac_DecoderInit(...) - API function
914 * This function initializes a ISAC instance prior to the decoder calls.
917 * - ISAC_main_inst : ISAC instance.
923 static int16_t DecoderInitLb(ISACLBStruct* instISAC) {
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;
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);
937 static int16_t DecoderInitUb(ISACUBStruct* instISAC) {
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;
944 WebRtcIsac_InitMasking(&instISAC->ISACdecUB_obj.maskfiltstr_obj);
945 WebRtcIsac_InitPostFilterbank(
946 &instISAC->ISACdecUB_obj.postfiltbankstr_obj);
950 int16_t WebRtcIsac_DecoderInit(ISACStruct* ISAC_main_inst) {
951 ISACMainStruct* instISAC = (ISACMainStruct*)ISAC_main_inst;
953 if (DecoderInitLb(&instISAC->instLB) < 0) {
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));
962 if (DecoderInitUb(&(instISAC->instUB)) < 0) {
966 if ((instISAC->initFlag & BIT_MASK_ENC_INIT) != BIT_MASK_ENC_INIT) {
967 WebRtcIsac_InitBandwidthEstimator(&instISAC->bwestimator_obj,
968 instISAC->encoderSamplingRateKHz,
969 instISAC->decoderSamplingRateKHz);
971 instISAC->initFlag |= BIT_MASK_DEC_INIT;
972 instISAC->resetFlag_8kHz = 0;
977 /****************************************************************************
978 * WebRtcIsac_UpdateBwEstimate(...)
980 * This function updates the estimate of the bandwidth.
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.
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)
995 * Return value : 0 - Ok
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,
1004 ISACMainStruct* instISAC = (ISACMainStruct*)ISAC_main_inst;
1006 #ifndef WEBRTC_ARCH_BIG_ENDIAN
1011 /* Check if decoder initiated. */
1012 if ((instISAC->initFlag & BIT_MASK_DEC_INIT) != BIT_MASK_DEC_INIT) {
1013 instISAC->errorCode = ISAC_DECODER_NOT_INITIATED;
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;
1025 WebRtcIsac_ResetBitstream(&(streamdata));
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);
1033 memcpy(streamdata.stream, encoded, 10);
1036 err = WebRtcIsac_EstimateBandwidth(&instISAC->bwestimator_obj, &streamdata,
1037 packet_size, rtp_seq_number, send_ts,
1038 arr_ts, instISAC->encoderSamplingRateKHz,
1039 instISAC->decoderSamplingRateKHz);
1041 /* Return error code if something went wrong. */
1042 instISAC->errorCode = -err;
1048 static int16_t Decode(ISACStruct* ISAC_main_inst,
1049 const uint8_t* encoded,
1050 int16_t lenEncodedBytes,
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;
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;
1069 int16_t totSizeBytes;
1072 ISACMainStruct* instISAC = (ISACMainStruct*)ISAC_main_inst;
1073 ISACUBDecStruct* decInstUB = &(instISAC->instUB.ISACdecUB_obj);
1074 ISACLBDecStruct* decInstLB = &(instISAC->instLB.ISACdecLB_obj);
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;
1083 if (lenEncodedBytes <= 0) {
1084 /* return error code if the packet length is null. */
1085 instISAC->errorCode = ISAC_EMPTY_PACKET;
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;
1095 /* Copy to lower-band bit-stream structure. */
1096 memcpy(instISAC->instLB.ISACdecLB_obj.bitstr_obj.stream, encoded,
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);
1105 if ((numDecodedBytesLB < 0) || (numDecodedBytesLB > lenEncodedLBBytes) ||
1106 (numSamplesLB > MAX_FRAMESAMPLES)) {
1107 instISAC->errorCode = ISAC_LENGTH_MISMATCH;
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. */
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;
1123 totSizeBytes += encoded[totSizeBytes];
1127 if (instISAC->decoderSamplingRateKHz == kIsacWideband) {
1128 for (k = 0; k < numSamplesLB; k++) {
1129 if (outFrame[k] > 32767) {
1131 } else if (outFrame[k] < -32768) {
1132 decoded[k] = -32768;
1134 decoded[k] = (int16_t)WebRtcIsac_lrint(outFrame[k]);
1140 /* We don't accept larger than 30ms (480 samples at lower-band)
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;
1148 outFrameLB[k] = (int16_t)WebRtcIsac_lrint(outFrame[k]);
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);
1158 /* Prepare for the potential increase of signal bandwidth. */
1159 instISAC->resetFlag_8kHz = 2;
1161 /* This includes the checksum and the bytes that stores the length. */
1162 int16_t lenNextStream = encoded[numDecodedBytesLB];
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. */
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);
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]);
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);
1189 /* A valid super-wideband biststream exists. */
1190 enum ISACBandwidth bandwidthKHz;
1191 int32_t maxDelayBit;
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;
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. */
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);
1209 memcpy(decInstUB->bitstr_obj.stream,
1210 &encoded[numDecodedBytesLB + 1], lenNextStream);
1212 /* Reset bit-stream object, this is the first decoding. */
1213 WebRtcIsac_ResetBitstream(&(decInstUB->bitstr_obj));
1215 /* Decode jitter information. */
1216 err = WebRtcIsac_DecodeJitterInfo(&decInstUB->bitstr_obj, &maxDelayBit);
1218 instISAC->errorCode = -err;
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);
1230 instISAC->errorCode = -err;
1235 /* Decode bandwidth information. */
1236 err = WebRtcIsac_DecodeBandwidth(&decInstUB->bitstr_obj,
1239 instISAC->errorCode = -err;
1243 switch (bandwidthKHz) {
1245 numDecodedBytesUB = WebRtcIsac_DecodeUb12(outFrame, decInstUB,
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 *
1256 const float rampStep = 2.0f / MAX_FRAMESAMPLES;
1258 memset(outFrame, 0, (MAX_FRAMESAMPLES >> 1) *
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;
1267 instISAC->resetFlag_8kHz -= 1;
1273 numDecodedBytesUB = WebRtcIsac_DecodeUb16(outFrame, decInstUB,
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;
1289 /* If there is no error Upper-band always decodes
1290 * 30 ms (480 samples). */
1291 numSamplesUB = FRAMESAMPLES;
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;
1300 outFrameUB[k] = (int16_t)WebRtcIsac_lrint(
1308 while (speechIdx < numSamplesLB) {
1309 WebRtcSpl_SynthesisQMF(&outFrameLB[speechIdx], &outFrameUB[speechIdx],
1310 FRAMESAMPLES_10ms, &decoded[(speechIdx << 1)],
1311 instISAC->synthesisFBState1,
1312 instISAC->synthesisFBState2);
1314 speechIdx += FRAMESAMPLES_10ms;
1318 return (numSamplesLB + numSamplesUB);
1327 /****************************************************************************
1328 * WebRtcIsac_Decode(...)
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).
1335 * - ISAC_main_inst : ISAC instance.
1336 * - encoded : encoded ISAC frame(s)
1337 * - len : bytes in encoded vector
1340 * - decoded : The decoded vector
1342 * Return value : >0 - number of samples in decoded vector
1346 int16_t WebRtcIsac_Decode(ISACStruct* ISAC_main_inst,
1347 const uint8_t* encoded,
1348 int16_t lenEncodedBytes,
1350 int16_t* speechType) {
1351 int16_t isRCUPayload = 0;
1352 return Decode(ISAC_main_inst, encoded, lenEncodedBytes, decoded,
1353 speechType, isRCUPayload);
1356 /****************************************************************************
1357 * WebRtcIsac_DecodeRcu(...)
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).
1365 * - ISAC_main_inst : ISAC instance.
1366 * - encoded : encoded ISAC RCU frame(s)
1367 * - len : bytes in encoded vector
1370 * - decoded : The decoded vector
1372 * Return value : >0 - number of samples in decoded vector
1378 int16_t WebRtcIsac_DecodeRcu(ISACStruct* ISAC_main_inst,
1379 const uint8_t* encoded,
1380 int16_t lenEncodedBytes,
1382 int16_t* speechType) {
1383 int16_t isRCUPayload = 1;
1384 return Decode(ISAC_main_inst, encoded, lenEncodedBytes, decoded,
1385 speechType, isRCUPayload);
1389 /****************************************************************************
1390 * WebRtcIsac_DecodePlc(...)
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).
1397 * - ISAC_main_inst : ISAC instance.
1398 * - noOfLostFrames : Number of PLC frames to produce
1401 * - decoded : The decoded vector
1403 * Return value : >0 - number of samples in decoded PLC vector
1406 int16_t WebRtcIsac_DecodePlc(ISACStruct* ISAC_main_inst,
1408 int16_t noOfLostFrames) {
1409 int16_t numSamples = 0;
1410 ISACMainStruct* instISAC = (ISACMainStruct*)ISAC_main_inst;
1412 /* Limit number of frames to two = 60 millisecond.
1413 * Otherwise we exceed data vectors. */
1414 if (noOfLostFrames > 2) {
1418 /* Get the number of samples per frame */
1419 switch (instISAC->decoderSamplingRateKHz) {
1420 case kIsacWideband: {
1421 numSamples = 480 * noOfLostFrames;
1424 case kIsacSuperWideband: {
1425 numSamples = 960 * noOfLostFrames;
1430 /* Set output samples to zero. */
1431 memset(decoded, 0, numSamples * sizeof(int16_t));
1436 /****************************************************************************
1437 * ControlLb(...) - Internal function for controlling Lower Band
1438 * ControlUb(...) - Internal function for controlling Upper Band
1439 * WebRtcIsac_Control(...) - API function
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.
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)
1450 * Return value : 0 - ok
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;
1458 return -ISAC_DISALLOWED_BOTTLENECK;
1461 if ((frameSize == 30) || (frameSize == 60)) {
1462 instISAC->ISACencLB_obj.new_framelength = (FS / 1000) * frameSize;
1464 return -ISAC_DISALLOWED_FRAME_LENGTH;
1470 static int16_t ControlUb(ISACUBStruct* instISAC, double rate) {
1471 if ((rate >= 10000) && (rate <= 32000)) {
1472 instISAC->ISACencUB_obj.bottleneck = rate;
1474 return -ISAC_DISALLOWED_BOTTLENECK;
1479 int16_t WebRtcIsac_Control(ISACStruct* ISAC_main_inst,
1480 int32_t bottleneckBPS,
1481 int16_t frameSize) {
1482 ISACMainStruct* instISAC = (ISACMainStruct*)ISAC_main_inst;
1486 enum ISACBandwidth bandwidthKHz;
1488 if (instISAC->codingMode == 0) {
1489 /* In adaptive mode. */
1490 instISAC->errorCode = ISAC_MODE_MISMATCH;
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;
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;
1508 if (WebRtcIsac_RateAllocation(bottleneckBPS, &rateLB, &rateUB,
1509 &bandwidthKHz) < 0) {
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;
1522 status = ControlLb(&instISAC->instLB, rateLB, frameSize);
1524 instISAC->errorCode = -status;
1527 if (bandwidthKHz != isac8kHz) {
1528 status = ControlUb(&(instISAC->instUB), rateUB);
1530 instISAC->errorCode = -status;
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. */
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));
1544 if (bandwidthKHz == isac12kHz) {
1545 instISAC->instUB.ISACencUB_obj.buffer_index =
1546 instISAC->instLB.ISACencLB_obj.buffer_index;
1548 instISAC->instUB.ISACencUB_obj.buffer_index =
1549 LB_TOTAL_DELAY_SAMPLES + instISAC->instLB.ISACencLB_obj.buffer_index;
1551 memcpy(&(instISAC->instUB.ISACencUB_obj.lastLPCVec),
1552 WebRtcIsac_kMeanLarUb16, sizeof(double) * UB_LPC_ORDER);
1556 /* Update the payload limit if the bandwidth is changing. */
1557 if (instISAC->bandwidthKHz != bandwidthKHz) {
1558 instISAC->bandwidthKHz = bandwidthKHz;
1559 UpdatePayloadSizeLimit(instISAC);
1561 instISAC->bottleneck = bottleneckBPS;
1566 /****************************************************************************
1567 * WebRtcIsac_ControlBwe(...)
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.
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.
1585 * Return value : 0 - ok
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;
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;
1602 /* Check that we are in channel-adaptive mode, otherwise, return (-1) */
1603 if (instISAC->codingMode != 0) {
1604 instISAC->errorCode = ISAC_MODE_MISMATCH;
1607 if ((frameSizeMs != 30) &&
1608 (instISAC->encoderSamplingRateKHz == kIsacSuperWideband)) {
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;
1617 instISAC->instLB.ISACencLB_obj.enforceFrameSize = 0;
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) {
1625 if (WebRtcIsac_RateAllocation(bottleneckBPS, &rateLB, &rateUB,
1629 instISAC->bwestimator_obj.send_bw_avg = (float)bottleneckBPS;
1630 instISAC->bandwidthKHz = bandwidth;
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) *
1640 instISAC->errorCode = ISAC_DISALLOWED_FRAME_LENGTH;
1648 /****************************************************************************
1649 * WebRtcIsac_GetDownLinkBwIndex(...)
1651 * This function returns index representing the Bandwidth estimate from
1652 * the other side to this side.
1655 * - ISAC_main_inst : iSAC structure
1658 * - bweIndex : Bandwidth estimate to transmit to other side.
1661 int16_t WebRtcIsac_GetDownLinkBwIndex(ISACStruct* ISAC_main_inst,
1663 int16_t* jitterInfo) {
1664 ISACMainStruct* instISAC = (ISACMainStruct*)ISAC_main_inst;
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;
1673 /* Call function to get Bandwidth Estimate. */
1674 WebRtcIsac_GetDownlinkBwJitIndexImpl(&(instISAC->bwestimator_obj), bweIndex,
1676 instISAC->decoderSamplingRateKHz);
1681 /****************************************************************************
1682 * WebRtcIsac_UpdateUplinkBw(...)
1684 * This function takes an index representing the Bandwidth estimate from
1685 * this side to other side and updates BWE.
1688 * - ISAC_main_inst : iSAC structure
1689 * - rateIndex : Bandwidth estimate from other side.
1691 * Return value : 0 - ok
1692 * -1 - index out of range
1694 int16_t WebRtcIsac_UpdateUplinkBw(ISACStruct* ISAC_main_inst,
1696 ISACMainStruct* instISAC = (ISACMainStruct*)ISAC_main_inst;
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;
1706 /* Call function to get Bandwidth Estimate. */
1707 returnVal = WebRtcIsac_UpdateUplinkBwImpl(
1708 &(instISAC->bwestimator_obj), bweIndex,
1709 instISAC->encoderSamplingRateKHz);
1711 if (returnVal < 0) {
1712 instISAC->errorCode = -returnVal;
1720 /****************************************************************************
1721 * WebRtcIsac_ReadBwIndex(...)
1723 * This function returns the index of the Bandwidth estimate from the
1727 * - encoded : Encoded bit-stream
1730 * - frameLength : Length of frame in packet (in samples)
1731 * - bweIndex : Bandwidth estimate in bit-stream
1734 int16_t WebRtcIsac_ReadBwIndex(const uint8_t* encoded,
1735 int16_t* bweIndex) {
1737 #ifndef WEBRTC_ARCH_BIG_ENDIAN
1742 WebRtcIsac_ResetBitstream(&(streamdata));
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);
1750 memcpy(streamdata.stream, encoded, 10);
1753 /* Decode frame length. */
1754 err = WebRtcIsac_DecodeFrameLen(&streamdata, bweIndex);
1759 /* Decode BW estimation. */
1760 err = WebRtcIsac_DecodeSendBW(&streamdata, bweIndex);
1769 /****************************************************************************
1770 * WebRtcIsac_ReadFrameLen(...)
1772 * This function returns the number of samples the decoder will generate if
1773 * the given payload is decoded.
1776 * - encoded : Encoded bitstream
1779 * - frameLength : Length of frame in packet (in samples)
1782 int16_t WebRtcIsac_ReadFrameLen(ISACStruct* ISAC_main_inst,
1783 const uint8_t* encoded,
1784 int16_t* frameLength) {
1786 #ifndef WEBRTC_ARCH_BIG_ENDIAN
1790 ISACMainStruct* instISAC;
1792 WebRtcIsac_ResetBitstream(&(streamdata));
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);
1800 memcpy(streamdata.stream, encoded, 10);
1803 /* Decode frame length. */
1804 err = WebRtcIsac_DecodeFrameLen(&streamdata, frameLength);
1808 instISAC = (ISACMainStruct*)ISAC_main_inst;
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
1820 /*******************************************************************************
1821 * WebRtcIsac_GetNewFrameLen(...)
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).
1828 * The primary usage is in CE to make the iSAC works in channel-adaptive mode
1831 * - ISAC_main_inst : iSAC struct
1833 * Return Value : frame lenght in samples
1836 int16_t WebRtcIsac_GetNewFrameLen(ISACStruct* ISAC_main_inst) {
1837 ISACMainStruct* instISAC = (ISACMainStruct*)ISAC_main_inst;
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);
1845 return ((instISAC->instLB.ISACencLB_obj.new_framelength) * 3);
1849 /****************************************************************************
1850 * WebRtcIsac_GetErrorCode(...)
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.
1858 * - ISAC_main_inst : ISAC instance
1860 * Return value : Error code
1862 int16_t WebRtcIsac_GetErrorCode(ISACStruct* ISAC_main_inst) {
1863 return ((ISACMainStruct*)ISAC_main_inst)->errorCode;
1867 /****************************************************************************
1868 * WebRtcIsac_GetUplinkBw(...)
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().
1880 * - ISAC_main_inst : iSAC instance
1883 * - *bottleneck : bottleneck in bits/sec
1885 * Return value : -1 if error happens
1886 * 0 bit-rates computed correctly.
1888 int16_t WebRtcIsac_GetUplinkBw(ISACStruct* ISAC_main_inst,
1889 int32_t* bottleneck) {
1890 ISACMainStruct* instISAC = (ISACMainStruct*)ISAC_main_inst;
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;
1896 *bottleneck = instISAC->bottleneck;
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;
1910 /******************************************************************************
1911 * WebRtcIsac_SetMaxPayloadSize(...)
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
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).
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.
1938 * Return value : 0 if successful
1939 * -1 if error happens
1941 int16_t WebRtcIsac_SetMaxPayloadSize(ISACStruct* ISAC_main_inst,
1942 int16_t maxPayloadBytes) {
1943 ISACMainStruct* instISAC = (ISACMainStruct*)ISAC_main_inst;
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;
1953 if (instISAC->encoderSamplingRateKHz == kIsacSuperWideband) {
1955 if (maxPayloadBytes < 120) {
1956 /* 'maxRate' is out of valid range
1957 * set to the acceptable value and return -1. */
1958 maxPayloadBytes = 120;
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;
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;
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;
1983 instISAC->maxPayloadSizeBytes = maxPayloadBytes;
1984 UpdatePayloadSizeLimit(instISAC);
1989 /******************************************************************************
1990 * WebRtcIsac_SetMaxRate(...)
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.
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.
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.
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).
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.
2021 * Return value : 0 if successful
2022 * -1 if error happens
2024 int16_t WebRtcIsac_SetMaxRate(ISACStruct* ISAC_main_inst,
2026 ISACMainStruct* instISAC = (ISACMainStruct*)ISAC_main_inst;
2027 int16_t maxRateInBytesPer30Ms;
2030 /* check if encoder initiated */
2031 if ((instISAC->initFlag & BIT_MASK_ENC_INIT) != BIT_MASK_ENC_INIT) {
2032 instISAC->errorCode = ISAC_ENCODER_NOT_INITIATED;
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);
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;
2049 if (maxRate > 53400) {
2050 /* 'maxRate' is out of valid range.
2051 * Set to the acceptable value and return -1. */
2052 maxRateInBytesPer30Ms = 200;
2056 if (maxRateInBytesPer30Ms < 120) {
2057 /* 'maxRate' is out of valid range
2058 * Set to the acceptable value and return -1. */
2059 maxRateInBytesPer30Ms = 120;
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;
2070 instISAC->maxRateBytesPer30Ms = maxRateInBytesPer30Ms;
2071 UpdatePayloadSizeLimit(instISAC);
2076 /****************************************************************************
2077 * WebRtcIsac_GetRedPayload(...)
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.
2086 * - ISAC_main_inst : iSAC struct
2089 * - encoded : the encoded data vector
2092 * Return value : >0 - Length (in bytes) of coded data
2095 int16_t WebRtcIsac_GetRedPayload(ISACStruct* ISAC_main_inst,
2097 Bitstr iSACBitStreamInst;
2098 int16_t streamLenLB;
2099 int16_t streamLenUB;
2102 ISACMainStruct* instISAC = (ISACMainStruct*)ISAC_main_inst;
2103 #ifndef WEBRTC_ARCH_BIG_ENDIAN
2107 if ((instISAC->initFlag & BIT_MASK_ENC_INIT) !=
2108 BIT_MASK_ENC_INIT) {
2109 instISAC->errorCode = ISAC_ENCODER_NOT_INITIATED;
2112 WebRtcIsac_ResetBitstream(&(iSACBitStreamInst));
2114 streamLenLB = WebRtcIsac_EncodeStoredDataLb(
2115 &instISAC->instLB.ISACencLB_obj.SaveEnc_obj,
2117 instISAC->instLB.ISACencLB_obj.lastBWIdx,
2118 RCU_TRANSCODING_SCALE);
2119 if (streamLenLB < 0) {
2123 /* convert from bytes to int16_t. */
2124 memcpy(encoded, iSACBitStreamInst.stream, streamLenLB);
2125 streamLen = streamLenLB;
2126 if (instISAC->bandwidthKHz == isac8kHz) {
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. */
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) {
2148 /* Generate CRC if required. */
2149 if ((instISAC->bandwidthKHz != isac8kHz) &&
2150 (streamLenUB > 0)) {
2152 streamLen += totalLenUB;
2153 encoded[streamLenLB] = (uint8_t)totalLenUB;
2154 memcpy(&encoded[streamLenLB + 1], iSACBitStreamInst.stream,
2157 WebRtcIsac_GetCrc((int16_t*)(&(encoded[streamLenLB + 1])),
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);
2165 memcpy(&encoded[streamLenLB + streamLenUB + 1], &crc,
2166 LEN_CHECK_SUM_WORD8);
2173 /****************************************************************************
2174 * WebRtcIsac_version(...)
2176 * This function returns the version number.
2179 * - version : Pointer to character string
2182 void WebRtcIsac_version(char* version) {
2183 strcpy(version, "4.3.0");
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.
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.
2203 * - ISAC_main_inst : iSAC instance
2204 * - sample_rate_hz : sampling rate in Hertz, valid values are 16000,
2207 * Return value : 0 if successful
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;
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;
2221 if (sample_rate_hz == 16000) {
2222 encoder_operational_rate = kIsacWideband;
2224 encoder_operational_rate = kIsacSuperWideband;
2227 if ((instISAC->initFlag & BIT_MASK_ENC_INIT) !=
2228 BIT_MASK_ENC_INIT) {
2229 if (encoder_operational_rate == kIsacWideband) {
2230 instISAC->bandwidthKHz = isac8kHz;
2232 instISAC->bandwidthKHz = isac16kHz;
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 /
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) {
2249 (bottleneck > 32000) ? 32000 : bottleneck, FRAMESIZE);
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));
2262 instISAC->bandwidthKHz = isac16kHz;
2263 instISAC->maxPayloadSizeBytes = STREAM_SIZE_MAX;
2264 instISAC->maxRateBytesPer30Ms = STREAM_SIZE_MAX;
2266 EncoderInitLb(instLB, codingMode, encoder_operational_rate);
2267 EncoderInitUb(instUB, instISAC->bandwidthKHz);
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));
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);
2282 instLB->ISACencLB_obj.enforceFrameSize = 0;
2283 instLB->ISACencLB_obj.new_framelength = FRAMESAMPLES;
2287 instISAC->encoderSamplingRateKHz = encoder_operational_rate;
2288 instISAC->in_sample_rate_hz = sample_rate_hz;
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.
2300 * - ISAC_main_inst : iSAC instance
2301 * - sample_rate_hz : sampling rate in Hertz, valid values are 16000
2304 * Return value : 0 if successful
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;
2312 if (sample_rate_hz == 16000) {
2313 decoder_operational_rate = kIsacWideband;
2314 } else if (sample_rate_hz == 32000) {
2315 decoder_operational_rate = kIsacSuperWideband;
2317 /* Sampling Frequency is not supported. */
2318 instISAC->errorCode = ISAC_UNSUPPORTED_SAMPLING_FREQUENCY;
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));
2331 if (DecoderInitUb(&(instISAC->instUB)) < 0) {
2335 instISAC->decoderSamplingRateKHz = decoder_operational_rate;
2340 /******************************************************************************
2341 * WebRtcIsac_EncSampRate()
2344 * - ISAC_main_inst : iSAC instance
2346 * Return value : sampling rate in Hertz. The input to encoder
2347 * is expected to be sampled in this rate.
2350 uint16_t WebRtcIsac_EncSampRate(ISACStruct* ISAC_main_inst) {
2351 ISACMainStruct* instISAC = (ISACMainStruct*)ISAC_main_inst;
2352 return instISAC->in_sample_rate_hz;
2356 /******************************************************************************
2357 * WebRtcIsac_DecSampRate()
2358 * Return the sampling rate of the decoded audio.
2361 * - ISAC_main_inst : iSAC instance
2363 * Return value : sampling rate in Hertz. Decoder output is
2364 * sampled at this rate.
2367 uint16_t WebRtcIsac_DecSampRate(ISACStruct* ISAC_main_inst) {
2368 ISACMainStruct* instISAC = (ISACMainStruct*)ISAC_main_inst;
2369 return instISAC->decoderSamplingRateKHz == kIsacWideband ? 16000 : 32000;