shithub: aacdec

Download patch

ref: 35a47287fa2dbae56347a5c12a324d712301f83a
parent: 7d5bdda48ab6c229d6186beb9ec8b97f820e4bba
author: menno <menno>
date: Fri Aug 30 14:06:26 EDT 2002

Made PNS standard compliant

--- a/libfaad/decoder.c
+++ b/libfaad/decoder.c
@@ -16,7 +16,7 @@
 ** along with this program; if not, write to the Free Software 
 ** Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
 **
-** $Id: decoder.c,v 1.25 2002/08/30 12:10:57 menno Exp $
+** $Id: decoder.c,v 1.26 2002/08/30 18:06:26 menno Exp $
 **/
 
 #include <stdlib.h>
@@ -678,6 +678,7 @@
             } else if (syntax_elements[i]->paired_channel == ch) {
                 ics = &(syntax_elements[i]->ics2);
                 ltp = &(ics->ltp2);
+                pch = 0;
                 right_channel = 1;
             }
         }
@@ -687,7 +688,10 @@
             ms_decode(ics, icsr, spec_coef[ch], spec_coef[pch], frame_len);
 
         /* pns decoding */
-        pns_decode(ics, spec_coef[ch], frame_len);
+        if ((!right_channel) && (pch != 0) && (ics->ms_mask_present))
+            pns_decode(ics, icsr, spec_coef[ch], spec_coef[pch], frame_len, 1);
+        else if (!pch)
+            pns_decode(ics, NULL, spec_coef[ch], NULL, frame_len, 0);
 
         /* intensity stereo decoding */
         if (!right_channel)
--- a/libfaad/pns.c
+++ b/libfaad/pns.c
@@ -16,7 +16,7 @@
 ** 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.12 2002/08/30 12:11:57 menno Exp $
+** $Id: pns.c,v 1.13 2002/08/30 18:06:26 menno Exp $
 **/
 
 #include "common.h"
@@ -24,23 +24,7 @@
 #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.
-*/
-
-
-static const unsigned char Parity [256] = {  // parity
+static const uint8_t Parity [256] = {  // parity
     0,1,1,0,1,0,0,1,1,0,0,1,0,1,1,0,1,0,0,1,0,1,1,0,0,1,1,0,1,0,0,1,
     1,0,0,1,0,1,1,0,0,1,1,0,1,0,0,1,0,1,1,0,1,0,0,1,1,0,0,1,0,1,1,0,
     1,0,0,1,0,1,1,0,0,1,1,0,1,0,0,1,0,1,1,0,1,0,0,1,1,0,0,1,0,1,1,0,
@@ -51,12 +35,12 @@
     1,0,0,1,0,1,1,0,0,1,1,0,1,0,0,1,0,1,1,0,1,0,0,1,1,0,0,1,0,1,1,0
 };
 
-static unsigned int  __r1 = 1;
-static unsigned int  __r2 = 1;
+static int32_t  __r1 = 1;
+static int32_t  __r2 = 1;
 
-static INLINE uint32_t random2()
+static INLINE int32_t random2()
 {
-    uint32_t  t1, t2, t3, t4;
+    int32_t t1, t2, t3, t4;
 
     t3   = t1 = __r1;   t4   = t2 = __r2;       // Parity calculation is done via table lookup, this is also available
     t1  &= 0xF5;        t2 >>= 25;              // on CPUs without parity, can be implemented in C and avoid unpredictable
@@ -76,10 +60,7 @@
     uint16_t i;
     real_t energy = 0;
 
-    /* 14496-3 says:
-       scale = 1.0f/(size * (fftw_real)sqrt(MEAN_NRG));
-    */
-    float32_t scale = 1.0/(float32_t)sqrt(size * MEAN_NRG);
+    float32_t scale = 1.0/(float32_t)(size * sqrt(MEAN_NRG));
 
     for (i = 0; i < size; i++)
     {
@@ -95,7 +76,9 @@
     }
 }
 
-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;
@@ -103,37 +86,95 @@
     uint8_t group = 0;
     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);
                 }
-            }
-            group++;
-        }
-    }
+
+/* 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 */
 }
--- a/libfaad/pns.h
+++ b/libfaad/pns.h
@@ -16,7 +16,7 @@
 ** 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.h,v 1.6 2002/08/14 12:12:42 menno Exp $
+** $Id: pns.h,v 1.7 2002/08/30 18:06:26 menno Exp $
 **/
 
 #ifndef __PNS_H__
@@ -33,9 +33,11 @@
 #define MEAN_NRG 1.537228e+18 /* (2^31)^2 / 3 */
 
 
-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);
 
-static INLINE uint32_t random2();
+static INLINE int32_t random2();
 static void gen_rand_vector(real_t *spec, uint16_t scale_factor, uint16_t size);
 
 static INLINE uint8_t is_noise(ic_stream *ics, uint8_t group, uint8_t sfb)