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 position within the filter.
208 * The position is relative to the in point.
209 * This will only be valid once mlt_filter_process is called.
211 * \public \memberof mlt_filter_s
212 * \param self a filter
213 * \param frame a frame
214 * \return the position
217 mlt_position mlt_filter_get_position( mlt_filter self, mlt_frame frame )
219 mlt_properties properties = MLT_FILTER_PROPERTIES( self );
220 mlt_position in = mlt_properties_get_position( properties, "in" );
221 const char *unique_id = mlt_properties_get( properties, "_unique_id" );
224 // Make the properties key from unique id
225 strcpy( name, "pos." );
226 strcat( name, unique_id );
228 return mlt_properties_get_position( MLT_FRAME_PROPERTIES( frame ), name ) - in;
231 /** Get the percent complete.
233 * This will only be valid once mlt_filter_process is called.
235 * \public \memberof mlt_filter_s
236 * \param self a filter
237 * \param frame a frame
238 * \return the progress in the range 0.0 to 1.0
241 double mlt_filter_get_progress( mlt_filter self, mlt_frame frame )
244 mlt_position in = mlt_filter_get_in( self );
245 mlt_position out = mlt_filter_get_out( self );
249 // If always active, use the frame's producer
250 mlt_producer producer = mlt_frame_get_original_producer( frame );
253 in = mlt_producer_get_in( producer );
254 out = mlt_producer_get_out( producer );
259 mlt_position position = mlt_filter_get_position( self, frame );
260 progress = ( double ) position / ( double ) ( out - in + 1 );
265 /** Process the frame.
267 * When fetching the frame position in a subclass process method, the frame's
268 * position is relative to the filter's producer - not the filter's in point
271 * \public \memberof mlt_filter_s
272 * \param self a filter
273 * \param frame a frame
278 mlt_frame mlt_filter_process( mlt_filter self, mlt_frame frame )
280 mlt_properties properties = MLT_FILTER_PROPERTIES( self );
281 int disable = mlt_properties_get_int( properties, "disable" );
282 const char *unique_id = mlt_properties_get( properties, "_unique_id" );
283 mlt_position position = mlt_frame_get_position( frame );
286 // Make the properties key from unique id
287 strcpy( name, "pos." );
288 strcat( name, unique_id );
290 // Save the position on the frame
291 mlt_properties_set_position( MLT_FRAME_PROPERTIES( frame ), name, position );
293 if ( disable || self->process == NULL )
296 return self->process( self, frame );
299 /** Get a frame from this filter.
301 * \private \memberof mlt_filter_s
302 * \param service a service
303 * \param[out] frame a frame by reference
304 * \param index as determined by the producer
305 * \return true on error
309 static int filter_get_frame( mlt_service service, mlt_frame_ptr frame, int index )
311 mlt_filter self = service->child;
313 // Get coords in/out/track
314 int track = mlt_filter_get_track( self );
315 int in = mlt_filter_get_in( self );
316 int out = mlt_filter_get_out( self );
318 // Get the producer this is connected to
319 mlt_service producer = mlt_service_producer( &self->parent );
321 // If the frame request is for this filters track, we need to process it
322 if ( index == track || track == -1 )
324 int ret = mlt_service_get_frame( producer, frame, index );
327 mlt_position position = mlt_frame_get_position( *frame );
328 if ( position >= in && ( out == 0 || position <= out ) )
329 *frame = mlt_filter_process( self, *frame );
334 *frame = mlt_frame_init( service );
340 return mlt_service_get_frame( producer, frame, index );
344 /** Close and destroy the filter.
346 * \public \memberof mlt_filter_s
347 * \param self a filter
351 void mlt_filter_close( mlt_filter self )
353 if ( self != NULL && mlt_properties_dec_ref( MLT_FILTER_PROPERTIES( self ) ) <= 0 )
355 if ( self->close != NULL )
361 self->parent.close = NULL;
362 mlt_service_close( &self->parent );