1 2 /* ----------------------------------------------------------------------------------------------------------- 3 Software License for The Fraunhofer FDK AAC Codec Library for Android 4 5 Copyright 1995 - 2013 Fraunhofer-Gesellschaft zur Frderung der angewandten Forschung e.V. 6 All rights reserved. 7 8 1. INTRODUCTION 9 The Fraunhofer FDK AAC Codec Library for Android ("FDK AAC Codec") is software that implements 10 the MPEG Advanced Audio Coding ("AAC") encoding and decoding scheme for digital audio. 11 This FDK AAC Codec software is intended to be used on a wide variety of Android devices. 12 13 AAC's HE-AAC and HE-AAC v2 versions are regarded as today's most efficient general perceptual 14 audio codecs. AAC-ELD is considered the best-performing full-bandwidth communications codec by 15 independent studies and is widely deployed. AAC has been standardized by ISO and IEC as part 16 of the MPEG specifications. 17 18 Patent licenses for necessary patent claims for the FDK AAC Codec (including those of Fraunhofer) 19 may be obtained through Via Licensing (www.vialicensing.com) or through the respective patent owners 20 individually for the purpose of encoding or decoding bit streams in products that are compliant with 21 the ISO/IEC MPEG audio standards. Please note that most manufacturers of Android devices already license 22 these patent claims through Via Licensing or directly from the patent owners, and therefore FDK AAC Codec 23 software may already be covered under those patent licenses when it is used for those licensed purposes only. 24 25 Commercially-licensed AAC software libraries, including floating-point versions with enhanced sound quality, 26 are also available from Fraunhofer. Users are encouraged to check the Fraunhofer website for additional 27 applications information and documentation. 28 29 2. COPYRIGHT LICENSE 30 31 Redistribution and use in source and binary forms, with or without modification, are permitted without 32 payment of copyright license fees provided that you satisfy the following conditions: 33 34 You must retain the complete text of this software license in redistributions of the FDK AAC Codec or 35 your modifications thereto in source code form. 36 37 You must retain the complete text of this software license in the documentation and/or other materials 38 provided with redistributions of the FDK AAC Codec or your modifications thereto in binary form. 39 You must make available free of charge copies of the complete source code of the FDK AAC Codec and your 40 modifications thereto to recipients of copies in binary form. 41 42 The name of Fraunhofer may not be used to endorse or promote products derived from this library without 43 prior written permission. 44 45 You may not charge copyright license fees for anyone to use, copy or distribute the FDK AAC Codec 46 software or your modifications thereto. 47 48 Your modified versions of the FDK AAC Codec must carry prominent notices stating that you changed the software 49 and the date of any change. For modified versions of the FDK AAC Codec, the term 50 "Fraunhofer FDK AAC Codec Library for Android" must be replaced by the term 51 "Third-Party Modified Version of the Fraunhofer FDK AAC Codec Library for Android." 52 53 3. NO PATENT LICENSE 54 55 NO EXPRESS OR IMPLIED LICENSES TO ANY PATENT CLAIMS, including without limitation the patents of Fraunhofer, 56 ARE GRANTED BY THIS SOFTWARE LICENSE. Fraunhofer provides no warranty of patent non-infringement with 57 respect to this software. 58 59 You may use this FDK AAC Codec software or modifications thereto only for purposes that are authorized 60 by appropriate patent licenses. 61 62 4. DISCLAIMER 63 64 This FDK AAC Codec software is provided by Fraunhofer on behalf of the copyright holders and contributors 65 "AS IS" and WITHOUT ANY EXPRESS OR IMPLIED WARRANTIES, including but not limited to the implied warranties 66 of merchantability and fitness for a particular purpose. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR 67 CONTRIBUTORS BE LIABLE for any direct, indirect, incidental, special, exemplary, or consequential damages, 68 including but not limited to procurement of substitute goods or services; loss of use, data, or profits, 69 or business interruption, however caused and on any theory of liability, whether in contract, strict 70 liability, or tort (including negligence), arising in any way out of the use of this software, even if 71 advised of the possibility of such damage. 72 73 5. CONTACT INFORMATION 74 75 Fraunhofer Institute for Integrated Circuits IIS 76 Attention: Audio and Multimedia Departments - FDK AAC LL 77 Am Wolfsmantel 33 78 91058 Erlangen, Germany 79 80 www.iis.fraunhofer.de/amm 81 amm-info (at) iis.fraunhofer.de 82 ----------------------------------------------------------------------------------------------------------- */ 83 84 /**************************** MPEG-4 HE-AAC Encoder ************************* 85 86 Initial author: M. Lohwasser 87 contents/description: FDK HE-AAC Encoder interface library functions 88 89 ****************************************************************************/ 90 91 #include "aacenc_lib.h" 92 #include "FDK_audio.h" 93 #include "aacenc.h" 94 95 #include "aacEnc_ram.h" 96 #include "FDK_core.h" /* FDK_tools versioning info */ 97 98 /* Encoder library info */ 99 #define AACENCODER_LIB_VL0 3 100 #define AACENCODER_LIB_VL1 4 101 #define AACENCODER_LIB_VL2 12 102 #define AACENCODER_LIB_TITLE "AAC Encoder" 103 #define AACENCODER_LIB_BUILD_DATE __DATE__ 104 #define AACENCODER_LIB_BUILD_TIME __TIME__ 105 106 107 #include "sbr_encoder.h" 108 #include "../src/sbr_ram.h" 109 #include "channel_map.h" 110 111 #include "psy_const.h" 112 #include "bitenc.h" 113 114 #include "tpenc_lib.h" 115 116 #include "metadata_main.h" 117 118 #define SBL(fl) (fl/8) /*!< Short block length (hardcoded to 8 short blocks per long block) */ 119 #define BSLA(fl) (4*SBL(fl)+SBL(fl)/2) /*!< AAC block switching look-ahead */ 120 #define DELAY_AAC(fl) (fl+BSLA(fl)) /*!< MDCT + blockswitching */ 121 #define DELAY_AACELD(fl) ((fl)/2) /*!< ELD FB delay (no framing delay included) */ 122 123 #define INPUTBUFFER_SIZE (1537+100+2048) 124 125 #define DEFAULT_HEADER_PERIOD_REPETITION_RATE 10 /*!< Default header repetition rate used in transport library and for SBR header. */ 126 127 //////////////////////////////////////////////////////////////////////////////////// 128 /** 129 * Flags to characterize encoder modules to be supported in present instance. 130 */ 131 enum { 132 ENC_MODE_FLAG_AAC = 0x0001, 133 ENC_MODE_FLAG_SBR = 0x0002, 134 ENC_MODE_FLAG_PS = 0x0004, 135 ENC_MODE_FLAG_SAC = 0x0008, 136 ENC_MODE_FLAG_META = 0x0010 137 }; 138 139 //////////////////////////////////////////////////////////////////////////////////// 140 typedef struct { 141 AUDIO_OBJECT_TYPE userAOT; /*!< Audio Object Type. */ 142 UINT userSamplerate; /*!< Sampling frequency. */ 143 UINT nChannels; /*!< will be set via channelMode. */ 144 CHANNEL_MODE userChannelMode; 145 UINT userBitrate; 146 UINT userBitrateMode; 147 UINT userBandwidth; 148 UINT userAfterburner; 149 UINT userFramelength; 150 UINT userAncDataRate; 151 152 UCHAR userTns; /*!< Use TNS coding. */ 153 UCHAR userPns; /*!< Use PNS coding. */ 154 UCHAR userIntensity; /*!< Use Intensity coding. */ 155 156 TRANSPORT_TYPE userTpType; /*!< Transport type */ 157 UCHAR userTpSignaling; /*!< Extension AOT signaling mode. */ 158 UCHAR userTpNsubFrames; /*!< Number of sub frames in a transport frame for LOAS/LATM or ADTS (default 1). */ 159 UCHAR userTpAmxv; /*!< AudioMuxVersion to be used for LATM (default 0). */ 160 UCHAR userTpProtection; 161 UCHAR userTpHeaderPeriod; /*!< Parameter used to configure LATM/LOAS SMC rate. Moreover this parameters is 162 used to configure repetition rate of PCE in raw_data_block. */ 163 164 UCHAR userErTools; /*!< Use VCB11, HCR and/or RVLC ER tool. */ 165 UINT userPceAdditions; /*!< Configure additional bits in PCE. */ 166 167 UCHAR userMetaDataMode; /*!< Meta data library configuration. */ 168 169 UCHAR userSbrEnabled; /*!< Enable SBR for ELD. */ 170 UINT userSbrRatio; /*!< SBR sampling rate ratio. Dual- or single-rate. */ 171 172 } USER_PARAM; 173 174 //////////////////////////////////////////////////////////////////////////////////// 175 176 /**************************************************************************** 177 Structure Definitions 178 ****************************************************************************/ 179 180 typedef struct AACENC_CONFIG *HANDLE_AACENC_CONFIG; 181 182 183 struct AACENCODER 184 { 185 USER_PARAM extParam; 186 CODER_CONFIG coderConfig; 187 188 /* AAC */ 189 AACENC_CONFIG aacConfig; 190 HANDLE_AAC_ENC hAacEnc; 191 192 /* SBR */ 193 HANDLE_SBR_ENCODER hEnvEnc; 194 195 /* Meta Data */ 196 HANDLE_FDK_METADATA_ENCODER hMetadataEnc; 197 INT metaDataAllowed; /* Signal whether chosen configuration allows metadata. Necessary for delay 198 compensation. Metadata mode is a separate parameter. */ 199 200 /* Transport */ 201 HANDLE_TRANSPORTENC hTpEnc; 202 203 /* Output */ 204 UCHAR *outBuffer; /* Internal bitstream buffer */ 205 INT outBufferInBytes; /* Size of internal bitstream buffer*/ 206 207 /* Input */ 208 INT_PCM *inputBuffer; /* Internal input buffer. Input source for AAC encoder */ 209 INT inputBufferOffset; /* Where to write new input samples. */ 210 211 INT nSamplesToRead; /* number of input samples neeeded for encoding one frame */ 212 INT nSamplesRead; /* number of input samples already in input buffer */ 213 INT nZerosAppended; /* appended zeros at end of file*/ 214 INT nDelay; /* encoder delay */ 215 216 AACENC_EXT_PAYLOAD extPayload [MAX_TOTAL_EXT_PAYLOADS]; 217 /* Extension payload */ 218 UCHAR extPayloadData [(1)][(8)][MAX_PAYLOAD_SIZE]; 219 UINT extPayloadSize [(1)][(8)]; /* payload sizes in bits */ 220 221 ULONG InitFlags; /* internal status to treggier re-initialization */ 222 223 224 /* Memory allocation info. */ 225 INT nMaxAacElements; 226 INT nMaxAacChannels; 227 INT nMaxSbrElements; 228 INT nMaxSbrChannels; 229 UINT nMaxSubFrames; 230 231 UINT encoder_modis; 232 233 /* Capability flags */ 234 UINT CAPF_tpEnc; 235 236 } ; 237 238 typedef struct 239 { 240 ULONG samplingRate; /*!< Encoder output sampling rate. */ 241 ULONG bitrateRange; /*!< Lower bitrate range for config entry. */ 242 243 UCHAR lowDelaySbr; /*!< 0: ELD sbr off, 244 1: ELD sbr on */ 245 246 UCHAR downsampledSbr; /*!< 0: ELD with dualrate sbr, 247 1: ELD with downsampled sbr */ 248 249 } ELD_SBR_CONFIGURATOR; 250 251 /** 252 * \brief This table defines ELD/SBR default configurations. 253 */ 254 static const ELD_SBR_CONFIGURATOR eldSbrAutoConfigTab[] = 255 { 256 { 48000, 0, 1, 0 }, 257 { 48000, 64001, 0, 0 }, 258 259 { 44100, 0, 1, 0 }, 260 { 44100, 64001, 0, 0 }, 261 262 { 32000, 0, 1, 0 }, 263 { 32000, 28000, 1, 1 }, 264 { 32000, 56000, 0, 0 }, 265 266 { 24000, 0, 1, 1 }, 267 { 24000, 40000, 0, 0 }, 268 269 { 16000, 0, 1, 1 }, 270 { 16000, 28000, 0, 0 } 271 272 }; 273 274 /* 275 * \brief Configure SBR for ELD configuration. 276 * 277 * This function finds default SBR configuration for ELD based on sampling rate and channel bitrate. 278 * Outputparameters are SBR on/off, and SBR ratio. 279 * 280 * \param samplingRate Audio signal sampling rate. 281 * \param channelMode Channel configuration to be used. 282 * \param totalBitrate Overall bitrate. 283 * \param eldSbr Pointer to eldSbr parameter, filled on return. 284 * \param eldSbrRatio Pointer to eldSbrRatio parameter, filled on return. 285 * 286 * \return - AACENC_OK, all fine. 287 * - AACENC_INVALID_CONFIG, on failure. 288 */ 289 static AACENC_ERROR eldSbrConfigurator( 290 const ULONG samplingRate, 291 const CHANNEL_MODE channelMode, 292 const ULONG totalBitrate, 293 UINT * const eldSbr, 294 UINT * const eldSbrRatio 295 ) 296 { 297 AACENC_ERROR err = AACENC_OK; 298 int i, cfgIdx = -1; 299 const ULONG channelBitrate = totalBitrate / FDKaacEnc_GetChannelModeConfiguration(channelMode)->nChannelsEff; 300 301 for (i=0; i<(sizeof(eldSbrAutoConfigTab)/sizeof(ELD_SBR_CONFIGURATOR)); i++) { 302 if ( (samplingRate <= eldSbrAutoConfigTab[i].samplingRate) 303 && (channelBitrate >= eldSbrAutoConfigTab[i].bitrateRange) ) 304 { 305 cfgIdx = i; 306 } 307 } 308 309 if (cfgIdx != -1) { 310 *eldSbr = (eldSbrAutoConfigTab[cfgIdx].lowDelaySbr==0) ? 0 : 1; 311 *eldSbrRatio = (eldSbrAutoConfigTab[cfgIdx].downsampledSbr==0) ? 2 : 1; 312 } 313 else { 314 err = AACENC_INVALID_CONFIG; /* no default configuration for eld-sbr available. */ 315 } 316 317 return err; 318 } 319 320 static inline INT isSbrActive(const HANDLE_AACENC_CONFIG hAacConfig) 321 { 322 INT sbrUsed = 0; 323 324 if ( (hAacConfig->audioObjectType==AOT_SBR) || (hAacConfig->audioObjectType==AOT_PS) 325 || (hAacConfig->audioObjectType==AOT_MP2_SBR) || (hAacConfig->audioObjectType==AOT_MP2_PS) 326 || (hAacConfig->audioObjectType==AOT_DABPLUS_SBR) || (hAacConfig->audioObjectType==AOT_DABPLUS_PS) 327 || (hAacConfig->audioObjectType==AOT_DRM_SBR) || (hAacConfig->audioObjectType==AOT_DRM_MPEG_PS) ) 328 { 329 sbrUsed = 1; 330 } 331 if (hAacConfig->audioObjectType == AOT_ER_AAC_ELD && (hAacConfig->syntaxFlags & AC_SBR_PRESENT)) 332 { 333 sbrUsed = 1; 334 } 335 336 return ( sbrUsed ); 337 } 338 339 static inline INT isPsActive(const AUDIO_OBJECT_TYPE audioObjectType) 340 { 341 INT psUsed = 0; 342 343 if ( (audioObjectType==AOT_PS) 344 || (audioObjectType==AOT_MP2_PS) 345 || (audioObjectType==AOT_DABPLUS_PS) 346 || (audioObjectType==AOT_DRM_MPEG_PS) ) 347 { 348 psUsed = 1; 349 } 350 351 return ( psUsed ); 352 } 353 354 static SBR_PS_SIGNALING getSbrSignalingMode( 355 const AUDIO_OBJECT_TYPE audioObjectType, 356 const TRANSPORT_TYPE transportType, 357 const UCHAR transportSignaling, 358 const UINT sbrRatio 359 ) 360 361 { 362 SBR_PS_SIGNALING sbrSignaling; 363 364 if (transportType==TT_UNKNOWN || sbrRatio==0) { 365 sbrSignaling = SIG_UNKNOWN; /* Needed parameters have not been set */ 366 return sbrSignaling; 367 } else { 368 sbrSignaling = SIG_IMPLICIT; /* default: implicit signaling */ 369 } 370 371 if ((audioObjectType==AOT_AAC_LC) || (audioObjectType==AOT_SBR) || (audioObjectType==AOT_PS) || 372 (audioObjectType==AOT_MP2_AAC_LC) || (audioObjectType==AOT_MP2_SBR) || (audioObjectType==AOT_MP2_PS) ) { 373 switch (transportType) { 374 case TT_MP4_ADIF: 375 case TT_MP4_ADTS: 376 sbrSignaling = SIG_IMPLICIT; /* For MPEG-2 transport types, only implicit signaling is possible */ 377 break; 378 379 case TT_MP4_RAW: 380 case TT_MP4_LATM_MCP1: 381 case TT_MP4_LATM_MCP0: 382 case TT_MP4_LOAS: 383 default: 384 if ( transportSignaling==0xFF ) { 385 /* Defaults */ 386 if ( sbrRatio==1 ) { 387 sbrSignaling = SIG_EXPLICIT_HIERARCHICAL; /* For downsampled SBR, explicit signaling is mandatory */ 388 } else { 389 sbrSignaling = SIG_IMPLICIT; /* For dual-rate SBR, implicit signaling is default */ 390 } 391 } else { 392 /* User set parameters */ 393 /* Attention: Backward compatible explicit signaling does only work with AMV1 for LATM/LOAS */ 394 sbrSignaling = (SBR_PS_SIGNALING)transportSignaling; 395 } 396 break; 397 } 398 } 399 400 return sbrSignaling; 401 } 402 403 /**************************************************************************** 404 Allocate Encoder 405 ****************************************************************************/ 406 407 H_ALLOC_MEM (_AacEncoder, AACENCODER) 408 C_ALLOC_MEM (_AacEncoder, AACENCODER, 1) 409 410 411 412 413 /* 414 * Map Encoder specific config structures to CODER_CONFIG. 415 */ 416 static void FDKaacEnc_MapConfig( 417 CODER_CONFIG *const cc, 418 const USER_PARAM *const extCfg, 419 const SBR_PS_SIGNALING sbrSignaling, 420 const HANDLE_AACENC_CONFIG hAacConfig 421 ) 422 { 423 AUDIO_OBJECT_TYPE transport_AOT = AOT_NULL_OBJECT; 424 FDKmemclear(cc, sizeof(CODER_CONFIG)); 425 426 cc->flags = 0; 427 428 /* Map virtual aot to transport aot. */ 429 switch (hAacConfig->audioObjectType) { 430 case AOT_MP2_AAC_LC: 431 transport_AOT = AOT_AAC_LC; 432 break; 433 case AOT_MP2_SBR: 434 transport_AOT = AOT_SBR; 435 cc->flags |= CC_SBR; 436 break; 437 case AOT_MP2_PS: 438 transport_AOT = AOT_PS; 439 cc->flags |= CC_SBR; 440 break; 441 default: 442 transport_AOT = hAacConfig->audioObjectType; 443 } 444 445 if (hAacConfig->audioObjectType == AOT_ER_AAC_ELD) { 446 cc->flags |= (hAacConfig->syntaxFlags & AC_SBR_PRESENT) ? CC_SBR : 0; 447 } 448 449 /* transport type is usually AAC-LC. */ 450 if ( (transport_AOT == AOT_SBR) || (transport_AOT == AOT_PS) ) { 451 cc->aot = AOT_AAC_LC; 452 } 453 else { 454 cc->aot = transport_AOT; 455 } 456 457 /* Configure extension aot. */ 458 if (sbrSignaling==SIG_IMPLICIT) { 459 cc->extAOT = AOT_NULL_OBJECT; /* implicit */ 460 } 461 else { 462 if ( (sbrSignaling==SIG_EXPLICIT_BW_COMPATIBLE) && ( (transport_AOT==AOT_SBR) || (transport_AOT==AOT_PS) ) ) { 463 cc->extAOT = AOT_SBR; /* explicit backward compatible */ 464 } 465 else { 466 cc->extAOT = transport_AOT; /* explicit hierarchical */ 467 } 468 } 469 470 if ( (transport_AOT==AOT_SBR) || (transport_AOT==AOT_PS) ) { 471 cc->sbrPresent=1; 472 if (transport_AOT==AOT_PS) { 473 cc->psPresent=1; 474 } 475 } 476 cc->sbrSignaling = sbrSignaling; 477 478 cc->extSamplingRate = extCfg->userSamplerate; 479 cc->bitRate = hAacConfig->bitRate; 480 cc->noChannels = hAacConfig->nChannels; 481 cc->flags |= CC_IS_BASELAYER; 482 cc->channelMode = hAacConfig->channelMode; 483 484 cc->nSubFrames = (hAacConfig->nSubFrames > 1 && extCfg->userTpNsubFrames == 1) 485 ? hAacConfig->nSubFrames 486 : extCfg->userTpNsubFrames; 487 488 cc->flags |= (extCfg->userTpProtection) ? CC_PROTECTION : 0; 489 490 if (extCfg->userTpHeaderPeriod!=0xFF) { 491 cc->headerPeriod = extCfg->userTpHeaderPeriod; 492 } 493 else { /* auto-mode */ 494 switch (extCfg->userTpType) { 495 case TT_MP4_ADTS: 496 case TT_MP4_LOAS: 497 case TT_MP4_LATM_MCP1: 498 cc->headerPeriod = DEFAULT_HEADER_PERIOD_REPETITION_RATE; 499 break; 500 default: 501 cc->headerPeriod = 0; 502 } 503 } 504 505 cc->samplesPerFrame = hAacConfig->framelength; 506 cc->samplingRate = hAacConfig->sampleRate; 507 508 /* Mpeg-4 signaling for transport library. */ 509 switch ( hAacConfig->audioObjectType ) { 510 case AOT_MP2_AAC_LC: 511 case AOT_MP2_SBR: 512 case AOT_MP2_PS: 513 cc->flags &= ~CC_MPEG_ID; /* Required for ADTS. */ 514 cc->extAOT = AOT_NULL_OBJECT; 515 break; 516 default: 517 cc->flags |= CC_MPEG_ID; 518 } 519 520 /* ER-tools signaling. */ 521 cc->flags |= (hAacConfig->syntaxFlags & AC_ER_VCB11) ? CC_VCB11 : 0; 522 cc->flags |= (hAacConfig->syntaxFlags & AC_ER_HCR) ? CC_HCR : 0; 523 cc->flags |= (hAacConfig->syntaxFlags & AC_ER_RVLC) ? CC_RVLC : 0; 524 525 /* Matrix mixdown coefficient configuration. */ 526 if ( (extCfg->userPceAdditions&0x1) && (hAacConfig->epConfig==-1) 527 && ((cc->channelMode==MODE_1_2_2)||(cc->channelMode==MODE_1_2_2_1)) ) 528 { 529 cc->matrixMixdownA = ((extCfg->userPceAdditions>>1)&0x3)+1; 530 cc->flags |= (extCfg->userPceAdditions>>3)&0x1 ? CC_PSEUDO_SURROUND : 0; 531 } 532 else { 533 cc->matrixMixdownA = 0; 534 } 535 } 536 537 /* 538 * Examine buffer descriptor regarding choosen identifier. 539 * 540 * \param pBufDesc Pointer to buffer descriptor 541 * \param identifier Buffer identifier to look for. 542 543 * \return - Buffer descriptor index. 544 * -1, if there is no entry available. 545 */ 546 static INT getBufDescIdx( 547 const AACENC_BufDesc *pBufDesc, 548 const AACENC_BufferIdentifier identifier 549 ) 550 { 551 INT i, idx = -1; 552 553 for (i=0; i<pBufDesc->numBufs; i++) { 554 if ( (AACENC_BufferIdentifier)pBufDesc->bufferIdentifiers[i] == identifier ) { 555 idx = i; 556 break; 557 } 558 } 559 return idx; 560 } 561 562 563 /**************************************************************************** 564 Function Declarations 565 ****************************************************************************/ 566 567 AAC_ENCODER_ERROR aacEncDefaultConfig(HANDLE_AACENC_CONFIG hAacConfig, 568 USER_PARAM *config) 569 { 570 /* make reasonable default settings */ 571 FDKaacEnc_AacInitDefaultConfig (hAacConfig); 572 573 /* clear configuration structure and copy default settings */ 574 FDKmemclear(config, sizeof(USER_PARAM)); 575 576 /* copy encoder configuration settings */ 577 config->nChannels = hAacConfig->nChannels; 578 config->userAOT = hAacConfig->audioObjectType = AOT_AAC_LC; 579 config->userSamplerate = hAacConfig->sampleRate; 580 config->userChannelMode = hAacConfig->channelMode; 581 config->userBitrate = hAacConfig->bitRate; 582 config->userBitrateMode = hAacConfig->bitrateMode; 583 config->userBandwidth = hAacConfig->bandWidth; 584 config->userTns = hAacConfig->useTns; 585 config->userPns = hAacConfig->usePns; 586 config->userIntensity = hAacConfig->useIS; 587 config->userAfterburner = hAacConfig->useRequant; 588 config->userFramelength = (UINT)-1; 589 590 if (hAacConfig->syntaxFlags & AC_ER_VCB11) { 591 config->userErTools |= 0x01; 592 } 593 if (hAacConfig->syntaxFlags & AC_ER_HCR) { 594 config->userErTools |= 0x02; 595 } 596 597 /* initialize transport parameters */ 598 config->userTpType = TT_UNKNOWN; 599 config->userTpAmxv = 0; 600 config->userTpSignaling = 0xFF; /* choose signaling automatically */ 601 config->userTpNsubFrames = 1; 602 config->userTpProtection = 0; /* not crc protected*/ 603 config->userTpHeaderPeriod = 0xFF; /* header period in auto mode */ 604 config->userPceAdditions = 0; /* no matrix mixdown coefficient */ 605 config->userMetaDataMode = 0; /* do not embed any meta data info */ 606 607 config->userAncDataRate = 0; 608 609 /* SBR rate is set to 0 here, which means it should be set automatically 610 in FDKaacEnc_AdjustEncSettings() if the user did not set a rate 611 expilicitely. */ 612 config->userSbrRatio = 0; 613 614 /* SBR enable set to -1 means to inquire ELD audio configurator for reasonable configuration. */ 615 config->userSbrEnabled = -1; 616 617 return AAC_ENC_OK; 618 } 619 620 static 621 void aacEncDistributeSbrBits(CHANNEL_MAPPING *channelMapping, SBR_ELEMENT_INFO *sbrElInfo, INT bitRate) 622 { 623 INT codebits = bitRate; 624 int el; 625 626 /* Copy Element info */ 627 for (el=0; el<channelMapping->nElements; el++) { 628 sbrElInfo[el].ChannelIndex[0] = channelMapping->elInfo[el].ChannelIndex[0]; 629 sbrElInfo[el].ChannelIndex[1] = channelMapping->elInfo[el].ChannelIndex[1]; 630 sbrElInfo[el].elType = channelMapping->elInfo[el].elType; 631 sbrElInfo[el].bitRate = (INT)(fMultNorm(channelMapping->elInfo[el].relativeBits, (FIXP_DBL)bitRate)); 632 sbrElInfo[el].instanceTag = channelMapping->elInfo[el].instanceTag; 633 sbrElInfo[el].nChannelsInEl = channelMapping->elInfo[el].nChannelsInEl; 634 635 codebits -= sbrElInfo[el].bitRate; 636 } 637 sbrElInfo[0].bitRate += codebits; 638 } 639 640 641 static 642 INT aacEncoder_LimitBitrate( 643 const HANDLE_TRANSPORTENC hTpEnc, 644 const INT samplingRate, 645 const INT frameLength, 646 const INT nChannels, 647 const CHANNEL_MODE channelMode, 648 INT bitRate, 649 const INT nSubFrames, 650 const INT sbrActive, 651 const INT sbrDownSampleRate, 652 const AUDIO_OBJECT_TYPE aot 653 ) 654 { 655 INT coreSamplingRate; 656 CHANNEL_MAPPING cm; 657 658 FDKaacEnc_InitChannelMapping(channelMode, CH_ORDER_MPEG, &cm); 659 660 if (sbrActive) { 661 coreSamplingRate = samplingRate >> (sbrEncoder_IsSingleRatePossible(aot) ? (sbrDownSampleRate-1):1); 662 } else { 663 coreSamplingRate = samplingRate; 664 } 665 666 /* Consider bandwidth channel bit rate limit (see bandwidth.cpp: GetBandwidthEntry()) */ 667 if (aot == AOT_ER_AAC_LD || aot == AOT_ER_AAC_ELD) { 668 bitRate = FDKmin(360000*nChannels, bitRate); 669 bitRate = FDKmax(8000*nChannels, bitRate); 670 } 671 672 if (aot == AOT_AAC_LC || aot == AOT_SBR || aot == AOT_PS) { 673 bitRate = FDKmin(576000*nChannels, bitRate); 674 /*bitRate = FDKmax(0*nChannels, bitRate);*/ 675 } 676 677 678 /* Limit bit rate in respect to the core coder */ 679 bitRate = FDKaacEnc_LimitBitrate( 680 hTpEnc, 681 coreSamplingRate, 682 frameLength, 683 nChannels, 684 cm.nChannelsEff, 685 bitRate, 686 -1, 687 NULL, 688 -1, 689 nSubFrames 690 ); 691 692 /* Limit bit rate in respect to available SBR modes if active */ 693 if (sbrActive) 694 { 695 int numIterations = 0; 696 INT initialBitrate, adjustedBitrate; 697 initialBitrate = adjustedBitrate = bitRate; 698 699 /* Find total bitrate which provides valid configuration for each SBR element. */ 700 do { 701 int e; 702 SBR_ELEMENT_INFO sbrElInfo[(8)]; 703 FDK_ASSERT(cm.nElements <= (8)); 704 705 initialBitrate = adjustedBitrate; 706 707 /* Get bit rate for each SBR element */ 708 aacEncDistributeSbrBits(&cm, sbrElInfo, initialBitrate); 709 710 for (e=0; e<cm.nElements; e++) 711 { 712 INT sbrElementBitRateIn, sbrBitRateOut; 713 714 if (cm.elInfo[e].elType != ID_SCE && cm.elInfo[e].elType != ID_CPE) { 715 continue; 716 } 717 sbrElementBitRateIn = sbrElInfo[e].bitRate; 718 sbrBitRateOut = sbrEncoder_LimitBitRate(sbrElementBitRateIn , cm.elInfo[e].nChannelsInEl, coreSamplingRate, aot); 719 if (sbrBitRateOut == 0) { 720 return 0; 721 } 722 723 /* If bitrates don't match, distribution and limiting needs to be determined again. 724 Abort element loop and restart with adapted bitrate. */ 725 if (sbrElementBitRateIn != sbrBitRateOut) { 726 727 if (sbrElementBitRateIn < sbrBitRateOut) { 728 adjustedBitrate = fMax(initialBitrate, (INT)fDivNorm((FIXP_DBL)(sbrBitRateOut+8), cm.elInfo[e].relativeBits)); 729 break; 730 } 731 732 if (sbrElementBitRateIn > sbrBitRateOut) { 733 adjustedBitrate = fMin(initialBitrate, (INT)fDivNorm((FIXP_DBL)(sbrBitRateOut-8), cm.elInfo[e].relativeBits)); 734 break; 735 } 736 737 } /* sbrElementBitRateIn != sbrBitRateOut */ 738 739 } /* elements */ 740 741 numIterations++; /* restrict iteration to worst case of num elements */ 742 743 } while ( (initialBitrate!=adjustedBitrate) && (numIterations<=cm.nElements) ); 744 745 /* Unequal bitrates mean that no reasonable bitrate configuration found. */ 746 bitRate = (initialBitrate==adjustedBitrate) ? adjustedBitrate : 0; 747 } 748 749 FDK_ASSERT(bitRate > 0); 750 751 return bitRate; 752 } 753 754 /* 755 * \brief Consistency check of given USER_PARAM struct and 756 * copy back configuration from public struct into internal 757 * encoder configuration struct. 758 * 759 * \hAacEncoder Internal encoder config which is to be updated 760 * \param config User provided config (public struct) 761 * \return returns always AAC_ENC_OK 762 */ 763 static 764 AACENC_ERROR FDKaacEnc_AdjustEncSettings(HANDLE_AACENCODER hAacEncoder, 765 USER_PARAM *config) 766 { 767 AACENC_ERROR err = AACENC_OK; 768 769 /* Get struct pointers. */ 770 HANDLE_AACENC_CONFIG hAacConfig = &hAacEncoder->aacConfig; 771 772 hAacConfig->nChannels = config->nChannels; 773 774 /* Encoder settings update. */ 775 hAacConfig->sampleRate = config->userSamplerate; 776 hAacConfig->useTns = config->userTns; 777 hAacConfig->usePns = config->userPns; 778 hAacConfig->useIS = config->userIntensity; 779 hAacConfig->bitRate = config->userBitrate; 780 hAacConfig->channelMode = config->userChannelMode; 781 hAacConfig->bitrateMode = config->userBitrateMode; 782 hAacConfig->bandWidth = config->userBandwidth; 783 hAacConfig->useRequant = config->userAfterburner; 784 785 hAacConfig->audioObjectType = config->userAOT; 786 hAacConfig->anc_Rate = config->userAncDataRate; 787 hAacConfig->syntaxFlags = 0; 788 hAacConfig->epConfig = -1; 789 790 /* Adapt internal AOT when necessary. */ 791 switch ( hAacConfig->audioObjectType ) { 792 case AOT_MP2_AAC_LC: 793 case AOT_MP2_SBR: 794 case AOT_MP2_PS: 795 hAacConfig->usePns = 0; 796 case AOT_AAC_LC: 797 case AOT_SBR: 798 case AOT_PS: 799 config->userTpType = (config->userTpType!=TT_UNKNOWN) ? config->userTpType : TT_MP4_ADTS; 800 hAacConfig->framelength = (config->userFramelength!=(UINT)-1) ? config->userFramelength : 1024; 801 if (hAacConfig->framelength != 1024) { 802 return AACENC_INVALID_CONFIG; 803 } 804 break; 805 case AOT_ER_AAC_LD: 806 hAacConfig->epConfig = 0; 807 hAacConfig->syntaxFlags |= AC_ER|AC_LD; 808 hAacConfig->syntaxFlags |= ((config->userErTools & 0x1) ? AC_ER_VCB11 : 0); 809 hAacConfig->syntaxFlags |= ((config->userErTools & 0x2) ? AC_ER_HCR : 0); 810 hAacConfig->syntaxFlags |= ((config->userErTools & 0x4) ? AC_ER_RVLC : 0); 811 config->userTpType = (config->userTpType!=TT_UNKNOWN) ? config->userTpType : TT_MP4_LOAS; 812 hAacConfig->framelength = (config->userFramelength!=(UINT)-1) ? config->userFramelength : 512; 813 if (hAacConfig->framelength != 512 && hAacConfig->framelength != 480) { 814 return AACENC_INVALID_CONFIG; 815 } 816 break; 817 case AOT_ER_AAC_ELD: 818 hAacConfig->epConfig = 0; 819 hAacConfig->syntaxFlags |= AC_ER|AC_ELD; 820 hAacConfig->syntaxFlags |= ((config->userErTools & 0x1) ? AC_ER_VCB11 : 0); 821 hAacConfig->syntaxFlags |= ((config->userErTools & 0x2) ? AC_ER_HCR : 0); 822 hAacConfig->syntaxFlags |= ((config->userErTools & 0x4) ? AC_ER_RVLC : 0); 823 hAacConfig->syntaxFlags |= ((config->userSbrEnabled==1) ? AC_SBR_PRESENT : 0); 824 config->userTpType = (config->userTpType!=TT_UNKNOWN) ? config->userTpType : TT_MP4_LOAS; 825 hAacConfig->framelength = (config->userFramelength!=(UINT)-1) ? config->userFramelength : 512; 826 if (hAacConfig->framelength != 512 && hAacConfig->framelength != 480) { 827 return AACENC_INVALID_CONFIG; 828 } 829 break; 830 default: 831 break; 832 } 833 834 switch ( hAacConfig->audioObjectType ) { 835 case AOT_ER_AAC_LD: 836 case AOT_ER_AAC_ELD: 837 if (config->userBitrateMode==8) { 838 hAacConfig->bitrateMode = 0; 839 } 840 if (config->userBitrateMode==0) { 841 hAacConfig->bitreservoir = 100*config->nChannels; /* default, reduced bitreservoir */ 842 } 843 if (hAacConfig->bitrateMode!=0) { 844 return AACENC_INVALID_CONFIG; 845 } 846 break; 847 default: 848 break; 849 } 850 851 hAacConfig->bitRate = config->userBitrate; 852 853 /* get bitrate in VBR configuration */ 854 if ( (hAacConfig->bitrateMode>=1) && (hAacConfig->bitrateMode<=5) ) { 855 /* In VBR mode; SBR-modul depends on bitrate, core encoder on bitrateMode. */ 856 hAacConfig->bitRate = FDKaacEnc_GetVBRBitrate(hAacConfig->bitrateMode, hAacConfig->channelMode); 857 } 858 859 860 861 /* Set default bitrate if no external bitrate declared. */ 862 if ( (hAacConfig->bitrateMode==0) && (config->userBitrate==(UINT)-1) ) { 863 INT bitrate = FDKaacEnc_GetChannelModeConfiguration(hAacConfig->channelMode)->nChannelsEff * hAacConfig->sampleRate; 864 865 if ( isPsActive(hAacConfig->audioObjectType) ) { 866 hAacConfig->bitRate = (bitrate>>1); /* 0.5 bit per sample */ 867 } 868 else if ( isSbrActive(hAacConfig) ) 869 { 870 if ( (config->userSbrRatio==2) || ((config->userSbrRatio==0)&&(hAacConfig->audioObjectType!=AOT_ER_AAC_ELD)) ) { 871 hAacConfig->bitRate = (bitrate + (bitrate>>2))>>1; /* 0.625 bits per sample */ 872 } 873 if ( (config->userSbrRatio==1) || ((config->userSbrRatio==0)&&(hAacConfig->audioObjectType==AOT_ER_AAC_ELD)) ) { 874 hAacConfig->bitRate = (bitrate + (bitrate>>3)); /* 1.125 bits per sample */ 875 } 876 } else 877 { 878 hAacConfig->bitRate = bitrate + (bitrate>>1); /* 1.5 bits per sample */ 879 } 880 } 881 882 /* Initialize SBR parameters */ 883 if ( (hAacConfig->audioObjectType==AOT_ER_AAC_ELD) 884 && (config->userSbrEnabled == (UCHAR)-1) && (config->userSbrRatio==0) ) 885 { 886 UINT eldSbr = 0; 887 UINT eldSbrRatio = 0; 888 889 if ( AACENC_OK!=(err=eldSbrConfigurator( 890 hAacConfig->sampleRate, 891 hAacConfig->channelMode, 892 hAacConfig->bitRate, 893 &eldSbr, 894 &eldSbrRatio)) ) 895 { 896 return err; 897 } 898 899 hAacConfig->syntaxFlags |= ((eldSbr) ? AC_SBR_PRESENT : 0); 900 hAacConfig->sbrRatio = eldSbrRatio; 901 } 902 else 903 if ( (config->userSbrRatio==0) && (isSbrActive(hAacConfig)) ) { 904 /* Automatic SBR ratio configuration 905 * - downsampled SBR for ELD 906 * - otherwise always dualrate SBR 907 */ 908 hAacConfig->sbrRatio = (hAacConfig->audioObjectType==AOT_ER_AAC_ELD) ? 1 : 2; 909 } 910 else { 911 /* SBR ratio has been set by the user, so use it. */ 912 hAacConfig->sbrRatio = config->userSbrRatio; 913 } 914 915 { 916 UCHAR tpSignaling=getSbrSignalingMode(hAacConfig->audioObjectType, config->userTpType, config->userTpSignaling, hAacConfig->sbrRatio); 917 918 if ( (hAacConfig->audioObjectType==AOT_AAC_LC || hAacConfig->audioObjectType==AOT_SBR || hAacConfig->audioObjectType==AOT_PS) && 919 (config->userTpType==TT_MP4_LATM_MCP1 || config->userTpType==TT_MP4_LATM_MCP0 || config->userTpType==TT_MP4_LOAS) && 920 (tpSignaling==1) && (config->userTpAmxv==0) ) { 921 /* For backward compatible explicit signaling, AMV1 has to be active */ 922 return AACENC_INVALID_CONFIG; 923 } 924 925 if ( (hAacConfig->audioObjectType==AOT_AAC_LC || hAacConfig->audioObjectType==AOT_SBR || hAacConfig->audioObjectType==AOT_PS) && 926 (tpSignaling==0) && (hAacConfig->sbrRatio==1)) { 927 /* Downsampled SBR has to be signaled explicitely (for transmission of SBR sampling fequency) */ 928 return AACENC_INVALID_CONFIG; 929 } 930 } 931 932 933 934 /* We need the frame length to call aacEncoder_LimitBitrate() */ 935 hAacConfig->bitRate = aacEncoder_LimitBitrate( 936 NULL, 937 hAacConfig->sampleRate, 938 hAacConfig->framelength, 939 hAacConfig->nChannels, 940 hAacConfig->channelMode, 941 hAacConfig->bitRate, 942 hAacConfig->nSubFrames, 943 isSbrActive(hAacConfig), 944 hAacConfig->sbrRatio, 945 hAacConfig->audioObjectType 946 ); 947 948 /* Configure PNS */ 949 if ( ((hAacConfig->bitrateMode>=1) && (hAacConfig->bitrateMode<=5)) /* VBR without PNS. */ 950 || (hAacConfig->useTns == 0) ) /* TNS required. */ 951 { 952 hAacConfig->usePns = 0; 953 } 954 955 if (hAacConfig->epConfig >= 0) { 956 hAacConfig->syntaxFlags |= AC_ER; 957 if (((INT)hAacConfig->channelMode < 1) || ((INT)hAacConfig->channelMode > 7)) { 958 return AACENC_INVALID_CONFIG; /* Cannel config 0 not supported. */ 959 } 960 } 961 962 if ( FDKaacEnc_DetermineEncoderMode(&hAacConfig->channelMode, hAacConfig->nChannels) != AAC_ENC_OK) { 963 return AACENC_INVALID_CONFIG; /* nChannels doesn't match chMode, this is just a check-up */ 964 } 965 966 if ( (hAacConfig->nChannels > hAacEncoder->nMaxAacChannels) 967 || ( (FDKaacEnc_GetChannelModeConfiguration(hAacConfig->channelMode)->nChannelsEff > hAacEncoder->nMaxSbrChannels) && 968 isSbrActive(hAacConfig) ) 969 ) 970 { 971 return AACENC_INVALID_CONFIG; /* not enough channels allocated */ 972 } 973 974 /* Meta data restriction. */ 975 switch (hAacConfig->audioObjectType) 976 { 977 /* Allow metadata support */ 978 case AOT_AAC_LC: 979 case AOT_SBR: 980 case AOT_PS: 981 hAacEncoder->metaDataAllowed = 1; 982 if (((INT)hAacConfig->channelMode < 1) || ((INT)hAacConfig->channelMode > 7)) { 983 config->userMetaDataMode = 0; 984 } 985 break; 986 /* Prohibit metadata support */ 987 default: 988 hAacEncoder->metaDataAllowed = 0; 989 } 990 991 return err; 992 } 993 994 static 995 INT aacenc_SbrCallback( 996 void * self, 997 HANDLE_FDK_BITSTREAM hBs, 998 const INT sampleRateIn, 999 const INT sampleRateOut, 1000 const INT samplesPerFrame, 1001 const AUDIO_OBJECT_TYPE coreCodec, 1002 const MP4_ELEMENT_ID elementID, 1003 const INT elementIndex 1004 ) 1005 { 1006 HANDLE_AACENCODER hAacEncoder = (HANDLE_AACENCODER)self; 1007 1008 sbrEncoder_GetHeader(hAacEncoder->hEnvEnc, hBs, elementIndex, 0); 1009 1010 return 0; 1011 } 1012 1013 static AACENC_ERROR aacEncInit(HANDLE_AACENCODER hAacEncoder, 1014 ULONG InitFlags, 1015 USER_PARAM *config) 1016 { 1017 AACENC_ERROR err = AACENC_OK; 1018 1019 INT aacBufferOffset = 0; 1020 HANDLE_SBR_ENCODER *hSbrEncoder = &hAacEncoder->hEnvEnc; 1021 HANDLE_AACENC_CONFIG hAacConfig = &hAacEncoder->aacConfig; 1022 1023 hAacEncoder->nZerosAppended = 0; /* count appended zeros */ 1024 1025 INT frameLength = hAacConfig->framelength; 1026 1027 if ( (InitFlags & AACENC_INIT_CONFIG) ) 1028 { 1029 CHANNEL_MODE prevChMode = hAacConfig->channelMode; 1030 1031 /* Verify settings and update: config -> heAacEncoder */ 1032 if ( (err=FDKaacEnc_AdjustEncSettings(hAacEncoder, config)) != AACENC_OK ) { 1033 return err; 1034 } 1035 frameLength = hAacConfig->framelength; /* adapt temporal framelength */ 1036 1037 /* Seamless channel reconfiguration in sbr not fully implemented */ 1038 if ( (prevChMode!=hAacConfig->channelMode) && isSbrActive(hAacConfig) ) { 1039 InitFlags |= AACENC_INIT_STATES; 1040 } 1041 } 1042 1043 /* Clear input buffer */ 1044 if ( (InitFlags == AACENC_INIT_ALL) ) { 1045 FDKmemclear(hAacEncoder->inputBuffer, sizeof(INT_PCM)*hAacEncoder->nMaxAacChannels*INPUTBUFFER_SIZE); 1046 } 1047 1048 if ( (InitFlags & AACENC_INIT_CONFIG) ) 1049 { 1050 aacBufferOffset = 0; 1051 if (hAacConfig->audioObjectType == AOT_ER_AAC_ELD) { 1052 hAacEncoder->nDelay = DELAY_AACELD(hAacConfig->framelength); 1053 } else 1054 { 1055 hAacEncoder->nDelay = DELAY_AAC(hAacConfig->framelength); /* AAC encoder delay */ 1056 } 1057 hAacConfig->ancDataBitRate = 0; 1058 } 1059 1060 if ( isSbrActive(hAacConfig) && 1061 ((InitFlags & AACENC_INIT_CONFIG) || (InitFlags & AACENC_INIT_STATES)) ) 1062 { 1063 INT sbrError; 1064 SBR_ELEMENT_INFO sbrElInfo[(8)]; 1065 CHANNEL_MAPPING channelMapping; 1066 1067 if ( FDKaacEnc_InitChannelMapping(hAacConfig->channelMode, 1068 hAacConfig->channelOrder, 1069 &channelMapping) != AAC_ENC_OK ) 1070 { 1071 return AACENC_INIT_ERROR; 1072 } 1073 1074 /* Check return value and if the SBR encoder can handle enough elements */ 1075 if (channelMapping.nElements > (8)) { 1076 return AACENC_INIT_ERROR; 1077 } 1078 1079 aacEncDistributeSbrBits(&channelMapping, sbrElInfo, hAacConfig->bitRate); 1080 1081 UINT initFlag = 0; 1082 initFlag += (InitFlags & AACENC_INIT_STATES) ? 1 : 0; 1083 1084 /* Let the SBR encoder take a look at the configuration and change if required. */ 1085 sbrError = sbrEncoder_Init( 1086 *hSbrEncoder, 1087 sbrElInfo, 1088 channelMapping.nElements, 1089 hAacEncoder->inputBuffer, 1090 &hAacConfig->bandWidth, 1091 &aacBufferOffset, 1092 &hAacConfig->nChannels, 1093 &hAacConfig->sampleRate, 1094 &hAacConfig->sbrRatio, 1095 &frameLength, 1096 hAacConfig->audioObjectType, 1097 &hAacEncoder->nDelay, 1098 (hAacConfig->audioObjectType == AOT_ER_AAC_ELD) ? 1 : TRANS_FAC, 1099 (config->userTpHeaderPeriod!=0xFF) ? config->userTpHeaderPeriod : DEFAULT_HEADER_PERIOD_REPETITION_RATE, 1100 initFlag 1101 ); 1102 1103 /* Suppress AOT reconfiguration and check error status. */ 1104 if (sbrError) { 1105 return AACENC_INIT_SBR_ERROR; 1106 } 1107 1108 if (hAacConfig->nChannels == 1) { 1109 hAacConfig->channelMode = MODE_1; 1110 } 1111 1112 /* Never use PNS if SBR is active */ 1113 if ( hAacConfig->usePns ) { 1114 hAacConfig->usePns = 0; 1115 } 1116 1117 /* estimated bitrate consumed by SBR or PS */ 1118 hAacConfig->ancDataBitRate = sbrEncoder_GetEstimateBitrate(*hSbrEncoder) ; 1119 1120 } /* sbr initialization */ 1121 1122 1123 /* 1124 * Initialize Transport - Module. 1125 */ 1126 if ( (InitFlags & AACENC_INIT_TRANSPORT) ) 1127 { 1128 UINT flags = 0; 1129 1130 FDKaacEnc_MapConfig( 1131 &hAacEncoder->coderConfig, 1132 config, 1133 getSbrSignalingMode(hAacConfig->audioObjectType, config->userTpType, config->userTpSignaling, hAacConfig->sbrRatio), 1134 hAacConfig); 1135 1136 /* create flags for transport encoder */ 1137 if (config->userTpAmxv == 1) { 1138 flags |= TP_FLAG_LATM_AMV; 1139 } 1140 /* Clear output buffer */ 1141 FDKmemclear(hAacEncoder->outBuffer, hAacEncoder->outBufferInBytes*sizeof(UCHAR)); 1142 1143 /* Initialize Bitstream encoder */ 1144 if ( transportEnc_Init(hAacEncoder->hTpEnc, hAacEncoder->outBuffer, hAacEncoder->outBufferInBytes, config->userTpType, &hAacEncoder->coderConfig, flags) != 0) { 1145 return AACENC_INIT_TP_ERROR; 1146 } 1147 1148 } /* transport initialization */ 1149 1150 /* 1151 * Initialize AAC - Core. 1152 */ 1153 if ( (InitFlags & AACENC_INIT_CONFIG) || 1154 (InitFlags & AACENC_INIT_STATES) ) 1155 { 1156 AAC_ENCODER_ERROR err; 1157 err = FDKaacEnc_Initialize(hAacEncoder->hAacEnc, 1158 hAacConfig, 1159 hAacEncoder->hTpEnc, 1160 (InitFlags & AACENC_INIT_STATES) ? 1 : 0); 1161 1162 if (err != AAC_ENC_OK) { 1163 return AACENC_INIT_AAC_ERROR; 1164 } 1165 1166 } /* aac initialization */ 1167 1168 /* 1169 * Initialize Meta Data - Encoder. 1170 */ 1171 if ( hAacEncoder->hMetadataEnc && (hAacEncoder->metaDataAllowed!=0) && 1172 ((InitFlags & AACENC_INIT_CONFIG) ||(InitFlags & AACENC_INIT_STATES)) ) 1173 { 1174 INT inputDataDelay = DELAY_AAC(hAacConfig->framelength); 1175 1176 if ( isSbrActive(hAacConfig) && hSbrEncoder!=NULL) { 1177 inputDataDelay = hAacConfig->sbrRatio*inputDataDelay + sbrEncoder_GetInputDataDelay(*hSbrEncoder); 1178 } 1179 1180 if ( FDK_MetadataEnc_Init(hAacEncoder->hMetadataEnc, 1181 ((InitFlags&AACENC_INIT_STATES) ? 1 : 0), 1182 config->userMetaDataMode, 1183 inputDataDelay, 1184 frameLength, 1185 config->userSamplerate, 1186 config->nChannels, 1187 config->userChannelMode, 1188 hAacConfig->channelOrder) != 0) 1189 { 1190 return AACENC_INIT_META_ERROR; 1191 } 1192 1193 hAacEncoder->nDelay += FDK_MetadataEnc_GetDelay(hAacEncoder->hMetadataEnc); 1194 } 1195 1196 /* 1197 * Update pointer to working buffer. 1198 */ 1199 if ( (InitFlags & AACENC_INIT_CONFIG) ) 1200 { 1201 hAacEncoder->inputBufferOffset = aacBufferOffset; 1202 1203 hAacEncoder->nSamplesToRead = frameLength * config->nChannels; 1204 1205 /* Make nDelay comparison compatible with config->nSamplesRead */ 1206 hAacEncoder->nDelay *= config->nChannels; 1207 1208 } /* parameter changed */ 1209 1210 return AACENC_OK; 1211 } 1212 1213 1214 AACENC_ERROR aacEncOpen( 1215 HANDLE_AACENCODER *phAacEncoder, 1216 const UINT encModules, 1217 const UINT maxChannels 1218 ) 1219 { 1220 AACENC_ERROR err = AACENC_OK; 1221 HANDLE_AACENCODER hAacEncoder = NULL; 1222 1223 if (phAacEncoder == NULL) { 1224 err = AACENC_INVALID_HANDLE; 1225 goto bail; 1226 } 1227 1228 /* allocate memory */ 1229 hAacEncoder = Get_AacEncoder(); 1230 1231 if (hAacEncoder == NULL) { 1232 err = AACENC_MEMORY_ERROR; 1233 goto bail; 1234 } 1235 1236 FDKmemclear(hAacEncoder, sizeof(AACENCODER)); 1237 1238 /* Specify encoder modules to be allocated. */ 1239 if (encModules==0) { 1240 hAacEncoder->encoder_modis = ENC_MODE_FLAG_AAC; 1241 hAacEncoder->encoder_modis |= ENC_MODE_FLAG_SBR; 1242 hAacEncoder->encoder_modis |= ENC_MODE_FLAG_PS; 1243 hAacEncoder->encoder_modis |= ENC_MODE_FLAG_META; 1244 } 1245 else { 1246 /* consider SAC and PS module */ 1247 hAacEncoder->encoder_modis = encModules; 1248 } 1249 1250 /* Determine max channel configuration. */ 1251 if (maxChannels==0) { 1252 hAacEncoder->nMaxAacChannels = (8); 1253 hAacEncoder->nMaxSbrChannels = (8); 1254 } 1255 else { 1256 hAacEncoder->nMaxAacChannels = (maxChannels&0x00FF); 1257 if ( (hAacEncoder->encoder_modis&ENC_MODE_FLAG_SBR) ) { 1258 hAacEncoder->nMaxSbrChannels = (maxChannels&0xFF00) ? (maxChannels>>8) : hAacEncoder->nMaxAacChannels; 1259 } 1260 1261 if ( (hAacEncoder->nMaxAacChannels>(8)) || (hAacEncoder->nMaxSbrChannels>(8)) ) { 1262 err = AACENC_INVALID_CONFIG; 1263 goto bail; 1264 } 1265 } /* maxChannels==0 */ 1266 1267 /* Max number of elements could be tuned any more. */ 1268 hAacEncoder->nMaxAacElements = fixMin((8), hAacEncoder->nMaxAacChannels); 1269 hAacEncoder->nMaxSbrElements = fixMin((8), hAacEncoder->nMaxSbrChannels); 1270 hAacEncoder->nMaxSubFrames = (1); 1271 1272 1273 /* In case of memory overlay, allocate memory out of libraries */ 1274 1275 hAacEncoder->inputBuffer = (INT_PCM*)FDKcalloc(hAacEncoder->nMaxAacChannels*INPUTBUFFER_SIZE, sizeof(INT_PCM)); 1276 1277 /* Open SBR Encoder */ 1278 if (hAacEncoder->encoder_modis&ENC_MODE_FLAG_SBR) { 1279 if ( sbrEncoder_Open(&hAacEncoder->hEnvEnc, 1280 hAacEncoder->nMaxSbrElements, 1281 hAacEncoder->nMaxSbrChannels, 1282 (hAacEncoder->encoder_modis&ENC_MODE_FLAG_PS) ? 1 : 0 ) ) 1283 { 1284 err = AACENC_MEMORY_ERROR; 1285 goto bail; 1286 } 1287 } /* (encoder_modis&ENC_MODE_FLAG_SBR) */ 1288 1289 1290 /* Open Aac Encoder */ 1291 if ( FDKaacEnc_Open(&hAacEncoder->hAacEnc, 1292 hAacEncoder->nMaxAacElements, 1293 hAacEncoder->nMaxAacChannels, 1294 (1)) != AAC_ENC_OK ) 1295 { 1296 err = AACENC_MEMORY_ERROR; 1297 goto bail; 1298 } 1299 1300 { /* Get bitstream outputbuffer size */ 1301 UINT ld_M; 1302 for (ld_M=1; (UINT)(1<<ld_M) < (hAacEncoder->nMaxSubFrames*hAacEncoder->nMaxAacChannels*6144)>>3; ld_M++) ; 1303 hAacEncoder->outBufferInBytes = (1<<ld_M); /* buffer has to be 2^n */ 1304 } 1305 hAacEncoder->outBuffer = GetRam_bsOutbuffer(); 1306 if (OUTPUTBUFFER_SIZE < hAacEncoder->outBufferInBytes ) { 1307 err = AACENC_MEMORY_ERROR; 1308 goto bail; 1309 } 1310 1311 /* Open Meta Data Encoder */ 1312 if (hAacEncoder->encoder_modis&ENC_MODE_FLAG_META) { 1313 if ( FDK_MetadataEnc_Open(&hAacEncoder->hMetadataEnc) ) 1314 { 1315 err = AACENC_MEMORY_ERROR; 1316 goto bail; 1317 } 1318 } /* (encoder_modis&ENC_MODE_FLAG_META) */ 1319 1320 /* Open Transport Encoder */ 1321 if ( transportEnc_Open(&hAacEncoder->hTpEnc) != 0 ) 1322 { 1323 err = AACENC_MEMORY_ERROR; 1324 goto bail; 1325 } 1326 else { 1327 C_ALLOC_SCRATCH_START(pLibInfo, LIB_INFO, FDK_MODULE_LAST); 1328 1329 FDKinitLibInfo( pLibInfo); 1330 transportEnc_GetLibInfo( pLibInfo ); 1331 1332 /* Get capabilty flag for transport encoder. */ 1333 hAacEncoder->CAPF_tpEnc = FDKlibInfo_getCapabilities( pLibInfo, FDK_TPENC); 1334 1335 C_ALLOC_SCRATCH_END(pLibInfo, LIB_INFO, FDK_MODULE_LAST); 1336 } 1337 if ( transportEnc_RegisterSbrCallback(hAacEncoder->hTpEnc, aacenc_SbrCallback, hAacEncoder) != 0 ) { 1338 err = AACENC_INIT_TP_ERROR; 1339 goto bail; 1340 } 1341 1342 /* Initialize encoder instance with default parameters. */ 1343 aacEncDefaultConfig(&hAacEncoder->aacConfig, &hAacEncoder->extParam); 1344 1345 /* Initialize headerPeriod in coderConfig for aacEncoder_GetParam(). */ 1346 hAacEncoder->coderConfig.headerPeriod = hAacEncoder->extParam.userTpHeaderPeriod; 1347 1348 /* All encoder modules have to be initialized */ 1349 hAacEncoder->InitFlags = AACENC_INIT_ALL; 1350 1351 /* Return encoder instance */ 1352 *phAacEncoder = hAacEncoder; 1353 1354 return err; 1355 1356 bail: 1357 aacEncClose(&hAacEncoder); 1358 1359 return err; 1360 } 1361 1362 1363 1364 AACENC_ERROR aacEncClose(HANDLE_AACENCODER *phAacEncoder) 1365 { 1366 AACENC_ERROR err = AACENC_OK; 1367 1368 if (phAacEncoder == NULL) { 1369 err = AACENC_INVALID_HANDLE; 1370 goto bail; 1371 } 1372 1373 if (*phAacEncoder != NULL) { 1374 HANDLE_AACENCODER hAacEncoder = *phAacEncoder; 1375 1376 1377 if (hAacEncoder->inputBuffer!=NULL) { 1378 FDKfree(hAacEncoder->inputBuffer); 1379 hAacEncoder->inputBuffer = NULL; 1380 } 1381 1382 if (hAacEncoder->outBuffer) { 1383 FreeRam_bsOutbuffer(&hAacEncoder->outBuffer); 1384 } 1385 1386 if (hAacEncoder->hEnvEnc) { 1387 sbrEncoder_Close (&hAacEncoder->hEnvEnc); 1388 } 1389 if (hAacEncoder->hAacEnc) { 1390 FDKaacEnc_Close (&hAacEncoder->hAacEnc); 1391 } 1392 1393 transportEnc_Close(&hAacEncoder->hTpEnc); 1394 1395 if (hAacEncoder->hMetadataEnc) { 1396 FDK_MetadataEnc_Close (&hAacEncoder->hMetadataEnc); 1397 } 1398 1399 Free_AacEncoder(phAacEncoder); 1400 } 1401 1402 bail: 1403 return err; 1404 } 1405 1406 AACENC_ERROR aacEncEncode( 1407 const HANDLE_AACENCODER hAacEncoder, 1408 const AACENC_BufDesc *inBufDesc, 1409 const AACENC_BufDesc *outBufDesc, 1410 const AACENC_InArgs *inargs, 1411 AACENC_OutArgs *outargs 1412 ) 1413 { 1414 AACENC_ERROR err = AACENC_OK; 1415 INT i, nBsBytes = 0; 1416 INT outBytes[(1)]; 1417 int nExtensions = 0; 1418 int ancDataExtIdx = -1; 1419 1420 /* deal with valid encoder handle */ 1421 if (hAacEncoder==NULL) { 1422 err = AACENC_INVALID_HANDLE; 1423 goto bail; 1424 } 1425 1426 1427 /* 1428 * Adjust user settings and trigger reinitialization. 1429 */ 1430 if (hAacEncoder->InitFlags!=0) { 1431 1432 err = aacEncInit(hAacEncoder, 1433 hAacEncoder->InitFlags, 1434 &hAacEncoder->extParam); 1435 1436 if (err!=AACENC_OK) { 1437 /* keep init flags alive! */ 1438 goto bail; 1439 } 1440 hAacEncoder->InitFlags = AACENC_INIT_NONE; 1441 } 1442 1443 if (outargs!=NULL) { 1444 FDKmemclear(outargs, sizeof(AACENC_OutArgs)); 1445 } 1446 1447 if (outBufDesc!=NULL) { 1448 for (i=0; i<outBufDesc->numBufs; i++) { 1449 if (outBufDesc->bufs[i]!=NULL) { 1450 FDKmemclear(outBufDesc->bufs[i], outBufDesc->bufSizes[i]); 1451 } 1452 } 1453 } 1454 1455 /* 1456 * If only encoder handle given, independent (re)initialization can be triggered. 1457 */ 1458 if ( (hAacEncoder!=NULL) & (inBufDesc==NULL) && (outBufDesc==NULL) && (inargs==NULL) && (outargs==NULL) ) { 1459 goto bail; 1460 } 1461 1462 /* reset buffer wich signals number of valid bytes in output bitstream buffer */ 1463 FDKmemclear(outBytes, hAacEncoder->aacConfig.nSubFrames*sizeof(INT)); 1464 1465 /* 1466 * Manage incoming audio samples. 1467 */ 1468 if ( (inargs->numInSamples > 0) && (getBufDescIdx(inBufDesc,IN_AUDIO_DATA) != -1) ) 1469 { 1470 /* Fetch data until nSamplesToRead reached */ 1471 INT idx = getBufDescIdx(inBufDesc,IN_AUDIO_DATA); 1472 INT newSamples = fixMax(0,fixMin(inargs->numInSamples, hAacEncoder->nSamplesToRead-hAacEncoder->nSamplesRead)); 1473 INT_PCM *pIn = hAacEncoder->inputBuffer+hAacEncoder->inputBufferOffset+hAacEncoder->nSamplesRead; 1474 1475 /* Copy new input samples to internal buffer */ 1476 if (inBufDesc->bufElSizes[idx]==(INT)sizeof(INT_PCM)) { 1477 FDKmemcpy(pIn, (INT_PCM*)inBufDesc->bufs[idx], newSamples*sizeof(INT_PCM)); /* Fast copy. */ 1478 } 1479 else if (inBufDesc->bufElSizes[idx]>(INT)sizeof(INT_PCM)) { 1480 for (i=0; i<newSamples; i++) { 1481 pIn[i] = (INT_PCM)(((LONG*)inBufDesc->bufs[idx])[i]>>16); /* Convert 32 to 16 bit. */ 1482 } 1483 } 1484 else { 1485 for (i=0; i<newSamples; i++) { 1486 pIn[i] = ((INT_PCM)(((SHORT*)inBufDesc->bufs[idx])[i]))<<16; /* Convert 16 to 32 bit. */ 1487 } 1488 } 1489 hAacEncoder->nSamplesRead += newSamples; 1490 1491 /* Number of fetched input buffer samples. */ 1492 outargs->numInSamples = newSamples; 1493 } 1494 1495 /* input buffer completely filled ? */ 1496 if (hAacEncoder->nSamplesRead < hAacEncoder->nSamplesToRead) 1497 { 1498 /* - eof reached and flushing enabled, or 1499 - return to main and wait for further incoming audio samples */ 1500 if (inargs->numInSamples==-1) 1501 { 1502 if ( (hAacEncoder->nZerosAppended < hAacEncoder->nDelay) 1503 ) 1504 { 1505 int nZeros = hAacEncoder->nSamplesToRead - hAacEncoder->nSamplesRead; 1506 1507 FDK_ASSERT(nZeros >= 0); 1508 1509 /* clear out until end-of-buffer */ 1510 if (nZeros) { 1511 FDKmemclear(hAacEncoder->inputBuffer+hAacEncoder->inputBufferOffset+hAacEncoder->nSamplesRead, sizeof(INT_PCM)*nZeros ); 1512 hAacEncoder->nZerosAppended += nZeros; 1513 hAacEncoder->nSamplesRead = hAacEncoder->nSamplesToRead; 1514 } 1515 } 1516 else { /* flushing completed */ 1517 err = AACENC_ENCODE_EOF; /* eof reached */ 1518 goto bail; 1519 } 1520 } 1521 else { /* inargs->numInSamples!= -1 */ 1522 goto bail; /* not enough samples in input buffer and no flushing enabled */ 1523 } 1524 } 1525 1526 /* init payload */ 1527 FDKmemclear(hAacEncoder->extPayload, sizeof(AACENC_EXT_PAYLOAD) * MAX_TOTAL_EXT_PAYLOADS); 1528 for (i = 0; i < MAX_TOTAL_EXT_PAYLOADS; i++) { 1529 hAacEncoder->extPayload[i].associatedChElement = -1; 1530 } 1531 FDKmemclear(hAacEncoder->extPayloadData, sizeof(hAacEncoder->extPayloadData)); 1532 FDKmemclear(hAacEncoder->extPayloadSize, sizeof(hAacEncoder->extPayloadSize)); 1533 1534 1535 /* 1536 * Calculate Meta Data info. 1537 */ 1538 if ( (hAacEncoder->hMetadataEnc!=NULL) && (hAacEncoder->metaDataAllowed!=0) ) { 1539 1540 const AACENC_MetaData *pMetaData = NULL; 1541 AACENC_EXT_PAYLOAD *pMetaDataExtPayload = NULL; 1542 UINT nMetaDataExtensions = 0; 1543 INT matrix_mixdown_idx = 0; 1544 1545 /* New meta data info available ? */ 1546 if ( getBufDescIdx(inBufDesc,IN_METADATA_SETUP) != -1 ) { 1547 pMetaData = (AACENC_MetaData*)inBufDesc->bufs[getBufDescIdx(inBufDesc,IN_METADATA_SETUP)]; 1548 } 1549 1550 FDK_MetadataEnc_Process(hAacEncoder->hMetadataEnc, 1551 hAacEncoder->inputBuffer+hAacEncoder->inputBufferOffset, 1552 hAacEncoder->nSamplesRead, 1553 pMetaData, 1554 &pMetaDataExtPayload, 1555 &nMetaDataExtensions, 1556 &matrix_mixdown_idx 1557 ); 1558 1559 for (i=0; i<(INT)nMetaDataExtensions; i++) { /* Get meta data extension payload. */ 1560 hAacEncoder->extPayload[nExtensions++] = pMetaDataExtPayload[i]; 1561 } 1562 1563 if ( (matrix_mixdown_idx!=-1) 1564 && ((hAacEncoder->extParam.userChannelMode==MODE_1_2_2)||(hAacEncoder->extParam.userChannelMode==MODE_1_2_2_1)) ) 1565 { 1566 /* Set matrix mixdown coefficient. */ 1567 UINT pceValue = (UINT)( (1<<3) | ((matrix_mixdown_idx&0x3)<<1) | 1 ); 1568 if (hAacEncoder->extParam.userPceAdditions != pceValue) { 1569 hAacEncoder->extParam.userPceAdditions = pceValue; 1570 hAacEncoder->InitFlags |= AACENC_INIT_TRANSPORT; 1571 } 1572 } 1573 } 1574 1575 1576 if ( isSbrActive(&hAacEncoder->aacConfig) ) { 1577 1578 INT nPayload = 0; 1579 1580 /* 1581 * Encode SBR data. 1582 */ 1583 if (sbrEncoder_EncodeFrame(hAacEncoder->hEnvEnc, 1584 hAacEncoder->inputBuffer, 1585 hAacEncoder->extParam.nChannels, 1586 hAacEncoder->extPayloadSize[nPayload], 1587 hAacEncoder->extPayloadData[nPayload] 1588 #if defined(EVAL_PACKAGE_SILENCE) || defined(EVAL_PACKAGE_SBR_SILENCE) 1589 ,hAacEncoder->hAacEnc->clearOutput 1590 #endif 1591 )) 1592 { 1593 err = AACENC_ENCODE_ERROR; 1594 goto bail; 1595 } 1596 else { 1597 /* Add SBR extension payload */ 1598 for (i = 0; i < (8); i++) { 1599 if (hAacEncoder->extPayloadSize[nPayload][i] > 0) { 1600 hAacEncoder->extPayload[nExtensions].pData = hAacEncoder->extPayloadData[nPayload][i]; 1601 { 1602 hAacEncoder->extPayload[nExtensions].dataSize = hAacEncoder->extPayloadSize[nPayload][i]; 1603 hAacEncoder->extPayload[nExtensions].associatedChElement = i; 1604 } 1605 hAacEncoder->extPayload[nExtensions].dataType = EXT_SBR_DATA; /* Once SBR Encoder supports SBR CRC set EXT_SBR_DATA_CRC */ 1606 nExtensions++; /* or EXT_SBR_DATA according to configuration. */ 1607 FDK_ASSERT(nExtensions<=MAX_TOTAL_EXT_PAYLOADS); 1608 } 1609 } 1610 nPayload++; 1611 } 1612 } /* sbrEnabled */ 1613 1614 if ( (inargs->numAncBytes > 0) && ( getBufDescIdx(inBufDesc,IN_ANCILLRY_DATA)!=-1 ) ) { 1615 INT idx = getBufDescIdx(inBufDesc,IN_ANCILLRY_DATA); 1616 hAacEncoder->extPayload[nExtensions].dataSize = inargs->numAncBytes * 8; 1617 hAacEncoder->extPayload[nExtensions].pData = (UCHAR*)inBufDesc->bufs[idx]; 1618 hAacEncoder->extPayload[nExtensions].dataType = EXT_DATA_ELEMENT; 1619 hAacEncoder->extPayload[nExtensions].associatedChElement = -1; 1620 ancDataExtIdx = nExtensions; /* store index */ 1621 nExtensions++; 1622 } 1623 1624 /* 1625 * Encode AAC - Core. 1626 */ 1627 if ( FDKaacEnc_EncodeFrame( hAacEncoder->hAacEnc, 1628 hAacEncoder->hTpEnc, 1629 hAacEncoder->inputBuffer, 1630 outBytes, 1631 hAacEncoder->extPayload 1632 ) != AAC_ENC_OK ) 1633 { 1634 err = AACENC_ENCODE_ERROR; 1635 goto bail; 1636 } 1637 1638 if (ancDataExtIdx >= 0) { 1639 outargs->numAncBytes = inargs->numAncBytes - (hAacEncoder->extPayload[ancDataExtIdx].dataSize>>3); 1640 } 1641 1642 /* samples exhausted */ 1643 hAacEncoder->nSamplesRead -= hAacEncoder->nSamplesToRead; 1644 1645 /* 1646 * Delay balancing buffer handling 1647 */ 1648 if (isSbrActive(&hAacEncoder->aacConfig)) { 1649 sbrEncoder_UpdateBuffers(hAacEncoder->hEnvEnc, hAacEncoder->inputBuffer); 1650 } 1651 1652 /* 1653 * Make bitstream public 1654 */ 1655 if (outBufDesc->numBufs>=1) { 1656 1657 INT bsIdx = getBufDescIdx(outBufDesc,OUT_BITSTREAM_DATA); 1658 INT auIdx = getBufDescIdx(outBufDesc,OUT_AU_SIZES); 1659 1660 for (i=0,nBsBytes=0; i<hAacEncoder->aacConfig.nSubFrames; i++) { 1661 nBsBytes += outBytes[i]; 1662 1663 if (auIdx!=-1) { 1664 ((INT*)outBufDesc->bufs[auIdx])[i] = outBytes[i]; 1665 } 1666 } 1667 1668 if ( (bsIdx!=-1) && (outBufDesc->bufSizes[bsIdx]>=nBsBytes) ) { 1669 FDKmemcpy(outBufDesc->bufs[bsIdx], hAacEncoder->outBuffer, sizeof(UCHAR)*nBsBytes); 1670 outargs->numOutBytes = nBsBytes; 1671 } 1672 else { 1673 /* output buffer too small, can't write valid bitstream */ 1674 err = AACENC_ENCODE_ERROR; 1675 goto bail; 1676 } 1677 } 1678 1679 bail: 1680 if (err == AACENC_ENCODE_ERROR) { 1681 /* All encoder modules have to be initialized */ 1682 hAacEncoder->InitFlags = AACENC_INIT_ALL; 1683 } 1684 1685 return err; 1686 } 1687 1688 static 1689 AAC_ENCODER_ERROR aacEncGetConf(HANDLE_AACENCODER hAacEncoder, 1690 UINT *size, 1691 UCHAR *confBuffer) 1692 { 1693 FDK_BITSTREAM tmpConf; 1694 UINT confType; 1695 UCHAR buf[64]; 1696 int err; 1697 1698 /* Init bit buffer */ 1699 FDKinitBitStream(&tmpConf, buf, 64, 0, BS_WRITER); 1700 1701 /* write conf in tmp buffer */ 1702 err = transportEnc_GetConf(hAacEncoder->hTpEnc, &hAacEncoder->coderConfig, &tmpConf, &confType); 1703 1704 /* copy data to outbuffer: length in bytes */ 1705 FDKbyteAlign(&tmpConf, 0); 1706 1707 /* Check buffer size */ 1708 if (FDKgetValidBits(&tmpConf) > ((*size)<<3)) 1709 return AAC_ENC_UNKNOWN; 1710 1711 FDKfetchBuffer(&tmpConf, confBuffer, size); 1712 1713 if (err != 0) 1714 return AAC_ENC_UNKNOWN; 1715 else 1716 return AAC_ENC_OK; 1717 } 1718 1719 1720 AACENC_ERROR aacEncGetLibInfo(LIB_INFO *info) 1721 { 1722 int i = 0; 1723 1724 if (info == NULL) { 1725 return AACENC_INVALID_HANDLE; 1726 } 1727 1728 FDK_toolsGetLibInfo( info ); 1729 transportEnc_GetLibInfo( info ); 1730 1731 sbrEncoder_GetLibInfo( info ); 1732 1733 /* search for next free tab */ 1734 for (i = 0; i < FDK_MODULE_LAST; i++) { 1735 if (info[i].module_id == FDK_NONE) break; 1736 } 1737 if (i == FDK_MODULE_LAST) { 1738 return AACENC_INIT_ERROR; 1739 } 1740 1741 info[i].module_id = FDK_AACENC; 1742 info[i].build_date = (char*)AACENCODER_LIB_BUILD_DATE; 1743 info[i].build_time = (char*)AACENCODER_LIB_BUILD_TIME; 1744 info[i].title = (char*)AACENCODER_LIB_TITLE; 1745 info[i].version = LIB_VERSION(AACENCODER_LIB_VL0, AACENCODER_LIB_VL1, AACENCODER_LIB_VL2);; 1746 LIB_VERSION_STRING(&info[i]); 1747 1748 /* Capability flags */ 1749 info[i].flags = 0 1750 | CAPF_AAC_1024 | CAPF_AAC_LC 1751 | CAPF_AAC_512 1752 | CAPF_AAC_480 1753 | CAPF_AAC_DRC 1754 ; 1755 /* End of flags */ 1756 1757 return AACENC_OK; 1758 } 1759 1760 AACENC_ERROR aacEncoder_SetParam( 1761 const HANDLE_AACENCODER hAacEncoder, 1762 const AACENC_PARAM param, 1763 const UINT value 1764 ) 1765 { 1766 AACENC_ERROR err = AACENC_OK; 1767 USER_PARAM *settings = &hAacEncoder->extParam; 1768 1769 /* check encoder handle */ 1770 if (hAacEncoder == NULL) { 1771 err = AACENC_INVALID_HANDLE; 1772 goto bail; 1773 } 1774 1775 /* apply param value */ 1776 switch (param) 1777 { 1778 case AACENC_AOT: 1779 if (settings->userAOT != (AUDIO_OBJECT_TYPE)value) { 1780 /* check if AOT matches the allocated modules */ 1781 switch ( value ) { 1782 case AOT_PS: 1783 case AOT_MP2_PS: 1784 if (!(hAacEncoder->encoder_modis & (ENC_MODE_FLAG_PS))) { 1785 err = AACENC_INVALID_CONFIG; 1786 goto bail; 1787 } 1788 case AOT_SBR: 1789 case AOT_MP2_SBR: 1790 if (!(hAacEncoder->encoder_modis & (ENC_MODE_FLAG_SBR))) { 1791 err = AACENC_INVALID_CONFIG; 1792 goto bail; 1793 } 1794 case AOT_AAC_LC: 1795 case AOT_MP2_AAC_LC: 1796 case AOT_ER_AAC_LD: 1797 case AOT_ER_AAC_ELD: 1798 if (!(hAacEncoder->encoder_modis & (ENC_MODE_FLAG_AAC))) { 1799 err = AACENC_INVALID_CONFIG; 1800 goto bail; 1801 } 1802 break; 1803 default: 1804 err = AACENC_INVALID_CONFIG; 1805 goto bail; 1806 }/* switch value */ 1807 settings->userAOT = (AUDIO_OBJECT_TYPE)value; 1808 hAacEncoder->InitFlags |= AACENC_INIT_CONFIG | AACENC_INIT_STATES | AACENC_INIT_TRANSPORT; 1809 } 1810 break; 1811 case AACENC_BITRATE: 1812 if (settings->userBitrate != value) { 1813 settings->userBitrate = value; 1814 hAacEncoder->InitFlags |= AACENC_INIT_CONFIG | AACENC_INIT_TRANSPORT; 1815 } 1816 break; 1817 case AACENC_BITRATEMODE: 1818 if (settings->userBitrateMode != value) { 1819 switch ( value ) { 1820 case 0: 1821 case 8: 1822 settings->userBitrateMode = value; 1823 hAacEncoder->InitFlags |= AACENC_INIT_CONFIG | AACENC_INIT_TRANSPORT; 1824 break; 1825 default: 1826 err = AACENC_INVALID_CONFIG; 1827 break; 1828 } /* switch value */ 1829 } 1830 break; 1831 case AACENC_SAMPLERATE: 1832 if (settings->userSamplerate != value) { 1833 if ( !( (value==8000) || (value==11025) || (value==12000) || (value==16000) || (value==22050) || (value==24000) || 1834 (value==32000) || (value==44100) || (value==48000) || (value==64000) || (value==88200) || (value==96000) ) ) 1835 { 1836 err = AACENC_INVALID_CONFIG; 1837 break; 1838 } 1839 settings->userSamplerate = value; 1840 hAacEncoder->nSamplesRead = 0; /* reset internal inputbuffer */ 1841 hAacEncoder->InitFlags |= AACENC_INIT_CONFIG | AACENC_INIT_STATES | AACENC_INIT_TRANSPORT; 1842 } 1843 break; 1844 case AACENC_CHANNELMODE: 1845 if (settings->userChannelMode != (CHANNEL_MODE)value) { 1846 const CHANNEL_MODE_CONFIG_TAB* pConfig = FDKaacEnc_GetChannelModeConfiguration((CHANNEL_MODE)value); 1847 if (pConfig==NULL) { 1848 err = AACENC_INVALID_CONFIG; 1849 break; 1850 } 1851 if ( (pConfig->nElements > hAacEncoder->nMaxAacElements) 1852 || (pConfig->nChannelsEff > hAacEncoder->nMaxAacChannels) 1853 || !(((value>=1) && (value<=7))||((value>=33) && (value<=34))) 1854 ) 1855 { 1856 err = AACENC_INVALID_CONFIG; 1857 break; 1858 } 1859 1860 settings->userChannelMode = (CHANNEL_MODE)value; 1861 settings->nChannels = pConfig->nChannels; 1862 hAacEncoder->nSamplesRead = 0; /* reset internal inputbuffer */ 1863 hAacEncoder->InitFlags |= AACENC_INIT_CONFIG | AACENC_INIT_TRANSPORT; 1864 } 1865 break; 1866 case AACENC_BANDWIDTH: 1867 if (settings->userBandwidth != value) { 1868 settings->userBandwidth = value; 1869 hAacEncoder->InitFlags |= AACENC_INIT_CONFIG; 1870 } 1871 break; 1872 case AACENC_CHANNELORDER: 1873 if (hAacEncoder->aacConfig.channelOrder != (CHANNEL_ORDER)value) { 1874 if (! ((value==0) || (value==1)) ) { 1875 err = AACENC_INVALID_CONFIG; 1876 break; 1877 } 1878 hAacEncoder->aacConfig.channelOrder = (CHANNEL_ORDER)value; 1879 hAacEncoder->nSamplesRead = 0; /* reset internal inputbuffer */ 1880 hAacEncoder->InitFlags |= AACENC_INIT_CONFIG | AACENC_INIT_STATES | AACENC_INIT_TRANSPORT; 1881 } 1882 break; 1883 case AACENC_AFTERBURNER: 1884 if (settings->userAfterburner != value) { 1885 if (! ((value==0) || (value==1)) ) { 1886 err = AACENC_INVALID_CONFIG; 1887 break; 1888 } 1889 settings->userAfterburner = value; 1890 hAacEncoder->InitFlags |= AACENC_INIT_CONFIG; 1891 } 1892 break; 1893 case AACENC_GRANULE_LENGTH: 1894 if (settings->userFramelength != value) { 1895 switch (value) { 1896 case 1024: 1897 case 512: 1898 case 480: 1899 settings->userFramelength = value; 1900 hAacEncoder->InitFlags |= AACENC_INIT_CONFIG | AACENC_INIT_TRANSPORT; 1901 break; 1902 default: 1903 err = AACENC_INVALID_CONFIG; 1904 break; 1905 } 1906 } 1907 break; 1908 case AACENC_SBR_RATIO: 1909 if (settings->userSbrRatio != value) { 1910 if (! ((value==0) || (value==1) || (value==2)) ) { 1911 err = AACENC_INVALID_CONFIG; 1912 break; 1913 } 1914 settings->userSbrRatio = value; 1915 hAacEncoder->InitFlags |= AACENC_INIT_CONFIG | AACENC_INIT_STATES | AACENC_INIT_TRANSPORT; 1916 } 1917 break; 1918 case AACENC_SBR_MODE: 1919 if (settings->userSbrEnabled != value) { 1920 settings->userSbrEnabled = value; 1921 hAacEncoder->InitFlags |= AACENC_INIT_CONFIG | AACENC_INIT_STATES | AACENC_INIT_TRANSPORT; 1922 } 1923 break; 1924 case AACENC_TRANSMUX: 1925 if (settings->userTpType != (TRANSPORT_TYPE)value) { 1926 1927 TRANSPORT_TYPE type = (TRANSPORT_TYPE)value; 1928 UINT flags = hAacEncoder->CAPF_tpEnc; 1929 1930 if ( !( ((type==TT_MP4_ADIF) && (flags&CAPF_ADIF)) 1931 || ((type==TT_MP4_ADTS) && (flags&CAPF_ADTS)) 1932 || ((type==TT_MP4_LATM_MCP0) && ((flags&CAPF_LATM) && (flags&CAPF_RAWPACKETS))) 1933 || ((type==TT_MP4_LATM_MCP1) && ((flags&CAPF_LATM) && (flags&CAPF_RAWPACKETS))) 1934 || ((type==TT_MP4_LOAS) && (flags&CAPF_LOAS)) 1935 || ((type==TT_MP4_RAW) && (flags&CAPF_RAWPACKETS)) 1936 ) ) 1937 { 1938 err = AACENC_INVALID_CONFIG; 1939 break; 1940 } 1941 settings->userTpType = (TRANSPORT_TYPE)value; 1942 hAacEncoder->InitFlags |= AACENC_INIT_TRANSPORT; 1943 } 1944 break; 1945 case AACENC_SIGNALING_MODE: 1946 if (settings->userTpSignaling != value) { 1947 if ( !((value==0) || (value==1) || (value==2)) ) { 1948 err = AACENC_INVALID_CONFIG; 1949 break; 1950 } 1951 settings->userTpSignaling = value; 1952 hAacEncoder->InitFlags |= AACENC_INIT_TRANSPORT; 1953 } 1954 break; 1955 case AACENC_PROTECTION: 1956 if (settings->userTpProtection != value) { 1957 if ( !((value==0) || (value==1)) ) { 1958 err = AACENC_INVALID_CONFIG; 1959 break; 1960 } 1961 settings->userTpProtection = value; 1962 hAacEncoder->InitFlags |= AACENC_INIT_TRANSPORT; 1963 } 1964 break; 1965 case AACENC_HEADER_PERIOD: 1966 if (settings->userTpHeaderPeriod != value) { 1967 settings->userTpHeaderPeriod = value; 1968 hAacEncoder->InitFlags |= AACENC_INIT_TRANSPORT; 1969 } 1970 break; 1971 case AACENC_TPSUBFRAMES: 1972 if (settings->userTpNsubFrames != value) { 1973 if (! ( (value>=1) && (value<=4) ) ) { 1974 err = AACENC_INVALID_CONFIG; 1975 break; 1976 } 1977 settings->userTpNsubFrames = value; 1978 hAacEncoder->InitFlags |= AACENC_INIT_TRANSPORT; 1979 } 1980 break; 1981 case AACENC_ANCILLARY_BITRATE: 1982 if (settings->userAncDataRate != value) { 1983 settings->userAncDataRate = value; 1984 } 1985 break; 1986 case AACENC_CONTROL_STATE: 1987 if (hAacEncoder->InitFlags != value) { 1988 if (value&AACENC_RESET_INBUFFER) { 1989 hAacEncoder->nSamplesRead = 0; 1990 } 1991 hAacEncoder->InitFlags = value; 1992 } 1993 break; 1994 case AACENC_METADATA_MODE: 1995 if ((UINT)settings->userMetaDataMode != value) { 1996 if ( !((value>=0) && (value<=2)) ) { 1997 err = AACENC_INVALID_CONFIG; 1998 break; 1999 } 2000 settings->userMetaDataMode = value; 2001 hAacEncoder->InitFlags |= AACENC_INIT_CONFIG; 2002 } 2003 break; 2004 default: 2005 err = AACENC_UNSUPPORTED_PARAMETER; 2006 break; 2007 } /* switch(param) */ 2008 2009 bail: 2010 return err; 2011 } 2012 2013 UINT aacEncoder_GetParam( 2014 const HANDLE_AACENCODER hAacEncoder, 2015 const AACENC_PARAM param 2016 ) 2017 { 2018 UINT value = 0; 2019 USER_PARAM *settings = &hAacEncoder->extParam; 2020 2021 /* check encoder handle */ 2022 if (hAacEncoder == NULL) { 2023 goto bail; 2024 } 2025 2026 /* apply param value */ 2027 switch (param) 2028 { 2029 case AACENC_AOT: 2030 value = (UINT)hAacEncoder->aacConfig.audioObjectType; 2031 break; 2032 case AACENC_BITRATE: 2033 value = (UINT)((hAacEncoder->aacConfig.bitrateMode==AACENC_BR_MODE_CBR) ? hAacEncoder->aacConfig.bitRate : -1); 2034 break; 2035 case AACENC_BITRATEMODE: 2036 value = (UINT)hAacEncoder->aacConfig.bitrateMode; 2037 break; 2038 case AACENC_SAMPLERATE: 2039 value = (UINT)hAacEncoder->coderConfig.extSamplingRate; 2040 break; 2041 case AACENC_CHANNELMODE: 2042 value = (UINT)hAacEncoder->aacConfig.channelMode; 2043 break; 2044 case AACENC_BANDWIDTH: 2045 value = (UINT)hAacEncoder->aacConfig.bandWidth; 2046 break; 2047 case AACENC_CHANNELORDER: 2048 value = (UINT)hAacEncoder->aacConfig.channelOrder; 2049 break; 2050 case AACENC_AFTERBURNER: 2051 value = (UINT)hAacEncoder->aacConfig.useRequant; 2052 break; 2053 case AACENC_GRANULE_LENGTH: 2054 value = (UINT)hAacEncoder->aacConfig.framelength; 2055 break; 2056 case AACENC_SBR_RATIO: 2057 value = isSbrActive(&hAacEncoder->aacConfig) ? hAacEncoder->aacConfig.sbrRatio : 0; 2058 break; 2059 case AACENC_SBR_MODE: 2060 value = (UINT) (hAacEncoder->aacConfig.syntaxFlags & AC_SBR_PRESENT) ? 1 : 0; 2061 break; 2062 case AACENC_TRANSMUX: 2063 value = (UINT)settings->userTpType; 2064 break; 2065 case AACENC_SIGNALING_MODE: 2066 value = (UINT)getSbrSignalingMode(hAacEncoder->aacConfig.audioObjectType, settings->userTpType, settings->userTpSignaling, hAacEncoder->aacConfig.sbrRatio); 2067 break; 2068 case AACENC_PROTECTION: 2069 value = (UINT)settings->userTpProtection; 2070 break; 2071 case AACENC_HEADER_PERIOD: 2072 value = (UINT)hAacEncoder->coderConfig.headerPeriod; 2073 break; 2074 case AACENC_TPSUBFRAMES: 2075 value = (UINT)settings->userTpNsubFrames; 2076 break; 2077 case AACENC_ANCILLARY_BITRATE: 2078 value = (UINT)hAacEncoder->aacConfig.anc_Rate; 2079 break; 2080 case AACENC_CONTROL_STATE: 2081 value = (UINT)hAacEncoder->InitFlags; 2082 break; 2083 case AACENC_METADATA_MODE: 2084 value = (hAacEncoder->metaDataAllowed==0) ? 0 : (UINT)settings->userMetaDataMode; 2085 break; 2086 default: 2087 //err = MPS_INVALID_PARAMETER; 2088 break; 2089 } /* switch(param) */ 2090 2091 bail: 2092 return value; 2093 } 2094 2095 AACENC_ERROR aacEncInfo( 2096 const HANDLE_AACENCODER hAacEncoder, 2097 AACENC_InfoStruct *pInfo 2098 ) 2099 { 2100 AACENC_ERROR err = AACENC_OK; 2101 2102 FDKmemclear(pInfo, sizeof(AACENC_InfoStruct)); 2103 pInfo->confSize = 64; /* pre-initialize */ 2104 2105 pInfo->maxOutBufBytes = ((hAacEncoder->nMaxAacChannels*6144)+7)>>3; 2106 pInfo->maxAncBytes = hAacEncoder->aacConfig.maxAncBytesPerAU; 2107 pInfo->inBufFillLevel = hAacEncoder->nSamplesRead/hAacEncoder->extParam.nChannels; 2108 pInfo->inputChannels = hAacEncoder->extParam.nChannels; 2109 pInfo->frameLength = hAacEncoder->nSamplesToRead/hAacEncoder->extParam.nChannels; 2110 pInfo->encoderDelay = hAacEncoder->nDelay/hAacEncoder->extParam.nChannels; 2111 2112 /* Get encoder configuration */ 2113 if ( aacEncGetConf(hAacEncoder, &pInfo->confSize, &pInfo->confBuf[0]) != AAC_ENC_OK) { 2114 err = AACENC_INIT_ERROR; 2115 goto bail; 2116 } 2117 bail: 2118 return err; 2119 } 2120 2121