Commit | Line | Data |
---|---|---|
48d58e59 BC |
1 | /* |
2 | * Copyright (c) 2009 Baptiste Coudurier <baptiste.coudurier@gmail.com> | |
3 | * | |
4 | * This file is part of FFmpeg. | |
5 | * | |
6 | * FFmpeg is free software; you can redistribute it and/or | |
7 | * modify it under the terms of the GNU Lesser General Public | |
8 | * License as published by the Free Software Foundation; either | |
9 | * version 2.1 of the License, or (at your option) any later version. | |
10 | * | |
11 | * FFmpeg is distributed in the hope that it will be useful, | |
12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | |
14 | * Lesser General Public License for more details. | |
15 | * | |
16 | * You should have received a copy of the GNU Lesser General Public | |
17 | * License along with FFmpeg; if not, write to the Free Software | |
18 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA | |
19 | */ | |
20 | ||
21 | #include <unistd.h> | |
22 | #include <fcntl.h> | |
23 | #include "timer.h" | |
c84d5aa7 | 24 | #include "time.h" |
48d58e59 | 25 | #include "random_seed.h" |
576fb48e | 26 | #include "avutil.h" |
48d58e59 | 27 | |
38e23c88 MR |
28 | static int read_random(uint32_t *dst, const char *file) |
29 | { | |
30 | int fd = open(file, O_RDONLY); | |
31 | int err = -1; | |
32 | ||
33 | if (fd == -1) | |
34 | return -1; | |
9958096e | 35 | err = read(fd, dst, sizeof(*dst)); |
38e23c88 MR |
36 | close(fd); |
37 | ||
38 | return err; | |
39 | } | |
40 | ||
c84d5aa7 MN |
41 | static uint32_t get_generic_seed(void) |
42 | { | |
43 | int last_t=0; | |
44 | int bits=0; | |
45 | uint64_t random=0; | |
b65c1ccf | 46 | unsigned i; |
c84d5aa7 MN |
47 | int s=0; |
48 | ||
49 | for(i=0;bits<64;i++){ | |
50 | int t= clock()>>s; | |
51 | if(last_t && t != last_t){ | |
b65c1ccf | 52 | if(i<10000 && s<24){ |
c84d5aa7 MN |
53 | s++; |
54 | i=t=0; | |
55 | }else{ | |
56 | random= 2*random + (i&1); | |
57 | bits++; | |
58 | } | |
59 | } | |
60 | last_t= t; | |
61 | } | |
62 | #ifdef AV_READ_TIME | |
63 | random ^= AV_READ_TIME(); | |
64 | #else | |
65 | random ^= clock(); | |
66 | #endif | |
67 | ||
68 | random += random>>32; | |
69 | ||
70 | return random; | |
71 | } | |
72 | ||
576fb48e | 73 | uint32_t av_get_random_seed(void) |
48d58e59 BC |
74 | { |
75 | uint32_t seed; | |
38e23c88 | 76 | |
16bfbfd0 MR |
77 | if (read_random(&seed, "/dev/urandom") == sizeof(seed)) |
78 | return seed; | |
79 | if (read_random(&seed, "/dev/random") == sizeof(seed)) | |
38e23c88 | 80 | return seed; |
c84d5aa7 | 81 | return get_generic_seed(); |
48d58e59 | 82 | } |
576fb48e MS |
83 | |
84 | #if LIBAVUTIL_VERSION_MAJOR < 51 | |
85 | attribute_deprecated uint32_t ff_random_get_seed(void); | |
86 | uint32_t ff_random_get_seed(void) | |
87 | { | |
88 | return av_get_random_seed(); | |
89 | } | |
90 | #endif |