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 )
218 // Look for the first filter again
219 filter = mlt_properties_get_data( properties, "_filter_0", NULL );
223 // Pass all properties down
224 mlt_filter temp = NULL;
229 // Number of filters found
232 // Loop for all properties
233 for ( i = 0; i < mlt_properties_count( properties ); i ++ )
235 // Get the name of this property
236 char *name = mlt_properties_get_name( properties, i );
238 // If the name does not contain a . and matches filter
239 if ( strchr( name, '.' ) == NULL && !strncmp( name, "filter", 6 ) )
241 // Strings to hold the id and pass down key
245 // Construct id and key
246 sprintf( id, "_filter_%d", count );
247 sprintf( key, "%s.", name );
250 temp = mlt_properties_get_data( properties, id, NULL );
254 mlt_properties_pass( MLT_FILTER_PROPERTIES( temp ), properties, key );
262 error = mlt_frame_get_image( frame, image, format, width, height, 1 );
264 // Only continue if we have both filter and composite
265 if ( composite != NULL )
267 // Get the resource of this filter (could be a shape [rectangle/circle] or an alpha provider of choice
268 char *resource = mlt_properties_get( properties, "resource" );
270 // Get the old resource in case it's changed
271 char *old_resource = mlt_properties_get( properties, "_old_resource" );
273 // String to hold the filter to query on
276 // Index to hold the count
279 // We will get the 'b frame' from the composite only if it's NULL
280 if ( b_frame == NULL )
283 b_frame = composite_copy_region( composite, frame, position );
285 // Ensure a destructor
286 mlt_properties_set_data( MLT_FRAME_PROPERTIES( frame ), name, b_frame, 0, ( mlt_destructor )mlt_frame_close, NULL );
289 // Set the position of the b_frame
290 mlt_frame_set_position( b_frame, position );
292 // Make sure the filter is in the correct position
293 while ( filter != NULL )
296 if ( mlt_properties_get_int( MLT_FILTER_PROPERTIES( filter ), "off" ) == 0 )
297 mlt_filter_process( filter, b_frame );
299 // Generate the key for the next
300 sprintf( id, "_filter_%d", ++ i );
302 // Get the next filter
303 filter = mlt_properties_get_data( properties, id, NULL );
306 // Allow filters to be attached to a region filter
307 filter = mlt_properties_get_data( properties, "_region_filter", NULL );
308 if ( filter != NULL )
309 mlt_service_apply_filters( MLT_FILTER_SERVICE( filter ), b_frame, 0 );
311 // Hmm - this is probably going to go wrong....
312 mlt_frame_set_position( frame, position );
314 // Get the b frame and process with composite if successful
315 mlt_transition_process( composite, frame, b_frame );
317 // If we have a shape producer copy the alpha mask from the shape frame to the b_frame
318 if ( strcmp( resource, "rectangle" ) != 0 )
320 // Get the producer from the transition
321 mlt_producer producer = mlt_properties_get_data( properties, "producer", NULL );
323 // If We have no producer then create one
324 if ( producer == NULL || ( old_resource != NULL && strcmp( resource, old_resource ) ) )
326 // Get the factory producer service
327 char *factory = mlt_properties_get( properties, "factory" );
329 // Store the old resource
330 mlt_properties_set( properties, "_old_resource", resource );
332 // Special case circle resource
333 if ( strcmp( resource, "circle" ) == 0 )
335 // Special case to ensure that fezzik produces a pixbuf with a NULL constructor
338 // Specify the svg circle
339 mlt_properties_set( properties, "producer.resource", "<svg width='100' height='100'><circle cx='50' cy='50' r='50' fill='black'/></svg>" );
342 // Create the producer
343 producer = mlt_factory_producer( factory, resource );
346 if ( producer != NULL )
348 // Get the producer properties
349 mlt_properties producer_properties = MLT_PRODUCER_PROPERTIES( producer );
351 // Ensure that we loop
352 mlt_properties_set( producer_properties, "eof", "loop" );
354 // Now pass all producer. properties on the transition down
355 mlt_properties_pass( producer_properties, properties, "producer." );
357 // Register the producer for reuse/destruction
358 mlt_properties_set_data( properties, "producer", producer, 0, ( mlt_destructor )mlt_producer_close, NULL );
362 // Now use the shape producer
363 if ( producer != NULL )
365 // We will get the alpha frame from the producer
366 mlt_frame shape_frame = NULL;
368 // Make sure the producer is in the correct position
369 mlt_producer_seek( producer, position );
371 // Get the shape frame
372 if ( mlt_service_get_frame( MLT_PRODUCER_SERVICE( producer ), &shape_frame, 0 ) == 0 )
374 // Ensure that the shape frame will be closed
375 mlt_properties_set_data( MLT_FRAME_PROPERTIES( b_frame ), "shape_frame", shape_frame, 0, ( mlt_destructor )mlt_frame_close, NULL );
377 // Specify the callback for evaluation
378 b_frame->get_alpha_mask = filter_get_alpha_mask;
384 error = mlt_frame_get_image( frame, image, format, width, height, 0 );
390 /** Filter processing.
393 static mlt_frame transition_process( mlt_transition this, mlt_frame a_frame, mlt_frame b_frame )
395 // Get the properties of the frame
396 mlt_properties properties = MLT_FRAME_PROPERTIES( a_frame );
398 // Get a unique name to store the frame position
399 char *name = mlt_properties_get( MLT_TRANSITION_PROPERTIES( this ), "_unique_id" );
401 // Assign the current position to the name
402 mlt_properties_set_position( properties, name, mlt_frame_get_position( a_frame ) );
404 // Push the transition on to the frame
405 mlt_frame_push_service( a_frame, this );
407 // Push the b_frame on to the stack
408 mlt_frame_push_frame( a_frame, b_frame );
410 // Push the transition method
411 mlt_frame_push_get_image( a_frame, transition_get_image );
417 /** Constructor for the transition.
420 mlt_transition transition_region_init( void *arg )
422 // Create a new transition
423 mlt_transition this = mlt_transition_new( );
425 // Further initialisation
428 // Get the properties from the transition
429 mlt_properties properties = MLT_TRANSITION_PROPERTIES( this );
431 // Assign the transition process method
432 this->process = transition_process;
435 mlt_properties_set( properties, "factory", "fezzik" );
437 // Resource defines the shape of the region
438 mlt_properties_set( properties, "resource", arg == NULL ? "rectangle" : arg );
441 // Return the transition