]> git.sesse.net Git - mlt/blob - src/modules/core/transition_region.c
Cleanup license declarations and remove dv1394d references.
[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 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.
10  *
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.
15  *
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
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.geometry", "0%,0%:100%x100%" );
71                 //mlt_properties_set_int( MLT_FILTER_PROPERTIES( filter ), "composite.fill", 1 );
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_int( MLT_FRAME_PROPERTIES( shape_frame ), "distort", 1 );
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 ++ = ( int )( ( ( *image ++ - 16 ) * 299 ) / 255 );
123                         image ++;
124                 }
125                 mlt_properties_set_data( MLT_FRAME_PROPERTIES( this ), "alpha", alpha, region_width * region_height, mlt_pool_release, NULL );
126         }
127         else
128         {
129                 mlt_properties_set_data( MLT_FRAME_PROPERTIES( this ), "alpha", alpha, region_width * region_height, NULL, NULL );
130         }
131
132         this->get_alpha_mask = NULL;
133
134         return alpha;
135 }
136
137 /** Do it :-).
138 */
139
140 static int transition_get_image( mlt_frame frame, uint8_t **image, mlt_image_format *format, int *width, int *height, int writable )
141 {
142         // Error we will return
143         int error = 0;
144
145         // We will get the 'b frame' from the frame stack
146         mlt_frame b_frame = mlt_frame_pop_frame( frame );
147
148         // Get the watermark transition object
149         mlt_transition this = mlt_frame_pop_service( frame );
150
151         // Get the properties of the transition
152         mlt_properties properties = MLT_TRANSITION_PROPERTIES( this );
153
154         // Get the composite from the transition
155         mlt_transition composite = mlt_properties_get_data( properties, "composite", NULL );
156
157         // Look for the first filter
158         mlt_filter filter = mlt_properties_get_data( properties, "_filter_0", NULL );
159
160         // Get the unique id of the filter (used to reacquire the producer position)
161         char *name = mlt_properties_get( properties, "_unique_id" );
162
163         // Get the original producer position
164         mlt_position position = mlt_properties_get_position( MLT_FRAME_PROPERTIES( frame ), name );
165
166         // Create a composite if we don't have one
167         if ( composite == NULL )
168         {
169                 // Create composite via the factory
170                 composite = mlt_factory_transition( "composite", NULL );
171
172                 // If we have one
173                 if ( composite != NULL )
174                 {
175                         // Get the properties
176                         mlt_properties composite_properties = MLT_TRANSITION_PROPERTIES( composite );
177
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 );
181
182                         // Pass all the composite. properties on the transition down
183                         mlt_properties_pass( composite_properties, properties, "composite." );
184
185                         // Register the composite for reuse/destruction
186                         mlt_properties_set_data( properties, "composite", composite, 0, ( mlt_destructor )mlt_transition_close, NULL );
187                 }
188         }
189         else
190         {
191                 // Pass all current properties down
192                 mlt_properties composite_properties = MLT_TRANSITION_PROPERTIES( composite );
193                 mlt_properties_pass( composite_properties, properties, "composite." );
194         }
195
196         // Create filters
197         if ( filter == NULL )
198         {
199                 // Loop Variable
200                 int i = 0;
201
202                 // Number of filters created
203                 int count = 0;
204
205                 // Loop for all properties
206                 for ( i = 0; i < mlt_properties_count( properties ); i ++ )
207                 {
208                         // Get the name of this property
209                         char *name = mlt_properties_get_name( properties, i );
210
211                         // If the name does not contain a . and matches filter
212                         if ( strchr( name, '.' ) == NULL && !strncmp( name, "filter", 6 ) )
213                         {
214                                 // Get the filter constructor
215                                 char *value = mlt_properties_get_value( properties, i );
216
217                                 // Create an instance
218                                 if ( create_instance( this, name, value, count ) == 0 )
219                                         count ++;
220                         }
221                 }
222         
223                 // Look for the first filter again
224                 filter = mlt_properties_get_data( properties, "_filter_0", NULL );
225         }
226         else
227         {
228                 // Pass all properties down
229                 mlt_filter temp = NULL;
230
231                 // Loop Variable
232                 int i = 0;
233
234                 // Number of filters found
235                 int count = 0;
236
237                 // Loop for all properties
238                 for ( i = 0; i < mlt_properties_count( properties ); i ++ )
239                 {
240                         // Get the name of this property
241                         char *name = mlt_properties_get_name( properties, i );
242
243                         // If the name does not contain a . and matches filter
244                         if ( strchr( name, '.' ) == NULL && !strncmp( name, "filter", 6 ) )
245                         {
246                                 // Strings to hold the id and pass down key
247                                 char id[ 256 ];
248                                 char key[ 256 ];
249
250                                 // Construct id and key
251                                 sprintf( id, "_filter_%d", count );
252                                 sprintf( key, "%s.", name );
253
254                                 // Get the filter
255                                 temp = mlt_properties_get_data( properties, id, NULL );
256
257                                 if ( temp != NULL )
258                                 {
259                                         mlt_properties_pass( MLT_FILTER_PROPERTIES( temp ), properties, key );
260                                         count ++;
261                                 }
262                         }
263                 }
264         }
265
266         // Get the image
267         error = mlt_frame_get_image( frame, image, format, width, height, 1 );
268
269         // Only continue if we have both filter and composite
270         if ( composite != NULL )
271         {
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" );
274
275                 // Get the old resource in case it's changed
276                 char *old_resource =  mlt_properties_get( properties, "_old_resource" );
277
278                 // String to hold the filter to query on
279                 char id[ 256 ];
280
281                 // Index to hold the count
282                 int i = 0;
283
284                 // We will get the 'b frame' from the composite only if it's NULL
285                 if ( b_frame == NULL )
286                 {
287                         // Copy the region
288                         b_frame = composite_copy_region( composite, frame, position );
289
290                         // Ensure a destructor
291                         mlt_properties_set_data( MLT_FRAME_PROPERTIES( frame ), name, b_frame, 0, ( mlt_destructor )mlt_frame_close, NULL );
292                 }
293
294                 // Set the position of the b_frame
295                 mlt_frame_set_position( b_frame, position );
296
297                 // Make sure the filter is in the correct position
298                 while ( filter != NULL )
299                 {
300                         // Stack this filter
301                         if ( mlt_properties_get_int( MLT_FILTER_PROPERTIES( filter ), "off" ) == 0 )
302                                 mlt_filter_process( filter, b_frame );
303
304                         // Generate the key for the next
305                         sprintf( id, "_filter_%d", ++ i );
306
307                         // Get the next filter
308                         filter = mlt_properties_get_data( properties, id, NULL );
309                 }
310
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 );
315
316                 // Hmm - this is probably going to go wrong....
317                 mlt_frame_set_position( frame, position );
318
319                 // Get the b frame and process with composite if successful
320                 mlt_transition_process( composite, frame, b_frame );
321
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 )
324                 {
325                         // Get the producer from the transition
326                         mlt_producer producer = mlt_properties_get_data( properties, "producer", NULL );
327
328                         // If We have no producer then create one
329                         if ( producer == NULL || ( old_resource != NULL && strcmp( resource, old_resource ) ) )
330                         {
331                                 // Get the factory producer service
332                                 char *factory = mlt_properties_get( properties, "factory" );
333
334                                 // Store the old resource
335                                 mlt_properties_set( properties, "_old_resource", resource );
336
337                                 // Special case circle resource
338                                 if ( strcmp( resource, "circle" ) == 0 )
339                                 {
340                                         // Special case to ensure that fezzik produces a pixbuf with a NULL constructor
341                                         resource = "pixbuf";
342
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>" );
345                                 }
346
347                                 // Create the producer 
348                                 producer = mlt_factory_producer( factory, resource );
349
350                                 // If we have one
351                                 if ( producer != NULL )
352                                 {
353                                         // Get the producer properties
354                                         mlt_properties producer_properties = MLT_PRODUCER_PROPERTIES( producer );
355
356                                         // Ensure that we loop
357                                         mlt_properties_set( producer_properties, "eof", "loop" );
358
359                                         // Now pass all producer. properties on the transition down
360                                         mlt_properties_pass( producer_properties, properties, "producer." );
361
362                                         // Register the producer for reuse/destruction
363                                         mlt_properties_set_data( properties, "producer", producer, 0, ( mlt_destructor )mlt_producer_close, NULL );
364                                 }
365                         }
366
367                         // Now use the shape producer
368                         if ( producer != NULL )
369                         {
370                                 // We will get the alpha frame from the producer
371                                 mlt_frame shape_frame = NULL;
372
373                                 // Make sure the producer is in the correct position
374                                 mlt_producer_seek( producer, position );
375
376                                 // Get the shape frame
377                                 if ( mlt_service_get_frame( MLT_PRODUCER_SERVICE( producer ), &shape_frame, 0 ) == 0 )
378                                 {
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 );
381
382                                         // Specify the callback for evaluation
383                                         b_frame->get_alpha_mask = filter_get_alpha_mask;
384                                 }
385                         }
386                 }
387
388                 // Get the image
389                 error = mlt_frame_get_image( frame, image, format, width, height, 0 );
390         }
391
392         return error;
393 }
394
395 /** Filter processing.
396 */
397
398 static mlt_frame transition_process( mlt_transition this, mlt_frame a_frame, mlt_frame b_frame )
399 {
400         // Get the properties of the frame
401         mlt_properties properties = MLT_FRAME_PROPERTIES( a_frame );
402
403         // Get a unique name to store the frame position
404         char *name = mlt_properties_get( MLT_TRANSITION_PROPERTIES( this ), "_unique_id" );
405
406         // Assign the current position to the name
407         mlt_properties_set_position( properties, name, mlt_frame_get_position( a_frame ) );
408
409         // Push the transition on to the frame
410         mlt_frame_push_service( a_frame, this );
411
412         // Push the b_frame on to the stack
413         mlt_frame_push_frame( a_frame, b_frame );
414
415         // Push the transition method
416         mlt_frame_push_get_image( a_frame, transition_get_image );
417
418         // Return the frame
419         return a_frame;
420 }
421
422 /** Constructor for the transition.
423 */
424
425 mlt_transition transition_region_init( void *arg )
426 {
427         // Create a new transition
428         mlt_transition this = mlt_transition_new( );
429
430         // Further initialisation
431         if ( this != NULL )
432         {
433                 // Get the properties from the transition
434                 mlt_properties properties = MLT_TRANSITION_PROPERTIES( this );
435
436                 // Assign the transition process method
437                 this->process = transition_process;
438
439                 // Default factory
440                 mlt_properties_set( properties, "factory", "fezzik" );
441                 
442                 // Resource defines the shape of the region
443                 mlt_properties_set( properties, "resource", arg == NULL ? "rectangle" : arg );
444
445                 // Inform apps and framework that this is a video only transition
446                 mlt_properties_set_int( properties, "_transition_type", 1 );
447         }
448
449         // Return the transition
450         return this;
451 }
452