summaryrefslogtreecommitdiffstats
path: root/media/libstagefright/codecs/aacdec/pvmp4audiodecoderframe.cpp
blob: 7a279dc6f43c6c17f060fc8e8e783b2267e5809f (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
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
/* ------------------------------------------------------------------
 * Copyright (C) 1998-2009 PacketVideo
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either
 * express or implied.
 * See the License for the specific language governing permissions
 * and limitations under the License.
 * -------------------------------------------------------------------
 */
/*

 Pathname: pvmp4audiodecodeframe

------------------------------------------------------------------------------
 REVISION HISTORY

 Description:  Modified from original shareware code

 Description:  Pulled in loop structure from console.c, so that this function
               now decodes all frames in the file.

               Original program used several global variables.  These have been
               eliminated, except for situations in which the global variables
               could be converted into const types.  Otherwise, they are passed
               by reference through the functions.

 Description:  Begin mods for file I/O removal

 Description:  Merged trans4m_freq_2_time, trans4m_time_2_freq, etc.

 Description:  Removing commented out sections of code.  This includes the
               removal of unneeded functions init_lt_pred, reset_mc_info,

 Description: Copied from aac_decode_frame.c and renamed file,
              Made many changes.

 Description: Prepare for code review

 Description: Update per review comments:
              1) Add comment about leaveGetLoop
              2) Remove inverseTNSCoef array
              3) fix wnd_shape_this_bk to wnd_shape_prev_bk in F to T
              4) Clean up comments
              5) Change call to long_term_synthesis

 Description: Remove division for calculation of bitrate.

 Description: Remove update of LTP buffers if not LTP audio object type.

 Description: Add hasmask to call to right_ch_sfb_tools_ms

 Description:
 Modified to call ltp related routines on the left channel
 before intensity is called on the right channel.  The previous version
 was causing a problem when IS was used on the right channel and LTP
 on the left channel for the same scalefactor band.

 This fix required creating a new function, apply_ms_synt, deleting another
 function (right_ch_sfb_tools_noms.c), and modifying the calling order of
 the other functions.

 Description: Made changes per review comments.

 Description: Changed name of right_ch_sfb_tools_ms to pns_intensity_right

 Description: Added cast, since pVars->inputStream.usedBits is UInt, and
 pExt->remainderBits is Int.

 pExt->remainderBits =
    (Int)(pVars->inputStream.usedBits & INBUF_BIT_MODULO_MASK);

 Description: Modified to pass a pointer to scratch memory into
 tns_setup_filter.c

 Description: Removed include of "s_TNSInfo.h"

 Description: Removed call to "tns_setup_filter" which has been eliminated
 by merging its functionality into "get_tns"

 Description:  Passing in a pointer to a q-format array, rather than
 the address of a single q-format, for the inverse filter case for
 apply_tns.

 Description:
 (1) Added #include of "e_ElementId.h"
     Previously, this function was relying on another include file
     to include "e_ElementId.h"

 (2) Updated the copyright header.

 Description:
 Per review comments, declared two temporary variables

    pChLeftShare  = pChVars[LEFT]->pShareWfxpCoef;
    pChRightShare = pChVars[RIGHT]->pShareWfxpCoef;

 Description:
    long_term_synthesis should have been invoked with max_sfb
    as the 2nd parameter, rather than pFrameInfo->sfb_per_win[0].

    Old
                long_term_synthesis(
                    pChVars[ch]->wnd,
                    pFrameInfo->sfb_per_win[0] ...

    Correction
                long_term_synthesis(
                    pChVars[ch]->wnd,
                    pChVars[ch]->pShareWfxpCoef->max_sfb ...

    This problem caused long_term_synthesis to read memory which
    was not initialized in get_ics_info.c

 Description:
 (1) Utilize scratch memory for the scratch Prog_Config.

 Description: (1) Modified to decode ID_END syntactic element after header

 Description:
 (1) Reconfigured LTP buffer as a circular buffer.  This saves
     2048 Int16->Int16 copies per frame.

 Description: Updated so ltp buffers are not used as a wasteful
 intermediate buffer for LC streams.  Data is transferred directly
 from the filterbank to the output stream.

 Description: Decode ADIF header if frame count is zero.
              The AudioSpecificConfig is decoded by a separate API.

 Description: Added comments explaining how the ltp_buffer_state
 variable is updated.


 Description: Modified code to take advantage of new trans4m_freq_2_time_fxp,
 which writes the output directly into a 16-bit output buffer.  This
 improvement allows faster operation by reducing the amount of memory
 transfers.  Speed can be further improved on most platforms via use of a
 DMA transfer in the function write_output.c

 Description: perChan[] is an array of structures in tDec_Int_File. Made
              corresponding changes.

 Description: Included changes in interface for q_normalize() and
              trans4m_freq_2_time_fxp.

 Description: Included changes in interface for long_term_prediction.

 Description: Added support for DSE (Data Streaming Channel). Added
              function get_dse() and included file get_dse.h

 Description: Added support for the ill-case when a raw data block contains
              only a terminator <ID_END>. This is illegal but is added
              for convinience

 Description: Added support for empty audio frames, such the one containing
              only DSE or FILL elements. A trap was added to stop processing
              when no audio information was sent.

 Description: Added support for adts format files. Added saturation to
              floating point version of aac+ decoding

 Description:

------------------------------------------------------------------------------
 INPUT AND OUTPUT DEFINITIONS

 Inputs:
    pExt = pointer to the external interface structure. See the file
           PVMP4AudioDecoder_API.h for a description of each field.
           Data type of pointer to a tPVMP4AudioDecoderExternal
           structure.

    pMem = void pointer to hide the internal implementation of the library
           It is cast back to a tDec_Int_File structure. This structure
           contains information that needs to persist between calls to
           this function, or is too big to be placed on the stack, even
           though the data is only needed during execution of this function
           Data type void pointer, internally pointer to a tDec_Int_File
           structure.

 Local Stores/Buffers/Pointers Needed: None
           (The memory set aside in pMem performs this task)

 Global Stores/Buffers/Pointers Needed: None

 Outputs:
     status = 0                       if no error occurred
              MP4AUDEC_NONRECOVERABLE if a non-recoverable error occurred
              MP4AUDEC_RECOVERABLE    if a recoverable error occurred.
              Presently a recoverable error does not exist, but this
              was a requirement.


 Pointers and Buffers Modified:
    pMem contents are modified.
    pExt: (more detail in the file PVMP4AudioDecoder_API.h)
    inputBufferUsedLength - number of array elements used up by the stream.
    remainderBits - remaining bits in the next UInt32 buffer
    samplingRate - sampling rate in samples per sec
    bitRate - bit rate in bits per second, varies frame to frame.
    encodedChannels - channels found on the file (informative)
    frameLength - length of the frame

 Local Stores Modified: None.

 Global Stores Modified: None.

------------------------------------------------------------------------------
 FUNCTION DESCRIPTION

 Decodes one frame of an MPEG-2/MPEG-4 encoded audio bitstream.

 This function calls the various components of the decoder in the proper order.


         Left Channel                                    Right Channel
             |                                                 |
             |                                                 |
             |                                                 |
            \|/                                               \|/
 #1 ____________________                           #2 ____________________
    |                  |                              |                  |
    | Huffman Decoding |                              | Huffman Decoding |
    |__________________|                              |__________________|
             |                                                 |
             |                                                 |
             |                                                 |
            \|/                                                |
 #3 ____________________                                       |
    |                  |                                       |
    |     PNS LEFT     |                                       |
    |__________________|                                       |
             |                                                 |
             |                                                 |
             |                                                 |
            \|/                                               \|/
 #4 ______________________________________________________________________
    |                                                                    |
    |                          Apply MS_Synt                             |
    |____________________________________________________________________|
             |                                                 |
             |                                                 |
            \|/                                                |
 #5 ____________________                                       |
    |                  |                                       W
    |       LTP        |                                       A
    |__________________|                                       I
             |                                                 T
             |                                                 |
             |                                                 F
            \|/                                                O
 #6 ____________________                                       R
    |                  |                                       |
    |   Time -> Freq   |                                       L
    |__________________|                                       E
             |                                                 F
             |                                                 T
             |                                                 |
            \|/                                                C
 #7 ____________________                                       H
    |                  |                                       A
    |    TNS Inverse   |                                       N
    |__________________|                                       N
             |                                                 E
             |                                                 L
             |                                                 |
            \|/                                                |
 #8 ____________________                                       |
    |                  |                                       |
    | Long Term Synth  |                                       |
    |__________________|                                       |
             |                                                 |
             |                                                \|/
             |                                     #9 ____________________
             |                                        |                  |
             |--DATA ON LEFT CHANNEL MAY BE USED----->| PNS/Intensity Rt |
             |                                        |__________________|
             |                                                 |
             |                                                 |
             |                                                \|/
             |                                    #10 ____________________
             W                                        |                  |
             A                                        |       LTP        |
             I                                        |__________________|
             T                                                 |
             |                                                 |
             F                                                 |
             O                                                \|/
             R                                    #11 ____________________
             |                                        |                  |
             R                                        |   Time -> Freq   |
             I                                        |__________________|
             G                                                 |
             H                                                 |
             T                                                 |
             |                                                \|/
             C                                    #12 ____________________
             H                                        |                  |
             A                                        |    TNS Inverse   |
             N                                        |__________________|
             N                                                 |
             E                                                 |
             L                                                 |
             |                                                \|/
             |                                    #13 ____________________
             |                                        |                  |
             |                                        | Long Term Synth  |
             |                                        |__________________|
             |                                                 |
             |                                                 |
             |                                                 |
            \|/                                               \|/
#14 ____________________                          #18 ____________________
    |                  |                              |                  |
    |       TNS        |                              |       TNS        |
    |__________________|                              |__________________|
             |                                                 |
             |                                                 |
             |                                                 |
            \|/                                               \|/
#15 ____________________                          #19 ____________________
    |                  |                              |                  |
    |   qFormatNorm    |                              |   qFormatNorm    |
    |__________________|                              |__________________|
             |                                                 |
             |                                                 |
             |                                                 |
            \|/                                               \|/
#16 ____________________                          #20 ____________________
    |                  |                              |                  |
    |   Freq / Time    |                              |   Freq / Time    |
    |__________________|                              |__________________|
             |                                                 |
             |                                                 |
             |                                                 |
            \|/                                               \|/
#17 ____________________                          #21 ____________________
    |                  |                              |                  |
    |   Limit Buffer   |                              |   Limit Buffer   |
    |__________________|                              |__________________|
             |                                                 |
             |                                                 |
             |                                                 |
            \|/                                               \|/
#22 ______________________________________________________________________
    |                                                                    |
    |                           Write Output                             |
    |____________________________________________________________________|


------------------------------------------------------------------------------
 REQUIREMENTS

 PacketVideo Document # CCC-AUD-AAC-ERS-0003

------------------------------------------------------------------------------
 REFERENCES

 (1) MPEG-2 NBC Audio Decoder
   "This software module was originally developed by AT&T, Dolby
   Laboratories, Fraunhofer Gesellschaft IIS in the course of development
   of the MPEG-2 NBC/MPEG-4 Audio standard ISO/IEC 13818-7, 14496-1,2 and
   3. This software module is an implementation of a part of one or more
   MPEG-2 NBC/MPEG-4 Audio tools as specified by the MPEG-2 NBC/MPEG-4
   Audio standard. ISO/IEC gives users of the MPEG-2 NBC/MPEG-4 Audio
   standards free license to this software module or modifications thereof
   for use in hardware or software products claiming conformance to the
   MPEG-2 NBC/MPEG-4 Audio  standards. Those intending to use this software
   module in hardware or software products are advised that this use may
   infringe existing patents. The original developer of this software
   module and his/her company, the subsequent editors and their companies,
   and ISO/IEC have no liability for use of this software module or
   modifications thereof in an implementation. Copyright is not released
   for non MPEG-2 NBC/MPEG-4 Audio conforming products.The original
   developer retains full right to use the code for his/her own purpose,
   assign or donate the code to a third party and to inhibit third party
   from using the code for non MPEG-2 NBC/MPEG-4 Audio conforming products.
   This copyright notice must be included in all copies or derivative
   works."
   Copyright(c)1996.

------------------------------------------------------------------------------
 RESOURCES USED
   When the code is written for a specific target processor the
     the resources used should be documented below.

 STACK USAGE: [stack count for this module] + [variable to represent
          stack usage for each subroutine called]

     where: [stack usage variable] = stack usage for [subroutine
         name] (see [filename].ext)

 DATA MEMORY USED: x words

 PROGRAM MEMORY USED: x words

 CLOCK CYCLES: [cycle count equation for this module] + [variable
           used to represent cycle count for each subroutine
           called]

     where: [cycle count variable] = cycle count for [subroutine
        name] (see [filename].ext)

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


/*----------------------------------------------------------------------------
; INCLUDES
----------------------------------------------------------------------------*/
#include "pv_audio_type_defs.h"

#include "s_tdec_int_chan.h"
#include "s_tdec_int_file.h"
#include "aac_mem_funcs.h"
#include "sfb.h"                   /* Where samp_rate_info[] is declared */
#include "e_tmp4audioobjecttype.h"
#include "e_elementid.h"


#include "get_adif_header.h"
#include "get_adts_header.h"
#include "get_audio_specific_config.h"
#include "ibstream.h"           /* where getbits is declared */

#include "huffman.h"            /* where huffdecode is declared */
#include "get_prog_config.h"
#include "getfill.h"
#include "pns_left.h"

#include "apply_ms_synt.h"
#include "pns_intensity_right.h"
#include "q_normalize.h"
#include "long_term_prediction.h"
#include "long_term_synthesis.h"
#include "ltp_common_internal.h"
#include "apply_tns.h"

#include "window_block_fxp.h"

#include "write_output.h"

#include "pvmp4audiodecoder_api.h"   /* Where this function is declared */
#include "get_dse.h"

#include "sbr_applied.h"
#include "sbr_open.h"
#include "get_sbr_bitstream.h"
#include "e_sbr_element_id.h"



/*----------------------------------------------------------------------------
; MACROS
; Define module specific macros here
----------------------------------------------------------------------------*/

/*----------------------------------------------------------------------------
; DEFINES
; Include all pre-processor statements here. Include conditional
; compile variables also.
----------------------------------------------------------------------------*/

#define LEFT (0)
#define RIGHT (1)


/*----------------------------------------------------------------------------
; LOCAL FUNCTION DEFINITIONS
; Function Prototype declaration
----------------------------------------------------------------------------*/

/*----------------------------------------------------------------------------
; LOCAL STORE/BUFFER/POINTER DEFINITIONS
; Variable declaration - defined here and used outside this module
----------------------------------------------------------------------------*/

/*----------------------------------------------------------------------------
; EXTERNAL FUNCTION REFERENCES
; Declare functions defined elsewhere and referenced in this module
----------------------------------------------------------------------------*/

void InitSbrSynFilterbank(bool bDownSampleSBR);



/*----------------------------------------------------------------------------
; EXTERNAL GLOBAL STORE/BUFFER/POINTER REFERENCES
; Declare variables used in this module but defined elsewhere
----------------------------------------------------------------------------*/

/*----------------------------------------------------------------------------
; FUNCTION CODE
----------------------------------------------------------------------------*/


OSCL_EXPORT_REF Int PVMP4AudioDecodeFrame(
    tPVMP4AudioDecoderExternal  *pExt,
    void                        *pMem)
{
    Int            frameLength;      /* Helper variable */
    Int            ch;
    Int            id_syn_ele;
    UInt           initialUsedBits;  /* Unsigned for C55x */
    Int            qFormatNorm;
    Int            qPredictedSamples;
    Bool           leaveGetLoop;
    MC_Info       *pMC_Info;        /* Helper pointer */
    FrameInfo     *pFrameInfo;      /* Helper pointer */
    tDec_Int_File *pVars;           /* Helper pointer */
    tDec_Int_Chan *pChVars[Chans];  /* Helper pointer */

    per_chan_share_w_fxpCoef *pChLeftShare;  /* Helper pointer */
    per_chan_share_w_fxpCoef *pChRightShare; /* Helper pointer */

    Int            status = MP4AUDEC_SUCCESS;


    Bool empty_frame;

#ifdef AAC_PLUS

    SBRDECODER_DATA *sbrDecoderData;
    SBR_DEC         *sbrDec;
    SBRBITSTREAM    *sbrBitStream;

#endif
    /*
     * Initialize "helper" pointers to existing memory.
     */
    pVars = (tDec_Int_File *)pMem;

    pMC_Info = &pVars->mc_info;

    pChVars[LEFT]  = &pVars->perChan[LEFT];
    pChVars[RIGHT] = &pVars->perChan[RIGHT];

    pChLeftShare = pChVars[LEFT]->pShareWfxpCoef;
    pChRightShare = pChVars[RIGHT]->pShareWfxpCoef;


#ifdef AAC_PLUS

    sbrDecoderData = (SBRDECODER_DATA *) & pVars->sbrDecoderData;
    sbrDec         = (SBR_DEC *) & pVars->sbrDec;
    sbrBitStream   = (SBRBITSTREAM *) & pVars->sbrBitStr;

#ifdef PARAMETRICSTEREO
    sbrDecoderData->hParametricStereoDec = (HANDLE_PS_DEC) & pVars->sbrDecoderData.ParametricStereoDec;
#endif

#endif
    /*
     * Translate input buffer variables.
     */
    pVars->inputStream.pBuffer = pExt->pInputBuffer;

    pVars->inputStream.inputBufferCurrentLength = (UInt)pExt->inputBufferCurrentLength;

    pVars->inputStream.availableBits =
        (UInt)(pExt->inputBufferCurrentLength << INBUF_ARRAY_INDEX_SHIFT);

    initialUsedBits =
        (UInt)((pExt->inputBufferUsedLength << INBUF_ARRAY_INDEX_SHIFT) +
               pExt->remainderBits);

    pVars->inputStream.usedBits = initialUsedBits;

    if (initialUsedBits > pVars->inputStream.availableBits)
    {
        status = MP4AUDEC_INVALID_FRAME;
    }
    else if (pVars->bno == 0)
    {
        /*
         * Attempt to read in ADIF format first because it is easily identified.
         * If its not an ADIF bitstream, get_adif_header rewinds the "pointer"
         * (actually usedBits).
         */
        status =
            get_adif_header(
                pVars,
                &(pVars->scratch.scratch_prog_config));

        byte_align(&pVars->inputStream);

        if (status == SUCCESS)
        {
            pVars->prog_config.file_is_adts = FALSE;
        }
        else  /* we've tried simple audio config, adif, then it should be adts */
        {
            pVars->prog_config.file_is_adts = TRUE;
        }
    }
    else if ((pVars->bno == 1) && (pVars->prog_config.file_is_adts == FALSE))
    {

        /*
         * There might be an ID_END element following immediately after the
         * AudioSpecificConfig header. This syntactic element should be read
         * and byte_aligned before proceeds to decode "real" AAC raw data.
         */
        id_syn_ele = (Int)getbits(LEN_SE_ID, &pVars->inputStream) ;

        if (id_syn_ele == ID_END)
        {

            byte_align(&pVars->inputStream);

            pExt->inputBufferUsedLength =
                pVars->inputStream.usedBits >> INBUF_ARRAY_INDEX_SHIFT;

            pExt->remainderBits = pVars->inputStream.usedBits & INBUF_BIT_MODULO_MASK;

            pVars->bno++;

            return(status);
        }
        else
        {
            /*
             * Rewind bitstream pointer so that the syntactic element can be
             * read when decoding raw bitstream
             */
            pVars->inputStream.usedBits -= LEN_SE_ID;
        }

    }

    if (pVars->prog_config.file_is_adts == TRUE)
    {
        /*
         *  If file is adts format, let the decoder handle only on data raw
         *  block at the time, once the last (or only) data block has been
         *  processed, then synch on the next header
         */
        if (pVars->prog_config.headerless_frames)
        {
            pVars->prog_config.headerless_frames--;  /* raw data block counter  */
        }
        else
        {
            status =  get_adts_header(pVars,
                                      &(pVars->syncword),
                                      &(pVars->invoke),
                                      3);     /*   CorrectlyReadFramesCount  */

            if (status != SUCCESS)
            {
                status = MP4AUDEC_LOST_FRAME_SYNC;    /*  we lost track of header */
            }
        }
    }
    else
    {
        byte_align(&pVars->inputStream);
    }

#ifdef AAC_PLUS
    sbrBitStream->NrElements = 0;
    sbrBitStream->NrElementsCore = 0;

#endif

    /*
     * The variable leaveGetLoop is used to signal that the following
     * loop can be left, which retrieves audio syntatic elements until
     * an ID_END is found, or an error occurs.
     */
    leaveGetLoop = FALSE;
    empty_frame  = TRUE;

    while ((leaveGetLoop == FALSE) && (status == SUCCESS))
    {
        /* get audio syntactic element */
        id_syn_ele = (Int)get9_n_lessbits(LEN_SE_ID, &pVars->inputStream);

        /*
         *  As fractional frames are a possible input, check that parsing does not
         *  go beyond the available bits before parsing the syntax.
         */
        if (pVars->inputStream.usedBits > pVars->inputStream.availableBits)
        {
            status = MP4AUDEC_INCOMPLETE_FRAME; /* possible EOF or fractional frame */
            id_syn_ele = ID_END;           /* quit while-loop */
        }

        switch (id_syn_ele)
        {
            case ID_END:        /* terminator field */
                leaveGetLoop = TRUE;
                break;

            case ID_SCE:        /* single channel */
            case ID_CPE:        /* channel pair */
                empty_frame = FALSE;
                status =
                    huffdecode(
                        id_syn_ele,
                        &(pVars->inputStream),
                        pVars,
                        pChVars);

#ifdef AAC_PLUS
                if (id_syn_ele == ID_SCE)
                {
                    sbrBitStream->sbrElement[sbrBitStream->NrElements].ElementID = SBR_ID_SCE;
                }
                else if (id_syn_ele == ID_CPE)
                {
                    sbrBitStream->sbrElement[sbrBitStream->NrElements].ElementID = SBR_ID_CPE;
                }
                sbrBitStream->NrElementsCore++;


#endif

                break;

            case ID_PCE:        /* program config element */
                /*
                 * PCE are not accepted in the middle of a
                 * raw_data_block. If found, a possible error may happen
                 * If a PCE is encountered during the first 2 frames,
                 * it will be read and accepted
                 * if its tag matches the first, with no error checking
                 * (inside of get_prog_config)
                 */

                if (pVars->bno <= 1)
                {
                    status = get_prog_config(pVars,
                                             &(pVars->scratch.scratch_prog_config));
                }
                else
                {
                    status = MP4AUDEC_INVALID_FRAME;
                }
                break;

            case ID_FIL:        /* fill element */
#ifdef AAC_PLUS
                get_sbr_bitstream(sbrBitStream, &pVars->inputStream);

#else
                getfill(&pVars->inputStream);
#endif

                break;

            case ID_DSE:       /* Data Streaming element */
                get_dse(pVars->share.data_stream_bytes,
                        &pVars->inputStream);
                break;

            default: /* Unsupported element, including ID_LFE */
                status = -1;  /* ERROR CODE needs to be updated */
                break;

        } /* end switch() */

    } /* end while() */

    byte_align(&pVars->inputStream);

    /*
     *   After parsing the first frame ( bno=0 (adif), bno=1 (raw))
     *   verify if implicit signalling is forcing to upsample AAC with
     *   no AAC+/eAAC+ content. If so, disable upsampling
     */

#ifdef AAC_PLUS
    if (pVars->bno <= 1)
    {
        if ((pVars->mc_info.ExtendedAudioObjectType == MP4AUDIO_AAC_LC) &&
                (!sbrBitStream->NrElements))
        {
            PVMP4AudioDecoderDisableAacPlus(pExt, pMem);
        }
    }
#endif

    /*
     *   There might be an empty raw data block with only a
     *   ID_END element or non audio ID_DSE, ID_FIL
     *   This is an "illegal" condition but this trap
     *   avoids any further processing
     */

    if (empty_frame == TRUE)
    {
        pExt->inputBufferUsedLength =
            pVars->inputStream.usedBits >> INBUF_ARRAY_INDEX_SHIFT;

        pExt->remainderBits = pVars->inputStream.usedBits & INBUF_BIT_MODULO_MASK;

        pVars->bno++;

        return(status);

    }

#ifdef AAC_PLUS

    if (sbrBitStream->NrElements)
    {
        /* for every core SCE or CPE there must be an SBR element, otherwise sths. wrong */
        if (sbrBitStream->NrElements != sbrBitStream->NrElementsCore)
        {
            status = MP4AUDEC_INVALID_FRAME;
        }

        if (pExt->aacPlusEnabled == false)
        {
            sbrBitStream->NrElements = 0;   /* disable aac processing  */
        }
    }
    else
    {
        /*
         *  This is AAC, but if aac+/eaac+ was declared in the stream, and there is not sbr content
         *  something is wrong
         */
        if (pMC_Info->sbrPresentFlag || pMC_Info->psPresentFlag)
        {
            status = MP4AUDEC_INVALID_FRAME;
        }
    }
#endif




    /*
     * Signal processing section.
     */
    frameLength = pVars->frameLength;

    if (status == SUCCESS)
    {
        /*
         *   PNS and INTENSITY STEREO and MS
         */

        pFrameInfo = pVars->winmap[pChVars[LEFT]->wnd];

        pns_left(
            pFrameInfo,
            pChLeftShare->group,
            pChLeftShare->cb_map,
            pChLeftShare->factors,
            pChLeftShare->lt_status.sfb_prediction_used,
            pChLeftShare->lt_status.ltp_data_present,
            pChVars[LEFT]->fxpCoef,
            pChLeftShare->qFormat,
            &(pVars->pns_cur_noise_state));

        /*
         * apply_ms_synt can only be ran for common windows.
         * (where both the left and right channel share the
         * same grouping, window length, etc.
         *
         * pVars->hasmask will be > 0 only if
         * common windows are enabled for this frame.
         */

        if (pVars->hasmask > 0)
        {
            apply_ms_synt(
                pFrameInfo,
                pChLeftShare->group,
                pVars->mask,
                pChLeftShare->cb_map,
                pChVars[LEFT]->fxpCoef,
                pChVars[RIGHT]->fxpCoef,
                pChLeftShare->qFormat,
                pChRightShare->qFormat);
        }

        for (ch = 0; (ch < pMC_Info->nch); ch++)
        {
            pFrameInfo = pVars->winmap[pChVars[ch]->wnd];

            /*
             * Note: This MP4 library assumes that if there are two channels,
             * then the second channel is right AND it was a coupled channel,
             * therefore there is no need to check the "is_cpe" flag.
             */

            if (ch > 0)
            {
                pns_intensity_right(
                    pVars->hasmask,
                    pFrameInfo,
                    pChRightShare->group,
                    pVars->mask,
                    pChRightShare->cb_map,
                    pChLeftShare->factors,
                    pChRightShare->factors,
                    pChRightShare->lt_status.sfb_prediction_used,
                    pChRightShare->lt_status.ltp_data_present,
                    pChVars[LEFT]->fxpCoef,
                    pChVars[RIGHT]->fxpCoef,
                    pChLeftShare->qFormat,
                    pChRightShare->qFormat,
                    &(pVars->pns_cur_noise_state));
            }

            if (pChVars[ch]->pShareWfxpCoef->lt_status.ltp_data_present != FALSE)
            {
                /*
                 * LTP - Long Term Prediction
                 */

                qPredictedSamples = long_term_prediction(
                                        pChVars[ch]->wnd,
                                        pChVars[ch]->pShareWfxpCoef->lt_status.
                                        weight_index,
                                        pChVars[ch]->pShareWfxpCoef->lt_status.
                                        delay,
                                        pChVars[ch]->ltp_buffer,
                                        pVars->ltp_buffer_state,
                                        pChVars[ch]->time_quant,
                                        pVars->share.predictedSamples,      /* Scratch */
                                        frameLength);

                trans4m_time_2_freq_fxp(
                    pVars->share.predictedSamples,
                    pChVars[ch]->wnd,
                    pChVars[ch]->wnd_shape_prev_bk,
                    pChVars[ch]->wnd_shape_this_bk,
                    &qPredictedSamples,
                    pVars->scratch.fft);   /* scratch memory for FFT */


                /*
                 * To solve a potential problem where a pointer tied to
                 * the qFormat was being incremented, a pointer to
                 * pChVars[ch]->qFormat is passed in here rather than
                 * the address of qPredictedSamples.
                 *
                 * Neither values are actually needed in the case of
                 * inverse filtering, but the pointer was being
                 * passed (and incremented) regardless.
                 *
                 * So, the solution is to pass a space of memory
                 * that a pointer can happily point to.
                 */

                /* This is the inverse filter */
                apply_tns(
                    pVars->share.predictedSamples,  /* scratch re-used for each ch */
                    pChVars[ch]->pShareWfxpCoef->qFormat,     /* Not used by the inv_filter */
                    pFrameInfo,
                    &(pChVars[ch]->pShareWfxpCoef->tns),
                    TRUE,                       /* TRUE is FIR */
                    pVars->scratch.tns_inv_filter);

                /*
                 * For the next function long_term_synthesis,
                 * the third param win_sfb_top[], and
                 * the tenth param coef_per_win,
                 * are used differently that in the rest of the project. This
                 * is because originally the ISO code was going to have
                 * these parameters change as the "short window" changed.
                 * These are all now the same value for each of the eight
                 * windows.  This is why there is a [0] at the
                 * end of each of theses parameters.
                 * Note in particular that win_sfb_top was originally an
                 * array of pointers to arrays, but inside long_term_synthesis
                 * it is now a simple array.
                 * When the rest of the project functions are changed, the
                 * structure FrameInfo changes, and the [0]'s are removed,
                 * this comment could go away.
                 */
                long_term_synthesis(
                    pChVars[ch]->wnd,
                    pChVars[ch]->pShareWfxpCoef->max_sfb,
                    pFrameInfo->win_sfb_top[0], /* Look above */
                    pChVars[ch]->pShareWfxpCoef->lt_status.win_prediction_used,
                    pChVars[ch]->pShareWfxpCoef->lt_status.sfb_prediction_used,
                    pChVars[ch]->fxpCoef,   /* input and output */
                    pChVars[ch]->pShareWfxpCoef->qFormat,   /* input and output */
                    pVars->share.predictedSamples,
                    qPredictedSamples,       /* q format for previous aray */
                    pFrameInfo->coef_per_win[0], /* Look above */
                    NUM_SHORT_WINDOWS,
                    NUM_RECONSTRUCTED_SFB);

            } /* end if (pChVars[ch]->lt_status.ltp_data_present != FALSE) */

        } /* for(ch) */

        for (ch = 0; (ch < pMC_Info->nch); ch++)
        {

            pFrameInfo = pVars->winmap[pChVars[ch]->wnd];

            /*
             * TNS - Temporal Noise Shaping
             */

            /* This is the forward filter
             *
             * A special note:  Scratch memory is not used by
             * the forward filter, but is passed in to maintain
             * common interface for inverse and forward filter
             */
            apply_tns(
                pChVars[ch]->fxpCoef,
                pChVars[ch]->pShareWfxpCoef->qFormat,
                pFrameInfo,
                &(pChVars[ch]->pShareWfxpCoef->tns),
                FALSE,                   /* FALSE is IIR */
                pVars->scratch.tns_inv_filter);

            /*
             * Normalize the q format across all scale factor bands
             * to one value.
             */
            qFormatNorm =
                q_normalize(
                    pChVars[ch]->pShareWfxpCoef->qFormat,
                    pFrameInfo,
                    pChVars[ch]->abs_max_per_window,
                    pChVars[ch]->fxpCoef);

            /*
             *  filterbank - converts frequency coeficients to time domain.
             */

#ifdef AAC_PLUS
            if (sbrBitStream->NrElements == 0 && pMC_Info->upsamplingFactor == 1)
            {
                trans4m_freq_2_time_fxp_2(
                    pChVars[ch]->fxpCoef,
                    pChVars[ch]->time_quant,
                    pChVars[ch]->wnd,   /* window sequence */
                    pChVars[ch]->wnd_shape_prev_bk,
                    pChVars[ch]->wnd_shape_this_bk,
                    qFormatNorm,
                    pChVars[ch]->abs_max_per_window,
                    pVars->scratch.fft,
                    &pExt->pOutputBuffer[ch]);
                /*
                 *  Update LTP buffers if needed
                 */

                if (pVars->mc_info.audioObjectType == MP4AUDIO_LTP)
                {
                    Int16 * pt = &pExt->pOutputBuffer[ch];
                    Int16 * ptr = &(pChVars[ch]->ltp_buffer[pVars->ltp_buffer_state]);
                    Int16  x, y;
                    for (Int32 i = HALF_LONG_WINDOW; i != 0; i--)
                    {
                        x = *pt;
                        pt += 2;
                        y = *pt;
                        pt += 2;
                        *(ptr++) =  x;
                        *(ptr++) =  y;
                    }
                }
            }
            else
            {
                trans4m_freq_2_time_fxp_1(
                    pChVars[ch]->fxpCoef,
                    pChVars[ch]->time_quant,
                    &(pChVars[ch]->ltp_buffer[pVars->ltp_buffer_state + 288]),
                    pChVars[ch]->wnd,   /* window sequence */
                    pChVars[ch]->wnd_shape_prev_bk,
                    pChVars[ch]->wnd_shape_this_bk,
                    qFormatNorm,
                    pChVars[ch]->abs_max_per_window,
                    pVars->scratch.fft);

            }
#else

            trans4m_freq_2_time_fxp_2(
                pChVars[ch]->fxpCoef,
                pChVars[ch]->time_quant,
                pChVars[ch]->wnd,   /* window sequence */
                pChVars[ch]->wnd_shape_prev_bk,
                pChVars[ch]->wnd_shape_this_bk,
                qFormatNorm,
                pChVars[ch]->abs_max_per_window,
                pVars->scratch.fft,
                &pExt->pOutputBuffer[ch]);
            /*
             *  Update LTP buffers only if needed
             */

            if (pVars->mc_info.audioObjectType == MP4AUDIO_LTP)
            {
                Int16 * pt = &pExt->pOutputBuffer[ch];
                Int16 * ptr = &(pChVars[ch]->ltp_buffer[pVars->ltp_buffer_state]);
                Int16  x, y;
                for (Int32 i = HALF_LONG_WINDOW; i != 0; i--)
                {
                    x = *pt;
                    pt += 2;
                    y = *pt;
                    pt += 2;
                    *(ptr++) =  x;
                    *(ptr++) =  y;
                }

            }


#endif


            /* Update the window shape */
            pChVars[ch]->wnd_shape_prev_bk = pChVars[ch]->wnd_shape_this_bk;

        } /* end for() */


        /*
         * Copy to the final output buffer, taking into account the desired
         * channels from the calling environment, the actual channels, and
         * whether the data should be interleaved or not.
         *
         * If the stream had only one channel, write_output will not use
         * the right channel data.
         *
         */


        /* CONSIDER USE OF DMA OPTIMIZATIONS WITHIN THE write_output FUNCTION.
         *
         * It is presumed that the ltp_buffer will reside in internal (fast)
         * memory, while the pExt->pOutputBuffer will reside in external
         * (slow) memory.
         *
         */


#ifdef AAC_PLUS

        if (sbrBitStream->NrElements || pMC_Info->upsamplingFactor == 2)
        {

            if (pVars->bno <= 1)   /* allows console to operate with ADIF and audio config */
            {
                if (sbrDec->outSampleRate == 0) /* do it only once (disregarding of signaling type) */
                {
                    sbr_open(samp_rate_info[pVars->mc_info.sampling_rate_idx].samp_rate,
                             sbrDec,
                             sbrDecoderData,
                             pVars->mc_info.bDownSampledSbr);
                }

            }
            pMC_Info->upsamplingFactor =
                sbrDecoderData->SbrChannel[0].frameData.sbr_header.sampleRateMode;


            /* reuse right aac spectrum channel  */
            {
                Int16 *pt_left  =  &(pChVars[LEFT ]->ltp_buffer[pVars->ltp_buffer_state]);
                Int16 *pt_right =  &(pChVars[RIGHT]->ltp_buffer[pVars->ltp_buffer_state]);

                if (sbr_applied(sbrDecoderData,
                                sbrBitStream,
                                pt_left,
                                pt_right,
                                pExt->pOutputBuffer,
                                sbrDec,
                                pVars,
                                pMC_Info->nch) != SBRDEC_OK)
                {
                    status = MP4AUDEC_INVALID_FRAME;
                }
            }


        }  /*  if( pExt->aacPlusEnabled == FALSE) */
#endif

        /*
         * Copied mono data in both channels or just leave it as mono,
         * according with desiredChannels (default is 2)
         */

        if (pExt->desiredChannels == 2)
        {

#if defined(AAC_PLUS)
#if defined(PARAMETRICSTEREO)&&defined(HQ_SBR)
            if (pMC_Info->nch != 2 && pMC_Info->psPresentFlag != 1)
#else
            if (pMC_Info->nch != 2)
#endif
#else
            if (pMC_Info->nch != 2)
#endif
            {
                /* mono */


                Int16 * pt  = &pExt->pOutputBuffer[0];
                Int16 * pt2 = &pExt->pOutputBuffer[1];
                Int i;
                if (pMC_Info->upsamplingFactor == 2)
                {
                    for (i = 0; i < 1024; i++)
                    {
                        *pt2 = *pt;
                        pt += 2;
                        pt2 += 2;
                    }
                    pt  = &pExt->pOutputBuffer_plus[0];
                    pt2 = &pExt->pOutputBuffer_plus[1];

                    for (i = 0; i < 1024; i++)
                    {
                        *pt2 = *pt;
                        pt += 2;
                        pt2 += 2;
                    }
                }
                else
                {
                    for (i = 0; i < 1024; i++)
                    {
                        *pt2 = *pt;
                        pt += 2;
                        pt2 += 2;
                    }
                }

            }

#if defined(AAC_PLUS)
#if defined(PARAMETRICSTEREO)&&defined(HQ_SBR)

            else if (pMC_Info->psPresentFlag == 1)
            {
                Int32 frameSize = 0;
                if (pExt->aacPlusEnabled == false)
                {
                    /*
                     *  Decoding eaac+ when only aac is enabled, copy L into R
                     */
                    frameSize = 1024;
                }
                else if (sbrDecoderData->SbrChannel[0].syncState != SBR_ACTIVE)
                {
                    /*
                     *  Decoding eaac+ when no PS data was found, copy upsampled L into R
                     */
                    frameSize = 2048;
                }

                Int16 * pt  = &pExt->pOutputBuffer[0];
                Int16 * pt2 = &pExt->pOutputBuffer[1];
                Int i;
                for (i = 0; i < frameSize; i++)
                {
                    *pt2 = *pt;
                    pt += 2;
                    pt2 += 2;
                }
            }
#endif
#endif

        }
        else
        {

#if defined(AAC_PLUS)
#if defined(PARAMETRICSTEREO)&&defined(HQ_SBR)
            if (pMC_Info->nch != 2 && pMC_Info->psPresentFlag != 1)
#else
            if (pMC_Info->nch != 2)
#endif
#else
            if (pMC_Info->nch != 2)
#endif
            {
                /* mono */
                Int16 * pt  = &pExt->pOutputBuffer[0];
                Int16 * pt2 = &pExt->pOutputBuffer[0];
                Int i;

                if (pMC_Info->upsamplingFactor == 2)
                {
                    for (i = 0; i < 1024; i++)
                    {
                        *pt2++ = *pt;
                        pt += 2;
                    }

                    pt  = &pExt->pOutputBuffer_plus[0];
                    pt2 = &pExt->pOutputBuffer_plus[0];

                    for (i = 0; i < 1024; i++)
                    {
                        *pt2++ = *pt;
                        pt += 2;
                    }
                }
                else
                {
                    for (i = 0; i < 1024; i++)
                    {
                        *pt2++ = *pt;
                        pt += 2;
                    }
                }

            }

        }




        /* pVars->ltp_buffer_state cycles between 0 and 1024.  The value
         * indicates the location of the data corresponding to t == -2.
         *
         * | t == -2 | t == -1 |  pVars->ltp_buffer_state == 0
         *
         * | t == -1 | t == -2 |  pVars->ltp_buffer_state == 1024
         *
         */

#ifdef AAC_PLUS
        if (sbrBitStream->NrElements == 0 && pMC_Info->upsamplingFactor == 1)
        {
            pVars->ltp_buffer_state ^= frameLength;
        }
        else
        {
            pVars->ltp_buffer_state ^= (frameLength + 288);
        }
#else
        pVars->ltp_buffer_state ^= frameLength;
#endif


        if (pVars->bno <= 1)
        {
            /*
             * to set these values only during the second call
             * when they change.
             */
            pExt->samplingRate =
                samp_rate_info[pVars->mc_info.sampling_rate_idx].samp_rate;

            pVars->mc_info.implicit_channeling = 0; /* disable flag, as this is allowed
                                                      * only the first time
                                                      */


#ifdef AAC_PLUS

            if (pMC_Info->upsamplingFactor == 2)
            {
                pExt->samplingRate *= pMC_Info->upsamplingFactor;
                pExt->aacPlusUpsamplingFactor = pMC_Info->upsamplingFactor;
            }

#endif

            pExt->extendedAudioObjectType = pMC_Info->ExtendedAudioObjectType;
            pExt->audioObjectType = pMC_Info->audioObjectType;

            pExt->encodedChannels = pMC_Info->nch;
            pExt->frameLength = pVars->frameLength;
        }

        pVars->bno++;


        /*
         * Using unit analysis, the bitrate is a function of the sampling rate, bits,
         * points in a frame
         *
         *     bits        samples                frame
         *     ----  =    --------- *  bits  *   -------
         *     sec           sec                  sample
         *
         * To save a divide, a shift is used. Presently only the value of
         * 1024 is used by this library, so make it the most accurate for that
         * value. This may need to be updated later.
         */

        pExt->bitRate = (pExt->samplingRate *
                         (pVars->inputStream.usedBits - initialUsedBits)) >> 10;  /*  LONG_WINDOW  1024 */

        pExt->bitRate >>= (pMC_Info->upsamplingFactor - 1);


    } /* end if (status == SUCCESS) */


    if (status != MP4AUDEC_SUCCESS)
    {
        /*
         *  A non-SUCCESS decoding could be due to an error on the bitstream or
         *  an incomplete frame. As access to the bitstream beyond frame boundaries
         *  are not allowed, in those cases the bitstream reading routine return a 0
         *  Zero values guarantees that the data structures are filled in with values
         *  that eventually will signal an error (like invalid parameters) or that allow
         *  completion of the parsing routine. Either way, the partial frame condition
         *  is verified at this time.
         */
        if (pVars->prog_config.file_is_adts == TRUE)
        {
            status = MP4AUDEC_LOST_FRAME_SYNC;
            pVars->prog_config.headerless_frames = 0; /* synchronization forced */
        }
        else
        {
            /*
             *  Check if the decoding error was due to buffer overrun, if it was,
             *  update status
             */
            if (pVars->inputStream.usedBits > pVars->inputStream.availableBits)
            {
                /* all bits were used but were not enough to complete decoding */
                pVars->inputStream.usedBits = pVars->inputStream.availableBits;

                status = MP4AUDEC_INCOMPLETE_FRAME; /* possible EOF or fractional frame */
            }
        }
    }

    /*
     * Translate from units of bits back into units of words.
     */

    pExt->inputBufferUsedLength =
        pVars->inputStream.usedBits >> INBUF_ARRAY_INDEX_SHIFT;

    pExt->remainderBits = (Int)(pVars->inputStream.usedBits & INBUF_BIT_MODULO_MASK);



    return (status);

} /* PVMP4AudioDecoderDecodeFrame */