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 /** Constructor method.
33 int mlt_filter_init( mlt_filter this, void *child )
35 mlt_service service = &this->parent;
36 memset( this, 0, sizeof( struct mlt_filter_s ) );
38 if ( mlt_service_init( service, this ) == 0 )
40 mlt_properties properties = MLT_SERVICE_PROPERTIES( service );
42 // Override the get_frame method
43 service->get_frame = filter_get_frame;
45 // Define the destructor
46 service->close = ( mlt_destructor )mlt_filter_close;
47 service->close_object = this;
49 // Default in, out, track properties
50 mlt_properties_set_position( properties, "in", 0 );
51 mlt_properties_set_position( properties, "out", 0 );
52 mlt_properties_set_int( properties, "track", 0 );
59 /** Create a new filter.
62 mlt_filter mlt_filter_new( )
64 mlt_filter this = calloc( 1, sizeof( struct mlt_filter_s ) );
66 mlt_filter_init( this, NULL );
70 /** Get the service associated to this filter
73 mlt_service mlt_filter_service( mlt_filter this )
75 return this != NULL ? &this->parent : NULL;
78 /** Get the properties associated to this filter.
81 mlt_properties mlt_filter_properties( mlt_filter this )
83 return MLT_SERVICE_PROPERTIES( MLT_FILTER_SERVICE( this ) );
86 /** Connect this filter to a producers track. Note that a filter only operates
87 on a single track, and by default it operates on the entirety of that track.
90 int mlt_filter_connect( mlt_filter this, mlt_service producer, int index )
92 int ret = mlt_service_connect_producer( &this->parent, producer, index );
94 // If the connection was successful, grab the producer, track and reset in/out
97 mlt_properties properties = MLT_SERVICE_PROPERTIES( &this->parent );
98 mlt_properties_set_position( properties, "in", 0 );
99 mlt_properties_set_position( properties, "out", 0 );
100 mlt_properties_set_int( properties, "track", index );
106 /** Tune the in/out points.
109 void mlt_filter_set_in_and_out( mlt_filter this, mlt_position in, mlt_position out )
111 mlt_properties properties = MLT_SERVICE_PROPERTIES( &this->parent );
112 mlt_properties_set_position( properties, "in", in );
113 mlt_properties_set_position( properties, "out", out );
116 /** Return the track that this filter is operating on.
119 int mlt_filter_get_track( mlt_filter this )
121 mlt_properties properties = MLT_SERVICE_PROPERTIES( &this->parent );
122 return mlt_properties_get_int( properties, "track" );
125 /** Get the in point.
128 mlt_position mlt_filter_get_in( mlt_filter this )
130 mlt_properties properties = MLT_SERVICE_PROPERTIES( &this->parent );
131 return mlt_properties_get_position( properties, "in" );
134 /** Get the out point.
137 mlt_position mlt_filter_get_out( mlt_filter this )
139 mlt_properties properties = MLT_SERVICE_PROPERTIES( &this->parent );
140 return mlt_properties_get_position( properties, "out" );
143 /** Process the frame.
146 mlt_frame mlt_filter_process( mlt_filter this, mlt_frame frame )
148 int disable = mlt_properties_get_int( MLT_FILTER_PROPERTIES( this ), "disable" );
149 if ( disable || this->process == NULL )
152 return this->process( this, frame );
155 /** Get a frame from this filter.
158 static int filter_get_frame( mlt_service service, mlt_frame_ptr frame, int index )
160 mlt_filter this = service->child;
162 // Get coords in/out/track
163 int track = mlt_filter_get_track( this );
164 int in = mlt_filter_get_in( this );
165 int out = mlt_filter_get_out( this );
167 // Get the producer this is connected to
168 mlt_service producer = mlt_service_producer( &this->parent );
170 // If the frame request is for this filters track, we need to process it
171 if ( index == track || track == -1 )
173 int ret = mlt_service_get_frame( producer, frame, index );
176 mlt_position position = mlt_frame_get_position( *frame );
177 if ( position >= in && ( out == 0 || position <= out ) )
178 *frame = mlt_filter_process( this, *frame );
183 *frame = mlt_frame_init( service );
189 return mlt_service_get_frame( producer, frame, index );
193 /** Close the filter.
196 void mlt_filter_close( mlt_filter this )
198 if ( this != NULL && mlt_properties_dec_ref( MLT_FILTER_PROPERTIES( this ) ) <= 0 )
200 if ( this->close != NULL )
206 this->parent.close = NULL;
207 mlt_service_close( &this->parent );