30bc5ca11aae77715b09e5b6ce38c828e96322a5
2 /* -----------------------------------------------------------------------------------------------------------
3 Software License for The Fraunhofer FDK AAC Codec Library for Android
5 © Copyright 1995 - 2013 Fraunhofer-Gesellschaft zur Förderung der angewandten Forschung e.V.
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.
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.
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.
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.
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:
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.
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.
42 The name of Fraunhofer may not be used to endorse or promote products derived from this library without
43 prior written permission.
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.
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."
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.
59 You may use this FDK AAC Codec software or modifications thereto only for purposes that are authorized
60 by appropriate patent licenses.
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.
73 5. CONTACT INFORMATION
75 Fraunhofer Institute for Integrated Circuits IIS
76 Attention: Audio and Multimedia Departments - FDK AAC LL
78 91058 Erlangen, Germany
80 www.iis.fraunhofer.de/amm
81 amm-info@iis.fraunhofer.de
82 ----------------------------------------------------------------------------------------------------------- */
86 \brief frequency scale
87 \author Tobias Chalupka
90 #include "sbrenc_freq_sca.h"
93 #include "genericStds.h"
96 static INT
getStartFreq(INT fsCore
, const INT start_freq
);
99 static INT
getStopFreq(INT fsCore
, const INT stop_freq
);
101 static INT
numberOfBands(INT b_p_o
, INT start
, INT stop
, FIXP_DBL warp_factor
);
102 static void CalcBands(INT
* diff
, INT start
, INT stop
, INT num_bands
);
103 static INT
modifyBands(INT max_band
, INT
* diff
, INT length
);
104 static void cumSum(INT start_value
, INT
* diff
, INT length
, UCHAR
*start_adress
);
108 /*******************************************************************************
109 Functionname: FDKsbrEnc_getSbrStartFreqRAW
110 *******************************************************************************
116 *******************************************************************************/
119 FDKsbrEnc_getSbrStartFreqRAW (INT startFreq
, INT fsCore
)
123 if ( startFreq
< 0 || startFreq
> 15) {
126 /* Update startFreq struct */
127 result
= getStartFreq(fsCore
, startFreq
);
129 result
= (result
*(fsCore
>>5)+1)>>1; /* (result*fsSBR/QMFbands+1)>>1; */
133 } /* End FDKsbrEnc_getSbrStartFreqRAW */
136 /*******************************************************************************
137 Functionname: getSbrStopFreq
138 *******************************************************************************
144 *******************************************************************************/
145 INT
FDKsbrEnc_getSbrStopFreqRAW (INT stopFreq
, INT fsCore
)
149 if ( stopFreq
< 0 || stopFreq
> 13)
152 /* Uppdate stopFreq struct */
153 result
= getStopFreq(fsCore
, stopFreq
);
154 result
= (result
*(fsCore
>>5)+1)>>1; /* (result*fsSBR/QMFbands+1)>>1; */
157 } /* End getSbrStopFreq */
160 /*******************************************************************************
161 Functionname: getStartFreq
162 *******************************************************************************
165 Arguments: fsCore - core sampling rate
169 *******************************************************************************/
171 getStartFreq(INT fsCore
, const INT start_freq
)
176 case 8000: k0_min
= 24; /* (3000 * nQmfChannels / fsSBR ) + 0.5 */
178 case 11025: k0_min
= 17; /* (3000 * nQmfChannels / fsSBR ) + 0.5 */
180 case 12000: k0_min
= 16; /* (3000 * nQmfChannels / fsSBR ) + 0.5 */
182 case 16000: k0_min
= 16; /* (4000 * nQmfChannels / fsSBR ) + 0.5 */
184 case 22050: k0_min
= 12; /* (4000 * nQmfChannels / fsSBR ) + 0.5 */
186 case 24000: k0_min
= 11; /* (4000 * nQmfChannels / fsSBR ) + 0.5 */
188 case 32000: k0_min
= 10; /* (5000 * nQmfChannels / fsSBR ) + 0.5 */
190 case 44100: k0_min
= 7; /* (5000 * nQmfChannels / fsSBR ) + 0.5 */
192 case 48000: k0_min
= 7; /* (5000 * nQmfChannels / fsSBR ) + 0.5 */
194 case 96000: k0_min
= 3; /* (5000 * nQmfChannels / fsSBR ) + 0.5 */
197 k0_min
=11; /* illegal fs */
205 INT v_offset
[]= {-8, -7, -6, -5, -4, -3, -2, -1, 0, 1, 2, 3, 4, 5, 6, 7};
206 return (k0_min
+ v_offset
[start_freq
]);
210 INT v_offset
[]= {-5, -4, -3, -2, -1, 0, 1, 2, 3, 4, 5, 6, 7, 9, 11, 13};
211 return (k0_min
+ v_offset
[start_freq
]);
215 INT v_offset
[]= {-5, -3, -2, -1, 0, 1, 2, 3, 4, 5, 6, 7, 9, 11, 13, 16};
216 return (k0_min
+ v_offset
[start_freq
]);
220 INT v_offset
[]= {-6, -4, -2, -1, 0, 1, 2, 3, 4, 5, 6, 7, 9, 11, 13, 16};
221 return (k0_min
+ v_offset
[start_freq
]);
227 INT v_offset
[]= {-4, -2, -1, 0, 1, 2, 3, 4, 5, 6, 7, 9, 11, 13, 16, 20};
228 return (k0_min
+ v_offset
[start_freq
]);
234 INT v_offset
[]= {-2, -1, 0, 1, 2, 3, 4, 5, 6, 7, 9, 11, 13, 16, 20, 24};
235 return (k0_min
+ v_offset
[start_freq
]);
239 INT v_offset
[]= {0, 1, 2, 3, 4, 5, 6, 7, 9, 11, 13, 16, 20, 24, 28, 33};
240 return (k0_min
+ v_offset
[start_freq
]);
243 } /* End getStartFreq */
246 /*******************************************************************************
247 Functionname: getStopFreq
248 *******************************************************************************
254 *******************************************************************************/
256 getStopFreq(INT fsCore
, const INT stop_freq
)
262 INT
*v_stop_freq
= NULL
;
263 INT v_stop_freq_16
[14] = {48,49,50,51,52,54,55,56,57,59,60,61,63,64};
264 INT v_stop_freq_22
[14] = {35,37,38,40,42,44,46,48,51,53,56,58,61,64};
265 INT v_stop_freq_24
[14] = {32,34,36,38,40,42,44,46,49,52,55,58,61,64};
266 INT v_stop_freq_32
[14] = {32,34,36,38,40,42,44,46,49,52,55,58,61,64};
267 INT v_stop_freq_44
[14] = {23,25,27,29,32,34,37,40,43,47,51,55,59,64};
268 INT v_stop_freq_48
[14] = {21,23,25,27,30,32,35,38,42,45,49,54,59,64};
269 INT v_stop_freq_64
[14] = {20,22,24,26,29,31,34,37,41,45,49,54,59,64};
270 INT v_stop_freq_88
[14] = {15,17,19,21,23,26,29,33,37,41,46,51,57,64};
271 INT v_stop_freq_96
[14] = {13,15,17,19,21,24,27,31,35,39,44,50,57,64};
272 INT v_stop_freq_192
[14] = {7, 8,10,12,14,16,19,23,27,32,38,46,54,64};
275 case 8000: k1_min
= 48;
276 v_stop_freq
=v_stop_freq_16
;
278 case 11025: k1_min
= 35;
279 v_stop_freq
=v_stop_freq_22
;
281 case 12000: k1_min
= 32;
282 v_stop_freq
=v_stop_freq_24
;
284 case 16000: k1_min
= 32;
285 v_stop_freq
=v_stop_freq_32
;
287 case 22050: k1_min
= 23;
288 v_stop_freq
=v_stop_freq_44
;
290 case 24000: k1_min
= 21;
291 v_stop_freq
=v_stop_freq_48
;
293 case 32000: k1_min
= 20;
294 v_stop_freq
=v_stop_freq_64
;
296 case 44100: k1_min
= 15;
297 v_stop_freq
=v_stop_freq_88
;
299 case 48000: k1_min
= 13;
300 v_stop_freq
=v_stop_freq_96
;
302 case 96000: k1_min
= 7;
303 v_stop_freq
=v_stop_freq_192
;
306 k1_min
= 21; /* illegal fs */
309 /* if no valid core samplingrate is used this loop produces
310 a segfault, because v_stop_freq is not initialized */
311 /* Ensure increasing bandwidth */
312 for(i
= 0; i
<= 12; i
++) {
313 v_dstop
[i
] = v_stop_freq
[i
+1] - v_stop_freq
[i
];
316 FDKsbrEnc_Shellsort_int(v_dstop
, 13); /* Sort bandwidth changes */
319 for(i
= 0; i
< stop_freq
; i
++) {
320 result
= result
+ v_dstop
[i
];
325 }/* End getStopFreq */
328 /*******************************************************************************
329 Functionname: FDKsbrEnc_FindStartAndStopBand
330 *******************************************************************************
333 Arguments: srSbr SBR sampling freqency
334 srCore AAC core sampling freqency
335 noChannels Number of QMF channels
336 startFreq SBR start frequency in QMF bands
337 stopFreq SBR start frequency in QMF bands
342 Return: Error code (0 is OK)
343 *******************************************************************************/
345 FDKsbrEnc_FindStartAndStopBand(
348 const INT noChannels
,
356 /* Update startFreq struct */
357 *k0
= getStartFreq(srCore
, startFreq
);
359 /* Test if start freq is outside corecoder range */
360 if( srSbr
*noChannels
< *k0
* srCore
) {
361 return (1); /* raise the cross-over frequency and/or lower the number
362 of target bands per octave (or lower the sampling frequency) */
365 /*Update stopFreq struct */
366 if ( stopFreq
< 14 ) {
367 *k2
= getStopFreq(srCore
, stopFreq
);
368 } else if( stopFreq
== 14 ) {
374 /* limit to Nyqvist */
375 if (*k2
> noChannels
) {
381 /* Test for invalid k0 k2 combinations */
382 if ( (srCore
== 22050) && ( (*k2
- *k0
) > MAX_FREQ_COEFFS_FS44100
) )
383 return (1); /* Number of bands exceeds valid range of MAX_FREQ_COEFFS for fs=44.1kHz */
385 if ( (srCore
>= 24000) && ( (*k2
- *k0
) > MAX_FREQ_COEFFS_FS48000
) )
386 return (1); /* Number of bands exceeds valid range of MAX_FREQ_COEFFS for fs>=48kHz */
388 if ((*k2
- *k0
) > MAX_FREQ_COEFFS
)
389 return (1);/*Number of bands exceeds valid range of MAX_FREQ_COEFFS */
392 return (1);/* Number of bands is negative */
398 /*******************************************************************************
399 Functionname: FDKsbrEnc_UpdateFreqScale
400 *******************************************************************************
406 *******************************************************************************/
408 FDKsbrEnc_UpdateFreqScale(
419 INT b_p_o
= 0; /* bands_per_octave */
420 FIXP_DBL warp
= FL2FXCONST_DBL(0.0f
);
423 /* Internal variables */
427 INT diff_tot
[MAX_OCTAVE
+ MAX_SECOND_REGION
];
428 INT
*diff0
= diff_tot
;
429 INT
*diff1
= diff_tot
+MAX_OCTAVE
;
435 if (freqScale
==1) b_p_o
= 12;
436 if (freqScale
==2) b_p_o
= 10;
437 if (freqScale
==3) b_p_o
= 8;
440 if(freqScale
> 0) /*Bark*/
443 warp
= FL2FXCONST_DBL(0.5f
); /* 1.0/(1.0*2.0) */
445 warp
= FL2FXCONST_DBL(1.0f
/2.6f
); /* 1.0/(1.3*2.0); */
448 if(4*k2
>= 9*k0
) /*two or more regions (how many times the basis band is copied)*/
452 num_bands0
=numberOfBands(b_p_o
, k0
, k1
, FL2FXCONST_DBL(0.5f
));
453 num_bands1
=numberOfBands(b_p_o
, k1
, k2
, warp
);
455 CalcBands(diff0
, k0
, k1
, num_bands0
);/*CalcBands1 => diff0 */
456 FDKsbrEnc_Shellsort_int( diff0
, num_bands0
);/*SortBands sort diff0 */
458 if (diff0
[0] == 0) /* too wide FB bands for target tuning */
460 return (1);/* raise the cross-over frequency and/or lower the number
461 of target bands per octave (or lower the sampling frequency */
464 cumSum(k0
, diff0
, num_bands0
, v_k_master
); /* cumsum */
466 CalcBands(diff1
, k1
, k2
, num_bands1
); /* CalcBands2 => diff1 */
467 FDKsbrEnc_Shellsort_int( diff1
, num_bands1
); /* SortBands sort diff1 */
468 if(diff0
[num_bands0
-1] > diff1
[0]) /* max(1) > min(2) */
470 if(modifyBands(diff0
[num_bands0
-1],diff1
, num_bands1
))
474 /* Add 2'nd region */
475 cumSum(k1
, diff1
, num_bands1
, &v_k_master
[num_bands0
]);
476 *h_num_bands
=num_bands0
+num_bands1
; /* Output nr of bands */
479 else /* one region */
483 num_bands0
=numberOfBands(b_p_o
, k0
, k1
, FL2FXCONST_DBL(0.5f
));
484 CalcBands(diff0
, k0
, k1
, num_bands0
);/* CalcBands1 => diff0 */
485 FDKsbrEnc_Shellsort_int( diff0
, num_bands0
); /* SortBands sort diff0 */
487 if (diff0
[0] == 0) /* too wide FB bands for target tuning */
489 return (1); /* raise the cross-over frequency and/or lower the number
490 of target bands per octave (or lower the sampling frequency */
493 cumSum(k0
, diff0
, num_bands0
, v_k_master
);/* cumsum */
494 *h_num_bands
=num_bands0
; /* Output nr of bands */
498 else /* Linear mode */
502 num_bands0
= 2 * ((k2
- k0
)/2); /* FLOOR to get to few number of bands*/
505 num_bands0
= 2 * (((k2
- k0
)/dk
+1)/2); /* ROUND to get closest fit */
508 k2_achived
= k0
+ num_bands0
*dk
;
509 k2_diff
= k2
- k2_achived
;
511 for(i
=0;i
<num_bands0
;i
++)
514 /* If linear scale wasn't achived */
515 /* and we got wide SBR are */
521 /* If linear scale wasn't achived */
522 /* and we got small SBR are */
528 /* Adjust diff vector to get sepc. SBR range */
529 while (k2_diff
!= 0) {
530 diff_tot
[i
] = diff_tot
[i
] - incr
;
532 k2_diff
= k2_diff
+ incr
;
535 cumSum(k0
, diff_tot
, num_bands0
, v_k_master
);/* cumsum */
536 *h_num_bands
=num_bands0
; /* Output nr of bands */
540 if (*h_num_bands
< 1)
541 return(1); /*To small sbr area */
544 }/* End FDKsbrEnc_UpdateFreqScale */
547 numberOfBands(INT b_p_o
, INT start
, INT stop
, FIXP_DBL warp_factor
)
550 /* result = 2* (INT) ( (double)b_p_o * (double)(FDKlog((double)stop/(double)start)/FDKlog((double)2)) * (double)FX_DBL2FL(warp_factor) + 0.5); */
551 result
= ( ( b_p_o
* fMult( (CalcLdInt(stop
) - CalcLdInt(start
)), warp_factor
) + (FL2FX_DBL(0.5f
)>>LD_DATA_SHIFT
)
552 ) >> ((DFRACT_BITS
-1)-LD_DATA_SHIFT
) ) << 1; /* do not optimize anymore (rounding!!) */
559 CalcBands(INT
* diff
, INT start
, INT stop
, INT num_bands
)
564 FIXP_DBL base
, exp
, tmp
;
567 for(i
=1; i
<= num_bands
; i
++)
569 base
= fDivNorm((FIXP_DBL
)stop
, (FIXP_DBL
)start
, &qb
);
570 exp
= fDivNorm((FIXP_DBL
)i
, (FIXP_DBL
)num_bands
, &qe
);
571 tmp
= fPow(base
, qb
, exp
, qe
, &qtmp
);
572 tmp
= fMult(tmp
, (FIXP_DBL
)(start
<<24));
573 current
= (INT
)scaleValue(tmp
, qtmp
-23);
574 current
= (current
+1) >> 1; /* rounding*/
575 diff
[i
-1] = current
-previous
;
583 cumSum(INT start_value
, INT
* diff
, INT length
, UCHAR
*start_adress
)
586 start_adress
[0]=start_value
;
587 for(i
=1;i
<=length
;i
++)
588 start_adress
[i
]=start_adress
[i
-1]+diff
[i
-1];
593 modifyBands(INT max_band_previous
, INT
* diff
, INT length
)
595 INT change
=max_band_previous
-diff
[0];
597 /* Limit the change so that the last band cannot get narrower than the first one */
598 if ( change
> (diff
[length
-1] - diff
[0]) / 2 )
599 change
= (diff
[length
-1] - diff
[0]) / 2;
602 diff
[length
-1] -= change
;
603 FDKsbrEnc_Shellsort_int(diff
, length
);
606 }/* End modifyBands */
609 /*******************************************************************************
610 Functionname: FDKsbrEnc_UpdateHiRes
611 *******************************************************************************
618 *******************************************************************************/
620 FDKsbrEnc_UpdateHiRes(
631 if( (v_k_master
[*xover_band
] > 32 ) || /* v_k_master[*xover_band] > noQMFChannels(dualRate)/divider */
632 ( *xover_band
> num_master
) ) {
633 /* xover_band error, too big for this startFreq. Will be clipped */
635 /* Calculate maximum value for xover_band */
638 while( (v_k_master
[max1
+1] < 32 ) && /* noQMFChannels(dualRate)/divider */
647 *num_hires
= num_master
- *xover_band
;
648 for(i
= *xover_band
; i
<= num_master
; i
++)
650 h_hires
[i
- *xover_band
] = v_k_master
[i
];
654 }/* End FDKsbrEnc_UpdateHiRes */
657 /*******************************************************************************
658 Functionname: FDKsbrEnc_UpdateLoRes
659 *******************************************************************************
665 *******************************************************************************/
667 FDKsbrEnc_UpdateLoRes(UCHAR
* h_lores
, INT
*num_lores
, UCHAR
* h_hires
, INT num_hires
)
671 if(num_hires
%2 == 0) /* if even number of hires bands */
673 *num_lores
=num_hires
/2;
674 /* Use every second lores=hires[0,2,4...] */
675 for(i
=0;i
<=*num_lores
;i
++)
676 h_lores
[i
]=h_hires
[i
*2];
679 else /* odd number of hires which means xover is odd */
681 *num_lores
=(num_hires
+1)/2;
683 /* Use lores=hires[0,1,3,5 ...] */
684 h_lores
[0]=h_hires
[0];
685 for(i
=1;i
<=*num_lores
;i
++)
687 h_lores
[i
]=h_hires
[i
*2-1];
691 }/* End FDKsbrEnc_UpdateLoRes */