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.geometry", "0%,0%:100%x100%" );
71 //mlt_properties_set_int( MLT_FILTER_PROPERTIES( filter ), "composite.fill", 1 );
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_int( MLT_FRAME_PROPERTIES( shape_frame ), "distort", 1 );
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 );
122 *p ++ = ( int )( ( ( *image ++ - 16 ) * 299 ) / 255 );
125 mlt_properties_set_data( MLT_FRAME_PROPERTIES( this ), "alpha", alpha, region_width * region_height, mlt_pool_release, NULL );
129 mlt_properties_set_data( MLT_FRAME_PROPERTIES( this ), "alpha", alpha, region_width * region_height, NULL, NULL );
132 this->get_alpha_mask = NULL;
140 static int transition_get_image( mlt_frame frame, uint8_t **image, mlt_image_format *format, int *width, int *height, int writable )
142 // Error we will return
145 // We will get the 'b frame' from the frame stack
146 mlt_frame b_frame = mlt_frame_pop_frame( frame );
148 // Get the watermark transition object
149 mlt_transition this = mlt_frame_pop_service( frame );
151 // Get the properties of the transition
152 mlt_properties properties = MLT_TRANSITION_PROPERTIES( this );
154 // Get the composite from the transition
155 mlt_transition composite = mlt_properties_get_data( properties, "composite", NULL );
157 // Look for the first filter
158 mlt_filter filter = mlt_properties_get_data( properties, "_filter_0", NULL );
160 // Get the unique id of the filter (used to reacquire the producer position)
161 char *name = mlt_properties_get( properties, "_unique_id" );
163 // Get the original producer position
164 mlt_position position = mlt_properties_get_position( MLT_FRAME_PROPERTIES( frame ), name );
166 // Create a composite if we don't have one
167 if ( composite == NULL )
169 // Create composite via the factory
170 composite = mlt_factory_transition( "composite", NULL );
173 if ( composite != NULL )
175 // Get the properties
176 mlt_properties composite_properties = MLT_TRANSITION_PROPERTIES( composite );
178 // We want to ensure that we don't get a wobble...
179 //mlt_properties_set_int( composite_properties, "distort", 1 );
180 mlt_properties_set_int( composite_properties, "progressive", 1 );
182 // Pass all the composite. properties on the transition down
183 mlt_properties_pass( composite_properties, properties, "composite." );
185 // Register the composite for reuse/destruction
186 mlt_properties_set_data( properties, "composite", composite, 0, ( mlt_destructor )mlt_transition_close, NULL );
191 // Pass all current properties down
192 mlt_properties composite_properties = MLT_TRANSITION_PROPERTIES( composite );
193 mlt_properties_pass( composite_properties, properties, "composite." );
197 if ( filter == NULL )
202 // Number of filters created
205 // Loop for all properties
206 for ( i = 0; i < mlt_properties_count( properties ); i ++ )
208 // Get the name of this property
209 char *name = mlt_properties_get_name( properties, i );
211 // If the name does not contain a . and matches filter
212 if ( strchr( name, '.' ) == NULL && !strncmp( name, "filter", 6 ) )
214 // Get the filter constructor
215 char *value = mlt_properties_get_value( properties, i );
217 // Create an instance
218 if ( create_instance( this, name, value, count ) == 0 )
223 // Look for the first filter again
224 filter = mlt_properties_get_data( properties, "_filter_0", NULL );
228 // Pass all properties down
229 mlt_filter temp = NULL;
234 // Number of filters found
237 // Loop for all properties
238 for ( i = 0; i < mlt_properties_count( properties ); i ++ )
240 // Get the name of this property
241 char *name = mlt_properties_get_name( properties, i );
243 // If the name does not contain a . and matches filter
244 if ( strchr( name, '.' ) == NULL && !strncmp( name, "filter", 6 ) )
246 // Strings to hold the id and pass down key
250 // Construct id and key
251 sprintf( id, "_filter_%d", count );
252 sprintf( key, "%s.", name );
255 temp = mlt_properties_get_data( properties, id, NULL );
259 mlt_properties_pass( MLT_FILTER_PROPERTIES( temp ), properties, key );
267 error = mlt_frame_get_image( frame, image, format, width, height, 1 );
269 // Only continue if we have both filter and composite
270 if ( composite != NULL )
272 // Get the resource of this filter (could be a shape [rectangle/circle] or an alpha provider of choice
273 char *resource = mlt_properties_get( properties, "resource" );
275 // Get the old resource in case it's changed
276 char *old_resource = mlt_properties_get( properties, "_old_resource" );
278 // String to hold the filter to query on
281 // Index to hold the count
284 // We will get the 'b frame' from the composite only if it's NULL
285 if ( b_frame == NULL )
288 b_frame = composite_copy_region( composite, frame, position );
290 // Ensure a destructor
291 mlt_properties_set_data( MLT_FRAME_PROPERTIES( frame ), name, b_frame, 0, ( mlt_destructor )mlt_frame_close, NULL );
294 // Set the position of the b_frame
295 mlt_frame_set_position( b_frame, position );
297 // Make sure the filter is in the correct position
298 while ( filter != NULL )
301 if ( mlt_properties_get_int( MLT_FILTER_PROPERTIES( filter ), "off" ) == 0 )
302 mlt_filter_process( filter, b_frame );
304 // Generate the key for the next
305 sprintf( id, "_filter_%d", ++ i );
307 // Get the next filter
308 filter = mlt_properties_get_data( properties, id, NULL );
311 // Allow filters to be attached to a region filter
312 filter = mlt_properties_get_data( properties, "_region_filter", NULL );
313 if ( filter != NULL )
314 mlt_service_apply_filters( MLT_FILTER_SERVICE( filter ), b_frame, 0 );
316 // Hmm - this is probably going to go wrong....
317 mlt_frame_set_position( frame, position );
319 // Get the b frame and process with composite if successful
320 mlt_transition_process( composite, frame, b_frame );
322 // If we have a shape producer copy the alpha mask from the shape frame to the b_frame
323 if ( strcmp( resource, "rectangle" ) != 0 )
325 // Get the producer from the transition
326 mlt_producer producer = mlt_properties_get_data( properties, "producer", NULL );
328 // If We have no producer then create one
329 if ( producer == NULL || ( old_resource != NULL && strcmp( resource, old_resource ) ) )
331 // Get the factory producer service
332 char *factory = mlt_properties_get( properties, "factory" );
334 // Store the old resource
335 mlt_properties_set( properties, "_old_resource", resource );
337 // Special case circle resource
338 if ( strcmp( resource, "circle" ) == 0 )
340 // Special case to ensure that fezzik produces a pixbuf with a NULL constructor
343 // Specify the svg circle
344 mlt_properties_set( properties, "producer.resource", "<svg width='100' height='100'><circle cx='50' cy='50' r='50' fill='black'/></svg>" );
347 // Create the producer
348 producer = mlt_factory_producer( factory, resource );
351 if ( producer != NULL )
353 // Get the producer properties
354 mlt_properties producer_properties = MLT_PRODUCER_PROPERTIES( producer );
356 // Ensure that we loop
357 mlt_properties_set( producer_properties, "eof", "loop" );
359 // Now pass all producer. properties on the transition down
360 mlt_properties_pass( producer_properties, properties, "producer." );
362 // Register the producer for reuse/destruction
363 mlt_properties_set_data( properties, "producer", producer, 0, ( mlt_destructor )mlt_producer_close, NULL );
367 // Now use the shape producer
368 if ( producer != NULL )
370 // We will get the alpha frame from the producer
371 mlt_frame shape_frame = NULL;
373 // Make sure the producer is in the correct position
374 mlt_producer_seek( producer, position );
376 // Get the shape frame
377 if ( mlt_service_get_frame( MLT_PRODUCER_SERVICE( producer ), &shape_frame, 0 ) == 0 )
379 // Ensure that the shape frame will be closed
380 mlt_properties_set_data( MLT_FRAME_PROPERTIES( b_frame ), "shape_frame", shape_frame, 0, ( mlt_destructor )mlt_frame_close, NULL );
382 // Specify the callback for evaluation
383 b_frame->get_alpha_mask = filter_get_alpha_mask;
389 error = mlt_frame_get_image( frame, image, format, width, height, 0 );
395 /** Filter processing.
398 static mlt_frame transition_process( mlt_transition this, mlt_frame a_frame, mlt_frame b_frame )
400 // Get the properties of the frame
401 mlt_properties properties = MLT_FRAME_PROPERTIES( a_frame );
403 // Get a unique name to store the frame position
404 char *name = mlt_properties_get( MLT_TRANSITION_PROPERTIES( this ), "_unique_id" );
406 // Assign the current position to the name
407 mlt_properties_set_position( properties, name, mlt_frame_get_position( a_frame ) );
409 // Push the transition on to the frame
410 mlt_frame_push_service( a_frame, this );
412 // Push the b_frame on to the stack
413 mlt_frame_push_frame( a_frame, b_frame );
415 // Push the transition method
416 mlt_frame_push_get_image( a_frame, transition_get_image );
422 /** Constructor for the transition.
425 mlt_transition transition_region_init( void *arg )
427 // Create a new transition
428 mlt_transition this = mlt_transition_new( );
430 // Further initialisation
433 // Get the properties from the transition
434 mlt_properties properties = MLT_TRANSITION_PROPERTIES( this );
436 // Assign the transition process method
437 this->process = transition_process;
440 mlt_properties_set( properties, "factory", "fezzik" );
442 // Resource defines the shape of the region
443 mlt_properties_set( properties, "resource", arg == NULL ? "rectangle" : arg );
445 // Inform apps and framework that this is a video only transition
446 mlt_properties_set_int( properties, "_transition_type", 1 );
449 // Return the transition