1 /* ------------------------------------------------------------------ 2 * Copyright (C) 1998-2009 PacketVideo 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either 13 * express or implied. 14 * See the License for the specific language governing permissions 15 * and limitations under the License. 16 * ------------------------------------------------------------------- 17 */ 18 /* 19 20 Pathname: pvmp4audiodecodeframe 21 22 ------------------------------------------------------------------------------ 23 REVISION HISTORY 24 25 Description: Modified from original shareware code 26 27 Description: Pulled in loop structure from console.c, so that this function 28 now decodes all frames in the file. 29 30 Original program used several global variables. These have been 31 eliminated, except for situations in which the global variables 32 could be converted into const types. Otherwise, they are passed 33 by reference through the functions. 34 35 Description: Begin mods for file I/O removal 36 37 Description: Merged trans4m_freq_2_time, trans4m_time_2_freq, etc. 38 39 Description: Removing commented out sections of code. This includes the 40 removal of unneeded functions init_lt_pred, reset_mc_info, 41 42 Description: Copied from aac_decode_frame.c and renamed file, 43 Made many changes. 44 45 Description: Prepare for code review 46 47 Description: Update per review comments: 48 1) Add comment about leaveGetLoop 49 2) Remove inverseTNSCoef array 50 3) fix wnd_shape_this_bk to wnd_shape_prev_bk in F to T 51 4) Clean up comments 52 5) Change call to long_term_synthesis 53 54 Description: Remove division for calculation of bitrate. 55 56 Description: Remove update of LTP buffers if not LTP audio object type. 57 58 Description: Add hasmask to call to right_ch_sfb_tools_ms 59 60 Description: 61 Modified to call ltp related routines on the left channel 62 before intensity is called on the right channel. The previous version 63 was causing a problem when IS was used on the right channel and LTP 64 on the left channel for the same scalefactor band. 65 66 This fix required creating a new function, apply_ms_synt, deleting another 67 function (right_ch_sfb_tools_noms.c), and modifying the calling order of 68 the other functions. 69 70 Description: Made changes per review comments. 71 72 Description: Changed name of right_ch_sfb_tools_ms to pns_intensity_right 73 74 Description: Added cast, since pVars->inputStream.usedBits is UInt, and 75 pExt->remainderBits is Int. 76 77 pExt->remainderBits = 78 (Int)(pVars->inputStream.usedBits & INBUF_BIT_MODULO_MASK); 79 80 Description: Modified to pass a pointer to scratch memory into 81 tns_setup_filter.c 82 83 Description: Removed include of "s_TNSInfo.h" 84 85 Description: Removed call to "tns_setup_filter" which has been eliminated 86 by merging its functionality into "get_tns" 87 88 Description: Passing in a pointer to a q-format array, rather than 89 the address of a single q-format, for the inverse filter case for 90 apply_tns. 91 92 Description: 93 (1) Added #include of "e_ElementId.h" 94 Previously, this function was relying on another include file 95 to include "e_ElementId.h" 96 97 (2) Updated the copyright header. 98 99 Description: 100 Per review comments, declared two temporary variables 101 102 pChLeftShare = pChVars[LEFT]->pShareWfxpCoef; 103 pChRightShare = pChVars[RIGHT]->pShareWfxpCoef; 104 105 Description: 106 long_term_synthesis should have been invoked with max_sfb 107 as the 2nd parameter, rather than pFrameInfo->sfb_per_win[0]. 108 109 Old 110 long_term_synthesis( 111 pChVars[ch]->wnd, 112 pFrameInfo->sfb_per_win[0] ... 113 114 Correction 115 long_term_synthesis( 116 pChVars[ch]->wnd, 117 pChVars[ch]->pShareWfxpCoef->max_sfb ... 118 119 This problem caused long_term_synthesis to read memory which 120 was not initialized in get_ics_info.c 121 122 Description: 123 (1) Utilize scratch memory for the scratch Prog_Config. 124 125 Description: (1) Modified to decode ID_END syntactic element after header 126 127 Description: 128 (1) Reconfigured LTP buffer as a circular buffer. This saves 129 2048 Int16->Int16 copies per frame. 130 131 Description: Updated so ltp buffers are not used as a wasteful 132 intermediate buffer for LC streams. Data is transferred directly 133 from the filterbank to the output stream. 134 135 Description: Decode ADIF header if frame count is zero. 136 The AudioSpecificConfig is decoded by a separate API. 137 138 Description: Added comments explaining how the ltp_buffer_state 139 variable is updated. 140 141 142 Description: Modified code to take advantage of new trans4m_freq_2_time_fxp, 143 which writes the output directly into a 16-bit output buffer. This 144 improvement allows faster operation by reducing the amount of memory 145 transfers. Speed can be further improved on most platforms via use of a 146 DMA transfer in the function write_output.c 147 148 Description: perChan[] is an array of structures in tDec_Int_File. Made 149 corresponding changes. 150 151 Description: Included changes in interface for q_normalize() and 152 trans4m_freq_2_time_fxp. 153 154 Description: Included changes in interface for long_term_prediction. 155 156 Description: Added support for DSE (Data Streaming Channel). Added 157 function get_dse() and included file get_dse.h 158 159 Description: Added support for the ill-case when a raw data block contains 160 only a terminator <ID_END>. This is illegal but is added 161 for convinience 162 163 Description: Added support for empty audio frames, such the one containing 164 only DSE or FILL elements. A trap was added to stop processing 165 when no audio information was sent. 166 167 Description: Added support for adts format files. Added saturation to 168 floating point version of aac+ decoding 169 170 Description: 171 172 ------------------------------------------------------------------------------ 173 INPUT AND OUTPUT DEFINITIONS 174 175 Inputs: 176 pExt = pointer to the external interface structure. See the file 177 PVMP4AudioDecoder_API.h for a description of each field. 178 Data type of pointer to a tPVMP4AudioDecoderExternal 179 structure. 180 181 pMem = void pointer to hide the internal implementation of the library 182 It is cast back to a tDec_Int_File structure. This structure 183 contains information that needs to persist between calls to 184 this function, or is too big to be placed on the stack, even 185 though the data is only needed during execution of this function 186 Data type void pointer, internally pointer to a tDec_Int_File 187 structure. 188 189 Local Stores/Buffers/Pointers Needed: None 190 (The memory set aside in pMem performs this task) 191 192 Global Stores/Buffers/Pointers Needed: None 193 194 Outputs: 195 status = 0 if no error occurred 196 MP4AUDEC_NONRECOVERABLE if a non-recoverable error occurred 197 MP4AUDEC_RECOVERABLE if a recoverable error occurred. 198 Presently a recoverable error does not exist, but this 199 was a requirement. 200 201 202 Pointers and Buffers Modified: 203 pMem contents are modified. 204 pExt: (more detail in the file PVMP4AudioDecoder_API.h) 205 inputBufferUsedLength - number of array elements used up by the stream. 206 remainderBits - remaining bits in the next UInt32 buffer 207 samplingRate - sampling rate in samples per sec 208 bitRate - bit rate in bits per second, varies frame to frame. 209 encodedChannels - channels found on the file (informative) 210 frameLength - length of the frame 211 212 Local Stores Modified: None. 213 214 Global Stores Modified: None. 215 216 ------------------------------------------------------------------------------ 217 FUNCTION DESCRIPTION 218 219 Decodes one frame of an MPEG-2/MPEG-4 encoded audio bitstream. 220 221 This function calls the various components of the decoder in the proper order. 222 223 224 Left Channel Right Channel 225 | | 226 | | 227 | | 228 \|/ \|/ 229 #1 ____________________ #2 ____________________ 230 | | | | 231 | Huffman Decoding | | Huffman Decoding | 232 |__________________| |__________________| 233 | | 234 | | 235 | | 236 \|/ | 237 #3 ____________________ | 238 | | | 239 | PNS LEFT | | 240 |__________________| | 241 | | 242 | | 243 | | 244 \|/ \|/ 245 #4 ______________________________________________________________________ 246 | | 247 | Apply MS_Synt | 248 |____________________________________________________________________| 249 | | 250 | | 251 \|/ | 252 #5 ____________________ | 253 | | W 254 | LTP | A 255 |__________________| I 256 | T 257 | | 258 | F 259 \|/ O 260 #6 ____________________ R 261 | | | 262 | Time -> Freq | L 263 |__________________| E 264 | F 265 | T 266 | | 267 \|/ C 268 #7 ____________________ H 269 | | A 270 | TNS Inverse | N 271 |__________________| N 272 | E 273 | L 274 | | 275 \|/ | 276 #8 ____________________ | 277 | | | 278 | Long Term Synth | | 279 |__________________| | 280 | | 281 | \|/ 282 | #9 ____________________ 283 | | | 284 |--DATA ON LEFT CHANNEL MAY BE USED----->| PNS/Intensity Rt | 285 | |__________________| 286 | | 287 | | 288 | \|/ 289 | #10 ____________________ 290 W | | 291 A | LTP | 292 I |__________________| 293 T | 294 | | 295 F | 296 O \|/ 297 R #11 ____________________ 298 | | | 299 R | Time -> Freq | 300 I |__________________| 301 G | 302 H | 303 T | 304 | \|/ 305 C #12 ____________________ 306 H | | 307 A | TNS Inverse | 308 N |__________________| 309 N | 310 E | 311 L | 312 | \|/ 313 | #13 ____________________ 314 | | | 315 | | Long Term Synth | 316 | |__________________| 317 | | 318 | | 319 | | 320 \|/ \|/ 321 #14 ____________________ #18 ____________________ 322 | | | | 323 | TNS | | TNS | 324 |__________________| |__________________| 325 | | 326 | | 327 | | 328 \|/ \|/ 329 #15 ____________________ #19 ____________________ 330 | | | | 331 | qFormatNorm | | qFormatNorm | 332 |__________________| |__________________| 333 | | 334 | | 335 | | 336 \|/ \|/ 337 #16 ____________________ #20 ____________________ 338 | | | | 339 | Freq / Time | | Freq / Time | 340 |__________________| |__________________| 341 | | 342 | | 343 | | 344 \|/ \|/ 345 #17 ____________________ #21 ____________________ 346 | | | | 347 | Limit Buffer | | Limit Buffer | 348 |__________________| |__________________| 349 | | 350 | | 351 | | 352 \|/ \|/ 353 #22 ______________________________________________________________________ 354 | | 355 | Write Output | 356 |____________________________________________________________________| 357 358 359 ------------------------------------------------------------------------------ 360 REQUIREMENTS 361 362 PacketVideo Document # CCC-AUD-AAC-ERS-0003 363 364 ------------------------------------------------------------------------------ 365 REFERENCES 366 367 (1) MPEG-2 NBC Audio Decoder 368 "This software module was originally developed by AT&T, Dolby 369 Laboratories, Fraunhofer Gesellschaft IIS in the course of development 370 of the MPEG-2 NBC/MPEG-4 Audio standard ISO/IEC 13818-7, 14496-1,2 and 371 3. This software module is an implementation of a part of one or more 372 MPEG-2 NBC/MPEG-4 Audio tools as specified by the MPEG-2 NBC/MPEG-4 373 Audio standard. ISO/IEC gives users of the MPEG-2 NBC/MPEG-4 Audio 374 standards free license to this software module or modifications thereof 375 for use in hardware or software products claiming conformance to the 376 MPEG-2 NBC/MPEG-4 Audio standards. Those intending to use this software 377 module in hardware or software products are advised that this use may 378 infringe existing patents. The original developer of this software 379 module and his/her company, the subsequent editors and their companies, 380 and ISO/IEC have no liability for use of this software module or 381 modifications thereof in an implementation. Copyright is not released 382 for non MPEG-2 NBC/MPEG-4 Audio conforming products.The original 383 developer retains full right to use the code for his/her own purpose, 384 assign or donate the code to a third party and to inhibit third party 385 from using the code for non MPEG-2 NBC/MPEG-4 Audio conforming products. 386 This copyright notice must be included in all copies or derivative 387 works." 388 Copyright(c)1996. 389 390 ------------------------------------------------------------------------------ 391 RESOURCES USED 392 When the code is written for a specific target processor the 393 the resources used should be documented below. 394 395 STACK USAGE: [stack count for this module] + [variable to represent 396 stack usage for each subroutine called] 397 398 where: [stack usage variable] = stack usage for [subroutine 399 name] (see [filename].ext) 400 401 DATA MEMORY USED: x words 402 403 PROGRAM MEMORY USED: x words 404 405 CLOCK CYCLES: [cycle count equation for this module] + [variable 406 used to represent cycle count for each subroutine 407 called] 408 409 where: [cycle count variable] = cycle count for [subroutine 410 name] (see [filename].ext) 411 412 ------------------------------------------------------------------------------ 413 */ 414 415 416 /*---------------------------------------------------------------------------- 417 ; INCLUDES 418 ----------------------------------------------------------------------------*/ 419 #include "pv_audio_type_defs.h" 420 421 #include "s_tdec_int_chan.h" 422 #include "s_tdec_int_file.h" 423 #include "aac_mem_funcs.h" 424 #include "sfb.h" /* Where samp_rate_info[] is declared */ 425 #include "e_tmp4audioobjecttype.h" 426 #include "e_elementid.h" 427 428 429 #include "get_adif_header.h" 430 #include "get_adts_header.h" 431 #include "get_audio_specific_config.h" 432 #include "ibstream.h" /* where getbits is declared */ 433 434 #include "huffman.h" /* where huffdecode is declared */ 435 #include "get_prog_config.h" 436 #include "getfill.h" 437 #include "pns_left.h" 438 439 #include "apply_ms_synt.h" 440 #include "pns_intensity_right.h" 441 #include "q_normalize.h" 442 #include "long_term_prediction.h" 443 #include "long_term_synthesis.h" 444 #include "ltp_common_internal.h" 445 #include "apply_tns.h" 446 447 #include "window_block_fxp.h" 448 449 #include "write_output.h" 450 451 #include "pvmp4audiodecoder_api.h" /* Where this function is declared */ 452 #include "get_dse.h" 453 454 #include "sbr_applied.h" 455 #include "sbr_open.h" 456 #include "get_sbr_bitstream.h" 457 #include "e_sbr_element_id.h" 458 459 460 461 /*---------------------------------------------------------------------------- 462 ; MACROS 463 ; Define module specific macros here 464 ----------------------------------------------------------------------------*/ 465 466 /*---------------------------------------------------------------------------- 467 ; DEFINES 468 ; Include all pre-processor statements here. Include conditional 469 ; compile variables also. 470 ----------------------------------------------------------------------------*/ 471 472 #define LEFT (0) 473 #define RIGHT (1) 474 475 476 /*---------------------------------------------------------------------------- 477 ; LOCAL FUNCTION DEFINITIONS 478 ; Function Prototype declaration 479 ----------------------------------------------------------------------------*/ 480 481 /*---------------------------------------------------------------------------- 482 ; LOCAL STORE/BUFFER/POINTER DEFINITIONS 483 ; Variable declaration - defined here and used outside this module 484 ----------------------------------------------------------------------------*/ 485 486 /*---------------------------------------------------------------------------- 487 ; EXTERNAL FUNCTION REFERENCES 488 ; Declare functions defined elsewhere and referenced in this module 489 ----------------------------------------------------------------------------*/ 490 491 void InitSbrSynFilterbank(bool bDownSampleSBR); 492 493 494 495 /*---------------------------------------------------------------------------- 496 ; EXTERNAL GLOBAL STORE/BUFFER/POINTER REFERENCES 497 ; Declare variables used in this module but defined elsewhere 498 ----------------------------------------------------------------------------*/ 499 500 /*---------------------------------------------------------------------------- 501 ; FUNCTION CODE 502 ----------------------------------------------------------------------------*/ 503 504 505 OSCL_EXPORT_REF Int PVMP4AudioDecodeFrame( 506 tPVMP4AudioDecoderExternal *pExt, 507 void *pMem) 508 { 509 Int frameLength; /* Helper variable */ 510 Int ch; 511 Int id_syn_ele; 512 UInt initialUsedBits; /* Unsigned for C55x */ 513 Int qFormatNorm; 514 Int qPredictedSamples; 515 Bool leaveGetLoop; 516 MC_Info *pMC_Info; /* Helper pointer */ 517 FrameInfo *pFrameInfo; /* Helper pointer */ 518 tDec_Int_File *pVars; /* Helper pointer */ 519 tDec_Int_Chan *pChVars[Chans]; /* Helper pointer */ 520 521 per_chan_share_w_fxpCoef *pChLeftShare; /* Helper pointer */ 522 per_chan_share_w_fxpCoef *pChRightShare; /* Helper pointer */ 523 524 Int status = MP4AUDEC_SUCCESS; 525 526 527 Bool empty_frame; 528 529 #ifdef AAC_PLUS 530 531 SBRDECODER_DATA *sbrDecoderData; 532 SBR_DEC *sbrDec; 533 SBRBITSTREAM *sbrBitStream; 534 535 #endif 536 /* 537 * Initialize "helper" pointers to existing memory. 538 */ 539 pVars = (tDec_Int_File *)pMem; 540 541 pMC_Info = &pVars->mc_info; 542 543 pChVars[LEFT] = &pVars->perChan[LEFT]; 544 pChVars[RIGHT] = &pVars->perChan[RIGHT]; 545 546 pChLeftShare = pChVars[LEFT]->pShareWfxpCoef; 547 pChRightShare = pChVars[RIGHT]->pShareWfxpCoef; 548 549 550 #ifdef AAC_PLUS 551 552 sbrDecoderData = (SBRDECODER_DATA *) & pVars->sbrDecoderData; 553 sbrDec = (SBR_DEC *) & pVars->sbrDec; 554 sbrBitStream = (SBRBITSTREAM *) & pVars->sbrBitStr; 555 556 #ifdef PARAMETRICSTEREO 557 sbrDecoderData->hParametricStereoDec = (HANDLE_PS_DEC) & pVars->sbrDecoderData.ParametricStereoDec; 558 #endif 559 560 #endif 561 /* 562 * Translate input buffer variables. 563 */ 564 pVars->inputStream.pBuffer = pExt->pInputBuffer; 565 566 pVars->inputStream.inputBufferCurrentLength = (UInt)pExt->inputBufferCurrentLength; 567 568 pVars->inputStream.availableBits = 569 (UInt)(pExt->inputBufferCurrentLength << INBUF_ARRAY_INDEX_SHIFT); 570 571 initialUsedBits = 572 (UInt)((pExt->inputBufferUsedLength << INBUF_ARRAY_INDEX_SHIFT) + 573 pExt->remainderBits); 574 575 pVars->inputStream.usedBits = initialUsedBits; 576 577 if (initialUsedBits > pVars->inputStream.availableBits) 578 { 579 status = MP4AUDEC_INVALID_FRAME; 580 } 581 else if (pVars->bno == 0) 582 { 583 /* 584 * Attempt to read in ADIF format first because it is easily identified. 585 * If its not an ADIF bitstream, get_adif_header rewinds the "pointer" 586 * (actually usedBits). 587 */ 588 status = 589 get_adif_header( 590 pVars, 591 &(pVars->scratch.scratch_prog_config)); 592 593 byte_align(&pVars->inputStream); 594 595 if (status == SUCCESS) 596 { 597 pVars->prog_config.file_is_adts = FALSE; 598 } 599 else /* we've tried simple audio config, adif, then it should be adts */ 600 { 601 pVars->prog_config.file_is_adts = TRUE; 602 } 603 } 604 else if ((pVars->bno == 1) && (pVars->prog_config.file_is_adts == FALSE)) 605 { 606 607 /* 608 * There might be an ID_END element following immediately after the 609 * AudioSpecificConfig header. This syntactic element should be read 610 * and byte_aligned before proceeds to decode "real" AAC raw data. 611 */ 612 id_syn_ele = (Int)getbits(LEN_SE_ID, &pVars->inputStream) ; 613 614 if (id_syn_ele == ID_END) 615 { 616 617 byte_align(&pVars->inputStream); 618 619 pExt->inputBufferUsedLength = 620 pVars->inputStream.usedBits >> INBUF_ARRAY_INDEX_SHIFT; 621 622 pExt->remainderBits = pVars->inputStream.usedBits & INBUF_BIT_MODULO_MASK; 623 624 pVars->bno++; 625 626 return(status); 627 } 628 else 629 { 630 /* 631 * Rewind bitstream pointer so that the syntactic element can be 632 * read when decoding raw bitstream 633 */ 634 pVars->inputStream.usedBits -= LEN_SE_ID; 635 } 636 637 } 638 639 if (pVars->prog_config.file_is_adts == TRUE) 640 { 641 /* 642 * If file is adts format, let the decoder handle only on data raw 643 * block at the time, once the last (or only) data block has been 644 * processed, then synch on the next header 645 */ 646 if (pVars->prog_config.headerless_frames) 647 { 648 pVars->prog_config.headerless_frames--; /* raw data block counter */ 649 } 650 else 651 { 652 status = get_adts_header(pVars, 653 &(pVars->syncword), 654 &(pVars->invoke), 655 3); /* CorrectlyReadFramesCount */ 656 657 if (status != SUCCESS) 658 { 659 status = MP4AUDEC_LOST_FRAME_SYNC; /* we lost track of header */ 660 } 661 } 662 } 663 else 664 { 665 byte_align(&pVars->inputStream); 666 } 667 668 #ifdef AAC_PLUS 669 sbrBitStream->NrElements = 0; 670 sbrBitStream->NrElementsCore = 0; 671 672 #endif 673 674 /* 675 * The variable leaveGetLoop is used to signal that the following 676 * loop can be left, which retrieves audio syntatic elements until 677 * an ID_END is found, or an error occurs. 678 */ 679 leaveGetLoop = FALSE; 680 empty_frame = TRUE; 681 682 while ((leaveGetLoop == FALSE) && (status == SUCCESS)) 683 { 684 /* get audio syntactic element */ 685 id_syn_ele = (Int)get9_n_lessbits(LEN_SE_ID, &pVars->inputStream); 686 687 /* 688 * As fractional frames are a possible input, check that parsing does not 689 * go beyond the available bits before parsing the syntax. 690 */ 691 if (pVars->inputStream.usedBits > pVars->inputStream.availableBits) 692 { 693 status = MP4AUDEC_INCOMPLETE_FRAME; /* possible EOF or fractional frame */ 694 id_syn_ele = ID_END; /* quit while-loop */ 695 } 696 697 switch (id_syn_ele) 698 { 699 case ID_END: /* terminator field */ 700 leaveGetLoop = TRUE; 701 break; 702 703 case ID_SCE: /* single channel */ 704 case ID_CPE: /* channel pair */ 705 empty_frame = FALSE; 706 status = 707 huffdecode( 708 id_syn_ele, 709 &(pVars->inputStream), 710 pVars, 711 pChVars); 712 713 #ifdef AAC_PLUS 714 if (id_syn_ele == ID_SCE) 715 { 716 sbrBitStream->sbrElement[sbrBitStream->NrElements].ElementID = SBR_ID_SCE; 717 } 718 else if (id_syn_ele == ID_CPE) 719 { 720 sbrBitStream->sbrElement[sbrBitStream->NrElements].ElementID = SBR_ID_CPE; 721 } 722 sbrBitStream->NrElementsCore++; 723 724 725 #endif 726 727 break; 728 729 case ID_PCE: /* program config element */ 730 /* 731 * PCE are not accepted in the middle of a 732 * raw_data_block. If found, a possible error may happen 733 * If a PCE is encountered during the first 2 frames, 734 * it will be read and accepted 735 * if its tag matches the first, with no error checking 736 * (inside of get_prog_config) 737 */ 738 739 if (pVars->bno <= 1) 740 { 741 status = get_prog_config(pVars, 742 &(pVars->scratch.scratch_prog_config)); 743 } 744 else 745 { 746 status = MP4AUDEC_INVALID_FRAME; 747 } 748 break; 749 750 case ID_FIL: /* fill element */ 751 #ifdef AAC_PLUS 752 get_sbr_bitstream(sbrBitStream, &pVars->inputStream); 753 754 #else 755 getfill(&pVars->inputStream); 756 #endif 757 758 break; 759 760 case ID_DSE: /* Data Streaming element */ 761 get_dse(pVars->share.data_stream_bytes, 762 &pVars->inputStream); 763 break; 764 765 default: /* Unsupported element, including ID_LFE */ 766 status = -1; /* ERROR CODE needs to be updated */ 767 break; 768 769 } /* end switch() */ 770 771 } /* end while() */ 772 773 byte_align(&pVars->inputStream); 774 775 /* 776 * After parsing the first frame ( bno=0 (adif), bno=1 (raw)) 777 * verify if implicit signalling is forcing to upsample AAC with 778 * no AAC+/eAAC+ content. If so, disable upsampling 779 */ 780 781 #ifdef AAC_PLUS 782 if (pVars->bno <= 1) 783 { 784 if ((pVars->mc_info.ExtendedAudioObjectType == MP4AUDIO_AAC_LC) && 785 (!sbrBitStream->NrElements)) 786 { 787 PVMP4AudioDecoderDisableAacPlus(pExt, pMem); 788 } 789 } 790 #endif 791 792 /* 793 * There might be an empty raw data block with only a 794 * ID_END element or non audio ID_DSE, ID_FIL 795 * This is an "illegal" condition but this trap 796 * avoids any further processing 797 */ 798 799 if (empty_frame == TRUE) 800 { 801 pExt->inputBufferUsedLength = 802 pVars->inputStream.usedBits >> INBUF_ARRAY_INDEX_SHIFT; 803 804 pExt->remainderBits = pVars->inputStream.usedBits & INBUF_BIT_MODULO_MASK; 805 806 pVars->bno++; 807 808 return(status); 809 810 } 811 812 #ifdef AAC_PLUS 813 814 if (sbrBitStream->NrElements) 815 { 816 /* for every core SCE or CPE there must be an SBR element, otherwise sths. wrong */ 817 if (sbrBitStream->NrElements != sbrBitStream->NrElementsCore) 818 { 819 status = MP4AUDEC_INVALID_FRAME; 820 } 821 822 if (pExt->aacPlusEnabled == false) 823 { 824 sbrBitStream->NrElements = 0; /* disable aac processing */ 825 } 826 } 827 else 828 { 829 /* 830 * This is AAC, but if aac+/eaac+ was declared in the stream, and there is not sbr content 831 * something is wrong 832 */ 833 if (pMC_Info->sbrPresentFlag || pMC_Info->psPresentFlag) 834 { 835 status = MP4AUDEC_INVALID_FRAME; 836 } 837 } 838 #endif 839 840 841 842 843 /* 844 * Signal processing section. 845 */ 846 frameLength = pVars->frameLength; 847 848 if (status == SUCCESS) 849 { 850 /* 851 * PNS and INTENSITY STEREO and MS 852 */ 853 854 pFrameInfo = pVars->winmap[pChVars[LEFT]->wnd]; 855 856 pns_left( 857 pFrameInfo, 858 pChLeftShare->group, 859 pChLeftShare->cb_map, 860 pChLeftShare->factors, 861 pChLeftShare->lt_status.sfb_prediction_used, 862 pChLeftShare->lt_status.ltp_data_present, 863 pChVars[LEFT]->fxpCoef, 864 pChLeftShare->qFormat, 865 &(pVars->pns_cur_noise_state)); 866 867 /* 868 * apply_ms_synt can only be ran for common windows. 869 * (where both the left and right channel share the 870 * same grouping, window length, etc. 871 * 872 * pVars->hasmask will be > 0 only if 873 * common windows are enabled for this frame. 874 */ 875 876 if (pVars->hasmask > 0) 877 { 878 apply_ms_synt( 879 pFrameInfo, 880 pChLeftShare->group, 881 pVars->mask, 882 pChLeftShare->cb_map, 883 pChVars[LEFT]->fxpCoef, 884 pChVars[RIGHT]->fxpCoef, 885 pChLeftShare->qFormat, 886 pChRightShare->qFormat); 887 } 888 889 for (ch = 0; (ch < pMC_Info->nch); ch++) 890 { 891 pFrameInfo = pVars->winmap[pChVars[ch]->wnd]; 892 893 /* 894 * Note: This MP4 library assumes that if there are two channels, 895 * then the second channel is right AND it was a coupled channel, 896 * therefore there is no need to check the "is_cpe" flag. 897 */ 898 899 if (ch > 0) 900 { 901 pns_intensity_right( 902 pVars->hasmask, 903 pFrameInfo, 904 pChRightShare->group, 905 pVars->mask, 906 pChRightShare->cb_map, 907 pChLeftShare->factors, 908 pChRightShare->factors, 909 pChRightShare->lt_status.sfb_prediction_used, 910 pChRightShare->lt_status.ltp_data_present, 911 pChVars[LEFT]->fxpCoef, 912 pChVars[RIGHT]->fxpCoef, 913 pChLeftShare->qFormat, 914 pChRightShare->qFormat, 915 &(pVars->pns_cur_noise_state)); 916 } 917 918 if (pChVars[ch]->pShareWfxpCoef->lt_status.ltp_data_present != FALSE) 919 { 920 /* 921 * LTP - Long Term Prediction 922 */ 923 924 qPredictedSamples = long_term_prediction( 925 pChVars[ch]->wnd, 926 pChVars[ch]->pShareWfxpCoef->lt_status. 927 weight_index, 928 pChVars[ch]->pShareWfxpCoef->lt_status. 929 delay, 930 pChVars[ch]->ltp_buffer, 931 pVars->ltp_buffer_state, 932 pChVars[ch]->time_quant, 933 pVars->share.predictedSamples, /* Scratch */ 934 frameLength); 935 936 trans4m_time_2_freq_fxp( 937 pVars->share.predictedSamples, 938 pChVars[ch]->wnd, 939 pChVars[ch]->wnd_shape_prev_bk, 940 pChVars[ch]->wnd_shape_this_bk, 941 &qPredictedSamples, 942 pVars->scratch.fft); /* scratch memory for FFT */ 943 944 945 /* 946 * To solve a potential problem where a pointer tied to 947 * the qFormat was being incremented, a pointer to 948 * pChVars[ch]->qFormat is passed in here rather than 949 * the address of qPredictedSamples. 950 * 951 * Neither values are actually needed in the case of 952 * inverse filtering, but the pointer was being 953 * passed (and incremented) regardless. 954 * 955 * So, the solution is to pass a space of memory 956 * that a pointer can happily point to. 957 */ 958 959 /* This is the inverse filter */ 960 apply_tns( 961 pVars->share.predictedSamples, /* scratch re-used for each ch */ 962 pChVars[ch]->pShareWfxpCoef->qFormat, /* Not used by the inv_filter */ 963 pFrameInfo, 964 &(pChVars[ch]->pShareWfxpCoef->tns), 965 TRUE, /* TRUE is FIR */ 966 pVars->scratch.tns_inv_filter); 967 968 /* 969 * For the next function long_term_synthesis, 970 * the third param win_sfb_top[], and 971 * the tenth param coef_per_win, 972 * are used differently that in the rest of the project. This 973 * is because originally the ISO code was going to have 974 * these parameters change as the "short window" changed. 975 * These are all now the same value for each of the eight 976 * windows. This is why there is a [0] at the 977 * end of each of theses parameters. 978 * Note in particular that win_sfb_top was originally an 979 * array of pointers to arrays, but inside long_term_synthesis 980 * it is now a simple array. 981 * When the rest of the project functions are changed, the 982 * structure FrameInfo changes, and the [0]'s are removed, 983 * this comment could go away. 984 */ 985 long_term_synthesis( 986 pChVars[ch]->wnd, 987 pChVars[ch]->pShareWfxpCoef->max_sfb, 988 pFrameInfo->win_sfb_top[0], /* Look above */ 989 pChVars[ch]->pShareWfxpCoef->lt_status.win_prediction_used, 990 pChVars[ch]->pShareWfxpCoef->lt_status.sfb_prediction_used, 991 pChVars[ch]->fxpCoef, /* input and output */ 992 pChVars[ch]->pShareWfxpCoef->qFormat, /* input and output */ 993 pVars->share.predictedSamples, 994 qPredictedSamples, /* q format for previous aray */ 995 pFrameInfo->coef_per_win[0], /* Look above */ 996 NUM_SHORT_WINDOWS, 997 NUM_RECONSTRUCTED_SFB); 998 999 } /* end if (pChVars[ch]->lt_status.ltp_data_present != FALSE) */ 1000 1001 } /* for(ch) */ 1002 1003 for (ch = 0; (ch < pMC_Info->nch); ch++) 1004 { 1005 1006 pFrameInfo = pVars->winmap[pChVars[ch]->wnd]; 1007 1008 /* 1009 * TNS - Temporal Noise Shaping 1010 */ 1011 1012 /* This is the forward filter 1013 * 1014 * A special note: Scratch memory is not used by 1015 * the forward filter, but is passed in to maintain 1016 * common interface for inverse and forward filter 1017 */ 1018 apply_tns( 1019 pChVars[ch]->fxpCoef, 1020 pChVars[ch]->pShareWfxpCoef->qFormat, 1021 pFrameInfo, 1022 &(pChVars[ch]->pShareWfxpCoef->tns), 1023 FALSE, /* FALSE is IIR */ 1024 pVars->scratch.tns_inv_filter); 1025 1026 /* 1027 * Normalize the q format across all scale factor bands 1028 * to one value. 1029 */ 1030 qFormatNorm = 1031 q_normalize( 1032 pChVars[ch]->pShareWfxpCoef->qFormat, 1033 pFrameInfo, 1034 pChVars[ch]->abs_max_per_window, 1035 pChVars[ch]->fxpCoef); 1036 1037 /* 1038 * filterbank - converts frequency coeficients to time domain. 1039 */ 1040 1041 #ifdef AAC_PLUS 1042 if (sbrBitStream->NrElements == 0 && pMC_Info->upsamplingFactor == 1) 1043 { 1044 trans4m_freq_2_time_fxp_2( 1045 pChVars[ch]->fxpCoef, 1046 pChVars[ch]->time_quant, 1047 pChVars[ch]->wnd, /* window sequence */ 1048 pChVars[ch]->wnd_shape_prev_bk, 1049 pChVars[ch]->wnd_shape_this_bk, 1050 qFormatNorm, 1051 pChVars[ch]->abs_max_per_window, 1052 pVars->scratch.fft, 1053 &pExt->pOutputBuffer[ch]); 1054 /* 1055 * Update LTP buffers if needed 1056 */ 1057 1058 if (pVars->mc_info.audioObjectType == MP4AUDIO_LTP) 1059 { 1060 Int16 * pt = &pExt->pOutputBuffer[ch]; 1061 Int16 * ptr = &(pChVars[ch]->ltp_buffer[pVars->ltp_buffer_state]); 1062 Int16 x, y; 1063 for (Int32 i = HALF_LONG_WINDOW; i != 0; i--) 1064 { 1065 x = *pt; 1066 pt += 2; 1067 y = *pt; 1068 pt += 2; 1069 *(ptr++) = x; 1070 *(ptr++) = y; 1071 } 1072 } 1073 } 1074 else 1075 { 1076 trans4m_freq_2_time_fxp_1( 1077 pChVars[ch]->fxpCoef, 1078 pChVars[ch]->time_quant, 1079 &(pChVars[ch]->ltp_buffer[pVars->ltp_buffer_state + 288]), 1080 pChVars[ch]->wnd, /* window sequence */ 1081 pChVars[ch]->wnd_shape_prev_bk, 1082 pChVars[ch]->wnd_shape_this_bk, 1083 qFormatNorm, 1084 pChVars[ch]->abs_max_per_window, 1085 pVars->scratch.fft); 1086 1087 } 1088 #else 1089 1090 trans4m_freq_2_time_fxp_2( 1091 pChVars[ch]->fxpCoef, 1092 pChVars[ch]->time_quant, 1093 pChVars[ch]->wnd, /* window sequence */ 1094 pChVars[ch]->wnd_shape_prev_bk, 1095 pChVars[ch]->wnd_shape_this_bk, 1096 qFormatNorm, 1097 pChVars[ch]->abs_max_per_window, 1098 pVars->scratch.fft, 1099 &pExt->pOutputBuffer[ch]); 1100 /* 1101 * Update LTP buffers only if needed 1102 */ 1103 1104 if (pVars->mc_info.audioObjectType == MP4AUDIO_LTP) 1105 { 1106 Int16 * pt = &pExt->pOutputBuffer[ch]; 1107 Int16 * ptr = &(pChVars[ch]->ltp_buffer[pVars->ltp_buffer_state]); 1108 Int16 x, y; 1109 for (Int32 i = HALF_LONG_WINDOW; i != 0; i--) 1110 { 1111 x = *pt; 1112 pt += 2; 1113 y = *pt; 1114 pt += 2; 1115 *(ptr++) = x; 1116 *(ptr++) = y; 1117 } 1118 1119 } 1120 1121 1122 #endif 1123 1124 1125 /* Update the window shape */ 1126 pChVars[ch]->wnd_shape_prev_bk = pChVars[ch]->wnd_shape_this_bk; 1127 1128 } /* end for() */ 1129 1130 1131 /* 1132 * Copy to the final output buffer, taking into account the desired 1133 * channels from the calling environment, the actual channels, and 1134 * whether the data should be interleaved or not. 1135 * 1136 * If the stream had only one channel, write_output will not use 1137 * the right channel data. 1138 * 1139 */ 1140 1141 1142 /* CONSIDER USE OF DMA OPTIMIZATIONS WITHIN THE write_output FUNCTION. 1143 * 1144 * It is presumed that the ltp_buffer will reside in internal (fast) 1145 * memory, while the pExt->pOutputBuffer will reside in external 1146 * (slow) memory. 1147 * 1148 */ 1149 1150 1151 #ifdef AAC_PLUS 1152 1153 if (sbrBitStream->NrElements || pMC_Info->upsamplingFactor == 2) 1154 { 1155 1156 if (pVars->bno <= 1) /* allows console to operate with ADIF and audio config */ 1157 { 1158 if (sbrDec->outSampleRate == 0) /* do it only once (disregarding of signaling type) */ 1159 { 1160 sbr_open(samp_rate_info[pVars->mc_info.sampling_rate_idx].samp_rate, 1161 sbrDec, 1162 sbrDecoderData, 1163 pVars->mc_info.bDownSampledSbr); 1164 } 1165 1166 } 1167 pMC_Info->upsamplingFactor = 1168 sbrDecoderData->SbrChannel[0].frameData.sbr_header.sampleRateMode; 1169 1170 1171 /* reuse right aac spectrum channel */ 1172 { 1173 Int16 *pt_left = &(pChVars[LEFT ]->ltp_buffer[pVars->ltp_buffer_state]); 1174 Int16 *pt_right = &(pChVars[RIGHT]->ltp_buffer[pVars->ltp_buffer_state]); 1175 1176 if (sbr_applied(sbrDecoderData, 1177 sbrBitStream, 1178 pt_left, 1179 pt_right, 1180 pExt->pOutputBuffer, 1181 sbrDec, 1182 pVars, 1183 pMC_Info->nch) != SBRDEC_OK) 1184 { 1185 status = MP4AUDEC_INVALID_FRAME; 1186 } 1187 } 1188 1189 1190 } /* if( pExt->aacPlusEnabled == FALSE) */ 1191 #endif 1192 1193 /* 1194 * Copied mono data in both channels or just leave it as mono, 1195 * according with desiredChannels (default is 2) 1196 */ 1197 1198 if (pExt->desiredChannels == 2) 1199 { 1200 1201 #if defined(AAC_PLUS) 1202 #if defined(PARAMETRICSTEREO)&&defined(HQ_SBR) 1203 if (pMC_Info->nch != 2 && pMC_Info->psPresentFlag != 1) 1204 #else 1205 if (pMC_Info->nch != 2) 1206 #endif 1207 #else 1208 if (pMC_Info->nch != 2) 1209 #endif 1210 { 1211 /* mono */ 1212 1213 1214 Int16 * pt = &pExt->pOutputBuffer[0]; 1215 Int16 * pt2 = &pExt->pOutputBuffer[1]; 1216 Int i; 1217 if (pMC_Info->upsamplingFactor == 2) 1218 { 1219 for (i = 0; i < 1024; i++) 1220 { 1221 *pt2 = *pt; 1222 pt += 2; 1223 pt2 += 2; 1224 } 1225 pt = &pExt->pOutputBuffer_plus[0]; 1226 pt2 = &pExt->pOutputBuffer_plus[1]; 1227 1228 for (i = 0; i < 1024; i++) 1229 { 1230 *pt2 = *pt; 1231 pt += 2; 1232 pt2 += 2; 1233 } 1234 } 1235 else 1236 { 1237 for (i = 0; i < 1024; i++) 1238 { 1239 *pt2 = *pt; 1240 pt += 2; 1241 pt2 += 2; 1242 } 1243 } 1244 1245 } 1246 1247 #if defined(AAC_PLUS) 1248 #if defined(PARAMETRICSTEREO)&&defined(HQ_SBR) 1249 1250 else if (pMC_Info->psPresentFlag == 1) 1251 { 1252 Int32 frameSize = 0; 1253 if (pExt->aacPlusEnabled == false) 1254 { 1255 /* 1256 * Decoding eaac+ when only aac is enabled, copy L into R 1257 */ 1258 frameSize = 1024; 1259 } 1260 else if (sbrDecoderData->SbrChannel[0].syncState != SBR_ACTIVE) 1261 { 1262 /* 1263 * Decoding eaac+ when no PS data was found, copy upsampled L into R 1264 */ 1265 frameSize = 2048; 1266 } 1267 1268 Int16 * pt = &pExt->pOutputBuffer[0]; 1269 Int16 * pt2 = &pExt->pOutputBuffer[1]; 1270 Int i; 1271 for (i = 0; i < frameSize; i++) 1272 { 1273 *pt2 = *pt; 1274 pt += 2; 1275 pt2 += 2; 1276 } 1277 } 1278 #endif 1279 #endif 1280 1281 } 1282 else 1283 { 1284 1285 #if defined(AAC_PLUS) 1286 #if defined(PARAMETRICSTEREO)&&defined(HQ_SBR) 1287 if (pMC_Info->nch != 2 && pMC_Info->psPresentFlag != 1) 1288 #else 1289 if (pMC_Info->nch != 2) 1290 #endif 1291 #else 1292 if (pMC_Info->nch != 2) 1293 #endif 1294 { 1295 /* mono */ 1296 Int16 * pt = &pExt->pOutputBuffer[0]; 1297 Int16 * pt2 = &pExt->pOutputBuffer[0]; 1298 Int i; 1299 1300 if (pMC_Info->upsamplingFactor == 2) 1301 { 1302 for (i = 0; i < 1024; i++) 1303 { 1304 *pt2++ = *pt; 1305 pt += 2; 1306 } 1307 1308 pt = &pExt->pOutputBuffer_plus[0]; 1309 pt2 = &pExt->pOutputBuffer_plus[0]; 1310 1311 for (i = 0; i < 1024; i++) 1312 { 1313 *pt2++ = *pt; 1314 pt += 2; 1315 } 1316 } 1317 else 1318 { 1319 for (i = 0; i < 1024; i++) 1320 { 1321 *pt2++ = *pt; 1322 pt += 2; 1323 } 1324 } 1325 1326 } 1327 1328 } 1329 1330 1331 1332 1333 /* pVars->ltp_buffer_state cycles between 0 and 1024. The value 1334 * indicates the location of the data corresponding to t == -2. 1335 * 1336 * | t == -2 | t == -1 | pVars->ltp_buffer_state == 0 1337 * 1338 * | t == -1 | t == -2 | pVars->ltp_buffer_state == 1024 1339 * 1340 */ 1341 1342 #ifdef AAC_PLUS 1343 if (sbrBitStream->NrElements == 0 && pMC_Info->upsamplingFactor == 1) 1344 { 1345 pVars->ltp_buffer_state ^= frameLength; 1346 } 1347 else 1348 { 1349 pVars->ltp_buffer_state ^= (frameLength + 288); 1350 } 1351 #else 1352 pVars->ltp_buffer_state ^= frameLength; 1353 #endif 1354 1355 1356 if (pVars->bno <= 1) 1357 { 1358 /* 1359 * to set these values only during the second call 1360 * when they change. 1361 */ 1362 pExt->samplingRate = 1363 samp_rate_info[pVars->mc_info.sampling_rate_idx].samp_rate; 1364 1365 pVars->mc_info.implicit_channeling = 0; /* disable flag, as this is allowed 1366 * only the first time 1367 */ 1368 1369 1370 #ifdef AAC_PLUS 1371 1372 if (pMC_Info->upsamplingFactor == 2) 1373 { 1374 pExt->samplingRate *= pMC_Info->upsamplingFactor; 1375 pExt->aacPlusUpsamplingFactor = pMC_Info->upsamplingFactor; 1376 } 1377 1378 #endif 1379 1380 pExt->extendedAudioObjectType = pMC_Info->ExtendedAudioObjectType; 1381 pExt->audioObjectType = pMC_Info->audioObjectType; 1382 1383 pExt->encodedChannels = pMC_Info->nch; 1384 pExt->frameLength = pVars->frameLength; 1385 } 1386 1387 pVars->bno++; 1388 1389 1390 /* 1391 * Using unit analysis, the bitrate is a function of the sampling rate, bits, 1392 * points in a frame 1393 * 1394 * bits samples frame 1395 * ---- = --------- * bits * ------- 1396 * sec sec sample 1397 * 1398 * To save a divide, a shift is used. Presently only the value of 1399 * 1024 is used by this library, so make it the most accurate for that 1400 * value. This may need to be updated later. 1401 */ 1402 1403 pExt->bitRate = (pExt->samplingRate * 1404 (pVars->inputStream.usedBits - initialUsedBits)) >> 10; /* LONG_WINDOW 1024 */ 1405 1406 pExt->bitRate >>= (pMC_Info->upsamplingFactor - 1); 1407 1408 1409 } /* end if (status == SUCCESS) */ 1410 1411 1412 if (status != MP4AUDEC_SUCCESS) 1413 { 1414 /* 1415 * A non-SUCCESS decoding could be due to an error on the bitstream or 1416 * an incomplete frame. As access to the bitstream beyond frame boundaries 1417 * are not allowed, in those cases the bitstream reading routine return a 0 1418 * Zero values guarantees that the data structures are filled in with values 1419 * that eventually will signal an error (like invalid parameters) or that allow 1420 * completion of the parsing routine. Either way, the partial frame condition 1421 * is verified at this time. 1422 */ 1423 if (pVars->prog_config.file_is_adts == TRUE) 1424 { 1425 status = MP4AUDEC_LOST_FRAME_SYNC; 1426 pVars->prog_config.headerless_frames = 0; /* synchronization forced */ 1427 } 1428 else 1429 { 1430 /* 1431 * Check if the decoding error was due to buffer overrun, if it was, 1432 * update status 1433 */ 1434 if (pVars->inputStream.usedBits > pVars->inputStream.availableBits) 1435 { 1436 /* all bits were used but were not enough to complete decoding */ 1437 pVars->inputStream.usedBits = pVars->inputStream.availableBits; 1438 1439 status = MP4AUDEC_INCOMPLETE_FRAME; /* possible EOF or fractional frame */ 1440 } 1441 } 1442 } 1443 1444 /* 1445 * Translate from units of bits back into units of words. 1446 */ 1447 1448 pExt->inputBufferUsedLength = 1449 pVars->inputStream.usedBits >> INBUF_ARRAY_INDEX_SHIFT; 1450 1451 pExt->remainderBits = (Int)(pVars->inputStream.usedBits & INBUF_BIT_MODULO_MASK); 1452 1453 1454 1455 return (status); 1456 1457 } /* PVMP4AudioDecoderDecodeFrame */ 1458 1459