Tag Parser  9.1.2
C++ library for reading and writing MP4 (iTunes), ID3, Vorbis, Opus, FLAC and Matroska tags
aacframe.cpp
Go to the documentation of this file.
1 #include "./aacframe.h"
2 #include "./aaccodebook.h"
3 
4 #include "../adts/adtsframe.h"
5 
6 #include "../mp4/mp4ids.h"
7 
8 #include "../exceptions.h"
9 
10 #include <c++utilities/io/bitreader.h>
11 
12 #include <istream>
13 #include <limits>
14 
15 using namespace std;
16 using namespace CppUtilities;
17 
23 namespace TagParser {
24 
26 
27 const std::uint8_t maxPredictionSfb[16] = { 33, 33, 38, 40, 40, 40, 41, 41, 37, 37, 37, 34, 64, 64, 64, 64 };
28 
29 const uint8_t swb512WindowCount[] = { 0, 0, 0, 36, 36, 37, 31, 31, 0, 0, 0, 0 };
30 
31 const std::uint8_t swb480WindowCount[] = { 0, 0, 0, 35, 35, 37, 30, 30, 0, 0, 0, 0 };
32 
33 const std::uint8_t swb960WindowCount[] = { 40, 40, 45, 49, 49, 49, 46, 46, 42, 42, 42, 40 };
34 
35 const std::uint8_t swb1024WindowCount[] = { 41, 41, 47, 49, 49, 51, 47, 47, 43, 43, 43, 40 };
36 
37 const std::uint8_t swb128WindowCount[] = { 12, 12, 12, 14, 14, 14, 15, 15, 15, 15, 15, 15 };
38 
39 const std::uint16_t swbOffset1024_96[] = { 0, 4, 8, 12, 16, 20, 24, 28, 32, 36, 40, 44, 48, 52, 56, 64, 72, 80, 88, 96, 108, 120, 132, 144, 156, 172,
40  188, 212, 240, 276, 320, 384, 448, 512, 576, 640, 704, 768, 832, 896, 960, 1024 };
41 
42 const std::uint16_t swbOffset128_96[] = { 0, 4, 8, 12, 16, 20, 24, 32, 40, 48, 64, 92, 128 };
43 
44 const std::uint16_t swbOffset1024_64[] = { 0, 4, 8, 12, 16, 20, 24, 28, 32, 36, 40, 44, 48, 52, 56, 64, 72, 80, 88, 100, 112, 124, 140, 156, 172, 192,
45  216, 240, 268, 304, 344, 384, 424, 464, 504, 544, 584, 624, 664, 704, 744, 784, 824, 864, 904, 944, 984, 1024 };
46 
47 const std::uint16_t swbOffset128_64[] = { 0, 4, 8, 12, 16, 20, 24, 32, 40, 48, 64, 92, 128 };
48 
49 const std::uint16_t swbOffset1024_48[] = { 0, 4, 8, 12, 16, 20, 24, 28, 32, 36, 40, 48, 56, 64, 72, 80, 88, 96, 108, 120, 132, 144, 160, 176, 196,
50  216, 240, 264, 292, 320, 352, 384, 416, 448, 480, 512, 544, 576, 608, 640, 672, 704, 736, 768, 800, 832, 864, 896, 928, 1024 };
51 
52 const std::uint16_t swbOffset512_48[] = { 0, 4, 8, 12, 16, 20, 24, 28, 32, 36, 40, 44, 48, 52, 56, 60, 68, 76, 84, 92, 100, 112, 124, 136, 148, 164,
53  184, 208, 236, 268, 300, 332, 364, 396, 428, 460, 512 };
54 
55 const std::uint16_t swbOffset480_48[] = { 0, 4, 8, 12, 16, 20, 24, 28, 32, 36, 40, 44, 48, 52, 56, 64, 72, 80, 88, 96, 108, 120, 132, 144, 156, 172,
56  188, 212, 240, 272, 304, 336, 368, 400, 432, 480 };
57 
58 const std::uint16_t swbOffset128_48[] = { 0, 4, 8, 12, 16, 20, 28, 36, 44, 56, 68, 80, 96, 112, 128 };
59 
60 const std::uint16_t swbOffset1024_32[] = { 0, 4, 8, 12, 16, 20, 24, 28, 32, 36, 40, 48, 56, 64, 72, 80, 88, 96, 108, 120, 132, 144, 160, 176, 196,
61  216, 240, 264, 292, 320, 352, 384, 416, 448, 480, 512, 544, 576, 608, 640, 672, 704, 736, 768, 800, 832, 864, 896, 928, 960, 992, 1024 };
62 
63 const std::uint16_t swbOffset512_32[] = { 0, 4, 8, 12, 16, 20, 24, 28, 32, 36, 40, 44, 48, 52, 56, 64, 72, 80, 88, 96, 108, 120, 132, 144, 160, 176,
64  192, 212, 236, 260, 288, 320, 352, 384, 416, 448, 480, 512 };
65 
66 const std::uint16_t swbOffset480_32[] = { 0, 4, 8, 12, 16, 20, 24, 28, 32, 36, 40, 44, 48, 52, 56, 60, 64, 72, 80, 88, 96, 104, 112, 124, 136, 148,
67  164, 180, 200, 224, 256, 288, 320, 352, 384, 416, 448, 480 };
68 
69 const std::uint16_t swbOffset1024_24[] = { 0, 4, 8, 12, 16, 20, 24, 28, 32, 36, 40, 44, 52, 60, 68, 76, 84, 92, 100, 108, 116, 124, 136, 148, 160,
70  172, 188, 204, 220, 240, 260, 284, 308, 336, 364, 396, 432, 468, 508, 552, 600, 652, 704, 768, 832, 896, 960, 1024 };
71 
72 const std::uint16_t swbOffset512_24[]
73  = { 0, 4, 8, 12, 16, 20, 24, 28, 32, 36, 40, 44, 52, 60, 68, 80, 92, 104, 120, 140, 164, 192, 224, 256, 288, 320, 352, 384, 416, 448, 480, 512 };
74 
75 const std::uint16_t swbOffset480_24[]
76  = { 0, 4, 8, 12, 16, 20, 24, 28, 32, 36, 40, 44, 52, 60, 68, 80, 92, 104, 120, 140, 164, 192, 224, 256, 288, 320, 352, 384, 416, 448, 480 };
77 
78 const std::uint16_t swbOffset128_24[] = { 0, 4, 8, 12, 16, 20, 24, 28, 36, 44, 52, 64, 76, 92, 108, 128 };
79 
80 const std::uint16_t swbOffset1024_16[] = { 0, 8, 16, 24, 32, 40, 48, 56, 64, 72, 80, 88, 100, 112, 124, 136, 148, 160, 172, 184, 196, 212, 228, 244,
81  260, 280, 300, 320, 344, 368, 396, 424, 456, 492, 532, 572, 616, 664, 716, 772, 832, 896, 960, 1024 };
82 
83 const std::uint16_t swbOffset128_16[] = { 0, 4, 8, 12, 16, 20, 24, 28, 32, 40, 48, 60, 72, 88, 108, 128 };
84 
85 const std::uint16_t swbOffset1024_8[] = { 0, 12, 24, 36, 48, 60, 72, 84, 96, 108, 120, 132, 144, 156, 172, 188, 204, 220, 236, 252, 268, 288, 308,
86  328, 348, 372, 396, 420, 448, 476, 508, 544, 580, 620, 664, 712, 764, 820, 880, 944, 1024 };
87 
88 const std::uint16_t swbOffset128_8[] = { 0, 4, 8, 12, 16, 20, 24, 28, 36, 44, 52, 60, 72, 88, 108, 128 };
89 
90 static const std::uint16_t *const swbOffset1024Window[] = {
91  swbOffset1024_96, /* 96000 */
92  swbOffset1024_96, /* 88200 */
93  swbOffset1024_64, /* 64000 */
94  swbOffset1024_48, /* 48000 */
95  swbOffset1024_48, /* 44100 */
96  swbOffset1024_32, /* 32000 */
97  swbOffset1024_24, /* 24000 */
98  swbOffset1024_24, /* 22050 */
99  swbOffset1024_16, /* 16000 */
100  swbOffset1024_16, /* 12000 */
101  swbOffset1024_16, /* 11025 */
102  swbOffset1024_8, /* 8000 */
103 };
104 
105 static const std::uint16_t *const swbOffset512Window[] = {
106  nullptr, /* 96000 */
107  nullptr, /* 88200 */
108  nullptr, /* 64000 */
109  swbOffset512_48, /* 48000 */
110  swbOffset512_48, /* 44100 */
111  swbOffset512_32, /* 32000 */
112  swbOffset512_24, /* 24000 */
113  swbOffset512_24, /* 22050 */
114  nullptr, /* 16000 */
115  nullptr, /* 12000 */
116  nullptr, /* 11025 */
117  nullptr, /* 8000 */
118 };
119 
120 static const std::uint16_t *const swbOffset480Window[] = {
121  nullptr, /* 96000 */
122  nullptr, /* 88200 */
123  nullptr, /* 64000 */
124  swbOffset480_48, /* 48000 */
125  swbOffset480_48, /* 44100 */
126  swbOffset480_32, /* 32000 */
127  swbOffset480_24, /* 24000 */
128  swbOffset480_24, /* 22050 */
129  nullptr, /* 16000 */
130  nullptr, /* 12000 */
131  nullptr, /* 11025 */
132  nullptr, /* 8000 */
133 };
134 
135 static const std::uint16_t *const swbOffset128Window[] = {
136  swbOffset128_96, /* 96000 */
137  swbOffset128_96, /* 88200 */
138  swbOffset128_64, /* 64000 */
139  swbOffset128_48, /* 48000 */
140  swbOffset128_48, /* 44100 */
141  swbOffset128_48, /* 32000 */
142  swbOffset128_24, /* 24000 */
143  swbOffset128_24, /* 22050 */
144  swbOffset128_16, /* 16000 */
145  swbOffset128_16, /* 12000 */
146  swbOffset128_16, /* 11025 */
147  swbOffset128_8 /* 8000 */
148 };
149 
153 AacLtpInfo::AacLtpInfo()
154  : lastBand(0)
155  , dataPresent(0)
156  , lag(0)
157  , lagUpdate(0)
158  , coef(0)
159  , longUsed{ 0 }
160  , shortUsed{ 0 }
161  , shortLagPresent{ 0 }
162  , shortLag{ 0 }
163 {
164 }
165 
169 AacPredictorInfo::AacPredictorInfo()
170  : maxSfb(0)
171  , reset(0)
172  , resetGroupNumber(0)
173  , predictionUsed{ 0 }
174 {
175 }
176 
180 AacPulseInfo::AacPulseInfo()
181  : count(0)
182  , startSfb(0)
183  , offset{ 0 }
184  , amp{ 0 }
185 {
186 }
187 
191 AacTnsInfo::AacTnsInfo()
192  : filt{ 0 }
193  , coefRes{ 0 }
194  , length{ { 0 } }
195  , order{ { 0 } }
196  , direction{ { 0 } }
197  , coefCompress{ { 0 } }
198  , coef{ { { 0 } } }
199 {
200 }
201 
205 AacSsrInfo::AacSsrInfo()
206  : maxBand(0)
207  , adjustNum{ { 0 } }
208  , alevcode{ { { 0 } } }
209  , aloccode{ { { 0 } } }
210 {
211 }
212 
216 AacDrcInfo::AacDrcInfo()
217  : present(0)
218  , bandCount(0)
219  , pceInstanceTag(0)
220  , excludedChannelsPresent(0)
221  , bandTop{ 0 }
222  , progRefLevel(0)
223  , dynamicRangeSign{ 0 }
224  , dynamicRangeControl{ 0 }
225  , excludeMask{ 0 }
226  , additionalExcludedChannels{ 0 }
227 {
228 }
229 
233 AacPsInfo::AacPsInfo()
234 {
235 }
236 
240 AacDrmPsInfo::AacDrmPsInfo()
241 {
242 }
243 
247 AacSbrInfo::AacSbrInfo(std::uint8_t sbrElementType, std::uint16_t samplingFrequency, std::uint16_t frameLength, bool isDrm)
248  : aacElementId(sbrElementType)
249  , samplingFrequency(samplingFrequency)
250  ,
251 
252  maxAacLine(0)
253  ,
254 
255  rate(0)
256  , justSeeked(0)
257  , ret(0)
258  ,
259 
260  ampRes{ 0 }
261  ,
262 
263  k0(0)
264  , kx(0)
265  , m(0)
266  , nMaster(0)
267  , nHigh(0)
268  , nLow(0)
269  , nq(0)
270  , nl{ 0 }
271  , n{ 0 }
272  ,
273 
274  fMaster{ 0 }
275  , fTableRes{ { 0 } }
276  , fTableNoise{ 0 }
277  , fTableLim{ { 0 } }
278  , fGroup{ { 0 } }
279  , ng{ 0 }
280  ,
281 
282  tableMapKToG{ 0 }
283  ,
284 
285  absBordLead{ 0 }
286  , absBordTrail{ 0 }
287  , relLeadCount{ 0 }
288  , relTrailCount{ 0 }
289  ,
290 
291  le{ 0 }
292  , lePrev{ 0 }
293  , lq{ 0 }
294  ,
295 
296  te{ { 0 } }
297  , tq{ { 0 } }
298  , f{ { 0 } }
299  , fPrev{ 0 }
300  ,
301 
302  //*G_temp_prev{{0}},
303  //*Q_temp_prev{{0}},
304  //GQ_ringbuf_index{0},
305 
306  e{ { { 0 } } }
307  , ePrev{ { 0 } }
308  ,
309  //E_orig{{{0}}},
310  //E_curr{{{0}}},
311  q{ { { 0 } } }
312  ,
313  //Q_div{{{0}}},
314  //Q_div2{{{0}}},
315  qPrev{ { 0 } }
316  ,
317 
318  la{ 0 }
319  , laPrev{ 0 }
320  ,
321 
322  bsInvfMode{ { 0 } }
323  , bsInvfModePrev{ { 0 } }
324  ,
325  //bwArray{{0}},
326  //bwArray_prev{{0}},
327 
328  noPatches(0)
329  , patchNoSubbands{ 0 }
330  , patchStartSubband{ 0 }
331  ,
332 
333  bsAddHarmonic{ { 0 } }
334  , bsAddHarmonicPrev{ { 0 } }
335  ,
336 
337  indexNoisePrev{ 0 }
338  , psiIsPrev{ 0 }
339  ,
340 
341  bsStartFreqPrev(numeric_limits<std::uint8_t>::max())
342  , bsStopFreqPrev(0)
343  , bsXoverBandPrev(0)
344  , bsFreqScalePrev(0)
345  , bsAlterScalePrev(0)
346  , bsNoiseBandsPrev(0)
347  ,
348 
349  prevEnvIsShort{ -1, -1 }
350  ,
351 
352  kxPrev(0)
353  , bsco(0)
354  , bscoPrev(0)
355  , mPrev(0)
356  , frameLength(frameLength)
357  ,
358 
359  reset(1)
360  , frame(0)
361  , headerCount(0)
362  ,
363 
364  idAac(0)
365  ,
366  //qmfa_info *qmfa{0},
367  //qmfs_info *qmfs{0},
368 
369  //qmf_t Xsbr{{{0}}},
370 
371  isDrmSbr(isDrm)
372  ,
373  //drm_ps_info *drm_ps;
374 
375  timeSlotsRateCount(aacSbrRate * (frameLength == 960 ? aacNoTimeSlots960 : aacNoTimeSlots))
376  , timeSlotsCount(frameLength == 960 ? aacNoTimeSlots960 : aacNoTimeSlots)
377  , tHfGen(8)
378  , tHfAdj(2)
379  ,
380 
381  psUsed(0)
382  , psResetFlag(0)
383  ,
384 
385  bsHeaderFlag(0)
386  , bsCrcFlag(0)
387  , bsSbrCrcBits(0)
388  , bsProtocolVersion(0)
389  , bsAmpRes(1)
390  , bsStartFreq(5)
391  , bsStopFreq(0)
392  , bsXoverBand(0)
393  , bsFreqScale(2)
394  , bsAlterScale(1)
395  , bsNoiseBands(2)
396  , bsLimiterBands(2)
397  , bsLimiterGains(2)
398  , bsInterpolFreq(1)
399  , bsSmoothingMode(1)
400  , bsSamplerateMode(1)
401  , bsAddHarmonicFlag{ 0 }
402  , bsAddHarmonicFlagPrev{ 0 }
403  , bsExtendedData(0)
404  , bsExtensionId(0)
405  , bsExtensionData(0)
406  , bsCoupling(0)
407  , bsFrameClass{ 0 }
408  , bsRelBord{ { 0 } }
409  , bsRelBord0{ { 0 } }
410  , bsRelBord1{ { 0 } }
411  , bsPointer{ 0 }
412  , bsAbsBord0{ 0 }
413  , bsAbsBord1{ 0 }
414  , bsRelCount0{ 0 }
415  , bsRelCount1{ 0 }
416  , bsDfEnv{ { 0 } }
417  , bsDfNoise{ { 0 } }
418 {
419  throw NotImplementedException(); // TODO
420  /*
421  switch (sbrElementType) {
422  using namespace AacSyntaxElementTypes;
423  case ChannelPairElement:
424 
425  break;
426  default:;
427  }
428  */
429 }
430 
434 AacProgramConfig::AacProgramConfig()
435  : elementInstanceTag(0)
436  , objectType(0)
437  , samplingFrequencyIndex(0)
438  , frontChannelElementCount(0)
439  , sideChannelElementCount(0)
440  , backChannelElementCount(0)
441  , lfeChannelElementCount(0)
442  , assocDataElementCount(0)
443  , validCcElementCount(0)
444  , monoMixdownPresent(0)
445  , monoMixdownElementNumber(0)
446  , stereoMixdownPresent(0)
447  , stereoMixdownElementNumber(0)
448  , matrixMixdownIdxPresent(0)
449  , pseudoSurroundEnable(0)
450  , matrixMixdownIdx(0)
451  , frontElementIsCpe{ 0 }
452  , frontElementTagSelect{ 0 }
453  , sideElementIsCpe{ 0 }
454  , sideElementTagSelect{ 0 }
455  , backElementIsCpe{ 0 }
456  , backElementTagSelect{ 0 }
457  , lfeElementTagSelect{ 0 }
458  , assocDataElementTagSelect{ 0 }
459  , ccElementIsIndSw{ 0 }
460  , validCcElementTagSelect{ 0 }
461  , channels(0)
462  , commentFieldBytes(0)
463  , commentFieldData{ 0 }
464  , frontChannelCount(0)
465  , sideChannelCount(0)
466  , backChannelCount(0)
467  , lfeChannelCount(0)
468  , sceChannel{ 0 }
469  , cpeChannel{ 0 }
470 {
471 }
472 
476 AacIcsInfo::AacIcsInfo()
477  : maxSfb(0)
478  , swbCount(0)
479  , windowGroupCount(0)
480  , windowCount(0)
481  , windowSequence(0)
482  , windowGroupLengths{ 0 }
483  , windowShape(0)
484  , scaleFactorGrouping(0)
485  , sectionSfbOffset{ { 0 } }
486  , swbOffset{ 0 }
487  , maxSwbOffset(0)
488  , sectionCb{ { 0 } }
489  , sectionStart{ { 0 } }
490  , sectionEnd{ { 0 } }
491  , sfbCb{ { 0 } }
492  , sectionsPerGroup{ 0 }
493  , globalGain(0)
494  , scaleFactors{ { 0 } }
495  , midSideCodingMaskPresent(0)
496  , midSideCodingUsed{ { 0 } }
497  , noiseUsed(0)
498  , isUsed(0)
499  , pulseDataPresent(0)
500  , tnsDataPresent(0)
501  , gainControlPresent(0)
502  , predictorDataPresent(0)
503  , reorderedSpectralDataLength(0)
504  , longestCodewordLength(0)
505  , sfConcealment(0)
506  , revGlobalGain(0)
507  , rvlcSfLength(0)
508  , dpcmNoiseNrg(0)
509  , sfEscapesPresent(0)
510  , rvlcEscapesLength(0)
511  , dpcmNoiseLastPos(0)
512 {
513 }
514 
526 void AacFrameElementParser::parseLtpInfo(const AacIcsInfo &ics, AacLtpInfo &ltp)
527 {
528  ltp.lag = 0;
529  if (m_mpeg4AudioObjectId == Mpeg4AudioObjectIds::ErAacLd) {
530  if ((ltp.lagUpdate = m_reader.readBit())) {
531  ltp.lag = m_reader.readBits<std::uint16_t>(10);
532  }
533  } else {
534  ltp.lag = m_reader.readBits<std::uint16_t>(11);
535  }
536  if (ltp.lag > (m_frameLength << 1)) {
537  throw InvalidDataException();
538  }
539  ltp.coef = m_reader.readBits<std::uint8_t>(3);
540  switch (ics.windowSequence) {
541  case AacIcsSequenceTypes::EightShortSequence:
542  for (std::uint8_t w = 0; w < ics.windowCount; ++w) {
543  if ((ltp.shortUsed[w] = m_reader.readBit())) {
544  if ((ltp.shortLagPresent[w] = m_reader.readBit())) {
545  ltp.shortLag[w] = m_reader.readBits<std::uint8_t>(4);
546  }
547  }
548  }
549  break;
550  default:
551  ltp.lastBand = std::min<std::uint8_t>(ics.maxSfb, aacMaxLtpSfb);
552  for (std::uint8_t sfb = 0; sfb < ltp.lastBand; ++sfb) {
553  ltp.longUsed[sfb] = m_reader.readBit();
554  }
555  }
556 }
557 
561 void AacFrameElementParser::parseIcsInfo(AacIcsInfo &ics)
562 {
563  using namespace AacIcsSequenceTypes;
564  if (m_reader.readBit()) { // isc reserved bit (must be 0)
565  throw InvalidDataException();
566  }
567  ics.windowSequence = m_reader.readBits<std::uint8_t>(2);
568  ics.windowShape = m_reader.readBit();
569  if (m_mpeg4AudioObjectId == Mpeg4AudioObjectIds::ErAacLd && ics.windowSequence != OnlyLongSequence) {
570  throw InvalidDataException(); // no block switching in LD
571  }
572  if (ics.windowSequence == EightShortSequence) {
573  ics.maxSfb = m_reader.readBits<std::uint8_t>(4);
574  ics.scaleFactorGrouping = m_reader.readBits<std::uint8_t>(7);
575  } else {
576  ics.maxSfb = m_reader.readBits<std::uint8_t>(6);
577  }
578  calculateWindowGroupingInfo(ics);
579  if (ics.windowSequence != EightShortSequence) {
580  if ((ics.predictorDataPresent = m_reader.readBit())) {
581  switch (m_mpeg4AudioObjectId) {
583  // MPEG-2 style AAC predictor
584  if ((ics.predictor.reset = m_reader.readBit())) {
585  ics.predictor.resetGroupNumber = m_reader.readBits<std::uint8_t>(5);
586  ics.predictor.maxSfb = ics.maxSfb;
587  }
588  if (ics.predictor.maxSfb > maxPredictionSfb[m_mpeg4SamplingFrequencyIndex]) {
589  ics.predictor.maxSfb = maxPredictionSfb[m_mpeg4SamplingFrequencyIndex];
590  }
591  for (std::uint8_t sfb = 0; sfb < ics.predictor.maxSfb; ++sfb) {
592  ics.predictor.predictionUsed[sfb] = m_reader.readBit();
593  }
594  break;
595  default:
596  // "Long Term Prediction"
597  if (m_mpeg4AudioObjectId < Mpeg4AudioObjectIds::ErAacLc) {
598  if ((ics.ltp1.dataPresent = m_reader.readBit())) {
599  parseLtpInfo(ics, ics.ltp1);
600  }
601  if (m_commonWindow) {
602  if ((ics.ltp2.dataPresent = m_reader.readBit())) {
603  parseLtpInfo(ics, ics.ltp2);
604  }
605  }
606  }
607  if (!m_commonWindow && (m_mpeg4AudioObjectId >= Mpeg4AudioObjectIds::ErAacLc)) {
608  if ((ics.ltp1.dataPresent = m_reader.readBit())) {
609  parseLtpInfo(ics, ics.ltp1);
610  }
611  }
612  }
613  }
614  }
615 }
616 
620 void AacFrameElementParser::parseSectionData(AacIcsInfo &ics)
621 {
622  const std::uint8_t sectionBits = ics.windowSequence == AacIcsSequenceTypes::EightShortSequence ? 3 : 5;
623  const std::uint8_t sectionEscValue = static_cast<std::uint8_t>((1 << sectionBits) - 1);
624  for (std::uint8_t groupIndex = 0, sectionIndex = 0; groupIndex < ics.windowGroupCount; ++groupIndex, sectionIndex = 0) {
625  for (std::uint8_t i = 0, sectionLength, sectionLengthIncrease; i < ics.maxSfb; i += sectionLength, ++sectionIndex) {
626  ics.sectionCb[groupIndex][sectionIndex] = m_reader.readBits<std::uint8_t>(m_aacSectionDataResilienceFlag ? 5 : 4);
627  sectionLength = 0;
628  sectionLengthIncrease
629  = (!m_aacSectionDataResilienceFlag && (ics.sectionCb[groupIndex][sectionIndex] < 16 || ics.sectionCb[groupIndex][sectionIndex] > 32)
630  && ics.sectionCb[groupIndex][sectionIndex] != 11)
631  ? m_reader.readBits<std::uint8_t>(sectionBits)
632  : 1;
633  while (sectionLengthIncrease == sectionEscValue) {
634  sectionLength += sectionLengthIncrease;
635  sectionLengthIncrease = m_reader.readBits<std::uint8_t>(sectionBits);
636  }
637  sectionLength += sectionLengthIncrease;
638  ics.sectionStart[groupIndex][sectionIndex] = i;
639  ics.sectionEnd[groupIndex][sectionIndex] = i + sectionLength;
640  if (ics.windowSequence == AacIcsSequenceTypes::EightShortSequence) {
641  if (i + sectionLength > 8 * 15) {
642  throw InvalidDataException();
643  } else if (sectionIndex >= 8 * 15) {
644  throw InvalidDataException();
645  }
646  } else {
647  if (i + sectionLength > aacMaxSfb) {
648  throw InvalidDataException();
649  } else if (sectionIndex >= aacMaxSfb) {
650  throw InvalidDataException();
651  }
652  }
653  for (std::uint8_t sfb = i; sfb < i + sectionLength; ++sfb) {
654  ics.sfbCb[groupIndex][sfb] = ics.sectionCb[groupIndex][sectionIndex];
655  }
656  }
657  ics.sectionsPerGroup[groupIndex] = sectionIndex;
658  }
659 }
660 
664 void AacFrameElementParser::decodeScaleFactorData(AacIcsInfo &ics)
665 {
666  std::int16_t tmp;
667  std::uint8_t noisePcmFlag = 1;
668  std::int16_t scaleFactor = ics.globalGain;
669  std::int16_t isPosition = 0;
670  std::int16_t noiseEnergy = ics.globalGain - 90;
671  using namespace AacScaleFactorTypes;
672  for (std::uint8_t group = 0; group < ics.windowGroupCount; ++group) {
673  for (std::uint8_t sfb = 0; sfb < ics.maxSfb; ++sfb) {
674  switch (ics.sfbCb[group][sfb]) {
675  case ZeroHcb: // zero book
676  ics.scaleFactors[group][sfb] = 0;
677  break;
678  case IntensityHcb: // intensity books
679  case IntensityHcb2:
680  ics.scaleFactors[group][sfb] = static_cast<std::uint16_t>(isPosition += (parseHuffmanScaleFactor() - 60));
681  break;
682  case NoiseHcb: // noise books
683  if (noisePcmFlag) {
684  noisePcmFlag = 0;
685  tmp = m_reader.readBits<std::int16_t>(9);
686  } else {
687  tmp = parseHuffmanScaleFactor() - 60;
688  }
689  ics.scaleFactors[group][sfb] = static_cast<std::uint16_t>(noiseEnergy += tmp);
690  break;
691  default: // spectral books
692  scaleFactor += parseHuffmanScaleFactor() - 60;
693  if (scaleFactor < 0 || scaleFactor > 255) {
694  throw InvalidDataException();
695  } else {
696  ics.scaleFactors[group][sfb] = 0;
697  }
698  }
699  }
700  }
701 }
702 
706 void AacFrameElementParser::decodeRvlcScaleFactorData(AacIcsInfo &ics)
707 {
708  if (ics.rvlcSfLength) {
709  m_reader.skipBits(ics.rvlcSfLength);
710  }
711  if (ics.sfEscapesPresent) {
712  m_reader.skipBits(ics.rvlcEscapesLength);
713  }
714  // TODO: decode RVLC scale factors and escapes
715 }
716 
720 void AacFrameElementParser::parseScaleFactorData(AacIcsInfo &ics)
721 {
722  if (!m_aacScalefactorDataResilienceFlag) {
723  decodeScaleFactorData(ics);
724  } else {
725  decodeRvlcScaleFactorData(ics);
726  }
727 }
728 
732 void AacFrameElementParser::parsePulseData(AacIcsInfo &ics)
733 {
734  AacPulseInfo &p = ics.pulse;
735  p.count = m_reader.readBits<std::uint8_t>(2);
736  p.startSfb = m_reader.readBits<std::uint8_t>(6);
737  if (p.startSfb > ics.swbCount) {
738  throw InvalidDataException();
739  }
740  for (std::uint8_t i = 0; i <= p.count; ++i) {
741  p.offset[i] = m_reader.readBits<std::uint8_t>(5);
742  p.amp[i] = m_reader.readBits<std::uint8_t>(4);
743  }
744 }
745 
749 void AacFrameElementParser::parseTnsData(AacIcsInfo &ics)
750 {
751  std::uint8_t filtBits, lengthBits, orderBits, startCoefBits, coefBits;
752  if (ics.windowSequence == AacIcsSequenceTypes::EightShortSequence) {
753  filtBits = 1;
754  lengthBits = 4;
755  orderBits = 3;
756  } else {
757  filtBits = 2;
758  lengthBits = 6;
759  orderBits = 5;
760  }
761  for (std::uint8_t window = 0; window < ics.windowCount; ++window) {
762  if ((ics.tns.filt[window] = m_reader.readBits<std::uint8_t>(filtBits))) {
763  startCoefBits = (ics.tns.coefRes[window] = m_reader.readBit()) ? 4 : 3;
764  }
765  for (std::uint8_t filt = 0; filt < ics.tns.filt[window]; ++filt) {
766  ics.tns.length[window][filt] = m_reader.readBits<std::uint8_t>(lengthBits);
767  if ((ics.tns.order[window][filt] = m_reader.readBits<std::uint8_t>(orderBits))) {
768  ics.tns.direction[window][filt] = m_reader.readBit();
769  ics.tns.coefCompress[window][filt] = m_reader.readBit();
770  coefBits = startCoefBits - ics.tns.coefCompress[window][filt];
771  for (std::uint8_t i = 0; i < ics.tns.order[window][filt]; ++i) {
772  ics.tns.coef[window][filt][i] = m_reader.readBits<std::uint8_t>(coefBits);
773  }
774  }
775  }
776  }
777 }
778 
782 void AacFrameElementParser::parseGainControlData(AacIcsInfo &ics)
783 {
784  AacSsrInfo &ssr = ics.ssr;
785  ssr.maxBand = m_reader.readBits<std::uint8_t>(2);
786  switch (ics.windowSequence) {
787  using namespace AacIcsSequenceTypes;
788  case OnlyLongSequence:
789  for (std::uint8_t bd = 1; bd <= ssr.maxBand; ++bd) {
790  for (std::uint8_t wd = 0; wd < 1; ++wd) {
791  ssr.adjustNum[bd][wd] = m_reader.readBits<std::uint8_t>(3);
792  for (std::uint8_t ad = 0; ad < ssr.adjustNum[bd][wd]; ++ad) {
793  ssr.alevcode[bd][wd][ad] = m_reader.readBits<std::uint8_t>(4);
794  ssr.aloccode[bd][wd][ad] = m_reader.readBits<std::uint8_t>(5);
795  }
796  }
797  }
798  break;
799  case LongStartSequence:
800  for (std::uint8_t bd = 1; bd <= ssr.maxBand; ++bd) {
801  for (std::uint8_t wd = 0; wd < 2; ++wd) {
802  ssr.adjustNum[bd][wd] = m_reader.readBits<std::uint8_t>(3);
803  for (std::uint8_t ad = 0; ad < ssr.adjustNum[bd][wd]; ++ad) {
804  ssr.alevcode[bd][wd][ad] = m_reader.readBits<std::uint8_t>(4);
805  ssr.aloccode[bd][wd][ad] = m_reader.readBits<std::uint8_t>(wd ? 2 : 4);
806  }
807  }
808  }
809  break;
810  case EightShortSequence:
811  for (std::uint8_t bd = 1; bd <= ssr.maxBand; ++bd) {
812  for (std::uint8_t wd = 0; wd < 8; ++wd) {
813  ssr.adjustNum[bd][wd] = m_reader.readBits<std::uint8_t>(3);
814  for (std::uint8_t ad = 0; ad < ssr.adjustNum[bd][wd]; ++ad) {
815  ssr.alevcode[bd][wd][ad] = m_reader.readBits<std::uint8_t>(4);
816  ssr.aloccode[bd][wd][ad] = m_reader.readBits<std::uint8_t>(2);
817  }
818  }
819  }
820  break;
821  case LongStopSequence:
822  for (std::uint8_t bd = 1; bd <= ssr.maxBand; ++bd) {
823  for (std::uint8_t wd = 0; wd < 2; ++wd) {
824  ssr.adjustNum[bd][wd] = m_reader.readBits<std::uint8_t>(3);
825  for (std::uint8_t ad = 0; ad < ssr.adjustNum[bd][wd]; ++ad) {
826  ssr.alevcode[bd][wd][ad] = m_reader.readBits<std::uint8_t>(4);
827  ssr.aloccode[bd][wd][ad] = m_reader.readBits<std::uint8_t>(wd ? 5 : 4);
828  }
829  }
830  }
831  break;
832  }
833 }
834 
838 void AacFrameElementParser::parseSpectralData(AacIcsInfo &ics, std::int16_t *specData)
839 {
840  //byte groups = 0;
841  //uint16 nshort = m_frameLength / 8;
842  for (std::uint8_t group = 0; group < ics.windowGroupCount; ++group) {
843  //byte p = groups * nshort;
844  for (std::uint8_t section = 0; section < ics.sectionsPerGroup[group]; ++section) {
845  using namespace AacScaleFactorTypes;
846  std::uint8_t sectionCb = ics.sectionCb[group][section];
847  std::uint16_t increment = (sectionCb >= FirstPairHcb) ? 2 : 4;
848  switch (sectionCb) {
849  case ZeroHcb:
850  case NoiseHcb:
851  case IntensityHcb:
852  case IntensityHcb2:
853  //p += (ics.sectionSfbOffset[group][ics.sectionEnd[group][section]] - ics.sectionSfbOffset[group][ics.sectionStart[group][section]]);
854  break;
855  default:
856  for (std::uint16_t k = ics.sectionSfbOffset[group][ics.sectionStart[group][section]];
857  k < ics.sectionSfbOffset[group][ics.sectionEnd[group][section]]; k += increment) {
858  parseHuffmanSpectralData(sectionCb, specData);
859  //p += increment;
860  }
861  }
862  }
863  //groups += ics.windowGroupLengths[group];
864  }
865 }
866 
870 void AacFrameElementParser::parseSideInfo(AacIcsInfo &ics, bool scaleFlag)
871 {
872  ics.globalGain = m_reader.readBits<std::uint8_t>(8);
873  if (!m_commonWindow && !scaleFlag) {
874  parseIcsInfo(ics);
875  }
876  parseSectionData(ics);
877  parseScaleFactorData(ics);
878  if (!scaleFlag) {
879  if ((ics.pulseDataPresent = m_reader.readBit())) {
880  parsePulseData(ics);
881  }
882  if ((ics.tnsDataPresent = m_reader.readBit())) {
883  parseTnsData(ics);
884  }
885  if ((ics.gainControlPresent = m_reader.readBit())) {
886  if (m_mpeg4AudioObjectId != Mpeg4AudioObjectIds::AacSsr) {
887  throw InvalidDataException();
888  } else {
889  parseGainControlData(ics);
890  }
891  }
892  }
893  if (m_aacScalefactorDataResilienceFlag) {
894  decodeRvlcScaleFactorData(ics);
895  }
896 }
897 
898 std::uint8_t AacFrameElementParser::parseExcludedChannels()
899 {
900  for (std::uint8_t i = 0; i < 7; ++i) {
901  m_drc.excludeMask[i] = m_reader.readBit();
902  }
903  std::uint8_t size = 0;
904  for (; (m_drc.additionalExcludedChannels[size] = m_reader.readBit()); ++size) {
905  for (std::uint8_t i = 0; i < 7; ++i) {
906  m_drc.excludeMask[i] = m_reader.readBit();
907  }
908  }
909  return size + 1;
910 }
911 
912 std::uint8_t AacFrameElementParser::parseDynamicRange()
913 {
914  std::uint8_t size = 1;
915  m_drc.bandCount = 1;
916  if (m_reader.readBit()) { // excluded channels present
917  m_drc.pceInstanceTag = m_reader.readBits<std::uint8_t>(4);
918  m_reader.skipBits(4); // skip reserved bits
919  ++size;
920  }
921  if ((m_drc.excludedChannelsPresent = m_reader.readBit())) {
922  size += parseExcludedChannels();
923  }
924  if (m_reader.readBit()) { // has bands data
925  m_drc.bandCount += m_reader.readBits<std::uint8_t>(4);
926  m_reader.skipBits(4); // skip reserved bits
927  ++size;
928  for (std::uint8_t i = 0; i < m_drc.bandCount; ++i, ++size) {
929  m_drc.bandTop[i] = m_reader.readBits<std::uint8_t>(8);
930  }
931  }
932  if (m_reader.readBit()) { // has prog ref level
933  m_drc.progRefLevel = m_reader.readBits<std::uint8_t>(7);
934  m_reader.skipBits(1); // skip reserved bit
935  ++size;
936  }
937  for (std::uint8_t i = 0; i < m_drc.bandCount; ++i) {
938  m_drc.dynamicRangeSign[i] = m_reader.readBit();
939  m_drc.dynamicRangeControl[i] = m_reader.readBits<std::uint8_t>(7);
940  ++size;
941  }
942  return size;
943 }
944 
945 std::int16_t AacFrameElementParser::sbrHuffmanDec(SbrHuffTab table)
946 {
947  std::uint8_t bit;
948  std::int16_t index = 0;
949  while (index >= 0) {
950  bit = m_reader.readBit();
951  index = table[index][bit];
952  }
953  return index + 64;
954 }
955 
956 void AacFrameElementParser::parseSbrGrid(std::shared_ptr<AacSbrInfo> &sbr, std::uint8_t channel)
957 {
958  std::uint8_t tmp, bsEnvCount;
959  //byte bsRelCount0, bsRelCount1;
960  switch ((sbr->bsFrameClass[channel] = m_reader.readBits<std::uint8_t>(2))) {
961  using namespace BsFrameClasses;
962  case FixFix:
963  tmp = m_reader.readBits<std::uint8_t>(2);
964  sbr->absBordLead[channel] = 0;
965  sbr->absBordTrail[channel] = sbr->timeSlotsCount;
966  sbr->relLeadCount[channel] = (bsEnvCount = min<std::uint8_t>(static_cast<std::uint8_t>(1 << tmp), 5)) - 1;
967  sbr->relTrailCount[channel] = 0;
968  tmp = m_reader.readBit();
969  for (std::uint8_t env = 0; env < bsEnvCount; ++env) {
970  sbr->f[channel][env] = tmp;
971  }
972  break;
973  case FixVar:
974  sbr->absBordLead[channel] = 0;
975  sbr->absBordTrail[channel] = m_reader.readBits<std::uint8_t>(2) + sbr->timeSlotsCount;
976  sbr->relLeadCount[channel] = 0;
977  sbr->relTrailCount[channel] = bsEnvCount = m_reader.readBits<std::uint8_t>(2);
978  for (std::uint8_t rel = 0; rel < bsEnvCount; ++rel) {
979  sbr->bsRelBord[channel][rel] = 2 * m_reader.readBits<std::uint8_t>(2) + 2;
980  }
981  sbr->bsPointer[channel] = m_reader.readBits<std::uint8_t>(static_cast<std::uint8_t>(sbrLog2(static_cast<std::int8_t>(bsEnvCount + 2))));
982  for (std::uint8_t env = 0; env <= bsEnvCount; ++env) {
983  sbr->f[channel][bsEnvCount - env] = m_reader.readBit();
984  }
985  break;
986  case VarFix:
987  sbr->absBordLead[channel] = m_reader.readBits<std::uint8_t>(2);
988  sbr->absBordTrail[channel] = sbr->timeSlotsCount;
989  sbr->relLeadCount[channel] = bsEnvCount = m_reader.readBits<std::uint8_t>(2);
990  sbr->relTrailCount[channel] = 0;
991  for (std::uint8_t rel = 0; rel < bsEnvCount; ++rel) {
992  sbr->bsRelBord[channel][rel] = 2 * m_reader.readBits<std::uint8_t>(2) + 2;
993  }
994  sbr->bsPointer[channel] = m_reader.readBits<std::uint8_t>(static_cast<std::uint8_t>(sbrLog2(static_cast<std::int8_t>(bsEnvCount + 2))));
995  for (std::uint8_t env = 0; env < bsEnvCount; ++env) {
996  sbr->f[channel][env] = m_reader.readBit();
997  }
998  break;
999  case VarVar:
1000  sbr->absBordLead[channel] = m_reader.readBits<std::uint8_t>(2);
1001  sbr->absBordTrail[channel] = m_reader.readBits<std::uint8_t>(2) + sbr->timeSlotsCount;
1002  //bsRelCount0 = m_reader.readBits<std::uint8_t>(2);
1003  //bsRelCount1 = m_reader.readBits<std::uint8_t>(2);
1004  m_reader.skipBits(4);
1005  bsEnvCount = min<std::uint8_t>(5, sbr->bsRelCount0[channel] + sbr->bsRelCount1[channel] + 1);
1006  for (std::uint8_t rel = 0; rel < sbr->bsRelCount0[channel]; ++rel) {
1007  sbr->bsRelBord0[channel][rel] = 2 * m_reader.readBits<std::uint8_t>(2) + 2;
1008  }
1009  for (std::uint8_t rel = 0; rel < sbr->bsRelCount1[channel]; ++rel) {
1010  sbr->bsRelBord1[channel][rel] = 2 * m_reader.readBits<std::uint8_t>(2) + 2;
1011  }
1012  sbr->bsPointer[channel] = m_reader.readBits<std::uint8_t>(
1013  static_cast<std::uint8_t>(sbrLog2(static_cast<std::int8_t>(sbr->bsRelCount0[channel] + sbr->bsRelCount1[channel] + 2))));
1014  for (std::uint8_t env = 0; env < bsEnvCount; ++env) {
1015  sbr->f[channel][env] = m_reader.readBit();
1016  }
1017  sbr->relLeadCount[channel] = sbr->bsRelCount0[channel];
1018  sbr->relTrailCount[channel] = sbr->bsRelCount1[channel];
1019  break;
1020  default:;
1021  }
1022  if ((sbr->le[channel] = min<std::uint8_t>(bsEnvCount, sbr->bsFrameClass[channel] == BsFrameClasses::VarVar ? 5 : 4)) <= 0) {
1023  throw InvalidDataException();
1024  }
1025  sbr->lq[channel] = sbr->le[channel] > 1 ? 2 : 1;
1026  // TODO: envelope time border vector, noise floor time border vector
1027 }
1028 
1029 void AacFrameElementParser::parseSbrDtdf(std::shared_ptr<AacSbrInfo> &sbr, std::uint8_t channel)
1030 {
1031  for (std::uint8_t i = 0; i < sbr->le[channel]; ++i) {
1032  sbr->bsDfEnv[channel][i] = m_reader.readBit();
1033  }
1034  for (std::uint8_t i = 0; i < sbr->lq[channel]; ++i) {
1035  sbr->bsDfNoise[channel][i] = m_reader.readBit();
1036  }
1037 }
1038 
1039 void AacFrameElementParser::parseInvfMode(std::shared_ptr<AacSbrInfo> &sbr, std::uint8_t channel)
1040 {
1041  for (std::uint8_t i = 0; i < sbr->nq; ++i) {
1042  sbr->bsInvfMode[channel][i] = m_reader.readBits<std::uint8_t>(2);
1043  }
1044 }
1045 
1046 void AacFrameElementParser::parseSbrEnvelope(std::shared_ptr<AacSbrInfo> &sbr, std::uint8_t channel)
1047 {
1048  std::int8_t delta;
1049  //SbrHuffTab tHuff;
1050  SbrHuffTab fHuff;
1051  if ((sbr->le[channel] == 1) && (sbr->bsFrameClass[channel] == BsFrameClasses::FixFix)) {
1052  sbr->ampRes[channel] = 0;
1053  } else {
1054  sbr->ampRes[channel] = sbr->bsAmpRes;
1055  }
1056  if ((sbr->bsCoupling) && (channel == 1)) {
1057  delta = 1;
1058  if (sbr->ampRes[channel]) {
1059  //tHuff = tHuffmanEnvBal30dB;
1060  fHuff = fHuffmanEnvBal30dB;
1061  } else {
1062  //tHuff = tHuffmanEnvBal15dB;
1063  fHuff = fHuffmanEnvBal15dB;
1064  }
1065  } else {
1066  delta = 0;
1067  if (sbr->ampRes[channel]) {
1068  //tHuff = tHuffmanEnv30dB;
1069  fHuff = fHuffmanEnv30dB;
1070  } else {
1071  //tHuff = tHuffmanEnv15dB;
1072  fHuff = fHuffmanEnv15dB;
1073  }
1074  }
1075  for (std::uint8_t env = 0; env < sbr->le[channel]; ++env) {
1076  if (sbr->bsDfEnv[channel][env] == 0) {
1077  if ((sbr->bsCoupling == 1) && (channel == 1)) {
1078  if (sbr->ampRes[channel]) {
1079  sbr->e[channel][0][env] = static_cast<std::int16_t>(m_reader.readBits<std::uint16_t>(5) << delta);
1080  } else {
1081  sbr->e[channel][0][env] = static_cast<std::int16_t>(m_reader.readBits<std::uint16_t>(6) << delta);
1082  }
1083  } else {
1084  if (sbr->ampRes[channel]) {
1085  sbr->e[channel][0][env] = static_cast<std::int16_t>(m_reader.readBits<std::uint16_t>(6) << delta);
1086  } else {
1087  sbr->e[channel][0][env] = static_cast<std::int16_t>(m_reader.readBits<std::uint16_t>(7) << delta);
1088  }
1089  }
1090  for (std::uint8_t band = 1; band < sbr->n[sbr->f[channel][env]]; ++band) {
1091  sbr->e[channel][band][env] = static_cast<std::int16_t>(sbrHuffmanDec(fHuff) << delta);
1092  }
1093  } else {
1094  for (std::uint8_t band = 0; band < sbr->n[sbr->f[channel][env]]; ++band) {
1095  sbr->e[channel][band][env] = static_cast<std::int16_t>(sbrHuffmanDec(fHuff) << delta);
1096  }
1097  }
1098  }
1099  // TODO: extract envelope data
1100 }
1101 
1102 void AacFrameElementParser::parseSbrNoise(std::shared_ptr<AacSbrInfo> &sbr, std::uint8_t channel)
1103 {
1104  std::int8_t delta;
1105  //SbrHuffTab tHuff;
1106  SbrHuffTab fHuff;
1107  if ((sbr->bsCoupling == 1) && (channel == 1)) {
1108  delta = 1;
1109  //tHuff = tHuffmanNoiseBal30dB;
1110  fHuff = fHuffmanEnvBal30dB;
1111  } else {
1112  delta = 1;
1113  //tHuff = tHuffmanNoise30dB;
1114  fHuff = fHuffmanEnv30dB;
1115  }
1116  for (std::uint8_t noise = 0; noise < sbr->lq[channel]; ++noise) {
1117  if (sbr->bsDfNoise[channel][noise] == 0) {
1118  if ((sbr->bsCoupling == 1) && (channel == 1)) {
1119  sbr->q[channel][0][noise] = m_reader.readBits<std::uint8_t>(5) << delta;
1120  } else {
1121  sbr->q[channel][0][noise] = m_reader.readBits<std::uint8_t>(5) << delta;
1122  }
1123  for (std::uint8_t band = 1; band < sbr->nq; ++band) {
1124  sbr->q[channel][band][noise] = sbrHuffmanDec(fHuff) << delta;
1125  }
1126  } else {
1127  for (std::uint8_t band = 0; band < sbr->nq; ++band) {
1128  sbr->q[channel][band][noise] = sbrHuffmanDec(fHuff) << delta;
1129  }
1130  }
1131  }
1132  // TODO: extract noise floor data
1133 }
1134 
1135 void AacFrameElementParser::parseSbrSinusoidalCoding(std::shared_ptr<AacSbrInfo> &sbr, std::uint8_t channel)
1136 {
1137  for (std::uint8_t i = 0; i < sbr->nHigh; ++i) {
1138  sbr->bsAddHarmonic[channel][i] = m_reader.readBit();
1139  }
1140 }
1141 
1142 std::uint16_t AacFrameElementParser::parseSbrExtension(std::shared_ptr<AacSbrInfo> &sbr, std::uint8_t extensionId, std::uint8_t)
1143 {
1144  std::uint8_t header;
1145  std::uint16_t res;
1146  switch (extensionId) {
1147  using namespace AacSbrExtensionIds;
1148  case Ps:
1149  if (sbr->psResetFlag) {
1150  sbr->ps->headerRead = 0;
1151  }
1152  res = parsePsData(sbr->ps, header);
1153  if (sbr->psUsed == 0 && header == 1) {
1154  sbr->psUsed = 1;
1155  }
1156  if (header == 1) {
1157  sbr->psResetFlag = 0;
1158  }
1159  return res;
1160  case DrmParametricStereo:
1161  sbr->psUsed = 1;
1162  return parseDrmPsData(sbr->drmPs);
1163  default:
1164  sbr->bsExtendedData = m_reader.readBits<std::uint8_t>(6);
1165  return 6;
1166  }
1167 }
1168 
1169 std::uint16_t AacFrameElementParser::parsePsData(std::shared_ptr<AacPsInfo> &ps, std::uint8_t &header)
1170 {
1171  if (m_reader.readBit()) {
1172  header = 1;
1173  ps->headerRead = 1;
1174  ps->use34HybridBands = 0;
1175  if ((ps->enableIID = m_reader.readBit())) {
1176  ps->iidMode = m_reader.readBits<std::uint8_t>(3);
1177  }
1178  }
1179  throw NotImplementedException(); // TODO
1180 }
1181 
1182 std::uint16_t AacFrameElementParser::parseDrmPsData(std::shared_ptr<AacDrmPsInfo> &drmPs)
1183 {
1184  CPP_UTILITIES_UNUSED(drmPs)
1185  throw NotImplementedException(); // TODO
1186 }
1187 
1188 void AacFrameElementParser::parseSbrSingleChannelElement(std::shared_ptr<AacSbrInfo> &sbr)
1189 {
1190  if (m_reader.readBit()) { // bs data extra
1191  m_reader.skipBits(4); // skip bs reserved
1192  }
1193  if (sbr->isDrmSbr) {
1194  m_reader.skipBits(1); // bs coupling
1195  }
1196  parseSbrGrid(sbr, 0);
1197  parseSbrDtdf(sbr, 0);
1198  parseInvfMode(sbr, 0);
1199  parseSbrEnvelope(sbr, 0);
1200  parseSbrNoise(sbr, 0);
1201  // TODO: envelope noise dequantisation
1202  if ((sbr->bsAddHarmonicFlag[0] = m_reader.readBit())) {
1203  parseSbrSinusoidalCoding(sbr, 0);
1204  }
1205  if ((sbr->bsExtendedData = m_reader.readBit())) {
1206  std::uint16_t cnt = m_reader.readBits<std::uint16_t>(4);
1207  if (cnt == 0xF) {
1208  cnt += m_reader.readBits<std::uint16_t>(8);
1209  }
1210  std::uint16_t bitsLeft = 8 * cnt;
1211  while (bitsLeft > 7) {
1212  sbr->bsExtensionId = m_reader.readBits<std::uint8_t>(2);
1213  std::uint16_t tmpBitCount = 2 + parseSbrExtension(sbr, sbr->bsExtensionId, static_cast<std::uint8_t>(bitsLeft));
1214  if (tmpBitCount > bitsLeft) {
1215  throw InvalidDataException();
1216  } else {
1217  bitsLeft -= tmpBitCount;
1218  }
1219  }
1220  if (bitsLeft) { // read remaining bits
1221  m_reader.skipBits(bitsLeft);
1222  }
1223  }
1224 }
1225 
1226 void AacFrameElementParser::parseSbrChannelPairElement(std::shared_ptr<AacSbrInfo> &sbr)
1227 {
1228  if (m_reader.readBit()) { // bs data extra
1229  m_reader.skipBits(8); // skip bs reserved
1230  }
1231  if ((sbr->bsCoupling = m_reader.readBit())) {
1232  parseSbrGrid(sbr, 0);
1233  // copy data from left to right
1234  sbr->bsFrameClass[1] = sbr->bsFrameClass[0];
1235  sbr->le[1] = sbr->le[0];
1236  sbr->lq[1] = sbr->lq[0];
1237  sbr->bsPointer[1] = sbr->bsPointer[0];
1238  for (std::uint8_t n = 0; n < sbr->le[0]; ++n) {
1239  sbr->te[1][n] = sbr->te[0][n];
1240  sbr->f[1][n] = sbr->f[0][n];
1241  }
1242  for (std::uint8_t n = 0; n < sbr->lq[0]; ++n) {
1243  sbr->tq[1][n] = sbr->tq[0][n];
1244  }
1245  parseSbrDtdf(sbr, 0);
1246  parseSbrDtdf(sbr, 1);
1247  parseInvfMode(sbr, 0);
1248  for (std::uint8_t n = 0; n < sbr->nq; ++n) {
1249  sbr->bsInvfMode[1][n] = sbr->bsInvfMode[0][n];
1250  }
1251  parseSbrEnvelope(sbr, 0);
1252  parseSbrNoise(sbr, 0);
1253  parseSbrEnvelope(sbr, 1);
1254  parseSbrNoise(sbr, 1);
1255  } else {
1256  parseSbrGrid(sbr, 0);
1257  parseSbrGrid(sbr, 1);
1258  parseSbrDtdf(sbr, 0);
1259  parseSbrDtdf(sbr, 1);
1260  parseInvfMode(sbr, 0);
1261  parseInvfMode(sbr, 1);
1262  parseSbrEnvelope(sbr, 0);
1263  parseSbrEnvelope(sbr, 1);
1264  parseSbrNoise(sbr, 0);
1265  parseSbrNoise(sbr, 1);
1266  }
1267  if ((sbr->bsAddHarmonicFlag[0] = m_reader.readBit())) {
1268  parseSbrSinusoidalCoding(sbr, 0);
1269  }
1270  if ((sbr->bsAddHarmonicFlag[1] = m_reader.readBit())) {
1271  parseSbrSinusoidalCoding(sbr, 1);
1272  }
1273  // TODO: envelope noise dequantisation (for both channels)
1274  if (sbr->bsCoupling) {
1275  // TODO: unmap envelope noise
1276  }
1277  if ((sbr->bsExtendedData = m_reader.readBit())) {
1278  std::uint16_t cnt = m_reader.readBits<std::uint16_t>(4);
1279  if (cnt == 0xF) {
1280  cnt += m_reader.readBits<std::uint16_t>(8);
1281  }
1282  std::uint16_t bitsLeft = 8 * cnt;
1283  while (bitsLeft > 7) {
1284  sbr->bsExtensionId = m_reader.readBits<std::uint8_t>(2);
1285  std::uint16_t tmpBitCount = 2 + parseSbrExtension(sbr, sbr->bsExtensionId, static_cast<std::uint8_t>(bitsLeft));
1286  if (tmpBitCount > bitsLeft) {
1287  throw InvalidDataException();
1288  } else {
1289  bitsLeft -= tmpBitCount;
1290  }
1291  }
1292  if (bitsLeft) { // read remaining bits
1293  m_reader.skipBits(bitsLeft);
1294  }
1295  }
1296 }
1297 
1298 shared_ptr<AacSbrInfo> AacFrameElementParser::makeSbrInfo(std::uint8_t sbrElement, bool isDrm)
1299 {
1300  if (m_mpeg4ExtensionSamplingFrequencyIndex >= sizeof(mpeg4SamplingFrequencyTable)
1301  && m_mpeg4SamplingFrequencyIndex >= sizeof(mpeg4SamplingFrequencyTable)) {
1302  throw InvalidDataException(); // sampling frequency index is invalid
1303  }
1304  return make_shared<AacSbrInfo>(m_elementId[sbrElement],
1305  m_mpeg4ExtensionSamplingFrequencyIndex < sizeof(mpeg4SamplingFrequencyTable)
1306  ? mpeg4SamplingFrequencyTable[m_mpeg4ExtensionSamplingFrequencyIndex]
1307  : mpeg4SamplingFrequencyTable[m_mpeg4SamplingFrequencyIndex] * 2,
1308  m_frameLength, isDrm);
1309 }
1310 
1311 void AacFrameElementParser::parseSbrExtensionData(std::uint8_t sbrElement, std::uint16_t count, bool crcFlag)
1312 {
1313  CPP_UTILITIES_UNUSED(count);
1314  //uint16 alignBitCount = 0;
1315  std::shared_ptr<AacSbrInfo> &sbr = m_sbrElements[sbrElement];
1316  if (m_psResetFlag) {
1317  sbr->psResetFlag = m_psResetFlag;
1318  }
1319  if (!sbr->isDrmSbr) {
1320  if (crcFlag) {
1321  sbr->bsSbrCrcBits = m_reader.readBits<std::uint16_t>(10);
1322  }
1323  }
1324  //auto startFrequ = sbr->bsStartFreq;
1325  //auto samplerateMode = sbr->bsSamplerateMode;
1326  //auto stopFrequ = sbr->bsStopFreq;
1327  //auto frequScale = sbr->bsFreqScale;
1328  //auto alterScale = sbr->bsAlterScale;
1329  //auto xoverBand = sbr->bsXoverBand;
1330  if ((sbr->bsHeaderFlag = m_reader.readBit())) {
1331  sbr->bsStartFreq = m_reader.readBits<std::uint8_t>(4);
1332  sbr->bsStopFreq = m_reader.readBits<std::uint8_t>(4);
1333  sbr->bsXoverBand = m_reader.readBits<std::uint8_t>(3);
1334  m_reader.skipBits(2);
1335  std::uint8_t bsExtraHeader1 = m_reader.readBit();
1336  std::uint8_t bsExtraHeader2 = m_reader.readBit();
1337  if (bsExtraHeader1) {
1338  sbr->bsFreqScale = m_reader.readBits<std::uint8_t>(2);
1339  sbr->bsAlterScale = m_reader.readBit();
1340  sbr->bsNoiseBands = m_reader.readBits<std::uint8_t>(2);
1341  } else {
1342  sbr->bsFreqScale = 2;
1343  sbr->bsAlterScale = 1;
1344  sbr->bsNoiseBands = 2;
1345  }
1346  if (bsExtraHeader2) {
1347  sbr->bsLimiterBands = m_reader.readBits<std::uint8_t>(2);
1348  sbr->bsLimiterGains = m_reader.readBits<std::uint8_t>(2);
1349  sbr->bsInterpolFreq = m_reader.readBit();
1350  sbr->bsSmoothingMode = m_reader.readBit();
1351  } else {
1352  sbr->bsLimiterBands = 2;
1353  sbr->bsLimiterGains = 2;
1354  sbr->bsInterpolFreq = 1;
1355  sbr->bsSmoothingMode = 1;
1356  }
1357  }
1358  if (sbr->headerCount) {
1359  if (sbr->reset || (sbr->bsHeaderFlag && sbr->justSeeked)) {
1360  // TODO: calc SBR tables; restore old values on error
1361  }
1362  sbr->rate = sbr->bsSamplerateMode ? 2 : 1;
1363  switch (sbr->aacElementId) {
1364  using namespace AacSyntaxElementTypes;
1365  case SingleChannelElement:
1366  parseSbrSingleChannelElement(sbr);
1367  break;
1368  case ChannelPairElement:
1369  parseSbrChannelPairElement(sbr);
1370  break;
1371  }
1372  }
1373 }
1374 
1375 std::uint8_t AacFrameElementParser::parseHuffmanScaleFactor()
1376 {
1377  std::uint16_t offset = 0;
1378  while (aacHcbSf[offset][1]) {
1379  offset += aacHcbSf[offset][m_reader.readBit()];
1380  if (offset > 240) {
1381  throw InvalidDataException();
1382  }
1383  }
1384  return aacHcbSf[offset][0];
1385 }
1386 
1387 void AacFrameElementParser::parseHuffmanSpectralData(std::uint8_t cb, std::int16_t *sp)
1388 {
1389  switch (cb) {
1390  case 1:
1391  case 2: // 2-step method for data quadruples
1392  huffman2StepQuad(cb, sp);
1393  break;
1394  case 3: // binary search for data quadruples
1395  huffmanBinaryQuadSign(cb, sp);
1396  break;
1397  case 4: // binary search for data paris
1398  huffmanBinaryPair(cb, sp);
1399  break;
1400  case 5: // 2-step method for data pairs
1401  huffman2StepPair(cb, sp);
1402  break;
1403  case 6: // 2-step method for data pairs
1404  huffman2StepPair(cb, sp);
1405  break;
1406  case 7:
1407  case 9: // binary search for data pairs
1408  huffmanBinaryPairSign(cb, sp);
1409  break;
1410  case 8:
1411  case 10: // 2-step method for data pairs
1412  huffman2StepPairSign(cb, sp);
1413  break;
1414  case 11:
1415  try {
1416  huffman2StepPairSign(11, sp);
1417  } catch (const InvalidDataException &) {
1418  sp[0] = huffmanGetEscape(sp[0]);
1419  sp[1] = huffmanGetEscape(sp[1]);
1420  throw;
1421  }
1422  sp[0] = huffmanGetEscape(sp[0]);
1423  sp[1] = huffmanGetEscape(sp[1]);
1424  break;
1425  case 12:
1426  try {
1427  huffman2StepPair(11, sp);
1428  } catch (const InvalidDataException &) {
1429  sp[0] = huffmanCodebook(0);
1430  sp[1] = huffmanCodebook(1);
1431  throw;
1432  }
1433  sp[0] = huffmanCodebook(0);
1434  sp[1] = huffmanCodebook(1);
1435  break;
1436  case 16:
1437  case 17:
1438  case 18:
1439  case 19:
1440  case 20:
1441  case 21:
1442  case 22:
1443  case 23:
1444  case 24:
1445  case 25:
1446  case 26:
1447  case 27:
1448  case 28:
1449  case 29:
1450  case 30:
1451  case 31:
1452  try {
1453  huffman2StepPairSign(11, sp);
1454  } catch (const InvalidDataException &) {
1455  sp[0] = huffmanGetEscape(sp[0]);
1456  sp[1] = huffmanGetEscape(sp[1]);
1457  vcb11CheckLav(cb, sp);
1458  throw;
1459  }
1460  sp[0] = huffmanGetEscape(sp[0]);
1461  sp[1] = huffmanGetEscape(sp[1]);
1462  vcb11CheckLav(cb, sp);
1463  break;
1464  default:
1465  throw InvalidDataException(); // non existend codebook number
1466  }
1467 }
1468 
1469 void AacFrameElementParser::huffmanSignBits(std::int16_t *sp, std::uint8_t len)
1470 {
1471  for (std::int16_t *end = sp + len; sp < end; ++sp) {
1472  if (*sp) {
1473  if (m_reader.readBit()) {
1474  *sp = -(*sp);
1475  }
1476  }
1477  }
1478 }
1479 
1480 void AacFrameElementParser::huffman2StepQuad(std::uint8_t cb, std::int16_t *sp)
1481 {
1482  std::uint32_t cw = m_reader.showBits<std::uint32_t>(aacHcbN[cb]);
1483  std::uint16_t offset = aacHcbTable[cb][cw].offset;
1484  uint8_t extraBits = aacHcbTable[cb][cw].extraBits;
1485  if (extraBits) {
1486  m_reader.skipBits(aacHcbN[cb]);
1487  offset += m_reader.showBits<std::uint16_t>(extraBits);
1488  m_reader.skipBits(aacHcb2QuadTable[cb][offset].bits - aacHcbN[cb]);
1489  } else {
1490  m_reader.skipBits(aacHcb2QuadTable[cb][offset].bits);
1491  }
1492  if (offset > aacHcb2QuadTableSize[cb]) {
1493  throw InvalidDataException();
1494  }
1495  sp[0] = aacHcb2QuadTable[cb][offset].x;
1496  sp[1] = aacHcb2QuadTable[cb][offset].y;
1497  sp[2] = aacHcb2QuadTable[cb][offset].v;
1498  sp[3] = aacHcb2QuadTable[cb][offset].w;
1499 }
1500 
1501 void AacFrameElementParser::huffmanBinaryQuadSign(std::uint8_t cb, std::int16_t *sp)
1502 {
1503  try {
1504  huffman2StepQuad(cb, sp);
1505  } catch (const InvalidDataException &) {
1506  huffmanSignBits(sp, 4);
1507  throw;
1508  }
1509  huffmanSignBits(sp, 4);
1510 }
1511 
1512 void AacFrameElementParser::huffmanBinaryPair(std::uint8_t cb, std::int16_t *sp)
1513 {
1514  std::uint16_t offset = 0;
1515  while (!aacHcbBinTable[cb][offset].isLeaf) {
1516  offset += aacHcbBinTable[cb][offset].data[m_reader.readBit()];
1517  }
1518  if (offset > aacHcbBinTableSize[cb]) {
1519  throw InvalidDataException();
1520  }
1521  sp[0] = aacHcbBinTable[cb][offset].data[0];
1522  sp[1] = aacHcbBinTable[cb][offset].data[1];
1523 }
1524 
1525 void AacFrameElementParser::huffman2StepPair(std::uint8_t cb, std::int16_t *sp)
1526 {
1527  std::uint32_t cw = m_reader.showBits<std::uint32_t>(aacHcbN[cb]);
1528  std::uint16_t offset = aacHcbTable[cb][cw].offset;
1529  uint8_t extraBits = aacHcbTable[cb][cw].extraBits;
1530  if (extraBits) {
1531  m_reader.skipBits(aacHcbN[cb]);
1532  offset += m_reader.showBits<std::uint16_t>(extraBits);
1533  m_reader.skipBits(aacHcb2PairTable[cb][offset].bits - aacHcbN[cb]);
1534  } else {
1535  m_reader.skipBits(aacHcb2PairTable[cb][offset].bits);
1536  }
1537  if (offset > aacHcb2PairTableSize[cb]) {
1538  throw InvalidDataException();
1539  }
1540  sp[0] = aacHcb2PairTable[cb][offset].x;
1541  sp[1] = aacHcb2PairTable[cb][offset].y;
1542 }
1543 
1544 void AacFrameElementParser::huffmanBinaryPairSign(std::uint8_t cb, std::int16_t *sp)
1545 {
1546  try {
1547  huffmanBinaryPair(cb, sp);
1548  } catch (const InvalidDataException &) {
1549  huffmanSignBits(sp, 2);
1550  throw;
1551  }
1552  huffmanSignBits(sp, 2);
1553 }
1554 
1555 void AacFrameElementParser::huffman2StepPairSign(std::uint8_t cb, std::int16_t *sp)
1556 {
1557  try {
1558  huffman2StepPair(cb, sp);
1559  } catch (const InvalidDataException &) {
1560  huffmanSignBits(sp, 2);
1561  throw;
1562  }
1563  huffmanSignBits(sp, 2);
1564 }
1565 
1566 std::int16_t AacFrameElementParser::huffmanGetEscape(std::int16_t sp)
1567 {
1568  std::uint8_t neg;
1569  if (sp < 0) {
1570  if (sp != -16)
1571  return sp;
1572  neg = 1;
1573  } else {
1574  if (sp != 16)
1575  return sp;
1576  neg = 0;
1577  }
1578  std::uint8_t size;
1579  for (size = 4; m_reader.readBit(); ++size) {
1580  }
1581  const std::int16_t off = m_reader.readBits<std::int16_t>(size);
1582  return static_cast<std::int16_t>(neg ? -(off | (1 << size)) : (off | (1 << size)));
1583 }
1584 
1585 void AacFrameElementParser::vcb11CheckLav(std::uint8_t cb, std::int16_t *sp)
1586 {
1587  static const uint16_t vcb11LavTab[] = { 16, 31, 47, 63, 95, 127, 159, 191, 223, 255, 319, 383, 511, 767, 1023, 2047 };
1588  std::uint16_t max = 0;
1589  if (cb < 16 || cb > 31)
1590  return;
1591  max = vcb11LavTab[cb - 16];
1592  if ((abs(sp[0]) > max) || (abs(sp[1]) > max)) {
1593  sp[0] = 0;
1594  sp[1] = 0;
1595  }
1596 }
1597 
1601 void AacFrameElementParser::calculateWindowGroupingInfo(AacIcsInfo &ics)
1602 {
1603  using namespace AacIcsSequenceTypes;
1604  switch (ics.windowSequence) {
1605  case OnlyLongSequence:
1606  case LongStartSequence:
1607  case LongStopSequence:
1608  ics.windowCount = ics.windowGroupCount = ics.windowGroupLengths[0] = 1;
1609  if (m_mpeg4AudioObjectId == Mpeg4AudioObjectIds::ErAacLd) {
1610  if (m_frameLength == 512) {
1611  ics.swbCount = swb512WindowCount[m_mpeg4SamplingFrequencyIndex];
1612  } else { // if 480
1613  ics.swbCount = swb480WindowCount[m_mpeg4SamplingFrequencyIndex];
1614  }
1615  } else {
1616  if (m_frameLength == 1024) {
1617  ics.swbCount = swb1024WindowCount[m_mpeg4SamplingFrequencyIndex];
1618  } else {
1619  ics.swbCount = swb960WindowCount[m_mpeg4SamplingFrequencyIndex];
1620  }
1621  }
1622  if (ics.maxSfb > ics.swbCount) {
1623  throw InvalidDataException();
1624  }
1625  if (m_mpeg4AudioObjectId == Mpeg4AudioObjectIds::ErAacLd) {
1626  if (m_frameLength == 512) {
1627  for (std::uint8_t i = 0; i <= ics.swbCount; ++i) {
1628  ics.sectionSfbOffset[0][i] = swbOffset512Window[m_mpeg4SamplingFrequencyIndex][i];
1629  ics.swbOffset[i] = swbOffset512Window[m_mpeg4SamplingFrequencyIndex][i];
1630  }
1631  } else {
1632  for (std::uint8_t i = 0; i <= ics.swbCount; ++i) {
1633  ics.sectionSfbOffset[0][i] = swbOffset480Window[m_mpeg4SamplingFrequencyIndex][i];
1634  ics.swbOffset[i] = swbOffset480Window[m_mpeg4SamplingFrequencyIndex][i];
1635  }
1636  }
1637  } else {
1638  for (std::uint8_t i = 0; i <= ics.swbCount; ++i) {
1639  ics.sectionSfbOffset[0][i] = swbOffset1024Window[m_mpeg4SamplingFrequencyIndex][i];
1640  ics.swbOffset[i] = swbOffset1024Window[m_mpeg4SamplingFrequencyIndex][i];
1641  }
1642  }
1643  ics.sectionSfbOffset[0][ics.swbCount] = ics.swbOffset[ics.swbCount] = ics.maxSwbOffset = m_frameLength;
1644  break;
1645  case EightShortSequence:
1646  ics.windowCount = 8;
1647  ics.windowGroupCount = ics.windowGroupLengths[0] = 1;
1648  ics.swbCount = swb128WindowCount[m_mpeg4SamplingFrequencyIndex];
1649  if (ics.maxSfb > ics.swbCount) {
1650  throw InvalidDataException();
1651  }
1652  for (std::uint8_t i = 0; i < ics.swbCount; ++i) {
1653  ics.swbOffset[i] = swbOffset128Window[m_mpeg4SamplingFrequencyIndex][i];
1654  }
1655  ics.swbOffset[ics.swbCount] = ics.maxSwbOffset = m_frameLength / 8;
1656  for (std::uint8_t i = 0; i < ics.windowCount - 1; ++i) {
1657  if (!(ics.scaleFactorGrouping & (1 << (6 - i)))) {
1658  ics.windowGroupLengths[ics.windowGroupCount] = 1;
1659  ++ics.windowGroupCount;
1660  } else {
1661  ++(ics.windowGroupLengths[ics.windowGroupCount - 1]);
1662  }
1663  }
1664  for (std::uint8_t g = 0; g < ics.windowGroupCount; ++g) {
1665  std::uint8_t sectionSfb = 0;
1666  std::uint16_t offset = 0, width;
1667  for (std::uint8_t i = 0; i < ics.swbCount; ++i) {
1668  if (i + 1 == ics.swbCount) {
1669  width = (m_frameLength / 8) - swbOffset128Window[m_mpeg4SamplingFrequencyIndex][i];
1670  } else {
1671  width = swbOffset128Window[m_mpeg4SamplingFrequencyIndex][i + 1] - swbOffset128Window[m_mpeg4SamplingFrequencyIndex][i];
1672  }
1673  width *= ics.windowGroupLengths[g];
1674  ics.sectionSfbOffset[g][sectionSfb++] = offset;
1675  offset += width;
1676  }
1677  ics.sectionSfbOffset[g][sectionSfb] = offset;
1678  }
1679  break;
1680  default:
1681  throw InvalidDataException();
1682  }
1683 }
1684 
1688 void AacFrameElementParser::parseIndividualChannelStream(AacIcsInfo &ics, std::int16_t *specData, bool scaleFlag)
1689 {
1690  parseSideInfo(ics, scaleFlag);
1691  if (m_mpeg4AudioObjectId >= Mpeg4AudioObjectIds::ErAacLc) {
1692  if (ics.tnsDataPresent) {
1693  parseTnsData(ics);
1694  }
1695  }
1696  if (m_mpeg4AudioObjectId == Mpeg4AudioObjectIds::ErParametric) { // DRM stuff?
1697  // TODO: check CRC
1698  throw NotImplementedException();
1699  }
1700  if (m_aacSpectralDataResilienceFlag) {
1701  // TODO: parseReorderedSpectralData(ic);
1702  throw NotImplementedException();
1703  } else {
1704  parseSpectralData(ics, specData);
1705  }
1706  if (ics.pulseDataPresent) {
1707  if (ics.windowSequence == AacIcsSequenceTypes::EightShortSequence) {
1708  throw InvalidDataException(); // pulse coding not allowed for short blocks
1709  } else {
1710  // TODO: reconstruct pulse coding
1711  //decodePulseData(ic);
1712  }
1713  }
1714 }
1715 
1719 void AacFrameElementParser::parseSingleChannelElement()
1720 {
1721  if (m_elementCount + 1 > aacMaxSyntaxElements) {
1722  throw NotImplementedException(); // can not parse frame with more than aacMaxSyntaxElements syntax elements
1723  }
1724  // TODO: check whether limit of channels is exceeded
1725 
1726  std::int16_t specData[1024] = { 0 };
1727  m_elementId[m_elementCount] = AacSyntaxElementTypes::SingleChannelElement;
1728  m_elementInstanceTag[m_elementCount] = m_reader.readBits<std::uint8_t>(4);
1729  //m_channel = channel;
1730  //m_pairedChannel = -1;
1731  parseIndividualChannelStream(m_ics1, specData);
1732  if (m_ics1.isUsed) {
1733  throw InvalidDataException(); // IS not allowed in single channel
1734  }
1735  // check wheter next bitstream element is a fill element (for SBR decoding)
1736  if (m_reader.showBits<std::uint8_t>(3) == AacSyntaxElementTypes::FillElement) {
1737  parseFillElement(m_elementCount);
1738  }
1739  // TODO: reconstruct single channel element
1740  // TODO: map output channels position to internal data channels
1741  m_channelCount += m_elementChannelCount[m_elementCount];
1742  ++m_elementCount;
1743 }
1744 
1748 void AacFrameElementParser::parseChannelPairElement()
1749 {
1750  if (m_elementCount + 2 > aacMaxSyntaxElements) {
1751  throw NotImplementedException(); // can not parse frame with more than aacMaxSyntaxElements syntax elements
1752  }
1753  // TODO: check whether limit of channels is exceeded
1754  m_elementId[m_elementCount] = AacSyntaxElementTypes::ChannelPairElement;
1755  m_elementChannelCount[m_elementCount] = 2; // number of output channels in CPE is always 2
1756 
1757  std::int16_t specData1[1024] = { 0 };
1758  std::int16_t specData2[1024] = { 0 };
1759  //m_channel = channels;
1760  //m_pairedChannel = channels + 1;
1761  m_elementInstanceTag[m_elementCount] = m_reader.readBits<std::uint8_t>(4);
1762  if ((m_commonWindow = m_reader.readBit())) {
1763  // both channels have common ics data
1764  parseIcsInfo(m_ics1);
1765  if ((m_ics1.midSideCodingMaskPresent = m_reader.readBits<std::uint8_t>(2) == 1)) { // ms mask present
1766  for (std::uint8_t g = 0; g < m_ics1.windowGroupCount; ++g) {
1767  for (std::uint8_t sfb = 0; sfb < m_ics1.maxSfb; ++sfb) {
1768  m_ics1.midSideCodingUsed[g][sfb] = m_reader.readBit();
1769  }
1770  }
1771  //m_reader.skipBits(m_ics1.windowGroupCount * m_ics1.maxSfb);
1772  }
1773  if (m_mpeg4AudioObjectId >= Mpeg4AudioObjectIds::ErAacLc && m_ics1.predictorDataPresent) {
1774  if ((m_ics1.ltp1.dataPresent = m_reader.readBit())) {
1775  parseLtpInfo(m_ics1, m_ics1.ltp1);
1776  }
1777  }
1778  m_ics2 = m_ics1;
1779  } else {
1780  m_ics1.midSideCodingMaskPresent = false;
1781  }
1782  parseIndividualChannelStream(m_ics1, specData1);
1783  if (m_commonWindow && m_mpeg4AudioObjectId >= Mpeg4AudioObjectIds::ErAacLc && m_ics1.predictorDataPresent) {
1784  if ((m_ics1.ltp2.dataPresent = m_reader.readBit())) {
1785  parseLtpInfo(m_ics1, m_ics1.ltp2);
1786  }
1787  }
1788  parseIndividualChannelStream(m_ics2, specData2);
1789  // check if next bitstream element is a fill element (for SBR decoding)
1790  if (m_reader.showBits<std::uint8_t>(3) == AacSyntaxElementTypes::FillElement) {
1791  parseFillElement(m_elementCount);
1792  }
1793  // TODO: reconstruct channel pair
1794  // TODO: map output channels position to internal data channels
1795  m_channelCount += 2;
1796  ++m_elementCount;
1797 }
1798 
1802 void AacFrameElementParser::parseCouplingChannelElement()
1803 {
1804  m_reader.skipBits(4); // element instance tag
1805  std::uint8_t swCceFlag = m_reader.readBit();
1806  std::uint8_t coupledElementCount = m_reader.readBits<std::uint8_t>(3);
1807  std::uint8_t gainElementLists = 0;
1808  for (std::uint8_t c = 0; c < coupledElementCount; ++c) {
1809  ++gainElementLists;
1810  std::uint8_t ccTargetIsCpe = m_reader.readBit();
1811  //byte ccTargetTagSelect = m_reader.readBits<byte>(4);
1812  m_reader.skipBits(4); // cc target tag select
1813  if (ccTargetIsCpe) {
1814  // cc left and right
1815  if (m_reader.readBit() & m_reader.readBit()) {
1816  ++gainElementLists;
1817  }
1818  }
1819  }
1820  m_reader.skipBits(4); // 1 bit cc domain, 1 bit gain element sign, 2 bits gain element scale
1821  AacIcsInfo ics;
1822  std::int16_t specData[1024];
1823  parseIndividualChannelStream(ics, specData);
1824  for (std::uint8_t c = 1; c < gainElementLists; ++c) {
1825  if (swCceFlag || m_reader.readBit()) {
1826  parseHuffmanScaleFactor();
1827  } else {
1828  for (std::uint8_t group = 0; group < ics.windowCount; ++group) {
1829  for (std::uint8_t sfb = 0; sfb < ics.maxSfb; ++sfb) {
1830  if (ics.sfbCb[group][sfb] != AacScaleFactorTypes::ZeroHcb) {
1831  parseHuffmanScaleFactor();
1832  }
1833  }
1834  }
1835  }
1836  }
1837 }
1838 
1842 void AacFrameElementParser::parseLowFrequencyElement()
1843 {
1844  parseSingleChannelElement();
1845 }
1846 
1850 void AacFrameElementParser::parseDataStreamElement()
1851 {
1852  std::uint8_t byteAligned = m_reader.readBit();
1853  std::uint16_t count = m_reader.readBits<std::uint16_t>(8);
1854  if (count == 0xFF) {
1855  count += m_reader.readBits<std::uint16_t>(8);
1856  }
1857  if (byteAligned) {
1858  m_reader.align();
1859  }
1860  m_reader.skipBits(count * 8);
1861 }
1862 
1866 void AacFrameElementParser::parseProgramConfigElement()
1867 {
1868  m_pce.elementInstanceTag = m_reader.readBits<std::uint8_t>(4);
1869  m_pce.objectType = m_reader.readBits<std::uint8_t>(2);
1870  m_pce.samplingFrequencyIndex = m_reader.readBits<std::uint8_t>(4);
1871  m_pce.frontChannelElementCount = m_reader.readBits<std::uint8_t>(4);
1872  m_pce.sideChannelElementCount = m_reader.readBits<std::uint8_t>(4);
1873  m_pce.backChannelElementCount = m_reader.readBits<std::uint8_t>(4);
1874  m_pce.lfeChannelElementCount = m_reader.readBits<std::uint8_t>(2);
1875  m_pce.assocDataElementCount = m_reader.readBits<std::uint8_t>(3);
1876  m_pce.validCcElementCount = m_reader.readBits<std::uint8_t>(4);
1877  if ((m_pce.monoMixdownPresent = m_reader.readBit())) {
1878  m_pce.monoMixdownElementNumber = m_reader.readBits<std::uint8_t>(4);
1879  }
1880  if ((m_pce.stereoMixdownPresent = m_reader.readBit())) {
1881  m_pce.stereoMixdownElementNumber = m_reader.readBits<std::uint8_t>(4);
1882  }
1883  if ((m_pce.matrixMixdownIdxPresent = m_reader.readBit())) {
1884  m_pce.matrixMixdownIdx = m_reader.readBits<std::uint8_t>(2);
1885  m_pce.pseudoSurroundEnable = m_reader.readBit();
1886  }
1887  std::uint8_t i;
1888  for (i = 0; i < m_pce.frontChannelElementCount; ++i) {
1889  m_pce.frontElementIsCpe[i] = m_reader.readBit();
1890  m_pce.frontElementTagSelect[i] = m_reader.readBits<std::uint8_t>(4);
1891  if (m_pce.frontElementIsCpe[i]) { // channel coupling element
1892  m_pce.cpeChannel[m_pce.frontElementTagSelect[i]] = m_pce.channels;
1893  m_pce.frontChannelCount += 2;
1894  m_pce.channels += 2;
1895  } else { // single channel element
1896  m_pce.sceChannel[m_pce.frontElementTagSelect[i]] = m_pce.channels;
1897  ++m_pce.frontChannelCount;
1898  ++m_pce.channels;
1899  }
1900  }
1901  for (i = 0; i < m_pce.sideChannelElementCount; ++i) {
1902  m_pce.sideElementIsCpe[i] = m_reader.readBit();
1903  m_pce.sideElementTagSelect[i] = m_reader.readBits<std::uint8_t>(4);
1904  if (m_pce.sideElementIsCpe[i]) { // channel coupling element
1905  m_pce.cpeChannel[m_pce.sideElementTagSelect[i]] = m_pce.channels;
1906  m_pce.sideChannelCount += 2;
1907  m_pce.channels += 2;
1908  } else { // single channel element
1909  m_pce.sceChannel[m_pce.sideElementTagSelect[i]] = m_pce.channels;
1910  ++m_pce.sideChannelCount;
1911  ++m_pce.channels;
1912  }
1913  }
1914  for (i = 0; i < m_pce.backChannelElementCount; ++i) {
1915  m_pce.backElementIsCpe[i] = m_reader.readBit();
1916  m_pce.backElementTagSelect[i] = m_reader.readBits<std::uint8_t>(4);
1917  if (m_pce.backElementIsCpe[i]) { // channel coupling element
1918  m_pce.cpeChannel[m_pce.backElementTagSelect[i]] = m_pce.channels;
1919  m_pce.backChannelCount += 2;
1920  m_pce.channels += 2;
1921  } else { // single channel element
1922  m_pce.sceChannel[m_pce.backElementTagSelect[i]] = m_pce.channels;
1923  ++m_pce.backChannelCount;
1924  ++m_pce.channels;
1925  }
1926  }
1927  for (i = 0; i < m_pce.lfeChannelElementCount; ++i) {
1928  m_pce.lfeElementTagSelect[i] = m_reader.readBits<std::uint8_t>(4);
1929  m_pce.sceChannel[m_pce.lfeElementTagSelect[i]] = m_pce.channels;
1930  ++m_pce.lfeChannelCount;
1931  ++m_pce.channels;
1932  }
1933  for (i = 0; i < m_pce.assocDataElementCount; ++i) {
1934  m_pce.assocDataElementTagSelect[i] = m_reader.readBits<std::uint8_t>(4);
1935  }
1936  for (i = 0; i < m_pce.validCcElementCount; ++i) {
1937  m_pce.ccElementIsIndSw[i] = m_reader.readBit();
1938  m_pce.validCcElementTagSelect[i] = m_reader.readBits<std::uint8_t>(4);
1939  }
1940  m_reader.align();
1941  m_pce.commentFieldBytes = m_reader.readBits<std::uint8_t>(8);
1942  for (i = 0; i < m_pce.commentFieldBytes; ++i) {
1943  m_pce.commentFieldData[i] = m_reader.readBits<std::uint8_t>(8);
1944  }
1945  m_pce.commentFieldData[i] = 0;
1946  if (m_pce.channels > aacMaxChannels) {
1947  throw NotImplementedException(); // supported channel maximum exceeded
1948  }
1949 }
1950 
1954 void AacFrameElementParser::parseFillElement(std::uint8_t sbrElement)
1955 {
1956  std::uint16_t count = m_reader.readBits<std::uint8_t>(4);
1957  bool crcFlag = 0;
1958  if (count == 0xF) {
1959  count += m_reader.readBits<std::uint8_t>(8);
1960  }
1961  while (count > 0) {
1962  continueWhile:
1963  switch (m_reader.readBits<std::uint8_t>(4)) { // extension type
1964  using namespace AacExtensionTypes;
1965  case DynamicRange:
1966  count -= parseDynamicRange();
1967  break;
1968  case SbrDataCrc:
1969  crcFlag = true;
1970  case SbrData:
1971  if (sbrElement == aacInvalidSbrElement) {
1972  throw InvalidDataException();
1973  } else {
1974  // ensure SBR element exists
1975  if (!m_sbrElements[sbrElement]) {
1976  m_sbrElements[sbrElement] = makeSbrInfo(sbrElement);
1977  }
1978  parseSbrExtensionData(sbrElement, count, crcFlag);
1979  // set global flags
1980  m_sbrPresentFlag = 1;
1981  if (m_sbrElements[sbrElement]->ps) {
1982  m_psUsed[sbrElement] = 1;
1983  m_psUsedGlobal = 1;
1984  }
1985  }
1986  count = 0;
1987  break;
1988  case FillData:
1989  m_reader.skipBits(4 + 8 * (count - 1));
1990  count = 0;
1991  break;
1992  case DataElement:
1993  // data element version
1994  if (m_reader.readBits<std::uint8_t>(4) == 0) {
1995  // ANC data
1996  std::uint8_t dataElementLength = 0, loopCounter = 0;
1997  std::uint16_t dataElementLengthPart;
1998  do {
1999  dataElementLengthPart = m_reader.readBits<std::uint8_t>(8);
2000  dataElementLength += dataElementLengthPart;
2001  ++loopCounter;
2002  } while (dataElementLengthPart == 0xFF);
2003  for (std::uint16_t i = 0; i < dataElementLength; ++i) {
2004  m_reader.skipBits(8); // data element byte
2005  count -= dataElementLength + loopCounter + 1;
2006  goto continueWhile;
2007  // FIXME: loop will run at most once
2008  }
2009  }
2010  m_reader.skipBits(8 * (count - 1));
2011  count = 0;
2012  break;
2013  case Fill:
2014  case SacData:
2015  default:
2016  m_reader.skipBits(4 + 8 * (count - 1));
2017  count = 0;
2018  }
2019  }
2020 }
2021 
2027 void AacFrameElementParser::parseRawDataBlock()
2028 {
2029  if (m_mpeg4AudioObjectId < Mpeg4AudioObjectIds::ErAacLc) {
2030  for (;;) {
2031  switch (m_reader.readBits<std::uint8_t>(3)) { // parse element type
2032  using namespace AacSyntaxElementTypes;
2033  case SingleChannelElement:
2034  parseSingleChannelElement();
2035  break;
2036  case ChannelPairElement:
2037  parseChannelPairElement();
2038  break;
2039  case ChannelCouplingElement:
2040  parseCouplingChannelElement();
2041  break;
2042  case LowFrequencyElement:
2043  parseLowFrequencyElement();
2044  break;
2045  case DataStreamElement:
2046  parseDataStreamElement();
2047  break;
2048  case ProgramConfigElement:
2049  parseProgramConfigElement();
2050  break;
2051  case FillElement:
2052  parseFillElement();
2053  break;
2054  case EndOfFrame:
2055  goto endOfBlock;
2056  default:;
2057  }
2058  }
2059  } else { // error resilience
2060  switch (m_mpeg4ChannelConfig) {
2061  using namespace Mpeg4ChannelConfigs;
2062  using namespace AacSyntaxElementTypes;
2063  case FrontCenter:
2064  parseSingleChannelElement();
2065  break;
2066  case FrontLeftFrontRight:
2067  parseChannelPairElement();
2068  break;
2070  parseSingleChannelElement();
2071  parseChannelPairElement();
2072  break;
2074  parseSingleChannelElement();
2075  parseChannelPairElement();
2076  parseSingleChannelElement();
2077  break;
2079  parseSingleChannelElement();
2080  parseChannelPairElement();
2081  parseChannelPairElement();
2082  break;
2084  parseSingleChannelElement();
2085  parseChannelPairElement();
2086  parseChannelPairElement();
2087  parseSingleChannelElement();
2088  break;
2090  parseSingleChannelElement();
2091  parseChannelPairElement();
2092  parseChannelPairElement();
2093  parseChannelPairElement();
2094  parseSingleChannelElement();
2095  break;
2096  }
2097  }
2098 endOfBlock:;
2099 }
2100 
2104 void AacFrameElementParser::parse(const AdtsFrame &adtsFrame, std::istream &stream, std::size_t dataSize)
2105 {
2106  auto data = make_unique<char[]>(dataSize);
2107  stream.read(data.get(), dataSize);
2108  parse(adtsFrame, data, dataSize);
2109 }
2110 
2114 void AacFrameElementParser::parse(const AdtsFrame &adtsFrame, std::unique_ptr<char[]> &data, std::size_t dataSize)
2115 {
2116  m_reader.reset(data.get(), dataSize);
2117  m_mpeg4AudioObjectId = adtsFrame.mpeg4AudioObjectId();
2118  m_mpeg4SamplingFrequencyIndex = adtsFrame.mpeg4SamplingFrequencyIndex();
2119  parseRawDataBlock();
2120 }
2121 
2123 
2124 } // namespace TagParser
TagParser::Mpeg4ChannelConfigs::FrontCenterFrontLeftFrontRightSideLeftSideRightBackLeftBackRightLFEChannel
@ FrontCenterFrontLeftFrontRightSideLeftSideRightBackLeftBackRightLFEChannel
Definition: mp4ids.h:621
TagParser::Mpeg4ChannelConfigs::FrontCenterFrontLeftFrontRightBackLeftBackRight
@ FrontCenterFrontLeftFrontRightBackLeftBackRight
Definition: mp4ids.h:619
TagParser::Mpeg4AudioObjectIds::ErAacLc
@ ErAacLc
Definition: mp4ids.h:577
aaccodebook.h
TagParser::Mpeg4ChannelConfigs::FrontCenterFrontLeftFrontRightBackLeftBackRightLFEChannel
@ FrontCenterFrontLeftFrontRightBackLeftBackRightLFEChannel
Definition: mp4ids.h:620
TagParser::Mpeg4AudioObjectIds::AacMain
@ AacMain
Definition: mp4ids.h:563
TagParser
Contains all classes and functions of the TagInfo library.
Definition: aaccodebook.h:10
TagParser::Mpeg4ChannelConfigs::FrontCenterFrontLeftFrontRightBackCenter
@ FrontCenterFrontLeftFrontRightBackCenter
Definition: mp4ids.h:618
TagParser::mpeg4SamplingFrequencyTable
std::uint32_t mpeg4SamplingFrequencyTable[13]
Definition: mp4ids.cpp:422
TagParser::Mpeg4ChannelConfigs::FrontCenter
@ FrontCenter
Definition: mp4ids.h:615
TagParser::Mpeg4AudioObjectIds::ErParametric
@ ErParametric
Definition: mp4ids.h:586
TagParser::Mpeg4AudioObjectIds::AacSsr
@ AacSsr
Definition: mp4ids.h:565
CppUtilities
Definition: abstractcontainer.h:15
TagParser::Mpeg4AudioObjectIds::Ps
@ Ps
Definition: mp4ids.h:588
aacframe.h
TagParser::Mpeg4ChannelConfigs::FrontCenterFrontLeftFrontRight
@ FrontCenterFrontLeftFrontRight
Definition: mp4ids.h:617
TagParser::Mpeg4ChannelConfigs::FrontLeftFrontRight
@ FrontLeftFrontRight
Definition: mp4ids.h:616
TagParser::Mpeg4AudioObjectIds::ErAacLd
@ ErAacLd
Definition: mp4ids.h:582