]> git.sesse.net Git - mlt/blob - src/modules/core/transition_region.c
c93eec89f028ae0ff1d3a7dbd366e755ba391ae4
[mlt] / src / modules / core / transition_region.c
1 /*
2  * transition_region.c -- region transition
3  * Copyright (C) 2003-2004 Ushodaya Enterprises Limited
4  * Author: Charles Yates <charles.yates@pandora.be>
5  *
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.
10  *
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.
15  *
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.
19  */
20
21 #include "transition_region.h"
22 #include "transition_composite.h"
23
24 #include <framework/mlt.h>
25
26 #include <stdio.h>
27 #include <stdlib.h>
28 #include <string.h>
29
30 static int create_instance( mlt_transition this, char *name, char *value, int count )
31 {
32         // Return from this function
33         int error = 0;
34
35         // Duplicate the value
36         char *type = strdup( value );
37
38         // Pointer to filter argument
39         char *arg = type == NULL ? NULL : strchr( type, ':' );
40
41         // New filter being created
42         mlt_filter filter = NULL;
43
44         // Cleanup type and arg
45         if ( arg != NULL )
46                 *arg ++ = '\0';
47
48         // Create the filter
49         filter = mlt_factory_filter( type, arg );
50
51         // If we have a filter, then initialise and store it
52         if ( filter != NULL )
53         {
54                 // Properties of this
55                 mlt_properties properties = mlt_transition_properties( this );
56
57                 // String to hold the property name
58                 char id[ 256 ];
59
60                 // String to hold the passdown key
61                 char key[ 256 ];
62
63                 // Construct id
64                 sprintf( id, "_filter_%d", count );
65
66                 // Counstruct key
67                 sprintf( key, "%s.", name );
68
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" );
72
73                 // Pass all the key properties on the filter down
74                 mlt_properties_pass( mlt_filter_properties( filter ), properties, key );
75
76                 // Ensure that filter is assigned
77                 mlt_properties_set_data( properties, id, filter, 0, ( mlt_destructor )mlt_filter_close, NULL );
78         }
79         else
80         {
81                 // Indicate that an error has occurred
82                 error = 1;
83         }
84
85         // Cleanup
86         free( type );
87
88         // Return error condition
89         return error;
90 }
91
92 static uint8_t *filter_get_alpha_mask( mlt_frame this )
93 {
94         uint8_t *alpha = NULL;
95
96         // Obtain properties of frame
97         mlt_properties properties = mlt_frame_properties( this );
98
99         // Get the shape frame
100         mlt_frame shape_frame = mlt_properties_get_data( properties, "shape_frame", NULL );
101
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;
107                                         
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, &region_width, &region_height, 0 );
111
112         alpha = mlt_frame_get_alpha_mask( shape_frame );
113
114         // Generate from the Y component of the image if no alpha available
115         if ( alpha == NULL )
116         {
117                 int size = region_width * region_height;
118                 uint8_t *p = mlt_pool_alloc( size );
119                 alpha = p;
120                 while ( size -- )
121                 {
122                         *p ++ = *image ++;
123                         image ++;
124                 }
125                 mlt_properties_set_data( mlt_frame_properties( shape_frame ), "alpha", alpha, 
126                                                                  region_width * region_height, mlt_pool_release, NULL );
127         }
128
129         return alpha;
130 }
131
132 /** Do it :-).
133 */
134
135 static int transition_get_image( mlt_frame frame, uint8_t **image, mlt_image_format *format, int *width, int *height, int writable )
136 {
137         // Error we will return
138         int error = 0;
139
140         // We will get the 'b frame' from the frame stack
141         mlt_frame b_frame = mlt_frame_pop_frame( frame );
142
143         // Get the watermark transition object
144         mlt_transition this = mlt_frame_pop_service( frame );
145
146         // Get the properties of the transition
147         mlt_properties properties = mlt_transition_properties( this );
148
149         // Get the composite from the transition
150         mlt_transition composite = mlt_properties_get_data( properties, "composite", NULL );
151
152         // Look for the first filter
153         mlt_filter filter = mlt_properties_get_data( properties, "_filter_0", NULL );
154
155         // Get the unique id of the filter (used to reacquire the producer position)
156         char *name = mlt_properties_get( properties, "_unique_id" );
157
158         // Get the original producer position
159         mlt_position position = mlt_properties_get_position( mlt_frame_properties( frame ), name );
160
161         // Create a composite if we don't have one
162         if ( composite == NULL )
163         {
164                 // Create composite via the factory
165                 composite = mlt_factory_transition( "composite", NULL );
166
167                 // If we have one
168                 if ( composite != NULL )
169                 {
170                         // Get the properties
171                         mlt_properties composite_properties = mlt_transition_properties( composite );
172
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" );
176
177                         // Pass all the composite. properties on the transition down
178                         mlt_properties_pass( composite_properties, properties, "composite." );
179
180                         // Register the composite for reuse/destruction
181                         mlt_properties_set_data( properties, "composite", composite, 0, ( mlt_destructor )mlt_transition_close, NULL );
182                 }
183         }
184         else
185         {
186                 // Pass all current properties down
187                 mlt_properties composite_properties = mlt_transition_properties( composite );
188                 mlt_properties_pass( composite_properties, properties, "composite." );
189         }
190
191         // Create filters
192         if ( filter == NULL )
193         {
194                 // Loop Variable
195                 int i = 0;
196
197                 // Number of filters created
198                 int count = 0;
199
200                 // Loop for all properties
201                 for ( i = 0; i < mlt_properties_count( properties ); i ++ )
202                 {
203                         // Get the name of this property
204                         char *name = mlt_properties_get_name( properties, i );
205
206                         // If the name does not contain a . and matches filter
207                         if ( strchr( name, '.' ) == NULL && !strncmp( name, "filter", 6 ) )
208                         {
209                                 // Get the filter constructor
210                                 char *value = mlt_properties_get_value( properties, i );
211
212                                 // Create an instance
213                                 if ( create_instance( this, name, value, count ) == 0 )
214                                         count ++;
215                         }
216                 }
217         }
218         else
219         {
220                 // Pass all properties down
221                 mlt_filter temp = NULL;
222
223                 // Loop Variable
224                 int i = 0;
225
226                 // Number of filters found
227                 int count = 0;
228
229                 // Loop for all properties
230                 for ( i = 0; i < mlt_properties_count( properties ); i ++ )
231                 {
232                         // Get the name of this property
233                         char *name = mlt_properties_get_name( properties, i );
234
235                         // If the name does not contain a . and matches filter
236                         if ( strchr( name, '.' ) == NULL && !strncmp( name, "filter", 6 ) )
237                         {
238                                 // Strings to hold the id and pass down key
239                                 char id[ 256 ];
240                                 char key[ 256 ];
241
242                                 // Construct id and key
243                                 sprintf( id, "_filter_%d", count );
244                                 sprintf( key, "%s.", name );
245
246                                 // Get the filter
247                                 temp = mlt_properties_get_data( properties, id, NULL );
248
249                                 if ( temp != NULL )
250                                 {
251                                         mlt_properties_pass( mlt_filter_properties( temp ), properties, key );
252                                         count ++;
253                                 }
254                         }
255                 }
256         }
257
258         // Get the image
259         error = mlt_frame_get_image( frame, image, format, width, height, 1 );
260
261         // Only continue if we have both filter and composite
262         if ( composite != NULL )
263         {
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" );
266
267                 // Get the old resource in case it's changed
268                 char *old_resource =  mlt_properties_get( properties, "_old_resource" );
269
270                 // String to hold the filter to query on
271                 char id[ 256 ];
272
273                 // Index to hold the count
274                 int i = 0;
275
276                 // We will get the 'b frame' from the composite only if it's NULL
277                 if ( b_frame == NULL )
278                 {
279                         // Copy the region
280                         b_frame = composite_copy_region( composite, frame, position );
281
282                         // Ensure a destructor
283                         mlt_properties_set_data( mlt_frame_properties( frame ), name, b_frame, 0, ( mlt_destructor )mlt_frame_close, NULL );
284                 }
285
286                 // Set the position of the b_frame
287                 mlt_frame_set_position( b_frame, position );
288
289                 // Make sure the filter is in the correct position
290                 while ( filter != NULL )
291                 {
292                         // Stack this filter
293                         if ( mlt_properties_get_int( mlt_filter_properties( filter ), "off" ) == 0 )
294                                 mlt_filter_process( filter, b_frame );
295
296                         // Generate the key for the next
297                         sprintf( id, "_filter_%d", ++ i );
298
299                         // Get the next filter
300                         filter = mlt_properties_get_data( properties, id, NULL );
301                 }
302
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 );
307
308                 // Hmm - this is probably going to go wrong....
309                 mlt_frame_set_position( frame, position );
310
311                 // Get the b frame and process with composite if successful
312                 mlt_transition_process( composite, frame, b_frame );
313
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 )
316                 {
317                         // Get the producer from the transition
318                         mlt_producer producer = mlt_properties_get_data( properties, "producer", NULL );
319
320                         // If We have no producer then create one
321                         if ( producer == NULL || ( old_resource != NULL && strcmp( resource, old_resource ) ) )
322                         {
323                                 // Get the factory producer service
324                                 char *factory = mlt_properties_get( properties, "factory" );
325
326                                 // Store the old resource
327                                 mlt_properties_set( properties, "_old_resource", resource );
328
329                                 // Special case circle resource
330                                 if ( strcmp( resource, "circle" ) == 0 )
331                                 {
332                                         // Special case to ensure that fezzik produces a pixbuf with a NULL constructor
333                                         resource = "pixbuf";
334
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>" );
337                                 }
338
339                                 // Create the producer 
340                                 producer = mlt_factory_producer( factory, resource );
341
342                                 // If we have one
343                                 if ( producer != NULL )
344                                 {
345                                         // Get the producer properties
346                                         mlt_properties producer_properties = mlt_producer_properties( producer );
347
348                                         // Ensure that we loop
349                                         mlt_properties_set( producer_properties, "eof", "loop" );
350
351                                         // Now pass all producer. properties on the transition down
352                                         mlt_properties_pass( producer_properties, properties, "producer." );
353
354                                         // Register the producer for reuse/destruction
355                                         mlt_properties_set_data( properties, "producer", producer, 0, ( mlt_destructor )mlt_producer_close, NULL );
356                                 }
357                         }
358
359                         // Now use the shape producer
360                         if ( producer != NULL )
361                         {
362                                 // We will get the alpha frame from the producer
363                                 mlt_frame shape_frame = NULL;
364
365                                 // Make sure the producer is in the correct position
366                                 mlt_producer_seek( producer, position );
367
368                                 // Get the shape frame
369                                 if ( mlt_service_get_frame( mlt_producer_service( producer ), &shape_frame, 0 ) == 0 )
370                                 {
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 );
373
374                                         // Specify the callback for evaluation
375                                         b_frame->get_alpha_mask = filter_get_alpha_mask;
376                                 }
377                         }
378                 }
379
380                 // Get the image
381                 error = mlt_frame_get_image( frame, image, format, width, height, 0 );
382         }
383
384         return error;
385 }
386
387 /** Filter processing.
388 */
389
390 static mlt_frame transition_process( mlt_transition this, mlt_frame a_frame, mlt_frame b_frame )
391 {
392         // Get the properties of the frame
393         mlt_properties properties = mlt_frame_properties( a_frame );
394
395         // Get a unique name to store the frame position
396         char *name = mlt_properties_get( mlt_transition_properties( this ), "_unique_id" );
397
398         // Assign the current position to the name
399         mlt_properties_set_position( properties, name, mlt_frame_get_position( a_frame ) );
400
401         // Push the transition on to the frame
402         mlt_frame_push_service( a_frame, this );
403
404         // Push the b_frame on to the stack
405         mlt_frame_push_frame( a_frame, b_frame );
406
407         // Push the transition method
408         mlt_frame_push_get_image( a_frame, transition_get_image );
409
410         // Return the frame
411         return a_frame;
412 }
413
414 /** Constructor for the transition.
415 */
416
417 mlt_transition transition_region_init( void *arg )
418 {
419         // Create a new transition
420         mlt_transition this = mlt_transition_new( );
421
422         // Further initialisation
423         if ( this != NULL )
424         {
425                 // Get the properties from the transition
426                 mlt_properties properties = mlt_transition_properties( this );
427
428                 // Assign the transition process method
429                 this->process = transition_process;
430
431                 // Default factory
432                 mlt_properties_set( properties, "factory", "fezzik" );
433                 
434                 // Resource defines the shape of the region
435                 mlt_properties_set( properties, "resource", arg == NULL ? "rectangle" : arg );
436         }
437
438         // Return the transition
439         return this;
440 }
441