aaccommon.h 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213
  1. /* ***** BEGIN LICENSE BLOCK *****
  2. * Source last modified: $Id: aaccommon.h,v 1.1 2005/02/26 01:47:34 jrecker Exp $
  3. *
  4. * Portions Copyright (c) 1995-2005 RealNetworks, Inc. All Rights Reserved.
  5. *
  6. * The contents of this file, and the files included with this file,
  7. * are subject to the current version of the RealNetworks Public
  8. * Source License (the "RPSL") available at
  9. * http://www.helixcommunity.org/content/rpsl unless you have licensed
  10. * the file under the current version of the RealNetworks Community
  11. * Source License (the "RCSL") available at
  12. * http://www.helixcommunity.org/content/rcsl, in which case the RCSL
  13. * will apply. You may also obtain the license terms directly from
  14. * RealNetworks. You may not use this file except in compliance with
  15. * the RPSL or, if you have a valid RCSL with RealNetworks applicable
  16. * to this file, the RCSL. Please see the applicable RPSL or RCSL for
  17. * the rights, obligations and limitations governing use of the
  18. * contents of the file.
  19. *
  20. * This file is part of the Helix DNA Technology. RealNetworks is the
  21. * developer of the Original Code and owns the copyrights in the
  22. * portions it created.
  23. *
  24. * This file, and the files included with this file, is distributed
  25. * and made available on an 'AS IS' basis, WITHOUT WARRANTY OF ANY
  26. * KIND, EITHER EXPRESS OR IMPLIED, AND REALNETWORKS HEREBY DISCLAIMS
  27. * ALL SUCH WARRANTIES, INCLUDING WITHOUT LIMITATION, ANY WARRANTIES
  28. * OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, QUIET
  29. * ENJOYMENT OR NON-INFRINGEMENT.
  30. *
  31. * Technology Compatibility Kit Test Suite(s) Location:
  32. * http://www.helixcommunity.org/content/tck
  33. *
  34. * Contributor(s):
  35. *
  36. * ***** END LICENSE BLOCK ***** */
  37. /**************************************************************************************
  38. * Fixed-point HE-AAC decoder
  39. * Jon Recker (jrecker@real.com)
  40. * February 2005
  41. *
  42. * aaccommon.h - implementation-independent API's, datatypes, and definitions
  43. **************************************************************************************/
  44. #ifndef _AACCOMMON_H
  45. #define _AACCOMMON_H
  46. #ifdef ESP8266
  47. # include "pgmspace.h"
  48. #elif defined(ESP_PLATFORM) && __has_include(<pgm_space.h>)
  49. # include <pgm_space.h>
  50. #else
  51. # define PROGMEM
  52. # define pgm_read_byte(addr) (*(const unsigned char *)(addr))
  53. # define pgm_read_word(addr) (*(const unsigned short *)(addr))
  54. #endif
  55. // Can't fit in ESP8266 RAM
  56. #ifndef ESP8266
  57. #define AAC_ENABLE_SBR 1
  58. #endif
  59. #pragma GCC optimize ("O3")
  60. #include "aacdec.h"
  61. #include "statname.h"
  62. /* 12-bit syncword */
  63. #define SYNCWORDH 0xff
  64. #define SYNCWORDL 0xf0
  65. #define MAX_NCHANS_ELEM 2 /* max number of channels in any single bitstream element (SCE,CPE,CCE,LFE) */
  66. #define ADTS_HEADER_BYTES 7
  67. #define NUM_SAMPLE_RATES 12
  68. #define NUM_DEF_CHAN_MAPS 8
  69. #define NUM_ELEMENTS 8
  70. #define MAX_NUM_PCE_ADIF 16
  71. #define MAX_WIN_GROUPS 8
  72. #define MAX_SFB_SHORT 15
  73. #define MAX_SF_BANDS (MAX_SFB_SHORT*MAX_WIN_GROUPS) /* worst case = 15 sfb's * 8 windows for short block */
  74. #define MAX_MS_MASK_BYTES ((MAX_SF_BANDS + 7) >> 3)
  75. #define MAX_PRED_SFB 41
  76. #define MAX_TNS_FILTERS 8
  77. #define MAX_TNS_COEFS 60
  78. #define MAX_TNS_ORDER 20
  79. #define MAX_PULSES 4
  80. #define MAX_GAIN_BANDS 3
  81. #define MAX_GAIN_WIN 8
  82. #define MAX_GAIN_ADJUST 7
  83. #define NSAMPS_LONG 1024
  84. #define NSAMPS_SHORT 128
  85. #define NUM_SYN_ID_BITS 3
  86. #define NUM_INST_TAG_BITS 4
  87. #define EXT_SBR_DATA 0x0d
  88. #define EXT_SBR_DATA_CRC 0x0e
  89. #define IS_ADIF(p) ((p)[0] == 'A' && (p)[1] == 'D' && (p)[2] == 'I' && (p)[3] == 'F')
  90. #define GET_ELE_ID(p) ((AACElementID)(*(p) >> (8-NUM_SYN_ID_BITS)))
  91. /* AAC file format */
  92. enum {
  93. AAC_FF_Unknown = 0, /* should be 0 on init */
  94. AAC_FF_ADTS = 1,
  95. AAC_FF_ADIF = 2,
  96. AAC_FF_RAW = 3
  97. };
  98. /* syntactic element type */
  99. enum {
  100. AAC_ID_INVALID = -1,
  101. AAC_ID_SCE = 0,
  102. AAC_ID_CPE = 1,
  103. AAC_ID_CCE = 2,
  104. AAC_ID_LFE = 3,
  105. AAC_ID_DSE = 4,
  106. AAC_ID_PCE = 5,
  107. AAC_ID_FIL = 6,
  108. AAC_ID_END = 7
  109. };
  110. typedef struct _AACDecInfo {
  111. /* pointers to platform-specific state information */
  112. void *psInfoBase; /* baseline MPEG-4 LC decoding */
  113. void *psInfoSBR; /* MPEG-4 SBR decoding */
  114. /* raw decoded data, before rounding to 16-bit PCM (for postprocessing such as SBR) */
  115. void *rawSampleBuf[AAC_MAX_NCHANS];
  116. int rawSampleBytes;
  117. int rawSampleFBits;
  118. /* fill data (can be used for processing SBR or other extensions) */
  119. unsigned char *fillBuf;
  120. int fillCount;
  121. int fillExtType;
  122. /* block information */
  123. int prevBlockID;
  124. int currBlockID;
  125. int currInstTag;
  126. int sbDeinterleaveReqd[MAX_NCHANS_ELEM];
  127. int adtsBlocksLeft;
  128. /* user-accessible info */
  129. int bitRate;
  130. int nChans;
  131. int sampRate;
  132. int profile;
  133. int format;
  134. int sbrEnabled;
  135. int tnsUsed;
  136. int pnsUsed;
  137. int frameCount;
  138. } AACDecInfo;
  139. /* decoder functions which must be implemented for each platform */
  140. AACDecInfo *AllocateBuffers(void);
  141. AACDecInfo *AllocateBuffersPre(void **space, int *len);
  142. void FreeBuffers(AACDecInfo *aacDecInfo);
  143. void ClearBuffer(void *buf, int nBytes);
  144. int UnpackADTSHeader(AACDecInfo *aacDecInfo, unsigned char **buf, int *bitOffset, int *bitsAvail);
  145. int GetADTSChannelMapping(AACDecInfo *aacDecInfo, unsigned char *buf, int bitOffset, int bitsAvail);
  146. int UnpackADIFHeader(AACDecInfo *aacDecInfo, unsigned char **buf, int *bitOffset, int *bitsAvail);
  147. int SetRawBlockParams(AACDecInfo *aacDecInfo, int copyLast, int nChans, int sampRate, int profile);
  148. int PrepareRawBlock(AACDecInfo *aacDecInfo);
  149. int FlushCodec(AACDecInfo *aacDecInfo);
  150. int DecodeNextElement(AACDecInfo *aacDecInfo, unsigned char **buf, int *bitOffset, int *bitsAvail);
  151. int DecodeNoiselessData(AACDecInfo *aacDecInfo, unsigned char **buf, int *bitOffset, int *bitsAvail, int ch);
  152. int Dequantize(AACDecInfo *aacDecInfo, int ch);
  153. int StereoProcess(AACDecInfo *aacDecInfo);
  154. int DeinterleaveShortBlocks(AACDecInfo *aacDecInfo, int ch);
  155. int PNS(AACDecInfo *aacDecInfo, int ch);
  156. int TNSFilter(AACDecInfo *aacDecInfo, int ch);
  157. int IMDCT(AACDecInfo *aacDecInfo, int ch, int chBase, short *outbuf);
  158. /* SBR specific functions */
  159. int InitSBR(AACDecInfo *aacDecInfo);
  160. int InitSBRPre(AACDecInfo *aacDecInfo, void **ptr, int *sz);
  161. void FreeSBR(AACDecInfo *aacDecInfo);
  162. int DecodeSBRBitstream(AACDecInfo *aacDecInfo, int chBase);
  163. int DecodeSBRData(AACDecInfo *aacDecInfo, int chBase, short *outbuf);
  164. int FlushCodecSBR(AACDecInfo *aacDecInfo);
  165. /* aactabs.c - global ROM tables */
  166. extern const int sampRateTab[NUM_SAMPLE_RATES];
  167. extern const int predSFBMax[NUM_SAMPLE_RATES];
  168. extern const int channelMapTab[NUM_DEF_CHAN_MAPS];
  169. extern const int elementNumChans[NUM_ELEMENTS];
  170. extern const unsigned /*char*/ int sfBandTotalShort[NUM_SAMPLE_RATES];
  171. extern const unsigned /*char*/ int sfBandTotalLong[NUM_SAMPLE_RATES];
  172. extern const int sfBandTabShortOffset[NUM_SAMPLE_RATES];
  173. extern const /*short*/ int sfBandTabShort[76];
  174. extern const int sfBandTabLongOffset[NUM_SAMPLE_RATES];
  175. extern const /*short*/ int sfBandTabLong[325];
  176. extern const int tnsMaxBandsShortOffset[AAC_NUM_PROFILES];
  177. extern const unsigned /*char*/ int tnsMaxBandsShort[2*NUM_SAMPLE_RATES];
  178. extern const unsigned /*char*/ int tnsMaxOrderShort[AAC_NUM_PROFILES];
  179. extern const int tnsMaxBandsLongOffset[AAC_NUM_PROFILES];
  180. extern const unsigned /*char*/ int tnsMaxBandsLong[2*NUM_SAMPLE_RATES];
  181. extern const unsigned /*char*/ int tnsMaxOrderLong[AAC_NUM_PROFILES];
  182. #endif /* _AACCOMMON_H */