Filter attachments to services
[melted] / 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 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 "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.start", "0%,0%:100%x100%" );
71 mlt_properties_set( mlt_filter_properties( filter ), "composite.fill", "true" );
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( mlt_frame_properties( shape_frame ), "distort", "true" );
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 ++ = *image ++;
123 image ++;
124 }
125 mlt_properties_set_data( mlt_frame_properties( shape_frame ), "alpha", alpha,
126 region_width * region_height, mlt_pool_release, NULL );
127 }
128
129 return alpha;
130 }
131
132 static void apply_filters( mlt_filter this, mlt_frame frame, int index )
133 {
134 mlt_service service = mlt_filter_service( this );
135 mlt_properties properties = mlt_filter_properties( this );
136 int i;
137
138 if ( index == 0 || mlt_properties_get_int( properties, "_filter_private" ) == 0 )
139 {
140 mlt_filter filter = NULL;
141 for ( i = 0; ( filter = mlt_service_filter( service, i ) ) != NULL; i ++ )
142 {
143 mlt_filter_process( filter, frame );
144 apply_filters( filter, frame, 1 );
145 }
146 }
147 }
148
149 /** Do it :-).
150 */
151
152 static int transition_get_image( mlt_frame frame, uint8_t **image, mlt_image_format *format, int *width, int *height, int writable )
153 {
154 // Error we will return
155 int error = 0;
156
157 // We will get the 'b frame' from the frame stack
158 mlt_frame b_frame = mlt_frame_pop_frame( frame );
159
160 // Get the watermark transition object
161 mlt_transition this = mlt_frame_pop_service( frame );
162
163 // Get the properties of the transition
164 mlt_properties properties = mlt_transition_properties( this );
165
166 // Get the composite from the transition
167 mlt_transition composite = mlt_properties_get_data( properties, "composite", NULL );
168
169 // Look for the first filter
170 mlt_filter filter = mlt_properties_get_data( properties, "_filter_0", NULL );
171
172 // Get the unique id of the filter (used to reacquire the producer position)
173 char *name = mlt_properties_get( properties, "_unique_id" );
174
175 // Get the original producer position
176 mlt_position position = mlt_properties_get_position( mlt_frame_properties( frame ), name );
177
178 // Create a composite if we don't have one
179 if ( composite == NULL )
180 {
181 // Create composite via the factory
182 composite = mlt_factory_transition( "composite", NULL );
183
184 // If we have one
185 if ( composite != NULL )
186 {
187 // Get the properties
188 mlt_properties composite_properties = mlt_transition_properties( composite );
189
190 // We want to ensure that we don't get a wobble...
191 mlt_properties_set( composite_properties, "distort", "true" );
192 mlt_properties_set( composite_properties, "progressive", "1" );
193
194 // Pass all the composite. properties on the transition down
195 mlt_properties_pass( composite_properties, properties, "composite." );
196
197 // Register the composite for reuse/destruction
198 mlt_properties_set_data( properties, "composite", composite, 0, ( mlt_destructor )mlt_transition_close, NULL );
199 }
200 }
201 else
202 {
203 // Pass all current properties down
204 mlt_properties composite_properties = mlt_transition_properties( composite );
205 mlt_properties_pass( composite_properties, properties, "composite." );
206 }
207
208 // Create filters
209 if ( filter == NULL )
210 {
211 // Loop Variable
212 int i = 0;
213
214 // Number of filters created
215 int count = 0;
216
217 // Loop for all properties
218 for ( i = 0; i < mlt_properties_count( properties ); i ++ )
219 {
220 // Get the name of this property
221 char *name = mlt_properties_get_name( properties, i );
222
223 // If the name does not contain a . and matches filter
224 if ( strchr( name, '.' ) == NULL && !strncmp( name, "filter", 6 ) )
225 {
226 // Get the filter constructor
227 char *value = mlt_properties_get_value( properties, i );
228
229 // Create an instance
230 if ( create_instance( this, name, value, count ) == 0 )
231 count ++;
232 }
233 }
234 }
235 else
236 {
237 // Pass all properties down
238 mlt_filter temp = NULL;
239
240 // Loop Variable
241 int i = 0;
242
243 // Number of filters found
244 int count = 0;
245
246 // Loop for all properties
247 for ( i = 0; i < mlt_properties_count( properties ); i ++ )
248 {
249 // Get the name of this property
250 char *name = mlt_properties_get_name( properties, i );
251
252 // If the name does not contain a . and matches filter
253 if ( strchr( name, '.' ) == NULL && !strncmp( name, "filter", 6 ) )
254 {
255 // Strings to hold the id and pass down key
256 char id[ 256 ];
257 char key[ 256 ];
258
259 // Construct id and key
260 sprintf( id, "_filter_%d", count );
261 sprintf( key, "%s.", name );
262
263 // Get the filter
264 temp = mlt_properties_get_data( properties, id, NULL );
265
266 if ( temp != NULL )
267 {
268 mlt_properties_pass( mlt_filter_properties( temp ), properties, key );
269 count ++;
270 }
271 }
272 }
273 }
274
275 // Get the image
276 error = mlt_frame_get_image( frame, image, format, width, height, 1 );
277
278 // Only continue if we have both filter and composite
279 if ( composite != NULL )
280 {
281 // Get the resource of this filter (could be a shape [rectangle/circle] or an alpha provider of choice
282 char *resource = mlt_properties_get( properties, "resource" );
283
284 // Get the old resource in case it's changed
285 char *old_resource = mlt_properties_get( properties, "_old_resource" );
286
287 // String to hold the filter to query on
288 char id[ 256 ];
289
290 // Index to hold the count
291 int i = 0;
292
293 // We will get the 'b frame' from the composite only if it's NULL
294 if ( b_frame == NULL )
295 {
296 // Copy the region
297 b_frame = composite_copy_region( composite, frame, position );
298
299 // Ensure a destructor
300 mlt_properties_set_data( mlt_frame_properties( frame ), name, b_frame, 0, ( mlt_destructor )mlt_frame_close, NULL );
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 apply_filters( 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 {
346 // Special case to ensure that fezzik produces a pixbuf with a NULL constructor
347 resource = "pixbuf";
348
349 // Specify the svg circle
350 mlt_properties_set( properties, "producer.resource", "<svg width='100' height='100'><circle cx='50' cy='50' r='50' fill='black'/></svg>" );
351 }
352
353 // Create the producer
354 producer = mlt_factory_producer( 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( mlt_frame_properties( b_frame ), "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 return error;
399 }
400
401 /** Filter processing.
402 */
403
404 static mlt_frame transition_process( mlt_transition this, mlt_frame a_frame, mlt_frame b_frame )
405 {
406 // Get the properties of the frame
407 mlt_properties properties = mlt_frame_properties( a_frame );
408
409 // Get a unique name to store the frame position
410 char *name = mlt_properties_get( mlt_transition_properties( this ), "_unique_id" );
411
412 // Assign the current position to the name
413 mlt_properties_set_position( properties, name, mlt_frame_get_position( a_frame ) );
414
415 // Push the transition on to the frame
416 mlt_frame_push_service( a_frame, this );
417
418 // Push the b_frame on to the stack
419 mlt_frame_push_frame( a_frame, b_frame );
420
421 // Push the transition method
422 mlt_frame_push_get_image( a_frame, transition_get_image );
423
424 // Return the frame
425 return a_frame;
426 }
427
428 /** Constructor for the transition.
429 */
430
431 mlt_transition transition_region_init( void *arg )
432 {
433 // Create a new transition
434 mlt_transition this = mlt_transition_new( );
435
436 // Further initialisation
437 if ( this != NULL )
438 {
439 // Get the properties from the transition
440 mlt_properties properties = mlt_transition_properties( this );
441
442 // Assign the transition process method
443 this->process = transition_process;
444
445 // Default factory
446 mlt_properties_set( properties, "factory", "fezzik" );
447
448 // Resource defines the shape of the region
449 mlt_properties_set( properties, "resource", arg == NULL ? "rectangle" : arg );
450 }
451
452 // Return the transition
453 return this;
454 }
455