]> git.sesse.net Git - mlt/blob - src/modules/core/producer_noise.c
Fix pause for noise producer.
[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 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.
10  *
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.
15  *
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
19  */
20
21 #include <framework/mlt_producer.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 typedef struct
33 {
34         unsigned int x;
35         unsigned int y;
36 } rand_seed;
37
38 static void init_seed( rand_seed* seed, int init )
39 {
40         // Use the initial value to initialize the seed to arbitrary values.
41         // This causes the algorithm to produce consistent results each time for the same frame number.
42         seed->x = 521288629 + init - ( init << 16 );
43         seed->y = 362436069 - init + ( init << 16 );
44 }
45
46 static inline unsigned int fast_rand( rand_seed* seed )
47 {
48         static unsigned int a = 18000, b = 30903;
49         seed->x = a * ( seed->x & 65535 ) + ( seed->x >> 16 );
50         seed->y = b * ( seed->y & 65535 ) + ( seed->y >> 16 );
51         return ( ( seed->x << 16 ) + ( seed->y & 65535 ) );
52 }
53
54 // Foward declarations
55 static int producer_get_frame( mlt_producer this, mlt_frame_ptr frame, int index );
56 static void producer_close( mlt_producer this );
57
58 /** Initialise.
59 */
60
61 mlt_producer producer_noise_init( mlt_profile profile, mlt_service_type type, const char *id, char *arg )
62 {
63         // Create a new producer object
64         mlt_producer this = mlt_producer_new( profile );
65
66         // Initialise the producer
67         if ( this != NULL )
68         {
69                 // Callback registration
70                 this->get_frame = producer_get_frame;
71                 this->close = ( mlt_destructor )producer_close;
72         }
73
74         return this;
75 }
76
77 static int producer_get_image( mlt_frame frame, uint8_t **buffer, mlt_image_format *format, int *width, int *height, int writable )
78 {
79         // Choose suitable out values if nothing specific requested
80         if ( *width <= 0 )
81                 *width = mlt_service_profile( MLT_PRODUCER_SERVICE( mlt_frame_get_original_producer( frame ) ) )->width;
82         if ( *height <= 0 )
83                 *height = mlt_service_profile( MLT_PRODUCER_SERVICE( mlt_frame_get_original_producer( frame ) ) )->height;
84
85         // Calculate the size of the image
86         int size = *width * *height * 2;
87
88         // Set the format being returned
89         *format = mlt_image_yuv422;
90
91         // Allocate the image
92         *buffer = mlt_pool_alloc( size );
93
94         // Update the frame
95         mlt_frame_set_image( frame, *buffer, size, mlt_pool_release );
96
97         // Before we write to the image, make sure we have one
98         if ( *buffer != NULL )
99         {
100                 // Calculate the end of the buffer
101                 uint8_t *p = *buffer + *width * *height * 2;
102
103                 // Value to hold a random number
104                 uint32_t value;
105
106                 // Initialize seed from the frame number.
107                 rand_seed seed;
108                 init_seed( &seed, mlt_frame_get_position( frame ) );
109
110                 // Generate random noise
111                 while ( p != *buffer )
112                 {
113                         value = fast_rand( &seed ) & 0xff;
114                         *( -- p ) = 128;
115                         *( -- p ) = value < 16 ? 16 : value > 240 ? 240 : value;
116                 }
117         }
118
119         return 0;
120 }
121
122 static int producer_get_audio( mlt_frame frame, int16_t **buffer, mlt_audio_format *format, int *frequency, int *channels, int *samples )
123 {
124         int size = 0;
125
126         // Correct the returns if necessary
127         *samples = *samples <= 0 ? 1920 : *samples;
128         *channels = *channels <= 0 ? 2 : *channels;
129         *frequency = *frequency <= 0 ? 48000 : *frequency;
130         *format = mlt_audio_s16;
131
132         // Calculate the size of the buffer
133         size = *samples * *channels * sizeof( int16_t );
134
135         // Allocate the buffer
136         *buffer = mlt_pool_alloc( size );
137
138         // Make sure we got one and fill it
139         if ( *buffer != NULL )
140         {
141                 int16_t *p = *buffer + size / 2;
142                 rand_seed seed;
143                 init_seed( &seed, mlt_frame_get_position( frame ) );
144                 while ( p != *buffer )
145                         *( -- p ) = fast_rand( &seed ) & 0x0f00;
146         }
147
148         // Set the buffer for destruction
149         mlt_frame_set_audio( frame, *buffer, *format, size, mlt_pool_release );
150
151         return 0;
152 }
153
154 static int producer_get_frame( mlt_producer this, mlt_frame_ptr frame, int index )
155 {
156         // Generate a frame
157         *frame = mlt_frame_init( MLT_PRODUCER_SERVICE( this ) );
158
159         // Check that we created a frame and initialise it
160         if ( *frame != NULL )
161         {
162                 // Obtain properties of frame
163                 mlt_properties properties = MLT_FRAME_PROPERTIES( *frame );
164
165                 // Aspect ratio is whatever it needs to be
166                 mlt_profile profile = mlt_service_profile( MLT_PRODUCER_SERVICE( this ) );
167                 mlt_properties_set_double( properties, "aspect_ratio", mlt_profile_sar( profile ) );
168
169                 // Set producer-specific frame properties
170                 mlt_properties_set_int( properties, "progressive", 1 );
171
172                 // Update timecode on the frame we're creating
173                 mlt_frame_set_position( *frame, mlt_producer_position( this ) );
174
175                 // Push the get_image method
176                 mlt_frame_push_get_image( *frame, producer_get_image );
177
178                 // Specify the audio
179                 mlt_frame_push_audio( *frame, producer_get_audio );
180         }
181
182         // Calculate the next timecode
183         mlt_producer_prepare_next( this );
184
185         return 0;
186 }
187
188 static void producer_close( mlt_producer this )
189 {
190         this->close = NULL;
191         mlt_producer_close( this );
192         free( this );
193 }
194