regionalised fx part 1
[melted] / 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 /** Do it :-).
89 */
90
91 static int filter_get_image( mlt_frame frame, uint8_t **image, mlt_image_format *format, int *width, int *height, int writable )
92 {
93 // Error we will return
94 int error = 0;
95
96 // Get the watermark filter object
97 mlt_filter this = mlt_frame_pop_service( frame );
98
99 // Get the properties of the filter
100 mlt_properties properties = mlt_filter_properties( this );
101
102 // Get the composite from the filter
103 mlt_transition composite = mlt_properties_get_data( properties, "composite", NULL );
104
105 // Look for the first filter
106 mlt_filter filter = mlt_properties_get_data( properties, "_filter_0", NULL );
107
108 // Create a composite if we don't have one
109 if ( composite == NULL )
110 {
111 // Create composite via the factory
112 composite = mlt_factory_transition( "composite", NULL );
113
114 // If we have one
115 if ( composite != NULL )
116 {
117 // Get the properties
118 mlt_properties composite_properties = mlt_transition_properties( composite );
119
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" );
123
124 // Pass all the composite. properties on the filter down
125 mlt_properties_pass( composite_properties, properties, "composite." );
126
127 // Register the composite for reuse/destruction
128 mlt_properties_set_data( properties, "composite", composite, 0, ( mlt_destructor )mlt_transition_close, NULL );
129 }
130 }
131
132 // Create filters
133 if ( filter == NULL )
134 {
135 // Loop Variable
136 int i = 0;
137
138 // Number of filters created
139 int count = 0;
140
141 // Loop for all properties
142 for ( i = 0; i < mlt_properties_count( properties ); i ++ )
143 {
144 // Get the name of this property
145 char *name = mlt_properties_get_name( properties, i );
146
147 // If the name does not contain a . and matches filter
148 if ( strchr( name, '.' ) == NULL && !strncmp( name, "filter", 6 ) )
149 {
150 // Get the filter constructor
151 char *value = mlt_properties_get_value( properties, i );
152
153 // Create an instance
154 if ( create_instance( this, name, value, count ) == 0 )
155 count ++;
156 }
157 }
158 }
159
160 // Get the image
161 error = mlt_frame_get_image( frame, image, format, width, height, 1 );
162
163 // Only continue if we have both filter and composite
164 if ( composite != NULL && filter != NULL )
165 {
166 // String to hold the filter to query on
167 char id[ 256 ];
168
169 // Index to hold the count
170 int i = 0;
171
172 // We will get the 'b frame' from the composite
173 mlt_frame b_frame = composite_copy_region( composite, frame );
174
175 // Make sure the filter is in the correct position
176 while ( filter != NULL )
177 {
178 // Stack this filter
179 mlt_filter_process( filter, b_frame );
180
181 // Generate the key for the next
182 sprintf( id, "_filter_%d", ++ i );
183
184 // Get the next filter
185 filter = mlt_properties_get_data( properties, id, NULL );
186 }
187
188 // Get the b frame and process with composite if successful
189 mlt_transition_process( composite, frame, b_frame );
190
191 // Get the image
192 error = mlt_frame_get_image( frame, image, format, width, height, 0 );
193
194 // Close the b frame
195 mlt_frame_close( b_frame );
196 }
197
198 return error;
199 }
200
201 /** Filter processing.
202 */
203
204 static mlt_frame filter_process( mlt_filter this, mlt_frame frame )
205 {
206 // Push the filter on to the frame
207 mlt_frame_push_service( frame, this );
208
209 // Push the filter method
210 mlt_frame_push_get_image( frame, filter_get_image );
211
212 // Return the frame
213 return frame;
214 }
215
216 /** Constructor for the filter.
217 */
218
219 mlt_filter filter_region_init( void *arg )
220 {
221 // Create a new filter
222 mlt_filter this = mlt_filter_new( );
223
224 // Further initialisation
225 if ( this != NULL )
226 {
227 // Get the properties from the filter
228 mlt_properties properties = mlt_filter_properties( this );
229
230 // Assign the filter process method
231 this->process = filter_process;
232
233 // Resource defines the shape of the region
234 mlt_properties_set( properties, "resource", arg == NULL ? "rectangle" : arg );
235 }
236
237 // Return the filter
238 return this;
239 }
240