b3a4b105f1cce68ed6179d82ae7464e888b481ff
[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 {
181 char string[ 2 ] = { value, 0 };
182 transport_action( producer, string );
183 }
184
185 if ( !silent && mlt_properties_get_int( properties, "stats_off" ) == 0 )
186 fprintf( stderr, "Current Position: %10d\r", (int)mlt_producer_position( producer ) );
187
188 if ( silent )
189 nanosleep( &tm, NULL );
190 }
191
192 if ( !silent )
193 fprintf( stderr, "\n" );
194 }
195 }
196
197 int main( int argc, char **argv )
198 {
199 int i;
200 mlt_consumer consumer = NULL;
201 mlt_producer inigo = NULL;
202 FILE *store = NULL;
203 char *name = NULL;
204 struct sched_param scp;
205
206 // Use realtime scheduling if possible
207 memset( &scp, '\0', sizeof( scp ) );
208 scp.sched_priority = sched_get_priority_max( SCHED_FIFO ) - 1;
209 #ifndef __DARWIN__
210 sched_setscheduler( 0, SCHED_FIFO, &scp );
211 #endif
212
213 // Construct the factory
214 mlt_factory_init( NULL );
215
216 // Check for serialisation switch first
217 for ( i = 1; i < argc; i ++ )
218 {
219 if ( !strcmp( argv[ i ], "-serialise" ) )
220 {
221 name = argv[ ++ i ];
222 if ( strstr( name, ".inigo" ) )
223 store = fopen( name, "w" );
224 }
225 }
226
227 // Get inigo producer
228 if ( argc > 1 )
229 inigo = mlt_factory_producer( "inigo", &argv[ 1 ] );
230
231 if ( argc > 1 && inigo != NULL && mlt_producer_get_length( inigo ) > 0 )
232 {
233 // Get inigo's properties
234 mlt_properties inigo_props = MLT_PRODUCER_PROPERTIES( inigo );
235
236 // Get the last group
237 mlt_properties group = mlt_properties_get_data( inigo_props, "group", 0 );
238
239 // Parse the arguments
240 for ( i = 1; i < argc; i ++ )
241 {
242 if ( !strcmp( argv[ i ], "-consumer" ) )
243 {
244 consumer = create_consumer( argv[ ++ i ], inigo );
245 while ( argv[ i + 1 ] != NULL && strstr( argv[ i + 1 ], "=" ) )
246 mlt_properties_parse( group, argv[ ++ i ] );
247 }
248 else if ( !strcmp( argv[ i ], "-serialise" ) )
249 {
250 i ++;
251 }
252 else
253 {
254 if ( store != NULL )
255 fprintf( store, "%s\n", argv[ i ] );
256
257 i ++;
258
259 while ( argv[ i ] != NULL && argv[ i ][ 0 ] != '-' )
260 {
261 if ( store != NULL )
262 fprintf( store, "%s\n", argv[ i ] );
263 i += 1;
264 }
265
266 i --;
267 }
268 }
269
270 // If we have no consumer, default to sdl
271 if ( store == NULL && consumer == NULL )
272 consumer = create_consumer( NULL, inigo );
273
274 if ( consumer != NULL && store == NULL )
275 {
276 // Apply group settings
277 mlt_properties properties = MLT_CONSUMER_PROPERTIES( consumer );
278 mlt_properties_inherit( properties, group );
279
280 // Connect consumer to inigo
281 mlt_consumer_connect( consumer, MLT_PRODUCER_SERVICE( inigo ) );
282
283 // Start the consumer
284 mlt_consumer_start( consumer );
285
286 // Transport functionality
287 transport( inigo, consumer );
288
289 // Stop the consumer
290 mlt_consumer_stop( consumer );
291 }
292 else if ( store != NULL )
293 {
294 fprintf( stderr, "Project saved as %s.\n", name );
295 fclose( store );
296 }
297 }
298 else
299 {
300 fprintf( stderr, "Usage: inigo [ -group [ name=value ]* ]\n"
301 " [ -consumer id[:arg] [ name=value ]* ]\n"
302 " [ -filter filter[:arg] [ name=value ] * ]\n"
303 " [ -attach filter[:arg] [ name=value ] * ]\n"
304 " [ -mix length [ -mixer transition ]* ]\n"
305 " [ -transition id[:arg] [ name=value ] * ]\n"
306 " [ -blank frames ]\n"
307 " [ -track ]\n"
308 " [ -split relative-frame ]\n"
309 " [ -join clips ]\n"
310 " [ -repeat times ]\n"
311 " [ producer [ name=value ] * ]+\n" );
312 }
313
314 // Close the consumer
315 if ( consumer != NULL )
316 mlt_consumer_close( consumer );
317
318 // Close the producer
319 if ( inigo != NULL )
320 mlt_producer_close( inigo );
321
322 // Close the factory
323 mlt_factory_close( );
324
325 return 0;
326 }