Add doxygen documentation for mlt_profile, mlt_pool, mlt_repository, and mlt_factory.
[melted] / src / framework / mlt_filter.c
1 /**
2 * \file mlt_filter.c
3 * \brief abstraction for all filter services
4 * \see mlt_filter_s
5 *
6 * Copyright (C) 2003-2009 Ushodaya Enterprises Limited
7 * \author Charles Yates <charles.yates@pandora.be>
8 *
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.
13 *
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.
18 *
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
22 */
23
24 #include "mlt_filter.h"
25 #include "mlt_frame.h"
26
27 #include <stdio.h>
28 #include <stdlib.h>
29 #include <string.h>
30
31 static int filter_get_frame( mlt_service this, mlt_frame_ptr frame, int index );
32
33 /** Initialize a new filter.
34 *
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
39 */
40
41 int mlt_filter_init( mlt_filter this, void *child )
42 {
43 mlt_service service = &this->parent;
44 memset( this, 0, sizeof( struct mlt_filter_s ) );
45 this->child = child;
46 if ( mlt_service_init( service, this ) == 0 )
47 {
48 mlt_properties properties = MLT_SERVICE_PROPERTIES( service );
49
50 // Override the get_frame method
51 service->get_frame = filter_get_frame;
52
53 // Define the destructor
54 service->close = ( mlt_destructor )mlt_filter_close;
55 service->close_object = this;
56
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 );
61
62 return 0;
63 }
64 return 1;
65 }
66
67 /** Create a new filter and initialize it.
68 *
69 * \public \memberof mlt_filter_s
70 * \return a new filter
71 */
72
73 mlt_filter mlt_filter_new( )
74 {
75 mlt_filter this = calloc( 1, sizeof( struct mlt_filter_s ) );
76 if ( this != NULL )
77 mlt_filter_init( this, NULL );
78 return this;
79 }
80
81 /** Get the service class interface.
82 *
83 * \public \memberof mlt_filter_s
84 * \param this a filter
85 * \return the service parent class
86 * \see MLT_FILTER_SERVICE
87 */
88
89 mlt_service mlt_filter_service( mlt_filter this )
90 {
91 return this != NULL ? &this->parent : NULL;
92 }
93
94 /** Get the filter properties.
95 *
96 * \public \memberof mlt_filter_s
97 * \param this a filter
98 * \return the properties list for the filter
99 * \see MLT_FILTER_PROPERTIES
100 */
101
102 mlt_properties mlt_filter_properties( mlt_filter this )
103 {
104 return MLT_SERVICE_PROPERTIES( MLT_FILTER_SERVICE( this ) );
105 }
106
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.
109 *
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)
114 */
115
116 int mlt_filter_connect( mlt_filter this, mlt_service producer, int index )
117 {
118 int ret = mlt_service_connect_producer( &this->parent, producer, index );
119
120 // If the connection was successful, grab the producer, track and reset in/out
121 if ( ret == 0 )
122 {
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 );
127 }
128
129 return ret;
130 }
131
132 /** Set the starting and ending time.
133 *
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
138 */
139
140
141 void mlt_filter_set_in_and_out( mlt_filter this, mlt_position in, mlt_position out )
142 {
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 );
146 }
147
148 /** Return the track that this filter is operating on.
149 *
150 * \public \memberof mlt_filter_s
151 * \param this a filter
152 * \return true on error
153 */
154
155
156 int mlt_filter_get_track( mlt_filter this )
157 {
158 mlt_properties properties = MLT_SERVICE_PROPERTIES( &this->parent );
159 return mlt_properties_get_int( properties, "track" );
160 }
161
162 /** Get the in point.
163 *
164 * \public \memberof mlt_filter_s
165 * \param this a filter
166 * \return the start time for the filter relative to the producer
167 */
168
169
170 mlt_position mlt_filter_get_in( mlt_filter this )
171 {
172 mlt_properties properties = MLT_SERVICE_PROPERTIES( &this->parent );
173 return mlt_properties_get_position( properties, "in" );
174 }
175
176 /** Get the out point.
177 *
178 * \public \memberof mlt_filter_s
179 * \param this a filter
180 * \return the ending time for the filter relative to the producer
181 */
182
183
184 mlt_position mlt_filter_get_out( mlt_filter this )
185 {
186 mlt_properties properties = MLT_SERVICE_PROPERTIES( &this->parent );
187 return mlt_properties_get_position( properties, "out" );
188 }
189
190 /** Process the frame.
191 *
192 * \public \memberof mlt_filter_s
193 * \param this a filter
194 * \param frame a frame
195 * \return a frame
196 */
197
198
199 mlt_frame mlt_filter_process( mlt_filter this, mlt_frame frame )
200 {
201 int disable = mlt_properties_get_int( MLT_FILTER_PROPERTIES( this ), "disable" );
202 if ( disable || this->process == NULL )
203 return frame;
204 else
205 return this->process( this, frame );
206 }
207
208 /** Get a frame from this filter.
209 *
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
215 */
216
217
218 static int filter_get_frame( mlt_service service, mlt_frame_ptr frame, int index )
219 {
220 mlt_filter this = service->child;
221
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 );
226
227 // Get the producer this is connected to
228 mlt_service producer = mlt_service_producer( &this->parent );
229
230 // If the frame request is for this filters track, we need to process it
231 if ( index == track || track == -1 )
232 {
233 int ret = mlt_service_get_frame( producer, frame, index );
234 if ( ret == 0 )
235 {
236 mlt_position position = mlt_frame_get_position( *frame );
237 if ( position >= in && ( out == 0 || position <= out ) )
238 *frame = mlt_filter_process( this, *frame );
239 return 0;
240 }
241 else
242 {
243 *frame = mlt_frame_init( service );
244 return 0;
245 }
246 }
247 else
248 {
249 return mlt_service_get_frame( producer, frame, index );
250 }
251 }
252
253 /** Close and destroy the filter.
254 *
255 * \public \memberof mlt_filter_s
256 * \param this a filter
257 */
258
259
260 void mlt_filter_close( mlt_filter this )
261 {
262 if ( this != NULL && mlt_properties_dec_ref( MLT_FILTER_PROPERTIES( this ) ) <= 0 )
263 {
264 if ( this->close != NULL )
265 {
266 this->close( this );
267 }
268 else
269 {
270 this->parent.close = NULL;
271 mlt_service_close( &this->parent );
272 }
273 free( this );
274 }
275 }