updated westley
[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 mlt_properties_set( properties, "resource", "<filter>" );
52
53 return 0;
54 }
55 return 1;
56 }
57
58 /** Get the service associated to this filter
59 */
60
61 mlt_service mlt_filter_service( mlt_filter this )
62 {
63 return &this->parent;
64 }
65
66 mlt_properties mlt_filter_properties( mlt_filter this )
67 {
68 return mlt_service_properties( mlt_filter_service( this ) );
69 }
70
71 /** Connect this filter to a producers track. Note that a filter only operates
72 on a single track, and by default it operates on the entirety of that track.
73 */
74
75 int mlt_filter_connect( mlt_filter this, mlt_service producer, int index )
76 {
77 int ret = mlt_service_connect_producer( &this->parent, producer, index );
78
79 // If the connection was successful, grab the producer, track and reset in/out
80 if ( ret == 0 )
81 {
82 mlt_properties properties = mlt_service_properties( &this->parent );
83 this->producer = producer;
84 mlt_properties_set_position( properties, "in", 0 );
85 mlt_properties_set_position( properties, "out", 0 );
86 mlt_properties_set_int( properties, "track", index );
87 }
88
89 return ret;
90 }
91
92 /** Tune the in/out points.
93 */
94
95 void mlt_filter_set_in_and_out( mlt_filter this, mlt_position in, mlt_position out )
96 {
97 mlt_properties properties = mlt_service_properties( &this->parent );
98 mlt_properties_set_position( properties, "in", in );
99 mlt_properties_set_position( properties, "out", out );
100 }
101
102 /** Return the track that this filter is operating on.
103 */
104
105 int mlt_filter_get_track( mlt_filter this )
106 {
107 mlt_properties properties = mlt_service_properties( &this->parent );
108 return mlt_properties_get_int( properties, "track" );
109 }
110
111 /** Get the in point.
112 */
113
114 mlt_position mlt_filter_get_in( mlt_filter this )
115 {
116 mlt_properties properties = mlt_service_properties( &this->parent );
117 return mlt_properties_get_position( properties, "in" );
118 }
119
120 /** Get the out point.
121 */
122
123 mlt_position mlt_filter_get_out( mlt_filter this )
124 {
125 mlt_properties properties = mlt_service_properties( &this->parent );
126 return mlt_properties_get_position( properties, "out" );
127 }
128
129 /** Process the frame.
130 */
131
132 static mlt_frame filter_process( mlt_filter this, mlt_frame frame )
133 {
134 if ( this->process == NULL )
135 return frame;
136 else
137 return this->process( this, frame );
138 }
139
140 /** Get a frame from this filter.
141 */
142
143 static int filter_get_frame( mlt_service service, mlt_frame_ptr frame, int index )
144 {
145 mlt_filter this = service->child;
146
147 // Get coords in/out/track
148 int track = mlt_filter_get_track( this );
149 int in = mlt_filter_get_in( this );
150 int out = mlt_filter_get_out( this );
151
152 // If the frame request is for this filters track, we need to process it
153 if ( index == track )
154 {
155 int ret = mlt_service_get_frame( this->producer, frame, index );
156 if ( ret == 0 )
157 {
158 if ( !mlt_frame_is_test_card( *frame ) )
159 {
160 mlt_position position = mlt_frame_get_position( *frame );
161 if ( position >= in && ( out == 0 || position < out ) )
162 *frame = filter_process( this, *frame );
163 }
164 return 0;
165 }
166 else
167 {
168 *frame = mlt_frame_init( );
169 return 0;
170 }
171 }
172 else
173 {
174 return mlt_service_get_frame( this->producer, frame, index );
175 }
176 }
177
178 /** Close the filter.
179 */
180
181 void mlt_filter_close( mlt_filter this )
182 {
183 if ( this->close != NULL )
184 this->close( this );
185 else
186 mlt_service_close( &this->parent );
187 }
188