pulsedata.cpp revision 4f0d97057c5c640b25518358886f8c47da9fc052
14ee2ad04344446e610172a0e73949212923014dfSebastian Redl
22cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor/* -----------------------------------------------------------------------------------------------------------
32cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas GregorSoftware License for The Fraunhofer FDK AAC Codec Library for Android
42cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor
52cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor� Copyright  1995 - 2013 Fraunhofer-Gesellschaft zur F�rderung der angewandten Forschung e.V.
62cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor  All rights reserved.
72cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor
82cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor 1.    INTRODUCTION
92cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas GregorThe Fraunhofer FDK AAC Codec Library for Android ("FDK AAC Codec") is software that implements
10a4232eb646d89e7d52424bb42eb87d9061f39e63Sebastian Redlthe MPEG Advanced Audio Coding ("AAC") encoding and decoding scheme for digital audio.
112cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas GregorThis FDK AAC Codec software is intended to be used on a wide variety of Android devices.
122cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor
132cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas GregorAAC's HE-AAC and HE-AAC v2 versions are regarded as today's most efficient general perceptual
147faa2ec03a7ef120ac165bb45b6c70a8b20c9f1cSebastian Redlaudio codecs. AAC-ELD is considered the best-performing full-bandwidth communications codec by
150eca89e9890db4d8336ce762a5b359a1d58ca02bArgyrios Kyrtzidisindependent studies and is widely deployed. AAC has been standardized by ISO and IEC as part
16e737f5041a36d0befb39ffeed8d50ba15916d3daDouglas Gregorof the MPEG specifications.
17e737f5041a36d0befb39ffeed8d50ba15916d3daDouglas Gregor
182cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas GregorPatent licenses for necessary patent claims for the FDK AAC Codec (including those of Fraunhofer)
192cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregormay be obtained through Via Licensing (www.vialicensing.com) or through the respective patent owners
202cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregorindividually for the purpose of encoding or decoding bit streams in products that are compliant with
212a7fb27913999d132cf9e10e03dc5271faa2e9d3John McCallthe ISO/IEC MPEG audio standards. Please note that most manufacturers of Android devices already license
2289eaf3af92c72c0c1aae807644e39cabc461d685Argyrios Kyrtzidisthese patent claims through Via Licensing or directly from the patent owners, and therefore FDK AAC Codec
230b7489194f9f89fac39d57211c1e7953ae50251fDouglas Gregorsoftware may already be covered under those patent licenses when it is used for those licensed purposes only.
247a1fad38256eb4c5129359be85ba1ea1678eb5c9John McCall
252cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas GregorCommercially-licensed AAC software libraries, including floating-point versions with enhanced sound quality,
26a1ee0c548b8aa4aaf93d1917e304e3da13171a08John McCallare also available from Fraunhofer. Users are encouraged to check the Fraunhofer website for additional
276ab7cd853e9c15cf986a8a7c3db1f8d20e275409Sebastian Redlapplications information and documentation.
287c5d24efcd2e505b5739f7def08dfe25ce59a1b2Chris Lattner
296a5a23f8e7fb65e028c8092bc1d1a1d9dfe2e9bcDouglas Gregor2.    COPYRIGHT LICENSE
307c5d24efcd2e505b5739f7def08dfe25ce59a1b2Chris Lattner
3183d63c78810556d26b62ac4cbae2eda6cdd2570cSteve NaroffRedistribution and use in source and binary forms, with or without modification, are permitted without
3214f79002e58556798e86168c63e48d533287eda5Douglas Gregorpayment of copyright license fees provided that you satisfy the following conditions:
3310e286aa8d39fb51a21412850265d9dae74613eeChris Lattner
343251ceb90b3fec68e86d6dcfa58836e20a7205c3Douglas GregorYou must retain the complete text of this software license in redistributions of the FDK AAC Codec or
3514f79002e58556798e86168c63e48d533287eda5Douglas Gregoryour modifications thereto in source code form.
36bd94500d3aa60092fb0f1e90f53fb0d03fa502a8Douglas Gregor
372bec0410d268779f601bd509e0302a500af7ac6aDouglas GregorYou must retain the complete text of this software license in the documentation and/or other materials
38ab41e63821dc60ad144d0684df8d79a9eef86b75Douglas Gregorprovided with redistributions of the FDK AAC Codec or your modifications thereto in binary form.
390a0d2b179085a52c10402feebeb6db8b4d96a140Douglas GregorYou must make available free of charge copies of the complete source code of the FDK AAC Codec and your
4017fc223395d51be582fc666bb6ea21bd1dff26dcDouglas Gregormodifications thereto to recipients of copies in binary form.
4117fc223395d51be582fc666bb6ea21bd1dff26dcDouglas Gregor
422596e429a61602312bdd149786045b8a90cd2d10Daniel DunbarThe name of Fraunhofer may not be used to endorse or promote products derived from this library without
432cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregorprior written permission.
44fbfd180495e7800975c6d9bdc6d24e706ef70e34Michael J. Spencer
4514f79002e58556798e86168c63e48d533287eda5Douglas GregorYou may not charge copyright license fees for anyone to use, copy or distribute the FDK AAC Codec
4603013fa9a0bf1ef4b907f5fec006c8f4000fdd21Michael J. Spencersoftware or your modifications thereto.
47f62d43d2afe1960755a1b5813cae1e5983bcac1bDouglas Gregor
483c304bd9ec2b4611572d4cbae9e1727bbecb5dc9Chris LattnerYour modified versions of the FDK AAC Codec must carry prominent notices stating that you changed the software
49cfbf1c7536e016dc275139dd842d4a5f059a749fDouglas Gregorand the date of any change. For modified versions of the FDK AAC Codec, the term
50f62d43d2afe1960755a1b5813cae1e5983bcac1bDouglas Gregor"Fraunhofer FDK AAC Codec Library for Android" must be replaced by the term
512cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor"Third-Party Modified Version of the Fraunhofer FDK AAC Codec Library for Android."
528538e8d43a3a9bd439c987c0de37bcbf035dd391Sebastian Redl
532cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor3.    NO PATENT LICENSE
54ade5000c8763f4bec41f452d7efa3a9b2a6d4712Sebastian Redl
555f9e272e632e951b1efe824cd16acb4d96077930Chris LattnerNO EXPRESS OR IMPLIED LICENSES TO ANY PATENT CLAIMS, including without limitation the patents of Fraunhofer,
565f9e272e632e951b1efe824cd16acb4d96077930Chris LattnerARE GRANTED BY THIS SOFTWARE LICENSE. Fraunhofer provides no warranty of patent non-infringement with
575f9e272e632e951b1efe824cd16acb4d96077930Chris Lattnerrespect to this software.
586e089c687cc2b914c46859ab7e46fe4c3c6b0afbBenjamin Kramer
59ade5000c8763f4bec41f452d7efa3a9b2a6d4712Sebastian RedlYou may use this FDK AAC Codec software or modifications thereto only for purposes that are authorized
606e089c687cc2b914c46859ab7e46fe4c3c6b0afbBenjamin Kramerby appropriate patent licenses.
616e089c687cc2b914c46859ab7e46fe4c3c6b0afbBenjamin Kramer
625f9e272e632e951b1efe824cd16acb4d96077930Chris Lattner4.    DISCLAIMER
635f9e272e632e951b1efe824cd16acb4d96077930Chris Lattner
646e089c687cc2b914c46859ab7e46fe4c3c6b0afbBenjamin KramerThis FDK AAC Codec software is provided by Fraunhofer on behalf of the copyright holders and contributors
65ade5000c8763f4bec41f452d7efa3a9b2a6d4712Sebastian Redl"AS IS" and WITHOUT ANY EXPRESS OR IMPLIED WARRANTIES, including but not limited to the implied warranties
66ade5000c8763f4bec41f452d7efa3a9b2a6d4712Sebastian Redlof merchantability and fitness for a particular purpose. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR
672cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas GregorCONTRIBUTORS BE LIABLE for any direct, indirect, incidental, special, exemplary, or consequential damages,
682cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregorincluding but not limited to procurement of substitute goods or services; loss of use, data, or profits,
692cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregoror business interruption, however caused and on any theory of liability, whether in contract, strict
7012b1c7615d4f9a2edc544be499f895f16ac100edChris Lattnerliability, or tort (including negligence), arising in any way out of the use of this software, even if
712cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregoradvised of the possibility of such damage.
723397c5570369f19b2d6c52e898f708d75ceede1fSebastian Redl
73a4232eb646d89e7d52424bb42eb87d9061f39e63Sebastian Redl5.    CONTACT INFORMATION
7489eaf3af92c72c0c1aae807644e39cabc461d685Argyrios Kyrtzidis
752cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas GregorFraunhofer Institute for Integrated Circuits IIS
762cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas GregorAttention: Audio and Multimedia Departments - FDK AAC LL
772cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas GregorAm Wolfsmantel 33
788538e8d43a3a9bd439c987c0de37bcbf035dd391Sebastian Redl91058 Erlangen, Germany
792cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor
8089eaf3af92c72c0c1aae807644e39cabc461d685Argyrios Kyrtzidiswww.iis.fraunhofer.de/amm
818538e8d43a3a9bd439c987c0de37bcbf035dd391Sebastian Redlamm-info@iis.fraunhofer.de
822cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor----------------------------------------------------------------------------------------------------------- */
832cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor
842cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor/*****************************  MPEG-4 AAC Decoder  **************************
852cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor
862cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor   Author(s):   Josef Hoepfl
872cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor   Description: pulse data tool
882cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor
892cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor******************************************************************************/
902cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor
912cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor#include "pulsedata.h"
922cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor
933397c5570369f19b2d6c52e898f708d75ceede1fSebastian Redl
94b219cfc4d75f0a03630b7c4509ef791b7e97b2c8David Blaikie#include "channelinfo.h"
952cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor
962cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor
973397c5570369f19b2d6c52e898f708d75ceede1fSebastian RedlINT CPulseData_Read(
982cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor        HANDLE_FDK_BITSTREAM bs,
998538e8d43a3a9bd439c987c0de37bcbf035dd391Sebastian Redl        CPulseData *const PulseData,
1002cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor        const SHORT *sfb_startlines,
1012cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor        const void *pIcsInfo,
1023397c5570369f19b2d6c52e898f708d75ceede1fSebastian Redl        const SHORT frame_length
1032cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor        )
1048538e8d43a3a9bd439c987c0de37bcbf035dd391Sebastian Redl{
1052cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor  int i, k=0;
1062cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor  const UINT MaxSfBands = GetScaleFactorBandsTransmitted((CIcsInfo*)pIcsInfo);
1073397c5570369f19b2d6c52e898f708d75ceede1fSebastian Redl
1081eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  /* reset pulse data flag */
1098538e8d43a3a9bd439c987c0de37bcbf035dd391Sebastian Redl  PulseData->PulseDataPresent = 0;
1102cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor
1112cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor  if ((PulseData->PulseDataPresent = (UCHAR) FDKreadBit(bs)) != 0) {
1123397c5570369f19b2d6c52e898f708d75ceede1fSebastian Redl    if (!IsLongBlock((CIcsInfo*)pIcsInfo)) {
113df1550fc59b51681d37225934fe4e3acac321621Richard Smith      return AAC_DEC_DECODE_FRAME_ERROR;
114df1550fc59b51681d37225934fe4e3acac321621Richard Smith    }
1158538e8d43a3a9bd439c987c0de37bcbf035dd391Sebastian Redl
1162cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor    PulseData->NumberPulse = (UCHAR) FDKreadBits(bs,2);
1172cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor    PulseData->PulseStartBand = (UCHAR) FDKreadBits(bs,6);
1183397c5570369f19b2d6c52e898f708d75ceede1fSebastian Redl
119df1550fc59b51681d37225934fe4e3acac321621Richard Smith    if (PulseData->PulseStartBand >= MaxSfBands) {
1208538e8d43a3a9bd439c987c0de37bcbf035dd391Sebastian Redl      return AAC_DEC_DECODE_FRAME_ERROR;
1212cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor    }
1222cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor
1233397c5570369f19b2d6c52e898f708d75ceede1fSebastian Redl    k = sfb_startlines[PulseData->PulseStartBand];
1241eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
1251eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump    for (i=0; i<=PulseData->NumberPulse; i++) {
1268538e8d43a3a9bd439c987c0de37bcbf035dd391Sebastian Redl      PulseData->PulseOffset[i] = (UCHAR) FDKreadBits(bs,5);
1272cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor      PulseData->PulseAmp[i] = (UCHAR) FDKreadBits(bs,4);
1282cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor      k += PulseData->PulseOffset[i];
1293397c5570369f19b2d6c52e898f708d75ceede1fSebastian Redl    }
1302cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor
1312cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor    if (k >= frame_length) {
1320953e767ff7817f97b3ab20896b229891eeff45bJohn McCall        return AAC_DEC_DECODE_FRAME_ERROR;
1332cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor    }
1342cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor  }
1353397c5570369f19b2d6c52e898f708d75ceede1fSebastian Redl
1362cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor
1372cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor  return 0;
1388538e8d43a3a9bd439c987c0de37bcbf035dd391Sebastian Redl}
1392cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor
1402cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregorvoid CPulseData_Apply(CPulseData *PulseData,                /*!< pointer to pulse data side info */
1413397c5570369f19b2d6c52e898f708d75ceede1fSebastian Redl                      const short *pScaleFactorBandOffsets, /*!< pointer to scalefactor band offsets */
1422cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor                      FIXP_DBL *coef)                     /*!< pointer to spectrum */
1438538e8d43a3a9bd439c987c0de37bcbf035dd391Sebastian Redl{
1442cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor  int i,k;
1452cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor
1463397c5570369f19b2d6c52e898f708d75ceede1fSebastian Redl  if (PulseData->PulseDataPresent)
1472cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor  {
1487e7eb3da052a6d80ddf2377cab0384c798f73f75Douglas Gregor    k = pScaleFactorBandOffsets[PulseData->PulseStartBand];
1497e7eb3da052a6d80ddf2377cab0384c798f73f75Douglas Gregor
150c9490c000f515c29f200a1215328d8ab9a0f3818Douglas Gregor    for (i=0; i<=PulseData->NumberPulse; i++)
1518538e8d43a3a9bd439c987c0de37bcbf035dd391Sebastian Redl    {
1522cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor      k += PulseData->PulseOffset[i];
1532cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor      if (coef [k] > (FIXP_DBL)0) coef[k] += (FIXP_DBL)(int)PulseData->PulseAmp[i];
1543397c5570369f19b2d6c52e898f708d75ceede1fSebastian Redl      else                          coef[k] -= (FIXP_DBL)(int)PulseData->PulseAmp[i];
1552cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor    }
1562cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor  }
157e86d78cf4754a6aef2cf9a33d847aa15338e276fBob Wilson}
1588538e8d43a3a9bd439c987c0de37bcbf035dd391Sebastian Redl
1592cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor