2 * filter_region.c -- region filter
3 * Copyright (C) 2003-2004 Ushodaya Enterprises Limited
4 * Author: Charles Yates <charles.yates@pandora.be>
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.
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.
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.
21 #include "filter_region.h"
22 #include "transition_composite.h"
24 #include <framework/mlt.h>
30 static int create_instance( mlt_filter this, char *name, char *value, int count )
32 // Return from this function
35 // Duplicate the value
36 char *type = strdup( value );
38 // Pointer to filter argument
39 char *arg = type == NULL ? NULL : strchr( type, ':' );
41 // New filter being created
42 mlt_filter filter = NULL;
44 // Cleanup type and arg
49 filter = mlt_factory_filter( type, arg );
51 // If we have a filter, then initialise and store it
55 mlt_properties properties = mlt_filter_properties( this );
57 // String to hold the property name
60 // String to hold the passdown key
64 sprintf( id, "_filter_%d", count );
67 sprintf( key, "%s.", name );
69 // Pass all the key properties on the filter down
70 mlt_properties_pass( mlt_filter_properties( filter ), properties, key );
72 // Ensure that filter is assigned
73 mlt_properties_set_data( properties, id, filter, 0, ( mlt_destructor )mlt_filter_close, NULL );
77 // Indicate that an error has occurred
84 // Return error condition
88 static uint8_t *filter_get_alpha_mask( mlt_frame this )
90 // Obtain properties of frame
91 mlt_properties properties = mlt_frame_properties( this );
93 // Return the alpha mask
94 return mlt_properties_get_data( properties, "alpha", NULL );
100 static int filter_get_image( mlt_frame frame, uint8_t **image, mlt_image_format *format, int *width, int *height, int writable )
102 // Error we will return
105 // Get the watermark filter object
106 mlt_filter this = mlt_frame_pop_service( frame );
108 // Get the properties of the filter
109 mlt_properties properties = mlt_filter_properties( this );
111 // Get the composite from the filter
112 mlt_transition composite = mlt_properties_get_data( properties, "composite", NULL );
114 // Look for the first filter
115 mlt_filter filter = mlt_properties_get_data( properties, "_filter_0", NULL );
117 // Create a composite if we don't have one
118 if ( composite == NULL )
120 // Create composite via the factory
121 composite = mlt_factory_transition( "composite", NULL );
124 if ( composite != NULL )
126 // Get the properties
127 mlt_properties composite_properties = mlt_transition_properties( composite );
129 // We want to ensure that we don't get a wobble...
130 mlt_properties_set( composite_properties, "distort", "true" );
131 mlt_properties_set( composite_properties, "progressive", "1" );
133 // Pass all the composite. properties on the filter down
134 mlt_properties_pass( composite_properties, properties, "composite." );
136 // Register the composite for reuse/destruction
137 mlt_properties_set_data( properties, "composite", composite, 0, ( mlt_destructor )mlt_transition_close, NULL );
142 if ( filter == NULL )
147 // Number of filters created
150 // Loop for all properties
151 for ( i = 0; i < mlt_properties_count( properties ); i ++ )
153 // Get the name of this property
154 char *name = mlt_properties_get_name( properties, i );
156 // If the name does not contain a . and matches filter
157 if ( strchr( name, '.' ) == NULL && !strncmp( name, "filter", 6 ) )
159 // Get the filter constructor
160 char *value = mlt_properties_get_value( properties, i );
162 // Create an instance
163 if ( create_instance( this, name, value, count ) == 0 )
170 error = mlt_frame_get_image( frame, image, format, width, height, 1 );
172 // Only continue if we have both filter and composite
173 if ( composite != NULL && filter != NULL )
175 // String to hold the filter to query on
178 // Index to hold the count
181 // We will get the 'b frame' from the composite
182 mlt_frame b_frame = composite_copy_region( composite, frame );
184 // Make sure the filter is in the correct position
185 while ( filter != NULL )
188 mlt_filter_process( filter, b_frame );
190 // Generate the key for the next
191 sprintf( id, "_filter_%d", ++ i );
193 // Get the next filter
194 filter = mlt_properties_get_data( properties, id, NULL );
197 // Get the b frame and process with composite if successful
198 mlt_transition_process( composite, frame, b_frame );
200 // See if we have a shape producer
201 // Copy the alpha mask from the shape frame to the b_frame
202 char *resource = mlt_properties_get( properties, "resource" );
204 if ( strcmp( resource, "rectangle" ) != 0 )
206 if ( strcmp( resource, "circle" ) == 0 )
208 resource = strdup( "pixbuf" );
209 mlt_properties_set( properties, "producer.resource", "<svg width='100' height='100'><circle cx='50' cy='50' r='50' fill='black'/></svg>" );
212 // Get the producer from the filter
213 mlt_producer producer = mlt_properties_get_data( properties, "producer", NULL );
215 if ( producer == NULL )
217 // Get the factory producer service
218 char *factory = mlt_properties_get( properties, "factory" );
220 // Create the producer
221 producer = mlt_factory_producer( factory, resource );
224 if ( producer != NULL )
226 // Get the producer properties
227 mlt_properties producer_properties = mlt_producer_properties( producer );
229 // Ensure that we loop
230 mlt_properties_set( producer_properties, "eof", "loop" );
232 // Now pass all producer. properties on the filter down
233 mlt_properties_pass( producer_properties, properties, "producer." );
235 // Register the producer for reuse/destruction
236 mlt_properties_set_data( properties, "producer", producer, 0, ( mlt_destructor )mlt_producer_close, NULL );
240 if ( producer != NULL )
242 // We will get the alpha frame from the producer
243 mlt_frame shape_frame = NULL;
245 // Get the unique id of the filter (used to reacquire the producer position)
246 char *name = mlt_properties_get( properties, "_unique_id" );
248 // Get the original producer position
249 mlt_position position = mlt_properties_get_position( mlt_frame_properties( frame ), name );
251 // Make sure the producer is in the correct position
252 mlt_producer_seek( producer, position );
254 // Get the shape frame
255 if ( mlt_service_get_frame( mlt_producer_service( producer ), &shape_frame, 0 ) == 0 )
257 int region_width = mlt_properties_get_int( mlt_frame_properties( b_frame ), "width" );
258 int region_height = mlt_properties_get_int( mlt_frame_properties( b_frame ), "height" );
260 // Get the shape image to trigger alpha creation
261 mlt_properties_set( mlt_frame_properties( shape_frame ), "distort", "true" );
262 error = mlt_frame_get_image( shape_frame, image, format, ®ion_width, ®ion_height, 1 );
264 // Only override any existing b_frame alpha if the shape has an alpha
265 if ( mlt_frame_get_alpha_mask( shape_frame ) != NULL )
267 // Set the b_frame alpha from the shape frame
268 mlt_properties_set_data( mlt_frame_properties( b_frame ), "alpha", mlt_frame_get_alpha_mask( shape_frame ), 0 , NULL, NULL );
269 b_frame->get_alpha_mask = filter_get_alpha_mask;
273 error = mlt_frame_get_image( frame, image, format, width, height, 0 );
276 mlt_frame_close( b_frame );
279 // Close the shape frame
280 mlt_frame_close( shape_frame );
284 if ( b_frame != NULL )
287 error = mlt_frame_get_image( frame, image, format, width, height, 0 );
290 mlt_frame_close( b_frame );
297 /** Filter processing.
300 static mlt_frame filter_process( mlt_filter this, mlt_frame frame )
302 // Push the filter on to the frame
303 mlt_frame_push_service( frame, this );
305 // Push the filter method
306 mlt_frame_push_get_image( frame, filter_get_image );
312 /** Constructor for the filter.
315 mlt_filter filter_region_init( void *arg )
317 // Create a new filter
318 mlt_filter this = mlt_filter_new( );
320 // Further initialisation
323 // Get the properties from the filter
324 mlt_properties properties = mlt_filter_properties( this );
326 // Assign the filter process method
327 this->process = filter_process;
329 mlt_properties_set( properties, "factory", "fezzik" );
331 // Resource defines the shape of the region
332 mlt_properties_set( properties, "resource", arg == NULL ? "rectangle" : arg );