b8fcd4cfde90d55f4dd53b671552714665e2fb89
[melted] / 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 /** Retrieve medatata value
87 */
88
89 const char* metadata_value(mlt_properties properties, char* name)
90 {
91 if (name == NULL) return "-";
92 char *meta = malloc( strlen(name) + 18 );
93 sprintf( meta, "meta.attr.%s.markup", name);
94 return mlt_properties_get( properties, meta);
95 }
96
97 /** Convert frames to Timecode
98 */
99
100 const char* frame_to_timecode( int frames , int fps)
101 {
102 char *res = malloc(12);
103 if (fps == 0) return "-";
104 int seconds = frames / (int) fps;
105 frames = frames % ((int) fps);
106 int minutes = seconds / 60;
107 seconds = seconds % 60;
108 int hours = minutes / 60;
109 minutes = minutes % 60;
110 sprintf(res, "%.2d:%.2d:%.2d:%.2d", hours, minutes, seconds, frames);
111 return res;
112 }
113
114 /** Process the frame for the requested type
115 */
116
117 static int process_feed( mlt_properties feed, mlt_filter filter, mlt_frame frame )
118 {
119 // Error return
120 int error = 1;
121
122 // Get the properties of the data show filter
123 mlt_properties filter_properties = MLT_FILTER_PROPERTIES( filter );
124
125 // Get the type requested by the feeding filter
126 char *type = mlt_properties_get( feed, "type" );
127
128 // Fetch the filter associated to this type
129 mlt_filter requested = mlt_properties_get_data( filter_properties, type, NULL );
130
131 // If it doesn't exist, then create it now
132 if ( requested == NULL )
133 {
134 // Source filter from profile
135 requested = obtain_filter( filter, type );
136
137 // Store it on the properties for subsequent retrieval/destruction
138 mlt_properties_set_data( filter_properties, type, requested, 0, ( mlt_destructor )mlt_filter_close, NULL );
139 }
140
141 // If we have one, then process it now...
142 if ( requested != NULL )
143 {
144 int i = 0;
145 mlt_properties properties = MLT_FILTER_PROPERTIES( requested );
146 static char *prefix = "properties.";
147 int len = strlen( prefix );
148
149 // Determine if this is an absolute or relative feed
150 int absolute = mlt_properties_get_int( feed, "absolute" );
151
152 // Make do with what we have
153 int length = !absolute ?
154 mlt_properties_get_int( feed, "out" ) - mlt_properties_get_int( feed, "in" ) + 1 :
155 mlt_properties_get_int( feed, "out" ) + 1;
156
157 // Repeat period
158 int period = mlt_properties_get_int( properties, "period" );
159 period = period == 0 ? 1 : period;
160
161 // Pass properties from feed into requested
162 for ( i = 0; i < mlt_properties_count( properties ); i ++ )
163 {
164 char *name = mlt_properties_get_name( properties, i );
165 char *key = mlt_properties_get_value( properties, i );
166 if ( !strncmp( name, prefix, len ) )
167 {
168 if ( !strncmp( name + len, "length[", 7 ) )
169 {
170 mlt_properties_set_position( properties, key, ( length - period ) / period );
171 }
172 else
173 {
174 char *value = mlt_properties_get( feed, name + len );
175 if ( value != NULL ) {
176 // check for metadata keywords in metadata markup if user requested so
177 if ( mlt_properties_get_int( filter_properties, "dynamic" ) == 1 && !strcmp( name + strlen( name ) - 6, "markup") )
178 {
179 // Find keywords which should be surrounded by '#', like: #title#
180 char* keywords = strtok ( value, "\\#" );
181 char result[] = "";
182 int ct = 0;
183 int fromStart = 0;
184 if ( value[0] == '\\' ) fromStart = 1;
185
186 while (keywords != NULL) {
187 if (ct % 2 == fromStart)
188 strcat( result, keywords);
189 else if (!strcmp(keywords, "timecode")) {
190 // special case: replace #tc# with current frame timecode
191 int pos = mlt_properties_get_int( feed, "position" );
192 const char *tc = frame_to_timecode(pos, mlt_properties_get_int( MLT_FRAME_PROPERTIES( frame ), "fps" ));
193 strcat( result, tc);
194 }
195 else {
196 // replace keyword with metadata value
197 const char * metavalue = metadata_value(MLT_FRAME_PROPERTIES( frame ), keywords);
198 if (metavalue == NULL) metavalue = "-";
199 strcat( result, metavalue);
200 }
201 keywords = strtok ( NULL, "\\#" );
202 ct++;
203 }
204 mlt_properties_set( properties, key, (char*) result );
205 }
206 else mlt_properties_set( properties, key, value );
207 }
208 }
209 }
210 }
211
212 // Set the original position on the frame
213 if ( absolute == 0 )
214 mlt_frame_set_position( frame, mlt_properties_get_int( feed, "position" ) - mlt_properties_get_int( feed, "in" ) );
215 else
216 mlt_frame_set_position( frame, mlt_properties_get_int( feed, "position" ) );
217
218 // Process the filter
219 mlt_filter_process( requested, frame );
220
221 // Should be ok...
222 error = 0;
223 }
224
225 return error;
226 }
227
228 void process_queue( mlt_deque data_queue, mlt_frame frame, mlt_filter filter )
229 {
230 if ( data_queue != NULL )
231 {
232 // Create a new queue for those that we can't handle
233 mlt_deque temp_queue = mlt_deque_init( );
234
235 // Iterate through each entry on the queue
236 while ( mlt_deque_peek_front( data_queue ) != NULL )
237 {
238 // Get the data feed
239 mlt_properties feed = mlt_deque_pop_front( data_queue );
240
241 if ( mlt_properties_get( MLT_FILTER_PROPERTIES( filter ), "debug" ) != NULL )
242 mlt_properties_debug( feed, mlt_properties_get( MLT_FILTER_PROPERTIES( filter ), "debug" ), stderr );
243
244 // Process the data feed...
245 if ( process_feed( feed, filter, frame ) == 0 )
246 mlt_properties_close( feed );
247 else
248 mlt_deque_push_back( temp_queue, feed );
249 }
250
251 // Now put the unprocessed feeds back on the stack
252 while ( mlt_deque_peek_front( temp_queue ) )
253 {
254 // Get the data feed
255 mlt_properties feed = mlt_deque_pop_front( temp_queue );
256
257 // Put it back on the data queue
258 mlt_deque_push_back( data_queue, feed );
259 }
260
261 // Close the temporary queue
262 mlt_deque_close( temp_queue );
263 }
264 }
265
266 /** Get the image.
267 */
268
269 static int filter_get_image( mlt_frame frame, uint8_t **image, mlt_image_format *format, int *width, int *height, int writable )
270 {
271 // Pop the service
272 mlt_filter filter = mlt_frame_pop_service( frame );
273
274 // Get the frame properties
275 mlt_properties frame_properties = MLT_FRAME_PROPERTIES( frame );
276
277 // Track specific
278 process_queue( mlt_properties_get_data( frame_properties, "data_queue", NULL ), frame, filter );
279
280 // Global
281 process_queue( mlt_properties_get_data( frame_properties, "global_queue", NULL ), frame, filter );
282
283 // Need to get the image
284 return mlt_frame_get_image( frame, image, format, width, height, 1 );
285 }
286
287
288 /** Filter processing.
289 */
290
291 static mlt_frame filter_process( mlt_filter this, mlt_frame frame )
292 {
293 // Push the filter
294 mlt_frame_push_service( frame, this );
295
296 // Register the get image method
297 mlt_frame_push_get_image( frame, filter_get_image );
298
299 // Return the frame
300 return frame;
301 }
302
303 /** Constructor for the filter.
304 */
305
306 mlt_filter filter_data_show_init( char *arg )
307 {
308 // Create the filter
309 mlt_filter this = mlt_filter_new( );
310
311 // Initialise it
312 if ( this != NULL )
313 {
314 // Get the properties
315 mlt_properties properties = MLT_FILTER_PROPERTIES( this );
316
317 // Assign the argument (default to titles)
318 mlt_properties_set( properties, "resource", arg == NULL ? NULL : arg );
319
320 // Specify the processing method
321 this->process = filter_process;
322 }
323
324 return this;
325 }
326