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"
31 static int filter_get_frame( mlt_service this, mlt_frame_ptr frame, int index );
33 /** Initialize a new filter.
35 * \public \memberof mlt_filter_s
36 * \param this a filter
37 * \param child the object of a subclass
38 * \return true if there was an error
41 int mlt_filter_init( mlt_filter this, void *child )
43 mlt_service service = &this->parent;
44 memset( this, 0, sizeof( struct mlt_filter_s ) );
46 if ( mlt_service_init( service, this ) == 0 )
48 mlt_properties properties = MLT_SERVICE_PROPERTIES( service );
50 // Override the get_frame method
51 service->get_frame = filter_get_frame;
53 // Define the destructor
54 service->close = ( mlt_destructor )mlt_filter_close;
55 service->close_object = this;
57 // Default in, out, track properties
58 mlt_properties_set_position( properties, "in", 0 );
59 mlt_properties_set_position( properties, "out", 0 );
60 mlt_properties_set_int( properties, "track", 0 );
67 /** Create a new filter and initialize it.
69 * \public \memberof mlt_filter_s
70 * \return a new filter
73 mlt_filter mlt_filter_new( )
75 mlt_filter this = calloc( 1, sizeof( struct mlt_filter_s ) );
77 mlt_filter_init( this, NULL );
81 /** Get the service class interface.
83 * \public \memberof mlt_filter_s
84 * \param this a filter
85 * \return the service parent class
86 * \see MLT_FILTER_SERVICE
89 mlt_service mlt_filter_service( mlt_filter this )
91 return this != NULL ? &this->parent : NULL;
94 /** Get the filter properties.
96 * \public \memberof mlt_filter_s
97 * \param this a filter
98 * \return the properties list for the filter
99 * \see MLT_FILTER_PROPERTIES
102 mlt_properties mlt_filter_properties( mlt_filter this )
104 return MLT_SERVICE_PROPERTIES( MLT_FILTER_SERVICE( this ) );
107 /** Connect this filter to a producers track. Note that a filter only operates
108 * on a single track, and by default it operates on the entirety of that track.
110 * \public \memberof mlt_filter_s
111 * \param this a filter
112 * \param producer the producer to which to connect this filter
113 * \param index which of potentially multiple producers to this service (0 based)
116 int mlt_filter_connect( mlt_filter this, mlt_service producer, int index )
118 int ret = mlt_service_connect_producer( &this->parent, producer, index );
120 // If the connection was successful, grab the producer, track and reset in/out
123 mlt_properties properties = MLT_SERVICE_PROPERTIES( &this->parent );
124 mlt_properties_set_position( properties, "in", 0 );
125 mlt_properties_set_position( properties, "out", 0 );
126 mlt_properties_set_int( properties, "track", index );
132 /** Set the starting and ending time.
134 * \public \memberof mlt_filter_s
135 * \param this a filter
136 * \param in the time relative to the producer at which start applying the filter
137 * \param out the time relative to the producer at which to stop applying the filter
141 void mlt_filter_set_in_and_out( mlt_filter this, mlt_position in, mlt_position out )
143 mlt_properties properties = MLT_SERVICE_PROPERTIES( &this->parent );
144 mlt_properties_set_position( properties, "in", in );
145 mlt_properties_set_position( properties, "out", out );
148 /** Return the track that this filter is operating on.
150 * \public \memberof mlt_filter_s
151 * \param this a filter
152 * \return true on error
156 int mlt_filter_get_track( mlt_filter this )
158 mlt_properties properties = MLT_SERVICE_PROPERTIES( &this->parent );
159 return mlt_properties_get_int( properties, "track" );
162 /** Get the in point.
164 * \public \memberof mlt_filter_s
165 * \param this a filter
166 * \return the start time for the filter relative to the producer
170 mlt_position mlt_filter_get_in( mlt_filter this )
172 mlt_properties properties = MLT_SERVICE_PROPERTIES( &this->parent );
173 return mlt_properties_get_position( properties, "in" );
176 /** Get the out point.
178 * \public \memberof mlt_filter_s
179 * \param this a filter
180 * \return the ending time for the filter relative to the producer
184 mlt_position mlt_filter_get_out( mlt_filter this )
186 mlt_properties properties = MLT_SERVICE_PROPERTIES( &this->parent );
187 return mlt_properties_get_position( properties, "out" );
190 /** Process the frame.
192 * \public \memberof mlt_filter_s
193 * \param this a filter
194 * \param frame a frame
199 mlt_frame mlt_filter_process( mlt_filter this, mlt_frame frame )
201 int disable = mlt_properties_get_int( MLT_FILTER_PROPERTIES( this ), "disable" );
202 if ( disable || this->process == NULL )
205 return this->process( this, frame );
208 /** Get a frame from this filter.
210 * \private \memberof mlt_filter_s
211 * \param service a service
212 * \param[out] frame a frame by reference
213 * \param index as determined by the producer
214 * \return true on error
218 static int filter_get_frame( mlt_service service, mlt_frame_ptr frame, int index )
220 mlt_filter this = service->child;
222 // Get coords in/out/track
223 int track = mlt_filter_get_track( this );
224 int in = mlt_filter_get_in( this );
225 int out = mlt_filter_get_out( this );
227 // Get the producer this is connected to
228 mlt_service producer = mlt_service_producer( &this->parent );
230 // If the frame request is for this filters track, we need to process it
231 if ( index == track || track == -1 )
233 int ret = mlt_service_get_frame( producer, frame, index );
236 mlt_position position = mlt_frame_get_position( *frame );
237 if ( position >= in && ( out == 0 || position <= out ) )
238 *frame = mlt_filter_process( this, *frame );
243 *frame = mlt_frame_init( service );
249 return mlt_service_get_frame( producer, frame, index );
253 /** Close and destroy the filter.
255 * \public \memberof mlt_filter_s
256 * \param this a filter
260 void mlt_filter_close( mlt_filter this )
262 if ( this != NULL && mlt_properties_dec_ref( MLT_FILTER_PROPERTIES( this ) ) <= 0 )
264 if ( this->close != NULL )
270 this->parent.close = NULL;
271 mlt_service_close( &this->parent );