1 /**
2 * \file mlt_filter.c
3 * \brief abstraction for all filter services
4 * \see mlt_filter_s
5 *
6 * Copyright (C) 2003-2020 Meltytech, LLC
7 *
8 * This library is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU Lesser General Public
10 * License as published by the Free Software Foundation; either
11 * version 2.1 of the License, or (at your option) any later version.
12 *
13 * This library is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 * Lesser General Public License for more details.
17 *
18 * You should have received a copy of the GNU Lesser General Public
19 * License along with this library; if not, write to the Free Software
20 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
21 */
22
23 #include "mlt_filter.h"
24 #include "mlt_frame.h"
25 #include "mlt_producer.h"
26
27 #include <stdio.h>
28 #include <stdlib.h>
29 #include <string.h>
30
31 static int filter_get_frame( mlt_service self, mlt_frame_ptr frame, int index );
32
33 /** Initialize a new filter.
34 *
35 * \public \memberof mlt_filter_s
36 * \param self a filter
37 * \param child the object of a subclass
38 * \return true if there was an error
39 */
40
mlt_filter_init(mlt_filter self,void * child)41 int mlt_filter_init( mlt_filter self, void *child )
42 {
43 mlt_service service = &self->parent;
44 memset( self, 0, sizeof( struct mlt_filter_s ) );
45 self->child = child;
46 if ( mlt_service_init( service, self ) == 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 = self;
56
57 mlt_properties_set( properties, "mlt_type", "filter" );
58 mlt_properties_set_position( properties, "in", 0 );
59 mlt_properties_set_position( properties, "out", 0 );
60
61 return 0;
62 }
63 return 1;
64 }
65
66 /** Create a new filter and initialize it.
67 *
68 * \public \memberof mlt_filter_s
69 * \return a new filter
70 */
71
mlt_filter_new()72 mlt_filter mlt_filter_new( )
73 {
74 mlt_filter self = calloc( 1, sizeof( struct mlt_filter_s ) );
75 if ( self != NULL && mlt_filter_init( self, NULL ) == 0 )
76 {
77 return self;
78 }
79 else
80 {
81 free(self);
82 return NULL;
83 }
84 }
85
86 /** Get the service class interface.
87 *
88 * \public \memberof mlt_filter_s
89 * \param self a filter
90 * \return the service parent class
91 * \see MLT_FILTER_SERVICE
92 */
93
mlt_filter_service(mlt_filter self)94 mlt_service mlt_filter_service( mlt_filter self )
95 {
96 return self != NULL ? &self->parent : NULL;
97 }
98
99 /** Get the filter properties.
100 *
101 * \public \memberof mlt_filter_s
102 * \param self a filter
103 * \return the properties list for the filter
104 * \see MLT_FILTER_PROPERTIES
105 */
106
mlt_filter_properties(mlt_filter self)107 mlt_properties mlt_filter_properties( mlt_filter self )
108 {
109 return MLT_SERVICE_PROPERTIES( MLT_FILTER_SERVICE( self ) );
110 }
111
112 /** Connect this filter to a producers track. Note that a filter only operates
113 * on a single track, and by default it operates on the entirety of that track.
114 *
115 * \public \memberof mlt_filter_s
116 * \param self a filter
117 * \param producer the producer to which to connect this filter
118 * \param index which of potentially multiple producers to this service (0 based)
119 */
120
mlt_filter_connect(mlt_filter self,mlt_service producer,int index)121 int mlt_filter_connect( mlt_filter self, mlt_service producer, int index )
122 {
123 int ret = mlt_service_connect_producer( &self->parent, producer, index );
124
125 // If the connection was successful, grab the producer, track and reset in/out
126 if ( ret == 0 )
127 {
128 mlt_properties properties = MLT_SERVICE_PROPERTIES( &self->parent );
129 mlt_properties_set_position( properties, "in", 0 );
130 mlt_properties_set_position( properties, "out", 0 );
131 mlt_properties_set_int( properties, "track", index );
132 }
133
134 return ret;
135 }
136
137 /** Set the starting and ending time.
138 *
139 * \public \memberof mlt_filter_s
140 * \param self a filter
141 * \param in the time relative to the producer at which start applying the filter
142 * \param out the time relative to the producer at which to stop applying the filter
143 */
144
145
mlt_filter_set_in_and_out(mlt_filter self,mlt_position in,mlt_position out)146 void mlt_filter_set_in_and_out( mlt_filter self, mlt_position in, mlt_position out )
147 {
148 mlt_properties properties = MLT_SERVICE_PROPERTIES( &self->parent );
149 mlt_properties_set_position( properties, "in", in );
150 mlt_properties_set_position( properties, "out", out );
151 }
152
153 /** Return the track that this filter is operating on.
154 *
155 * \public \memberof mlt_filter_s
156 * \param self a filter
157 * \return true on error
158 */
159
160
mlt_filter_get_track(mlt_filter self)161 int mlt_filter_get_track( mlt_filter self )
162 {
163 mlt_properties properties = MLT_SERVICE_PROPERTIES( &self->parent );
164 return mlt_properties_get_int( properties, "track" );
165 }
166
167 /** Get the in point.
168 *
169 * \public \memberof mlt_filter_s
170 * \param self a filter
171 * \return the start time for the filter relative to the producer
172 */
173
174
mlt_filter_get_in(mlt_filter self)175 mlt_position mlt_filter_get_in( mlt_filter self )
176 {
177 mlt_properties properties = MLT_SERVICE_PROPERTIES( &self->parent );
178 return mlt_properties_get_position( properties, "in" );
179 }
180
181 /** Get the out point.
182 *
183 * \public \memberof mlt_filter_s
184 * \param self a filter
185 * \return the ending time for the filter relative to the producer
186 */
187
188
mlt_filter_get_out(mlt_filter self)189 mlt_position mlt_filter_get_out( mlt_filter self )
190 {
191 mlt_properties properties = MLT_SERVICE_PROPERTIES( &self->parent );
192 return mlt_properties_get_position( properties, "out" );
193 }
194
195 /** Get the duration.
196 *
197 * \public \memberof mlt_filter_s
198 * \param self a filter
199 * \return the duration or zero if unlimited
200 */
201
mlt_filter_get_length(mlt_filter self)202 mlt_position mlt_filter_get_length( mlt_filter self )
203 {
204 mlt_properties properties = MLT_SERVICE_PROPERTIES( &self->parent );
205 mlt_position in = mlt_properties_get_position( properties, "in" );
206 mlt_position out = mlt_properties_get_position( properties, "out" );
207 return ( out > 0 ) ? ( out - in + 1 ) : 0;
208 }
209
210 /** Get the duration.
211 *
212 * This version works with filters with no explicit in and out by getting the
213 * length of the frame's producer.
214 *
215 * \public \memberof mlt_filter_s
216 * \param self a filter
217 * \param frame a frame from which to get its producer
218 * \return the duration or zero if unlimited
219 */
220
mlt_filter_get_length2(mlt_filter self,mlt_frame frame)221 mlt_position mlt_filter_get_length2( mlt_filter self, mlt_frame frame )
222 {
223 mlt_properties properties = MLT_SERVICE_PROPERTIES( &self->parent );
224 mlt_position in = mlt_properties_get_position( properties, "in" );
225 mlt_position out = mlt_properties_get_position( properties, "out" );
226
227 if ( out == 0 && frame )
228 {
229 // If always active, use the frame's producer
230 mlt_producer producer = mlt_frame_get_original_producer( frame );
231 if ( producer )
232 {
233 producer = mlt_producer_cut_parent( producer );
234 in = mlt_producer_get_in( producer );
235 out = mlt_producer_get_out( producer );
236 }
237 }
238 return ( out > 0 ) ? ( out - in + 1 ) : 0;
239 }
240
241 /** Get the position within the filter.
242 *
243 * The position is relative to the in point.
244 * This will only be valid once mlt_filter_process is called.
245 *
246 * \public \memberof mlt_filter_s
247 * \param self a filter
248 * \param frame a frame
249 * \return the position
250 */
251
mlt_filter_get_position(mlt_filter self,mlt_frame frame)252 mlt_position mlt_filter_get_position( mlt_filter self, mlt_frame frame )
253 {
254 mlt_properties properties = MLT_FILTER_PROPERTIES( self );
255 mlt_position in = mlt_properties_get_position( properties, "in" );
256 const char *unique_id = mlt_properties_get( properties, "_unique_id" );
257 char name[64];
258
259 // Make the properties key from unique id
260 snprintf( name, sizeof(name), "pos.%s", unique_id );
261
262 return mlt_properties_get_position( MLT_FRAME_PROPERTIES( frame ), name ) - in;
263 }
264
265 /** Get the percent complete.
266 *
267 * This will only be valid once mlt_filter_process is called.
268 *
269 * \public \memberof mlt_filter_s
270 * \param self a filter
271 * \param frame a frame
272 * \return the progress in the range 0.0 to 1.0
273 */
274
mlt_filter_get_progress(mlt_filter self,mlt_frame frame)275 double mlt_filter_get_progress( mlt_filter self, mlt_frame frame )
276 {
277 double position = mlt_filter_get_position( self, frame );
278 double length = mlt_filter_get_length2( self, frame );
279 if (length > 1)
280 return position / (length - 1);
281 else
282 return 1.0;
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
mlt_filter_process(mlt_filter self,mlt_frame frame)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[64];
305
306 // Make the properties key from unique id
307 snprintf( name, sizeof(name), "pos.%s", unique_id );
308
309 // Save the position on the frame
310 mlt_properties_set_position( MLT_FRAME_PROPERTIES( frame ), name, position );
311
312 if ( disable || !self || !self->process )
313 {
314 return frame;
315 }
316 else
317 {
318 // Add a reference to this filter on the frame
319 mlt_properties_inc_ref( MLT_FILTER_PROPERTIES(self) );
320 snprintf( name, sizeof(name), "filter.%s", unique_id );
321 name[sizeof(name) -1] = '\0';
322 mlt_properties_set_data( MLT_FRAME_PROPERTIES(frame), name, self, 0,
323 (mlt_destructor) mlt_filter_close, NULL );
324
325 return self->process( self, frame );
326 }
327 }
328
329 /** Get a frame from this filter.
330 *
331 * \private \memberof mlt_filter_s
332 * \param service a service
333 * \param[out] frame a frame by reference
334 * \param index as determined by the producer
335 * \return true on error
336 */
337
338
filter_get_frame(mlt_service service,mlt_frame_ptr frame,int index)339 static int filter_get_frame( mlt_service service, mlt_frame_ptr frame, int index )
340 {
341 mlt_filter self = service->child;
342
343 // Get coords in/out/track
344 int track = mlt_filter_get_track( self );
345 int in = mlt_filter_get_in( self );
346 int out = mlt_filter_get_out( self );
347
348 // Get the producer this is connected to
349 mlt_service producer = mlt_service_producer( &self->parent );
350
351 // If the frame request is for this filters track, we need to process it
352 if ( index == track || track == -1 )
353 {
354 int ret = mlt_service_get_frame( producer, frame, index );
355 if ( ret == 0 )
356 {
357 mlt_position position = mlt_frame_get_position( *frame );
358 if ( position >= in && ( out == 0 || position <= out ) )
359 *frame = mlt_filter_process( self, *frame );
360 return 0;
361 }
362 else
363 {
364 *frame = mlt_frame_init( service );
365 return 0;
366 }
367 }
368 else
369 {
370 return mlt_service_get_frame( producer, frame, index );
371 }
372 }
373
374 /** Close and destroy the filter.
375 *
376 * \public \memberof mlt_filter_s
377 * \param self a filter
378 */
379
380
mlt_filter_close(mlt_filter self)381 void mlt_filter_close( mlt_filter self )
382 {
383 if ( self != NULL && mlt_properties_dec_ref( MLT_FILTER_PROPERTIES( self ) ) <= 0 )
384 {
385 if ( self->close != NULL )
386 {
387 self->close( self );
388 }
389 else
390 {
391 self->parent.close = NULL;
392 mlt_service_close( &self->parent );
393 }
394 free( self );
395 }
396 }
397