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