• Main Page
  • Related Pages
  • Modules
  • Data Structures
  • Files
  • Examples
  • File List
  • Globals

libavutil/random_seed.c

Go to the documentation of this file.
00001 /*
00002  * Copyright (c) 2009 Baptiste Coudurier <baptiste.coudurier@gmail.com>
00003  *
00004  * This file is part of FFmpeg.
00005  *
00006  * FFmpeg is free software; you can redistribute it and/or
00007  * modify it under the terms of the GNU Lesser General Public
00008  * License as published by the Free Software Foundation; either
00009  * version 2.1 of the License, or (at your option) any later version.
00010  *
00011  * FFmpeg is distributed in the hope that it will be useful,
00012  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00013  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00014  * Lesser General Public License for more details.
00015  *
00016  * You should have received a copy of the GNU Lesser General Public
00017  * License along with FFmpeg; if not, write to the Free Software
00018  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
00019  */
00020 
00021 #include <unistd.h>
00022 #include <fcntl.h>
00023 #include "timer.h"
00024 #include "time.h"
00025 #include "random_seed.h"
00026 #include "avutil.h"
00027 
00028 static int read_random(uint32_t *dst, const char *file)
00029 {
00030     int fd = open(file, O_RDONLY);
00031     int err = -1;
00032 
00033     if (fd == -1)
00034         return -1;
00035     err = read(fd, dst, sizeof(*dst));
00036     close(fd);
00037 
00038     return err;
00039 }
00040 
00041 static uint32_t get_generic_seed(void)
00042 {
00043     clock_t last_t=0;
00044     int bits=0;
00045     uint64_t random=0;
00046     unsigned i;
00047     float s=0.000000000001;
00048 
00049     for(i=0;bits<64;i++){
00050         clock_t t= clock();
00051         if(last_t && fabs(t-last_t)>s || t==(clock_t)-1){
00052             if(i<10000 && s<(1<<24)){
00053                 s+=s;
00054                 i=t=0;
00055             }else{
00056                 random= 2*random + (i&1);
00057                 bits++;
00058             }
00059         }
00060         last_t= t;
00061     }
00062 #ifdef AV_READ_TIME
00063     random ^= AV_READ_TIME();
00064 #else
00065     random ^= clock();
00066 #endif
00067 
00068     random += random>>32;
00069 
00070     return random;
00071 }
00072 
00073 uint32_t av_get_random_seed(void)
00074 {
00075     uint32_t seed;
00076 
00077     if (read_random(&seed, "/dev/urandom") == sizeof(seed))
00078         return seed;
00079     if (read_random(&seed, "/dev/random")  == sizeof(seed))
00080         return seed;
00081     return get_generic_seed();
00082 }
00083 
00084 #if LIBAVUTIL_VERSION_MAJOR < 51
00085 attribute_deprecated uint32_t ff_random_get_seed(void);
00086 uint32_t ff_random_get_seed(void)
00087 {
00088     return av_get_random_seed();
00089 }
00090 #endif

Generated on Wed Apr 11 2012 07:31:37 for FFmpeg by  doxygen 1.7.1