]> git.sesse.net Git - mlt/blob - src/modules/inigo/producer_inigo.c
Cloning optimisations and introduction of the service parser
[mlt] / src / modules / inigo / producer_inigo.c
1 /*
2  * producer_inigo.c -- simple inigo test case
3  * Copyright (C) 2003-2004 Ushodaya Enterprises Limited
4  * Author: Charles Yates <charles.yates@pandora.be>
5  *
6  * This program is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation; either version 2 of the License, or
9  * (at your option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program; if not, write to the Free Software Foundation,
18  * Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
19  */
20
21 #include "producer_inigo.h"
22
23 #include <stdio.h>
24 #include <stdlib.h>
25 #include <string.h>
26
27 #include <framework/mlt.h>
28
29 mlt_producer producer_inigo_file_init( char *file )
30 {
31         FILE *input = fopen( file, "r" );
32         char **args = calloc( sizeof( char * ), 1000 );
33         int count = 0;
34         char temp[ 2048 ];
35
36         if ( input != NULL )
37         {
38                 while( fgets( temp, 2048, input ) )
39                 {
40                         temp[ strlen( temp ) - 1 ] = '\0';
41                         if ( strcmp( temp, "" ) )
42                                 args[ count ++ ] = strdup( temp );
43                 }
44         }
45
46         mlt_producer result = producer_inigo_init( args );
47
48         if ( result != NULL )
49         {
50                 mlt_properties properties = mlt_producer_properties( result );
51                 mlt_properties_set( properties, "resource", file );
52         }
53
54         while( count -- )
55                 free( args[ count ] );
56         free( args );
57
58         return result;
59 }
60
61 static void track_service( mlt_field field, void *service, mlt_destructor destructor )
62 {
63         mlt_properties properties = mlt_field_properties( field );
64         int registered = mlt_properties_get_int( properties, "registered" );
65         char *key = mlt_properties_get( properties, "registered" );
66         mlt_properties_set_data( properties, key, service, 0, destructor, NULL );
67         mlt_properties_set_int( properties, "registered", ++ registered );
68 }
69
70 static mlt_producer create_producer( mlt_field field, char *file )
71 {
72         mlt_producer result = mlt_factory_producer( "fezzik", file );
73
74         if ( result != NULL )
75                 track_service( field, result, ( mlt_destructor )mlt_producer_close );
76
77         return result;
78 }
79
80 static mlt_filter create_attach( mlt_field field, char *id, int track )
81 {
82         char *arg = strchr( id, ':' );
83         if ( arg != NULL )
84                 *arg ++ = '\0';
85         mlt_filter filter = mlt_factory_filter( id, arg );
86         if ( filter != NULL )
87                 track_service( field, filter, ( mlt_destructor )mlt_filter_close );
88         return filter;
89 }
90
91 static mlt_filter create_filter( mlt_field field, char *id, int track )
92 {
93         char *arg = strchr( id, ':' );
94         if ( arg != NULL )
95                 *arg ++ = '\0';
96         mlt_filter filter = mlt_factory_filter( id, arg );
97         if ( filter != NULL )
98         {
99                 mlt_field_plant_filter( field, filter, track );
100                 track_service( field, filter, ( mlt_destructor )mlt_filter_close );
101         }
102         return filter;
103 }
104
105 static mlt_transition create_transition( mlt_field field, char *id, int track )
106 {
107         char *arg = strchr( id, ':' );
108         if ( arg != NULL )
109                 *arg ++ = '\0';
110         mlt_transition transition = mlt_factory_transition( id, arg );
111         if ( transition != NULL )
112         {
113                 mlt_field_plant_transition( field, transition, track, track + 1 );
114                 track_service( field, transition, ( mlt_destructor )mlt_transition_close );
115         }
116         return transition;
117 }
118
119 mlt_producer producer_inigo_init( char **argv )
120 {
121         int i;
122         int track = 0;
123         mlt_producer producer = NULL;
124         mlt_tractor mix = NULL;
125         mlt_playlist playlist = mlt_playlist_init( );
126         mlt_properties group = mlt_properties_new( );
127         mlt_properties properties = group;
128         mlt_tractor tractor = mlt_tractor_new( );
129         mlt_field field = mlt_tractor_field( tractor );
130         mlt_properties field_properties = mlt_field_properties( field );
131         mlt_multitrack multitrack = mlt_tractor_multitrack( tractor );
132         char *title = NULL;
133
134         // We need to track the number of registered filters
135         mlt_properties_set_int( field_properties, "registered", 0 );
136
137         // Parse the arguments
138         for ( i = 0; argv[ i ] != NULL; i ++ )
139         {
140                 if ( !strcmp( argv[ i ], "-group" ) )
141                 {
142                         if ( mlt_properties_count( group ) != 0 )
143                         {
144                                 mlt_properties_close( group );
145                                 group = mlt_properties_new( );
146                         }
147                         if ( group != NULL )
148                                 properties = group;
149                 }
150                 else if ( !strcmp( argv[ i ], "-attach" ) || 
151                                   !strcmp( argv[ i ], "-attach-cut" ) ||
152                                   !strcmp( argv[ i ], "-attach-clip" ) )
153                 {
154                         int type = !strcmp( argv[ i ], "-attach" ) ? 0 : 
155                                            !strcmp( argv[ i ], "-attach-cut" ) ? 1 : 
156                                            2;
157                         mlt_filter filter = create_attach( field, argv[ ++ i ], track );
158                         if ( producer != NULL && !mlt_producer_is_cut( producer ) )
159                         {
160                                 mlt_playlist_clip_info info;
161                                 mlt_playlist_append( playlist, producer );
162                                 mlt_playlist_get_clip_info( playlist, &info, mlt_playlist_count( playlist ) - 1 );
163                                 producer = info.cut;
164                         }
165
166                         if ( type == 1 || type == 2 )
167                         {
168                                 mlt_playlist_clip_info info;
169                                 mlt_playlist_get_clip_info( playlist, &info, mlt_playlist_count( playlist ) - 1 );
170                                 producer = info.cut;
171                         }
172
173                         if ( filter != NULL && mlt_playlist_count( playlist ) > 0 )
174                         {
175                                 if ( type == 0 )
176                                         mlt_service_attach( ( mlt_service )properties, filter );
177                                 else if ( type == 1 )
178                                         mlt_service_attach( ( mlt_service )producer, filter );
179                                 else if ( type == 2 )
180                                         mlt_service_attach( ( mlt_service )mlt_producer_cut_parent( producer ), filter );
181
182                                 properties = mlt_filter_properties( filter );
183                                 mlt_properties_inherit( properties, group );
184                         }
185                 }
186                 else if ( !strcmp( argv[ i ], "-repeat" ) )
187                 {
188                         int repeat = atoi( argv[ ++ i ] );
189                         if ( producer != NULL && !mlt_producer_is_cut( producer ) )
190                                 mlt_playlist_append( playlist, producer );
191                         producer = NULL;
192                         if ( mlt_playlist_count( playlist ) > 0 )
193                         {
194                                 mlt_playlist_clip_info info;
195                                 mlt_playlist_repeat_clip( playlist, mlt_playlist_count( playlist ) - 1, repeat );
196                                 mlt_playlist_get_clip_info( playlist, &info, mlt_playlist_count( playlist ) - 1 );
197                                 producer = info.cut;
198                                 properties = mlt_producer_properties( producer );
199                         }
200                 }
201                 else if ( !strcmp( argv[ i ], "-split" ) )
202                 {
203                         int split = atoi( argv[ ++ i ] );
204                         if ( producer != NULL && !mlt_producer_is_cut( producer ) )
205                                 mlt_playlist_append( playlist, producer );
206                         producer = NULL;
207                         if ( mlt_playlist_count( playlist ) > 0 )
208                         {
209                                 mlt_playlist_clip_info info;
210                                 mlt_playlist_get_clip_info( playlist, &info, mlt_playlist_count( playlist ) - 1 );
211                                 split = split < 0 ? info.frame_out + split : split;
212                                 mlt_playlist_split( playlist, mlt_playlist_count( playlist ) - 1, split );
213                                 mlt_playlist_get_clip_info( playlist, &info, mlt_playlist_count( playlist ) - 1 );
214                                 producer = info.cut;
215                                 properties = mlt_producer_properties( producer );
216                         }
217                 }
218                 else if ( !strcmp( argv[ i ], "-swap" ) )
219                 {
220                         if ( producer != NULL && !mlt_producer_is_cut( producer ) )
221                                 mlt_playlist_append( playlist, producer );
222                         producer = NULL;
223                         if ( mlt_playlist_count( playlist ) >= 2 )
224                         {
225                                 mlt_playlist_clip_info info;
226                                 mlt_playlist_move( playlist, mlt_playlist_count( playlist ) - 2, mlt_playlist_count( playlist ) - 1 );
227                                 mlt_playlist_get_clip_info( playlist, &info, mlt_playlist_count( playlist ) - 1 );
228                                 producer = info.cut;
229                                 properties = mlt_producer_properties( producer );
230                         }
231                 }
232                 else if ( !strcmp( argv[ i ], "-join" ) )
233                 {
234                         int clips = atoi( argv[ ++ i ] );
235                         if ( producer != NULL && !mlt_producer_is_cut( producer ) )
236                                 mlt_playlist_append( playlist, producer );
237                         producer = NULL;
238                         if ( mlt_playlist_count( playlist ) > 0 )
239                         {
240                                 mlt_playlist_clip_info info;
241                                 mlt_playlist_join( playlist, mlt_playlist_count( playlist ) - clips - 1, clips, 0 );
242                                 mlt_playlist_get_clip_info( playlist, &info, mlt_playlist_count( playlist ) - 1 );
243                                 producer = info.cut;
244                                 properties = mlt_producer_properties( producer );
245                         }
246                 }
247                 else if ( !strcmp( argv[ i ], "-remove" ) )
248                 {
249                         if ( producer != NULL && !mlt_producer_is_cut( producer ) )
250                                 mlt_playlist_append( playlist, producer );
251                         producer = NULL;
252                         if ( mlt_playlist_count( playlist ) > 0 )
253                         {
254                                 mlt_playlist_clip_info info;
255                                 mlt_playlist_remove( playlist, mlt_playlist_count( playlist ) - 1 );
256                                 mlt_playlist_get_clip_info( playlist, &info, mlt_playlist_count( playlist ) - 1 );
257                                 producer = info.cut;
258                                 properties = mlt_producer_properties( producer );
259                         }
260                 }
261                 else if ( !strcmp( argv[ i ], "-mix" ) )
262                 {
263                         int length = atoi( argv[ ++ i ] );
264                         if ( producer != NULL && !mlt_producer_is_cut( producer ) )
265                                 mlt_playlist_append( playlist, producer );
266                         producer = NULL;
267                         if ( mlt_playlist_count( playlist ) >= 2 )
268                         {
269                                 if ( mlt_playlist_mix( playlist, mlt_playlist_count( playlist ) - 2, length, NULL ) == 0 )
270                                 {
271                                         mlt_playlist_clip_info info;
272                                         mlt_playlist_get_clip_info( playlist, &info, mlt_playlist_count( playlist ) - 1 );
273                                         if ( mlt_properties_get_data( ( mlt_properties )info.producer, "mlt_mix", NULL ) == NULL )
274                                                 mlt_playlist_get_clip_info( playlist, &info, mlt_playlist_count( playlist ) - 2 );
275                                         mix = ( mlt_tractor )mlt_properties_get_data( ( mlt_properties )info.producer, "mlt_mix", NULL );
276                                         properties = NULL;
277                                 }
278                                 else
279                                 {
280                                         fprintf( stderr, "Mix failed?\n" );
281                                 }
282                         }
283                         else
284                         {
285                                 fprintf( stderr, "Invalid position for a mix...\n" );
286                         }
287                 }
288                 else if ( !strcmp( argv[ i ], "-mixer" ) )
289                 {
290                         if ( mix != NULL )
291                         {
292                                 char *id = strdup( argv[ ++ i ] );
293                                 char *arg = strchr( id, ':' );
294                                 mlt_field field = mlt_tractor_field( mix );
295                                 mlt_transition transition = NULL;
296                                 if ( arg != NULL )
297                                         *arg ++ = '\0';
298                                 transition = mlt_factory_transition( id, arg );
299                                 if ( transition != NULL )
300                                 {
301                                         properties = mlt_transition_properties( transition );
302                                         mlt_properties_inherit( properties, group );
303                                         mlt_field_plant_transition( field, transition, 0, 1 );
304                                         mlt_properties_set_position( properties, "in", 0 );
305                                         mlt_properties_set_position( properties, "out", mlt_producer_get_out( ( mlt_producer )mix ) );
306                                         mlt_transition_close( transition );
307                                 }
308                                 free( id );
309                         }
310                         else
311                         {
312                                 fprintf( stderr, "Invalid mixer...\n" );
313                         }
314                 }
315                 else if ( !strcmp( argv[ i ], "-filter" ) )
316                 {
317                         mlt_filter filter = create_filter( field, argv[ ++ i ], track );
318                         if ( filter != NULL )
319                         {
320                                 properties = mlt_filter_properties( filter );
321                                 mlt_properties_inherit( properties, group );
322                         }
323                 }
324                 else if ( !strcmp( argv[ i ], "-transition" ) )
325                 {
326                         mlt_transition transition = create_transition( field, argv[ ++ i ], track - 1 );
327                         if ( transition != NULL )
328                         {
329                                 properties = mlt_transition_properties( transition );
330                                 mlt_properties_inherit( properties, group );
331                         }
332                 }
333                 else if ( !strcmp( argv[ i ], "-blank" ) )
334                 {
335                         if ( producer != NULL && !mlt_producer_is_cut( producer ) )
336                                 mlt_playlist_append( playlist, producer );
337                         producer = NULL;
338                         mlt_playlist_blank( playlist, atof( argv[ ++ i ] ) );
339                 }
340                 else if ( !strcmp( argv[ i ], "-track" ) ||
341                                   !strcmp( argv[ i ], "-hide-track" ) ||
342                                   !strcmp( argv[ i ], "-hide-video" ) ||
343                                   !strcmp( argv[ i ], "-hide-audio" ) )
344                 {
345                         if ( producer != NULL && !mlt_producer_is_cut( producer ) )
346                                 mlt_playlist_append( playlist, producer );
347                         producer = NULL;
348                         mlt_multitrack_connect( multitrack, mlt_playlist_producer( playlist ), track ++ );
349                         track_service( field, playlist, ( mlt_destructor )mlt_playlist_close );
350                         playlist = mlt_playlist_init( );
351                         if ( playlist != NULL )
352                         {
353                                 properties = mlt_playlist_properties( playlist );
354                                 if ( !strcmp( argv[ i ], "-hide-track" ) )
355                                         mlt_properties_set_int( properties, "hide", 3 );
356                                 else if ( !strcmp( argv[ i ], "-hide-video" ) )
357                                         mlt_properties_set_int( properties, "hide", 1 );
358                                 else if ( !strcmp( argv[ i ], "-hide-audio" ) )
359                                         mlt_properties_set_int( properties, "hide", 2 );
360                         }
361                 }
362                 else if ( strchr( argv[ i ], '=' ) )
363                 {
364                         mlt_properties_parse( properties, argv[ i ] );
365                 }
366                 else if ( argv[ i ][ 0 ] != '-' )
367                 {
368                         if ( producer != NULL && !mlt_producer_is_cut( producer ) )
369                                 mlt_playlist_append( playlist, producer );
370                         if ( title == NULL )
371                                 title = argv[ i ];
372                         producer = create_producer( field, argv[ i ] );
373                         if ( producer != NULL )
374                         {
375                                 properties = mlt_producer_properties( producer );
376                                 mlt_properties_inherit( properties, group );
377                         }
378                 }
379                 else
380                 {
381                         if ( !strcmp( argv[ i ], "-serialise" ) )
382                                 i += 2;
383                         else if ( !strcmp( argv[ i ], "-consumer" ) )
384                                 i += 2;
385
386                         while ( argv[ i ] != NULL && strchr( argv[ i ], '=' ) )
387                                 i ++;
388
389                         i --;
390                 }
391         }
392
393         // Connect last producer to playlist
394         if ( producer != NULL && !mlt_producer_is_cut( producer ) )
395                 mlt_playlist_append( playlist, producer );
396
397         // Track the last playlist too
398         track_service( field, playlist, ( mlt_destructor )mlt_playlist_close );
399
400         // We must have a playlist to connect
401         if ( mlt_playlist_count( playlist ) > 0 )
402                 mlt_multitrack_connect( multitrack, mlt_playlist_producer( playlist ), track );
403
404         mlt_producer prod = mlt_tractor_producer( tractor );
405         mlt_producer_optimise( prod );
406         mlt_properties props = mlt_tractor_properties( tractor );
407         mlt_properties_set_data( props, "group", group, 0, ( mlt_destructor )mlt_properties_close, NULL );
408         mlt_properties_set_position( props, "length", mlt_producer_get_out( mlt_multitrack_producer( multitrack ) ) + 1 );
409         mlt_producer_set_in_and_out( prod, 0, mlt_producer_get_out( mlt_multitrack_producer( multitrack ) ) );
410         mlt_properties_set_double( props, "fps", mlt_producer_get_fps( mlt_multitrack_producer( multitrack ) ) );
411         if ( title != NULL )
412                 mlt_properties_set( props, "title", strchr( title, '/' ) ? strrchr( title, '/' ) + 1 : title );
413
414         return prod;
415 }