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