• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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):   A. Groeschel
98 
99    Description: channel mapping functionality
100 
101 *******************************************************************************/
102 
103 #include "channel_map.h"
104 #include "bitenc.h"
105 #include "psy_const.h"
106 #include "qc_data.h"
107 #include "aacEnc_ram.h"
108 #include "FDK_tools_rom.h"
109 
110 /* channel_assignment treats the relationship of Input file channels
111    to the encoder channels.
112    This is necessary because the usual order in RIFF files (.wav)
113    is different from the elements order in the coder given
114    by Table 8.1 (implicit speaker mapping) of the AAC standard.
115 
116    In mono and stereo case, this is trivial.
117    In mc case, it looks like this:
118 
119    Channel         Input file       coder chan
120 5ch:
121    front center    2                0 (SCE channel)
122    left center     0                1 (1st of 1st CPE)
123    right center    1                2 (2nd of 1st CPE)
124    left surround   3                3 (1st of 2nd CPE)
125    right surround  4                4 (2nd of 2nd CPE)
126 
127 5.1ch:
128    front center    2                0 (SCE channel)
129    left center     0                1 (1st of 1st CPE)
130    right center    1                2 (2nd of 1st CPE)
131    left surround   4                3 (1st of 2nd CPE)
132    right surround  5                4 (2nd of 2nd CPE)
133    LFE             3                5 (LFE)
134 */
135 
136 /* Channel mode configuration tab provides,
137    corresponding number of channels and elements
138 */
139 static const CHANNEL_MODE_CONFIG_TAB channelModeConfig[] = {
140     {MODE_1, 1, 1, 1},             /* chCfg  1, SCE                 */
141     {MODE_2, 2, 2, 1},             /* chCfg  2, CPE                 */
142     {MODE_1_2, 3, 3, 2},           /* chCfg  3, SCE,CPE             */
143     {MODE_1_2_1, 4, 4, 3},         /* chCfg  4, SCE,CPE,SCE         */
144     {MODE_1_2_2, 5, 5, 3},         /* chCfg  5, SCE,CPE,CPE         */
145     {MODE_1_2_2_1, 6, 5, 4},       /* chCfg  6, SCE,CPE,CPE,LFE     */
146     {MODE_1_2_2_2_1, 8, 7, 5},     /* chCfg  7, SCE,CPE,CPE,CPE,LFE */
147     {MODE_6_1, 7, 6, 5},           /* chCfg 11, SCE,CPE,CPE,SCE,LFE */
148     {MODE_7_1_BACK, 8, 7, 5},      /* chCfg 12, SCE,CPE,CPE,CPE,LFE */
149     {MODE_7_1_TOP_FRONT, 8, 7, 5}, /* chCfg 14, SCE,CPE,CPE,LFE,CPE */
150     {MODE_7_1_REAR_SURROUND, 8, 7,
151      5}, /* same as MODE_7_1_BACK,  SCE,CPE,CPE,CPE,LFE */
152     {MODE_7_1_FRONT_CENTER, 8, 7,
153      5}, /* same as MODE_1_2_2_2_1, SCE,CPE,CPE,CPE,LFE */
154 
155 };
156 
FDKaacEnc_DetermineEncoderMode(CHANNEL_MODE * mode,INT nChannels)157 AAC_ENCODER_ERROR FDKaacEnc_DetermineEncoderMode(CHANNEL_MODE* mode,
158                                                  INT nChannels) {
159   INT i;
160   CHANNEL_MODE encMode = MODE_INVALID;
161 
162   if (*mode == MODE_UNKNOWN) {
163     for (i = 0; i < (INT)sizeof(channelModeConfig) /
164                         (INT)sizeof(CHANNEL_MODE_CONFIG_TAB);
165          i++) {
166       if (channelModeConfig[i].nChannels == nChannels) {
167         encMode = channelModeConfig[i].encMode;
168         break;
169       }
170     }
171     *mode = encMode;
172   } else {
173     /* check if valid channel configuration */
174     if (FDKaacEnc_GetChannelModeConfiguration(*mode)->nChannels == nChannels) {
175       encMode = *mode;
176     }
177   }
178 
179   if (encMode == MODE_INVALID) {
180     return AAC_ENC_UNSUPPORTED_CHANNELCONFIG;
181   }
182 
183   return AAC_ENC_OK;
184 }
185 
FDKaacEnc_initElement(ELEMENT_INFO * elInfo,MP4_ELEMENT_ID elType,INT * cnt,FDK_channelMapDescr * mapDescr,UINT mapIdx,INT * it_cnt,const FIXP_DBL relBits)186 static INT FDKaacEnc_initElement(ELEMENT_INFO* elInfo, MP4_ELEMENT_ID elType,
187                                  INT* cnt, FDK_channelMapDescr* mapDescr,
188                                  UINT mapIdx, INT* it_cnt,
189                                  const FIXP_DBL relBits) {
190   INT error = 0;
191   INT counter = *cnt;
192 
193   elInfo->elType = elType;
194   elInfo->relativeBits = relBits;
195 
196   switch (elInfo->elType) {
197     case ID_SCE:
198     case ID_LFE:
199     case ID_CCE:
200       elInfo->nChannelsInEl = 1;
201       elInfo->ChannelIndex[0] =
202           FDK_chMapDescr_getMapValue(mapDescr, counter++, mapIdx);
203       elInfo->instanceTag = it_cnt[elType]++;
204       break;
205     case ID_CPE:
206       elInfo->nChannelsInEl = 2;
207       elInfo->ChannelIndex[0] =
208           FDK_chMapDescr_getMapValue(mapDescr, counter++, mapIdx);
209       elInfo->ChannelIndex[1] =
210           FDK_chMapDescr_getMapValue(mapDescr, counter++, mapIdx);
211       elInfo->instanceTag = it_cnt[elType]++;
212       break;
213     case ID_DSE:
214       elInfo->nChannelsInEl = 0;
215       elInfo->ChannelIndex[0] = 0;
216       elInfo->ChannelIndex[1] = 0;
217       elInfo->instanceTag = it_cnt[elType]++;
218       break;
219     default:
220       error = 1;
221   };
222   *cnt = counter;
223   return error;
224 }
225 
FDKaacEnc_InitChannelMapping(CHANNEL_MODE mode,CHANNEL_ORDER co,CHANNEL_MAPPING * cm)226 AAC_ENCODER_ERROR FDKaacEnc_InitChannelMapping(CHANNEL_MODE mode,
227                                                CHANNEL_ORDER co,
228                                                CHANNEL_MAPPING* cm) {
229   INT count = 0; /* count through coder channels */
230   INT it_cnt[ID_END + 1];
231   INT i;
232   UINT mapIdx;
233   FDK_channelMapDescr mapDescr;
234 
235   for (i = 0; i < ID_END; i++) it_cnt[i] = 0;
236 
237   FDKmemclear(cm, sizeof(CHANNEL_MAPPING));
238 
239   /* init channel mapping*/
240   for (i = 0; i < (INT)sizeof(channelModeConfig) /
241                       (INT)sizeof(CHANNEL_MODE_CONFIG_TAB);
242        i++) {
243     if (channelModeConfig[i].encMode == mode) {
244       cm->encMode = channelModeConfig[i].encMode;
245       cm->nChannels = channelModeConfig[i].nChannels;
246       cm->nChannelsEff = channelModeConfig[i].nChannelsEff;
247       cm->nElements = channelModeConfig[i].nElements;
248 
249       break;
250     }
251   }
252 
253   /* init map descriptor */
254   FDK_chMapDescr_init(&mapDescr, NULL, 0, (co == CH_ORDER_MPEG) ? 1 : 0);
255   switch (mode) {
256     case MODE_7_1_REAR_SURROUND: /* MODE_7_1_REAR_SURROUND is equivalent to
257                                     MODE_7_1_BACK */
258       mapIdx = (INT)MODE_7_1_BACK;
259       break;
260     case MODE_7_1_FRONT_CENTER: /* MODE_7_1_FRONT_CENTER is equivalent to
261                                    MODE_1_2_2_2_1 */
262       mapIdx = (INT)MODE_1_2_2_2_1;
263       break;
264     default:
265       mapIdx =
266           (INT)mode > 14
267               ? 0
268               : (INT)
269                     mode; /* if channel config > 14 MPEG mapping will be used */
270   }
271 
272   /* init element info struct */
273   switch (mode) {
274     case MODE_1:
275       /* (mono) sce  */
276       FDKaacEnc_initElement(&cm->elInfo[0], ID_SCE, &count, &mapDescr, mapIdx,
277                             it_cnt, (FIXP_DBL)MAXVAL_DBL);
278       break;
279     case MODE_2:
280       /* (stereo) cpe */
281       FDKaacEnc_initElement(&cm->elInfo[0], ID_CPE, &count, &mapDescr, mapIdx,
282                             it_cnt, (FIXP_DBL)MAXVAL_DBL);
283       break;
284 
285     case MODE_1_2:
286       /* sce + cpe */
287       FDKaacEnc_initElement(&cm->elInfo[0], ID_SCE, &count, &mapDescr, mapIdx,
288                             it_cnt, FL2FXCONST_DBL(0.4f));
289       FDKaacEnc_initElement(&cm->elInfo[1], ID_CPE, &count, &mapDescr, mapIdx,
290                             it_cnt, FL2FXCONST_DBL(0.6f));
291       break;
292 
293     case MODE_1_2_1:
294       /* sce + cpe + sce */
295       FDKaacEnc_initElement(&cm->elInfo[0], ID_SCE, &count, &mapDescr, mapIdx,
296                             it_cnt, FL2FXCONST_DBL(0.3f));
297       FDKaacEnc_initElement(&cm->elInfo[1], ID_CPE, &count, &mapDescr, mapIdx,
298                             it_cnt, FL2FXCONST_DBL(0.4f));
299       FDKaacEnc_initElement(&cm->elInfo[2], ID_SCE, &count, &mapDescr, mapIdx,
300                             it_cnt, FL2FXCONST_DBL(0.3f));
301       break;
302 
303     case MODE_1_2_2:
304       /* sce + cpe + cpe */
305       FDKaacEnc_initElement(&cm->elInfo[0], ID_SCE, &count, &mapDescr, mapIdx,
306                             it_cnt, FL2FXCONST_DBL(0.26f));
307       FDKaacEnc_initElement(&cm->elInfo[1], ID_CPE, &count, &mapDescr, mapIdx,
308                             it_cnt, FL2FXCONST_DBL(0.37f));
309       FDKaacEnc_initElement(&cm->elInfo[2], ID_CPE, &count, &mapDescr, mapIdx,
310                             it_cnt, FL2FXCONST_DBL(0.37f));
311       break;
312 
313     case MODE_1_2_2_1:
314       /* (5.1) sce + cpe + cpe + lfe */
315       FDKaacEnc_initElement(&cm->elInfo[0], ID_SCE, &count, &mapDescr, mapIdx,
316                             it_cnt, FL2FXCONST_DBL(0.24f));
317       FDKaacEnc_initElement(&cm->elInfo[1], ID_CPE, &count, &mapDescr, mapIdx,
318                             it_cnt, FL2FXCONST_DBL(0.35f));
319       FDKaacEnc_initElement(&cm->elInfo[2], ID_CPE, &count, &mapDescr, mapIdx,
320                             it_cnt, FL2FXCONST_DBL(0.35f));
321       FDKaacEnc_initElement(&cm->elInfo[3], ID_LFE, &count, &mapDescr, mapIdx,
322                             it_cnt, FL2FXCONST_DBL(0.06f));
323       break;
324 
325     case MODE_6_1:
326       /* (6.1) sce + cpe + cpe + sce + lfe */
327       FDKaacEnc_initElement(&cm->elInfo[0], ID_SCE, &count, &mapDescr, mapIdx,
328                             it_cnt, FL2FXCONST_DBL(0.2f));
329       FDKaacEnc_initElement(&cm->elInfo[1], ID_CPE, &count, &mapDescr, mapIdx,
330                             it_cnt, FL2FXCONST_DBL(0.275f));
331       FDKaacEnc_initElement(&cm->elInfo[2], ID_CPE, &count, &mapDescr, mapIdx,
332                             it_cnt, FL2FXCONST_DBL(0.275f));
333       FDKaacEnc_initElement(&cm->elInfo[3], ID_SCE, &count, &mapDescr, mapIdx,
334                             it_cnt, FL2FXCONST_DBL(0.2f));
335       FDKaacEnc_initElement(&cm->elInfo[4], ID_LFE, &count, &mapDescr, mapIdx,
336                             it_cnt, FL2FXCONST_DBL(0.05f));
337       break;
338 
339     case MODE_1_2_2_2_1:
340     case MODE_7_1_BACK:
341     case MODE_7_1_TOP_FRONT:
342     case MODE_7_1_REAR_SURROUND:
343     case MODE_7_1_FRONT_CENTER: {
344       /* (7.1) sce + cpe + cpe + cpe + lfe */
345       /* (7.1 top) sce + cpe + cpe + lfe + cpe */
346 
347       FDKaacEnc_initElement(&cm->elInfo[0], ID_SCE, &count, &mapDescr, mapIdx,
348                             it_cnt, FL2FXCONST_DBL(0.18f));
349       FDKaacEnc_initElement(&cm->elInfo[1], ID_CPE, &count, &mapDescr, mapIdx,
350                             it_cnt, FL2FXCONST_DBL(0.26f));
351       FDKaacEnc_initElement(&cm->elInfo[2], ID_CPE, &count, &mapDescr, mapIdx,
352                             it_cnt, FL2FXCONST_DBL(0.26f));
353       if (mode != MODE_7_1_TOP_FRONT) {
354         FDKaacEnc_initElement(&cm->elInfo[3], ID_CPE, &count, &mapDescr, mapIdx,
355                               it_cnt, FL2FXCONST_DBL(0.26f));
356         FDKaacEnc_initElement(&cm->elInfo[4], ID_LFE, &count, &mapDescr, mapIdx,
357                               it_cnt, FL2FXCONST_DBL(0.04f));
358       } else {
359         FDKaacEnc_initElement(&cm->elInfo[3], ID_LFE, &count, &mapDescr, mapIdx,
360                               it_cnt, FL2FXCONST_DBL(0.04f));
361         FDKaacEnc_initElement(&cm->elInfo[4], ID_CPE, &count, &mapDescr, mapIdx,
362                               it_cnt, FL2FXCONST_DBL(0.26f));
363       }
364       break;
365     }
366 
367     default:
368       //*chMap=0;
369       return AAC_ENC_UNSUPPORTED_CHANNELCONFIG;
370   };
371 
372   FDK_ASSERT(cm->nElements <= ((8)));
373 
374   return AAC_ENC_OK;
375 }
376 
FDKaacEnc_InitElementBits(QC_STATE * hQC,CHANNEL_MAPPING * cm,INT bitrateTot,INT averageBitsTot,INT maxChannelBits)377 AAC_ENCODER_ERROR FDKaacEnc_InitElementBits(QC_STATE* hQC, CHANNEL_MAPPING* cm,
378                                             INT bitrateTot, INT averageBitsTot,
379                                             INT maxChannelBits) {
380   int sc_brTot = CountLeadingBits(bitrateTot);
381 
382   switch (cm->encMode) {
383     case MODE_1:
384       hQC->elementBits[0]->chBitrateEl = bitrateTot;
385 
386       hQC->elementBits[0]->maxBitsEl = maxChannelBits;
387 
388       hQC->elementBits[0]->relativeBitsEl = cm->elInfo[0].relativeBits;
389       break;
390 
391     case MODE_2:
392       hQC->elementBits[0]->chBitrateEl = bitrateTot >> 1;
393 
394       hQC->elementBits[0]->maxBitsEl = 2 * maxChannelBits;
395 
396       hQC->elementBits[0]->relativeBitsEl = cm->elInfo[0].relativeBits;
397       break;
398     case MODE_1_2: {
399       hQC->elementBits[0]->relativeBitsEl = cm->elInfo[0].relativeBits;
400       hQC->elementBits[1]->relativeBitsEl = cm->elInfo[1].relativeBits;
401       FIXP_DBL sceRate = cm->elInfo[0].relativeBits;
402       FIXP_DBL cpeRate = cm->elInfo[1].relativeBits;
403 
404       hQC->elementBits[0]->chBitrateEl =
405           fMult(sceRate, (FIXP_DBL)(bitrateTot << sc_brTot)) >> sc_brTot;
406       hQC->elementBits[1]->chBitrateEl =
407           fMult(cpeRate, (FIXP_DBL)(bitrateTot << sc_brTot)) >> (sc_brTot + 1);
408 
409       hQC->elementBits[0]->maxBitsEl = maxChannelBits;
410       hQC->elementBits[1]->maxBitsEl = 2 * maxChannelBits;
411       break;
412     }
413     case MODE_1_2_1: {
414       /* sce + cpe + sce */
415       hQC->elementBits[0]->relativeBitsEl = cm->elInfo[0].relativeBits;
416       hQC->elementBits[1]->relativeBitsEl = cm->elInfo[1].relativeBits;
417       hQC->elementBits[2]->relativeBitsEl = cm->elInfo[2].relativeBits;
418       FIXP_DBL sce1Rate = cm->elInfo[0].relativeBits;
419       FIXP_DBL cpeRate = cm->elInfo[1].relativeBits;
420       FIXP_DBL sce2Rate = cm->elInfo[2].relativeBits;
421 
422       hQC->elementBits[0]->chBitrateEl =
423           fMult(sce1Rate, (FIXP_DBL)(bitrateTot << sc_brTot)) >> sc_brTot;
424       hQC->elementBits[1]->chBitrateEl =
425           fMult(cpeRate, (FIXP_DBL)(bitrateTot << sc_brTot)) >> (sc_brTot + 1);
426       hQC->elementBits[2]->chBitrateEl =
427           fMult(sce2Rate, (FIXP_DBL)(bitrateTot << sc_brTot)) >> sc_brTot;
428 
429       hQC->elementBits[0]->maxBitsEl = maxChannelBits;
430       hQC->elementBits[1]->maxBitsEl = 2 * maxChannelBits;
431       hQC->elementBits[2]->maxBitsEl = maxChannelBits;
432       break;
433     }
434     case MODE_1_2_2: {
435       /* sce + cpe + cpe */
436       hQC->elementBits[0]->relativeBitsEl = cm->elInfo[0].relativeBits;
437       hQC->elementBits[1]->relativeBitsEl = cm->elInfo[1].relativeBits;
438       hQC->elementBits[2]->relativeBitsEl = cm->elInfo[2].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 
443       hQC->elementBits[0]->chBitrateEl =
444           fMult(sceRate, (FIXP_DBL)(bitrateTot << sc_brTot)) >> sc_brTot;
445       hQC->elementBits[1]->chBitrateEl =
446           fMult(cpe1Rate, (FIXP_DBL)(bitrateTot << sc_brTot)) >> (sc_brTot + 1);
447       hQC->elementBits[2]->chBitrateEl =
448           fMult(cpe2Rate, (FIXP_DBL)(bitrateTot << sc_brTot)) >> (sc_brTot + 1);
449 
450       hQC->elementBits[0]->maxBitsEl = maxChannelBits;
451       hQC->elementBits[1]->maxBitsEl = 2 * maxChannelBits;
452       hQC->elementBits[2]->maxBitsEl = 2 * maxChannelBits;
453       break;
454     }
455     case MODE_1_2_2_1: {
456       /* (5.1) sce + cpe + cpe + lfe */
457       hQC->elementBits[0]->relativeBitsEl = cm->elInfo[0].relativeBits;
458       hQC->elementBits[1]->relativeBitsEl = cm->elInfo[1].relativeBits;
459       hQC->elementBits[2]->relativeBitsEl = cm->elInfo[2].relativeBits;
460       hQC->elementBits[3]->relativeBitsEl = cm->elInfo[3].relativeBits;
461       FIXP_DBL sceRate = cm->elInfo[0].relativeBits;
462       FIXP_DBL cpe1Rate = cm->elInfo[1].relativeBits;
463       FIXP_DBL cpe2Rate = cm->elInfo[2].relativeBits;
464       FIXP_DBL lfeRate = cm->elInfo[3].relativeBits;
465 
466       int maxBitsTot =
467           maxChannelBits * 5; /* LFE does not add to bit reservoir */
468       int sc = CountLeadingBits(fixMax(maxChannelBits, averageBitsTot));
469       int maxLfeBits = (int)fMax(
470           (INT)((fMult(lfeRate, (FIXP_DBL)(maxChannelBits << sc)) >> sc) << 1),
471           (INT)((fMult(FL2FXCONST_DBL(1.1f / 2.f),
472                        fMult(lfeRate, (FIXP_DBL)(averageBitsTot << sc)))
473                  << 1) >>
474                 sc));
475 
476       maxChannelBits = (maxBitsTot - maxLfeBits);
477       sc = CountLeadingBits(maxChannelBits);
478 
479       maxChannelBits =
480           fMult((FIXP_DBL)maxChannelBits << sc, GetInvInt(5)) >> sc;
481 
482       hQC->elementBits[0]->chBitrateEl =
483           fMult(sceRate, (FIXP_DBL)(bitrateTot << sc_brTot)) >> sc_brTot;
484       hQC->elementBits[1]->chBitrateEl =
485           fMult(cpe1Rate, (FIXP_DBL)(bitrateTot << sc_brTot)) >> (sc_brTot + 1);
486       hQC->elementBits[2]->chBitrateEl =
487           fMult(cpe2Rate, (FIXP_DBL)(bitrateTot << sc_brTot)) >> (sc_brTot + 1);
488       hQC->elementBits[3]->chBitrateEl =
489           fMult(lfeRate, (FIXP_DBL)(bitrateTot << sc_brTot)) >> sc_brTot;
490 
491       hQC->elementBits[0]->maxBitsEl = maxChannelBits;
492       hQC->elementBits[1]->maxBitsEl = 2 * maxChannelBits;
493       hQC->elementBits[2]->maxBitsEl = 2 * maxChannelBits;
494       hQC->elementBits[3]->maxBitsEl = maxLfeBits;
495 
496       break;
497     }
498     case MODE_6_1: {
499       /* (6.1) sce + cpe + cpe + sce + lfe */
500       FIXP_DBL sceRate = hQC->elementBits[0]->relativeBitsEl =
501           cm->elInfo[0].relativeBits;
502       FIXP_DBL cpe1Rate = hQC->elementBits[1]->relativeBitsEl =
503           cm->elInfo[1].relativeBits;
504       FIXP_DBL cpe2Rate = hQC->elementBits[2]->relativeBitsEl =
505           cm->elInfo[2].relativeBits;
506       FIXP_DBL sce2Rate = hQC->elementBits[3]->relativeBitsEl =
507           cm->elInfo[3].relativeBits;
508       FIXP_DBL lfeRate = hQC->elementBits[4]->relativeBitsEl =
509           cm->elInfo[4].relativeBits;
510 
511       int maxBitsTot =
512           maxChannelBits * 6; /* LFE does not add to bit reservoir */
513       int sc = CountLeadingBits(fixMax(maxChannelBits, averageBitsTot));
514       int maxLfeBits = (int)fMax(
515           (INT)((fMult(lfeRate, (FIXP_DBL)(maxChannelBits << sc)) >> sc) << 1),
516           (INT)((fMult(FL2FXCONST_DBL(1.1f / 2.f),
517                        fMult(lfeRate, (FIXP_DBL)(averageBitsTot << sc)))
518                  << 1) >>
519                 sc));
520 
521       maxChannelBits = (maxBitsTot - maxLfeBits) / 6;
522 
523       hQC->elementBits[0]->chBitrateEl =
524           fMult(sceRate, (FIXP_DBL)(bitrateTot << sc_brTot)) >> sc_brTot;
525       hQC->elementBits[1]->chBitrateEl =
526           fMult(cpe1Rate, (FIXP_DBL)(bitrateTot << sc_brTot)) >> (sc_brTot + 1);
527       hQC->elementBits[2]->chBitrateEl =
528           fMult(cpe2Rate, (FIXP_DBL)(bitrateTot << sc_brTot)) >> (sc_brTot + 1);
529       hQC->elementBits[3]->chBitrateEl =
530           fMult(sce2Rate, (FIXP_DBL)(bitrateTot << sc_brTot)) >> (sc_brTot + 1);
531       hQC->elementBits[4]->chBitrateEl =
532           fMult(lfeRate, (FIXP_DBL)(bitrateTot << sc_brTot)) >> sc_brTot;
533 
534       hQC->elementBits[0]->maxBitsEl = maxChannelBits;
535       hQC->elementBits[1]->maxBitsEl = 2 * maxChannelBits;
536       hQC->elementBits[2]->maxBitsEl = 2 * maxChannelBits;
537       hQC->elementBits[3]->maxBitsEl = maxChannelBits;
538       hQC->elementBits[4]->maxBitsEl = maxLfeBits;
539       break;
540     }
541     case MODE_7_1_TOP_FRONT:
542     case MODE_7_1_BACK:
543     case MODE_7_1_REAR_SURROUND:
544     case MODE_7_1_FRONT_CENTER:
545     case MODE_1_2_2_2_1: {
546       int cpe3Idx = (cm->encMode != MODE_7_1_TOP_FRONT) ? 3 : 4;
547       int lfeIdx = (cm->encMode != MODE_7_1_TOP_FRONT) ? 4 : 3;
548 
549       /* (7.1) sce + cpe + cpe + cpe + lfe */
550       FIXP_DBL sceRate = hQC->elementBits[0]->relativeBitsEl =
551           cm->elInfo[0].relativeBits;
552       FIXP_DBL cpe1Rate = hQC->elementBits[1]->relativeBitsEl =
553           cm->elInfo[1].relativeBits;
554       FIXP_DBL cpe2Rate = hQC->elementBits[2]->relativeBitsEl =
555           cm->elInfo[2].relativeBits;
556       FIXP_DBL cpe3Rate = hQC->elementBits[cpe3Idx]->relativeBitsEl =
557           cm->elInfo[cpe3Idx].relativeBits;
558       FIXP_DBL lfeRate = hQC->elementBits[lfeIdx]->relativeBitsEl =
559           cm->elInfo[lfeIdx].relativeBits;
560 
561       int maxBitsTot =
562           maxChannelBits * 7; /* LFE does not add to bit reservoir */
563       int sc = CountLeadingBits(fixMax(maxChannelBits, averageBitsTot));
564       int maxLfeBits = (int)fMax(
565           (INT)((fMult(lfeRate, (FIXP_DBL)(maxChannelBits << sc)) >> sc) << 1),
566           (INT)((fMult(FL2FXCONST_DBL(1.1f / 2.f),
567                        fMult(lfeRate, (FIXP_DBL)(averageBitsTot << sc)))
568                  << 1) >>
569                 sc));
570 
571       maxChannelBits = (maxBitsTot - maxLfeBits) / 7;
572 
573       hQC->elementBits[0]->chBitrateEl =
574           fMult(sceRate, (FIXP_DBL)(bitrateTot << sc_brTot)) >> sc_brTot;
575       hQC->elementBits[1]->chBitrateEl =
576           fMult(cpe1Rate, (FIXP_DBL)(bitrateTot << sc_brTot)) >> (sc_brTot + 1);
577       hQC->elementBits[2]->chBitrateEl =
578           fMult(cpe2Rate, (FIXP_DBL)(bitrateTot << sc_brTot)) >> (sc_brTot + 1);
579       hQC->elementBits[cpe3Idx]->chBitrateEl =
580           fMult(cpe3Rate, (FIXP_DBL)(bitrateTot << sc_brTot)) >> (sc_brTot + 1);
581       hQC->elementBits[lfeIdx]->chBitrateEl =
582           fMult(lfeRate, (FIXP_DBL)(bitrateTot << sc_brTot)) >> sc_brTot;
583 
584       hQC->elementBits[0]->maxBitsEl = maxChannelBits;
585       hQC->elementBits[1]->maxBitsEl = 2 * maxChannelBits;
586       hQC->elementBits[2]->maxBitsEl = 2 * maxChannelBits;
587       hQC->elementBits[cpe3Idx]->maxBitsEl = 2 * maxChannelBits;
588       hQC->elementBits[lfeIdx]->maxBitsEl = maxLfeBits;
589       break;
590     }
591 
592     default:
593       return AAC_ENC_UNSUPPORTED_CHANNELCONFIG;
594   }
595 
596   return AAC_ENC_OK;
597 }
598 
599 /********************************************************************************/
600 /*                                                                              */
601 /* function:    GetMonoStereoMODE(const CHANNEL_MODE mode) */
602 /*                                                                              */
603 /* description: Determines encoder setting from channel mode. */
604 /*              Multichannel modes are mapped to mono or stereo modes */
605 /*              returns MODE_MONO in case of mono,                           */
606 /*                      MODE_STEREO in case of stereo                        */
607 /*                      MODE_INVALID in case of error                        */
608 /*                                                                              */
609 /* input:       CHANNEL_MODE mode: Encoder mode (see qc_data.h). */
610 /* output:      return: CM_STEREO_MODE monoStereoSetting */
611 /*              (MODE_INVALID: error,                                        */
612 /*               MODE_MONO:    mono                                          */
613 /*               MODE_STEREO:  stereo).                                      */
614 /*                                                                              */
615 /* misc:        No memory is allocated. */
616 /*                                                                              */
617 /********************************************************************************/
618 
FDKaacEnc_GetMonoStereoMode(const CHANNEL_MODE mode)619 ELEMENT_MODE FDKaacEnc_GetMonoStereoMode(const CHANNEL_MODE mode) {
620   ELEMENT_MODE monoStereoSetting = EL_MODE_INVALID;
621 
622   switch (mode) {
623     case MODE_1: /* mono setups */
624       monoStereoSetting = EL_MODE_MONO;
625       break;
626 
627     case MODE_2: /* stereo setups */
628     case MODE_1_2:
629     case MODE_1_2_1:
630     case MODE_1_2_2:
631     case MODE_1_2_2_1:
632     case MODE_6_1:
633     case MODE_1_2_2_2_1:
634     case MODE_7_1_REAR_SURROUND:
635     case MODE_7_1_FRONT_CENTER:
636     case MODE_7_1_BACK:
637     case MODE_7_1_TOP_FRONT:
638       monoStereoSetting = EL_MODE_STEREO;
639       break;
640 
641     default: /* error */
642       monoStereoSetting = EL_MODE_INVALID;
643       break;
644   }
645 
646   return monoStereoSetting;
647 }
648 
FDKaacEnc_GetChannelModeConfiguration(const CHANNEL_MODE mode)649 const CHANNEL_MODE_CONFIG_TAB* FDKaacEnc_GetChannelModeConfiguration(
650     const CHANNEL_MODE mode) {
651   INT i;
652   const CHANNEL_MODE_CONFIG_TAB* cm_config = NULL;
653 
654   /* get channel mode config */
655   for (i = 0; i < (INT)sizeof(channelModeConfig) /
656                       (INT)sizeof(CHANNEL_MODE_CONFIG_TAB);
657        i++) {
658     if (channelModeConfig[i].encMode == mode) {
659       cm_config = &channelModeConfig[i];
660       break;
661     }
662   }
663   return cm_config;
664 }
665