]> git.sesse.net Git - mlt/blob - src/modules/core/filter_region.c
some bugfixes, filter_shape producer, pixbuf takes svg xml, fezzik can take a service...
[mlt] / src / modules / core / filter_region.c
1 /*
2  * filter_region.c -- region filter
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 "filter_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_filter 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_filter_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                 // Pass all the key properties on the filter down
70                 mlt_properties_pass( mlt_filter_properties( filter ), properties, key );
71
72                 // Ensure that filter is assigned
73                 mlt_properties_set_data( properties, id, filter, 0, ( mlt_destructor )mlt_filter_close, NULL );
74         }
75         else
76         {
77                 // Indicate that an error has occurred
78                 error = 1;
79         }
80
81         // Cleanup
82         free( type );
83
84         // Return error condition
85         return error;
86 }
87
88 static uint8_t *filter_get_alpha_mask( mlt_frame this )
89 {
90         // Obtain properties of frame
91         mlt_properties properties = mlt_frame_properties( this );
92
93         // Return the alpha mask
94         return mlt_properties_get_data( properties, "alpha", NULL );
95 }
96
97 /** Do it :-).
98 */
99
100 static int filter_get_image( mlt_frame frame, uint8_t **image, mlt_image_format *format, int *width, int *height, int writable )
101 {
102         // Error we will return
103         int error = 0;
104
105         // Get the watermark filter object
106         mlt_filter this = mlt_frame_pop_service( frame );
107
108         // Get the properties of the filter
109         mlt_properties properties = mlt_filter_properties( this );
110
111         // Get the composite from the filter
112         mlt_transition composite = mlt_properties_get_data( properties, "composite", NULL );
113
114         // Look for the first filter
115         mlt_filter filter = mlt_properties_get_data( properties, "_filter_0", NULL );
116
117         // Create a composite if we don't have one
118         if ( composite == NULL )
119         {
120                 // Create composite via the factory
121                 composite = mlt_factory_transition( "composite", NULL );
122
123                 // If we have one
124                 if ( composite != NULL )
125                 {
126                         // Get the properties
127                         mlt_properties composite_properties = mlt_transition_properties( composite );
128
129                         // We want to ensure that we don't get a wobble...
130                         mlt_properties_set( composite_properties, "distort", "true" );
131                         mlt_properties_set( composite_properties, "progressive", "1" );
132
133                         // Pass all the composite. properties on the filter down
134                         mlt_properties_pass( composite_properties, properties, "composite." );
135
136                         // Register the composite for reuse/destruction
137                         mlt_properties_set_data( properties, "composite", composite, 0, ( mlt_destructor )mlt_transition_close, NULL );
138                 }
139         }
140
141         // Create filters
142         if ( filter == NULL )
143         {
144                 // Loop Variable
145                 int i = 0;
146
147                 // Number of filters created
148                 int count = 0;
149
150                 // Loop for all properties
151                 for ( i = 0; i < mlt_properties_count( properties ); i ++ )
152                 {
153                         // Get the name of this property
154                         char *name = mlt_properties_get_name( properties, i );
155
156                         // If the name does not contain a . and matches filter
157                         if ( strchr( name, '.' ) == NULL && !strncmp( name, "filter", 6 ) )
158                         {
159                                 // Get the filter constructor
160                                 char *value = mlt_properties_get_value( properties, i );
161
162                                 // Create an instance
163                                 if ( create_instance( this, name, value, count ) == 0 )
164                                         count ++;
165                         }
166                 }
167         }
168
169         // Get the image
170         error = mlt_frame_get_image( frame, image, format, width, height, 1 );
171
172         // Only continue if we have both filter and composite
173         if ( composite != NULL && filter != NULL )
174         {
175                 // String to hold the filter to query on
176                 char id[ 256 ];
177
178                 // Index to hold the count
179                 int i = 0;
180
181                 // We will get the 'b frame' from the composite
182                 mlt_frame b_frame = composite_copy_region( composite, frame );
183
184                 // Make sure the filter is in the correct position
185                 while ( filter != NULL )
186                 {
187                         // Stack this filter
188                         mlt_filter_process( filter, b_frame );
189
190                         // Generate the key for the next
191                         sprintf( id, "_filter_%d", ++ i );
192
193                         // Get the next filter
194                         filter = mlt_properties_get_data( properties, id, NULL );
195                 }
196
197                 // Get the b frame and process with composite if successful
198                 mlt_transition_process( composite, frame, b_frame );
199
200                 // See if we have a shape producer
201                 // Copy the alpha mask from the shape frame to the b_frame
202                 char *resource =  mlt_properties_get( properties, "resource" );
203
204                 if ( strcmp( resource, "rectangle" ) != 0 )
205                 {
206                         if ( strcmp( resource, "circle" ) == 0 )
207                         {
208                                 resource = strdup( "pixbuf" );
209                                 mlt_properties_set( properties, "producer.resource", "<svg width='100' height='100'><circle cx='50' cy='50' r='50' fill='black'/></svg>" );
210                         }
211
212                         // Get the producer from the filter
213                         mlt_producer producer = mlt_properties_get_data( properties, "producer", NULL );
214
215                         if ( producer == NULL )
216                         {
217                                 // Get the factory producer service
218                                 char *factory = mlt_properties_get( properties, "factory" );
219
220                                 // Create the producer
221                                 producer = mlt_factory_producer( factory, resource );
222
223                                 // If we have one
224                                 if ( producer != NULL )
225                                 {
226                                         // Get the producer properties
227                                         mlt_properties producer_properties = mlt_producer_properties( producer );
228
229                                         // Ensure that we loop
230                                         mlt_properties_set( producer_properties, "eof", "loop" );
231
232                                         // Now pass all producer. properties on the filter down
233                                         mlt_properties_pass( producer_properties, properties, "producer." );
234
235                                         // Register the producer for reuse/destruction
236                                         mlt_properties_set_data( properties, "producer", producer, 0, ( mlt_destructor )mlt_producer_close, NULL );
237                                 }
238                         }
239
240                         if ( producer != NULL )
241                         {
242                                 // We will get the alpha frame from the producer
243                                 mlt_frame shape_frame = NULL;
244
245                                 // Get the unique id of the filter (used to reacquire the producer position)
246                                 char *name = mlt_properties_get( properties, "_unique_id" );
247
248                                 // Get the original producer position
249                                 mlt_position position = mlt_properties_get_position( mlt_frame_properties( frame ), name );
250
251                                 // Make sure the producer is in the correct position
252                                 mlt_producer_seek( producer, position );
253
254                                 // Get the shape frame
255                                 if ( mlt_service_get_frame( mlt_producer_service( producer ), &shape_frame, 0 ) == 0 )
256                                 {
257                                         int region_width = mlt_properties_get_int( mlt_frame_properties( b_frame ), "width" );
258                                         int region_height = mlt_properties_get_int( mlt_frame_properties( b_frame ), "height" );
259                                         
260                                         // Get the shape image to trigger alpha creation
261                                         mlt_properties_set( mlt_frame_properties( shape_frame ), "distort", "true" );
262                                         error = mlt_frame_get_image( shape_frame, image, format, &region_width, &region_height, 1 );
263
264                                         // Only override any existing b_frame alpha if the shape has an alpha
265                                         if ( mlt_frame_get_alpha_mask( shape_frame ) != NULL )
266                                         {
267                                                 // Set the b_frame alpha from the shape frame
268                                                 mlt_properties_set_data( mlt_frame_properties( b_frame ), "alpha", mlt_frame_get_alpha_mask( shape_frame ), 0 , NULL, NULL );
269                                                 b_frame->get_alpha_mask = filter_get_alpha_mask;
270                                         }
271                                         
272                                         // Get the image
273                                         error = mlt_frame_get_image( frame, image, format, width, height, 0 );
274
275                                         // Close the b frame
276                                         mlt_frame_close( b_frame );
277                                         b_frame = NULL;
278
279                                         // Close the shape frame
280                                         mlt_frame_close( shape_frame );
281                                 }
282                         }
283                 }
284                 if ( b_frame != NULL )
285                 {
286                         // Get the image
287                         error = mlt_frame_get_image( frame, image, format, width, height, 0 );
288
289                         // Close the b frame
290                         mlt_frame_close( b_frame );
291                 }
292         }
293
294         return error;
295 }
296
297 /** Filter processing.
298 */
299
300 static mlt_frame filter_process( mlt_filter this, mlt_frame frame )
301 {
302         // Push the filter on to the frame
303         mlt_frame_push_service( frame, this );
304
305         // Push the filter method
306         mlt_frame_push_get_image( frame, filter_get_image );
307
308         // Return the frame
309         return frame;
310 }
311
312 /** Constructor for the filter.
313 */
314
315 mlt_filter filter_region_init( void *arg )
316 {
317         // Create a new filter
318         mlt_filter this = mlt_filter_new( );
319
320         // Further initialisation
321         if ( this != NULL )
322         {
323                 // Get the properties from the filter
324                 mlt_properties properties = mlt_filter_properties( this );
325
326                 // Assign the filter process method
327                 this->process = filter_process;
328
329                 mlt_properties_set( properties, "factory", "fezzik" );
330                 
331                 // Resource defines the shape of the region
332                 mlt_properties_set( properties, "resource", arg == NULL ? "rectangle" : arg );
333         }
334
335         // Return the filter
336         return this;
337 }
338