better propogating of producer and transition properties to the frame in pango and...
[melted] / src / inigo / inigo.c
1 #include <stdio.h>
2 #include <stdlib.h>
3 #include <string.h>
4
5 #include <framework/mlt.h>
6
7 #include "io.h"
8
9 mlt_producer create_producer( char *file )
10 {
11 mlt_producer result = NULL;
12
13 // 1st Line preferences
14 if ( strstr( file, ".mpg" ) )
15 result = mlt_factory_producer( "mcmpeg", file );
16 else if ( strstr( file, ".mpeg" ) )
17 result = mlt_factory_producer( "mcmpeg", file );
18 else if ( strstr( file, ".dv" ) )
19 result = mlt_factory_producer( "mcdv", file );
20 else if ( strstr( file, ".dif" ) )
21 result = mlt_factory_producer( "mcdv", file );
22 else if ( strstr( file, ".jpg" ) )
23 result = mlt_factory_producer( "pixbuf", file );
24 else if ( strstr( file, ".JPG" ) )
25 result = mlt_factory_producer( "pixbuf", file );
26 else if ( strstr( file, ".jpeg" ) )
27 result = mlt_factory_producer( "pixbuf", file );
28 else if ( strstr( file, ".png" ) )
29 result = mlt_factory_producer( "pixbuf", file );
30 else if ( strstr( file, ".txt" ) )
31 result = mlt_factory_producer( "pango", file );
32
33 // 2nd Line fallbacks
34 if ( result == NULL && strstr( file, ".dv" ) )
35 result = mlt_factory_producer( "libdv", file );
36 else if ( result == NULL && strstr( file, ".dif" ) )
37 result = mlt_factory_producer( "libdv", file );
38
39 // 3rd line fallbacks
40 if ( result == NULL )
41 result = mlt_factory_producer( "ffmpeg", file );
42
43 return result;
44 }
45
46 void transport_action( mlt_producer producer, char *value )
47 {
48 mlt_properties properties = mlt_producer_properties( producer );
49 mlt_multitrack multitrack = mlt_properties_get_data( properties, "multitrack", NULL );
50
51 if ( strlen( value ) == 1 )
52 {
53 switch( value[ 0 ] )
54 {
55 case 'q':
56 mlt_properties_set_int( properties, "done", 1 );
57 break;
58 case '0':
59 mlt_producer_set_speed( producer, 1 );
60 mlt_producer_seek( producer, 0 );
61 break;
62 case '1':
63 mlt_producer_set_speed( producer, -10 );
64 break;
65 case '2':
66 mlt_producer_set_speed( producer, -5 );
67 break;
68 case '3':
69 mlt_producer_set_speed( producer, -2 );
70 break;
71 case '4':
72 mlt_producer_set_speed( producer, -1 );
73 break;
74 case '5':
75 mlt_producer_set_speed( producer, 0 );
76 break;
77 case '6':
78 mlt_producer_set_speed( producer, 1 );
79 break;
80 case '7':
81 mlt_producer_set_speed( producer, 2 );
82 break;
83 case '8':
84 mlt_producer_set_speed( producer, 5 );
85 break;
86 case '9':
87 mlt_producer_set_speed( producer, 10 );
88 break;
89 case 'j':
90 if ( multitrack != NULL )
91 {
92 mlt_timecode time = mlt_multitrack_clip( multitrack, mlt_whence_relative_current, -1 );
93 mlt_producer_seek( producer, time );
94 }
95 break;
96 case 'k':
97 if ( multitrack != NULL )
98 {
99 mlt_timecode time = mlt_multitrack_clip( multitrack, mlt_whence_relative_current, 0 );
100 mlt_producer_seek( producer, time );
101 }
102 break;
103 case 'l':
104 if ( multitrack != NULL )
105 {
106 mlt_timecode time = mlt_multitrack_clip( multitrack, mlt_whence_relative_current, 1 );
107 mlt_producer_seek( producer, time );
108 }
109 break;
110 }
111 }
112 }
113
114 mlt_consumer create_consumer( char *id, mlt_producer producer )
115 {
116 char *arg = strchr( id, ':' );
117 if ( arg != NULL )
118 *arg ++ = '\0';
119 mlt_consumer consumer = mlt_factory_consumer( id, arg );
120 if ( consumer != NULL )
121 {
122 mlt_properties properties = mlt_consumer_properties( consumer );
123 mlt_properties_set_data( properties, "transport_callback", transport_action, 0, NULL, NULL );
124 mlt_properties_set_data( properties, "transport_producer", producer, 0, NULL, NULL );
125 }
126 return consumer;
127 }
128
129 void track_service( mlt_field field, void *service, mlt_destructor destructor )
130 {
131 mlt_properties properties = mlt_field_properties( field );
132 int registered = mlt_properties_get_int( properties, "registered" );
133 char *key = mlt_properties_get( properties, "registered" );
134 mlt_properties_set_data( properties, key, service, 0, destructor, NULL );
135 mlt_properties_set_int( properties, "registered", ++ registered );
136 }
137
138 mlt_filter create_filter( mlt_field field, char *id, int track )
139 {
140 char *arg = strchr( id, ':' );
141 if ( arg != NULL )
142 *arg ++ = '\0';
143 mlt_filter filter = mlt_factory_filter( id, arg );
144 if ( filter != NULL )
145 {
146 mlt_field_plant_filter( field, filter, track );
147 track_service( field, filter, ( mlt_destructor )mlt_filter_close );
148 }
149 return filter;
150 }
151
152 mlt_transition create_transition( mlt_field field, char *id, int track )
153 {
154 char *arg = strchr( id, ':' );
155 if ( arg != NULL )
156 *arg ++ = '\0';
157 mlt_transition transition = mlt_factory_transition( id, arg );
158 if ( transition != NULL )
159 {
160 mlt_field_plant_transition( field, transition, track, track + 1 );
161 track_service( field, transition, ( mlt_destructor )mlt_transition_close );
162 }
163 return transition;
164 }
165
166 void set_properties( mlt_properties properties, char *namevalue )
167 {
168 mlt_properties_parse( properties, namevalue );
169 }
170
171 void transport( mlt_producer producer )
172 {
173 mlt_properties properties = mlt_producer_properties( producer );
174
175 term_init( );
176
177 fprintf( stderr, "+-----+ +-----+ +-----+ +-----+ +-----+ +-----+ +-----+ +-----+ +-----+\n" );
178 fprintf( stderr, "|1=-10| |2= -5| |3= -2| |4= -1| |5= 0| |6= 1| |7= 2| |8= 5| |9= 10|\n" );
179 fprintf( stderr, "+-----+ +-----+ +-----+ +-----+ +-----+ +-----+ +-----+ +-----+ +-----+\n" );
180
181 fprintf( stderr, "+---------------------------------------------------------------------+\n" );
182 fprintf( stderr, "| j = previous, k = restart current, l = next |\n" );
183 fprintf( stderr, "| 0 = restart, q = quit |\n" );
184 fprintf( stderr, "+---------------------------------------------------------------------+\n" );
185
186 while( mlt_properties_get_int( properties, "done" ) == 0 )
187 {
188 int value = term_read( );
189 if ( value != -1 )
190 transport_action( producer, ( char * )&value );
191 }
192 }
193
194 int main( int argc, char **argv )
195 {
196 int i;
197 int track = 0;
198 mlt_consumer consumer = NULL;
199 mlt_producer producer = NULL;
200 mlt_playlist playlist = mlt_playlist_init( );
201 mlt_properties group = mlt_properties_new( );
202 mlt_properties properties = group;
203 mlt_field field = mlt_field_init( );
204 mlt_properties field_properties = mlt_field_properties( field );
205 mlt_multitrack multitrack = mlt_field_multitrack( field );
206
207 // Construct the factory
208 mlt_factory_init( getenv( "MLT_REPOSITORY" ) );
209
210 // We need to track the number of registered filters
211 mlt_properties_set_int( field_properties, "registered", 0 );
212
213 // Parse the arguments
214 for ( i = 1; i < argc; i ++ )
215 {
216 if ( !strcmp( argv[ i ], "-consumer" ) )
217 {
218 consumer = create_consumer( argv[ ++ i ], mlt_multitrack_producer( multitrack ) );
219 if ( consumer != NULL )
220 {
221 properties = mlt_consumer_properties( consumer );
222 mlt_properties_inherit( properties, group );
223 }
224 }
225 else if ( !strcmp( argv[ i ], "-group" ) )
226 {
227 if ( mlt_properties_count( group ) != 0 )
228 {
229 mlt_properties_close( group );
230 group = mlt_properties_new( );
231 }
232 if ( group != NULL )
233 properties = group;
234 }
235 else if ( !strcmp( argv[ i ], "-filter" ) )
236 {
237 mlt_filter filter = create_filter( field, argv[ ++ i ], track );
238 if ( filter != NULL )
239 {
240 properties = mlt_filter_properties( filter );
241 mlt_properties_inherit( properties, group );
242 }
243 }
244 else if ( !strcmp( argv[ i ], "-transition" ) )
245 {
246 mlt_transition transition = create_transition( field, argv[ ++ i ], track );
247 if ( transition != NULL )
248 {
249 properties = mlt_transition_properties( transition );
250 mlt_properties_inherit( properties, group );
251 }
252 }
253 else if ( !strcmp( argv[ i ], "-blank" ) )
254 {
255 if ( producer != NULL )
256 mlt_playlist_append( playlist, producer );
257 producer = NULL;
258 mlt_playlist_blank( playlist, atof( argv[ ++ i ] ) );
259 }
260 else if ( !strcmp( argv[ i ], "-track" ) )
261 {
262 if ( producer != NULL )
263 mlt_playlist_append( playlist, producer );
264 producer = NULL;
265 mlt_multitrack_connect( multitrack, mlt_playlist_producer( playlist ), track ++ );
266 playlist = mlt_playlist_init( );
267 }
268 else if ( !strstr( argv[ i ], "=" ) )
269 {
270 if ( producer != NULL )
271 mlt_playlist_append( playlist, producer );
272 producer = create_producer( argv[ i ] );
273 if ( producer != NULL )
274 {
275 properties = mlt_producer_properties( producer );
276 mlt_properties_inherit( properties, group );
277 }
278 }
279 else
280 {
281 set_properties( properties, argv[ i ] );
282 }
283 }
284
285 // Connect producer to playlist
286 if ( producer != NULL )
287 mlt_playlist_append( playlist, producer );
288
289
290 // We must have a producer at this point
291 if ( mlt_playlist_count( playlist ) > 0 )
292 {
293 // If we have no consumer, default to sdl
294 if ( consumer == NULL )
295 {
296 consumer = create_consumer( "sdl", mlt_multitrack_producer( multitrack ) );
297 if ( consumer != NULL )
298 {
299 properties = mlt_consumer_properties( consumer );
300 mlt_properties_inherit( properties, group );
301 }
302 }
303
304 // Connect multitrack to producer
305 mlt_multitrack_connect( multitrack, mlt_playlist_producer( playlist ), track );
306
307 // Connect consumer to tractor
308 mlt_consumer_connect( consumer, mlt_field_service( field ) );
309
310 // Transport functionality
311 transport( mlt_multitrack_producer( multitrack ) );
312
313 // Close the services
314 mlt_consumer_close( consumer );
315 mlt_producer_close( producer );
316 }
317 else
318 {
319 fprintf( stderr, "Usage: inigo [ -group [ name=value ]* ]\n"
320 " [ -consumer id[:arg] [ name=value ]* ]\n"
321 " [ -filter id[:arg] [ name=value ] * ]\n"
322 " [ -transition id[:arg] [ name=value ] * ]\n"
323 " [ -blank time ]\n"
324 " [ producer [ name=value ] * ]+\n" );
325 }
326
327 // Close the field
328 mlt_field_close( field );
329
330 // Close the group
331 mlt_properties_close( group );
332
333 // Close the factory
334 mlt_factory_close( );
335
336 return 0;
337 }