Initial revision
[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 service->get_frame = filter_get_frame;
43 return 0;
44 }
45 return 1;
46 }
47
48 /** Get the service associated to this filter
49 */
50
51 mlt_service mlt_filter_service( mlt_filter this )
52 {
53 return &this->parent;
54 }
55
56 /** Connect this filter to a producers track. Note that a filter only operates
57 on a single track, and by default it operates on the entirety of that track.
58 */
59
60 int mlt_filter_connect( mlt_filter this, mlt_service producer, int index )
61 {
62 int ret = mlt_service_connect_producer( &this->parent, producer, index );
63
64 // If the connection was successful, grab the producer, track and reset in/out
65 if ( ret == 0 )
66 {
67 this->producer = producer;
68 this->track = index;
69 this->in = 0;
70 this->out = 0;
71 }
72
73 return ret;
74 }
75
76 /** Tune the in/out points.
77 */
78
79 void mlt_filter_set_in_and_out( mlt_filter this, mlt_timecode in, mlt_timecode out )
80 {
81 this->in = in;
82 this->out = out;
83 }
84
85 /** Return the track that this filter is operating on.
86 */
87
88 int mlt_filter_get_track( mlt_filter this )
89 {
90 return this->track;
91 }
92
93 /** Get the in point.
94 */
95
96 mlt_timecode mlt_filter_get_in( mlt_filter this )
97 {
98 return this->in;
99 }
100
101 /** Get the out point.
102 */
103
104 mlt_timecode mlt_filter_get_out( mlt_filter this )
105 {
106 return this->out;
107 }
108
109 /** Process the frame.
110 */
111
112 static mlt_frame filter_process( mlt_filter this, mlt_frame frame )
113 {
114 if ( this->process == NULL )
115 return frame;
116 else
117 return this->process( this, frame );
118 }
119
120 /** Get a frame from this filter.
121 */
122
123 static int filter_get_frame( mlt_service service, mlt_frame_ptr frame, int index )
124 {
125 mlt_filter this = service->child;
126
127 // If the frame request is for this filters track, we need to process it
128 if ( index == this->track )
129 {
130 int ret = mlt_service_get_frame( this->producer, frame, index );
131 if ( ret == 0 )
132 {
133 if ( !mlt_frame_is_test_card( *frame ) )
134 {
135 mlt_timecode timecode = mlt_frame_get_timecode( *frame );
136 if ( timecode >= this->in && ( this->out == 0 || timecode < this->out ) )
137 *frame = filter_process( this, *frame );
138 }
139 return 0;
140 }
141 else
142 {
143 *frame = mlt_frame_init( );
144 return 0;
145 }
146 }
147 else
148 {
149 return mlt_service_get_frame( this->producer, frame, index );
150 }
151 }
152
153 /** Close the filter.
154 */
155
156 void mlt_filter_close( mlt_filter this )
157 {
158 if ( this->close != NULL )
159 this->close( this );
160 else
161 mlt_service_close( &this->parent );
162 }
163