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