1e2f8246e680ffac2db8abe3b84de39a08701cae
[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 /** Do it :-).
133 */
134
135 static int transition_get_image( mlt_frame frame, uint8_t **image, mlt_image_format *format, int *width, int *height, int writable )
136 {
137 // Error we will return
138 int error = 0;
139
140 // We will get the 'b frame' from the frame stack
141 mlt_frame b_frame = mlt_frame_pop_frame( frame );
142
143 // Get the watermark transition object
144 mlt_transition this = mlt_frame_pop_service( frame );
145
146 // Get the properties of the transition
147 mlt_properties properties = mlt_transition_properties( this );
148
149 // Get the composite from the transition
150 mlt_transition composite = mlt_properties_get_data( properties, "composite", NULL );
151
152 // Look for the first filter
153 mlt_filter filter = mlt_properties_get_data( properties, "_filter_0", NULL );
154
155 // Get the unique id of the filter (used to reacquire the producer position)
156 char *name = mlt_properties_get( properties, "_unique_id" );
157
158 // Get the original producer position
159 mlt_position position = mlt_properties_get_position( mlt_frame_properties( frame ), name );
160
161 // Create a composite if we don't have one
162 if ( composite == NULL )
163 {
164 // Create composite via the factory
165 composite = mlt_factory_transition( "composite", NULL );
166
167 // If we have one
168 if ( composite != NULL )
169 {
170 // Get the properties
171 mlt_properties composite_properties = mlt_transition_properties( composite );
172
173 // We want to ensure that we don't get a wobble...
174 mlt_properties_set( composite_properties, "distort", "true" );
175 mlt_properties_set( composite_properties, "progressive", "1" );
176
177 // Pass all the composite. properties on the transition down
178 mlt_properties_pass( composite_properties, properties, "composite." );
179
180 // Register the composite for reuse/destruction
181 mlt_properties_set_data( properties, "composite", composite, 0, ( mlt_destructor )mlt_transition_close, NULL );
182 }
183 }
184 else
185 {
186 // Pass all current properties down
187 mlt_properties composite_properties = mlt_transition_properties( composite );
188 mlt_properties_pass( composite_properties, properties, "composite." );
189 }
190
191 // Create filters
192 if ( filter == NULL )
193 {
194 // Loop Variable
195 int i = 0;
196
197 // Number of filters created
198 int count = 0;
199
200 // Loop for all properties
201 for ( i = 0; i < mlt_properties_count( properties ); i ++ )
202 {
203 // Get the name of this property
204 char *name = mlt_properties_get_name( properties, i );
205
206 // If the name does not contain a . and matches filter
207 if ( strchr( name, '.' ) == NULL && !strncmp( name, "filter", 6 ) )
208 {
209 // Get the filter constructor
210 char *value = mlt_properties_get_value( properties, i );
211
212 // Create an instance
213 if ( create_instance( this, name, value, count ) == 0 )
214 count ++;
215 }
216 }
217 }
218 else
219 {
220 // Pass all properties down
221 mlt_filter temp = NULL;
222
223 // Loop Variable
224 int i = 0;
225
226 // Number of filters found
227 int count = 0;
228
229 // Loop for all properties
230 for ( i = 0; i < mlt_properties_count( properties ); i ++ )
231 {
232 // Get the name of this property
233 char *name = mlt_properties_get_name( properties, i );
234
235 // If the name does not contain a . and matches filter
236 if ( strchr( name, '.' ) == NULL && !strncmp( name, "filter", 6 ) )
237 {
238 // Strings to hold the id and pass down key
239 char id[ 256 ];
240 char key[ 256 ];
241
242 // Construct id and key
243 sprintf( id, "_filter_%d", count );
244 sprintf( key, "%s.", name );
245
246 // Get the filter
247 temp = mlt_properties_get_data( properties, id, NULL );
248
249 if ( temp != NULL )
250 {
251 mlt_properties_pass( mlt_filter_properties( temp ), properties, key );
252 count ++;
253 }
254 }
255 }
256 }
257
258 // Get the image
259 error = mlt_frame_get_image( frame, image, format, width, height, 1 );
260
261 // Only continue if we have both filter and composite
262 if ( composite != NULL )
263 {
264 // Get the resource of this filter (could be a shape [rectangle/circle] or an alpha provider of choice
265 char *resource = mlt_properties_get( properties, "resource" );
266
267 // Get the old resource in case it's changed
268 char *old_resource = mlt_properties_get( properties, "_old_resource" );
269
270 // String to hold the filter to query on
271 char id[ 256 ];
272
273 // Index to hold the count
274 int i = 0;
275
276 // We will get the 'b frame' from the composite only if it's NULL
277 if ( b_frame == NULL )
278 {
279 // Copy the region
280 b_frame = composite_copy_region( composite, frame, position );
281
282 // Ensure a destructor
283 mlt_properties_set_data( mlt_frame_properties( frame ), name, b_frame, 0, ( mlt_destructor )mlt_frame_close, NULL );
284 }
285
286 // Make sure the filter is in the correct position
287 while ( filter != NULL )
288 {
289 // Stack this filter
290 if ( mlt_properties_get_int( mlt_filter_properties( filter ), "off" ) == 0 )
291 mlt_filter_process( filter, b_frame );
292
293 // Generate the key for the next
294 sprintf( id, "_filter_%d", ++ i );
295
296 // Get the next filter
297 filter = mlt_properties_get_data( properties, id, NULL );
298 }
299
300 // Allow filters to be attached to a region filter
301 filter = mlt_properties_get_data( properties, "_region_filter", NULL );
302 if ( filter != NULL )
303 mlt_service_apply_filters( mlt_filter_service( filter ), b_frame, 0 );
304
305 // Hmm - this is probably going to go wrong....
306 mlt_frame_set_position( frame, position );
307
308 // Get the b frame and process with composite if successful
309 mlt_transition_process( composite, frame, b_frame );
310
311 // If we have a shape producer copy the alpha mask from the shape frame to the b_frame
312 if ( strcmp( resource, "rectangle" ) != 0 )
313 {
314 // Get the producer from the transition
315 mlt_producer producer = mlt_properties_get_data( properties, "producer", NULL );
316
317 // If We have no producer then create one
318 if ( producer == NULL || ( old_resource != NULL && strcmp( resource, old_resource ) ) )
319 {
320 // Get the factory producer service
321 char *factory = mlt_properties_get( properties, "factory" );
322
323 // Store the old resource
324 mlt_properties_set( properties, "_old_resource", resource );
325
326 // Special case circle resource
327 if ( strcmp( resource, "circle" ) == 0 )
328 {
329 // Special case to ensure that fezzik produces a pixbuf with a NULL constructor
330 resource = "pixbuf";
331
332 // Specify the svg circle
333 mlt_properties_set( properties, "producer.resource", "<svg width='100' height='100'><circle cx='50' cy='50' r='50' fill='black'/></svg>" );
334 }
335
336 // Create the producer
337 producer = mlt_factory_producer( factory, resource );
338
339 // If we have one
340 if ( producer != NULL )
341 {
342 // Get the producer properties
343 mlt_properties producer_properties = mlt_producer_properties( producer );
344
345 // Ensure that we loop
346 mlt_properties_set( producer_properties, "eof", "loop" );
347
348 // Now pass all producer. properties on the transition down
349 mlt_properties_pass( producer_properties, properties, "producer." );
350
351 // Register the producer for reuse/destruction
352 mlt_properties_set_data( properties, "producer", producer, 0, ( mlt_destructor )mlt_producer_close, NULL );
353 }
354 }
355
356 // Now use the shape producer
357 if ( producer != NULL )
358 {
359 // We will get the alpha frame from the producer
360 mlt_frame shape_frame = NULL;
361
362 // Make sure the producer is in the correct position
363 mlt_producer_seek( producer, position );
364
365 // Get the shape frame
366 if ( mlt_service_get_frame( mlt_producer_service( producer ), &shape_frame, 0 ) == 0 )
367 {
368 // Ensure that the shape frame will be closed
369 mlt_properties_set_data( mlt_frame_properties( b_frame ), "shape_frame", shape_frame, 0, ( mlt_destructor )mlt_frame_close, NULL );
370
371 // Specify the callback for evaluation
372 b_frame->get_alpha_mask = filter_get_alpha_mask;
373 }
374 }
375 }
376
377 // Get the image
378 error = mlt_frame_get_image( frame, image, format, width, height, 0 );
379 }
380
381 return error;
382 }
383
384 /** Filter processing.
385 */
386
387 static mlt_frame transition_process( mlt_transition this, mlt_frame a_frame, mlt_frame b_frame )
388 {
389 // Get the properties of the frame
390 mlt_properties properties = mlt_frame_properties( a_frame );
391
392 // Get a unique name to store the frame position
393 char *name = mlt_properties_get( mlt_transition_properties( this ), "_unique_id" );
394
395 // Assign the current position to the name
396 mlt_properties_set_position( properties, name, mlt_frame_get_position( a_frame ) );
397
398 // Push the transition on to the frame
399 mlt_frame_push_service( a_frame, this );
400
401 // Push the b_frame on to the stack
402 mlt_frame_push_frame( a_frame, b_frame );
403
404 // Push the transition method
405 mlt_frame_push_get_image( a_frame, transition_get_image );
406
407 // Return the frame
408 return a_frame;
409 }
410
411 /** Constructor for the transition.
412 */
413
414 mlt_transition transition_region_init( void *arg )
415 {
416 // Create a new transition
417 mlt_transition this = mlt_transition_new( );
418
419 // Further initialisation
420 if ( this != NULL )
421 {
422 // Get the properties from the transition
423 mlt_properties properties = mlt_transition_properties( this );
424
425 // Assign the transition process method
426 this->process = transition_process;
427
428 // Default factory
429 mlt_properties_set( properties, "factory", "fezzik" );
430
431 // Resource defines the shape of the region
432 mlt_properties_set( properties, "resource", arg == NULL ? "rectangle" : arg );
433 }
434
435 // Return the transition
436 return this;
437 }
438