]> git.sesse.net Git - mlt/blob - src/modules/core/transition_region.c
improve readability
[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 transition, 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         mlt_profile profile = mlt_service_profile( MLT_TRANSITION_SERVICE( transition ) );
50         filter = mlt_factory_filter( profile, type, arg );
51
52         // If we have a filter, then initialise and store it
53         if ( filter != NULL )
54         {
55                 // Properties of transition
56                 mlt_properties properties = MLT_TRANSITION_PROPERTIES( transition );
57
58                 // String to hold the property name
59                 char id[ 256 ];
60
61                 // String to hold the passdown key
62                 char key[ 256 ];
63
64                 // Construct id
65                 sprintf( id, "_filter_%d", count );
66
67                 // Counstruct key
68                 sprintf( key, "%s.", name );
69
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 );
73
74                 // Pass all the key properties on the filter down
75                 mlt_properties_pass( MLT_FILTER_PROPERTIES( filter ), properties, key );
76                 mlt_properties_pass_list( MLT_FILTER_PROPERTIES( filter ), properties, "in, out, length" );
77
78                 // Ensure that filter is assigned
79                 mlt_properties_set_data( properties, id, filter, 0, ( mlt_destructor )mlt_filter_close, NULL );
80         }
81         else
82         {
83                 // Indicate that an error has occurred
84                 error = 1;
85         }
86
87         // Cleanup
88         free( type );
89
90         // Return error condition
91         return error;
92 }
93
94 static uint8_t *filter_get_alpha_mask( mlt_frame frame )
95 {
96         uint8_t *alpha = NULL;
97
98         // Obtain properties of frame
99         mlt_properties properties = MLT_FRAME_PROPERTIES( frame );
100
101         // Get the shape frame
102         mlt_frame shape_frame = mlt_properties_get_data( properties, "shape_frame", NULL );
103
104         // Get the width and height of the image
105         int region_width = mlt_properties_get_int( properties, "width" );
106         int region_height = mlt_properties_get_int( properties, "height" );
107         uint8_t *image = NULL;
108         mlt_image_format format = mlt_image_yuv422;
109                                         
110         // Get the shape image to trigger alpha creation
111         mlt_properties_set_int( MLT_FRAME_PROPERTIES( shape_frame ), "distort", 1 );
112         mlt_frame_get_image( shape_frame, &image, &format, &region_width, &region_height, 0 );
113
114         alpha = mlt_frame_get_alpha_mask( shape_frame );
115
116         // Generate from the Y component of the image if no alpha available
117         if ( alpha == NULL )
118         {
119                 int size = region_width * region_height;
120                 uint8_t *p = mlt_pool_alloc( size );
121                 alpha = p;
122                 while ( size -- )
123                 {
124                         *p ++ = ( int )( ( ( *image ++ - 16 ) * 299 ) / 255 );
125                         image ++;
126                 }
127                 mlt_frame_set_alpha( frame, alpha, region_width * region_height, mlt_pool_release );
128         }
129         else
130         {
131                 mlt_frame_set_alpha( frame, alpha, region_width * region_height, NULL );
132         }
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 transition = mlt_frame_pop_service( frame );
150
151         // Get the properties of the transition
152         mlt_properties properties = MLT_TRANSITION_PROPERTIES( transition );
153
154         // Get the properties of the a frame
155         mlt_properties a_props = MLT_FRAME_PROPERTIES( frame );
156
157         mlt_service_lock( MLT_TRANSITION_SERVICE( transition ) );
158
159         // Get the composite from the transition
160         mlt_transition composite = mlt_properties_get_data( properties, "composite", NULL );
161
162         // Look for the first filter
163         mlt_filter filter = mlt_properties_get_data( properties, "_filter_0", NULL );
164
165         // Get the position
166         mlt_position position = mlt_transition_get_position( transition, frame );
167
168         // Create a composite if we don't have one
169         if ( composite == NULL )
170         {
171                 // Create composite via the factory
172                 mlt_profile profile = mlt_service_profile( MLT_TRANSITION_SERVICE( transition ) );
173                 composite = mlt_factory_transition( profile, "composite", NULL );
174
175                 // If we have one
176                 if ( composite != NULL )
177                 {
178                         // Get the properties
179                         mlt_properties composite_properties = MLT_TRANSITION_PROPERTIES( composite );
180
181                         // We want to ensure that we don't get a wobble...
182                         //mlt_properties_set_int( composite_properties, "distort", 1 );
183                         mlt_properties_set_int( composite_properties, "progressive", 1 );
184
185                         // Pass all the composite. properties on the transition down
186                         mlt_properties_pass( composite_properties, properties, "composite." );
187
188                         // Register the composite for reuse/destruction
189                         mlt_properties_set_data( properties, "composite", composite, 0, ( mlt_destructor )mlt_transition_close, NULL );
190                 }
191         }
192         else
193         {
194                 // Pass all current properties down
195                 mlt_properties composite_properties = MLT_TRANSITION_PROPERTIES( composite );
196                 mlt_properties_pass( composite_properties, properties, "composite." );
197         }
198
199         // Create filters
200         if ( filter == NULL )
201         {
202                 // Loop Variable
203                 int i = 0;
204
205                 // Number of filters created
206                 int count = 0;
207
208                 // Loop for all properties
209                 for ( i = 0; i < mlt_properties_count( properties ); i ++ )
210                 {
211                         // Get the name of this property
212                         char *name = mlt_properties_get_name( properties, i );
213
214                         // If the name does not contain a . and matches filter
215                         if ( strchr( name, '.' ) == NULL && !strncmp( name, "filter", 6 ) )
216                         {
217                                 // Get the filter constructor
218                                 char *value = mlt_properties_get_value( properties, i );
219
220                                 // Create an instance
221                                 if ( create_instance( transition, name, value, count ) == 0 )
222                                         count ++;
223                         }
224                 }
225         
226                 // Look for the first filter again
227                 filter = mlt_properties_get_data( properties, "_filter_0", NULL );
228         }
229         else
230         {
231                 // Pass all properties down
232                 mlt_filter temp = NULL;
233
234                 // Loop Variable
235                 int i = 0;
236
237                 // Number of filters found
238                 int count = 0;
239
240                 // Loop for all properties
241                 for ( i = 0; i < mlt_properties_count( properties ); i ++ )
242                 {
243                         // Get the name of this property
244                         char *name = mlt_properties_get_name( properties, i );
245
246                         // If the name does not contain a . and matches filter
247                         if ( strchr( name, '.' ) == NULL && !strncmp( name, "filter", 6 ) )
248                         {
249                                 // Strings to hold the id and pass down key
250                                 char id[ 256 ];
251                                 char key[ 256 ];
252
253                                 // Construct id and key
254                                 sprintf( id, "_filter_%d", count );
255                                 sprintf( key, "%s.", name );
256
257                                 // Get the filter
258                                 temp = mlt_properties_get_data( properties, id, NULL );
259
260                                 if ( temp != NULL )
261                                 {
262                                         mlt_properties_pass( MLT_FILTER_PROPERTIES( temp ), properties, key );
263                                         count ++;
264                                 }
265                         }
266                 }
267         }
268
269         mlt_properties_set_int( a_props, "width", *width );
270         mlt_properties_set_int( a_props, "height", *height );
271
272         // Only continue if we have both filter and composite
273         if ( composite != NULL )
274         {
275                 // Get the resource of this filter (could be a shape [rectangle/circle] or an alpha provider of choice
276                 const char *resource =  mlt_properties_get( properties, "resource" );
277
278                 // Get the old resource in case it's changed
279                 char *old_resource =  mlt_properties_get( properties, "_old_resource" );
280
281                 // String to hold the filter to query on
282                 char id[ 256 ];
283
284                 // Index to hold the count
285                 int i = 0;
286
287                 // We will get the 'b frame' from the composite only if it's NULL (region filter)
288                 if ( b_frame == NULL )
289                 {
290                         // Copy the region
291                         b_frame = composite_copy_region( composite, frame, position );
292
293                         // Ensure a destructor
294                         char *name = mlt_properties_get( properties, "_unique_id" );
295                         mlt_properties_set_data( a_props, name, b_frame, 0, ( mlt_destructor )mlt_frame_close, NULL );
296                 }
297
298                 // Properties of the B frame
299                 mlt_properties b_props = MLT_FRAME_PROPERTIES( b_frame );
300
301                 // filter_only prevents copying the alpha channel of the shape to the output frame
302                 // by compositing filtered frame over itself
303                 if ( mlt_properties_get_int( properties, "filter_only" ) )
304                 {
305                         frame = composite_copy_region( composite, b_frame, position );
306                 }
307
308                 // Make sure the filter is in the correct position
309                 while ( filter != NULL )
310                 {
311                         // Stack this filter
312                         if ( mlt_properties_get_int( MLT_FILTER_PROPERTIES( filter ), "off" ) == 0 )
313                                 mlt_filter_process( filter, b_frame );
314
315                         // Generate the key for the next
316                         sprintf( id, "_filter_%d", ++ i );
317
318                         // Get the next filter
319                         filter = mlt_properties_get_data( properties, id, NULL );
320                 }
321
322                 // Allow filters to be attached to a region filter
323                 filter = mlt_properties_get_data( properties, "_region_filter", NULL );
324                 if ( filter != NULL )
325                         mlt_service_apply_filters( MLT_FILTER_SERVICE( filter ), b_frame, 0 );
326
327                 // Hmm - this is probably going to go wrong....
328                 mlt_frame_set_position( frame, position );
329
330                 // Get the b frame and process with composite if successful
331                 mlt_transition_process( composite, frame, b_frame );
332
333                 // If we have a shape producer copy the alpha mask from the shape frame to the b_frame
334                 if ( strcmp( resource, "rectangle" ) != 0 )
335                 {
336                         // Get the producer from the transition
337                         mlt_producer producer = mlt_properties_get_data( properties, "producer", NULL );
338
339                         // If We have no producer then create one
340                         if ( producer == NULL || ( old_resource != NULL && strcmp( resource, old_resource ) ) )
341                         {
342                                 // Get the factory producer service
343                                 char *factory = mlt_properties_get( properties, "factory" );
344
345                                 // Store the old resource
346                                 mlt_properties_set( properties, "_old_resource", resource );
347
348                                 // Special case circle resource
349                                 if ( strcmp( resource, "circle" ) == 0 )
350                                         resource = "pixbuf:<svg width='100' height='100'><circle cx='50' cy='50' r='50' fill='black'/></svg>";
351
352                                 // Create the producer
353                                 mlt_profile profile = mlt_service_profile( MLT_TRANSITION_SERVICE( transition ) );
354                                 producer = mlt_factory_producer( profile, factory, resource );
355
356                                 // If we have one
357                                 if ( producer != NULL )
358                                 {
359                                         // Get the producer properties
360                                         mlt_properties producer_properties = MLT_PRODUCER_PROPERTIES( producer );
361
362                                         // Ensure that we loop
363                                         mlt_properties_set( producer_properties, "eof", "loop" );
364
365                                         // Now pass all producer. properties on the transition down
366                                         mlt_properties_pass( producer_properties, properties, "producer." );
367
368                                         // Register the producer for reuse/destruction
369                                         mlt_properties_set_data( properties, "producer", producer, 0, ( mlt_destructor )mlt_producer_close, NULL );
370                                 }
371                         }
372
373                         // Now use the shape producer
374                         if ( producer != NULL )
375                         {
376                                 // We will get the alpha frame from the producer
377                                 mlt_frame shape_frame = NULL;
378
379                                 // Make sure the producer is in the correct position
380                                 mlt_producer_seek( producer, position );
381
382                                 // Get the shape frame
383                                 if ( mlt_service_get_frame( MLT_PRODUCER_SERVICE( producer ), &shape_frame, 0 ) == 0 )
384                                 {
385                                         // Ensure that the shape frame will be closed
386                                         mlt_properties_set_data( b_props, "shape_frame", shape_frame, 0, ( mlt_destructor )mlt_frame_close, NULL );
387
388                                         // Specify the callback for evaluation
389                                         b_frame->get_alpha_mask = filter_get_alpha_mask;
390                                 }
391                         }
392                 }
393
394                 // Get the image
395                 error = mlt_frame_get_image( frame, image, format, width, height, 0 );
396         }
397
398         mlt_service_unlock( MLT_TRANSITION_SERVICE( transition ) );
399
400         return error;
401 }
402
403 /** Filter processing.
404 */
405
406 static mlt_frame transition_process( mlt_transition transition, mlt_frame a_frame, mlt_frame b_frame )
407 {
408         // Push the transition on to the frame
409         mlt_frame_push_service( a_frame, transition );
410
411         // Push the b_frame on to the stack
412         mlt_frame_push_frame( a_frame, b_frame );
413
414         // Push the transition method
415         mlt_frame_push_get_image( a_frame, transition_get_image );
416
417         // Return the frame
418         return a_frame;
419 }
420
421 /** Constructor for the transition.
422 */
423
424 mlt_transition transition_region_init( mlt_profile profile, mlt_service_type type, const char *id, char *arg )
425 {
426         // Create a new transition
427         mlt_transition transition = mlt_transition_new( );
428
429         // Further initialisation
430         if ( transition != NULL )
431         {
432                 // Get the properties from the transition
433                 mlt_properties properties = MLT_TRANSITION_PROPERTIES( transition );
434
435                 // Assign the transition process method
436                 transition->process = transition_process;
437
438                 // Default factory
439                 mlt_properties_set( properties, "factory", mlt_environment( "MLT_PRODUCER" ) );
440                 
441                 // Resource defines the shape of the region
442                 mlt_properties_set( properties, "resource", arg == NULL ? "rectangle" : arg );
443
444                 // Inform apps and framework that this is a video only transition
445                 mlt_properties_set_int( properties, "_transition_type", 1 );
446         }
447
448         // Return the transition
449         return transition;
450 }
451