1 
2 /* -----------------------------------------------------------------------------------------------------------
3 Software License for The Fraunhofer FDK AAC Codec Library for Android
4 
5 � Copyright  1995 - 2013 Fraunhofer-Gesellschaft zur F�rderung der angewandten Forschung e.V.
6   All rights reserved.
7 
8  1.    INTRODUCTION
9 The Fraunhofer FDK AAC Codec Library for Android ("FDK AAC Codec") is software that implements
10 the MPEG Advanced Audio Coding ("AAC") encoding and decoding scheme for digital audio.
11 This FDK AAC Codec software is intended to be used on a wide variety of Android devices.
12 
13 AAC's HE-AAC and HE-AAC v2 versions are regarded as today's most efficient general perceptual
14 audio codecs. AAC-ELD is considered the best-performing full-bandwidth communications codec by
15 independent studies and is widely deployed. AAC has been standardized by ISO and IEC as part
16 of the MPEG specifications.
17 
18 Patent licenses for necessary patent claims for the FDK AAC Codec (including those of Fraunhofer)
19 may be obtained through Via Licensing (www.vialicensing.com) or through the respective patent owners
20 individually for the purpose of encoding or decoding bit streams in products that are compliant with
21 the ISO/IEC MPEG audio standards. Please note that most manufacturers of Android devices already license
22 these patent claims through Via Licensing or directly from the patent owners, and therefore FDK AAC Codec
23 software may already be covered under those patent licenses when it is used for those licensed purposes only.
24 
25 Commercially-licensed AAC software libraries, including floating-point versions with enhanced sound quality,
26 are also available from Fraunhofer. Users are encouraged to check the Fraunhofer website for additional
27 applications information and documentation.
28 
29 2.    COPYRIGHT LICENSE
30 
31 Redistribution and use in source and binary forms, with or without modification, are permitted without
32 payment of copyright license fees provided that you satisfy the following conditions:
33 
34 You must retain the complete text of this software license in redistributions of the FDK AAC Codec or
35 your modifications thereto in source code form.
36 
37 You must retain the complete text of this software license in the documentation and/or other materials
38 provided with redistributions of the FDK AAC Codec or your modifications thereto in binary form.
39 You must make available free of charge copies of the complete source code of the FDK AAC Codec and your
40 modifications thereto to recipients of copies in binary form.
41 
42 The name of Fraunhofer may not be used to endorse or promote products derived from this library without
43 prior written permission.
44 
45 You may not charge copyright license fees for anyone to use, copy or distribute the FDK AAC Codec
46 software or your modifications thereto.
47 
48 Your modified versions of the FDK AAC Codec must carry prominent notices stating that you changed the software
49 and the date of any change. For modified versions of the FDK AAC Codec, the term
50 "Fraunhofer FDK AAC Codec Library for Android" must be replaced by the term
51 "Third-Party Modified Version of the Fraunhofer FDK AAC Codec Library for Android."
52 
53 3.    NO PATENT LICENSE
54 
55 NO EXPRESS OR IMPLIED LICENSES TO ANY PATENT CLAIMS, including without limitation the patents of Fraunhofer,
56 ARE GRANTED BY THIS SOFTWARE LICENSE. Fraunhofer provides no warranty of patent non-infringement with
57 respect to this software.
58 
59 You may use this FDK AAC Codec software or modifications thereto only for purposes that are authorized
60 by appropriate patent licenses.
61 
62 4.    DISCLAIMER
63 
64 This FDK AAC Codec software is provided by Fraunhofer on behalf of the copyright holders and contributors
65 "AS IS" and WITHOUT ANY EXPRESS OR IMPLIED WARRANTIES, including but not limited to the implied warranties
66 of merchantability and fitness for a particular purpose. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR
67 CONTRIBUTORS BE LIABLE for any direct, indirect, incidental, special, exemplary, or consequential damages,
68 including but not limited to procurement of substitute goods or services; loss of use, data, or profits,
69 or business interruption, however caused and on any theory of liability, whether in contract, strict
70 liability, or tort (including negligence), arising in any way out of the use of this software, even if
71 advised of the possibility of such damage.
72 
73 5.    CONTACT INFORMATION
74 
75 Fraunhofer Institute for Integrated Circuits IIS
76 Attention: Audio and Multimedia Departments - FDK AAC LL
77 Am Wolfsmantel 33
78 91058 Erlangen, Germany
79 
80 www.iis.fraunhofer.de/amm
81 amm-info@iis.fraunhofer.de
82 ----------------------------------------------------------------------------------------------------------- */
83 
84 /*!
85   \file
86   \brief  Frequency scale calculation
87 */
88 
89 #include "sbrdec_freq_sca.h"
90 
91 #include "transcendent.h"
92 #include "sbr_rom.h"
93 #include "env_extr.h"
94 
95 #include "genericStds.h"      /* need log() for debug-code only */
96 
97 #define MAX_OCTAVE         29
98 #define MAX_SECOND_REGION  50
99 
100 
101 static int  numberOfBands(FIXP_SGL bpo_div16, int start, int stop, int warpFlag);
102 static void CalcBands(UCHAR * diff, UCHAR start, UCHAR stop, UCHAR num_bands);
103 static SBR_ERROR modifyBands(UCHAR max_band, UCHAR * diff, UCHAR length);
104 static void cumSum(UCHAR start_value, UCHAR* diff, UCHAR length, UCHAR *start_adress);
105 
106 
107 
108 /*!
109   \brief     Retrieve QMF-band where the SBR range starts
110 
111   Convert startFreq which was read from the bitstream into a
112   QMF-channel number.
113 
114   \return  Number of start band
115 */
116 static UCHAR
getStartBand(UINT fs,UCHAR startFreq,UINT headerDataFlags)117 getStartBand(UINT   fs,               /*!< Output sampling frequency */
118              UCHAR  startFreq,        /*!< Index to table of possible start bands */
119              UINT   headerDataFlags)  /*!< Info to SBR mode */
120 {
121   INT  band;
122   UINT fsMapped;
123 
124     fsMapped = fs;
125 
126   switch (fsMapped) {
127     case 96000:
128     case 88200:
129       band = FDK_sbrDecoder_sbr_start_freq_88[startFreq];
130       break;
131     case 64000:
132       band = FDK_sbrDecoder_sbr_start_freq_64[startFreq];
133       break;
134     case 48000:
135       band = FDK_sbrDecoder_sbr_start_freq_48[startFreq];
136       break;
137     case 44100:
138       band = FDK_sbrDecoder_sbr_start_freq_44[startFreq];
139       break;
140     case 32000:
141       band = FDK_sbrDecoder_sbr_start_freq_32[startFreq];
142       break;
143     case 24000:
144       band = FDK_sbrDecoder_sbr_start_freq_24[startFreq];
145       break;
146     case 22050:
147       band = FDK_sbrDecoder_sbr_start_freq_22[startFreq];
148       break;
149     case 16000:
150       band = FDK_sbrDecoder_sbr_start_freq_16[startFreq];
151       break;
152     default:
153       band = 255;
154   }
155 
156   return band;
157 }
158 
159 
160 /*!
161   \brief     Retrieve QMF-band where the SBR range starts
162 
163   Convert startFreq which was read from the bitstream into a
164   QMF-channel number.
165 
166   \return  Number of start band
167 */
168 static UCHAR
getStopBand(UINT fs,UCHAR stopFreq,UINT headerDataFlags,UCHAR k0)169 getStopBand(UINT   fs,               /*!< Output sampling frequency */
170             UCHAR  stopFreq,         /*!< Index to table of possible start bands */
171             UINT   headerDataFlags,  /*!< Info to SBR mode */
172             UCHAR  k0)               /*!< Start freq index */
173 {
174   UCHAR k2;
175 
176   if (stopFreq < 14) {
177     INT    stopMin;
178     UCHAR  diff_tot[MAX_OCTAVE + MAX_SECOND_REGION];
179     UCHAR *diff0 = diff_tot;
180     UCHAR *diff1 = diff_tot+MAX_OCTAVE;
181 
182     if (fs < 32000) {
183       stopMin = (((2*6000*2*(64)) / fs) + 1) >> 1;
184     }
185     else {
186       if (fs < 64000) {
187         stopMin = (((2*8000*2*(64)) / fs) + 1) >> 1;
188       }
189       else {
190         stopMin = (((2*10000*2*(64)) / fs) + 1) >> 1;
191       }
192     }
193 
194     /*
195       Choose a stop band between k1 and 64 depending on stopFreq (0..13),
196       based on a logarithmic scale.
197       The vectors diff0 and diff1 are used temporarily here.
198     */
199     CalcBands( diff0, stopMin, 64, 13);
200     shellsort( diff0, 13);
201     cumSum(stopMin, diff0, 13, diff1);
202     k2 = diff1[stopFreq];
203   }
204   else if (stopFreq==14)
205     k2 = 2*k0;
206   else
207     k2 = 3*k0;
208 
209   /* Limit to Nyquist */
210   if (k2 > (64))
211     k2 = (64);
212 
213 
214   /* Range checks */
215   /* 1 <= difference <= 48; 1 <= fs <= 96000 */
216   if ( ((k2 - k0) > MAX_FREQ_COEFFS) || (k2 <= k0) ) {
217     return 255;
218   }
219 
220   if (headerDataFlags & (SBRDEC_SYNTAX_USAC|SBRDEC_SYNTAX_RSVD50)) {
221     /* 1 <= difference <= 35; 42000 <= fs <= 96000 */
222     if ( (fs >= 42000) && ( (k2 - k0) > MAX_FREQ_COEFFS_FS44100 ) ) {
223       return 255;
224     }
225     /* 1 <= difference <= 32; 46009 <= fs <= 96000 */
226     if ( (fs >= 46009) && ( (k2 - k0) > MAX_FREQ_COEFFS_FS48000 ) ) {
227       return 255;
228     }
229   }
230   else {
231     /* 1 <= difference <= 35; fs == 44100 */
232     if ( (fs == 44100) && ( (k2 - k0) > MAX_FREQ_COEFFS_FS44100 ) ) {
233       return 255;
234     }
235     /* 1 <= difference <= 32; 48000 <= fs <= 96000 */
236     if ( (fs >= 48000) && ( (k2 - k0) > MAX_FREQ_COEFFS_FS48000 ) ) {
237       return 255;
238     }
239   }
240 
241   return k2;
242 }
243 
244 
245 /*!
246   \brief     Generates master frequency tables
247 
248   Frequency tables are calculated according to the selected domain
249   (linear/logarithmic) and granularity.
250   IEC 14496-3 4.6.18.3.2.1
251 
252   \return  errorCode, 0 if successful
253 */
254 SBR_ERROR
sbrdecUpdateFreqScale(UCHAR * v_k_master,UCHAR * numMaster,UINT fs,HANDLE_SBR_HEADER_DATA hHeaderData,UINT flags)255 sbrdecUpdateFreqScale(UCHAR * v_k_master,    /*!< Master table to be created */
256                       UCHAR *numMaster,      /*!< Number of entries in master table */
257                       UINT   fs,             /*!< SBR working sampling rate */
258                       HANDLE_SBR_HEADER_DATA hHeaderData, /*!< Control data from bitstream */
259                       UINT flags)
260 {
261   FIXP_SGL bpo_div16;        /* bands_per_octave divided by 16 */
262   INT      dk=0;
263 
264   /* Internal variables */
265   UCHAR  k0, k2, i;
266   UCHAR  num_bands0 = 0;
267   UCHAR  num_bands1 = 0;
268   UCHAR  diff_tot[MAX_OCTAVE + MAX_SECOND_REGION];
269   UCHAR *diff0 = diff_tot;
270   UCHAR *diff1 = diff_tot+MAX_OCTAVE;
271   INT    k2_achived;
272   INT    k2_diff;
273   INT    incr=0;
274 
275   /*
276     Determine start band
277   */
278   k0 = getStartBand(fs, hHeaderData->bs_data.startFreq, flags);
279   if (k0 == 255) {
280     return SBRDEC_UNSUPPORTED_CONFIG;
281   }
282 
283   /*
284     Determine stop band
285   */
286   k2 = getStopBand(fs, hHeaderData->bs_data.stopFreq, flags, k0);
287   if (k2 == 255) {
288     return SBRDEC_UNSUPPORTED_CONFIG;
289   }
290 
291   if(hHeaderData->bs_data.freqScale>0) { /* Bark */
292     INT k1;
293 
294     if(hHeaderData->bs_data.freqScale==1) {
295       bpo_div16 = FL2FXCONST_SGL(12.0f/16.0f);
296     }
297     else if(hHeaderData->bs_data.freqScale==2) {
298       bpo_div16 = FL2FXCONST_SGL(10.0f/16.0f);
299     }
300     else {
301       bpo_div16 =  FL2FXCONST_SGL(8.0f/16.0f);
302     }
303 
304 
305     if( 1000 * k2 > 2245 * k0 ) { /* Two or more regions */
306       k1 = 2*k0;
307 
308       num_bands0 = numberOfBands(bpo_div16, k0, k1, 0);
309       num_bands1 = numberOfBands(bpo_div16, k1, k2, hHeaderData->bs_data.alterScale );
310       if ( num_bands0 < 1) {
311         return SBRDEC_UNSUPPORTED_CONFIG;
312       }
313       if ( num_bands1 < 1 ) {
314         return SBRDEC_UNSUPPORTED_CONFIG;
315       }
316 
317       CalcBands(diff0, k0, k1, num_bands0);
318       shellsort( diff0, num_bands0);
319       if (diff0[0] == 0) {
320 #ifdef DEBUG_TOOLS
321 #endif
322         return SBRDEC_UNSUPPORTED_CONFIG;
323       }
324 
325       cumSum(k0, diff0, num_bands0, v_k_master);
326 
327       CalcBands(diff1, k1, k2, num_bands1);
328       shellsort( diff1, num_bands1);
329       if(diff0[num_bands0-1] > diff1[0]) {
330         SBR_ERROR err;
331 
332         err = modifyBands(diff0[num_bands0-1],diff1, num_bands1);
333         if (err)
334           return SBRDEC_UNSUPPORTED_CONFIG;
335       }
336 
337       /* Add 2nd region */
338       cumSum(k1, diff1, num_bands1, &v_k_master[num_bands0]);
339       *numMaster = num_bands0 + num_bands1;     /* Output nr of bands */
340 
341     }
342     else { /* Only one region */
343       k1=k2;
344 
345       num_bands0 = numberOfBands(bpo_div16, k0, k1, 0);
346       if ( num_bands0 < 1) {
347         return SBRDEC_UNSUPPORTED_CONFIG;
348       }
349       CalcBands(diff0, k0, k1, num_bands0);
350       shellsort(diff0, num_bands0);
351       if (diff0[0] == 0) {
352 #ifdef DEBUG_TOOLS
353 #endif
354         return SBRDEC_UNSUPPORTED_CONFIG;
355       }
356 
357       cumSum(k0, diff0, num_bands0, v_k_master);
358       *numMaster = num_bands0;        /* Output nr of bands */
359 
360     }
361   }
362   else { /* Linear mode */
363      if (hHeaderData->bs_data.alterScale==0) {
364         dk = 1;
365         /* FLOOR to get to few number of bands (next lower even number) */
366         num_bands0 = (k2 - k0) & 254;
367       } else {
368         dk = 2;
369         num_bands0 = ( ((k2 - k0) >> 1) + 1 ) & 254; /* ROUND to the closest fit */
370       }
371 
372       if (num_bands0 < 1) {
373         return SBRDEC_UNSUPPORTED_CONFIG;
374         /* We must return already here because 'i' can become negative below. */
375       }
376 
377       k2_achived = k0 + num_bands0*dk;
378       k2_diff = k2 - k2_achived;
379 
380       for(i=0;i<num_bands0;i++)
381         diff_tot[i] = dk;
382 
383       /* If linear scale wasn't achieved */
384       /* and we got too wide SBR area */
385       if (k2_diff < 0) {
386           incr = 1;
387           i = 0;
388       }
389 
390       /* If linear scale wasn't achieved */
391       /* and we got too small SBR area */
392       if (k2_diff > 0) {
393           incr = -1;
394           i = num_bands0-1;
395       }
396 
397       /* Adjust diff vector to get sepc. SBR range */
398       while (k2_diff != 0) {
399         diff_tot[i] = diff_tot[i] - incr;
400         i = i + incr;
401         k2_diff = k2_diff + incr;
402       }
403 
404       cumSum(k0, diff_tot, num_bands0, v_k_master);/* cumsum */
405     *numMaster = num_bands0;  /* Output nr of bands */
406   }
407 
408   if (*numMaster < 1) {
409     return SBRDEC_UNSUPPORTED_CONFIG;
410   }
411 
412 
413   /*
414     Print out the calculated table
415   */
416 
417   return SBRDEC_OK;
418 }
419 
420 
421 /*!
422   \brief     Calculate frequency ratio of one SBR band
423 
424   All SBR bands should span a constant frequency range in the logarithmic
425   domain. This function calculates the ratio of any SBR band's upper and lower
426   frequency.
427 
428  \return    num_band-th root of k_start/k_stop
429 */
calcFactorPerBand(int k_start,int k_stop,int num_bands)430 static FIXP_SGL calcFactorPerBand(int k_start, int k_stop, int num_bands)
431 {
432 /* Scaled bandfactor and step 1 bit right to avoid overflow
433  * use double data type */
434   FIXP_DBL bandfactor = FL2FXCONST_DBL(0.25f); /* Start value */
435   FIXP_DBL step = FL2FXCONST_DBL(0.125f);      /* Initial increment for factor */
436 
437   int    direction = 1;
438 
439 /* Because saturation can't be done in INT IIS,
440  * changed start and stop data type from FIXP_SGL to FIXP_DBL */
441   FIXP_DBL start = k_start << (DFRACT_BITS-8);
442   FIXP_DBL stop = k_stop << (DFRACT_BITS-8);
443 
444   FIXP_DBL temp;
445 
446   int   j, i=0;
447 
448   while ( step > FL2FXCONST_DBL(0.0f)) {
449     i++;
450     temp = stop;
451 
452     /* Calculate temp^num_bands: */
453     for (j=0; j<num_bands; j++)
454       //temp = fMult(temp,bandfactor);
455       temp = fMultDiv2(temp,bandfactor)<<2;
456 
457     if (temp<start) { /* Factor too strong, make it weaker */
458       if (direction == 0)
459         /* Halfen step. Right shift is not done as fract because otherwise the
460            lowest bit cannot be cleared due to rounding */
461         step = (FIXP_DBL)((LONG)step >> 1);
462       direction = 1;
463       bandfactor = bandfactor + step;
464     }
465     else {  /* Factor is too weak: make it stronger */
466       if (direction == 1)
467         step = (FIXP_DBL)((LONG)step >> 1);
468       direction = 0;
469       bandfactor = bandfactor - step;
470     }
471 
472     if (i>100) {
473       step = FL2FXCONST_DBL(0.0f);
474     }
475   }
476   return FX_DBL2FX_SGL(bandfactor<<1);
477 }
478 
479 
480 /*!
481   \brief     Calculate number of SBR bands between start and stop band
482 
483   Given the number of bands per octave, this function calculates how many
484   bands fit in the given frequency range.
485   When the warpFlag is set, the 'band density' is decreased by a factor
486   of 1/1.3
487 
488   \return    number of bands
489 */
490 static int
numberOfBands(FIXP_SGL bpo_div16,int start,int stop,int warpFlag)491 numberOfBands(FIXP_SGL bpo_div16, /*!< Input: number of bands per octave divided by 16 */
492               int    start,     /*!< First QMF band of SBR frequency range */
493               int    stop,      /*!< Last QMF band of SBR frequency range + 1 */
494               int    warpFlag)  /*!< Stretching flag */
495 {
496   FIXP_SGL num_bands_div128;
497   int    num_bands;
498 
499   num_bands_div128 = FX_DBL2FX_SGL(fMult(FDK_getNumOctavesDiv8(start,stop),bpo_div16));
500 
501   if (warpFlag) {
502     /* Apply the warp factor of 1.3 to get wider bands.  We use a value
503        of 32768/25200 instead of the exact value to avoid critical cases
504        of rounding.
505     */
506     num_bands_div128 = FX_DBL2FX_SGL(fMult(num_bands_div128, FL2FXCONST_SGL(25200.0/32768.0)));
507   }
508 
509   /* add scaled 1 for rounding to even numbers: */
510   num_bands_div128 = num_bands_div128 + FL2FXCONST_SGL( 1.0f/128.0f );
511   /* scale back to right aligned integer and double the value: */
512   num_bands = 2 * ((LONG)num_bands_div128 >> (FRACT_BITS - 7));
513 
514   return(num_bands);
515 }
516 
517 
518 /*!
519   \brief     Calculate width of SBR bands
520 
521   Given the desired number of bands within the SBR frequency range,
522   this function calculates the width of each SBR band in QMF channels.
523   The bands get wider from start to stop (bark scale).
524 */
525 static void
CalcBands(UCHAR * diff,UCHAR start,UCHAR stop,UCHAR num_bands)526 CalcBands(UCHAR * diff,    /*!< Vector of widths to be calculated */
527           UCHAR start,     /*!< Lower end of subband range */
528           UCHAR stop,      /*!< Upper end of subband range */
529           UCHAR num_bands) /*!< Desired number of bands */
530 {
531   int i;
532   int previous;
533   int current;
534   FIXP_SGL exact, temp;
535   FIXP_SGL bandfactor = calcFactorPerBand(start, stop, num_bands);
536 
537   previous = stop; /* Start with highest QMF channel */
538   exact = (FIXP_SGL)(stop << (FRACT_BITS-8)); /* Shift left to gain some accuracy */
539 
540   for(i=num_bands-1; i>=0; i--) {
541     /* Calculate border of next lower sbr band */
542     exact = FX_DBL2FX_SGL(fMult(exact,bandfactor));
543 
544     /* Add scaled 0.5 for rounding:
545        We use a value 128/256 instead of 0.5 to avoid some critical cases of rounding. */
546     temp = exact +  FL2FXCONST_SGL(128.0/32768.0);
547 
548     /* scale back to right alinged integer: */
549     current = (LONG)temp >> (FRACT_BITS-8);
550 
551     /* Save width of band i */
552     diff[i] = previous - current;
553     previous = current;
554   }
555 }
556 
557 
558 /*!
559   \brief     Calculate cumulated sum vector from delta vector
560 */
561 static void
cumSum(UCHAR start_value,UCHAR * diff,UCHAR length,UCHAR * start_adress)562 cumSum(UCHAR start_value, UCHAR* diff, UCHAR length, UCHAR *start_adress)
563 {
564   int i;
565   start_adress[0]=start_value;
566   for(i=1; i<=length; i++)
567     start_adress[i] = start_adress[i-1] + diff[i-1];
568 }
569 
570 
571 /*!
572   \brief     Adapt width of frequency bands in the second region
573 
574   If SBR spans more than 2 octaves, the upper part of a bark-frequency-scale
575   is calculated separately. This function tries to avoid that the second region
576   starts with a band smaller than the highest band of the first region.
577 */
578 static SBR_ERROR
modifyBands(UCHAR max_band_previous,UCHAR * diff,UCHAR length)579 modifyBands(UCHAR max_band_previous, UCHAR * diff, UCHAR length)
580 {
581   int change = max_band_previous - diff[0];
582 
583   /* Limit the change so that the last band cannot get narrower than the first one */
584   if ( change > (diff[length-1]-diff[0])>>1 )
585     change = (diff[length-1]-diff[0])>>1;
586 
587   diff[0] += change;
588   diff[length-1] -= change;
589   shellsort(diff, length);
590 
591   return SBRDEC_OK;
592 }
593 
594 
595 /*!
596   \brief   Update high resolution frequency band table
597 */
598 static void
sbrdecUpdateHiRes(UCHAR * h_hires,UCHAR * num_hires,UCHAR * v_k_master,UCHAR num_bands,UCHAR xover_band)599 sbrdecUpdateHiRes(UCHAR * h_hires,
600                   UCHAR * num_hires,
601                   UCHAR * v_k_master,
602                   UCHAR num_bands,
603                   UCHAR xover_band)
604 {
605   UCHAR i;
606 
607   *num_hires = num_bands-xover_band;
608 
609   for(i=xover_band; i<=num_bands; i++) {
610     h_hires[i-xover_band] = v_k_master[i];
611   }
612 }
613 
614 
615 /*!
616   \brief  Build low resolution table out of high resolution table
617 */
618 static void
sbrdecUpdateLoRes(UCHAR * h_lores,UCHAR * num_lores,UCHAR * h_hires,UCHAR num_hires)619 sbrdecUpdateLoRes(UCHAR * h_lores,
620                   UCHAR * num_lores,
621                   UCHAR * h_hires,
622                   UCHAR num_hires)
623 {
624   UCHAR i;
625 
626   if( (num_hires & 1) == 0) {
627     /* If even number of hires bands */
628     *num_lores = num_hires >> 1;
629     /* Use every second lores=hires[0,2,4...] */
630     for(i=0; i<=*num_lores; i++)
631       h_lores[i] = h_hires[i*2];
632   }
633   else {
634     /* Odd number of hires, which means xover is odd */
635     *num_lores = (num_hires+1) >> 1;
636     /* Use lores=hires[0,1,3,5 ...] */
637     h_lores[0] = h_hires[0];
638     for(i=1; i<=*num_lores; i++) {
639       h_lores[i] = h_hires[i*2-1];
640     }
641   }
642 }
643 
644 
645 /*!
646   \brief   Derive a low-resolution frequency-table from the master frequency table
647 */
648 void
sbrdecDownSampleLoRes(UCHAR * v_result,UCHAR num_result,UCHAR * freqBandTableRef,UCHAR num_Ref)649 sbrdecDownSampleLoRes(UCHAR *v_result,
650                       UCHAR num_result,
651                       UCHAR *freqBandTableRef,
652                       UCHAR num_Ref)
653 {
654   int step;
655   int i,j;
656   int org_length,result_length;
657   int v_index[MAX_FREQ_COEFFS>>1];
658 
659   /* init */
660   org_length = num_Ref;
661   result_length = num_result;
662 
663   v_index[0] = 0;   /* Always use left border */
664   i=0;
665   while(org_length > 0) {
666     /* Create downsample vector */
667     i++;
668     step = org_length / result_length;
669     org_length = org_length - step;
670     result_length--;
671     v_index[i] = v_index[i-1] + step;
672   }
673 
674   for(j=0;j<=i;j++) {
675     /* Use downsample vector to index LoResolution vector */
676     v_result[j]=freqBandTableRef[v_index[j]];
677   }
678 
679 }
680 
681 
682 /*!
683   \brief   Sorting routine
684 */
shellsort(UCHAR * in,UCHAR n)685 void shellsort(UCHAR *in, UCHAR n)
686 {
687 
688   int i, j, v, w;
689   int inc = 1;
690 
691   do
692     inc = 3 * inc + 1;
693   while (inc <= n);
694 
695   do {
696     inc = inc / 3;
697     for (i = inc; i < n; i++) {
698       v = in[i];
699       j = i;
700       while ((w=in[j-inc]) > v) {
701         in[j] = w;
702         j -= inc;
703         if (j < inc)
704           break;
705       }
706       in[j] = v;
707     }
708   } while (inc > 1);
709 
710 }
711 
712 
713 
714 /*!
715   \brief   Reset frequency band tables
716   \return  errorCode, 0 if successful
717 */
718 SBR_ERROR
resetFreqBandTables(HANDLE_SBR_HEADER_DATA hHeaderData,const UINT flags)719 resetFreqBandTables(HANDLE_SBR_HEADER_DATA hHeaderData, const UINT flags)
720 {
721   SBR_ERROR err = SBRDEC_OK;
722   int k2,kx, lsb, usb;
723   int     intTemp;
724   UCHAR    nBandsLo, nBandsHi;
725   HANDLE_FREQ_BAND_DATA hFreq = &hHeaderData->freqBandData;
726 
727   /* Calculate master frequency function */
728   err = sbrdecUpdateFreqScale(hFreq->v_k_master,
729                               &hFreq->numMaster,
730                               hHeaderData->sbrProcSmplRate,
731                               hHeaderData,
732                               flags);
733 
734   if ( err || (hHeaderData->bs_info.xover_band > hFreq->numMaster) ) {
735     return SBRDEC_UNSUPPORTED_CONFIG;
736   }
737 
738   /* Derive Hiresolution from master frequency function */
739   sbrdecUpdateHiRes(hFreq->freqBandTable[1], &nBandsHi, hFreq->v_k_master, hFreq->numMaster, hHeaderData->bs_info.xover_band );
740   /* Derive Loresolution from Hiresolution */
741   sbrdecUpdateLoRes(hFreq->freqBandTable[0], &nBandsLo, hFreq->freqBandTable[1], nBandsHi);
742 
743 
744   hFreq->nSfb[0] = nBandsLo;
745   hFreq->nSfb[1] = nBandsHi;
746 
747   /* Check index to freqBandTable[0] */
748   if ( !(nBandsLo > 0) || (nBandsLo > (MAX_FREQ_COEFFS>>1)) ) {
749     return SBRDEC_UNSUPPORTED_CONFIG;
750   }
751 
752   lsb = hFreq->freqBandTable[0][0];
753   usb = hFreq->freqBandTable[0][nBandsLo];
754 
755   /* Additional check for lsb */
756   if ( (lsb > (32)) || (lsb >= usb) ) {
757     return SBRDEC_UNSUPPORTED_CONFIG;
758   }
759 
760 
761   /* Calculate number of noise bands */
762 
763   k2 = hFreq->freqBandTable[1][nBandsHi];
764   kx = hFreq->freqBandTable[1][0];
765 
766   if (hHeaderData->bs_data.noise_bands == 0)
767   {
768     hFreq->nNfb = 1;
769   }
770   else /* Calculate no of noise bands 1,2 or 3 bands/octave */
771   {
772     /* Fetch number of octaves divided by 32 */
773     intTemp = (LONG)FDK_getNumOctavesDiv8(kx,k2) >> 2;
774 
775     /* Integer-Multiplication with number of bands: */
776     intTemp = intTemp * hHeaderData->bs_data.noise_bands;
777 
778     /* Add scaled 0.5 for rounding: */
779     intTemp = intTemp + (LONG)FL2FXCONST_SGL(0.5f/32.0f);
780 
781     /* Convert to right-aligned integer: */
782     intTemp = intTemp >> (FRACT_BITS - 1 /*sign*/ - 5 /* rescale */);
783 
784     /* Compare with float calculation */
785     FDK_ASSERT( intTemp ==  (int)((hHeaderData->bs_data.noise_bands * FDKlog( (float)k2/kx) / (float)(FDKlog(2.0)))+0.5) );
786 
787     if( intTemp==0)
788       intTemp=1;
789 
790     hFreq->nNfb = intTemp;
791   }
792 
793   hFreq->nInvfBands = hFreq->nNfb;
794 
795   if( hFreq->nNfb > MAX_NOISE_COEFFS ) {
796     return SBRDEC_UNSUPPORTED_CONFIG;
797   }
798 
799   /* Get noise bands */
800   sbrdecDownSampleLoRes(hFreq->freqBandTableNoise,
801                         hFreq->nNfb,
802                         hFreq->freqBandTable[0],
803                         nBandsLo);
804 
805 
806 
807 
808   hFreq->lowSubband  = lsb;
809   hFreq->highSubband = usb;
810 
811   return SBRDEC_OK;
812 }
813