]> git.sesse.net Git - mlt/blob - src/modules/core/transition_region.c
d24def91c65ab4ade47948e4fdeb25d4296d1a0b
[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         // Obtain properties of frame
95         mlt_properties properties = mlt_frame_properties( this );
96
97         // Get the shape frame
98         mlt_frame shape_frame = mlt_properties_get_data( properties, "shape_frame", NULL );
99
100         // Get the width and height of the image
101         int region_width = mlt_properties_get_int( mlt_frame_properties( this ), "width" );
102         int region_height = mlt_properties_get_int( mlt_frame_properties( this ), "height" );
103         uint8_t *image = NULL;
104         mlt_image_format format = mlt_image_yuv422;
105                                         
106         // Get the shape image to trigger alpha creation
107         mlt_properties_set( mlt_frame_properties( shape_frame ), "distort", "true" );
108         mlt_frame_get_image( shape_frame, &image, &format, &region_width, &region_height, 0 );
109
110         return mlt_frame_get_alpha_mask( shape_frame );
111 }
112
113 /** Do it :-).
114 */
115
116 static int transition_get_image( mlt_frame frame, uint8_t **image, mlt_image_format *format, int *width, int *height, int writable )
117 {
118         // Error we will return
119         int error = 0;
120
121         // We will get the 'b frame' from the frame stack
122         mlt_frame b_frame = mlt_frame_pop_frame( frame );
123
124         // Get the watermark transition object
125         mlt_transition this = mlt_frame_pop_service( frame );
126
127         // Get the properties of the transition
128         mlt_properties properties = mlt_transition_properties( this );
129
130         // Get the composite from the transition
131         mlt_transition composite = mlt_properties_get_data( properties, "composite", NULL );
132
133         // Look for the first filter
134         mlt_filter filter = mlt_properties_get_data( properties, "_filter_0", NULL );
135
136         // Get the unique id of the filter (used to reacquire the producer position)
137         char *name = mlt_properties_get( properties, "_unique_id" );
138
139         // Get the original producer position
140         mlt_position position = mlt_properties_get_position( mlt_frame_properties( frame ), name );
141
142         // Create a composite if we don't have one
143         if ( composite == NULL )
144         {
145                 // Create composite via the factory
146                 composite = mlt_factory_transition( "composite", NULL );
147
148                 // If we have one
149                 if ( composite != NULL )
150                 {
151                         // Get the properties
152                         mlt_properties composite_properties = mlt_transition_properties( composite );
153
154                         // We want to ensure that we don't get a wobble...
155                         mlt_properties_set( composite_properties, "distort", "true" );
156                         mlt_properties_set( composite_properties, "progressive", "1" );
157
158                         // Pass all the composite. properties on the transition down
159                         mlt_properties_pass( composite_properties, properties, "composite." );
160
161                         // Register the composite for reuse/destruction
162                         mlt_properties_set_data( properties, "composite", composite, 0, ( mlt_destructor )mlt_transition_close, NULL );
163                 }
164         }
165         else
166         {
167                 // Pass all current properties down
168                 mlt_properties composite_properties = mlt_transition_properties( composite );
169                 mlt_properties_pass( composite_properties, properties, "composite." );
170         }
171
172         // Create filters
173         if ( filter == NULL )
174         {
175                 // Loop Variable
176                 int i = 0;
177
178                 // Number of filters created
179                 int count = 0;
180
181                 // Loop for all properties
182                 for ( i = 0; i < mlt_properties_count( properties ); i ++ )
183                 {
184                         // Get the name of this property
185                         char *name = mlt_properties_get_name( properties, i );
186
187                         // If the name does not contain a . and matches filter
188                         if ( strchr( name, '.' ) == NULL && !strncmp( name, "filter", 6 ) )
189                         {
190                                 // Get the filter constructor
191                                 char *value = mlt_properties_get_value( properties, i );
192
193                                 // Create an instance
194                                 if ( create_instance( this, name, value, count ) == 0 )
195                                         count ++;
196                         }
197                 }
198         }
199         else
200         {
201                 // Pass all properties down
202                 mlt_filter temp = NULL;
203
204                 // Loop Variable
205                 int i = 0;
206
207                 // Number of filters found
208                 int count = 0;
209
210                 // Loop for all properties
211                 for ( i = 0; i < mlt_properties_count( properties ); i ++ )
212                 {
213                         // Get the name of this property
214                         char *name = mlt_properties_get_name( properties, i );
215
216                         // If the name does not contain a . and matches filter
217                         if ( strchr( name, '.' ) == NULL && !strncmp( name, "filter", 6 ) )
218                         {
219                                 // Strings to hold the id and pass down key
220                                 char id[ 256 ];
221                                 char key[ 256 ];
222
223                                 // Construct id and key
224                                 sprintf( id, "_filter_%d", count );
225                                 sprintf( key, "%s.", name );
226
227                                 // Get the filter
228                                 temp = mlt_properties_get_data( properties, id, NULL );
229
230                                 if ( temp != NULL )
231                                 {
232                                         mlt_properties_pass( mlt_filter_properties( temp ), properties, key );
233                                         count ++;
234                                 }
235                         }
236                 }
237         }
238
239         // Get the image
240         error = mlt_frame_get_image( frame, image, format, width, height, 1 );
241
242         // Only continue if we have both filter and composite
243         if ( composite != NULL )
244         {
245                 // Get the resource of this filter (could be a shape [rectangle/circle] or an alpha provider of choice
246                 char *resource =  mlt_properties_get( properties, "resource" );
247
248                 // String to hold the filter to query on
249                 char id[ 256 ];
250
251                 // Index to hold the count
252                 int i = 0;
253
254                 // We will get the 'b frame' from the composite only if it's NULL
255                 if ( b_frame == NULL )
256                 {
257                         // Copy the region
258                         b_frame = composite_copy_region( composite, frame, position );
259
260                         // Ensure a destructor
261                         mlt_properties_set_data( mlt_frame_properties( frame ), name, b_frame, 0, ( mlt_destructor )mlt_frame_close, NULL );
262                 }
263
264                 // Make sure the filter is in the correct position
265                 while ( filter != NULL )
266                 {
267                         // Stack this filter
268                         mlt_filter_process( filter, b_frame );
269
270                         // Generate the key for the next
271                         sprintf( id, "_filter_%d", ++ i );
272
273                         // Get the next filter
274                         filter = mlt_properties_get_data( properties, id, NULL );
275                 }
276
277                 // Hmm - this is probably going to go wrong....
278                 mlt_frame_set_position( frame, position );
279
280                 // Get the b frame and process with composite if successful
281                 mlt_transition_process( composite, frame, b_frame );
282
283                 // If we have a shape producer copy the alpha mask from the shape frame to the b_frame
284                 if ( strcmp( resource, "rectangle" ) != 0 )
285                 {
286                         // Get the producer from the transition
287                         mlt_producer producer = mlt_properties_get_data( properties, "producer", NULL );
288
289                         // If We have no producer then create one
290                         if ( producer == NULL )
291                         {
292                                 // Get the factory producer service
293                                 char *factory = mlt_properties_get( properties, "factory" );
294
295                                 // Special case circle resource
296                                 if ( strcmp( resource, "circle" ) == 0 )
297                                 {
298                                         // Special case to ensure that fezzik produces a pixbuf with a NULL constructor
299                                         resource = "pixbuf";
300
301                                         // Specify the svg circle
302                                         mlt_properties_set( properties, "producer.resource", "<svg width='100' height='100'><circle cx='50' cy='50' r='50' fill='black'/></svg>" );
303                                 }
304
305                                 // Create the producer 
306                                 producer = mlt_factory_producer( factory, resource );
307
308                                 // If we have one
309                                 if ( producer != NULL )
310                                 {
311                                         // Get the producer properties
312                                         mlt_properties producer_properties = mlt_producer_properties( producer );
313
314                                         // Ensure that we loop
315                                         mlt_properties_set( producer_properties, "eof", "loop" );
316
317                                         // Now pass all producer. properties on the transition down
318                                         mlt_properties_pass( producer_properties, properties, "producer." );
319
320                                         // Register the producer for reuse/destruction
321                                         mlt_properties_set_data( properties, "producer", producer, 0, ( mlt_destructor )mlt_producer_close, NULL );
322                                 }
323                         }
324
325                         // Now use the shape producer
326                         if ( producer != NULL )
327                         {
328                                 // We will get the alpha frame from the producer
329                                 mlt_frame shape_frame = NULL;
330
331                                 // Make sure the producer is in the correct position
332                                 mlt_producer_seek( producer, position );
333
334                                 // Get the shape frame
335                                 if ( mlt_service_get_frame( mlt_producer_service( producer ), &shape_frame, 0 ) == 0 )
336                                 {
337                                         // Ensure that the shape frame will be closed
338                                         mlt_properties_set_data( mlt_frame_properties( b_frame ), "shape_frame", shape_frame, 0, ( mlt_destructor )mlt_frame_close, NULL );
339
340                                         // Specify the callback for evaluation
341                                         b_frame->get_alpha_mask = filter_get_alpha_mask;
342                                 }
343                         }
344                 }
345
346                 // Get the image
347                 error = mlt_frame_get_image( frame, image, format, width, height, 0 );
348         }
349
350         return error;
351 }
352
353 /** Filter processing.
354 */
355
356 static mlt_frame transition_process( mlt_transition this, mlt_frame a_frame, mlt_frame b_frame )
357 {
358         // Get the properties of the frame
359         mlt_properties properties = mlt_frame_properties( a_frame );
360
361         // Get a unique name to store the frame position
362         char *name = mlt_properties_get( mlt_transition_properties( this ), "_unique_id" );
363
364         // Assign the current position to the name
365         mlt_properties_set_position( properties, name, mlt_frame_get_position( a_frame ) );
366
367         // Push the transition on to the frame
368         mlt_frame_push_service( a_frame, this );
369
370         // Push the b_frame on to the stack
371         mlt_frame_push_frame( a_frame, b_frame );
372
373         // Push the transition method
374         mlt_frame_push_get_image( a_frame, transition_get_image );
375
376         // Return the frame
377         return a_frame;
378 }
379
380 /** Constructor for the transition.
381 */
382
383 mlt_transition transition_region_init( void *arg )
384 {
385         // Create a new transition
386         mlt_transition this = mlt_transition_new( );
387
388         // Further initialisation
389         if ( this != NULL )
390         {
391                 // Get the properties from the transition
392                 mlt_properties properties = mlt_transition_properties( this );
393
394                 // Assign the transition process method
395                 this->process = transition_process;
396
397                 // Default factory
398                 mlt_properties_set( properties, "factory", "fezzik" );
399                 
400                 // Resource defines the shape of the region
401                 mlt_properties_set( properties, "resource", arg == NULL ? "rectangle" : arg );
402         }
403
404         // Return the transition
405         return this;
406 }
407