2 * transition_region.c -- region transition
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 "transition_region.h"
22 #include "transition_composite.h"
24 #include <framework/mlt.h>
30 static int create_instance( mlt_transition 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_transition_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 // 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" );
73 // Pass all the key properties on the filter down
74 mlt_properties_pass( mlt_filter_properties( filter ), properties, key );
76 // Ensure that filter is assigned
77 mlt_properties_set_data( properties, id, filter, 0, ( mlt_destructor )mlt_filter_close, NULL );
81 // Indicate that an error has occurred
88 // Return error condition
92 static uint8_t *filter_get_alpha_mask( mlt_frame this )
94 uint8_t *alpha = NULL;
96 // Obtain properties of frame
97 mlt_properties properties = mlt_frame_properties( this );
99 // Get the shape frame
100 mlt_frame shape_frame = mlt_properties_get_data( properties, "shape_frame", NULL );
102 // Get the width and height of the image
103 int region_width = mlt_properties_get_int( mlt_frame_properties( this ), "width" );
104 int region_height = mlt_properties_get_int( mlt_frame_properties( this ), "height" );
105 uint8_t *image = NULL;
106 mlt_image_format format = mlt_image_yuv422;
108 // Get the shape image to trigger alpha creation
109 mlt_properties_set( mlt_frame_properties( shape_frame ), "distort", "true" );
110 mlt_frame_get_image( shape_frame, &image, &format, ®ion_width, ®ion_height, 0 );
112 alpha = mlt_frame_get_alpha_mask( shape_frame );
114 // Generate from the Y component of the image if no alpha available
117 int size = region_width * region_height;
118 uint8_t *p = mlt_pool_alloc( size );
125 mlt_properties_set_data( mlt_frame_properties( shape_frame ), "alpha", alpha,
126 region_width * region_height, mlt_pool_release, NULL );
135 static int transition_get_image( mlt_frame frame, uint8_t **image, mlt_image_format *format, int *width, int *height, int writable )
137 // Error we will return
140 // We will get the 'b frame' from the frame stack
141 mlt_frame b_frame = mlt_frame_pop_frame( frame );
143 // Get the watermark transition object
144 mlt_transition this = mlt_frame_pop_service( frame );
146 // Get the properties of the transition
147 mlt_properties properties = mlt_transition_properties( this );
149 // Get the composite from the transition
150 mlt_transition composite = mlt_properties_get_data( properties, "composite", NULL );
152 // Look for the first filter
153 mlt_filter filter = mlt_properties_get_data( properties, "_filter_0", NULL );
155 // Get the unique id of the filter (used to reacquire the producer position)
156 char *name = mlt_properties_get( properties, "_unique_id" );
158 // Get the original producer position
159 mlt_position position = mlt_properties_get_position( mlt_frame_properties( frame ), name );
161 // Create a composite if we don't have one
162 if ( composite == NULL )
164 // Create composite via the factory
165 composite = mlt_factory_transition( "composite", NULL );
168 if ( composite != NULL )
170 // Get the properties
171 mlt_properties composite_properties = mlt_transition_properties( composite );
173 // We want to ensure that we don't get a wobble...
174 mlt_properties_set( composite_properties, "distort", "true" );
175 mlt_properties_set( composite_properties, "progressive", "1" );
177 // Pass all the composite. properties on the transition down
178 mlt_properties_pass( composite_properties, properties, "composite." );
180 // Register the composite for reuse/destruction
181 mlt_properties_set_data( properties, "composite", composite, 0, ( mlt_destructor )mlt_transition_close, NULL );
186 // Pass all current properties down
187 mlt_properties composite_properties = mlt_transition_properties( composite );
188 mlt_properties_pass( composite_properties, properties, "composite." );
192 if ( filter == NULL )
197 // Number of filters created
200 // Loop for all properties
201 for ( i = 0; i < mlt_properties_count( properties ); i ++ )
203 // Get the name of this property
204 char *name = mlt_properties_get_name( properties, i );
206 // If the name does not contain a . and matches filter
207 if ( strchr( name, '.' ) == NULL && !strncmp( name, "filter", 6 ) )
209 // Get the filter constructor
210 char *value = mlt_properties_get_value( properties, i );
212 // Create an instance
213 if ( create_instance( this, name, value, count ) == 0 )
220 // Pass all properties down
221 mlt_filter temp = NULL;
226 // Number of filters found
229 // Loop for all properties
230 for ( i = 0; i < mlt_properties_count( properties ); i ++ )
232 // Get the name of this property
233 char *name = mlt_properties_get_name( properties, i );
235 // If the name does not contain a . and matches filter
236 if ( strchr( name, '.' ) == NULL && !strncmp( name, "filter", 6 ) )
238 // Strings to hold the id and pass down key
242 // Construct id and key
243 sprintf( id, "_filter_%d", count );
244 sprintf( key, "%s.", name );
247 temp = mlt_properties_get_data( properties, id, NULL );
251 mlt_properties_pass( mlt_filter_properties( temp ), properties, key );
259 error = mlt_frame_get_image( frame, image, format, width, height, 1 );
261 // Only continue if we have both filter and composite
262 if ( composite != NULL )
264 // Get the resource of this filter (could be a shape [rectangle/circle] or an alpha provider of choice
265 char *resource = mlt_properties_get( properties, "resource" );
267 // Get the old resource in case it's changed
268 char *old_resource = mlt_properties_get( properties, "_old_resource" );
270 // String to hold the filter to query on
273 // Index to hold the count
276 // We will get the 'b frame' from the composite only if it's NULL
277 if ( b_frame == NULL )
280 b_frame = composite_copy_region( composite, frame, position );
282 // Ensure a destructor
283 mlt_properties_set_data( mlt_frame_properties( frame ), name, b_frame, 0, ( mlt_destructor )mlt_frame_close, NULL );
286 // Set the position of the b_frame
287 mlt_frame_set_position( b_frame, position );
289 // Make sure the filter is in the correct position
290 while ( filter != NULL )
293 if ( mlt_properties_get_int( mlt_filter_properties( filter ), "off" ) == 0 )
294 mlt_filter_process( filter, b_frame );
296 // Generate the key for the next
297 sprintf( id, "_filter_%d", ++ i );
299 // Get the next filter
300 filter = mlt_properties_get_data( properties, id, NULL );
303 // Allow filters to be attached to a region filter
304 filter = mlt_properties_get_data( properties, "_region_filter", NULL );
305 if ( filter != NULL )
306 mlt_service_apply_filters( mlt_filter_service( filter ), b_frame, 0 );
308 // Hmm - this is probably going to go wrong....
309 mlt_frame_set_position( frame, position );
311 // Get the b frame and process with composite if successful
312 mlt_transition_process( composite, frame, b_frame );
314 // If we have a shape producer copy the alpha mask from the shape frame to the b_frame
315 if ( strcmp( resource, "rectangle" ) != 0 )
317 // Get the producer from the transition
318 mlt_producer producer = mlt_properties_get_data( properties, "producer", NULL );
320 // If We have no producer then create one
321 if ( producer == NULL || ( old_resource != NULL && strcmp( resource, old_resource ) ) )
323 // Get the factory producer service
324 char *factory = mlt_properties_get( properties, "factory" );
326 // Store the old resource
327 mlt_properties_set( properties, "_old_resource", resource );
329 // Special case circle resource
330 if ( strcmp( resource, "circle" ) == 0 )
332 // Special case to ensure that fezzik produces a pixbuf with a NULL constructor
335 // Specify the svg circle
336 mlt_properties_set( properties, "producer.resource", "<svg width='100' height='100'><circle cx='50' cy='50' r='50' fill='black'/></svg>" );
339 // Create the producer
340 producer = mlt_factory_producer( factory, resource );
343 if ( producer != NULL )
345 // Get the producer properties
346 mlt_properties producer_properties = mlt_producer_properties( producer );
348 // Ensure that we loop
349 mlt_properties_set( producer_properties, "eof", "loop" );
351 // Now pass all producer. properties on the transition down
352 mlt_properties_pass( producer_properties, properties, "producer." );
354 // Register the producer for reuse/destruction
355 mlt_properties_set_data( properties, "producer", producer, 0, ( mlt_destructor )mlt_producer_close, NULL );
359 // Now use the shape producer
360 if ( producer != NULL )
362 // We will get the alpha frame from the producer
363 mlt_frame shape_frame = NULL;
365 // Make sure the producer is in the correct position
366 mlt_producer_seek( producer, position );
368 // Get the shape frame
369 if ( mlt_service_get_frame( mlt_producer_service( producer ), &shape_frame, 0 ) == 0 )
371 // Ensure that the shape frame will be closed
372 mlt_properties_set_data( mlt_frame_properties( b_frame ), "shape_frame", shape_frame, 0, ( mlt_destructor )mlt_frame_close, NULL );
374 // Specify the callback for evaluation
375 b_frame->get_alpha_mask = filter_get_alpha_mask;
381 error = mlt_frame_get_image( frame, image, format, width, height, 0 );
387 /** Filter processing.
390 static mlt_frame transition_process( mlt_transition this, mlt_frame a_frame, mlt_frame b_frame )
392 // Get the properties of the frame
393 mlt_properties properties = mlt_frame_properties( a_frame );
395 // Get a unique name to store the frame position
396 char *name = mlt_properties_get( mlt_transition_properties( this ), "_unique_id" );
398 // Assign the current position to the name
399 mlt_properties_set_position( properties, name, mlt_frame_get_position( a_frame ) );
401 // Push the transition on to the frame
402 mlt_frame_push_service( a_frame, this );
404 // Push the b_frame on to the stack
405 mlt_frame_push_frame( a_frame, b_frame );
407 // Push the transition method
408 mlt_frame_push_get_image( a_frame, transition_get_image );
414 /** Constructor for the transition.
417 mlt_transition transition_region_init( void *arg )
419 // Create a new transition
420 mlt_transition this = mlt_transition_new( );
422 // Further initialisation
425 // Get the properties from the transition
426 mlt_properties properties = mlt_transition_properties( this );
428 // Assign the transition process method
429 this->process = transition_process;
432 mlt_properties_set( properties, "factory", "fezzik" );
434 // Resource defines the shape of the region
435 mlt_properties_set( properties, "resource", arg == NULL ? "rectangle" : arg );
438 // Return the transition