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