]> git.sesse.net Git - mlt/blob - src/framework/mlt_filter.c
22eaa3fbd4b0e6c2919ee4058e4901a48abc0731
[mlt] / 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 #include "mlt_producer.h"
27
28 #include <stdio.h>
29 #include <stdlib.h>
30 #include <string.h>
31
32 static int filter_get_frame( mlt_service self, mlt_frame_ptr frame, int index );
33
34 /** Initialize a new filter.
35  *
36  * \public \memberof mlt_filter_s
37  * \param self a filter
38  * \param child the object of a subclass
39  * \return true if there was an error
40  */
41
42 int mlt_filter_init( mlt_filter self, void *child )
43 {
44         mlt_service service = &self->parent;
45         memset( self, 0, sizeof( struct mlt_filter_s ) );
46         self->child = child;
47         if ( mlt_service_init( service, self ) == 0 )
48         {
49                 mlt_properties properties = MLT_SERVICE_PROPERTIES( service );
50
51                 // Override the get_frame method
52                 service->get_frame = filter_get_frame;
53
54                 // Define the destructor
55                 service->close = ( mlt_destructor )mlt_filter_close;
56                 service->close_object = self;
57
58                 // Default in, out, track properties
59                 mlt_properties_set_position( properties, "in", 0 );
60                 mlt_properties_set_position( properties, "out", 0 );
61                 mlt_properties_set_int( properties, "track", 0 );
62
63                 return 0;
64         }
65         return 1;
66 }
67
68 /** Create a new filter and initialize it.
69  *
70  * \public \memberof mlt_filter_s
71  * \return a new filter
72  */
73
74 mlt_filter mlt_filter_new( )
75 {
76         mlt_filter self = calloc( 1, sizeof( struct mlt_filter_s ) );
77         if ( self != NULL )
78                 mlt_filter_init( self, NULL );
79         return self;
80 }
81
82 /** Get the service class interface.
83  *
84  * \public \memberof mlt_filter_s
85  * \param self a filter
86  * \return the service parent class
87  * \see MLT_FILTER_SERVICE
88  */
89
90 mlt_service mlt_filter_service( mlt_filter self )
91 {
92         return self != NULL ? &self->parent : NULL;
93 }
94
95 /** Get the filter properties.
96  *
97  * \public \memberof mlt_filter_s
98  * \param self a filter
99  * \return the properties list for the filter
100  * \see MLT_FILTER_PROPERTIES
101  */
102
103 mlt_properties mlt_filter_properties( mlt_filter self )
104 {
105         return MLT_SERVICE_PROPERTIES( MLT_FILTER_SERVICE( self ) );
106 }
107
108 /** Connect this filter to a producers track. Note that a filter only operates
109  * on a single track, and by default it operates on the entirety of that track.
110  *
111  * \public \memberof mlt_filter_s
112  * \param self a filter
113  * \param producer the producer to which to connect this filter
114  * \param index which of potentially multiple producers to this service (0 based)
115  */
116
117 int mlt_filter_connect( mlt_filter self, mlt_service producer, int index )
118 {
119         int ret = mlt_service_connect_producer( &self->parent, producer, index );
120
121         // If the connection was successful, grab the producer, track and reset in/out
122         if ( ret == 0 )
123         {
124                 mlt_properties properties = MLT_SERVICE_PROPERTIES( &self->parent );
125                 mlt_properties_set_position( properties, "in", 0 );
126                 mlt_properties_set_position( properties, "out", 0 );
127                 mlt_properties_set_int( properties, "track", index );
128         }
129
130         return ret;
131 }
132
133 /** Set the starting and ending time.
134  *
135  * \public \memberof mlt_filter_s
136  * \param self a filter
137  * \param in the time relative to the producer at which start applying the filter
138  * \param out the time relative to the producer at which to stop applying the filter
139  */
140
141
142 void mlt_filter_set_in_and_out( mlt_filter self, mlt_position in, mlt_position out )
143 {
144         mlt_properties properties = MLT_SERVICE_PROPERTIES( &self->parent );
145         mlt_properties_set_position( properties, "in", in );
146         mlt_properties_set_position( properties, "out", out );
147 }
148
149 /** Return the track that this filter is operating on.
150  *
151  * \public \memberof mlt_filter_s
152  * \param self a filter
153  * \return true on error
154  */
155
156
157 int mlt_filter_get_track( mlt_filter self )
158 {
159         mlt_properties properties = MLT_SERVICE_PROPERTIES( &self->parent );
160         return mlt_properties_get_int( properties, "track" );
161 }
162
163 /** Get the in point.
164  *
165  * \public \memberof mlt_filter_s
166  * \param self a filter
167  * \return the start time for the filter relative to the producer
168  */
169
170
171 mlt_position mlt_filter_get_in( mlt_filter self )
172 {
173         mlt_properties properties = MLT_SERVICE_PROPERTIES( &self->parent );
174         return mlt_properties_get_position( properties, "in" );
175 }
176
177 /** Get the out point.
178  *
179  * \public \memberof mlt_filter_s
180  * \param self a filter
181  * \return the ending time for the filter relative to the producer
182  */
183
184
185 mlt_position mlt_filter_get_out( mlt_filter self )
186 {
187         mlt_properties properties = MLT_SERVICE_PROPERTIES( &self->parent );
188         return mlt_properties_get_position( properties, "out" );
189 }
190
191 /** Get the duration.
192  *
193  * \public \memberof mlt_filter_s
194  * \param self a filter
195  * \return the duration or zero if unlimited
196  */
197
198 mlt_position mlt_filter_get_length( mlt_filter self )
199 {
200         mlt_properties properties = MLT_SERVICE_PROPERTIES( &self->parent );
201         mlt_position in = mlt_properties_get_position( properties, "in" );
202         mlt_position out = mlt_properties_get_position( properties, "out" );
203         return ( out > 0 ) ? ( out - in + 1 ) : 0;
204 }
205
206 /** Get the position within the filter.
207  *
208  * The position is relative to the in point.
209  * This will only be valid once mlt_filter_process is called.
210  *
211  * \public \memberof mlt_filter_s
212  * \param self a filter
213  * \param frame a frame
214  * \return the position
215  */
216
217 mlt_position mlt_filter_get_position( mlt_filter self, mlt_frame frame )
218 {
219         mlt_properties properties = MLT_FILTER_PROPERTIES( self );
220         mlt_position in = mlt_properties_get_position( properties, "in" );
221         const char *unique_id = mlt_properties_get( properties, "_unique_id" );
222         char name[20];
223
224         // Make the properties key from unique id
225         strcpy( name, "pos." );
226         strcat( name, unique_id );
227
228         return mlt_properties_get_position( MLT_FRAME_PROPERTIES( frame ), name ) - in;
229 }
230
231 /** Get the percent complete.
232  *
233  * This will only be valid once mlt_filter_process is called.
234  *
235  * \public \memberof mlt_filter_s
236  * \param self a filter
237  * \param frame a frame
238  * \return the progress in the range 0.0 to 1.0
239  */
240
241 double mlt_filter_get_progress( mlt_filter self, mlt_frame frame )
242 {
243         double progress = 0;
244         mlt_position in = mlt_filter_get_in( self );
245         mlt_position out = mlt_filter_get_out( self );
246
247         if ( out == 0 )
248         {
249                 // If always active, use the frame's producer
250                 mlt_producer producer = mlt_frame_get_original_producer( frame );
251                 if ( producer )
252                 {
253                         in = mlt_producer_get_in( producer );
254                         out = mlt_producer_get_out( producer );
255                 }
256         }
257         if ( out != 0 )
258         {
259                 mlt_position position = mlt_filter_get_position( self, frame );
260                 progress = ( double ) position / ( double ) ( out - in + 1 );
261         }
262         return progress;
263 }
264
265 /** Process the frame.
266  *
267  * When fetching the frame position in a subclass process method, the frame's
268  * position is relative to the filter's producer - not the filter's in point
269  * or timeline.
270  *
271  * \public \memberof mlt_filter_s
272  * \param self a filter
273  * \param frame a frame
274  * \return a frame
275  */
276
277
278 mlt_frame mlt_filter_process( mlt_filter self, mlt_frame frame )
279 {
280         mlt_properties properties = MLT_FILTER_PROPERTIES( self );
281         int disable = mlt_properties_get_int( properties, "disable" );
282         const char *unique_id = mlt_properties_get( properties, "_unique_id" );
283         mlt_position position = mlt_frame_get_position( frame );
284         char name[20];
285
286         // Make the properties key from unique id
287         strcpy( name, "pos." );
288         strcat( name, unique_id );
289
290         // Save the position on the frame
291         mlt_properties_set_position( MLT_FRAME_PROPERTIES( frame ), name, position );
292
293         if ( disable || self->process == NULL )
294                 return frame;
295         else
296                 return self->process( self, frame );
297 }
298
299 /** Get a frame from this filter.
300  *
301  * \private \memberof mlt_filter_s
302  * \param service a service
303  * \param[out] frame a frame by reference
304  * \param index as determined by the producer
305  * \return true on error
306  */
307
308
309 static int filter_get_frame( mlt_service service, mlt_frame_ptr frame, int index )
310 {
311         mlt_filter self = service->child;
312
313         // Get coords in/out/track
314         int track = mlt_filter_get_track( self );
315         int in = mlt_filter_get_in( self );
316         int out = mlt_filter_get_out( self );
317
318         // Get the producer this is connected to
319         mlt_service producer = mlt_service_producer( &self->parent );
320
321         // If the frame request is for this filters track, we need to process it
322         if ( index == track || track == -1 )
323         {
324                 int ret = mlt_service_get_frame( producer, frame, index );
325                 if ( ret == 0 )
326                 {
327                         mlt_position position = mlt_frame_get_position( *frame );
328                         if ( position >= in && ( out == 0 || position <= out ) )
329                                 *frame = mlt_filter_process( self, *frame );
330                         return 0;
331                 }
332                 else
333                 {
334                         *frame = mlt_frame_init( service );
335                         return 0;
336                 }
337         }
338         else
339         {
340                 return mlt_service_get_frame( producer, frame, index );
341         }
342 }
343
344 /** Close and destroy the filter.
345  *
346  * \public \memberof mlt_filter_s
347  * \param self a filter
348  */
349
350
351 void mlt_filter_close( mlt_filter self )
352 {
353         if ( self != NULL && mlt_properties_dec_ref( MLT_FILTER_PROPERTIES( self ) ) <= 0 )
354         {
355                 if ( self->close != NULL )
356                 {
357                         self->close( self );
358                 }
359                 else
360                 {
361                         self->parent.close = NULL;
362                         mlt_service_close( &self->parent );
363                 }
364                 free( self );
365         }
366 }