2003-08-30 22:30:28 +00:00
|
|
|
|
/*
|
|
|
|
|
** FAAD2 - Freeware Advanced Audio (AAC) Decoder including SBR decoding
|
2004-06-02 22:59:04 +00:00
|
|
|
|
** Copyright (C) 2003-2004 M. Bakker, Ahead Software AG, http://www.nero.com
|
2003-08-30 22:30:28 +00:00
|
|
|
|
**
|
|
|
|
|
** This program is free software; you can redistribute it and/or modify
|
|
|
|
|
** it under the terms of the GNU General Public License as published by
|
|
|
|
|
** the Free Software Foundation; either version 2 of the License, or
|
|
|
|
|
** (at your option) any later version.
|
|
|
|
|
**
|
|
|
|
|
** This program is distributed in the hope that it will be useful,
|
|
|
|
|
** but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
|
** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
|
** GNU General Public License for more details.
|
|
|
|
|
**
|
|
|
|
|
** You should have received a copy of the GNU General Public License
|
|
|
|
|
** along with this program; if not, write to the Free Software
|
|
|
|
|
** Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
|
|
|
|
|
**
|
|
|
|
|
** Any non-GPL usage of this software or parts of this software is strictly
|
|
|
|
|
** forbidden.
|
|
|
|
|
**
|
|
|
|
|
** Commercial non-GPL licensing of this software is possible.
|
|
|
|
|
** For more info contact Ahead Software through Mpeg4AAClicense@nero.com.
|
|
|
|
|
**
|
2004-06-23 13:50:53 +00:00
|
|
|
|
** Initially modified for use with MPlayer by Arpad Gere<EFBFBD>ffy on 2003/08/30
|
2004-09-24 17:31:36 +00:00
|
|
|
|
** $Id: pns.c,v 1.4 2004/06/23 13:50:51 diego Exp $
|
2004-06-23 13:50:53 +00:00
|
|
|
|
** detailed CVS changelog at http://www.mplayerhq.hu/cgi-bin/cvsweb.cgi/main/
|
2003-08-30 22:30:28 +00:00
|
|
|
|
**/
|
|
|
|
|
|
|
|
|
|
#include "common.h"
|
|
|
|
|
#include "structs.h"
|
|
|
|
|
|
|
|
|
|
#include "pns.h"
|
|
|
|
|
|
|
|
|
|
|
2004-06-02 22:59:04 +00:00
|
|
|
|
/* static function declarations */
|
|
|
|
|
static void gen_rand_vector(real_t *spec, int16_t scale_factor, uint16_t size,
|
|
|
|
|
uint8_t sub);
|
|
|
|
|
|
|
|
|
|
|
2003-08-30 22:30:28 +00:00
|
|
|
|
#ifdef FIXED_POINT
|
|
|
|
|
|
2003-10-03 22:23:26 +00:00
|
|
|
|
#define DIV(A, B) (((int64_t)A << REAL_BITS)/B)
|
2003-08-30 22:30:28 +00:00
|
|
|
|
|
|
|
|
|
#define step(shift) \
|
|
|
|
|
if ((0x40000000l >> shift) + root <= value) \
|
|
|
|
|
{ \
|
|
|
|
|
value -= (0x40000000l >> shift) + root; \
|
|
|
|
|
root = (root >> 1) | (0x40000000l >> shift); \
|
|
|
|
|
} else { \
|
|
|
|
|
root = root >> 1; \
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/* fixed point square root approximation */
|
2003-10-03 22:23:26 +00:00
|
|
|
|
/* !!!! ONLY WORKS FOR EVEN %REAL_BITS% !!!! */
|
2003-08-30 22:30:28 +00:00
|
|
|
|
real_t fp_sqrt(real_t value)
|
|
|
|
|
{
|
|
|
|
|
real_t root = 0;
|
|
|
|
|
|
|
|
|
|
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);
|
|
|
|
|
|
|
|
|
|
if (root < value)
|
|
|
|
|
++root;
|
|
|
|
|
|
2003-10-03 22:23:26 +00:00
|
|
|
|
root <<= (REAL_BITS/2);
|
2003-08-30 22:30:28 +00:00
|
|
|
|
|
|
|
|
|
return root;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static real_t pow2_table[] =
|
|
|
|
|
{
|
|
|
|
|
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.
|
|
|
|
|
*/
|
2003-10-03 22:23:26 +00:00
|
|
|
|
static INLINE void gen_rand_vector(real_t *spec, int16_t scale_factor, uint16_t size,
|
|
|
|
|
uint8_t sub)
|
2003-08-30 22:30:28 +00:00
|
|
|
|
{
|
|
|
|
|
#ifndef FIXED_POINT
|
|
|
|
|
uint16_t i;
|
|
|
|
|
real_t energy = 0.0;
|
|
|
|
|
|
2003-10-03 22:23:26 +00:00
|
|
|
|
real_t scale = (real_t)1.0/(real_t)size;
|
2003-08-30 22:30:28 +00:00
|
|
|
|
|
|
|
|
|
for (i = 0; i < size; i++)
|
|
|
|
|
{
|
|
|
|
|
real_t tmp = scale*(real_t)(int32_t)random_int();
|
|
|
|
|
spec[i] = tmp;
|
|
|
|
|
energy += tmp*tmp;
|
|
|
|
|
}
|
|
|
|
|
|
2003-10-03 22:23:26 +00:00
|
|
|
|
scale = (real_t)1.0/(real_t)sqrt(energy);
|
2003-08-30 22:30:28 +00:00
|
|
|
|
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;
|
|
|
|
|
|
|
|
|
|
for (i = 0; i < size; i++)
|
|
|
|
|
{
|
2003-10-03 22:23:26 +00:00
|
|
|
|
/* this can be replaced by a 16 bit random generator!!!! */
|
|
|
|
|
real_t tmp = (int32_t)random_int();
|
|
|
|
|
if (tmp < 0)
|
|
|
|
|
tmp = -(tmp & ((1<<(REAL_BITS-1))-1));
|
|
|
|
|
else
|
|
|
|
|
tmp = (tmp & ((1<<(REAL_BITS-1))-1));
|
2003-08-30 22:30:28 +00:00
|
|
|
|
|
2004-06-02 22:59:04 +00:00
|
|
|
|
energy += MUL_R(tmp,tmp);
|
2003-08-30 22:30:28 +00:00
|
|
|
|
|
2003-10-03 22:23:26 +00:00
|
|
|
|
spec[i] = tmp;
|
2003-08-30 22:30:28 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
energy = fp_sqrt(energy);
|
|
|
|
|
if (energy > 0)
|
|
|
|
|
{
|
2003-10-03 22:23:26 +00:00
|
|
|
|
scale = DIV(REAL_CONST(1),energy);
|
2003-08-30 22:30:28 +00:00
|
|
|
|
|
2004-09-24 17:31:36 +00:00
|
|
|
|
exp = scale_factor >> 2;
|
|
|
|
|
frac = scale_factor & 3;
|
2003-08-30 22:30:28 +00:00
|
|
|
|
|
2003-10-03 22:23:26 +00:00
|
|
|
|
/* IMDCT pre-scaling */
|
|
|
|
|
exp -= sub;
|
|
|
|
|
|
2003-08-30 22:30:28 +00:00
|
|
|
|
if (exp < 0)
|
|
|
|
|
scale >>= -exp;
|
|
|
|
|
else
|
|
|
|
|
scale <<= exp;
|
|
|
|
|
|
|
|
|
|
if (frac)
|
2004-09-24 17:31:36 +00:00
|
|
|
|
scale = MUL_C(scale, pow2_table[frac]);
|
2003-08-30 22:30:28 +00:00
|
|
|
|
|
|
|
|
|
for (i = 0; i < size; i++)
|
|
|
|
|
{
|
2004-06-02 22:59:04 +00:00
|
|
|
|
spec[i] = MUL_R(spec[i], scale);
|
2003-08-30 22:30:28 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
#endif
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void pns_decode(ic_stream *ics_left, ic_stream *ics_right,
|
|
|
|
|
real_t *spec_left, real_t *spec_right, uint16_t frame_len,
|
2003-10-03 22:23:26 +00:00
|
|
|
|
uint8_t channel_pair, uint8_t object_type)
|
2003-08-30 22:30:28 +00:00
|
|
|
|
{
|
|
|
|
|
uint8_t g, sfb, b;
|
|
|
|
|
uint16_t size, offs;
|
|
|
|
|
|
|
|
|
|
uint8_t group = 0;
|
|
|
|
|
uint16_t nshort = frame_len >> 3;
|
|
|
|
|
|
2003-10-03 22:23:26 +00:00
|
|
|
|
uint8_t sub = 0;
|
|
|
|
|
|
|
|
|
|
#ifdef FIXED_POINT
|
|
|
|
|
/* IMDCT scaling */
|
|
|
|
|
if (object_type == LD)
|
|
|
|
|
{
|
|
|
|
|
sub = 9 /*9*/;
|
|
|
|
|
} else {
|
|
|
|
|
if (ics_left->window_sequence == EIGHT_SHORT_SEQUENCE)
|
|
|
|
|
sub = 7 /*7*/;
|
|
|
|
|
else
|
|
|
|
|
sub = 10 /*10*/;
|
|
|
|
|
}
|
|
|
|
|
#endif
|
|
|
|
|
|
2003-08-30 22:30:28 +00:00
|
|
|
|
for (g = 0; g < ics_left->num_window_groups; g++)
|
|
|
|
|
{
|
|
|
|
|
/* Do perceptual noise substitution decoding */
|
|
|
|
|
for (b = 0; b < ics_left->window_group_length[g]; b++)
|
|
|
|
|
{
|
|
|
|
|
for (sfb = 0; sfb < ics_left->max_sfb; sfb++)
|
|
|
|
|
{
|
|
|
|
|
if (is_noise(ics_left, g, sfb))
|
|
|
|
|
{
|
2004-09-24 17:31:36 +00:00
|
|
|
|
#ifdef LTP_DEC
|
2003-08-30 22:30:28 +00:00
|
|
|
|
/* 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_left->ltp.long_used[sfb] = 0;
|
|
|
|
|
ics_left->ltp2.long_used[sfb] = 0;
|
2004-09-24 17:31:36 +00:00
|
|
|
|
#endif
|
2003-08-30 22:30:28 +00:00
|
|
|
|
|
2004-09-24 17:31:36 +00:00
|
|
|
|
#ifdef MAIN_DEC
|
2003-08-30 22:30:28 +00:00
|
|
|
|
/* For scalefactor bands coded using PNS the corresponding
|
|
|
|
|
predictors are switched to "off".
|
|
|
|
|
*/
|
|
|
|
|
ics_left->pred.prediction_used[sfb] = 0;
|
2004-09-24 17:31:36 +00:00
|
|
|
|
#endif
|
2003-08-30 22:30:28 +00:00
|
|
|
|
|
|
|
|
|
offs = ics_left->swb_offset[sfb];
|
|
|
|
|
size = ics_left->swb_offset[sfb+1] - offs;
|
|
|
|
|
|
|
|
|
|
/* Generate random vector */
|
|
|
|
|
gen_rand_vector(&spec_left[(group*nshort)+offs],
|
2003-10-03 22:23:26 +00:00
|
|
|
|
ics_left->scale_factors[g][sfb], size, sub);
|
2003-08-30 22:30:28 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/* 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) &&
|
|
|
|
|
(ics_left->ms_used[g][sfb])) ||
|
|
|
|
|
(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)*/ {
|
2004-09-24 17:31:36 +00:00
|
|
|
|
#ifdef LTP_DEC
|
2003-08-30 22:30:28 +00:00
|
|
|
|
ics_right->ltp.long_used[sfb] = 0;
|
|
|
|
|
ics_right->ltp2.long_used[sfb] = 0;
|
2004-09-24 17:31:36 +00:00
|
|
|
|
#endif
|
|
|
|
|
#ifdef MAIN_DEC
|
2003-08-30 22:30:28 +00:00
|
|
|
|
ics_right->pred.prediction_used[sfb] = 0;
|
2004-09-24 17:31:36 +00:00
|
|
|
|
#endif
|
2003-08-30 22:30:28 +00:00
|
|
|
|
|
|
|
|
|
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],
|
2003-10-03 22:23:26 +00:00
|
|
|
|
ics_right->scale_factors[g][sfb], size, sub);
|
2003-08-30 22:30:28 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
} /* sfb */
|
|
|
|
|
group++;
|
|
|
|
|
} /* b */
|
|
|
|
|
} /* g */
|
|
|
|
|
}
|