+ Correction to the disabled case (should be disable-motion_est and plugins should...
[melted] / src / inigo / inigo.c
1 #include <stdio.h>
2 #include <stdlib.h>
3 #include <string.h>
4 #include <sched.h>
5
6 #include <framework/mlt.h>
7
8 #include "io.h"
9
10 static void transport_action( mlt_producer producer, char *value )
11 {
12 mlt_properties properties = MLT_PRODUCER_PROPERTIES( producer );
13 mlt_multitrack multitrack = mlt_properties_get_data( properties, "multitrack", NULL );
14 mlt_consumer consumer = mlt_properties_get_data( properties, "transport_consumer", NULL );
15
16 mlt_properties_set_int( properties, "stats_off", 0 );
17
18 if ( strlen( value ) == 1 )
19 {
20 switch( value[ 0 ] )
21 {
22 case 'q':
23 mlt_properties_set_int( properties, "done", 1 );
24 break;
25 case '0':
26 mlt_producer_set_speed( producer, 1 );
27 mlt_producer_seek( producer, 0 );
28 break;
29 case '1':
30 mlt_producer_set_speed( producer, -10 );
31 break;
32 case '2':
33 mlt_producer_set_speed( producer, -5 );
34 break;
35 case '3':
36 mlt_producer_set_speed( producer, -2 );
37 break;
38 case '4':
39 mlt_producer_set_speed( producer, -1 );
40 break;
41 case '5':
42 mlt_producer_set_speed( producer, 0 );
43 break;
44 case '6':
45 case ' ':
46 mlt_producer_set_speed( producer, 1 );
47 break;
48 case '7':
49 mlt_producer_set_speed( producer, 2 );
50 break;
51 case '8':
52 mlt_producer_set_speed( producer, 5 );
53 break;
54 case '9':
55 mlt_producer_set_speed( producer, 10 );
56 break;
57 case 'd':
58 if ( multitrack != NULL )
59 {
60 int i = 0;
61 mlt_position last = -1;
62 fprintf( stderr, "\n" );
63 for ( i = 0; 1; i ++ )
64 {
65 mlt_position time = mlt_multitrack_clip( multitrack, mlt_whence_relative_start, i );
66 if ( time == last )
67 break;
68 last = time;
69 fprintf( stderr, "%d: %d\n", i, (int)time );
70 }
71 }
72 break;
73
74 case 'g':
75 if ( multitrack != NULL )
76 {
77 mlt_position time = mlt_multitrack_clip( multitrack, mlt_whence_relative_current, 0 );
78 mlt_producer_seek( producer, time );
79 }
80 break;
81 case 'H':
82 if ( producer != NULL )
83 {
84 mlt_position position = mlt_producer_position( producer );
85 mlt_producer_seek( producer, position - ( mlt_producer_get_fps( producer ) * 60 ) );
86 }
87 break;
88 case 'h':
89 if ( producer != NULL )
90 {
91 mlt_position position = mlt_producer_position( producer );
92 mlt_producer_set_speed( producer, 0 );
93 mlt_producer_seek( producer, position - 1 );
94 }
95 break;
96 case 'j':
97 if ( multitrack != NULL )
98 {
99 mlt_position time = mlt_multitrack_clip( multitrack, mlt_whence_relative_current, 1 );
100 mlt_producer_seek( producer, time );
101 }
102 break;
103 case 'k':
104 if ( multitrack != NULL )
105 {
106 mlt_position time = mlt_multitrack_clip( multitrack, mlt_whence_relative_current, -1 );
107 mlt_producer_seek( producer, time );
108 }
109 break;
110 case 'l':
111 if ( producer != NULL )
112 {
113 mlt_position position = mlt_producer_position( producer );
114 if ( mlt_producer_get_speed( producer ) != 0 )
115 mlt_producer_set_speed( producer, 0 );
116 else
117 mlt_producer_seek( producer, position + 1 );
118 }
119 break;
120 case 'L':
121 if ( producer != NULL )
122 {
123 mlt_position position = mlt_producer_position( producer );
124 mlt_producer_seek( producer, position + ( mlt_producer_get_fps( producer ) * 60 ) );
125 }
126 break;
127 }
128
129 mlt_properties_set_int( MLT_CONSUMER_PROPERTIES( consumer ), "refresh", 1 );
130 }
131
132 mlt_properties_set_int( properties, "stats_off", 0 );
133 }
134
135 static mlt_consumer create_consumer( char *id, mlt_producer producer )
136 {
137 char *arg = id != NULL ? strchr( id, ':' ) : NULL;
138 if ( arg != NULL )
139 *arg ++ = '\0';
140 mlt_consumer consumer = mlt_factory_consumer( id, arg );
141 if ( consumer != NULL )
142 {
143 mlt_properties properties = MLT_CONSUMER_PROPERTIES( consumer );
144 mlt_properties_set_data( properties, "transport_callback", transport_action, 0, NULL, NULL );
145 mlt_properties_set_data( properties, "transport_producer", producer, 0, NULL, NULL );
146 mlt_properties_set_data( MLT_PRODUCER_PROPERTIES( producer ), "transport_consumer", consumer, 0, NULL, NULL );
147 }
148 return consumer;
149 }
150
151 static void transport( mlt_producer producer, mlt_consumer consumer )
152 {
153 mlt_properties properties = MLT_PRODUCER_PROPERTIES( producer );
154 int silent = mlt_properties_get_int( MLT_CONSUMER_PROPERTIES( consumer ), "silent" );
155 struct timespec tm = { 0, 40000 };
156
157 if ( mlt_properties_get_int( properties, "done" ) == 0 && !mlt_consumer_is_stopped( consumer ) )
158 {
159 if ( !silent )
160 {
161 term_init( );
162
163 fprintf( stderr, "+-----+ +-----+ +-----+ +-----+ +-----+ +-----+ +-----+ +-----+ +-----+\n" );
164 fprintf( stderr, "|1=-10| |2= -5| |3= -2| |4= -1| |5= 0| |6= 1| |7= 2| |8= 5| |9= 10|\n" );
165 fprintf( stderr, "+-----+ +-----+ +-----+ +-----+ +-----+ +-----+ +-----+ +-----+ +-----+\n" );
166
167 fprintf( stderr, "+---------------------------------------------------------------------+\n" );
168 fprintf( stderr, "| H = back 1 minute, L = forward 1 minute |\n" );
169 fprintf( stderr, "| h = previous frame, l = next frame |\n" );
170 fprintf( stderr, "| g = start of clip, j = next clip, k = previous clip |\n" );
171 fprintf( stderr, "| 0 = restart, q = quit, space = play |\n" );
172 fprintf( stderr, "+---------------------------------------------------------------------+\n" );
173 }
174
175 while( mlt_properties_get_int( properties, "done" ) == 0 && !mlt_consumer_is_stopped( consumer ) )
176 {
177 int value = silent ? -1 : term_read( );
178
179 if ( value != -1 )
180 transport_action( producer, ( char * )&value );
181
182 if ( !silent && mlt_properties_get_int( properties, "stats_off" ) == 0 )
183 fprintf( stderr, "Current Position: %10d\r", (int)mlt_producer_position( producer ) );
184
185 if ( silent )
186 nanosleep( &tm, NULL );
187 }
188
189 if ( !silent )
190 fprintf( stderr, "\n" );
191 }
192 }
193
194 int main( int argc, char **argv )
195 {
196 int i;
197 mlt_consumer consumer = NULL;
198 mlt_producer inigo = NULL;
199 FILE *store = NULL;
200 char *name = NULL;
201 struct sched_param scp;
202
203 // Use realtime scheduling if possible
204 memset( &scp, '\0', sizeof( scp ) );
205 scp.sched_priority = sched_get_priority_max( SCHED_FIFO ) - 1;
206 #ifndef __DARWIN__
207 sched_setscheduler( 0, SCHED_FIFO, &scp );
208 #endif
209
210 // Construct the factory
211 mlt_factory_init( NULL );
212
213 // Check for serialisation switch first
214 for ( i = 1; i < argc; i ++ )
215 {
216 if ( !strcmp( argv[ i ], "-serialise" ) )
217 {
218 name = argv[ ++ i ];
219 if ( strstr( name, ".inigo" ) )
220 store = fopen( name, "w" );
221 }
222 }
223
224 // Get inigo producer
225 if ( argc > 1 )
226 inigo = mlt_factory_producer( "inigo", &argv[ 1 ] );
227
228 if ( argc > 1 && inigo != NULL && mlt_producer_get_length( inigo ) > 0 )
229 {
230 // Get inigo's properties
231 mlt_properties inigo_props = MLT_PRODUCER_PROPERTIES( inigo );
232
233 // Get the last group
234 mlt_properties group = mlt_properties_get_data( inigo_props, "group", 0 );
235
236 // Parse the arguments
237 for ( i = 1; i < argc; i ++ )
238 {
239 if ( !strcmp( argv[ i ], "-consumer" ) )
240 {
241 consumer = create_consumer( argv[ ++ i ], inigo );
242 while ( argv[ i + 1 ] != NULL && strstr( argv[ i + 1 ], "=" ) )
243 mlt_properties_parse( group, argv[ ++ i ] );
244 }
245 else if ( !strcmp( argv[ i ], "-serialise" ) )
246 {
247 i ++;
248 }
249 else
250 {
251 if ( store != NULL )
252 fprintf( store, "%s\n", argv[ i ] );
253
254 i ++;
255
256 while ( argv[ i ] != NULL && argv[ i ][ 0 ] != '-' )
257 {
258 if ( store != NULL )
259 fprintf( store, "%s\n", argv[ i ] );
260 i += 1;
261 }
262
263 i --;
264 }
265 }
266
267 // If we have no consumer, default to sdl
268 if ( store == NULL && consumer == NULL )
269 consumer = create_consumer( NULL, inigo );
270
271 if ( consumer != NULL && store == NULL )
272 {
273 // Apply group settings
274 mlt_properties properties = MLT_CONSUMER_PROPERTIES( consumer );
275 mlt_properties_inherit( properties, group );
276
277 // Connect consumer to inigo
278 mlt_consumer_connect( consumer, MLT_PRODUCER_SERVICE( inigo ) );
279
280 // Start the consumer
281 mlt_consumer_start( consumer );
282
283 // Transport functionality
284 transport( inigo, consumer );
285
286 // Stop the consumer
287 mlt_consumer_stop( consumer );
288 }
289 else if ( store != NULL )
290 {
291 fprintf( stderr, "Project saved as %s.\n", name );
292 fclose( store );
293 }
294 }
295 else
296 {
297 fprintf( stderr, "Usage: inigo [ -group [ name=value ]* ]\n"
298 " [ -consumer id[:arg] [ name=value ]* ]\n"
299 " [ -filter filter[:arg] [ name=value ] * ]\n"
300 " [ -attach filter[:arg] [ name=value ] * ]\n"
301 " [ -mix length [ -mixer transition ]* ]\n"
302 " [ -transition id[:arg] [ name=value ] * ]\n"
303 " [ -blank frames ]\n"
304 " [ -track ]\n"
305 " [ -split relative-frame ]\n"
306 " [ -join clips ]\n"
307 " [ -repeat times ]\n"
308 " [ producer [ name=value ] * ]+\n" );
309 }
310
311 // Close the consumer
312 if ( consumer != NULL )
313 mlt_consumer_close( consumer );
314
315 // Close the producer
316 if ( inigo != NULL )
317 mlt_producer_close( inigo );
318
319 // Close the factory
320 mlt_factory_close( );
321
322 return 0;
323 }