[Ffmpeg-devel] [RFC] av_random...

Ryan Martell rdm4
Mon Jan 8 03:08:01 CET 2007

On Jan 6, 2007, at 5:42 AM, Michael Niedermayer wrote:

> Hi
> On Fri, Jan 05, 2007 at 07:56:41PM -0600, Ryan Martell wrote:
>> Hi--
>> I need to have a random for generating a GUID for mms streaming, and
>> there also needs to be one for AC3 (right?).  Michael suggested
>> "a LFG or mersenne twister based av_random() for libavutil would
>> certainly be welcome"
>> I have tweaked some example source code, and have sent an email
>> asking the author if he would allow it for inclusion; if not, I'll
>> rewrite it based on the published formula.
>> Does this an interface work for you:
>> #define AV_RANDOM_PERIOD 624
> i dont like that name at all, as its not the period of the generator
> maybe simply AV_RANDOM_N would do or maybe calling it
> degree of recurrence like wikipedia does ...


>> typedef struct {
>>     unsigned int magic_cookie; ///< prevent someone from calling
>> random without the state setup (sets it up automatically)
>>     unsigned long mt[AV_RANDOM_PERIOD];	///< the array for the state
>> vector
>>     int mti;
>> } AVRandomState;
>> void av_init_random(unsigned long seed, AVRandomState *state);
>> unsigned long av_random(AVRandomState *state); // 32 bit random
>> double av_random_real1(AVRandomState *state); // [0-1] as double
>> Or would you rather have:
>> AVRandomState *av_init_random(unsigned long seed);
>> void av_free_random(AVRandomState *state);
>> unsigned long av_random(AVRandomState *state);
> this is closer to what i had in mind ...

Well, this doesn't make as much sense with the random being inlined,  
since the AVRandomState would have to be visible for the inlining to  
work.  In my mind, it would make sense for it to use a malloc/free if  
the structure was opaque (in the c file), but not if it's in the h file.

> though i would like av_random() to be in a header file and be  
> static inline
> as its likely called often, the function is pretty small and speed  
> might
> matter
> also long -> int, POSIX gurantees that int is >=32bit
> and avoid (u)int32_t in the code as it might be slow on some obscure
> architectures

How about something like the attached (this is only the header file,  
not the c file.).

I looked at the AC3 stuff, and from what I can tell, it will repeat  
after N (624) iterations, since it's not regenerating the untempered  
values.  This corrects that.

-------------- next part --------------
A non-text attachment was scrubbed...
Name: random.h
Type: application/octet-stream
Size: 2614 bytes
Desc: not available
URL: <http://lists.mplayerhq.hu/pipermail/ffmpeg-devel/attachments/20070107/fd61628b/attachment.obj>

More information about the ffmpeg-devel mailing list