Tag Parser  8.2.0
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 
13 
14 class AdtsFrame;
15 
16 constexpr auto aacMaxChannels = 64;
17 constexpr auto aacMaxSyntaxElements = 48;
18 constexpr auto aacMaxWindowGroups = 8;
19 constexpr auto aacMaxSfb = 51;
20 constexpr auto aacMaxLtpSfb = 40;
21 constexpr auto aacMaxltpSfbS = 8;
22 constexpr auto aacInvalidSbrElement = 0xFF;
23 constexpr auto aacNoTimeSlots960 = 15;
24 constexpr auto aacNoTimeSlots = 16;
25 constexpr auto aacSbrRate = 2;
26 constexpr auto aacSbrM = 49;
27 constexpr auto aacSbrMaxLe = 5;
28 constexpr auto aacSbrMaxNtsrhfg = 40;
29 
30 using SbrHuffTab = const sbyte (*)[2];
31 
32 namespace AacSyntaxElementTypes {
33 enum KnownTypes : byte {
34  SingleChannelElement,
35  ChannelPairElement,
36  ChannelCouplingElement,
37  LowFrequencyElement,
38  DataStreamElement,
39  ProgramConfigElement,
40  FillElement,
41  EndOfFrame
42 };
43 }
44 
45 namespace AacIcsSequenceTypes {
46 enum KnownTypes : byte { OnlyLongSequence, LongStartSequence, EightShortSequence, LongStopSequence };
47 }
48 
49 namespace AacScaleFactorTypes {
50 enum KnownTypes : byte { ZeroHcb = 0, FirstPairHcb = 5, EscHcb = 11, QuadLen = 4, PairLen = 2, NoiseHcb = 13, IntensityHcb2 = 14, IntensityHcb = 15 };
51 }
52 
53 namespace AacExtensionTypes {
54 enum KnownTypes : byte { Fill = 0, FillData = 1, DataElement = 2, DynamicRange = 11, SacData = 12, SbrData = 13, SbrDataCrc = 14 };
55 }
56 
57 namespace BsFrameClasses {
58 enum BsFrameClass : byte { FixFix, FixVar, VarFix, VarVar };
59 }
60 
61 namespace AacSbrExtensionIds {
62 enum KnownIds : byte { DrmParametricStereo = 0, Ps = 2 };
63 }
64 
65 struct LIB_EXPORT AacLtpInfo {
66  AacLtpInfo();
67  byte lastBand;
68  byte dataPresent;
69  uint16 lag;
70  byte lagUpdate;
71  byte coef;
72  byte longUsed[aacMaxLtpSfb];
73  byte shortUsed[8];
74  byte shortLagPresent[8];
75  byte shortLag[8];
76 };
77 
78 struct LIB_EXPORT AacPredictorInfo {
79  AacPredictorInfo();
80  byte maxSfb;
81  byte reset;
82  byte resetGroupNumber;
83  byte predictionUsed[aacMaxSfb];
84 };
85 
86 struct LIB_EXPORT AacPulseInfo {
87  AacPulseInfo();
88  byte count;
89  byte startSfb;
90  byte offset[4];
91  byte amp[4];
92 };
93 
94 struct LIB_EXPORT AacTnsInfo {
95  AacTnsInfo();
96  byte filt[8];
97  byte coefRes[8];
98  byte length[8][4];
99  byte order[8][4];
100  byte direction[8][4];
101  byte coefCompress[8][4];
102  byte coef[8][4][32];
103 };
104 
105 struct LIB_EXPORT AacSsrInfo {
106  AacSsrInfo();
107  byte maxBand;
108  byte adjustNum[4][8];
109  byte alevcode[4][8][8];
110  byte aloccode[4][8][8];
111 };
112 
113 struct LIB_EXPORT AacDrcInfo {
114  AacDrcInfo();
115  byte present;
116  byte bandCount;
117  byte pceInstanceTag;
118  byte excludedChannelsPresent;
119  byte bandTop[17];
120  byte progRefLevel;
121  byte dynamicRangeSign[17];
122  byte dynamicRangeControl[17];
123  byte excludeMask[aacMaxChannels];
124  byte additionalExcludedChannels[aacMaxChannels];
125 };
126 
127 struct LIB_EXPORT AacPsInfo {
128  AacPsInfo();
129  byte headerRead;
130  byte use34HybridBands;
131  byte enableIID; // Inter-channel Intensity Difference
132  byte iidMode;
133  byte iidParCount;
134  byte iidopdParCount;
135  // TODO
136 };
137 
138 struct LIB_EXPORT AacDrmPsInfo {
139  AacDrmPsInfo();
140  byte headerRead;
141  byte use34HybridBands;
142  byte enableIID; // Inter-channel Intensity Difference
143  byte iidMode;
144  byte iidParCount;
145  byte iidopdParCount;
146  // TODO
147 };
148 
149 struct LIB_EXPORT AacSbrInfo {
150  AacSbrInfo(byte sbrElementType, uint16 samplingFrequency, uint16 frameLength, bool isDrm);
151 
152  byte aacElementId;
153  byte samplingFrequency;
154 
155  uint32 maxAacLine;
156 
157  byte rate;
158  byte justSeeked;
159  byte ret;
160 
161  byte ampRes[2];
162 
163  byte k0;
164  byte kx;
165  byte m;
166  byte nMaster;
167  byte nHigh;
168  byte nLow;
169  byte nq;
170  byte nl[4];
171  byte n[2];
172 
173  byte fMaster[64];
174  byte fTableRes[2][64];
175  byte fTableNoise[64];
176  byte fTableLim[4][64];
177  byte fGroup[5][64];
178  byte ng[5];
179 
180  byte tableMapKToG[64];
181 
182  byte absBordLead[2];
183  byte absBordTrail[2];
184  byte relLeadCount[2];
185  byte relTrailCount[2];
186 
187  byte le[2];
188  byte lePrev[2];
189  byte lq[2];
190 
191  byte te[2][aacSbrMaxLe + 1];
192  byte tq[2][3];
193  byte f[2][aacSbrMaxLe + 1];
194  byte fPrev[2];
195 
196  //real_t *gTempPrev[2][5];
197  //real_t *qTempPrev[2][5];
198  //sbyte gqRingbufIndex[2];
199 
200  int16 e[2][64][aacSbrMaxLe];
201  int16 ePrev[2][64];
202  //real_t eOrig[2][64][aacSbrMaxLe];
203  //real_t eCurr[2][64][aacSbrMaxLe];
204  int32 q[2][64][2];
205  //real_t qDiv[2][64][2];
206  //real_t qDiv2[2][64][2];
207  int32 qPrev[2][64];
208 
209  sbyte la[2];
210  sbyte laPrev[2];
211 
212  byte bsInvfMode[2][aacSbrMaxLe];
213  byte bsInvfModePrev[2][aacSbrMaxLe];
214  //real_t bwArray[2][64];
215  //real_t bwArrayPrev[2][64];
216 
217  byte noPatches;
218  byte patchNoSubbands[64];
219  byte patchStartSubband[64];
220 
221  byte bsAddHarmonic[2][64];
222  byte bsAddHarmonicPrev[2][64];
223 
224  uint16 indexNoisePrev[2];
225  byte psiIsPrev[2];
226 
227  byte bsStartFreqPrev;
228  byte bsStopFreqPrev;
229  byte bsXoverBandPrev;
230  byte bsFreqScalePrev;
231  byte bsAlterScalePrev;
232  byte bsNoiseBandsPrev;
233 
234  sbyte prevEnvIsShort[2];
235 
236  sbyte kxPrev;
237  byte bsco;
238  byte bscoPrev;
239  byte mPrev;
240  uint16 frameLength;
241 
242  byte reset;
243  uint32 frame;
244  uint32 headerCount;
245 
246  byte idAac;
247  //qmfa_info *qmfa[2];
248  //qmfs_info *qmfs[2];
249 
250  //qmf_t Xsbr[2][aacSbrMaxNtsrhfg][64];
251 
252  byte isDrmSbr;
253  std::shared_ptr<AacDrmPsInfo> drmPs;
254 
255  byte timeSlotsRateCount;
256  byte timeSlotsCount;
257  byte tHfGen;
258  byte tHfAdj;
259 
260  std::shared_ptr<AacPsInfo> ps;
261  byte psUsed;
262  byte psResetFlag;
263 
264  byte bsHeaderFlag;
265  byte bsCrcFlag;
266  uint16 bsSbrCrcBits;
267  byte bsProtocolVersion;
268  byte bsAmpRes;
269  byte bsStartFreq;
270  byte bsStopFreq;
271  byte bsXoverBand;
272  byte bsFreqScale;
273  byte bsAlterScale;
274  byte bsNoiseBands;
275  byte bsLimiterBands;
276  byte bsLimiterGains;
277  byte bsInterpolFreq;
278  byte bsSmoothingMode;
279  byte bsSamplerateMode;
280  byte bsAddHarmonicFlag[2];
281  byte bsAddHarmonicFlagPrev[2];
282  byte bsExtendedData;
283  byte bsExtensionId;
284  byte bsExtensionData;
285  byte bsCoupling;
286  byte bsFrameClass[2];
287  byte bsRelBord[2][9];
288  byte bsRelBord0[2][9];
289  byte bsRelBord1[2][9];
290  byte bsPointer[2];
291  byte bsAbsBord0[2];
292  byte bsAbsBord1[2];
293  byte bsRelCount0[2];
294  byte bsRelCount1[2];
295  byte bsDfEnv[2][9];
296  byte bsDfNoise[2][3];
297 };
298 
299 struct LIB_EXPORT AacProgramConfig {
300  AacProgramConfig();
301  byte elementInstanceTag;
302  byte objectType;
303  byte samplingFrequencyIndex;
304  byte frontChannelElementCount;
305  byte sideChannelElementCount;
306  byte backChannelElementCount;
307  byte lfeChannelElementCount;
308  byte assocDataElementCount;
309  byte validCcElementCount;
310  byte monoMixdownPresent;
311  byte monoMixdownElementNumber;
312  byte stereoMixdownPresent;
313  byte stereoMixdownElementNumber;
314  byte matrixMixdownIdxPresent;
315  byte pseudoSurroundEnable;
316  byte matrixMixdownIdx;
317  byte frontElementIsCpe[16];
318  byte frontElementTagSelect[16];
319  byte sideElementIsCpe[16];
320  byte sideElementTagSelect[16];
321  byte backElementIsCpe[16];
322  byte backElementTagSelect[16];
323  byte lfeElementTagSelect[16];
324  byte assocDataElementTagSelect[16];
325  byte ccElementIsIndSw[16];
326  byte validCcElementTagSelect[16];
327  byte channels;
328  byte commentFieldBytes;
329  byte commentFieldData[257];
330  byte frontChannelCount;
331  byte sideChannelCount;
332  byte backChannelCount;
333  byte lfeChannelCount;
334  byte sceChannel[16];
335  byte cpeChannel[16];
336 };
337 
338 struct LIB_EXPORT AacIcsInfo {
339  AacIcsInfo();
340 
341  byte maxSfb;
342 
343  byte swbCount;
344  byte windowGroupCount;
345  byte windowCount;
346  byte windowSequence;
347  byte windowGroupLengths[8];
348  byte windowShape;
349  byte scaleFactorGrouping;
350  uint16 sectionSfbOffset[8][15 * 8];
351  uint16 swbOffset[52];
352  uint16 maxSwbOffset;
353 
354  byte sectionCb[8][15 * 8];
355  uint16 sectionStart[8][15 * 8];
356  uint16 sectionEnd[8][15 * 8];
357  byte sfbCb[8][15 * 8];
358  byte sectionsPerGroup[8];
359 
360  byte globalGain;
361  uint16 scaleFactors[8][51];
362 
363  byte midSideCodingMaskPresent;
364  byte midSideCodingUsed[aacMaxWindowGroups][aacMaxSfb];
365 
366  byte noiseUsed;
367  byte isUsed;
368 
369  byte pulseDataPresent;
370  byte tnsDataPresent;
371  byte gainControlPresent;
372  byte predictorDataPresent;
373 
374  AacPulseInfo pulse;
375  AacTnsInfo tns;
376  AacPredictorInfo predictor;
377  AacLtpInfo ltp1;
378  AacLtpInfo ltp2;
379  AacSsrInfo ssr;
380  std::shared_ptr<AacSbrInfo> sbr;
381 
382  // error resilience
383  uint16 reorderedSpectralDataLength;
384  byte longestCodewordLength;
385  byte sfConcealment;
386  byte revGlobalGain;
387  uint16 rvlcSfLength;
388  uint16 dpcmNoiseNrg;
389  byte sfEscapesPresent;
390  byte rvlcEscapesLength;
391  uint16 dpcmNoiseLastPos;
392 };
393 
394 class LIB_EXPORT AacFrameElementParser {
395 public:
396  AacFrameElementParser(
397  byte audioObjectId, byte samplingFrequencyIndex, byte extensionSamplingFrequencyIndex, byte channelConfig, uint16 frameLength = 1024);
398 
399  void parse(const AdtsFrame &adtsFrame, std::unique_ptr<char[]> &data, std::size_t dataSize);
400  void parse(const AdtsFrame &adtsFrame, std::istream &stream, std::size_t dataSize);
401 
402 private:
403  void parseLtpInfo(const AacIcsInfo &ics, AacLtpInfo &ltp);
404  void parseIcsInfo(AacIcsInfo &ics);
405  void parseSectionData(AacIcsInfo &ics);
406  void decodeScaleFactorData(AacIcsInfo &ics);
407  void decodeRvlcScaleFactorData(AacIcsInfo &ics);
408  void parseScaleFactorData(AacIcsInfo &ics);
409  void parsePulseData(AacIcsInfo &ics);
410  void parseTnsData(AacIcsInfo &ics);
411  void parseGainControlData(AacIcsInfo &ics);
412  void parseSpectralData(AacIcsInfo &ics, int16 *specData);
413  void parseSideInfo(AacIcsInfo &ics, bool scaleFlag);
414  byte parseExcludedChannels();
415  byte parseDynamicRange();
416  static sbyte sbrLog2(const sbyte val);
417  int16 sbrHuffmanDec(SbrHuffTab table);
418  void parseSbrGrid(std::shared_ptr<AacSbrInfo> &sbr, byte channel);
419  void parseSbrDtdf(std::shared_ptr<AacSbrInfo> &sbr, byte channel);
420  void parseInvfMode(std::shared_ptr<AacSbrInfo> &sbr, byte channel);
421  void parseSbrEnvelope(std::shared_ptr<AacSbrInfo> &sbr, byte channel);
422  void parseSbrNoise(std::shared_ptr<AacSbrInfo> &sbr, byte channel);
423  void parseSbrSinusoidalCoding(std::shared_ptr<AacSbrInfo> &sbr, byte channel);
424  uint16 parseSbrExtension(std::shared_ptr<AacSbrInfo> &sbr, byte extensionId, byte bitsLeft);
425  uint16 parsePsData(std::shared_ptr<AacPsInfo> &ps, byte &header);
426  uint16 parseDrmPsData(std::shared_ptr<AacDrmPsInfo> &drmPs);
427  void parseSbrSingleChannelElement(std::shared_ptr<AacSbrInfo> &sbr);
428  void parseSbrChannelPairElement(std::shared_ptr<AacSbrInfo> &sbr);
429  std::shared_ptr<AacSbrInfo> makeSbrInfo(byte sbrElement, bool isDrm = false);
430  void parseSbrExtensionData(byte sbrElement, uint16 count, bool crcFlag);
431  byte parseHuffmanScaleFactor();
432  void parseHuffmanSpectralData(byte cb, int16 *sp);
433  void huffmanSignBits(int16 *sp, byte len);
434  void huffman2StepQuad(byte cb, int16 *sp);
435  void huffmanBinaryQuadSign(byte cb, int16 *sp);
436  void huffmanBinaryPair(byte cb, int16 *sp);
437  void huffman2StepPair(byte cb, int16 *sp);
438  void huffmanBinaryPairSign(byte cb, int16 *sp);
439  void huffman2StepPairSign(byte cb, int16 *sp);
440  int16 huffmanGetEscape(int16 sp);
441  constexpr static int16 huffmanCodebook(byte i);
442  static void vcb11CheckLav(byte cb, int16 *sp);
443  void calculateWindowGroupingInfo(AacIcsInfo &ics);
444  void parseIndividualChannelStream(AacIcsInfo &ics, int16 *specData, bool scaleFlag = false);
445  void parseSingleChannelElement();
446  void parseChannelPairElement();
447  void parseCouplingChannelElement();
448  void parseLowFrequencyElement();
449  void parseDataStreamElement();
450  void parseProgramConfigElement();
451  void parseFillElement(byte sbrElement = aacInvalidSbrElement);
452  void parseRawDataBlock();
453 
454  // these fields contain setup information
455  IoUtilities::BitReader m_reader;
456  byte m_mpeg4AudioObjectId;
457  byte m_mpeg4SamplingFrequencyIndex;
458  byte m_mpeg4ExtensionSamplingFrequencyIndex;
459  byte m_mpeg4ChannelConfig;
460  uint16 m_frameLength;
461  byte m_aacSectionDataResilienceFlag;
462  byte m_aacScalefactorDataResilienceFlag;
463  byte m_aacSpectralDataResilienceFlag;
464  // these fields will be parsed
465  byte m_elementId[aacMaxChannels];
466  byte m_channelCount;
467  byte m_elementCount;
468  byte m_elementChannelCount[aacMaxSyntaxElements];
469  //byte m_channel;
470  //int16 m_pairedChannel;
471  byte m_elementInstanceTag[aacMaxSyntaxElements];
472  byte m_commonWindow;
473  AacIcsInfo m_ics1;
474  AacIcsInfo m_ics2;
475  AacDrcInfo m_drc;
476  AacProgramConfig m_pce;
477  byte m_sbrPresentFlag;
478  //byte m_forceUpSampling;
479  //byte m_downSampledSbr;
480  std::shared_ptr<AacSbrInfo> m_sbrElements[aacMaxSyntaxElements];
481  byte m_psUsed[aacMaxSyntaxElements];
482  byte m_psUsedGlobal;
483  byte m_psResetFlag;
484 };
485 
489 inline AacFrameElementParser::AacFrameElementParser(
490  byte audioObjectId, byte samplingFrequencyIndex, byte extensionSamplingFrequencyIndex, byte channelConfig, uint16 frameLength)
491  : m_reader(nullptr, nullptr)
492  , m_mpeg4AudioObjectId(audioObjectId)
493  , m_mpeg4SamplingFrequencyIndex(samplingFrequencyIndex)
494  , m_mpeg4ExtensionSamplingFrequencyIndex(extensionSamplingFrequencyIndex)
495  , m_mpeg4ChannelConfig(channelConfig)
496  , m_frameLength(frameLength)
497  , m_aacSpectralDataResilienceFlag(0)
498  , m_elementId{ 0 }
499  , m_channelCount(0)
500  , m_elementCount(0)
501  , m_elementChannelCount{ 0 }
502  , m_elementInstanceTag{ 0 }
503  , m_commonWindow(0)
504  ,
505  //m_channel(0),
506  //m_pairedChannel(0),
507  m_sbrPresentFlag(0)
508  ,
509  //m_forceUpSampling(0),
510  //m_downSampledSbr(0),
511  m_sbrElements{ 0 }
512  , m_psUsed{ 0 }
513  , m_psUsedGlobal(0)
514  , m_psResetFlag(0)
515 {
516 }
517 
518 inline sbyte AacFrameElementParser::sbrLog2(const sbyte val)
519 {
520  static const int log2tab[] = { 0, 0, 1, 2, 2, 3, 3, 3, 3, 4 };
521  return (val < 10 && val >= 0) ? log2tab[val] : 0;
522 }
523 
524 constexpr int16 AacFrameElementParser::huffmanCodebook(byte i)
525 {
526  return static_cast<int16>(i ? (16428320 & 0xFFFF) : ((16428320 >> 16) & 0xFFFF));
527 }
528 
530 
531 } // namespace TagParser
532 
533 #endif // TAG_PARSER_AACFRAME_H
Contains all classes and functions of the TagInfo library.
Definition: aaccodebook.h:9