/src/aac/libAACenc/src/ms_stereo.cpp
Line | Count | Source (jump to first uncovered line) |
1 | | /* ----------------------------------------------------------------------------- |
2 | | Software License for The Fraunhofer FDK AAC Codec Library for Android |
3 | | |
4 | | © Copyright 1995 - 2018 Fraunhofer-Gesellschaft zur Förderung der angewandten |
5 | | Forschung e.V. All rights reserved. |
6 | | |
7 | | 1. INTRODUCTION |
8 | | The Fraunhofer FDK AAC Codec Library for Android ("FDK AAC Codec") is software |
9 | | that implements the MPEG Advanced Audio Coding ("AAC") encoding and decoding |
10 | | scheme for digital audio. This FDK AAC Codec software is intended to be used on |
11 | | a wide variety of Android devices. |
12 | | |
13 | | AAC's HE-AAC and HE-AAC v2 versions are regarded as today's most efficient |
14 | | general perceptual audio codecs. AAC-ELD is considered the best-performing |
15 | | full-bandwidth communications codec by independent studies and is widely |
16 | | deployed. AAC has been standardized by ISO and IEC as part of the MPEG |
17 | | specifications. |
18 | | |
19 | | Patent licenses for necessary patent claims for the FDK AAC Codec (including |
20 | | those of Fraunhofer) may be obtained through Via Licensing |
21 | | (www.vialicensing.com) or through the respective patent owners individually for |
22 | | the purpose of encoding or decoding bit streams in products that are compliant |
23 | | with the ISO/IEC MPEG audio standards. Please note that most manufacturers of |
24 | | Android devices already license these patent claims through Via Licensing or |
25 | | directly from the patent owners, and therefore FDK AAC Codec software may |
26 | | already be covered under those patent licenses when it is used for those |
27 | | licensed purposes only. |
28 | | |
29 | | Commercially-licensed AAC software libraries, including floating-point versions |
30 | | with enhanced sound quality, are also available from Fraunhofer. Users are |
31 | | encouraged to check the Fraunhofer website for additional applications |
32 | | information and documentation. |
33 | | |
34 | | 2. COPYRIGHT LICENSE |
35 | | |
36 | | Redistribution and use in source and binary forms, with or without modification, |
37 | | are permitted without payment of copyright license fees provided that you |
38 | | satisfy the following conditions: |
39 | | |
40 | | You must retain the complete text of this software license in redistributions of |
41 | | the FDK AAC Codec or your modifications thereto in source code form. |
42 | | |
43 | | You must retain the complete text of this software license in the documentation |
44 | | and/or other materials provided with redistributions of the FDK AAC Codec or |
45 | | your modifications thereto in binary form. You must make available free of |
46 | | charge copies of the complete source code of the FDK AAC Codec and your |
47 | | modifications thereto to recipients of copies in binary form. |
48 | | |
49 | | The name of Fraunhofer may not be used to endorse or promote products derived |
50 | | from this library without prior written permission. |
51 | | |
52 | | You may not charge copyright license fees for anyone to use, copy or distribute |
53 | | the FDK AAC Codec software or your modifications thereto. |
54 | | |
55 | | Your modified versions of the FDK AAC Codec must carry prominent notices stating |
56 | | that you changed the software and the date of any change. For modified versions |
57 | | of the FDK AAC Codec, the term "Fraunhofer FDK AAC Codec Library for Android" |
58 | | must be replaced by the term "Third-Party Modified Version of the Fraunhofer FDK |
59 | | AAC Codec Library for Android." |
60 | | |
61 | | 3. NO PATENT LICENSE |
62 | | |
63 | | NO EXPRESS OR IMPLIED LICENSES TO ANY PATENT CLAIMS, including without |
64 | | limitation the patents of Fraunhofer, ARE GRANTED BY THIS SOFTWARE LICENSE. |
65 | | Fraunhofer provides no warranty of patent non-infringement with respect to this |
66 | | software. |
67 | | |
68 | | You may use this FDK AAC Codec software or modifications thereto only for |
69 | | purposes that are authorized by appropriate patent licenses. |
70 | | |
71 | | 4. DISCLAIMER |
72 | | |
73 | | This FDK AAC Codec software is provided by Fraunhofer on behalf of the copyright |
74 | | holders and contributors "AS IS" and WITHOUT ANY EXPRESS OR IMPLIED WARRANTIES, |
75 | | including but not limited to the implied warranties of merchantability and |
76 | | fitness for a particular purpose. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR |
77 | | CONTRIBUTORS BE LIABLE for any direct, indirect, incidental, special, exemplary, |
78 | | or consequential damages, including but not limited to procurement of substitute |
79 | | goods or services; loss of use, data, or profits, or business interruption, |
80 | | however caused and on any theory of liability, whether in contract, strict |
81 | | liability, or tort (including negligence), arising in any way out of the use of |
82 | | this software, even if advised of the possibility of such damage. |
83 | | |
84 | | 5. CONTACT INFORMATION |
85 | | |
86 | | Fraunhofer Institute for Integrated Circuits IIS |
87 | | Attention: Audio and Multimedia Departments - FDK AAC LL |
88 | | Am Wolfsmantel 33 |
89 | | 91058 Erlangen, Germany |
90 | | |
91 | | www.iis.fraunhofer.de/amm |
92 | | amm-info@iis.fraunhofer.de |
93 | | ----------------------------------------------------------------------------- */ |
94 | | |
95 | | /**************************** AAC encoder library ****************************** |
96 | | |
97 | | Author(s): M.Werner |
98 | | |
99 | | Description: MS stereo processing |
100 | | |
101 | | *******************************************************************************/ |
102 | | |
103 | | #include "ms_stereo.h" |
104 | | |
105 | | #include "psy_const.h" |
106 | | |
107 | | /* static const float scaleMinThres = 1.0f; */ /* 0.75f for 3db boost */ |
108 | | |
109 | | void FDKaacEnc_MsStereoProcessing(PSY_DATA *RESTRICT psyData[(2)], |
110 | | PSY_OUT_CHANNEL *psyOutChannel[2], |
111 | | const INT *isBook, INT *msDigest, /* output */ |
112 | | INT *msMask, /* output */ |
113 | | const INT allowMS, const INT sfbCnt, |
114 | | const INT sfbPerGroup, |
115 | | const INT maxSfbPerGroup, |
116 | 0 | const INT *sfbOffset) { |
117 | 0 | FIXP_DBL *sfbEnergyLeft = |
118 | 0 | psyData[0]->sfbEnergy.Long; /* modified where msMask==1 */ |
119 | 0 | FIXP_DBL *sfbEnergyRight = |
120 | 0 | psyData[1]->sfbEnergy.Long; /* modified where msMask==1 */ |
121 | 0 | const FIXP_DBL *sfbEnergyMid = psyData[0]->sfbEnergyMS.Long; |
122 | 0 | const FIXP_DBL *sfbEnergySide = psyData[1]->sfbEnergyMS.Long; |
123 | 0 | FIXP_DBL *sfbThresholdLeft = |
124 | 0 | psyData[0]->sfbThreshold.Long; /* modified where msMask==1 */ |
125 | 0 | FIXP_DBL *sfbThresholdRight = |
126 | 0 | psyData[1]->sfbThreshold.Long; /* modified where msMask==1 */ |
127 | |
|
128 | 0 | FIXP_DBL *sfbSpreadEnLeft = psyData[0]->sfbSpreadEnergy.Long; |
129 | 0 | FIXP_DBL *sfbSpreadEnRight = psyData[1]->sfbSpreadEnergy.Long; |
130 | |
|
131 | 0 | FIXP_DBL *sfbEnergyLeftLdData = |
132 | 0 | psyOutChannel[0]->sfbEnergyLdData; /* modified where msMask==1 */ |
133 | 0 | FIXP_DBL *sfbEnergyRightLdData = |
134 | 0 | psyOutChannel[1]->sfbEnergyLdData; /* modified where msMask==1 */ |
135 | 0 | FIXP_DBL *sfbEnergyMidLdData = psyData[0]->sfbEnergyMSLdData; |
136 | 0 | FIXP_DBL *sfbEnergySideLdData = psyData[1]->sfbEnergyMSLdData; |
137 | 0 | FIXP_DBL *sfbThresholdLeftLdData = |
138 | 0 | psyOutChannel[0]->sfbThresholdLdData; /* modified where msMask==1 */ |
139 | 0 | FIXP_DBL *sfbThresholdRightLdData = |
140 | 0 | psyOutChannel[1]->sfbThresholdLdData; /* modified where msMask==1 */ |
141 | |
|
142 | 0 | FIXP_DBL *mdctSpectrumLeft = |
143 | 0 | psyData[0]->mdctSpectrum; /* modified where msMask==1 */ |
144 | 0 | FIXP_DBL *mdctSpectrumRight = |
145 | 0 | psyData[1]->mdctSpectrum; /* modified where msMask==1 */ |
146 | |
|
147 | 0 | INT sfb, sfboffs, j; /* loop counters */ |
148 | 0 | FIXP_DBL pnlrLdData, pnmsLdData; |
149 | 0 | FIXP_DBL minThresholdLdData; |
150 | 0 | FIXP_DBL minThreshold; |
151 | 0 | INT useMS; |
152 | |
|
153 | 0 | INT msMaskTrueSomewhere = 0; /* to determine msDigest */ |
154 | 0 | INT numMsMaskFalse = |
155 | 0 | 0; /* number of non-intensity bands where L/R coding is used */ |
156 | |
|
157 | 0 | for (sfb = 0; sfb < sfbCnt; sfb += sfbPerGroup) { |
158 | 0 | for (sfboffs = 0; sfboffs < maxSfbPerGroup; sfboffs++) { |
159 | 0 | if ((isBook == NULL) ? 1 : (isBook[sfb + sfboffs] == 0)) { |
160 | 0 | FIXP_DBL tmp; |
161 | | |
162 | | /* |
163 | | minThreshold=min(sfbThresholdLeft[sfb+sfboffs], |
164 | | sfbThresholdRight[sfb+sfboffs])*scaleMinThres; pnlr = |
165 | | (sfbThresholdLeft[sfb+sfboffs]/ |
166 | | max(sfbEnergyLeft[sfb+sfboffs],sfbThresholdLeft[sfb+sfboffs]))* |
167 | | (sfbThresholdRight[sfb+sfboffs]/ |
168 | | max(sfbEnergyRight[sfb+sfboffs],sfbThresholdRight[sfb+sfboffs])); |
169 | | pnms = |
170 | | (minThreshold/max(sfbEnergyMid[sfb+sfboffs],minThreshold))* |
171 | | (minThreshold/max(sfbEnergySide[sfb+sfboffs],minThreshold)); |
172 | | useMS = (pnms > pnlr); |
173 | | */ |
174 | | |
175 | | /* we assume that scaleMinThres == 1.0f and we can drop it */ |
176 | 0 | minThresholdLdData = fixMin(sfbThresholdLeftLdData[sfb + sfboffs], |
177 | 0 | sfbThresholdRightLdData[sfb + sfboffs]); |
178 | | |
179 | | /* pnlrLdData = sfbThresholdLeftLdData[sfb+sfboffs] - |
180 | | max(sfbEnergyLeftLdData[sfb+sfboffs], |
181 | | sfbThresholdLeftLdData[sfb+sfboffs]) + |
182 | | sfbThresholdRightLdData[sfb+sfboffs] - |
183 | | max(sfbEnergyRightLdData[sfb+sfboffs], |
184 | | sfbThresholdRightLdData[sfb+sfboffs]); */ |
185 | 0 | tmp = fixMax(sfbEnergyLeftLdData[sfb + sfboffs], |
186 | 0 | sfbThresholdLeftLdData[sfb + sfboffs]); |
187 | 0 | pnlrLdData = (sfbThresholdLeftLdData[sfb + sfboffs] >> 1) - (tmp >> 1); |
188 | 0 | pnlrLdData = pnlrLdData + (sfbThresholdRightLdData[sfb + sfboffs] >> 1); |
189 | 0 | tmp = fixMax(sfbEnergyRightLdData[sfb + sfboffs], |
190 | 0 | sfbThresholdRightLdData[sfb + sfboffs]); |
191 | 0 | pnlrLdData = pnlrLdData - (tmp >> 1); |
192 | | |
193 | | /* pnmsLdData = minThresholdLdData - |
194 | | max(sfbEnergyMidLdData[sfb+sfboffs], minThresholdLdData) + |
195 | | minThresholdLdData - max(sfbEnergySideLdData[sfb+sfboffs], |
196 | | minThresholdLdData); */ |
197 | 0 | tmp = fixMax(sfbEnergyMidLdData[sfb + sfboffs], minThresholdLdData); |
198 | 0 | pnmsLdData = minThresholdLdData - (tmp >> 1); |
199 | 0 | tmp = fixMax(sfbEnergySideLdData[sfb + sfboffs], minThresholdLdData); |
200 | 0 | pnmsLdData = pnmsLdData - (tmp >> 1); |
201 | 0 | useMS = ((allowMS != 0) && (pnmsLdData > pnlrLdData)) ? 1 : 0; |
202 | |
|
203 | 0 | if (useMS) { |
204 | 0 | msMask[sfb + sfboffs] = 1; |
205 | 0 | msMaskTrueSomewhere = 1; |
206 | 0 | for (j = sfbOffset[sfb + sfboffs]; j < sfbOffset[sfb + sfboffs + 1]; |
207 | 0 | j++) { |
208 | 0 | FIXP_DBL specL, specR; |
209 | 0 | specL = mdctSpectrumLeft[j] >> 1; |
210 | 0 | specR = mdctSpectrumRight[j] >> 1; |
211 | 0 | mdctSpectrumLeft[j] = specL + specR; |
212 | 0 | mdctSpectrumRight[j] = specL - specR; |
213 | 0 | } |
214 | 0 | minThreshold = fixMin(sfbThresholdLeft[sfb + sfboffs], |
215 | 0 | sfbThresholdRight[sfb + sfboffs]); |
216 | 0 | sfbThresholdLeft[sfb + sfboffs] = sfbThresholdRight[sfb + sfboffs] = |
217 | 0 | minThreshold; |
218 | 0 | sfbThresholdLeftLdData[sfb + sfboffs] = |
219 | 0 | sfbThresholdRightLdData[sfb + sfboffs] = minThresholdLdData; |
220 | 0 | sfbEnergyLeft[sfb + sfboffs] = sfbEnergyMid[sfb + sfboffs]; |
221 | 0 | sfbEnergyRight[sfb + sfboffs] = sfbEnergySide[sfb + sfboffs]; |
222 | 0 | sfbEnergyLeftLdData[sfb + sfboffs] = |
223 | 0 | sfbEnergyMidLdData[sfb + sfboffs]; |
224 | 0 | sfbEnergyRightLdData[sfb + sfboffs] = |
225 | 0 | sfbEnergySideLdData[sfb + sfboffs]; |
226 | |
|
227 | 0 | sfbSpreadEnLeft[sfb + sfboffs] = sfbSpreadEnRight[sfb + sfboffs] = |
228 | 0 | fixMin(sfbSpreadEnLeft[sfb + sfboffs], |
229 | 0 | sfbSpreadEnRight[sfb + sfboffs]) >> |
230 | 0 | 1; |
231 | |
|
232 | 0 | } else { |
233 | 0 | msMask[sfb + sfboffs] = 0; |
234 | 0 | numMsMaskFalse++; |
235 | 0 | } /* useMS */ |
236 | 0 | } /* isBook */ |
237 | 0 | else { |
238 | | /* keep mDigest from IS module */ |
239 | 0 | if (msMask[sfb + sfboffs]) { |
240 | 0 | msMaskTrueSomewhere = 1; |
241 | 0 | } |
242 | | /* prohibit MS_MASK_ALL in combination with IS */ |
243 | 0 | numMsMaskFalse = 9; |
244 | 0 | } /* isBook */ |
245 | 0 | } /* sfboffs */ |
246 | 0 | } /* sfb */ |
247 | |
|
248 | 0 | if (msMaskTrueSomewhere == 1) { |
249 | 0 | if ((numMsMaskFalse == 0) || |
250 | 0 | ((numMsMaskFalse < maxSfbPerGroup) && (numMsMaskFalse < 9))) { |
251 | 0 | *msDigest = SI_MS_MASK_ALL; |
252 | | /* loop through M/S bands; if msMask==0, set it to 1 and apply M/S */ |
253 | 0 | for (sfb = 0; sfb < sfbCnt; sfb += sfbPerGroup) { |
254 | 0 | for (sfboffs = 0; sfboffs < maxSfbPerGroup; sfboffs++) { |
255 | 0 | if (((isBook == NULL) ? 1 : (isBook[sfb + sfboffs] == 0)) && |
256 | 0 | (msMask[sfb + sfboffs] == 0)) { |
257 | 0 | msMask[sfb + sfboffs] = 1; |
258 | | /* apply M/S coding */ |
259 | 0 | for (j = sfbOffset[sfb + sfboffs]; j < sfbOffset[sfb + sfboffs + 1]; |
260 | 0 | j++) { |
261 | 0 | FIXP_DBL specL, specR; |
262 | 0 | specL = mdctSpectrumLeft[j] >> 1; |
263 | 0 | specR = mdctSpectrumRight[j] >> 1; |
264 | 0 | mdctSpectrumLeft[j] = specL + specR; |
265 | 0 | mdctSpectrumRight[j] = specL - specR; |
266 | 0 | } |
267 | 0 | minThreshold = fixMin(sfbThresholdLeft[sfb + sfboffs], |
268 | 0 | sfbThresholdRight[sfb + sfboffs]); |
269 | 0 | sfbThresholdLeft[sfb + sfboffs] = sfbThresholdRight[sfb + sfboffs] = |
270 | 0 | minThreshold; |
271 | 0 | minThresholdLdData = fixMin(sfbThresholdLeftLdData[sfb + sfboffs], |
272 | 0 | sfbThresholdRightLdData[sfb + sfboffs]); |
273 | 0 | sfbThresholdLeftLdData[sfb + sfboffs] = |
274 | 0 | sfbThresholdRightLdData[sfb + sfboffs] = minThresholdLdData; |
275 | 0 | sfbEnergyLeft[sfb + sfboffs] = sfbEnergyMid[sfb + sfboffs]; |
276 | 0 | sfbEnergyRight[sfb + sfboffs] = sfbEnergySide[sfb + sfboffs]; |
277 | 0 | sfbEnergyLeftLdData[sfb + sfboffs] = |
278 | 0 | sfbEnergyMidLdData[sfb + sfboffs]; |
279 | 0 | sfbEnergyRightLdData[sfb + sfboffs] = |
280 | 0 | sfbEnergySideLdData[sfb + sfboffs]; |
281 | |
|
282 | 0 | sfbSpreadEnLeft[sfb + sfboffs] = sfbSpreadEnRight[sfb + sfboffs] = |
283 | 0 | fixMin(sfbSpreadEnLeft[sfb + sfboffs], |
284 | 0 | sfbSpreadEnRight[sfb + sfboffs]) >> |
285 | 0 | 1; |
286 | 0 | } |
287 | 0 | } |
288 | 0 | } |
289 | 0 | } else { |
290 | 0 | *msDigest = SI_MS_MASK_SOME; |
291 | 0 | } |
292 | 0 | } else { |
293 | 0 | *msDigest = SI_MS_MASK_NONE; |
294 | 0 | } |
295 | 0 | } |