b6ef9708701d30576b2d83435bbf65f85568dfca
[melted] / src / modules / core / filter_region.c
1 /*
2 * filter_region.c -- region filter
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 "filter_region.h"
22 #include "transition_composite.h"
23
24 #include <framework/mlt.h>
25
26 #include <stdio.h>
27 #include <stdlib.h>
28 #include <string.h>
29
30 static int create_instance( mlt_filter this, char *name, char *value, int count )
31 {
32 // Return from this function
33 int error = 0;
34
35 // Duplicate the value
36 char *type = strdup( value );
37
38 // Pointer to filter argument
39 char *arg = type == NULL ? NULL : strchr( type, ':' );
40
41 // New filter being created
42 mlt_filter filter = NULL;
43
44 // Cleanup type and arg
45 if ( arg != NULL )
46 *arg ++ = '\0';
47
48 // Create the filter
49 filter = mlt_factory_filter( type, arg );
50
51 // If we have a filter, then initialise and store it
52 if ( filter != NULL )
53 {
54 // Properties of this
55 mlt_properties properties = mlt_filter_properties( this );
56
57 // String to hold the property name
58 char id[ 256 ];
59
60 // String to hold the passdown key
61 char key[ 256 ];
62
63 // Construct id
64 sprintf( id, "_filter_%d", count );
65
66 // Counstruct key
67 sprintf( key, "%s.", name );
68
69 // Just in case, let's assume that the filter here has a composite
70 mlt_properties_set( mlt_filter_properties( filter ), "composite.start", "0%,0%:100%x100%" );
71 mlt_properties_set( mlt_filter_properties( filter ), "composite.fill", "true" );
72
73 // Pass all the key properties on the filter down
74 mlt_properties_pass( mlt_filter_properties( filter ), properties, key );
75
76 // Ensure that filter is assigned
77 mlt_properties_set_data( properties, id, filter, 0, ( mlt_destructor )mlt_filter_close, NULL );
78 }
79 else
80 {
81 // Indicate that an error has occurred
82 error = 1;
83 }
84
85 // Cleanup
86 free( type );
87
88 // Return error condition
89 return error;
90 }
91
92 static uint8_t *filter_get_alpha_mask( mlt_frame this )
93 {
94 // Obtain properties of frame
95 mlt_properties properties = mlt_frame_properties( this );
96
97 // Return the alpha mask
98 return mlt_properties_get_data( properties, "alpha", NULL );
99 }
100
101 /** Do it :-).
102 */
103
104 static int filter_get_image( mlt_frame frame, uint8_t **image, mlt_image_format *format, int *width, int *height, int writable )
105 {
106 // Error we will return
107 int error = 0;
108
109 // Get the watermark filter object
110 mlt_filter this = mlt_frame_pop_service( frame );
111
112 // Get the properties of the filter
113 mlt_properties properties = mlt_filter_properties( this );
114
115 // Get the composite from the filter
116 mlt_transition composite = mlt_properties_get_data( properties, "composite", NULL );
117
118 // Look for the first filter
119 mlt_filter filter = mlt_properties_get_data( properties, "_filter_0", NULL );
120
121 // Get the unique id of the filter (used to reacquire the producer position)
122 char *name = mlt_properties_get( properties, "_unique_id" );
123
124 // Get the original producer position
125 mlt_position position = mlt_properties_get_position( mlt_frame_properties( frame ), name );
126
127 // Create a composite if we don't have one
128 if ( composite == NULL )
129 {
130 // Create composite via the factory
131 composite = mlt_factory_transition( "composite", NULL );
132
133 // If we have one
134 if ( composite != NULL )
135 {
136 // Get the properties
137 mlt_properties composite_properties = mlt_transition_properties( composite );
138
139 // We want to ensure that we don't get a wobble...
140 mlt_properties_set( composite_properties, "distort", "true" );
141 mlt_properties_set( composite_properties, "progressive", "1" );
142
143 // Pass all the composite. properties on the filter down
144 mlt_properties_pass( composite_properties, properties, "composite." );
145
146 // Register the composite for reuse/destruction
147 mlt_properties_set_data( properties, "composite", composite, 0, ( mlt_destructor )mlt_transition_close, NULL );
148 }
149 }
150
151 // Create filters
152 if ( filter == NULL )
153 {
154 // Loop Variable
155 int i = 0;
156
157 // Number of filters created
158 int count = 0;
159
160 // Loop for all properties
161 for ( i = 0; i < mlt_properties_count( properties ); i ++ )
162 {
163 // Get the name of this property
164 char *name = mlt_properties_get_name( properties, i );
165
166 // If the name does not contain a . and matches filter
167 if ( strchr( name, '.' ) == NULL && !strncmp( name, "filter", 6 ) )
168 {
169 // Get the filter constructor
170 char *value = mlt_properties_get_value( properties, i );
171
172 // Create an instance
173 if ( create_instance( this, name, value, count ) == 0 )
174 count ++;
175 }
176 }
177 }
178
179 // Get the image
180 error = mlt_frame_get_image( frame, image, format, width, height, 1 );
181
182 // Only continue if we have both filter and composite
183 if ( composite != NULL && filter != NULL )
184 {
185 // Get the resource of this filter (could be a shape [rectangle/circle] or an alpha provider of choice
186 char *resource = mlt_properties_get( properties, "resource" );
187
188 // String to hold the filter to query on
189 char id[ 256 ];
190
191 // Index to hold the count
192 int i = 0;
193
194 // We will get the 'b frame' from the composite
195 mlt_frame b_frame = composite_copy_region( composite, frame, position );
196
197 // Make sure the filter is in the correct position
198 while ( filter != NULL )
199 {
200 // Stack this filter
201 mlt_filter_process( filter, b_frame );
202
203 // Generate the key for the next
204 sprintf( id, "_filter_%d", ++ i );
205
206 // Get the next filter
207 filter = mlt_properties_get_data( properties, id, NULL );
208 }
209
210 // Hmm - this is probably going to go wrong....
211 mlt_frame_set_position( frame, position );
212
213 // Get the b frame and process with composite if successful
214 mlt_transition_process( composite, frame, b_frame );
215
216 // If we have a shape producer copy the alpha mask from the shape frame to the b_frame
217 if ( strcmp( resource, "rectangle" ) != 0 )
218 {
219 // Get the producer from the filter
220 mlt_producer producer = mlt_properties_get_data( properties, "producer", NULL );
221
222 // If We have no producer then create one
223 if ( producer == NULL )
224 {
225 // Get the factory producer service
226 char *factory = mlt_properties_get( properties, "factory" );
227
228 // Special case circle resource
229 if ( strcmp( resource, "circle" ) == 0 )
230 {
231 // Special case to ensure that fezzik produces a pixbuf with a NULL constructor
232 resource = "pixbuf";
233
234 // Specify the svg circle
235 mlt_properties_set( properties, "producer.resource", "<svg width='100' height='100'><circle cx='50' cy='50' r='50' fill='black'/></svg>" );
236 }
237
238 // Create the producer
239 producer = mlt_factory_producer( factory, resource );
240
241 // If we have one
242 if ( producer != NULL )
243 {
244 // Get the producer properties
245 mlt_properties producer_properties = mlt_producer_properties( producer );
246
247 // Ensure that we loop
248 mlt_properties_set( producer_properties, "eof", "loop" );
249
250 // Now pass all producer. properties on the filter down
251 mlt_properties_pass( producer_properties, properties, "producer." );
252
253 // Register the producer for reuse/destruction
254 mlt_properties_set_data( properties, "producer", producer, 0, ( mlt_destructor )mlt_producer_close, NULL );
255 }
256 }
257
258 // Now use the shape producer
259 if ( producer != NULL )
260 {
261 // We will get the alpha frame from the producer
262 mlt_frame shape_frame = NULL;
263
264 // Make sure the producer is in the correct position
265 mlt_producer_seek( producer, position );
266
267 // Get the shape frame
268 if ( mlt_service_get_frame( mlt_producer_service( producer ), &shape_frame, 0 ) == 0 )
269 {
270 int region_width = mlt_properties_get_int( mlt_frame_properties( b_frame ), "width" );
271 int region_height = mlt_properties_get_int( mlt_frame_properties( b_frame ), "height" );
272
273 // Get the shape image to trigger alpha creation
274 mlt_properties_set( mlt_frame_properties( shape_frame ), "distort", "true" );
275 error = mlt_frame_get_image( shape_frame, image, format, &region_width, &region_height, 1 );
276
277 // Only override any existing b_frame alpha if the shape has an alpha
278 if ( mlt_frame_get_alpha_mask( shape_frame ) != NULL )
279 {
280 // Set the b_frame alpha from the shape frame
281 mlt_properties_set_data( mlt_frame_properties( b_frame ), "alpha", mlt_frame_get_alpha_mask( shape_frame ), 0 , NULL, NULL );
282 b_frame->get_alpha_mask = filter_get_alpha_mask;
283 }
284
285 // Ensure that the shape frame will be closed
286 mlt_properties_set_data( mlt_frame_properties( b_frame ), "shape_frame", shape_frame, 0, ( mlt_destructor )mlt_frame_close, NULL );
287 }
288 }
289 }
290
291 // Get the image
292 error = mlt_frame_get_image( frame, image, format, width, height, 0 );
293
294 // Close the b frame
295 mlt_frame_close( b_frame );
296 }
297
298 return error;
299 }
300
301 /** Filter processing.
302 */
303
304 static mlt_frame filter_process( mlt_filter this, mlt_frame frame )
305 {
306 // Get the properties of the frame
307 mlt_properties properties = mlt_frame_properties( frame );
308
309 // Get a unique name to store the frame position
310 char *name = mlt_properties_get( mlt_filter_properties( this ), "_unique_id" );
311
312 // Assign the current position to the name
313 mlt_properties_set_position( properties, name, mlt_frame_get_position( frame ) );
314
315 // Push the filter on to the frame
316 mlt_frame_push_service( frame, this );
317
318 // Push the filter method
319 mlt_frame_push_get_image( frame, filter_get_image );
320
321 // Return the frame
322 return frame;
323 }
324
325 /** Constructor for the filter.
326 */
327
328 mlt_filter filter_region_init( void *arg )
329 {
330 // Create a new filter
331 mlt_filter this = mlt_filter_new( );
332
333 // Further initialisation
334 if ( this != NULL )
335 {
336 // Get the properties from the filter
337 mlt_properties properties = mlt_filter_properties( this );
338
339 // Assign the filter process method
340 this->process = filter_process;
341
342 mlt_properties_set( properties, "factory", "fezzik" );
343
344 // Resource defines the shape of the region
345 mlt_properties_set( properties, "resource", arg == NULL ? "rectangle" : arg );
346 }
347
348 // Return the filter
349 return this;
350 }
351