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