]> git.sesse.net Git - mlt/blob - src/framework/mlt_filter.c
A little debugging.
[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 && mlt_filter_init( self, NULL ) == 0 )
78         {
79                 return self;
80         }
81         else
82         {
83                 free(self);
84                 return NULL;
85         }
86 }
87
88 /** Get the service class interface.
89  *
90  * \public \memberof mlt_filter_s
91  * \param self a filter
92  * \return the service parent class
93  * \see MLT_FILTER_SERVICE
94  */
95
96 mlt_service mlt_filter_service( mlt_filter self )
97 {
98         return self != NULL ? &self->parent : NULL;
99 }
100
101 /** Get the filter properties.
102  *
103  * \public \memberof mlt_filter_s
104  * \param self a filter
105  * \return the properties list for the filter
106  * \see MLT_FILTER_PROPERTIES
107  */
108
109 mlt_properties mlt_filter_properties( mlt_filter self )
110 {
111         return MLT_SERVICE_PROPERTIES( MLT_FILTER_SERVICE( self ) );
112 }
113
114 /** Connect this filter to a producers track. Note that a filter only operates
115  * on a single track, and by default it operates on the entirety of that track.
116  *
117  * \public \memberof mlt_filter_s
118  * \param self a filter
119  * \param producer the producer to which to connect this filter
120  * \param index which of potentially multiple producers to this service (0 based)
121  */
122
123 int mlt_filter_connect( mlt_filter self, mlt_service producer, int index )
124 {
125         int ret = mlt_service_connect_producer( &self->parent, producer, index );
126
127         // If the connection was successful, grab the producer, track and reset in/out
128         if ( ret == 0 )
129         {
130                 mlt_properties properties = MLT_SERVICE_PROPERTIES( &self->parent );
131                 mlt_properties_set_position( properties, "in", 0 );
132                 mlt_properties_set_position( properties, "out", 0 );
133                 mlt_properties_set_int( properties, "track", index );
134         }
135
136         return ret;
137 }
138
139 /** Set the starting and ending time.
140  *
141  * \public \memberof mlt_filter_s
142  * \param self a filter
143  * \param in the time relative to the producer at which start applying the filter
144  * \param out the time relative to the producer at which to stop applying the filter
145  */
146
147
148 void mlt_filter_set_in_and_out( mlt_filter self, mlt_position in, mlt_position out )
149 {
150         mlt_properties properties = MLT_SERVICE_PROPERTIES( &self->parent );
151         mlt_properties_set_position( properties, "in", in );
152         mlt_properties_set_position( properties, "out", out );
153 }
154
155 /** Return the track that this filter is operating on.
156  *
157  * \public \memberof mlt_filter_s
158  * \param self a filter
159  * \return true on error
160  */
161
162
163 int mlt_filter_get_track( mlt_filter self )
164 {
165         mlt_properties properties = MLT_SERVICE_PROPERTIES( &self->parent );
166         return mlt_properties_get_int( properties, "track" );
167 }
168
169 /** Get the in point.
170  *
171  * \public \memberof mlt_filter_s
172  * \param self a filter
173  * \return the start time for the filter relative to the producer
174  */
175
176
177 mlt_position mlt_filter_get_in( mlt_filter self )
178 {
179         mlt_properties properties = MLT_SERVICE_PROPERTIES( &self->parent );
180         return mlt_properties_get_position( properties, "in" );
181 }
182
183 /** Get the out point.
184  *
185  * \public \memberof mlt_filter_s
186  * \param self a filter
187  * \return the ending time for the filter relative to the producer
188  */
189
190
191 mlt_position mlt_filter_get_out( mlt_filter self )
192 {
193         mlt_properties properties = MLT_SERVICE_PROPERTIES( &self->parent );
194         return mlt_properties_get_position( properties, "out" );
195 }
196
197 /** Get the duration.
198  *
199  * \public \memberof mlt_filter_s
200  * \param self a filter
201  * \return the duration or zero if unlimited
202  */
203
204 mlt_position mlt_filter_get_length( mlt_filter self )
205 {
206         mlt_properties properties = MLT_SERVICE_PROPERTIES( &self->parent );
207         mlt_position in = mlt_properties_get_position( properties, "in" );
208         mlt_position out = mlt_properties_get_position( properties, "out" );
209         return ( out > 0 ) ? ( out - in + 1 ) : 0;
210 }
211
212 /** Get the duration.
213  *
214  * This version works with filters with no explicit in and out by getting the
215  * length of the frame's producer.
216  *
217  * \public \memberof mlt_filter_s
218  * \param self a filter
219  * \param frame a frame from which to get its producer
220  * \return the duration or zero if unlimited
221  */
222
223 mlt_position mlt_filter_get_length2( mlt_filter self, mlt_frame frame )
224 {
225         mlt_properties properties = MLT_SERVICE_PROPERTIES( &self->parent );
226         mlt_position in = mlt_properties_get_position( properties, "in" );
227         mlt_position out = mlt_properties_get_position( properties, "out" );
228
229         if ( out == 0 && frame )
230         {
231                 // If always active, use the frame's producer
232                 mlt_producer producer = mlt_frame_get_original_producer( frame );
233                 if ( producer )
234                 {
235                         producer = mlt_producer_cut_parent( producer );
236                         in = mlt_producer_get_in( producer );
237                         out = mlt_producer_get_out( producer );
238                 }
239         }
240         return ( out > 0 ) ? ( out - in + 1 ) : 0;
241 }
242
243 /** Get the position within the filter.
244  *
245  * The position is relative to the in point.
246  * This will only be valid once mlt_filter_process is called.
247  *
248  * \public \memberof mlt_filter_s
249  * \param self a filter
250  * \param frame a frame
251  * \return the position
252  */
253
254 mlt_position mlt_filter_get_position( mlt_filter self, mlt_frame frame )
255 {
256         mlt_properties properties = MLT_FILTER_PROPERTIES( self );
257         mlt_position in = mlt_properties_get_position( properties, "in" );
258         const char *unique_id = mlt_properties_get( properties, "_unique_id" );
259         char name[20];
260
261         // Make the properties key from unique id
262         snprintf( name, 20, "pos.%s", unique_id );
263         name[20 - 1] = '\0';
264
265         return mlt_properties_get_position( MLT_FRAME_PROPERTIES( frame ), name ) - in;
266 }
267
268 /** Get the percent complete.
269  *
270  * This will only be valid once mlt_filter_process is called.
271  *
272  * \public \memberof mlt_filter_s
273  * \param self a filter
274  * \param frame a frame
275  * \return the progress in the range 0.0 to 1.0
276  */
277
278 double mlt_filter_get_progress( mlt_filter self, mlt_frame frame )
279 {
280         double position = mlt_filter_get_position( self, frame );
281         double length = mlt_filter_get_length2( self, frame );
282         return position / length;
283 }
284
285 /** Process the frame.
286  *
287  * When fetching the frame position in a subclass process method, the frame's
288  * position is relative to the filter's producer - not the filter's in point
289  * or timeline.
290  *
291  * \public \memberof mlt_filter_s
292  * \param self a filter
293  * \param frame a frame
294  * \return a frame
295  */
296
297
298 mlt_frame mlt_filter_process( mlt_filter self, mlt_frame frame )
299 {
300         mlt_properties properties = MLT_FILTER_PROPERTIES( self );
301         int disable = mlt_properties_get_int( properties, "disable" );
302         const char *unique_id = mlt_properties_get( properties, "_unique_id" );
303         mlt_position position = mlt_frame_get_position( frame );
304         char name[30];
305
306         // Make the properties key from unique id
307         snprintf( name, sizeof(name), "pos.%s", unique_id );
308         name[sizeof(name) -1] = '\0';
309
310         // Save the position on the frame
311         mlt_properties_set_position( MLT_FRAME_PROPERTIES( frame ), name, position );
312
313         if ( disable || self->process == NULL )
314         {
315                 return frame;
316         }
317         else
318         {
319                 // Add a reference to this filter on the frame
320                 mlt_properties_inc_ref( MLT_FILTER_PROPERTIES(self) );
321                 snprintf( name, sizeof(name), "filter.%s", unique_id );
322                 name[sizeof(name) -1] = '\0';
323                 mlt_properties_set_data( MLT_FRAME_PROPERTIES(frame), name, self, 0,
324                         (mlt_destructor) mlt_filter_close, NULL );
325
326                 return self->process( self, frame );
327         }
328 }
329
330 /** Get a frame from this filter.
331  *
332  * \private \memberof mlt_filter_s
333  * \param service a service
334  * \param[out] frame a frame by reference
335  * \param index as determined by the producer
336  * \return true on error
337  */
338
339
340 static int filter_get_frame( mlt_service service, mlt_frame_ptr frame, int index )
341 {
342         mlt_filter self = service->child;
343
344         // Get coords in/out/track
345         int track = mlt_filter_get_track( self );
346         int in = mlt_filter_get_in( self );
347         int out = mlt_filter_get_out( self );
348
349         // Get the producer this is connected to
350         mlt_service producer = mlt_service_producer( &self->parent );
351
352         // If the frame request is for this filters track, we need to process it
353         if ( index == track || track == -1 )
354         {
355                 int ret = mlt_service_get_frame( producer, frame, index );
356                 if ( ret == 0 )
357                 {
358                         mlt_position position = mlt_frame_get_position( *frame );
359                         if ( position >= in && ( out == 0 || position <= out ) )
360                                 *frame = mlt_filter_process( self, *frame );
361                         return 0;
362                 }
363                 else
364                 {
365                         *frame = mlt_frame_init( service );
366                         return 0;
367                 }
368         }
369         else
370         {
371                 return mlt_service_get_frame( producer, frame, index );
372         }
373 }
374
375 /** Close and destroy the filter.
376  *
377  * \public \memberof mlt_filter_s
378  * \param self a filter
379  */
380
381
382 void mlt_filter_close( mlt_filter self )
383 {
384         if ( self != NULL && mlt_properties_dec_ref( MLT_FILTER_PROPERTIES( self ) ) <= 0 )
385         {
386                 if ( self->close != NULL )
387                 {
388                         self->close( self );
389                 }
390                 else
391                 {
392                         self->parent.close = NULL;
393                         mlt_service_close( &self->parent );
394                 }
395                 free( self );
396         }
397 }