mirror of https://github.com/FFmpeg/FFmpeg.git
Originally committed as revision 18116 to svn://svn.ffmpeg.org/ffmpeg/trunkrelease/0.6
parent
6a03a34b01
commit
ee6624ef4a
3 changed files with 1 additions and 177 deletions
@ -1,99 +0,0 @@ |
||||
/*
|
||||
* Mersenne Twister PRNG algorithm |
||||
* Copyright (c) 2006 Ryan Martell |
||||
* Based on a C program for MT19937, with initialization improved 2002/1/26. |
||||
* Coded by Takuji Nishimura and Makoto Matsumoto. |
||||
* |
||||
* This file is part of FFmpeg. |
||||
* |
||||
* FFmpeg is free software; you can redistribute it and/or |
||||
* modify it under the terms of the GNU Lesser General Public |
||||
* License as published by the Free Software Foundation; either |
||||
* version 2.1 of the License, or (at your option) any later version. |
||||
* |
||||
* FFmpeg 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 |
||||
* Lesser General Public License for more details. |
||||
* |
||||
* You should have received a copy of the GNU Lesser General Public |
||||
* License along with FFmpeg; if not, write to the Free Software |
||||
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA |
||||
*/ |
||||
|
||||
|
||||
/**
|
||||
* See http://en.wikipedia.org/wiki/Mersenne_twister
|
||||
* for an explanation of this algorithm. |
||||
*/ |
||||
#include <stdio.h> |
||||
#include "random.h" |
||||
|
||||
|
||||
/* period parameters */ |
||||
#define M 397 |
||||
#define A 0x9908b0df /* constant vector a */ |
||||
#define UPPER_MASK 0x80000000 /* most significant w-r bits */ |
||||
#define LOWER_MASK 0x7fffffff /* least significant r bits */ |
||||
|
||||
/** Initializes mt[AV_RANDOM_N] with a seed. */ |
||||
void av_random_init(AVRandomState *state, unsigned int seed) |
||||
{ |
||||
int index; |
||||
|
||||
/*
|
||||
This differs from the Wikipedia article. Source is from the |
||||
Makoto Matsumoto and Takuji Nishimura code, with the following comment: |
||||
*/ |
||||
/* See Knuth TAOCP Vol2. 3rd Ed. P.106 for multiplier. */ |
||||
/* In the previous versions, MSBs of the seed affect */ |
||||
/* only MSBs of the array mt[]. */ |
||||
state->mt[0] = seed & 0xffffffff; |
||||
for (index = 1; index < AV_RANDOM_N; index++) { |
||||
unsigned int prev= state->mt[index - 1]; |
||||
state->mt[index] = (1812433253UL * (prev ^ (prev>>30)) + index) & 0xffffffff; |
||||
} |
||||
state->index= index; // Will cause it to generate untempered numbers in the first iteration.
|
||||
} |
||||
|
||||
/** Generates AV_RANDOM_N words at one time (which will then be tempered later).
|
||||
* av_random calls this; you shouldn't. */ |
||||
void av_random_generate_untempered_numbers(AVRandomState *state) |
||||
{ |
||||
int kk; |
||||
unsigned int y; |
||||
|
||||
for (kk = 0; kk < AV_RANDOM_N - M; kk++) { |
||||
y = (state->mt[kk] & UPPER_MASK) | (state->mt[kk + 1] & LOWER_MASK); |
||||
state->mt[kk] = state->mt[kk + M] ^ (y >> 1) ^ ((y&1)*A); |
||||
} |
||||
for (; kk < AV_RANDOM_N - 1; kk++) { |
||||
y = (state->mt[kk] & UPPER_MASK) | (state->mt[kk + 1] & LOWER_MASK); |
||||
state->mt[kk] = state->mt[kk + (M - AV_RANDOM_N)] ^ (y >> 1) ^ ((y&1)*A); |
||||
} |
||||
y = (state->mt[AV_RANDOM_N - 1] & UPPER_MASK) | (state->mt[0] & LOWER_MASK); |
||||
state->mt[AV_RANDOM_N - 1] = state->mt[M - 1] ^ (y >> 1) ^ ((y&1)*A); |
||||
state->index = 0; |
||||
} |
||||
|
||||
#ifdef TEST |
||||
#include "common.h" |
||||
#include "log.h" |
||||
int main(void) |
||||
{ |
||||
int x=0; |
||||
int i, j; |
||||
AVRandomState state; |
||||
|
||||
av_random_init(&state, 0xdeadbeef); |
||||
for (j = 0; j < 10000; j++) { |
||||
START_TIMER |
||||
for (i = 0; i < 624; i++) { |
||||
x+= av_random(&state); |
||||
} |
||||
STOP_TIMER("624 calls of av_random"); |
||||
} |
||||
av_log(NULL, AV_LOG_ERROR, "final value:%X\n", x); |
||||
return 0; |
||||
} |
||||
#endif |
@ -1,76 +0,0 @@ |
||||
/*
|
||||
* Mersenne Twister PRNG algorithm |
||||
* Copyright (c) 2006 Ryan Martell |
||||
* Based on a C program for MT19937, with initialization improved 2002/1/26. |
||||
* Coded by Takuji Nishimura and Makoto Matsumoto. |
||||
* |
||||
* This file is part of FFmpeg. |
||||
* |
||||
* FFmpeg is free software; you can redistribute it and/or |
||||
* modify it under the terms of the GNU Lesser General Public |
||||
* License as published by the Free Software Foundation; either |
||||
* version 2.1 of the License, or (at your option) any later version. |
||||
* |
||||
* FFmpeg 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 |
||||
* Lesser General Public License for more details. |
||||
* |
||||
* You should have received a copy of the GNU Lesser General Public |
||||
* License along with FFmpeg; if not, write to the Free Software |
||||
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA |
||||
*/ |
||||
|
||||
#ifndef AVUTIL_RANDOM_H |
||||
#define AVUTIL_RANDOM_H |
||||
|
||||
#define AV_RANDOM_N 624 |
||||
|
||||
#include "avutil.h" |
||||
#include "common.h" |
||||
|
||||
typedef struct { |
||||
unsigned int mt[AV_RANDOM_N]; ///< the array for the state vector
|
||||
int index; ///< Current untempered value we use as the base.
|
||||
} AVRandomState; |
||||
|
||||
|
||||
attribute_deprecated void av_random_init(AVRandomState *state, unsigned int seed); ///< To be inlined, the struct must be visible. So it does not make sense to try and keep it opaque with malloc/free-like calls.
|
||||
attribute_deprecated void av_random_generate_untempered_numbers(AVRandomState *state); ///< Regenerate the untempered numbers (must be done every 624 iterations, or it will loop).
|
||||
|
||||
/**
|
||||
* Generates a random number from the interval [0,0xffffffff]. |
||||
* |
||||
* Please do NOT use the Mersenne Twister, it is slow. Use the random number |
||||
* generator from lfg.c/h or a simple LCG like state = state*1664525+1013904223. |
||||
* If you still choose to use MT, expect that you will have to provide |
||||
* some evidence that it makes a difference for the case where you use it. |
||||
*/ |
||||
attribute_deprecated static inline unsigned int av_random(AVRandomState *state) |
||||
{ |
||||
unsigned int y; |
||||
|
||||
// Regenerate the untempered numbers if we should...
|
||||
if (state->index >= AV_RANDOM_N) |
||||
av_random_generate_untempered_numbers(state); |
||||
|
||||
// Grab one...
|
||||
y = state->mt[state->index++]; |
||||
|
||||
/* Now temper (Mersenne Twister coefficients). The coefficients for MT19937 are.. */ |
||||
y ^= (y >> 11); |
||||
y ^= (y << 7) & 0x9d2c5680; |
||||
y ^= (y << 15) & 0xefc60000; |
||||
y ^= (y >> 18); |
||||
|
||||
return y; |
||||
} |
||||
|
||||
/** Returns a random number in the range [0-1] as double. */ |
||||
attribute_deprecated static inline double av_random_real1(AVRandomState *state) |
||||
{ |
||||
/* divided by 2^32-1 */ |
||||
return av_random(state) * (1.0 / 4294967296.0); |
||||
} |
||||
|
||||
#endif /* AVUTIL_RANDOM_H */ |
Loading…
Reference in new issue