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