Tag Parser  7.1.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, bsRelCount0, bsRelCount1;
960  switch ((sbr->bsFrameClass[channel] = m_reader.readBits<byte>(2))) {
961  using namespace BsFrameClasses;
962  case FixFix:
963  tmp = m_reader.readBits<byte>(2);
964  sbr->absBordLead[channel] = 0;
965  sbr->absBordTrail[channel] = sbr->timeSlotsCount;
966  sbr->relLeadCount[channel] = (bsEnvCount = min<byte>(static_cast<byte>(1 << tmp), 5)) - 1;
967  sbr->relTrailCount[channel] = 0;
968  tmp = m_reader.readBit();
969  for (byte env = 0; env < bsEnvCount; ++env) {
970  sbr->f[channel][env] = tmp;
971  }
972  break;
973  case FixVar:
974  sbr->absBordLead[channel] = 0;
975  sbr->absBordTrail[channel] = m_reader.readBits<byte>(2) + sbr->timeSlotsCount;
976  sbr->relLeadCount[channel] = 0;
977  sbr->relTrailCount[channel] = bsEnvCount = m_reader.readBits<byte>(2);
978  for (byte rel = 0; rel < bsEnvCount; ++rel) {
979  sbr->bsRelBord[channel][rel] = 2 * m_reader.readBits<byte>(2) + 2;
980  }
981  sbr->bsPointer[channel] = m_reader.readBits<byte>(static_cast<byte>(sbrLog2(static_cast<sbyte>(bsEnvCount + 2))));
982  for (byte env = 0; env <= bsEnvCount; ++env) {
983  sbr->f[channel][bsEnvCount - env] = m_reader.readBit();
984  }
985  break;
986  case VarFix:
987  sbr->absBordLead[channel] = m_reader.readBits<byte>(2);
988  sbr->absBordTrail[channel] = sbr->timeSlotsCount;
989  sbr->relLeadCount[channel] = bsEnvCount = m_reader.readBits<byte>(2);
990  sbr->relTrailCount[channel] = 0;
991  for (byte rel = 0; rel < bsEnvCount; ++rel) {
992  sbr->bsRelBord[channel][rel] = 2 * m_reader.readBits<byte>(2) + 2;
993  }
994  sbr->bsPointer[channel] = m_reader.readBits<byte>(static_cast<byte>(sbrLog2(static_cast<sbyte>(bsEnvCount + 2))));
995  for (byte env = 0; env < bsEnvCount; ++env) {
996  sbr->f[channel][env] = m_reader.readBit();
997  }
998  break;
999  case VarVar:
1000  sbr->absBordLead[channel] = m_reader.readBits<byte>(2);
1001  sbr->absBordTrail[channel] = m_reader.readBits<byte>(2) + sbr->timeSlotsCount;
1002  bsRelCount0 = m_reader.readBits<byte>(2);
1003  bsRelCount1 = m_reader.readBits<byte>(2);
1004  bsEnvCount = min<byte>(5, sbr->bsRelCount0[channel] + sbr->bsRelCount1[channel] + 1);
1005  for (byte rel = 0; rel < sbr->bsRelCount0[channel]; ++rel) {
1006  sbr->bsRelBord0[channel][rel] = 2 * m_reader.readBits<byte>(2) + 2;
1007  }
1008  for (byte rel = 0; rel < sbr->bsRelCount1[channel]; ++rel) {
1009  sbr->bsRelBord1[channel][rel] = 2 * m_reader.readBits<byte>(2) + 2;
1010  }
1011  sbr->bsPointer[channel]
1012  = m_reader.readBits<byte>(static_cast<byte>(sbrLog2(static_cast<sbyte>(sbr->bsRelCount0[channel] + sbr->bsRelCount1[channel] + 2))));
1013  for (byte env = 0; env < bsEnvCount; ++env) {
1014  sbr->f[channel][env] = m_reader.readBit();
1015  }
1016  sbr->relLeadCount[channel] = sbr->bsRelCount0[channel];
1017  sbr->relTrailCount[channel] = sbr->bsRelCount1[channel];
1018  break;
1019  default:;
1020  }
1021  if ((sbr->le[channel] = min<byte>(bsEnvCount, sbr->bsFrameClass[channel] == BsFrameClasses::VarVar ? 5 : 4)) <= 0) {
1022  throw InvalidDataException();
1023  }
1024  sbr->lq[channel] = sbr->le[channel] > 1 ? 2 : 1;
1025  // TODO: envelope time border vector, noise floor time border vector
1026 }
1027 
1028 void AacFrameElementParser::parseSbrDtdf(std::shared_ptr<AacSbrInfo> &sbr, byte channel)
1029 {
1030  for (byte i = 0; i < sbr->le[channel]; ++i) {
1031  sbr->bsDfEnv[channel][i] = m_reader.readBit();
1032  }
1033  for (byte i = 0; i < sbr->lq[channel]; ++i) {
1034  sbr->bsDfNoise[channel][i] = m_reader.readBit();
1035  }
1036 }
1037 
1038 void AacFrameElementParser::parseInvfMode(std::shared_ptr<AacSbrInfo> &sbr, byte channel)
1039 {
1040  for (byte i = 0; i < sbr->nq; ++i) {
1041  sbr->bsInvfMode[channel][i] = m_reader.readBits<byte>(2);
1042  }
1043 }
1044 
1045 void AacFrameElementParser::parseSbrEnvelope(std::shared_ptr<AacSbrInfo> &sbr, byte channel)
1046 {
1047  sbyte delta;
1048  SbrHuffTab tHuff, fHuff;
1049  if ((sbr->le[channel] == 1) && (sbr->bsFrameClass[channel] == BsFrameClasses::FixFix)) {
1050  sbr->ampRes[channel] = 0;
1051  } else {
1052  sbr->ampRes[channel] = sbr->bsAmpRes;
1053  }
1054  if ((sbr->bsCoupling) && (channel == 1)) {
1055  delta = 1;
1056  if (sbr->ampRes[channel]) {
1057  tHuff = tHuffmanEnvBal30dB;
1058  fHuff = fHuffmanEnvBal30dB;
1059  } else {
1060  tHuff = tHuffmanEnvBal15dB;
1061  fHuff = fHuffmanEnvBal15dB;
1062  }
1063  } else {
1064  delta = 0;
1065  if (sbr->ampRes[channel]) {
1066  tHuff = tHuffmanEnv30dB;
1067  fHuff = fHuffmanEnv30dB;
1068  } else {
1069  tHuff = tHuffmanEnv15dB;
1070  fHuff = fHuffmanEnv15dB;
1071  }
1072  }
1073  for (byte env = 0; env < sbr->le[channel]; ++env) {
1074  if (sbr->bsDfEnv[channel][env] == 0) {
1075  if ((sbr->bsCoupling == 1) && (channel == 1)) {
1076  if (sbr->ampRes[channel]) {
1077  sbr->e[channel][0][env] = static_cast<int16>(m_reader.readBits<uint16>(5) << delta);
1078  } else {
1079  sbr->e[channel][0][env] = static_cast<int16>(m_reader.readBits<uint16>(6) << delta);
1080  }
1081  } else {
1082  if (sbr->ampRes[channel]) {
1083  sbr->e[channel][0][env] = static_cast<int16>(m_reader.readBits<uint16>(6) << delta);
1084  } else {
1085  sbr->e[channel][0][env] = static_cast<int16>(m_reader.readBits<uint16>(7) << delta);
1086  }
1087  }
1088  for (byte band = 1; band < sbr->n[sbr->f[channel][env]]; ++band) {
1089  sbr->e[channel][band][env] = static_cast<int16>(sbrHuffmanDec(fHuff) << delta);
1090  }
1091  } else {
1092  for (byte band = 0; band < sbr->n[sbr->f[channel][env]]; ++band) {
1093  sbr->e[channel][band][env] = static_cast<int16>(sbrHuffmanDec(fHuff) << delta);
1094  }
1095  }
1096  }
1097  // TODO: extract envelope data
1098 }
1099 
1100 void AacFrameElementParser::parseSbrNoise(std::shared_ptr<AacSbrInfo> &sbr, byte channel)
1101 {
1102  sbyte delta;
1103  SbrHuffTab tHuff, fHuff;
1104  if ((sbr->bsCoupling == 1) && (channel == 1)) {
1105  delta = 1;
1106  tHuff = tHuffmanNoiseBal30dB;
1107  fHuff = fHuffmanEnvBal30dB;
1108  } else {
1109  delta = 1;
1110  tHuff = tHuffmanNoise30dB;
1111  fHuff = fHuffmanEnv30dB;
1112  }
1113  for (byte noise = 0; noise < sbr->lq[channel]; ++noise) {
1114  if (sbr->bsDfNoise[channel][noise] == 0) {
1115  if ((sbr->bsCoupling == 1) && (channel == 1)) {
1116  sbr->q[channel][0][noise] = m_reader.readBits<byte>(5) << delta;
1117  } else {
1118  sbr->q[channel][0][noise] = m_reader.readBits<byte>(5) << delta;
1119  }
1120  for (byte band = 1; band < sbr->nq; ++band) {
1121  sbr->q[channel][band][noise] = sbrHuffmanDec(fHuff) << delta;
1122  }
1123  } else {
1124  for (byte band = 0; band < sbr->nq; ++band) {
1125  sbr->q[channel][band][noise] = sbrHuffmanDec(fHuff) << delta;
1126  }
1127  }
1128  }
1129  // TODO: extract noise floor data
1130 }
1131 
1132 void AacFrameElementParser::parseSbrSinusoidalCoding(std::shared_ptr<AacSbrInfo> &sbr, byte channel)
1133 {
1134  for (byte i = 0; i < sbr->nHigh; ++i) {
1135  sbr->bsAddHarmonic[channel][i] = m_reader.readBit();
1136  }
1137 }
1138 
1139 uint16 AacFrameElementParser::parseSbrExtension(std::shared_ptr<AacSbrInfo> &sbr, byte extensionId, byte)
1140 {
1141  byte header;
1142  uint16 res;
1143  switch (extensionId) {
1144  using namespace AacSbrExtensionIds;
1145  case Ps:
1146  if (sbr->psResetFlag) {
1147  sbr->ps->headerRead = 0;
1148  }
1149  res = parsePsData(sbr->ps, header);
1150  if (sbr->psUsed == 0 && header == 1) {
1151  sbr->psUsed = 1;
1152  }
1153  if (header == 1) {
1154  sbr->psResetFlag = 0;
1155  }
1156  return res;
1157  case DrmParametricStereo:
1158  sbr->psUsed = 1;
1159  return parseDrmPsData(sbr->drmPs);
1160  default:
1161  sbr->bsExtendedData = m_reader.readBits<byte>(6);
1162  return 6;
1163  }
1164 }
1165 
1166 uint16 AacFrameElementParser::parsePsData(std::shared_ptr<AacPsInfo> &ps, byte &header)
1167 {
1168  if (m_reader.readBit()) {
1169  header = 1;
1170  ps->headerRead = 1;
1171  ps->use34HybridBands = 0;
1172  if ((ps->enableIID = m_reader.readBit())) {
1173  ps->iidMode = m_reader.readBits<byte>(3);
1174  }
1175  }
1176  throw NotImplementedException(); // TODO
1177 }
1178 
1179 uint16 AacFrameElementParser::parseDrmPsData(std::shared_ptr<AacDrmPsInfo> &drmPs)
1180 {
1181  VAR_UNUSED(drmPs)
1182  throw NotImplementedException(); // TODO
1183 }
1184 
1185 void AacFrameElementParser::parseSbrSingleChannelElement(std::shared_ptr<AacSbrInfo> &sbr)
1186 {
1187  if (m_reader.readBit()) { // bs data extra
1188  m_reader.skipBits(4); // skip bs reserved
1189  }
1190  if (sbr->isDrmSbr) {
1191  m_reader.skipBits(1); // bs coupling
1192  }
1193  parseSbrGrid(sbr, 0);
1194  parseSbrDtdf(sbr, 0);
1195  parseInvfMode(sbr, 0);
1196  parseSbrEnvelope(sbr, 0);
1197  parseSbrNoise(sbr, 0);
1198  // TODO: envelope noise dequantisation
1199  if ((sbr->bsAddHarmonicFlag[0] = m_reader.readBit())) {
1200  parseSbrSinusoidalCoding(sbr, 0);
1201  }
1202  if ((sbr->bsExtendedData = m_reader.readBit())) {
1203  uint16 cnt = m_reader.readBits<uint16>(4);
1204  if (cnt == 0xF) {
1205  cnt += m_reader.readBits<uint16>(8);
1206  }
1207  uint16 bitsLeft = 8 * cnt;
1208  while (bitsLeft > 7) {
1209  sbr->bsExtensionId = m_reader.readBits<byte>(2);
1210  uint16 tmpBitCount = 2 + parseSbrExtension(sbr, sbr->bsExtensionId, static_cast<byte>(bitsLeft));
1211  if (tmpBitCount > bitsLeft) {
1212  throw InvalidDataException();
1213  } else {
1214  bitsLeft -= tmpBitCount;
1215  }
1216  }
1217  if (bitsLeft) { // read remaining bits
1218  m_reader.skipBits(bitsLeft);
1219  }
1220  }
1221 }
1222 
1223 void AacFrameElementParser::parseSbrChannelPairElement(std::shared_ptr<AacSbrInfo> &sbr)
1224 {
1225  if (m_reader.readBit()) { // bs data extra
1226  m_reader.skipBits(8); // skip bs reserved
1227  }
1228  if ((sbr->bsCoupling = m_reader.readBit())) {
1229  parseSbrGrid(sbr, 0);
1230  // copy data from left to right
1231  sbr->bsFrameClass[1] = sbr->bsFrameClass[0];
1232  sbr->le[1] = sbr->le[0];
1233  sbr->lq[1] = sbr->lq[0];
1234  sbr->bsPointer[1] = sbr->bsPointer[0];
1235  for (byte n = 0; n < sbr->le[0]; ++n) {
1236  sbr->te[1][n] = sbr->te[0][n];
1237  sbr->f[1][n] = sbr->f[0][n];
1238  }
1239  for (byte n = 0; n < sbr->lq[0]; ++n) {
1240  sbr->tq[1][n] = sbr->tq[0][n];
1241  }
1242  parseSbrDtdf(sbr, 0);
1243  parseSbrDtdf(sbr, 1);
1244  parseInvfMode(sbr, 0);
1245  for (byte n = 0; n < sbr->nq; ++n) {
1246  sbr->bsInvfMode[1][n] = sbr->bsInvfMode[0][n];
1247  }
1248  parseSbrEnvelope(sbr, 0);
1249  parseSbrNoise(sbr, 0);
1250  parseSbrEnvelope(sbr, 1);
1251  parseSbrNoise(sbr, 1);
1252  } else {
1253  parseSbrGrid(sbr, 0);
1254  parseSbrGrid(sbr, 1);
1255  parseSbrDtdf(sbr, 0);
1256  parseSbrDtdf(sbr, 1);
1257  parseInvfMode(sbr, 0);
1258  parseInvfMode(sbr, 1);
1259  parseSbrEnvelope(sbr, 0);
1260  parseSbrEnvelope(sbr, 1);
1261  parseSbrNoise(sbr, 0);
1262  parseSbrNoise(sbr, 1);
1263  }
1264  if ((sbr->bsAddHarmonicFlag[0] = m_reader.readBit())) {
1265  parseSbrSinusoidalCoding(sbr, 0);
1266  }
1267  if ((sbr->bsAddHarmonicFlag[1] = m_reader.readBit())) {
1268  parseSbrSinusoidalCoding(sbr, 1);
1269  }
1270  // TODO: envelope noise dequantisation (for both channels)
1271  if (sbr->bsCoupling) {
1272  // TODO: unmap envelope noise
1273  }
1274  if ((sbr->bsExtendedData = m_reader.readBit())) {
1275  uint16 cnt = m_reader.readBits<uint16>(4);
1276  if (cnt == 0xF) {
1277  cnt += m_reader.readBits<uint16>(8);
1278  }
1279  uint16 bitsLeft = 8 * cnt;
1280  while (bitsLeft > 7) {
1281  sbr->bsExtensionId = m_reader.readBits<byte>(2);
1282  uint16 tmpBitCount = 2 + parseSbrExtension(sbr, sbr->bsExtensionId, static_cast<byte>(bitsLeft));
1283  if (tmpBitCount > bitsLeft) {
1284  throw InvalidDataException();
1285  } else {
1286  bitsLeft -= tmpBitCount;
1287  }
1288  }
1289  if (bitsLeft) { // read remaining bits
1290  m_reader.skipBits(bitsLeft);
1291  }
1292  }
1293 }
1294 
1295 shared_ptr<AacSbrInfo> AacFrameElementParser::makeSbrInfo(byte sbrElement, bool isDrm)
1296 {
1297  if (m_mpeg4ExtensionSamplingFrequencyIndex >= sizeof(mpeg4SamplingFrequencyTable)
1298  && m_mpeg4SamplingFrequencyIndex >= sizeof(mpeg4SamplingFrequencyTable)) {
1299  throw InvalidDataException(); // sampling frequency index is invalid
1300  }
1301  return make_shared<AacSbrInfo>(m_elementId[sbrElement],
1302  m_mpeg4ExtensionSamplingFrequencyIndex < sizeof(mpeg4SamplingFrequencyTable)
1303  ? mpeg4SamplingFrequencyTable[m_mpeg4ExtensionSamplingFrequencyIndex]
1304  : mpeg4SamplingFrequencyTable[m_mpeg4SamplingFrequencyIndex] * 2,
1305  m_frameLength, isDrm);
1306 }
1307 
1308 void AacFrameElementParser::parseSbrExtensionData(byte sbrElement, uint16 count, bool crcFlag)
1309 {
1310  VAR_UNUSED(count);
1311  //uint16 alignBitCount = 0;
1312  std::shared_ptr<AacSbrInfo> &sbr = m_sbrElements[sbrElement];
1313  if (m_psResetFlag) {
1314  sbr->psResetFlag = m_psResetFlag;
1315  }
1316  if (!sbr->isDrmSbr) {
1317  if (crcFlag) {
1318  sbr->bsSbrCrcBits = m_reader.readBits<uint16>(10);
1319  }
1320  }
1321  //auto startFrequ = sbr->bsStartFreq;
1322  //auto samplerateMode = sbr->bsSamplerateMode;
1323  //auto stopFrequ = sbr->bsStopFreq;
1324  //auto frequScale = sbr->bsFreqScale;
1325  //auto alterScale = sbr->bsAlterScale;
1326  //auto xoverBand = sbr->bsXoverBand;
1327  if ((sbr->bsHeaderFlag = m_reader.readBit())) {
1328  sbr->bsStartFreq = m_reader.readBits<byte>(4);
1329  sbr->bsStopFreq = m_reader.readBits<byte>(4);
1330  sbr->bsXoverBand = m_reader.readBits<byte>(3);
1331  m_reader.skipBits(2);
1332  byte bsExtraHeader1 = m_reader.readBit();
1333  byte bsExtraHeader2 = m_reader.readBit();
1334  if (bsExtraHeader1) {
1335  sbr->bsFreqScale = m_reader.readBits<byte>(2);
1336  sbr->bsAlterScale = m_reader.readBit();
1337  sbr->bsNoiseBands = m_reader.readBits<byte>(2);
1338  } else {
1339  sbr->bsFreqScale = 2;
1340  sbr->bsAlterScale = 1;
1341  sbr->bsNoiseBands = 2;
1342  }
1343  if (bsExtraHeader2) {
1344  sbr->bsLimiterBands = m_reader.readBits<byte>(2);
1345  sbr->bsLimiterGains = m_reader.readBits<byte>(2);
1346  sbr->bsInterpolFreq = m_reader.readBit();
1347  sbr->bsSmoothingMode = m_reader.readBit();
1348  } else {
1349  sbr->bsLimiterBands = 2;
1350  sbr->bsLimiterGains = 2;
1351  sbr->bsInterpolFreq = 1;
1352  sbr->bsSmoothingMode = 1;
1353  }
1354  }
1355  if (sbr->headerCount) {
1356  if (sbr->reset || (sbr->bsHeaderFlag && sbr->justSeeked)) {
1357  // TODO: calc SBR tables; restore old values on error
1358  }
1359  sbr->rate = sbr->bsSamplerateMode ? 2 : 1;
1360  switch (sbr->aacElementId) {
1361  using namespace AacSyntaxElementTypes;
1362  case SingleChannelElement:
1363  parseSbrSingleChannelElement(sbr);
1364  break;
1365  case ChannelPairElement:
1366  parseSbrChannelPairElement(sbr);
1367  break;
1368  }
1369  }
1370 }
1371 
1372 byte AacFrameElementParser::parseHuffmanScaleFactor()
1373 {
1374  uint16 offset = 0;
1375  while (aacHcbSf[offset][1]) {
1376  offset += aacHcbSf[offset][m_reader.readBit()];
1377  if (offset > 240) {
1378  throw InvalidDataException();
1379  }
1380  }
1381  return aacHcbSf[offset][0];
1382 }
1383 
1384 void AacFrameElementParser::parseHuffmanSpectralData(byte cb, int16 *sp)
1385 {
1386  switch (cb) {
1387  case 1:
1388  case 2: // 2-step method for data quadruples
1389  huffman2StepQuad(cb, sp);
1390  break;
1391  case 3: // binary search for data quadruples
1392  huffmanBinaryQuadSign(cb, sp);
1393  break;
1394  case 4: // binary search for data paris
1395  huffmanBinaryPair(cb, sp);
1396  break;
1397  case 5: // 2-step method for data pairs
1398  huffman2StepPair(cb, sp);
1399  break;
1400  case 6: // 2-step method for data pairs
1401  huffman2StepPair(cb, sp);
1402  break;
1403  case 7:
1404  case 9: // binary search for data pairs
1405  huffmanBinaryPairSign(cb, sp);
1406  break;
1407  case 8:
1408  case 10: // 2-step method for data pairs
1409  huffman2StepPairSign(cb, sp);
1410  break;
1411  case 11:
1412  try {
1413  huffman2StepPairSign(11, sp);
1414  } catch (const InvalidDataException &) {
1415  sp[0] = huffmanGetEscape(sp[0]);
1416  sp[1] = huffmanGetEscape(sp[1]);
1417  throw;
1418  }
1419  sp[0] = huffmanGetEscape(sp[0]);
1420  sp[1] = huffmanGetEscape(sp[1]);
1421  break;
1422  case 12:
1423  try {
1424  huffman2StepPair(11, sp);
1425  } catch (const InvalidDataException &) {
1426  sp[0] = huffmanCodebook(0);
1427  sp[1] = huffmanCodebook(1);
1428  throw;
1429  }
1430  sp[0] = huffmanCodebook(0);
1431  sp[1] = huffmanCodebook(1);
1432  break;
1433  case 16:
1434  case 17:
1435  case 18:
1436  case 19:
1437  case 20:
1438  case 21:
1439  case 22:
1440  case 23:
1441  case 24:
1442  case 25:
1443  case 26:
1444  case 27:
1445  case 28:
1446  case 29:
1447  case 30:
1448  case 31:
1449  try {
1450  huffman2StepPairSign(11, sp);
1451  } catch (const InvalidDataException &) {
1452  sp[0] = huffmanGetEscape(sp[0]);
1453  sp[1] = huffmanGetEscape(sp[1]);
1454  vcb11CheckLav(cb, sp);
1455  throw;
1456  }
1457  sp[0] = huffmanGetEscape(sp[0]);
1458  sp[1] = huffmanGetEscape(sp[1]);
1459  vcb11CheckLav(cb, sp);
1460  break;
1461  default:
1462  throw InvalidDataException(); // non existend codebook number
1463  }
1464 }
1465 
1466 void AacFrameElementParser::huffmanSignBits(int16 *sp, byte len)
1467 {
1468  for (int16 *end = sp + len; sp < end; ++sp) {
1469  if (*sp) {
1470  if (m_reader.readBit()) {
1471  *sp = -(*sp);
1472  }
1473  }
1474  }
1475 }
1476 
1477 void AacFrameElementParser::huffman2StepQuad(byte cb, int16 *sp)
1478 {
1479  uint32 cw = m_reader.showBits<uint32>(aacHcbN[cb]);
1480  uint16 offset = aacHcbTable[cb][cw].offset;
1481  uint8_t extraBits = aacHcbTable[cb][cw].extraBits;
1482  if (extraBits) {
1483  m_reader.skipBits(aacHcbN[cb]);
1484  offset += m_reader.showBits<uint16>(extraBits);
1485  m_reader.skipBits(aacHcb2QuadTable[cb][offset].bits - aacHcbN[cb]);
1486  } else {
1487  m_reader.skipBits(aacHcb2QuadTable[cb][offset].bits);
1488  }
1489  if (offset > aacHcb2QuadTableSize[cb]) {
1490  throw InvalidDataException();
1491  }
1492  sp[0] = aacHcb2QuadTable[cb][offset].x;
1493  sp[1] = aacHcb2QuadTable[cb][offset].y;
1494  sp[2] = aacHcb2QuadTable[cb][offset].v;
1495  sp[3] = aacHcb2QuadTable[cb][offset].w;
1496 }
1497 
1498 void AacFrameElementParser::huffmanBinaryQuadSign(byte cb, int16 *sp)
1499 {
1500  try {
1501  huffman2StepQuad(cb, sp);
1502  } catch (const InvalidDataException &) {
1503  huffmanSignBits(sp, 4);
1504  throw;
1505  }
1506  huffmanSignBits(sp, 4);
1507 }
1508 
1509 void AacFrameElementParser::huffmanBinaryPair(byte cb, int16 *sp)
1510 {
1511  uint16 offset = 0;
1512  while (!aacHcbBinTable[cb][offset].isLeaf) {
1513  offset += aacHcbBinTable[cb][offset].data[m_reader.readBit()];
1514  }
1515  if (offset > aacHcbBinTableSize[cb]) {
1516  throw InvalidDataException();
1517  }
1518  sp[0] = aacHcbBinTable[cb][offset].data[0];
1519  sp[1] = aacHcbBinTable[cb][offset].data[1];
1520 }
1521 
1522 void AacFrameElementParser::huffman2StepPair(byte cb, int16 *sp)
1523 {
1524  uint32 cw = m_reader.showBits<uint32>(aacHcbN[cb]);
1525  uint16 offset = aacHcbTable[cb][cw].offset;
1526  uint8_t extraBits = aacHcbTable[cb][cw].extraBits;
1527  if (extraBits) {
1528  m_reader.skipBits(aacHcbN[cb]);
1529  offset += m_reader.showBits<uint16>(extraBits);
1530  m_reader.skipBits(aacHcb2PairTable[cb][offset].bits - aacHcbN[cb]);
1531  } else {
1532  m_reader.skipBits(aacHcb2PairTable[cb][offset].bits);
1533  }
1534  if (offset > aacHcb2PairTableSize[cb]) {
1535  throw InvalidDataException();
1536  }
1537  sp[0] = aacHcb2PairTable[cb][offset].x;
1538  sp[1] = aacHcb2PairTable[cb][offset].y;
1539 }
1540 
1541 void AacFrameElementParser::huffmanBinaryPairSign(byte cb, int16 *sp)
1542 {
1543  try {
1544  huffmanBinaryPair(cb, sp);
1545  } catch (const InvalidDataException &) {
1546  huffmanSignBits(sp, 2);
1547  throw;
1548  }
1549  huffmanSignBits(sp, 2);
1550 }
1551 
1552 void AacFrameElementParser::huffman2StepPairSign(byte cb, int16 *sp)
1553 {
1554  try {
1555  huffman2StepPair(cb, sp);
1556  } catch (const InvalidDataException &) {
1557  huffmanSignBits(sp, 2);
1558  throw;
1559  }
1560  huffmanSignBits(sp, 2);
1561 }
1562 
1563 int16 AacFrameElementParser::huffmanGetEscape(int16 sp)
1564 {
1565  byte neg;
1566  if (sp < 0) {
1567  if (sp != -16)
1568  return sp;
1569  neg = 1;
1570  } else {
1571  if (sp != 16)
1572  return sp;
1573  neg = 0;
1574  }
1575  byte size;
1576  for (size = 4; m_reader.readBit(); ++size) {
1577  }
1578  const int16 off = m_reader.readBits<int16>(size);
1579  return static_cast<int16>(neg ? -(off | (1 << size)) : (off | (1 << size)));
1580 }
1581 
1582 void AacFrameElementParser::vcb11CheckLav(byte cb, int16 *sp)
1583 {
1584  static const uint16_t vcb11LavTab[] = { 16, 31, 47, 63, 95, 127, 159, 191, 223, 255, 319, 383, 511, 767, 1023, 2047 };
1585  uint16 max = 0;
1586  if (cb < 16 || cb > 31)
1587  return;
1588  max = vcb11LavTab[cb - 16];
1589  if ((abs(sp[0]) > max) || (abs(sp[1]) > max)) {
1590  sp[0] = 0;
1591  sp[1] = 0;
1592  }
1593 }
1594 
1598 void AacFrameElementParser::calculateWindowGroupingInfo(AacIcsInfo &ics)
1599 {
1600  using namespace AacIcsSequenceTypes;
1601  switch (ics.windowSequence) {
1602  case OnlyLongSequence:
1603  case LongStartSequence:
1604  case LongStopSequence:
1605  ics.windowCount = ics.windowGroupCount = ics.windowGroupLengths[0] = 1;
1606  if (m_mpeg4AudioObjectId == Mpeg4AudioObjectIds::ErAacLd) {
1607  if (m_frameLength == 512) {
1608  ics.swbCount = swb512WindowCount[m_mpeg4SamplingFrequencyIndex];
1609  } else { // if 480
1610  ics.swbCount = swb480WindowCount[m_mpeg4SamplingFrequencyIndex];
1611  }
1612  } else {
1613  if (m_frameLength == 1024) {
1614  ics.swbCount = swb1024WindowCount[m_mpeg4SamplingFrequencyIndex];
1615  } else {
1616  ics.swbCount = swb960WindowCount[m_mpeg4SamplingFrequencyIndex];
1617  }
1618  }
1619  if (ics.maxSfb > ics.swbCount) {
1620  throw InvalidDataException();
1621  }
1622  if (m_mpeg4AudioObjectId == Mpeg4AudioObjectIds::ErAacLd) {
1623  if (m_frameLength == 512) {
1624  for (byte i = 0; i <= ics.swbCount; ++i) {
1625  ics.sectionSfbOffset[0][i] = swbOffset512Window[m_mpeg4SamplingFrequencyIndex][i];
1626  ics.swbOffset[i] = swbOffset512Window[m_mpeg4SamplingFrequencyIndex][i];
1627  }
1628  } else {
1629  for (byte i = 0; i <= ics.swbCount; ++i) {
1630  ics.sectionSfbOffset[0][i] = swbOffset480Window[m_mpeg4SamplingFrequencyIndex][i];
1631  ics.swbOffset[i] = swbOffset480Window[m_mpeg4SamplingFrequencyIndex][i];
1632  }
1633  }
1634  } else {
1635  for (byte i = 0; i <= ics.swbCount; ++i) {
1636  ics.sectionSfbOffset[0][i] = swbOffset1024Window[m_mpeg4SamplingFrequencyIndex][i];
1637  ics.swbOffset[i] = swbOffset1024Window[m_mpeg4SamplingFrequencyIndex][i];
1638  }
1639  }
1640  ics.sectionSfbOffset[0][ics.swbCount] = ics.swbOffset[ics.swbCount] = ics.maxSwbOffset = m_frameLength;
1641  break;
1642  case EightShortSequence:
1643  ics.windowCount = 8;
1644  ics.windowGroupCount = ics.windowGroupLengths[0] = 1;
1645  ics.swbCount = swb128WindowCount[m_mpeg4SamplingFrequencyIndex];
1646  if (ics.maxSfb > ics.swbCount) {
1647  throw InvalidDataException();
1648  }
1649  for (byte i = 0; i < ics.swbCount; ++i) {
1650  ics.swbOffset[i] = swbOffset128Window[m_mpeg4SamplingFrequencyIndex][i];
1651  }
1652  ics.swbOffset[ics.swbCount] = ics.maxSwbOffset = m_frameLength / 8;
1653  for (byte i = 0; i < ics.windowCount - 1; ++i) {
1654  if (!(ics.scaleFactorGrouping & (1 << (6 - i)))) {
1655  ics.windowGroupLengths[ics.windowGroupCount] = 1;
1656  ++ics.windowGroupCount;
1657  } else {
1658  ++(ics.windowGroupLengths[ics.windowGroupCount - 1]);
1659  }
1660  }
1661  for (byte g = 0; g < ics.windowGroupCount; ++g) {
1662  byte sectionSfb = 0;
1663  uint16 offset = 0, width;
1664  for (byte i = 0; i < ics.swbCount; ++i) {
1665  if (i + 1 == ics.swbCount) {
1666  width = (m_frameLength / 8) - swbOffset128Window[m_mpeg4SamplingFrequencyIndex][i];
1667  } else {
1668  width = swbOffset128Window[m_mpeg4SamplingFrequencyIndex][i + 1] - swbOffset128Window[m_mpeg4SamplingFrequencyIndex][i];
1669  }
1670  width *= ics.windowGroupLengths[g];
1671  ics.sectionSfbOffset[g][sectionSfb++] = offset;
1672  offset += width;
1673  }
1674  ics.sectionSfbOffset[g][sectionSfb] = offset;
1675  }
1676  break;
1677  default:
1678  throw InvalidDataException();
1679  }
1680 }
1681 
1685 void AacFrameElementParser::parseIndividualChannelStream(AacIcsInfo &ics, int16 *specData, bool scaleFlag)
1686 {
1687  parseSideInfo(ics, scaleFlag);
1688  if (m_mpeg4AudioObjectId >= Mpeg4AudioObjectIds::ErAacLc) {
1689  if (ics.tnsDataPresent) {
1690  parseTnsData(ics);
1691  }
1692  }
1693  if (m_mpeg4AudioObjectId == Mpeg4AudioObjectIds::ErParametric) { // DRM stuff?
1694  // TODO: check CRC
1695  throw NotImplementedException();
1696  }
1697  if (m_aacSpectralDataResilienceFlag) {
1698  // TODO: parseReorderedSpectralData(ic);
1699  throw NotImplementedException();
1700  } else {
1701  parseSpectralData(ics, specData);
1702  }
1703  if (ics.pulseDataPresent) {
1704  if (ics.windowSequence == AacIcsSequenceTypes::EightShortSequence) {
1705  throw InvalidDataException(); // pulse coding not allowed for short blocks
1706  } else {
1707  // TODO: reconstruct pulse coding
1708  //decodePulseData(ic);
1709  }
1710  }
1711 }
1712 
1716 void AacFrameElementParser::parseSingleChannelElement()
1717 {
1718  if (m_elementCount + 1 > aacMaxSyntaxElements) {
1719  throw NotImplementedException(); // can not parse frame with more than aacMaxSyntaxElements syntax elements
1720  }
1721  // TODO: check whether limit of channels is exceeded
1722 
1723  int16 specData[1024] = { 0 };
1724  m_elementId[m_elementCount] = AacSyntaxElementTypes::SingleChannelElement;
1725  m_elementInstanceTag[m_elementCount] = m_reader.readBits<byte>(4);
1726  //m_channel = channel;
1727  //m_pairedChannel = -1;
1728  parseIndividualChannelStream(m_ics1, specData);
1729  if (m_ics1.isUsed) {
1730  throw InvalidDataException(); // IS not allowed in single channel
1731  }
1732  // check wheter next bitstream element is a fill element (for SBR decoding)
1733  if (m_reader.showBits<byte>(3) == AacSyntaxElementTypes::FillElement) {
1734  parseFillElement(m_elementCount);
1735  }
1736  // TODO: reconstruct single channel element
1737  // TODO: map output channels position to internal data channels
1738  m_channelCount += m_elementChannelCount[m_elementCount];
1739  ++m_elementCount;
1740 }
1741 
1745 void AacFrameElementParser::parseChannelPairElement()
1746 {
1747  if (m_elementCount + 2 > aacMaxSyntaxElements) {
1748  throw NotImplementedException(); // can not parse frame with more than aacMaxSyntaxElements syntax elements
1749  }
1750  // TODO: check whether limit of channels is exceeded
1751  m_elementId[m_elementCount] = AacSyntaxElementTypes::ChannelPairElement;
1752  m_elementChannelCount[m_elementCount] = 2; // number of output channels in CPE is always 2
1753 
1754  int16 specData1[1024] = { 0 };
1755  int16 specData2[1024] = { 0 };
1756  //m_channel = channels;
1757  //m_pairedChannel = channels + 1;
1758  m_elementInstanceTag[m_elementCount] = m_reader.readBits<byte>(4);
1759  if ((m_commonWindow = m_reader.readBit())) {
1760  // both channels have common ics data
1761  parseIcsInfo(m_ics1);
1762  if ((m_ics1.midSideCodingMaskPresent = m_reader.readBits<byte>(2) == 1)) { // ms mask present
1763  for (byte g = 0; g < m_ics1.windowGroupCount; ++g) {
1764  for (byte sfb = 0; sfb < m_ics1.maxSfb; ++sfb) {
1765  m_ics1.midSideCodingUsed[g][sfb] = m_reader.readBit();
1766  }
1767  }
1768  //m_reader.skipBits(m_ics1.windowGroupCount * m_ics1.maxSfb);
1769  }
1770  if (m_mpeg4AudioObjectId >= Mpeg4AudioObjectIds::ErAacLc && m_ics1.predictorDataPresent) {
1771  if ((m_ics1.ltp1.dataPresent = m_reader.readBit())) {
1772  parseLtpInfo(m_ics1, m_ics1.ltp1);
1773  }
1774  }
1775  m_ics2 = m_ics1;
1776  } else {
1777  m_ics1.midSideCodingMaskPresent = false;
1778  }
1779  parseIndividualChannelStream(m_ics1, specData1);
1780  if (m_commonWindow && m_mpeg4AudioObjectId >= Mpeg4AudioObjectIds::ErAacLc && m_ics1.predictorDataPresent) {
1781  if ((m_ics1.ltp2.dataPresent = m_reader.readBit())) {
1782  parseLtpInfo(m_ics1, m_ics1.ltp2);
1783  }
1784  }
1785  parseIndividualChannelStream(m_ics2, specData2);
1786  // check if next bitstream element is a fill element (for SBR decoding)
1787  if (m_reader.showBits<byte>(3) == AacSyntaxElementTypes::FillElement) {
1788  parseFillElement(m_elementCount);
1789  }
1790  // TODO: reconstruct channel pair
1791  // TODO: map output channels position to internal data channels
1792  m_channelCount += 2;
1793  ++m_elementCount;
1794 }
1795 
1799 void AacFrameElementParser::parseCouplingChannelElement()
1800 {
1801  m_reader.skipBits(4); // element instance tag
1802  byte swCceFlag = m_reader.readBit();
1803  byte coupledElementCount = m_reader.readBits<byte>(3);
1804  byte gainElementLists = 0;
1805  for (byte c = 0; c < coupledElementCount; ++c) {
1806  ++gainElementLists;
1807  byte ccTargetIsCpe = m_reader.readBit();
1808  //byte ccTargetTagSelect = m_reader.readBits<byte>(4);
1809  m_reader.skipBits(4); // cc target tag select
1810  if (ccTargetIsCpe) {
1811  // cc left and right
1812  if (m_reader.readBit() & m_reader.readBit()) {
1813  ++gainElementLists;
1814  }
1815  }
1816  }
1817  m_reader.skipBits(4); // 1 bit cc domain, 1 bit gain element sign, 2 bits gain element scale
1818  AacIcsInfo ics;
1819  int16 specData[1024];
1820  parseIndividualChannelStream(ics, specData);
1821  for (byte c = 1; c < gainElementLists; ++c) {
1822  if (swCceFlag || m_reader.readBit()) {
1823  parseHuffmanScaleFactor();
1824  } else {
1825  for (byte group = 0; group < ics.windowCount; ++group) {
1826  for (byte sfb = 0; sfb < ics.maxSfb; ++sfb) {
1827  if (ics.sfbCb[group][sfb] != AacScaleFactorTypes::ZeroHcb) {
1828  parseHuffmanScaleFactor();
1829  }
1830  }
1831  }
1832  }
1833  }
1834 }
1835 
1839 void AacFrameElementParser::parseLowFrequencyElement()
1840 {
1841  parseSingleChannelElement();
1842 }
1843 
1847 void AacFrameElementParser::parseDataStreamElement()
1848 {
1849  byte byteAligned = m_reader.readBit();
1850  uint16 count = m_reader.readBits<uint16>(8);
1851  if (count == 0xFF) {
1852  count += m_reader.readBits<uint16>(8);
1853  }
1854  if (byteAligned) {
1855  m_reader.align();
1856  }
1857  m_reader.skipBits(count * 8);
1858 }
1859 
1863 void AacFrameElementParser::parseProgramConfigElement()
1864 {
1865  m_pce.elementInstanceTag = m_reader.readBits<byte>(4);
1866  m_pce.objectType = m_reader.readBits<byte>(2);
1867  m_pce.samplingFrequencyIndex = m_reader.readBits<byte>(4);
1868  m_pce.frontChannelElementCount = m_reader.readBits<byte>(4);
1869  m_pce.sideChannelElementCount = m_reader.readBits<byte>(4);
1870  m_pce.backChannelElementCount = m_reader.readBits<byte>(4);
1871  m_pce.lfeChannelElementCount = m_reader.readBits<byte>(2);
1872  m_pce.assocDataElementCount = m_reader.readBits<byte>(3);
1873  m_pce.validCcElementCount = m_reader.readBits<byte>(4);
1874  if ((m_pce.monoMixdownPresent = m_reader.readBit())) {
1875  m_pce.monoMixdownElementNumber = m_reader.readBits<byte>(4);
1876  }
1877  if ((m_pce.stereoMixdownPresent = m_reader.readBit())) {
1878  m_pce.stereoMixdownElementNumber = m_reader.readBits<byte>(4);
1879  }
1880  if ((m_pce.matrixMixdownIdxPresent = m_reader.readBit())) {
1881  m_pce.matrixMixdownIdx = m_reader.readBits<byte>(2);
1882  m_pce.pseudoSurroundEnable = m_reader.readBit();
1883  }
1884  byte i;
1885  for (i = 0; i < m_pce.frontChannelElementCount; ++i) {
1886  m_pce.frontElementIsCpe[i] = m_reader.readBit();
1887  m_pce.frontElementTagSelect[i] = m_reader.readBits<byte>(4);
1888  if (m_pce.frontElementIsCpe[i]) { // channel coupling element
1889  m_pce.cpeChannel[m_pce.frontElementTagSelect[i]] = m_pce.channels;
1890  m_pce.frontChannelCount += 2;
1891  m_pce.channels += 2;
1892  } else { // single channel element
1893  m_pce.sceChannel[m_pce.frontElementTagSelect[i]] = m_pce.channels;
1894  ++m_pce.frontChannelCount;
1895  ++m_pce.channels;
1896  }
1897  }
1898  for (i = 0; i < m_pce.sideChannelElementCount; ++i) {
1899  m_pce.sideElementIsCpe[i] = m_reader.readBit();
1900  m_pce.sideElementTagSelect[i] = m_reader.readBits<byte>(4);
1901  if (m_pce.sideElementIsCpe[i]) { // channel coupling element
1902  m_pce.cpeChannel[m_pce.sideElementTagSelect[i]] = m_pce.channels;
1903  m_pce.sideChannelCount += 2;
1904  m_pce.channels += 2;
1905  } else { // single channel element
1906  m_pce.sceChannel[m_pce.sideElementTagSelect[i]] = m_pce.channels;
1907  ++m_pce.sideChannelCount;
1908  ++m_pce.channels;
1909  }
1910  }
1911  for (i = 0; i < m_pce.backChannelElementCount; ++i) {
1912  m_pce.backElementIsCpe[i] = m_reader.readBit();
1913  m_pce.backElementTagSelect[i] = m_reader.readBits<byte>(4);
1914  if (m_pce.backElementIsCpe[i]) { // channel coupling element
1915  m_pce.cpeChannel[m_pce.backElementTagSelect[i]] = m_pce.channels;
1916  m_pce.backChannelCount += 2;
1917  m_pce.channels += 2;
1918  } else { // single channel element
1919  m_pce.sceChannel[m_pce.backElementTagSelect[i]] = m_pce.channels;
1920  ++m_pce.backChannelCount;
1921  ++m_pce.channels;
1922  }
1923  }
1924  for (i = 0; i < m_pce.lfeChannelElementCount; ++i) {
1925  m_pce.lfeElementTagSelect[i] = m_reader.readBits<byte>(4);
1926  m_pce.sceChannel[m_pce.lfeElementTagSelect[i]] = m_pce.channels;
1927  ++m_pce.lfeChannelCount;
1928  ++m_pce.channels;
1929  }
1930  for (i = 0; i < m_pce.assocDataElementCount; ++i) {
1931  m_pce.assocDataElementTagSelect[i] = m_reader.readBits<byte>(4);
1932  }
1933  for (i = 0; i < m_pce.validCcElementCount; ++i) {
1934  m_pce.ccElementIsIndSw[i] = m_reader.readBit();
1935  m_pce.validCcElementTagSelect[i] = m_reader.readBits<byte>(4);
1936  }
1937  m_reader.align();
1938  m_pce.commentFieldBytes = m_reader.readBits<byte>(8);
1939  for (i = 0; i < m_pce.commentFieldBytes; ++i) {
1940  m_pce.commentFieldData[i] = m_reader.readBits<byte>(8);
1941  }
1942  m_pce.commentFieldData[i] = 0;
1943  if (m_pce.channels > aacMaxChannels) {
1944  throw NotImplementedException(); // supported channel maximum exceeded
1945  }
1946 }
1947 
1951 void AacFrameElementParser::parseFillElement(byte sbrElement)
1952 {
1953  uint16 count = m_reader.readBits<byte>(4);
1954  bool crcFlag = 0;
1955  if (count == 0xF) {
1956  count += m_reader.readBits<byte>(8);
1957  }
1958  while (count > 0) {
1959  continueWhile:
1960  switch (m_reader.readBits<byte>(4)) { // extension type
1961  using namespace AacExtensionTypes;
1962  case DynamicRange:
1963  count -= parseDynamicRange();
1964  break;
1965  case SbrDataCrc:
1966  crcFlag = true;
1967  case SbrData:
1968  if (sbrElement == aacInvalidSbrElement) {
1969  throw InvalidDataException();
1970  } else {
1971  // ensure SBR element exists
1972  if (!m_sbrElements[sbrElement]) {
1973  m_sbrElements[sbrElement] = makeSbrInfo(sbrElement);
1974  }
1975  parseSbrExtensionData(sbrElement, count, crcFlag);
1976  // set global flags
1977  m_sbrPresentFlag = 1;
1978  if (m_sbrElements[sbrElement]->ps) {
1979  m_psUsed[sbrElement] = 1;
1980  m_psUsedGlobal = 1;
1981  }
1982  }
1983  count = 0;
1984  break;
1985  case FillData:
1986  m_reader.skipBits(4 + 8 * (count - 1));
1987  count = 0;
1988  break;
1989  case DataElement:
1990  // data element version
1991  if (m_reader.readBits<byte>(4) == 0) {
1992  // ANC data
1993  byte dataElementLength = 0, loopCounter = 0;
1994  uint16 dataElementLengthPart;
1995  do {
1996  dataElementLengthPart = m_reader.readBits<byte>(8);
1997  dataElementLength += dataElementLengthPart;
1998  ++loopCounter;
1999  } while (dataElementLengthPart == 0xFF);
2000  for (uint16 i = 0; i < dataElementLength; ++i) {
2001  m_reader.skipBits(8); // data element byte
2002  count -= dataElementLength + loopCounter + 1;
2003  goto continueWhile;
2004  }
2005  }
2006  m_reader.skipBits(8 * (count - 1));
2007  count = 0;
2008  case Fill:
2009  case SacData:
2010  default:
2011  m_reader.skipBits(4 + 8 * (count - 1));
2012  count = 0;
2013  }
2014  }
2015 }
2016 
2022 void AacFrameElementParser::parseRawDataBlock()
2023 {
2024  if (m_mpeg4AudioObjectId < Mpeg4AudioObjectIds::ErAacLc) {
2025  for (;;) {
2026  switch (m_reader.readBits<byte>(3)) { // parse element type
2027  using namespace AacSyntaxElementTypes;
2028  case SingleChannelElement:
2029  parseSingleChannelElement();
2030  break;
2031  case ChannelPairElement:
2032  parseChannelPairElement();
2033  break;
2034  case ChannelCouplingElement:
2035  parseCouplingChannelElement();
2036  break;
2037  case LowFrequencyElement:
2038  parseLowFrequencyElement();
2039  break;
2040  case DataStreamElement:
2041  parseDataStreamElement();
2042  break;
2043  case ProgramConfigElement:
2044  parseProgramConfigElement();
2045  break;
2046  case FillElement:
2047  parseFillElement();
2048  break;
2049  case EndOfFrame:
2050  goto endOfBlock;
2051  default:;
2052  }
2053  }
2054  } else { // error resilience
2055  switch (m_mpeg4ChannelConfig) {
2056  using namespace Mpeg4ChannelConfigs;
2057  using namespace AacSyntaxElementTypes;
2058  case FrontCenter:
2059  parseSingleChannelElement();
2060  break;
2061  case FrontLeftFrontRight:
2062  parseChannelPairElement();
2063  break;
2065  parseSingleChannelElement();
2066  parseChannelPairElement();
2067  break;
2069  parseSingleChannelElement();
2070  parseChannelPairElement();
2071  parseSingleChannelElement();
2072  break;
2074  parseSingleChannelElement();
2075  parseChannelPairElement();
2076  parseChannelPairElement();
2077  break;
2079  parseSingleChannelElement();
2080  parseChannelPairElement();
2081  parseChannelPairElement();
2082  parseSingleChannelElement();
2083  break;
2085  parseSingleChannelElement();
2086  parseChannelPairElement();
2087  parseChannelPairElement();
2088  parseChannelPairElement();
2089  parseSingleChannelElement();
2090  break;
2091  }
2092  }
2093 endOfBlock:;
2094 }
2095 
2099 void AacFrameElementParser::parse(const AdtsFrame &adtsFrame, std::istream &stream, std::size_t dataSize)
2100 {
2101  auto data = make_unique<char[]>(dataSize);
2102  stream.read(data.get(), dataSize);
2103  parse(adtsFrame, data, dataSize);
2104 }
2105 
2109 void AacFrameElementParser::parse(const AdtsFrame &adtsFrame, std::unique_ptr<char[]> &data, std::size_t dataSize)
2110 {
2111  m_reader.reset(data.get(), dataSize);
2112  m_mpeg4AudioObjectId = adtsFrame.mpeg4AudioObjectId();
2113  m_mpeg4SamplingFrequencyIndex = adtsFrame.mpeg4SamplingFrequencyIndex();
2114  parseRawDataBlock();
2115 }
2116 
2118 
2119 } // namespace TagParser
STL namespace.
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:408