blob: f782f396dca8faba710f8e1bc1bf0b130c4a1361
1 | /* ***** BEGIN LICENSE BLOCK ***** |
2 | * Source last modified: $Id: noiseless.c,v 1.1 2005/02/26 01:47:35 jrecker Exp $ |
3 | * |
4 | * Portions Copyright (c) 1995-2005 RealNetworks, Inc. All Rights Reserved. |
5 | * |
6 | * The contents of this file, and the files included with this file, |
7 | * are subject to the current version of the RealNetworks Public |
8 | * Source License (the "RPSL") available at |
9 | * http://www.helixcommunity.org/content/rpsl unless you have licensed |
10 | * the file under the current version of the RealNetworks Community |
11 | * Source License (the "RCSL") available at |
12 | * http://www.helixcommunity.org/content/rcsl, in which case the RCSL |
13 | * will apply. You may also obtain the license terms directly from |
14 | * RealNetworks. You may not use this file except in compliance with |
15 | * the RPSL or, if you have a valid RCSL with RealNetworks applicable |
16 | * to this file, the RCSL. Please see the applicable RPSL or RCSL for |
17 | * the rights, obligations and limitations governing use of the |
18 | * contents of the file. |
19 | * |
20 | * This file is part of the Helix DNA Technology. RealNetworks is the |
21 | * developer of the Original Code and owns the copyrights in the |
22 | * portions it created. |
23 | * |
24 | * This file, and the files included with this file, is distributed |
25 | * and made available on an 'AS IS' basis, WITHOUT WARRANTY OF ANY |
26 | * KIND, EITHER EXPRESS OR IMPLIED, AND REALNETWORKS HEREBY DISCLAIMS |
27 | * ALL SUCH WARRANTIES, INCLUDING WITHOUT LIMITATION, ANY WARRANTIES |
28 | * OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, QUIET |
29 | * ENJOYMENT OR NON-INFRINGEMENT. |
30 | * |
31 | * Technology Compatibility Kit Test Suite(s) Location: |
32 | * http://www.helixcommunity.org/content/tck |
33 | * |
34 | * Contributor(s): |
35 | * |
36 | * ***** END LICENSE BLOCK ***** */ |
37 | |
38 | /************************************************************************************** |
39 | * Fixed-point HE-AAC decoder |
40 | * Jon Recker (jrecker@real.com) |
41 | * February 2005 |
42 | * |
43 | * noiseless.c - decode channel info, scalefactors, quantized coefficients, |
44 | * scalefactor band codebook, and TNS coefficients from bitstream |
45 | **************************************************************************************/ |
46 | |
47 | #include "coder.h" |
48 | //#include <core/dsp.h> |
49 | #include <stdio.h> |
50 | |
51 | |
52 | /************************************************************************************** |
53 | * Function: DecodeICSInfo |
54 | * |
55 | * Description: decode individual channel stream info |
56 | * |
57 | * Inputs: BitStreamInfo struct pointing to start of ICS info |
58 | * (14496-3, table 4.4.6) |
59 | * sample rate index |
60 | * |
61 | * Outputs: updated icsInfo struct |
62 | * |
63 | * Return: none |
64 | **************************************************************************************/ |
65 | void DecodeICSInfo(BitStreamInfo *bsi, ICSInfo *icsInfo, int sampRateIdx) |
66 | { |
67 | int sfb, g, mask; |
68 | |
69 | icsInfo->icsResBit = GetBits(bsi, 1); |
70 | icsInfo->winSequence = GetBits(bsi, 2); |
71 | icsInfo->winShape = GetBits(bsi, 1); |
72 | if (icsInfo->winSequence == 2) { |
73 | /* short block */ |
74 | icsInfo->maxSFB = GetBits(bsi, 4); |
75 | icsInfo->sfGroup = GetBits(bsi, 7); |
76 | icsInfo->numWinGroup = 1; |
77 | icsInfo->winGroupLen[0] = 1; |
78 | mask = 0x40; /* start with bit 6 */ |
79 | for (g = 0; g < 7; g++) { |
80 | if (icsInfo->sfGroup & mask) { |
81 | icsInfo->winGroupLen[icsInfo->numWinGroup - 1]++; |
82 | } else { |
83 | icsInfo->numWinGroup++; |
84 | icsInfo->winGroupLen[icsInfo->numWinGroup - 1] = 1; |
85 | } |
86 | mask >>= 1; |
87 | } |
88 | } else { |
89 | /* long block */ |
90 | icsInfo->maxSFB = GetBits(bsi, 6); |
91 | icsInfo->predictorDataPresent = GetBits(bsi, 1); |
92 | if (icsInfo->predictorDataPresent) { |
93 | icsInfo->predictorReset = GetBits(bsi, 1); |
94 | if (icsInfo->predictorReset) { |
95 | icsInfo->predictorResetGroupNum = GetBits(bsi, 5); |
96 | } |
97 | for (sfb = 0; sfb < MIN(icsInfo->maxSFB, predSFBMax[sampRateIdx]); sfb++) { |
98 | icsInfo->predictionUsed[sfb] = GetBits(bsi, 1); |
99 | } |
100 | } |
101 | icsInfo->numWinGroup = 1; |
102 | icsInfo->winGroupLen[0] = 1; |
103 | } |
104 | } |
105 | |
106 | /************************************************************************************** |
107 | * Function: DecodeSectionData |
108 | * |
109 | * Description: decode section data (scale factor band groupings and |
110 | * associated Huffman codebooks) |
111 | * |
112 | * Inputs: BitStreamInfo struct pointing to start of ICS info |
113 | * (14496-3, table 4.4.25) |
114 | * window sequence (short or long blocks) |
115 | * number of window groups (1 for long blocks, 1-8 for short blocks) |
116 | * max coded scalefactor band |
117 | * |
118 | * Outputs: index of Huffman codebook for each scalefactor band in each section |
119 | * |
120 | * Return: none |
121 | * |
122 | * Notes: sectCB, sectEnd, sfbCodeBook, ordered by window groups for short blocks |
123 | **************************************************************************************/ |
124 | static void DecodeSectionData(BitStreamInfo *bsi, int winSequence, int numWinGrp, int maxSFB, unsigned char *sfbCodeBook) |
125 | { |
126 | int g, cb, sfb; |
127 | int sectLen, sectLenBits, sectLenIncr, sectEscapeVal; |
128 | |
129 | sectLenBits = (winSequence == 2 ? 3 : 5); |
130 | sectEscapeVal = (1 << sectLenBits) - 1; |
131 | |
132 | for (g = 0; g < numWinGrp; g++) { |
133 | sfb = 0; |
134 | while (sfb < maxSFB) { |
135 | cb = GetBits(bsi, 4); /* next section codebook */ |
136 | sectLen = 0; |
137 | do { |
138 | sectLenIncr = GetBits(bsi, sectLenBits); |
139 | sectLen += sectLenIncr; |
140 | } while (sectLenIncr == sectEscapeVal); |
141 | |
142 | sfb += sectLen; |
143 | while (sectLen--) { |
144 | *sfbCodeBook++ = (unsigned char)cb; |
145 | } |
146 | } |
147 | ASSERT(sfb == maxSFB); |
148 | } |
149 | } |
150 | |
151 | /************************************************************************************** |
152 | * Function: DecodeOneScaleFactor |
153 | * |
154 | * Description: decode one scalefactor using scalefactor Huffman codebook |
155 | * |
156 | * Inputs: BitStreamInfo struct pointing to start of next coded scalefactor |
157 | * |
158 | * Outputs: updated BitstreamInfo struct |
159 | * |
160 | * Return: one decoded scalefactor, including index_offset of -60 |
161 | **************************************************************************************/ |
162 | static int DecodeOneScaleFactor(BitStreamInfo *bsi) |
163 | { |
164 | int nBits, val; |
165 | unsigned int bitBuf; |
166 | |
167 | /* decode next scalefactor from bitstream */ |
168 | bitBuf = GetBitsNoAdvance(bsi, huffTabScaleFactInfo.maxBits) << (32 - huffTabScaleFactInfo.maxBits); |
169 | nBits = DecodeHuffmanScalar(huffTabScaleFact, &huffTabScaleFactInfo, bitBuf, &val); |
170 | AdvanceBitstream(bsi, nBits); |
171 | |
172 | return val; |
173 | } |
174 | |
175 | /************************************************************************************** |
176 | * Function: DecodeScaleFactors |
177 | * |
178 | * Description: decode scalefactors, PNS energy, and intensity stereo weights |
179 | * |
180 | * Inputs: BitStreamInfo struct pointing to start of ICS info |
181 | * (14496-3, table 4.4.26) |
182 | * number of window groups (1 for long blocks, 1-8 for short blocks) |
183 | * max coded scalefactor band |
184 | * global gain (starting value for differential scalefactor coding) |
185 | * index of Huffman codebook for each scalefactor band in each section |
186 | * |
187 | * Outputs: decoded scalefactor for each section |
188 | * |
189 | * Return: none |
190 | * |
191 | * Notes: sfbCodeBook, scaleFactors ordered by window groups for short blocks |
192 | * for section with codebook 13, scaleFactors buffer has decoded PNS |
193 | * energy instead of regular scalefactor |
194 | * for section with codebook 14 or 15, scaleFactors buffer has intensity |
195 | * stereo weight instead of regular scalefactor |
196 | **************************************************************************************/ |
197 | static void DecodeScaleFactors(BitStreamInfo *bsi, int numWinGrp, int maxSFB, int globalGain, |
198 | unsigned char *sfbCodeBook, short *scaleFactors) |
199 | { |
200 | int g, sfbCB, nrg, npf, val, sf, is; |
201 | |
202 | /* starting values for differential coding */ |
203 | sf = globalGain; |
204 | is = 0; |
205 | nrg = globalGain - 90 - 256; |
206 | npf = 1; |
207 | |
208 | for (g = 0; g < numWinGrp * maxSFB; g++) { |
209 | sfbCB = *sfbCodeBook++; |
210 | |
211 | if (sfbCB == 14 || sfbCB == 15) { |
212 | /* intensity stereo - differential coding */ |
213 | val = DecodeOneScaleFactor(bsi); |
214 | is += val; |
215 | *scaleFactors++ = (short)is; |
216 | } else if (sfbCB == 13) { |
217 | /* PNS - first energy is directly coded, rest are Huffman coded (npf = noise_pcm_flag) */ |
218 | if (npf) { |
219 | val = GetBits(bsi, 9); |
220 | npf = 0; |
221 | } else { |
222 | val = DecodeOneScaleFactor(bsi); |
223 | } |
224 | nrg += val; |
225 | *scaleFactors++ = (short)nrg; |
226 | } else if (sfbCB >= 1 && sfbCB <= 11) { |
227 | /* regular (non-zero) region - differential coding */ |
228 | val = DecodeOneScaleFactor(bsi); |
229 | sf += val; |
230 | *scaleFactors++ = (short)sf; |
231 | } else { |
232 | /* inactive scalefactor band if codebook 0 */ |
233 | *scaleFactors++ = 0; |
234 | } |
235 | } |
236 | } |
237 | |
238 | /************************************************************************************** |
239 | * Function: DecodePulseInfo |
240 | * |
241 | * Description: decode pulse information |
242 | * |
243 | * Inputs: BitStreamInfo struct pointing to start of pulse info |
244 | * (14496-3, table 4.4.7) |
245 | * |
246 | * Outputs: updated PulseInfo struct |
247 | * |
248 | * Return: none |
249 | **************************************************************************************/ |
250 | static void DecodePulseInfo(BitStreamInfo *bsi, PulseInfo *pi) |
251 | { |
252 | int i; |
253 | |
254 | pi->numPulse = GetBits(bsi, 2) + 1; /* add 1 here */ |
255 | pi->startSFB = GetBits(bsi, 6); |
256 | for (i = 0; i < pi->numPulse; i++) { |
257 | pi->offset[i] = GetBits(bsi, 5); |
258 | pi->amp[i] = GetBits(bsi, 4); |
259 | } |
260 | } |
261 | |
262 | /************************************************************************************** |
263 | * Function: DecodeTNSInfo |
264 | * |
265 | * Description: decode TNS filter information |
266 | * |
267 | * Inputs: BitStreamInfo struct pointing to start of TNS info |
268 | * (14496-3, table 4.4.27) |
269 | * window sequence (short or long blocks) |
270 | * |
271 | * Outputs: updated TNSInfo struct |
272 | * buffer of decoded (signed) TNS filter coefficients |
273 | * |
274 | * Return: none |
275 | **************************************************************************************/ |
276 | static void DecodeTNSInfo(BitStreamInfo *bsi, int winSequence, TNSInfo *ti, signed char *tnsCoef) |
277 | { |
278 | int i, w, f, coefBits, compress; |
279 | signed char c, s, n; |
280 | signed char sgnMask[3] = { 0x02, 0x04, 0x08}; |
281 | signed char negMask[3] = {~0x03, ~0x07, ~0x0f}; |
282 | unsigned char *filtLength, *filtOrder, *filtDir; |
283 | |
284 | filtLength = ti->length; |
285 | filtOrder = ti->order; |
286 | filtDir = ti->dir; |
287 | |
288 | if (winSequence == 2) { |
289 | /* short blocks */ |
290 | for (w = 0; w < NWINDOWS_SHORT; w++) { |
291 | ti->numFilt[w] = GetBits(bsi, 1); |
292 | if (ti->numFilt[w]) { |
293 | ti->coefRes[w] = GetBits(bsi, 1) + 3; |
294 | *filtLength = GetBits(bsi, 4); |
295 | *filtOrder = GetBits(bsi, 3); |
296 | if (*filtOrder) { |
297 | *filtDir++ = GetBits(bsi, 1); |
298 | compress = GetBits(bsi, 1); |
299 | coefBits = (int)ti->coefRes[w] - compress; /* 2, 3, or 4 */ |
300 | s = sgnMask[coefBits - 2]; |
301 | n = negMask[coefBits - 2]; |
302 | for (i = 0; i < *filtOrder; i++) { |
303 | c = GetBits(bsi, coefBits); |
304 | if (c & s) { |
305 | c |= n; |
306 | } |
307 | *tnsCoef++ = c; |
308 | } |
309 | } |
310 | filtLength++; |
311 | filtOrder++; |
312 | } |
313 | } |
314 | } else { |
315 | /* long blocks */ |
316 | ti->numFilt[0] = GetBits(bsi, 2); |
317 | if (ti->numFilt[0]) { |
318 | ti->coefRes[0] = GetBits(bsi, 1) + 3; |
319 | } |
320 | for (f = 0; f < ti->numFilt[0]; f++) { |
321 | *filtLength = GetBits(bsi, 6); |
322 | *filtOrder = GetBits(bsi, 5); |
323 | if (*filtOrder) { |
324 | *filtDir++ = GetBits(bsi, 1); |
325 | compress = GetBits(bsi, 1); |
326 | coefBits = (int)ti->coefRes[0] - compress; /* 2, 3, or 4 */ |
327 | s = sgnMask[coefBits - 2]; |
328 | n = negMask[coefBits - 2]; |
329 | for (i = 0; i < *filtOrder; i++) { |
330 | c = GetBits(bsi, coefBits); |
331 | if (c & s) { |
332 | c |= n; |
333 | } |
334 | *tnsCoef++ = c; |
335 | } |
336 | } |
337 | filtLength++; |
338 | filtOrder++; |
339 | } |
340 | } |
341 | } |
342 | |
343 | /* bitstream field lengths for gain control data: |
344 | * gainBits[winSequence][0] = maxWindow (how many gain windows there are) |
345 | * gainBits[winSequence][1] = locBitsZero (bits for alocCode if window == 0) |
346 | * gainBits[winSequence][2] = locBits (bits for alocCode if window != 0) |
347 | */ |
348 | static const unsigned char gainBits[4][3] = { |
349 | {1, 5, 5}, /* long */ |
350 | {2, 4, 2}, /* start */ |
351 | {8, 2, 2}, /* short */ |
352 | {2, 4, 5}, /* stop */ |
353 | }; |
354 | |
355 | /************************************************************************************** |
356 | * Function: DecodeGainControlInfo |
357 | * |
358 | * Description: decode gain control information (SSR profile only) |
359 | * |
360 | * Inputs: BitStreamInfo struct pointing to start of gain control info |
361 | * (14496-3, table 4.4.12) |
362 | * window sequence (short or long blocks) |
363 | * |
364 | * Outputs: updated GainControlInfo struct |
365 | * |
366 | * Return: none |
367 | **************************************************************************************/ |
368 | static void DecodeGainControlInfo(BitStreamInfo *bsi, int winSequence, GainControlInfo *gi) |
369 | { |
370 | int bd, wd, ad; |
371 | int locBits, locBitsZero, maxWin; |
372 | |
373 | gi->maxBand = GetBits(bsi, 2); |
374 | maxWin = (int)gainBits[winSequence][0]; |
375 | locBitsZero = (int)gainBits[winSequence][1]; |
376 | locBits = (int)gainBits[winSequence][2]; |
377 | |
378 | for (bd = 1; bd <= gi->maxBand; bd++) { |
379 | for (wd = 0; wd < maxWin; wd++) { |
380 | gi->adjNum[bd][wd] = GetBits(bsi, 3); |
381 | for (ad = 0; ad < gi->adjNum[bd][wd]; ad++) { |
382 | gi->alevCode[bd][wd][ad] = GetBits(bsi, 4); |
383 | gi->alocCode[bd][wd][ad] = GetBits(bsi, (wd == 0 ? locBitsZero : locBits)); |
384 | } |
385 | } |
386 | } |
387 | } |
388 | |
389 | /************************************************************************************** |
390 | * Function: DecodeICS |
391 | * |
392 | * Description: decode individual channel stream |
393 | * |
394 | * Inputs: platform specific info struct |
395 | * BitStreamInfo struct pointing to start of individual channel stream |
396 | * (14496-3, table 4.4.24) |
397 | * index of current channel |
398 | * |
399 | * Outputs: updated section data, scale factor data, pulse data, TNS data, |
400 | * and gain control data |
401 | * |
402 | * Return: none |
403 | **************************************************************************************/ |
404 | static void DecodeICS(PSInfoBase *psi, BitStreamInfo *bsi, int ch) |
405 | { |
406 | int globalGain; |
407 | ICSInfo *icsInfo; |
408 | PulseInfo *pi; |
409 | TNSInfo *ti; |
410 | GainControlInfo *gi; |
411 | |
412 | icsInfo = (ch == 1 && psi->commonWin == 1) ? &(psi->icsInfo[0]) : &(psi->icsInfo[ch]); |
413 | |
414 | globalGain = GetBits(bsi, 8); |
415 | if (!psi->commonWin) { |
416 | DecodeICSInfo(bsi, icsInfo, psi->sampRateIdx); |
417 | } |
418 | |
419 | DecodeSectionData(bsi, icsInfo->winSequence, icsInfo->numWinGroup, icsInfo->maxSFB, psi->sfbCodeBook[ch]); |
420 | |
421 | DecodeScaleFactors(bsi, icsInfo->numWinGroup, icsInfo->maxSFB, globalGain, psi->sfbCodeBook[ch], psi->scaleFactors[ch]); |
422 | |
423 | pi = &psi->pulseInfo[ch]; |
424 | pi->pulseDataPresent = GetBits(bsi, 1); |
425 | if (pi->pulseDataPresent) { |
426 | DecodePulseInfo(bsi, pi); |
427 | } |
428 | |
429 | ti = &psi->tnsInfo[ch]; |
430 | ti->tnsDataPresent = GetBits(bsi, 1); |
431 | if (ti->tnsDataPresent) { |
432 | DecodeTNSInfo(bsi, icsInfo->winSequence, ti, ti->coef); |
433 | } |
434 | |
435 | gi = &psi->gainControlInfo[ch]; |
436 | gi->gainControlDataPresent = GetBits(bsi, 1); |
437 | if (gi->gainControlDataPresent) { |
438 | DecodeGainControlInfo(bsi, icsInfo->winSequence, gi); |
439 | } |
440 | } |
441 | |
442 | /************************************************************************************** |
443 | * Function: DecodeNoiselessData |
444 | * |
445 | * Description: decode noiseless data (side info and transform coefficients) |
446 | * |
447 | * Inputs: valid AACDecInfo struct |
448 | * double pointer to buffer pointing to start of individual channel stream |
449 | * (14496-3, table 4.4.24) |
450 | * pointer to bit offset |
451 | * pointer to number of valid bits remaining in buf |
452 | * index of current channel |
453 | * |
454 | * Outputs: updated global gain, section data, scale factor data, pulse data, |
455 | * TNS data, gain control data, and spectral data |
456 | * |
457 | * Return: 0 if successful, error code (< 0) if error |
458 | **************************************************************************************/ |
459 | int DecodeNoiselessData(AACDecInfo *aacDecInfo, unsigned char **buf, int *bitOffset, int *bitsAvail, int ch) |
460 | { |
461 | int bitsUsed; |
462 | BitStreamInfo bsi; |
463 | PSInfoBase *psi; |
464 | ICSInfo *icsInfo; |
465 | |
466 | /* validate pointers */ |
467 | if (!aacDecInfo || !aacDecInfo->psInfoBase) { |
468 | return ERR_AAC_NULL_POINTER; |
469 | } |
470 | psi = (PSInfoBase *)(aacDecInfo->psInfoBase); |
471 | icsInfo = (ch == 1 && psi->commonWin == 1) ? &(psi->icsInfo[0]) : &(psi->icsInfo[ch]); |
472 | |
473 | if (AACDataSource == 1) { |
474 | SetBitstreamPointer(&bsi, (*bitsAvail + 7) >> 3, *buf); |
475 | GetBits(&bsi, *bitOffset); |
476 | } else { |
477 | if (((*bitsAvail + 7) >> 3) < 0) { |
478 | return ERR_AAC_INDATA_UNDERFLOW; |
479 | } |
480 | } |
481 | |
482 | DecodeICS(psi, &bsi, ch); |
483 | |
484 | if (icsInfo->winSequence == 2) { |
485 | DecodeSpectrumShort(psi, &bsi, ch); |
486 | } else { |
487 | DecodeSpectrumLong(psi, &bsi, ch); |
488 | } |
489 | |
490 | if (AACDataSource == 1) { |
491 | bitsUsed = CalcBitsUsed(&bsi, *buf, *bitOffset); |
492 | *buf += ((bitsUsed + *bitOffset) >> 3); |
493 | *bitOffset = ((bitsUsed + *bitOffset) & 0x07); |
494 | *bitsAvail -= bitsUsed; |
495 | if (*bitsAvail < 0) { |
496 | return ERR_AAC_INDATA_UNDERFLOW; |
497 | } |
498 | } |
499 | aacDecInfo->sbDeinterleaveReqd[ch] = 0; |
500 | aacDecInfo->tnsUsed |= psi->tnsInfo[ch].tnsDataPresent; /* set flag if TNS used for any channel */ |
501 | |
502 | return ERR_AAC_NONE; |
503 | } |
504 |