2 * filter_dynamictext.c -- dynamic text overlay filter
3 * Copyright (C) 2011 Ushodaya Enterprises Limited
4 * Author: Brian Matherly <pez4brian@yahoo.com>
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.
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.
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
21 #include <framework/mlt.h>
25 #include <sys/types.h> // for stat()
26 #include <sys/stat.h> // for stat()
27 #include <unistd.h> // for stat()
28 #include <time.h> // for strftime() and gtime()
30 #define MAX_TEXT_LEN 512
32 /** Get the next token and indicate whether it is enclosed in "# #".
34 static int get_next_token(char* str, int* pos, char* token, int* is_keyword)
37 int str_len = strlen(str);
39 if( (*pos) >= str_len || str[*pos] == '\0' )
44 if( str[*pos] == '#' )
54 while( *pos < str_len && token_pos < MAX_TEXT_LEN - 1)
56 if( str[*pos] == '\\' && str[(*pos) + 1] == '#' )
58 // Escape Sequence - "#" preceeded by "\" - copy the # into the token.
59 token[token_pos] = '#';
64 else if( str[*pos] == '#' )
68 // Found the end of the keyword
75 token[token_pos] = str[*pos];
81 token[token_pos] = '\0';
86 static void get_timecode_str( mlt_filter filter, mlt_frame frame, char* text )
88 int frames = mlt_frame_get_position( frame );
89 double fps = mlt_profile_fps( mlt_service_profile( MLT_FILTER_SERVICE( filter ) ) );
93 strncat( text, "-", MAX_TEXT_LEN - strlen(text) - 1 );
97 int seconds = frames / fps;
98 frames = frames % lrint( fps );
99 int minutes = seconds / 60;
100 seconds = seconds % 60;
101 int hours = minutes / 60;
102 minutes = minutes % 60;
103 sprintf(tc, "%.2d:%.2d:%.2d:%.2d", hours, minutes, seconds, frames);
104 strncat( text, tc, MAX_TEXT_LEN - strlen(text) - 1 );
108 static void get_frame_str( mlt_filter filter, mlt_frame frame, char* text )
110 int pos = mlt_frame_get_position( frame );
112 snprintf( s, sizeof(s) - 1, "%d", pos );
113 strncat( text, s, MAX_TEXT_LEN - strlen(text) - 1 );
116 static void get_filedate_str( mlt_filter filter, mlt_frame frame, char* text )
118 mlt_producer producer = mlt_producer_cut_parent(mlt_frame_get_original_producer( frame ));
119 mlt_properties producer_properties = MLT_PRODUCER_PROPERTIES(producer);
120 char* filename = mlt_properties_get( producer_properties, "resource");
121 struct stat file_info;
123 if( !stat(filename, &file_info))
125 struct tm* time_info = gmtime( &(file_info.st_mtime) );
127 strftime( date, 11, "%Y/%m/%d", time_info );
128 strncat( text, date, MAX_TEXT_LEN - strlen(text) - 1);
133 /** Perform substitution for keywords that are enclosed in "# #".
136 static void substitute_keywords(mlt_filter filter, char* result, char* value, mlt_frame frame)
138 char keyword[MAX_TEXT_LEN] = "";
142 while ( get_next_token(value, &pos, keyword, &is_keyword) )
146 strncat( result, keyword, MAX_TEXT_LEN - strlen(result) - 1 );
148 else if ( !strcmp( keyword, "timecode" ) )
150 get_timecode_str(filter, frame, result);
152 else if ( !strcmp( keyword, "frame" ) )
154 get_frame_str(filter, frame, result);
156 else if ( !strcmp( keyword, "filedate" ) )
158 get_filedate_str(filter, frame, result);
160 else if ( !strcmp( keyword, "resource" ) )
162 // special case: replace #resource# with cut parent resource name
163 mlt_producer producer = mlt_producer_cut_parent(mlt_frame_get_original_producer( frame ));
164 mlt_properties producer_properties = MLT_PRODUCER_PROPERTIES(producer);
165 strncat( result, mlt_properties_get( producer_properties, "resource"), MAX_TEXT_LEN - strlen(result) - 1 );
169 // replace keyword with property value from this frame
170 mlt_properties frame_properties = MLT_FRAME_PROPERTIES( frame );
171 char *frame_value = mlt_properties_get( frame_properties, keyword );
174 strncat( result, frame_value, MAX_TEXT_LEN - strlen(result) - 1 );
180 static void apply_filter(mlt_filter filter, mlt_frame frame )
182 mlt_properties my_properties = MLT_FILTER_PROPERTIES( filter );
183 mlt_filter watermark = mlt_properties_get_data( my_properties, "_watermark", NULL );
184 mlt_properties watermark_properties = MLT_FILTER_PROPERTIES( watermark );
185 char* dynamic_text = mlt_properties_get( my_properties, "argument");
187 // Check for keywords in dynamic text
190 // Apply keyword substitution before passing the text to the filter.
191 char result[MAX_TEXT_LEN] = "";
192 substitute_keywords( filter, result, dynamic_text, frame );
193 mlt_properties_set( watermark_properties, "producer.markup", (char*) result );
196 // Pass the properties to the watermark filter composite transition
197 mlt_properties_set( watermark_properties, "composite.geometry", mlt_properties_get( my_properties, "geometry" ) );
199 // Pass the properties to the watermark filter pango producer
200 mlt_properties_set( watermark_properties, "producer.font", mlt_properties_get( my_properties, "font" ) );
201 mlt_properties_set( watermark_properties, "producer.weight", mlt_properties_get( my_properties, "weight" ) );
202 mlt_properties_set( watermark_properties, "producer.fgcolour", mlt_properties_get( my_properties, "fgcolour" ) );
203 mlt_properties_set( watermark_properties, "producer.bgcolour", mlt_properties_get( my_properties, "bgcolour" ) );
205 // Process the filter
206 mlt_filter_process( watermark, frame );
212 static int filter_get_image( mlt_frame frame, uint8_t **image, mlt_image_format *format, int *width, int *height, int writable )
215 mlt_filter filter = mlt_frame_pop_service( frame );
217 mlt_service_lock( MLT_FILTER_SERVICE( filter ) );
219 apply_filter(filter, frame);
221 mlt_service_unlock( MLT_FILTER_SERVICE( filter ) );
223 // Need to get the image
224 return mlt_frame_get_image( frame, image, format, width, height, 1 );
228 /** Filter processing.
231 static mlt_frame filter_process( mlt_filter filter, mlt_frame frame )
234 mlt_frame_push_service( frame, filter );
236 // Register the get image method
237 mlt_frame_push_get_image( frame, filter_get_image );
243 /** Constructor for the filter.
246 mlt_filter filter_dynamictext_init( mlt_profile profile, mlt_service_type type, const char *id, void *arg )
249 mlt_filter filter = mlt_filter_new( );
250 mlt_filter watermark = mlt_factory_filter( profile, "watermark", "pango:" );
253 if ( filter && watermark )
255 // Get the properties
256 mlt_properties properties = MLT_FILTER_PROPERTIES( filter );
258 // Store the watermark filter for future use
259 mlt_properties_set_data( properties, "_watermark", watermark, 0, ( mlt_destructor )mlt_filter_close, NULL );
261 // Assign default values
262 mlt_properties_set( properties, "argument", arg ? arg: "#timecode#" );
263 mlt_properties_set( properties, "geometry", "0%/0%:100%x100%:100" );
264 mlt_properties_set( properties, "font", "Sans 48" );
265 mlt_properties_set( properties, "weight", "400" );
266 mlt_properties_set( properties, "fgcolour", "0x000000ff" );
267 mlt_properties_set( properties, "bgcolour", "0x00000020" );
269 // Specify the processing method
270 filter->process = filter_process;
272 else // filter or watermark failed for some reason
276 mlt_filter_close(filter);
281 mlt_filter_close(watermark);