1
2/* -----------------------------------------------------------------------------------------------------------
3Software License for The Fraunhofer FDK AAC Codec Library for Android
4
5© Copyright  1995 - 2012 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[/*(6)*/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};
143
144static const CHANNEL_ASSIGNMENT_INFO_TAB assignmentInfoTabWav[] =
145{
146     { MODE_INVALID,                 {-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1} },  /*  invalid   */
147     { MODE_1,                       { 0,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1} },  /*  mono      */
148     { MODE_2,                       { 0, 1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1} },  /*  stereo    */
149     { MODE_1_2,                     { 2, 0, 1,-1,-1,-1,-1,-1,-1,-1,-1,-1} },  /*  3ch       */
150     { MODE_1_2_1,                   { 2, 0, 1, 3,-1,-1,-1,-1,-1,-1,-1,-1} },  /*  4ch       */
151     { MODE_1_2_2,                   { 2, 0, 1, 3, 4,-1,-1,-1,-1,-1,-1,-1} },  /*  5ch       */
152     { MODE_1_2_2_1,                 { 2, 0, 1, 4, 5, 3,-1,-1,-1,-1,-1,-1} },  /*  5.1ch     */
153     { MODE_1_2_2_2_1,               { 2, 0, 1, 6, 7, 4, 5, 3,-1,-1,-1,-1} },  /*  7.1ch     */
154};
155
156/* Channel mode configuration tab provides,
157   corresponding number of channels and elements
158*/
159static const CHANNEL_MODE_CONFIG_TAB channelModeConfig[] =
160{
161   { MODE_1,                        1,  1,  1 },   /* SCE                 */
162   { MODE_2,                        2,  2,  1 },   /* CPE                 */
163   { MODE_1_2,                      3,  3,  2 },   /* SCE,CPE             */
164   { MODE_1_2_1,                    4,  4,  3 },   /* SCE,CPE,SCE         */
165   { MODE_1_2_2,                    5,  5,  3 },   /* SCE,CPE,CPE         */
166   { MODE_1_2_2_1,                  6,  5,  4 },   /* SCE,CPE,CPE,LFE     */
167   { MODE_1_2_2_2_1,                8,  7,  5 },   /* SCE,CPE,CPE,CPE,LFE */
168};
169
170#define MAX_MODES (sizeof(assignmentInfoTabWav)/sizeof(CHANNEL_ASSIGNMENT_INFO_TAB))
171
172const INT* FDKaacEnc_getChannelAssignment(CHANNEL_MODE encMode, CHANNEL_ORDER co)
173{
174  const CHANNEL_ASSIGNMENT_INFO_TAB *pTab;
175  int i;
176
177  if (co == CH_ORDER_MPEG)
178    pTab = assignmentInfoTabMpeg;
179  else
180    pTab = assignmentInfoTabWav;
181
182  for(i=MAX_MODES-1; i>0; i--) {
183    if (encMode== pTab[i].encoderMode) {
184      break;
185    }
186  }
187  return (pTab[i].channel_assignment);
188}
189
190AAC_ENCODER_ERROR FDKaacEnc_DetermineEncoderMode(CHANNEL_MODE* mode, INT nChannels)
191{
192  INT i;
193  CHANNEL_MODE encMode = MODE_INVALID;
194
195  if (*mode==MODE_UNKNOWN) {
196    for (i=0; i<(INT)sizeof(channelModeConfig)/(INT)sizeof(CHANNEL_MODE_CONFIG_TAB); i++) {
197      if (channelModeConfig[i].nChannels==nChannels) {
198          encMode = channelModeConfig[i].encMode;
199          break;
200      }
201    }
202    *mode = encMode;
203  }
204  else {
205    /* check if valid channel configuration */
206    if (FDKaacEnc_GetChannelModeConfiguration(*mode)->nChannels==nChannels) {
207        encMode = *mode;
208    }
209  }
210
211  if (encMode==MODE_INVALID) {
212    return AAC_ENC_UNSUPPORTED_CHANNELCONFIG;
213  }
214
215  return AAC_ENC_OK;
216}
217
218static 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) {
219
220  INT error=0;
221  INT counter =*cnt;
222
223  const INT *assign = FDKaacEnc_getChannelAssignment(mode, co);
224
225  elInfo->elType=elType;
226  elInfo->relativeBits = relBits;
227
228  switch(elInfo->elType) {
229  case ID_SCE: case ID_LFE: case ID_CCE:
230    elInfo->nChannelsInEl=1;
231    elInfo->ChannelIndex[0]=assign[counter++];
232    elInfo->instanceTag=it_cnt[elType]++;
233
234    break;
235  case ID_CPE:
236    elInfo->nChannelsInEl=2;
237    elInfo->ChannelIndex[0]=assign[counter++];
238    elInfo->ChannelIndex[1]=assign[counter++];
239    elInfo->instanceTag=it_cnt[elType]++;
240    break;
241  case ID_DSE:
242    elInfo->nChannelsInEl=0;
243    elInfo->ChannelIndex[0]=0;
244    elInfo->ChannelIndex[1]=0;
245    elInfo->instanceTag=it_cnt[elType]++;
246    break;
247  default: error=1;
248  };
249  *cnt = counter;
250  return error;
251
252}
253
254AAC_ENCODER_ERROR FDKaacEnc_InitChannelMapping(CHANNEL_MODE mode, CHANNEL_ORDER co, CHANNEL_MAPPING* cm)
255{
256  INT count=0; /* count through coder channels */
257  INT it_cnt[ID_END+1];
258  INT i;
259
260  for (i=0; i<ID_END; i++)
261    it_cnt[i]=0;
262
263  FDKmemclear(cm, sizeof(CHANNEL_MAPPING));
264
265  /* init channel mapping*/
266  for (i=0; i<(INT)sizeof(channelModeConfig)/(INT)sizeof(CHANNEL_MODE_CONFIG_TAB); i++) {
267    if (channelModeConfig[i].encMode==mode)
268    {
269        cm->encMode      = channelModeConfig[i].encMode;
270        cm->nChannels    = channelModeConfig[i].nChannels;
271        cm->nChannelsEff = channelModeConfig[i].nChannelsEff;
272        cm->nElements    = channelModeConfig[i].nElements;
273
274        break;
275    }
276  }
277
278  /* init element info struct */
279  switch(mode) {
280    case MODE_1:
281      /* (mono) sce  */
282      FDKaacEnc_initElement(&cm->elInfo[0], ID_SCE, &count, mode, co, it_cnt, (FIXP_DBL)MAXVAL_DBL);
283      break;
284    case MODE_2:
285      /* (stereo) cpe */
286      FDKaacEnc_initElement(&cm->elInfo[0], ID_CPE, &count, mode, co, it_cnt, (FIXP_DBL)MAXVAL_DBL);
287      break;
288
289    case MODE_1_2:
290      /* sce + cpe */
291      FDKaacEnc_initElement(&cm->elInfo[0], ID_SCE, &count, mode, co, it_cnt, FL2FXCONST_DBL(0.4f));
292      FDKaacEnc_initElement(&cm->elInfo[1], ID_CPE, &count, mode, co, it_cnt, FL2FXCONST_DBL(0.6f));
293      break;
294
295    case MODE_1_2_1:
296      /* sce + cpe + sce */
297      FDKaacEnc_initElement(&cm->elInfo[0], ID_SCE, &count, mode, co, it_cnt, FL2FXCONST_DBL(0.3f));
298      FDKaacEnc_initElement(&cm->elInfo[1], ID_CPE, &count, mode, co, it_cnt, FL2FXCONST_DBL(0.4f));
299      FDKaacEnc_initElement(&cm->elInfo[2], ID_SCE, &count, mode, co, it_cnt, FL2FXCONST_DBL(0.3f));
300      break;
301
302    case MODE_1_2_2:
303      /* sce + cpe + cpe */
304      FDKaacEnc_initElement(&cm->elInfo[0], ID_SCE, &count, mode, co, it_cnt, FL2FXCONST_DBL(0.26f));
305      FDKaacEnc_initElement(&cm->elInfo[1], ID_CPE, &count, mode, co, it_cnt, FL2FXCONST_DBL(0.37f));
306      FDKaacEnc_initElement(&cm->elInfo[2], ID_CPE, &count, mode, co, it_cnt, FL2FXCONST_DBL(0.37f));
307      break;
308
309    case MODE_1_2_2_1:
310      /* (5.1) sce + cpe + cpe + lfe */
311      FDKaacEnc_initElement(&cm->elInfo[0], ID_SCE, &count, mode, co, it_cnt, FL2FXCONST_DBL(0.24f));
312      FDKaacEnc_initElement(&cm->elInfo[1], ID_CPE, &count, mode, co, it_cnt, FL2FXCONST_DBL(0.35f));
313      FDKaacEnc_initElement(&cm->elInfo[2], ID_CPE, &count, mode, co, it_cnt, FL2FXCONST_DBL(0.35f));
314      FDKaacEnc_initElement(&cm->elInfo[3], ID_LFE, &count, mode, co, it_cnt, FL2FXCONST_DBL(0.06f));
315      break;
316
317    case MODE_1_2_2_2_1:
318      /* (7.1) sce + cpe + cpe + cpe + lfe */
319      FDKaacEnc_initElement(&cm->elInfo[0], ID_SCE, &count, mode, co, it_cnt, FL2FXCONST_DBL(0.18f));
320      FDKaacEnc_initElement(&cm->elInfo[1], ID_CPE, &count, mode, co, it_cnt, FL2FXCONST_DBL(0.26f));
321      FDKaacEnc_initElement(&cm->elInfo[2], ID_CPE, &count, mode, co, it_cnt, FL2FXCONST_DBL(0.26f));
322      FDKaacEnc_initElement(&cm->elInfo[3], ID_CPE, &count, mode, co, it_cnt, FL2FXCONST_DBL(0.26f));
323      FDKaacEnc_initElement(&cm->elInfo[4], ID_LFE, &count, mode, co, it_cnt, FL2FXCONST_DBL(0.04f));
324      break;
325
326    default:
327      //*chMap=0;
328      return AAC_ENC_UNSUPPORTED_CHANNELCONFIG;
329  };
330
331
332  FDK_ASSERT(cm->nElements<=(6));
333
334
335  return AAC_ENC_OK;
336}
337
338AAC_ENCODER_ERROR FDKaacEnc_InitElementBits(QC_STATE *hQC,
339                    CHANNEL_MAPPING *cm,
340                    INT bitrateTot,
341                    INT averageBitsTot,
342                    INT maxChannelBits)
343{
344  int sc_brTot = CountLeadingBits(bitrateTot);
345
346  switch(cm->encMode) {
347  case MODE_1:
348    hQC->elementBits[0]->chBitrateEl      = bitrateTot;
349
350    hQC->elementBits[0]->maxBitsEl        = maxChannelBits;
351
352    hQC->elementBits[0]->relativeBitsEl   = cm->elInfo[0].relativeBits;
353    break;
354
355  case MODE_2:
356    hQC->elementBits[0]->chBitrateEl      = bitrateTot>>1;
357
358    hQC->elementBits[0]->maxBitsEl        = 2*maxChannelBits;
359
360    hQC->elementBits[0]->relativeBitsEl   = cm->elInfo[0].relativeBits;
361    break;
362  case MODE_1_2: {
363    hQC->elementBits[0]->relativeBitsEl = cm->elInfo[0].relativeBits;
364    hQC->elementBits[1]->relativeBitsEl = cm->elInfo[1].relativeBits;
365    FIXP_DBL sceRate = cm->elInfo[0].relativeBits;
366    FIXP_DBL cpeRate = cm->elInfo[1].relativeBits;
367
368    hQC->elementBits[0]->chBitrateEl      = fMult(sceRate, (FIXP_DBL)(bitrateTot<<sc_brTot))>>sc_brTot;
369    hQC->elementBits[1]->chBitrateEl      = fMult(cpeRate, (FIXP_DBL)(bitrateTot<<sc_brTot))>>(sc_brTot+1);
370
371    hQC->elementBits[0]->maxBitsEl        = maxChannelBits;
372    hQC->elementBits[1]->maxBitsEl        = 2*maxChannelBits;
373    break;
374  }
375  case MODE_1_2_1: {
376    /* sce + cpe + sce */
377    hQC->elementBits[0]->relativeBitsEl = cm->elInfo[0].relativeBits;
378    hQC->elementBits[1]->relativeBitsEl = cm->elInfo[1].relativeBits;
379    hQC->elementBits[2]->relativeBitsEl = cm->elInfo[2].relativeBits;
380    FIXP_DBL sce1Rate = cm->elInfo[0].relativeBits;
381    FIXP_DBL cpeRate  = cm->elInfo[1].relativeBits;
382    FIXP_DBL sce2Rate = cm->elInfo[2].relativeBits;
383
384    hQC->elementBits[0]->chBitrateEl      = fMult(sce1Rate, (FIXP_DBL)(bitrateTot<<sc_brTot))>>sc_brTot;
385    hQC->elementBits[1]->chBitrateEl      = fMult(cpeRate,  (FIXP_DBL)(bitrateTot<<sc_brTot))>>(sc_brTot+1);
386    hQC->elementBits[2]->chBitrateEl      = fMult(sce2Rate, (FIXP_DBL)(bitrateTot<<sc_brTot))>>sc_brTot;
387
388    hQC->elementBits[0]->maxBitsEl        = maxChannelBits;
389    hQC->elementBits[1]->maxBitsEl        = 2*maxChannelBits;
390    hQC->elementBits[2]->maxBitsEl        = maxChannelBits;
391    break;
392  }
393  case MODE_1_2_2: {
394    /* sce + cpe + cpe */
395    hQC->elementBits[0]->relativeBitsEl = cm->elInfo[0].relativeBits;
396    hQC->elementBits[1]->relativeBitsEl = cm->elInfo[1].relativeBits;
397    hQC->elementBits[2]->relativeBitsEl = cm->elInfo[2].relativeBits;
398    FIXP_DBL sceRate  = cm->elInfo[0].relativeBits;
399    FIXP_DBL cpe1Rate = cm->elInfo[1].relativeBits;
400    FIXP_DBL cpe2Rate = cm->elInfo[2].relativeBits;
401
402    hQC->elementBits[0]->chBitrateEl      = fMult(sceRate,  (FIXP_DBL)(bitrateTot<<sc_brTot))>>sc_brTot;
403    hQC->elementBits[1]->chBitrateEl      = fMult(cpe1Rate, (FIXP_DBL)(bitrateTot<<sc_brTot))>>(sc_brTot+1);
404    hQC->elementBits[2]->chBitrateEl      = fMult(cpe2Rate, (FIXP_DBL)(bitrateTot<<sc_brTot))>>(sc_brTot+1);
405
406    hQC->elementBits[0]->maxBitsEl        = maxChannelBits;
407    hQC->elementBits[1]->maxBitsEl        = 2*maxChannelBits;
408    hQC->elementBits[2]->maxBitsEl        = 2*maxChannelBits;
409    break;
410  }
411
412  case MODE_1_2_2_1: {
413    /* (5.1) sce + cpe + cpe + lfe */
414    hQC->elementBits[0]->relativeBitsEl = cm->elInfo[0].relativeBits;
415    hQC->elementBits[1]->relativeBitsEl = cm->elInfo[1].relativeBits;
416    hQC->elementBits[2]->relativeBitsEl = cm->elInfo[2].relativeBits;
417    hQC->elementBits[3]->relativeBitsEl = cm->elInfo[3].relativeBits;
418    FIXP_DBL sceRate  = cm->elInfo[0].relativeBits;
419    FIXP_DBL cpe1Rate = cm->elInfo[1].relativeBits;
420    FIXP_DBL cpe2Rate = cm->elInfo[2].relativeBits;
421    FIXP_DBL lfeRate  = cm->elInfo[3].relativeBits;
422
423    int maxBitsTot = maxChannelBits * 5; /* LFE does not add to bit reservoir */
424    int sc = CountLeadingBits(fixMax(maxChannelBits,averageBitsTot));
425    int maxLfeBits = (int) FDKmax ( (INT)((fMult(lfeRate,(FIXP_DBL)(maxChannelBits<<sc))>>sc)<<1),
426                                    (INT)((fMult(FL2FXCONST_DBL(1.1f/2.f),fMult(lfeRate,(FIXP_DBL)(averageBitsTot<<sc)))<<1)>>sc) );
427
428    maxChannelBits = (maxBitsTot - maxLfeBits);
429    sc = CountLeadingBits(maxChannelBits);
430
431    maxChannelBits = fMult((FIXP_DBL)maxChannelBits<<sc,GetInvInt(5))>>sc;
432
433    hQC->elementBits[0]->chBitrateEl      = fMult(sceRate,  (FIXP_DBL)(bitrateTot<<sc_brTot))>>sc_brTot;
434    hQC->elementBits[1]->chBitrateEl      = fMult(cpe1Rate, (FIXP_DBL)(bitrateTot<<sc_brTot))>>(sc_brTot+1);
435    hQC->elementBits[2]->chBitrateEl      = fMult(cpe2Rate, (FIXP_DBL)(bitrateTot<<sc_brTot))>>(sc_brTot+1);
436    hQC->elementBits[3]->chBitrateEl      = fMult(lfeRate,  (FIXP_DBL)(bitrateTot<<sc_brTot))>>sc_brTot;
437
438    hQC->elementBits[0]->maxBitsEl        = maxChannelBits;
439    hQC->elementBits[1]->maxBitsEl        = 2*maxChannelBits;
440    hQC->elementBits[2]->maxBitsEl        = 2*maxChannelBits;
441    hQC->elementBits[3]->maxBitsEl        = maxLfeBits;
442
443    break;
444  }
445
446  case MODE_1_2_2_2_1:{
447      /* (7.1) sce + cpe + cpe + cpe + lfe */
448    hQC->elementBits[0]->relativeBitsEl = cm->elInfo[0].relativeBits;
449    hQC->elementBits[1]->relativeBitsEl = cm->elInfo[1].relativeBits;
450    hQC->elementBits[2]->relativeBitsEl = cm->elInfo[2].relativeBits;
451    hQC->elementBits[3]->relativeBitsEl = cm->elInfo[3].relativeBits;
452    hQC->elementBits[4]->relativeBitsEl = cm->elInfo[4].relativeBits;
453    FIXP_DBL sceRate  = cm->elInfo[0].relativeBits;
454    FIXP_DBL cpe1Rate = cm->elInfo[1].relativeBits;
455    FIXP_DBL cpe2Rate = cm->elInfo[2].relativeBits;
456    FIXP_DBL cpe3Rate = cm->elInfo[3].relativeBits;
457    FIXP_DBL lfeRate  = cm->elInfo[4].relativeBits;
458
459    int maxBitsTot = maxChannelBits * 7; /* LFE does not add to bit reservoir */
460    int sc = CountLeadingBits(fixMax(maxChannelBits,averageBitsTot));
461    int maxLfeBits = (int) FDKmax ( (INT)((fMult(lfeRate,(FIXP_DBL)(maxChannelBits<<sc))>>sc)<<1),
462                                    (INT)((fMult(FL2FXCONST_DBL(1.1f/2.f),fMult(lfeRate,(FIXP_DBL)(averageBitsTot<<sc)))<<1)>>sc) );
463
464    maxChannelBits = (maxBitsTot - maxLfeBits) / 7;
465
466    hQC->elementBits[0]->chBitrateEl = fMult(sceRate,  (FIXP_DBL)(bitrateTot<<sc_brTot))>>sc_brTot;
467    hQC->elementBits[1]->chBitrateEl = fMult(cpe1Rate, (FIXP_DBL)(bitrateTot<<sc_brTot))>>(sc_brTot+1);
468    hQC->elementBits[2]->chBitrateEl = fMult(cpe2Rate, (FIXP_DBL)(bitrateTot<<sc_brTot))>>(sc_brTot+1);
469    hQC->elementBits[3]->chBitrateEl = fMult(cpe3Rate, (FIXP_DBL)(bitrateTot<<sc_brTot))>>(sc_brTot+1);
470    hQC->elementBits[4]->chBitrateEl = fMult(lfeRate,  (FIXP_DBL)(bitrateTot<<sc_brTot))>>sc_brTot;
471
472    hQC->elementBits[0]->maxBitsEl = maxChannelBits;
473    hQC->elementBits[1]->maxBitsEl = 2*maxChannelBits;
474    hQC->elementBits[2]->maxBitsEl = 2*maxChannelBits;
475    hQC->elementBits[3]->maxBitsEl = 2*maxChannelBits;
476    hQC->elementBits[4]->maxBitsEl = maxLfeBits;
477    break;
478  }
479
480  default:
481    return AAC_ENC_UNSUPPORTED_CHANNELCONFIG;
482  }
483
484  return AAC_ENC_OK;
485}
486
487/********************************************************************************/
488/*                                                                              */
489/* function:    GetMonoStereoMODE(const CHANNEL_MODE mode)                      */
490/*                                                                              */
491/* description: Determines encoder setting from channel mode.                   */
492/*              Multichannel modes are mapped to mono or stereo modes           */
493/*              returns MODE_MONO in case of mono,                           */
494/*                      MODE_STEREO in case of stereo                        */
495/*                      MODE_INVALID in case of error                        */
496/*                                                                              */
497/* input:       CHANNEL_MODE mode: Encoder mode (see qc_data.h).                */
498/* output:      return: CM_STEREO_MODE monoStereoSetting                        */
499/*              (MODE_INVALID: error,                                        */
500/*               MODE_MONO:    mono                                          */
501/*               MODE_STEREO:  stereo).                                      */
502/*                                                                              */
503/* misc:        No memory is allocated.                                         */
504/*                                                                              */
505/********************************************************************************/
506
507ELEMENT_MODE FDKaacEnc_GetMonoStereoMode(const CHANNEL_MODE mode){
508
509  ELEMENT_MODE monoStereoSetting = EL_MODE_INVALID;
510
511  switch(mode){
512  case MODE_1:        /* mono setups */
513    monoStereoSetting = EL_MODE_MONO;
514    break;
515  case MODE_2:        /* stereo setups */
516  case MODE_1_2:
517  case MODE_1_2_1:
518  case MODE_1_2_2:
519  case MODE_1_2_2_1:
520  case MODE_1_2_2_2_1:
521    monoStereoSetting = EL_MODE_STEREO;
522    break;
523  default:            /* error */
524    monoStereoSetting = EL_MODE_INVALID;
525    break;
526  }
527
528  return monoStereoSetting;
529}
530
531const CHANNEL_MODE_CONFIG_TAB* FDKaacEnc_GetChannelModeConfiguration(const CHANNEL_MODE mode)
532{
533  INT i;
534  const CHANNEL_MODE_CONFIG_TAB *cm_config = NULL;
535
536  /* get channel mode config */
537  for (i=0; i<(INT)sizeof(channelModeConfig)/(INT)sizeof(CHANNEL_MODE_CONFIG_TAB); i++) {
538    if (channelModeConfig[i].encMode==mode)
539    {
540        cm_config = &channelModeConfig[i];
541        break;
542    }
543  }
544  return cm_config;
545}
546