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 /*****************************  MPEG-4 AAC Decoder  **************************
85 
86    Author(s):   Josef Hoepfl
87    Description: joint stereo processing
88 
89 ******************************************************************************/
90 
91 #include "stereo.h"
92 
93 
94 #include "aac_rom.h"
95 #include "FDK_bitstream.h"
96 #include "channelinfo.h"
97 
98 enum
99 {
100   L = 0,
101   R = 1
102 };
103 
104 
CJointStereo_Read(HANDLE_FDK_BITSTREAM bs,CJointStereoData * pJointStereoData,const int windowGroups,const int scaleFactorBandsTransmitted,const UINT flags)105 int CJointStereo_Read(
106         HANDLE_FDK_BITSTREAM bs,
107         CJointStereoData *pJointStereoData,
108         const int windowGroups,
109         const int scaleFactorBandsTransmitted,
110         const UINT flags
111         )
112 {
113   int group,band;
114 
115   pJointStereoData->MsMaskPresent = (UCHAR) FDKreadBits(bs,2);
116 
117   FDKmemclear(pJointStereoData->MsUsed, scaleFactorBandsTransmitted*sizeof(UCHAR));
118 
119   switch (pJointStereoData->MsMaskPresent)
120   {
121     case 0 : /* no M/S */
122       /* all flags are already cleared */
123       break ;
124 
125     case 1 : /* read ms_used */
126 
127       for (group=0; group<windowGroups; group++)
128       {
129         for (band=0; band<scaleFactorBandsTransmitted; band++)
130         {
131           pJointStereoData->MsUsed[band] |= (FDKreadBits(bs,1) << group);
132         }
133       }
134       break ;
135 
136     case 2 : /* full spectrum M/S */
137 
138       for (band=0; band<scaleFactorBandsTransmitted; band++)
139       {
140         pJointStereoData->MsUsed[band] = 255 ;  /* set all flags to 1 */
141       }
142       break ;
143   }
144 
145   return 0;
146 }
147 
CJointStereo_ApplyMS(CAacDecoderChannelInfo * pAacDecoderChannelInfo[2],const SHORT * pScaleFactorBandOffsets,const UCHAR * pWindowGroupLength,const int windowGroups,const int scaleFactorBandsTransmittedL,const int scaleFactorBandsTransmittedR)148 void CJointStereo_ApplyMS(
149         CAacDecoderChannelInfo *pAacDecoderChannelInfo[2],
150         const SHORT *pScaleFactorBandOffsets,
151         const UCHAR *pWindowGroupLength,
152         const int windowGroups,
153         const int scaleFactorBandsTransmittedL,
154         const int scaleFactorBandsTransmittedR
155         )
156 {
157   CJointStereoData *pJointStereoData = &pAacDecoderChannelInfo[L]->pComData->jointStereoData;
158   int window, group, scaleFactorBandsTransmitted;
159 
160   FDK_ASSERT(scaleFactorBandsTransmittedL == scaleFactorBandsTransmittedR);
161   scaleFactorBandsTransmitted = scaleFactorBandsTransmittedL;
162   for (window = 0, group = 0; group < windowGroups; group++)
163   {
164     UCHAR groupMask = 1 << group;
165 
166     for (int groupwin=0; groupwin<pWindowGroupLength[group]; groupwin++, window++)
167     {
168       int band;
169       FIXP_DBL *leftSpectrum, *rightSpectrum;
170       SHORT *leftScale = &pAacDecoderChannelInfo[L]->pDynData->aSfbScale[window*16];
171       SHORT *rightScale = &pAacDecoderChannelInfo[R]->pDynData->aSfbScale[window*16];
172 
173       leftSpectrum = SPEC(pAacDecoderChannelInfo[L]->pSpectralCoefficient, window, pAacDecoderChannelInfo[L]->granuleLength);
174       rightSpectrum = SPEC(pAacDecoderChannelInfo[R]->pSpectralCoefficient, window, pAacDecoderChannelInfo[R]->granuleLength);
175 
176       for (band=0; band<scaleFactorBandsTransmitted; band++)
177       {
178         if (pJointStereoData->MsUsed[band] & groupMask)
179         {
180           int lScale=leftScale[band];
181           int rScale=rightScale[band];
182           int commonScale=lScale > rScale ? lScale:rScale;
183 
184           /* ISO/IEC 14496-3 Chapter 4.6.8.1.1 :
185              M/S joint channel coding can only be used if common_window is �1�. */
186           FDK_ASSERT(GetWindowSequence(&pAacDecoderChannelInfo[L]->icsInfo) == GetWindowSequence(&pAacDecoderChannelInfo[R]->icsInfo));
187           FDK_ASSERT(GetWindowShape(&pAacDecoderChannelInfo[L]->icsInfo) == GetWindowShape(&pAacDecoderChannelInfo[R]->icsInfo));
188 
189           commonScale++;
190           leftScale[band]=commonScale;
191           rightScale[band]=commonScale;
192 
193           lScale = fMin(DFRACT_BITS-1, commonScale - lScale);
194           rScale = fMin(DFRACT_BITS-1, commonScale - rScale);
195 
196           FDK_ASSERT(lScale >= 0 && rScale >= 0);
197 
198           for (int index=pScaleFactorBandOffsets[band]; index<pScaleFactorBandOffsets[band+1]; index++)
199           {
200             FIXP_DBL leftCoefficient  = leftSpectrum [index] ;
201             FIXP_DBL rightCoefficient = rightSpectrum [index] ;
202 
203             leftCoefficient >>= lScale ;
204             rightCoefficient >>= rScale ;
205 
206             leftSpectrum [index] = leftCoefficient + rightCoefficient ;
207             rightSpectrum [index] = leftCoefficient - rightCoefficient ;
208           }
209         }
210       }
211     }
212   }
213 
214   /* Reset MsUsed flags if no explicit signalling was transmitted. Necessary for intensity coding.
215      PNS correlation signalling was mapped before calling CJointStereo_ApplyMS(). */
216   if (pJointStereoData->MsMaskPresent == 2) {
217     FDKmemclear(pJointStereoData->MsUsed, JointStereoMaximumBands * sizeof(UCHAR));
218   }
219 }
220 
CJointStereo_ApplyIS(CAacDecoderChannelInfo * pAacDecoderChannelInfo[2],const SHORT * pScaleFactorBandOffsets,const UCHAR * pWindowGroupLength,const int windowGroups,const int scaleFactorBandsTransmitted,const UINT CommonWindow)221 void CJointStereo_ApplyIS(
222         CAacDecoderChannelInfo *pAacDecoderChannelInfo[2],
223         const SHORT *pScaleFactorBandOffsets,
224         const UCHAR *pWindowGroupLength,
225         const int windowGroups,
226         const int scaleFactorBandsTransmitted,
227         const UINT CommonWindow
228         )
229 {
230   CJointStereoData *pJointStereoData = &pAacDecoderChannelInfo[L]->pComData->jointStereoData;
231 
232   for (int window=0,group=0; group<windowGroups; group++)
233   {
234     UCHAR *CodeBook;
235     SHORT *ScaleFactor;
236     UCHAR groupMask = 1 << group;
237 
238     CodeBook = &pAacDecoderChannelInfo[R]->pDynData->aCodeBook[group*16];
239     ScaleFactor = &pAacDecoderChannelInfo[R]->pDynData->aScaleFactor[group*16];
240 
241     for (int groupwin=0; groupwin<pWindowGroupLength[group]; groupwin++, window++)
242     {
243       FIXP_DBL *leftSpectrum, *rightSpectrum;
244       SHORT *leftScale = &pAacDecoderChannelInfo[L]->pDynData->aSfbScale[window*16];
245       SHORT *rightScale = &pAacDecoderChannelInfo[R]->pDynData->aSfbScale[window*16];
246       int band;
247 
248       leftSpectrum = SPEC(pAacDecoderChannelInfo[L]->pSpectralCoefficient, window, pAacDecoderChannelInfo[L]->granuleLength);
249       rightSpectrum = SPEC(pAacDecoderChannelInfo[R]->pSpectralCoefficient, window, pAacDecoderChannelInfo[R]->granuleLength);
250 
251       for (band=0; band<scaleFactorBandsTransmitted; band++)
252       {
253         if ((CodeBook [band] == INTENSITY_HCB) ||
254             (CodeBook [band] == INTENSITY_HCB2))
255         {
256           int bandScale = -(ScaleFactor [band] + 100) ;
257 
258           int msb = bandScale >> 2 ;
259           int lsb = bandScale & 0x03 ;
260 
261           /* exponent of MantissaTable[lsb][0] is 1, thus msb+1 below. */
262           FIXP_DBL scale = MantissaTable[lsb][0];
263 
264           /* ISO/IEC 14496-3 Chapter 4.6.8.2.3 :
265              The use of intensity stereo coding is signaled by the use of the pseudo codebooks
266              INTENSITY_HCB and INTENSITY_HCB2 (15 and 14) only in the right channel of a
267              channel_pair_element() having a common ics_info() (common_window == 1). */
268           FDK_ASSERT(GetWindowSequence(&pAacDecoderChannelInfo[L]->icsInfo) == GetWindowSequence(&pAacDecoderChannelInfo[R]->icsInfo));
269           FDK_ASSERT(GetWindowShape(&pAacDecoderChannelInfo[L]->icsInfo) == GetWindowShape(&pAacDecoderChannelInfo[R]->icsInfo));
270 
271           rightScale[band] = leftScale[band]+msb+1;
272 
273           if (CommonWindow && (pJointStereoData->MsUsed[band] & groupMask))
274           {
275 
276             if (CodeBook[band] == INTENSITY_HCB) /* _NOT_ in-phase */
277             {
278               scale = -scale ;
279             }
280           }
281           else
282           {
283             if (CodeBook[band] == INTENSITY_HCB2) /* out-of-phase */
284             {
285               scale = -scale ;
286             }
287           }
288 
289           for (int index=pScaleFactorBandOffsets[band]; index<pScaleFactorBandOffsets[band+1]; index++)
290           {
291             rightSpectrum[index] = fMult(leftSpectrum[index],scale);
292           }
293         }
294       }
295     }
296   }
297 }
298