4 #include "../adts/adtsframe.h" 6 #include "../mp4/mp4ids.h" 8 #include "../exceptions.h" 10 #include <c++utilities/io/bitreader.h> 11 #include <c++utilities/misc/memory.h> 28 const byte maxPredictionSfb[16] = { 33, 33, 38, 40, 40, 40, 41, 41, 37, 37, 37, 34, 64, 64, 64, 64 };
30 const uint8_t swb512WindowCount[] = { 0, 0, 0, 36, 36, 37, 31, 31, 0, 0, 0, 0 };
32 const byte swb480WindowCount[] = { 0, 0, 0, 35, 35, 37, 30, 30, 0, 0, 0, 0 };
34 const byte swb960WindowCount[] = { 40, 40, 45, 49, 49, 49, 46, 46, 42, 42, 42, 40 };
36 const byte swb1024WindowCount[] = { 41, 41, 47, 49, 49, 51, 47, 47, 43, 43, 43, 40 };
38 const byte swb128WindowCount[] = { 12, 12, 12, 14, 14, 14, 15, 15, 15, 15, 15, 15 };
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 };
43 const uint16 swbOffset128_96[] = { 0, 4, 8, 12, 16, 20, 24, 32, 40, 48, 64, 92, 128 };
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 };
48 const uint16 swbOffset128_64[] = { 0, 4, 8, 12, 16, 20, 24, 32, 40, 48, 64, 92, 128 };
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 };
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 };
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 };
59 const uint16 swbOffset128_48[] = { 0, 4, 8, 12, 16, 20, 28, 36, 44, 56, 68, 80, 96, 112, 128 };
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 };
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 };
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 };
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 };
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 };
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 };
79 const uint16 swbOffset128_24[] = { 0, 4, 8, 12, 16, 20, 24, 28, 36, 44, 52, 64, 76, 92, 108, 128 };
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 };
84 const uint16 swbOffset128_16[] = { 0, 4, 8, 12, 16, 20, 24, 28, 32, 40, 48, 60, 72, 88, 108, 128 };
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 };
89 const uint16 swbOffset128_8[] = { 0, 4, 8, 12, 16, 20, 24, 28, 36, 44, 52, 60, 72, 88, 108, 128 };
91 static const uint16 *
const swbOffset1024Window[] = {
106 static const uint16 *
const swbOffset512Window[] = {
121 static const uint16 *
const swbOffset480Window[] = {
136 static const uint16 *
const swbOffset128Window[] = {
154 AacLtpInfo::AacLtpInfo()
162 , shortLagPresent{ 0 }
170 AacPredictorInfo::AacPredictorInfo()
173 , resetGroupNumber(0)
174 , predictionUsed{ 0 }
181 AacPulseInfo::AacPulseInfo()
192 AacTnsInfo::AacTnsInfo()
198 , coefCompress{ { 0 } }
206 AacSsrInfo::AacSsrInfo()
209 , alevcode{ { { 0 } } }
210 , aloccode{ { { 0 } } }
217 AacDrcInfo::AacDrcInfo()
221 , excludedChannelsPresent(0)
224 , dynamicRangeSign{ 0 }
225 , dynamicRangeControl{ 0 }
227 , additionalExcludedChannels{ 0 }
234 AacPsInfo::AacPsInfo()
241 AacDrmPsInfo::AacDrmPsInfo()
248 AacSbrInfo::AacSbrInfo(
byte sbrElementType, uint16 samplingFrequency, uint16 frameLength,
bool isDrm)
249 : aacElementId(sbrElementType)
250 , samplingFrequency(samplingFrequency)
324 , bsInvfModePrev{ { 0 } }
330 , patchNoSubbands{ 0 }
331 , patchStartSubband{ 0 }
334 bsAddHarmonic{ { 0 } }
335 , bsAddHarmonicPrev{ { 0 } }
342 bsStartFreqPrev(numeric_limits<byte>::max())
346 , bsAlterScalePrev(0)
347 , bsNoiseBandsPrev(0)
350 prevEnvIsShort{ -1, -1 }
357 , frameLength(frameLength)
376 timeSlotsRateCount(aacSbrRate * (frameLength == 960 ? aacNoTimeSlots960 : aacNoTimeSlots))
377 , timeSlotsCount(frameLength == 960 ? aacNoTimeSlots960 : aacNoTimeSlots)
389 , bsProtocolVersion(0)
401 , bsSamplerateMode(1)
402 , bsAddHarmonicFlag{ 0 }
403 , bsAddHarmonicFlagPrev{ 0 }
410 , bsRelBord0{ { 0 } }
411 , bsRelBord1{ { 0 } }
420 throw NotImplementedException();
435 AacProgramConfig::AacProgramConfig()
436 : elementInstanceTag(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 }
463 , commentFieldBytes(0)
464 , commentFieldData{ 0 }
465 , frontChannelCount(0)
466 , sideChannelCount(0)
467 , backChannelCount(0)
477 AacIcsInfo::AacIcsInfo()
480 , windowGroupCount(0)
483 , windowGroupLengths{ 0 }
485 , scaleFactorGrouping(0)
486 , sectionSfbOffset{ { 0 } }
490 , sectionStart{ { 0 } }
491 , sectionEnd{ { 0 } }
493 , sectionsPerGroup{ 0 }
495 , scaleFactors{ { 0 } }
496 , midSideCodingMaskPresent(0)
497 , midSideCodingUsed{ { 0 } }
500 , pulseDataPresent(0)
502 , gainControlPresent(0)
503 , predictorDataPresent(0)
504 , reorderedSpectralDataLength(0)
505 , longestCodewordLength(0)
510 , sfEscapesPresent(0)
511 , rvlcEscapesLength(0)
512 , dpcmNoiseLastPos(0)
527 void AacFrameElementParser::parseLtpInfo(
const AacIcsInfo &ics, AacLtpInfo <p)
531 if ((ltp.lagUpdate = m_reader.readBit())) {
532 ltp.lag = m_reader.readBits<uint16>(10);
535 ltp.lag = m_reader.readBits<uint16>(11);
537 if (ltp.lag > (m_frameLength << 1)) {
538 throw InvalidDataException();
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);
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();
562 void AacFrameElementParser::parseIcsInfo(AacIcsInfo &ics)
564 using namespace AacIcsSequenceTypes;
565 if (m_reader.readBit()) {
566 throw InvalidDataException();
568 ics.windowSequence = m_reader.readBits<
byte>(2);
569 ics.windowShape = m_reader.readBit();
571 throw InvalidDataException();
573 if (ics.windowSequence == EightShortSequence) {
574 ics.maxSfb = m_reader.readBits<
byte>(4);
575 ics.scaleFactorGrouping = m_reader.readBits<
byte>(7);
577 ics.maxSfb = m_reader.readBits<
byte>(6);
579 calculateWindowGroupingInfo(ics);
580 if (ics.windowSequence != EightShortSequence) {
581 if ((ics.predictorDataPresent = m_reader.readBit())) {
582 switch (m_mpeg4AudioObjectId) {
585 if ((ics.predictor.reset = m_reader.readBit())) {
586 ics.predictor.resetGroupNumber = m_reader.readBits<
byte>(5);
587 ics.predictor.maxSfb = ics.maxSfb;
589 if (ics.predictor.maxSfb > maxPredictionSfb[m_mpeg4SamplingFrequencyIndex]) {
590 ics.predictor.maxSfb = maxPredictionSfb[m_mpeg4SamplingFrequencyIndex];
592 for (
byte sfb = 0; sfb < ics.predictor.maxSfb; ++sfb) {
593 ics.predictor.predictionUsed[sfb] = m_reader.readBit();
599 if ((ics.ltp1.dataPresent = m_reader.readBit())) {
600 parseLtpInfo(ics, ics.ltp1);
602 if (m_commonWindow) {
603 if ((ics.ltp2.dataPresent = m_reader.readBit())) {
604 parseLtpInfo(ics, ics.ltp2);
609 if ((ics.ltp1.dataPresent = m_reader.readBit())) {
610 parseLtpInfo(ics, ics.ltp1);
621 void AacFrameElementParser::parseSectionData(AacIcsInfo &ics)
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);
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)
634 while (sectionLengthIncrease == sectionEscValue) {
635 sectionLength += sectionLengthIncrease;
636 sectionLengthIncrease = m_reader.readBits<
byte>(sectionBits);
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();
648 if (i + sectionLength > aacMaxSfb) {
649 throw InvalidDataException();
650 }
else if (sectionIndex >= aacMaxSfb) {
651 throw InvalidDataException();
654 for (
byte sfb = i; sfb < i + sectionLength; ++sfb) {
655 ics.sfbCb[groupIndex][sfb] = ics.sectionCb[groupIndex][sectionIndex];
658 ics.sectionsPerGroup[groupIndex] = sectionIndex;
665 void AacFrameElementParser::decodeScaleFactorData(AacIcsInfo &ics)
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]) {
677 ics.scaleFactors[group][sfb] = 0;
681 ics.scaleFactors[group][sfb] = static_cast<uint16>(isPosition += (parseHuffmanScaleFactor() - 60));
686 tmp = m_reader.readBits<int16>(9);
688 tmp = parseHuffmanScaleFactor() - 60;
690 ics.scaleFactors[group][sfb] = static_cast<uint16>(noiseEnergy += tmp);
693 scaleFactor += parseHuffmanScaleFactor() - 60;
694 if (scaleFactor < 0 || scaleFactor > 255) {
695 throw InvalidDataException();
697 ics.scaleFactors[group][sfb] = 0;
707 void AacFrameElementParser::decodeRvlcScaleFactorData(AacIcsInfo &ics)
709 if (ics.rvlcSfLength) {
710 m_reader.skipBits(ics.rvlcSfLength);
712 if (ics.sfEscapesPresent) {
713 m_reader.skipBits(ics.rvlcEscapesLength);
721 void AacFrameElementParser::parseScaleFactorData(AacIcsInfo &ics)
723 if (!m_aacScalefactorDataResilienceFlag) {
724 decodeScaleFactorData(ics);
726 decodeRvlcScaleFactorData(ics);
733 void AacFrameElementParser::parsePulseData(AacIcsInfo &ics)
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();
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);
750 void AacFrameElementParser::parseTnsData(AacIcsInfo &ics)
752 byte filtBits, lengthBits, orderBits, startCoefBits, coefBits;
753 if (ics.windowSequence == AacIcsSequenceTypes::EightShortSequence) {
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;
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);
783 void AacFrameElementParser::parseGainControlData(AacIcsInfo &ics)
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);
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);
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);
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);
839 void AacFrameElementParser::parseSpectralData(AacIcsInfo &ics, int16 *specData)
843 for (
byte group = 0; group < ics.windowGroupCount; ++group) {
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;
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);
871 void AacFrameElementParser::parseSideInfo(AacIcsInfo &ics,
bool scaleFlag)
873 ics.globalGain = m_reader.readBits<
byte>(8);
874 if (!m_commonWindow && !scaleFlag) {
877 parseSectionData(ics);
878 parseScaleFactorData(ics);
880 if ((ics.pulseDataPresent = m_reader.readBit())) {
883 if ((ics.tnsDataPresent = m_reader.readBit())) {
886 if ((ics.gainControlPresent = m_reader.readBit())) {
888 throw InvalidDataException();
890 parseGainControlData(ics);
894 if (m_aacScalefactorDataResilienceFlag) {
895 decodeRvlcScaleFactorData(ics);
899 byte AacFrameElementParser::parseExcludedChannels()
901 for (
byte i = 0; i < 7; ++i) {
902 m_drc.excludeMask[i] = m_reader.readBit();
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();
913 byte AacFrameElementParser::parseDynamicRange()
917 if (m_reader.readBit()) {
918 m_drc.pceInstanceTag = m_reader.readBits<
byte>(4);
919 m_reader.skipBits(4);
922 if ((m_drc.excludedChannelsPresent = m_reader.readBit())) {
923 size += parseExcludedChannels();
925 if (m_reader.readBit()) {
926 m_drc.bandCount += m_reader.readBits<
byte>(4);
927 m_reader.skipBits(4);
929 for (
byte i = 0; i < m_drc.bandCount; ++i, ++size) {
930 m_drc.bandTop[i] = m_reader.readBits<
byte>(8);
933 if (m_reader.readBit()) {
934 m_drc.progRefLevel = m_reader.readBits<
byte>(7);
935 m_reader.skipBits(1);
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);
946 int16 AacFrameElementParser::sbrHuffmanDec(SbrHuffTab table)
951 bit = m_reader.readBit();
952 index = table[index][bit];
957 void AacFrameElementParser::parseSbrGrid(std::shared_ptr<AacSbrInfo> &sbr,
byte channel)
959 byte tmp, bsEnvCount;
961 switch ((sbr->bsFrameClass[channel] = m_reader.readBits<
byte>(2))) {
962 using namespace BsFrameClasses;
964 tmp = m_reader.readBits<
byte>(2);
965 sbr->absBordLead[channel] = 0;
966 sbr->absBordTrail[channel] = sbr->timeSlotsCount;
967 sbr->relLeadCount[channel] = (bsEnvCount = min<byte>(static_cast<byte>(1 << tmp), 5)) - 1;
968 sbr->relTrailCount[channel] = 0;
969 tmp = m_reader.readBit();
970 for (
byte env = 0; env < bsEnvCount; ++env) {
971 sbr->f[channel][env] = tmp;
975 sbr->absBordLead[channel] = 0;
976 sbr->absBordTrail[channel] = m_reader.readBits<
byte>(2) + sbr->timeSlotsCount;
977 sbr->relLeadCount[channel] = 0;
978 sbr->relTrailCount[channel] = bsEnvCount = m_reader.readBits<
byte>(2);
979 for (
byte rel = 0; rel < bsEnvCount; ++rel) {
980 sbr->bsRelBord[channel][rel] = 2 * m_reader.readBits<
byte>(2) + 2;
982 sbr->bsPointer[channel] = m_reader.readBits<
byte>(static_cast<byte>(sbrLog2(static_cast<sbyte>(bsEnvCount + 2))));
983 for (
byte env = 0; env <= bsEnvCount; ++env) {
984 sbr->f[channel][bsEnvCount - env] = m_reader.readBit();
988 sbr->absBordLead[channel] = m_reader.readBits<
byte>(2);
989 sbr->absBordTrail[channel] = sbr->timeSlotsCount;
990 sbr->relLeadCount[channel] = bsEnvCount = m_reader.readBits<
byte>(2);
991 sbr->relTrailCount[channel] = 0;
992 for (
byte rel = 0; rel < bsEnvCount; ++rel) {
993 sbr->bsRelBord[channel][rel] = 2 * m_reader.readBits<
byte>(2) + 2;
995 sbr->bsPointer[channel] = m_reader.readBits<
byte>(static_cast<byte>(sbrLog2(static_cast<sbyte>(bsEnvCount + 2))));
996 for (
byte env = 0; env < bsEnvCount; ++env) {
997 sbr->f[channel][env] = m_reader.readBit();
1001 sbr->absBordLead[channel] = m_reader.readBits<
byte>(2);
1002 sbr->absBordTrail[channel] = m_reader.readBits<
byte>(2) + sbr->timeSlotsCount;
1005 m_reader.skipBits(4);
1006 bsEnvCount = min<byte>(5, sbr->bsRelCount0[channel] + sbr->bsRelCount1[channel] + 1);
1007 for (
byte rel = 0; rel < sbr->bsRelCount0[channel]; ++rel) {
1008 sbr->bsRelBord0[channel][rel] = 2 * m_reader.readBits<
byte>(2) + 2;
1010 for (
byte rel = 0; rel < sbr->bsRelCount1[channel]; ++rel) {
1011 sbr->bsRelBord1[channel][rel] = 2 * m_reader.readBits<
byte>(2) + 2;
1013 sbr->bsPointer[channel]
1014 = m_reader.readBits<
byte>(static_cast<byte>(sbrLog2(static_cast<sbyte>(sbr->bsRelCount0[channel] + sbr->bsRelCount1[channel] + 2))));
1015 for (
byte env = 0; env < bsEnvCount; ++env) {
1016 sbr->f[channel][env] = m_reader.readBit();
1018 sbr->relLeadCount[channel] = sbr->bsRelCount0[channel];
1019 sbr->relTrailCount[channel] = sbr->bsRelCount1[channel];
1023 if ((sbr->le[channel] = min<byte>(bsEnvCount, sbr->bsFrameClass[channel] == BsFrameClasses::VarVar ? 5 : 4)) <= 0) {
1024 throw InvalidDataException();
1026 sbr->lq[channel] = sbr->le[channel] > 1 ? 2 : 1;
1030 void AacFrameElementParser::parseSbrDtdf(std::shared_ptr<AacSbrInfo> &sbr,
byte channel)
1032 for (
byte i = 0; i < sbr->le[channel]; ++i) {
1033 sbr->bsDfEnv[channel][i] = m_reader.readBit();
1035 for (
byte i = 0; i < sbr->lq[channel]; ++i) {
1036 sbr->bsDfNoise[channel][i] = m_reader.readBit();
1040 void AacFrameElementParser::parseInvfMode(std::shared_ptr<AacSbrInfo> &sbr,
byte channel)
1042 for (
byte i = 0; i < sbr->nq; ++i) {
1043 sbr->bsInvfMode[channel][i] = m_reader.readBits<
byte>(2);
1047 void AacFrameElementParser::parseSbrEnvelope(std::shared_ptr<AacSbrInfo> &sbr,
byte channel)
1052 if ((sbr->le[channel] == 1) && (sbr->bsFrameClass[channel] == BsFrameClasses::FixFix)) {
1053 sbr->ampRes[channel] = 0;
1055 sbr->ampRes[channel] = sbr->bsAmpRes;
1057 if ((sbr->bsCoupling) && (channel == 1)) {
1059 if (sbr->ampRes[channel]) {
1061 fHuff = fHuffmanEnvBal30dB;
1064 fHuff = fHuffmanEnvBal15dB;
1068 if (sbr->ampRes[channel]) {
1070 fHuff = fHuffmanEnv30dB;
1073 fHuff = fHuffmanEnv15dB;
1076 for (
byte env = 0; env < sbr->le[channel]; ++env) {
1077 if (sbr->bsDfEnv[channel][env] == 0) {
1078 if ((sbr->bsCoupling == 1) && (channel == 1)) {
1079 if (sbr->ampRes[channel]) {
1080 sbr->e[channel][0][env] = static_cast<int16>(m_reader.readBits<uint16>(5) << delta);
1082 sbr->e[channel][0][env] = static_cast<int16>(m_reader.readBits<uint16>(6) << delta);
1085 if (sbr->ampRes[channel]) {
1086 sbr->e[channel][0][env] = static_cast<int16>(m_reader.readBits<uint16>(6) << delta);
1088 sbr->e[channel][0][env] = static_cast<int16>(m_reader.readBits<uint16>(7) << delta);
1091 for (
byte band = 1; band < sbr->n[sbr->f[channel][env]]; ++band) {
1092 sbr->e[channel][band][env] = static_cast<int16>(sbrHuffmanDec(fHuff) << delta);
1095 for (
byte band = 0; band < sbr->n[sbr->f[channel][env]]; ++band) {
1096 sbr->e[channel][band][env] = static_cast<int16>(sbrHuffmanDec(fHuff) << delta);
1103 void AacFrameElementParser::parseSbrNoise(std::shared_ptr<AacSbrInfo> &sbr,
byte channel)
1108 if ((sbr->bsCoupling == 1) && (channel == 1)) {
1111 fHuff = fHuffmanEnvBal30dB;
1115 fHuff = fHuffmanEnv30dB;
1117 for (
byte noise = 0; noise < sbr->lq[channel]; ++noise) {
1118 if (sbr->bsDfNoise[channel][noise] == 0) {
1119 if ((sbr->bsCoupling == 1) && (channel == 1)) {
1120 sbr->q[channel][0][noise] = m_reader.readBits<
byte>(5) << delta;
1122 sbr->q[channel][0][noise] = m_reader.readBits<
byte>(5) << delta;
1124 for (
byte band = 1; band < sbr->nq; ++band) {
1125 sbr->q[channel][band][noise] = sbrHuffmanDec(fHuff) << delta;
1128 for (
byte band = 0; band < sbr->nq; ++band) {
1129 sbr->q[channel][band][noise] = sbrHuffmanDec(fHuff) << delta;
1136 void AacFrameElementParser::parseSbrSinusoidalCoding(std::shared_ptr<AacSbrInfo> &sbr,
byte channel)
1138 for (
byte i = 0; i < sbr->nHigh; ++i) {
1139 sbr->bsAddHarmonic[channel][i] = m_reader.readBit();
1143 uint16 AacFrameElementParser::parseSbrExtension(std::shared_ptr<AacSbrInfo> &sbr,
byte extensionId,
byte)
1147 switch (extensionId) {
1148 using namespace AacSbrExtensionIds;
1150 if (sbr->psResetFlag) {
1151 sbr->ps->headerRead = 0;
1153 res = parsePsData(sbr->ps, header);
1154 if (sbr->psUsed == 0 && header == 1) {
1158 sbr->psResetFlag = 0;
1161 case DrmParametricStereo:
1163 return parseDrmPsData(sbr->drmPs);
1165 sbr->bsExtendedData = m_reader.readBits<
byte>(6);
1170 uint16 AacFrameElementParser::parsePsData(std::shared_ptr<AacPsInfo> &ps,
byte &header)
1172 if (m_reader.readBit()) {
1175 ps->use34HybridBands = 0;
1176 if ((ps->enableIID = m_reader.readBit())) {
1177 ps->iidMode = m_reader.readBits<
byte>(3);
1180 throw NotImplementedException();
1183 uint16 AacFrameElementParser::parseDrmPsData(std::shared_ptr<AacDrmPsInfo> &drmPs)
1186 throw NotImplementedException();
1189 void AacFrameElementParser::parseSbrSingleChannelElement(std::shared_ptr<AacSbrInfo> &sbr)
1191 if (m_reader.readBit()) {
1192 m_reader.skipBits(4);
1194 if (sbr->isDrmSbr) {
1195 m_reader.skipBits(1);
1197 parseSbrGrid(sbr, 0);
1198 parseSbrDtdf(sbr, 0);
1199 parseInvfMode(sbr, 0);
1200 parseSbrEnvelope(sbr, 0);
1201 parseSbrNoise(sbr, 0);
1203 if ((sbr->bsAddHarmonicFlag[0] = m_reader.readBit())) {
1204 parseSbrSinusoidalCoding(sbr, 0);
1206 if ((sbr->bsExtendedData = m_reader.readBit())) {
1207 uint16 cnt = m_reader.readBits<uint16>(4);
1209 cnt += m_reader.readBits<uint16>(8);
1211 uint16 bitsLeft = 8 * cnt;
1212 while (bitsLeft > 7) {
1213 sbr->bsExtensionId = m_reader.readBits<
byte>(2);
1214 uint16 tmpBitCount = 2 + parseSbrExtension(sbr, sbr->bsExtensionId, static_cast<byte>(bitsLeft));
1215 if (tmpBitCount > bitsLeft) {
1216 throw InvalidDataException();
1218 bitsLeft -= tmpBitCount;
1222 m_reader.skipBits(bitsLeft);
1227 void AacFrameElementParser::parseSbrChannelPairElement(std::shared_ptr<AacSbrInfo> &sbr)
1229 if (m_reader.readBit()) {
1230 m_reader.skipBits(8);
1232 if ((sbr->bsCoupling = m_reader.readBit())) {
1233 parseSbrGrid(sbr, 0);
1235 sbr->bsFrameClass[1] = sbr->bsFrameClass[0];
1236 sbr->le[1] = sbr->le[0];
1237 sbr->lq[1] = sbr->lq[0];
1238 sbr->bsPointer[1] = sbr->bsPointer[0];
1239 for (
byte n = 0; n < sbr->le[0]; ++n) {
1240 sbr->te[1][n] = sbr->te[0][n];
1241 sbr->f[1][n] = sbr->f[0][n];
1243 for (
byte n = 0; n < sbr->lq[0]; ++n) {
1244 sbr->tq[1][n] = sbr->tq[0][n];
1246 parseSbrDtdf(sbr, 0);
1247 parseSbrDtdf(sbr, 1);
1248 parseInvfMode(sbr, 0);
1249 for (
byte n = 0; n < sbr->nq; ++n) {
1250 sbr->bsInvfMode[1][n] = sbr->bsInvfMode[0][n];
1252 parseSbrEnvelope(sbr, 0);
1253 parseSbrNoise(sbr, 0);
1254 parseSbrEnvelope(sbr, 1);
1255 parseSbrNoise(sbr, 1);
1257 parseSbrGrid(sbr, 0);
1258 parseSbrGrid(sbr, 1);
1259 parseSbrDtdf(sbr, 0);
1260 parseSbrDtdf(sbr, 1);
1261 parseInvfMode(sbr, 0);
1262 parseInvfMode(sbr, 1);
1263 parseSbrEnvelope(sbr, 0);
1264 parseSbrEnvelope(sbr, 1);
1265 parseSbrNoise(sbr, 0);
1266 parseSbrNoise(sbr, 1);
1268 if ((sbr->bsAddHarmonicFlag[0] = m_reader.readBit())) {
1269 parseSbrSinusoidalCoding(sbr, 0);
1271 if ((sbr->bsAddHarmonicFlag[1] = m_reader.readBit())) {
1272 parseSbrSinusoidalCoding(sbr, 1);
1275 if (sbr->bsCoupling) {
1278 if ((sbr->bsExtendedData = m_reader.readBit())) {
1279 uint16 cnt = m_reader.readBits<uint16>(4);
1281 cnt += m_reader.readBits<uint16>(8);
1283 uint16 bitsLeft = 8 * cnt;
1284 while (bitsLeft > 7) {
1285 sbr->bsExtensionId = m_reader.readBits<
byte>(2);
1286 uint16 tmpBitCount = 2 + parseSbrExtension(sbr, sbr->bsExtensionId, static_cast<byte>(bitsLeft));
1287 if (tmpBitCount > bitsLeft) {
1288 throw InvalidDataException();
1290 bitsLeft -= tmpBitCount;
1294 m_reader.skipBits(bitsLeft);
1299 shared_ptr<AacSbrInfo> AacFrameElementParser::makeSbrInfo(
byte sbrElement,
bool isDrm)
1303 throw InvalidDataException();
1305 return make_shared<AacSbrInfo>(m_elementId[sbrElement],
1309 m_frameLength, isDrm);
1312 void AacFrameElementParser::parseSbrExtensionData(
byte sbrElement, uint16 count,
bool crcFlag)
1316 std::shared_ptr<AacSbrInfo> &sbr = m_sbrElements[sbrElement];
1317 if (m_psResetFlag) {
1318 sbr->psResetFlag = m_psResetFlag;
1320 if (!sbr->isDrmSbr) {
1322 sbr->bsSbrCrcBits = m_reader.readBits<uint16>(10);
1331 if ((sbr->bsHeaderFlag = m_reader.readBit())) {
1332 sbr->bsStartFreq = m_reader.readBits<
byte>(4);
1333 sbr->bsStopFreq = m_reader.readBits<
byte>(4);
1334 sbr->bsXoverBand = m_reader.readBits<
byte>(3);
1335 m_reader.skipBits(2);
1336 byte bsExtraHeader1 = m_reader.readBit();
1337 byte bsExtraHeader2 = m_reader.readBit();
1338 if (bsExtraHeader1) {
1339 sbr->bsFreqScale = m_reader.readBits<
byte>(2);
1340 sbr->bsAlterScale = m_reader.readBit();
1341 sbr->bsNoiseBands = m_reader.readBits<
byte>(2);
1343 sbr->bsFreqScale = 2;
1344 sbr->bsAlterScale = 1;
1345 sbr->bsNoiseBands = 2;
1347 if (bsExtraHeader2) {
1348 sbr->bsLimiterBands = m_reader.readBits<
byte>(2);
1349 sbr->bsLimiterGains = m_reader.readBits<
byte>(2);
1350 sbr->bsInterpolFreq = m_reader.readBit();
1351 sbr->bsSmoothingMode = m_reader.readBit();
1353 sbr->bsLimiterBands = 2;
1354 sbr->bsLimiterGains = 2;
1355 sbr->bsInterpolFreq = 1;
1356 sbr->bsSmoothingMode = 1;
1359 if (sbr->headerCount) {
1360 if (sbr->reset || (sbr->bsHeaderFlag && sbr->justSeeked)) {
1363 sbr->rate = sbr->bsSamplerateMode ? 2 : 1;
1364 switch (sbr->aacElementId) {
1365 using namespace AacSyntaxElementTypes;
1366 case SingleChannelElement:
1367 parseSbrSingleChannelElement(sbr);
1369 case ChannelPairElement:
1370 parseSbrChannelPairElement(sbr);
1376 byte AacFrameElementParser::parseHuffmanScaleFactor()
1379 while (aacHcbSf[offset][1]) {
1380 offset += aacHcbSf[offset][m_reader.readBit()];
1382 throw InvalidDataException();
1385 return aacHcbSf[offset][0];
1388 void AacFrameElementParser::parseHuffmanSpectralData(
byte cb, int16 *sp)
1393 huffman2StepQuad(cb, sp);
1396 huffmanBinaryQuadSign(cb, sp);
1399 huffmanBinaryPair(cb, sp);
1402 huffman2StepPair(cb, sp);
1405 huffman2StepPair(cb, sp);
1409 huffmanBinaryPairSign(cb, sp);
1413 huffman2StepPairSign(cb, sp);
1417 huffman2StepPairSign(11, sp);
1418 }
catch (
const InvalidDataException &) {
1419 sp[0] = huffmanGetEscape(sp[0]);
1420 sp[1] = huffmanGetEscape(sp[1]);
1423 sp[0] = huffmanGetEscape(sp[0]);
1424 sp[1] = huffmanGetEscape(sp[1]);
1428 huffman2StepPair(11, sp);
1429 }
catch (
const InvalidDataException &) {
1430 sp[0] = huffmanCodebook(0);
1431 sp[1] = huffmanCodebook(1);
1434 sp[0] = huffmanCodebook(0);
1435 sp[1] = huffmanCodebook(1);
1454 huffman2StepPairSign(11, sp);
1455 }
catch (
const InvalidDataException &) {
1456 sp[0] = huffmanGetEscape(sp[0]);
1457 sp[1] = huffmanGetEscape(sp[1]);
1458 vcb11CheckLav(cb, sp);
1461 sp[0] = huffmanGetEscape(sp[0]);
1462 sp[1] = huffmanGetEscape(sp[1]);
1463 vcb11CheckLav(cb, sp);
1466 throw InvalidDataException();
1470 void AacFrameElementParser::huffmanSignBits(int16 *sp,
byte len)
1472 for (int16 *end = sp + len; sp < end; ++sp) {
1474 if (m_reader.readBit()) {
1481 void AacFrameElementParser::huffman2StepQuad(
byte cb, int16 *sp)
1483 uint32 cw = m_reader.showBits<uint32>(aacHcbN[cb]);
1484 uint16 offset = aacHcbTable[cb][cw].offset;
1485 uint8_t extraBits = aacHcbTable[cb][cw].extraBits;
1487 m_reader.skipBits(aacHcbN[cb]);
1488 offset += m_reader.showBits<uint16>(extraBits);
1489 m_reader.skipBits(aacHcb2QuadTable[cb][offset].bits - aacHcbN[cb]);
1491 m_reader.skipBits(aacHcb2QuadTable[cb][offset].bits);
1493 if (offset > aacHcb2QuadTableSize[cb]) {
1494 throw InvalidDataException();
1496 sp[0] = aacHcb2QuadTable[cb][offset].x;
1497 sp[1] = aacHcb2QuadTable[cb][offset].y;
1498 sp[2] = aacHcb2QuadTable[cb][offset].v;
1499 sp[3] = aacHcb2QuadTable[cb][offset].w;
1502 void AacFrameElementParser::huffmanBinaryQuadSign(
byte cb, int16 *sp)
1505 huffman2StepQuad(cb, sp);
1506 }
catch (
const InvalidDataException &) {
1507 huffmanSignBits(sp, 4);
1510 huffmanSignBits(sp, 4);
1513 void AacFrameElementParser::huffmanBinaryPair(
byte cb, int16 *sp)
1516 while (!aacHcbBinTable[cb][offset].isLeaf) {
1517 offset += aacHcbBinTable[cb][offset].data[m_reader.readBit()];
1519 if (offset > aacHcbBinTableSize[cb]) {
1520 throw InvalidDataException();
1522 sp[0] = aacHcbBinTable[cb][offset].data[0];
1523 sp[1] = aacHcbBinTable[cb][offset].data[1];
1526 void AacFrameElementParser::huffman2StepPair(
byte cb, int16 *sp)
1528 uint32 cw = m_reader.showBits<uint32>(aacHcbN[cb]);
1529 uint16 offset = aacHcbTable[cb][cw].offset;
1530 uint8_t extraBits = aacHcbTable[cb][cw].extraBits;
1532 m_reader.skipBits(aacHcbN[cb]);
1533 offset += m_reader.showBits<uint16>(extraBits);
1534 m_reader.skipBits(aacHcb2PairTable[cb][offset].bits - aacHcbN[cb]);
1536 m_reader.skipBits(aacHcb2PairTable[cb][offset].bits);
1538 if (offset > aacHcb2PairTableSize[cb]) {
1539 throw InvalidDataException();
1541 sp[0] = aacHcb2PairTable[cb][offset].x;
1542 sp[1] = aacHcb2PairTable[cb][offset].y;
1545 void AacFrameElementParser::huffmanBinaryPairSign(
byte cb, int16 *sp)
1548 huffmanBinaryPair(cb, sp);
1549 }
catch (
const InvalidDataException &) {
1550 huffmanSignBits(sp, 2);
1553 huffmanSignBits(sp, 2);
1556 void AacFrameElementParser::huffman2StepPairSign(
byte cb, int16 *sp)
1559 huffman2StepPair(cb, sp);
1560 }
catch (
const InvalidDataException &) {
1561 huffmanSignBits(sp, 2);
1564 huffmanSignBits(sp, 2);
1567 int16 AacFrameElementParser::huffmanGetEscape(int16 sp)
1580 for (size = 4; m_reader.readBit(); ++size) {
1582 const int16 off = m_reader.readBits<int16>(size);
1583 return static_cast<int16>(neg ? -(off | (1 << size)) : (off | (1 << size)));
1586 void AacFrameElementParser::vcb11CheckLav(
byte cb, int16 *sp)
1588 static const uint16_t vcb11LavTab[] = { 16, 31, 47, 63, 95, 127, 159, 191, 223, 255, 319, 383, 511, 767, 1023, 2047 };
1590 if (cb < 16 || cb > 31)
1592 max = vcb11LavTab[cb - 16];
1593 if ((abs(sp[0]) > max) || (abs(sp[1]) > max)) {
1602 void AacFrameElementParser::calculateWindowGroupingInfo(AacIcsInfo &ics)
1604 using namespace AacIcsSequenceTypes;
1605 switch (ics.windowSequence) {
1606 case OnlyLongSequence:
1607 case LongStartSequence:
1608 case LongStopSequence:
1609 ics.windowCount = ics.windowGroupCount = ics.windowGroupLengths[0] = 1;
1611 if (m_frameLength == 512) {
1612 ics.swbCount = swb512WindowCount[m_mpeg4SamplingFrequencyIndex];
1614 ics.swbCount = swb480WindowCount[m_mpeg4SamplingFrequencyIndex];
1617 if (m_frameLength == 1024) {
1618 ics.swbCount = swb1024WindowCount[m_mpeg4SamplingFrequencyIndex];
1620 ics.swbCount = swb960WindowCount[m_mpeg4SamplingFrequencyIndex];
1623 if (ics.maxSfb > ics.swbCount) {
1624 throw InvalidDataException();
1627 if (m_frameLength == 512) {
1628 for (
byte i = 0; i <= ics.swbCount; ++i) {
1629 ics.sectionSfbOffset[0][i] = swbOffset512Window[m_mpeg4SamplingFrequencyIndex][i];
1630 ics.swbOffset[i] = swbOffset512Window[m_mpeg4SamplingFrequencyIndex][i];
1633 for (
byte i = 0; i <= ics.swbCount; ++i) {
1634 ics.sectionSfbOffset[0][i] = swbOffset480Window[m_mpeg4SamplingFrequencyIndex][i];
1635 ics.swbOffset[i] = swbOffset480Window[m_mpeg4SamplingFrequencyIndex][i];
1639 for (
byte i = 0; i <= ics.swbCount; ++i) {
1640 ics.sectionSfbOffset[0][i] = swbOffset1024Window[m_mpeg4SamplingFrequencyIndex][i];
1641 ics.swbOffset[i] = swbOffset1024Window[m_mpeg4SamplingFrequencyIndex][i];
1644 ics.sectionSfbOffset[0][ics.swbCount] = ics.swbOffset[ics.swbCount] = ics.maxSwbOffset = m_frameLength;
1646 case EightShortSequence:
1647 ics.windowCount = 8;
1648 ics.windowGroupCount = ics.windowGroupLengths[0] = 1;
1649 ics.swbCount = swb128WindowCount[m_mpeg4SamplingFrequencyIndex];
1650 if (ics.maxSfb > ics.swbCount) {
1651 throw InvalidDataException();
1653 for (
byte i = 0; i < ics.swbCount; ++i) {
1654 ics.swbOffset[i] = swbOffset128Window[m_mpeg4SamplingFrequencyIndex][i];
1656 ics.swbOffset[ics.swbCount] = ics.maxSwbOffset = m_frameLength / 8;
1657 for (
byte i = 0; i < ics.windowCount - 1; ++i) {
1658 if (!(ics.scaleFactorGrouping & (1 << (6 - i)))) {
1659 ics.windowGroupLengths[ics.windowGroupCount] = 1;
1660 ++ics.windowGroupCount;
1662 ++(ics.windowGroupLengths[ics.windowGroupCount - 1]);
1665 for (
byte g = 0; g < ics.windowGroupCount; ++g) {
1666 byte sectionSfb = 0;
1667 uint16 offset = 0, width;
1668 for (
byte i = 0; i < ics.swbCount; ++i) {
1669 if (i + 1 == ics.swbCount) {
1670 width = (m_frameLength / 8) - swbOffset128Window[m_mpeg4SamplingFrequencyIndex][i];
1672 width = swbOffset128Window[m_mpeg4SamplingFrequencyIndex][i + 1] - swbOffset128Window[m_mpeg4SamplingFrequencyIndex][i];
1674 width *= ics.windowGroupLengths[g];
1675 ics.sectionSfbOffset[g][sectionSfb++] = offset;
1678 ics.sectionSfbOffset[g][sectionSfb] = offset;
1682 throw InvalidDataException();
1689 void AacFrameElementParser::parseIndividualChannelStream(AacIcsInfo &ics, int16 *specData,
bool scaleFlag)
1691 parseSideInfo(ics, scaleFlag);
1693 if (ics.tnsDataPresent) {
1699 throw NotImplementedException();
1701 if (m_aacSpectralDataResilienceFlag) {
1703 throw NotImplementedException();
1705 parseSpectralData(ics, specData);
1707 if (ics.pulseDataPresent) {
1708 if (ics.windowSequence == AacIcsSequenceTypes::EightShortSequence) {
1709 throw InvalidDataException();
1720 void AacFrameElementParser::parseSingleChannelElement()
1722 if (m_elementCount + 1 > aacMaxSyntaxElements) {
1723 throw NotImplementedException();
1727 int16 specData[1024] = { 0 };
1728 m_elementId[m_elementCount] = AacSyntaxElementTypes::SingleChannelElement;
1729 m_elementInstanceTag[m_elementCount] = m_reader.readBits<
byte>(4);
1732 parseIndividualChannelStream(m_ics1, specData);
1733 if (m_ics1.isUsed) {
1734 throw InvalidDataException();
1737 if (m_reader.showBits<
byte>(3) == AacSyntaxElementTypes::FillElement) {
1738 parseFillElement(m_elementCount);
1742 m_channelCount += m_elementChannelCount[m_elementCount];
1749 void AacFrameElementParser::parseChannelPairElement()
1751 if (m_elementCount + 2 > aacMaxSyntaxElements) {
1752 throw NotImplementedException();
1755 m_elementId[m_elementCount] = AacSyntaxElementTypes::ChannelPairElement;
1756 m_elementChannelCount[m_elementCount] = 2;
1758 int16 specData1[1024] = { 0 };
1759 int16 specData2[1024] = { 0 };
1762 m_elementInstanceTag[m_elementCount] = m_reader.readBits<
byte>(4);
1763 if ((m_commonWindow = m_reader.readBit())) {
1765 parseIcsInfo(m_ics1);
1766 if ((m_ics1.midSideCodingMaskPresent = m_reader.readBits<
byte>(2) == 1)) {
1767 for (
byte g = 0; g < m_ics1.windowGroupCount; ++g) {
1768 for (
byte sfb = 0; sfb < m_ics1.maxSfb; ++sfb) {
1769 m_ics1.midSideCodingUsed[g][sfb] = m_reader.readBit();
1775 if ((m_ics1.ltp1.dataPresent = m_reader.readBit())) {
1776 parseLtpInfo(m_ics1, m_ics1.ltp1);
1781 m_ics1.midSideCodingMaskPresent =
false;
1783 parseIndividualChannelStream(m_ics1, specData1);
1785 if ((m_ics1.ltp2.dataPresent = m_reader.readBit())) {
1786 parseLtpInfo(m_ics1, m_ics1.ltp2);
1789 parseIndividualChannelStream(m_ics2, specData2);
1791 if (m_reader.showBits<
byte>(3) == AacSyntaxElementTypes::FillElement) {
1792 parseFillElement(m_elementCount);
1796 m_channelCount += 2;
1803 void AacFrameElementParser::parseCouplingChannelElement()
1805 m_reader.skipBits(4);
1806 byte swCceFlag = m_reader.readBit();
1807 byte coupledElementCount = m_reader.readBits<
byte>(3);
1808 byte gainElementLists = 0;
1809 for (
byte c = 0; c < coupledElementCount; ++c) {
1811 byte ccTargetIsCpe = m_reader.readBit();
1813 m_reader.skipBits(4);
1814 if (ccTargetIsCpe) {
1816 if (m_reader.readBit() & m_reader.readBit()) {
1821 m_reader.skipBits(4);
1823 int16 specData[1024];
1824 parseIndividualChannelStream(ics, specData);
1825 for (
byte c = 1; c < gainElementLists; ++c) {
1826 if (swCceFlag || m_reader.readBit()) {
1827 parseHuffmanScaleFactor();
1829 for (
byte group = 0; group < ics.windowCount; ++group) {
1830 for (
byte sfb = 0; sfb < ics.maxSfb; ++sfb) {
1831 if (ics.sfbCb[group][sfb] != AacScaleFactorTypes::ZeroHcb) {
1832 parseHuffmanScaleFactor();
1843 void AacFrameElementParser::parseLowFrequencyElement()
1845 parseSingleChannelElement();
1851 void AacFrameElementParser::parseDataStreamElement()
1853 byte byteAligned = m_reader.readBit();
1854 uint16 count = m_reader.readBits<uint16>(8);
1855 if (count == 0xFF) {
1856 count += m_reader.readBits<uint16>(8);
1861 m_reader.skipBits(count * 8);
1867 void AacFrameElementParser::parseProgramConfigElement()
1869 m_pce.elementInstanceTag = m_reader.readBits<
byte>(4);
1870 m_pce.objectType = m_reader.readBits<
byte>(2);
1871 m_pce.samplingFrequencyIndex = m_reader.readBits<
byte>(4);
1872 m_pce.frontChannelElementCount = m_reader.readBits<
byte>(4);
1873 m_pce.sideChannelElementCount = m_reader.readBits<
byte>(4);
1874 m_pce.backChannelElementCount = m_reader.readBits<
byte>(4);
1875 m_pce.lfeChannelElementCount = m_reader.readBits<
byte>(2);
1876 m_pce.assocDataElementCount = m_reader.readBits<
byte>(3);
1877 m_pce.validCcElementCount = m_reader.readBits<
byte>(4);
1878 if ((m_pce.monoMixdownPresent = m_reader.readBit())) {
1879 m_pce.monoMixdownElementNumber = m_reader.readBits<
byte>(4);
1881 if ((m_pce.stereoMixdownPresent = m_reader.readBit())) {
1882 m_pce.stereoMixdownElementNumber = m_reader.readBits<
byte>(4);
1884 if ((m_pce.matrixMixdownIdxPresent = m_reader.readBit())) {
1885 m_pce.matrixMixdownIdx = m_reader.readBits<
byte>(2);
1886 m_pce.pseudoSurroundEnable = m_reader.readBit();
1889 for (i = 0; i < m_pce.frontChannelElementCount; ++i) {
1890 m_pce.frontElementIsCpe[i] = m_reader.readBit();
1891 m_pce.frontElementTagSelect[i] = m_reader.readBits<
byte>(4);
1892 if (m_pce.frontElementIsCpe[i]) {
1893 m_pce.cpeChannel[m_pce.frontElementTagSelect[i]] = m_pce.channels;
1894 m_pce.frontChannelCount += 2;
1895 m_pce.channels += 2;
1897 m_pce.sceChannel[m_pce.frontElementTagSelect[i]] = m_pce.channels;
1898 ++m_pce.frontChannelCount;
1902 for (i = 0; i < m_pce.sideChannelElementCount; ++i) {
1903 m_pce.sideElementIsCpe[i] = m_reader.readBit();
1904 m_pce.sideElementTagSelect[i] = m_reader.readBits<
byte>(4);
1905 if (m_pce.sideElementIsCpe[i]) {
1906 m_pce.cpeChannel[m_pce.sideElementTagSelect[i]] = m_pce.channels;
1907 m_pce.sideChannelCount += 2;
1908 m_pce.channels += 2;
1910 m_pce.sceChannel[m_pce.sideElementTagSelect[i]] = m_pce.channels;
1911 ++m_pce.sideChannelCount;
1915 for (i = 0; i < m_pce.backChannelElementCount; ++i) {
1916 m_pce.backElementIsCpe[i] = m_reader.readBit();
1917 m_pce.backElementTagSelect[i] = m_reader.readBits<
byte>(4);
1918 if (m_pce.backElementIsCpe[i]) {
1919 m_pce.cpeChannel[m_pce.backElementTagSelect[i]] = m_pce.channels;
1920 m_pce.backChannelCount += 2;
1921 m_pce.channels += 2;
1923 m_pce.sceChannel[m_pce.backElementTagSelect[i]] = m_pce.channels;
1924 ++m_pce.backChannelCount;
1928 for (i = 0; i < m_pce.lfeChannelElementCount; ++i) {
1929 m_pce.lfeElementTagSelect[i] = m_reader.readBits<
byte>(4);
1930 m_pce.sceChannel[m_pce.lfeElementTagSelect[i]] = m_pce.channels;
1931 ++m_pce.lfeChannelCount;
1934 for (i = 0; i < m_pce.assocDataElementCount; ++i) {
1935 m_pce.assocDataElementTagSelect[i] = m_reader.readBits<
byte>(4);
1937 for (i = 0; i < m_pce.validCcElementCount; ++i) {
1938 m_pce.ccElementIsIndSw[i] = m_reader.readBit();
1939 m_pce.validCcElementTagSelect[i] = m_reader.readBits<
byte>(4);
1942 m_pce.commentFieldBytes = m_reader.readBits<
byte>(8);
1943 for (i = 0; i < m_pce.commentFieldBytes; ++i) {
1944 m_pce.commentFieldData[i] = m_reader.readBits<
byte>(8);
1946 m_pce.commentFieldData[i] = 0;
1947 if (m_pce.channels > aacMaxChannels) {
1948 throw NotImplementedException();
1955 void AacFrameElementParser::parseFillElement(
byte sbrElement)
1957 uint16 count = m_reader.readBits<
byte>(4);
1960 count += m_reader.readBits<
byte>(8);
1964 switch (m_reader.readBits<
byte>(4)) {
1965 using namespace AacExtensionTypes;
1967 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(4 + 8 * (count - 1));
1995 if (m_reader.readBits<
byte>(4) == 0) {
1997 byte dataElementLength = 0, loopCounter = 0;
1998 uint16 dataElementLengthPart;
2000 dataElementLengthPart = m_reader.readBits<
byte>(8);
2001 dataElementLength += dataElementLengthPart;
2003 }
while (dataElementLengthPart == 0xFF);
2004 for (uint16 i = 0; i < dataElementLength; ++i) {
2005 m_reader.skipBits(8);
2006 count -= dataElementLength + loopCounter + 1;
2011 m_reader.skipBits(8 * (count - 1));
2017 m_reader.skipBits(4 + 8 * (count - 1));
2028 void AacFrameElementParser::parseRawDataBlock()
2032 switch (m_reader.readBits<
byte>(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();
2105 void AacFrameElementParser::parse(
const AdtsFrame &adtsFrame, std::istream &stream, std::size_t dataSize)
2107 auto data = make_unique<char[]>(dataSize);
2108 stream.read(data.get(), dataSize);
2109 parse(adtsFrame, data, dataSize);
2115 void 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();
Contains utility classes helping to read and write streams.
Contains all classes and functions of the TagInfo library.
uint32 mpeg4SamplingFrequencyTable[13]