Imported Debian version 0.1.3.1
[deb_fdk-aac.git] / libAACenc / src / channel_map.cpp
1
2 /* -----------------------------------------------------------------------------------------------------------
3 Software 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
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.
12
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.
17
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.
24
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.
28
29 2. COPYRIGHT LICENSE
30
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:
33
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.
36
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.
41
42 The name of Fraunhofer may not be used to endorse or promote products derived from this library without
43 prior written permission.
44
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.
47
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."
52
53 3. NO PATENT LICENSE
54
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.
58
59 You may use this FDK AAC Codec software or modifications thereto only for purposes that are authorized
60 by appropriate patent licenses.
61
62 4. DISCLAIMER
63
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.
72
73 5. CONTACT INFORMATION
74
75 Fraunhofer Institute for Integrated Circuits IIS
76 Attention: Audio and Multimedia Departments - FDK AAC LL
77 Am Wolfsmantel 33
78 91058 Erlangen, Germany
79
80 www.iis.fraunhofer.de/amm
81 amm-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
108 5ch:
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
115 5.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
124 typedef struct {
125
126 CHANNEL_MODE encoderMode;
127 INT channel_assignment[/*(8)*/12];
128
129 } CHANNEL_ASSIGNMENT_INFO_TAB;
130
131
132 static 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
146 static 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 static const CHANNEL_ASSIGNMENT_INFO_TAB assignmentInfoTabWg4[] =
161 {
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 */
170 };
171
172 /* Channel mode configuration tab provides,
173 corresponding number of channels and elements
174 */
175 static const CHANNEL_MODE_CONFIG_TAB channelModeConfig[] =
176 {
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 },
186 };
187
188 #define MAX_MODES (sizeof(assignmentInfoTabWav)/sizeof(CHANNEL_ASSIGNMENT_INFO_TAB))
189
190 const INT* FDKaacEnc_getChannelAssignment(CHANNEL_MODE encMode, CHANNEL_ORDER co)
191 {
192 const CHANNEL_ASSIGNMENT_INFO_TAB *pTab;
193 int i;
194
195 if (co == CH_ORDER_MPEG)
196 pTab = assignmentInfoTabMpeg;
197 else if (co == CH_ORDER_WAV)
198 pTab = assignmentInfoTabWav;
199 else
200 pTab = assignmentInfoTabWg4;
201
202 for(i=MAX_MODES-1; i>0; i--) {
203 if (encMode== pTab[i].encoderMode) {
204 break;
205 }
206 }
207 return (pTab[i].channel_assignment);
208 }
209
210 AAC_ENCODER_ERROR FDKaacEnc_DetermineEncoderMode(CHANNEL_MODE* mode, INT nChannels)
211 {
212 INT i;
213 CHANNEL_MODE encMode = MODE_INVALID;
214
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;
219 break;
220 }
221 }
222 *mode = encMode;
223 }
224 else {
225 /* check if valid channel configuration */
226 if (FDKaacEnc_GetChannelModeConfiguration(*mode)->nChannels==nChannels) {
227 encMode = *mode;
228 }
229 }
230
231 if (encMode==MODE_INVALID) {
232 return AAC_ENC_UNSUPPORTED_CHANNELCONFIG;
233 }
234
235 return AAC_ENC_OK;
236 }
237
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) {
239
240 INT error=0;
241 INT counter =*cnt;
242
243 const INT *assign = FDKaacEnc_getChannelAssignment(mode, co);
244
245 elInfo->elType=elType;
246 elInfo->relativeBits = relBits;
247
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]++;
253
254 break;
255 case ID_CPE:
256 elInfo->nChannelsInEl=2;
257 elInfo->ChannelIndex[0]=assign[counter++];
258 elInfo->ChannelIndex[1]=assign[counter++];
259 elInfo->instanceTag=it_cnt[elType]++;
260 break;
261 case ID_DSE:
262 elInfo->nChannelsInEl=0;
263 elInfo->ChannelIndex[0]=0;
264 elInfo->ChannelIndex[1]=0;
265 elInfo->instanceTag=it_cnt[elType]++;
266 break;
267 default: error=1;
268 };
269 *cnt = counter;
270 return error;
271
272 }
273
274 AAC_ENCODER_ERROR FDKaacEnc_InitChannelMapping(CHANNEL_MODE mode, CHANNEL_ORDER co, CHANNEL_MAPPING* cm)
275 {
276 INT count=0; /* count through coder channels */
277 INT it_cnt[ID_END+1];
278 INT i;
279
280 for (i=0; i<ID_END; i++)
281 it_cnt[i]=0;
282
283 FDKmemclear(cm, sizeof(CHANNEL_MAPPING));
284
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)
288 {
289 cm->encMode = channelModeConfig[i].encMode;
290 cm->nChannels = channelModeConfig[i].nChannels;
291 cm->nChannelsEff = channelModeConfig[i].nChannelsEff;
292 cm->nElements = channelModeConfig[i].nElements;
293
294 break;
295 }
296 }
297
298 /* init element info struct */
299 switch(mode) {
300 case MODE_1:
301 /* (mono) sce */
302 FDKaacEnc_initElement(&cm->elInfo[0], ID_SCE, &count, mode, co, it_cnt, (FIXP_DBL)MAXVAL_DBL);
303 break;
304 case MODE_2:
305 /* (stereo) cpe */
306 FDKaacEnc_initElement(&cm->elInfo[0], ID_CPE, &count, mode, co, it_cnt, (FIXP_DBL)MAXVAL_DBL);
307 break;
308
309 case MODE_1_2:
310 /* sce + cpe */
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));
313 break;
314
315 case MODE_1_2_1:
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));
320 break;
321
322 case MODE_1_2_2:
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));
327 break;
328
329 case MODE_1_2_2_1:
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));
335 break;
336
337 case MODE_1_2_2_2_1:
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));
346 break;
347 default:
348 //*chMap=0;
349 return AAC_ENC_UNSUPPORTED_CHANNELCONFIG;
350 };
351
352
353 FDK_ASSERT(cm->nElements<=(8));
354
355
356 return AAC_ENC_OK;
357 }
358
359 AAC_ENCODER_ERROR FDKaacEnc_InitElementBits(QC_STATE *hQC,
360 CHANNEL_MAPPING *cm,
361 INT bitrateTot,
362 INT averageBitsTot,
363 INT maxChannelBits)
364 {
365 int sc_brTot = CountLeadingBits(bitrateTot);
366
367 switch(cm->encMode) {
368 case MODE_1:
369 hQC->elementBits[0]->chBitrateEl = bitrateTot;
370
371 hQC->elementBits[0]->maxBitsEl = maxChannelBits;
372
373 hQC->elementBits[0]->relativeBitsEl = cm->elInfo[0].relativeBits;
374 break;
375
376 case MODE_2:
377 hQC->elementBits[0]->chBitrateEl = bitrateTot>>1;
378
379 hQC->elementBits[0]->maxBitsEl = 2*maxChannelBits;
380
381 hQC->elementBits[0]->relativeBitsEl = cm->elInfo[0].relativeBits;
382 break;
383 case MODE_1_2: {
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;
388
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);
391
392 hQC->elementBits[0]->maxBitsEl = maxChannelBits;
393 hQC->elementBits[1]->maxBitsEl = 2*maxChannelBits;
394 break;
395 }
396 case MODE_1_2_1: {
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;
404
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;
408
409 hQC->elementBits[0]->maxBitsEl = maxChannelBits;
410 hQC->elementBits[1]->maxBitsEl = 2*maxChannelBits;
411 hQC->elementBits[2]->maxBitsEl = maxChannelBits;
412 break;
413 }
414 case MODE_1_2_2: {
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;
422
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);
426
427 hQC->elementBits[0]->maxBitsEl = maxChannelBits;
428 hQC->elementBits[1]->maxBitsEl = 2*maxChannelBits;
429 hQC->elementBits[2]->maxBitsEl = 2*maxChannelBits;
430 break;
431 }
432
433 case MODE_1_2_2_1: {
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;
443
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) );
448
449 maxChannelBits = (maxBitsTot - maxLfeBits);
450 sc = CountLeadingBits(maxChannelBits);
451
452 maxChannelBits = fMult((FIXP_DBL)maxChannelBits<<sc,GetInvInt(5))>>sc;
453
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;
458
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;
463
464 break;
465 }
466 case MODE_7_1_REAR_SURROUND:
467 case MODE_7_1_FRONT_CENTER:
468 case MODE_1_2_2_2_1: {
469 int cpe3Idx = 3;
470 int lfeIdx = 4;
471
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;
478
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) );
483
484 maxChannelBits = (maxBitsTot - maxLfeBits) / 7;
485
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;
491
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;
497 break;
498 }
499 default:
500 return AAC_ENC_UNSUPPORTED_CHANNELCONFIG;
501 }
502
503 return AAC_ENC_OK;
504 }
505
506 /********************************************************************************/
507 /* */
508 /* function: GetMonoStereoMODE(const CHANNEL_MODE mode) */
509 /* */
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 */
515 /* */
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). */
521 /* */
522 /* misc: No memory is allocated. */
523 /* */
524 /********************************************************************************/
525
526 ELEMENT_MODE FDKaacEnc_GetMonoStereoMode(const CHANNEL_MODE mode){
527
528 ELEMENT_MODE monoStereoSetting = EL_MODE_INVALID;
529
530 switch(mode){
531 case MODE_1: /* mono setups */
532 monoStereoSetting = EL_MODE_MONO;
533 break;
534 case MODE_2: /* stereo setups */
535 case MODE_1_2:
536 case MODE_1_2_1:
537 case MODE_1_2_2:
538 case MODE_1_2_2_1:
539 case MODE_1_2_2_2_1:
540 case MODE_7_1_REAR_SURROUND:
541 case MODE_7_1_FRONT_CENTER:
542 monoStereoSetting = EL_MODE_STEREO;
543 break;
544 default: /* error */
545 monoStereoSetting = EL_MODE_INVALID;
546 break;
547 }
548
549 return monoStereoSetting;
550 }
551
552 const CHANNEL_MODE_CONFIG_TAB* FDKaacEnc_GetChannelModeConfiguration(const CHANNEL_MODE mode)
553 {
554 INT i;
555 const CHANNEL_MODE_CONFIG_TAB *cm_config = NULL;
556
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)
560 {
561 cm_config = &channelModeConfig[i];
562 break;
563 }
564 }
565 return cm_config;
566 }