aboutsummaryrefslogtreecommitdiffstats
path: root/libAACenc/src/aacenc.cpp
blob: c2c744f10112000694e12f6a449dfab680b05271 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
/***************************  Fast MPEG AAC Audio Encoder  **********************

                     (C) Copyright Fraunhofer IIS (1999)
                               All Rights Reserved

    Please be advised that this software and/or program delivery is
    Confidential Information of Fraunhofer and subject to and covered by the

    Fraunhofer IIS Software Evaluation Agreement
    between Google Inc. and  Fraunhofer
    effective and in full force since March 1, 2012.

    You may use this software and/or program only under the terms and
    conditions described in the above mentioned Fraunhofer IIS Software
    Evaluation Agreement. Any other and/or further use requires a separate agreement.


   This software and/or program is protected by copyright law and international
   treaties. Any reproduction or distribution of this software and/or program,
   or any portion of it, may result in severe civil and criminal penalties, and
   will be prosecuted to the maximum extent possible under law.

   $Id$
   Initial author:       M. Schug / A. Groeschel
   contents/description: fast aac coder functions

******************************************************************************/

#include "aacenc.h"

#include "bitenc.h"
#include "interface.h"
#include "psy_configuration.h"
#include "psy_main.h"
#include "qc_main.h"
#include "bandwidth.h"
#include "channel_map.h"
#include "tns_func.h"
#include "aacEnc_ram.h"

#include "genericStds.h"




#define MIN_BUFSIZE_PER_EFF_CHAN 6144

static AAC_ENCODER_ERROR FDKaacEnc_InitCheckAncillary(INT bitRate,
                                                      INT framelength,
                                                      INT ancillaryRate,
                                                      INT *ancillaryBitsPerFrame,
                                                      INT sampleRate);

INT FDKaacEnc_LimitBitrate(
        HANDLE_TRANSPORTENC hTpEnc,
        INT coreSamplingRate,
        INT frameLength,
        INT nChannels,
        INT nChannelsEff,
        INT bitRate,
        INT averageBits,
        INT *pAverageBitsPerFrame,
        INT bitrateMode,
        INT nSubFrames
        )
{
  INT transportBits, prevBitRate, averageBitsPerFrame, shift = 0, iter=0;

  while ( (frameLength & ~((1<<(shift+1))-1)) == frameLength
    && (coreSamplingRate & ~((1<<(shift+1))-1)) == coreSamplingRate )
  {
    shift ++;
  }

  do {
    prevBitRate = bitRate;
     averageBitsPerFrame = (bitRate*(frameLength>>shift)) / (coreSamplingRate>>shift) / nSubFrames;

    if (pAverageBitsPerFrame != NULL) {
      *pAverageBitsPerFrame = averageBitsPerFrame;
    }

    if (hTpEnc != NULL) {
      transportBits = transportEnc_GetStaticBits(hTpEnc, averageBitsPerFrame);
    } else {
      /* Assume some worst case */
      transportBits = 208;
    }

    bitRate = FDKmax(bitRate, ((((40 * nChannels) + transportBits + frameLength) * (coreSamplingRate)) / frameLength) );
    FDK_ASSERT(bitRate >= 0);

    bitRate = FDKmin(bitRate, ((nChannelsEff * MIN_BUFSIZE_PER_EFF_CHAN)*(coreSamplingRate>>shift)) / (frameLength>>shift)) ;
    FDK_ASSERT(bitRate >= 0);

  } while (prevBitRate != bitRate && iter++ < 3) ;

  return bitRate;
}


typedef struct
{
  AACENC_BITRATE_MODE bitrateMode;
  int chanBitrate[2]; /* mono/stereo settings */
} CONFIG_TAB_ENTRY_VBR;

static const CONFIG_TAB_ENTRY_VBR configTabVBR[] = {
  {AACENC_BR_MODE_CBR,   {     0,     0}} ,
  {AACENC_BR_MODE_VBR_1, { 32000, 20000}} ,
  {AACENC_BR_MODE_VBR_2, { 40000, 32000}} ,
  {AACENC_BR_MODE_VBR_3, { 56000, 48000}} ,
  {AACENC_BR_MODE_VBR_4, { 72000, 64000}} ,
  {AACENC_BR_MODE_VBR_5, {112000, 96000}}
};

/*-----------------------------------------------------------------------------

     functionname: FDKaacEnc_GetVBRBitrate
     description:  Get VBR bitrate from vbr quality
     input params: int vbrQuality (VBR0, VBR1, VBR2)
                   channelMode
     returns:      vbr bitrate

 ------------------------------------------------------------------------------*/
INT FDKaacEnc_GetVBRBitrate(INT bitrateMode, CHANNEL_MODE channelMode)
{
  INT bitrate = 0;
  INT monoStereoMode = 0; /* default mono */

  if (FDKaacEnc_GetMonoStereoMode(channelMode)==EL_MODE_STEREO) {
      monoStereoMode = 1;
  }

  switch((AACENC_BITRATE_MODE)bitrateMode){
  case AACENC_BR_MODE_VBR_1:
  case AACENC_BR_MODE_VBR_2:
  case AACENC_BR_MODE_VBR_3:
  case AACENC_BR_MODE_VBR_4:
  case AACENC_BR_MODE_VBR_5:
    bitrate = configTabVBR[bitrateMode].chanBitrate[monoStereoMode];
    break;
  case AACENC_BR_MODE_INVALID:
  case AACENC_BR_MODE_CBR:
  case AACENC_BR_MODE_SFR:
  case AACENC_BR_MODE_FF:
  default:
    bitrate = 0;
    break;
  }

  /* convert channel bitrate to overall bitrate*/
  bitrate *= FDKaacEnc_GetChannelModeConfiguration(channelMode)->nChannelsEff;

  return bitrate;
}

/**
 * \brief  Convert encoder bitreservoir value for transport library.
 *
 * \param bitrateMode           Bitratemode used in current encoder instance. Se ::AACENC_BITRATE_MODE
 * \param bitresTotal           Encoder bitreservoir level in bits.
 *
 * \return  Corrected bitreservoir level used in transport library.
 */
static INT FDKaacEnc_EncBitresToTpBitres(
        const AACENC_BITRATE_MODE bitrateMode,
        const INT                 bitresTotal
        )
{
  INT transporBitreservoir = 0;

  switch (bitrateMode) {
    case AACENC_BR_MODE_CBR:
      transporBitreservoir = bitresTotal; /* encoder bitreservoir level */
      break;
    case AACENC_BR_MODE_VBR_1:
    case AACENC_BR_MODE_VBR_2:
    case AACENC_BR_MODE_VBR_3:
    case AACENC_BR_MODE_VBR_4:
    case AACENC_BR_MODE_VBR_5:
      transporBitreservoir = FDK_INT_MAX; /* signal variable bitrate */
      break;
    case AACENC_BR_MODE_FF:
    case AACENC_BR_MODE_SFR:
      transporBitreservoir = 0;           /* super framing and fixed framing */
      break;                              /* without bitreservoir signaling */
    default:
    case AACENC_BR_MODE_INVALID:
      transporBitreservoir = 0;           /* invalid configuration*/
      FDK_ASSERT(0);
  }

  return transporBitreservoir;
}

/*-----------------------------------------------------------------------------

     functionname: FDKaacEnc_AacInitDefaultConfig
     description:  gives reasonable default configuration
     returns:      ---

 ------------------------------------------------------------------------------*/
void FDKaacEnc_AacInitDefaultConfig(AACENC_CONFIG *config)
{
    /* make thepre initialization of the structs flexible */
    FDKmemclear(config, sizeof(AACENC_CONFIG));

    /* default ancillary */
    config->anc_Rate = 0;         /* no ancillary data */
    config->ancDataBitRate = 0;   /* no additional consumed bitrate */

    /* default configurations */
    config->bitRate         = -1;                   /* bitrate must be set*/
    config->averageBits     = -1;                   /* instead of bitrate/s we can configure bits/superframe */
    config->bitrateMode     = 0;
    config->bandWidth       = 0;                    /* get bandwidth from table */
    config->useTns          = TNS_ENABLE_MASK;      /* tns enabled completly */
    config->usePns          = 1;                    /* depending on channelBitrate this might be set to 0 later */
    config->useIS           = 1;                    /* Intensity Stereo Configuration */
    config->framelength     = DEFAULT_FRAMELENGTH;  /* used frame size */
    config->syntaxFlags     = 0;                    /* default syntax with no specialities */
    config->epConfig        = -1;                   /* no ER syntax -> no additional error protection */
    config->nSubFrames      = 1;                    /* default, no sub frames */
    config->channelOrder    = CH_ORDER_MPEG;        /* Use MPEG channel ordering. */
    config->channelMode     = MODE_UNKNOWN;
    config->minBitsPerFrame = -1;                   /* minum number of bits in each AU */
    config->maxBitsPerFrame = -1;                   /* minum number of bits in each AU */
    config->bitreservoir    = -1;                   /* default, uninitialized value */

    /* init tabs in fixpoint_math */
    InitLdInt();
    InitInvSqrtTab();
}


/*---------------------------------------------------------------------------

    functionname: FDKaacEnc_Open
    description:  allocate and initialize a new encoder instance
    returns:      error code

  ---------------------------------------------------------------------------*/
AAC_ENCODER_ERROR FDKaacEnc_Open(HANDLE_AAC_ENC  *phAacEnc,
                                 const INT        nElements,
                                 const INT        nChannels,
                                 const INT        nSubFrames)
{
  AAC_ENCODER_ERROR ErrorStatus;
  AAC_ENC           *hAacEnc = NULL;
  UCHAR             *dynamicRAM = NULL;

  if (phAacEnc==NULL) {
    return AAC_ENC_INVALID_HANDLE;
  }

  /* allocate encoder structure */
  hAacEnc = GetRam_aacEnc_AacEncoder();
  if (hAacEnc == NULL) {
    ErrorStatus = AAC_ENC_NO_MEMORY;
    goto bail;
  }
  FDKmemclear(hAacEnc, sizeof(AAC_ENC));

  hAacEnc->dynamic_RAM = GetAACdynamic_RAM();
  dynamicRAM = (UCHAR*)hAacEnc->dynamic_RAM;

  /* allocate the Psy aud Psy Out structure */
   ErrorStatus = FDKaacEnc_PsyNew(&hAacEnc->psyKernel,
                                  nElements,
                                  nChannels
                                 ,dynamicRAM
                                 );
   if (ErrorStatus != AAC_ENC_OK)
     goto bail;

   ErrorStatus = FDKaacEnc_PsyOutNew(hAacEnc->psyOut,
                                     nElements,
                                     nChannels,
                                     nSubFrames
                                    ,dynamicRAM
                                    );
   if (ErrorStatus != AAC_ENC_OK)
     goto bail;

  /* allocate the Q&C Out structure */
  ErrorStatus = FDKaacEnc_QCOutNew(hAacEnc->qcOut,
                                   nElements,
                                   nChannels,
                                   nSubFrames
                                  ,dynamicRAM
                                   );
  if (ErrorStatus != AAC_ENC_OK)
    goto bail;

  /* allocate the Q&C kernel */
  ErrorStatus = FDKaacEnc_QCNew(&hAacEnc->qcKernel,
                                 nElements
                                ,dynamicRAM
                                );
  if (ErrorStatus != AAC_ENC_OK)
    goto bail;

  hAacEnc->maxChannels = nChannels;
  hAacEnc->maxElements = nElements;
  hAacEnc->maxFrames   = nSubFrames;

bail:
  *phAacEnc = hAacEnc;
  return ErrorStatus;
}


AAC_ENCODER_ERROR FDKaacEnc_Initialize(HANDLE_AAC_ENC      hAacEnc,
                                       AACENC_CONFIG      *config,     /* pre-initialized config struct */
                                       HANDLE_TRANSPORTENC hTpEnc,
                                       ULONG               initFlags)
{
  AAC_ENCODER_ERROR ErrorStatus;
  INT psyBitrate, tnsMask; //INT profile = 1;
  CHANNEL_MAPPING   *cm = NULL;

  INT qmbfac, qbw;
  FIXP_DBL mbfac, bw_ratio;
  QC_INIT qcInit;
  INT averageBitsPerFrame = 0;

  if (config==NULL)
    return AAC_ENC_INVALID_HANDLE;

  /******************* sanity checks *******************/

  /* check config structure */
  if (config->nChannels  < 1 || config->nChannels > (6)) {
    return AAC_ENC_UNSUPPORTED_CHANNELCONFIG;
  }

  /* check sample rate */
  switch (config->sampleRate)
  {
    case 8000:
    case 11025:
    case 12000:
    case 16000:
    case 22050:
    case 24000:
    case 32000:
    case 44100:
    case 48000:
    case 64000:
    case 88200:
    case 96000:
      break;
    default:
      return AAC_ENC_UNSUPPORTED_SAMPLINGRATE;
  }

  /* bitrate has to be set */
  if (config->bitRate==-1) {
      return AAC_ENC_UNSUPPORTED_BITRATE;
  }

  /* check bit rate */

  if (FDKaacEnc_LimitBitrate(
          hTpEnc,
          config->sampleRate,
          config->framelength,
          config->nChannels,
          FDKaacEnc_GetChannelModeConfiguration(config->channelMode)->nChannelsEff,
          config->bitRate,
          config->averageBits,
         &averageBitsPerFrame,
          config->bitrateMode,
          config->nSubFrames
          ) != config->bitRate )
  {
    return AAC_ENC_UNSUPPORTED_BITRATE;
  }

  if (config->syntaxFlags & AC_ER_VCB11) {
      return AAC_ENC_UNSUPPORTED_ER_FORMAT;
  }
  if (config->syntaxFlags & AC_ER_HCR) {
      return AAC_ENC_UNSUPPORTED_ER_FORMAT;
  }

  /* check frame length */
  switch (config->framelength)
  {
    case 1024:
      if ( config->audioObjectType != AOT_AAC_LC
        && config->audioObjectType != AOT_SBR
        && config->audioObjectType != AOT_PS
        && config->audioObjectType != AOT_ER_AAC_LC
        && config->audioObjectType != AOT_AAC_SCAL )
      {
        return AAC_ENC_INVALID_FRAME_LENGTH;
      }
      break;
    case 512:
    case 480:
      if ( config->audioObjectType != AOT_ER_AAC_LD
        && config->audioObjectType != AOT_ER_AAC_ELD )
      {
        return AAC_ENC_INVALID_FRAME_LENGTH;
      }
      break;
    default:
      return AAC_ENC_INVALID_FRAME_LENGTH;
  }

  if (config->anc_Rate != 0) {

     ErrorStatus = FDKaacEnc_InitCheckAncillary(config->bitRate,
                                                config->framelength,
                                                config->anc_Rate,
                                               &hAacEnc->ancillaryBitsPerFrame,
                                                config->sampleRate);
     if (ErrorStatus != AAC_ENC_OK)
       goto bail;


     /* update estimated consumed bitrate */
     config->ancDataBitRate += ( (hAacEnc->ancillaryBitsPerFrame * config->sampleRate) / config->framelength );

  }

  /* maximal allowed DSE bytes in frame */
  {
  /* fixpoint calculation*/
  INT q_res, encBitrate, sc;
  FIXP_DBL tmp = fDivNorm(config->framelength, config->sampleRate, &q_res);
  encBitrate = (config->bitRate/*-config->ancDataBitRate*/)- (INT)(config->nChannels*8000);
  sc = CountLeadingBits(encBitrate);
  config->maxAncBytesPerAU = FDKmin( (256), FDKmax(0,(INT)(fMultDiv2(tmp, (FIXP_DBL)(encBitrate<<sc))>>(-q_res+sc-1+3))) );
  }

  /* bind config to hAacEnc->config */
  hAacEnc->config = config;

  /* set hAacEnc->bitrateMode */
  hAacEnc->bitrateMode = (AACENC_BITRATE_MODE)config->bitrateMode;

  hAacEnc->encoderMode = config->channelMode;

  ErrorStatus = FDKaacEnc_InitChannelMapping(hAacEnc->encoderMode, config->channelOrder, &hAacEnc->channelMapping);
  if (ErrorStatus != AAC_ENC_OK)
    goto bail;

  cm = &hAacEnc->channelMapping;

  ErrorStatus = FDKaacEnc_DetermineBandWidth(&hAacEnc->config->bandWidth,
                                             config->bandWidth,
                                             config->bitRate - config->ancDataBitRate,
                                             hAacEnc->bitrateMode,
                                             config->sampleRate,
                                             config->framelength,
                                             cm,
                                             hAacEnc->encoderMode);
  if (ErrorStatus != AAC_ENC_OK)
    goto bail;

  hAacEnc->bandwidth90dB = (INT)hAacEnc->config->bandWidth;

  tnsMask = config->useTns ? TNS_ENABLE_MASK : 0x0;
  psyBitrate = config->bitRate - config->ancDataBitRate;

  ErrorStatus = FDKaacEnc_psyInit(hAacEnc->psyKernel,
                                  hAacEnc->psyOut,
                                  hAacEnc->maxFrames,
                                  hAacEnc->maxChannels,
                                  config->audioObjectType,
                                  cm);
  if (ErrorStatus != AAC_ENC_OK)
    goto bail;

  ErrorStatus = FDKaacEnc_psyMainInit(hAacEnc->psyKernel,
                                      config->audioObjectType,
                                      cm,
                                      config->sampleRate,
                                      config->framelength,
                                      psyBitrate,
                                      tnsMask,
                                      hAacEnc->bandwidth90dB,
                                      config->usePns,
                                      config->useIS,
                                      config->syntaxFlags,
                                      initFlags);
  if (ErrorStatus != AAC_ENC_OK)
    goto bail;

  ErrorStatus = FDKaacEnc_QCOutInit(hAacEnc->qcOut, hAacEnc->maxFrames, cm);
  if (ErrorStatus != AAC_ENC_OK)
    goto bail;



  qcInit.channelMapping      = &hAacEnc->channelMapping;
  qcInit.sceCpe              = 0;

  {
      int maxBitres;
      qcInit.averageBits     = (averageBitsPerFrame+7)&~7;
      maxBitres              = (MIN_BUFSIZE_PER_EFF_CHAN*cm->nChannelsEff) - qcInit.averageBits;
      qcInit.bitRes          = (config->bitreservoir!=-1) ? FDKmin(config->bitreservoir, maxBitres) : maxBitres;

      qcInit.maxBits         = fixMin(MIN_BUFSIZE_PER_EFF_CHAN*cm->nChannelsEff, ((averageBitsPerFrame+7)&~7)+qcInit.bitRes);
      qcInit.maxBits         = (config->maxBitsPerFrame!=-1) ? fixMin(qcInit.maxBits, config->maxBitsPerFrame) : qcInit.maxBits;

      qcInit.minBits         = fixMax(0, ((averageBitsPerFrame-1)&~7)-qcInit.bitRes-transportEnc_GetStaticBits(hTpEnc, ((averageBitsPerFrame+7)&~7)+qcInit.bitRes));
      qcInit.minBits         = (config->minBitsPerFrame!=-1) ? fixMax(qcInit.minBits, config->minBitsPerFrame) : qcInit.minBits;
  }

  qcInit.nSubFrames          = config->nSubFrames;
  qcInit.padding.paddingRest = config->sampleRate;

  /* Calc meanPe */
  bw_ratio = fDivNorm((FIXP_DBL)hAacEnc->bandwidth90dB, (FIXP_DBL)(config->sampleRate>>1), &qbw);
  qbw = DFRACT_BITS-1-qbw;
  /* qcInit.meanPe = 10.0f * FRAME_LEN_LONG * hAacEnc->bandwidth90dB/(config->sampleRate/2.0f); */
  qcInit.meanPe = fMult(bw_ratio, (FIXP_DBL)((10*config->framelength)<<16)) >> (qbw-15);

  /* Calc maxBitFac */
  mbfac = fDivNorm((MIN_BUFSIZE_PER_EFF_CHAN-744)*cm->nChannelsEff, qcInit.averageBits/qcInit.nSubFrames, &qmbfac);
  qmbfac = DFRACT_BITS-1-qmbfac;
  qcInit.maxBitFac = (qmbfac > 24) ? (mbfac >> (qmbfac - 24)):(mbfac << (24 - qmbfac));

  switch(config->bitrateMode){
  case AACENC_BR_MODE_CBR:
    qcInit.bitrateMode = QCDATA_BR_MODE_CBR;
    break;
  case AACENC_BR_MODE_VBR_1:
    qcInit.bitrateMode = QCDATA_BR_MODE_VBR_1;
    break;
  case AACENC_BR_MODE_VBR_2:
    qcInit.bitrateMode = QCDATA_BR_MODE_VBR_2;
    break;
  case AACENC_BR_MODE_VBR_3:
    qcInit.bitrateMode = QCDATA_BR_MODE_VBR_3;
    break;
  case AACENC_BR_MODE_VBR_4:
    qcInit.bitrateMode = QCDATA_BR_MODE_VBR_4;
    break;
  case AACENC_BR_MODE_VBR_5:
    qcInit.bitrateMode = QCDATA_BR_MODE_VBR_5;
    break;
  case AACENC_BR_MODE_SFR:
    qcInit.bitrateMode = QCDATA_BR_MODE_SFR;
    break;
  case AACENC_BR_MODE_FF:
    qcInit.bitrateMode = QCDATA_BR_MODE_FF;
    break;
  default:
    ErrorStatus = AAC_ENC_UNSUPPORTED_BITRATE_MODE;
    goto bail;
  }

  qcInit.invQuant = (config->useRequant)?2:0;

  /* maxIterations should be set to the maximum number of requantization iterations that are
   * allowed before the crash recovery functionality is activated. This setting should be adjusted
   * to the processing power available, i.e. to the processing power headroom in one frame that is
   * still left after normal encoding without requantization. Please note that if activated this
   * functionality is used most likely only in cases where the encoder is operating beyond
   * recommended settings, i.e. the audio quality is suboptimal anyway. Activating the crash
   * recovery does not further reduce audio quality significantly in these cases. */
  if ( (config->audioObjectType == AOT_ER_AAC_LD) || (config->audioObjectType == AOT_ER_AAC_ELD) ) {
    qcInit.maxIterations = 2;
  }
  else
  {
    qcInit.maxIterations = 5;
  }

  qcInit.bitrate = config->bitRate - config->ancDataBitRate;

  qcInit.staticBits = transportEnc_GetStaticBits(hTpEnc, qcInit.averageBits/qcInit.nSubFrames);

  ErrorStatus = FDKaacEnc_QCInit(hAacEnc->qcKernel, &qcInit);
  if (ErrorStatus != AAC_ENC_OK)
    goto bail;

  /* Map virtual aot's to intern aot used in bitstream writer. */
  switch (hAacEnc->config->audioObjectType) {
    case AOT_MP2_AAC_LC:
    case AOT_DABPLUS_AAC_LC:
      hAacEnc->aot = AOT_AAC_LC;
      break;
    case AOT_MP2_SBR:
    case AOT_DABPLUS_SBR:
      hAacEnc->aot = AOT_SBR;
      break;
    case AOT_MP2_PS:
    case AOT_DABPLUS_PS:
      hAacEnc->aot = AOT_PS;
      break;
    default:
      hAacEnc->aot = hAacEnc->config->audioObjectType;
  }

  /* common things */

  return AAC_ENC_OK;

bail:

  return ErrorStatus;
}


/*---------------------------------------------------------------------------

    functionname: FDKaacEnc_EncodeFrame
    description:  encodes one frame
    returns:      error code

  ---------------------------------------------------------------------------*/
AAC_ENCODER_ERROR FDKaacEnc_EncodeFrame( HANDLE_AAC_ENC       hAacEnc,          /* encoder handle */
                                         HANDLE_TRANSPORTENC  hTpEnc,
                                         INT_PCM* RESTRICT    inputBuffer,
                                         INT*                 nOutBytes,
                                         AACENC_EXT_PAYLOAD   extPayload[MAX_TOTAL_EXT_PAYLOADS]
                                       )
{
    AAC_ENCODER_ERROR ErrorStatus;
    int    el, n, c=0;
    UCHAR  extPayloadUsed[MAX_TOTAL_EXT_PAYLOADS];

    CHANNEL_MAPPING *cm      = &hAacEnc->channelMapping;



    PSY_OUT *psyOut = hAacEnc->psyOut[c];
    QC_OUT  *qcOut  = hAacEnc->qcOut[c];

    FDKmemclear(extPayloadUsed, MAX_TOTAL_EXT_PAYLOADS * sizeof(UCHAR));

    qcOut->elementExtBits = 0; /* sum up all extended bit of each element */
    qcOut->staticBits     = 0; /* sum up side info bits of each element */
    qcOut->totalNoRedPe   = 0; /* sum up PE */

    /* advance psychoacoustics */
    for (el=0; el<cm->nElements; el++) {
        ELEMENT_INFO elInfo = cm->elInfo[el];

        if ( (elInfo.elType == ID_SCE)
          || (elInfo.elType == ID_CPE)
          || (elInfo.elType == ID_LFE) )
        {
            int ch;

            /* update pointer!*/
            for(ch=0;ch<elInfo.nChannelsInEl;ch++) {
                PSY_OUT_CHANNEL *psyOutChan = psyOut->psyOutElement[el]->psyOutChannel[ch];
                QC_OUT_CHANNEL  *qcOutChan = qcOut->qcElement[el]->qcOutChannel[ch];

                psyOutChan->mdctSpectrum       = qcOutChan->mdctSpectrum;
                psyOutChan->sfbSpreadEnergy  = qcOutChan->sfbSpreadEnergy;
                psyOutChan->sfbEnergy          = qcOutChan->sfbEnergy;
                psyOutChan->sfbEnergyLdData    = qcOutChan->sfbEnergyLdData;
                psyOutChan->sfbMinSnrLdData    = qcOutChan->sfbMinSnrLdData;
                psyOutChan->sfbThresholdLdData = qcOutChan->sfbThresholdLdData;

            }

            FDKaacEnc_psyMain(elInfo.nChannelsInEl,
                              hAacEnc->psyKernel->psyElement[el],
                              hAacEnc->psyKernel->psyDynamic,
                              hAacEnc->psyKernel->psyConf,
                              psyOut->psyOutElement[el],
                              inputBuffer,
                              cm->elInfo[el].ChannelIndex,
                              cm->nChannels

                    );

            /* FormFactor, Pe and staticBitDemand calculation */
            ErrorStatus = FDKaacEnc_QCMainPrepare(&elInfo,
                                                   hAacEnc->qcKernel->hAdjThr->adjThrStateElem[el],
                                                   psyOut->psyOutElement[el],
                                                   qcOut->qcElement[el],
                                                   hAacEnc->aot,
                                                   hAacEnc->config->syntaxFlags,
                                                   hAacEnc->config->epConfig);

            if (ErrorStatus != AAC_ENC_OK)
              return ErrorStatus;

            /*-------------------------------------------- */

            qcOut->qcElement[el]->extBitsUsed = 0;
            qcOut->qcElement[el]->nExtensions = 0;
            /* reset extension payload */
            FDKmemclear(&qcOut->qcElement[el]->extension, (1)*sizeof(QC_OUT_EXTENSION));

            for ( n = 0; n < MAX_TOTAL_EXT_PAYLOADS; n++ ) {
                if ( !extPayloadUsed[n]
                  && (extPayload[n].associatedChElement == el)
                  && (extPayload[n].dataSize > 0)
                  && (extPayload[n].pData != NULL) )
                {
                    int idx = qcOut->qcElement[el]->nExtensions++;

                    qcOut->qcElement[el]->extension[idx].type         = extPayload[n].dataType;   /* Perform a sanity check on the type? */
                    qcOut->qcElement[el]->extension[idx].nPayloadBits = extPayload[n].dataSize;
                    qcOut->qcElement[el]->extension[idx].pPayload = extPayload[n].pData;
                    /* Now ask the bitstream encoder how many bits we need to encode the data with the current bitstream syntax: */
                    qcOut->qcElement[el]->extBitsUsed +=
                          FDKaacEnc_writeExtensionData( NULL,
                                                       &qcOut->qcElement[el]->extension[idx],
                                                        0, 0,
                                                        hAacEnc->config->syntaxFlags,
                                                        hAacEnc->aot,
                                                        hAacEnc->config->epConfig );
                    extPayloadUsed[n] = 1;
                }
            }

            /* sum up extension and static bits for all channel elements */
            qcOut->elementExtBits += qcOut->qcElement[el]->extBitsUsed;
            qcOut->staticBits   += qcOut->qcElement[el]->staticBitsUsed;

            /* sum up pe */
            qcOut->totalNoRedPe += qcOut->qcElement[el]->peData.pe;
        }
    }

    qcOut->nExtensions   = 0;
    qcOut->globalExtBits = 0;

    /* reset extension payload */
    FDKmemclear(&qcOut->extension, (2+2)*sizeof(QC_OUT_EXTENSION));

    /* Add extension payload not assigned to an channel element
      (Ancillary data is the only supported type up to now) */
    for ( n = 0; n < MAX_TOTAL_EXT_PAYLOADS; n++ ) {
        if ( !extPayloadUsed[n]
          && (extPayload[n].associatedChElement == -1)
          && (extPayload[n].pData != NULL) )
        {
            UINT payloadBits = 0;

            if (extPayload[n].dataType == EXT_DATA_ELEMENT) {
                if (hAacEnc->ancillaryBitsPerFrame) {
                    /* granted frame dse bitrate */
                    payloadBits = hAacEnc->ancillaryBitsPerFrame;
                }
                else {
                    /* write anc data if bitrate constraint fulfilled */
                    if ((extPayload[n].dataSize>>3) <= hAacEnc->config->maxAncBytesPerAU) {
                        payloadBits = extPayload[n].dataSize;
                    }
                }
                payloadBits = fixMin( extPayload[n].dataSize, payloadBits );
            } else {
                payloadBits = extPayload[n].dataSize;
            }

            if (payloadBits > 0)
            {
                int idx = qcOut->nExtensions++;

                qcOut->extension[idx].type         = extPayload[n].dataType;   /* Perform a sanity check on the type? */
                qcOut->extension[idx].nPayloadBits = payloadBits;
                qcOut->extension[idx].pPayload = extPayload[n].pData;
                /* Now ask the bitstream encoder how many bits we need to encode the data with the current bitstream syntax: */
                qcOut->globalExtBits += FDKaacEnc_writeExtensionData( NULL,
                                                                     &qcOut->extension[idx],
                                                                      0, 0,
                                                                      hAacEnc->config->syntaxFlags,
                                                                      hAacEnc->aot,
                                                                      hAacEnc->config->epConfig );
                if (extPayload[n].dataType == EXT_DATA_ELEMENT) {
                    /* substract the processed bits */
                    extPayload[n].dataSize -= payloadBits;
                }
                extPayloadUsed[n] = 1;
            }
        }
    }

    if (!(hAacEnc->config->syntaxFlags & (AC_SCALABLE|AC_ER))) {
      qcOut->globalExtBits += EL_ID_BITS;  /* add bits for ID_END */
    }

    /* build bitstream all nSubFrames */
    {
        INT totalBits    = 0;   /* Total AU bits */;
        INT avgTotalBits = 0;

        /*-------------------------------------------- */
        /* Get average total bits */
        /*-------------------------------------------- */
        {
            /* frame wise bitrate adaption */
            FDKaacEnc_AdjustBitrate(hAacEnc->qcKernel,
                                    cm,
                                   &avgTotalBits,
                                    hAacEnc->config->bitRate,
                                    hAacEnc->config->sampleRate,
                                    hAacEnc->config->framelength);

            /* adjust super frame bitrate */
            avgTotalBits *= hAacEnc->config->nSubFrames;
        }

        /* Make first estimate of transport header overhead.
           Take maximum possible frame size into account to prevent bitreservoir underrun. */
        hAacEnc->qcKernel->globHdrBits = transportEnc_GetStaticBits(hTpEnc, avgTotalBits + hAacEnc->qcKernel->bitResTot);


        /*-------------------------------------------- */
        /*-------------------------------------------- */
        /*-------------------------------------------- */

        ErrorStatus = FDKaacEnc_QCMain(hAacEnc->qcKernel,
                                       hAacEnc->psyOut,
                                       hAacEnc->qcOut,
                                       avgTotalBits,
                                       cm
                                      ,hAacEnc->aot,
                                       hAacEnc->config->syntaxFlags,
                                       hAacEnc->config->epConfig);

        if (ErrorStatus != AAC_ENC_OK)
            return ErrorStatus;
        /*-------------------------------------------- */

        /*-------------------------------------------- */
        ErrorStatus = FDKaacEnc_updateFillBits(cm,
                                               hAacEnc->qcKernel,
                                               hAacEnc->qcKernel->elementBits,
                                               hAacEnc->qcOut);
        if (ErrorStatus != AAC_ENC_OK)
            return ErrorStatus;

            /*-------------------------------------------- */
            ErrorStatus = FDKaacEnc_FinalizeBitConsumption(cm,
                                                           hAacEnc->qcKernel,
                                                           qcOut,
                                                           qcOut->qcElement,
                                                           hTpEnc,
                                                           hAacEnc->aot,
                                                           hAacEnc->config->syntaxFlags,
                                                           hAacEnc->config->epConfig);
            if (ErrorStatus != AAC_ENC_OK)
                return ErrorStatus;
            /*-------------------------------------------- */
            totalBits += qcOut->totalBits;


        /*-------------------------------------------- */
        FDKaacEnc_updateBitres(cm,
                               hAacEnc->qcKernel,
                               hAacEnc->qcOut);

        /*-------------------------------------------- */

        /* for ( all sub frames ) ... */
              /* write bitstream header */
              transportEnc_WriteAccessUnit(
                    hTpEnc,
                    totalBits,
                    FDKaacEnc_EncBitresToTpBitres(hAacEnc->bitrateMode, hAacEnc->qcKernel->bitResTot),
                    cm->nChannelsEff);

              /* write bitstream */
              ErrorStatus = FDKaacEnc_WriteBitstream(
                    hTpEnc,
                    cm,
                    qcOut,
                    psyOut,
                    hAacEnc->qcKernel,
                    hAacEnc->aot,
                    hAacEnc->config->syntaxFlags,
                    hAacEnc->config->epConfig);

              if (ErrorStatus != AAC_ENC_OK)
                return ErrorStatus;

              /* transportEnc_EndAccessUnit() is being called inside FDKaacEnc_WriteBitstream() */
              transportEnc_GetFrame(hTpEnc, nOutBytes);

    } /* -end- if (curFrame==hAacEnc->qcKernel->nSubFrames) */


    /*-------------------------------------------- */
    return AAC_ENC_OK;
}

/*---------------------------------------------------------------------------

    functionname:FDKaacEnc_Close
    description: delete encoder instance
    returns:

  ---------------------------------------------------------------------------*/

void FDKaacEnc_Close( HANDLE_AAC_ENC*  phAacEnc)   /* encoder handle */
{
    if (*phAacEnc == NULL) {
      return;
    }
    AAC_ENC *hAacEnc = (AAC_ENC*)*phAacEnc;

   if (hAacEnc->dynamic_RAM != NULL)
       FreeAACdynamic_RAM(&hAacEnc->dynamic_RAM);

    FDKaacEnc_PsyClose(&hAacEnc->psyKernel,hAacEnc->psyOut);

    FDKaacEnc_QCClose(&hAacEnc->qcKernel, hAacEnc->qcOut);

    FreeRam_aacEnc_AacEncoder(phAacEnc);
}


/* The following functions are in this source file only for convenience and */
/* need not be visible outside of a possible encoder library. */

/* basic defines for ancillary data */
#define MAX_ANCRATE 19200            /* ancillary rate >= 19200 isn't valid */

/*---------------------------------------------------------------------------

    functionname:  FDKaacEnc_InitCheckAncillary
    description:   initialize and check ancillary data struct
    return:        if success or NULL if error

  ---------------------------------------------------------------------------*/
static AAC_ENCODER_ERROR FDKaacEnc_InitCheckAncillary(INT bitRate,
                                                      INT framelength,
                                                      INT ancillaryRate,
                                                      INT *ancillaryBitsPerFrame,
                                                      INT sampleRate)
{
  INT diffToByteAlign;

  /* don't use negative ancillary rates */
  if ( ancillaryRate < -1 )
    return AAC_ENC_UNSUPPORTED_ANC_BITRATE;

  /* check if ancillary rate is ok */
  if ( (ancillaryRate != (-1)) && (ancillaryRate != 0) ) {
    /* ancRate <= 15% of bitrate && ancRate < 19200 */
    if ( ( ancillaryRate >= MAX_ANCRATE ) ||
         ( (ancillaryRate * 20) > (bitRate * 3) ) ) {
      return AAC_ENC_UNSUPPORTED_ANC_BITRATE;
    }
  }
  else if (ancillaryRate == -1) {
    /* if no special ancRate is requested but a ancillary file is
       stated, then generate a ancillary rate matching to the bitrate */
    if (bitRate >= (MAX_ANCRATE * 10)) {
      /* ancillary rate is 19199 */
      ancillaryRate = (MAX_ANCRATE - 1);
    }
    else { /* 10% of bitrate */
      ancillaryRate = bitRate / 10;
    }
  }

  /* make ancillaryBitsPerFrame byte align */
  *ancillaryBitsPerFrame = (ancillaryRate * framelength ) / sampleRate;
  diffToByteAlign = *ancillaryBitsPerFrame % 8;
  *ancillaryBitsPerFrame = *ancillaryBitsPerFrame - diffToByteAlign;

  return AAC_ENC_OK;
}