2 * mlt_filter.c -- abstraction for all filter services
3 * Copyright (C) 2003-2004 Ushodaya Enterprises Limited
4 * Author: Charles Yates <charles.yates@pandora.be>
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2.1 of the License, or (at your option) any later version.
11 * This library 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 GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
21 #include "mlt_filter.h"
22 #include "mlt_frame.h"
28 static int filter_get_frame( mlt_service
this, mlt_frame_ptr frame
, int index
);
30 /** Initialize a new filter.
32 * \public \memberof mlt_filter_s
33 * \param this a filter
34 * \param child the object of a subclass
35 * \return true if there was an error
38 int mlt_filter_init( mlt_filter
this, void *child
)
40 mlt_service service
= &this->parent
;
41 memset( this, 0, sizeof( struct mlt_filter_s
) );
43 if ( mlt_service_init( service
, this ) == 0 )
45 mlt_properties properties
= MLT_SERVICE_PROPERTIES( service
);
47 // Override the get_frame method
48 service
->get_frame
= filter_get_frame
;
50 // Define the destructor
51 service
->close
= ( mlt_destructor
)mlt_filter_close
;
52 service
->close_object
= this;
54 // Default in, out, track properties
55 mlt_properties_set_position( properties
, "in", 0 );
56 mlt_properties_set_position( properties
, "out", 0 );
57 mlt_properties_set_int( properties
, "track", 0 );
64 /** Create a new filter and initialize it.
66 * \public \memberof mlt_filter_s
67 * \return a new filter
70 mlt_filter
mlt_filter_new( )
72 mlt_filter
this = calloc( 1, sizeof( struct mlt_filter_s
) );
74 mlt_filter_init( this, NULL
);
78 /** Get the service class interface.
80 * \public \memberof mlt_filter_s
81 * \param this a filter
82 * \return the service parent class
83 * \see MLT_FILTER_SERVICE
86 mlt_service
mlt_filter_service( mlt_filter
this )
88 return this != NULL ?
&this->parent
: NULL
;
91 /** Get the filter properties.
93 * \public \memberof mlt_filter_s
94 * \param this a filter
95 * \return the properties list for the filter
96 * \see MLT_FILTER_PROPERTIES
99 mlt_properties
mlt_filter_properties( mlt_filter
this )
101 return MLT_SERVICE_PROPERTIES( MLT_FILTER_SERVICE( this ) );
104 /** Connect this filter to a producers track. Note that a filter only operates
105 * on a single track, and by default it operates on the entirety of that track.
107 * \public \memberof mlt_filter_s
108 * \param this a filter
109 * \param producer the producer to which to connect this filter
110 * \param index which of potentially multiple producers to this service (0 based)
113 int mlt_filter_connect( mlt_filter
this, mlt_service producer
, int index
)
115 int ret
= mlt_service_connect_producer( &this->parent
, producer
, index
);
117 // If the connection was successful, grab the producer, track and reset in/out
120 mlt_properties properties
= MLT_SERVICE_PROPERTIES( &this->parent
);
121 mlt_properties_set_position( properties
, "in", 0 );
122 mlt_properties_set_position( properties
, "out", 0 );
123 mlt_properties_set_int( properties
, "track", index
);
129 /** Set the starting and ending time.
131 * \public \memberof mlt_filter_s
132 * \param this a filter
133 * \param in the time relative to the producer at which start applying the filter
134 * \param out the time relative to the producer at which to stop applying the filter
138 void mlt_filter_set_in_and_out( mlt_filter
this, mlt_position in
, mlt_position out
)
140 mlt_properties properties
= MLT_SERVICE_PROPERTIES( &this->parent
);
141 mlt_properties_set_position( properties
, "in", in
);
142 mlt_properties_set_position( properties
, "out", out
);
145 /** Return the track that this filter is operating on.
147 * \public \memberof mlt_filter_s
148 * \param this a filter
149 * \return true on error
153 int mlt_filter_get_track( mlt_filter
this )
155 mlt_properties properties
= MLT_SERVICE_PROPERTIES( &this->parent
);
156 return mlt_properties_get_int( properties
, "track" );
159 /** Get the in point.
161 * \public \memberof mlt_filter_s
162 * \param this a filter
163 * \return the start time for the filter relative to the producer
167 mlt_position
mlt_filter_get_in( mlt_filter
this )
169 mlt_properties properties
= MLT_SERVICE_PROPERTIES( &this->parent
);
170 return mlt_properties_get_position( properties
, "in" );
173 /** Get the out point.
175 * \public \memberof mlt_filter_s
176 * \param this a filter
177 * \return the ending time for the filter relative to the producer
181 mlt_position
mlt_filter_get_out( mlt_filter
this )
183 mlt_properties properties
= MLT_SERVICE_PROPERTIES( &this->parent
);
184 return mlt_properties_get_position( properties
, "out" );
187 /** Process the frame.
189 * \public \memberof mlt_filter_s
190 * \param this a filter
191 * \param frame a frame
196 mlt_frame
mlt_filter_process( mlt_filter
this, mlt_frame frame
)
198 int disable
= mlt_properties_get_int( MLT_FILTER_PROPERTIES( this ), "disable" );
199 if ( disable
|| this->process
== NULL
)
202 return this->process( this, frame
);
205 /** Get a frame from this filter.
207 * \private \memberof mlt_filter_s
208 * \param service a service
209 * \param[out] frame a frame by reference
210 * \param index as determined by the producer
211 * \return true on error
215 static int filter_get_frame( mlt_service service
, mlt_frame_ptr frame
, int index
)
217 mlt_filter
this = service
->child
;
219 // Get coords in/out/track
220 int track
= mlt_filter_get_track( this );
221 int in
= mlt_filter_get_in( this );
222 int out
= mlt_filter_get_out( this );
224 // Get the producer this is connected to
225 mlt_service producer
= mlt_service_producer( &this->parent
);
227 // If the frame request is for this filters track, we need to process it
228 if ( index
== track
|| track
== -1 )
230 int ret
= mlt_service_get_frame( producer
, frame
, index
);
233 mlt_position position
= mlt_frame_get_position( *frame
);
234 if ( position
>= in
&& ( out
== 0 || position
<= out
) )
235 *frame
= mlt_filter_process( this, *frame
);
240 *frame
= mlt_frame_init( service
);
246 return mlt_service_get_frame( producer
, frame
, index
);
250 /** Close and destroy the filter.
252 * \public \memberof mlt_filter_s
253 * \param this a filter
257 void mlt_filter_close( mlt_filter
this )
259 if ( this != NULL
&& mlt_properties_dec_ref( MLT_FILTER_PROPERTIES( this ) ) <= 0 )
261 if ( this->close
!= NULL
)
267 this->parent
.close
= NULL
;
268 mlt_service_close( &this->parent
);