shithub: aacdec

Download patch

ref: 31e91fdd154690af55d8a043ec96a86ecc414172
parent: a2a56b3ea59188e2e2b60b69a0a8f77aa3093144
author: menno <menno>
date: Tue Oct 1 17:55:49 EDT 2002

Fixed random number generator

--- a/libfaad/common.c
+++ b/libfaad/common.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: common.c,v 1.3 2002/09/27 08:37:22 menno Exp $
+** $Id: common.c,v 1.4 2002/10/01 21:55:49 menno Exp $
 **/
 
 /* just some common functions that could be used anywhere */
@@ -88,6 +88,59 @@
     }
 
     return -1;
+}
+
+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,
+	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,
+	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,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
+};
+
+static uint32_t  __r1 = 1;
+static uint32_t  __r2 = 1;
+
+
+/*
+ *  This is a simple random number generator with good quality for audio purposes.
+ *  It consists of two polycounters with opposite rotation direction and different
+ *  periods. The periods are coprime, so the total period is the product of both.
+ *
+ *     -------------------------------------------------------------------------------------------------
+ * +-> |31:30:29:28:27:26:25:24:23:22:21:20:19:18:17:16:15:14:13:12:11:10: 9: 8: 7: 6: 5: 4: 3: 2: 1: 0|
+ * |   -------------------------------------------------------------------------------------------------
+ * |                                                                          |  |  |  |     |        |
+ * |                                                                          +--+--+--+-XOR-+--------+
+ * |                                                                                      |
+ * +--------------------------------------------------------------------------------------+
+ *
+ *     -------------------------------------------------------------------------------------------------
+ *     |31:30:29:28:27:26:25:24:23:22:21:20:19:18:17:16:15:14:13:12:11:10: 9: 8: 7: 6: 5: 4: 3: 2: 1: 0| <-+
+ *     -------------------------------------------------------------------------------------------------   |
+ *       |  |           |  |                                                                               |
+ *       +--+----XOR----+--+                                                                               |
+ *                |                                                                                        |
+ *                +----------------------------------------------------------------------------------------+
+ *
+ *
+ *  The first has an period of 3*5*17*257*65537, the second of 7*47*73*178481,
+ *  which gives a period of 18.410.713.077.675.721.215. The result is the
+ *  XORed values of both generators.
+ */
+uint32_t random_int(void)
+{
+	uint32_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
+	t1   = Parity [t1]; t2  &= 0x63;            // jumps and slow rotate through the carry flag operations.
+	t1 <<= 31;          t2   = Parity [t2];
+
+	return (__r1 = (t3 >> 1) | t1 ) ^ (__r2 = (t4 + t4) | t2 );
 }
 
 #if 0
--- a/libfaad/common.h
+++ b/libfaad/common.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: common.h,v 1.23 2002/09/27 08:37:22 menno Exp $
+** $Id: common.h,v 1.24 2002/10/01 21:55:49 menno Exp $
 **/
 
 #ifndef __COMMON_H__
@@ -205,6 +205,7 @@
 uint32_t int_log2(uint32_t val);
 int8_t can_decode_ot(uint8_t object_type);
 uint8_t get_sr_index(uint32_t samplerate);
+uint32_t random_int(void);
 
 #ifndef M_PI
 #define M_PI 3.14159265358979323846f
--- a/libfaad/dither.c
+++ b/libfaad/dither.c
@@ -6,7 +6,7 @@
  * random functions for dithering.
  *
  * last modified:
- * $Id: dither.c,v 1.4 2002/09/13 13:08:45 menno Exp $
+ * $Id: dither.c,v 1.5 2002/10/01 21:55:49 menno Exp $
  */
 #include "common.h"
 
@@ -14,63 +14,6 @@
 
 #include <memory.h>
 #include "dither.h"
-
-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,
-	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,
-	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,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
-};
-
-static uint32_t  __r1 = 1;
-static uint32_t  __r2 = 1;
-
-
-/*
- *  This is a simple random number generator with good quality for audio purposes.
- *  It consists of two polycounters with opposite rotation direction and different
- *  periods. The periods are coprime, so the total period is the product of both.
- *
- *     -------------------------------------------------------------------------------------------------
- * +-> |31:30:29:28:27:26:25:24:23:22:21:20:19:18:17:16:15:14:13:12:11:10: 9: 8: 7: 6: 5: 4: 3: 2: 1: 0|
- * |   -------------------------------------------------------------------------------------------------
- * |                                                                          |  |  |  |     |        |
- * |                                                                          +--+--+--+-XOR-+--------+
- * |                                                                                      |
- * +--------------------------------------------------------------------------------------+
- *
- *     -------------------------------------------------------------------------------------------------
- *     |31:30:29:28:27:26:25:24:23:22:21:20:19:18:17:16:15:14:13:12:11:10: 9: 8: 7: 6: 5: 4: 3: 2: 1: 0| <-+
- *     -------------------------------------------------------------------------------------------------   |
- *       |  |           |  |                                                                               |
- *       +--+----XOR----+--+                                                                               |
- *                |                                                                                        |
- *                +----------------------------------------------------------------------------------------+
- *
- *
- *  The first has an period of 3*5*17*257*65537, the second of 7*47*73*178481,
- *  which gives a period of 18.410.713.077.675.721.215. The result is the
- *  XORed values of both generators.
- */
-
-
-uint32_t
-random_int ( void )
-{
-	uint32_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
-	t1   = Parity [t1]; t2  &= 0x63;            // jumps and slow rotate through the carry flag operations.
-	t1 <<= 31;          t2   = Parity [t2];
-
-	return (__r1 = (t3 >> 1) | t1 ) ^ (__r2 = (t4 + t4) | t2 );
-}
-
 
 
 double
--- 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.17 2002/09/13 13:08:45 menno Exp $
+** $Id: pns.c,v 1.18 2002/10/01 21:55:49 menno Exp $
 **/
 
 #include "common.h"
@@ -24,32 +24,6 @@
 #include "pns.h"
 
 
-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,
-    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,
-    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,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
-};
-
-static int32_t  __r1 = 1;
-static int32_t  __r2 = 1;
-
-static INLINE int32_t random2()
-{
-    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
-    t1   = Parity [t1]; t2  &= 0x63;            // jumps and slow rotate through the carry flag operations.
-    t1 <<= 31;          t2   = Parity [t2];
-
-    return (__r1 = (t3 >> 1) | t1 ) ^ (__r2 = (t4 + t4) | t2 );
-}
-
 #ifdef FIXED_POINT
 
 #define DIV(A, B) (((int64_t)A << COEF_BITS)/B)
@@ -108,7 +82,7 @@
 
     for (i = 0; i < size; i++)
     {
-        real_t tmp = scale*(real_t)random2();
+        real_t tmp = scale*(real_t)(int32_t)random_int();
         spec[i] = tmp;
         energy += tmp*tmp;
     }
@@ -126,7 +100,7 @@
 
     for (i = 0; i < size; i++)
     {
-        real_t tmp = ISQRT_MEAN_NRG * random2();
+        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);