]> git.sesse.net Git - mlt/blob - src/modules/core/filter_data_show.c
2843ddee2df7c11cba6cf79278629956c923d318
[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                 // Get the profile requested
48                 char *profile = mlt_properties_get( filter_properties, "profile" );
49
50                 // Load the specified profile or use the default
51                 if ( profile != NULL )
52                 {
53                         profile_properties = mlt_properties_load( profile );
54                 }
55                 else
56                 {
57                         // Sometimes C can be laborious.. 
58                         static char *default_file = "/data_fx.properties";
59                         char *temp = malloc( strlen( mlt_factory_prefix( ) ) + strlen( default_file ) + 1 );
60                         if ( temp != NULL )
61                         {
62                                 strcpy( temp, mlt_factory_prefix( ) );
63                                 strcat( temp, default_file );
64                                 profile_properties = mlt_properties_load( temp );
65                                 free( temp );
66                         }
67                 }
68
69                 // Store for later retrieval
70                 mlt_properties_set_data( filter_properties, "profile_properties", profile_properties, 0, ( mlt_destructor )mlt_properties_close, NULL );
71         }
72
73         if ( profile_properties != NULL )
74         {
75                 for ( i = 0; i < mlt_properties_count( profile_properties ); i ++ )
76                 {
77                         char *name = mlt_properties_get_name( profile_properties, i );
78                         char *value = mlt_properties_get_value( profile_properties, i );
79         
80                         if ( result == NULL && !strcmp( name, type ) && result == NULL )
81                                 result = mlt_factory_filter( value, NULL );
82                         else if ( result != NULL && !strncmp( name, type, type_len ) && name[ type_len ] == '.' )
83                                 mlt_properties_set( mlt_filter_properties( result ), name + type_len + 1, value );
84                         else if ( result != NULL )
85                                 break;
86                 }
87         }
88
89         return result;
90 }
91
92 /** Process the frame for the requested type
93 */
94
95 static int process_feed( mlt_properties feed, mlt_filter filter, mlt_frame frame )
96 {
97         // Error return
98         int error = 1;
99
100         // Get the properties of the data show filter
101         mlt_properties filter_properties = mlt_filter_properties( filter );
102
103         // Get the type requested by the feeding filter
104         char *type = mlt_properties_get( feed, "type" );
105
106         // Fetch the filter associated to this type
107         mlt_filter requested = mlt_properties_get_data( filter_properties, type, NULL );
108
109         // Calculate the length of the feed
110         int length = mlt_properties_get_int( feed, "out" ) - mlt_properties_get_int( feed, "in" ) + 1;
111
112         // If it doesn't exist, then create it now
113         if ( requested == NULL )
114         {
115                 // Source filter from profile
116                 requested = obtain_filter( filter, type );
117
118                 // Store it on the properties for subsequent retrieval/destruction
119                 mlt_properties_set_data( filter_properties, type, requested, 0, ( mlt_destructor )mlt_filter_close, NULL );
120         }
121
122         // If we have one, then process it now...
123         if ( requested != NULL )
124         {
125                 int i = 0;
126                 mlt_properties properties = mlt_filter_properties( requested );
127                 static char *prefix = "properties.";
128                 int len = strlen( prefix );
129
130                 // Pass properties from feed into requested
131                 for ( i = 0; i < mlt_properties_count( properties ); i ++ )
132                 {
133                         char *name = mlt_properties_get_name( properties, i );
134                         char *key = mlt_properties_get_value( properties, i );
135                         if ( !strncmp( name, prefix, len ) )
136                         {
137                                 if ( !strncmp( name + len, "length[", 7 ) )
138                                 {
139                                         int period = mlt_properties_get_int( properties, "period" );
140                                         period = period == 0 ? 1 : period;
141                                         mlt_properties_set_position( properties, key, length / period );
142                                 }
143                                 else
144                                 {
145                                         char *value = mlt_properties_get( feed, name + len );
146                                         if ( value != NULL )
147                                                 mlt_properties_set( properties, key, value );
148                                 }
149                         }
150                 }
151
152                 // Set the original position on the frame
153                 mlt_frame_set_position( frame, mlt_properties_get_int( feed, "position" ) );
154
155                 // Process the filter
156                 mlt_filter_process( requested, frame );
157
158                 // Should be ok...
159                 error = 0;
160         }
161
162         return error;
163 }
164
165 /** Get the image.
166 */
167
168 static int filter_get_image( mlt_frame frame, uint8_t **image, mlt_image_format *format, int *width, int *height, int writable )
169 {
170         // Pop the service
171         mlt_filter filter = mlt_frame_pop_service( frame );
172
173         // Get the frame properties
174         mlt_properties frame_properties = mlt_frame_properties( frame );
175
176         // Fetch the data queue
177         mlt_deque data_queue = mlt_properties_get_data( frame_properties, "data_queue", NULL );
178
179         // Iterate through each entry on the queue
180         while ( data_queue != NULL && mlt_deque_peek_front( data_queue ) != NULL )
181         {
182                 // Get the data feed
183                 mlt_properties feed = mlt_deque_pop_front( data_queue );
184
185                 // Process the data feed...
186                 process_feed( feed, filter, frame );
187
188                 // Close the feed
189                 mlt_properties_close( feed );
190         }
191
192         // Need to get the image
193         return mlt_frame_get_image( frame, image, format, width, height, 1 );
194 }
195
196
197 /** Filter processing.
198 */
199
200 static mlt_frame filter_process( mlt_filter this, mlt_frame frame )
201 {
202         // Push the filter
203         mlt_frame_push_service( frame, this );
204
205         // Register the get image method
206         mlt_frame_push_get_image( frame, filter_get_image );
207
208         // Return the frame
209         return frame;
210 }
211
212 /** Constructor for the filter.
213 */
214
215 mlt_filter filter_data_show_init( char *arg )
216 {
217         // Create the filter
218         mlt_filter this = mlt_filter_new( );
219
220         // Initialise it
221         if ( this != NULL )
222         {
223                 // Get the properties
224                 mlt_properties properties = mlt_filter_properties( this );
225
226                 // Assign the argument (default to titles)
227                 mlt_properties_set( properties, "profile", arg == NULL ? NULL : arg );
228
229                 // Specify the processing method
230                 this->process = filter_process;
231         }
232
233         return this;
234 }
235