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