Tag Parser  7.0.3
C++ library for reading and writing MP4 (iTunes), ID3, Vorbis, Opus, FLAC and Matroska tags
aacframe.h
Go to the documentation of this file.
1 #ifndef TAG_PARSER_AACFRAME_H
2 #define TAG_PARSER_AACFRAME_H
3 
4 // NOTE: The AAC parser is still WIP. It does not work yet and its API/ABI may change even in patch releases.
5 
6 #include <c++utilities/io/bitreader.h>
7 
8 #include <memory>
9 
10 namespace TagParser {
11 
12 class AdtsFrame;
13 
14 constexpr auto aacMaxChannels = 64;
15 constexpr auto aacMaxSyntaxElements = 48;
16 constexpr auto aacMaxWindowGroups = 8;
17 constexpr auto aacMaxSfb = 51;
18 constexpr auto aacMaxLtpSfb = 40;
19 constexpr auto aacMaxltpSfbS = 8;
20 constexpr auto aacInvalidSbrElement = 0xFF;
21 constexpr auto aacNoTimeSlots960 = 15;
22 constexpr auto aacNoTimeSlots = 16;
23 constexpr auto aacSbrRate = 2;
24 constexpr auto aacSbrM = 49;
25 constexpr auto aacSbrMaxLe = 5;
26 constexpr auto aacSbrMaxNtsrhfg = 40;
27 
28 typedef const sbyte (*SbrHuffTab)[2];
29 
30 namespace AacSyntaxElementTypes {
31 enum KnownTypes : byte {
40 };
41 }
42 
43 namespace AacIcsSequenceTypes {
45 }
46 
47 namespace AacScaleFactorTypes {
48 enum KnownTypes : byte { ZeroHcb = 0, FirstPairHcb = 5, EscHcb = 11, QuadLen = 4, PairLen = 2, NoiseHcb = 13, IntensityHcb2 = 14, IntensityHcb = 15 };
49 }
50 
51 namespace AacExtensionTypes {
52 enum KnownTypes : byte { Fill = 0, FillData = 1, DataElement = 2, DynamicRange = 11, SacData = 12, SbrData = 13, SbrDataCrc = 14 };
53 }
54 
55 namespace BsFrameClasses {
56 enum BsFrameClass : byte { FixFix, FixVar, VarFix, VarVar };
57 }
58 
59 namespace AacSbrExtensionIds {
60 enum KnownIds : byte { DrmParametricStereo = 0, Ps = 2 };
61 }
62 
63 struct LIB_EXPORT AacLtpInfo {
64  AacLtpInfo();
65  byte lastBand;
67  uint16 lag;
68  byte lagUpdate;
69  byte coef;
70  byte longUsed[aacMaxLtpSfb];
71  byte shortUsed[8];
72  byte shortLagPresent[8];
73  byte shortLag[8];
74 };
75 
76 struct LIB_EXPORT AacPredictorInfo {
78  byte maxSfb;
79  byte reset;
81  byte predictionUsed[aacMaxSfb];
82 };
83 
84 struct LIB_EXPORT AacPulseInfo {
85  AacPulseInfo();
86  byte count;
87  byte startSfb;
88  byte offset[4];
89  byte amp[4];
90 };
91 
92 struct LIB_EXPORT AacTnsInfo {
93  AacTnsInfo();
94  byte filt[8];
95  byte coefRes[8];
96  byte length[8][4];
97  byte order[8][4];
98  byte direction[8][4];
99  byte coefCompress[8][4];
100  byte coef[8][4][32];
101 };
102 
103 struct LIB_EXPORT AacSsrInfo {
104  AacSsrInfo();
105  byte maxBand;
106  byte adjustNum[4][8];
107  byte alevcode[4][8][8];
108  byte aloccode[4][8][8];
109 };
110 
111 struct LIB_EXPORT AacDrcInfo {
112  AacDrcInfo();
113  byte present;
114  byte bandCount;
117  byte bandTop[17];
119  byte dynamicRangeSign[17];
120  byte dynamicRangeControl[17];
121  byte excludeMask[aacMaxChannels];
122  byte additionalExcludedChannels[aacMaxChannels];
123 };
124 
125 struct LIB_EXPORT AacPsInfo {
126  AacPsInfo();
129  byte enableIID; // Inter-channel Intensity Difference
130  byte iidMode;
133  // TODO
134 };
135 
136 struct LIB_EXPORT AacDrmPsInfo {
137  AacDrmPsInfo();
140  byte enableIID; // Inter-channel Intensity Difference
141  byte iidMode;
144  // TODO
145 };
146 
147 struct LIB_EXPORT AacSbrInfo {
148  AacSbrInfo(byte sbrElementType, uint16 samplingFrequency, uint16 frameLength, bool isDrm);
149 
152 
153  uint32 maxAacLine;
154 
155  byte rate;
157  byte ret;
158 
159  byte ampRes[2];
160 
161  byte k0;
162  byte kx;
163  byte m;
164  byte nMaster;
165  byte nHigh;
166  byte nLow;
167  byte nq;
168  byte nl[4];
169  byte n[2];
170 
171  byte fMaster[64];
172  byte fTableRes[2][64];
173  byte fTableNoise[64];
174  byte fTableLim[4][64];
175  byte fGroup[5][64];
176  byte ng[5];
177 
178  byte tableMapKToG[64];
179 
180  byte absBordLead[2];
181  byte absBordTrail[2];
182  byte relLeadCount[2];
183  byte relTrailCount[2];
184 
185  byte le[2];
186  byte lePrev[2];
187  byte lq[2];
188 
189  byte te[2][aacSbrMaxLe + 1];
190  byte tq[2][3];
191  byte f[2][aacSbrMaxLe + 1];
192  byte fPrev[2];
193 
194  //real_t *gTempPrev[2][5];
195  //real_t *qTempPrev[2][5];
196  //sbyte gqRingbufIndex[2];
197 
198  int16 e[2][64][aacSbrMaxLe];
199  int16 ePrev[2][64];
200  //real_t eOrig[2][64][aacSbrMaxLe];
201  //real_t eCurr[2][64][aacSbrMaxLe];
202  int32 q[2][64][2];
203  //real_t qDiv[2][64][2];
204  //real_t qDiv2[2][64][2];
205  int32 qPrev[2][64];
206 
207  sbyte la[2];
208  sbyte laPrev[2];
209 
210  byte bsInvfMode[2][aacSbrMaxLe];
211  byte bsInvfModePrev[2][aacSbrMaxLe];
212  //real_t bwArray[2][64];
213  //real_t bwArrayPrev[2][64];
214 
215  byte noPatches;
216  byte patchNoSubbands[64];
217  byte patchStartSubband[64];
218 
219  byte bsAddHarmonic[2][64];
220  byte bsAddHarmonicPrev[2][64];
221 
222  uint16 indexNoisePrev[2];
223  byte psiIsPrev[2];
224 
231 
232  sbyte prevEnvIsShort[2];
233 
234  sbyte kxPrev;
235  byte bsco;
236  byte bscoPrev;
237  byte mPrev;
238  uint16 frameLength;
239 
240  byte reset;
241  uint32 frame;
242  uint32 headerCount;
243 
244  byte idAac;
245  //qmfa_info *qmfa[2];
246  //qmfs_info *qmfs[2];
247 
248  //qmf_t Xsbr[2][aacSbrMaxNtsrhfg][64];
249 
250  byte isDrmSbr;
251  std::shared_ptr<AacDrmPsInfo> drmPs;
252 
255  byte tHfGen;
256  byte tHfAdj;
257 
258  std::shared_ptr<AacPsInfo> ps;
259  byte psUsed;
261 
263  byte bsCrcFlag;
264  uint16 bsSbrCrcBits;
266  byte bsAmpRes;
278  byte bsAddHarmonicFlag[2];
279  byte bsAddHarmonicFlagPrev[2];
284  byte bsFrameClass[2];
285  byte bsRelBord[2][9];
286  byte bsRelBord0[2][9];
287  byte bsRelBord1[2][9];
288  byte bsPointer[2];
289  byte bsAbsBord0[2];
290  byte bsAbsBord1[2];
291  byte bsRelCount0[2];
292  byte bsRelCount1[2];
293  byte bsDfEnv[2][9];
294  byte bsDfNoise[2][3];
295 };
296 
297 struct LIB_EXPORT AacProgramConfig {
315  byte frontElementIsCpe[16];
316  byte frontElementTagSelect[16];
317  byte sideElementIsCpe[16];
318  byte sideElementTagSelect[16];
319  byte backElementIsCpe[16];
320  byte backElementTagSelect[16];
321  byte lfeElementTagSelect[16];
322  byte assocDataElementTagSelect[16];
323  byte ccElementIsIndSw[16];
324  byte validCcElementTagSelect[16];
325  byte channels;
327  byte commentFieldData[257];
332  byte sceChannel[16];
333  byte cpeChannel[16];
334 };
335 
336 struct LIB_EXPORT AacIcsInfo {
337  AacIcsInfo();
338 
339  byte maxSfb;
340 
341  byte swbCount;
345  byte windowGroupLengths[8];
348  uint16 sectionSfbOffset[8][15 * 8];
349  uint16 swbOffset[52];
350  uint16 maxSwbOffset;
351 
352  byte sectionCb[8][15 * 8];
353  uint16 sectionStart[8][15 * 8];
354  uint16 sectionEnd[8][15 * 8];
355  byte sfbCb[8][15 * 8];
356  byte sectionsPerGroup[8];
357 
359  uint16 scaleFactors[8][51];
360 
362  byte midSideCodingUsed[aacMaxWindowGroups][aacMaxSfb];
363 
364  byte noiseUsed;
365  byte isUsed;
366 
371 
378  std::shared_ptr<AacSbrInfo> sbr;
379 
380  // error resilience
385  uint16 rvlcSfLength;
386  uint16 dpcmNoiseNrg;
390 };
391 
392 class LIB_EXPORT AacFrameElementParser {
393 public:
395  byte audioObjectId, byte samplingFrequencyIndex, byte extensionSamplingFrequencyIndex, byte channelConfig, uint16 frameLength = 1024);
396 
397  void parse(const AdtsFrame &adtsFrame, std::unique_ptr<char[]> &data, std::size_t dataSize);
398  void parse(const AdtsFrame &adtsFrame, std::istream &stream, std::size_t dataSize);
399 
400 private:
401  void parseLtpInfo(const AacIcsInfo &ics, AacLtpInfo &ltp);
402  void parseIcsInfo(AacIcsInfo &ics);
403  void parseSectionData(AacIcsInfo &ics);
404  void decodeScaleFactorData(AacIcsInfo &ics);
405  void decodeRvlcScaleFactorData(AacIcsInfo &ics);
406  void parseScaleFactorData(AacIcsInfo &ics);
407  void parsePulseData(AacIcsInfo &ics);
408  void parseTnsData(AacIcsInfo &ics);
409  void parseGainControlData(AacIcsInfo &ics);
410  void parseSpectralData(AacIcsInfo &ics, int16 *specData);
411  void parseSideInfo(AacIcsInfo &ics, bool scaleFlag);
412  byte parseExcludedChannels();
413  byte parseDynamicRange();
414  static sbyte sbrLog2(const sbyte val);
415  int16 sbrHuffmanDec(SbrHuffTab table);
416  void parseSbrGrid(std::shared_ptr<AacSbrInfo> &sbr, byte channel);
417  void parseSbrDtdf(std::shared_ptr<AacSbrInfo> &sbr, byte channel);
418  void parseInvfMode(std::shared_ptr<AacSbrInfo> &sbr, byte channel);
419  void parseSbrEnvelope(std::shared_ptr<AacSbrInfo> &sbr, byte channel);
420  void parseSbrNoise(std::shared_ptr<AacSbrInfo> &sbr, byte channel);
421  void parseSbrSinusoidalCoding(std::shared_ptr<AacSbrInfo> &sbr, byte channel);
422  uint16 parseSbrExtension(std::shared_ptr<AacSbrInfo> &sbr, byte extensionId, byte bitsLeft);
423  uint16 parsePsData(std::shared_ptr<AacPsInfo> &ps, byte &header);
424  uint16 parseDrmPsData(std::shared_ptr<AacDrmPsInfo> &drmPs);
425  void parseSbrSingleChannelElement(std::shared_ptr<AacSbrInfo> &sbr);
426  void parseSbrChannelPairElement(std::shared_ptr<AacSbrInfo> &sbr);
427  std::shared_ptr<AacSbrInfo> makeSbrInfo(byte sbrElement, bool isDrm = false);
428  void parseSbrExtensionData(byte sbrElement, uint16 count, bool crcFlag);
429  byte parseHuffmanScaleFactor();
430  void parseHuffmanSpectralData(byte cb, int16 *sp);
431  void huffmanSignBits(int16 *sp, byte len);
432  void huffman2StepQuad(byte cb, int16 *sp);
433  void huffmanBinaryQuadSign(byte cb, int16 *sp);
434  void huffmanBinaryPair(byte cb, int16 *sp);
435  void huffman2StepPair(byte cb, int16 *sp);
436  void huffmanBinaryPairSign(byte cb, int16 *sp);
437  void huffman2StepPairSign(byte cb, int16 *sp);
438  int16 huffmanGetEscape(int16 sp);
439  constexpr static int16 huffmanCodebook(byte i);
440  static void vcb11CheckLav(byte cb, int16 *sp);
441  void calculateWindowGroupingInfo(AacIcsInfo &ics);
442  void parseIndividualChannelStream(AacIcsInfo &ics, int16 *specData, bool scaleFlag = false);
443  void parseSingleChannelElement();
444  void parseChannelPairElement();
445  void parseCouplingChannelElement();
446  void parseLowFrequencyElement();
447  void parseDataStreamElement();
448  void parseProgramConfigElement();
449  void parseFillElement(byte sbrElement = aacInvalidSbrElement);
450  void parseRawDataBlock();
451 
452  // these fields contain setup information
453  IoUtilities::BitReader m_reader;
454  byte m_mpeg4AudioObjectId;
455  byte m_mpeg4SamplingFrequencyIndex;
456  byte m_mpeg4ExtensionSamplingFrequencyIndex;
457  byte m_mpeg4ChannelConfig;
458  uint16 m_frameLength;
459  byte m_aacSectionDataResilienceFlag;
460  byte m_aacScalefactorDataResilienceFlag;
461  byte m_aacSpectralDataResilienceFlag;
462  // these fields will be parsed
463  byte m_elementId[aacMaxChannels];
464  byte m_channelCount;
465  byte m_elementCount;
466  byte m_elementChannelCount[aacMaxSyntaxElements];
467  //byte m_channel;
468  //int16 m_pairedChannel;
469  byte m_elementInstanceTag[aacMaxSyntaxElements];
470  byte m_commonWindow;
471  AacIcsInfo m_ics1;
472  AacIcsInfo m_ics2;
473  AacDrcInfo m_drc;
474  AacProgramConfig m_pce;
475  byte m_sbrPresentFlag;
476  //byte m_forceUpSampling;
477  //byte m_downSampledSbr;
478  std::shared_ptr<AacSbrInfo> m_sbrElements[aacMaxSyntaxElements];
479  byte m_psUsed[aacMaxSyntaxElements];
480  byte m_psUsedGlobal;
481  byte m_psResetFlag;
482 };
483 
488  byte audioObjectId, byte samplingFrequencyIndex, byte extensionSamplingFrequencyIndex, byte channelConfig, uint16 frameLength)
489  : m_reader(nullptr, nullptr)
490  , m_mpeg4AudioObjectId(audioObjectId)
491  , m_mpeg4SamplingFrequencyIndex(samplingFrequencyIndex)
492  , m_mpeg4ExtensionSamplingFrequencyIndex(extensionSamplingFrequencyIndex)
493  , m_mpeg4ChannelConfig(channelConfig)
494  , m_frameLength(frameLength)
495  , m_aacSpectralDataResilienceFlag(0)
496  , m_elementId{ 0 }
497  , m_channelCount(0)
498  , m_elementCount(0)
499  , m_elementChannelCount{ 0 }
500  , m_elementInstanceTag{ 0 }
501  , m_commonWindow(0)
502  ,
503  //m_channel(0),
504  //m_pairedChannel(0),
505  m_sbrPresentFlag(0)
506  ,
507  //m_forceUpSampling(0),
508  //m_downSampledSbr(0),
509  m_sbrElements{ 0 }
510  , m_psUsed{ 0 }
511  , m_psUsedGlobal(0)
512  , m_psResetFlag(0)
513 {
514 }
515 
516 inline sbyte AacFrameElementParser::sbrLog2(const sbyte val)
517 {
518  static const int log2tab[] = { 0, 0, 1, 2, 2, 3, 3, 3, 3, 4 };
519  return (val < 10 && val >= 0) ? log2tab[val] : 0;
520 }
521 
522 constexpr int16 AacFrameElementParser::huffmanCodebook(byte i)
523 {
524  return static_cast<int16>(i ? (16428320 & 0xFFFF) : ((16428320 >> 16) & 0xFFFF));
525 }
526 
527 } // namespace TagParser
528 
529 #endif // TAG_PARSER_AACFRAME_H
const sbyte(* SbrHuffTab)[2]
Definition: aacframe.h:28
uint16 reorderedSpectralDataLength
Definition: aacframe.h:381
AacPulseInfo pulse
Definition: aacframe.h:372
constexpr auto aacMaxltpSfbS
Definition: aacframe.h:19
constexpr auto aacSbrRate
Definition: aacframe.h:23
constexpr auto aacSbrMaxLe
Definition: aacframe.h:25
constexpr auto aacMaxWindowGroups
Definition: aacframe.h:16
std::shared_ptr< AacSbrInfo > sbr
Definition: aacframe.h:378
constexpr auto aacSbrM
Definition: aacframe.h:24
constexpr auto aacMaxSfb
Definition: aacframe.h:17
std::shared_ptr< AacPsInfo > ps
Definition: aacframe.h:258
constexpr auto aacNoTimeSlots
Definition: aacframe.h:22
constexpr auto aacSbrMaxNtsrhfg
Definition: aacframe.h:26
std::shared_ptr< AacDrmPsInfo > drmPs
Definition: aacframe.h:251
constexpr auto aacMaxLtpSfb
Definition: aacframe.h:18
constexpr auto aacInvalidSbrElement
Definition: aacframe.h:20
byte excludedChannelsPresent
Definition: aacframe.h:116
constexpr auto aacMaxSyntaxElements
Definition: aacframe.h:15
AacPredictorInfo predictor
Definition: aacframe.h:374
byte midSideCodingMaskPresent
Definition: aacframe.h:361
constexpr auto aacMaxChannels
Definition: aacframe.h:14
AacFrameElementParser(byte audioObjectId, byte samplingFrequencyIndex, byte extensionSamplingFrequencyIndex, byte channelConfig, uint16 frameLength=1024)
Constructs a new parser with the specified setup information.
Definition: aacframe.h:487
constexpr auto aacNoTimeSlots960
Definition: aacframe.h:21