2 * filter_region.c -- region filter
3 * Copyright (C) 2003-2004 Ushodaya Enterprises Limited
4 * Author: Charles Yates <charles.yates@pandora.be>
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.
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.
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.
21 #include "filter_region.h"
22 #include "transition_composite.h"
24 #include <framework/mlt.h>
30 static int create_instance( mlt_filter this, char *name, char *value, int count )
32 // Return from this function
35 // Duplicate the value
36 char *type = strdup( value );
38 // Pointer to filter argument
39 char *arg = type == NULL ? NULL : strchr( type, ':' );
41 // New filter being created
42 mlt_filter filter = NULL;
44 // Cleanup type and arg
49 filter = mlt_factory_filter( type, arg );
51 // If we have a filter, then initialise and store it
55 mlt_properties properties = mlt_filter_properties( this );
57 // String to hold the property name
60 // String to hold the passdown key
64 sprintf( id, "_filter_%d", count );
67 sprintf( key, "%s.", name );
69 // Pass all the key properties on the filter down
70 mlt_properties_pass( mlt_filter_properties( filter ), properties, key );
72 // Ensure that filter is assigned
73 mlt_properties_set_data( properties, id, filter, 0, ( mlt_destructor )mlt_filter_close, NULL );
77 // Indicate that an error has occurred
84 // Return error condition
91 static int filter_get_image( mlt_frame frame, uint8_t **image, mlt_image_format *format, int *width, int *height, int writable )
93 // Error we will return
96 // Get the watermark filter object
97 mlt_filter this = mlt_frame_pop_service( frame );
99 // Get the properties of the filter
100 mlt_properties properties = mlt_filter_properties( this );
102 // Get the composite from the filter
103 mlt_transition composite = mlt_properties_get_data( properties, "composite", NULL );
105 // Look for the first filter
106 mlt_filter filter = mlt_properties_get_data( properties, "_filter_0", NULL );
108 // Create a composite if we don't have one
109 if ( composite == NULL )
111 // Create composite via the factory
112 composite = mlt_factory_transition( "composite", NULL );
115 if ( composite != NULL )
117 // Get the properties
118 mlt_properties composite_properties = mlt_transition_properties( composite );
120 // We want to ensure that we don't get a wobble...
121 mlt_properties_set( composite_properties, "distort", "true" );
122 mlt_properties_set( composite_properties, "progressive", "1" );
124 // Pass all the composite. properties on the filter down
125 mlt_properties_pass( composite_properties, properties, "composite." );
127 // Register the composite for reuse/destruction
128 mlt_properties_set_data( properties, "composite", composite, 0, ( mlt_destructor )mlt_transition_close, NULL );
133 if ( filter == NULL )
138 // Number of filters created
141 // Loop for all properties
142 for ( i = 0; i < mlt_properties_count( properties ); i ++ )
144 // Get the name of this property
145 char *name = mlt_properties_get_name( properties, i );
147 // If the name does not contain a . and matches filter
148 if ( strchr( name, '.' ) == NULL && !strncmp( name, "filter", 6 ) )
150 // Get the filter constructor
151 char *value = mlt_properties_get_value( properties, i );
153 // Create an instance
154 if ( create_instance( this, name, value, count ) == 0 )
161 error = mlt_frame_get_image( frame, image, format, width, height, 1 );
163 // Only continue if we have both filter and composite
164 if ( composite != NULL && filter != NULL )
166 // String to hold the filter to query on
169 // Index to hold the count
172 // We will get the 'b frame' from the composite
173 mlt_frame b_frame = composite_copy_region( composite, frame );
175 // Make sure the filter is in the correct position
176 while ( filter != NULL )
179 mlt_filter_process( filter, b_frame );
181 // Generate the key for the next
182 sprintf( id, "_filter_%d", ++ i );
184 // Get the next filter
185 filter = mlt_properties_get_data( properties, id, NULL );
188 // Get the b frame and process with composite if successful
189 mlt_transition_process( composite, frame, b_frame );
192 error = mlt_frame_get_image( frame, image, format, width, height, 0 );
195 mlt_frame_close( b_frame );
201 /** Filter processing.
204 static mlt_frame filter_process( mlt_filter this, mlt_frame frame )
206 // Push the filter on to the frame
207 mlt_frame_push_service( frame, this );
209 // Push the filter method
210 mlt_frame_push_get_image( frame, filter_get_image );
216 /** Constructor for the filter.
219 mlt_filter filter_region_init( void *arg )
221 // Create a new filter
222 mlt_filter this = mlt_filter_new( );
224 // Further initialisation
227 // Get the properties from the filter
228 mlt_properties properties = mlt_filter_properties( this );
230 // Assign the filter process method
231 this->process = filter_process;
233 // Resource defines the shape of the region
234 mlt_properties_set( properties, "resource", arg == NULL ? "rectangle" : arg );