]> git.sesse.net Git - mlt/blob - src/modules/core/producer_noise.c
0e39eb7a3097e2d4d78f190f7665b3bc0b3c6993
[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 static unsigned int seed_x = 521288629;
33 static unsigned int seed_y = 362436069;
34
35 static inline unsigned 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( mlt_profile profile, mlt_service_type type, const char *id, char *arg )
51 {
52         // Create a new producer object
53         mlt_producer this = mlt_producer_new( profile );
54
55         // Initialise the producer
56         if ( this != NULL )
57         {
58                 // Callback registration
59                 this->get_frame = producer_get_frame;
60                 this->close = ( mlt_destructor )producer_close;
61         }
62
63         return this;
64 }
65
66 static int producer_get_image( mlt_frame frame, uint8_t **buffer, mlt_image_format *format, int *width, int *height, int writable )
67 {
68         // Choose suitable out values if nothing specific requested
69         if ( *width <= 0 )
70                 *width = mlt_service_profile( MLT_PRODUCER_SERVICE( mlt_frame_get_original_producer( frame ) ) )->width;
71         if ( *height <= 0 )
72                 *height = mlt_service_profile( MLT_PRODUCER_SERVICE( mlt_frame_get_original_producer( frame ) ) )->height;
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_frame_set_image( frame, *buffer, size, mlt_pool_release );
85
86         // Before we write to the image, make sure we have one
87         if ( *buffer != NULL )
88         {
89                 // Calculate the end of the buffer
90                 uint8_t *p = *buffer + *width * *height * 2;
91
92                 // Value to hold a random number
93                 uint32_t value;
94
95                 // Generate random noise
96                 while ( p != *buffer )
97                 {
98                         value = fast_rand( ) & 0xff;
99                         *( -- p ) = 128;
100                         *( -- p ) = value < 16 ? 16 : value > 240 ? 240 : value;
101                 }
102         }
103
104         return 0;
105 }
106
107 static int producer_get_audio( mlt_frame frame, int16_t **buffer, mlt_audio_format *format, int *frequency, int *channels, int *samples )
108 {
109         int size = 0;
110
111         // Correct the returns if necessary
112         *samples = *samples <= 0 ? 1920 : *samples;
113         *channels = *channels <= 0 ? 2 : *channels;
114         *frequency = *frequency <= 0 ? 48000 : *frequency;
115         *format = mlt_audio_s16;
116
117         // Calculate the size of the buffer
118         size = *samples * *channels * sizeof( int16_t );
119
120         // Allocate the buffer
121         *buffer = mlt_pool_alloc( size );
122
123         // Make sure we got one and fill it
124         if ( *buffer != NULL )
125         {
126                 int16_t *p = *buffer + size / 2;
127                 while ( p != *buffer ) 
128                         *( -- p ) = fast_rand( ) & 0x0f00;
129         }
130
131         // Set the buffer for destruction
132         mlt_frame_set_audio( frame, *buffer, *format, size, mlt_pool_release );
133
134         return 0;
135 }
136
137 static int producer_get_frame( mlt_producer this, mlt_frame_ptr frame, int index )
138 {
139         // Generate a frame
140         *frame = mlt_frame_init( MLT_PRODUCER_SERVICE( this ) );
141
142         // Check that we created a frame and initialise it
143         if ( *frame != NULL )
144         {
145                 // Obtain properties of frame
146                 mlt_properties properties = MLT_FRAME_PROPERTIES( *frame );
147
148                 // Aspect ratio is whatever it needs to be
149                 mlt_profile profile = mlt_service_profile( MLT_PRODUCER_SERVICE( this ) );
150                 mlt_properties_set_double( properties, "aspect_ratio", mlt_profile_sar( profile ) );
151
152                 // Set producer-specific frame properties
153                 mlt_properties_set_int( properties, "progressive", 1 );
154
155                 // Update timecode on the frame we're creating
156                 mlt_frame_set_position( *frame, mlt_producer_position( this ) );
157
158                 // Push the get_image method
159                 mlt_frame_push_get_image( *frame, producer_get_image );
160
161                 // Specify the audio
162                 mlt_frame_push_audio( *frame, producer_get_audio );
163         }
164
165         // Calculate the next timecode
166         mlt_producer_prepare_next( this );
167
168         return 0;
169 }
170
171 static void producer_close( mlt_producer this )
172 {
173         this->close = NULL;
174         mlt_producer_close( this );
175         free( this );
176 }
177