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