summaryrefslogtreecommitdiff
path: root/audio_codec/libfaad/helixaac/noiseless.c (plain)
blob: e519e9d9af72ed27a382a0c1e3b2cd03e6ff34e9
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 <stdio.h>
49
50
51/**************************************************************************************
52 * Function: DecodeICSInfo
53 *
54 * Description: decode individual channel stream info
55 *
56 * Inputs: BitStreamInfo struct pointing to start of ICS info
57 * (14496-3, table 4.4.6)
58 * sample rate index
59 *
60 * Outputs: updated icsInfo struct
61 *
62 * Return: none
63 **************************************************************************************/
64void DecodeICSInfo(BitStreamInfo *bsi, ICSInfo *icsInfo, int sampRateIdx)
65{
66 int sfb, g, mask;
67
68 icsInfo->icsResBit = GetBits(bsi, 1);
69 icsInfo->winSequence = GetBits(bsi, 2);
70 icsInfo->winShape = GetBits(bsi, 1);
71 if (icsInfo->winSequence == 2) {
72 /* short block */
73 icsInfo->maxSFB = GetBits(bsi, 4);
74 icsInfo->sfGroup = GetBits(bsi, 7);
75 icsInfo->numWinGroup = 1;
76 icsInfo->winGroupLen[0] = 1;
77 mask = 0x40; /* start with bit 6 */
78 for (g = 0; g < 7; g++) {
79 if (icsInfo->sfGroup & mask) {
80 icsInfo->winGroupLen[icsInfo->numWinGroup - 1]++;
81 } else {
82 icsInfo->numWinGroup++;
83 icsInfo->winGroupLen[icsInfo->numWinGroup - 1] = 1;
84 }
85 mask >>= 1;
86 }
87 } else {
88 /* long block */
89 icsInfo->maxSFB = GetBits(bsi, 6);
90 icsInfo->predictorDataPresent = GetBits(bsi, 1);
91 if (icsInfo->predictorDataPresent) {
92 icsInfo->predictorReset = GetBits(bsi, 1);
93 if (icsInfo->predictorReset) {
94 icsInfo->predictorResetGroupNum = GetBits(bsi, 5);
95 }
96 for (sfb = 0; sfb < MIN(icsInfo->maxSFB, predSFBMax[sampRateIdx]); sfb++) {
97 icsInfo->predictionUsed[sfb] = GetBits(bsi, 1);
98 }
99 }
100 icsInfo->numWinGroup = 1;
101 icsInfo->winGroupLen[0] = 1;
102 }
103}
104
105/**************************************************************************************
106 * Function: DecodeSectionData
107 *
108 * Description: decode section data (scale factor band groupings and
109 * associated Huffman codebooks)
110 *
111 * Inputs: BitStreamInfo struct pointing to start of ICS info
112 * (14496-3, table 4.4.25)
113 * window sequence (short or long blocks)
114 * number of window groups (1 for long blocks, 1-8 for short blocks)
115 * max coded scalefactor band
116 *
117 * Outputs: index of Huffman codebook for each scalefactor band in each section
118 *
119 * Return: none
120 *
121 * Notes: sectCB, sectEnd, sfbCodeBook, ordered by window groups for short blocks
122 **************************************************************************************/
123static int DecodeSectionData(BitStreamInfo *bsi, int winSequence, int numWinGrp, int maxSFB, unsigned char *sfbCodeBook)
124{
125 int g, cb, sfb;
126 int sectLen, sectLenBits, sectLenIncr, sectEscapeVal;
127
128 sectLenBits = (winSequence == 2 ? 3 : 5);
129 sectEscapeVal = (1 << sectLenBits) - 1;
130
131 for (g = 0; g < numWinGrp; g++) {
132 sfb = 0;
133 while (sfb < maxSFB) {
134 cb = GetBits(bsi, 4); /* next section codebook */
135 sectLen = 0;
136 do {
137 sectLenIncr = GetBits(bsi, sectLenBits);
138 sectLen += sectLenIncr;
139 } while (sectLenIncr == sectEscapeVal);
140
141 sfb += sectLen;
142 while (sectLen--) {
143 *sfbCodeBook++ = (unsigned char)cb;
144 }
145 }
146 ASSERT(sfb == maxSFB, ERR_AAC_HUFFMAN_DECODING);
147 }
148 return ERR_AAC_NONE;
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 **************************************************************************************/
162static 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 **************************************************************************************/
197static 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 **************************************************************************************/
250static 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 **************************************************************************************/
276static 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 */
348static 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 **************************************************************************************/
368static 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 **************************************************************************************/
404static int DecodeICS(PSInfoBase *psi, BitStreamInfo *bsi, int ch)
405{
406 int globalGain;
407 ICSInfo *icsInfo;
408 PulseInfo *pi;
409 TNSInfo *ti;
410 GainControlInfo *gi;
411 int err = ERR_AAC_NONE;
412
413 icsInfo = (ch == 1 && psi->commonWin == 1) ? &(psi->icsInfo[0]) : &(psi->icsInfo[ch]);
414
415 globalGain = GetBits(bsi, 8);
416 if (!psi->commonWin) {
417 DecodeICSInfo(bsi, icsInfo, psi->sampRateIdx);
418 }
419
420 err = DecodeSectionData(bsi, icsInfo->winSequence, icsInfo->numWinGroup, icsInfo->maxSFB, psi->sfbCodeBook[ch]);
421 if (err) {
422 return err;
423 }
424 DecodeScaleFactors(bsi, icsInfo->numWinGroup, icsInfo->maxSFB, globalGain, psi->sfbCodeBook[ch], psi->scaleFactors[ch]);
425
426 pi = &psi->pulseInfo[ch];
427 pi->pulseDataPresent = GetBits(bsi, 1);
428 if (pi->pulseDataPresent) {
429 DecodePulseInfo(bsi, pi);
430 }
431
432 ti = &psi->tnsInfo[ch];
433 ti->tnsDataPresent = GetBits(bsi, 1);
434 if (ti->tnsDataPresent) {
435 DecodeTNSInfo(bsi, icsInfo->winSequence, ti, ti->coef);
436 }
437
438 gi = &psi->gainControlInfo[ch];
439 gi->gainControlDataPresent = GetBits(bsi, 1);
440 if (gi->gainControlDataPresent) {
441 /* disable SSR, as we can decode out this part */
442 return ERR_AAC_SSR_GAIN_NOT_ADDED;
443 DecodeGainControlInfo(bsi, icsInfo->winSequence, gi);
444 }
445 return err;
446}
447
448/**************************************************************************************
449 * Function: DecodeNoiselessData
450 *
451 * Description: decode noiseless data (side info and transform coefficients)
452 *
453 * Inputs: valid AACDecInfo struct
454 * double pointer to buffer pointing to start of individual channel stream
455 * (14496-3, table 4.4.24)
456 * pointer to bit offset
457 * pointer to number of valid bits remaining in buf
458 * index of current channel
459 *
460 * Outputs: updated global gain, section data, scale factor data, pulse data,
461 * TNS data, gain control data, and spectral data
462 *
463 * Return: 0 if successful, error code (< 0) if error
464 **************************************************************************************/
465int DecodeNoiselessData(AACDecInfo *aacDecInfo, unsigned char **buf, int *bitOffset, int *bitsAvail, int ch)
466{
467 int bitsUsed;
468 BitStreamInfo bsi;
469 PSInfoBase *psi;
470 ICSInfo *icsInfo;
471 int err = 0;
472 /* validate pointers */
473 if (!aacDecInfo || !aacDecInfo->psInfoBase) {
474 return ERR_AAC_NULL_POINTER;
475 }
476 psi = (PSInfoBase *)(aacDecInfo->psInfoBase);
477 icsInfo = (ch == 1 && psi->commonWin == 1) ? &(psi->icsInfo[0]) : &(psi->icsInfo[ch]);
478
479 if (AACDataSource == 1) {
480 SetBitstreamPointer(&bsi, (*bitsAvail + 7) >> 3, *buf);
481 GetBits(&bsi, *bitOffset);
482 } else {
483 if (((*bitsAvail + 7) >> 3) < 0) {
484 return ERR_AAC_INDATA_UNDERFLOW;
485 }
486 }
487
488 err = DecodeICS(psi, &bsi, ch);
489 if (err) {
490 return err;
491 }
492 if (icsInfo->winSequence == 2) {
493 err = DecodeSpectrumShort(psi, &bsi, ch);
494 } else {
495 err = DecodeSpectrumLong(psi, &bsi, ch);
496 }
497 if (err) {
498 return err;
499 }
500 if (AACDataSource == 1) {
501 bitsUsed = CalcBitsUsed(&bsi, *buf, *bitOffset);
502 *buf += ((bitsUsed + *bitOffset) >> 3);
503 *bitOffset = ((bitsUsed + *bitOffset) & 0x07);
504 *bitsAvail -= bitsUsed;
505 if (*bitsAvail < 0) {
506 return ERR_AAC_INDATA_UNDERFLOW;
507 }
508 }
509 aacDecInfo->sbDeinterleaveReqd[ch] = 0;
510 aacDecInfo->tnsUsed |= psi->tnsInfo[ch].tnsDataPresent; /* set flag if TNS used for any channel */
511
512 return ERR_AAC_NONE;
513}
514