3 * \brief abstraction for all filter services
6 * Copyright (C) 2003-2009 Ushodaya Enterprises Limited
7 * \author Charles Yates <charles.yates@pandora.be>
9 * This library is free software; you can redistribute it and/or
10 * modify it under the terms of the GNU Lesser General Public
11 * License as published by the Free Software Foundation; either
12 * version 2.1 of the License, or (at your option) any later version.
14 * This library is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 * Lesser General Public License for more details.
19 * You should have received a copy of the GNU Lesser General Public
20 * License along with this library; if not, write to the Free Software
21 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
24 #include "mlt_filter.h"
25 #include "mlt_frame.h"
26 #include "mlt_producer.h"
32 static int filter_get_frame( mlt_service self, mlt_frame_ptr frame, int index );
34 /** Initialize a new filter.
36 * \public \memberof mlt_filter_s
37 * \param self a filter
38 * \param child the object of a subclass
39 * \return true if there was an error
42 int mlt_filter_init( mlt_filter self, void *child )
44 mlt_service service = &self->parent;
45 memset( self, 0, sizeof( struct mlt_filter_s ) );
47 if ( mlt_service_init( service, self ) == 0 )
49 mlt_properties properties = MLT_SERVICE_PROPERTIES( service );
51 // Override the get_frame method
52 service->get_frame = filter_get_frame;
54 // Define the destructor
55 service->close = ( mlt_destructor )mlt_filter_close;
56 service->close_object = self;
58 // Default in, out, track properties
59 mlt_properties_set_position( properties, "in", 0 );
60 mlt_properties_set_position( properties, "out", 0 );
61 mlt_properties_set_int( properties, "track", 0 );
68 /** Create a new filter and initialize it.
70 * \public \memberof mlt_filter_s
71 * \return a new filter
74 mlt_filter mlt_filter_new( )
76 mlt_filter self = calloc( 1, sizeof( struct mlt_filter_s ) );
77 if ( self != NULL && mlt_filter_init( self, NULL ) == 0 )
88 /** Get the service class interface.
90 * \public \memberof mlt_filter_s
91 * \param self a filter
92 * \return the service parent class
93 * \see MLT_FILTER_SERVICE
96 mlt_service mlt_filter_service( mlt_filter self )
98 return self != NULL ? &self->parent : NULL;
101 /** Get the filter properties.
103 * \public \memberof mlt_filter_s
104 * \param self a filter
105 * \return the properties list for the filter
106 * \see MLT_FILTER_PROPERTIES
109 mlt_properties mlt_filter_properties( mlt_filter self )
111 return MLT_SERVICE_PROPERTIES( MLT_FILTER_SERVICE( self ) );
114 /** Connect this filter to a producers track. Note that a filter only operates
115 * on a single track, and by default it operates on the entirety of that track.
117 * \public \memberof mlt_filter_s
118 * \param self a filter
119 * \param producer the producer to which to connect this filter
120 * \param index which of potentially multiple producers to this service (0 based)
123 int mlt_filter_connect( mlt_filter self, mlt_service producer, int index )
125 int ret = mlt_service_connect_producer( &self->parent, producer, index );
127 // If the connection was successful, grab the producer, track and reset in/out
130 mlt_properties properties = MLT_SERVICE_PROPERTIES( &self->parent );
131 mlt_properties_set_position( properties, "in", 0 );
132 mlt_properties_set_position( properties, "out", 0 );
133 mlt_properties_set_int( properties, "track", index );
139 /** Set the starting and ending time.
141 * \public \memberof mlt_filter_s
142 * \param self a filter
143 * \param in the time relative to the producer at which start applying the filter
144 * \param out the time relative to the producer at which to stop applying the filter
148 void mlt_filter_set_in_and_out( mlt_filter self, mlt_position in, mlt_position out )
150 mlt_properties properties = MLT_SERVICE_PROPERTIES( &self->parent );
151 mlt_properties_set_position( properties, "in", in );
152 mlt_properties_set_position( properties, "out", out );
155 /** Return the track that this filter is operating on.
157 * \public \memberof mlt_filter_s
158 * \param self a filter
159 * \return true on error
163 int mlt_filter_get_track( mlt_filter self )
165 mlt_properties properties = MLT_SERVICE_PROPERTIES( &self->parent );
166 return mlt_properties_get_int( properties, "track" );
169 /** Get the in point.
171 * \public \memberof mlt_filter_s
172 * \param self a filter
173 * \return the start time for the filter relative to the producer
177 mlt_position mlt_filter_get_in( mlt_filter self )
179 mlt_properties properties = MLT_SERVICE_PROPERTIES( &self->parent );
180 return mlt_properties_get_position( properties, "in" );
183 /** Get the out point.
185 * \public \memberof mlt_filter_s
186 * \param self a filter
187 * \return the ending time for the filter relative to the producer
191 mlt_position mlt_filter_get_out( mlt_filter self )
193 mlt_properties properties = MLT_SERVICE_PROPERTIES( &self->parent );
194 return mlt_properties_get_position( properties, "out" );
197 /** Get the duration.
199 * \public \memberof mlt_filter_s
200 * \param self a filter
201 * \return the duration or zero if unlimited
204 mlt_position mlt_filter_get_length( mlt_filter self )
206 mlt_properties properties = MLT_SERVICE_PROPERTIES( &self->parent );
207 mlt_position in = mlt_properties_get_position( properties, "in" );
208 mlt_position out = mlt_properties_get_position( properties, "out" );
209 return ( out > 0 ) ? ( out - in + 1 ) : 0;
212 /** Get the duration.
214 * This version works with filters with no explicit in and out by getting the
215 * length of the frame's producer.
217 * \public \memberof mlt_filter_s
218 * \param self a filter
219 * \param frame a frame from which to get its producer
220 * \return the duration or zero if unlimited
223 mlt_position mlt_filter_get_length2( mlt_filter self, mlt_frame frame )
225 mlt_properties properties = MLT_SERVICE_PROPERTIES( &self->parent );
226 mlt_position in = mlt_properties_get_position( properties, "in" );
227 mlt_position out = mlt_properties_get_position( properties, "out" );
229 if ( out == 0 && frame )
231 // If always active, use the frame's producer
232 mlt_producer producer = mlt_frame_get_original_producer( frame );
235 producer = mlt_producer_cut_parent( producer );
236 in = mlt_producer_get_in( producer );
237 out = mlt_producer_get_out( producer );
240 return ( out > 0 ) ? ( out - in + 1 ) : 0;
243 /** Get the position within the filter.
245 * The position is relative to the in point.
246 * This will only be valid once mlt_filter_process is called.
248 * \public \memberof mlt_filter_s
249 * \param self a filter
250 * \param frame a frame
251 * \return the position
254 mlt_position mlt_filter_get_position( mlt_filter self, mlt_frame frame )
256 mlt_properties properties = MLT_FILTER_PROPERTIES( self );
257 mlt_position in = mlt_properties_get_position( properties, "in" );
258 const char *unique_id = mlt_properties_get( properties, "_unique_id" );
261 // Make the properties key from unique id
262 snprintf( name, 20, "pos.%s", unique_id );
265 return mlt_properties_get_position( MLT_FRAME_PROPERTIES( frame ), name ) - in;
268 /** Get the percent complete.
270 * This will only be valid once mlt_filter_process is called.
272 * \public \memberof mlt_filter_s
273 * \param self a filter
274 * \param frame a frame
275 * \return the progress in the range 0.0 to 1.0
278 double mlt_filter_get_progress( mlt_filter self, mlt_frame frame )
280 double position = mlt_filter_get_position( self, frame );
281 double length = mlt_filter_get_length2( self, frame );
282 return position / length;
285 /** Process the frame.
287 * When fetching the frame position in a subclass process method, the frame's
288 * position is relative to the filter's producer - not the filter's in point
291 * \public \memberof mlt_filter_s
292 * \param self a filter
293 * \param frame a frame
298 mlt_frame mlt_filter_process( mlt_filter self, mlt_frame frame )
300 mlt_properties properties = MLT_FILTER_PROPERTIES( self );
301 int disable = mlt_properties_get_int( properties, "disable" );
302 const char *unique_id = mlt_properties_get( properties, "_unique_id" );
303 mlt_position position = mlt_frame_get_position( frame );
306 // Make the properties key from unique id
307 snprintf( name, sizeof(name), "pos.%s", unique_id );
308 name[sizeof(name) -1] = '\0';
310 // Save the position on the frame
311 mlt_properties_set_position( MLT_FRAME_PROPERTIES( frame ), name, position );
313 if ( disable || self->process == NULL )
319 // Add a reference to this filter on the frame
320 mlt_properties_inc_ref( MLT_FILTER_PROPERTIES(self) );
321 snprintf( name, sizeof(name), "filter.%s", unique_id );
322 name[sizeof(name) -1] = '\0';
323 mlt_properties_set_data( MLT_FRAME_PROPERTIES(frame), name, self, 0,
324 (mlt_destructor) mlt_filter_close, NULL );
326 return self->process( self, frame );
330 /** Get a frame from this filter.
332 * \private \memberof mlt_filter_s
333 * \param service a service
334 * \param[out] frame a frame by reference
335 * \param index as determined by the producer
336 * \return true on error
340 static int filter_get_frame( mlt_service service, mlt_frame_ptr frame, int index )
342 mlt_filter self = service->child;
344 // Get coords in/out/track
345 int track = mlt_filter_get_track( self );
346 int in = mlt_filter_get_in( self );
347 int out = mlt_filter_get_out( self );
349 // Get the producer this is connected to
350 mlt_service producer = mlt_service_producer( &self->parent );
352 // If the frame request is for this filters track, we need to process it
353 if ( index == track || track == -1 )
355 int ret = mlt_service_get_frame( producer, frame, index );
358 mlt_position position = mlt_frame_get_position( *frame );
359 if ( position >= in && ( out == 0 || position <= out ) )
360 *frame = mlt_filter_process( self, *frame );
365 *frame = mlt_frame_init( service );
371 return mlt_service_get_frame( producer, frame, index );
375 /** Close and destroy the filter.
377 * \public \memberof mlt_filter_s
378 * \param self a filter
382 void mlt_filter_close( mlt_filter self )
384 if ( self != NULL && mlt_properties_dec_ref( MLT_FILTER_PROPERTIES( self ) ) <= 0 )
386 if ( self->close != NULL )
392 self->parent.close = NULL;
393 mlt_service_close( &self->parent );