]> git.sesse.net Git - mlt/blobdiff - src/modules/core/producer_noise.c
Fix pause for noise producer.
[mlt] / src / modules / core / producer_noise.c
index 825f5912493f3def1b37289bad2838681275c373..b894f51a3b098c3c0f90f2ba5948b7b30462c424 100644 (file)
 /** Random number generator
 */
 
-static unsigned int seed_x = 521288629;
-static unsigned int seed_y = 362436069;
+typedef struct
+{
+       unsigned int x;
+       unsigned int y;
+} rand_seed;
+
+static void init_seed( rand_seed* seed, int init )
+{
+       // Use the initial value to initialize the seed to arbitrary values.
+       // This causes the algorithm to produce consistent results each time for the same frame number.
+       seed->x = 521288629 + init - ( init << 16 );
+       seed->y = 362436069 - init + ( init << 16 );
+}
 
-static inline unsigned int fast_rand( )
+static inline unsigned int fast_rand( rand_seed* seed )
 {
-   static unsigned int a = 18000, b = 30903;
-   seed_x = a * ( seed_x & 65535 ) + ( seed_x >> 16 );
-   seed_y = b * ( seed_y & 65535 ) + ( seed_y >> 16 );
-   return ( ( seed_x << 16 ) + ( seed_y & 65535 ) );
+       static unsigned int a = 18000, b = 30903;
+       seed->x = a * ( seed->x & 65535 ) + ( seed->x >> 16 );
+       seed->y = b * ( seed->y & 65535 ) + ( seed->y >> 16 );
+       return ( ( seed->x << 16 ) + ( seed->y & 65535 ) );
 }
 
 // Foward declarations
@@ -50,7 +61,7 @@ static void producer_close( mlt_producer this );
 mlt_producer producer_noise_init( mlt_profile profile, mlt_service_type type, const char *id, char *arg )
 {
        // Create a new producer object
-       mlt_producer this = mlt_producer_new( );
+       mlt_producer this = mlt_producer_new( profile );
 
        // Initialise the producer
        if ( this != NULL )
@@ -65,8 +76,11 @@ mlt_producer producer_noise_init( mlt_profile profile, mlt_service_type type, co
 
 static int producer_get_image( mlt_frame frame, uint8_t **buffer, mlt_image_format *format, int *width, int *height, int writable )
 {
-       // Obtain properties of frame
-       mlt_properties properties = MLT_FRAME_PROPERTIES( frame );
+       // Choose suitable out values if nothing specific requested
+       if ( *width <= 0 )
+               *width = mlt_service_profile( MLT_PRODUCER_SERVICE( mlt_frame_get_original_producer( frame ) ) )->width;
+       if ( *height <= 0 )
+               *height = mlt_service_profile( MLT_PRODUCER_SERVICE( mlt_frame_get_original_producer( frame ) ) )->height;
 
        // Calculate the size of the image
        int size = *width * *height * 2;
@@ -78,9 +92,7 @@ static int producer_get_image( mlt_frame frame, uint8_t **buffer, mlt_image_form
        *buffer = mlt_pool_alloc( size );
 
        // Update the frame
-       mlt_properties_set_data( properties, "image", *buffer, size, mlt_pool_release, NULL );
-       mlt_properties_set_int( properties, "width", *width );
-       mlt_properties_set_int( properties, "height", *height );
+       mlt_frame_set_image( frame, *buffer, size, mlt_pool_release );
 
        // Before we write to the image, make sure we have one
        if ( *buffer != NULL )
@@ -91,10 +103,14 @@ static int producer_get_image( mlt_frame frame, uint8_t **buffer, mlt_image_form
                // Value to hold a random number
                uint32_t value;
 
+               // Initialize seed from the frame number.
+               rand_seed seed;
+               init_seed( &seed, mlt_frame_get_position( frame ) );
+
                // Generate random noise
                while ( p != *buffer )
                {
-                       value = fast_rand( ) & 0xff;
+                       value = fast_rand( &seed ) & 0xff;
                        *( -- p ) = 128;
                        *( -- p ) = value < 16 ? 16 : value > 240 ? 240 : value;
                }
@@ -105,15 +121,13 @@ static int producer_get_image( mlt_frame frame, uint8_t **buffer, mlt_image_form
 
 static int producer_get_audio( mlt_frame frame, int16_t **buffer, mlt_audio_format *format, int *frequency, int *channels, int *samples )
 {
-       // Get the frame properties
-       mlt_properties properties = MLT_FRAME_PROPERTIES( frame );
-
        int size = 0;
 
        // Correct the returns if necessary
        *samples = *samples <= 0 ? 1920 : *samples;
        *channels = *channels <= 0 ? 2 : *channels;
        *frequency = *frequency <= 0 ? 48000 : *frequency;
+       *format = mlt_audio_s16;
 
        // Calculate the size of the buffer
        size = *samples * *channels * sizeof( int16_t );
@@ -125,12 +139,14 @@ static int producer_get_audio( mlt_frame frame, int16_t **buffer, mlt_audio_form
        if ( *buffer != NULL )
        {
                int16_t *p = *buffer + size / 2;
-               while ( p != *buffer ) 
-                       *( -- p ) = fast_rand( ) & 0x0f00;
+               rand_seed seed;
+               init_seed( &seed, mlt_frame_get_position( frame ) );
+               while ( p != *buffer )
+                       *( -- p ) = fast_rand( &seed ) & 0x0f00;
        }
 
        // Set the buffer for destruction
-       mlt_properties_set_data( properties, "audio", *buffer, size, ( mlt_destructor )mlt_pool_release, NULL );
+       mlt_frame_set_audio( frame, *buffer, *format, size, mlt_pool_release );
 
        return 0;
 }
@@ -147,7 +163,8 @@ static int producer_get_frame( mlt_producer this, mlt_frame_ptr frame, int index
                mlt_properties properties = MLT_FRAME_PROPERTIES( *frame );
 
                // Aspect ratio is whatever it needs to be
-               mlt_properties_set_double( properties, "aspect_ratio", 0 );
+               mlt_profile profile = mlt_service_profile( MLT_PRODUCER_SERVICE( this ) );
+               mlt_properties_set_double( properties, "aspect_ratio", mlt_profile_sar( profile ) );
 
                // Set producer-specific frame properties
                mlt_properties_set_int( properties, "progressive", 1 );