]> git.sesse.net Git - mlt/blob - src/modules/core/filter_data_show.c
move binary modules to libdir - affects MLT_REPOSITORY
[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 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 <framework/mlt.h>
22 #include <stdlib.h>
23 #include <string.h>
24
25 /** Handle the profile.
26 */
27
28 static mlt_filter obtain_filter( mlt_filter filter, char *type )
29 {
30         // Result to return
31         mlt_filter result = NULL;
32
33         // Miscelaneous variable
34         int i = 0;
35         int type_len = strlen( type );
36
37         // Get the properties of the data show filter
38         mlt_properties filter_properties = MLT_FILTER_PROPERTIES( filter );
39
40         // Get the profile properties
41         mlt_properties profile_properties = mlt_properties_get_data( filter_properties, "profile_properties", NULL );
42
43         // Obtain the profile_properties if we haven't already
44         if ( profile_properties == NULL )
45         {
46                 char temp[ 512 ];
47
48                 // Get the profile requested
49                 char *profile = mlt_properties_get( filter_properties, "resource" );
50
51                 // If none is specified, pick up the default for this normalisation
52                 if ( profile == NULL )
53                         sprintf( temp, "%s/feeds/%s/data_fx.properties", mlt_environment( "MLT_DATA" ), mlt_environment( "MLT_NORMALISATION" ) );
54                 else if ( strchr( profile, '%' ) )
55                         sprintf( temp, "%s/feeds/%s/%s", mlt_environment( "MLT_DATA" ), mlt_environment( "MLT_NORMALISATION" ), strchr( profile, '%' ) + 1 );
56                 else
57                         strcpy( temp, profile );
58
59                 // Load the specified profile or use the default
60                 profile_properties = mlt_properties_load( temp );
61
62                 // Store for later retrieval
63                 mlt_properties_set_data( filter_properties, "profile_properties", profile_properties, 0, ( mlt_destructor )mlt_properties_close, NULL );
64         }
65
66         if ( profile_properties != NULL )
67         {
68                 for ( i = 0; i < mlt_properties_count( profile_properties ); i ++ )
69                 {
70                         char *name = mlt_properties_get_name( profile_properties, i );
71                         char *value = mlt_properties_get_value( profile_properties, i );
72         
73                         if ( result == NULL && !strcmp( name, type ) && result == NULL )
74                                 result = mlt_factory_filter( mlt_service_profile( MLT_FILTER_SERVICE( filter ) ), value, NULL );
75                         else if ( result != NULL && !strncmp( name, type, type_len ) && name[ type_len ] == '.' )
76                                 mlt_properties_set( MLT_FILTER_PROPERTIES( result ), name + type_len + 1, value );
77                         else if ( result != NULL )
78                                 break;
79                 }
80         }
81
82         return result;
83 }
84
85 /** Retrieve medatata value 
86 */
87
88 char* metadata_value(mlt_properties properties, char* name)
89 {
90         if (name == NULL) return NULL;
91         char *meta = malloc( strlen(name) + 18 );
92         sprintf( meta, "meta.attr.%s.markup", name);
93         char *result = mlt_properties_get( properties, meta);
94         free(meta);
95         return result;
96 }
97
98 /** Convert frames to Timecode 
99 */
100
101 char* frame_to_timecode( int frames , int fps)
102 {
103         if (fps == 0) return strdup("-");
104         char *res = malloc(12);
105         int seconds = frames / (int) fps;
106         frames = frames % ((int) fps);
107         int minutes = seconds / 60;
108         seconds = seconds % 60;
109         int hours = minutes / 60;
110         minutes = minutes % 60;
111         sprintf(res, "%.2d:%.2d:%.2d:%.2d", hours, minutes, seconds, frames);
112         return res;
113 }
114
115 /** Process the frame for the requested type
116 */
117
118 static int process_feed( mlt_properties feed, mlt_filter filter, mlt_frame frame )
119 {
120         // Error return
121         int error = 1;
122
123         // Get the properties of the data show filter
124         mlt_properties filter_properties = MLT_FILTER_PROPERTIES( filter );
125
126         // Get the type requested by the feeding filter
127         char *type = mlt_properties_get( feed, "type" );
128
129         // Fetch the filter associated to this type
130         mlt_filter requested = mlt_properties_get_data( filter_properties, type, NULL );
131
132         // If it doesn't exist, then create it now
133         if ( requested == NULL )
134         {
135                 // Source filter from profile
136                 requested = obtain_filter( filter, type );
137
138                 // Store it on the properties for subsequent retrieval/destruction
139                 mlt_properties_set_data( filter_properties, type, requested, 0, ( mlt_destructor )mlt_filter_close, NULL );
140         }
141
142         // If we have one, then process it now...
143         if ( requested != NULL )
144         {
145                 int i = 0;
146                 mlt_properties properties = MLT_FILTER_PROPERTIES( requested );
147                 static char *prefix = "properties.";
148                 int len = strlen( prefix );
149
150                 // Determine if this is an absolute or relative feed
151                 int absolute = mlt_properties_get_int( feed, "absolute" );
152
153                 // Make do with what we have
154                 int length = !absolute ? 
155                                          mlt_properties_get_int( feed, "out" ) - mlt_properties_get_int( feed, "in" ) + 1 :
156                                          mlt_properties_get_int( feed, "out" ) + 1;
157
158                 // Repeat period
159                 int period = mlt_properties_get_int( properties, "period" );
160                 period = period == 0 ? 1 : period;
161
162                 // Pass properties from feed into requested
163                 for ( i = 0; i < mlt_properties_count( properties ); i ++ )
164                 {
165                         char *name = mlt_properties_get_name( properties, i );
166                         char *key = mlt_properties_get_value( properties, i );
167                         if ( !strncmp( name, prefix, len ) )
168                         {
169                                 if ( !strncmp( name + len, "length[", 7 ) )
170                                 {
171                                         mlt_properties_set_position( properties, key, ( length - period ) / period );
172                                 }
173                                 else
174                                 {
175                                         char *value = mlt_properties_get( feed, name + len );
176                                         if ( value != NULL )
177                                         {
178                                                 // check for metadata keywords in metadata markup if user requested so
179                                                 if ( mlt_properties_get_int( filter_properties, "dynamic" ) == 1  && !strcmp( name + strlen( name ) - 6, "markup") )
180                                                 {
181                                                         // Find keywords which should be surrounded by '#', like: #title#
182                                                         char* keywords = strtok( value, "#" );
183                                                         char result[512] = ""; // XXX: how much is enough?
184                                                         int ct = 0;
185                                                         int fromStart = ( value[0] == '#' ) ? 1 : 0;
186                                                         
187                                                         while ( keywords != NULL )
188                                                         {
189                                                                 if ( ct % 2 == fromStart )
190                                                                 {
191                                                                         // backslash in front of # suppresses substitution
192                                                                         if ( keywords[ strlen( keywords ) -1 ] == '\\' )
193                                                                         {
194                                                                                 // keep characters except backslash
195                                                                                 strncat( result, keywords, strlen( keywords ) -1 );
196                                                                                 strcat( result, "#" );
197                                                                                 ct++;
198                                                                         }
199                                                                         else
200                                                                         {
201                                                                                 strcat( result, keywords );
202                                                                         }
203                                                                 }
204                                                                 else if ( !strcmp( keywords, "timecode" ) )
205                                                                 {
206                                                                         // special case: replace #timecode# with current frame timecode
207                                                                         int pos = mlt_properties_get_int( feed, "position" );
208                                                                         char *tc = frame_to_timecode( pos, mlt_profile_fps( NULL ) );
209                                                                         strcat( result, tc );
210                                                                         free( tc );
211                                                                 }
212                                                                 else
213                                                                 {
214                                                                         // replace keyword with metadata value
215                                                                         char *metavalue = metadata_value( MLT_FRAME_PROPERTIES( frame ), keywords );
216                                                                         strcat( result, metavalue ? metavalue : "-" );
217                                                                 }
218                                                                 keywords = strtok( NULL, "#" );
219                                                                 ct++;
220                                                         }
221                                                         mlt_properties_set( properties, key, (char*) result );
222                                                 }
223                                                 else mlt_properties_set( properties, key, value );
224                                         }
225                                 }
226                         }
227                 }
228
229                 // Set the original position on the frame
230                 if ( absolute == 0 )
231                         mlt_frame_set_position( frame, mlt_properties_get_int( feed, "position" ) - mlt_properties_get_int( feed, "in" ) );
232                 else
233                         mlt_frame_set_position( frame, mlt_properties_get_int( feed, "position" ) );
234
235                 // Process the filter
236                 mlt_filter_process( requested, frame );
237
238                 // Should be ok...
239                 error = 0;
240         }
241
242         return error;
243 }
244
245 void process_queue( mlt_deque data_queue, mlt_frame frame, mlt_filter filter )
246 {
247         if ( data_queue != NULL )
248         {
249                 // Create a new queue for those that we can't handle
250                 mlt_deque temp_queue = mlt_deque_init( );
251
252                 // Iterate through each entry on the queue
253                 while ( mlt_deque_peek_front( data_queue ) != NULL )
254                 {
255                         // Get the data feed
256                         mlt_properties feed = mlt_deque_pop_front( data_queue );
257
258                         if ( mlt_properties_get( MLT_FILTER_PROPERTIES( filter ), "debug" ) != NULL )
259                                 mlt_properties_debug( feed, mlt_properties_get( MLT_FILTER_PROPERTIES( filter ), "debug" ), stderr );
260
261                         // Process the data feed...
262                         if ( process_feed( feed, filter, frame ) == 0 )
263                                 mlt_properties_close( feed );
264                         else
265                                 mlt_deque_push_back( temp_queue, feed );
266                 }
267         
268                 // Now put the unprocessed feeds back on the stack
269                 while ( mlt_deque_peek_front( temp_queue ) )
270                 {
271                         // Get the data feed
272                         mlt_properties feed = mlt_deque_pop_front( temp_queue );
273         
274                         // Put it back on the data queue
275                         mlt_deque_push_back( data_queue, feed );
276                 }
277         
278                 // Close the temporary queue
279                 mlt_deque_close( temp_queue );
280         }
281 }
282
283 /** Get the image.
284 */
285
286 static int filter_get_image( mlt_frame frame, uint8_t **image, mlt_image_format *format, int *width, int *height, int writable )
287 {
288         // Pop the service
289         mlt_filter filter = mlt_frame_pop_service( frame );
290
291         // Get the frame properties
292         mlt_properties frame_properties = MLT_FRAME_PROPERTIES( frame );
293
294         // Track specific
295         process_queue( mlt_properties_get_data( frame_properties, "data_queue", NULL ), frame, filter );
296
297         // Global
298         process_queue( mlt_properties_get_data( frame_properties, "global_queue", NULL ), frame, filter );
299
300         // Need to get the image
301         return mlt_frame_get_image( frame, image, format, width, height, 1 );
302 }
303
304
305 /** Filter processing.
306 */
307
308 static mlt_frame filter_process( mlt_filter this, mlt_frame frame )
309 {
310         // Push the filter
311         mlt_frame_push_service( frame, this );
312
313         // Register the get image method
314         mlt_frame_push_get_image( frame, filter_get_image );
315
316         // Return the frame
317         return frame;
318 }
319
320 /** Constructor for the filter.
321 */
322
323 mlt_filter filter_data_show_init( mlt_profile profile, mlt_service_type type, const char *id, void *arg )
324 {
325         // Create the filter
326         mlt_filter this = mlt_filter_new( );
327
328         // Initialise it
329         if ( this != NULL )
330         {
331                 // Get the properties
332                 mlt_properties properties = MLT_FILTER_PROPERTIES( this );
333
334                 // Assign the argument (default to titles)
335                 mlt_properties_set( properties, "resource", arg == NULL ? NULL : arg );
336
337                 // Specify the processing method
338                 this->process = filter_process;
339         }
340
341         return this;
342 }
343