fezzik gets a rhyming dictionary
[melted] / 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
46 /** Initialise.
47 */
48
49 mlt_producer producer_noise_init( void *arg )
50 {
51 // Create a new producer object
52 mlt_producer this = mlt_producer_new( );
53
54 // Initialise the producer
55 if ( this != NULL )
56 {
57 // Callback registration
58 this->get_frame = producer_get_frame;
59 }
60
61 return this;
62 }
63
64 static int producer_get_image( mlt_frame frame, uint8_t **buffer, mlt_image_format *format, int *width, int *height, int writable )
65 {
66 // Obtain properties of frame
67 mlt_properties properties = mlt_frame_properties( frame );
68
69 // Calculate the size of the image
70 int size = *width * *height * 2;
71
72 // Set the format being returned
73 *format = mlt_image_yuv422;
74
75 // Allocate the image
76 *buffer = mlt_pool_alloc( size );
77
78 // Update the frame
79 mlt_properties_set_data( properties, "image", *buffer, size, mlt_pool_release, NULL );
80 mlt_properties_set_int( properties, "width", *width );
81 mlt_properties_set_int( properties, "height", *height );
82
83 // Before we write to the image, make sure we have one
84 if ( *buffer != NULL )
85 {
86 // Calculate the end of the buffer
87 uint8_t *p = *buffer + *width * *height * 2;
88
89 // Value to hold a random number
90 uint32_t value;
91
92 // Generate random noise
93 while ( p != *buffer )
94 {
95 value = fast_rand( ) & 0xff;
96 *( -- p ) = 128;
97 *( -- p ) = value < 16 ? 16 : value > 240 ? 240 : value;
98 }
99 }
100
101 return 0;
102 }
103
104 static int producer_get_audio( mlt_frame frame, int16_t **buffer, mlt_audio_format *format, int *frequency, int *channels, int *samples )
105 {
106 // Get the frame properties
107 mlt_properties properties = mlt_frame_properties( frame );
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
116 // Calculate the size of the buffer
117 size = *samples * *channels * sizeof( int16_t );
118
119 // Allocate the buffer
120 *buffer = mlt_pool_alloc( size );
121
122 // Make sure we got one and fill it
123 if ( *buffer != NULL )
124 {
125 int16_t *p = *buffer + size / 2;
126 while ( p != *buffer )
127 *( -- p ) = fast_rand( ) & 0xff;
128 }
129
130 // Set the buffer for destruction
131 mlt_properties_set_data( properties, "audio", *buffer, size, ( mlt_destructor )mlt_pool_release, NULL );
132
133 return 0;
134 }
135
136 static int producer_get_frame( mlt_producer this, mlt_frame_ptr frame, int index )
137 {
138 // Generate a frame
139 *frame = mlt_frame_init( );
140
141 // Check that we created a frame and initialise it
142 if ( *frame != NULL )
143 {
144 // Obtain properties of frame
145 mlt_properties properties = mlt_frame_properties( *frame );
146
147 // Aspect ratio is 1?
148 mlt_properties_set_double( properties, "aspect_ratio", 1.0 );
149
150 // Set producer-specific frame properties
151 mlt_properties_set_int( properties, "progressive", 1 );
152
153 // Update timecode on the frame we're creating
154 mlt_frame_set_position( *frame, mlt_producer_position( this ) );
155
156 // Push the get_image method
157 mlt_frame_push_get_image( *frame, producer_get_image );
158
159 // Specify the audio
160 ( *frame )->get_audio = producer_get_audio;
161 }
162
163 // Calculate the next timecode
164 mlt_producer_prepare_next( this );
165
166 return 0;
167 }
168
169