/src/aac/libAACdec/src/aacdec_drc.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 - 2020 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 decoder library ****************************** |
96 | | |
97 | | Author(s): Christian Griebel |
98 | | |
99 | | Description: Dynamic range control (DRC) decoder tool for AAC |
100 | | |
101 | | *******************************************************************************/ |
102 | | |
103 | | #include "aacdec_drc.h" |
104 | | |
105 | | #include "channelinfo.h" |
106 | | #include "aac_rom.h" |
107 | | |
108 | | #include "sbrdecoder.h" |
109 | | |
110 | | /* |
111 | | * Dynamic Range Control |
112 | | */ |
113 | | |
114 | | /* For parameter conversion */ |
115 | 0 | #define DRC_PARAMETER_BITS (7) |
116 | 0 | #define DRC_MAX_QUANT_STEPS (1 << DRC_PARAMETER_BITS) |
117 | 0 | #define DRC_MAX_QUANT_FACTOR (DRC_MAX_QUANT_STEPS - 1) |
118 | | #define DRC_PARAM_QUANT_STEP \ |
119 | 1.60k | (FL2FXCONST_DBL(1.0f / (float)DRC_MAX_QUANT_FACTOR)) |
120 | 163k | #define DRC_PARAM_SCALE (1) |
121 | | #define DRC_SCALING_MAX \ |
122 | 1.60k | ((FIXP_DBL)((INT)(DRC_PARAM_QUANT_STEP >> DRC_PARAM_SCALE) * (INT)127)) |
123 | | |
124 | 306k | #define DRC_BLOCK_LEN (1024) |
125 | 306k | #define DRC_BAND_MULT (4) |
126 | 306k | #define DRC_BLOCK_LEN_DIV_BAND_MULT (DRC_BLOCK_LEN / DRC_BAND_MULT) |
127 | | |
128 | 28.7k | #define MAX_REFERENCE_LEVEL (127) |
129 | | |
130 | 0 | #define DRC_HEAVY_THRESHOLD_DB (10) |
131 | | |
132 | 40.1k | #define DVB_ANC_DATA_SYNC_BYTE (0xBC) /* DVB ancillary data sync byte. */ |
133 | | |
134 | 86.2k | #define OFF 0 |
135 | 1.45M | #define ON 1 |
136 | | |
137 | 0 | static INT convert_drcParam(FIXP_DBL param_dbl) { |
138 | | /* converts an internal DRC boost/cut scaling factor in FIXP_DBL |
139 | | (which is downscaled by DRC_PARAM_SCALE) |
140 | | back to an integer value between 0 and 127. */ |
141 | 0 | LONG param_long; |
142 | |
|
143 | 0 | param_long = (LONG)param_dbl >> 7; |
144 | 0 | param_long = param_long * (INT)DRC_MAX_QUANT_FACTOR; |
145 | 0 | param_long >>= 31 - 7 - DRC_PARAM_SCALE - 1; |
146 | 0 | param_long += 1; /* for rounding */ |
147 | 0 | param_long >>= 1; |
148 | |
|
149 | 0 | return (INT)param_long; |
150 | 0 | } |
151 | | |
152 | | /*! |
153 | | \brief Disable DRC |
154 | | |
155 | | \self Handle of DRC info |
156 | | |
157 | | \return none |
158 | | */ |
159 | 641k | void aacDecoder_drcDisable(HANDLE_AAC_DRC self) { |
160 | 641k | self->enable = 0; |
161 | 641k | self->applyExtGain = 0; |
162 | 641k | self->progRefLevelPresent = 0; |
163 | 641k | } |
164 | | |
165 | | /*! |
166 | | \brief Reset DRC information |
167 | | |
168 | | \self Handle of DRC info |
169 | | |
170 | | \return none |
171 | | */ |
172 | 28.7k | void aacDecoder_drcReset(HANDLE_AAC_DRC self) { |
173 | 28.7k | self->applyExtGain = 0; |
174 | 28.7k | self->additionalGainPrev = AACDEC_DRC_GAIN_INIT_VALUE; |
175 | 28.7k | self->additionalGainFilterState = AACDEC_DRC_GAIN_INIT_VALUE; |
176 | 28.7k | self->additionalGainFilterState1 = AACDEC_DRC_GAIN_INIT_VALUE; |
177 | 28.7k | } |
178 | | |
179 | | /*! |
180 | | \brief Initialize DRC information |
181 | | |
182 | | \self Handle of DRC info |
183 | | |
184 | | \return none |
185 | | */ |
186 | 28.7k | void aacDecoder_drcInit(HANDLE_AAC_DRC self) { |
187 | 28.7k | CDrcParams *pParams; |
188 | | |
189 | 28.7k | if (self == NULL) { |
190 | 0 | return; |
191 | 0 | } |
192 | | |
193 | | /* init control fields */ |
194 | 28.7k | self->enable = OFF; |
195 | 28.7k | self->numThreads = 0; |
196 | | |
197 | | /* init params */ |
198 | 28.7k | pParams = &self->params; |
199 | 28.7k | pParams->bsDelayEnable = 0; |
200 | 28.7k | pParams->cut = FL2FXCONST_DBL(0.0f); |
201 | 28.7k | pParams->usrCut = FL2FXCONST_DBL(0.0f); |
202 | 28.7k | pParams->boost = FL2FXCONST_DBL(0.0f); |
203 | 28.7k | pParams->usrBoost = FL2FXCONST_DBL(0.0f); |
204 | 28.7k | pParams->targetRefLevel = 96; |
205 | 28.7k | pParams->expiryFrame = AACDEC_DRC_DFLT_EXPIRY_FRAMES; |
206 | 28.7k | pParams->applyHeavyCompression = OFF; |
207 | 28.7k | pParams->usrApplyHeavyCompression = OFF; |
208 | | |
209 | 28.7k | pParams->defaultPresentationMode = DISABLED_PARAMETER_HANDLING; |
210 | 28.7k | pParams->encoderTargetLevel = MAX_REFERENCE_LEVEL; /* worst case assumption */ |
211 | | |
212 | 28.7k | self->update = 1; |
213 | 28.7k | self->numOutChannels = 0; |
214 | 28.7k | self->prevAacNumChannels = 0; |
215 | | |
216 | | /* initial program ref level = target ref level */ |
217 | 28.7k | self->progRefLevel = pParams->targetRefLevel; |
218 | 28.7k | self->progRefLevelPresent = 0; |
219 | 28.7k | self->presMode = -1; |
220 | 28.7k | self->uniDrcPrecedence = 0; |
221 | | |
222 | 28.7k | aacDecoder_drcReset(self); |
223 | 28.7k | } |
224 | | |
225 | | /*! |
226 | | \brief Initialize DRC control data for one channel |
227 | | |
228 | | \self Handle of DRC info |
229 | | |
230 | | \return none |
231 | | */ |
232 | 255k | void aacDecoder_drcInitChannelData(CDrcChannelData *pDrcChData) { |
233 | 255k | if (pDrcChData != NULL) { |
234 | 255k | pDrcChData->expiryCount = 0; |
235 | 255k | pDrcChData->numBands = 1; |
236 | 255k | pDrcChData->bandTop[0] = DRC_BLOCK_LEN_DIV_BAND_MULT - 1; |
237 | 255k | pDrcChData->drcValue[0] = 0; |
238 | 255k | pDrcChData->drcInterpolationScheme = 0; |
239 | 255k | pDrcChData->drcDataType = UNKNOWN_PAYLOAD; |
240 | 255k | } |
241 | 255k | } |
242 | | |
243 | | /*! |
244 | | \brief Set one single DRC parameter |
245 | | |
246 | | \self Handle of DRC info. |
247 | | \param Parameter to be set. |
248 | | \value Value to be set. |
249 | | |
250 | | \return an error code. |
251 | | */ |
252 | | AAC_DECODER_ERROR aacDecoder_drcSetParam(HANDLE_AAC_DRC self, |
253 | 973k | AACDEC_DRC_PARAM param, INT value) { |
254 | 973k | AAC_DECODER_ERROR ErrorStatus = AAC_DEC_OK; |
255 | | |
256 | 973k | switch (param) { |
257 | 0 | case DRC_CUT_SCALE: |
258 | | /* set attenuation scale factor */ |
259 | 0 | if ((value < 0) || (value > DRC_MAX_QUANT_FACTOR)) { |
260 | 0 | return AAC_DEC_SET_PARAM_FAIL; |
261 | 0 | } |
262 | 0 | if (self == NULL) { |
263 | 0 | return AAC_DEC_INVALID_HANDLE; |
264 | 0 | } |
265 | 0 | self->params.usrCut = (FIXP_DBL)( |
266 | 0 | (INT)(DRC_PARAM_QUANT_STEP >> DRC_PARAM_SCALE) * (INT)value); |
267 | 0 | self->update = 1; |
268 | 0 | break; |
269 | 0 | case DRC_BOOST_SCALE: |
270 | | /* set boost factor */ |
271 | 0 | if ((value < 0) || (value > DRC_MAX_QUANT_FACTOR)) { |
272 | 0 | return AAC_DEC_SET_PARAM_FAIL; |
273 | 0 | } |
274 | 0 | if (self == NULL) { |
275 | 0 | return AAC_DEC_INVALID_HANDLE; |
276 | 0 | } |
277 | 0 | self->params.usrBoost = (FIXP_DBL)( |
278 | 0 | (INT)(DRC_PARAM_QUANT_STEP >> DRC_PARAM_SCALE) * (INT)value); |
279 | 0 | self->update = 1; |
280 | 0 | break; |
281 | 0 | case TARGET_REF_LEVEL: |
282 | 0 | if (value > MAX_REFERENCE_LEVEL || value < -MAX_REFERENCE_LEVEL) { |
283 | 0 | return AAC_DEC_SET_PARAM_FAIL; |
284 | 0 | } |
285 | 0 | if (self == NULL) { |
286 | 0 | return AAC_DEC_INVALID_HANDLE; |
287 | 0 | } |
288 | 0 | if (value < 0) { |
289 | 0 | self->params.targetRefLevel = -1; |
290 | 0 | } else { |
291 | 0 | if (self->params.targetRefLevel != (SCHAR)value) { |
292 | 0 | self->params.targetRefLevel = (SCHAR)value; |
293 | 0 | self->progRefLevel = (SCHAR)value; /* Always set the program reference |
294 | | level equal to the target level |
295 | | according to 4.5.2.7.3 of |
296 | | ISO/IEC 14496-3. */ |
297 | 0 | } |
298 | 0 | self->update = 1; |
299 | 0 | } |
300 | 0 | break; |
301 | 0 | case APPLY_HEAVY_COMPRESSION: |
302 | 0 | if ((value != OFF) && (value != ON)) { |
303 | 0 | return AAC_DEC_SET_PARAM_FAIL; |
304 | 0 | } |
305 | 0 | if (self == NULL) { |
306 | 0 | return AAC_DEC_INVALID_HANDLE; |
307 | 0 | } |
308 | | /* Store new parameter value */ |
309 | 0 | self->params.usrApplyHeavyCompression = (UCHAR)value; |
310 | 0 | self->update = 1; |
311 | 0 | break; |
312 | 0 | case DEFAULT_PRESENTATION_MODE: |
313 | 0 | if (value < AAC_DRC_PARAMETER_HANDLING_DISABLED || |
314 | 0 | value > AAC_DRC_PRESENTATION_MODE_2_DEFAULT) { |
315 | 0 | return AAC_DEC_SET_PARAM_FAIL; |
316 | 0 | } |
317 | 0 | if (self == NULL) { |
318 | 0 | return AAC_DEC_INVALID_HANDLE; |
319 | 0 | } |
320 | 0 | self->params.defaultPresentationMode = |
321 | 0 | (AACDEC_DRC_PARAMETER_HANDLING)value; |
322 | 0 | self->update = 1; |
323 | 0 | break; |
324 | 0 | case ENCODER_TARGET_LEVEL: |
325 | 0 | if (value > MAX_REFERENCE_LEVEL || value < 0) { |
326 | 0 | return AAC_DEC_SET_PARAM_FAIL; |
327 | 0 | } |
328 | 0 | if (self == NULL) { |
329 | 0 | return AAC_DEC_INVALID_HANDLE; |
330 | 0 | } |
331 | 0 | self->params.encoderTargetLevel = (UCHAR)value; |
332 | 0 | self->update = 1; |
333 | 0 | break; |
334 | 75.7k | case DRC_BS_DELAY: |
335 | 75.7k | if (value < 0 || value > 1) { |
336 | 0 | return AAC_DEC_SET_PARAM_FAIL; |
337 | 0 | } |
338 | 75.7k | if (self == NULL) { |
339 | 0 | return AAC_DEC_INVALID_HANDLE; |
340 | 0 | } |
341 | 75.7k | self->params.bsDelayEnable = value; |
342 | 75.7k | break; |
343 | 410k | case DRC_DATA_EXPIRY_FRAME: |
344 | 410k | if (self == NULL) { |
345 | 0 | return AAC_DEC_INVALID_HANDLE; |
346 | 0 | } |
347 | 410k | self->params.expiryFrame = (value > 0) ? (UINT)value : 0; |
348 | 410k | break; |
349 | 0 | case MAX_OUTPUT_CHANNELS: |
350 | 0 | if (self == NULL) { |
351 | 0 | return AAC_DEC_INVALID_HANDLE; |
352 | 0 | } |
353 | 0 | self->numOutChannels = (INT)value; |
354 | 0 | self->update = 1; |
355 | 0 | break; |
356 | 487k | case UNIDRC_PRECEDENCE: |
357 | 487k | if (self == NULL) { |
358 | 0 | return AAC_DEC_INVALID_HANDLE; |
359 | 0 | } |
360 | 487k | self->uniDrcPrecedence = (UCHAR)value; |
361 | 487k | break; |
362 | 0 | default: |
363 | 0 | return AAC_DEC_SET_PARAM_FAIL; |
364 | 973k | } /* switch(param) */ |
365 | | |
366 | 973k | return ErrorStatus; |
367 | 973k | } |
368 | | |
369 | | static int parseExcludedChannels(UINT *excludedChnsMask, |
370 | 36.1k | HANDLE_FDK_BITSTREAM bs) { |
371 | 36.1k | UINT excludeMask = 0; |
372 | 36.1k | UINT i, j; |
373 | 36.1k | int bitCnt = 9; |
374 | | |
375 | 289k | for (i = 0, j = 1; i < 7; i++, j <<= 1) { |
376 | 253k | if (FDKreadBits(bs, 1)) { |
377 | 222k | excludeMask |= j; |
378 | 222k | } |
379 | 253k | } |
380 | | |
381 | | /* additional_excluded_chns */ |
382 | 79.8k | while (FDKreadBits(bs, 1)) { |
383 | 349k | for (i = 0; i < 7; i++, j <<= 1) { |
384 | 305k | if (FDKreadBits(bs, 1)) { |
385 | 208k | excludeMask |= j; |
386 | 208k | } |
387 | 305k | } |
388 | 43.6k | bitCnt += 9; |
389 | 43.6k | FDK_ASSERT(j < (UINT)-1); |
390 | 43.6k | } |
391 | | |
392 | 36.1k | *excludedChnsMask = excludeMask; |
393 | | |
394 | 36.1k | return (bitCnt); |
395 | 36.1k | } |
396 | | |
397 | | /*! |
398 | | \brief Save DRC payload bitstream position |
399 | | |
400 | | \self Handle of DRC info |
401 | | \bs Handle of FDK bitstream |
402 | | |
403 | | \return The number of DRC payload bits |
404 | | */ |
405 | | int aacDecoder_drcMarkPayload(HANDLE_AAC_DRC self, HANDLE_FDK_BITSTREAM bs, |
406 | 220k | AACDEC_DRC_PAYLOAD_TYPE type) { |
407 | 220k | UINT bsStartPos; |
408 | 220k | int i, numBands = 1, bitCnt = 0; |
409 | | |
410 | 220k | if (self == NULL) { |
411 | 0 | return 0; |
412 | 0 | } |
413 | | |
414 | 220k | bsStartPos = FDKgetValidBits(bs); |
415 | | |
416 | 220k | switch (type) { |
417 | 183k | case MPEG_DRC_EXT_DATA: { |
418 | 183k | bitCnt = 4; |
419 | | |
420 | 183k | if (FDKreadBits(bs, 1)) { /* pce_tag_present */ |
421 | 10.0k | FDKreadBits(bs, 8); /* pce_instance_tag + drc_tag_reserved_bits */ |
422 | 10.0k | bitCnt += 8; |
423 | 10.0k | } |
424 | | |
425 | 183k | if (FDKreadBits(bs, 1)) { /* excluded_chns_present */ |
426 | 176k | FDKreadBits(bs, 7); /* exclude mask [0..7] */ |
427 | 176k | bitCnt += 8; |
428 | 373k | while (FDKreadBits(bs, 1)) { /* additional_excluded_chns */ |
429 | 196k | FDKreadBits(bs, 7); /* exclude mask [x..y] */ |
430 | 196k | bitCnt += 8; |
431 | 196k | } |
432 | 176k | } |
433 | | |
434 | 183k | if (FDKreadBits(bs, 1)) { /* drc_bands_present */ |
435 | 173k | numBands += FDKreadBits(bs, 4); /* drc_band_incr */ |
436 | 173k | FDKreadBits(bs, 4); /* reserved */ |
437 | 173k | bitCnt += 8; |
438 | 1.39M | for (i = 0; i < numBands; i++) { |
439 | 1.22M | FDKreadBits(bs, 8); /* drc_band_top[i] */ |
440 | 1.22M | bitCnt += 8; |
441 | 1.22M | } |
442 | 173k | } |
443 | | |
444 | 183k | if (FDKreadBits(bs, 1)) { /* prog_ref_level_present */ |
445 | 173k | FDKreadBits(bs, 8); /* prog_ref_level + prog_ref_level_reserved_bits */ |
446 | 173k | bitCnt += 8; |
447 | 173k | } |
448 | | |
449 | 1.41M | for (i = 0; i < numBands; i++) { |
450 | 1.22M | FDKreadBits(bs, 8); /* dyn_rng_sgn[i] + dyn_rng_ctl[i] */ |
451 | 1.22M | bitCnt += 8; |
452 | 1.22M | } |
453 | | |
454 | 183k | if ((self->numPayloads < MAX_DRC_THREADS) && |
455 | 183k | ((INT)FDKgetValidBits(bs) >= 0)) { |
456 | 40.4k | self->drcPayloadPosition[self->numPayloads++] = bsStartPos; |
457 | 40.4k | } |
458 | 183k | } break; |
459 | | |
460 | 37.6k | case DVB_DRC_ANC_DATA: |
461 | 37.6k | bitCnt += 8; |
462 | | /* check sync word */ |
463 | 37.6k | if (FDKreadBits(bs, 8) == DVB_ANC_DATA_SYNC_BYTE) { |
464 | 9.40k | int dmxLevelsPresent, compressionPresent; |
465 | 9.40k | int coarseGrainTcPresent, fineGrainTcPresent; |
466 | | |
467 | | /* bs_info field */ |
468 | 9.40k | FDKreadBits( |
469 | 9.40k | bs, |
470 | 9.40k | 8); /* mpeg_audio_type, dolby_surround_mode, presentation_mode */ |
471 | 9.40k | bitCnt += 8; |
472 | | |
473 | | /* Evaluate ancillary_data_status */ |
474 | 9.40k | FDKreadBits(bs, 3); /* reserved, set to 0 */ |
475 | 9.40k | dmxLevelsPresent = |
476 | 9.40k | FDKreadBits(bs, 1); /* downmixing_levels_MPEG4_status */ |
477 | 9.40k | FDKreadBits(bs, 1); /* reserved, set to 0 */ |
478 | 9.40k | compressionPresent = |
479 | 9.40k | FDKreadBits(bs, 1); /* audio_coding_mode_and_compression status */ |
480 | 9.40k | coarseGrainTcPresent = |
481 | 9.40k | FDKreadBits(bs, 1); /* coarse_grain_timecode_status */ |
482 | 9.40k | fineGrainTcPresent = |
483 | 9.40k | FDKreadBits(bs, 1); /* fine_grain_timecode_status */ |
484 | 9.40k | bitCnt += 8; |
485 | | |
486 | | /* MPEG4 downmixing levels */ |
487 | 9.40k | if (dmxLevelsPresent) { |
488 | 5.96k | FDKreadBits(bs, 8); /* downmixing_levels_MPEG4 */ |
489 | 5.96k | bitCnt += 8; |
490 | 5.96k | } |
491 | | /* audio coding mode and compression status */ |
492 | 9.40k | if (compressionPresent) { |
493 | 3.46k | FDKreadBits(bs, 16); /* audio_coding_mode, Compression_value */ |
494 | 3.46k | bitCnt += 16; |
495 | 3.46k | } |
496 | | /* coarse grain timecode */ |
497 | 9.40k | if (coarseGrainTcPresent) { |
498 | 3.41k | FDKreadBits(bs, 16); /* coarse_grain_timecode */ |
499 | 3.41k | bitCnt += 16; |
500 | 3.41k | } |
501 | | /* fine grain timecode */ |
502 | 9.40k | if (fineGrainTcPresent) { |
503 | 2.88k | FDKreadBits(bs, 16); /* fine_grain_timecode */ |
504 | 2.88k | bitCnt += 16; |
505 | 2.88k | } |
506 | 9.40k | if (!self->dvbAncDataAvailable && ((INT)FDKgetValidBits(bs) >= 0)) { |
507 | 2.55k | self->dvbAncDataPosition = bsStartPos; |
508 | 2.55k | self->dvbAncDataAvailable = 1; |
509 | 2.55k | } |
510 | 9.40k | } |
511 | 37.6k | break; |
512 | | |
513 | 0 | default: |
514 | 0 | break; |
515 | 220k | } |
516 | | |
517 | 220k | return (bitCnt); |
518 | 220k | } |
519 | | |
520 | | /*! |
521 | | \brief Parse DRC parameters from bitstream |
522 | | |
523 | | \bs Handle of FDK bitstream (in) |
524 | | \pDrcBs Pointer to DRC payload data container (out) |
525 | | \payloadPosition Bitstream position of MPEG DRC data chunk (in) |
526 | | |
527 | | \return Flag telling whether new DRC data has been found or not. |
528 | | */ |
529 | | static int aacDecoder_drcParse(HANDLE_FDK_BITSTREAM bs, CDrcPayload *pDrcBs, |
530 | 40.2k | UINT payloadPosition) { |
531 | 40.2k | int i, numBands; |
532 | | |
533 | | /* Move to the beginning of the DRC payload field */ |
534 | 40.2k | FDKpushBiDirectional(bs, (INT)FDKgetValidBits(bs) - (INT)payloadPosition); |
535 | | |
536 | | /* pce_tag_present */ |
537 | 40.2k | if (FDKreadBits(bs, 1)) { |
538 | 6.78k | pDrcBs->pceInstanceTag = FDKreadBits(bs, 4); /* pce_instance_tag */ |
539 | | /* only one program supported */ |
540 | 6.78k | FDKreadBits(bs, 4); /* drc_tag_reserved_bits */ |
541 | 33.4k | } else { |
542 | 33.4k | pDrcBs->pceInstanceTag = -1; /* not present */ |
543 | 33.4k | } |
544 | | |
545 | 40.2k | if (FDKreadBits(bs, 1)) { /* excluded_chns_present */ |
546 | | /* get excluded_chn_mask */ |
547 | 36.1k | parseExcludedChannels(&pDrcBs->excludedChnsMask, bs); |
548 | 36.1k | } else { |
549 | 4.09k | pDrcBs->excludedChnsMask = 0; |
550 | 4.09k | } |
551 | | |
552 | 40.2k | numBands = 1; |
553 | 40.2k | if (FDKreadBits(bs, 1)) /* drc_bands_present */ |
554 | 33.9k | { |
555 | | /* get band_incr */ |
556 | 33.9k | numBands += FDKreadBits(bs, 4); /* drc_band_incr */ |
557 | 33.9k | pDrcBs->channelData.drcInterpolationScheme = |
558 | 33.9k | FDKreadBits(bs, 4); /* drc_interpolation_scheme */ |
559 | | /* band_top */ |
560 | 271k | for (i = 0; i < numBands; i++) { |
561 | 237k | pDrcBs->channelData.bandTop[i] = FDKreadBits(bs, 8); /* drc_band_top[i] */ |
562 | 237k | } |
563 | 33.9k | } else { |
564 | 6.36k | pDrcBs->channelData.bandTop[0] = DRC_BLOCK_LEN_DIV_BAND_MULT - |
565 | 6.36k | 1; /* ... comprising the whole spectrum. */ |
566 | 6.36k | ; |
567 | 6.36k | } |
568 | | |
569 | 40.2k | pDrcBs->channelData.numBands = numBands; |
570 | | |
571 | 40.2k | if (FDKreadBits(bs, 1)) /* prog_ref_level_present */ |
572 | 34.3k | { |
573 | 34.3k | pDrcBs->progRefLevel = FDKreadBits(bs, 7); /* prog_ref_level */ |
574 | 34.3k | FDKreadBits(bs, 1); /* prog_ref_level_reserved_bits */ |
575 | 34.3k | } else { |
576 | 5.97k | pDrcBs->progRefLevel = -1; |
577 | 5.97k | } |
578 | | |
579 | 284k | for (i = 0; i < numBands; i++) { |
580 | 244k | pDrcBs->channelData.drcValue[i] = FDKreadBits(bs, 1) |
581 | 244k | << 7; /* dyn_rng_sgn[i] */ |
582 | 244k | pDrcBs->channelData.drcValue[i] |= |
583 | 244k | FDKreadBits(bs, 7) & 0x7F; /* dyn_rng_ctl[i] */ |
584 | 244k | } |
585 | | |
586 | | /* Set DRC payload type */ |
587 | 40.2k | pDrcBs->channelData.drcDataType = MPEG_DRC_EXT_DATA; |
588 | | |
589 | 40.2k | return (1); |
590 | 40.2k | } |
591 | | |
592 | | /*! |
593 | | \brief Parse heavy compression value transported in DSEs of DVB streams with |
594 | | MPEG-4 content. |
595 | | |
596 | | \bs Handle of FDK bitstream (in) |
597 | | \pDrcBs Pointer to DRC payload data container (out) |
598 | | \payloadPosition Bitstream position of DVB ancillary data chunk |
599 | | |
600 | | \return Flag telling whether new DRC data has been found or not. |
601 | | */ |
602 | 2.68k | #define DVB_COMPRESSION_SCALE (8) /* 48,164 dB */ |
603 | | |
604 | | static int aacDecoder_drcReadCompression(HANDLE_FDK_BITSTREAM bs, |
605 | | CDrcPayload *pDrcBs, |
606 | 2.43k | UINT payloadPosition) { |
607 | 2.43k | int foundDrcData = 0; |
608 | 2.43k | int dmxLevelsPresent, compressionPresent; |
609 | | |
610 | | /* Move to the beginning of the DRC payload field */ |
611 | 2.43k | FDKpushBiDirectional(bs, (INT)FDKgetValidBits(bs) - (INT)payloadPosition); |
612 | | |
613 | | /* Sanity checks */ |
614 | 2.43k | if (FDKgetValidBits(bs) < 24) { |
615 | 0 | return 0; |
616 | 0 | } |
617 | | |
618 | | /* Check sync word */ |
619 | 2.43k | if (FDKreadBits(bs, 8) != DVB_ANC_DATA_SYNC_BYTE) { |
620 | 0 | return 0; |
621 | 0 | } |
622 | | |
623 | | /* Evaluate bs_info field */ |
624 | 2.43k | if (FDKreadBits(bs, 2) != 3) { /* mpeg_audio_type */ |
625 | | /* No MPEG-4 audio data */ |
626 | 727 | return 0; |
627 | 727 | } |
628 | 1.71k | FDKreadBits(bs, 2); /* dolby_surround_mode */ |
629 | 1.71k | pDrcBs->presMode = FDKreadBits(bs, 2); /* presentation_mode */ |
630 | 1.71k | FDKreadBits(bs, 1); /* stereo_downmix_mode */ |
631 | 1.71k | if (FDKreadBits(bs, 1) != 0) { /* reserved, set to 0 */ |
632 | 106 | return 0; |
633 | 106 | } |
634 | | |
635 | | /* Evaluate ancillary_data_status */ |
636 | 1.60k | if (FDKreadBits(bs, 3) != 0) { /* reserved, set to 0 */ |
637 | 15 | return 0; |
638 | 15 | } |
639 | 1.59k | dmxLevelsPresent = FDKreadBits(bs, 1); /* downmixing_levels_MPEG4_status */ |
640 | 1.59k | /*extensionPresent =*/FDKreadBits(bs, |
641 | 1.59k | 1); /* ancillary_data_extension_status; */ |
642 | 1.59k | compressionPresent = |
643 | 1.59k | FDKreadBits(bs, 1); /* audio_coding_mode_and_compression status */ |
644 | 1.59k | /*coarseGrainTcPresent =*/FDKreadBits(bs, |
645 | 1.59k | 1); /* coarse_grain_timecode_status */ |
646 | 1.59k | /*fineGrainTcPresent =*/FDKreadBits(bs, 1); /* fine_grain_timecode_status */ |
647 | | |
648 | 1.59k | if (dmxLevelsPresent) { |
649 | 290 | FDKreadBits(bs, 8); /* downmixing_levels_MPEG4 */ |
650 | 290 | } |
651 | | |
652 | | /* audio_coding_mode_and_compression_status */ |
653 | 1.59k | if (compressionPresent) { |
654 | 1.46k | UCHAR compressionOn, compressionValue; |
655 | | |
656 | | /* audio_coding_mode */ |
657 | 1.46k | if (FDKreadBits(bs, 7) != 0) { /* The reserved bits shall be set to "0". */ |
658 | 225 | return 0; |
659 | 225 | } |
660 | 1.23k | compressionOn = (UCHAR)FDKreadBits(bs, 1); /* compression_on */ |
661 | 1.23k | compressionValue = (UCHAR)FDKreadBits(bs, 8); /* Compression_value */ |
662 | | |
663 | 1.23k | if (compressionOn) { |
664 | | /* A compression value is available so store the data just like MPEG DRC |
665 | | * data */ |
666 | 1.21k | pDrcBs->channelData.numBands = 1; /* One band ... */ |
667 | 1.21k | pDrcBs->channelData.drcValue[0] = |
668 | 1.21k | compressionValue; /* ... with one value ... */ |
669 | 1.21k | pDrcBs->channelData.bandTop[0] = |
670 | 1.21k | DRC_BLOCK_LEN_DIV_BAND_MULT - |
671 | 1.21k | 1; /* ... comprising the whole spectrum. */ |
672 | 1.21k | ; |
673 | 1.21k | pDrcBs->pceInstanceTag = -1; /* Not present */ |
674 | 1.21k | pDrcBs->progRefLevel = -1; /* Not present */ |
675 | 1.21k | pDrcBs->channelData.drcDataType = |
676 | 1.21k | DVB_DRC_ANC_DATA; /* Set DRC payload type to DVB. */ |
677 | 1.21k | foundDrcData = 1; |
678 | 1.21k | } |
679 | 1.23k | } |
680 | | |
681 | 1.36k | return (foundDrcData); |
682 | 1.59k | } |
683 | | |
684 | | /* |
685 | | * Extract DRC payload from bitstream and map it to channels. |
686 | | * Valid return values are: |
687 | | * -1 : An unexpected error occured. |
688 | | * 0 : No error and no valid DRC data available. |
689 | | * 1 : No error and valid DRC data has been mapped. |
690 | | */ |
691 | | static int aacDecoder_drcExtractAndMap( |
692 | | HANDLE_AAC_DRC self, HANDLE_FDK_BITSTREAM hBs, |
693 | | CAacDecoderStaticChannelInfo *pAacDecoderStaticChannelInfo[], |
694 | | UCHAR pceInstanceTag, |
695 | | UCHAR channelMapping[], /* Channel mapping translating drcChannel index to |
696 | | canonical channel index */ |
697 | 486k | int validChannels) { |
698 | 486k | CDrcPayload threadBs[MAX_DRC_THREADS]; |
699 | 486k | CDrcPayload *validThreadBs[MAX_DRC_THREADS]; |
700 | 486k | CDrcParams *pParams; |
701 | 486k | UINT backupBsPosition; |
702 | 486k | int result = 0; |
703 | 486k | int i, thread, validThreads = 0; |
704 | | |
705 | 486k | FDK_ASSERT(self != NULL); |
706 | 486k | FDK_ASSERT(hBs != NULL); |
707 | 486k | FDK_ASSERT(pAacDecoderStaticChannelInfo != NULL); |
708 | | |
709 | 0 | pParams = &self->params; |
710 | | |
711 | 486k | self->numThreads = 0; |
712 | 486k | backupBsPosition = FDKgetValidBits(hBs); |
713 | | |
714 | 527k | for (i = 0; i < self->numPayloads && self->numThreads < MAX_DRC_THREADS; |
715 | 486k | i++) { |
716 | | /* Init payload data chunk. The memclear is very important because it |
717 | | initializes the most values. Without it the module wouldn't work properly |
718 | | or crash. */ |
719 | 40.2k | FDKmemclear(&threadBs[self->numThreads], sizeof(CDrcPayload)); |
720 | 40.2k | threadBs[self->numThreads].channelData.bandTop[0] = |
721 | 40.2k | DRC_BLOCK_LEN_DIV_BAND_MULT - 1; |
722 | | |
723 | | /* Extract payload */ |
724 | 40.2k | self->numThreads += aacDecoder_drcParse(hBs, &threadBs[self->numThreads], |
725 | 40.2k | self->drcPayloadPosition[i]); |
726 | 40.2k | } |
727 | 486k | self->numPayloads = 0; |
728 | | |
729 | 486k | if (self->dvbAncDataAvailable && |
730 | 486k | self->numThreads < MAX_DRC_THREADS) { /* Append a DVB heavy compression |
731 | | payload thread if available. */ |
732 | | |
733 | | /* Init payload data chunk. The memclear is very important because it |
734 | | initializes the most values. Without it the module wouldn't work properly |
735 | | or crash. */ |
736 | 2.43k | FDKmemclear(&threadBs[self->numThreads], sizeof(CDrcPayload)); |
737 | 2.43k | threadBs[self->numThreads].channelData.bandTop[0] = |
738 | 2.43k | DRC_BLOCK_LEN_DIV_BAND_MULT - 1; |
739 | | |
740 | | /* Extract payload */ |
741 | 2.43k | self->numThreads += aacDecoder_drcReadCompression( |
742 | 2.43k | hBs, &threadBs[self->numThreads], self->dvbAncDataPosition); |
743 | 2.43k | } |
744 | 486k | self->dvbAncDataAvailable = 0; |
745 | | |
746 | | /* Reset the bitbufffer */ |
747 | 486k | FDKpushBiDirectional(hBs, (INT)FDKgetValidBits(hBs) - (INT)backupBsPosition); |
748 | | |
749 | | /* calculate number of valid bits in excl_chn_mask */ |
750 | | |
751 | | /* coupling channels not supported */ |
752 | | |
753 | | /* check for valid threads */ |
754 | 528k | for (thread = 0; thread < self->numThreads; thread++) { |
755 | 41.4k | CDrcPayload *pThreadBs = &threadBs[thread]; |
756 | 41.4k | int numExclChns = 0; |
757 | | |
758 | 41.4k | switch ((AACDEC_DRC_PAYLOAD_TYPE)pThreadBs->channelData.drcDataType) { |
759 | 0 | default: |
760 | 0 | continue; |
761 | 40.2k | case MPEG_DRC_EXT_DATA: |
762 | 41.4k | case DVB_DRC_ANC_DATA: |
763 | 41.4k | break; |
764 | 41.4k | } |
765 | | |
766 | 41.4k | if (pThreadBs->pceInstanceTag >= 0) { /* if PCE tag present */ |
767 | 6.78k | if (pThreadBs->pceInstanceTag != pceInstanceTag) { |
768 | 5.63k | continue; /* don't accept */ |
769 | 5.63k | } |
770 | 6.78k | } |
771 | | |
772 | | /* calculate number of excluded channels */ |
773 | 35.8k | if (pThreadBs->excludedChnsMask > 0) { |
774 | 31.2k | INT exclMask = pThreadBs->excludedChnsMask; |
775 | 31.2k | int ch; |
776 | 68.5k | for (ch = 0; ch < validChannels; ch++) { |
777 | 37.3k | numExclChns += exclMask & 0x1; |
778 | 37.3k | exclMask >>= 1; |
779 | 37.3k | } |
780 | 31.2k | } |
781 | 35.8k | if (numExclChns < validChannels) { |
782 | 10.9k | validThreadBs[validThreads] = pThreadBs; |
783 | 10.9k | validThreads++; |
784 | 10.9k | } |
785 | 35.8k | } |
786 | | |
787 | | /* map DRC bitstream information onto DRC channel information */ |
788 | 497k | for (thread = 0; thread < validThreads; thread++) { |
789 | 10.9k | CDrcPayload *pThreadBs = validThreadBs[thread]; |
790 | 10.9k | INT exclMask = pThreadBs->excludedChnsMask; |
791 | 10.9k | AACDEC_DRC_PAYLOAD_TYPE drcPayloadType = |
792 | 10.9k | (AACDEC_DRC_PAYLOAD_TYPE)pThreadBs->channelData.drcDataType; |
793 | 10.9k | int ch; |
794 | | |
795 | | /* last progRefLevel transmitted is the one that is used |
796 | | * (but it should really only be transmitted once per block!) |
797 | | */ |
798 | 10.9k | if (pThreadBs->progRefLevel >= 0) { |
799 | 5.34k | self->progRefLevel = pThreadBs->progRefLevel; |
800 | 5.34k | self->progRefLevelPresent = 1; |
801 | 5.34k | self->prlExpiryCount = 0; /* Got a new value -> Reset counter */ |
802 | 5.34k | } |
803 | | |
804 | 10.9k | if (drcPayloadType == DVB_DRC_ANC_DATA) { |
805 | | /* Announce the presentation mode of this valid thread. */ |
806 | 1.21k | self->presMode = pThreadBs->presMode; |
807 | 1.21k | } |
808 | | |
809 | | /* SCE, CPE and LFE */ |
810 | 32.9k | for (ch = 0; ch < validChannels; ch++) { |
811 | 22.0k | AACDEC_DRC_PAYLOAD_TYPE prvPayloadType = UNKNOWN_PAYLOAD; |
812 | 22.0k | int mapedChannel = channelMapping[ch]; |
813 | | |
814 | 22.0k | if ((mapedChannel >= validChannels) || |
815 | 22.0k | ((exclMask & (1 << mapedChannel)) != 0)) |
816 | 3.02k | continue; |
817 | | |
818 | 19.0k | if ((pParams->expiryFrame <= 0) || |
819 | 19.0k | (pAacDecoderStaticChannelInfo[ch]->drcData.expiryCount < |
820 | 19.0k | pParams->expiryFrame)) { |
821 | 19.0k | prvPayloadType = |
822 | 19.0k | (AACDEC_DRC_PAYLOAD_TYPE)pAacDecoderStaticChannelInfo[ch] |
823 | 19.0k | ->drcData.drcDataType; |
824 | 19.0k | } |
825 | 19.0k | if (((drcPayloadType == MPEG_DRC_EXT_DATA) && |
826 | 19.0k | (prvPayloadType != DVB_DRC_ANC_DATA)) || |
827 | 19.0k | ((drcPayloadType == DVB_DRC_ANC_DATA) && |
828 | 4.10k | (pParams->applyHeavyCompression == |
829 | 16.8k | ON))) { /* copy thread to channel */ |
830 | 16.8k | pAacDecoderStaticChannelInfo[ch]->drcData = pThreadBs->channelData; |
831 | 16.8k | result = 1; |
832 | 16.8k | } |
833 | 19.0k | } |
834 | | /* CCEs not supported by now */ |
835 | 10.9k | } |
836 | | |
837 | | /* Increment and check expiry counter for the program reference level: */ |
838 | 486k | if ((pParams->expiryFrame > 0) && |
839 | 486k | (self->prlExpiryCount++ > |
840 | 0 | pParams->expiryFrame)) { /* The program reference level is too old, so |
841 | | set it back to the target level. */ |
842 | 0 | self->progRefLevelPresent = 0; |
843 | 0 | self->progRefLevel = pParams->targetRefLevel; |
844 | 0 | self->prlExpiryCount = 0; |
845 | 0 | } |
846 | | |
847 | 486k | return result; |
848 | 486k | } |
849 | | |
850 | | void aacDecoder_drcApply(HANDLE_AAC_DRC self, void *pSbrDec, |
851 | | CAacDecoderChannelInfo *pAacDecoderChannelInfo, |
852 | | CDrcChannelData *pDrcChData, FIXP_DBL *extGain, |
853 | | int ch, /* needed only for SBR */ |
854 | 865k | int aacFrameSize, int bSbrPresent) { |
855 | 865k | int band, bin, numBands; |
856 | 865k | int bottom = 0; |
857 | 865k | int modifyBins = 0; |
858 | | |
859 | 865k | FIXP_DBL max_mantissa; |
860 | 865k | INT max_exponent; |
861 | | |
862 | 865k | FIXP_DBL norm_mantissa = FL2FXCONST_DBL(0.5f); |
863 | 865k | INT norm_exponent = 1; |
864 | | |
865 | 865k | FIXP_DBL fact_mantissa[MAX_DRC_BANDS]; |
866 | 865k | INT fact_exponent[MAX_DRC_BANDS]; |
867 | | |
868 | 865k | CDrcParams *pParams = &self->params; |
869 | | |
870 | 865k | FIXP_DBL *pSpectralCoefficient = |
871 | 865k | SPEC_LONG(pAacDecoderChannelInfo->pSpectralCoefficient); |
872 | 865k | CIcsInfo *pIcsInfo = &pAacDecoderChannelInfo->icsInfo; |
873 | 865k | SHORT *pSpecScale = pAacDecoderChannelInfo->specScale; |
874 | | |
875 | 865k | int winSeq = pIcsInfo->WindowSequence; |
876 | | |
877 | | /* Increment and check expiry counter */ |
878 | 865k | if ((pParams->expiryFrame > 0) && |
879 | 865k | (++pDrcChData->expiryCount > |
880 | 0 | pParams->expiryFrame)) { /* The DRC data is too old, so delete it. */ |
881 | 0 | aacDecoder_drcInitChannelData(pDrcChData); |
882 | 0 | } |
883 | | |
884 | 865k | if (self->enable != ON) { |
885 | 439k | sbrDecoder_drcDisable((HANDLE_SBRDECODER)pSbrDec, ch); |
886 | 439k | if (extGain != NULL) { |
887 | 439k | INT gainScale = (INT)*extGain; |
888 | | /* The gain scaling must be passed to the function in the buffer pointed |
889 | | * on by extGain. */ |
890 | 439k | if (gainScale >= 0 && gainScale <= DFRACT_BITS) { |
891 | 439k | *extGain = scaleValue(norm_mantissa, norm_exponent - gainScale); |
892 | 439k | } else { |
893 | 0 | FDK_ASSERT(0); |
894 | 0 | } |
895 | 439k | } |
896 | 0 | return; |
897 | 439k | } |
898 | | |
899 | 425k | numBands = pDrcChData->numBands; |
900 | | |
901 | | /* If program reference normalization is done in the digital domain, |
902 | | modify factor to perform normalization. prog_ref_level can |
903 | | alternatively be passed to the system for modification of the level in |
904 | | the analog domain. Analog level modification avoids problems with |
905 | | reduced DAC SNR (if signal is attenuated) or clipping (if signal is |
906 | | boosted) */ |
907 | | |
908 | 425k | if (pParams->targetRefLevel >= 0) { |
909 | | /* 0.5^((targetRefLevel - progRefLevel)/24) */ |
910 | 425k | norm_mantissa = |
911 | 425k | fLdPow(FL2FXCONST_DBL(-1.0), /* log2(0.5) */ |
912 | 425k | 0, |
913 | 425k | (FIXP_DBL)((INT)(FL2FXCONST_DBL(1.0f / 24.0) >> 3) * |
914 | 425k | (INT)(pParams->targetRefLevel - self->progRefLevel)), |
915 | 425k | 3, &norm_exponent); |
916 | 425k | } |
917 | | /* Always export the normalization gain (if possible). */ |
918 | 425k | if (extGain != NULL) { |
919 | 425k | INT gainScale = (INT)*extGain; |
920 | | /* The gain scaling must be passed to the function in the buffer pointed on |
921 | | * by extGain. */ |
922 | 425k | if (gainScale >= 0 && gainScale <= DFRACT_BITS) { |
923 | 425k | *extGain = scaleValue(norm_mantissa, norm_exponent - gainScale); |
924 | 425k | } else { |
925 | 0 | FDK_ASSERT(0); |
926 | 0 | } |
927 | 425k | } |
928 | | /* Reset normalization gain since this module must not apply it */ |
929 | 425k | norm_mantissa = FL2FXCONST_DBL(0.5f); |
930 | 425k | norm_exponent = 1; |
931 | | |
932 | | /* calc scale factors */ |
933 | 967k | for (band = 0; band < numBands; band++) { |
934 | 542k | UCHAR drcVal = pDrcChData->drcValue[band]; |
935 | | |
936 | 542k | fact_mantissa[band] = FL2FXCONST_DBL(0.5f); |
937 | 542k | fact_exponent[band] = 1; |
938 | | |
939 | 542k | if ((pParams->applyHeavyCompression == ON) && |
940 | 542k | ((AACDEC_DRC_PAYLOAD_TYPE)pDrcChData->drcDataType == |
941 | 4.37k | DVB_DRC_ANC_DATA)) { |
942 | 2.86k | INT compressionFactorVal_e; |
943 | 2.86k | int valX, valY; |
944 | | |
945 | 2.86k | valX = drcVal >> 4; |
946 | 2.86k | valY = drcVal & 0x0F; |
947 | | |
948 | | /* calculate the unscaled heavy compression factor. |
949 | | compressionFactor = 48.164 - 6.0206*valX - 0.4014*valY dB |
950 | | range: -48.166 dB to 48.164 dB */ |
951 | 2.86k | if (drcVal != 0x7F) { |
952 | 2.68k | fact_mantissa[band] = fPowInt( |
953 | 2.68k | FL2FXCONST_DBL(0.95483867181), /* -0.4014dB = 0.95483867181 */ |
954 | 2.68k | 0, valY, &compressionFactorVal_e); |
955 | | |
956 | | /* -0.0008dB (48.164 - 6.0206*8 = -0.0008) */ |
957 | 2.68k | fact_mantissa[band] = |
958 | 2.68k | fMult(FL2FXCONST_DBL(0.99990790084), fact_mantissa[band]); |
959 | | |
960 | 2.68k | fact_exponent[band] = |
961 | 2.68k | DVB_COMPRESSION_SCALE - valX + compressionFactorVal_e; |
962 | 2.68k | } |
963 | 539k | } else if ((AACDEC_DRC_PAYLOAD_TYPE)pDrcChData->drcDataType == |
964 | 539k | MPEG_DRC_EXT_DATA) { |
965 | | /* apply the scaled dynamic range control words to factor. |
966 | | * if scaling drc_cut (or drc_boost), or control word drc_mantissa is 0 |
967 | | * then there is no dynamic range compression |
968 | | * |
969 | | * if pDrcChData->drcSgn[band] is |
970 | | * 1 then gain is < 1 : factor = 2^(-self->cut * |
971 | | * pDrcChData->drcMag[band] / 24) 0 then gain is > 1 : factor = 2^( |
972 | | * self->boost * pDrcChData->drcMag[band] / 24) |
973 | | */ |
974 | | |
975 | 180k | if ((drcVal & 0x7F) > 0) { |
976 | 161k | FIXP_DBL tParamVal = (drcVal & 0x80) ? -pParams->cut : pParams->boost; |
977 | | |
978 | 161k | fact_mantissa[band] = f2Pow( |
979 | 161k | (FIXP_DBL)((INT)fMult(FL2FXCONST_DBL(1.0f / 192.0f), tParamVal) * |
980 | 161k | (drcVal & 0x7F)), |
981 | 161k | 3 + DRC_PARAM_SCALE, &fact_exponent[band]); |
982 | 161k | } |
983 | 180k | } |
984 | | |
985 | 542k | fact_mantissa[band] = fMult(fact_mantissa[band], norm_mantissa); |
986 | 542k | fact_exponent[band] += norm_exponent; |
987 | | |
988 | 542k | } /* end loop over bands */ |
989 | | |
990 | | /* normalizations */ |
991 | 425k | { |
992 | 425k | int res; |
993 | | |
994 | 425k | max_mantissa = FL2FXCONST_DBL(0.0f); |
995 | 425k | max_exponent = 0; |
996 | 967k | for (band = 0; band < numBands; band++) { |
997 | 542k | max_mantissa = fixMax(max_mantissa, fact_mantissa[band]); |
998 | 542k | max_exponent = fixMax(max_exponent, fact_exponent[band]); |
999 | 542k | } |
1000 | | |
1001 | | /* left shift factors to gain accurancy */ |
1002 | 425k | res = CntLeadingZeros(max_mantissa) - 1; |
1003 | | |
1004 | | /* above topmost DRC band gain factor is 1 */ |
1005 | 425k | if (((pDrcChData->bandTop[fMax(0, numBands - 1)] + 1) << 2) < aacFrameSize) |
1006 | 26.3k | res = 0; |
1007 | | |
1008 | 425k | if (res > 0) { |
1009 | 399k | res = fixMin(res, max_exponent); |
1010 | 399k | max_exponent -= res; |
1011 | | |
1012 | 799k | for (band = 0; band < numBands; band++) { |
1013 | 400k | fact_mantissa[band] <<= res; |
1014 | 400k | fact_exponent[band] -= res; |
1015 | 400k | } |
1016 | 399k | } |
1017 | | |
1018 | | /* normalize magnitudes to one scale factor */ |
1019 | 967k | for (band = 0; band < numBands; band++) { |
1020 | 542k | if (fact_exponent[band] < max_exponent) { |
1021 | 541 | fact_mantissa[band] >>= max_exponent - fact_exponent[band]; |
1022 | 541 | } |
1023 | 542k | if (fact_mantissa[band] != FL2FXCONST_DBL(0.5f)) { |
1024 | 144k | modifyBins = 1; |
1025 | 144k | } |
1026 | 542k | } |
1027 | 425k | if (max_exponent != 1) { |
1028 | 29.1k | modifyBins = 1; |
1029 | 29.1k | } |
1030 | 425k | } |
1031 | | |
1032 | | /* apply factor to spectral lines |
1033 | | * short blocks must take care that bands fall on |
1034 | | * block boundaries! |
1035 | | */ |
1036 | 425k | if (!bSbrPresent) { |
1037 | 148k | bottom = 0; |
1038 | | |
1039 | 148k | if (!modifyBins) { |
1040 | | /* We don't have to modify the spectral bins because the fractional part |
1041 | | of all factors is 0.5. In order to keep accurancy we don't apply the |
1042 | | factor but decrease the exponent instead. */ |
1043 | 145k | max_exponent -= 1; |
1044 | 145k | } else { |
1045 | 16.7k | for (band = 0; band < numBands; band++) { |
1046 | 13.3k | int top = fixMin((int)((pDrcChData->bandTop[band] + 1) << 2), |
1047 | 13.3k | aacFrameSize); /* ... * DRC_BAND_MULT; */ |
1048 | | |
1049 | 2.55M | for (bin = bottom; bin < top; bin++) { |
1050 | 2.53M | pSpectralCoefficient[bin] = |
1051 | 2.53M | fMult(pSpectralCoefficient[bin], fact_mantissa[band]); |
1052 | 2.53M | } |
1053 | | |
1054 | 13.3k | bottom = top; |
1055 | 13.3k | } |
1056 | 3.36k | } |
1057 | | |
1058 | | /* above topmost DRC band gain factor is 1 */ |
1059 | 148k | if (max_exponent > 0) { |
1060 | 1.46M | for (bin = bottom; bin < aacFrameSize; bin += 1) { |
1061 | 1.46M | pSpectralCoefficient[bin] >>= max_exponent; |
1062 | 1.46M | } |
1063 | 3.31k | } |
1064 | | |
1065 | | /* adjust scaling */ |
1066 | 148k | pSpecScale[0] += max_exponent; |
1067 | | |
1068 | 148k | if (winSeq == BLOCK_SHORT) { |
1069 | 15.9k | int win; |
1070 | 127k | for (win = 1; win < 8; win++) { |
1071 | 111k | pSpecScale[win] += max_exponent; |
1072 | 111k | } |
1073 | 15.9k | } |
1074 | 276k | } else { |
1075 | 276k | HANDLE_SBRDECODER hSbrDecoder = (HANDLE_SBRDECODER)pSbrDec; |
1076 | 276k | numBands = pDrcChData->numBands; |
1077 | | |
1078 | | /* feed factors into SBR decoder for application in QMF domain. */ |
1079 | 276k | sbrDecoder_drcFeedChannel(hSbrDecoder, ch, numBands, fact_mantissa, |
1080 | 276k | max_exponent, pDrcChData->drcInterpolationScheme, |
1081 | 276k | winSeq, pDrcChData->bandTop); |
1082 | 276k | } |
1083 | | |
1084 | 425k | return; |
1085 | 865k | } |
1086 | | |
1087 | | /* |
1088 | | * DRC parameter and presentation mode handling |
1089 | | */ |
1090 | | static void aacDecoder_drcParameterHandling(HANDLE_AAC_DRC self, |
1091 | | INT aacNumChannels, |
1092 | | SCHAR prevDrcProgRefLevel, |
1093 | 486k | SCHAR prevDrcPresMode) { |
1094 | 486k | int isDownmix, isMonoDownmix, isStereoDownmix; |
1095 | 486k | int dDmx, dHr; |
1096 | 486k | AACDEC_DRC_PARAMETER_HANDLING drcParameterHandling; |
1097 | 486k | CDrcParams *p; |
1098 | | |
1099 | 486k | FDK_ASSERT(self != NULL); |
1100 | | |
1101 | 0 | p = &self->params; |
1102 | | |
1103 | 486k | if (self->progRefLevel != prevDrcProgRefLevel) self->update = 1; |
1104 | | |
1105 | 486k | if (self->presMode != prevDrcPresMode) self->update = 1; |
1106 | | |
1107 | 486k | if (self->prevAacNumChannels != aacNumChannels) self->update = 1; |
1108 | | |
1109 | | /* return if no relevant parameter has changed */ |
1110 | 486k | if (!self->update) { |
1111 | 463k | return; |
1112 | 463k | } |
1113 | | |
1114 | | /* derive downmix property. aacNumChannels: number of channels in aac stream, |
1115 | | * numOutChannels: number of output channels */ |
1116 | 23.3k | isDownmix = (aacNumChannels > self->numOutChannels); |
1117 | 23.3k | isDownmix = (isDownmix && (self->numOutChannels > 0)); |
1118 | 23.3k | isMonoDownmix = (isDownmix && (self->numOutChannels == 1)); |
1119 | 23.3k | isStereoDownmix = (isDownmix && (self->numOutChannels == 2)); |
1120 | | |
1121 | 23.3k | if ((self->presMode == 1) || (self->presMode == 2)) { |
1122 | 804 | drcParameterHandling = (AACDEC_DRC_PARAMETER_HANDLING)self->presMode; |
1123 | 22.5k | } else { /* no presentation mode -> use parameter handling specified by |
1124 | | AAC_DRC_DEFAULT_PRESENTATION_MODE */ |
1125 | 22.5k | drcParameterHandling = p->defaultPresentationMode; |
1126 | 22.5k | } |
1127 | | |
1128 | | /* by default, do as desired */ |
1129 | 23.3k | p->cut = p->usrCut; |
1130 | 23.3k | p->boost = p->usrBoost; |
1131 | 23.3k | p->applyHeavyCompression = p->usrApplyHeavyCompression; |
1132 | | |
1133 | 23.3k | switch (drcParameterHandling) { |
1134 | 22.5k | case DISABLED_PARAMETER_HANDLING: |
1135 | 22.5k | default: |
1136 | | /* use drc parameters as requested */ |
1137 | 22.5k | break; |
1138 | | |
1139 | 22.5k | case ENABLED_PARAMETER_HANDLING: |
1140 | | /* dDmx: estimated headroom reduction due to downmix, format: -1/4*dB |
1141 | | dDmx = floor(-4*20*log10(aacNumChannels/numOutChannels)) */ |
1142 | 0 | if (isDownmix) { |
1143 | 0 | FIXP_DBL dmxTmp; |
1144 | 0 | int e_log, e_mult; |
1145 | 0 | dmxTmp = fDivNorm(self->numOutChannels, |
1146 | 0 | aacNumChannels); /* inverse division -> |
1147 | | negative sign after |
1148 | | logarithm */ |
1149 | 0 | dmxTmp = fLog2(dmxTmp, 0, &e_log); |
1150 | 0 | dmxTmp = fMultNorm( |
1151 | 0 | dmxTmp, FL2FXCONST_DBL(4.0f * 20.0f * 0.30103f / (float)(1 << 5)), |
1152 | 0 | &e_mult); /* e = e_log + e_mult + 5 */ |
1153 | 0 | dDmx = (int)scaleValue(dmxTmp, e_log + e_mult + 5 - (DFRACT_BITS - 1)); |
1154 | 0 | } else { |
1155 | 0 | dDmx = 0; |
1156 | 0 | } |
1157 | | |
1158 | | /* dHr: Full estimated (decoder) headroom reduction due to loudness |
1159 | | * normalisation (DTL - PRL) and downmix. Format: -1/4*dB */ |
1160 | 0 | if (p->targetRefLevel >= 0) { /* if target level is provided */ |
1161 | 0 | dHr = p->targetRefLevel + dDmx - self->progRefLevel; |
1162 | 0 | } else { |
1163 | 0 | dHr = dDmx; |
1164 | 0 | } |
1165 | |
|
1166 | 0 | if (dHr < 0) { /* if headroom is reduced */ |
1167 | | /* Use compression, but as little as possible. */ |
1168 | | /* eHr: Headroom provided by encoder, format: -1/4 dB */ |
1169 | 0 | int eHr = fixMin(p->encoderTargetLevel - self->progRefLevel, 0); |
1170 | 0 | if (eHr < |
1171 | 0 | dHr) { /* if encoder provides more headroom than decoder needs */ |
1172 | | /* derive scaling of light DRC */ |
1173 | 0 | FIXP_DBL calcFactor_norm; |
1174 | 0 | INT calcFactor; /* fraction of DRC gains that is minimally needed for |
1175 | | clipping prevention */ |
1176 | 0 | calcFactor_norm = |
1177 | 0 | fDivNorm(-dHr, -eHr); /* 0.0 < calcFactor_norm < 1.0 */ |
1178 | 0 | calcFactor_norm = calcFactor_norm >> DRC_PARAM_SCALE; |
1179 | | /* quantize to 128 steps */ |
1180 | 0 | calcFactor = convert_drcParam( |
1181 | 0 | calcFactor_norm); /* convert to integer value between 0 and 127 */ |
1182 | 0 | calcFactor_norm = (FIXP_DBL)( |
1183 | 0 | (INT)(DRC_PARAM_QUANT_STEP >> DRC_PARAM_SCALE) * calcFactor); |
1184 | 0 | p->cut = (calcFactor_norm > p->cut) |
1185 | 0 | ? calcFactor_norm |
1186 | 0 | : p->cut; /* use calcFactor_norm as lower limit */ |
1187 | 0 | } else { |
1188 | | /* encoder provides equal or less headroom than decoder needs */ |
1189 | | /* the time domain limiter must always be active in this case. It is |
1190 | | * assumed that the framework activates it by default */ |
1191 | 0 | p->cut = DRC_SCALING_MAX; |
1192 | 0 | if ((dHr - eHr) <= |
1193 | 0 | -4 * DRC_HEAVY_THRESHOLD_DB) { /* use heavy compression if |
1194 | | headroom deficit is equal or |
1195 | | higher than |
1196 | | DRC_HEAVY_THRESHOLD_DB */ |
1197 | 0 | p->applyHeavyCompression = ON; |
1198 | 0 | } |
1199 | 0 | } |
1200 | 0 | } else { /* dHr >= 0 */ |
1201 | | /* no restrictions required, as headroom is not reduced. */ |
1202 | | /* p->cut = p->usrCut; */ |
1203 | 0 | } |
1204 | 0 | break; |
1205 | | |
1206 | | /* presentation mode 1 and 2 according to ETSI TS 101 154: |
1207 | | Digital Video Broadcasting (DVB); Specification for the use of Video |
1208 | | and Audio Coding in Broadcasting Applications based on the MPEG-2 |
1209 | | Transport Stream, section C.5.4., "Decoding", and Table C.33. Also |
1210 | | according to amendment 4 to ISO/IEC 14496-3, section 4.5.2.14.2.4, and |
1211 | | Table AMD4.11. ISO DRC -> applyHeavyCompression = OFF (Use |
1212 | | light compression, MPEG-style) Compression_value -> |
1213 | | applyHeavyCompression = ON (Use heavy compression, DVB-style) scaling |
1214 | | restricted -> p->cut = DRC_SCALING_MAX */ |
1215 | | |
1216 | 796 | case DRC_PRESENTATION_MODE_1: /* presentation mode 1, Light:-31/Heavy:-23 */ |
1217 | 796 | if ((p->targetRefLevel >= 0) && |
1218 | 796 | (p->targetRefLevel < |
1219 | 796 | 124)) { /* if target level is provided and > -31 dB */ |
1220 | | /* playback up to -23 dB */ |
1221 | 796 | p->applyHeavyCompression = ON; |
1222 | 796 | } else { /* target level <= -31 dB or not provided */ |
1223 | | /* playback -31 dB */ |
1224 | 0 | if (isMonoDownmix || isStereoDownmix) { /* stereo or mono downmixing */ |
1225 | 0 | p->cut = DRC_SCALING_MAX; |
1226 | 0 | } |
1227 | 0 | } |
1228 | 796 | break; |
1229 | | |
1230 | 8 | case DRC_PRESENTATION_MODE_2: /* presentation mode 2, Light:-23/Heavy:-23 */ |
1231 | 8 | if ((p->targetRefLevel >= 0) && |
1232 | 8 | (p->targetRefLevel < |
1233 | 8 | 124)) { /* if target level is provided and > -31 dB */ |
1234 | | /* playback up to -23 dB */ |
1235 | 8 | if (isMonoDownmix) { /* if mono downmix */ |
1236 | 0 | p->applyHeavyCompression = ON; |
1237 | 8 | } else { |
1238 | 8 | p->applyHeavyCompression = OFF; |
1239 | 8 | p->cut = DRC_SCALING_MAX; |
1240 | 8 | } |
1241 | 8 | } else { /* target level <= -31 dB or not provided */ |
1242 | | /* playback -31 dB */ |
1243 | 0 | p->applyHeavyCompression = OFF; |
1244 | 0 | if (isMonoDownmix || isStereoDownmix) { /* stereo or mono downmixing */ |
1245 | 0 | p->cut = DRC_SCALING_MAX; |
1246 | 0 | } |
1247 | 0 | } |
1248 | 8 | break; |
1249 | 23.3k | } /* switch (drcParameterHandling) */ |
1250 | | |
1251 | | /* With heavy compression, there is no scaling. |
1252 | | Scaling factors are set for notification only. */ |
1253 | 23.3k | if (p->applyHeavyCompression == ON) { |
1254 | 796 | p->boost = DRC_SCALING_MAX; |
1255 | 796 | p->cut = DRC_SCALING_MAX; |
1256 | 796 | } |
1257 | | |
1258 | | /* switch on/off processing */ |
1259 | 23.3k | self->enable = ((p->boost > (FIXP_DBL)0) || (p->cut > (FIXP_DBL)0) || |
1260 | 23.3k | (p->applyHeavyCompression == ON) || (p->targetRefLevel >= 0)); |
1261 | 23.3k | self->enable = (self->enable && !self->uniDrcPrecedence); |
1262 | | |
1263 | 23.3k | self->prevAacNumChannels = aacNumChannels; |
1264 | 23.3k | self->update = 0; |
1265 | 23.3k | } |
1266 | | |
1267 | | /* |
1268 | | * Prepare DRC processing |
1269 | | * Valid return values are: |
1270 | | * -1 : An unexpected error occured. |
1271 | | * 0 : No error and no valid DRC data available. |
1272 | | * 1 : No error and valid DRC data has been mapped. |
1273 | | */ |
1274 | | int aacDecoder_drcProlog( |
1275 | | HANDLE_AAC_DRC self, HANDLE_FDK_BITSTREAM hBs, |
1276 | | CAacDecoderStaticChannelInfo *pAacDecoderStaticChannelInfo[], |
1277 | | UCHAR pceInstanceTag, |
1278 | | UCHAR channelMapping[], /* Channel mapping translating drcChannel index to |
1279 | | canonical channel index */ |
1280 | 487k | int validChannels) { |
1281 | 487k | int result = 0; |
1282 | | |
1283 | 487k | if (self == NULL) { |
1284 | 0 | return -1; |
1285 | 0 | } |
1286 | | |
1287 | 487k | if (!self->params.bsDelayEnable) { |
1288 | | /* keep previous progRefLevel and presMode for update flag in |
1289 | | * drcParameterHandling */ |
1290 | 427k | INT prevPRL, prevPM = 0; |
1291 | 427k | prevPRL = self->progRefLevel; |
1292 | 427k | prevPM = self->presMode; |
1293 | | |
1294 | 427k | result = aacDecoder_drcExtractAndMap( |
1295 | 427k | self, hBs, pAacDecoderStaticChannelInfo, pceInstanceTag, channelMapping, |
1296 | 427k | validChannels); |
1297 | | |
1298 | 427k | if (result < 0) { |
1299 | 0 | return result; |
1300 | 0 | } |
1301 | | |
1302 | | /* Drc parameter handling */ |
1303 | 427k | aacDecoder_drcParameterHandling(self, validChannels, prevPRL, prevPM); |
1304 | 427k | } |
1305 | | |
1306 | 487k | return result; |
1307 | 487k | } |
1308 | | |
1309 | | /* |
1310 | | * Finalize DRC processing |
1311 | | * Valid return values are: |
1312 | | * -1 : An unexpected error occured. |
1313 | | * 0 : No error and no valid DRC data available. |
1314 | | * 1 : No error and valid DRC data has been mapped. |
1315 | | */ |
1316 | | int aacDecoder_drcEpilog( |
1317 | | HANDLE_AAC_DRC self, HANDLE_FDK_BITSTREAM hBs, |
1318 | | CAacDecoderStaticChannelInfo *pAacDecoderStaticChannelInfo[], |
1319 | | UCHAR pceInstanceTag, |
1320 | | UCHAR channelMapping[], /* Channel mapping translating drcChannel index to |
1321 | | canonical channel index */ |
1322 | 486k | int validChannels) { |
1323 | 486k | int result = 0; |
1324 | | |
1325 | 486k | if (self == NULL) { |
1326 | 0 | return -1; |
1327 | 0 | } |
1328 | | |
1329 | 486k | if (self->params.bsDelayEnable) { |
1330 | | /* keep previous progRefLevel and presMode for update flag in |
1331 | | * drcParameterHandling */ |
1332 | 58.9k | INT prevPRL, prevPM = 0; |
1333 | 58.9k | prevPRL = self->progRefLevel; |
1334 | 58.9k | prevPM = self->presMode; |
1335 | | |
1336 | 58.9k | result = aacDecoder_drcExtractAndMap( |
1337 | 58.9k | self, hBs, pAacDecoderStaticChannelInfo, pceInstanceTag, channelMapping, |
1338 | 58.9k | validChannels); |
1339 | | |
1340 | 58.9k | if (result < 0) { |
1341 | 0 | return result; |
1342 | 0 | } |
1343 | | |
1344 | | /* Drc parameter handling */ |
1345 | 58.9k | aacDecoder_drcParameterHandling(self, validChannels, prevPRL, prevPM); |
1346 | 58.9k | } |
1347 | | |
1348 | 486k | return result; |
1349 | 486k | } |
1350 | | |
1351 | | /* |
1352 | | * Export relevant metadata info from bitstream payload. |
1353 | | */ |
1354 | | void aacDecoder_drcGetInfo(HANDLE_AAC_DRC self, SCHAR *pPresMode, |
1355 | 486k | SCHAR *pProgRefLevel) { |
1356 | 486k | if (self != NULL) { |
1357 | 486k | if (pPresMode != NULL) { |
1358 | 486k | *pPresMode = self->presMode; |
1359 | 486k | } |
1360 | 486k | if (pProgRefLevel != NULL) { |
1361 | 486k | if (self->progRefLevelPresent) { |
1362 | 34.6k | *pProgRefLevel = self->progRefLevel; |
1363 | 451k | } else { |
1364 | 451k | *pProgRefLevel = -1; |
1365 | 451k | } |
1366 | 486k | } |
1367 | 486k | } |
1368 | 486k | } |
1369 | | |
1370 | | /** |
1371 | | * \brief Apply DRC Level Normalization. |
1372 | | * |
1373 | | * This function prepares/applies the gain values for the DRC Level |
1374 | | * Normalization and returns the exponent of the time data. The following two |
1375 | | * cases are handled: |
1376 | | * |
1377 | | * - Limiter enabled: |
1378 | | * The input data must be interleaved. |
1379 | | * One gain per sample is written to the buffer pGainPerSample. |
1380 | | * If necessary the time data is rescaled. |
1381 | | * |
1382 | | * - Limiter disabled: |
1383 | | * The input data can be interleaved or deinterleaved. |
1384 | | * The gain values are applied to the time data. |
1385 | | * If necessary the time data is rescaled. |
1386 | | * |
1387 | | * \param hDrcInfo [i/o] handle to drc data structure. |
1388 | | * \param samplesIn [i/o] pointer to time data. |
1389 | | * \param pGain [i ] pointer to gain to be applied to |
1390 | | * the time data. |
1391 | | * \param pGainPerSample [o ] pointer to the gain per sample to |
1392 | | * be applied to the time data in the limiter. |
1393 | | * \param gain_scale [i ] exponent to be applied to the time |
1394 | | * data. |
1395 | | * \param gain_delay [i ] delay[samples] with which the gains |
1396 | | * in pGain shall be applied (gain_delay <= nSamples). |
1397 | | * \param nSamples [i ] number of samples per frame. |
1398 | | * \param channels [i ] number of channels. |
1399 | | * \param stride [i ] channel stride of time data. |
1400 | | * \param limiterEnabled [i ] 1 if limiter is enabled, otherwise |
1401 | | * 0. |
1402 | | * |
1403 | | * \return exponent of time data |
1404 | | */ |
1405 | | INT applyDrcLevelNormalization(HANDLE_AAC_DRC hDrcInfo, PCM_DEC *samplesIn, |
1406 | | FIXP_DBL *pGain, FIXP_DBL *pGainPerSample, |
1407 | | const INT gain_scale, const UINT gain_delay, |
1408 | | const UINT nSamples, const UINT channels, |
1409 | 34.1k | const UINT stride, const UINT limiterEnabled) { |
1410 | 34.1k | UINT i; |
1411 | 34.1k | INT additionalGain_scaling; |
1412 | 34.1k | FIXP_DBL additionalGain; |
1413 | | |
1414 | 34.1k | FDK_ASSERT(gain_delay <= nSamples); |
1415 | | |
1416 | 0 | FIXP_DBL additionalGainSmoothState = hDrcInfo->additionalGainFilterState; |
1417 | 34.1k | FIXP_DBL additionalGainSmoothState1 = hDrcInfo->additionalGainFilterState1; |
1418 | | |
1419 | 34.1k | if (!gain_delay) { |
1420 | 0 | additionalGain = pGain[0]; |
1421 | | |
1422 | | /* Apply the additional scaling gain_scale[0] that has no delay and no |
1423 | | * smoothing */ |
1424 | 0 | additionalGain_scaling = |
1425 | 0 | fMin(gain_scale, CntLeadingZeros(additionalGain) - 1); |
1426 | 0 | additionalGain = scaleValue(additionalGain, additionalGain_scaling); |
1427 | | |
1428 | | /* if it's not possible to fully apply gain_scale to additionalGain, apply |
1429 | | * it to the input signal */ |
1430 | 0 | additionalGain_scaling -= gain_scale; |
1431 | |
|
1432 | 0 | if (additionalGain_scaling) { |
1433 | 0 | scaleValuesSaturate(samplesIn, channels * nSamples, |
1434 | 0 | -additionalGain_scaling); |
1435 | 0 | } |
1436 | |
|
1437 | 0 | if (limiterEnabled) { |
1438 | 0 | FDK_ASSERT(pGainPerSample != NULL); |
1439 | | |
1440 | 0 | for (i = 0; i < nSamples; i++) { |
1441 | 0 | pGainPerSample[i] = additionalGain; |
1442 | 0 | } |
1443 | 0 | } else { |
1444 | 0 | for (i = 0; i < channels * nSamples; i++) { |
1445 | 0 | samplesIn[i] = FIXP_DBL2PCM_DEC(fMult(samplesIn[i], additionalGain)); |
1446 | 0 | } |
1447 | 0 | } |
1448 | 34.1k | } else { |
1449 | 34.1k | UINT inc; |
1450 | 34.1k | FIXP_DBL additionalGainUnfiltered; |
1451 | | |
1452 | 34.1k | inc = (stride == 1) ? channels : 1; |
1453 | | |
1454 | 61.1M | for (i = 0; i < nSamples; i++) { |
1455 | 61.1M | if (i < gain_delay) { |
1456 | 32.4M | additionalGainUnfiltered = hDrcInfo->additionalGainPrev; |
1457 | 32.4M | } else { |
1458 | 28.7M | additionalGainUnfiltered = pGain[0]; |
1459 | 28.7M | } |
1460 | | |
1461 | | /* Smooth additionalGain */ |
1462 | | |
1463 | | /* [b,a] = butter(1, 0.01) */ |
1464 | 61.1M | static const FIXP_SGL b[] = {FL2FXCONST_SGL(0.015466 * 2.0), |
1465 | 61.1M | FL2FXCONST_SGL(0.015466 * 2.0)}; |
1466 | 61.1M | static const FIXP_SGL a[] = {(FIXP_SGL)MAXVAL_SGL, |
1467 | 61.1M | FL2FXCONST_SGL(-0.96907)}; |
1468 | | |
1469 | 61.1M | additionalGain = -fMult(additionalGainSmoothState, a[1]) + |
1470 | 61.1M | fMultDiv2(additionalGainUnfiltered, b[0]) + |
1471 | 61.1M | fMultDiv2(additionalGainSmoothState1, b[1]); |
1472 | 61.1M | additionalGainSmoothState1 = additionalGainUnfiltered; |
1473 | 61.1M | additionalGainSmoothState = additionalGain; |
1474 | | |
1475 | | /* Apply the additional scaling gain_scale[0] that has no delay and no |
1476 | | * smoothing */ |
1477 | 61.1M | additionalGain_scaling = |
1478 | 61.1M | fMin(gain_scale, CntLeadingZeros(additionalGain) - 1); |
1479 | 61.1M | additionalGain = scaleValue(additionalGain, additionalGain_scaling); |
1480 | | |
1481 | | /* if it's not possible to fully apply gain_scale[0] to additionalGain, |
1482 | | * apply it to the input signal */ |
1483 | 61.1M | additionalGain_scaling -= gain_scale; |
1484 | | |
1485 | 61.1M | if (limiterEnabled) { |
1486 | 59.6M | FDK_ASSERT(stride == 1); |
1487 | 59.6M | FDK_ASSERT(pGainPerSample != NULL); |
1488 | | |
1489 | 59.6M | if (additionalGain_scaling) { |
1490 | 7.35k | scaleValuesSaturate(samplesIn, channels, -additionalGain_scaling); |
1491 | 7.35k | } |
1492 | | |
1493 | 59.6M | pGainPerSample[i] = additionalGain; |
1494 | 59.6M | } else { |
1495 | 1.52M | if (additionalGain_scaling) { |
1496 | 176k | for (UINT k = 0; k < channels; k++) { |
1497 | 151k | scaleValuesSaturate(&samplesIn[k * stride], 1, |
1498 | 151k | -additionalGain_scaling); |
1499 | 151k | } |
1500 | 25.2k | } |
1501 | | |
1502 | 5.26M | for (UINT k = 0; k < channels; k++) { |
1503 | 3.74M | samplesIn[k * stride] = |
1504 | 3.74M | FIXP_DBL2PCM_DEC(fMult(samplesIn[k * stride], additionalGain)); |
1505 | 3.74M | } |
1506 | 1.52M | } |
1507 | | |
1508 | 0 | samplesIn += inc; |
1509 | 61.1M | } |
1510 | 34.1k | } |
1511 | | |
1512 | 0 | hDrcInfo->additionalGainPrev = pGain[0]; |
1513 | 34.1k | hDrcInfo->additionalGainFilterState = additionalGainSmoothState; |
1514 | 34.1k | hDrcInfo->additionalGainFilterState1 = additionalGainSmoothState1; |
1515 | | |
1516 | 34.1k | return (AACDEC_DRC_GAIN_SCALING); |
1517 | 34.1k | } |