00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00029 #include <stdio.h>
00030 #include "random.h"
00031
00032
00033
00034 #define M 397
00035 #define A 0x9908b0df
00036 #define UPPER_MASK 0x80000000
00037 #define LOWER_MASK 0x7fffffff
00038
00040 void av_random_init(AVRandomState *state, unsigned int seed)
00041 {
00042 int index;
00043
00044
00045
00046
00047
00048
00049
00050
00051 state->mt[0] = seed & 0xffffffff;
00052 for (index = 1; index < AV_RANDOM_N; index++) {
00053 unsigned int prev= state->mt[index - 1];
00054 state->mt[index] = (1812433253UL * (prev ^ (prev>>30)) + index) & 0xffffffff;
00055 }
00056 state->index= index;
00057 }
00058
00059 #if LIBAVUTIL_VERSION_MAJOR < 50
00060 void av_init_random(unsigned int seed, AVRandomState *state)
00061 {
00062 av_random_init(state, seed);
00063 }
00064 #endif
00065
00068 void av_random_generate_untempered_numbers(AVRandomState *state)
00069 {
00070 int kk;
00071 unsigned int y;
00072
00073 for (kk = 0; kk < AV_RANDOM_N - M; kk++) {
00074 y = (state->mt[kk] & UPPER_MASK) | (state->mt[kk + 1] & LOWER_MASK);
00075 state->mt[kk] = state->mt[kk + M] ^ (y >> 1) ^ ((y&1)*A);
00076 }
00077 for (; kk < AV_RANDOM_N - 1; kk++) {
00078 y = (state->mt[kk] & UPPER_MASK) | (state->mt[kk + 1] & LOWER_MASK);
00079 state->mt[kk] = state->mt[kk + (M - AV_RANDOM_N)] ^ (y >> 1) ^ ((y&1)*A);
00080 }
00081 y = (state->mt[AV_RANDOM_N - 1] & UPPER_MASK) | (state->mt[0] & LOWER_MASK);
00082 state->mt[AV_RANDOM_N - 1] = state->mt[M - 1] ^ (y >> 1) ^ ((y&1)*A);
00083 state->index = 0;
00084 }
00085
00086 #ifdef TEST
00087 #include "common.h"
00088 #include "log.h"
00089 int main(void)
00090 {
00091 int x=0;
00092 int i, j;
00093 AVRandomState state;
00094
00095 av_random_init(&state, 0xdeadbeef);
00096 for (j = 0; j < 10000; j++) {
00097 START_TIMER
00098 for (i = 0; i < 624; i++) {
00099 x+= av_random(&state);
00100 }
00101 STOP_TIMER("624 calls of av_random");
00102 }
00103 av_log(NULL, AV_LOG_ERROR, "final value:%X\n", x);
00104 return 0;
00105 }
00106 #endif