-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathpns.c
379 lines (335 loc) · 12.5 KB
/
pns.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
/* ***** BEGIN LICENSE BLOCK *****
* Source last modified: $Id: pns.c,v 1.2 2005/03/10 17:01:56 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
*
* pns.c - perceptual noise substitution
**************************************************************************************/
#include "coder.h"
#include "assembly.h"
#ifdef AAC_ENABLE_PNS
/**************************************************************************************
* Function: Get32BitVal
*
* Description: generate 32-bit unsigned random number
*
* Inputs: last number calculated (seed, first time through)
*
* Outputs: new number, saved in *last
*
* Return: 32-bit number, uniformly distributed between [0, 2^32)
*
* Notes: uses simple linear congruential generator
**************************************************************************************/
static unsigned int Get32BitVal(unsigned int *last)
{
unsigned int r = *last;
/* use same coefs as MPEG reference code (classic LCG)
* use unsigned multiply to force reliable wraparound behavior in C (mod 2^32)
*/
r = (1664525U * r) + 1013904223U;
*last = r;
return r;
}
#define NUM_ITER_INVSQRT 4
#define X0_COEF_2 0xc0000000 /* Q29: -2.0 */
#define X0_OFF_2 0x60000000 /* Q29: 3.0 */
#define Q26_3 0x0c000000 /* Q26: 3.0 */
/**************************************************************************************
* Function: InvRootR
*
* Description: use Newton's method to solve for x = 1/sqrt(r)
*
* Inputs: r in Q30 format, range = [0.25, 1] (normalize inputs to this range)
*
* Outputs: none
*
* Return: x = Q29, range = (1, 2)
*
* Notes: guaranteed to converge and not overflow for any r in this range
*
* xn+1 = xn - f(xn)/f'(xn)
* f(x) = 1/sqrt(r) - x = 0 (find root)
* = 1/x^2 - r
* f'(x) = -2/x^3
*
* so xn+1 = xn/2 * (3 - r*xn^2)
*
* NUM_ITER_INVSQRT = 3, maxDiff = 1.3747e-02
* NUM_ITER_INVSQRT = 4, maxDiff = 3.9832e-04
**************************************************************************************/
static int InvRootR(int r)
{
int i, xn, t;
/* use linear equation for initial guess
* x0 = -2*r + 3 (so x0 always >= correct answer in range [0.25, 1))
* xn = Q29 (at every step)
*/
xn = (MULSHIFT32(r, X0_COEF_2) << 2) + X0_OFF_2;
for (i = 0; i < NUM_ITER_INVSQRT; i++) {
t = MULSHIFT32(xn, xn); /* Q26 = Q29*Q29 */
t = Q26_3 - (MULSHIFT32(r, t) << 2); /* Q26 = Q26 - (Q31*Q26 << 1) */
xn = MULSHIFT32(xn, t) << (6 - 1); /* Q29 = (Q29*Q26 << 6), and -1 for division by 2 */
}
/* clip to range (1.0, 2.0)
* (because of rounding, this can converge to xn slightly > 2.0 when r is near 0.25)
*/
if (xn >> 30)
xn = (1 << 30) - 1;
return xn;
}
/**************************************************************************************
* Function: ScaleNoiseVector
*
* Description: apply scaling to vector of noise coefficients for one scalefactor band
*
* Inputs: unscaled coefficients
* number of coefficients in vector (one scalefactor band of coefs)
* scalefactor for this band (i.e. noise energy)
*
* Outputs: nVals coefficients in Q(FBITS_OUT_DQ_OFF)
*
* Return: guard bit mask (OR of abs value of all noise coefs)
**************************************************************************************/
static int ScaleNoiseVector(int *coef, int nVals, int sf)
{
/* pow(2, i/4.0) for i = [0,1,2,3], format = Q30 */
static const int pow14[4] PROGMEM = {
0x40000000, 0x4c1bf829, 0x5a82799a, 0x6ba27e65
};
int i, c, spec, energy, sq, scalef, scalei, invSqrtEnergy, z, gbMask;
energy = 0;
for (i = 0; i < nVals; i++) {
spec = coef[i];
/* max nVals = max SFB width = 96, so energy can gain < 2^7 bits in accumulation */
sq = (spec * spec) >> 8; /* spec*spec range = (-2^30, 2^30) */
energy += sq;
}
/* unless nVals == 1 (or the number generator is broken...), this should not happen */
if (energy == 0)
return 0; /* coef[i] must = 0 for i = [0, nVals-1], so gbMask = 0 */
/* pow(2, sf/4) * pow(2, FBITS_OUT_DQ_OFF) */
scalef = pow14[sf & 0x3];
scalei = (sf >> 2) + FBITS_OUT_DQ_OFF;
/* energy has implied factor of 2^-8 since we shifted the accumulator
* normalize energy to range [0.25, 1.0), calculate 1/sqrt(1), and denormalize
* i.e. divide input by 2^(30-z) and convert to Q30
* output of 1/sqrt(i) now has extra factor of 2^((30-z)/2)
* for energy > 0, z is an even number between 0 and 28
* final scaling of invSqrtEnergy:
* 2^(15 - z/2) to compensate for implicit 2^(30-z) factor in input
* +4 to compensate for implicit 2^-8 factor in input
*/
z = CLZ(energy) - 2; /* energy has at least 2 leading zeros (see acc loop) */
z &= 0xfffffffe; /* force even */
invSqrtEnergy = InvRootR(energy << z); /* energy << z must be in range [0x10000000, 0x40000000] */
scalei -= (15 - z/2 + 4); /* nInt = 1/sqrt(energy) in Q29 */
/* normalize for final scaling */
z = CLZ(invSqrtEnergy) - 1;
invSqrtEnergy <<= z;
scalei -= (z - 3 - 2); /* -2 for scalef, z-3 for invSqrtEnergy */
scalef = MULSHIFT32(scalef, invSqrtEnergy); /* scalef (input) = Q30, invSqrtEnergy = Q29 * 2^z */
gbMask = 0;
if (scalei < 0) {
scalei = -scalei;
if (scalei > 31)
scalei = 31;
for (i = 0; i < nVals; i++) {
c = MULSHIFT32(coef[i], scalef) >> scalei;
gbMask |= FASTABS(c);
coef[i] = c;
}
} else {
/* for scalei <= 16, no clipping possible (coef[i] is < 2^15 before scaling)
* for scalei > 16, just saturate exponent (rare)
* scalef is close to full-scale (since we normalized invSqrtEnergy)
* remember, we are just producing noise here
*/
if (scalei > 16)
scalei = 16;
for (i = 0; i < nVals; i++) {
c = MULSHIFT32(coef[i] << scalei, scalef);
coef[i] = c;
gbMask |= FASTABS(c);
}
}
return gbMask;
}
/**************************************************************************************
* Function: GenerateNoiseVector
*
* Description: create vector of noise coefficients for one scalefactor band
*
* Inputs: seed for number generator
* number of coefficients to generate
*
* Outputs: buffer of nVals coefficients, range = [-2^15, 2^15)
* updated seed for number generator
*
* Return: none
**************************************************************************************/
static void GenerateNoiseVector(int *coef, int *last, int nVals)
{
int i;
for (i = 0; i < nVals; i++)
coef[i] = ((signed int)Get32BitVal((unsigned int *)last)) >> 16;
}
/**************************************************************************************
* Function: CopyNoiseVector
*
* Description: copy vector of noise coefficients for one scalefactor band from L to R
*
* Inputs: buffer of left coefficients
* number of coefficients to copy
*
* Outputs: buffer of right coefficients
*
* Return: none
**************************************************************************************/
static void CopyNoiseVector(int *coefL, int *coefR, int nVals)
{
int i;
for (i = 0; i < nVals; i++)
coefR[i] = coefL[i];
}
/**************************************************************************************
* Function: PNS
*
* Description: apply perceptual noise substitution, if enabled (MPEG-4 only)
*
* Inputs: valid AACDecInfo struct
* index of current channel
*
* Outputs: shaped noise in scalefactor bands where PNS is active
* updated minimum guard bit count for this channel
*
* Return: 0 if successful, -1 if error
**************************************************************************************/
int PNS(AACDecInfo *aacDecInfo, int ch)
{
int gp, sfb, win, width, nSamps, gb, gbMask;
int *coef;
const /*short*/ int *sfbTab;
unsigned char *sfbCodeBook;
short *scaleFactors;
int msMaskOffset, checkCorr, genNew;
unsigned char msMask;
unsigned char *msMaskPtr;
PSInfoBase *psi;
ICSInfo *icsInfo;
/* validate pointers */
if (!aacDecInfo || !aacDecInfo->psInfoBase)
return -1;
psi = (PSInfoBase *)(aacDecInfo->psInfoBase);
icsInfo = (ch == 1 && psi->commonWin == 1) ? &(psi->icsInfo[0]) : &(psi->icsInfo[ch]);
if (!psi->pnsUsed[ch])
return 0;
if (icsInfo->winSequence == 2) {
sfbTab = sfBandTabShort + sfBandTabShortOffset[psi->sampRateIdx];
nSamps = NSAMPS_SHORT;
} else {
sfbTab = sfBandTabLong + sfBandTabLongOffset[psi->sampRateIdx];
nSamps = NSAMPS_LONG;
}
coef = psi->coef[ch];
sfbCodeBook = psi->sfbCodeBook[ch];
scaleFactors = psi->scaleFactors[ch];
checkCorr = (aacDecInfo->currBlockID == AAC_ID_CPE && psi->commonWin == 1 ? 1 : 0);
gbMask = 0;
for (gp = 0; gp < icsInfo->numWinGroup; gp++) {
for (win = 0; win < icsInfo->winGroupLen[gp]; win++) {
msMaskPtr = psi->msMaskBits + ((gp*icsInfo->maxSFB) >> 3);
msMaskOffset = ((gp*icsInfo->maxSFB) & 0x07);
msMask = (*msMaskPtr++) >> msMaskOffset;
for (sfb = 0; sfb < icsInfo->maxSFB; sfb++) {
width = sfbTab[sfb+1] - sfbTab[sfb];
if (sfbCodeBook[sfb] == 13) {
if (ch == 0) {
/* generate new vector, copy into ch 1 if it's possible that the channels will be correlated
* if ch 1 has PNS enabled for this SFB but it's uncorrelated (i.e. ms_used == 0),
* the copied values will be overwritten when we process ch 1
*/
if(coef + width > psi->coef[ch] + AAC_MAX_NSAMPS)
return ERR_AAC_INVALID_FRAME;
GenerateNoiseVector(coef, &psi->pnsLastVal, width);
if (checkCorr && psi->sfbCodeBook[1][gp*icsInfo->maxSFB + sfb] == 13)
CopyNoiseVector(coef, psi->coef[1] + (coef - psi->coef[0]), width);
} else {
/* generate new vector if no correlation between channels */
genNew = 1;
if (checkCorr && psi->sfbCodeBook[0][gp*icsInfo->maxSFB + sfb] == 13) {
if ( (psi->msMaskPresent == 1 && (msMask & 0x01)) || psi->msMaskPresent == 2 )
genNew = 0;
}
if (genNew) {
if(coef + width > psi->coef[ch] + AAC_MAX_NSAMPS)
return ERR_AAC_INVALID_FRAME;
GenerateNoiseVector(coef, &psi->pnsLastVal, width);
}
}
gbMask |= ScaleNoiseVector(coef, width, psi->scaleFactors[ch][gp*icsInfo->maxSFB + sfb]);
}
coef += width;
/* get next mask bit (should be branchless on ARM) */
msMask >>= 1;
if (++msMaskOffset == 8) {
msMask = *msMaskPtr++;
msMaskOffset = 0;
}
}
coef += (nSamps - sfbTab[icsInfo->maxSFB]);
}
sfbCodeBook += icsInfo->maxSFB;
scaleFactors += icsInfo->maxSFB;
}
/* update guard bit count if necessary */
gb = CLZ(gbMask) - 1;
if (psi->gbCurrent[ch] > gb)
psi->gbCurrent[ch] = gb;
return 0;
}
#else
int PNS(AACDecInfo *aacDecInfo, int ch)
{
if (aacDecInfo->pnsUsed)
return ERR_AAC_PNS;
else
return ERR_AAC_NONE;
}
#endif