move binary modules to libdir - affects MLT_REPOSITORY
[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 /** Constructor method.
31 */
32
33 int mlt_filter_init( mlt_filter this, void *child )
34 {
35 mlt_service service = &this->parent;
36 memset( this, 0, sizeof( struct mlt_filter_s ) );
37 this->child = child;
38 if ( mlt_service_init( service, this ) == 0 )
39 {
40 mlt_properties properties = MLT_SERVICE_PROPERTIES( service );
41
42 // Override the get_frame method
43 service->get_frame = filter_get_frame;
44
45 // Define the destructor
46 service->close = ( mlt_destructor )mlt_filter_close;
47 service->close_object = this;
48
49 // Default in, out, track properties
50 mlt_properties_set_position( properties, "in", 0 );
51 mlt_properties_set_position( properties, "out", 0 );
52 mlt_properties_set_int( properties, "track", 0 );
53
54 return 0;
55 }
56 return 1;
57 }
58
59 /** Create a new filter.
60 */
61
62 mlt_filter mlt_filter_new( )
63 {
64 mlt_filter this = calloc( 1, sizeof( struct mlt_filter_s ) );
65 if ( this != NULL )
66 mlt_filter_init( this, NULL );
67 return this;
68 }
69
70 /** Get the service associated to this filter
71 */
72
73 mlt_service mlt_filter_service( mlt_filter this )
74 {
75 return this != NULL ? &this->parent : NULL;
76 }
77
78 /** Get the properties associated to this filter.
79 */
80
81 mlt_properties mlt_filter_properties( mlt_filter this )
82 {
83 return MLT_SERVICE_PROPERTIES( MLT_FILTER_SERVICE( this ) );
84 }
85
86 /** Connect this filter to a producers track. Note that a filter only operates
87 on a single track, and by default it operates on the entirety of that track.
88 */
89
90 int mlt_filter_connect( mlt_filter this, mlt_service producer, int index )
91 {
92 int ret = mlt_service_connect_producer( &this->parent, producer, index );
93
94 // If the connection was successful, grab the producer, track and reset in/out
95 if ( ret == 0 )
96 {
97 mlt_properties properties = MLT_SERVICE_PROPERTIES( &this->parent );
98 mlt_properties_set_position( properties, "in", 0 );
99 mlt_properties_set_position( properties, "out", 0 );
100 mlt_properties_set_int( properties, "track", index );
101 }
102
103 return ret;
104 }
105
106 /** Tune the in/out points.
107 */
108
109 void mlt_filter_set_in_and_out( mlt_filter this, mlt_position in, mlt_position out )
110 {
111 mlt_properties properties = MLT_SERVICE_PROPERTIES( &this->parent );
112 mlt_properties_set_position( properties, "in", in );
113 mlt_properties_set_position( properties, "out", out );
114 }
115
116 /** Return the track that this filter is operating on.
117 */
118
119 int mlt_filter_get_track( mlt_filter this )
120 {
121 mlt_properties properties = MLT_SERVICE_PROPERTIES( &this->parent );
122 return mlt_properties_get_int( properties, "track" );
123 }
124
125 /** Get the in point.
126 */
127
128 mlt_position mlt_filter_get_in( mlt_filter this )
129 {
130 mlt_properties properties = MLT_SERVICE_PROPERTIES( &this->parent );
131 return mlt_properties_get_position( properties, "in" );
132 }
133
134 /** Get the out point.
135 */
136
137 mlt_position mlt_filter_get_out( mlt_filter this )
138 {
139 mlt_properties properties = MLT_SERVICE_PROPERTIES( &this->parent );
140 return mlt_properties_get_position( properties, "out" );
141 }
142
143 /** Process the frame.
144 */
145
146 mlt_frame mlt_filter_process( mlt_filter this, mlt_frame frame )
147 {
148 int disable = mlt_properties_get_int( MLT_FILTER_PROPERTIES( this ), "disable" );
149 if ( disable || this->process == NULL )
150 return frame;
151 else
152 return this->process( this, frame );
153 }
154
155 /** Get a frame from this filter.
156 */
157
158 static int filter_get_frame( mlt_service service, mlt_frame_ptr frame, int index )
159 {
160 mlt_filter this = service->child;
161
162 // Get coords in/out/track
163 int track = mlt_filter_get_track( this );
164 int in = mlt_filter_get_in( this );
165 int out = mlt_filter_get_out( this );
166
167 // Get the producer this is connected to
168 mlt_service producer = mlt_service_producer( &this->parent );
169
170 // If the frame request is for this filters track, we need to process it
171 if ( index == track || track == -1 )
172 {
173 int ret = mlt_service_get_frame( producer, frame, index );
174 if ( ret == 0 )
175 {
176 mlt_position position = mlt_frame_get_position( *frame );
177 if ( position >= in && ( out == 0 || position <= out ) )
178 *frame = mlt_filter_process( this, *frame );
179 return 0;
180 }
181 else
182 {
183 *frame = mlt_frame_init( service );
184 return 0;
185 }
186 }
187 else
188 {
189 return mlt_service_get_frame( producer, frame, index );
190 }
191 }
192
193 /** Close the filter.
194 */
195
196 void mlt_filter_close( mlt_filter this )
197 {
198 if ( this != NULL && mlt_properties_dec_ref( MLT_FILTER_PROPERTIES( this ) ) <= 0 )
199 {
200 if ( this->close != NULL )
201 {
202 this->close( this );
203 }
204 else
205 {
206 this->parent.close = NULL;
207 mlt_service_close( &this->parent );
208 }
209 free( this );
210 }
211 }