10#include <c++utilities/io/bitreader.h>
27const std::uint8_t maxPredictionSfb[16] = { 33, 33, 38, 40, 40, 40, 41, 41, 37, 37, 37, 34, 64, 64, 64, 64 };
29const uint8_t swb512WindowCount[] = { 0, 0, 0, 36, 36, 37, 31, 31, 0, 0, 0, 0 };
31const std::uint8_t swb480WindowCount[] = { 0, 0, 0, 35, 35, 37, 30, 30, 0, 0, 0, 0 };
33const std::uint8_t swb960WindowCount[] = { 40, 40, 45, 49, 49, 49, 46, 46, 42, 42, 42, 40 };
35const std::uint8_t swb1024WindowCount[] = { 41, 41, 47, 49, 49, 51, 47, 47, 43, 43, 43, 40 };
37const std::uint8_t swb128WindowCount[] = { 12, 12, 12, 14, 14, 14, 15, 15, 15, 15, 15, 15 };
39const std::uint16_t swbOffset1024_96[] = { 0, 4, 8, 12, 16, 20, 24, 28, 32, 36, 40, 44, 48, 52, 56, 64, 72, 80, 88, 96, 108, 120, 132, 144, 156, 172,
40 188, 212, 240, 276, 320, 384, 448, 512, 576, 640, 704, 768, 832, 896, 960, 1024 };
42const std::uint16_t swbOffset128_96[] = { 0, 4, 8, 12, 16, 20, 24, 32, 40, 48, 64, 92, 128 };
44const std::uint16_t swbOffset1024_64[] = { 0, 4, 8, 12, 16, 20, 24, 28, 32, 36, 40, 44, 48, 52, 56, 64, 72, 80, 88, 100, 112, 124, 140, 156, 172, 192,
45 216, 240, 268, 304, 344, 384, 424, 464, 504, 544, 584, 624, 664, 704, 744, 784, 824, 864, 904, 944, 984, 1024 };
47const std::uint16_t swbOffset128_64[] = { 0, 4, 8, 12, 16, 20, 24, 32, 40, 48, 64, 92, 128 };
49const std::uint16_t swbOffset1024_48[] = { 0, 4, 8, 12, 16, 20, 24, 28, 32, 36, 40, 48, 56, 64, 72, 80, 88, 96, 108, 120, 132, 144, 160, 176, 196,
50 216, 240, 264, 292, 320, 352, 384, 416, 448, 480, 512, 544, 576, 608, 640, 672, 704, 736, 768, 800, 832, 864, 896, 928, 1024 };
52const std::uint16_t swbOffset512_48[] = { 0, 4, 8, 12, 16, 20, 24, 28, 32, 36, 40, 44, 48, 52, 56, 60, 68, 76, 84, 92, 100, 112, 124, 136, 148, 164,
53 184, 208, 236, 268, 300, 332, 364, 396, 428, 460, 512 };
55const std::uint16_t swbOffset480_48[] = { 0, 4, 8, 12, 16, 20, 24, 28, 32, 36, 40, 44, 48, 52, 56, 64, 72, 80, 88, 96, 108, 120, 132, 144, 156, 172,
56 188, 212, 240, 272, 304, 336, 368, 400, 432, 480 };
58const std::uint16_t swbOffset128_48[] = { 0, 4, 8, 12, 16, 20, 28, 36, 44, 56, 68, 80, 96, 112, 128 };
60const std::uint16_t swbOffset1024_32[] = { 0, 4, 8, 12, 16, 20, 24, 28, 32, 36, 40, 48, 56, 64, 72, 80, 88, 96, 108, 120, 132, 144, 160, 176, 196,
61 216, 240, 264, 292, 320, 352, 384, 416, 448, 480, 512, 544, 576, 608, 640, 672, 704, 736, 768, 800, 832, 864, 896, 928, 960, 992, 1024 };
63const std::uint16_t swbOffset512_32[] = { 0, 4, 8, 12, 16, 20, 24, 28, 32, 36, 40, 44, 48, 52, 56, 64, 72, 80, 88, 96, 108, 120, 132, 144, 160, 176,
64 192, 212, 236, 260, 288, 320, 352, 384, 416, 448, 480, 512 };
66const std::uint16_t swbOffset480_32[] = { 0, 4, 8, 12, 16, 20, 24, 28, 32, 36, 40, 44, 48, 52, 56, 60, 64, 72, 80, 88, 96, 104, 112, 124, 136, 148,
67 164, 180, 200, 224, 256, 288, 320, 352, 384, 416, 448, 480 };
69const std::uint16_t swbOffset1024_24[] = { 0, 4, 8, 12, 16, 20, 24, 28, 32, 36, 40, 44, 52, 60, 68, 76, 84, 92, 100, 108, 116, 124, 136, 148, 160,
70 172, 188, 204, 220, 240, 260, 284, 308, 336, 364, 396, 432, 468, 508, 552, 600, 652, 704, 768, 832, 896, 960, 1024 };
72const std::uint16_t swbOffset512_24[]
73 = { 0, 4, 8, 12, 16, 20, 24, 28, 32, 36, 40, 44, 52, 60, 68, 80, 92, 104, 120, 140, 164, 192, 224, 256, 288, 320, 352, 384, 416, 448, 480, 512 };
75const std::uint16_t swbOffset480_24[]
76 = { 0, 4, 8, 12, 16, 20, 24, 28, 32, 36, 40, 44, 52, 60, 68, 80, 92, 104, 120, 140, 164, 192, 224, 256, 288, 320, 352, 384, 416, 448, 480 };
78const std::uint16_t swbOffset128_24[] = { 0, 4, 8, 12, 16, 20, 24, 28, 36, 44, 52, 64, 76, 92, 108, 128 };
80const std::uint16_t swbOffset1024_16[] = { 0, 8, 16, 24, 32, 40, 48, 56, 64, 72, 80, 88, 100, 112, 124, 136, 148, 160, 172, 184, 196, 212, 228, 244,
81 260, 280, 300, 320, 344, 368, 396, 424, 456, 492, 532, 572, 616, 664, 716, 772, 832, 896, 960, 1024 };
83const std::uint16_t swbOffset128_16[] = { 0, 4, 8, 12, 16, 20, 24, 28, 32, 40, 48, 60, 72, 88, 108, 128 };
85const std::uint16_t swbOffset1024_8[] = { 0, 12, 24, 36, 48, 60, 72, 84, 96, 108, 120, 132, 144, 156, 172, 188, 204, 220, 236, 252, 268, 288, 308,
86 328, 348, 372, 396, 420, 448, 476, 508, 544, 580, 620, 664, 712, 764, 820, 880, 944, 1024 };
88const std::uint16_t swbOffset128_8[] = { 0, 4, 8, 12, 16, 20, 24, 28, 36, 44, 52, 60, 72, 88, 108, 128 };
90static const std::uint16_t *
const swbOffset1024Window[] = {
105static const std::uint16_t *
const swbOffset512Window[] = {
120static const std::uint16_t *
const swbOffset480Window[] = {
135static const std::uint16_t *
const swbOffset128Window[] = {
153AacLtpInfo::AacLtpInfo()
161 , shortLagPresent{ 0 }
169AacPredictorInfo::AacPredictorInfo()
172 , resetGroupNumber(0)
173 , predictionUsed{ 0 }
180AacPulseInfo::AacPulseInfo()
191AacTnsInfo::AacTnsInfo()
197 , coefCompress{ { 0 } }
205AacSsrInfo::AacSsrInfo()
208 , alevcode{ { { 0 } } }
209 , aloccode{ { { 0 } } }
216AacDrcInfo::AacDrcInfo()
220 , excludedChannelsPresent(0)
223 , dynamicRangeSign{ 0 }
224 , dynamicRangeControl{ 0 }
226 , additionalExcludedChannels{ 0 }
233AacPsInfo::AacPsInfo()
240AacDrmPsInfo::AacDrmPsInfo()
247AacSbrInfo::AacSbrInfo(std::uint8_t sbrElementType, std::uint16_t samplingFrequency, std::uint16_t frameLength,
bool isDrm)
248 : aacElementId(sbrElementType)
249 , samplingFrequency(samplingFrequency)
323 , bsInvfModePrev{ { 0 } }
329 , patchNoSubbands{ 0 }
330 , patchStartSubband{ 0 }
333 bsAddHarmonic{ { 0 } }
334 , bsAddHarmonicPrev{ { 0 } }
341 bsStartFreqPrev(numeric_limits<std::uint8_t>::max())
345 , bsAlterScalePrev(0)
346 , bsNoiseBandsPrev(0)
349 prevEnvIsShort{ -1, -1 }
356 , frameLength(frameLength)
375 timeSlotsRateCount(aacSbrRate * (frameLength == 960 ? aacNoTimeSlots960 : aacNoTimeSlots))
376 , timeSlotsCount(frameLength == 960 ? aacNoTimeSlots960 : aacNoTimeSlots)
388 , bsProtocolVersion(0)
400 , bsSamplerateMode(1)
401 , bsAddHarmonicFlag{ 0 }
402 , bsAddHarmonicFlagPrev{ 0 }
409 , bsRelBord0{ { 0 } }
410 , bsRelBord1{ { 0 } }
419 throw NotImplementedException();
434AacProgramConfig::AacProgramConfig()
435 : elementInstanceTag(0)
437 , samplingFrequencyIndex(0)
438 , frontChannelElementCount(0)
439 , sideChannelElementCount(0)
440 , backChannelElementCount(0)
441 , lfeChannelElementCount(0)
442 , assocDataElementCount(0)
443 , validCcElementCount(0)
444 , monoMixdownPresent(0)
445 , monoMixdownElementNumber(0)
446 , stereoMixdownPresent(0)
447 , stereoMixdownElementNumber(0)
448 , matrixMixdownIdxPresent(0)
449 , pseudoSurroundEnable(0)
450 , matrixMixdownIdx(0)
451 , frontElementIsCpe{ 0 }
452 , frontElementTagSelect{ 0 }
453 , sideElementIsCpe{ 0 }
454 , sideElementTagSelect{ 0 }
455 , backElementIsCpe{ 0 }
456 , backElementTagSelect{ 0 }
457 , lfeElementTagSelect{ 0 }
458 , assocDataElementTagSelect{ 0 }
459 , ccElementIsIndSw{ 0 }
460 , validCcElementTagSelect{ 0 }
462 , commentFieldBytes(0)
463 , commentFieldData{ 0 }
464 , frontChannelCount(0)
465 , sideChannelCount(0)
466 , backChannelCount(0)
476AacIcsInfo::AacIcsInfo()
479 , windowGroupCount(0)
482 , windowGroupLengths{ 0 }
484 , scaleFactorGrouping(0)
485 , sectionSfbOffset{ { 0 } }
489 , sectionStart{ { 0 } }
490 , sectionEnd{ { 0 } }
492 , sectionsPerGroup{ 0 }
494 , scaleFactors{ { 0 } }
495 , midSideCodingMaskPresent(0)
496 , midSideCodingUsed{ { 0 } }
499 , pulseDataPresent(0)
501 , gainControlPresent(0)
502 , predictorDataPresent(0)
503 , reorderedSpectralDataLength(0)
504 , longestCodewordLength(0)
509 , sfEscapesPresent(0)
510 , rvlcEscapesLength(0)
511 , dpcmNoiseLastPos(0)
526void AacFrameElementParser::parseLtpInfo(
const AacIcsInfo &ics, AacLtpInfo <p)
529 if (m_mpeg4AudioObjectId == Mpeg4AudioObjectIds::ErAacLd) {
530 if ((ltp.lagUpdate = m_reader.readBit())) {
531 ltp.lag = m_reader.readBits<std::uint16_t>(10);
534 ltp.lag = m_reader.readBits<std::uint16_t>(11);
536 if (ltp.lag > (m_frameLength << 1)) {
537 throw InvalidDataException();
539 ltp.coef = m_reader.readBits<std::uint8_t>(3);
540 switch (ics.windowSequence) {
541 case AacIcsSequenceTypes::EightShortSequence:
542 for (std::uint8_t w = 0; w < ics.windowCount; ++w) {
543 if ((ltp.shortUsed[w] = m_reader.readBit())) {
544 if ((ltp.shortLagPresent[w] = m_reader.readBit())) {
545 ltp.shortLag[w] = m_reader.readBits<std::uint8_t>(4);
551 ltp.lastBand = std::min<std::uint8_t>(ics.maxSfb, aacMaxLtpSfb);
552 for (std::uint8_t sfb = 0; sfb < ltp.lastBand; ++sfb) {
553 ltp.longUsed[sfb] = m_reader.readBit();
561void AacFrameElementParser::parseIcsInfo(AacIcsInfo &ics)
563 using namespace AacIcsSequenceTypes;
564 if (m_reader.readBit()) {
565 throw InvalidDataException();
567 ics.windowSequence = m_reader.readBits<std::uint8_t>(2);
568 ics.windowShape = m_reader.readBit();
569 if (m_mpeg4AudioObjectId == Mpeg4AudioObjectIds::ErAacLd && ics.windowSequence != OnlyLongSequence) {
570 throw InvalidDataException();
572 if (ics.windowSequence == EightShortSequence) {
573 ics.maxSfb = m_reader.readBits<std::uint8_t>(4);
574 ics.scaleFactorGrouping = m_reader.readBits<std::uint8_t>(7);
576 ics.maxSfb = m_reader.readBits<std::uint8_t>(6);
578 calculateWindowGroupingInfo(ics);
579 if (ics.windowSequence != EightShortSequence) {
580 if ((ics.predictorDataPresent = m_reader.readBit())) {
581 switch (m_mpeg4AudioObjectId) {
582 case Mpeg4AudioObjectIds::AacMain:
584 if ((ics.predictor.reset = m_reader.readBit())) {
585 ics.predictor.resetGroupNumber = m_reader.readBits<std::uint8_t>(5);
586 ics.predictor.maxSfb = ics.maxSfb;
588 if (ics.predictor.maxSfb > maxPredictionSfb[m_mpeg4SamplingFrequencyIndex]) {
589 ics.predictor.maxSfb = maxPredictionSfb[m_mpeg4SamplingFrequencyIndex];
591 for (std::uint8_t sfb = 0; sfb < ics.predictor.maxSfb; ++sfb) {
592 ics.predictor.predictionUsed[sfb] = m_reader.readBit();
597 if (m_mpeg4AudioObjectId < Mpeg4AudioObjectIds::ErAacLc) {
598 if ((ics.ltp1.dataPresent = m_reader.readBit())) {
599 parseLtpInfo(ics, ics.ltp1);
601 if (m_commonWindow) {
602 if ((ics.ltp2.dataPresent = m_reader.readBit())) {
603 parseLtpInfo(ics, ics.ltp2);
607 if (!m_commonWindow && (m_mpeg4AudioObjectId >= Mpeg4AudioObjectIds::ErAacLc)) {
608 if ((ics.ltp1.dataPresent = m_reader.readBit())) {
609 parseLtpInfo(ics, ics.ltp1);
620void AacFrameElementParser::parseSectionData(AacIcsInfo &ics)
622 const std::uint8_t sectionBits = ics.windowSequence == AacIcsSequenceTypes::EightShortSequence ? 3 : 5;
623 const std::uint8_t sectionEscValue =
static_cast<std::uint8_t
>((1 << sectionBits) - 1);
624 for (std::uint8_t groupIndex = 0, sectionIndex = 0; groupIndex < ics.windowGroupCount; ++groupIndex, sectionIndex = 0) {
625 for (std::uint8_t i = 0, sectionLength, sectionLengthIncrease; i < ics.maxSfb; i += sectionLength, ++sectionIndex) {
626 ics.sectionCb[groupIndex][sectionIndex] = m_reader.readBits<std::uint8_t>(m_aacSectionDataResilienceFlag ? 5 : 4);
628 sectionLengthIncrease
629 = (!m_aacSectionDataResilienceFlag && (ics.sectionCb[groupIndex][sectionIndex] < 16 || ics.sectionCb[groupIndex][sectionIndex] > 32)
630 && ics.sectionCb[groupIndex][sectionIndex] != 11)
631 ? m_reader.readBits<std::uint8_t>(sectionBits)
633 while (sectionLengthIncrease == sectionEscValue) {
634 sectionLength += sectionLengthIncrease;
635 sectionLengthIncrease = m_reader.readBits<std::uint8_t>(sectionBits);
637 sectionLength += sectionLengthIncrease;
638 ics.sectionStart[groupIndex][sectionIndex] = i;
639 ics.sectionEnd[groupIndex][sectionIndex] = i + sectionLength;
640 if (ics.windowSequence == AacIcsSequenceTypes::EightShortSequence) {
641 if (i + sectionLength > 8 * 15) {
642 throw InvalidDataException();
643 }
else if (sectionIndex >= 8 * 15) {
644 throw InvalidDataException();
647 if (i + sectionLength > aacMaxSfb) {
648 throw InvalidDataException();
649 }
else if (sectionIndex >= aacMaxSfb) {
650 throw InvalidDataException();
653 for (std::uint8_t sfb = i; sfb < i + sectionLength; ++sfb) {
654 ics.sfbCb[groupIndex][sfb] = ics.sectionCb[groupIndex][sectionIndex];
657 ics.sectionsPerGroup[groupIndex] = sectionIndex;
664void AacFrameElementParser::decodeScaleFactorData(AacIcsInfo &ics)
667 std::uint8_t noisePcmFlag = 1;
668 std::int16_t scaleFactor = ics.globalGain;
669 std::int16_t isPosition = 0;
670 std::int16_t noiseEnergy = ics.globalGain - 90;
671 using namespace AacScaleFactorTypes;
672 for (std::uint8_t group = 0; group < ics.windowGroupCount; ++group) {
673 for (std::uint8_t sfb = 0; sfb < ics.maxSfb; ++sfb) {
674 switch (ics.sfbCb[group][sfb]) {
676 ics.scaleFactors[group][sfb] = 0;
680 ics.scaleFactors[group][sfb] =
static_cast<std::uint16_t
>(isPosition +=
static_cast<std::int16_t
>(parseHuffmanScaleFactor() - 60));
685 tmp = m_reader.readBits<std::int16_t>(9);
687 tmp = parseHuffmanScaleFactor() - 60;
689 ics.scaleFactors[group][sfb] =
static_cast<std::uint16_t
>(noiseEnergy += tmp);
692 scaleFactor +=
static_cast<std::int16_t
>(parseHuffmanScaleFactor() - 60);
693 if (scaleFactor < 0 || scaleFactor > 255) {
694 throw InvalidDataException();
696 ics.scaleFactors[group][sfb] = 0;
706void AacFrameElementParser::decodeRvlcScaleFactorData(AacIcsInfo &ics)
708 if (ics.rvlcSfLength) {
709 m_reader.skipBits(ics.rvlcSfLength);
711 if (ics.sfEscapesPresent) {
712 m_reader.skipBits(ics.rvlcEscapesLength);
720void AacFrameElementParser::parseScaleFactorData(AacIcsInfo &ics)
722 if (!m_aacScalefactorDataResilienceFlag) {
723 decodeScaleFactorData(ics);
725 decodeRvlcScaleFactorData(ics);
732void AacFrameElementParser::parsePulseData(AacIcsInfo &ics)
734 AacPulseInfo &p = ics.pulse;
735 p.count = m_reader.readBits<std::uint8_t>(2);
736 p.startSfb = m_reader.readBits<std::uint8_t>(6);
737 if (p.startSfb > ics.swbCount) {
738 throw InvalidDataException();
740 for (std::uint8_t i = 0; i <= p.count; ++i) {
741 p.offset[i] = m_reader.readBits<std::uint8_t>(5);
742 p.amp[i] = m_reader.readBits<std::uint8_t>(4);
749void AacFrameElementParser::parseTnsData(AacIcsInfo &ics)
751 std::uint8_t filtBits, lengthBits, orderBits, startCoefBits, coefBits;
752 if (ics.windowSequence == AacIcsSequenceTypes::EightShortSequence) {
761 for (std::uint8_t window = 0; window < ics.windowCount; ++window) {
762 if ((ics.tns.filt[window] = m_reader.readBits<std::uint8_t>(filtBits))) {
763 startCoefBits = (ics.tns.coefRes[window] = m_reader.readBit()) ? 4 : 3;
765 for (std::uint8_t filt = 0; filt < ics.tns.filt[window]; ++filt) {
766 ics.tns.length[window][filt] = m_reader.readBits<std::uint8_t>(lengthBits);
767 if ((ics.tns.order[window][filt] = m_reader.readBits<std::uint8_t>(orderBits))) {
768 ics.tns.direction[window][filt] = m_reader.readBit();
769 ics.tns.coefCompress[window][filt] = m_reader.readBit();
770 coefBits = startCoefBits - ics.tns.coefCompress[window][filt];
771 for (std::uint8_t i = 0; i < ics.tns.order[window][filt]; ++i) {
772 ics.tns.coef[window][filt][i] = m_reader.readBits<std::uint8_t>(coefBits);
782void AacFrameElementParser::parseGainControlData(AacIcsInfo &ics)
784 AacSsrInfo &ssr = ics.ssr;
785 ssr.maxBand = m_reader.readBits<std::uint8_t>(2);
786 switch (ics.windowSequence) {
787 using namespace AacIcsSequenceTypes;
788 case OnlyLongSequence:
789 for (std::uint8_t bd = 1; bd <= ssr.maxBand; ++bd) {
790 for (std::uint8_t wd = 0; wd < 1; ++wd) {
791 ssr.adjustNum[bd][wd] = m_reader.readBits<std::uint8_t>(3);
792 for (std::uint8_t ad = 0; ad < ssr.adjustNum[bd][wd]; ++ad) {
793 ssr.alevcode[bd][wd][ad] = m_reader.readBits<std::uint8_t>(4);
794 ssr.aloccode[bd][wd][ad] = m_reader.readBits<std::uint8_t>(5);
799 case LongStartSequence:
800 for (std::uint8_t bd = 1; bd <= ssr.maxBand; ++bd) {
801 for (std::uint8_t wd = 0; wd < 2; ++wd) {
802 ssr.adjustNum[bd][wd] = m_reader.readBits<std::uint8_t>(3);
803 for (std::uint8_t ad = 0; ad < ssr.adjustNum[bd][wd]; ++ad) {
804 ssr.alevcode[bd][wd][ad] = m_reader.readBits<std::uint8_t>(4);
805 ssr.aloccode[bd][wd][ad] = m_reader.readBits<std::uint8_t>(wd ? 2 : 4);
810 case EightShortSequence:
811 for (std::uint8_t bd = 1; bd <= ssr.maxBand; ++bd) {
812 for (std::uint8_t wd = 0; wd < 8; ++wd) {
813 ssr.adjustNum[bd][wd] = m_reader.readBits<std::uint8_t>(3);
814 for (std::uint8_t ad = 0; ad < ssr.adjustNum[bd][wd]; ++ad) {
815 ssr.alevcode[bd][wd][ad] = m_reader.readBits<std::uint8_t>(4);
816 ssr.aloccode[bd][wd][ad] = m_reader.readBits<std::uint8_t>(2);
821 case LongStopSequence:
822 for (std::uint8_t bd = 1; bd <= ssr.maxBand; ++bd) {
823 for (std::uint8_t wd = 0; wd < 2; ++wd) {
824 ssr.adjustNum[bd][wd] = m_reader.readBits<std::uint8_t>(3);
825 for (std::uint8_t ad = 0; ad < ssr.adjustNum[bd][wd]; ++ad) {
826 ssr.alevcode[bd][wd][ad] = m_reader.readBits<std::uint8_t>(4);
827 ssr.aloccode[bd][wd][ad] = m_reader.readBits<std::uint8_t>(wd ? 5 : 4);
838void AacFrameElementParser::parseSpectralData(AacIcsInfo &ics, std::int16_t *specData)
842 for (std::uint8_t group = 0; group < ics.windowGroupCount; ++group) {
844 for (std::uint8_t section = 0; section < ics.sectionsPerGroup[group]; ++section) {
845 using namespace AacScaleFactorTypes;
846 std::uint8_t sectionCb = ics.sectionCb[group][section];
847 std::uint16_t increment = (sectionCb >= FirstPairHcb) ? 2 : 4;
856 for (std::uint16_t k = ics.sectionSfbOffset[group][ics.sectionStart[group][section]];
857 k < ics.sectionSfbOffset[group][ics.sectionEnd[group][section]]; k += increment) {
858 parseHuffmanSpectralData(sectionCb, specData);
870void AacFrameElementParser::parseSideInfo(AacIcsInfo &ics,
bool scaleFlag)
872 ics.globalGain = m_reader.readBits<std::uint8_t>(8);
873 if (!m_commonWindow && !scaleFlag) {
876 parseSectionData(ics);
877 parseScaleFactorData(ics);
879 if ((ics.pulseDataPresent = m_reader.readBit())) {
882 if ((ics.tnsDataPresent = m_reader.readBit())) {
885 if ((ics.gainControlPresent = m_reader.readBit())) {
886 if (m_mpeg4AudioObjectId != Mpeg4AudioObjectIds::AacSsr) {
887 throw InvalidDataException();
889 parseGainControlData(ics);
893 if (m_aacScalefactorDataResilienceFlag) {
894 decodeRvlcScaleFactorData(ics);
898std::uint8_t AacFrameElementParser::parseExcludedChannels()
900 for (std::uint8_t i = 0; i < 7; ++i) {
901 m_drc.excludeMask[i] = m_reader.readBit();
903 std::uint8_t size = 0;
904 for (; (m_drc.additionalExcludedChannels[size] = m_reader.readBit()); ++size) {
905 for (std::uint8_t i = 0; i < 7; ++i) {
906 m_drc.excludeMask[i] = m_reader.readBit();
912std::uint8_t AacFrameElementParser::parseDynamicRange()
914 std::uint8_t size = 1;
916 if (m_reader.readBit()) {
917 m_drc.pceInstanceTag = m_reader.readBits<std::uint8_t>(4);
918 m_reader.skipBits(4);
921 if ((m_drc.excludedChannelsPresent = m_reader.readBit())) {
922 size += parseExcludedChannels();
924 if (m_reader.readBit()) {
925 m_drc.bandCount += m_reader.readBits<std::uint8_t>(4);
926 m_reader.skipBits(4);
928 for (std::uint8_t i = 0; i < m_drc.bandCount; ++i, ++size) {
929 m_drc.bandTop[i] = m_reader.readBits<std::uint8_t>(8);
932 if (m_reader.readBit()) {
933 m_drc.progRefLevel = m_reader.readBits<std::uint8_t>(7);
934 m_reader.skipBits(1);
937 for (std::uint8_t i = 0; i < m_drc.bandCount; ++i) {
938 m_drc.dynamicRangeSign[i] = m_reader.readBit();
939 m_drc.dynamicRangeControl[i] = m_reader.readBits<std::uint8_t>(7);
945std::int16_t AacFrameElementParser::sbrHuffmanDec(SbrHuffTab table)
948 std::int16_t index = 0;
950 bit = m_reader.readBit();
951 index = table[index][bit];
956void AacFrameElementParser::parseSbrGrid(std::shared_ptr<AacSbrInfo> &sbr, std::uint8_t channel)
958 std::uint8_t tmp, bsEnvCount = 0;
960 switch ((sbr->bsFrameClass[channel] = m_reader.readBits<std::uint8_t>(2))) {
961 using namespace BsFrameClasses;
963 tmp = m_reader.readBits<std::uint8_t>(2);
964 sbr->absBordLead[channel] = 0;
965 sbr->absBordTrail[channel] = sbr->timeSlotsCount;
966 sbr->relLeadCount[channel] = (bsEnvCount = min<std::uint8_t>(
static_cast<std::uint8_t
>(1 << tmp), 5)) - 1;
967 sbr->relTrailCount[channel] = 0;
968 tmp = m_reader.readBit();
969 for (std::uint8_t env = 0; env < bsEnvCount; ++env) {
970 sbr->f[channel][env] = tmp;
974 sbr->absBordLead[channel] = 0;
975 sbr->absBordTrail[channel] = m_reader.readBits<std::uint8_t>(2) + sbr->timeSlotsCount;
976 sbr->relLeadCount[channel] = 0;
977 sbr->relTrailCount[channel] = bsEnvCount = m_reader.readBits<std::uint8_t>(2);
978 for (std::uint8_t rel = 0; rel < bsEnvCount; ++rel) {
979 sbr->bsRelBord[channel][rel] =
static_cast<std::uint8_t
>(2 * m_reader.readBits<std::uint8_t>(2) + 2);
981 sbr->bsPointer[channel] = m_reader.readBits<std::uint8_t>(
static_cast<std::uint8_t
>(sbrLog2(
static_cast<std::int8_t
>(bsEnvCount + 2))));
982 for (std::uint8_t env = 0; env <= bsEnvCount; ++env) {
983 sbr->f[channel][bsEnvCount - env] = m_reader.readBit();
987 sbr->absBordLead[channel] = m_reader.readBits<std::uint8_t>(2);
988 sbr->absBordTrail[channel] = sbr->timeSlotsCount;
989 sbr->relLeadCount[channel] = bsEnvCount = m_reader.readBits<std::uint8_t>(2);
990 sbr->relTrailCount[channel] = 0;
991 for (std::uint8_t rel = 0; rel < bsEnvCount; ++rel) {
992 sbr->bsRelBord[channel][rel] =
static_cast<std::uint8_t
>(2 * m_reader.readBits<std::uint8_t>(2) + 2);
994 sbr->bsPointer[channel] = m_reader.readBits<std::uint8_t>(
static_cast<std::uint8_t
>(sbrLog2(
static_cast<std::int8_t
>(bsEnvCount + 2))));
995 for (std::uint8_t env = 0; env < bsEnvCount; ++env) {
996 sbr->f[channel][env] = m_reader.readBit();
1000 sbr->absBordLead[channel] = m_reader.readBits<std::uint8_t>(2);
1001 sbr->absBordTrail[channel] = m_reader.readBits<std::uint8_t>(2) + sbr->timeSlotsCount;
1004 m_reader.skipBits(4);
1005 bsEnvCount = min<std::uint8_t>(5,
static_cast<std::uint8_t
>(sbr->bsRelCount0[channel] + sbr->bsRelCount1[channel] + 1));
1006 for (std::uint8_t rel = 0; rel < sbr->bsRelCount0[channel]; ++rel) {
1007 sbr->bsRelBord0[channel][rel] =
static_cast<std::uint8_t
>(2 * m_reader.readBits<std::uint8_t>(2) + 2);
1009 for (std::uint8_t rel = 0; rel < sbr->bsRelCount1[channel]; ++rel) {
1010 sbr->bsRelBord1[channel][rel] =
static_cast<std::uint8_t
>(2 * m_reader.readBits<std::uint8_t>(2) + 2);
1012 sbr->bsPointer[channel] = m_reader.readBits<std::uint8_t>(
1013 static_cast<std::uint8_t
>(sbrLog2(
static_cast<std::int8_t
>(sbr->bsRelCount0[channel] + sbr->bsRelCount1[channel] + 2))));
1014 for (std::uint8_t env = 0; env < bsEnvCount; ++env) {
1015 sbr->f[channel][env] = m_reader.readBit();
1017 sbr->relLeadCount[channel] = sbr->bsRelCount0[channel];
1018 sbr->relTrailCount[channel] = sbr->bsRelCount1[channel];
1022 if ((sbr->le[channel] = min<std::uint8_t>(bsEnvCount, sbr->bsFrameClass[channel] == BsFrameClasses::VarVar ? 5 : 4)) <= 0) {
1023 throw InvalidDataException();
1025 sbr->lq[channel] = sbr->le[channel] > 1 ? 2 : 1;
1029void AacFrameElementParser::parseSbrDtdf(std::shared_ptr<AacSbrInfo> &sbr, std::uint8_t channel)
1031 for (std::uint8_t i = 0; i < sbr->le[channel]; ++i) {
1032 sbr->bsDfEnv[channel][i] = m_reader.readBit();
1034 for (std::uint8_t i = 0; i < sbr->lq[channel]; ++i) {
1035 sbr->bsDfNoise[channel][i] = m_reader.readBit();
1039void AacFrameElementParser::parseInvfMode(std::shared_ptr<AacSbrInfo> &sbr, std::uint8_t channel)
1041 for (std::uint8_t i = 0; i < sbr->nq; ++i) {
1042 sbr->bsInvfMode[channel][i] = m_reader.readBits<std::uint8_t>(2);
1046void AacFrameElementParser::parseSbrEnvelope(std::shared_ptr<AacSbrInfo> &sbr, std::uint8_t channel)
1051 if ((sbr->le[channel] == 1) && (sbr->bsFrameClass[channel] == BsFrameClasses::FixFix)) {
1052 sbr->ampRes[channel] = 0;
1054 sbr->ampRes[channel] = sbr->bsAmpRes;
1056 if ((sbr->bsCoupling) && (channel == 1)) {
1058 if (sbr->ampRes[channel]) {
1060 fHuff = fHuffmanEnvBal30dB;
1063 fHuff = fHuffmanEnvBal15dB;
1067 if (sbr->ampRes[channel]) {
1069 fHuff = fHuffmanEnv30dB;
1072 fHuff = fHuffmanEnv15dB;
1075 for (std::uint8_t env = 0; env < sbr->le[channel]; ++env) {
1076 if (sbr->bsDfEnv[channel][env] == 0) {
1077 if ((sbr->bsCoupling == 1) && (channel == 1)) {
1078 if (sbr->ampRes[channel]) {
1079 sbr->e[channel][0][env] =
static_cast<std::int16_t
>(m_reader.readBits<std::uint16_t>(5) << delta);
1081 sbr->e[channel][0][env] =
static_cast<std::int16_t
>(m_reader.readBits<std::uint16_t>(6) << delta);
1084 if (sbr->ampRes[channel]) {
1085 sbr->e[channel][0][env] =
static_cast<std::int16_t
>(m_reader.readBits<std::uint16_t>(6) << delta);
1087 sbr->e[channel][0][env] =
static_cast<std::int16_t
>(m_reader.readBits<std::uint16_t>(7) << delta);
1090 for (std::uint8_t band = 1; band < sbr->n[sbr->f[channel][env]]; ++band) {
1091 sbr->e[channel][band][env] =
static_cast<std::int16_t
>(sbrHuffmanDec(fHuff) << delta);
1094 for (std::uint8_t band = 0; band < sbr->n[sbr->f[channel][env]]; ++band) {
1095 sbr->e[channel][band][env] =
static_cast<std::int16_t
>(sbrHuffmanDec(fHuff) << delta);
1102void AacFrameElementParser::parseSbrNoise(std::shared_ptr<AacSbrInfo> &sbr, std::uint8_t channel)
1107 if ((sbr->bsCoupling == 1) && (channel == 1)) {
1110 fHuff = fHuffmanEnvBal30dB;
1114 fHuff = fHuffmanEnv30dB;
1116 for (std::uint8_t noise = 0; noise < sbr->lq[channel]; ++noise) {
1117 if (sbr->bsDfNoise[channel][noise] == 0) {
1118 if ((sbr->bsCoupling == 1) && (channel == 1)) {
1119 sbr->q[channel][0][noise] = m_reader.readBits<std::uint8_t>(5) << delta;
1121 sbr->q[channel][0][noise] = m_reader.readBits<std::uint8_t>(5) << delta;
1123 for (std::uint8_t band = 1; band < sbr->nq; ++band) {
1124 sbr->q[channel][band][noise] = sbrHuffmanDec(fHuff) << delta;
1127 for (std::uint8_t band = 0; band < sbr->nq; ++band) {
1128 sbr->q[channel][band][noise] = sbrHuffmanDec(fHuff) << delta;
1135void AacFrameElementParser::parseSbrSinusoidalCoding(std::shared_ptr<AacSbrInfo> &sbr, std::uint8_t channel)
1137 for (std::uint8_t i = 0; i < sbr->nHigh; ++i) {
1138 sbr->bsAddHarmonic[channel][i] = m_reader.readBit();
1142std::uint16_t AacFrameElementParser::parseSbrExtension(std::shared_ptr<AacSbrInfo> &sbr, std::uint8_t extensionId, std::uint8_t)
1144 std::uint8_t header;
1146 switch (extensionId) {
1147 using namespace AacSbrExtensionIds;
1149 if (sbr->psResetFlag) {
1150 sbr->ps->headerRead = 0;
1152 res = parsePsData(sbr->ps, header);
1153 if (sbr->psUsed == 0 && header == 1) {
1157 sbr->psResetFlag = 0;
1160 case DrmParametricStereo:
1162 return parseDrmPsData(sbr->drmPs);
1164 sbr->bsExtendedData = m_reader.readBits<std::uint8_t>(6);
1169std::uint16_t AacFrameElementParser::parsePsData(std::shared_ptr<AacPsInfo> &ps, std::uint8_t &header)
1171 if (m_reader.readBit()) {
1174 ps->use34HybridBands = 0;
1175 if ((ps->enableIID = m_reader.readBit())) {
1176 ps->iidMode = m_reader.readBits<std::uint8_t>(3);
1179 throw NotImplementedException();
1182std::uint16_t AacFrameElementParser::parseDrmPsData(std::shared_ptr<AacDrmPsInfo> &drmPs)
1184 CPP_UTILITIES_UNUSED(drmPs)
1185 throw NotImplementedException();
1188void AacFrameElementParser::parseSbrSingleChannelElement(std::shared_ptr<AacSbrInfo> &sbr)
1190 if (m_reader.readBit()) {
1191 m_reader.skipBits(4);
1193 if (sbr->isDrmSbr) {
1194 m_reader.skipBits(1);
1196 parseSbrGrid(sbr, 0);
1197 parseSbrDtdf(sbr, 0);
1198 parseInvfMode(sbr, 0);
1199 parseSbrEnvelope(sbr, 0);
1200 parseSbrNoise(sbr, 0);
1202 if ((sbr->bsAddHarmonicFlag[0] = m_reader.readBit())) {
1203 parseSbrSinusoidalCoding(sbr, 0);
1205 if ((sbr->bsExtendedData = m_reader.readBit())) {
1206 std::uint16_t cnt = m_reader.readBits<std::uint16_t>(4);
1208 cnt += m_reader.readBits<std::uint16_t>(8);
1210 std::uint16_t bitsLeft = 8 * cnt;
1211 while (bitsLeft > 7) {
1212 sbr->bsExtensionId = m_reader.readBits<std::uint8_t>(2);
1213 std::uint16_t tmpBitCount = 2 + parseSbrExtension(sbr, sbr->bsExtensionId,
static_cast<std::uint8_t
>(bitsLeft));
1214 if (tmpBitCount > bitsLeft) {
1215 throw InvalidDataException();
1217 bitsLeft -= tmpBitCount;
1221 m_reader.skipBits(bitsLeft);
1226void AacFrameElementParser::parseSbrChannelPairElement(std::shared_ptr<AacSbrInfo> &sbr)
1228 if (m_reader.readBit()) {
1229 m_reader.skipBits(8);
1231 if ((sbr->bsCoupling = m_reader.readBit())) {
1232 parseSbrGrid(sbr, 0);
1234 sbr->bsFrameClass[1] = sbr->bsFrameClass[0];
1235 sbr->le[1] = sbr->le[0];
1236 sbr->lq[1] = sbr->lq[0];
1237 sbr->bsPointer[1] = sbr->bsPointer[0];
1238 for (std::uint8_t n = 0; n < sbr->le[0]; ++n) {
1239 sbr->te[1][n] = sbr->te[0][n];
1240 sbr->f[1][n] = sbr->f[0][n];
1242 for (std::uint8_t n = 0; n < sbr->lq[0]; ++n) {
1243 sbr->tq[1][n] = sbr->tq[0][n];
1245 parseSbrDtdf(sbr, 0);
1246 parseSbrDtdf(sbr, 1);
1247 parseInvfMode(sbr, 0);
1248 for (std::uint8_t n = 0; n < sbr->nq; ++n) {
1249 sbr->bsInvfMode[1][n] = sbr->bsInvfMode[0][n];
1251 parseSbrEnvelope(sbr, 0);
1252 parseSbrNoise(sbr, 0);
1253 parseSbrEnvelope(sbr, 1);
1254 parseSbrNoise(sbr, 1);
1256 parseSbrGrid(sbr, 0);
1257 parseSbrGrid(sbr, 1);
1258 parseSbrDtdf(sbr, 0);
1259 parseSbrDtdf(sbr, 1);
1260 parseInvfMode(sbr, 0);
1261 parseInvfMode(sbr, 1);
1262 parseSbrEnvelope(sbr, 0);
1263 parseSbrEnvelope(sbr, 1);
1264 parseSbrNoise(sbr, 0);
1265 parseSbrNoise(sbr, 1);
1267 if ((sbr->bsAddHarmonicFlag[0] = m_reader.readBit())) {
1268 parseSbrSinusoidalCoding(sbr, 0);
1270 if ((sbr->bsAddHarmonicFlag[1] = m_reader.readBit())) {
1271 parseSbrSinusoidalCoding(sbr, 1);
1274 if (sbr->bsCoupling) {
1277 if ((sbr->bsExtendedData = m_reader.readBit())) {
1278 std::uint16_t cnt = m_reader.readBits<std::uint16_t>(4);
1280 cnt += m_reader.readBits<std::uint16_t>(8);
1282 std::uint16_t bitsLeft = 8 * cnt;
1283 while (bitsLeft > 7) {
1284 sbr->bsExtensionId = m_reader.readBits<std::uint8_t>(2);
1285 std::uint16_t tmpBitCount = 2 + parseSbrExtension(sbr, sbr->bsExtensionId,
static_cast<std::uint8_t
>(bitsLeft));
1286 if (tmpBitCount > bitsLeft) {
1287 throw InvalidDataException();
1289 bitsLeft -= tmpBitCount;
1293 m_reader.skipBits(bitsLeft);
1298shared_ptr<AacSbrInfo> AacFrameElementParser::makeSbrInfo(std::uint8_t sbrElement,
bool isDrm)
1300 if (m_mpeg4ExtensionSamplingFrequencyIndex >=
sizeof(mpeg4SamplingFrequencyTable)
1301 && m_mpeg4SamplingFrequencyIndex >=
sizeof(mpeg4SamplingFrequencyTable)) {
1302 throw InvalidDataException();
1304 return make_shared<AacSbrInfo>(m_elementId[sbrElement],
1305 m_mpeg4ExtensionSamplingFrequencyIndex <
sizeof(mpeg4SamplingFrequencyTable)
1306 ? mpeg4SamplingFrequencyTable[m_mpeg4ExtensionSamplingFrequencyIndex]
1307 : mpeg4SamplingFrequencyTable[m_mpeg4SamplingFrequencyIndex] * 2,
1308 m_frameLength, isDrm);
1311void AacFrameElementParser::parseSbrExtensionData(std::uint8_t sbrElement, std::uint16_t count,
bool crcFlag)
1313 CPP_UTILITIES_UNUSED(count);
1315 std::shared_ptr<AacSbrInfo> &sbr = m_sbrElements[sbrElement];
1316 if (m_psResetFlag) {
1317 sbr->psResetFlag = m_psResetFlag;
1319 if (!sbr->isDrmSbr) {
1321 sbr->bsSbrCrcBits = m_reader.readBits<std::uint16_t>(10);
1330 if ((sbr->bsHeaderFlag = m_reader.readBit())) {
1331 sbr->bsStartFreq = m_reader.readBits<std::uint8_t>(4);
1332 sbr->bsStopFreq = m_reader.readBits<std::uint8_t>(4);
1333 sbr->bsXoverBand = m_reader.readBits<std::uint8_t>(3);
1334 m_reader.skipBits(2);
1335 std::uint8_t bsExtraHeader1 = m_reader.readBit();
1336 std::uint8_t bsExtraHeader2 = m_reader.readBit();
1337 if (bsExtraHeader1) {
1338 sbr->bsFreqScale = m_reader.readBits<std::uint8_t>(2);
1339 sbr->bsAlterScale = m_reader.readBit();
1340 sbr->bsNoiseBands = m_reader.readBits<std::uint8_t>(2);
1342 sbr->bsFreqScale = 2;
1343 sbr->bsAlterScale = 1;
1344 sbr->bsNoiseBands = 2;
1346 if (bsExtraHeader2) {
1347 sbr->bsLimiterBands = m_reader.readBits<std::uint8_t>(2);
1348 sbr->bsLimiterGains = m_reader.readBits<std::uint8_t>(2);
1349 sbr->bsInterpolFreq = m_reader.readBit();
1350 sbr->bsSmoothingMode = m_reader.readBit();
1352 sbr->bsLimiterBands = 2;
1353 sbr->bsLimiterGains = 2;
1354 sbr->bsInterpolFreq = 1;
1355 sbr->bsSmoothingMode = 1;
1358 if (sbr->headerCount) {
1359 if (sbr->reset || (sbr->bsHeaderFlag && sbr->justSeeked)) {
1362 sbr->rate = sbr->bsSamplerateMode ? 2 : 1;
1363 switch (sbr->aacElementId) {
1364 using namespace AacSyntaxElementTypes;
1365 case SingleChannelElement:
1366 parseSbrSingleChannelElement(sbr);
1368 case ChannelPairElement:
1369 parseSbrChannelPairElement(sbr);
1375std::uint8_t AacFrameElementParser::parseHuffmanScaleFactor()
1377 std::uint16_t offset = 0;
1378 while (aacHcbSf[offset][1]) {
1379 offset += aacHcbSf[offset][m_reader.readBit()];
1381 throw InvalidDataException();
1384 return aacHcbSf[offset][0];
1387void AacFrameElementParser::parseHuffmanSpectralData(std::uint8_t cb, std::int16_t *sp)
1392 huffman2StepQuad(cb, sp);
1395 huffmanBinaryQuadSign(cb, sp);
1398 huffmanBinaryPair(cb, sp);
1401 huffman2StepPair(cb, sp);
1404 huffman2StepPair(cb, sp);
1408 huffmanBinaryPairSign(cb, sp);
1412 huffman2StepPairSign(cb, sp);
1416 huffman2StepPairSign(11, sp);
1417 }
catch (
const InvalidDataException &) {
1418 sp[0] = huffmanGetEscape(sp[0]);
1419 sp[1] = huffmanGetEscape(sp[1]);
1422 sp[0] = huffmanGetEscape(sp[0]);
1423 sp[1] = huffmanGetEscape(sp[1]);
1427 huffman2StepPair(11, sp);
1428 }
catch (
const InvalidDataException &) {
1429 sp[0] = huffmanCodebook(0);
1430 sp[1] = huffmanCodebook(1);
1433 sp[0] = huffmanCodebook(0);
1434 sp[1] = huffmanCodebook(1);
1453 huffman2StepPairSign(11, sp);
1454 }
catch (
const InvalidDataException &) {
1455 sp[0] = huffmanGetEscape(sp[0]);
1456 sp[1] = huffmanGetEscape(sp[1]);
1457 vcb11CheckLav(cb, sp);
1460 sp[0] = huffmanGetEscape(sp[0]);
1461 sp[1] = huffmanGetEscape(sp[1]);
1462 vcb11CheckLav(cb, sp);
1465 throw InvalidDataException();
1469void AacFrameElementParser::huffmanSignBits(std::int16_t *sp, std::uint8_t len)
1471 for (std::int16_t *end = sp + len; sp < end; ++sp) {
1473 if (m_reader.readBit()) {
1480void AacFrameElementParser::huffman2StepQuad(std::uint8_t cb, std::int16_t *sp)
1482 std::uint32_t cw = m_reader.showBits<std::uint32_t>(aacHcbN[cb]);
1483 std::uint16_t offset = aacHcbTable[cb][cw].offset;
1484 uint8_t extraBits = aacHcbTable[cb][cw].extraBits;
1486 m_reader.skipBits(aacHcbN[cb]);
1487 offset += m_reader.showBits<std::uint16_t>(extraBits);
1488 m_reader.skipBits(aacHcb2QuadTable[cb][offset].bits - aacHcbN[cb]);
1490 m_reader.skipBits(aacHcb2QuadTable[cb][offset].bits);
1492 if (offset > aacHcb2QuadTableSize[cb]) {
1493 throw InvalidDataException();
1495 sp[0] = aacHcb2QuadTable[cb][offset].x;
1496 sp[1] = aacHcb2QuadTable[cb][offset].y;
1497 sp[2] = aacHcb2QuadTable[cb][offset].v;
1498 sp[3] = aacHcb2QuadTable[cb][offset].w;
1501void AacFrameElementParser::huffmanBinaryQuadSign(std::uint8_t cb, std::int16_t *sp)
1504 huffman2StepQuad(cb, sp);
1505 }
catch (
const InvalidDataException &) {
1506 huffmanSignBits(sp, 4);
1509 huffmanSignBits(sp, 4);
1512void AacFrameElementParser::huffmanBinaryPair(std::uint8_t cb, std::int16_t *sp)
1514 std::uint16_t offset = 0;
1515 while (!aacHcbBinTable[cb][offset].isLeaf) {
1516 offset +=
static_cast<std::uint16_t
>(aacHcbBinTable[cb][offset].data[m_reader.readBit()]);
1518 if (offset > aacHcbBinTableSize[cb]) {
1519 throw InvalidDataException();
1521 sp[0] = aacHcbBinTable[cb][offset].data[0];
1522 sp[1] = aacHcbBinTable[cb][offset].data[1];
1525void AacFrameElementParser::huffman2StepPair(std::uint8_t cb, std::int16_t *sp)
1527 std::uint32_t cw = m_reader.showBits<std::uint32_t>(aacHcbN[cb]);
1528 std::uint16_t offset = aacHcbTable[cb][cw].offset;
1529 uint8_t extraBits = aacHcbTable[cb][cw].extraBits;
1531 m_reader.skipBits(aacHcbN[cb]);
1532 offset += m_reader.showBits<std::uint16_t>(extraBits);
1533 m_reader.skipBits(aacHcb2PairTable[cb][offset].bits - aacHcbN[cb]);
1535 m_reader.skipBits(aacHcb2PairTable[cb][offset].bits);
1537 if (offset > aacHcb2PairTableSize[cb]) {
1538 throw InvalidDataException();
1540 sp[0] = aacHcb2PairTable[cb][offset].x;
1541 sp[1] = aacHcb2PairTable[cb][offset].y;
1544void AacFrameElementParser::huffmanBinaryPairSign(std::uint8_t cb, std::int16_t *sp)
1547 huffmanBinaryPair(cb, sp);
1548 }
catch (
const InvalidDataException &) {
1549 huffmanSignBits(sp, 2);
1552 huffmanSignBits(sp, 2);
1555void AacFrameElementParser::huffman2StepPairSign(std::uint8_t cb, std::int16_t *sp)
1558 huffman2StepPair(cb, sp);
1559 }
catch (
const InvalidDataException &) {
1560 huffmanSignBits(sp, 2);
1563 huffmanSignBits(sp, 2);
1566std::int16_t AacFrameElementParser::huffmanGetEscape(std::int16_t sp)
1579 for (size = 4; m_reader.readBit(); ++size) {
1581 const std::int16_t off = m_reader.readBits<std::int16_t>(size);
1582 return static_cast<std::int16_t
>(neg ? -(off | (1 << size)) : (off | (1 << size)));
1585void AacFrameElementParser::vcb11CheckLav(std::uint8_t cb, std::int16_t *sp)
1587 static const uint16_t vcb11LavTab[] = { 16, 31, 47, 63, 95, 127, 159, 191, 223, 255, 319, 383, 511, 767, 1023, 2047 };
1588 std::uint16_t max = 0;
1589 if (cb < 16 || cb > 31)
1591 max = vcb11LavTab[cb - 16];
1592 if ((abs(sp[0]) > max) || (abs(sp[1]) > max)) {
1601void AacFrameElementParser::calculateWindowGroupingInfo(AacIcsInfo &ics)
1603 using namespace AacIcsSequenceTypes;
1604 switch (ics.windowSequence) {
1605 case OnlyLongSequence:
1606 case LongStartSequence:
1607 case LongStopSequence:
1608 ics.windowCount = ics.windowGroupCount = ics.windowGroupLengths[0] = 1;
1609 if (m_mpeg4AudioObjectId == Mpeg4AudioObjectIds::ErAacLd) {
1610 if (m_frameLength == 512) {
1611 ics.swbCount = swb512WindowCount[m_mpeg4SamplingFrequencyIndex];
1613 ics.swbCount = swb480WindowCount[m_mpeg4SamplingFrequencyIndex];
1616 if (m_frameLength == 1024) {
1617 ics.swbCount = swb1024WindowCount[m_mpeg4SamplingFrequencyIndex];
1619 ics.swbCount = swb960WindowCount[m_mpeg4SamplingFrequencyIndex];
1622 if (ics.maxSfb > ics.swbCount) {
1623 throw InvalidDataException();
1625 if (m_mpeg4AudioObjectId == Mpeg4AudioObjectIds::ErAacLd) {
1626 if (m_frameLength == 512) {
1627 for (std::uint8_t i = 0; i <= ics.swbCount; ++i) {
1628 ics.sectionSfbOffset[0][i] = swbOffset512Window[m_mpeg4SamplingFrequencyIndex][i];
1629 ics.swbOffset[i] = swbOffset512Window[m_mpeg4SamplingFrequencyIndex][i];
1632 for (std::uint8_t i = 0; i <= ics.swbCount; ++i) {
1633 ics.sectionSfbOffset[0][i] = swbOffset480Window[m_mpeg4SamplingFrequencyIndex][i];
1634 ics.swbOffset[i] = swbOffset480Window[m_mpeg4SamplingFrequencyIndex][i];
1638 for (std::uint8_t i = 0; i <= ics.swbCount; ++i) {
1639 ics.sectionSfbOffset[0][i] = swbOffset1024Window[m_mpeg4SamplingFrequencyIndex][i];
1640 ics.swbOffset[i] = swbOffset1024Window[m_mpeg4SamplingFrequencyIndex][i];
1643 ics.sectionSfbOffset[0][ics.swbCount] = ics.swbOffset[ics.swbCount] = ics.maxSwbOffset = m_frameLength;
1645 case EightShortSequence:
1646 ics.windowCount = 8;
1647 ics.windowGroupCount = ics.windowGroupLengths[0] = 1;
1648 ics.swbCount = swb128WindowCount[m_mpeg4SamplingFrequencyIndex];
1649 if (ics.maxSfb > ics.swbCount) {
1650 throw InvalidDataException();
1652 for (std::uint8_t i = 0; i < ics.swbCount; ++i) {
1653 ics.swbOffset[i] = swbOffset128Window[m_mpeg4SamplingFrequencyIndex][i];
1655 ics.swbOffset[ics.swbCount] = ics.maxSwbOffset = m_frameLength / 8;
1656 for (std::uint8_t i = 0; i < ics.windowCount - 1; ++i) {
1657 if (!(ics.scaleFactorGrouping & (1 << (6 - i)))) {
1658 ics.windowGroupLengths[ics.windowGroupCount] = 1;
1659 ++ics.windowGroupCount;
1661 ++(ics.windowGroupLengths[ics.windowGroupCount - 1]);
1664 for (std::uint8_t g = 0; g < ics.windowGroupCount; ++g) {
1665 std::uint8_t sectionSfb = 0;
1666 std::uint16_t offset = 0, width;
1667 for (std::uint8_t i = 0; i < ics.swbCount; ++i) {
1668 if (i + 1 == ics.swbCount) {
1669 width = (m_frameLength / 8) - swbOffset128Window[m_mpeg4SamplingFrequencyIndex][i];
1671 width = swbOffset128Window[m_mpeg4SamplingFrequencyIndex][i + 1] - swbOffset128Window[m_mpeg4SamplingFrequencyIndex][i];
1673 width *= ics.windowGroupLengths[g];
1674 ics.sectionSfbOffset[g][sectionSfb++] = offset;
1677 ics.sectionSfbOffset[g][sectionSfb] = offset;
1681 throw InvalidDataException();
1688void AacFrameElementParser::parseIndividualChannelStream(AacIcsInfo &ics, std::int16_t *specData,
bool scaleFlag)
1690 parseSideInfo(ics, scaleFlag);
1691 if (m_mpeg4AudioObjectId >= Mpeg4AudioObjectIds::ErAacLc) {
1692 if (ics.tnsDataPresent) {
1696 if (m_mpeg4AudioObjectId == Mpeg4AudioObjectIds::ErParametric) {
1698 throw NotImplementedException();
1700 if (m_aacSpectralDataResilienceFlag) {
1702 throw NotImplementedException();
1704 parseSpectralData(ics, specData);
1706 if (ics.pulseDataPresent) {
1707 if (ics.windowSequence == AacIcsSequenceTypes::EightShortSequence) {
1708 throw InvalidDataException();
1719void AacFrameElementParser::parseSingleChannelElement()
1721 if (m_elementCount + 1 > aacMaxSyntaxElements) {
1722 throw NotImplementedException();
1726 std::int16_t specData[1024] = { 0 };
1727 m_elementId[m_elementCount] = AacSyntaxElementTypes::SingleChannelElement;
1728 m_elementInstanceTag[m_elementCount] = m_reader.readBits<std::uint8_t>(4);
1731 parseIndividualChannelStream(m_ics1, specData);
1732 if (m_ics1.isUsed) {
1733 throw InvalidDataException();
1736 if (m_reader.showBits<std::uint8_t>(3) == AacSyntaxElementTypes::FillElement) {
1737 parseFillElement(m_elementCount);
1741 m_channelCount += m_elementChannelCount[m_elementCount];
1748void AacFrameElementParser::parseChannelPairElement()
1750 if (m_elementCount + 2 > aacMaxSyntaxElements) {
1751 throw NotImplementedException();
1754 m_elementId[m_elementCount] = AacSyntaxElementTypes::ChannelPairElement;
1755 m_elementChannelCount[m_elementCount] = 2;
1757 std::int16_t specData1[1024] = { 0 };
1758 std::int16_t specData2[1024] = { 0 };
1761 m_elementInstanceTag[m_elementCount] = m_reader.readBits<std::uint8_t>(4);
1762 if ((m_commonWindow = m_reader.readBit())) {
1764 parseIcsInfo(m_ics1);
1765 if ((m_ics1.midSideCodingMaskPresent = m_reader.readBits<std::uint8_t>(2) == 1)) {
1766 for (std::uint8_t g = 0; g < m_ics1.windowGroupCount; ++g) {
1767 for (std::uint8_t sfb = 0; sfb < m_ics1.maxSfb; ++sfb) {
1768 m_ics1.midSideCodingUsed[g][sfb] = m_reader.readBit();
1773 if (m_mpeg4AudioObjectId >= Mpeg4AudioObjectIds::ErAacLc && m_ics1.predictorDataPresent) {
1774 if ((m_ics1.ltp1.dataPresent = m_reader.readBit())) {
1775 parseLtpInfo(m_ics1, m_ics1.ltp1);
1780 m_ics1.midSideCodingMaskPresent =
false;
1782 parseIndividualChannelStream(m_ics1, specData1);
1783 if (m_commonWindow && m_mpeg4AudioObjectId >= Mpeg4AudioObjectIds::ErAacLc && m_ics1.predictorDataPresent) {
1784 if ((m_ics1.ltp2.dataPresent = m_reader.readBit())) {
1785 parseLtpInfo(m_ics1, m_ics1.ltp2);
1788 parseIndividualChannelStream(m_ics2, specData2);
1790 if (m_reader.showBits<std::uint8_t>(3) == AacSyntaxElementTypes::FillElement) {
1791 parseFillElement(m_elementCount);
1795 m_channelCount += 2;
1802void AacFrameElementParser::parseCouplingChannelElement()
1804 m_reader.skipBits(4);
1805 std::uint8_t swCceFlag = m_reader.readBit();
1806 std::uint8_t coupledElementCount = m_reader.readBits<std::uint8_t>(3);
1807 std::uint8_t gainElementLists = 0;
1808 for (std::uint8_t c = 0; c < coupledElementCount; ++c) {
1810 std::uint8_t ccTargetIsCpe = m_reader.readBit();
1812 m_reader.skipBits(4);
1813 if (ccTargetIsCpe) {
1815 if (m_reader.readBit() & m_reader.readBit()) {
1820 m_reader.skipBits(4);
1822 std::int16_t specData[1024];
1823 parseIndividualChannelStream(ics, specData);
1824 for (std::uint8_t c = 1; c < gainElementLists; ++c) {
1825 if (swCceFlag || m_reader.readBit()) {
1826 parseHuffmanScaleFactor();
1828 for (std::uint8_t group = 0; group < ics.windowCount; ++group) {
1829 for (std::uint8_t sfb = 0; sfb < ics.maxSfb; ++sfb) {
1830 if (ics.sfbCb[group][sfb] != AacScaleFactorTypes::ZeroHcb) {
1831 parseHuffmanScaleFactor();
1842void AacFrameElementParser::parseLowFrequencyElement()
1844 parseSingleChannelElement();
1850void AacFrameElementParser::parseDataStreamElement()
1852 std::uint8_t byteAligned = m_reader.readBit();
1853 std::uint16_t count = m_reader.readBits<std::uint16_t>(8);
1854 if (count == 0xFF) {
1855 count += m_reader.readBits<std::uint16_t>(8);
1860 m_reader.skipBits(count * 8);
1866void AacFrameElementParser::parseProgramConfigElement()
1868 m_pce.elementInstanceTag = m_reader.readBits<std::uint8_t>(4);
1869 m_pce.objectType = m_reader.readBits<std::uint8_t>(2);
1870 m_pce.samplingFrequencyIndex = m_reader.readBits<std::uint8_t>(4);
1871 m_pce.frontChannelElementCount = m_reader.readBits<std::uint8_t>(4);
1872 m_pce.sideChannelElementCount = m_reader.readBits<std::uint8_t>(4);
1873 m_pce.backChannelElementCount = m_reader.readBits<std::uint8_t>(4);
1874 m_pce.lfeChannelElementCount = m_reader.readBits<std::uint8_t>(2);
1875 m_pce.assocDataElementCount = m_reader.readBits<std::uint8_t>(3);
1876 m_pce.validCcElementCount = m_reader.readBits<std::uint8_t>(4);
1877 if ((m_pce.monoMixdownPresent = m_reader.readBit())) {
1878 m_pce.monoMixdownElementNumber = m_reader.readBits<std::uint8_t>(4);
1880 if ((m_pce.stereoMixdownPresent = m_reader.readBit())) {
1881 m_pce.stereoMixdownElementNumber = m_reader.readBits<std::uint8_t>(4);
1883 if ((m_pce.matrixMixdownIdxPresent = m_reader.readBit())) {
1884 m_pce.matrixMixdownIdx = m_reader.readBits<std::uint8_t>(2);
1885 m_pce.pseudoSurroundEnable = m_reader.readBit();
1888 for (i = 0; i < m_pce.frontChannelElementCount; ++i) {
1889 m_pce.frontElementIsCpe[i] = m_reader.readBit();
1890 m_pce.frontElementTagSelect[i] = m_reader.readBits<std::uint8_t>(4);
1891 if (m_pce.frontElementIsCpe[i]) {
1892 m_pce.cpeChannel[m_pce.frontElementTagSelect[i]] = m_pce.channels;
1893 m_pce.frontChannelCount += 2;
1894 m_pce.channels += 2;
1896 m_pce.sceChannel[m_pce.frontElementTagSelect[i]] = m_pce.channels;
1897 ++m_pce.frontChannelCount;
1901 for (i = 0; i < m_pce.sideChannelElementCount; ++i) {
1902 m_pce.sideElementIsCpe[i] = m_reader.readBit();
1903 m_pce.sideElementTagSelect[i] = m_reader.readBits<std::uint8_t>(4);
1904 if (m_pce.sideElementIsCpe[i]) {
1905 m_pce.cpeChannel[m_pce.sideElementTagSelect[i]] = m_pce.channels;
1906 m_pce.sideChannelCount += 2;
1907 m_pce.channels += 2;
1909 m_pce.sceChannel[m_pce.sideElementTagSelect[i]] = m_pce.channels;
1910 ++m_pce.sideChannelCount;
1914 for (i = 0; i < m_pce.backChannelElementCount; ++i) {
1915 m_pce.backElementIsCpe[i] = m_reader.readBit();
1916 m_pce.backElementTagSelect[i] = m_reader.readBits<std::uint8_t>(4);
1917 if (m_pce.backElementIsCpe[i]) {
1918 m_pce.cpeChannel[m_pce.backElementTagSelect[i]] = m_pce.channels;
1919 m_pce.backChannelCount += 2;
1920 m_pce.channels += 2;
1922 m_pce.sceChannel[m_pce.backElementTagSelect[i]] = m_pce.channels;
1923 ++m_pce.backChannelCount;
1927 for (i = 0; i < m_pce.lfeChannelElementCount; ++i) {
1928 m_pce.lfeElementTagSelect[i] = m_reader.readBits<std::uint8_t>(4);
1929 m_pce.sceChannel[m_pce.lfeElementTagSelect[i]] = m_pce.channels;
1930 ++m_pce.lfeChannelCount;
1933 for (i = 0; i < m_pce.assocDataElementCount; ++i) {
1934 m_pce.assocDataElementTagSelect[i] = m_reader.readBits<std::uint8_t>(4);
1936 for (i = 0; i < m_pce.validCcElementCount; ++i) {
1937 m_pce.ccElementIsIndSw[i] = m_reader.readBit();
1938 m_pce.validCcElementTagSelect[i] = m_reader.readBits<std::uint8_t>(4);
1941 m_pce.commentFieldBytes = m_reader.readBits<std::uint8_t>(8);
1942 for (i = 0; i < m_pce.commentFieldBytes; ++i) {
1943 m_pce.commentFieldData[i] = m_reader.readBits<std::uint8_t>(8);
1945 m_pce.commentFieldData[i] = 0;
1946 if (m_pce.channels > aacMaxChannels) {
1947 throw NotImplementedException();
1954void AacFrameElementParser::parseFillElement(std::uint8_t sbrElement)
1956 std::uint16_t count = m_reader.readBits<std::uint8_t>(4);
1959 count += m_reader.readBits<std::uint8_t>(8);
1963 switch (m_reader.readBits<std::uint8_t>(4)) {
1964 using namespace AacExtensionTypes;
1966 count -= parseDynamicRange();
1972 if (sbrElement == aacInvalidSbrElement) {
1973 throw InvalidDataException();
1976 if (!m_sbrElements[sbrElement]) {
1977 m_sbrElements[sbrElement] = makeSbrInfo(sbrElement);
1979 parseSbrExtensionData(sbrElement, count, crcFlag);
1981 m_sbrPresentFlag = 1;
1982 if (m_sbrElements[sbrElement]->ps) {
1983 m_psUsed[sbrElement] = 1;
1990 m_reader.skipBits(
static_cast<std::size_t
>(4 + 8 * (count - 1)));
1995 if (m_reader.readBits<std::uint8_t>(4) == 0) {
1997 std::uint8_t dataElementLength = 0, loopCounter = 0;
1998 std::uint16_t dataElementLengthPart;
2000 dataElementLengthPart = m_reader.readBits<std::uint8_t>(8);
2001 dataElementLength +=
static_cast<std::uint8_t
>(dataElementLengthPart);
2003 }
while (dataElementLengthPart == 0xFF);
2004 for (std::uint16_t i = 0; i < dataElementLength; ++i) {
2005 m_reader.skipBits(8);
2006 count -=
static_cast<std::uint16_t
>(dataElementLength + loopCounter + 1);
2011 m_reader.skipBits(
static_cast<std::size_t
>(8 * (count - 1)));
2017 m_reader.skipBits(
static_cast<std::size_t
>(4 + 8 * (count - 1)));
2028void AacFrameElementParser::parseRawDataBlock()
2030 if (m_mpeg4AudioObjectId < Mpeg4AudioObjectIds::ErAacLc) {
2032 switch (m_reader.readBits<std::uint8_t>(3)) {
2033 using namespace AacSyntaxElementTypes;
2034 case SingleChannelElement:
2035 parseSingleChannelElement();
2037 case ChannelPairElement:
2038 parseChannelPairElement();
2040 case ChannelCouplingElement:
2041 parseCouplingChannelElement();
2043 case LowFrequencyElement:
2044 parseLowFrequencyElement();
2046 case DataStreamElement:
2047 parseDataStreamElement();
2049 case ProgramConfigElement:
2050 parseProgramConfigElement();
2061 switch (m_mpeg4ChannelConfig) {
2062 using namespace Mpeg4ChannelConfigs;
2063 using namespace AacSyntaxElementTypes;
2065 parseSingleChannelElement();
2068 parseChannelPairElement();
2071 parseSingleChannelElement();
2072 parseChannelPairElement();
2075 parseSingleChannelElement();
2076 parseChannelPairElement();
2077 parseSingleChannelElement();
2080 parseSingleChannelElement();
2081 parseChannelPairElement();
2082 parseChannelPairElement();
2085 parseSingleChannelElement();
2086 parseChannelPairElement();
2087 parseChannelPairElement();
2088 parseSingleChannelElement();
2091 parseSingleChannelElement();
2092 parseChannelPairElement();
2093 parseChannelPairElement();
2094 parseChannelPairElement();
2095 parseSingleChannelElement();
2105void AacFrameElementParser::parse(
const AdtsFrame &adtsFrame, std::istream &stream, std::size_t dataSize)
2107 auto data = make_unique<char[]>(dataSize);
2108 stream.read(data.get(),
static_cast<std::streamsize
>(dataSize));
2109 parse(adtsFrame, data, dataSize);
2115void AacFrameElementParser::parse(
const AdtsFrame &adtsFrame, std::unique_ptr<
char[]> &data, std::size_t dataSize)
2117 m_reader.reset(data.get(), dataSize);
2118 m_mpeg4AudioObjectId = adtsFrame.mpeg4AudioObjectId();
2119 m_mpeg4SamplingFrequencyIndex = adtsFrame.mpeg4SamplingFrequencyIndex();
2120 parseRawDataBlock();
@ FrontCenterFrontLeftFrontRightBackLeftBackRightLFEChannel
@ FrontCenterFrontLeftFrontRightBackCenter
@ FrontCenterFrontLeftFrontRightSideLeftSideRightBackLeftBackRightLFEChannel
@ FrontCenterFrontLeftFrontRight
@ FrontCenterFrontLeftFrontRightBackLeftBackRight
Contains all classes and functions of the TagInfo library.