1
2/* -----------------------------------------------------------------------------------------------------------
3Software License for The Fraunhofer FDK AAC Codec Library for Android
4
5© Copyright  1995 - 2012 Fraunhofer-Gesellschaft zur Förderung der angewandten Forschung e.V.
6  All rights reserved.
7
8 1.    INTRODUCTION
9The Fraunhofer FDK AAC Codec Library for Android ("FDK AAC Codec") is software that implements
10the MPEG Advanced Audio Coding ("AAC") encoding and decoding scheme for digital audio.
11This FDK AAC Codec software is intended to be used on a wide variety of Android devices.
12
13AAC's HE-AAC and HE-AAC v2 versions are regarded as today's most efficient general perceptual
14audio codecs. AAC-ELD is considered the best-performing full-bandwidth communications codec by
15independent studies and is widely deployed. AAC has been standardized by ISO and IEC as part
16of the MPEG specifications.
17
18Patent licenses for necessary patent claims for the FDK AAC Codec (including those of Fraunhofer)
19may be obtained through Via Licensing (www.vialicensing.com) or through the respective patent owners
20individually for the purpose of encoding or decoding bit streams in products that are compliant with
21the ISO/IEC MPEG audio standards. Please note that most manufacturers of Android devices already license
22these patent claims through Via Licensing or directly from the patent owners, and therefore FDK AAC Codec
23software may already be covered under those patent licenses when it is used for those licensed purposes only.
24
25Commercially-licensed AAC software libraries, including floating-point versions with enhanced sound quality,
26are also available from Fraunhofer. Users are encouraged to check the Fraunhofer website for additional
27applications information and documentation.
28
292.    COPYRIGHT LICENSE
30
31Redistribution and use in source and binary forms, with or without modification, are permitted without
32payment of copyright license fees provided that you satisfy the following conditions:
33
34You must retain the complete text of this software license in redistributions of the FDK AAC Codec or
35your modifications thereto in source code form.
36
37You must retain the complete text of this software license in the documentation and/or other materials
38provided with redistributions of the FDK AAC Codec or your modifications thereto in binary form.
39You must make available free of charge copies of the complete source code of the FDK AAC Codec and your
40modifications thereto to recipients of copies in binary form.
41
42The name of Fraunhofer may not be used to endorse or promote products derived from this library without
43prior written permission.
44
45You may not charge copyright license fees for anyone to use, copy or distribute the FDK AAC Codec
46software or your modifications thereto.
47
48Your modified versions of the FDK AAC Codec must carry prominent notices stating that you changed the software
49and 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
533.    NO PATENT LICENSE
54
55NO EXPRESS OR IMPLIED LICENSES TO ANY PATENT CLAIMS, including without limitation the patents of Fraunhofer,
56ARE GRANTED BY THIS SOFTWARE LICENSE. Fraunhofer provides no warranty of patent non-infringement with
57respect to this software.
58
59You may use this FDK AAC Codec software or modifications thereto only for purposes that are authorized
60by appropriate patent licenses.
61
624.    DISCLAIMER
63
64This 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
66of merchantability and fitness for a particular purpose. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR
67CONTRIBUTORS BE LIABLE for any direct, indirect, incidental, special, exemplary, or consequential damages,
68including but not limited to procurement of substitute goods or services; loss of use, data, or profits,
69or business interruption, however caused and on any theory of liability, whether in contract, strict
70liability, or tort (including negligence), arising in any way out of the use of this software, even if
71advised of the possibility of such damage.
72
735.    CONTACT INFORMATION
74
75Fraunhofer Institute for Integrated Circuits IIS
76Attention: Audio and Multimedia Departments - FDK AAC LL
77Am Wolfsmantel 33
7891058 Erlangen, Germany
79
80www.iis.fraunhofer.de/amm
81amm-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
98enum
99{
100  L = 0,
101  R = 1
102};
103
104
105int 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
148void 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
221void 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