2 * producer_noise.c -- noise generating producer
3 * Copyright (C) 2003-2004 Ushodaya Enterprises Limited
4 * Author: Charles Yates <charles.yates@pandora.be>
6 * This library 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.
11 * This library 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.
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
21 #include <framework/mlt_producer.h>
22 #include <framework/mlt_frame.h>
23 #include <framework/mlt_pool.h>
29 /** Random number generator
32 static unsigned int seed_x = 521288629;
33 static unsigned int seed_y = 362436069;
35 static inline unsigned int fast_rand( )
37 static unsigned int a = 18000, b = 30903;
38 seed_x = a * ( seed_x & 65535 ) + ( seed_x >> 16 );
39 seed_y = b * ( seed_y & 65535 ) + ( seed_y >> 16 );
40 return ( ( seed_x << 16 ) + ( seed_y & 65535 ) );
43 // Foward declarations
44 static int producer_get_frame( mlt_producer this, mlt_frame_ptr frame, int index );
45 static void producer_close( mlt_producer this );
50 mlt_producer producer_noise_init( mlt_profile profile, mlt_service_type type, const char *id, char *arg )
52 // Create a new producer object
53 mlt_producer this = mlt_producer_new( profile );
55 // Initialise the producer
58 // Callback registration
59 this->get_frame = producer_get_frame;
60 this->close = ( mlt_destructor )producer_close;
66 static int producer_get_image( mlt_frame frame, uint8_t **buffer, mlt_image_format *format, int *width, int *height, int writable )
68 // Calculate the size of the image
69 int size = *width * *height * 2;
71 // Set the format being returned
72 *format = mlt_image_yuv422;
75 *buffer = mlt_pool_alloc( size );
78 mlt_frame_set_image( frame, *buffer, size, mlt_pool_release );
80 // Before we write to the image, make sure we have one
81 if ( *buffer != NULL )
83 // Calculate the end of the buffer
84 uint8_t *p = *buffer + *width * *height * 2;
86 // Value to hold a random number
89 // Generate random noise
90 while ( p != *buffer )
92 value = fast_rand( ) & 0xff;
94 *( -- p ) = value < 16 ? 16 : value > 240 ? 240 : value;
101 static int producer_get_audio( mlt_frame frame, int16_t **buffer, mlt_audio_format *format, int *frequency, int *channels, int *samples )
103 // Get the frame properties
104 mlt_properties properties = MLT_FRAME_PROPERTIES( frame );
108 // Correct the returns if necessary
109 *samples = *samples <= 0 ? 1920 : *samples;
110 *channels = *channels <= 0 ? 2 : *channels;
111 *frequency = *frequency <= 0 ? 48000 : *frequency;
113 // Calculate the size of the buffer
114 size = *samples * *channels * sizeof( int16_t );
116 // Allocate the buffer
117 *buffer = mlt_pool_alloc( size );
119 // Make sure we got one and fill it
120 if ( *buffer != NULL )
122 int16_t *p = *buffer + size / 2;
123 while ( p != *buffer )
124 *( -- p ) = fast_rand( ) & 0x0f00;
127 // Set the buffer for destruction
128 mlt_properties_set_data( properties, "audio", *buffer, size, ( mlt_destructor )mlt_pool_release, NULL );
133 static int producer_get_frame( mlt_producer this, mlt_frame_ptr frame, int index )
136 *frame = mlt_frame_init( MLT_PRODUCER_SERVICE( this ) );
138 // Check that we created a frame and initialise it
139 if ( *frame != NULL )
141 // Obtain properties of frame
142 mlt_properties properties = MLT_FRAME_PROPERTIES( *frame );
144 // Aspect ratio is whatever it needs to be
145 mlt_properties_set_double( properties, "aspect_ratio", mlt_properties_get_double( MLT_PRODUCER_PROPERTIES( this ), "aspect_ratio" ) );
147 // Set producer-specific frame properties
148 mlt_properties_set_int( properties, "progressive", 1 );
150 // Update timecode on the frame we're creating
151 mlt_frame_set_position( *frame, mlt_producer_position( this ) );
153 // Push the get_image method
154 mlt_frame_push_get_image( *frame, producer_get_image );
157 mlt_frame_push_audio( *frame, producer_get_audio );
160 // Calculate the next timecode
161 mlt_producer_prepare_next( this );
166 static void producer_close( mlt_producer this )
169 mlt_producer_close( this );