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 Audio Encoder **************************
85 
86    Initial author:       M.Werner
87    contents/description: MS stereo processing
88 
89 ******************************************************************************/
90 #include "ms_stereo.h"
91 
92 #include "psy_const.h"
93 
94 /* static const float scaleMinThres = 1.0f; */ /* 0.75f for 3db boost */
95 
FDKaacEnc_MsStereoProcessing(PSY_DATA * RESTRICT psyData[(2)],PSY_OUT_CHANNEL * psyOutChannel[2],const INT * isBook,INT * msDigest,INT * msMask,const INT sfbCnt,const INT sfbPerGroup,const INT maxSfbPerGroup,const INT * sfbOffset)96 void FDKaacEnc_MsStereoProcessing(PSY_DATA   *RESTRICT psyData[(2)],
97                         PSY_OUT_CHANNEL* psyOutChannel[2],
98                         const INT  *isBook,
99                         INT        *msDigest,       /* output */
100                         INT        *msMask,         /* output */
101                         const INT   sfbCnt,
102                         const INT   sfbPerGroup,
103                         const INT   maxSfbPerGroup,
104                         const INT  *sfbOffset)
105 {
106     FIXP_DBL        *sfbEnergyLeft = psyData[0]->sfbEnergy.Long;                       /* modified where msMask==1 */
107     FIXP_DBL        *sfbEnergyRight = psyData[1]->sfbEnergy.Long;                      /* modified where msMask==1 */
108     const FIXP_DBL  *sfbEnergyMid = psyData[0]->sfbEnergyMS.Long;
109     const FIXP_DBL  *sfbEnergySide = psyData[1]->sfbEnergyMS.Long;
110     FIXP_DBL        *sfbThresholdLeft = psyData[0]->sfbThreshold.Long;                 /* modified where msMask==1 */
111     FIXP_DBL        *sfbThresholdRight = psyData[1]->sfbThreshold.Long;                /* modified where msMask==1 */
112 
113     FIXP_DBL        *sfbSpreadEnLeft = psyData[0]->sfbSpreadEnergy.Long;
114     FIXP_DBL        *sfbSpreadEnRight = psyData[1]->sfbSpreadEnergy.Long;
115 
116     FIXP_DBL        *sfbEnergyLeftLdData = psyOutChannel[0]->sfbEnergyLdData;        /* modified where msMask==1 */
117     FIXP_DBL        *sfbEnergyRightLdData = psyOutChannel[1]->sfbEnergyLdData;       /* modified where msMask==1 */
118     FIXP_DBL        *sfbEnergyMidLdData = psyData[0]->sfbEnergyMSLdData;
119     FIXP_DBL        *sfbEnergySideLdData = psyData[1]->sfbEnergyMSLdData;
120     FIXP_DBL        *sfbThresholdLeftLdData = psyOutChannel[0]->sfbThresholdLdData;  /* modified where msMask==1 */
121     FIXP_DBL        *sfbThresholdRightLdData = psyOutChannel[1]->sfbThresholdLdData; /* modified where msMask==1 */
122 
123     FIXP_DBL        *mdctSpectrumLeft = psyData[0]->mdctSpectrum;                      /* modified where msMask==1 */
124     FIXP_DBL        *mdctSpectrumRight = psyData[1]->mdctSpectrum;                     /* modified where msMask==1 */
125 
126     INT             sfb,sfboffs, j;           /* loop counters         */
127     FIXP_DBL        pnlrLdData, pnmsLdData;
128     FIXP_DBL        minThresholdLdData;
129     FIXP_DBL        minThreshold;
130     INT             useMS;
131 
132     INT             msMaskTrueSomewhere = 0;  /* to determine msDigest */
133     INT             numMsMaskFalse = 0;       /* number of non-intensity bands where L/R coding is used */
134 
135     for(sfb=0; sfb<sfbCnt; sfb+=sfbPerGroup) {
136       for(sfboffs=0;sfboffs<maxSfbPerGroup;sfboffs++) {
137 
138         if ( (isBook==NULL) ? 1 : (isBook[sfb+sfboffs] == 0) ) {
139           FIXP_DBL tmp;
140 
141 /*
142           minThreshold=min(sfbThresholdLeft[sfb+sfboffs], sfbThresholdRight[sfb+sfboffs])*scaleMinThres;
143           pnlr = (sfbThresholdLeft[sfb+sfboffs]/
144                  max(sfbEnergyLeft[sfb+sfboffs],sfbThresholdLeft[sfb+sfboffs]))*
145                  (sfbThresholdRight[sfb+sfboffs]/
146                  max(sfbEnergyRight[sfb+sfboffs],sfbThresholdRight[sfb+sfboffs]));
147           pnms = (minThreshold/max(sfbEnergyMid[sfb+sfboffs],minThreshold))*
148                  (minThreshold/max(sfbEnergySide[sfb+sfboffs],minThreshold));
149           useMS = (pnms > pnlr);
150 */
151 
152           /* we assume that scaleMinThres == 1.0f and we can drop it */
153           minThresholdLdData = fixMin(sfbThresholdLeftLdData[sfb+sfboffs], sfbThresholdRightLdData[sfb+sfboffs]);
154 
155           /* pnlrLdData = sfbThresholdLeftLdData[sfb+sfboffs] -
156                         max(sfbEnergyLeftLdData[sfb+sfboffs], sfbThresholdLeftLdData[sfb+sfboffs]) +
157                         sfbThresholdRightLdData[sfb+sfboffs] -
158                         max(sfbEnergyRightLdData[sfb+sfboffs], sfbThresholdRightLdData[sfb+sfboffs]); */
159           tmp = fixMax(sfbEnergyLeftLdData[sfb+sfboffs], sfbThresholdLeftLdData[sfb+sfboffs]);
160           pnlrLdData = (sfbThresholdLeftLdData[sfb+sfboffs]>>1) - (tmp>>1);
161           pnlrLdData = pnlrLdData + (sfbThresholdRightLdData[sfb+sfboffs]>>1);
162           tmp = fixMax(sfbEnergyRightLdData[sfb+sfboffs], sfbThresholdRightLdData[sfb+sfboffs]);
163           pnlrLdData = pnlrLdData - (tmp>>1);
164 
165           /* pnmsLdData = minThresholdLdData - max(sfbEnergyMidLdData[sfb+sfboffs], minThresholdLdData) +
166                         minThresholdLdData - max(sfbEnergySideLdData[sfb+sfboffs], minThresholdLdData); */
167           tmp = fixMax(sfbEnergyMidLdData[sfb+sfboffs], minThresholdLdData);
168           pnmsLdData = minThresholdLdData - (tmp>>1);
169           tmp = fixMax(sfbEnergySideLdData[sfb+sfboffs], minThresholdLdData);
170           pnmsLdData = pnmsLdData - (tmp>>1);
171           useMS = (pnmsLdData > (pnlrLdData));
172 
173 
174           if (useMS) {
175             msMask[sfb+sfboffs] = 1;
176             msMaskTrueSomewhere = 1;
177             for(j=sfbOffset[sfb+sfboffs]; j<sfbOffset[sfb+sfboffs+1]; j++) {
178               FIXP_DBL specL, specR;
179               specL = mdctSpectrumLeft[j]>>1;
180               specR = mdctSpectrumRight[j]>>1;
181               mdctSpectrumLeft[j] = specL + specR;
182               mdctSpectrumRight[j] = specL - specR;
183             }
184             minThreshold = fixMin(sfbThresholdLeft[sfb+sfboffs], sfbThresholdRight[sfb+sfboffs]);
185             sfbThresholdLeft[sfb+sfboffs] = sfbThresholdRight[sfb+sfboffs] = minThreshold;
186             sfbThresholdLeftLdData[sfb+sfboffs] = sfbThresholdRightLdData[sfb+sfboffs] = minThresholdLdData;
187             sfbEnergyLeft[sfb+sfboffs] = sfbEnergyMid[sfb+sfboffs];
188             sfbEnergyRight[sfb+sfboffs] = sfbEnergySide[sfb+sfboffs];
189             sfbEnergyLeftLdData[sfb+sfboffs] = sfbEnergyMidLdData[sfb+sfboffs];
190             sfbEnergyRightLdData[sfb+sfboffs] = sfbEnergySideLdData[sfb+sfboffs];
191 
192             sfbSpreadEnLeft[sfb+sfboffs] = sfbSpreadEnRight[sfb+sfboffs] =
193                      fixMin( sfbSpreadEnLeft[sfb+sfboffs],
194                              sfbSpreadEnRight[sfb+sfboffs] ) >> 1;
195 
196           }
197           else {
198             msMask[sfb+sfboffs] = 0;
199             numMsMaskFalse++;
200           } /* useMS */
201         } /* isBook */
202         else {
203           /* keep mDigest from IS module */
204           if (msMask[sfb+sfboffs]) {
205             msMaskTrueSomewhere = 1;
206           }
207           /* prohibit MS_MASK_ALL in combination with IS */
208           numMsMaskFalse = 9;
209         } /* isBook */
210       } /* sfboffs */
211     } /* sfb */
212 
213 
214     if(msMaskTrueSomewhere == 1) {
215       if ((numMsMaskFalse == 0) || ((numMsMaskFalse < maxSfbPerGroup) && (numMsMaskFalse < 9))) {
216         *msDigest = SI_MS_MASK_ALL;
217         /* loop through M/S bands; if msMask==0, set it to 1 and apply M/S */
218         for (sfb = 0; sfb < sfbCnt; sfb += sfbPerGroup) {
219           for (sfboffs = 0; sfboffs < maxSfbPerGroup; sfboffs++) {
220             if (( (isBook == NULL) ? 1 : (isBook[sfb+sfboffs] == 0) ) && (msMask[sfb+sfboffs] == 0)) {
221               msMask[sfb+sfboffs] = 1;
222               /* apply M/S coding */
223               for(j=sfbOffset[sfb+sfboffs]; j<sfbOffset[sfb+sfboffs+1]; j++) {
224                 FIXP_DBL specL, specR;
225                 specL = mdctSpectrumLeft[j]>>1;
226                 specR = mdctSpectrumRight[j]>>1;
227                 mdctSpectrumLeft[j] = specL + specR;
228                 mdctSpectrumRight[j] = specL - specR;
229               }
230               minThreshold = fixMin(sfbThresholdLeft[sfb+sfboffs], sfbThresholdRight[sfb+sfboffs]);
231               sfbThresholdLeft[sfb+sfboffs] = sfbThresholdRight[sfb+sfboffs] = minThreshold;
232               minThresholdLdData = fixMin(sfbThresholdLeftLdData[sfb+sfboffs], sfbThresholdRightLdData[sfb+sfboffs]);
233               sfbThresholdLeftLdData[sfb+sfboffs] = sfbThresholdRightLdData[sfb+sfboffs] = minThresholdLdData;
234               sfbEnergyLeft[sfb+sfboffs] = sfbEnergyMid[sfb+sfboffs];
235               sfbEnergyRight[sfb+sfboffs] = sfbEnergySide[sfb+sfboffs];
236               sfbEnergyLeftLdData[sfb+sfboffs] = sfbEnergyMidLdData[sfb+sfboffs];
237               sfbEnergyRightLdData[sfb+sfboffs] = sfbEnergySideLdData[sfb+sfboffs];
238 
239               sfbSpreadEnLeft[sfb+sfboffs] = sfbSpreadEnRight[sfb+sfboffs] =
240                        fixMin( sfbSpreadEnLeft[sfb+sfboffs],
241                                sfbSpreadEnRight[sfb+sfboffs] ) >> 1;
242             }
243           }
244         }
245       } else {
246         *msDigest = SI_MS_MASK_SOME;
247       }
248     } else {
249       *msDigest = SI_MS_MASK_NONE;
250     }
251 }
252