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