12228e360595641dd906bf1773307f43d304f5b2The Android Open Source Project
22228e360595641dd906bf1773307f43d304f5b2The Android Open Source Project/* -----------------------------------------------------------------------------------------------------------
32228e360595641dd906bf1773307f43d304f5b2The Android Open Source ProjectSoftware License for The Fraunhofer FDK AAC Codec Library for Android
42228e360595641dd906bf1773307f43d304f5b2The Android Open Source Project
54f0d97057c5c640b25518358886f8c47da9fc052Jean-Michel Trivi� Copyright  1995 - 2013 Fraunhofer-Gesellschaft zur F�rderung der angewandten Forschung e.V.
62228e360595641dd906bf1773307f43d304f5b2The Android Open Source Project  All rights reserved.
72228e360595641dd906bf1773307f43d304f5b2The Android Open Source Project
82228e360595641dd906bf1773307f43d304f5b2The Android Open Source Project 1.    INTRODUCTION
92228e360595641dd906bf1773307f43d304f5b2The Android Open Source ProjectThe Fraunhofer FDK AAC Codec Library for Android ("FDK AAC Codec") is software that implements
102228e360595641dd906bf1773307f43d304f5b2The Android Open Source Projectthe MPEG Advanced Audio Coding ("AAC") encoding and decoding scheme for digital audio.
112228e360595641dd906bf1773307f43d304f5b2The Android Open Source ProjectThis FDK AAC Codec software is intended to be used on a wide variety of Android devices.
122228e360595641dd906bf1773307f43d304f5b2The Android Open Source Project
132228e360595641dd906bf1773307f43d304f5b2The Android Open Source ProjectAAC's HE-AAC and HE-AAC v2 versions are regarded as today's most efficient general perceptual
142228e360595641dd906bf1773307f43d304f5b2The Android Open Source Projectaudio codecs. AAC-ELD is considered the best-performing full-bandwidth communications codec by
152228e360595641dd906bf1773307f43d304f5b2The Android Open Source Projectindependent studies and is widely deployed. AAC has been standardized by ISO and IEC as part
162228e360595641dd906bf1773307f43d304f5b2The Android Open Source Projectof the MPEG specifications.
172228e360595641dd906bf1773307f43d304f5b2The Android Open Source Project
182228e360595641dd906bf1773307f43d304f5b2The Android Open Source ProjectPatent licenses for necessary patent claims for the FDK AAC Codec (including those of Fraunhofer)
192228e360595641dd906bf1773307f43d304f5b2The Android Open Source Projectmay be obtained through Via Licensing (www.vialicensing.com) or through the respective patent owners
202228e360595641dd906bf1773307f43d304f5b2The Android Open Source Projectindividually for the purpose of encoding or decoding bit streams in products that are compliant with
212228e360595641dd906bf1773307f43d304f5b2The Android Open Source Projectthe ISO/IEC MPEG audio standards. Please note that most manufacturers of Android devices already license
222228e360595641dd906bf1773307f43d304f5b2The Android Open Source Projectthese patent claims through Via Licensing or directly from the patent owners, and therefore FDK AAC Codec
232228e360595641dd906bf1773307f43d304f5b2The Android Open Source Projectsoftware may already be covered under those patent licenses when it is used for those licensed purposes only.
242228e360595641dd906bf1773307f43d304f5b2The Android Open Source Project
252228e360595641dd906bf1773307f43d304f5b2The Android Open Source ProjectCommercially-licensed AAC software libraries, including floating-point versions with enhanced sound quality,
262228e360595641dd906bf1773307f43d304f5b2The Android Open Source Projectare also available from Fraunhofer. Users are encouraged to check the Fraunhofer website for additional
272228e360595641dd906bf1773307f43d304f5b2The Android Open Source Projectapplications information and documentation.
282228e360595641dd906bf1773307f43d304f5b2The Android Open Source Project
292228e360595641dd906bf1773307f43d304f5b2The Android Open Source Project2.    COPYRIGHT LICENSE
302228e360595641dd906bf1773307f43d304f5b2The Android Open Source Project
312228e360595641dd906bf1773307f43d304f5b2The Android Open Source ProjectRedistribution and use in source and binary forms, with or without modification, are permitted without
322228e360595641dd906bf1773307f43d304f5b2The Android Open Source Projectpayment of copyright license fees provided that you satisfy the following conditions:
332228e360595641dd906bf1773307f43d304f5b2The Android Open Source Project
342228e360595641dd906bf1773307f43d304f5b2The Android Open Source ProjectYou must retain the complete text of this software license in redistributions of the FDK AAC Codec or
352228e360595641dd906bf1773307f43d304f5b2The Android Open Source Projectyour modifications thereto in source code form.
362228e360595641dd906bf1773307f43d304f5b2The Android Open Source Project
372228e360595641dd906bf1773307f43d304f5b2The Android Open Source ProjectYou must retain the complete text of this software license in the documentation and/or other materials
382228e360595641dd906bf1773307f43d304f5b2The Android Open Source Projectprovided with redistributions of the FDK AAC Codec or your modifications thereto in binary form.
392228e360595641dd906bf1773307f43d304f5b2The Android Open Source ProjectYou must make available free of charge copies of the complete source code of the FDK AAC Codec and your
402228e360595641dd906bf1773307f43d304f5b2The Android Open Source Projectmodifications thereto to recipients of copies in binary form.
412228e360595641dd906bf1773307f43d304f5b2The Android Open Source Project
422228e360595641dd906bf1773307f43d304f5b2The Android Open Source ProjectThe name of Fraunhofer may not be used to endorse or promote products derived from this library without
432228e360595641dd906bf1773307f43d304f5b2The Android Open Source Projectprior written permission.
442228e360595641dd906bf1773307f43d304f5b2The Android Open Source Project
452228e360595641dd906bf1773307f43d304f5b2The Android Open Source ProjectYou may not charge copyright license fees for anyone to use, copy or distribute the FDK AAC Codec
462228e360595641dd906bf1773307f43d304f5b2The Android Open Source Projectsoftware or your modifications thereto.
472228e360595641dd906bf1773307f43d304f5b2The Android Open Source Project
482228e360595641dd906bf1773307f43d304f5b2The Android Open Source ProjectYour modified versions of the FDK AAC Codec must carry prominent notices stating that you changed the software
492228e360595641dd906bf1773307f43d304f5b2The Android Open Source Projectand the date of any change. For modified versions of the FDK AAC Codec, the term
502228e360595641dd906bf1773307f43d304f5b2The Android Open Source Project"Fraunhofer FDK AAC Codec Library for Android" must be replaced by the term
512228e360595641dd906bf1773307f43d304f5b2The Android Open Source Project"Third-Party Modified Version of the Fraunhofer FDK AAC Codec Library for Android."
522228e360595641dd906bf1773307f43d304f5b2The Android Open Source Project
532228e360595641dd906bf1773307f43d304f5b2The Android Open Source Project3.    NO PATENT LICENSE
542228e360595641dd906bf1773307f43d304f5b2The Android Open Source Project
552228e360595641dd906bf1773307f43d304f5b2The Android Open Source ProjectNO EXPRESS OR IMPLIED LICENSES TO ANY PATENT CLAIMS, including without limitation the patents of Fraunhofer,
562228e360595641dd906bf1773307f43d304f5b2The Android Open Source ProjectARE GRANTED BY THIS SOFTWARE LICENSE. Fraunhofer provides no warranty of patent non-infringement with
572228e360595641dd906bf1773307f43d304f5b2The Android Open Source Projectrespect to this software.
582228e360595641dd906bf1773307f43d304f5b2The Android Open Source Project
592228e360595641dd906bf1773307f43d304f5b2The Android Open Source ProjectYou may use this FDK AAC Codec software or modifications thereto only for purposes that are authorized
602228e360595641dd906bf1773307f43d304f5b2The Android Open Source Projectby appropriate patent licenses.
612228e360595641dd906bf1773307f43d304f5b2The Android Open Source Project
622228e360595641dd906bf1773307f43d304f5b2The Android Open Source Project4.    DISCLAIMER
632228e360595641dd906bf1773307f43d304f5b2The Android Open Source Project
642228e360595641dd906bf1773307f43d304f5b2The Android Open Source ProjectThis FDK AAC Codec software is provided by Fraunhofer on behalf of the copyright holders and contributors
652228e360595641dd906bf1773307f43d304f5b2The Android Open Source Project"AS IS" and WITHOUT ANY EXPRESS OR IMPLIED WARRANTIES, including but not limited to the implied warranties
662228e360595641dd906bf1773307f43d304f5b2The Android Open Source Projectof merchantability and fitness for a particular purpose. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR
672228e360595641dd906bf1773307f43d304f5b2The Android Open Source ProjectCONTRIBUTORS BE LIABLE for any direct, indirect, incidental, special, exemplary, or consequential damages,
682228e360595641dd906bf1773307f43d304f5b2The Android Open Source Projectincluding but not limited to procurement of substitute goods or services; loss of use, data, or profits,
692228e360595641dd906bf1773307f43d304f5b2The Android Open Source Projector business interruption, however caused and on any theory of liability, whether in contract, strict
702228e360595641dd906bf1773307f43d304f5b2The Android Open Source Projectliability, or tort (including negligence), arising in any way out of the use of this software, even if
712228e360595641dd906bf1773307f43d304f5b2The Android Open Source Projectadvised of the possibility of such damage.
722228e360595641dd906bf1773307f43d304f5b2The Android Open Source Project
732228e360595641dd906bf1773307f43d304f5b2The Android Open Source Project5.    CONTACT INFORMATION
742228e360595641dd906bf1773307f43d304f5b2The Android Open Source Project
752228e360595641dd906bf1773307f43d304f5b2The Android Open Source ProjectFraunhofer Institute for Integrated Circuits IIS
762228e360595641dd906bf1773307f43d304f5b2The Android Open Source ProjectAttention: Audio and Multimedia Departments - FDK AAC LL
772228e360595641dd906bf1773307f43d304f5b2The Android Open Source ProjectAm Wolfsmantel 33
782228e360595641dd906bf1773307f43d304f5b2The Android Open Source Project91058 Erlangen, Germany
792228e360595641dd906bf1773307f43d304f5b2The Android Open Source Project
802228e360595641dd906bf1773307f43d304f5b2The Android Open Source Projectwww.iis.fraunhofer.de/amm
812228e360595641dd906bf1773307f43d304f5b2The Android Open Source Projectamm-info@iis.fraunhofer.de
822228e360595641dd906bf1773307f43d304f5b2The Android Open Source Project----------------------------------------------------------------------------------------------------------- */
832228e360595641dd906bf1773307f43d304f5b2The Android Open Source Project
842228e360595641dd906bf1773307f43d304f5b2The Android Open Source Project/******************************** MPEG Audio Encoder **************************
852228e360595641dd906bf1773307f43d304f5b2The Android Open Source Project
862228e360595641dd906bf1773307f43d304f5b2The Android Open Source Project   Initial author:       Alex Groeschel
872228e360595641dd906bf1773307f43d304f5b2The Android Open Source Project   contents/description: ADTS Transport Headers support
882228e360595641dd906bf1773307f43d304f5b2The Android Open Source Project
892228e360595641dd906bf1773307f43d304f5b2The Android Open Source Project******************************************************************************/
902228e360595641dd906bf1773307f43d304f5b2The Android Open Source Project
912228e360595641dd906bf1773307f43d304f5b2The Android Open Source Project#include "tpenc_adts.h"
922228e360595641dd906bf1773307f43d304f5b2The Android Open Source Project
932228e360595641dd906bf1773307f43d304f5b2The Android Open Source Project
942228e360595641dd906bf1773307f43d304f5b2The Android Open Source Project#include "tpenc_lib.h"
952228e360595641dd906bf1773307f43d304f5b2The Android Open Source Project#include "tpenc_asc.h"
962228e360595641dd906bf1773307f43d304f5b2The Android Open Source Project
972228e360595641dd906bf1773307f43d304f5b2The Android Open Source Project
982228e360595641dd906bf1773307f43d304f5b2The Android Open Source Projectint adtsWrite_CrcStartReg(
992228e360595641dd906bf1773307f43d304f5b2The Android Open Source Project                                 HANDLE_ADTS pAdts,          /*!< pointer to adts stucture */
1002228e360595641dd906bf1773307f43d304f5b2The Android Open Source Project                                 HANDLE_FDK_BITSTREAM hBs,   /*!< handle to current bit buffer structure */
1012228e360595641dd906bf1773307f43d304f5b2The Android Open Source Project                                 int mBits                   /*!< number of bits in crc region */
1022228e360595641dd906bf1773307f43d304f5b2The Android Open Source Project                                )
1032228e360595641dd906bf1773307f43d304f5b2The Android Open Source Project{
1042228e360595641dd906bf1773307f43d304f5b2The Android Open Source Project  if (pAdts->protection_absent) {
1052228e360595641dd906bf1773307f43d304f5b2The Android Open Source Project    return 0;
1062228e360595641dd906bf1773307f43d304f5b2The Android Open Source Project  }
1072228e360595641dd906bf1773307f43d304f5b2The Android Open Source Project  return ( FDKcrcStartReg(&pAdts->crcInfo, hBs, mBits) );
1082228e360595641dd906bf1773307f43d304f5b2The Android Open Source Project}
1092228e360595641dd906bf1773307f43d304f5b2The Android Open Source Project
1102228e360595641dd906bf1773307f43d304f5b2The Android Open Source Projectvoid adtsWrite_CrcEndReg(
1112228e360595641dd906bf1773307f43d304f5b2The Android Open Source Project                                HANDLE_ADTS pAdts, /*!< pointer to adts crc info stucture */
1122228e360595641dd906bf1773307f43d304f5b2The Android Open Source Project                                HANDLE_FDK_BITSTREAM hBs,   /*!< handle to current bit buffer structure */
1132228e360595641dd906bf1773307f43d304f5b2The Android Open Source Project                                int reg                    /*!< crc region */
1142228e360595641dd906bf1773307f43d304f5b2The Android Open Source Project                               )
1152228e360595641dd906bf1773307f43d304f5b2The Android Open Source Project{
1162228e360595641dd906bf1773307f43d304f5b2The Android Open Source Project  if (pAdts->protection_absent == 0)
1172228e360595641dd906bf1773307f43d304f5b2The Android Open Source Project  {
1182228e360595641dd906bf1773307f43d304f5b2The Android Open Source Project    FDKcrcEndReg(&pAdts->crcInfo, hBs, reg);
1192228e360595641dd906bf1773307f43d304f5b2The Android Open Source Project  }
1202228e360595641dd906bf1773307f43d304f5b2The Android Open Source Project}
1212228e360595641dd906bf1773307f43d304f5b2The Android Open Source Project
1222228e360595641dd906bf1773307f43d304f5b2The Android Open Source Projectint adtsWrite_GetHeaderBits( HANDLE_ADTS hAdts )
1232228e360595641dd906bf1773307f43d304f5b2The Android Open Source Project{
1242228e360595641dd906bf1773307f43d304f5b2The Android Open Source Project  int bits = 0;
1252228e360595641dd906bf1773307f43d304f5b2The Android Open Source Project
1262228e360595641dd906bf1773307f43d304f5b2The Android Open Source Project  if (hAdts->currentBlock == 0) {
1272228e360595641dd906bf1773307f43d304f5b2The Android Open Source Project    /* Static and variable header bits */
1282228e360595641dd906bf1773307f43d304f5b2The Android Open Source Project    bits = 56;
1292228e360595641dd906bf1773307f43d304f5b2The Android Open Source Project    if (!hAdts->protection_absent) {
1302228e360595641dd906bf1773307f43d304f5b2The Android Open Source Project      /* Add header/ single raw data block CRC bits */
1312228e360595641dd906bf1773307f43d304f5b2The Android Open Source Project      bits += 16;
1322228e360595641dd906bf1773307f43d304f5b2The Android Open Source Project      if (hAdts->num_raw_blocks>0) {
1332228e360595641dd906bf1773307f43d304f5b2The Android Open Source Project        /* Add bits of raw data block position markers */
1342228e360595641dd906bf1773307f43d304f5b2The Android Open Source Project        bits += (hAdts->num_raw_blocks)*16;
1352228e360595641dd906bf1773307f43d304f5b2The Android Open Source Project      }
1362228e360595641dd906bf1773307f43d304f5b2The Android Open Source Project    }
1372228e360595641dd906bf1773307f43d304f5b2The Android Open Source Project  }
1382228e360595641dd906bf1773307f43d304f5b2The Android Open Source Project  if (!hAdts->protection_absent && hAdts->num_raw_blocks>0) {
1392228e360595641dd906bf1773307f43d304f5b2The Android Open Source Project    /* Add raw data block CRC bits. Not really part of the header, put they cause bit overhead to be accounted. */
1402228e360595641dd906bf1773307f43d304f5b2The Android Open Source Project    bits += 16;
1412228e360595641dd906bf1773307f43d304f5b2The Android Open Source Project  }
1422228e360595641dd906bf1773307f43d304f5b2The Android Open Source Project
1432228e360595641dd906bf1773307f43d304f5b2The Android Open Source Project  hAdts->headerBits = bits;
1442228e360595641dd906bf1773307f43d304f5b2The Android Open Source Project
1452228e360595641dd906bf1773307f43d304f5b2The Android Open Source Project  return bits;
1462228e360595641dd906bf1773307f43d304f5b2The Android Open Source Project}
1472228e360595641dd906bf1773307f43d304f5b2The Android Open Source Project
1482228e360595641dd906bf1773307f43d304f5b2The Android Open Source ProjectINT adtsWrite_Init(HANDLE_ADTS hAdts, CODER_CONFIG *config)
1492228e360595641dd906bf1773307f43d304f5b2The Android Open Source Project{
1502228e360595641dd906bf1773307f43d304f5b2The Android Open Source Project  /* Sanity checks */
1512228e360595641dd906bf1773307f43d304f5b2The Android Open Source Project  if ( config->nSubFrames < 1
1522228e360595641dd906bf1773307f43d304f5b2The Android Open Source Project    || config->nSubFrames > 4
1532228e360595641dd906bf1773307f43d304f5b2The Android Open Source Project    || (int)config->aot > 4
1542228e360595641dd906bf1773307f43d304f5b2The Android Open Source Project    || (int)config->aot < 1 ) {
1552228e360595641dd906bf1773307f43d304f5b2The Android Open Source Project    return -1;
1562228e360595641dd906bf1773307f43d304f5b2The Android Open Source Project  }
1572228e360595641dd906bf1773307f43d304f5b2The Android Open Source Project
1582228e360595641dd906bf1773307f43d304f5b2The Android Open Source Project  /* fixed header */
1592228e360595641dd906bf1773307f43d304f5b2The Android Open Source Project  if (config->flags & CC_MPEG_ID) {
1602228e360595641dd906bf1773307f43d304f5b2The Android Open Source Project    hAdts->mpeg_id = 0; /* MPEG 4 */
1612228e360595641dd906bf1773307f43d304f5b2The Android Open Source Project  } else {
1622228e360595641dd906bf1773307f43d304f5b2The Android Open Source Project    hAdts->mpeg_id = 1; /* MPEG 2 */
1632228e360595641dd906bf1773307f43d304f5b2The Android Open Source Project  }
1642228e360595641dd906bf1773307f43d304f5b2The Android Open Source Project  hAdts->layer=0;
1652228e360595641dd906bf1773307f43d304f5b2The Android Open Source Project  hAdts->protection_absent = ! (config->flags & CC_PROTECTION);
1662228e360595641dd906bf1773307f43d304f5b2The Android Open Source Project  hAdts->profile = ((int)config->aot) - 1;
1672228e360595641dd906bf1773307f43d304f5b2The Android Open Source Project  hAdts->sample_freq_index = getSamplingRateIndex(config->samplingRate);
1682228e360595641dd906bf1773307f43d304f5b2The Android Open Source Project  hAdts->sample_freq = config->samplingRate;
1692228e360595641dd906bf1773307f43d304f5b2The Android Open Source Project  hAdts->private_bit=0;
1702228e360595641dd906bf1773307f43d304f5b2The Android Open Source Project  hAdts->channel_mode = config->channelMode;
1712228e360595641dd906bf1773307f43d304f5b2The Android Open Source Project  hAdts->original=0;
1722228e360595641dd906bf1773307f43d304f5b2The Android Open Source Project  hAdts->home=0;
1732228e360595641dd906bf1773307f43d304f5b2The Android Open Source Project  /* variable header */
1742228e360595641dd906bf1773307f43d304f5b2The Android Open Source Project  hAdts->copyright_id=0;
1752228e360595641dd906bf1773307f43d304f5b2The Android Open Source Project  hAdts->copyright_start=0;
1762228e360595641dd906bf1773307f43d304f5b2The Android Open Source Project
1772228e360595641dd906bf1773307f43d304f5b2The Android Open Source Project  hAdts->num_raw_blocks=config->nSubFrames-1; /* 0 means 1 raw data block */
1782228e360595641dd906bf1773307f43d304f5b2The Android Open Source Project
1792228e360595641dd906bf1773307f43d304f5b2The Android Open Source Project  FDKcrcInit(&hAdts->crcInfo, 0x8005, 0xFFFF, 16);
1802228e360595641dd906bf1773307f43d304f5b2The Android Open Source Project
1812228e360595641dd906bf1773307f43d304f5b2The Android Open Source Project  hAdts->currentBlock = 0;
1822228e360595641dd906bf1773307f43d304f5b2The Android Open Source Project
1832228e360595641dd906bf1773307f43d304f5b2The Android Open Source Project
1842228e360595641dd906bf1773307f43d304f5b2The Android Open Source Project  return 0;
1852228e360595641dd906bf1773307f43d304f5b2The Android Open Source Project}
1862228e360595641dd906bf1773307f43d304f5b2The Android Open Source Project
1872228e360595641dd906bf1773307f43d304f5b2The Android Open Source Projectint adtsWrite_EncodeHeader(HANDLE_ADTS hAdts,
1882228e360595641dd906bf1773307f43d304f5b2The Android Open Source Project                                 HANDLE_FDK_BITSTREAM hBitStream,
1892228e360595641dd906bf1773307f43d304f5b2The Android Open Source Project                                 int buffer_fullness,
1902228e360595641dd906bf1773307f43d304f5b2The Android Open Source Project                                 int frame_length)
1912228e360595641dd906bf1773307f43d304f5b2The Android Open Source Project{
1922228e360595641dd906bf1773307f43d304f5b2The Android Open Source Project  INT crcIndex = 0;
1932228e360595641dd906bf1773307f43d304f5b2The Android Open Source Project
1942228e360595641dd906bf1773307f43d304f5b2The Android Open Source Project
1952228e360595641dd906bf1773307f43d304f5b2The Android Open Source Project  hAdts->headerBits = adtsWrite_GetHeaderBits(hAdts);
1962228e360595641dd906bf1773307f43d304f5b2The Android Open Source Project
1972228e360595641dd906bf1773307f43d304f5b2The Android Open Source Project  FDK_ASSERT(((frame_length+hAdts->headerBits)/8)<0x2000);      /*13 bit*/
1982228e360595641dd906bf1773307f43d304f5b2The Android Open Source Project  FDK_ASSERT(buffer_fullness<0x800);    /* 11 bit   */
1992228e360595641dd906bf1773307f43d304f5b2The Android Open Source Project
2002228e360595641dd906bf1773307f43d304f5b2The Android Open Source Project  if (!hAdts->protection_absent) {
2012228e360595641dd906bf1773307f43d304f5b2The Android Open Source Project    FDKcrcReset(&hAdts->crcInfo);
2022228e360595641dd906bf1773307f43d304f5b2The Android Open Source Project  }
2032228e360595641dd906bf1773307f43d304f5b2The Android Open Source Project
2042228e360595641dd906bf1773307f43d304f5b2The Android Open Source Project  if (hAdts->currentBlock == 0) {
2052228e360595641dd906bf1773307f43d304f5b2The Android Open Source Project    FDKresetBitbuffer(hBitStream, BS_WRITER);
2062228e360595641dd906bf1773307f43d304f5b2The Android Open Source Project  }
2072228e360595641dd906bf1773307f43d304f5b2The Android Open Source Project
2082228e360595641dd906bf1773307f43d304f5b2The Android Open Source Project  hAdts->subFrameStartBit = FDKgetValidBits(hBitStream);
2092228e360595641dd906bf1773307f43d304f5b2The Android Open Source Project
2102228e360595641dd906bf1773307f43d304f5b2The Android Open Source Project  /* Skip new header if this is raw data block 1..n */
2112228e360595641dd906bf1773307f43d304f5b2The Android Open Source Project  if (hAdts->currentBlock == 0)
2122228e360595641dd906bf1773307f43d304f5b2The Android Open Source Project  {
2132228e360595641dd906bf1773307f43d304f5b2The Android Open Source Project    FDKresetBitbuffer(hBitStream, BS_WRITER);
2142228e360595641dd906bf1773307f43d304f5b2The Android Open Source Project
2152228e360595641dd906bf1773307f43d304f5b2The Android Open Source Project    if (hAdts->num_raw_blocks == 0) {
2162228e360595641dd906bf1773307f43d304f5b2The Android Open Source Project      crcIndex = adtsWrite_CrcStartReg(hAdts, hBitStream, 0);
2172228e360595641dd906bf1773307f43d304f5b2The Android Open Source Project    }
2182228e360595641dd906bf1773307f43d304f5b2The Android Open Source Project
2192228e360595641dd906bf1773307f43d304f5b2The Android Open Source Project    /* fixed header */
2202228e360595641dd906bf1773307f43d304f5b2The Android Open Source Project    FDKwriteBits(hBitStream, 0xFFF, 12);
2212228e360595641dd906bf1773307f43d304f5b2The Android Open Source Project    FDKwriteBits(hBitStream, hAdts->mpeg_id, 1);
2222228e360595641dd906bf1773307f43d304f5b2The Android Open Source Project    FDKwriteBits(hBitStream, hAdts->layer, 2);
2232228e360595641dd906bf1773307f43d304f5b2The Android Open Source Project    FDKwriteBits(hBitStream, hAdts->protection_absent, 1);
2242228e360595641dd906bf1773307f43d304f5b2The Android Open Source Project    FDKwriteBits(hBitStream, hAdts->profile, 2);
2252228e360595641dd906bf1773307f43d304f5b2The Android Open Source Project    FDKwriteBits(hBitStream, hAdts->sample_freq_index, 4);
2262228e360595641dd906bf1773307f43d304f5b2The Android Open Source Project    FDKwriteBits(hBitStream, hAdts->private_bit, 1);
2272228e360595641dd906bf1773307f43d304f5b2The Android Open Source Project    FDKwriteBits(hBitStream, getChannelConfig(hAdts->channel_mode), 3);
2282228e360595641dd906bf1773307f43d304f5b2The Android Open Source Project    FDKwriteBits(hBitStream, hAdts->original, 1);
2292228e360595641dd906bf1773307f43d304f5b2The Android Open Source Project    FDKwriteBits(hBitStream, hAdts->home, 1);
2302228e360595641dd906bf1773307f43d304f5b2The Android Open Source Project    /* variable header */
2312228e360595641dd906bf1773307f43d304f5b2The Android Open Source Project    FDKwriteBits(hBitStream, hAdts->copyright_id, 1);
2322228e360595641dd906bf1773307f43d304f5b2The Android Open Source Project    FDKwriteBits(hBitStream, hAdts->copyright_start, 1);
2332228e360595641dd906bf1773307f43d304f5b2The Android Open Source Project    FDKwriteBits(hBitStream, (frame_length + hAdts->headerBits)>>3, 13);
2342228e360595641dd906bf1773307f43d304f5b2The Android Open Source Project    FDKwriteBits(hBitStream, buffer_fullness, 11);
2352228e360595641dd906bf1773307f43d304f5b2The Android Open Source Project    FDKwriteBits(hBitStream, hAdts->num_raw_blocks, 2);
2362228e360595641dd906bf1773307f43d304f5b2The Android Open Source Project
2372228e360595641dd906bf1773307f43d304f5b2The Android Open Source Project    if (!hAdts->protection_absent) {
2382228e360595641dd906bf1773307f43d304f5b2The Android Open Source Project      int i;
2392228e360595641dd906bf1773307f43d304f5b2The Android Open Source Project
2402228e360595641dd906bf1773307f43d304f5b2The Android Open Source Project      /* End header CRC portion for single raw data block and write dummy zero values for unknown fields. */
2412228e360595641dd906bf1773307f43d304f5b2The Android Open Source Project      if (hAdts->num_raw_blocks == 0) {
2422228e360595641dd906bf1773307f43d304f5b2The Android Open Source Project        adtsWrite_CrcEndReg(hAdts, hBitStream, crcIndex);
2432228e360595641dd906bf1773307f43d304f5b2The Android Open Source Project      } else {
2442228e360595641dd906bf1773307f43d304f5b2The Android Open Source Project        for (i=0; i<hAdts->num_raw_blocks; i++) {
2452228e360595641dd906bf1773307f43d304f5b2The Android Open Source Project          FDKwriteBits(hBitStream, 0, 16);
2462228e360595641dd906bf1773307f43d304f5b2The Android Open Source Project        }
2472228e360595641dd906bf1773307f43d304f5b2The Android Open Source Project      }
2482228e360595641dd906bf1773307f43d304f5b2The Android Open Source Project      FDKwriteBits(hBitStream, 0, 16);
2492228e360595641dd906bf1773307f43d304f5b2The Android Open Source Project    }
2502228e360595641dd906bf1773307f43d304f5b2The Android Open Source Project  } /* End of ADTS header */
2512228e360595641dd906bf1773307f43d304f5b2The Android Open Source Project
2522228e360595641dd906bf1773307f43d304f5b2The Android Open Source Project  return 0;
2532228e360595641dd906bf1773307f43d304f5b2The Android Open Source Project}
2542228e360595641dd906bf1773307f43d304f5b2The Android Open Source Project
2552228e360595641dd906bf1773307f43d304f5b2The Android Open Source Projectvoid adtsWrite_EndRawDataBlock(HANDLE_ADTS hAdts,
2562228e360595641dd906bf1773307f43d304f5b2The Android Open Source Project                          HANDLE_FDK_BITSTREAM hBs,
2572228e360595641dd906bf1773307f43d304f5b2The Android Open Source Project                          int *pBits)
2582228e360595641dd906bf1773307f43d304f5b2The Android Open Source Project{
2592228e360595641dd906bf1773307f43d304f5b2The Android Open Source Project  if (!hAdts->protection_absent) {
2602228e360595641dd906bf1773307f43d304f5b2The Android Open Source Project    FDK_BITSTREAM bsWriter;
2612228e360595641dd906bf1773307f43d304f5b2The Android Open Source Project
2622228e360595641dd906bf1773307f43d304f5b2The Android Open Source Project    FDKinitBitStream(&bsWriter, hBs->hBitBuf.Buffer, hBs->hBitBuf.bufSize, 0, BS_WRITER);
2632228e360595641dd906bf1773307f43d304f5b2The Android Open Source Project    FDKpushFor(&bsWriter, 56);
2642228e360595641dd906bf1773307f43d304f5b2The Android Open Source Project
2652228e360595641dd906bf1773307f43d304f5b2The Android Open Source Project    if (hAdts->num_raw_blocks == 0) {
2662228e360595641dd906bf1773307f43d304f5b2The Android Open Source Project      FDKwriteBits(&bsWriter, FDKcrcGetCRC(&hAdts->crcInfo), 16);
2672228e360595641dd906bf1773307f43d304f5b2The Android Open Source Project    } else {
2682228e360595641dd906bf1773307f43d304f5b2The Android Open Source Project      int distance;
2692228e360595641dd906bf1773307f43d304f5b2The Android Open Source Project
2702228e360595641dd906bf1773307f43d304f5b2The Android Open Source Project      /* Write CRC of current raw data block */
2712228e360595641dd906bf1773307f43d304f5b2The Android Open Source Project      FDKwriteBits(hBs, FDKcrcGetCRC(&hAdts->crcInfo), 16);
2722228e360595641dd906bf1773307f43d304f5b2The Android Open Source Project
2732228e360595641dd906bf1773307f43d304f5b2The Android Open Source Project      /* Write distance to current data block */
2742228e360595641dd906bf1773307f43d304f5b2The Android Open Source Project      if (hAdts->currentBlock < hAdts->num_raw_blocks) {
2752228e360595641dd906bf1773307f43d304f5b2The Android Open Source Project        FDKpushFor(&bsWriter, hAdts->currentBlock*16);
2762228e360595641dd906bf1773307f43d304f5b2The Android Open Source Project        distance = FDKgetValidBits(hBs) - (56 + (hAdts->num_raw_blocks)*16 + 16);
2772228e360595641dd906bf1773307f43d304f5b2The Android Open Source Project        FDKwriteBits(&bsWriter, distance>>3, 16);
2782228e360595641dd906bf1773307f43d304f5b2The Android Open Source Project      }
2792228e360595641dd906bf1773307f43d304f5b2The Android Open Source Project    }
2802228e360595641dd906bf1773307f43d304f5b2The Android Open Source Project    FDKsyncCache(&bsWriter);
2812228e360595641dd906bf1773307f43d304f5b2The Android Open Source Project  }
2822228e360595641dd906bf1773307f43d304f5b2The Android Open Source Project
2832228e360595641dd906bf1773307f43d304f5b2The Android Open Source Project  /* Write total frame lenth for multiple raw data blocks and header CRC */
2842228e360595641dd906bf1773307f43d304f5b2The Android Open Source Project  if (hAdts->num_raw_blocks > 0 && hAdts->currentBlock == hAdts->num_raw_blocks) {
2852228e360595641dd906bf1773307f43d304f5b2The Android Open Source Project    FDK_BITSTREAM bsWriter;
2862228e360595641dd906bf1773307f43d304f5b2The Android Open Source Project    int crcIndex = 0;
2872228e360595641dd906bf1773307f43d304f5b2The Android Open Source Project
2882228e360595641dd906bf1773307f43d304f5b2The Android Open Source Project    FDKinitBitStream(&bsWriter, hBs->hBitBuf.Buffer, hBs->hBitBuf.bufSize, 0, BS_WRITER);
2892228e360595641dd906bf1773307f43d304f5b2The Android Open Source Project
2902228e360595641dd906bf1773307f43d304f5b2The Android Open Source Project    if (!hAdts->protection_absent) {
2912228e360595641dd906bf1773307f43d304f5b2The Android Open Source Project      FDKcrcReset(&hAdts->crcInfo);
2922228e360595641dd906bf1773307f43d304f5b2The Android Open Source Project      crcIndex = FDKcrcStartReg(&hAdts->crcInfo, &bsWriter, 0);
2932228e360595641dd906bf1773307f43d304f5b2The Android Open Source Project    }
2942228e360595641dd906bf1773307f43d304f5b2The Android Open Source Project    /* Write total frame length */
2952228e360595641dd906bf1773307f43d304f5b2The Android Open Source Project    FDKpushFor(&bsWriter, 56-28+2);
2962228e360595641dd906bf1773307f43d304f5b2The Android Open Source Project    FDKwriteBits(&bsWriter, FDKgetValidBits(hBs)>>3, 13);
2972228e360595641dd906bf1773307f43d304f5b2The Android Open Source Project
2982228e360595641dd906bf1773307f43d304f5b2The Android Open Source Project    /* Write header CRC */
2992228e360595641dd906bf1773307f43d304f5b2The Android Open Source Project    if (!hAdts->protection_absent) {
3002228e360595641dd906bf1773307f43d304f5b2The Android Open Source Project      FDKpushFor(&bsWriter, 11+2 + (hAdts->num_raw_blocks)*16);
3012228e360595641dd906bf1773307f43d304f5b2The Android Open Source Project      FDKcrcEndReg(&hAdts->crcInfo, &bsWriter, crcIndex);
3022228e360595641dd906bf1773307f43d304f5b2The Android Open Source Project      FDKwriteBits(&bsWriter, FDKcrcGetCRC(&hAdts->crcInfo), 16);
3032228e360595641dd906bf1773307f43d304f5b2The Android Open Source Project    }
3042228e360595641dd906bf1773307f43d304f5b2The Android Open Source Project    FDKsyncCache(&bsWriter);
3052228e360595641dd906bf1773307f43d304f5b2The Android Open Source Project  }
3062228e360595641dd906bf1773307f43d304f5b2The Android Open Source Project
3072228e360595641dd906bf1773307f43d304f5b2The Android Open Source Project  /* Correct *pBits to reflect the amount of bits of the current subframe */
3082228e360595641dd906bf1773307f43d304f5b2The Android Open Source Project  *pBits -= hAdts->subFrameStartBit;
3092228e360595641dd906bf1773307f43d304f5b2The Android Open Source Project  if (!hAdts->protection_absent && hAdts->num_raw_blocks > 0) {
3102228e360595641dd906bf1773307f43d304f5b2The Android Open Source Project    /* Fixup CRC bits, since they come after each raw data block */
3112228e360595641dd906bf1773307f43d304f5b2The Android Open Source Project    *pBits += 16;
3122228e360595641dd906bf1773307f43d304f5b2The Android Open Source Project  }
3132228e360595641dd906bf1773307f43d304f5b2The Android Open Source Project  hAdts->currentBlock++;
3142228e360595641dd906bf1773307f43d304f5b2The Android Open Source Project}
3152228e360595641dd906bf1773307f43d304f5b2The Android Open Source Project
316