]> git.sesse.net Git - mlt/blob - src/modules/core/producer_noise.c
Minor tweaks
[mlt] / src / modules / core / producer_noise.c
1 /*
2  * producer_noise.c -- noise generating producer
3  * Copyright (C) 2003-2004 Ushodaya Enterprises Limited
4  * Author: Charles Yates <charles.yates@pandora.be>
5  *
6  * This program is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation; either version 2 of the License, or
9  * (at your option) any later version.
10  *
11  * This program 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
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program; if not, write to the Free Software Foundation,
18  * Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
19  */
20
21 #include "producer_noise.h"
22 #include <framework/mlt_frame.h>
23 #include <framework/mlt_pool.h>
24
25 #include <stdio.h>
26 #include <stdlib.h>
27 #include <string.h>
28
29 /** Random number generator
30 */
31
32 static unsigned int seed_x = 521288629;
33 static unsigned int seed_y = 362436069;
34
35 static unsigned inline int fast_rand( )
36 {
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 ) );
41 }
42
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 );
46
47 /** Initialise.
48 */
49
50 mlt_producer producer_noise_init( void *arg )
51 {
52         // Create a new producer object
53         mlt_producer this = mlt_producer_new( );
54
55         // Initialise the producer
56         if ( this != NULL )
57         {
58                 // Synthetic - aspect ratio of 1
59                 mlt_properties_set_double( mlt_producer_properties( this ), "aspect_ratio", 1 );
60
61                 // Callback registration
62                 this->get_frame = producer_get_frame;
63                 this->close = producer_close;
64         }
65
66         return this;
67 }
68
69 static int producer_get_image( mlt_frame frame, uint8_t **buffer, mlt_image_format *format, int *width, int *height, int writable )
70 {
71         // Obtain properties of frame
72         mlt_properties properties = mlt_frame_properties( frame );
73
74         // Calculate the size of the image
75         int size = *width * *height * 2;
76
77         // Set the format being returned
78         *format = mlt_image_yuv422;
79
80         // Allocate the image
81         *buffer = mlt_pool_alloc( size );
82
83         // Update the frame
84         mlt_properties_set_data( properties, "image", *buffer, size, mlt_pool_release, NULL );
85         mlt_properties_set_int( properties, "width", *width );
86         mlt_properties_set_int( properties, "height", *height );
87
88         // Before we write to the image, make sure we have one
89         if ( *buffer != NULL )
90         {
91                 // Calculate the end of the buffer
92                 uint8_t *p = *buffer + *width * *height * 2;
93
94                 // Value to hold a random number
95                 uint32_t value;
96
97                 // Generate random noise
98                 while ( p != *buffer )
99                 {
100                         value = fast_rand( ) & 0xff;
101                         *( -- p ) = 128;
102                         *( -- p ) = value < 16 ? 16 : value > 240 ? 240 : value;
103                 }
104         }
105
106         return 0;
107 }
108
109 static int producer_get_audio( mlt_frame frame, int16_t **buffer, mlt_audio_format *format, int *frequency, int *channels, int *samples )
110 {
111         // Get the frame properties
112         mlt_properties properties = mlt_frame_properties( frame );
113
114         int size = 0;
115
116         // Correct the returns if necessary
117         *samples = *samples <= 0 ? 1920 : *samples;
118         *channels = *channels <= 0 ? 2 : *channels;
119         *frequency = *frequency <= 0 ? 48000 : *frequency;
120
121         // Calculate the size of the buffer
122         size = *samples * *channels * sizeof( int16_t );
123
124         // Allocate the buffer
125         *buffer = mlt_pool_alloc( size );
126
127         // Make sure we got one and fill it
128         if ( *buffer != NULL )
129         {
130                 int16_t *p = *buffer + size / 2;
131                 while ( p != *buffer ) 
132                         *( -- p ) = fast_rand( ) & 0x0f00;
133         }
134
135         // Set the buffer for destruction
136         mlt_properties_set_data( properties, "audio", *buffer, size, ( mlt_destructor )mlt_pool_release, NULL );
137
138         return 0;
139 }
140
141 static int producer_get_frame( mlt_producer this, mlt_frame_ptr frame, int index )
142 {
143         // Generate a frame
144         *frame = mlt_frame_init( );
145
146         // Check that we created a frame and initialise it
147         if ( *frame != NULL )
148         {
149                 // Obtain properties of frame
150                 mlt_properties properties = mlt_frame_properties( *frame );
151
152                 // Obtain properties of producer
153                 mlt_properties producer_props = mlt_producer_properties( this );
154
155                 // Determine if we're producing PAL or NTSC
156                 int is_pal = mlt_properties_get_double( producer_props, "fps" ) == 25.0;
157
158                 // Aspect ratio is 1?
159                 mlt_properties_set_double( properties, "aspect_ratio", is_pal ? 128.0/117.0 : 72.0/79.0 );
160
161                 // Set producer-specific frame properties
162                 mlt_properties_set_int( properties, "progressive", 1 );
163
164                 // Update timecode on the frame we're creating
165                 mlt_frame_set_position( *frame, mlt_producer_position( this ) );
166
167                 // Push the get_image method
168                 mlt_frame_push_get_image( *frame, producer_get_image );
169
170                 // Specify the audio
171                 ( *frame )->get_audio = producer_get_audio;
172         }
173
174         // Calculate the next timecode
175         mlt_producer_prepare_next( this );
176
177         return 0;
178 }
179
180 static void producer_close( mlt_producer this )
181 {
182         this->close = NULL;
183         mlt_producer_close( this );
184         free( this );
185 }
186