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 ) );
78 mlt_filter_init( self, NULL );
82 /** Get the service class interface.
84 * \public \memberof mlt_filter_s
85 * \param self a filter
86 * \return the service parent class
87 * \see MLT_FILTER_SERVICE
90 mlt_service mlt_filter_service( mlt_filter self )
92 return self != NULL ? &self->parent : NULL;
95 /** Get the filter properties.
97 * \public \memberof mlt_filter_s
98 * \param self a filter
99 * \return the properties list for the filter
100 * \see MLT_FILTER_PROPERTIES
103 mlt_properties mlt_filter_properties( mlt_filter self )
105 return MLT_SERVICE_PROPERTIES( MLT_FILTER_SERVICE( self ) );
108 /** Connect this filter to a producers track. Note that a filter only operates
109 * on a single track, and by default it operates on the entirety of that track.
111 * \public \memberof mlt_filter_s
112 * \param self a filter
113 * \param producer the producer to which to connect this filter
114 * \param index which of potentially multiple producers to this service (0 based)
117 int mlt_filter_connect( mlt_filter self, mlt_service producer, int index )
119 int ret = mlt_service_connect_producer( &self->parent, producer, index );
121 // If the connection was successful, grab the producer, track and reset in/out
124 mlt_properties properties = MLT_SERVICE_PROPERTIES( &self->parent );
125 mlt_properties_set_position( properties, "in", 0 );
126 mlt_properties_set_position( properties, "out", 0 );
127 mlt_properties_set_int( properties, "track", index );
133 /** Set the starting and ending time.
135 * \public \memberof mlt_filter_s
136 * \param self a filter
137 * \param in the time relative to the producer at which start applying the filter
138 * \param out the time relative to the producer at which to stop applying the filter
142 void mlt_filter_set_in_and_out( mlt_filter self, mlt_position in, mlt_position out )
144 mlt_properties properties = MLT_SERVICE_PROPERTIES( &self->parent );
145 mlt_properties_set_position( properties, "in", in );
146 mlt_properties_set_position( properties, "out", out );
149 /** Return the track that this filter is operating on.
151 * \public \memberof mlt_filter_s
152 * \param self a filter
153 * \return true on error
157 int mlt_filter_get_track( mlt_filter self )
159 mlt_properties properties = MLT_SERVICE_PROPERTIES( &self->parent );
160 return mlt_properties_get_int( properties, "track" );
163 /** Get the in point.
165 * \public \memberof mlt_filter_s
166 * \param self a filter
167 * \return the start time for the filter relative to the producer
171 mlt_position mlt_filter_get_in( mlt_filter self )
173 mlt_properties properties = MLT_SERVICE_PROPERTIES( &self->parent );
174 return mlt_properties_get_position( properties, "in" );
177 /** Get the out point.
179 * \public \memberof mlt_filter_s
180 * \param self a filter
181 * \return the ending time for the filter relative to the producer
185 mlt_position mlt_filter_get_out( mlt_filter self )
187 mlt_properties properties = MLT_SERVICE_PROPERTIES( &self->parent );
188 return mlt_properties_get_position( properties, "out" );
191 /** Get the duration.
193 * \public \memberof mlt_filter_s
194 * \param self a filter
195 * \return the duration or zero if unlimited
198 mlt_position mlt_filter_get_length( mlt_filter self )
200 mlt_properties properties = MLT_SERVICE_PROPERTIES( &self->parent );
201 mlt_position in = mlt_properties_get_position( properties, "in" );
202 mlt_position out = mlt_properties_get_position( properties, "out" );
203 return ( out > 0 ) ? ( out - in + 1 ) : 0;
206 /** Get the duration.
208 * This version works with filters with no explicit in and out by getting the
209 * length of the frame's producer.
211 * \public \memberof mlt_filter_s
212 * \param self a filter
213 * \param frame a frame from which to get its producer
214 * \return the duration or zero if unlimited
217 mlt_position mlt_filter_get_length2( mlt_filter self, mlt_frame frame )
219 mlt_properties properties = MLT_SERVICE_PROPERTIES( &self->parent );
220 mlt_position in = mlt_properties_get_position( properties, "in" );
221 mlt_position out = mlt_properties_get_position( properties, "out" );
223 if ( out == 0 && frame )
225 // If always active, use the frame's producer
226 mlt_producer producer = mlt_frame_get_original_producer( frame );
229 producer = mlt_producer_cut_parent( producer );
230 in = mlt_producer_get_in( producer );
231 out = mlt_producer_get_out( producer );
234 return ( out > 0 ) ? ( out - in + 1 ) : 0;
237 /** Get the position within the filter.
239 * The position is relative to the in point.
240 * This will only be valid once mlt_filter_process is called.
242 * \public \memberof mlt_filter_s
243 * \param self a filter
244 * \param frame a frame
245 * \return the position
248 mlt_position mlt_filter_get_position( mlt_filter self, mlt_frame frame )
250 mlt_properties properties = MLT_FILTER_PROPERTIES( self );
251 mlt_position in = mlt_properties_get_position( properties, "in" );
252 const char *unique_id = mlt_properties_get( properties, "_unique_id" );
255 // Make the properties key from unique id
256 strcpy( name, "pos." );
257 strcat( name, unique_id );
259 return mlt_properties_get_position( MLT_FRAME_PROPERTIES( frame ), name ) - in;
262 /** Get the percent complete.
264 * This will only be valid once mlt_filter_process is called.
266 * \public \memberof mlt_filter_s
267 * \param self a filter
268 * \param frame a frame
269 * \return the progress in the range 0.0 to 1.0
272 double mlt_filter_get_progress( mlt_filter self, mlt_frame frame )
274 double position = mlt_filter_get_position( self, frame );
275 double length = mlt_filter_get_length2( self, frame );
276 return position / length;
279 /** Process the frame.
281 * When fetching the frame position in a subclass process method, the frame's
282 * position is relative to the filter's producer - not the filter's in point
285 * \public \memberof mlt_filter_s
286 * \param self a filter
287 * \param frame a frame
292 mlt_frame mlt_filter_process( mlt_filter self, mlt_frame frame )
294 mlt_properties properties = MLT_FILTER_PROPERTIES( self );
295 int disable = mlt_properties_get_int( properties, "disable" );
296 const char *unique_id = mlt_properties_get( properties, "_unique_id" );
297 mlt_position position = mlt_frame_get_position( frame );
300 // Make the properties key from unique id
301 strcpy( name, "pos." );
302 strcat( name, unique_id );
304 // Save the position on the frame
305 mlt_properties_set_position( MLT_FRAME_PROPERTIES( frame ), name, position );
307 if ( disable || self->process == NULL )
310 return self->process( self, frame );
313 /** Get a frame from this filter.
315 * \private \memberof mlt_filter_s
316 * \param service a service
317 * \param[out] frame a frame by reference
318 * \param index as determined by the producer
319 * \return true on error
323 static int filter_get_frame( mlt_service service, mlt_frame_ptr frame, int index )
325 mlt_filter self = service->child;
327 // Get coords in/out/track
328 int track = mlt_filter_get_track( self );
329 int in = mlt_filter_get_in( self );
330 int out = mlt_filter_get_out( self );
332 // Get the producer this is connected to
333 mlt_service producer = mlt_service_producer( &self->parent );
335 // If the frame request is for this filters track, we need to process it
336 if ( index == track || track == -1 )
338 int ret = mlt_service_get_frame( producer, frame, index );
341 mlt_position position = mlt_frame_get_position( *frame );
342 if ( position >= in && ( out == 0 || position <= out ) )
343 *frame = mlt_filter_process( self, *frame );
348 *frame = mlt_frame_init( service );
354 return mlt_service_get_frame( producer, frame, index );
358 /** Close and destroy the filter.
360 * \public \memberof mlt_filter_s
361 * \param self a filter
365 void mlt_filter_close( mlt_filter self )
367 if ( self != NULL && mlt_properties_dec_ref( MLT_FILTER_PROPERTIES( self ) ) <= 0 )
369 if ( self->close != NULL )
375 self->parent.close = NULL;
376 mlt_service_close( &self->parent );