]> git.sesse.net Git - mlt/blob - src/modules/core/filter_data_show.c
b16f8fe40c33effe306e1ff0da7fe05c57789345
[mlt] / src / modules / core / filter_data_show.c
1 /*
2  * filter_data_show.c -- data feed filter
3  * Copyright (C) 2004-2005 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 "filter_data.h"
22 #include <framework/mlt.h>
23 #include <stdlib.h>
24 #include <string.h>
25
26 /** Handle the profile.
27 */
28
29 static mlt_filter obtain_filter( mlt_filter filter, char *type )
30 {
31         // Result to return
32         mlt_filter result = NULL;
33
34         // Miscelaneous variable
35         int i = 0;
36         int type_len = strlen( type );
37
38         // Get the properties of the data show filter
39         mlt_properties filter_properties = MLT_FILTER_PROPERTIES( filter );
40
41         // Get the profile properties
42         mlt_properties profile_properties = mlt_properties_get_data( filter_properties, "profile_properties", NULL );
43
44         // Obtain the profile_properties if we haven't already
45         if ( profile_properties == NULL )
46         {
47                 char temp[ 512 ];
48
49                 // Get the profile requested
50                 char *profile = mlt_properties_get( filter_properties, "resource" );
51
52                 // If none is specified, pick up the default for this normalisation
53                 if ( profile == NULL )
54                         sprintf( temp, "%s/feeds/%s/data_fx.properties", mlt_factory_prefix( ), mlt_environment( "MLT_NORMALISATION" ) );
55                 else if ( strchr( profile, '%' ) )
56                         sprintf( temp, "%s/feeds/%s/%s", mlt_factory_prefix( ), mlt_environment( "MLT_NORMALISATION" ), strchr( profile, '%' ) + 1 );
57                 else
58                         strcpy( temp, profile );
59
60                 // Load the specified profile or use the default
61                 profile_properties = mlt_properties_load( temp );
62
63                 // Store for later retrieval
64                 mlt_properties_set_data( filter_properties, "profile_properties", profile_properties, 0, ( mlt_destructor )mlt_properties_close, NULL );
65         }
66
67         if ( profile_properties != NULL )
68         {
69                 for ( i = 0; i < mlt_properties_count( profile_properties ); i ++ )
70                 {
71                         char *name = mlt_properties_get_name( profile_properties, i );
72                         char *value = mlt_properties_get_value( profile_properties, i );
73         
74                         if ( result == NULL && !strcmp( name, type ) && result == NULL )
75                                 result = mlt_factory_filter( value, NULL );
76                         else if ( result != NULL && !strncmp( name, type, type_len ) && name[ type_len ] == '.' )
77                                 mlt_properties_set( MLT_FILTER_PROPERTIES( result ), name + type_len + 1, value );
78                         else if ( result != NULL )
79                                 break;
80                 }
81         }
82
83         return result;
84 }
85
86 /** Process the frame for the requested type
87 */
88
89 static int process_feed( mlt_properties feed, mlt_filter filter, mlt_frame frame )
90 {
91         // Error return
92         int error = 1;
93
94         // Get the properties of the data show filter
95         mlt_properties filter_properties = MLT_FILTER_PROPERTIES( filter );
96
97         // Get the type requested by the feeding filter
98         char *type = mlt_properties_get( feed, "type" );
99
100         // Fetch the filter associated to this type
101         mlt_filter requested = mlt_properties_get_data( filter_properties, type, NULL );
102
103         // Calculate the length of the feed
104         int length = mlt_properties_get_int( feed, "out" ) - mlt_properties_get_int( feed, "in" ) + 1;
105
106         // If it doesn't exist, then create it now
107         if ( requested == NULL )
108         {
109                 // Source filter from profile
110                 requested = obtain_filter( filter, type );
111
112                 // Store it on the properties for subsequent retrieval/destruction
113                 mlt_properties_set_data( filter_properties, type, requested, 0, ( mlt_destructor )mlt_filter_close, NULL );
114         }
115
116         // If we have one, then process it now...
117         if ( requested != NULL )
118         {
119                 int i = 0;
120                 mlt_properties properties = MLT_FILTER_PROPERTIES( requested );
121                 static char *prefix = "properties.";
122                 int len = strlen( prefix );
123
124                 // Pass properties from feed into requested
125                 for ( i = 0; i < mlt_properties_count( properties ); i ++ )
126                 {
127                         char *name = mlt_properties_get_name( properties, i );
128                         char *key = mlt_properties_get_value( properties, i );
129                         if ( !strncmp( name, prefix, len ) )
130                         {
131                                 if ( !strncmp( name + len, "length[", 7 ) )
132                                 {
133                                         int period = mlt_properties_get_int( properties, "period" );
134                                         period = period == 0 ? 1 : period;
135                                         mlt_properties_set_position( properties, key, length / period );
136                                 }
137                                 else
138                                 {
139                                         char *value = mlt_properties_get( feed, name + len );
140                                         if ( value != NULL )
141                                                 mlt_properties_set( properties, key, value );
142                                 }
143                         }
144                 }
145
146                 // Set the original position on the frame
147                 mlt_frame_set_position( frame, mlt_properties_get_int( feed, "position" ) - mlt_properties_get_int( feed, "in" ) );
148
149                 // Process the filter
150                 mlt_filter_process( requested, frame );
151
152                 // Should be ok...
153                 error = 0;
154         }
155
156         return error;
157 }
158
159 /** Get the image.
160 */
161
162 static int filter_get_image( mlt_frame frame, uint8_t **image, mlt_image_format *format, int *width, int *height, int writable )
163 {
164         // Pop the service
165         mlt_filter filter = mlt_frame_pop_service( frame );
166
167         // Get the frame properties
168         mlt_properties frame_properties = MLT_FRAME_PROPERTIES( frame );
169
170         // Fetch the data queue
171         mlt_deque data_queue = mlt_properties_get_data( frame_properties, "data_queue", NULL );
172
173         // Create a new queue for those that we can't handle
174         mlt_deque temp_queue = mlt_deque_init( );
175
176         // Iterate through each entry on the queue
177         while ( data_queue != NULL && mlt_deque_peek_front( data_queue ) != NULL )
178         {
179                 // Get the data feed
180                 mlt_properties feed = mlt_deque_pop_front( data_queue );
181
182                 // Process the data feed...
183                 if ( process_feed( feed, filter, frame ) == 0 )
184                         mlt_properties_close( feed );
185                 else
186                         mlt_deque_push_back( temp_queue, feed );
187         }
188
189         // Now put the unprocessed feeds back on the stack
190         while ( data_queue != NULL && mlt_deque_peek_front( temp_queue ) )
191         {
192                 // Get the data feed
193                 mlt_properties feed = mlt_deque_pop_front( temp_queue );
194
195                 // Put it back on the data queue
196                 mlt_deque_push_back( data_queue, feed );
197         }
198
199         // Close the temporary queue
200         mlt_deque_close( temp_queue );
201
202         // Need to get the image
203         return mlt_frame_get_image( frame, image, format, width, height, 1 );
204 }
205
206
207 /** Filter processing.
208 */
209
210 static mlt_frame filter_process( mlt_filter this, mlt_frame frame )
211 {
212         // Push the filter
213         mlt_frame_push_service( frame, this );
214
215         // Register the get image method
216         mlt_frame_push_get_image( frame, filter_get_image );
217
218         // Return the frame
219         return frame;
220 }
221
222 /** Constructor for the filter.
223 */
224
225 mlt_filter filter_data_show_init( char *arg )
226 {
227         // Create the filter
228         mlt_filter this = mlt_filter_new( );
229
230         // Initialise it
231         if ( this != NULL )
232         {
233                 // Get the properties
234                 mlt_properties properties = MLT_FILTER_PROPERTIES( this );
235
236                 // Assign the argument (default to titles)
237                 mlt_properties_set( properties, "resource", arg == NULL ? NULL : arg );
238
239                 // Specify the processing method
240                 this->process = filter_process;
241         }
242
243         return this;
244 }
245