]> git.sesse.net Git - mlt/blob - src/framework/mlt_filter.c
Add mlt_filter_get_length2.
[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 duration.
207  *
208  * This version works with filters with no explicit in and out by getting the
209  * length of the frame's producer.
210  *
211  * \public \memberof mlt_filter_s
212  * \param self a filter
213  * \param frame a frame from which to get its producer
214  * \return the duration or zero if unlimited
215  */
216
217 mlt_position mlt_filter_get_length2( mlt_filter self, mlt_frame frame )
218 {
219         mlt_properties properties = MLT_SERVICE_PROPERTIES( &self->parent );
220         mlt_position in = mlt_properties_get_position( properties, "in" );
221         mlt_position out = mlt_properties_get_position( properties, "out" );
222
223         if ( out == 0 && frame )
224         {
225                 // If always active, use the frame's producer
226                 mlt_producer producer = mlt_frame_get_original_producer( frame );
227                 if ( producer )
228                 {
229                         producer = mlt_producer_cut_parent( producer );
230                         in = mlt_producer_get_in( producer );
231                         out = mlt_producer_get_out( producer );
232                 }
233         }
234         return ( out > 0 ) ? ( out - in + 1 ) : 0;
235 }
236
237 /** Get the position within the filter.
238  *
239  * The position is relative to the in point.
240  * This will only be valid once mlt_filter_process is called.
241  *
242  * \public \memberof mlt_filter_s
243  * \param self a filter
244  * \param frame a frame
245  * \return the position
246  */
247
248 mlt_position mlt_filter_get_position( mlt_filter self, mlt_frame frame )
249 {
250         mlt_properties properties = MLT_FILTER_PROPERTIES( self );
251         mlt_position in = mlt_properties_get_position( properties, "in" );
252         const char *unique_id = mlt_properties_get( properties, "_unique_id" );
253         char name[20];
254
255         // Make the properties key from unique id
256         strcpy( name, "pos." );
257         strcat( name, unique_id );
258
259         return mlt_properties_get_position( MLT_FRAME_PROPERTIES( frame ), name ) - in;
260 }
261
262 /** Get the percent complete.
263  *
264  * This will only be valid once mlt_filter_process is called.
265  *
266  * \public \memberof mlt_filter_s
267  * \param self a filter
268  * \param frame a frame
269  * \return the progress in the range 0.0 to 1.0
270  */
271
272 double mlt_filter_get_progress( mlt_filter self, mlt_frame frame )
273 {
274         double position = mlt_filter_get_position( self, frame );
275         double length = mlt_filter_get_length2( self, frame );
276         return position / length;
277 }
278
279 /** Process the frame.
280  *
281  * When fetching the frame position in a subclass process method, the frame's
282  * position is relative to the filter's producer - not the filter's in point
283  * or timeline.
284  *
285  * \public \memberof mlt_filter_s
286  * \param self a filter
287  * \param frame a frame
288  * \return a frame
289  */
290
291
292 mlt_frame mlt_filter_process( mlt_filter self, mlt_frame frame )
293 {
294         mlt_properties properties = MLT_FILTER_PROPERTIES( self );
295         int disable = mlt_properties_get_int( properties, "disable" );
296         const char *unique_id = mlt_properties_get( properties, "_unique_id" );
297         mlt_position position = mlt_frame_get_position( frame );
298         char name[20];
299
300         // Make the properties key from unique id
301         strcpy( name, "pos." );
302         strcat( name, unique_id );
303
304         // Save the position on the frame
305         mlt_properties_set_position( MLT_FRAME_PROPERTIES( frame ), name, position );
306
307         if ( disable || self->process == NULL )
308                 return frame;
309         else
310                 return self->process( self, frame );
311 }
312
313 /** Get a frame from this filter.
314  *
315  * \private \memberof mlt_filter_s
316  * \param service a service
317  * \param[out] frame a frame by reference
318  * \param index as determined by the producer
319  * \return true on error
320  */
321
322
323 static int filter_get_frame( mlt_service service, mlt_frame_ptr frame, int index )
324 {
325         mlt_filter self = service->child;
326
327         // Get coords in/out/track
328         int track = mlt_filter_get_track( self );
329         int in = mlt_filter_get_in( self );
330         int out = mlt_filter_get_out( self );
331
332         // Get the producer this is connected to
333         mlt_service producer = mlt_service_producer( &self->parent );
334
335         // If the frame request is for this filters track, we need to process it
336         if ( index == track || track == -1 )
337         {
338                 int ret = mlt_service_get_frame( producer, frame, index );
339                 if ( ret == 0 )
340                 {
341                         mlt_position position = mlt_frame_get_position( *frame );
342                         if ( position >= in && ( out == 0 || position <= out ) )
343                                 *frame = mlt_filter_process( self, *frame );
344                         return 0;
345                 }
346                 else
347                 {
348                         *frame = mlt_frame_init( service );
349                         return 0;
350                 }
351         }
352         else
353         {
354                 return mlt_service_get_frame( producer, frame, index );
355         }
356 }
357
358 /** Close and destroy the filter.
359  *
360  * \public \memberof mlt_filter_s
361  * \param self a filter
362  */
363
364
365 void mlt_filter_close( mlt_filter self )
366 {
367         if ( self != NULL && mlt_properties_dec_ref( MLT_FILTER_PROPERTIES( self ) ) <= 0 )
368         {
369                 if ( self->close != NULL )
370                 {
371                         self->close( self );
372                 }
373                 else
374                 {
375                         self->parent.close = NULL;
376                         mlt_service_close( &self->parent );
377                 }
378                 free( self );
379         }
380 }