1
2/* -----------------------------------------------------------------------------------------------------------
3Software License for The Fraunhofer FDK AAC Codec Library for Android
4
5� Copyright  1995 - 2013 Fraunhofer-Gesellschaft zur F�rderung der angewandten Forschung e.V.
6  All rights reserved.
7
8 1.    INTRODUCTION
9The Fraunhofer FDK AAC Codec Library for Android ("FDK AAC Codec") is software that implements
10the MPEG Advanced Audio Coding ("AAC") encoding and decoding scheme for digital audio.
11This FDK AAC Codec software is intended to be used on a wide variety of Android devices.
12
13AAC's HE-AAC and HE-AAC v2 versions are regarded as today's most efficient general perceptual
14audio codecs. AAC-ELD is considered the best-performing full-bandwidth communications codec by
15independent studies and is widely deployed. AAC has been standardized by ISO and IEC as part
16of the MPEG specifications.
17
18Patent licenses for necessary patent claims for the FDK AAC Codec (including those of Fraunhofer)
19may be obtained through Via Licensing (www.vialicensing.com) or through the respective patent owners
20individually for the purpose of encoding or decoding bit streams in products that are compliant with
21the ISO/IEC MPEG audio standards. Please note that most manufacturers of Android devices already license
22these patent claims through Via Licensing or directly from the patent owners, and therefore FDK AAC Codec
23software may already be covered under those patent licenses when it is used for those licensed purposes only.
24
25Commercially-licensed AAC software libraries, including floating-point versions with enhanced sound quality,
26are also available from Fraunhofer. Users are encouraged to check the Fraunhofer website for additional
27applications information and documentation.
28
292.    COPYRIGHT LICENSE
30
31Redistribution and use in source and binary forms, with or without modification, are permitted without
32payment of copyright license fees provided that you satisfy the following conditions:
33
34You must retain the complete text of this software license in redistributions of the FDK AAC Codec or
35your modifications thereto in source code form.
36
37You must retain the complete text of this software license in the documentation and/or other materials
38provided with redistributions of the FDK AAC Codec or your modifications thereto in binary form.
39You must make available free of charge copies of the complete source code of the FDK AAC Codec and your
40modifications thereto to recipients of copies in binary form.
41
42The name of Fraunhofer may not be used to endorse or promote products derived from this library without
43prior written permission.
44
45You may not charge copyright license fees for anyone to use, copy or distribute the FDK AAC Codec
46software or your modifications thereto.
47
48Your modified versions of the FDK AAC Codec must carry prominent notices stating that you changed the software
49and the date of any change. For modified versions of the FDK AAC Codec, the term
50"Fraunhofer FDK AAC Codec Library for Android" must be replaced by the term
51"Third-Party Modified Version of the Fraunhofer FDK AAC Codec Library for Android."
52
533.    NO PATENT LICENSE
54
55NO EXPRESS OR IMPLIED LICENSES TO ANY PATENT CLAIMS, including without limitation the patents of Fraunhofer,
56ARE GRANTED BY THIS SOFTWARE LICENSE. Fraunhofer provides no warranty of patent non-infringement with
57respect to this software.
58
59You may use this FDK AAC Codec software or modifications thereto only for purposes that are authorized
60by appropriate patent licenses.
61
624.    DISCLAIMER
63
64This 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
66of merchantability and fitness for a particular purpose. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR
67CONTRIBUTORS BE LIABLE for any direct, indirect, incidental, special, exemplary, or consequential damages,
68including but not limited to procurement of substitute goods or services; loss of use, data, or profits,
69or business interruption, however caused and on any theory of liability, whether in contract, strict
70liability, or tort (including negligence), arising in any way out of the use of this software, even if
71advised of the possibility of such damage.
72
735.    CONTACT INFORMATION
74
75Fraunhofer Institute for Integrated Circuits IIS
76Attention: Audio and Multimedia Departments - FDK AAC LL
77Am Wolfsmantel 33
7891058 Erlangen, Germany
79
80www.iis.fraunhofer.de/amm
81amm-info@iis.fraunhofer.de
82----------------------------------------------------------------------------------------------------------- */
83
84/*************************  Fast MPEG AAC Audio Encoder  **********************
85
86   Initial author:       A. Groeschel
87   contents/description: channel mapping functionality
88
89******************************************************************************/
90
91#include "channel_map.h"
92#include "bitenc.h"
93#include "psy_const.h"
94#include "qc_data.h"
95#include "aacEnc_ram.h"
96
97
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.
103
104   In mono and stereo case, this is trivial.
105   In mc case, it looks like this:
106
107   Channel         Input file       coder chan
1085ch:
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)
114
1155.1ch:
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)
121   LFE             3                5 (LFE)
122*/
123
124typedef struct {
125
126  CHANNEL_MODE  encoderMode;
127  INT           channel_assignment[/*(8)*/12];
128
129} CHANNEL_ASSIGNMENT_INFO_TAB;
130
131
132static const CHANNEL_ASSIGNMENT_INFO_TAB assignmentInfoTabMpeg[] =
133{
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     */
144};
145
146static const CHANNEL_ASSIGNMENT_INFO_TAB assignmentInfoTabWav[] =
147{
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     */
158};
159
160/* Channel mode configuration tab provides,
161   corresponding number of channels and elements
162*/
163static const CHANNEL_MODE_CONFIG_TAB channelModeConfig[] =
164{
165   { MODE_1,                        1,  1,  1 },   /* SCE                 */
166   { MODE_2,                        2,  2,  1 },   /* CPE                 */
167   { MODE_1_2,                      3,  3,  2 },   /* SCE,CPE             */
168   { MODE_1_2_1,                    4,  4,  3 },   /* SCE,CPE,SCE         */
169   { MODE_1_2_2,                    5,  5,  3 },   /* SCE,CPE,CPE         */
170   { MODE_1_2_2_1,                  6,  5,  4 },   /* SCE,CPE,CPE,LFE     */
171   { MODE_1_2_2_2_1,                8,  7,  5 },   /* SCE,CPE,CPE,CPE,LFE */
172   { MODE_7_1_REAR_SURROUND,        8,  7,  5 },
173   { MODE_7_1_FRONT_CENTER,         8,  7,  5 },
174};
175
176#define MAX_MODES (sizeof(assignmentInfoTabWav)/sizeof(CHANNEL_ASSIGNMENT_INFO_TAB))
177
178const INT* FDKaacEnc_getChannelAssignment(CHANNEL_MODE encMode, CHANNEL_ORDER co)
179{
180  const CHANNEL_ASSIGNMENT_INFO_TAB *pTab;
181  int i;
182
183  if (co == CH_ORDER_MPEG)
184    pTab = assignmentInfoTabMpeg;
185  else
186    pTab = assignmentInfoTabWav;
187
188  for(i=MAX_MODES-1; i>0; i--) {
189    if (encMode== pTab[i].encoderMode) {
190      break;
191    }
192  }
193  return (pTab[i].channel_assignment);
194}
195
196AAC_ENCODER_ERROR FDKaacEnc_DetermineEncoderMode(CHANNEL_MODE* mode, INT nChannels)
197{
198  INT i;
199  CHANNEL_MODE encMode = MODE_INVALID;
200
201  if (*mode==MODE_UNKNOWN) {
202    for (i=0; i<(INT)sizeof(channelModeConfig)/(INT)sizeof(CHANNEL_MODE_CONFIG_TAB); i++) {
203      if (channelModeConfig[i].nChannels==nChannels) {
204          encMode = channelModeConfig[i].encMode;
205          break;
206      }
207    }
208    *mode = encMode;
209  }
210  else {
211    /* check if valid channel configuration */
212    if (FDKaacEnc_GetChannelModeConfiguration(*mode)->nChannels==nChannels) {
213        encMode = *mode;
214    }
215  }
216
217  if (encMode==MODE_INVALID) {
218    return AAC_ENC_UNSUPPORTED_CHANNELCONFIG;
219  }
220
221  return AAC_ENC_OK;
222}
223
224static 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) {
225
226  INT error=0;
227  INT counter =*cnt;
228
229  const INT *assign = FDKaacEnc_getChannelAssignment(mode, co);
230
231  elInfo->elType=elType;
232  elInfo->relativeBits = relBits;
233
234  switch(elInfo->elType) {
235  case ID_SCE: case ID_LFE: case ID_CCE:
236    elInfo->nChannelsInEl=1;
237    elInfo->ChannelIndex[0]=assign[counter++];
238    elInfo->instanceTag=it_cnt[elType]++;
239
240    break;
241  case ID_CPE:
242    elInfo->nChannelsInEl=2;
243    elInfo->ChannelIndex[0]=assign[counter++];
244    elInfo->ChannelIndex[1]=assign[counter++];
245    elInfo->instanceTag=it_cnt[elType]++;
246    break;
247  case ID_DSE:
248    elInfo->nChannelsInEl=0;
249    elInfo->ChannelIndex[0]=0;
250    elInfo->ChannelIndex[1]=0;
251    elInfo->instanceTag=it_cnt[elType]++;
252    break;
253  default: error=1;
254  };
255  *cnt = counter;
256  return error;
257
258}
259
260AAC_ENCODER_ERROR FDKaacEnc_InitChannelMapping(CHANNEL_MODE mode, CHANNEL_ORDER co, CHANNEL_MAPPING* cm)
261{
262  INT count=0; /* count through coder channels */
263  INT it_cnt[ID_END+1];
264  INT i;
265
266  for (i=0; i<ID_END; i++)
267    it_cnt[i]=0;
268
269  FDKmemclear(cm, sizeof(CHANNEL_MAPPING));
270
271  /* init channel mapping*/
272  for (i=0; i<(INT)sizeof(channelModeConfig)/(INT)sizeof(CHANNEL_MODE_CONFIG_TAB); i++) {
273    if (channelModeConfig[i].encMode==mode)
274    {
275        cm->encMode      = channelModeConfig[i].encMode;
276        cm->nChannels    = channelModeConfig[i].nChannels;
277        cm->nChannelsEff = channelModeConfig[i].nChannelsEff;
278        cm->nElements    = channelModeConfig[i].nElements;
279
280        break;
281    }
282  }
283
284  /* init element info struct */
285  switch(mode) {
286    case MODE_1:
287      /* (mono) sce  */
288      FDKaacEnc_initElement(&cm->elInfo[0], ID_SCE, &count, mode, co, it_cnt, (FIXP_DBL)MAXVAL_DBL);
289      break;
290    case MODE_2:
291      /* (stereo) cpe */
292      FDKaacEnc_initElement(&cm->elInfo[0], ID_CPE, &count, mode, co, it_cnt, (FIXP_DBL)MAXVAL_DBL);
293      break;
294
295    case MODE_1_2:
296      /* sce + cpe */
297      FDKaacEnc_initElement(&cm->elInfo[0], ID_SCE, &count, mode, co, it_cnt, FL2FXCONST_DBL(0.4f));
298      FDKaacEnc_initElement(&cm->elInfo[1], ID_CPE, &count, mode, co, it_cnt, FL2FXCONST_DBL(0.6f));
299      break;
300
301    case MODE_1_2_1:
302      /* sce + cpe + sce */
303      FDKaacEnc_initElement(&cm->elInfo[0], ID_SCE, &count, mode, co, it_cnt, FL2FXCONST_DBL(0.3f));
304      FDKaacEnc_initElement(&cm->elInfo[1], ID_CPE, &count, mode, co, it_cnt, FL2FXCONST_DBL(0.4f));
305      FDKaacEnc_initElement(&cm->elInfo[2], ID_SCE, &count, mode, co, it_cnt, FL2FXCONST_DBL(0.3f));
306      break;
307
308    case MODE_1_2_2:
309      /* sce + cpe + cpe */
310      FDKaacEnc_initElement(&cm->elInfo[0], ID_SCE, &count, mode, co, it_cnt, FL2FXCONST_DBL(0.26f));
311      FDKaacEnc_initElement(&cm->elInfo[1], ID_CPE, &count, mode, co, it_cnt, FL2FXCONST_DBL(0.37f));
312      FDKaacEnc_initElement(&cm->elInfo[2], ID_CPE, &count, mode, co, it_cnt, FL2FXCONST_DBL(0.37f));
313      break;
314
315    case MODE_1_2_2_1:
316      /* (5.1) sce + cpe + cpe + lfe */
317      FDKaacEnc_initElement(&cm->elInfo[0], ID_SCE, &count, mode, co, it_cnt, FL2FXCONST_DBL(0.24f));
318      FDKaacEnc_initElement(&cm->elInfo[1], ID_CPE, &count, mode, co, it_cnt, FL2FXCONST_DBL(0.35f));
319      FDKaacEnc_initElement(&cm->elInfo[2], ID_CPE, &count, mode, co, it_cnt, FL2FXCONST_DBL(0.35f));
320      FDKaacEnc_initElement(&cm->elInfo[3], ID_LFE, &count, mode, co, it_cnt, FL2FXCONST_DBL(0.06f));
321      break;
322
323    case MODE_1_2_2_2_1:
324    case MODE_7_1_REAR_SURROUND:
325    case MODE_7_1_FRONT_CENTER:
326      /* (7.1) sce + cpe + cpe + cpe + lfe */
327      FDKaacEnc_initElement(&cm->elInfo[0], ID_SCE, &count, mode, co, it_cnt, FL2FXCONST_DBL(0.18f));
328      FDKaacEnc_initElement(&cm->elInfo[1], ID_CPE, &count, mode, co, it_cnt, FL2FXCONST_DBL(0.26f));
329      FDKaacEnc_initElement(&cm->elInfo[2], ID_CPE, &count, mode, co, it_cnt, FL2FXCONST_DBL(0.26f));
330      FDKaacEnc_initElement(&cm->elInfo[3], ID_CPE, &count, mode, co, it_cnt, FL2FXCONST_DBL(0.26f));
331      FDKaacEnc_initElement(&cm->elInfo[4], ID_LFE, &count, mode, co, it_cnt, FL2FXCONST_DBL(0.04f));
332      break;
333    default:
334      //*chMap=0;
335      return AAC_ENC_UNSUPPORTED_CHANNELCONFIG;
336  };
337
338
339  FDK_ASSERT(cm->nElements<=(8));
340
341
342  return AAC_ENC_OK;
343}
344
345AAC_ENCODER_ERROR FDKaacEnc_InitElementBits(QC_STATE *hQC,
346                    CHANNEL_MAPPING *cm,
347                    INT bitrateTot,
348                    INT averageBitsTot,
349                    INT maxChannelBits)
350{
351  int sc_brTot = CountLeadingBits(bitrateTot);
352
353  switch(cm->encMode) {
354  case MODE_1:
355    hQC->elementBits[0]->chBitrateEl      = bitrateTot;
356
357    hQC->elementBits[0]->maxBitsEl        = maxChannelBits;
358
359    hQC->elementBits[0]->relativeBitsEl   = cm->elInfo[0].relativeBits;
360    break;
361
362  case MODE_2:
363    hQC->elementBits[0]->chBitrateEl      = bitrateTot>>1;
364
365    hQC->elementBits[0]->maxBitsEl        = 2*maxChannelBits;
366
367    hQC->elementBits[0]->relativeBitsEl   = cm->elInfo[0].relativeBits;
368    break;
369  case MODE_1_2: {
370    hQC->elementBits[0]->relativeBitsEl = cm->elInfo[0].relativeBits;
371    hQC->elementBits[1]->relativeBitsEl = cm->elInfo[1].relativeBits;
372    FIXP_DBL sceRate = cm->elInfo[0].relativeBits;
373    FIXP_DBL cpeRate = cm->elInfo[1].relativeBits;
374
375    hQC->elementBits[0]->chBitrateEl      = fMult(sceRate, (FIXP_DBL)(bitrateTot<<sc_brTot))>>sc_brTot;
376    hQC->elementBits[1]->chBitrateEl      = fMult(cpeRate, (FIXP_DBL)(bitrateTot<<sc_brTot))>>(sc_brTot+1);
377
378    hQC->elementBits[0]->maxBitsEl        = maxChannelBits;
379    hQC->elementBits[1]->maxBitsEl        = 2*maxChannelBits;
380    break;
381  }
382  case MODE_1_2_1: {
383    /* sce + cpe + sce */
384    hQC->elementBits[0]->relativeBitsEl = cm->elInfo[0].relativeBits;
385    hQC->elementBits[1]->relativeBitsEl = cm->elInfo[1].relativeBits;
386    hQC->elementBits[2]->relativeBitsEl = cm->elInfo[2].relativeBits;
387    FIXP_DBL sce1Rate = cm->elInfo[0].relativeBits;
388    FIXP_DBL cpeRate  = cm->elInfo[1].relativeBits;
389    FIXP_DBL sce2Rate = cm->elInfo[2].relativeBits;
390
391    hQC->elementBits[0]->chBitrateEl      = fMult(sce1Rate, (FIXP_DBL)(bitrateTot<<sc_brTot))>>sc_brTot;
392    hQC->elementBits[1]->chBitrateEl      = fMult(cpeRate,  (FIXP_DBL)(bitrateTot<<sc_brTot))>>(sc_brTot+1);
393    hQC->elementBits[2]->chBitrateEl      = fMult(sce2Rate, (FIXP_DBL)(bitrateTot<<sc_brTot))>>sc_brTot;
394
395    hQC->elementBits[0]->maxBitsEl        = maxChannelBits;
396    hQC->elementBits[1]->maxBitsEl        = 2*maxChannelBits;
397    hQC->elementBits[2]->maxBitsEl        = maxChannelBits;
398    break;
399  }
400  case MODE_1_2_2: {
401    /* sce + cpe + cpe */
402    hQC->elementBits[0]->relativeBitsEl = cm->elInfo[0].relativeBits;
403    hQC->elementBits[1]->relativeBitsEl = cm->elInfo[1].relativeBits;
404    hQC->elementBits[2]->relativeBitsEl = cm->elInfo[2].relativeBits;
405    FIXP_DBL sceRate  = cm->elInfo[0].relativeBits;
406    FIXP_DBL cpe1Rate = cm->elInfo[1].relativeBits;
407    FIXP_DBL cpe2Rate = cm->elInfo[2].relativeBits;
408
409    hQC->elementBits[0]->chBitrateEl      = fMult(sceRate,  (FIXP_DBL)(bitrateTot<<sc_brTot))>>sc_brTot;
410    hQC->elementBits[1]->chBitrateEl      = fMult(cpe1Rate, (FIXP_DBL)(bitrateTot<<sc_brTot))>>(sc_brTot+1);
411    hQC->elementBits[2]->chBitrateEl      = fMult(cpe2Rate, (FIXP_DBL)(bitrateTot<<sc_brTot))>>(sc_brTot+1);
412
413    hQC->elementBits[0]->maxBitsEl        = maxChannelBits;
414    hQC->elementBits[1]->maxBitsEl        = 2*maxChannelBits;
415    hQC->elementBits[2]->maxBitsEl        = 2*maxChannelBits;
416    break;
417  }
418
419  case MODE_1_2_2_1: {
420    /* (5.1) sce + cpe + cpe + lfe */
421    hQC->elementBits[0]->relativeBitsEl = cm->elInfo[0].relativeBits;
422    hQC->elementBits[1]->relativeBitsEl = cm->elInfo[1].relativeBits;
423    hQC->elementBits[2]->relativeBitsEl = cm->elInfo[2].relativeBits;
424    hQC->elementBits[3]->relativeBitsEl = cm->elInfo[3].relativeBits;
425    FIXP_DBL sceRate  = cm->elInfo[0].relativeBits;
426    FIXP_DBL cpe1Rate = cm->elInfo[1].relativeBits;
427    FIXP_DBL cpe2Rate = cm->elInfo[2].relativeBits;
428    FIXP_DBL lfeRate  = cm->elInfo[3].relativeBits;
429
430    int maxBitsTot = maxChannelBits * 5; /* LFE does not add to bit reservoir */
431    int sc = CountLeadingBits(fixMax(maxChannelBits,averageBitsTot));
432    int maxLfeBits = (int) FDKmax ( (INT)((fMult(lfeRate,(FIXP_DBL)(maxChannelBits<<sc))>>sc)<<1),
433                                    (INT)((fMult(FL2FXCONST_DBL(1.1f/2.f),fMult(lfeRate,(FIXP_DBL)(averageBitsTot<<sc)))<<1)>>sc) );
434
435    maxChannelBits = (maxBitsTot - maxLfeBits);
436    sc = CountLeadingBits(maxChannelBits);
437
438    maxChannelBits = fMult((FIXP_DBL)maxChannelBits<<sc,GetInvInt(5))>>sc;
439
440    hQC->elementBits[0]->chBitrateEl      = fMult(sceRate,  (FIXP_DBL)(bitrateTot<<sc_brTot))>>sc_brTot;
441    hQC->elementBits[1]->chBitrateEl      = fMult(cpe1Rate, (FIXP_DBL)(bitrateTot<<sc_brTot))>>(sc_brTot+1);
442    hQC->elementBits[2]->chBitrateEl      = fMult(cpe2Rate, (FIXP_DBL)(bitrateTot<<sc_brTot))>>(sc_brTot+1);
443    hQC->elementBits[3]->chBitrateEl      = fMult(lfeRate,  (FIXP_DBL)(bitrateTot<<sc_brTot))>>sc_brTot;
444
445    hQC->elementBits[0]->maxBitsEl        = maxChannelBits;
446    hQC->elementBits[1]->maxBitsEl        = 2*maxChannelBits;
447    hQC->elementBits[2]->maxBitsEl        = 2*maxChannelBits;
448    hQC->elementBits[3]->maxBitsEl        = maxLfeBits;
449
450    break;
451  }
452  case MODE_7_1_REAR_SURROUND:
453  case MODE_7_1_FRONT_CENTER:
454  case MODE_1_2_2_2_1: {
455    int cpe3Idx = 3;
456    int lfeIdx  = 4;
457
458    /* (7.1) sce + cpe + cpe + cpe + lfe */
459    FIXP_DBL sceRate  = hQC->elementBits[0]->relativeBitsEl = cm->elInfo[0].relativeBits;
460    FIXP_DBL cpe1Rate = hQC->elementBits[1]->relativeBitsEl = cm->elInfo[1].relativeBits;
461    FIXP_DBL cpe2Rate = hQC->elementBits[2]->relativeBitsEl = cm->elInfo[2].relativeBits;
462    FIXP_DBL cpe3Rate = hQC->elementBits[cpe3Idx]->relativeBitsEl = cm->elInfo[cpe3Idx].relativeBits;
463    FIXP_DBL lfeRate  = hQC->elementBits[lfeIdx]->relativeBitsEl = cm->elInfo[lfeIdx].relativeBits;
464
465    int maxBitsTot = maxChannelBits * 7; /* LFE does not add to bit reservoir */
466    int sc = CountLeadingBits(fixMax(maxChannelBits,averageBitsTot));
467    int maxLfeBits = (int) FDKmax ( (INT)((fMult(lfeRate,(FIXP_DBL)(maxChannelBits<<sc))>>sc)<<1),
468                                    (INT)((fMult(FL2FXCONST_DBL(1.1f/2.f),fMult(lfeRate,(FIXP_DBL)(averageBitsTot<<sc)))<<1)>>sc) );
469
470    maxChannelBits = (maxBitsTot - maxLfeBits) / 7;
471
472    hQC->elementBits[0]->chBitrateEl = fMult(sceRate,  (FIXP_DBL)(bitrateTot<<sc_brTot))>>sc_brTot;
473    hQC->elementBits[1]->chBitrateEl = fMult(cpe1Rate, (FIXP_DBL)(bitrateTot<<sc_brTot))>>(sc_brTot+1);
474    hQC->elementBits[2]->chBitrateEl = fMult(cpe2Rate, (FIXP_DBL)(bitrateTot<<sc_brTot))>>(sc_brTot+1);
475    hQC->elementBits[cpe3Idx]->chBitrateEl = fMult(cpe3Rate, (FIXP_DBL)(bitrateTot<<sc_brTot))>>(sc_brTot+1);
476    hQC->elementBits[lfeIdx]->chBitrateEl = fMult(lfeRate,  (FIXP_DBL)(bitrateTot<<sc_brTot))>>sc_brTot;
477
478    hQC->elementBits[0]->maxBitsEl = maxChannelBits;
479    hQC->elementBits[1]->maxBitsEl = 2*maxChannelBits;
480    hQC->elementBits[2]->maxBitsEl = 2*maxChannelBits;
481    hQC->elementBits[cpe3Idx]->maxBitsEl = 2*maxChannelBits;
482    hQC->elementBits[lfeIdx]->maxBitsEl = maxLfeBits;
483    break;
484  }
485  default:
486    return AAC_ENC_UNSUPPORTED_CHANNELCONFIG;
487  }
488
489  return AAC_ENC_OK;
490}
491
492/********************************************************************************/
493/*                                                                              */
494/* function:    GetMonoStereoMODE(const CHANNEL_MODE mode)                      */
495/*                                                                              */
496/* description: Determines encoder setting from channel mode.                   */
497/*              Multichannel modes are mapped to mono or stereo modes           */
498/*              returns MODE_MONO in case of mono,                           */
499/*                      MODE_STEREO in case of stereo                        */
500/*                      MODE_INVALID in case of error                        */
501/*                                                                              */
502/* input:       CHANNEL_MODE mode: Encoder mode (see qc_data.h).                */
503/* output:      return: CM_STEREO_MODE monoStereoSetting                        */
504/*              (MODE_INVALID: error,                                        */
505/*               MODE_MONO:    mono                                          */
506/*               MODE_STEREO:  stereo).                                      */
507/*                                                                              */
508/* misc:        No memory is allocated.                                         */
509/*                                                                              */
510/********************************************************************************/
511
512ELEMENT_MODE FDKaacEnc_GetMonoStereoMode(const CHANNEL_MODE mode){
513
514  ELEMENT_MODE monoStereoSetting = EL_MODE_INVALID;
515
516  switch(mode){
517  case MODE_1:        /* mono setups */
518    monoStereoSetting = EL_MODE_MONO;
519    break;
520  case MODE_2:        /* stereo setups */
521  case MODE_1_2:
522  case MODE_1_2_1:
523  case MODE_1_2_2:
524  case MODE_1_2_2_1:
525  case MODE_1_2_2_2_1:
526  case MODE_7_1_REAR_SURROUND:
527  case MODE_7_1_FRONT_CENTER:
528    monoStereoSetting = EL_MODE_STEREO;
529    break;
530  default:            /* error */
531    monoStereoSetting = EL_MODE_INVALID;
532    break;
533  }
534
535  return monoStereoSetting;
536}
537
538const CHANNEL_MODE_CONFIG_TAB* FDKaacEnc_GetChannelModeConfiguration(const CHANNEL_MODE mode)
539{
540  INT i;
541  const CHANNEL_MODE_CONFIG_TAB *cm_config = NULL;
542
543  /* get channel mode config */
544  for (i=0; i<(INT)sizeof(channelModeConfig)/(INT)sizeof(CHANNEL_MODE_CONFIG_TAB); i++) {
545    if (channelModeConfig[i].encMode==mode)
546    {
547        cm_config = &channelModeConfig[i];
548        break;
549    }
550  }
551  return cm_config;
552}
553