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