summaryrefslogtreecommitdiff
path: root/src/libfaad/pns.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/libfaad/pns.c')
-rw-r--r--src/libfaad/pns.c216
1 files changed, 168 insertions, 48 deletions
diff --git a/src/libfaad/pns.c b/src/libfaad/pns.c
index 8df56cb9c..b14aba5d7 100644
--- a/src/libfaad/pns.c
+++ b/src/libfaad/pns.c
@@ -16,105 +16,225 @@
** along with this program; if not, write to the Free Software
** Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
**
-** $Id: pns.c,v 1.1 2002/07/14 23:43:01 miguelfreitas Exp $
+** $Id: pns.c,v 1.2 2002/12/16 19:00:53 miguelfreitas Exp $
**/
#include "common.h"
+#include "structs.h"
#include "pns.h"
-/* Needs some more work */
-/* From the spec:
- If the same scalefactor band and group is coded by perceptual noise
- substitution in both channels of a channel pair, the correlation of
- the noise signal can be controlled by means of the ms_used field: While
- the default noise generation process works independently for each channel
- (separate generation of random vectors), the same random vector is used
- for both channels if ms_used[] is set for a particular scalefactor band
- and group. In this case, no M/S stereo coding is carried out (because M/S
- stereo coding and noise substitution coding are mutually exclusive).
- If the same scalefactor band and group is coded by perceptual noise
- substitution in only one channel of a channel pair the setting of ms_used[]
- is not evaluated.
-*/
+#ifdef FIXED_POINT
+#define DIV(A, B) (((int64_t)A << COEF_BITS)/B)
+#define step(shift) \
+ if ((0x40000000l >> shift) + root <= value) \
+ { \
+ value -= (0x40000000l >> shift) + root; \
+ root = (root >> 1) | (0x40000000l >> shift); \
+ } else { \
+ root = root >> 1; \
+ }
-static INLINE int32_t random2()
+/* fixed point square root approximation */
+real_t fp_sqrt(real_t value)
{
- static int32_t state = 1;
+ real_t root = 0;
- state = (1664525L * state) + 1013904223L; /* Numerical recipes */
+ step( 0); step( 2); step( 4); step( 6);
+ step( 8); step(10); step(12); step(14);
+ step(16); step(18); step(20); step(22);
+ step(24); step(26); step(28); step(30);
- return state;
+ if (root < value)
+ ++root;
+
+ root <<= (COEF_BITS/2);
+
+ return root;
}
+static real_t pow2_table[] =
+{
+ COEF_CONST(0.59460355750136),
+ COEF_CONST(0.70710678118655),
+ COEF_CONST(0.84089641525371),
+ COEF_CONST(1.0),
+ COEF_CONST(1.18920711500272),
+ COEF_CONST(1.41421356237310),
+ COEF_CONST(1.68179283050743)
+};
+#endif
+
/* The function gen_rand_vector(addr, size) generates a vector of length
<size> with signed random values of average energy MEAN_NRG per random
value. A suitable random number generator can be realized using one
multiplication/accumulation per random value.
*/
-static INLINE void gen_rand_vector(real_t *spec, uint16_t scale_factor, uint16_t size)
+static INLINE void gen_rand_vector(real_t *spec, int16_t scale_factor, uint16_t size)
{
+#ifndef FIXED_POINT
uint16_t i;
- real_t scale;
+ real_t energy = 0.0;
+
+ real_t scale = 1.0/(real_t)size * ISQRT_MEAN_NRG;
for (i = 0; i < size; i++)
{
- spec[i] = (real_t)random2();
+ real_t tmp = scale*(real_t)(int32_t)random_int();
+ spec[i] = tmp;
+ energy += tmp*tmp;
}
- /* 14496-3 says:
- scale = 1.0f/(size * (real_t)sqrt(MEAN_NRG));
- */
- scale = 1.0f/(real_t)sqrt(size * MEAN_NRG);
- scale = MUL(scale, (real_t)exp(LN2 * 0.25 * scale_factor));
+ scale = 1.0/(real_t)sqrt(energy);
+ scale *= (real_t)pow(2.0, 0.25 * scale_factor);
+ for (i = 0; i < size; i++)
+ {
+ spec[i] *= scale;
+ }
+#else
+ uint16_t i;
+ real_t energy = 0, scale;
+ int32_t exp, frac;
- /* Scale random vector to desired target energy */
for (i = 0; i < size; i++)
- spec[i] = MUL(spec[i], scale);
+ {
+ real_t tmp = ISQRT_MEAN_NRG * (int32_t)random_int();
+ tmp = MUL_C_C(COEF_CONST(1)/size, tmp);
+
+ energy += MUL_C_C(tmp,tmp);
+
+ /* convert COEF to REAL */
+ spec[i] = (tmp >> -(REAL_BITS-COEF_BITS));
+ }
+
+ energy = fp_sqrt(energy);
+ if (energy > 0)
+ {
+ scale = DIV(COEF_CONST(1),energy);
+
+ scale >>= -(REAL_BITS-COEF_BITS);
+
+ exp = scale_factor / 4;
+ frac = scale_factor % 4;
+
+ if (exp < 0)
+ scale >>= -exp;
+ else
+ scale <<= exp;
+
+ if (frac)
+ scale = MUL_R_C(scale, pow2_table[frac + 3]);
+
+ for (i = 0; i < size; i++)
+ {
+ spec[i] = MUL(spec[i], scale);
+ }
+ }
+#endif
}
-void pns_decode(ic_stream *ics, real_t *spec, uint16_t frame_len)
+void pns_decode(ic_stream *ics_left, ic_stream *ics_right,
+ real_t *spec_left, real_t *spec_right, uint16_t frame_len,
+ uint8_t channel_pair)
{
uint8_t g, sfb, b;
uint16_t size, offs;
uint8_t group = 0;
- uint16_t nshort = frame_len/8;
+ uint16_t nshort = frame_len >> 3;
- for (g = 0; g < ics->num_window_groups; g++)
+ for (g = 0; g < ics_left->num_window_groups; g++)
{
/* Do perceptual noise substitution decoding */
- for (b = 0; b < ics->window_group_length[g]; b++)
+ for (b = 0; b < ics_left->window_group_length[g]; b++)
{
- for (sfb = 0; sfb < ics->max_sfb; sfb++)
+ for (sfb = 0; sfb < ics_left->max_sfb; sfb++)
{
- if (is_noise(ics, g, sfb))
+ if (is_noise(ics_left, g, sfb))
{
/* Simultaneous use of LTP and PNS is not prevented in the
syntax. If both LTP, and PNS are enabled on the same
scalefactor band, PNS takes precedence, and no prediction
is applied to this band.
- */
- ics->ltp.long_used[sfb] = 0;
- ics->ltp2.long_used[sfb] = 0;
+ */
+ ics_left->ltp.long_used[sfb] = 0;
+ ics_left->ltp2.long_used[sfb] = 0;
/* For scalefactor bands coded using PNS the corresponding
predictors are switched to "off".
- */
- ics->pred.prediction_used[sfb] = 0;
-
- offs = ics->swb_offset[sfb];
- size = ics->swb_offset[sfb+1] - offs;
+ */
+ ics_left->pred.prediction_used[sfb] = 0;
+
+ offs = ics_left->swb_offset[sfb];
+ size = ics_left->swb_offset[sfb+1] - offs;
/* Generate random vector */
- gen_rand_vector(&spec[(group*nshort)+offs],
- ics->scale_factors[g][sfb], size);
+ gen_rand_vector(&spec_left[(group*nshort)+offs],
+ ics_left->scale_factors[g][sfb], size);
+ }
+
+/* From the spec:
+ If the same scalefactor band and group is coded by perceptual noise
+ substitution in both channels of a channel pair, the correlation of
+ the noise signal can be controlled by means of the ms_used field: While
+ the default noise generation process works independently for each channel
+ (separate generation of random vectors), the same random vector is used
+ for both channels if ms_used[] is set for a particular scalefactor band
+ and group. In this case, no M/S stereo coding is carried out (because M/S
+ stereo coding and noise substitution coding are mutually exclusive).
+ If the same scalefactor band and group is coded by perceptual noise
+ substitution in only one channel of a channel pair the setting of ms_used[]
+ is not evaluated.
+*/
+ if (channel_pair)
+ {
+ if (is_noise(ics_right, g, sfb))
+ {
+ if (ics_left->ms_mask_present == 1)
+ {
+ if (ics_left->ms_used[g][sfb])
+ {
+ uint16_t c;
+
+ offs = ics_right->swb_offset[sfb];
+ size = ics_right->swb_offset[sfb+1] - offs;
+
+ for (c = 0; c < size; c++)
+ {
+ spec_right[(group*nshort) + offs + c] =
+ spec_left[(group*nshort) + offs + c];
+ }
+ }
+ } else if (ics_left->ms_mask_present == 2) {
+ uint16_t c;
+
+ offs = ics_right->swb_offset[sfb];
+ size = ics_right->swb_offset[sfb+1] - offs;
+
+ for (c = 0; c < size; c++)
+ {
+ spec_right[(group*nshort) + offs + c] =
+ spec_left[(group*nshort) + offs + c];
+ }
+ } else /*if (ics_left->ms_mask_present == 0)*/ {
+ ics_right->ltp.long_used[sfb] = 0;
+ ics_right->ltp2.long_used[sfb] = 0;
+ ics_right->pred.prediction_used[sfb] = 0;
+
+ offs = ics_right->swb_offset[sfb];
+ size = ics_right->swb_offset[sfb+1] - offs;
+
+ /* Generate random vector */
+ gen_rand_vector(&spec_right[(group*nshort)+offs],
+ ics_right->scale_factors[g][sfb], size);
+ }
+ }
}
- }
+ } /* sfb */
group++;
- }
- }
+ } /* b */
+ } /* g */
}