1/* ------------------------------------------------------------------
2 * Copyright (C) 1998-2009 PacketVideo
3 *
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
7 *
8 *      http://www.apache.org/licenses/LICENSE-2.0
9 *
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
13 * express or implied.
14 * See the License for the specific language governing permissions
15 * and limitations under the License.
16 * -------------------------------------------------------------------
17 */
18/*
19 Filename: calc_sbr_envelope.c
20
21------------------------------------------------------------------------------
22 REVISION HISTORY
23
24
25 Who:                                   Date: MM/DD/YYYY
26 Description:
27
28------------------------------------------------------------------------------
29 INPUT AND OUTPUT DEFINITIONS
30
31
32
33------------------------------------------------------------------------------
34 FUNCTION DESCRIPTION
35
36
37------------------------------------------------------------------------------
38 REQUIREMENTS
39
40
41------------------------------------------------------------------------------
42 REFERENCES
43
44SC 29 Software Copyright Licencing Disclaimer:
45
46This software module was originally developed by
47  Coding Technologies
48
49and edited by
50  -
51
52in the course of development of the ISO/IEC 13818-7 and ISO/IEC 14496-3
53standards for reference purposes and its performance may not have been
54optimized. This software module is an implementation of one or more tools as
55specified by the ISO/IEC 13818-7 and ISO/IEC 14496-3 standards.
56ISO/IEC gives users free license to this software module or modifications
57thereof for use in products claiming conformance to audiovisual and
58image-coding related ITU Recommendations and/or ISO/IEC International
59Standards. ISO/IEC gives users the same free license to this software module or
60modifications thereof for research purposes and further ISO/IEC standardisation.
61Those intending to use this software module in products are advised that its
62use may infringe existing patents. ISO/IEC have no liability for use of this
63software module or modifications thereof. Copyright is not released for
64products that do not conform to audiovisual and image-coding related ITU
65Recommendations and/or ISO/IEC International Standards.
66The original developer retains full right to modify and use the code for its
67own purpose, assign or donate the code to a third party and to inhibit third
68parties from using the code for products that do not conform to audiovisual and
69image-coding related ITU Recommendations and/or ISO/IEC International Standards.
70This copyright notice must be included in all copies or derivative works.
71Copyright (c) ISO/IEC 2002.
72
73------------------------------------------------------------------------------
74 PSEUDO-CODE
75
76------------------------------------------------------------------------------
77*/
78
79
80/*----------------------------------------------------------------------------
81; INCLUDES
82----------------------------------------------------------------------------*/
83
84#ifdef AAC_PLUS
85
86
87#include    "calc_sbr_envelope.h"
88#include    "sbr_envelope_calc_tbl.h"
89#include    "sbr_create_limiter_bands.h"
90#include    "aac_mem_funcs.h"
91
92#include    "fxp_mul32.h"
93#include    "pv_normalize.h"
94
95#include    "sbr_aliasing_reduction.h"
96
97/*----------------------------------------------------------------------------
98; MACROS
99; Define module specific macros here
100----------------------------------------------------------------------------*/
101
102
103/*----------------------------------------------------------------------------
104; DEFINES
105; Include all pre-processor statements here. Include conditional
106; compile variables also.
107----------------------------------------------------------------------------*/
108#include    "pv_sqrt.h"
109
110#include    "pv_div.h"
111#include    "fxp_mul32.h"
112#include    "pv_normalize.h"
113
114#define Q30fmt(x)   (Int32)(x*((Int32)1<<30) + (x>=0?0.5F:-0.5F))
115#define Q28fmt(x)   (Int32)(x*((Int32)1<<28) + (x>=0?0.5F:-0.5F))
116#define Q15fmt(x)   (Int32)(x*((Int32)1<<15) + (x>=0?0.5F:-0.5F))
117
118
119/*----------------------------------------------------------------------------
120; LOCAL FUNCTION DEFINITIONS
121; Function Prototype declaration
122----------------------------------------------------------------------------*/
123#ifdef __cplusplus
124extern "C"
125{
126#endif
127
128    void envelope_application_LC(Int32  *aBufR,
129    Int32  *nrg_gain_man,
130    Int32  *nrg_gain_exp,
131    Int32  *noise_level_man,
132    Int32  *noise_level_exp,
133    Int32  *nrg_tone_man,
134    Int32  *nrg_tone_exp,
135    Int32  band_nrg_tone_detector,
136    const Int32 *frame_info,
137    Int32  *harm_index,
138    Int32  *phase_index,
139    Int32  i,
140    Int32  lowSubband,
141    Int32  noSubbands,
142    Int32  noNoiseFlag);
143
144
145    void energy_estimation_LC(Int32 *aBufR,
146                              Int32 *nrg_est_man,
147                              Int32 *nrg_est_exp,
148                              const Int32 *frame_info,
149                              Int32 i,
150                              Int32 k,
151                              Int32 c,
152                              Int32 ui2);
153
154#ifdef HQ_SBR
155
156
157    void envelope_application(Int32  *aBufR,
158                              Int32  *aBufI,
159                              Int32  *nrg_gain_man,
160                              Int32  *nrg_gain_exp,
161                              Int32  *noise_level_man,
162                              Int32  *noise_level_exp,
163                              Int32  *nrg_tone_man,
164                              Int32  *nrg_tone_exp,
165                              Int32 *fBuf_man[64],
166                              Int32 *fBuf_exp[64],
167                              Int32 *fBufN_man[64],
168                              Int32 *fBufN_exp[64],
169                              const Int32 *frame_info,
170                              Int32  *harm_index,
171                              Int32  *phase_index,
172                              Int32  i,
173                              Int32  lowSubband,
174                              Int32  noSubbands,
175                              Int32  noNoiseFlag,
176                              Int32  band_nrg_tone_detector,
177                              Int32  maxSmoothLength,
178                              Int32  smooth_length);
179
180
181    void energy_estimation(Int32 *aBufR,
182                           Int32 *aBufI,
183                           Int32 *nrg_est_man,
184                           Int32 *nrg_est_exp,
185                           const Int32 *frame_info,
186                           Int32 i,
187                           Int32 k,
188                           Int32 c,
189                           Int32 ui2);
190
191#endif
192
193#ifdef __cplusplus
194}
195#endif
196
197/*----------------------------------------------------------------------------
198; LOCAL STORE/BUFFER/POINTER DEFINITIONS
199; Variable declaration - defined here and used outside this module
200----------------------------------------------------------------------------*/
201
202/*----------------------------------------------------------------------------
203; EXTERNAL FUNCTION REFERENCES
204; Declare functions defined elsewhere and referenced in this module
205----------------------------------------------------------------------------*/
206
207/*----------------------------------------------------------------------------
208; EXTERNAL GLOBAL STORE/BUFFER/POINTER REFERENCES
209; Declare variables used in this module but defined elsewhere
210----------------------------------------------------------------------------*/
211
212/*----------------------------------------------------------------------------
213; FUNCTION CODE
214----------------------------------------------------------------------------*/
215
216void calc_sbr_envelope(SBR_FRAME_DATA *frameData,
217                       Int32 *aBufR,
218                       Int32 *aBufI,
219                       Int freqBandTable1[2][MAX_FREQ_COEFFS + 1],
220                       const Int32 *nSfb,
221                       Int32 freqBandTable2[MAX_NOISE_COEFFS + 1],
222                       Int32 nNBands,
223                       Int32 reset,
224                       Int32 *degreeAlias,
225                       Int32 *harm_index,
226                       Int32 *phase_index,
227                       Int32 hFp[64],
228                       Int32 *sUp,
229                       Int32 limSbc[][13],
230                       Int32 *gateMode,
231#ifdef HQ_SBR
232                       Int32 *fBuf_man[64],
233                       Int32 *fBuf_exp[64],
234                       Int32 *fBufN_man[64],
235                       Int32 *fBufN_exp[64],
236#endif
237                       Int32 scratch_mem[][64],
238                       struct PATCH Patch,
239                       Int32  sqrt_cache[][4],
240                       Int32  LC_flag)
241{
242
243    Int32 c;
244    Int32 li;
245    Int32 ui;
246    Int32 i;
247    Int32 j;
248    Int32 k = 0;
249    Int32 l;
250    Int m = 0;
251    Int kk = 0;
252    Int o;
253    Int next = -1;
254    Int32 ui2;
255    Int flag;
256    Int noNoiseFlag;
257    Int *ptr;
258
259
260    UInt32 nrg = 0;
261    Int32 nrg_exp = 0;
262    struct intg_div   quotient;
263    struct intg_sqrt  root_sq;
264
265    Int32 aux1;
266
267    Int32 *nL_man       = frameData->sbrNoiseFloorLevel_man;
268    Int32 *nL_exp       = frameData->sbrNoiseFloorLevel_exp;
269
270    Int32 *sfb_nrg_man  = frameData->iEnvelope_man;
271    Int32 *sfb_nrg_exp  = frameData->iEnvelope_exp;
272
273    Int32 tmp_q1;
274    Int32 tmp_q2;
275
276    Int32 g_max_man;
277    Int32 g_max_exp;
278
279    Int32 p_ref_man;
280    Int32 p_ref_exp;
281
282    Int32 p_est_man;
283    Int32 p_est_exp;
284
285    Int32 p_adj_man;
286    Int32 p_adj_exp;
287    Int32 avg_gain;
288
289    Int32 boost_gain_q;
290
291    Int32 band_nrg_tone_detector;
292
293    Int32 *nrg_est_man     = scratch_mem[0];
294    Int32 *nrg_est_exp     = scratch_mem[1];
295    Int32 *nrg_ref_man     = scratch_mem[2];
296    Int32 *nrg_ref_exp     = scratch_mem[3];
297    Int32 *nrg_gain_man    = scratch_mem[4];
298    Int32 *nrg_gain_exp    = scratch_mem[5];
299    Int32 *noise_level_man = scratch_mem[6];
300    Int32 *noise_level_exp = scratch_mem[7];
301    Int32 *nrg_tone_man    = scratch_mem[8];
302    Int32 *nrg_tone_exp    = scratch_mem[9];
303    Int32 *hF              = scratch_mem[10];
304
305
306
307    const Int32 *frame_info = frameData->frameInfo;
308    Int32 int_mode          = frameData->sbr_header.interpolFreq;
309
310
311
312
313
314    Int32 dontUseTheseGainValues[64];
315
316#ifdef HQ_SBR
317
318    Int32 n;
319    Int32 smooth_length;
320    Int32 smoothingLength   = frameData->sbr_header.smoothingLength;
321    Int32 maxSmoothLength   = smoothLengths[0];
322
323#endif
324
325    Int32 limiterBand       = frameData->sbr_header.limiterBands;
326    Int32 limiterGains      = frameData->sbr_header.limiterGains;
327    Int32 *addHarmonics     = frameData->addHarmonics;
328
329    Int32 lowSubband        = freqBandTable1[LOW_RES][0];
330    Int32 noSubbands        = freqBandTable1[LOW_RES][nSfb[LOW_RES]] - lowSubband;
331    Int32 nEnv              = frame_info[0];
332    Int32 sEnv              = frame_info[(nEnv + 1)<<1];
333
334    /* ensure that noSubbands in the range [0,64] */
335    noSubbands = (noSubbands >> 31) ^ noSubbands;
336    if (noSubbands > 64)
337    {
338        noSubbands = 64;
339    }
340
341    if (reset)
342    {
343        *sUp = 1;
344        *phase_index = 0;
345        sbr_create_limiter_bands(limSbc,
346                                 gateMode,
347                                 freqBandTable1[LOW_RES],
348                                 Patch,
349                                 nSfb[LOW_RES]);
350    }
351
352    /* Mapping. */
353    pv_memset((void*)hF, 0, (sizeof(*hF) << 6));
354
355    ptr  = freqBandTable1[HI];
356    l = *(ptr++);
357
358    for (i = nSfb[HI]; i != 0; i--)
359    {
360        k     = *(ptr++);
361        j     = ((k + l) >> 1) - lowSubband;
362        l   = k;
363        hF[j] = *(addHarmonics++);
364    }
365
366
367    /* Envelope adjustment. */
368
369    for (i = 0; i < nEnv; i++)
370    {
371
372        if (frame_info[1+i] == frame_info[(nEnv<<1)+4+kk])
373        {
374            kk++, next++;
375        }
376
377        noNoiseFlag = (i == sEnv || i == frameData->prevEnvIsShort) ? 1 : 0;
378
379#ifdef HQ_SBR
380        smooth_length = (noNoiseFlag ? 0 : smoothLengths[smoothingLength]);
381#endif
382
383
384        /* Estimate levels. */
385        c = 0;
386        o = 0;
387
388        band_nrg_tone_detector = 0;
389
390        Int kkkk = freqBandTable1[ frame_info[nEnv+2+i] ][0];
391
392        for (j = 0; j <  nSfb[frame_info[nEnv+2+i]]; j++)
393        {
394            li = freqBandTable1[ frame_info[nEnv+2+i] ][j    ];
395            ui = freqBandTable1[ frame_info[nEnv+2+i] ][j + 1];
396            flag = 0;
397
398            for (k = li; k < ui; k++)
399            {                               /* Calculate the average energy over the current envelope, */
400                ui2   = (frame_info[1+i] << 1);
401
402                if (LC_flag == ON)
403                {
404                    energy_estimation_LC((Int32 *)aBufR,
405                                         nrg_est_man,
406                                         nrg_est_exp,
407                                         frame_info,
408                                         i,
409                                         k - kkkk,
410                                         c,
411                                         ui2);
412                }
413#ifdef HQ_SBR
414                else
415                {
416
417                    energy_estimation((Int32 *)aBufR,
418                                      (Int32 *)aBufI,
419                                      nrg_est_man,
420                                      nrg_est_exp,
421                                      frame_info,
422                                      i,
423                                      k - kkkk,
424                                      c,
425                                      ui2);
426                }
427#endif
428
429                flag = (hF[c] && (i >= sEnv || hFp[c+lowSubband])) ? 1 : flag;
430                c++;
431            }
432
433
434            ui2 = freqBandTable2[o+1];
435
436            if (!int_mode)
437            {                                /* If no interpolation is used,   */
438
439                tmp_q1 = -100;
440
441                for (k = c - (ui - li); k < c; k++)
442                {
443                    if (tmp_q1 < nrg_est_exp[k])
444                    {
445                        tmp_q1 = nrg_est_exp[k];
446                    }
447                }
448
449                nrg = 0;
450                for (k = c - (ui - li); k < c; k++)
451                {    /* average the energy in all the QMF bands, */
452                    nrg += nrg_est_man[k] >> (tmp_q1 - nrg_est_exp[k]); /* for the whole scalefactor band.  */
453                }
454                nrg /= (ui - li);
455                nrg_exp = tmp_q1;
456
457            }
458
459            c -= (ui - li);
460
461            for (k = 0; k < ui - li; k++)
462            {
463                o = (k + li >= ui2) ? o + 1 : o;
464                ui2 = freqBandTable2[o+1];
465                /*
466                 *  If no interpolation is used, use the averaged energy from above,
467                 *  otherwise do nothing.
468                 */
469
470
471                if (!int_mode)
472                {
473                    nrg_est_man[c] = nrg;
474                    nrg_est_exp[c] = nrg_exp;
475                }
476
477                if (LC_flag == ON)
478                {
479                    nrg_est_exp[c] += 1;
480
481                    if (flag)
482                    {
483                        dontUseTheseGainValues[k + li - lowSubband] = 1;
484                    }
485                    else
486                    {
487                        dontUseTheseGainValues[k + li - lowSubband] = 0;
488                    }
489                }
490
491                nrg_ref_man[c] = sfb_nrg_man[m];
492                nrg_ref_exp[c] = sfb_nrg_exp[m];
493
494                /*
495                 *  compute nL/(1 + nL);   where nL = nL_man*2^nL_exp
496                 */
497                aux1 = next * nNBands + o;
498
499                tmp_q1 = nL_exp[aux1];
500
501                if (tmp_q1 >= 0)
502                {
503                    pv_div(nL_man[aux1], nL_man[aux1] + (0x3FFFFFFF >> tmp_q1), &quotient);
504                }
505                else
506                {
507                    tmp_q1 = nL_man[aux1] >> (-tmp_q1);
508                    pv_div(tmp_q1, tmp_q1 + 0x3FFFFFFF, &quotient);
509                }
510
511                /*
512                 *  tmp_q1 = nL/(1 + nL)*nrg_ref[c];
513                 */
514
515                tmp_q1 = fxp_mul32_Q30(quotient.quotient >> quotient.shift_factor,  nrg_ref_man[c]);
516
517                if (flag)
518                {
519                    /*
520                     *  Calculate levels and gain, dependent on whether a synthetic, a sine is present or not.
521                     *
522                     *  nrg_gain[c]=(float)pv_sqrt( tmp/(nrg_est[c] + 1), sqrt_cache[1] );
523                     */
524
525
526                    pv_div(tmp_q1, nrg_est_man[c] + 1, &quotient);
527                    /*
528                     *  nrg_est_man[c] is an integer number, while tmp_q1 and quotient.quotient
529                     *  are fractions in Q30
530                     */
531
532                    tmp_q2 = nrg_ref_exp[c] - nrg_est_exp[c] - quotient.shift_factor - 30;
533
534                    pv_sqrt(quotient.quotient, tmp_q2, &root_sq, sqrt_cache[1]);
535                    nrg_gain_man[c] = root_sq.root;     /*  in Q28 format */
536                    nrg_gain_exp[c] = root_sq.shift_factor;
537
538
539                    /*
540                     *  nrg_tone[c]=(float)( (hF[c] && (i >= sEnv || hFp[c+lowSubband])) ?
541                     *                          pv_sqrt(nrg_ref[c]/(1+tmp_nL), sqrt_cache[2]) : 0);
542                     */
543                    if (hF[c] && (i >= sEnv || hFp[c+lowSubband]))
544                    {
545                        /*
546                         *  nrg_ref[c] and  nL, as well as quotient.quotient
547                         *  are fractions in Q30
548                         */
549
550                        /*  aux1 == next*nNBands + o */
551
552                        tmp_q2 = nL_exp[aux1];
553                        /*
554                         *  nrg_ref[c]/(1+tmp_nL)
555                         */
556
557                        if (tmp_q2 >= 0)
558                        {
559                            pv_div(nrg_ref_man[c], nL_man[aux1] + (0x3FFFFFFF >> tmp_q2), &quotient);
560                        }
561                        else
562                        {
563                            tmp_q2 = nL_man[aux1] >> (-tmp_q2);
564                            pv_div(nrg_ref_man[c], tmp_q2 + 0x3FFFFFFF, &quotient);
565                            tmp_q2 = 0;     /* exponent has been applied to the sum ((man>>exp) + 1)  */
566                        }
567
568                        tmp_q2 = nrg_ref_exp[c] - tmp_q2 - quotient.shift_factor;
569
570                        pv_sqrt(quotient.quotient, tmp_q2, &root_sq, sqrt_cache[2]);
571                        nrg_tone_man[c]    = root_sq.root;
572                        nrg_tone_exp[c]    = root_sq.shift_factor;
573
574                    }
575                    else
576                    {
577                        nrg_tone_man[c]    = 0;
578                        nrg_tone_exp[c]    = 0;
579                    }
580
581                }
582                else
583                {
584                    if (noNoiseFlag)
585                    {
586                        /*
587                         * nrg_gain[c] = (float) pv_sqrt(nrg_ref[c] /(nrg_est[c] + 1), sqrt_cache[3]);
588                         */
589
590                        pv_div(nrg_ref_man[c], nrg_est_man[c] + 1, &quotient);
591
592                        /*
593                         *  nrg_est_man[c] is an integer number, while nrg_ref_man[c] and
594                         *  quotient.quotient are fractions in Q30
595                         */
596
597                        tmp_q2 = nrg_ref_exp[c] - nrg_est_exp[c] - quotient.shift_factor - 30;
598
599                        pv_sqrt(quotient.quotient, tmp_q2, &root_sq, sqrt_cache[3]);
600                        nrg_gain_man[c] = root_sq.root;
601                        nrg_gain_exp[c] = root_sq.shift_factor;
602
603                    }
604                    else
605                    {
606                        /*
607                         *  nrg_gain[c] = (float) pv_sqrt(nrg_ref[c]/((nrg_est[c] + 1)*(1+tmp_nL)), sqrt_cache[4]);
608                         */
609                        /*  aux1 == next*nNBands + o */
610
611                        tmp_q2 = nL_exp[aux1];
612                        /*
613                         *  nrg_ref[c]/((nrg_est[c] + 1)*(1+tmp_nL))
614                         */
615
616                        if (nrg_est_man[c] == 0)
617                        {
618                            tmp_q2 = 0;     /*  avoid division by 0 in next if-else, this could be due to
619                                                rounding noise */
620                        }
621
622
623                        if (tmp_q2 >= 0)
624                        {
625
626                            tmp_q2 = fxp_mul32_Q30(nrg_est_man[c] + 1, nL_man[aux1] + (0x3FFFFFFF >> tmp_q2));
627                            pv_div(nrg_ref_man[c], tmp_q2, &quotient);
628                            /*
629                             *  nrg_est_man[c] is an integer number, while nrg_ref_man[c] and
630                             *  quotient.quotient are fractions in Q30
631                             */
632                            tmp_q2 = nrg_ref_exp[c] - quotient.shift_factor - 30 - nL_exp[aux1];
633                            if (nrg_est_man[c])
634                            {
635                                tmp_q2 -=  nrg_est_exp[c];
636                            }
637
638                            tmp_q2 = nrg_ref_exp[c] - nrg_est_exp[c] - quotient.shift_factor - 30 - nL_exp[aux1];
639                        }
640                        else
641                        {
642                            if (tmp_q2 > - 10)
643                            {
644                                tmp_q2 = nL_man[aux1] >> (-tmp_q2);
645
646                                tmp_q2 = fxp_mul32_Q30(nrg_est_man[c] + 1, tmp_q2 + 0x3FFFFFFF);
647                            }
648                            else
649                            {
650                                tmp_q2 = nrg_est_man[c] + 1;
651                            }
652
653
654                            pv_div(nrg_ref_man[c], tmp_q2, &quotient);
655                            /*
656                             *  nrg_est_man[c] is an integer number, while nrg_ref_man[c] and
657                             *  quotient.quotient are fractions in Q30
658                             */
659
660                            tmp_q2 = nrg_ref_exp[c] - quotient.shift_factor - 30;
661                            if (nrg_est_man[c])
662                            {
663                                tmp_q2 -=  nrg_est_exp[c];
664                            }
665
666                        }
667
668                        pv_sqrt(quotient.quotient, tmp_q2, &root_sq, sqrt_cache[4]);
669                        nrg_gain_man[c] = root_sq.root;
670                        nrg_gain_exp[c] = root_sq.shift_factor;
671
672                    }
673
674                    nrg_tone_man[c]    = 0;
675                    nrg_tone_exp[c]    = -100;
676
677                }
678
679                band_nrg_tone_detector |= nrg_tone_man[c];   /*  detect any tone activity  */
680
681                pv_sqrt(tmp_q1, nrg_ref_exp[c], &root_sq, sqrt_cache[5]);
682                noise_level_man[c] = root_sq.root;
683                noise_level_exp[c] = root_sq.shift_factor;
684
685                c++;
686
687            }   /* ---- end-for-loop (k) ------ */
688            m++;
689
690        }   /* -------- Estimate levels end-for-loop (j) ----- */
691
692
693
694        /*
695         *      Limiter
696         */
697
698
699        for (c = 0; c < gateMode[limiterBand]; c++)
700        {
701
702            p_ref_man = 0;
703            p_est_man = 0;
704
705            /*
706             *  get max exponent for the reference and estimated energy
707             */
708            p_ref_exp = -100;
709            p_est_exp = -100;
710
711            for (k = limSbc[limiterBand][c]; k < limSbc[limiterBand][c + 1]; k++)
712            {
713                if (p_ref_exp < nrg_ref_exp[k])
714                {
715                    p_ref_exp = nrg_ref_exp[k];    /* max */
716                }
717                if (p_est_exp < nrg_est_exp[k])
718                {
719                    p_est_exp = nrg_est_exp[k];    /* max */
720                }
721            }
722
723            k -= limSbc[limiterBand][c];     /*  number of element used in the addition */
724
725            while (k != 0)      /*  bit guard protection depends on log2(k)  */
726            {
727                k >>= 1;
728                p_ref_exp++;       /*  add extra bit-overflow-guard, nrg_ref_exp is in Q30 format */
729            }
730
731
732            for (k = limSbc[limiterBand][c]; k < limSbc[limiterBand][c + 1]; k++)
733            {   /*Calculate the average gain for the current limiter band.*/
734                p_ref_man += (nrg_ref_man[k] >> (p_ref_exp - nrg_ref_exp[k]));
735                p_est_man += (nrg_est_man[k] >> (p_est_exp - nrg_est_exp[k]));
736
737            }
738
739            if (p_est_man)
740            {
741                /*
742                 *  "average gain" (not equal to average of nrg_gain)
743                 */
744                pv_div(p_ref_man, p_est_man, &quotient);
745
746                tmp_q2 = p_ref_exp - 30 - p_est_exp - quotient.shift_factor;
747
748                /*
749                 *  avg_gain = sqrt(p_ref/p_est)
750                 */
751                pv_sqrt(quotient.quotient, tmp_q2, &root_sq, sqrt_cache[6]);
752                avg_gain  = root_sq.root;
753                g_max_exp = root_sq.shift_factor;
754
755                /*
756                 *  maximum gain allowed is calculated from table.
757                 */
758
759                /*
760                 *  g_max = avg_gain * limGains[limiterGains];
761                 */
762
763                g_max_man = fxp_mul32_Q30(avg_gain, limGains[limiterGains]);   /*  table is in Q30 */
764
765                if (limiterGains == 3)
766                {
767                    g_max_exp = limGains[4];
768                }
769
770                tmp_q1 = g_max_exp >= 16 ? g_max_exp : 16;
771
772                tmp_q2 = g_max_man >> (tmp_q1 - g_max_exp);
773                tmp_q1 = Q28fmt(1.52587890625F) >> (tmp_q1 - 16);
774
775                if (tmp_q2 > tmp_q1)
776                {
777                    /* upper limit, +100 dB */
778                    g_max_man = Q28fmt(1.52587890625F);
779                    g_max_exp = 16;
780                }
781            }
782            else
783            {
784                /*  Qfmt(1.52587890625F)    exp = 16 */
785                g_max_man = Q28fmt(1.52587890625F);
786                g_max_exp = 16;
787            }
788
789            /*
790             *  Compute Adjusted power p_adj
791             */
792            for (k = limSbc[limiterBand][c]; k < limSbc[limiterBand][c + 1]; k++)
793            {
794
795                tmp_q1 = g_max_exp >= nrg_gain_exp[k] ? g_max_exp : nrg_gain_exp[k];
796
797                tmp_q2 = g_max_man >> (tmp_q1 - g_max_exp);
798                tmp_q1 = nrg_gain_man[k] >> (tmp_q1 - nrg_gain_exp[k]);
799                /*
800                 *  if(g_max <= nrg_gain[k])
801                 */
802                if (tmp_q2 <= tmp_q1)
803                {
804                    tmp_q1 = fxp_mul32_Q28(noise_level_man[k], g_max_man);
805                    pv_div(tmp_q1, nrg_gain_man[k], &quotient);
806                    noise_level_man[k] = quotient.quotient >> 2;   /* in Q28 */
807                    noise_level_exp[k] = noise_level_exp[k] + g_max_exp - quotient.shift_factor - nrg_gain_exp[k];
808
809                    nrg_gain_man[k] =  g_max_man;       /* gains with noise supression */
810                    nrg_gain_exp[k] =  g_max_exp;
811                }
812            }
813
814            p_adj_exp = -100;
815
816            for (k = limSbc[limiterBand][c]; k < limSbc[limiterBand][c + 1]; k++)
817            {
818                tmp_q1 = nrg_est_exp[k] + (nrg_gain_exp[k] << 1) + 28;  /* 28 to match shift down by mult32_Q28  */
819
820                if (p_adj_exp < tmp_q1)
821                {
822                    p_adj_exp = tmp_q1;
823                }
824                if (nrg_tone_man[k])
825                {
826                    tmp_q1 = (nrg_tone_exp[k] << 1);
827                    if (p_adj_exp < tmp_q1)
828                    {
829                        p_adj_exp = tmp_q1;
830                    }
831                }
832                else if (!noNoiseFlag)
833                {
834                    tmp_q1 = (noise_level_exp[k] << 1);
835
836                    if (p_adj_exp < tmp_q1)
837                    {
838                        p_adj_exp = tmp_q1;
839                    }
840                }
841            }
842
843            p_adj_exp += 1; /* overflow bit-guard*/
844
845            p_adj_man = 0;
846
847            for (k = limSbc[limiterBand][c]; k < limSbc[limiterBand][c + 1]; k++)
848            {
849                /*
850                 *  p_adj += nrg_gain[k]*nrg_gain[k]*nrg_est[k];
851                 */
852
853                if (p_adj_exp - (nrg_est_exp[k] + (nrg_gain_exp[k] << 1)) < 59)
854                {
855                    tmp_q1 = fxp_mul32_Q28(nrg_gain_man[k], nrg_gain_man[k]);
856                    tmp_q1 = fxp_mul32_Q28(tmp_q1, nrg_est_man[k]);
857                    p_adj_man += (tmp_q1 >> (p_adj_exp - (nrg_est_exp[k] + (nrg_gain_exp[k] << 1) + 28)));
858                }
859
860                if (nrg_tone_man[k])
861                {
862                    /*
863                     *  p_adj += nrg_tone[k]*nrg_tone[k];
864                     */
865                    if (p_adj_exp - (nrg_tone_exp[k] << 1) < 31)
866                    {
867                        tmp_q1 = fxp_mul32_Q28(nrg_tone_man[k], nrg_tone_man[k]);
868                        p_adj_man += (tmp_q1 >> (p_adj_exp - (nrg_tone_exp[k] << 1)));
869                    }
870                }
871                else if (!noNoiseFlag)
872                {
873                    /*
874                     *  p_adj += noise_level[k]*noise_level[k];
875                     */
876
877                    if (p_adj_exp - (noise_level_exp[k] << 1) < 31)
878                    {
879                        tmp_q1 = fxp_mul32_Q28(noise_level_man[k], noise_level_man[k]);
880                        p_adj_man += (tmp_q1 >> (p_adj_exp - (noise_level_exp[k] << 1)));
881                    }
882
883                }
884            }
885
886
887            if (p_adj_man)
888            {
889                pv_div(p_ref_man, p_adj_man, &quotient);
890                tmp_q2 = p_ref_exp - p_adj_exp - 58 - quotient.shift_factor;   /*  58 <> Q30 + Q28 */
891
892                pv_sqrt(quotient.quotient, tmp_q2, &root_sq, sqrt_cache[7]);
893
894                if (root_sq.shift_factor > -28)
895                {
896                    boost_gain_q = root_sq.root << (root_sq.shift_factor + 28);
897                }
898                else
899                {
900                    boost_gain_q = root_sq.root >> (-28 - root_sq.shift_factor);
901                }
902
903                tmp_q1 = root_sq.shift_factor >= -28 ? root_sq.shift_factor : -28;
904
905                tmp_q2 = root_sq.root >> (tmp_q1 - root_sq.shift_factor);
906                tmp_q1 = Q28fmt(1.584893192f) >> (tmp_q1 + 28);
907
908
909                if (tmp_q2 > tmp_q1)
910                {
911                    boost_gain_q = Q28fmt(1.584893192f);
912                }
913            }
914            else
915            {
916                boost_gain_q = Q28fmt(1.584893192f);
917            }
918
919            if (band_nrg_tone_detector)
920            {
921                for (k = limSbc[limiterBand][c]; k < limSbc[limiterBand][c + 1]; k++)
922                {
923                    nrg_gain_man[k]    = fxp_mul32_Q28(nrg_gain_man[k], boost_gain_q);
924                    noise_level_man[k] = fxp_mul32_Q28(noise_level_man[k], boost_gain_q);
925                    nrg_tone_man[k]    = fxp_mul32_Q28(nrg_tone_man[k], boost_gain_q);
926                }
927            }
928            else
929            {
930
931                for (k = limSbc[limiterBand][c]; k < limSbc[limiterBand][c + 1]; k++)
932                {
933                    nrg_gain_man[k]    = fxp_mul32_Q28(nrg_gain_man[k], boost_gain_q);
934                    noise_level_man[k] = fxp_mul32_Q28(noise_level_man[k], boost_gain_q);
935                }
936
937
938            }
939
940        }   /* Limiter  End for loop (c) */
941
942
943        if (LC_flag == ON)
944        {
945
946            /*
947             *          Aliasing correction
948             */
949
950            sbr_aliasing_reduction(degreeAlias,
951                                   nrg_gain_man,
952                                   nrg_gain_exp,
953                                   nrg_est_man,
954                                   nrg_est_exp,
955                                   dontUseTheseGainValues,
956                                   noSubbands,
957                                   lowSubband,
958                                   sqrt_cache,
959                                   scratch_mem[3]);
960
961            if (*sUp)     /* Init only done once upon reset */
962            {
963                *sUp = 0;
964            }
965
966            envelope_application_LC((Int32 *)aBufR,
967                                    nrg_gain_man,
968                                    nrg_gain_exp,
969                                    noise_level_man,
970                                    noise_level_exp,
971                                    nrg_tone_man,
972                                    nrg_tone_exp,
973                                    band_nrg_tone_detector,
974                                    frame_info,
975                                    harm_index,
976                                    phase_index,
977                                    i,
978                                    lowSubband,
979                                    noSubbands,
980                                    noNoiseFlag);
981        }
982#ifdef HQ_SBR
983        else
984        {
985
986            if (*sUp)     /* Init only done once upon reset */
987            {
988                for (n = 0; n < maxSmoothLength; n++)
989                {
990                    pv_memcpy(fBuf_man[n],     nrg_gain_man, noSubbands*sizeof(*fBuf_man[n]));
991                    pv_memcpy(fBufN_man[n], noise_level_man, noSubbands*sizeof(*fBufN_man[n]));
992                    pv_memcpy(fBuf_exp[n],     nrg_gain_exp, noSubbands*sizeof(*fBuf_exp[n]));
993                    pv_memcpy(fBufN_exp[n], noise_level_exp, noSubbands*sizeof(*fBufN_exp[n]));
994                }
995                *sUp = 0;
996            }
997
998
999            envelope_application((Int32 *)aBufR,
1000                                 (Int32 *)aBufI,
1001                                 nrg_gain_man,
1002                                 nrg_gain_exp,
1003                                 noise_level_man,
1004                                 noise_level_exp,
1005                                 nrg_tone_man,
1006                                 nrg_tone_exp,
1007                                 fBuf_man,
1008                                 fBuf_exp,
1009                                 fBufN_man,
1010                                 fBufN_exp,
1011                                 frame_info,
1012                                 harm_index,
1013                                 phase_index,
1014                                 i,
1015                                 lowSubband,
1016                                 noSubbands,
1017                                 noNoiseFlag,
1018                                 band_nrg_tone_detector,
1019                                 maxSmoothLength,
1020                                 smooth_length);
1021
1022        }
1023#endif
1024
1025    }   /* -----  Envelope adjustment end for-loop (i) ---- */
1026
1027
1028    pv_memcpy(&hFp[0] + lowSubband,
1029              hF,
1030              (64 - lowSubband)*sizeof(*hF));
1031
1032    if (sEnv == nEnv)
1033    {
1034        frameData->prevEnvIsShort = 0;
1035    }
1036    else
1037    {
1038        frameData->prevEnvIsShort = -1;
1039    }
1040
1041
1042}
1043
1044
1045
1046/*----------------------------------------------------------------------------
1047; FUNCTION CODE
1048----------------------------------------------------------------------------*/
1049
1050void envelope_application_LC(Int32  *aBufR,
1051                             Int32  *nrg_gain_man,
1052                             Int32  *nrg_gain_exp,
1053                             Int32  *noise_level_man,
1054                             Int32  *noise_level_exp,
1055                             Int32  *nrg_tone_man,
1056                             Int32  *nrg_tone_exp,
1057                             Int32  band_nrg_tone_detector,
1058                             const Int32 *frame_info,
1059                             Int32  *harm_index,
1060                             Int32  *phase_index,
1061                             Int32  i,
1062                             Int32  lowSubband,
1063                             Int32  noSubbands,
1064                             Int32  noNoiseFlag)
1065{
1066
1067    Int32 *ptrReal;
1068    Int32 sb_gain_man;
1069    Int32 sb_noise_man;
1070    Int32 sb_noise_exp;
1071    Int32 l;
1072    Int32 k;
1073    Int32 tmp_q1;
1074    Int32 tmp_q2;
1075    Int32 tone_count;
1076    Int16 tmp_16;
1077    Int32 indexMinus1;
1078    Int32 indexPlus1;
1079
1080
1081    /*
1082     *          Application
1083     */
1084
1085    if (band_nrg_tone_detector)     /* Add tone energy only if energy is detected  */
1086    {
1087
1088        /*
1089         *  pre-calculate tone application
1090         */
1091        for (k = 0; k < noSubbands; k++)
1092        {
1093            tmp_q2 = (-nrg_tone_exp[k]);
1094            tmp_q1 = nrg_tone_man[k];
1095            tmp_q2 = tmp_q1 >> tmp_q2;
1096            tmp_q1 = fxp_mul32_by_16(tmp_q2, Q15fmt(0.0163f));
1097            nrg_tone_man[k] = tmp_q2;
1098            nrg_tone_exp[k] = tmp_q1;
1099            noise_level_exp[k] += 1;
1100            nrg_gain_exp[k] += 28;
1101        }
1102
1103        for (l = (frame_info[1+i] << 1); l < (frame_info[2+i] << 1); l++)
1104        {
1105            ptrReal = (aBufR + l * SBR_NUM_BANDS);
1106
1107            tone_count = 0;
1108
1109            indexPlus1  = (*harm_index + 1) & 3;
1110
1111            if (indexPlus1 & 1)    /*  if indexPlus1 is odd */
1112            {
1113                for (k = 0; k < noSubbands; k++)
1114                {
1115
1116                    sb_gain_man = nrg_gain_man[k];
1117                    tmp_q1 = *ptrReal;
1118                    tmp_q2 = nrg_gain_exp[k];
1119                    tmp_q1 = fxp_mul32_Q28(tmp_q1, sb_gain_man);
1120
1121                    if (tmp_q2 < 0)
1122                    {
1123                        if (tmp_q2 > -32)
1124                        {
1125                            *ptrReal = tmp_q1 >> (-tmp_q2);
1126                        }
1127                    }
1128                    else
1129                    {
1130                        *ptrReal = tmp_q1 << tmp_q2;
1131                    }
1132
1133                    *phase_index = (*phase_index + 1) & 511;
1134
1135                    if (!nrg_tone_man[k] && !noNoiseFlag)
1136
1137                    {
1138                        tmp_16 = rP_LCx[*phase_index];
1139                        sb_noise_man = noise_level_man[k];
1140                        sb_noise_exp = noise_level_exp[k];
1141
1142                        tmp_q1 = fxp_mul32_by_16(sb_noise_man, tmp_16);
1143
1144                        if (sb_noise_exp < 0)
1145                        {
1146                            if (sb_noise_exp > -32)
1147                            {
1148                                *ptrReal += tmp_q1 >> (-sb_noise_exp);
1149                            }
1150                        }
1151                        else
1152                        {
1153                            *ptrReal += tmp_q1 << sb_noise_exp;
1154                        }
1155                    }
1156
1157                    tmp_q1 = nrg_tone_man[k];
1158
1159                    if (*harm_index)
1160                    {
1161                        *ptrReal -= tmp_q1;
1162                    }
1163                    else
1164                    {
1165                        *ptrReal += tmp_q1;
1166                    }
1167
1168                    if (tmp_q1)
1169                    {
1170                        tone_count++;
1171                    }
1172
1173                    ptrReal++;
1174
1175                }   /*  for-loop (k) */
1176
1177            }
1178            else        /*  if indexPlus1 is even */
1179            {
1180                indexMinus1 = (*harm_index - 1) & 3;
1181
1182                /*  ---  k = 0  ----- */
1183
1184                sb_gain_man = nrg_gain_man[0];
1185                tmp_q1 = *ptrReal;
1186                tmp_q2 = nrg_gain_exp[0];
1187                tmp_q1 = fxp_mul32_Q28(tmp_q1, sb_gain_man);
1188
1189                if (tmp_q2 < 0)
1190                {
1191                    if (tmp_q2 > -32)
1192                    {
1193                        *ptrReal = tmp_q1 >> (-tmp_q2);
1194                    }
1195                }
1196                else
1197                {
1198                    *ptrReal = tmp_q1 << tmp_q2;
1199                }
1200
1201                *phase_index = (*phase_index + 1) & 511;
1202
1203                tmp_q1 = nrg_tone_exp[0];
1204                tmp_q2 = nrg_tone_exp[1];
1205
1206                if ((indexPlus1 != 0) ^((lowSubband & 1) != 0))
1207                {
1208                    *(ptrReal - 1) -= tmp_q1;
1209                    *(ptrReal)   += tmp_q2;
1210                }
1211                else
1212                {
1213                    *(ptrReal - 1) += tmp_q1;
1214                    *(ptrReal)   -= tmp_q2;
1215                }
1216
1217                if (!nrg_tone_man[0] && !noNoiseFlag)
1218                {
1219                    tmp_16 = rP_LCx[*phase_index];
1220                    sb_noise_man = noise_level_man[0];
1221                    sb_noise_exp = noise_level_exp[0];
1222
1223                    tmp_q1 = fxp_mul32_by_16(sb_noise_man, tmp_16);
1224
1225                    if (sb_noise_exp < 0)
1226                    {
1227                        if (sb_noise_exp > -32)
1228                        {
1229                            *ptrReal += tmp_q1 >> (-sb_noise_exp);
1230                        }
1231                    }
1232                    else
1233                    {
1234                        *ptrReal += tmp_q1 << sb_noise_exp;
1235                    }
1236                }
1237                else
1238                {
1239                    tone_count++;
1240                }
1241
1242                ptrReal++;
1243
1244                /* ----  */
1245
1246                for (k = 1; k < noSubbands - 1; k++)
1247                {
1248
1249                    sb_gain_man = nrg_gain_man[k];
1250                    tmp_q1 = *ptrReal;
1251                    tmp_q2 = nrg_gain_exp[k];
1252                    tmp_q1 = fxp_mul32_Q28(tmp_q1, sb_gain_man);
1253
1254                    if (tmp_q2 < 0)
1255                    {
1256                        if (tmp_q2 > -32)
1257                        {
1258                            *ptrReal = tmp_q1 >> (-tmp_q2);
1259                        }
1260                    }
1261                    else
1262                    {
1263                        *ptrReal = tmp_q1 << tmp_q2;
1264                    }
1265
1266                    *phase_index = (*phase_index + 1) & 511;
1267
1268
1269                    if (tone_count < 16)
1270                    {
1271                        tmp_q1 = nrg_tone_exp[k - 1];
1272                        tmp_q2 = nrg_tone_exp[k + 1];
1273
1274                        tmp_q1 -= tmp_q2;
1275
1276
1277                        if ((indexPlus1 != 0) ^(((k + lowSubband) & 1) != 0))
1278                        {
1279                            *(ptrReal) -= tmp_q1;
1280                        }
1281                        else
1282                        {
1283                            *(ptrReal) += tmp_q1;
1284                        }
1285                    }   /*   if (tone_count < 16)  */
1286
1287
1288                    if (!nrg_tone_man[k] && !noNoiseFlag)
1289                    {
1290                        tmp_16 = rP_LCx[*phase_index];
1291                        sb_noise_man = noise_level_man[k];
1292                        sb_noise_exp = noise_level_exp[k];
1293
1294                        tmp_q1 = fxp_mul32_by_16(sb_noise_man, tmp_16);
1295
1296                        if (sb_noise_exp < 0)
1297                        {
1298                            if (sb_noise_exp > -32)
1299                            {
1300                                *ptrReal += tmp_q1 >> (-sb_noise_exp);
1301                            }
1302                        }
1303                        else
1304                        {
1305                            *ptrReal += tmp_q1 << sb_noise_exp;
1306                        }
1307                    }
1308                    else
1309                    {
1310                        tone_count++;
1311                    }
1312
1313                    ptrReal++;
1314
1315                }   /*  for-loop (k) */
1316
1317                sb_gain_man = nrg_gain_man[k];
1318                tmp_q1 = *ptrReal;
1319                tmp_q2 = nrg_gain_exp[k];
1320                tmp_q1 = fxp_mul32_Q28(tmp_q1, sb_gain_man);
1321
1322                if (tmp_q2 < 0)
1323                {
1324                    if (tmp_q2 > -31)
1325                    {
1326                        *ptrReal = tmp_q1 >> (-tmp_q2);
1327                    }
1328                }
1329                else
1330                {
1331                    *ptrReal = tmp_q1 << tmp_q2;
1332                }
1333
1334                *phase_index = (*phase_index + 1) & 511;
1335
1336
1337                if ((tone_count < 16) && !(indexMinus1 &1))
1338                {
1339                    tmp_q1 = nrg_tone_exp[k - 1];
1340                    tmp_q2 = nrg_tone_exp[k    ];
1341
1342                    if ((indexMinus1 != 0) ^(((k + lowSubband) & 1) != 0))
1343                    {
1344                        *(ptrReal)   += tmp_q1;
1345
1346                        if (k + lowSubband < 62)
1347                        {
1348                            *(ptrReal + 1) -= tmp_q2;
1349                        }
1350                    }
1351                    else
1352                    {
1353                        *(ptrReal)   -= tmp_q1;
1354
1355                        if (k + lowSubband < 62)
1356                        {
1357                            *(ptrReal + 1) += tmp_q2;
1358                        }
1359                    }
1360                }   /*   if (tone_count < 16)  */
1361
1362
1363                if (!nrg_tone_man[k] && !noNoiseFlag)
1364                {
1365                    tmp_16 = rP_LCx[*phase_index];
1366                    sb_noise_man = noise_level_man[k];
1367                    sb_noise_exp = noise_level_exp[k];
1368
1369                    tmp_q1 = fxp_mul32_by_16(sb_noise_man, tmp_16);
1370
1371                    if (sb_noise_exp < 0)
1372                    {
1373                        if (sb_noise_exp > -31)
1374                        {
1375                            *ptrReal += tmp_q1 >> (-sb_noise_exp);
1376                        }
1377                    }
1378                    else
1379                    {
1380                        *ptrReal += tmp_q1 << sb_noise_exp;
1381                    }
1382                }
1383
1384            }   /*  if indexPlus1 is odd */
1385
1386            *harm_index = indexPlus1;
1387
1388
1389        }        /*  for-loop (l) */
1390
1391    }
1392    else        /*   if ( band_nrg_tone_detector)   */
1393    {
1394
1395        for (k = 0; k < noSubbands; k++)
1396        {
1397            tmp_q1 = noise_level_exp[k];
1398            tmp_q2 = nrg_gain_exp[k];
1399            noise_level_exp[k] =  tmp_q1 + 1;
1400            nrg_gain_exp[k] = tmp_q2 + 28;
1401        }
1402
1403        for (l = (frame_info[1+i] << 1); l < (frame_info[2+i] << 1); l++)
1404        {
1405            ptrReal = (aBufR + l * SBR_NUM_BANDS);
1406
1407            for (k = 0; k < noSubbands; k++)
1408            {
1409
1410                tmp_q1 = *ptrReal;
1411                sb_gain_man = nrg_gain_man[k];
1412
1413                tmp_q2 = nrg_gain_exp[k];
1414
1415                tmp_q1 = fxp_mul32_Q28(tmp_q1, sb_gain_man);
1416
1417                if (tmp_q2 < 0)
1418                {
1419                    if (tmp_q2 > -31)
1420                    {
1421                        *ptrReal = tmp_q1 >> (-tmp_q2);
1422                    }
1423                }
1424                else
1425                {
1426                    *ptrReal = tmp_q1 << tmp_q2;
1427                }
1428
1429                *phase_index = (*phase_index + 1) & 511;
1430
1431                if (! noNoiseFlag)
1432                {
1433                    tmp_16 = rP_LCx[*phase_index];
1434                    sb_noise_man = noise_level_man[k];
1435                    sb_noise_exp = noise_level_exp[k];
1436
1437                    tmp_q1 = fxp_mul32_by_16(sb_noise_man, tmp_16);
1438
1439                    if (sb_noise_exp < 0)
1440                    {
1441                        if (sb_noise_exp > -31)
1442                        {
1443                            *ptrReal += tmp_q1 >> (-sb_noise_exp);
1444                        }
1445                    }
1446                    else
1447                    {
1448                        *ptrReal += tmp_q1 << sb_noise_exp;
1449                    }
1450                }
1451
1452                ptrReal++;
1453
1454            }   /*  for-loop (k) */
1455
1456            *harm_index  = (*harm_index + 1) & 3;
1457
1458
1459        }   /*  for-loop (l) */
1460
1461    }
1462
1463}
1464
1465
1466
1467/*----------------------------------------------------------------------------
1468; FUNCTION CODE
1469----------------------------------------------------------------------------*/
1470
1471
1472#define Qfmt15(a)   (Int32)(a*((Int32)1<<15) + (a>=0?0.5F:-0.5F))
1473
1474
1475const Int16 pow2[39] = { 0, 0, 1, 0, 2,
1476                         0, Qfmt15(2 / 6.0f), 0, 3, 0, Qfmt15(2 / 10.0f), 0, Qfmt15(2 / 12.0f), 0, Qfmt15(2 / 14.0f), 0, 4,
1477                         0, Qfmt15(2 / 18.0f),    0, Qfmt15(2 / 20.0f), 0, Qfmt15(2 / 22.0f), 0, Qfmt15(2 / 24.0f),
1478                         0, Qfmt15(2 / 26.0f), 0, Qfmt15(2 / 28.0f), 0, Qfmt15(2 / 30.0f), 0, 5, 0, Qfmt15(2 / 34.0f),
1479                         0, Qfmt15(2 / 36.0f), 0, Qfmt15(2 / 38.0f)
1480                       };
1481
1482void energy_estimation_LC(Int32 *aBufR,
1483                          Int32 *nrg_est_man,
1484                          Int32 *nrg_est_exp,
1485                          const Int32 *frame_info,
1486                          Int32 i,
1487                          Int32 k,
1488                          Int32 c,
1489                          Int32 ui2)
1490{
1491
1492
1493    Int32  aux1;
1494    Int32  aux2;
1495    Int32  l;
1496
1497
1498    int64_t nrg_h = 0;
1499    Int32 tmp1;
1500    UInt32 tmp2;
1501
1502    for (l = ui2; l < (frame_info[2+i] << 1); l++)
1503    {
1504
1505        aux1 = aBufR[l++*SBR_NUM_BANDS + k ];
1506        aux2 = aBufR[l  *SBR_NUM_BANDS + k ];
1507
1508        nrg_h = fxp_mac64_Q31(nrg_h, aux1, aux1);
1509        nrg_h = fxp_mac64_Q31(nrg_h, aux2, aux2);
1510    }
1511
1512    /*
1513     *  Check for overflow and saturate if needed
1514     */
1515    if (nrg_h < 0)
1516    {
1517        nrg_h = 0x7fffffff;
1518    }
1519
1520
1521    if (nrg_h)
1522    {
1523        tmp2 = (UInt32)(nrg_h >> 32);
1524        if (tmp2)
1525        {
1526            aux2 = pv_normalize(tmp2);
1527            aux2 -= 1;                  /*  ensure Q30 */
1528            nrg_h = (nrg_h << aux2) >> 33;
1529            tmp2 = (UInt32)(nrg_h);
1530            nrg_est_exp[c] = 33 - aux2;
1531        }
1532        else
1533        {
1534            tmp2 = (UInt32)(nrg_h >> 2);
1535            aux2 = pv_normalize(tmp2);
1536            aux2 -= 1;                  /*  ensure Q30 */
1537
1538            tmp2 = (tmp2 << aux2);
1539            nrg_est_exp[c] =  -aux2 + 2;
1540        }
1541
1542        tmp1 = (l - ui2);
1543
1544        aux2 = pow2[tmp1];
1545        if (tmp1 == (tmp1 & (-tmp1)))
1546        {
1547            nrg_est_man[c] = tmp2 >> aux2;
1548        }
1549        else
1550        {
1551            nrg_est_man[c] = fxp_mul32_by_16(tmp2, aux2);
1552        }
1553
1554    }
1555    else
1556    {
1557        nrg_est_man[c] = 0;
1558        nrg_est_exp[c] = -100;
1559    }
1560
1561
1562
1563
1564
1565}
1566
1567
1568
1569
1570
1571
1572#if HQ_SBR
1573
1574/*----------------------------------------------------------------------------
1575; FUNCTION CODE
1576----------------------------------------------------------------------------*/
1577
1578void envelope_application(Int32  *aBufR,
1579                          Int32  *aBufI,
1580                          Int32  *nrg_gain_man,
1581                          Int32  *nrg_gain_exp,
1582                          Int32  *noise_level_man,
1583                          Int32  *noise_level_exp,
1584                          Int32  *nrg_tone_man,
1585                          Int32  *nrg_tone_exp,
1586                          Int32  *fBuf_man[64],
1587                          Int32  *fBuf_exp[64],
1588                          Int32  *fBufN_man[64],
1589                          Int32  *fBufN_exp[64],
1590                          const  Int32 *frame_info,
1591                          Int32  *harm_index,
1592                          Int32  *phase_index,
1593                          Int32  i,
1594                          Int32  lowSubband,
1595                          Int32  noSubbands,
1596                          Int32  noNoiseFlag,
1597                          Int32  band_nrg_tone_detector,
1598                          Int32  maxSmoothLength,
1599                          Int32  smooth_length)
1600{
1601
1602    Int32 *ptrReal;
1603    Int32 *ptrImag;
1604    Int32 sb_gain_man;
1605    Int32 sb_gain_exp;
1606    Int32 sb_noise_man;
1607    Int32 sb_noise_exp;
1608    Int32 l;
1609    Int32 k;
1610    Int32 n;
1611    Int32 tmp_q1;
1612    Int32 tmp_q2;
1613    Int32  aux1;
1614    Int32  aux2;
1615    Int32  filter_history = 0;
1616
1617
1618    if (band_nrg_tone_detector)     /* Add tone energy only if energy is detected  */
1619    {
1620
1621        /*
1622         *  pre-calculate tone application
1623         */
1624
1625        ptrReal = nrg_tone_exp;
1626        ptrImag = nrg_tone_man;
1627        tmp_q1 = - *(ptrReal++);
1628        aux1   =   *(ptrImag);
1629        for (k = 0; k < noSubbands; k++)
1630        {
1631            *(ptrImag++) = aux1 >> tmp_q1;
1632            tmp_q1 = - *(ptrReal++);
1633            aux1   =   *(ptrImag);
1634        }
1635
1636        /*
1637         *          Application
1638         */
1639
1640        for (l = (frame_info[1+i] << 1); l < (frame_info[2+i] << 1); l++)
1641        {
1642            ptrReal = (aBufR + l * SBR_NUM_BANDS);
1643            ptrImag = (aBufI + l * SBR_NUM_BANDS);
1644
1645            if (filter_history <= maxSmoothLength)     /* no more update is needed as buffer will have same info */
1646            {
1647                pv_memmove(fBuf_man[maxSmoothLength], nrg_gain_man, noSubbands*sizeof(*nrg_gain_man));
1648                pv_memmove(fBuf_exp[maxSmoothLength], nrg_gain_exp, noSubbands*sizeof(*nrg_gain_exp));
1649                pv_memmove(fBufN_man[maxSmoothLength], noise_level_man, noSubbands*sizeof(*noise_level_man));
1650                pv_memmove(fBufN_exp[maxSmoothLength], noise_level_exp, noSubbands*sizeof(*noise_level_exp));
1651            }
1652
1653            /*
1654             *  nrg_gain_max bounded to 1.584893192*1e5, which requires (32-bit) Q14 notation
1655             */
1656            for (k = 0; k < noSubbands; k++)
1657            {
1658                if (smooth_length == 0)     /* no filter-smooth needed */
1659                {
1660                    sb_gain_man = nrg_gain_man[k];
1661                    sb_gain_exp = nrg_gain_exp[k];
1662
1663                    sb_noise_man = noise_level_man[k];
1664                    sb_noise_exp = noise_level_exp[k];
1665
1666                }
1667                else
1668                {   /* else  smooth_length == 4  and fir_4 filter is being used */
1669
1670                    sb_gain_exp = fBuf_exp[maxSmoothLength][k];
1671
1672                    sb_noise_exp = fBufN_exp[maxSmoothLength][k];
1673
1674                    for (n = maxSmoothLength - smooth_length; n < maxSmoothLength; n++)
1675                    {
1676                        if (sb_gain_exp  < fBuf_exp[n][k])
1677                        {
1678                            sb_gain_exp = fBuf_exp[n][k];
1679                        }
1680
1681                        if (sb_noise_exp  < fBufN_exp[n][k])
1682                        {
1683                            sb_noise_exp = fBufN_exp[n][k];
1684                        }
1685                    }
1686
1687                    sb_gain_man = fxp_mul32_Q30(fBuf_man[maxSmoothLength][k], Q30fmt(0.33333333333333f));
1688                    sb_gain_man  = sb_gain_man >> (sb_gain_exp - fBuf_exp[maxSmoothLength][k]);
1689
1690                    sb_noise_man = fxp_mul32_Q30(fBufN_man[maxSmoothLength][k], Q30fmt(0.33333333333333f));
1691                    sb_noise_man = sb_noise_man >> (sb_noise_exp - fBufN_exp[maxSmoothLength][k]);
1692
1693                    n = maxSmoothLength - smooth_length;
1694
1695                    tmp_q1 = fxp_mul32_Q30(fBuf_man[n][k], Q30fmt(0.03183050093751f));
1696                    sb_gain_man  += tmp_q1 >> (sb_gain_exp - fBuf_exp[n][k]);
1697
1698                    tmp_q1 = fxp_mul32_Q30(fBufN_man[n][k], Q30fmt(0.03183050093751f));
1699                    sb_noise_man += tmp_q1 >> (sb_noise_exp - fBufN_exp[n++][k]);
1700
1701                    tmp_q1 = fxp_mul32_Q30(fBuf_man[n][k], Q30fmt(0.11516383427084f));
1702                    sb_gain_man  += tmp_q1 >> (sb_gain_exp - fBuf_exp[n][k]);
1703
1704                    tmp_q1 = fxp_mul32_Q30(fBufN_man[n][k], Q30fmt(0.11516383427084f));
1705                    sb_noise_man += tmp_q1 >> (sb_noise_exp - fBufN_exp[n++][k]);
1706
1707                    tmp_q1 = fxp_mul32_Q30(fBuf_man[n][k], Q30fmt(0.21816949906249f));
1708                    sb_gain_man  += tmp_q1 >> (sb_gain_exp - fBuf_exp[n][k]);
1709
1710                    tmp_q1 = fxp_mul32_Q30(fBufN_man[n][k], Q30fmt(0.21816949906249f));
1711                    sb_noise_man += tmp_q1 >> (sb_noise_exp - fBufN_exp[n++][k]);
1712
1713                    tmp_q1 = fxp_mul32_Q30(fBuf_man[n][k], Q30fmt(0.30150283239582f));
1714                    sb_gain_man  += tmp_q1 >> (sb_gain_exp - fBuf_exp[n][k]);
1715
1716                    tmp_q1 = fxp_mul32_Q30(fBufN_man[n][k], Q30fmt(0.30150283239582f));
1717                    sb_noise_man += tmp_q1 >> (sb_noise_exp - fBufN_exp[n][k]);
1718
1719                }
1720
1721
1722
1723                /*
1724                 *    *ptrReal  = *ptrReal * sb_gain ;
1725                 *    *ptrImag  = *ptrImag * sb_gain;
1726                 */
1727                aux1 = *ptrReal;
1728                aux2 = *ptrImag;
1729                sb_gain_exp += 32;
1730                aux1 = fxp_mul32_Q31(aux1, sb_gain_man);
1731                aux2 = fxp_mul32_Q31(aux2, sb_gain_man);
1732
1733
1734                if (sb_gain_exp < 0)
1735                {
1736                    sb_gain_exp = -sb_gain_exp;
1737                    if (sb_gain_exp < 32)
1738                    {
1739                        *ptrReal = (aux1 >> sb_gain_exp);
1740                        *ptrImag = (aux2 >> sb_gain_exp);
1741                    }
1742                }
1743                else
1744                {
1745                    *ptrReal = (aux1 << sb_gain_exp);
1746                    *ptrImag = (aux2 << sb_gain_exp);
1747                }
1748
1749
1750
1751                /*
1752                 *     if ( sb_noise != 0)
1753                 *     {
1754                 *         *ptrReal += sb_noise * rP[*phase_index][0];
1755                 *         *ptrImag += sb_noise * rP[*phase_index][1];
1756                 *     }
1757                 */
1758                *phase_index = (*phase_index + 1) & 511;
1759
1760                if (nrg_tone_man[k] || noNoiseFlag)
1761                {
1762                    sb_noise_man = 0;
1763                    sb_noise_exp = 0;
1764                }
1765                else
1766                {
1767
1768                    Int32 tmp = rPxx[*phase_index];
1769                    sb_noise_exp += 1;
1770                    tmp_q1 = fxp_mul32_by_16t(sb_noise_man, tmp);
1771                    tmp_q2 = fxp_mul32_by_16b(sb_noise_man, tmp);
1772
1773
1774                    if (sb_noise_exp < 0)
1775                    {
1776                        if (sb_noise_exp > -32)
1777                        {
1778                            *ptrReal += tmp_q1 >> (-sb_noise_exp);
1779                            *ptrImag += tmp_q2 >> (-sb_noise_exp);
1780                        }
1781                    }
1782                    else
1783                    {
1784                        *ptrReal += tmp_q1 << sb_noise_exp;
1785                        *ptrImag += tmp_q2 << sb_noise_exp;
1786                    }
1787                }
1788
1789                /*
1790                 *      tmp_q1 = nrg_tone[k]
1791                 */
1792
1793                tmp_q1 = nrg_tone_man[k];
1794
1795                if (*harm_index & 1)
1796                {
1797                    if ((((k + lowSubband) & 1) != 0) ^(*harm_index != 1))
1798                    {
1799                        *ptrImag  -=  tmp_q1;
1800                    }
1801                    else
1802                    {
1803                        *ptrImag  +=  tmp_q1;
1804                    }
1805                }
1806                else
1807                {
1808                    *ptrReal += (*harm_index) ? -tmp_q1 : tmp_q1;
1809                }
1810
1811                *ptrReal++ <<= 10;
1812                *ptrImag++ <<= 10;
1813
1814
1815            }    /*  for-loop (k) */
1816
1817
1818            *harm_index = (*harm_index + 1) & 3;
1819
1820            /*
1821             *  Update smoothing filter history
1822             */
1823
1824            if (filter_history++ < maxSmoothLength)     /* no more update is needed as buffer will have same info */
1825            {
1826                /*
1827                 *  mantissas
1828                 */
1829
1830                ptrReal = (Int32 *)fBuf_man[0];
1831                ptrImag = (Int32 *)fBufN_man[0];
1832
1833                for (n = 0; n < maxSmoothLength; n++)
1834                {
1835                    fBuf_man[n]  = fBuf_man[n+1];
1836                    fBufN_man[n] = fBufN_man[n+1];
1837                }
1838
1839                fBuf_man[maxSmoothLength]  = ptrReal;
1840                fBufN_man[maxSmoothLength] = ptrImag;
1841
1842                /*
1843                 *  exponents
1844                 */
1845                ptrReal = (Int32 *)fBuf_exp[0];
1846                ptrImag = (Int32 *)fBufN_exp[0];
1847
1848                for (n = 0; n < maxSmoothLength; n++)
1849                {
1850                    fBuf_exp[n]  = fBuf_exp[n+1];
1851                    fBufN_exp[n] = fBufN_exp[n+1];
1852                }
1853
1854                fBuf_exp[maxSmoothLength]  = ptrReal;
1855                fBufN_exp[maxSmoothLength] = ptrImag;
1856            }
1857
1858        }   /*  for-loop (l) */
1859
1860
1861    }
1862    else        /*   ----  if ( band_nrg_tone_detector) ---- */
1863    {
1864
1865        /*
1866         *          Application
1867         */
1868
1869        for (l = (frame_info[1+i] << 1); l < (frame_info[2+i] << 1); l++)
1870        {
1871            ptrReal = (aBufR + l * SBR_NUM_BANDS);
1872            ptrImag = (aBufI + l * SBR_NUM_BANDS);
1873
1874            if (filter_history <= maxSmoothLength)     /* no more update is needed as buffer will have same info */
1875            {
1876                pv_memmove(fBuf_man[maxSmoothLength], nrg_gain_man, noSubbands*sizeof(*nrg_gain_man));
1877                pv_memmove(fBuf_exp[maxSmoothLength], nrg_gain_exp, noSubbands*sizeof(*nrg_gain_exp));
1878                pv_memmove(fBufN_man[maxSmoothLength], noise_level_man, noSubbands*sizeof(*noise_level_man));
1879                pv_memmove(fBufN_exp[maxSmoothLength], noise_level_exp, noSubbands*sizeof(*noise_level_exp));
1880            }
1881
1882            /*
1883             *  nrg_gain_max bounded to 1.584893192*1e5, which requires (32-bit) Q14 notation
1884             */
1885            for (k = 0; k < noSubbands; k++)
1886            {
1887                if (smooth_length == 0)     /* no filter-smooth needed */
1888                {
1889                    sb_gain_man = nrg_gain_man[k];
1890                    sb_gain_exp = nrg_gain_exp[k];
1891
1892                    sb_noise_man = noise_level_man[k];
1893                    sb_noise_exp = noise_level_exp[k];
1894
1895                }
1896                else
1897                {   /* else  smooth_length == 4  and fir_4 filter is being used */
1898
1899                    sb_gain_exp = fBuf_exp[maxSmoothLength][k];
1900
1901                    sb_noise_exp = fBufN_exp[maxSmoothLength][k];
1902
1903                    for (n = maxSmoothLength - smooth_length; n < maxSmoothLength; n++)
1904                    {
1905                        if (sb_gain_exp  < fBuf_exp[n][k])
1906                        {
1907                            sb_gain_exp = fBuf_exp[n][k];
1908                        }
1909
1910                        if (sb_noise_exp  < fBufN_exp[n][k])
1911                        {
1912                            sb_noise_exp = fBufN_exp[n][k];
1913                        }
1914                    }
1915
1916                    sb_gain_man = fxp_mul32_Q30(fBuf_man[maxSmoothLength][k], Q30fmt(0.33333333333333f));
1917                    sb_gain_man  = sb_gain_man >> (sb_gain_exp - fBuf_exp[maxSmoothLength][k]);
1918
1919                    sb_noise_man = fxp_mul32_Q30(fBufN_man[maxSmoothLength][k], Q30fmt(0.33333333333333f));
1920                    sb_noise_man = sb_noise_man >> (sb_noise_exp - fBufN_exp[maxSmoothLength][k]);
1921
1922                    n = maxSmoothLength - smooth_length;
1923
1924                    tmp_q1 = fxp_mul32_Q30(fBuf_man[n][k], Q30fmt(0.03183050093751f));
1925                    sb_gain_man  += tmp_q1 >> (sb_gain_exp - fBuf_exp[n][k]);
1926
1927                    tmp_q1 = fxp_mul32_Q30(fBufN_man[n][k], Q30fmt(0.03183050093751f));
1928                    sb_noise_man += tmp_q1 >> (sb_noise_exp - fBufN_exp[n++][k]);
1929
1930                    tmp_q1 = fxp_mul32_Q30(fBuf_man[n][k], Q30fmt(0.11516383427084f));
1931                    sb_gain_man  += tmp_q1 >> (sb_gain_exp - fBuf_exp[n][k]);
1932
1933                    tmp_q1 = fxp_mul32_Q30(fBufN_man[n][k], Q30fmt(0.11516383427084f));
1934                    sb_noise_man += tmp_q1 >> (sb_noise_exp - fBufN_exp[n++][k]);
1935
1936                    tmp_q1 = fxp_mul32_Q30(fBuf_man[n][k], Q30fmt(0.21816949906249f));
1937                    sb_gain_man  += tmp_q1 >> (sb_gain_exp - fBuf_exp[n][k]);
1938
1939                    tmp_q1 = fxp_mul32_Q30(fBufN_man[n][k], Q30fmt(0.21816949906249f));
1940                    sb_noise_man += tmp_q1 >> (sb_noise_exp - fBufN_exp[n++][k]);
1941
1942                    tmp_q1 = fxp_mul32_Q30(fBuf_man[n][k], Q30fmt(0.30150283239582f));
1943                    sb_gain_man  += tmp_q1 >> (sb_gain_exp - fBuf_exp[n][k]);
1944
1945                    tmp_q1 = fxp_mul32_Q30(fBufN_man[n][k], Q30fmt(0.30150283239582f));
1946                    sb_noise_man += tmp_q1 >> (sb_noise_exp - fBufN_exp[n][k]);
1947
1948                }
1949
1950
1951
1952                /*
1953                 *    *ptrReal  = *ptrReal * sb_gain ;
1954                 *    *ptrImag  = *ptrImag * sb_gain;
1955                 */
1956                aux1 = *ptrReal;
1957                aux2 = *ptrImag;
1958                sb_gain_exp += 32;
1959                aux1 = fxp_mul32_Q31(aux1, sb_gain_man);
1960                aux2 = fxp_mul32_Q31(aux2, sb_gain_man);
1961
1962
1963
1964                /*
1965                 *     if ( sb_noise != 0)
1966                 *     {
1967                 *         *ptrReal += sb_noise * rP[*phase_index][0];
1968                 *         *ptrImag += sb_noise * rP[*phase_index][1];
1969                 *     }
1970                 */
1971
1972
1973                if (sb_gain_exp < 0)
1974                {
1975                    if (sb_gain_exp > -32)
1976                    {
1977                        if (sb_gain_exp > -10)
1978                        {
1979                            *ptrReal = aux1 << (10 + sb_gain_exp);
1980                            *ptrImag = aux2 << (10 + sb_gain_exp);
1981                        }
1982                        else
1983                        {
1984                            *ptrReal = aux1 >> (-sb_gain_exp - 10);
1985                            *ptrImag = aux2 >> (-sb_gain_exp - 10);
1986                        }
1987                    }
1988                }
1989                else
1990                {
1991                    *ptrReal = aux1 << (sb_gain_exp + 10);
1992                    *ptrImag = aux2 << (sb_gain_exp + 10);
1993                }
1994
1995
1996
1997
1998                /*
1999                 *     if ( sb_noise != 0)
2000                 *     {
2001                 *         *ptrReal += sb_noise * rP[*phase_index][0];
2002                 *         *ptrImag += sb_noise * rP[*phase_index][1];
2003                 *     }
2004                 */
2005                *phase_index = (*phase_index + 1) & 511;
2006
2007                if (!noNoiseFlag)
2008                {
2009
2010                    Int32 tmp = rPxx[*phase_index];
2011                    sb_noise_exp += 1;
2012                    tmp_q1 = fxp_mul32_by_16t(sb_noise_man, tmp);
2013                    tmp_q2 = fxp_mul32_by_16b(sb_noise_man, tmp);
2014
2015                    if (sb_noise_exp < 0)
2016                    {
2017                        if (sb_noise_exp > -32)
2018                        {
2019                            if (sb_noise_exp > -10)
2020                            {
2021                                *ptrReal += tmp_q1 << (10 + sb_noise_exp);
2022                                *ptrImag += tmp_q2 << (10 + sb_noise_exp);
2023                            }
2024                            else
2025                            {
2026                                *ptrReal += tmp_q1 >> (-sb_noise_exp - 10);
2027                                *ptrImag += tmp_q2 >> (-sb_noise_exp - 10);
2028                            }
2029                        }
2030                    }
2031                    else
2032                    {
2033                        *ptrReal += tmp_q1 << (sb_noise_exp + 10);
2034                        *ptrImag += tmp_q2 << (sb_noise_exp + 10);
2035                    }
2036                }
2037
2038                ptrReal++;
2039                ptrImag++;
2040
2041
2042            }    /*  for-loop (k) */
2043
2044
2045            *harm_index = (*harm_index + 1) & 3;
2046
2047            /*
2048             *  Update smoothing filter history
2049             */
2050
2051            if (filter_history++ < maxSmoothLength)     /* no more update is needed as buffer will have same info */
2052            {
2053                /*
2054                 *  mantissas
2055                 */
2056
2057                ptrReal = (Int32 *)fBuf_man[0];
2058                ptrImag = (Int32 *)fBufN_man[0];
2059
2060                for (n = 0; n < maxSmoothLength; n++)
2061                {
2062                    fBuf_man[n]  = fBuf_man[n+1];
2063                    fBufN_man[n] = fBufN_man[n+1];
2064                }
2065
2066                fBuf_man[maxSmoothLength]  = ptrReal;
2067                fBufN_man[maxSmoothLength] = ptrImag;
2068
2069                /*
2070                 *  exponents
2071                 */
2072                ptrReal = (Int32 *)fBuf_exp[0];
2073                ptrImag = (Int32 *)fBufN_exp[0];
2074
2075                for (n = 0; n < maxSmoothLength; n++)
2076                {
2077                    fBuf_exp[n]  = fBuf_exp[n+1];
2078                    fBufN_exp[n] = fBufN_exp[n+1];
2079                }
2080
2081                fBuf_exp[maxSmoothLength]  = ptrReal;
2082                fBufN_exp[maxSmoothLength] = ptrImag;
2083            }
2084
2085        }   /*  for-loop (l) */
2086
2087    }       /*  if ( band_nrg_tone_detector) */
2088
2089}
2090
2091
2092/*----------------------------------------------------------------------------
2093; FUNCTION CODE
2094----------------------------------------------------------------------------*/
2095
2096void energy_estimation(Int32 *aBufR,
2097                       Int32 *aBufI,
2098                       Int32 *nrg_est_man,
2099                       Int32 *nrg_est_exp,
2100                       const Int32 *frame_info,
2101                       Int32 i,
2102                       Int32 k,
2103                       Int32 c,
2104                       Int32 ui2)
2105{
2106
2107    Int32  aux1;
2108    Int32  aux2;
2109    Int32  l;
2110
2111
2112
2113    int64_t nrg_h = 0;
2114    Int32 tmp1;
2115    Int32 tmp2;
2116
2117    aux1 = aBufR[ui2*SBR_NUM_BANDS + k];
2118    aux2 = aBufI[ui2*SBR_NUM_BANDS + k];
2119    for (l = ui2 + 1; l < (frame_info[2+i] << 1);  l++)
2120    {
2121        nrg_h = fxp_mac64_Q31(nrg_h, aux1, aux1);
2122        nrg_h = fxp_mac64_Q31(nrg_h, aux2, aux2);
2123        aux1 = aBufR[l*SBR_NUM_BANDS + k];
2124        aux2 = aBufI[l*SBR_NUM_BANDS + k];
2125    }
2126    nrg_h = fxp_mac64_Q31(nrg_h, aux1, aux1);
2127    nrg_h = fxp_mac64_Q31(nrg_h, aux2, aux2);
2128
2129
2130    /*
2131     *  Check for overflow and saturate if needed
2132     */
2133    if (nrg_h < 0)
2134    {
2135        nrg_h = 0x7fffffff;
2136    }
2137
2138    if (nrg_h)
2139    {
2140
2141        aux1 = (UInt32)(nrg_h >> 32);
2142        if (aux1)
2143        {
2144            aux2 = pv_normalize(aux1);
2145            if (aux2)
2146            {
2147                aux2 -= 1;                  /*  ensure Q30 */
2148                nrg_h = (nrg_h << aux2) >> 33;
2149                tmp2 = (UInt32)(nrg_h);
2150                nrg_est_exp[c] = 33 - aux2;
2151            }
2152            else
2153            {
2154                tmp2 = (UInt32)(aux1 >> 1);
2155                nrg_est_exp[c] = 33 ;
2156
2157
2158            }
2159        }
2160        else
2161        {
2162            aux1 = (UInt32)(nrg_h >> 1);
2163            aux2 = pv_normalize(aux1);
2164
2165            tmp2 = (aux1 << aux2);
2166            nrg_est_exp[c] =  -aux2 + 1;
2167
2168
2169        }
2170
2171
2172
2173        tmp1 = (l - ui2);
2174        aux2 = pow2[tmp1];
2175        if (tmp1 == (tmp1 & (-tmp1)))
2176        {
2177            nrg_est_man[c] = tmp2 >> aux2;
2178        }
2179        else
2180        {
2181            nrg_est_man[c] = fxp_mul32_by_16(tmp2, aux2);
2182        }
2183    }
2184    else
2185    {
2186        nrg_est_man[c] = 0;
2187        nrg_est_exp[c] = -100;
2188    }
2189
2190
2191}
2192
2193
2194
2195
2196
2197#endif
2198
2199
2200#endif
2201
2202
2203
2204