1 /*
2  *  Copyright (C) 2005-2021 Team Kodi (https://kodi.tv)
3  *
4  *  SPDX-License-Identifier: GPL-2.0-or-later
5  *  See LICENSE.md for more information.
6  */
7 
8 #include "ICS.h"
9 
10 #include "../BitStream.h"
11 #include "../huffman/Decoder.h"
12 #include "ICSInfo.h"
13 
14 #include <stdexcept>
15 
16 using namespace aac;
17 using namespace aac::elements;
18 
ICS()19 ICS::ICS() : m_icsInfo(new ICSInfo)
20 {
21 }
22 
23 ICS::~ICS() = default;
24 
Decode(bool commonWindow,BitStream & stream,int profile,int sampleFrequencyIndex)25 void ICS::Decode(bool commonWindow, BitStream& stream, int profile, int sampleFrequencyIndex)
26 {
27   // 8 bits global gain
28   stream.SkipBits(8);
29 
30   if (!commonWindow)
31     m_icsInfo->Decode(commonWindow, stream, profile, sampleFrequencyIndex);
32 
33   DecodeSectionData(stream);
34   DecodeScaleFactorData(stream);
35 
36   const bool pulseDataPresent = stream.ReadBool();
37   if (pulseDataPresent)
38   {
39     if (m_icsInfo->GetWindowSequence() == EIGHT_SHORT_SEQUENCE)
40       throw std::logic_error(
41           "aac::elements::ICS::Decode - Pulse data not allowed for short frames");
42 
43     DecodePulseData(stream);
44   }
45 
46   const bool tnsDataPresent = stream.ReadBool();
47   if (tnsDataPresent)
48     DecodeTNSData(stream);
49 
50   const bool gainControlDataPresent = stream.ReadBool();
51   if (gainControlDataPresent)
52     DecodeGainControlData(stream);
53 
54   DecodeSpectralData(stream);
55 }
56 
DecodeSectionData(BitStream & stream)57 void ICS::DecodeSectionData(BitStream& stream)
58 {
59   const int bits = (m_icsInfo->GetWindowSequence() == EIGHT_SHORT_SEQUENCE) ? 3 : 5;
60   const int escVal = (1 << bits) - 1;
61 
62   const int windowGroupCount = m_icsInfo->GetWindowGroupCount();
63   const int maxSFB = m_icsInfo->GetMaxSFB();
64 
65   int idx = 0;
66   for (int g = 0; g < windowGroupCount; ++g)
67   {
68     int k = 0;
69     while (k < maxSFB)
70     {
71       int end = k;
72       const int sectCB = stream.ReadBits(4);
73 
74       if (sectCB == 12)
75         throw std::logic_error(
76             "aac::elements::ICS::DecodeSectionData - Invalid huffman codebook: 12");
77 
78       int incr;
79       while ((incr = stream.ReadBits(bits)) == escVal && stream.GetBitsLeft() >= bits)
80         end += incr;
81 
82       end += incr;
83 
84       if (stream.GetBitsLeft() < 0 || incr == escVal)
85         throw std::logic_error("aac::elements::ICS::DecodeSectionData - stream past eof");
86 
87       if (end > m_icsInfo->GetMaxSFB())
88         throw std::logic_error("aac::elements::ICS::DecodeSectionData - Too many bands");
89 
90       for (; k < end; ++k)
91       {
92         m_sfbCB[idx] = sectCB;
93         m_sectEnd[idx++] = end;
94       }
95     }
96   }
97 }
98 
DecodeScaleFactorData(BitStream & stream)99 void ICS::DecodeScaleFactorData(BitStream& stream)
100 {
101   bool noiseFlag = true;
102 
103   const int windowGroupCount = m_icsInfo->GetWindowGroupCount();
104   const int maxSFB = m_icsInfo->GetMaxSFB();
105 
106   int idx = 0;
107   for (int g = 0; g < windowGroupCount; ++g)
108   {
109     for (int sfb = 0; sfb < maxSFB;)
110     {
111       const int end = m_sectEnd[idx];
112       switch (m_sfbCB[idx])
113       {
114         case huffman::ZERO_HCB:
115           for (; sfb < end; ++sfb, ++idx)
116             ;
117           break;
118         case huffman::INTENSITY_HCB:
119         case huffman::INTENSITY_HCB2:
120           for (; sfb < end; ++sfb, ++idx)
121           {
122             static constexpr int SF_DELTA = 60;
123 
124             if (huffman::Decoder::DecodeScaleFactor(stream) - SF_DELTA > 255)
125               throw std::logic_error(
126                   "aac::elements::ICS::DecodeScaleFactor - Scalefactor out of range");
127           }
128           break;
129         case huffman::NOISE_HCB:
130           for (; sfb < end; ++sfb, ++idx)
131           {
132             if (noiseFlag)
133             {
134               stream.SkipBits(9);
135               noiseFlag = false;
136             }
137             else
138             {
139               huffman::Decoder::DecodeScaleFactor(stream);
140             }
141           }
142           break;
143         default:
144           for (; sfb < end; ++sfb, ++idx)
145           {
146             huffman::Decoder::DecodeScaleFactor(stream);
147           }
148           break;
149       }
150     }
151   }
152 }
153 
DecodePulseData(BitStream & stream)154 void ICS::DecodePulseData(BitStream& stream)
155 {
156   const int pulseCount = stream.ReadBits(2);
157 
158   // 6 bits pulse start sfb
159   stream.SkipBits(6);
160 
161   //    for (int i = 0; i < pulseCount + 1; ++i)
162   //    {
163   //      // 5 bits pulse offset, 4 bits pulse amp
164   //      m_stream.SkipBits(9);
165   //    }
166   stream.SkipBits(9 * (pulseCount + 1));
167 }
168 
DecodeTNSData(BitStream & stream)169 void ICS::DecodeTNSData(BitStream& stream)
170 {
171   int bits[3];
172   if (m_icsInfo->GetWindowSequence() == EIGHT_SHORT_SEQUENCE)
173   {
174     bits[0] = 1;
175     bits[1] = 4;
176     bits[2] = 3;
177   }
178   else
179   {
180     bits[0] = 2;
181     bits[1] = 6;
182     bits[2] = 5;
183   }
184 
185   const int windowCount = m_icsInfo->GetWindowCount();
186 
187   for (int w = 0; w < windowCount; ++w)
188   {
189     const int nFilt = stream.ReadBits(bits[0]);
190     if (nFilt != 0)
191     {
192       const int coefRes = stream.ReadBit();
193 
194       for (int filt = 0; filt < nFilt; ++filt)
195       {
196         stream.SkipBits(bits[1]);
197 
198         const int order = stream.ReadBits(bits[2]);
199         if (order != 0)
200         {
201           // 1 bit direction
202           stream.SkipBit();
203 
204           const int coefCompress = stream.ReadBit();
205           const int coefLen = coefRes + 3 - coefCompress;
206 
207           //            for (int i = 0; i < order; ++i)
208           //            {
209           //              m_stream.SkipBits(coefLen);
210           //            }
211           stream.SkipBits(order * coefLen);
212         }
213       }
214     }
215   }
216 }
217 
DecodeGainControlData(BitStream & stream)218 void ICS::DecodeGainControlData(BitStream& stream)
219 {
220   const int maxBand = stream.ReadBits(2) + 1;
221 
222   int wdLen, locBits, locBits2 = 0;
223   switch (m_icsInfo->GetWindowSequence())
224   {
225     case ONLY_LONG_SEQUENCE:
226       wdLen = 1;
227       locBits = 5;
228       locBits2 = 5;
229       break;
230     case EIGHT_SHORT_SEQUENCE:
231       wdLen = 8;
232       locBits = 2;
233       locBits2 = 2;
234       break;
235     case LONG_START_SEQUENCE:
236       wdLen = 2;
237       locBits = 4;
238       locBits2 = 2;
239       break;
240     case LONG_STOP_SEQUENCE:
241       wdLen = 2;
242       locBits = 4;
243       locBits2 = 5;
244       break;
245     default:
246       return;
247   }
248 
249   for (int bd = 1; bd < maxBand; ++bd)
250   {
251     for (int wd = 0; wd < wdLen; ++wd)
252     {
253       const int len = stream.ReadBits(3);
254       for (int k = 0; k < len; ++k)
255       {
256         stream.SkipBits(4);
257         const int bits = (wd == 0) ? locBits : locBits2;
258         stream.SkipBits(bits);
259       }
260     }
261   }
262 }
263 
DecodeSpectralData(BitStream & stream)264 void ICS::DecodeSpectralData(BitStream& stream)
265 {
266   const int windowGroupCount = m_icsInfo->GetWindowGroupCount();
267   const int maxSFB = m_icsInfo->GetMaxSFB();
268   const uint16_t* swbOffsets = m_icsInfo->GetSWBOffsets();
269 
270   int idx = 0;
271   for (int g = 0; g < windowGroupCount; ++g)
272   {
273     const int groupLen = m_icsInfo->GetWindowGroupLengths()[g];
274 
275     for (int sfb = 0; sfb < maxSFB; ++sfb, ++idx)
276     {
277       const int hcb = m_sfbCB[idx];
278       const int width = swbOffsets[sfb + 1] - swbOffsets[sfb];
279 
280       if (hcb == huffman::ZERO_HCB || hcb == huffman::INTENSITY_HCB ||
281           hcb == huffman::INTENSITY_HCB2 || hcb == huffman::NOISE_HCB)
282       {
283         continue;
284       }
285       else
286       {
287         for (int w = 0; w < groupLen; ++w)
288         {
289           static constexpr int FIRST_PAIR_HCB = 5;
290 
291           const int num = (hcb >= FIRST_PAIR_HCB) ? 2 : 4;
292           for (int k = 0; k < width; k += num)
293           {
294             int buf[4] = {};
295             huffman::Decoder::DecodeSpectralData(stream, hcb, buf, 0);
296           }
297         }
298       }
299     }
300   }
301 }
302