1 /* ------------------------------------------------------------------
2 * Copyright (C) 1998-2009 PacketVideo
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either
14 * See the License for the specific language governing permissions
15 * and limitations under the License.
16 * -------------------------------------------------------------------
18 /****************************************************************************************
19 Portions of this file are derived from the following 3GPP standard:
22 ANSI-C code for the Adaptive Multi-Rate - Wideband (AMR-WB) speech codec
23 Available from http://www.3gpp.org
25 (C) 2007, 3GPP Organizational Partners (ARIB, ATIS, CCSA, ETSI, TTA, TTC)
26 Permission to distribute, modify and use this file under the standard license
27 terms listed above has been obtained from the copyright holder.
28 ****************************************************************************************/
30 ------------------------------------------------------------------------------
34 Filename: dtx_decoder_amr_wb.cpp
36 ------------------------------------------------------------------------------
37 INPUT AND OUTPUT DEFINITIONS
40 ------------------------------------------------------------------------------
45 ------------------------------------------------------------------------------
49 ------------------------------------------------------------------------------
52 ------------------------------------------------------------------------------
55 ------------------------------------------------------------------------------
59 /*----------------------------------------------------------------------------
61 ----------------------------------------------------------------------------*/
63 #include "pv_amr_wb_type_defs.h"
64 #include "pvamrwbdecoder_basic_op.h"
65 #include "pvamrwb_math_op.h"
66 #include "pvamrwbdecoder_cnst.h"
67 #include "pvamrwbdecoder_acelp.h" /* prototype of functions */
68 #include "get_amr_wb_bits.h"
71 /*----------------------------------------------------------------------------
73 ; Define module specific macros here
74 ----------------------------------------------------------------------------*/
77 /*----------------------------------------------------------------------------
79 ; Include all pre-processor statements here. Include conditional
80 ; compile variables also.
81 ----------------------------------------------------------------------------*/
83 /*----------------------------------------------------------------------------
84 ; LOCAL FUNCTION DEFINITIONS
85 ; Function Prototype declaration
86 ----------------------------------------------------------------------------*/
88 /*----------------------------------------------------------------------------
89 ; LOCAL STORE/BUFFER/POINTER DEFINITIONS
90 ; Variable declaration - defined here and used outside this module
91 ----------------------------------------------------------------------------*/
93 /*----------------------------------------------------------------------------
94 ; EXTERNAL FUNCTION REFERENCES
95 ; Declare functions defined elsewhere and referenced in this module
96 ----------------------------------------------------------------------------*/
98 /*----------------------------------------------------------------------------
99 ; EXTERNAL GLOBAL STORE/BUFFER/POINTER REFERENCES
100 ; Declare variables used in this module but defined elsewhere
101 ----------------------------------------------------------------------------*/
103 /*----------------------------------------------------------------------------
105 ----------------------------------------------------------------------------*/
107 * Function : dtx_dec_amr_wb_reset
109 int16 dtx_dec_amr_wb_reset(dtx_decState * st, const int16 isf_init[])
114 if (st == (dtx_decState *) NULL)
116 /* dtx_dec_amr_wb_reset invalid parameter */
119 st->since_last_sid = 0;
120 st->true_sid_period_inv = (1 << 13); /* 0.25 in Q15 */
123 st->old_log_en = 3500;
124 /* low level noise for better performance in DTX handover cases */
126 st->cng_seed = RANDOM_INITSEED;
130 /* Init isf_hist[] and decoder log frame energy */
131 pv_memcpy((void *)st->isf, (void *)isf_init, M*sizeof(*isf_init));
133 pv_memcpy((void *)st->isf_old, (void *)isf_init, M*sizeof(*isf_init));
135 for (i = 0; i < DTX_HIST_SIZE; i++)
137 pv_memcpy((void *)&st->isf_hist[i * M], (void *)isf_init, M*sizeof(*isf_init));
138 st->log_en_hist[i] = st->log_en;
141 st->dtxHangoverCount = DTX_HANG_CONST;
142 st->decAnaElapsedCount = 32767;
146 st->dtxHangoverAdded = 0;
148 st->dtxGlobalState = SPEECH;
149 st->data_updated = 0;
151 st->dither_seed = RANDOM_INITSEED;
159 Table of new SPD synthesis states
161 | previous SPD_synthesis_state
163 frame_type | SPEECH | DTX | DTX_MUTE
164 ---------------------------------------------------------------
165 RX_SPEECH_GOOD , | | |
166 RX_SPEECH_PR_DEGRADED | SPEECH | SPEECH | SPEECH
167 ----------------------------------------------------------------
168 RX_SPEECH_BAD, | SPEECH | DTX | DTX_MUTE
169 ----------------------------------------------------------------
170 RX_SID_FIRST, | DTX | DTX/(DTX_MUTE)| DTX_MUTE
171 ----------------------------------------------------------------
172 RX_SID_UPDATE, | DTX | DTX | DTX
173 ----------------------------------------------------------------
174 RX_SID_BAD, | DTX | DTX/(DTX_MUTE)| DTX_MUTE
175 ----------------------------------------------------------------
176 RX_NO_DATA, | SPEECH | DTX/(DTX_MUTE)| DTX_MUTE
177 RX_SPARE |(class2 garb.)| |
178 ----------------------------------------------------------------
181 /*----------------------------------------------------------------------------
183 ----------------------------------------------------------------------------*/
186 * Function : dtx_dec_amr_wb
188 int16 dtx_dec_amr_wb(
189 dtx_decState * st, /* i/o : State struct */
190 int16 * exc2, /* o : CN excitation */
191 int16 new_state, /* i : New DTX state */
192 int16 isf[], /* o : CN ISF vector */
202 int32 L_isf[M], L_log_en_int, level32, ener32;
204 int16 tmp_int_length;
205 int16 tmp, exp, exp0, log_en_int_e, log_en_int_m, level;
207 /* This function is called if synthesis state is not SPEECH the globally passed inputs to this function
208 * are st->sid_frame st->valid_data st->dtxHangoverAdded new_state (SPEECH, DTX, DTX_MUTE) */
210 if ((st->dtxHangoverAdded != 0) &&
211 (st->sid_frame != 0))
213 /* sid_first after dtx hangover period */
214 /* or sid_upd after dtxhangover */
216 /* consider twice the last frame */
217 ptr = st->hist_ptr + 1;
219 if (ptr == DTX_HIST_SIZE)
222 pv_memcpy((void *)&st->isf_hist[ptr * M], (void *)&st->isf_hist[st->hist_ptr * M], M*sizeof(*st->isf_hist));
224 st->log_en_hist[ptr] = st->log_en_hist[st->hist_ptr];
226 /* compute mean log energy and isf from decoded signal (SID_FIRST) */
228 for (i = 0; i < M; i++)
233 /* average energy and isf */
234 for (i = 0; i < DTX_HIST_SIZE; i++)
236 /* Division by DTX_HIST_SIZE = 8 has been done in dtx_buffer log_en is in Q10 */
237 st->log_en = add_int16(st->log_en, st->log_en_hist[i]);
239 for (j = 0; j < M; j++)
241 L_isf[j] = add_int32(L_isf[j], (int32)(st->isf_hist[i * M + j]));
245 /* st->log_en in Q9 */
248 /* Add 2 in Q9, in order to have only positive values for Pow2 */
249 /* this value is subtracted back after Pow2 function */
255 for (j = 0; j < M; j++)
257 st->isf[j] = (int16)(L_isf[j] >> 3); /* divide by 8 */
262 if (st->sid_frame != 0)
264 /* Set old SID parameters, always shift */
265 /* even if there is no new valid_data */
267 pv_memcpy((void *)st->isf_old, (void *)st->isf, M*sizeof(*st->isf));
269 st->old_log_en = st->log_en;
271 if (st->valid_data != 0) /* new data available (no CRC) */
273 /* st->true_sid_period_inv = 1.0f/st->since_last_sid; */
274 /* Compute interpolation factor, since the division only works * for values of since_last_sid <
275 * 32 we have to limit the * interpolation to 32 frames */
276 tmp_int_length = st->since_last_sid;
279 if (tmp_int_length > 32)
284 if (tmp_int_length >= 2)
286 st->true_sid_period_inv = div_16by16(1 << 10, shl_int16(tmp_int_length, 10));
290 st->true_sid_period_inv = 1 << 14; /* 0.5 it Q15 */
293 ind[0] = Serial_parm(6, prms);
294 ind[1] = Serial_parm(6, prms);
295 ind[2] = Serial_parm(6, prms);
296 ind[3] = Serial_parm(5, prms);
297 ind[4] = Serial_parm(5, prms);
299 Disf_ns(ind, st->isf);
301 log_en_index = Serial_parm(6, prms);
303 /* read background noise stationarity information */
304 st->CN_dith = Serial_parm_1bit(prms);
306 /* st->log_en = (float)log_en_index / 2.625 - 2.0; */
307 /* log2(E) in Q9 (log2(E) lies in between -2:22) */
308 st->log_en = shl_int16(log_en_index, 15 - 6);
310 /* Divide by 2.625 */
311 st->log_en = mult_int16(st->log_en, 12483);
312 /* Subtract 2 in Q9 is done later, after Pow2 function */
314 /* no interpolation at startup after coder reset */
315 /* or when SID_UPD has been received right after SPEECH */
317 if ((st->data_updated == 0) || (st->dtxGlobalState == SPEECH))
319 pv_memcpy((void *)st->isf_old, (void *)st->isf, M*sizeof(*st->isf));
321 st->old_log_en = st->log_en;
323 } /* endif valid_data */
324 } /* endif sid_frame */
327 if ((st->sid_frame != 0) && (st->valid_data != 0))
329 st->since_last_sid = 0;
331 /* Interpolate SID info */
332 int_fac = shl_int16(st->since_last_sid, 10); /* Q10 */
333 int_fac = mult_int16(int_fac, st->true_sid_period_inv); /* Q10 * Q15 -> Q10 */
335 /* Maximize to 1.0 in Q10 */
341 int_fac = shl_int16(int_fac, 4); /* Q10 -> Q14 */
343 L_log_en_int = mul_16by16_to_int32(int_fac, st->log_en); /* Q14 * Q9 -> Q24 */
345 for (i = 0; i < M; i++)
347 isf[i] = mult_int16(int_fac, st->isf[i]);/* Q14 * Q15 -> Q14 */
350 int_fac = 16384 - int_fac; /* 1-k in Q14 */
352 /* ( Q14 * Q9 -> Q24 ) + Q24 -> Q24 */
353 L_log_en_int = mac_16by16_to_int32(L_log_en_int, int_fac, st->old_log_en);
355 for (i = 0; i < M; i++)
357 /* Q14 + (Q14 * Q15 -> Q14) -> Q14 */
358 isf[i] = add_int16(isf[i], mult_int16(int_fac, st->isf_old[i]));
359 isf[i] = shl_int16(isf[i], 1); /* Q14 -> Q15 */
362 /* If background noise is non-stationary, insert comfort noise dithering */
363 if (st->CN_dith != 0)
365 CN_dithering(isf, &L_log_en_int, &st->dither_seed);
367 /* L_log_en_int corresponds to log2(E)+2 in Q24, i.e log2(gain)+1 in Q25 */
371 /* Find integer part */
372 log_en_int_e = extract_h(L_log_en_int);
374 /* Find fractional part */
375 log_en_int_m = (int16)(sub_int32(L_log_en_int, L_deposit_h(log_en_int_e)) >> 1);
377 /* Subtract 2 from L_log_en_int in Q9, i.e divide the gain by 2 (energy by 4) */
378 /* Add 16 in order to have the result of pow2 in Q16 */
381 /* level = (float)( pow( 2.0f, log_en ) ); */
382 level32 = power_of_2(log_en_int_e, log_en_int_m); /* Q16 */
384 exp0 = normalize_amr_wb(level32);
385 level32 <<= exp0; /* level in Q31 */
387 level = (int16)(level32 >> 16); /* level in Q15 */
389 /* generate white noise vector */
390 for (i = 0; i < L_FRAME; i++)
392 exc2[i] = noise_gen_amrwb(&(st->cng_seed)) >> 4;
395 /* gain = level / sqrt(ener) * sqrt(L_FRAME) */
397 /* energy of generated excitation */
398 ener32 = Dot_product12(exc2, exc2, L_FRAME, &exp);
400 one_ov_sqrt_norm(&ener32, &exp);
402 gain = extract_h(ener32);
404 gain = mult_int16(level, gain); /* gain in Q15 */
408 /* Multiply by sqrt(L_FRAME)=16, i.e. shift left by 4 */
411 for (i = 0; i < L_FRAME; i++)
413 tmp = mult_int16(exc2[i], gain); /* Q0 * Q15 */
414 exc2[i] = shl_int16(tmp, exp);
418 if (new_state == DTX_MUTE)
420 /* mute comfort noise as it has been quite a long time since last SID update was performed */
422 tmp_int_length = st->since_last_sid;
424 if (tmp_int_length > 32)
429 st->true_sid_period_inv = div_16by16(1 << 10, shl_int16(tmp_int_length, 10));
431 st->since_last_sid = 0;
432 st->old_log_en = st->log_en;
433 /* subtract 1/8 in Q9 (energy), i.e -3/8 dB */
436 /* reset interpolation length timer if data has been updated. */
438 if ((st->sid_frame != 0) &&
439 ((st->valid_data != 0) ||
440 ((st->valid_data == 0) && (st->dtxHangoverAdded) != 0)))
442 st->since_last_sid = 0;
443 st->data_updated = 1;
449 /*----------------------------------------------------------------------------
451 ----------------------------------------------------------------------------*/
453 void dtx_dec_amr_wb_activity_update(
461 int16 log_en_e, log_en_m, log_en;
466 if (st->hist_ptr == DTX_HIST_SIZE)
470 pv_memcpy((void *)&st->isf_hist[st->hist_ptr * M], (void *)isf, M*sizeof(*isf));
473 /* compute log energy based on excitation frame energy in Q0 */
475 for (i = 0; i < L_FRAME; i++)
477 L_frame_en = mac_16by16_to_int32(L_frame_en, exc[i], exc[i]);
481 /* log_en = (float)log10(L_frame_en/(float)L_FRAME)/(float)log10(2.0f); */
482 amrwb_log_2(L_frame_en, &log_en_e, &log_en_m);
484 /* convert exponent and mantissa to int16 Q7. Q7 is used to simplify averaging in dtx_enc */
485 log_en = shl_int16(log_en_e, 7); /* Q7 */
486 log_en += log_en_m >> 8;
488 /* Divide by L_FRAME = 256, i.e subtract 8 in Q7 = 1024 */
491 /* insert into log energy buffer */
492 st->log_en_hist[st->hist_ptr] = log_en;
499 Table of new SPD synthesis states
501 | previous SPD_synthesis_state
503 frame_type | SPEECH | DTX | DTX_MUTE
504 ---------------------------------------------------------------
505 RX_SPEECH_GOOD , | | |
506 RX_SPEECH_PR_DEGRADED | SPEECH | SPEECH | SPEECH
507 ----------------------------------------------------------------
508 RX_SPEECH_BAD, | SPEECH | DTX | DTX_MUTE
509 ----------------------------------------------------------------
510 RX_SID_FIRST, | DTX | DTX/(DTX_MUTE)| DTX_MUTE
511 ----------------------------------------------------------------
512 RX_SID_UPDATE, | DTX | DTX | DTX
513 ----------------------------------------------------------------
514 RX_SID_BAD, | DTX | DTX/(DTX_MUTE)| DTX_MUTE
515 ----------------------------------------------------------------
516 RX_NO_DATA, | SPEECH | DTX/(DTX_MUTE)| DTX_MUTE
517 RX_SPARE |(class2 garb.)| |
518 ----------------------------------------------------------------
522 /*----------------------------------------------------------------------------
524 ----------------------------------------------------------------------------*/
526 int16 rx_amr_wb_dtx_handler(
527 dtx_decState * st, /* i/o : State struct */
528 int16 frame_type /* i : Frame type */
534 /* DTX if SID frame or previously in DTX{_MUTE} and (NO_RX OR BAD_SPEECH) */
538 if ((frame_type == RX_SID_FIRST) ||
539 (frame_type == RX_SID_UPDATE) ||
540 (frame_type == RX_SID_BAD) ||
541 (((st->dtxGlobalState == DTX) ||
542 (st->dtxGlobalState == DTX_MUTE)) &&
543 ((frame_type == RX_NO_DATA) ||
544 (frame_type == RX_SPEECH_BAD) ||
545 (frame_type == RX_SPEECH_LOST))))
549 /* stay in mute for these input types */
551 if ((st->dtxGlobalState == DTX_MUTE) &&
552 ((frame_type == RX_SID_BAD) ||
553 (frame_type == RX_SID_FIRST) ||
554 (frame_type == RX_SPEECH_LOST) ||
555 (frame_type == RX_NO_DATA)))
559 /* evaluate if noise parameters are too old */
560 /* since_last_sid is reset when CN parameters have been updated */
561 st->since_last_sid = add_int16(st->since_last_sid, 1);
563 /* no update of sid parameters in DTX for a long while */
565 if (st->since_last_sid > DTX_MAX_EMPTY_THRESH)
573 st->since_last_sid = 0;
576 /* reset the decAnaElapsed Counter when receiving CNI data the first time, to robustify counter missmatch
577 * after handover this might delay the bwd CNI analysis in the new decoder slightly. */
579 if ((st->data_updated == 0) &&
580 (frame_type == RX_SID_UPDATE))
582 st->decAnaElapsedCount = 0;
584 /* update the SPE-SPD DTX hangover synchronization */
585 /* to know when SPE has added dtx hangover */
586 st->decAnaElapsedCount = add_int16(st->decAnaElapsedCount, 1);
587 st->dtxHangoverAdded = 0;
590 if ((frame_type == RX_SID_FIRST) ||
591 (frame_type == RX_SID_UPDATE) ||
592 (frame_type == RX_SID_BAD) ||
593 (frame_type == RX_NO_DATA))
603 if (encState == SPEECH)
605 st->dtxHangoverCount = DTX_HANG_CONST;
610 if (st->decAnaElapsedCount > DTX_ELAPSED_FRAMES_THRESH)
612 st->dtxHangoverAdded = 1;
613 st->decAnaElapsedCount = 0;
614 st->dtxHangoverCount = 0;
616 else if (st->dtxHangoverCount == 0)
618 st->decAnaElapsedCount = 0;
622 st->dtxHangoverCount--;
626 if (newState != SPEECH)
628 /* DTX or DTX_MUTE CN data is not in a first SID, first SIDs are marked as SID_BAD but will do
629 * backwards analysis if a hangover period has been added according to the state machine above */
635 if (frame_type == RX_SID_FIRST)
639 else if (frame_type == RX_SID_UPDATE)
644 else if (frame_type == RX_SID_BAD)
647 st->dtxHangoverAdded = 0; /* use old data */
651 /* newState is used by both SPEECH AND DTX synthesis routines */
655 /*----------------------------------------------------------------------------
657 ----------------------------------------------------------------------------*/
659 void aver_isf_history(
666 int16 isf_tmp[2 * M];
669 /* Memorize in isf_tmp[][] the ISF vectors to be replaced by */
670 /* the median ISF vector prior to the averaging */
671 for (k = 0; k < 2; k++)
674 if (indices[k] + 1 != 0)
676 for (i = 0; i < M; i++)
678 isf_tmp[k * M + i] = isf_old[indices[k] * M + i];
679 isf_old[indices[k] * M + i] = isf_old[indices[2] * M + i];
684 /* Perform the ISF averaging */
685 for (j = 0; j < M; j++)
689 for (i = 0; i < DTX_HIST_SIZE; i++)
691 L_tmp = add_int32(L_tmp, (int32)(isf_old[i * M + j]));
696 /* Retrieve from isf_tmp[][] the ISF vectors saved prior to averaging */
697 for (k = 0; k < 2; k++)
700 if (indices[k] + 1 != 0)
702 for (i = 0; i < M; i++)
704 isf_old[indices[k] * M + i] = isf_tmp[k * M + i];
713 /*----------------------------------------------------------------------------
715 ----------------------------------------------------------------------------*/
717 void find_frame_indices(
723 int32 L_tmp, summin, summax, summax2nd;
727 /* Remove the effect of the oldest frame from the column */
728 /* sum sumD[0..DTX_HIST_SIZE-1]. sumD[DTX_HIST_SIZE] is */
729 /* not updated since it will be removed later. */
731 tmp = DTX_HIST_SIZE_MIN_ONE;
733 for (i = 0; i < DTX_HIST_SIZE_MIN_ONE; i++)
736 st->sumD[i] = sub_int32(st->sumD[i], st->D[j]);
740 /* Shift the column sum sumD. The element sumD[DTX_HIST_SIZE-1] */
741 /* corresponding to the oldest frame is removed. The sum of */
742 /* the distances between the latest isf and other isfs, */
743 /* i.e. the element sumD[0], will be computed during this call. */
744 /* Hence this element is initialized to zero. */
746 for (i = DTX_HIST_SIZE_MIN_ONE; i > 0; i--)
748 st->sumD[i] = st->sumD[i - 1];
752 /* Remove the oldest frame from the distance matrix. */
753 /* Note that the distance matrix is replaced by a one- */
754 /* dimensional array to save static memory. */
757 for (i = 27; i >= 12; i -= tmp)
760 for (j = tmp; j > 0; j--)
762 st->D[i - j + 1] = st->D[i - j - tmp];
766 /* Compute the first column of the distance matrix D */
767 /* (squared Euclidean distances from isf1[] to isf_old_tx[][]). */
770 for (i = 1; i < DTX_HIST_SIZE; i++)
772 /* Compute the distance between the latest isf and the other isfs. */
777 ptr = DTX_HIST_SIZE_MIN_ONE;
780 for (j = 0; j < M; j++)
782 tmp = sub_int16(isf_old_tx[st->hist_ptr * M + j], isf_old_tx[ptr * M + j]);
783 L_tmp = mac_16by16_to_int32(L_tmp, tmp, tmp);
785 st->D[i - 1] = L_tmp;
787 /* Update also the column sums. */
788 st->sumD[0] = add_int32(st->sumD[0], st->D[i - 1]);
789 st->sumD[i] = add_int32(st->sumD[i], st->D[i - 1]);
792 /* Find the minimum and maximum distances */
793 summax = st->sumD[0];
794 summin = st->sumD[0];
797 for (i = 1; i < DTX_HIST_SIZE; i++)
800 if (st->sumD[i] > summax)
803 summax = st->sumD[i];
806 if (st->sumD[i] < summin)
809 summin = st->sumD[i];
813 /* Find the second largest distance */
814 summax2nd = -2147483647L;
816 for (i = 0; i < DTX_HIST_SIZE; i++)
819 if ((st->sumD[i] > summax2nd) && (i != indices[0]))
822 summax2nd = st->sumD[i];
826 for (i = 0; i < 3; i++)
828 indices[i] = sub_int16(st->hist_ptr, indices[i]);
832 indices[i] = add_int16(indices[i], DTX_HIST_SIZE);
836 /* If maximum distance/MED_THRESH is smaller than minimum distance */
837 /* then the median ISF vector replacement is not performed */
838 tmp = normalize_amr_wb(summax);
841 L_tmp = mul_16by16_to_int32(amr_wb_round(summax), INV_MED_THRESH);
847 /* If second largest distance/MED_THRESH is smaller than */
848 /* minimum distance then the median ISF vector replacement is */
850 summax2nd = shl_int32(summax2nd, tmp);
851 L_tmp = mul_16by16_to_int32(amr_wb_round(summax2nd), INV_MED_THRESH);
861 /*----------------------------------------------------------------------------
863 ----------------------------------------------------------------------------*/
865 int16 dithering_control(dtx_encState * st)
867 int16 i, tmp, mean, CN_dith, gain_diff;
870 /* determine how stationary the spectrum of background noise is */
872 for (i = 0; i < 8; i++)
874 ISF_diff = add_int32(ISF_diff, st->sumD[i]);
876 if ((ISF_diff >> 26) > 0)
885 /* determine how stationary the energy of background noise is */
887 for (i = 0; i < DTX_HIST_SIZE; i++)
889 mean = add_int16(mean, st->log_en_hist[i]);
893 for (i = 0; i < DTX_HIST_SIZE; i++)
895 tmp = sub_int16(st->log_en_hist[i], mean);
896 tmp = tmp - (tmp < 0);
898 gain_diff += tmp ^(tmp >> 15); /* tmp ^sign(tmp) */;
900 if (gain_diff > GAIN_THR)
908 /*----------------------------------------------------------------------------
910 ----------------------------------------------------------------------------*/
914 int32 * L_log_en_int,
918 int16 temp, temp1, i, dither_fac, rand_dith;
921 /* Insert comfort noise dithering for energy parameter */
922 rand_dith = noise_gen_amrwb(dither_seed) >> 1;
923 rand_dith2 = noise_gen_amrwb(dither_seed) >> 1;
924 rand_dith += rand_dith2;
925 *L_log_en_int = add_int32(*L_log_en_int, mul_16by16_to_int32(rand_dith, GAIN_FACTOR));
927 if (*L_log_en_int < 0)
931 /* Insert comfort noise dithering for spectral parameters (ISF-vector) */
932 dither_fac = ISF_FACTOR_LOW;
934 rand_dith = noise_gen_amrwb(dither_seed) >> 1;
935 rand_dith2 = noise_gen_amrwb(dither_seed) >> 1;
936 rand_dith += rand_dith2;
937 temp = add_int16(isf[0], mult_int16_r(rand_dith, dither_fac));
939 /* Make sure that isf[0] will not get negative values */
949 for (i = 1; i < M - 1; i++)
951 dither_fac = add_int16(dither_fac, ISF_FACTOR_STEP);
953 rand_dith = noise_gen_amrwb(dither_seed) >> 1;
954 rand_dith2 = noise_gen_amrwb(dither_seed) >> 1;
955 rand_dith += rand_dith2;
956 temp = add_int16(isf[i], mult_int16_r(rand_dith, dither_fac));
957 temp1 = sub_int16(temp, isf[i - 1]);
959 /* Make sure that isf spacing remains at least ISF_DITH_GAP Hz */
960 if (temp1 < ISF_DITH_GAP)
962 isf[i] = isf[i - 1] + ISF_DITH_GAP;
970 /* Make sure that isf[M-2] will not get values above 16384 */
971 if (isf[M - 2] > 16384)