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