Mercurial > libavutil.hg
annotate random.h @ 664:01e418ebd7d6 libavutil
Silence one icc warning:
variable "..." is used before its value is set
author | cehoyos |
---|---|
date | Tue, 10 Feb 2009 09:12:52 +0000 |
parents | 683a6dbdd2b2 |
children | 68c2c0caf942 |
rev | line source |
---|---|
239
06fd8e5c5a04
av_random() by Ryan Martell rdm4 name-server-seperator martellventures d0t com
michael
parents:
diff
changeset
|
1 /* |
637 | 2 * Mersenne Twister PRNG algorithm |
613
ed203c477511
cosmetics: Remove pointless period after copyright statement non-sentences.
diego
parents:
612
diff
changeset
|
3 * Copyright (c) 2006 Ryan Martell |
636 | 4 * Based on a C program for MT19937, with initialization improved 2002/1/26. |
5 * Coded by Takuji Nishimura and Makoto Matsumoto. | |
239
06fd8e5c5a04
av_random() by Ryan Martell rdm4 name-server-seperator martellventures d0t com
michael
parents:
diff
changeset
|
6 * |
06fd8e5c5a04
av_random() by Ryan Martell rdm4 name-server-seperator martellventures d0t com
michael
parents:
diff
changeset
|
7 * This file is part of FFmpeg. |
06fd8e5c5a04
av_random() by Ryan Martell rdm4 name-server-seperator martellventures d0t com
michael
parents:
diff
changeset
|
8 * |
06fd8e5c5a04
av_random() by Ryan Martell rdm4 name-server-seperator martellventures d0t com
michael
parents:
diff
changeset
|
9 * FFmpeg is free software; you can redistribute it and/or |
06fd8e5c5a04
av_random() by Ryan Martell rdm4 name-server-seperator martellventures d0t com
michael
parents:
diff
changeset
|
10 * modify it under the terms of the GNU Lesser General Public |
06fd8e5c5a04
av_random() by Ryan Martell rdm4 name-server-seperator martellventures d0t com
michael
parents:
diff
changeset
|
11 * License as published by the Free Software Foundation; either |
06fd8e5c5a04
av_random() by Ryan Martell rdm4 name-server-seperator martellventures d0t com
michael
parents:
diff
changeset
|
12 * version 2.1 of the License, or (at your option) any later version. |
06fd8e5c5a04
av_random() by Ryan Martell rdm4 name-server-seperator martellventures d0t com
michael
parents:
diff
changeset
|
13 * |
06fd8e5c5a04
av_random() by Ryan Martell rdm4 name-server-seperator martellventures d0t com
michael
parents:
diff
changeset
|
14 * FFmpeg is distributed in the hope that it will be useful, |
06fd8e5c5a04
av_random() by Ryan Martell rdm4 name-server-seperator martellventures d0t com
michael
parents:
diff
changeset
|
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of |
06fd8e5c5a04
av_random() by Ryan Martell rdm4 name-server-seperator martellventures d0t com
michael
parents:
diff
changeset
|
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
06fd8e5c5a04
av_random() by Ryan Martell rdm4 name-server-seperator martellventures d0t com
michael
parents:
diff
changeset
|
17 * Lesser General Public License for more details. |
06fd8e5c5a04
av_random() by Ryan Martell rdm4 name-server-seperator martellventures d0t com
michael
parents:
diff
changeset
|
18 * |
06fd8e5c5a04
av_random() by Ryan Martell rdm4 name-server-seperator martellventures d0t com
michael
parents:
diff
changeset
|
19 * You should have received a copy of the GNU Lesser General Public |
06fd8e5c5a04
av_random() by Ryan Martell rdm4 name-server-seperator martellventures d0t com
michael
parents:
diff
changeset
|
20 * License along with FFmpeg; if not, write to the Free Software |
06fd8e5c5a04
av_random() by Ryan Martell rdm4 name-server-seperator martellventures d0t com
michael
parents:
diff
changeset
|
21 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA |
06fd8e5c5a04
av_random() by Ryan Martell rdm4 name-server-seperator martellventures d0t com
michael
parents:
diff
changeset
|
22 */ |
06fd8e5c5a04
av_random() by Ryan Martell rdm4 name-server-seperator martellventures d0t com
michael
parents:
diff
changeset
|
23 |
567 | 24 #ifndef AVUTIL_RANDOM_H |
25 #define AVUTIL_RANDOM_H | |
239
06fd8e5c5a04
av_random() by Ryan Martell rdm4 name-server-seperator martellventures d0t com
michael
parents:
diff
changeset
|
26 |
06fd8e5c5a04
av_random() by Ryan Martell rdm4 name-server-seperator martellventures d0t com
michael
parents:
diff
changeset
|
27 #define AV_RANDOM_N 624 |
06fd8e5c5a04
av_random() by Ryan Martell rdm4 name-server-seperator martellventures d0t com
michael
parents:
diff
changeset
|
28 |
612
84d82b1dcd99
Drop the deprecated av_init_random() at the next libavutil major bump.
stefano
parents:
609
diff
changeset
|
29 #include "avutil.h" |
609
70367460f323
Deprecate av_init_random() in favour of av_random_init(), with a more natural
stefano
parents:
567
diff
changeset
|
30 #include "common.h" |
70367460f323
Deprecate av_init_random() in favour of av_random_init(), with a more natural
stefano
parents:
567
diff
changeset
|
31 |
239
06fd8e5c5a04
av_random() by Ryan Martell rdm4 name-server-seperator martellventures d0t com
michael
parents:
diff
changeset
|
32 typedef struct { |
06fd8e5c5a04
av_random() by Ryan Martell rdm4 name-server-seperator martellventures d0t com
michael
parents:
diff
changeset
|
33 unsigned int mt[AV_RANDOM_N]; ///< the array for the state vector |
540 | 34 int index; ///< Current untempered value we use as the base. |
239
06fd8e5c5a04
av_random() by Ryan Martell rdm4 name-server-seperator martellventures d0t com
michael
parents:
diff
changeset
|
35 } AVRandomState; |
06fd8e5c5a04
av_random() by Ryan Martell rdm4 name-server-seperator martellventures d0t com
michael
parents:
diff
changeset
|
36 |
06fd8e5c5a04
av_random() by Ryan Martell rdm4 name-server-seperator martellventures d0t com
michael
parents:
diff
changeset
|
37 |
612
84d82b1dcd99
Drop the deprecated av_init_random() at the next libavutil major bump.
stefano
parents:
609
diff
changeset
|
38 #if LIBAVUTIL_VERSION_MAJOR < 50 |
609
70367460f323
Deprecate av_init_random() in favour of av_random_init(), with a more natural
stefano
parents:
567
diff
changeset
|
39 attribute_deprecated void av_init_random(unsigned int seed, AVRandomState *state); |
612
84d82b1dcd99
Drop the deprecated av_init_random() at the next libavutil major bump.
stefano
parents:
609
diff
changeset
|
40 #endif |
609
70367460f323
Deprecate av_init_random() in favour of av_random_init(), with a more natural
stefano
parents:
567
diff
changeset
|
41 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. |
540 | 42 void av_random_generate_untempered_numbers(AVRandomState *state); ///< Regenerate the untempered numbers (must be done every 624 iterations, or it will loop). |
239
06fd8e5c5a04
av_random() by Ryan Martell rdm4 name-server-seperator martellventures d0t com
michael
parents:
diff
changeset
|
43 |
534 | 44 /** |
540 | 45 * Generates a random number from the interval [0,0xffffffff]. |
534 | 46 * |
637 | 47 * Please do NOT use the Mersenne Twister, it is slow. Use the random number |
48 * generator from lfg.c/h or a simple LCG like state = state*1664525+1013904223. | |
534 | 49 * If you still choose to use MT, expect that you will have to provide |
50 * some evidence that it makes a difference for the case where you use it. | |
51 */ | |
239
06fd8e5c5a04
av_random() by Ryan Martell rdm4 name-server-seperator martellventures d0t com
michael
parents:
diff
changeset
|
52 static inline unsigned int av_random(AVRandomState *state) |
06fd8e5c5a04
av_random() by Ryan Martell rdm4 name-server-seperator martellventures d0t com
michael
parents:
diff
changeset
|
53 { |
06fd8e5c5a04
av_random() by Ryan Martell rdm4 name-server-seperator martellventures d0t com
michael
parents:
diff
changeset
|
54 unsigned int y; |
06fd8e5c5a04
av_random() by Ryan Martell rdm4 name-server-seperator martellventures d0t com
michael
parents:
diff
changeset
|
55 |
540 | 56 // Regenerate the untempered numbers if we should... |
239
06fd8e5c5a04
av_random() by Ryan Martell rdm4 name-server-seperator martellventures d0t com
michael
parents:
diff
changeset
|
57 if (state->index >= AV_RANDOM_N) |
06fd8e5c5a04
av_random() by Ryan Martell rdm4 name-server-seperator martellventures d0t com
michael
parents:
diff
changeset
|
58 av_random_generate_untempered_numbers(state); |
06fd8e5c5a04
av_random() by Ryan Martell rdm4 name-server-seperator martellventures d0t com
michael
parents:
diff
changeset
|
59 |
540 | 60 // Grab one... |
239
06fd8e5c5a04
av_random() by Ryan Martell rdm4 name-server-seperator martellventures d0t com
michael
parents:
diff
changeset
|
61 y = state->mt[state->index++]; |
06fd8e5c5a04
av_random() by Ryan Martell rdm4 name-server-seperator martellventures d0t com
michael
parents:
diff
changeset
|
62 |
540 | 63 /* Now temper (Mersenne Twister coefficients). The coefficients for MT19937 are.. */ |
239
06fd8e5c5a04
av_random() by Ryan Martell rdm4 name-server-seperator martellventures d0t com
michael
parents:
diff
changeset
|
64 y ^= (y >> 11); |
06fd8e5c5a04
av_random() by Ryan Martell rdm4 name-server-seperator martellventures d0t com
michael
parents:
diff
changeset
|
65 y ^= (y << 7) & 0x9d2c5680; |
06fd8e5c5a04
av_random() by Ryan Martell rdm4 name-server-seperator martellventures d0t com
michael
parents:
diff
changeset
|
66 y ^= (y << 15) & 0xefc60000; |
06fd8e5c5a04
av_random() by Ryan Martell rdm4 name-server-seperator martellventures d0t com
michael
parents:
diff
changeset
|
67 y ^= (y >> 18); |
06fd8e5c5a04
av_random() by Ryan Martell rdm4 name-server-seperator martellventures d0t com
michael
parents:
diff
changeset
|
68 |
06fd8e5c5a04
av_random() by Ryan Martell rdm4 name-server-seperator martellventures d0t com
michael
parents:
diff
changeset
|
69 return y; |
06fd8e5c5a04
av_random() by Ryan Martell rdm4 name-server-seperator martellventures d0t com
michael
parents:
diff
changeset
|
70 } |
06fd8e5c5a04
av_random() by Ryan Martell rdm4 name-server-seperator martellventures d0t com
michael
parents:
diff
changeset
|
71 |
637 | 72 /** Returns a random number in the range [0-1] as double. */ |
239
06fd8e5c5a04
av_random() by Ryan Martell rdm4 name-server-seperator martellventures d0t com
michael
parents:
diff
changeset
|
73 static inline double av_random_real1(AVRandomState *state) |
06fd8e5c5a04
av_random() by Ryan Martell rdm4 name-server-seperator martellventures d0t com
michael
parents:
diff
changeset
|
74 { |
06fd8e5c5a04
av_random() by Ryan Martell rdm4 name-server-seperator martellventures d0t com
michael
parents:
diff
changeset
|
75 /* divided by 2^32-1 */ |
06fd8e5c5a04
av_random() by Ryan Martell rdm4 name-server-seperator martellventures d0t com
michael
parents:
diff
changeset
|
76 return av_random(state) * (1.0 / 4294967296.0); |
06fd8e5c5a04
av_random() by Ryan Martell rdm4 name-server-seperator martellventures d0t com
michael
parents:
diff
changeset
|
77 } |
06fd8e5c5a04
av_random() by Ryan Martell rdm4 name-server-seperator martellventures d0t com
michael
parents:
diff
changeset
|
78 |
567 | 79 #endif /* AVUTIL_RANDOM_H */ |