2 /* -----------------------------------------------------------------------------------------------------------
3 Software License for The Fraunhofer FDK AAC Codec Library for Android
5 © Copyright 1995 - 2013 Fraunhofer-Gesellschaft zur Förderung der angewandten Forschung e.V.
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.
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.
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.
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.
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:
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.
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.
42 The name of Fraunhofer may not be used to endorse or promote products derived from this library without
43 prior written permission.
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.
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."
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.
59 You may use this FDK AAC Codec software or modifications thereto only for purposes that are authorized
60 by appropriate patent licenses.
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.
73 5. CONTACT INFORMATION
75 Fraunhofer Institute for Integrated Circuits IIS
76 Attention: Audio and Multimedia Departments - FDK AAC LL
78 91058 Erlangen, Germany
80 www.iis.fraunhofer.de/amm
81 amm-info@iis.fraunhofer.de
82 ----------------------------------------------------------------------------------------------------------- */
84 /************************* Fast MPEG AAC Audio Encoder **********************
86 Initial author: A. Groeschel
87 contents/description: channel mapping functionality
89 ******************************************************************************/
91 #include "channel_map.h"
93 #include "psy_const.h"
95 #include "aacEnc_ram.h"
98 /* channel_assignment treats the relationship of Input file channels
99 to the encoder channels.
100 This is necessary because the usual order in RIFF files (.wav)
101 is different from the elements order in the coder given
102 by Table 8.1 (implicit speaker mapping) of the AAC standard.
104 In mono and stereo case, this is trivial.
105 In mc case, it looks like this:
107 Channel Input file coder chan
109 front center 2 0 (SCE channel)
110 left center 0 1 (1st of 1st CPE)
111 right center 1 2 (2nd of 1st CPE)
112 left surround 3 3 (1st of 2nd CPE)
113 right surround 4 4 (2nd of 2nd CPE)
116 front center 2 0 (SCE channel)
117 left center 0 1 (1st of 1st CPE)
118 right center 1 2 (2nd of 1st CPE)
119 left surround 4 3 (1st of 2nd CPE)
120 right surround 5 4 (2nd of 2nd CPE)
126 CHANNEL_MODE encoderMode
;
127 INT channel_assignment
[/*(8)*/12];
129 } CHANNEL_ASSIGNMENT_INFO_TAB
;
132 static const CHANNEL_ASSIGNMENT_INFO_TAB assignmentInfoTabMpeg
[] =
134 { MODE_INVALID
, {-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1} }, /* invalid */
135 { MODE_1
, { 0,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1} }, /* mono */
136 { MODE_2
, { 0, 1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1} }, /* stereo */
137 { MODE_1_2
, { 0, 1, 2,-1,-1,-1,-1,-1,-1,-1,-1,-1} }, /* 3ch */
138 { MODE_1_2_1
, { 0, 1, 2, 3,-1,-1,-1,-1,-1,-1,-1,-1} }, /* 4ch */
139 { MODE_1_2_2
, { 0, 1, 2, 3, 4,-1,-1,-1,-1,-1,-1,-1} }, /* 5ch */
140 { MODE_1_2_2_1
, { 0, 1, 2, 3, 4, 5,-1,-1,-1,-1,-1,-1} }, /* 5.1ch */
141 { MODE_1_2_2_2_1
, { 0, 1, 2, 3, 4, 5, 6, 7,-1,-1,-1,-1} }, /* 7.1ch */
142 { MODE_7_1_REAR_SURROUND
, { 0, 1, 2, 3, 4, 5, 6, 7,-1,-1,-1,-1} }, /* 7.1ch */
143 { MODE_7_1_FRONT_CENTER
, { 0, 1, 2, 3, 4, 5, 6, 7,-1,-1,-1,-1} } /* 7.1ch */
146 static const CHANNEL_ASSIGNMENT_INFO_TAB assignmentInfoTabWav
[] =
148 { MODE_INVALID
, {-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1} }, /* invalid */
149 { MODE_1
, { 0,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1} }, /* mono */
150 { MODE_2
, { 0, 1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1} }, /* stereo */
151 { MODE_1_2
, { 2, 0, 1,-1,-1,-1,-1,-1,-1,-1,-1,-1} }, /* 3ch */
152 { MODE_1_2_1
, { 2, 0, 1, 3,-1,-1,-1,-1,-1,-1,-1,-1} }, /* 4ch */
153 { MODE_1_2_2
, { 2, 0, 1, 3, 4,-1,-1,-1,-1,-1,-1,-1} }, /* 5ch */
154 { MODE_1_2_2_1
, { 2, 0, 1, 4, 5, 3,-1,-1,-1,-1,-1,-1} }, /* 5.1ch */
155 { MODE_1_2_2_2_1
, { 2, 6, 7, 0, 1, 4, 5, 3,-1,-1,-1,-1} }, /* 7.1ch */
156 { MODE_7_1_REAR_SURROUND
, { 2, 0, 1, 6, 7, 4, 5, 3,-1,-1,-1,-1} }, /* 7.1ch */
157 { MODE_7_1_FRONT_CENTER
, { 2, 6, 7, 0, 1, 4, 5, 3,-1,-1,-1,-1} }, /* 7.1ch */
160 static const CHANNEL_ASSIGNMENT_INFO_TAB assignmentInfoTabWg4
[] =
162 { MODE_INVALID
, {-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1} }, /* invalid */
163 { MODE_1
, { 0,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1} }, /* mono */
164 { MODE_2
, { 0, 1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1} }, /* stereo */
165 { MODE_1_2
, { 2, 0, 1,-1,-1,-1,-1,-1,-1,-1,-1,-1} }, /* 3ch */
166 { MODE_1_2_1
, { 3, 0, 1, 2,-1,-1,-1,-1,-1,-1,-1,-1} }, /* 4ch */
167 { MODE_1_2_2
, { 4, 0, 1, 2, 3,-1,-1,-1,-1,-1,-1,-1} }, /* 5ch */
168 { MODE_1_2_2_1
, { 4, 0, 1, 2, 3, 5,-1,-1,-1,-1,-1,-1} }, /* 5.1ch */
169 { MODE_1_2_2_2_1
, { 6, 0, 1, 2, 3, 4, 5, 7,-1,-1,-1,-1} }, /* 7.1ch */
172 /* Channel mode configuration tab provides,
173 corresponding number of channels and elements
175 static const CHANNEL_MODE_CONFIG_TAB channelModeConfig
[] =
177 { MODE_1
, 1, 1, 1 }, /* SCE */
178 { MODE_2
, 2, 2, 1 }, /* CPE */
179 { MODE_1_2
, 3, 3, 2 }, /* SCE,CPE */
180 { MODE_1_2_1
, 4, 4, 3 }, /* SCE,CPE,SCE */
181 { MODE_1_2_2
, 5, 5, 3 }, /* SCE,CPE,CPE */
182 { MODE_1_2_2_1
, 6, 5, 4 }, /* SCE,CPE,CPE,LFE */
183 { MODE_1_2_2_2_1
, 8, 7, 5 }, /* SCE,CPE,CPE,CPE,LFE */
184 { MODE_7_1_REAR_SURROUND
, 8, 7, 5 },
185 { MODE_7_1_FRONT_CENTER
, 8, 7, 5 },
188 #define MAX_MODES (sizeof(assignmentInfoTabWav)/sizeof(CHANNEL_ASSIGNMENT_INFO_TAB))
190 const INT
* FDKaacEnc_getChannelAssignment(CHANNEL_MODE encMode
, CHANNEL_ORDER co
)
192 const CHANNEL_ASSIGNMENT_INFO_TAB
*pTab
;
195 if (co
== CH_ORDER_MPEG
)
196 pTab
= assignmentInfoTabMpeg
;
197 else if (co
== CH_ORDER_WAV
)
198 pTab
= assignmentInfoTabWav
;
200 pTab
= assignmentInfoTabWg4
;
202 for(i
=MAX_MODES
-1; i
>0; i
--) {
203 if (encMode
== pTab
[i
].encoderMode
) {
207 return (pTab
[i
].channel_assignment
);
210 AAC_ENCODER_ERROR
FDKaacEnc_DetermineEncoderMode(CHANNEL_MODE
* mode
, INT nChannels
)
213 CHANNEL_MODE encMode
= MODE_INVALID
;
215 if (*mode
==MODE_UNKNOWN
) {
216 for (i
=0; i
<(INT
)sizeof(channelModeConfig
)/(INT
)sizeof(CHANNEL_MODE_CONFIG_TAB
); i
++) {
217 if (channelModeConfig
[i
].nChannels
==nChannels
) {
218 encMode
= channelModeConfig
[i
].encMode
;
225 /* check if valid channel configuration */
226 if (FDKaacEnc_GetChannelModeConfiguration(*mode
)->nChannels
==nChannels
) {
231 if (encMode
==MODE_INVALID
) {
232 return AAC_ENC_UNSUPPORTED_CHANNELCONFIG
;
238 static INT
FDKaacEnc_initElement (ELEMENT_INFO
* elInfo
, MP4_ELEMENT_ID elType
, INT
* cnt
, CHANNEL_MODE mode
, CHANNEL_ORDER co
, INT
* it_cnt
, const FIXP_DBL relBits
) {
243 const INT
*assign
= FDKaacEnc_getChannelAssignment(mode
, co
);
245 elInfo
->elType
=elType
;
246 elInfo
->relativeBits
= relBits
;
248 switch(elInfo
->elType
) {
249 case ID_SCE
: case ID_LFE
: case ID_CCE
:
250 elInfo
->nChannelsInEl
=1;
251 elInfo
->ChannelIndex
[0]=assign
[counter
++];
252 elInfo
->instanceTag
=it_cnt
[elType
]++;
256 elInfo
->nChannelsInEl
=2;
257 elInfo
->ChannelIndex
[0]=assign
[counter
++];
258 elInfo
->ChannelIndex
[1]=assign
[counter
++];
259 elInfo
->instanceTag
=it_cnt
[elType
]++;
262 elInfo
->nChannelsInEl
=0;
263 elInfo
->ChannelIndex
[0]=0;
264 elInfo
->ChannelIndex
[1]=0;
265 elInfo
->instanceTag
=it_cnt
[elType
]++;
274 AAC_ENCODER_ERROR
FDKaacEnc_InitChannelMapping(CHANNEL_MODE mode
, CHANNEL_ORDER co
, CHANNEL_MAPPING
* cm
)
276 INT count
=0; /* count through coder channels */
277 INT it_cnt
[ID_END
+1];
280 for (i
=0; i
<ID_END
; i
++)
283 FDKmemclear(cm
, sizeof(CHANNEL_MAPPING
));
285 /* init channel mapping*/
286 for (i
=0; i
<(INT
)sizeof(channelModeConfig
)/(INT
)sizeof(CHANNEL_MODE_CONFIG_TAB
); i
++) {
287 if (channelModeConfig
[i
].encMode
==mode
)
289 cm
->encMode
= channelModeConfig
[i
].encMode
;
290 cm
->nChannels
= channelModeConfig
[i
].nChannels
;
291 cm
->nChannelsEff
= channelModeConfig
[i
].nChannelsEff
;
292 cm
->nElements
= channelModeConfig
[i
].nElements
;
298 /* init element info struct */
302 FDKaacEnc_initElement(&cm
->elInfo
[0], ID_SCE
, &count
, mode
, co
, it_cnt
, (FIXP_DBL
)MAXVAL_DBL
);
306 FDKaacEnc_initElement(&cm
->elInfo
[0], ID_CPE
, &count
, mode
, co
, it_cnt
, (FIXP_DBL
)MAXVAL_DBL
);
311 FDKaacEnc_initElement(&cm
->elInfo
[0], ID_SCE
, &count
, mode
, co
, it_cnt
, FL2FXCONST_DBL(0.4f
));
312 FDKaacEnc_initElement(&cm
->elInfo
[1], ID_CPE
, &count
, mode
, co
, it_cnt
, FL2FXCONST_DBL(0.6f
));
316 /* sce + cpe + sce */
317 FDKaacEnc_initElement(&cm
->elInfo
[0], ID_SCE
, &count
, mode
, co
, it_cnt
, FL2FXCONST_DBL(0.3f
));
318 FDKaacEnc_initElement(&cm
->elInfo
[1], ID_CPE
, &count
, mode
, co
, it_cnt
, FL2FXCONST_DBL(0.4f
));
319 FDKaacEnc_initElement(&cm
->elInfo
[2], ID_SCE
, &count
, mode
, co
, it_cnt
, FL2FXCONST_DBL(0.3f
));
323 /* sce + cpe + cpe */
324 FDKaacEnc_initElement(&cm
->elInfo
[0], ID_SCE
, &count
, mode
, co
, it_cnt
, FL2FXCONST_DBL(0.26f
));
325 FDKaacEnc_initElement(&cm
->elInfo
[1], ID_CPE
, &count
, mode
, co
, it_cnt
, FL2FXCONST_DBL(0.37f
));
326 FDKaacEnc_initElement(&cm
->elInfo
[2], ID_CPE
, &count
, mode
, co
, it_cnt
, FL2FXCONST_DBL(0.37f
));
330 /* (5.1) sce + cpe + cpe + lfe */
331 FDKaacEnc_initElement(&cm
->elInfo
[0], ID_SCE
, &count
, mode
, co
, it_cnt
, FL2FXCONST_DBL(0.24f
));
332 FDKaacEnc_initElement(&cm
->elInfo
[1], ID_CPE
, &count
, mode
, co
, it_cnt
, FL2FXCONST_DBL(0.35f
));
333 FDKaacEnc_initElement(&cm
->elInfo
[2], ID_CPE
, &count
, mode
, co
, it_cnt
, FL2FXCONST_DBL(0.35f
));
334 FDKaacEnc_initElement(&cm
->elInfo
[3], ID_LFE
, &count
, mode
, co
, it_cnt
, FL2FXCONST_DBL(0.06f
));
338 case MODE_7_1_REAR_SURROUND
:
339 case MODE_7_1_FRONT_CENTER
:
340 /* (7.1) sce + cpe + cpe + cpe + lfe */
341 FDKaacEnc_initElement(&cm
->elInfo
[0], ID_SCE
, &count
, mode
, co
, it_cnt
, FL2FXCONST_DBL(0.18f
));
342 FDKaacEnc_initElement(&cm
->elInfo
[1], ID_CPE
, &count
, mode
, co
, it_cnt
, FL2FXCONST_DBL(0.26f
));
343 FDKaacEnc_initElement(&cm
->elInfo
[2], ID_CPE
, &count
, mode
, co
, it_cnt
, FL2FXCONST_DBL(0.26f
));
344 FDKaacEnc_initElement(&cm
->elInfo
[3], ID_CPE
, &count
, mode
, co
, it_cnt
, FL2FXCONST_DBL(0.26f
));
345 FDKaacEnc_initElement(&cm
->elInfo
[4], ID_LFE
, &count
, mode
, co
, it_cnt
, FL2FXCONST_DBL(0.04f
));
349 return AAC_ENC_UNSUPPORTED_CHANNELCONFIG
;
353 FDK_ASSERT(cm
->nElements
<=(8));
359 AAC_ENCODER_ERROR
FDKaacEnc_InitElementBits(QC_STATE
*hQC
,
365 int sc_brTot
= CountLeadingBits(bitrateTot
);
367 switch(cm
->encMode
) {
369 hQC
->elementBits
[0]->chBitrateEl
= bitrateTot
;
371 hQC
->elementBits
[0]->maxBitsEl
= maxChannelBits
;
373 hQC
->elementBits
[0]->relativeBitsEl
= cm
->elInfo
[0].relativeBits
;
377 hQC
->elementBits
[0]->chBitrateEl
= bitrateTot
>>1;
379 hQC
->elementBits
[0]->maxBitsEl
= 2*maxChannelBits
;
381 hQC
->elementBits
[0]->relativeBitsEl
= cm
->elInfo
[0].relativeBits
;
384 hQC
->elementBits
[0]->relativeBitsEl
= cm
->elInfo
[0].relativeBits
;
385 hQC
->elementBits
[1]->relativeBitsEl
= cm
->elInfo
[1].relativeBits
;
386 FIXP_DBL sceRate
= cm
->elInfo
[0].relativeBits
;
387 FIXP_DBL cpeRate
= cm
->elInfo
[1].relativeBits
;
389 hQC
->elementBits
[0]->chBitrateEl
= fMult(sceRate
, (FIXP_DBL
)(bitrateTot
<<sc_brTot
))>>sc_brTot
;
390 hQC
->elementBits
[1]->chBitrateEl
= fMult(cpeRate
, (FIXP_DBL
)(bitrateTot
<<sc_brTot
))>>(sc_brTot
+1);
392 hQC
->elementBits
[0]->maxBitsEl
= maxChannelBits
;
393 hQC
->elementBits
[1]->maxBitsEl
= 2*maxChannelBits
;
397 /* sce + cpe + sce */
398 hQC
->elementBits
[0]->relativeBitsEl
= cm
->elInfo
[0].relativeBits
;
399 hQC
->elementBits
[1]->relativeBitsEl
= cm
->elInfo
[1].relativeBits
;
400 hQC
->elementBits
[2]->relativeBitsEl
= cm
->elInfo
[2].relativeBits
;
401 FIXP_DBL sce1Rate
= cm
->elInfo
[0].relativeBits
;
402 FIXP_DBL cpeRate
= cm
->elInfo
[1].relativeBits
;
403 FIXP_DBL sce2Rate
= cm
->elInfo
[2].relativeBits
;
405 hQC
->elementBits
[0]->chBitrateEl
= fMult(sce1Rate
, (FIXP_DBL
)(bitrateTot
<<sc_brTot
))>>sc_brTot
;
406 hQC
->elementBits
[1]->chBitrateEl
= fMult(cpeRate
, (FIXP_DBL
)(bitrateTot
<<sc_brTot
))>>(sc_brTot
+1);
407 hQC
->elementBits
[2]->chBitrateEl
= fMult(sce2Rate
, (FIXP_DBL
)(bitrateTot
<<sc_brTot
))>>sc_brTot
;
409 hQC
->elementBits
[0]->maxBitsEl
= maxChannelBits
;
410 hQC
->elementBits
[1]->maxBitsEl
= 2*maxChannelBits
;
411 hQC
->elementBits
[2]->maxBitsEl
= maxChannelBits
;
415 /* sce + cpe + cpe */
416 hQC
->elementBits
[0]->relativeBitsEl
= cm
->elInfo
[0].relativeBits
;
417 hQC
->elementBits
[1]->relativeBitsEl
= cm
->elInfo
[1].relativeBits
;
418 hQC
->elementBits
[2]->relativeBitsEl
= cm
->elInfo
[2].relativeBits
;
419 FIXP_DBL sceRate
= cm
->elInfo
[0].relativeBits
;
420 FIXP_DBL cpe1Rate
= cm
->elInfo
[1].relativeBits
;
421 FIXP_DBL cpe2Rate
= cm
->elInfo
[2].relativeBits
;
423 hQC
->elementBits
[0]->chBitrateEl
= fMult(sceRate
, (FIXP_DBL
)(bitrateTot
<<sc_brTot
))>>sc_brTot
;
424 hQC
->elementBits
[1]->chBitrateEl
= fMult(cpe1Rate
, (FIXP_DBL
)(bitrateTot
<<sc_brTot
))>>(sc_brTot
+1);
425 hQC
->elementBits
[2]->chBitrateEl
= fMult(cpe2Rate
, (FIXP_DBL
)(bitrateTot
<<sc_brTot
))>>(sc_brTot
+1);
427 hQC
->elementBits
[0]->maxBitsEl
= maxChannelBits
;
428 hQC
->elementBits
[1]->maxBitsEl
= 2*maxChannelBits
;
429 hQC
->elementBits
[2]->maxBitsEl
= 2*maxChannelBits
;
434 /* (5.1) sce + cpe + cpe + lfe */
435 hQC
->elementBits
[0]->relativeBitsEl
= cm
->elInfo
[0].relativeBits
;
436 hQC
->elementBits
[1]->relativeBitsEl
= cm
->elInfo
[1].relativeBits
;
437 hQC
->elementBits
[2]->relativeBitsEl
= cm
->elInfo
[2].relativeBits
;
438 hQC
->elementBits
[3]->relativeBitsEl
= cm
->elInfo
[3].relativeBits
;
439 FIXP_DBL sceRate
= cm
->elInfo
[0].relativeBits
;
440 FIXP_DBL cpe1Rate
= cm
->elInfo
[1].relativeBits
;
441 FIXP_DBL cpe2Rate
= cm
->elInfo
[2].relativeBits
;
442 FIXP_DBL lfeRate
= cm
->elInfo
[3].relativeBits
;
444 int maxBitsTot
= maxChannelBits
* 5; /* LFE does not add to bit reservoir */
445 int sc
= CountLeadingBits(fixMax(maxChannelBits
,averageBitsTot
));
446 int maxLfeBits
= (int) FDKmax ( (INT
)((fMult(lfeRate
,(FIXP_DBL
)(maxChannelBits
<<sc
))>>sc
)<<1),
447 (INT
)((fMult(FL2FXCONST_DBL(1.1f
/2.f
),fMult(lfeRate
,(FIXP_DBL
)(averageBitsTot
<<sc
)))<<1)>>sc
) );
449 maxChannelBits
= (maxBitsTot
- maxLfeBits
);
450 sc
= CountLeadingBits(maxChannelBits
);
452 maxChannelBits
= fMult((FIXP_DBL
)maxChannelBits
<<sc
,GetInvInt(5))>>sc
;
454 hQC
->elementBits
[0]->chBitrateEl
= fMult(sceRate
, (FIXP_DBL
)(bitrateTot
<<sc_brTot
))>>sc_brTot
;
455 hQC
->elementBits
[1]->chBitrateEl
= fMult(cpe1Rate
, (FIXP_DBL
)(bitrateTot
<<sc_brTot
))>>(sc_brTot
+1);
456 hQC
->elementBits
[2]->chBitrateEl
= fMult(cpe2Rate
, (FIXP_DBL
)(bitrateTot
<<sc_brTot
))>>(sc_brTot
+1);
457 hQC
->elementBits
[3]->chBitrateEl
= fMult(lfeRate
, (FIXP_DBL
)(bitrateTot
<<sc_brTot
))>>sc_brTot
;
459 hQC
->elementBits
[0]->maxBitsEl
= maxChannelBits
;
460 hQC
->elementBits
[1]->maxBitsEl
= 2*maxChannelBits
;
461 hQC
->elementBits
[2]->maxBitsEl
= 2*maxChannelBits
;
462 hQC
->elementBits
[3]->maxBitsEl
= maxLfeBits
;
466 case MODE_7_1_REAR_SURROUND
:
467 case MODE_7_1_FRONT_CENTER
:
468 case MODE_1_2_2_2_1
: {
472 /* (7.1) sce + cpe + cpe + cpe + lfe */
473 FIXP_DBL sceRate
= hQC
->elementBits
[0]->relativeBitsEl
= cm
->elInfo
[0].relativeBits
;
474 FIXP_DBL cpe1Rate
= hQC
->elementBits
[1]->relativeBitsEl
= cm
->elInfo
[1].relativeBits
;
475 FIXP_DBL cpe2Rate
= hQC
->elementBits
[2]->relativeBitsEl
= cm
->elInfo
[2].relativeBits
;
476 FIXP_DBL cpe3Rate
= hQC
->elementBits
[cpe3Idx
]->relativeBitsEl
= cm
->elInfo
[cpe3Idx
].relativeBits
;
477 FIXP_DBL lfeRate
= hQC
->elementBits
[lfeIdx
]->relativeBitsEl
= cm
->elInfo
[lfeIdx
].relativeBits
;
479 int maxBitsTot
= maxChannelBits
* 7; /* LFE does not add to bit reservoir */
480 int sc
= CountLeadingBits(fixMax(maxChannelBits
,averageBitsTot
));
481 int maxLfeBits
= (int) FDKmax ( (INT
)((fMult(lfeRate
,(FIXP_DBL
)(maxChannelBits
<<sc
))>>sc
)<<1),
482 (INT
)((fMult(FL2FXCONST_DBL(1.1f
/2.f
),fMult(lfeRate
,(FIXP_DBL
)(averageBitsTot
<<sc
)))<<1)>>sc
) );
484 maxChannelBits
= (maxBitsTot
- maxLfeBits
) / 7;
486 hQC
->elementBits
[0]->chBitrateEl
= fMult(sceRate
, (FIXP_DBL
)(bitrateTot
<<sc_brTot
))>>sc_brTot
;
487 hQC
->elementBits
[1]->chBitrateEl
= fMult(cpe1Rate
, (FIXP_DBL
)(bitrateTot
<<sc_brTot
))>>(sc_brTot
+1);
488 hQC
->elementBits
[2]->chBitrateEl
= fMult(cpe2Rate
, (FIXP_DBL
)(bitrateTot
<<sc_brTot
))>>(sc_brTot
+1);
489 hQC
->elementBits
[cpe3Idx
]->chBitrateEl
= fMult(cpe3Rate
, (FIXP_DBL
)(bitrateTot
<<sc_brTot
))>>(sc_brTot
+1);
490 hQC
->elementBits
[lfeIdx
]->chBitrateEl
= fMult(lfeRate
, (FIXP_DBL
)(bitrateTot
<<sc_brTot
))>>sc_brTot
;
492 hQC
->elementBits
[0]->maxBitsEl
= maxChannelBits
;
493 hQC
->elementBits
[1]->maxBitsEl
= 2*maxChannelBits
;
494 hQC
->elementBits
[2]->maxBitsEl
= 2*maxChannelBits
;
495 hQC
->elementBits
[cpe3Idx
]->maxBitsEl
= 2*maxChannelBits
;
496 hQC
->elementBits
[lfeIdx
]->maxBitsEl
= maxLfeBits
;
500 return AAC_ENC_UNSUPPORTED_CHANNELCONFIG
;
506 /********************************************************************************/
508 /* function: GetMonoStereoMODE(const CHANNEL_MODE mode) */
510 /* description: Determines encoder setting from channel mode. */
511 /* Multichannel modes are mapped to mono or stereo modes */
512 /* returns MODE_MONO in case of mono, */
513 /* MODE_STEREO in case of stereo */
514 /* MODE_INVALID in case of error */
516 /* input: CHANNEL_MODE mode: Encoder mode (see qc_data.h). */
517 /* output: return: CM_STEREO_MODE monoStereoSetting */
518 /* (MODE_INVALID: error, */
519 /* MODE_MONO: mono */
520 /* MODE_STEREO: stereo). */
522 /* misc: No memory is allocated. */
524 /********************************************************************************/
526 ELEMENT_MODE
FDKaacEnc_GetMonoStereoMode(const CHANNEL_MODE mode
){
528 ELEMENT_MODE monoStereoSetting
= EL_MODE_INVALID
;
531 case MODE_1
: /* mono setups */
532 monoStereoSetting
= EL_MODE_MONO
;
534 case MODE_2
: /* stereo setups */
540 case MODE_7_1_REAR_SURROUND
:
541 case MODE_7_1_FRONT_CENTER
:
542 monoStereoSetting
= EL_MODE_STEREO
;
545 monoStereoSetting
= EL_MODE_INVALID
;
549 return monoStereoSetting
;
552 const CHANNEL_MODE_CONFIG_TAB
* FDKaacEnc_GetChannelModeConfiguration(const CHANNEL_MODE mode
)
555 const CHANNEL_MODE_CONFIG_TAB
*cm_config
= NULL
;
557 /* get channel mode config */
558 for (i
=0; i
<(INT
)sizeof(channelModeConfig
)/(INT
)sizeof(CHANNEL_MODE_CONFIG_TAB
); i
++) {
559 if (channelModeConfig
[i
].encMode
==mode
)
561 cm_config
= &channelModeConfig
[i
];