mlt_filter.[ch], mlt_transition.[ch], mlt_consumer.[ch]: improve doxygen for filter...
[melted] / src / framework / mlt_filter.c
1 /*
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>
5 *
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.
10 *
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.
15 *
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
19 */
20
21 #include "mlt_filter.h"
22 #include "mlt_frame.h"
23
24 #include <stdio.h>
25 #include <stdlib.h>
26 #include <string.h>
27
28 static int filter_get_frame( mlt_service this, mlt_frame_ptr frame, int index );
29
30 /** Initialize a new filter.
31 *
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
36 */
37
38 int mlt_filter_init( mlt_filter this, void *child )
39 {
40 mlt_service service = &this->parent;
41 memset( this, 0, sizeof( struct mlt_filter_s ) );
42 this->child = child;
43 if ( mlt_service_init( service, this ) == 0 )
44 {
45 mlt_properties properties = MLT_SERVICE_PROPERTIES( service );
46
47 // Override the get_frame method
48 service->get_frame = filter_get_frame;
49
50 // Define the destructor
51 service->close = ( mlt_destructor )mlt_filter_close;
52 service->close_object = this;
53
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 );
58
59 return 0;
60 }
61 return 1;
62 }
63
64 /** Create a new filter and initialize it.
65 *
66 * \public \memberof mlt_filter_s
67 * \return a new filter
68 */
69
70 mlt_filter mlt_filter_new( )
71 {
72 mlt_filter this = calloc( 1, sizeof( struct mlt_filter_s ) );
73 if ( this != NULL )
74 mlt_filter_init( this, NULL );
75 return this;
76 }
77
78 /** Get the service class interface.
79 *
80 * \public \memberof mlt_filter_s
81 * \param this a filter
82 * \return the service parent class
83 * \see MLT_FILTER_SERVICE
84 */
85
86 mlt_service mlt_filter_service( mlt_filter this )
87 {
88 return this != NULL ? &this->parent : NULL;
89 }
90
91 /** Get the filter properties.
92 *
93 * \public \memberof mlt_filter_s
94 * \param this a filter
95 * \return the properties list for the filter
96 * \see MLT_FILTER_PROPERTIES
97 */
98
99 mlt_properties mlt_filter_properties( mlt_filter this )
100 {
101 return MLT_SERVICE_PROPERTIES( MLT_FILTER_SERVICE( this ) );
102 }
103
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.
106 *
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)
111 */
112
113 int mlt_filter_connect( mlt_filter this, mlt_service producer, int index )
114 {
115 int ret = mlt_service_connect_producer( &this->parent, producer, index );
116
117 // If the connection was successful, grab the producer, track and reset in/out
118 if ( ret == 0 )
119 {
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 );
124 }
125
126 return ret;
127 }
128
129 /** Set the starting and ending time.
130 *
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
135 */
136
137
138 void mlt_filter_set_in_and_out( mlt_filter this, mlt_position in, mlt_position out )
139 {
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 );
143 }
144
145 /** Return the track that this filter is operating on.
146 *
147 * \public \memberof mlt_filter_s
148 * \param this a filter
149 * \return true on error
150 */
151
152
153 int mlt_filter_get_track( mlt_filter this )
154 {
155 mlt_properties properties = MLT_SERVICE_PROPERTIES( &this->parent );
156 return mlt_properties_get_int( properties, "track" );
157 }
158
159 /** Get the in point.
160 *
161 * \public \memberof mlt_filter_s
162 * \param this a filter
163 * \return the start time for the filter relative to the producer
164 */
165
166
167 mlt_position mlt_filter_get_in( mlt_filter this )
168 {
169 mlt_properties properties = MLT_SERVICE_PROPERTIES( &this->parent );
170 return mlt_properties_get_position( properties, "in" );
171 }
172
173 /** Get the out point.
174 *
175 * \public \memberof mlt_filter_s
176 * \param this a filter
177 * \return the ending time for the filter relative to the producer
178 */
179
180
181 mlt_position mlt_filter_get_out( mlt_filter this )
182 {
183 mlt_properties properties = MLT_SERVICE_PROPERTIES( &this->parent );
184 return mlt_properties_get_position( properties, "out" );
185 }
186
187 /** Process the frame.
188 *
189 * \public \memberof mlt_filter_s
190 * \param this a filter
191 * \param frame a frame
192 * \return a frame
193 */
194
195
196 mlt_frame mlt_filter_process( mlt_filter this, mlt_frame frame )
197 {
198 int disable = mlt_properties_get_int( MLT_FILTER_PROPERTIES( this ), "disable" );
199 if ( disable || this->process == NULL )
200 return frame;
201 else
202 return this->process( this, frame );
203 }
204
205 /** Get a frame from this filter.
206 *
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
212 */
213
214
215 static int filter_get_frame( mlt_service service, mlt_frame_ptr frame, int index )
216 {
217 mlt_filter this = service->child;
218
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 );
223
224 // Get the producer this is connected to
225 mlt_service producer = mlt_service_producer( &this->parent );
226
227 // If the frame request is for this filters track, we need to process it
228 if ( index == track || track == -1 )
229 {
230 int ret = mlt_service_get_frame( producer, frame, index );
231 if ( ret == 0 )
232 {
233 mlt_position position = mlt_frame_get_position( *frame );
234 if ( position >= in && ( out == 0 || position <= out ) )
235 *frame = mlt_filter_process( this, *frame );
236 return 0;
237 }
238 else
239 {
240 *frame = mlt_frame_init( service );
241 return 0;
242 }
243 }
244 else
245 {
246 return mlt_service_get_frame( producer, frame, index );
247 }
248 }
249
250 /** Close and destroy the filter.
251 *
252 * \public \memberof mlt_filter_s
253 * \param this a filter
254 */
255
256
257 void mlt_filter_close( mlt_filter this )
258 {
259 if ( this != NULL && mlt_properties_dec_ref( MLT_FILTER_PROPERTIES( this ) ) <= 0 )
260 {
261 if ( this->close != NULL )
262 {
263 this->close( this );
264 }
265 else
266 {
267 this->parent.close = NULL;
268 mlt_service_close( &this->parent );
269 }
270 free( this );
271 }
272 }