Filter cleanup and fixes
[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 program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
10 *
11 * This program 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
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software Foundation,
18 * 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 // Default in, out, track properties
48 mlt_properties_set_position( properties, "in", 0 );
49 mlt_properties_set_position( properties, "out", 0 );
50 mlt_properties_set_int( properties, "track", 0 );
51
52 return 0;
53 }
54 return 1;
55 }
56
57 /** Create a new filter.
58 */
59
60 mlt_filter mlt_filter_new( )
61 {
62 mlt_filter this = calloc( 1, sizeof( struct mlt_filter_s ) );
63 if ( this != NULL )
64 mlt_filter_init( this, NULL );
65 return this;
66 }
67
68 /** Get the service associated to this filter
69 */
70
71 mlt_service mlt_filter_service( mlt_filter this )
72 {
73 return &this->parent;
74 }
75
76 /** Get the properties associated to this filter.
77 */
78
79 mlt_properties mlt_filter_properties( mlt_filter this )
80 {
81 return mlt_service_properties( mlt_filter_service( this ) );
82 }
83
84 /** Connect this filter to a producers track. Note that a filter only operates
85 on a single track, and by default it operates on the entirety of that track.
86 */
87
88 int mlt_filter_connect( mlt_filter this, mlt_service producer, int index )
89 {
90 int ret = mlt_service_connect_producer( &this->parent, producer, index );
91
92 // If the connection was successful, grab the producer, track and reset in/out
93 if ( ret == 0 )
94 {
95 mlt_properties properties = mlt_service_properties( &this->parent );
96 mlt_properties_set_position( properties, "in", 0 );
97 mlt_properties_set_position( properties, "out", 0 );
98 mlt_properties_set_int( properties, "track", index );
99 }
100
101 return ret;
102 }
103
104 /** Tune the in/out points.
105 */
106
107 void mlt_filter_set_in_and_out( mlt_filter this, mlt_position in, mlt_position out )
108 {
109 mlt_properties properties = mlt_service_properties( &this->parent );
110 mlt_properties_set_position( properties, "in", in );
111 mlt_properties_set_position( properties, "out", out );
112 }
113
114 /** Return the track that this filter is operating on.
115 */
116
117 int mlt_filter_get_track( mlt_filter this )
118 {
119 mlt_properties properties = mlt_service_properties( &this->parent );
120 return mlt_properties_get_int( properties, "track" );
121 }
122
123 /** Get the in point.
124 */
125
126 mlt_position mlt_filter_get_in( mlt_filter this )
127 {
128 mlt_properties properties = mlt_service_properties( &this->parent );
129 return mlt_properties_get_position( properties, "in" );
130 }
131
132 /** Get the out point.
133 */
134
135 mlt_position mlt_filter_get_out( mlt_filter this )
136 {
137 mlt_properties properties = mlt_service_properties( &this->parent );
138 return mlt_properties_get_position( properties, "out" );
139 }
140
141 /** Process the frame.
142 */
143
144 mlt_frame mlt_filter_process( mlt_filter this, mlt_frame frame )
145 {
146 if ( this->process == NULL )
147 return frame;
148 else
149 return this->process( this, frame );
150 }
151
152 /** Get a frame from this filter.
153 */
154
155 static int filter_get_frame( mlt_service service, mlt_frame_ptr frame, int index )
156 {
157 mlt_filter this = service->child;
158
159 // Get coords in/out/track
160 int track = mlt_filter_get_track( this );
161 int in = mlt_filter_get_in( this );
162 int out = mlt_filter_get_out( this );
163
164 // Get the producer this is connected to
165 mlt_service producer = mlt_service_producer( &this->parent );
166
167 // If the frame request is for this filters track, we need to process it
168 if ( index == track )
169 {
170 int ret = mlt_service_get_frame( producer, frame, index );
171 if ( ret == 0 )
172 {
173 mlt_position position = mlt_frame_get_position( *frame );
174 if ( position >= in && ( out == 0 || position <= out ) )
175 *frame = mlt_filter_process( this, *frame );
176 return 0;
177 }
178 else
179 {
180 *frame = mlt_frame_init( );
181 return 0;
182 }
183 }
184 else
185 {
186 return mlt_service_get_frame( producer, frame, index );
187 }
188 }
189
190 /** Close the filter.
191 */
192
193 void mlt_filter_close( mlt_filter this )
194 {
195 if ( this->close != NULL )
196 this->close( this );
197 else
198 mlt_service_close( &this->parent );
199 free( this );
200 }