2 * transition_region.c -- region transition
3 * Copyright (C) 2003-2004 Ushodaya Enterprises Limited
4 * Author: Charles Yates <charles.yates@pandora.be>
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2.1 of the License, or (at your option) any later version.
11 * This library 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 GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, write to the Free Software
18 * Foundation, 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 mlt_profile profile = mlt_service_profile( MLT_TRANSITION_SERVICE( this ) );
50 filter = mlt_factory_filter( profile, type, arg );
52 // If we have a filter, then initialise and store it
56 mlt_properties properties = MLT_TRANSITION_PROPERTIES( this );
58 // String to hold the property name
61 // String to hold the passdown key
65 sprintf( id, "_filter_%d", count );
68 sprintf( key, "%s.", name );
70 // Just in case, let's assume that the filter here has a composite
71 //mlt_properties_set( MLT_FILTER_PROPERTIES( filter ), "composite.geometry", "0%,0%:100%x100%" );
72 //mlt_properties_set_int( MLT_FILTER_PROPERTIES( filter ), "composite.fill", 1 );
74 // Pass all the key properties on the filter down
75 mlt_properties_pass( MLT_FILTER_PROPERTIES( filter ), properties, key );
77 // Ensure that filter is assigned
78 mlt_properties_set_data( properties, id, filter, 0, ( mlt_destructor )mlt_filter_close, NULL );
82 // Indicate that an error has occurred
89 // Return error condition
93 static uint8_t *filter_get_alpha_mask( mlt_frame this )
95 uint8_t *alpha = NULL;
97 // Obtain properties of frame
98 mlt_properties properties = MLT_FRAME_PROPERTIES( this );
100 // Get the shape frame
101 mlt_frame shape_frame = mlt_properties_get_data( properties, "shape_frame", NULL );
103 // Get the width and height of the image
104 int region_width = mlt_properties_get_int( MLT_FRAME_PROPERTIES( this ), "width" );
105 int region_height = mlt_properties_get_int( MLT_FRAME_PROPERTIES( this ), "height" );
106 uint8_t *image = NULL;
107 mlt_image_format format = mlt_image_yuv422;
109 // Get the shape image to trigger alpha creation
110 mlt_properties_set_int( MLT_FRAME_PROPERTIES( shape_frame ), "distort", 1 );
111 mlt_frame_get_image( shape_frame, &image, &format, ®ion_width, ®ion_height, 0 );
113 alpha = mlt_frame_get_alpha_mask( shape_frame );
115 // Generate from the Y component of the image if no alpha available
118 int size = region_width * region_height;
119 uint8_t *p = mlt_pool_alloc( size );
123 *p ++ = ( int )( ( ( *image ++ - 16 ) * 299 ) / 255 );
126 mlt_frame_set_alpha( this, alpha, region_width * region_height, mlt_pool_release );
130 mlt_frame_set_alpha( this, alpha, region_width * region_height, NULL );
133 this->get_alpha_mask = NULL;
141 static int transition_get_image( mlt_frame frame, uint8_t **image, mlt_image_format *format, int *width, int *height, int writable )
143 // Error we will return
146 // We will get the 'b frame' from the frame stack
147 mlt_frame b_frame = mlt_frame_pop_frame( frame );
149 // Get the watermark transition object
150 mlt_transition this = mlt_frame_pop_service( frame );
152 // Get the properties of the transition
153 mlt_properties properties = MLT_TRANSITION_PROPERTIES( this );
155 mlt_service_lock( MLT_TRANSITION_SERVICE( this ) );
157 // Get the composite from the transition
158 mlt_transition composite = mlt_properties_get_data( properties, "composite", NULL );
160 // Look for the first filter
161 mlt_filter filter = mlt_properties_get_data( properties, "_filter_0", NULL );
164 mlt_position position = mlt_transition_get_position( this, frame );
166 // Create a composite if we don't have one
167 if ( composite == NULL )
169 // Create composite via the factory
170 mlt_profile profile = mlt_service_profile( MLT_TRANSITION_SERVICE( this ) );
171 composite = mlt_factory_transition( profile, "composite", NULL );
174 if ( composite != NULL )
176 // Get the properties
177 mlt_properties composite_properties = MLT_TRANSITION_PROPERTIES( composite );
179 // We want to ensure that we don't get a wobble...
180 //mlt_properties_set_int( composite_properties, "distort", 1 );
181 mlt_properties_set_int( composite_properties, "progressive", 1 );
183 // Pass all the composite. properties on the transition down
184 mlt_properties_pass( composite_properties, properties, "composite." );
186 // Register the composite for reuse/destruction
187 mlt_properties_set_data( properties, "composite", composite, 0, ( mlt_destructor )mlt_transition_close, NULL );
192 // Pass all current properties down
193 mlt_properties composite_properties = MLT_TRANSITION_PROPERTIES( composite );
194 mlt_properties_pass( composite_properties, properties, "composite." );
198 if ( filter == NULL )
203 // Number of filters created
206 // Loop for all properties
207 for ( i = 0; i < mlt_properties_count( properties ); i ++ )
209 // Get the name of this property
210 char *name = mlt_properties_get_name( properties, i );
212 // If the name does not contain a . and matches filter
213 if ( strchr( name, '.' ) == NULL && !strncmp( name, "filter", 6 ) )
215 // Get the filter constructor
216 char *value = mlt_properties_get_value( properties, i );
218 // Create an instance
219 if ( create_instance( this, name, value, count ) == 0 )
224 // Look for the first filter again
225 filter = mlt_properties_get_data( properties, "_filter_0", NULL );
229 // Pass all properties down
230 mlt_filter temp = NULL;
235 // Number of filters found
238 // Loop for all properties
239 for ( i = 0; i < mlt_properties_count( properties ); i ++ )
241 // Get the name of this property
242 char *name = mlt_properties_get_name( properties, i );
244 // If the name does not contain a . and matches filter
245 if ( strchr( name, '.' ) == NULL && !strncmp( name, "filter", 6 ) )
247 // Strings to hold the id and pass down key
251 // Construct id and key
252 sprintf( id, "_filter_%d", count );
253 sprintf( key, "%s.", name );
256 temp = mlt_properties_get_data( properties, id, NULL );
260 mlt_properties_pass( MLT_FILTER_PROPERTIES( temp ), properties, key );
267 mlt_properties_set_int( MLT_FRAME_PROPERTIES( frame ), "width", *width );
268 mlt_properties_set_int( MLT_FRAME_PROPERTIES( frame ), "height", *height );
270 // Only continue if we have both filter and composite
271 if ( composite != NULL )
273 // Get the resource of this filter (could be a shape [rectangle/circle] or an alpha provider of choice
274 const char *resource = mlt_properties_get( properties, "resource" );
276 // Get the old resource in case it's changed
277 char *old_resource = mlt_properties_get( properties, "_old_resource" );
279 // String to hold the filter to query on
282 // Index to hold the count
285 // We will get the 'b frame' from the composite only if it's NULL
286 if ( b_frame == NULL )
289 b_frame = composite_copy_region( composite, frame, position );
291 // Ensure a destructor
292 char *name = mlt_properties_get( properties, "_unique_id" );
293 mlt_properties_set_data( MLT_FRAME_PROPERTIES( frame ), name, b_frame, 0, ( mlt_destructor )mlt_frame_close, NULL );
296 // Make sure the filter is in the correct position
297 while ( filter != NULL )
300 if ( mlt_properties_get_int( MLT_FILTER_PROPERTIES( filter ), "off" ) == 0 )
301 mlt_filter_process( filter, b_frame );
303 // Generate the key for the next
304 sprintf( id, "_filter_%d", ++ i );
306 // Get the next filter
307 filter = mlt_properties_get_data( properties, id, NULL );
310 // Allow filters to be attached to a region filter
311 filter = mlt_properties_get_data( properties, "_region_filter", NULL );
312 if ( filter != NULL )
313 mlt_service_apply_filters( MLT_FILTER_SERVICE( filter ), b_frame, 0 );
315 // Hmm - this is probably going to go wrong....
316 mlt_frame_set_position( frame, position );
318 // Get the b frame and process with composite if successful
319 mlt_transition_process( composite, frame, b_frame );
321 // If we have a shape producer copy the alpha mask from the shape frame to the b_frame
322 if ( strcmp( resource, "rectangle" ) != 0 )
324 // Get the producer from the transition
325 mlt_producer producer = mlt_properties_get_data( properties, "producer", NULL );
327 // If We have no producer then create one
328 if ( producer == NULL || ( old_resource != NULL && strcmp( resource, old_resource ) ) )
330 // Get the factory producer service
331 char *factory = mlt_properties_get( properties, "factory" );
333 // Store the old resource
334 mlt_properties_set( properties, "_old_resource", resource );
336 // Special case circle resource
337 if ( strcmp( resource, "circle" ) == 0 )
338 resource = "pixbuf:<svg width='100' height='100'><circle cx='50' cy='50' r='50' fill='black'/></svg>";
340 // Create the producer
341 mlt_profile profile = mlt_service_profile( MLT_TRANSITION_SERVICE( this ) );
342 producer = mlt_factory_producer( profile, factory, resource );
345 if ( producer != NULL )
347 // Get the producer properties
348 mlt_properties producer_properties = MLT_PRODUCER_PROPERTIES( producer );
350 // Ensure that we loop
351 mlt_properties_set( producer_properties, "eof", "loop" );
353 // Now pass all producer. properties on the transition down
354 mlt_properties_pass( producer_properties, properties, "producer." );
356 // Register the producer for reuse/destruction
357 mlt_properties_set_data( properties, "producer", producer, 0, ( mlt_destructor )mlt_producer_close, NULL );
361 // Now use the shape producer
362 if ( producer != NULL )
364 // We will get the alpha frame from the producer
365 mlt_frame shape_frame = NULL;
367 // Make sure the producer is in the correct position
368 mlt_producer_seek( producer, position );
370 // Get the shape frame
371 if ( mlt_service_get_frame( MLT_PRODUCER_SERVICE( producer ), &shape_frame, 0 ) == 0 )
373 // Ensure that the shape frame will be closed
374 mlt_properties_set_data( MLT_FRAME_PROPERTIES( b_frame ), "shape_frame", shape_frame, 0, ( mlt_destructor )mlt_frame_close, NULL );
376 // Specify the callback for evaluation
377 b_frame->get_alpha_mask = filter_get_alpha_mask;
383 error = mlt_frame_get_image( frame, image, format, width, height, 0 );
386 mlt_service_unlock( MLT_TRANSITION_SERVICE( this ) );
391 /** Filter processing.
394 static mlt_frame transition_process( mlt_transition this, mlt_frame a_frame, mlt_frame b_frame )
396 // Push the transition on to the frame
397 mlt_frame_push_service( a_frame, this );
399 // Push the b_frame on to the stack
400 mlt_frame_push_frame( a_frame, b_frame );
402 // Push the transition method
403 mlt_frame_push_get_image( a_frame, transition_get_image );
409 /** Constructor for the transition.
412 mlt_transition transition_region_init( mlt_profile profile, mlt_service_type type, const char *id, char *arg )
414 // Create a new transition
415 mlt_transition this = mlt_transition_new( );
417 // Further initialisation
420 // Get the properties from the transition
421 mlt_properties properties = MLT_TRANSITION_PROPERTIES( this );
423 // Assign the transition process method
424 this->process = transition_process;
427 mlt_properties_set( properties, "factory", mlt_environment( "MLT_PRODUCER" ) );
429 // Resource defines the shape of the region
430 mlt_properties_set( properties, "resource", arg == NULL ? "rectangle" : arg );
432 // Inform apps and framework that this is a video only transition
433 mlt_properties_set_int( properties, "_transition_type", 1 );
436 // Return the transition