]> git.sesse.net Git - mlt/blob - src/modules/melt/producer_melt.c
Rename inigo, fezzik, and westley.
[mlt] / src / modules / melt / producer_melt.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 library is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Lesser General Public
8  * License as published by the Free Software Foundation; either
9  * version 2.1 of the License, or (at your option) any later version.
10  *
11  * This library 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 GNU
14  * Lesser General Public License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public
17  * License along with this library; if not, write to the Free Software
18  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
19  */
20
21 #include <stdio.h>
22 #include <stdlib.h>
23 #include <string.h>
24
25 #include <framework/mlt.h>
26
27 mlt_producer producer_inigo_init( mlt_profile profile, mlt_service_type type, const char *id, char **argv );
28
29 mlt_producer producer_inigo_file_init( mlt_profile profile, mlt_service_type type, const char *id, 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( profile, type, id, 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_profile profile, mlt_field field, char *file )
71 {
72         mlt_producer result = mlt_factory_producer( profile, "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_profile profile, mlt_field field, char *id, int track )
81 {
82         char *temp = strdup( id );
83         char *arg = strchr( temp, ':' );
84         if ( arg != NULL )
85                 *arg ++ = '\0';
86         mlt_filter filter = mlt_factory_filter( profile, temp, arg );
87         if ( filter != NULL )
88                 track_service( field, filter, ( mlt_destructor )mlt_filter_close );
89         free( temp );
90         return filter;
91 }
92
93 static mlt_filter create_filter( mlt_profile profile, mlt_field field, char *id, int track )
94 {
95         char *temp = strdup( id );
96         char *arg = strchr( temp, ':' );
97         if ( arg != NULL )
98                 *arg ++ = '\0';
99         mlt_filter filter = mlt_factory_filter( profile, temp, arg );
100         if ( filter != NULL )
101         {
102                 mlt_field_plant_filter( field, filter, track );
103                 track_service( field, filter, ( mlt_destructor )mlt_filter_close );
104         }
105         free( temp );
106         return filter;
107 }
108
109 static mlt_transition create_transition( mlt_profile profile, mlt_field field, char *id, int track )
110 {
111         char *arg = strchr( id, ':' );
112         if ( arg != NULL )
113                 *arg ++ = '\0';
114         mlt_transition transition = mlt_factory_transition( profile, id, arg );
115         if ( transition != NULL )
116         {
117                 mlt_field_plant_transition( field, transition, track, track + 1 );
118                 track_service( field, transition, ( mlt_destructor )mlt_transition_close );
119         }
120         return transition;
121 }
122
123 mlt_producer producer_inigo_init( mlt_profile profile, mlt_service_type type, const char *id, char **argv )
124 {
125         int i;
126         int track = 0;
127         mlt_producer producer = NULL;
128         mlt_tractor mix = NULL;
129         mlt_playlist playlist = mlt_playlist_init( );
130         mlt_properties group = mlt_properties_new( );
131         mlt_tractor tractor = mlt_tractor_new( );
132         mlt_properties properties = MLT_TRACTOR_PROPERTIES( tractor );
133         mlt_field field = mlt_tractor_field( tractor );
134         mlt_properties field_properties = mlt_field_properties( field );
135         mlt_multitrack multitrack = mlt_tractor_multitrack( tractor );
136         char *title = NULL;
137
138         // Assistance for template construction (allows -track usage to specify the first track)
139         mlt_properties_set_int( MLT_PLAYLIST_PROPERTIES( playlist ), "_inigo_first", 1 );
140
141         // We need to track the number of registered filters
142         mlt_properties_set_int( field_properties, "registered", 0 );
143
144         // Parse the arguments
145         if ( argv )
146         for ( i = 0; argv[ i ] != NULL; i ++ )
147         {
148                 if ( !strcmp( argv[ i ], "-group" ) )
149                 {
150                         if ( mlt_properties_count( group ) != 0 )
151                         {
152                                 mlt_properties_close( group );
153                                 group = mlt_properties_new( );
154                         }
155                         if ( group != NULL )
156                                 properties = group;
157                 }
158                 else if ( !strcmp( argv[ i ], "-attach" ) || 
159                                   !strcmp( argv[ i ], "-attach-cut" ) ||
160                                   !strcmp( argv[ i ], "-attach-track" ) ||
161                                   !strcmp( argv[ i ], "-attach-clip" ) )
162                 {
163                         int type = !strcmp( argv[ i ], "-attach" ) ? 0 : 
164                                            !strcmp( argv[ i ], "-attach-cut" ) ? 1 : 
165                                            !strcmp( argv[ i ], "-attach-track" ) ? 2 : 3;
166                         mlt_filter filter = create_attach( profile, field, argv[ ++ i ], track );
167                         if ( producer != NULL && !mlt_producer_is_cut( producer ) )
168                         {
169                                 mlt_playlist_clip_info info;
170                                 mlt_playlist_append( playlist, producer );
171                                 mlt_playlist_get_clip_info( playlist, &info, mlt_playlist_count( playlist ) - 1 );
172                                 producer = info.cut;
173                         }
174
175                         if ( type == 1 || type == 2 )
176                         {
177                                 mlt_playlist_clip_info info;
178                                 mlt_playlist_get_clip_info( playlist, &info, mlt_playlist_count( playlist ) - 1 );
179                                 producer = info.cut;
180                         }
181
182                         if ( filter != NULL && mlt_playlist_count( playlist ) > 0 )
183                         {
184                                 if ( type == 0 )
185                                         mlt_service_attach( ( mlt_service )properties, filter );
186                                 else if ( type == 1 )
187                                         mlt_service_attach( ( mlt_service )producer, filter );
188                                 else if ( type == 2 )
189                                         mlt_service_attach( ( mlt_service )playlist, filter );
190                                 else if ( type == 3 )
191                                         mlt_service_attach( ( mlt_service )mlt_producer_cut_parent( producer ), filter );
192
193                                 properties = MLT_FILTER_PROPERTIES( filter );
194                                 mlt_properties_inherit( properties, group );
195                         }
196                         else if ( filter != NULL )
197                         {
198                                 mlt_service_attach( ( mlt_service )playlist, filter );
199                                 properties = MLT_FILTER_PROPERTIES( filter );
200                                 mlt_properties_inherit( properties, group );
201                         }
202                 }
203                 else if ( !strcmp( argv[ i ], "-repeat" ) )
204                 {
205                         int repeat = atoi( argv[ ++ i ] );
206                         if ( producer != NULL && !mlt_producer_is_cut( producer ) )
207                                 mlt_playlist_append( playlist, producer );
208                         producer = NULL;
209                         if ( mlt_playlist_count( playlist ) > 0 )
210                         {
211                                 mlt_playlist_clip_info info;
212                                 mlt_playlist_repeat_clip( playlist, mlt_playlist_count( playlist ) - 1, repeat );
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 ], "-split" ) )
219                 {
220                         int split = atoi( argv[ ++ i ] );
221                         if ( producer != NULL && !mlt_producer_is_cut( producer ) )
222                                 mlt_playlist_append( playlist, producer );
223                         producer = NULL;
224                         if ( mlt_playlist_count( playlist ) > 0 )
225                         {
226                                 mlt_playlist_clip_info info;
227                                 mlt_playlist_get_clip_info( playlist, &info, mlt_playlist_count( playlist ) - 1 );
228                                 split = split < 0 ? info.frame_out + split : split;
229                                 mlt_playlist_split( playlist, mlt_playlist_count( playlist ) - 1, split );
230                                 mlt_playlist_get_clip_info( playlist, &info, mlt_playlist_count( playlist ) - 1 );
231                                 producer = info.cut;
232                                 properties = MLT_PRODUCER_PROPERTIES( producer );
233                         }
234                 }
235                 else if ( !strcmp( argv[ i ], "-swap" ) )
236                 {
237                         if ( producer != NULL && !mlt_producer_is_cut( producer ) )
238                                 mlt_playlist_append( playlist, producer );
239                         producer = NULL;
240                         if ( mlt_playlist_count( playlist ) >= 2 )
241                         {
242                                 mlt_playlist_clip_info info;
243                                 mlt_playlist_move( playlist, mlt_playlist_count( playlist ) - 2, mlt_playlist_count( playlist ) - 1 );
244                                 mlt_playlist_get_clip_info( playlist, &info, mlt_playlist_count( playlist ) - 1 );
245                                 producer = info.cut;
246                                 properties = MLT_PRODUCER_PROPERTIES( producer );
247                         }
248                 }
249                 else if ( !strcmp( argv[ i ], "-join" ) )
250                 {
251                         int clips = atoi( argv[ ++ i ] );
252                         if ( producer != NULL && !mlt_producer_is_cut( producer ) )
253                                 mlt_playlist_append( playlist, producer );
254                         producer = NULL;
255                         if ( mlt_playlist_count( playlist ) > 0 )
256                         {
257                                 mlt_playlist_clip_info info;
258                                 int clip = clips <= 0 ? 0 : mlt_playlist_count( playlist ) - clips - 1;
259                                 if ( clip < 0 ) clip = 0;
260                                 if ( clip >= mlt_playlist_count( playlist ) ) clip = mlt_playlist_count( playlist ) - 2;
261                                 if ( clips < 0 ) clips =  mlt_playlist_count( playlist ) - 1;
262                                 mlt_playlist_join( playlist, clip, clips, 0 );
263                                 mlt_playlist_get_clip_info( playlist, &info, mlt_playlist_count( playlist ) - 1 );
264                                 producer = info.cut;
265                                 properties = MLT_PRODUCER_PROPERTIES( producer );
266                         }
267                 }
268                 else if ( !strcmp( argv[ i ], "-remove" ) )
269                 {
270                         if ( producer != NULL && !mlt_producer_is_cut( producer ) )
271                                 mlt_playlist_append( playlist, producer );
272                         producer = NULL;
273                         if ( mlt_playlist_count( playlist ) > 0 )
274                         {
275                                 mlt_playlist_clip_info info;
276                                 mlt_playlist_remove( playlist, mlt_playlist_count( playlist ) - 1 );
277                                 mlt_playlist_get_clip_info( playlist, &info, mlt_playlist_count( playlist ) - 1 );
278                                 producer = info.cut;
279                                 properties = MLT_PRODUCER_PROPERTIES( producer );
280                         }
281                 }
282                 else if ( !strcmp( argv[ i ], "-mix" ) )
283                 {
284                         int length = atoi( argv[ ++ i ] );
285                         if ( producer != NULL && !mlt_producer_is_cut( producer ) )
286                                 mlt_playlist_append( playlist, producer );
287                         producer = NULL;
288                         if ( mlt_playlist_count( playlist ) >= 2 )
289                         {
290                                 if ( mlt_playlist_mix( playlist, mlt_playlist_count( playlist ) - 2, length, NULL ) == 0 )
291                                 {
292                                         mlt_playlist_clip_info info;
293                                         mlt_playlist_get_clip_info( playlist, &info, mlt_playlist_count( playlist ) - 1 );
294                                         if ( mlt_properties_get_data( ( mlt_properties )info.producer, "mlt_mix", NULL ) == NULL )
295                                                 mlt_playlist_get_clip_info( playlist, &info, mlt_playlist_count( playlist ) - 2 );
296                                         mix = ( mlt_tractor )mlt_properties_get_data( ( mlt_properties )info.producer, "mlt_mix", NULL );
297                                         properties = NULL;
298                                 }
299                                 else
300                                 {
301                                         fprintf( stderr, "Mix failed?\n" );
302                                 }
303                         }
304                         else
305                         {
306                                 fprintf( stderr, "Invalid position for a mix...\n" );
307                         }
308                 }
309                 else if ( !strcmp( argv[ i ], "-mixer" ) )
310                 {
311                         if ( mix != NULL )
312                         {
313                                 char *id = strdup( argv[ ++ i ] );
314                                 char *arg = strchr( id, ':' );
315                                 mlt_field field = mlt_tractor_field( mix );
316                                 mlt_transition transition = NULL;
317                                 if ( arg != NULL )
318                                         *arg ++ = '\0';
319                                 transition = mlt_factory_transition( profile, id, arg );
320                                 if ( transition != NULL )
321                                 {
322                                         properties = MLT_TRANSITION_PROPERTIES( transition );
323                                         mlt_properties_inherit( properties, group );
324                                         mlt_field_plant_transition( field, transition, 0, 1 );
325                                         mlt_properties_set_position( properties, "in", 0 );
326                                         mlt_properties_set_position( properties, "out", mlt_producer_get_out( ( mlt_producer )mix ) );
327                                         mlt_transition_close( transition );
328                                 }
329                                 free( id );
330                         }
331                         else
332                         {
333                                 fprintf( stderr, "Invalid mixer...\n" );
334                         }
335                 }
336                 else if ( !strcmp( argv[ i ], "-filter" ) )
337                 {
338                         mlt_filter filter = create_filter( profile, field, argv[ ++ i ], track );
339                         if ( filter != NULL )
340                         {
341                                 properties = MLT_FILTER_PROPERTIES( filter );
342                                 mlt_properties_inherit( properties, group );
343                         }
344                 }
345                 else if ( !strcmp( argv[ i ], "-transition" ) )
346                 {
347                         mlt_transition transition = create_transition( profile, field, argv[ ++ i ], track - 1 );
348                         if ( transition != NULL )
349                         {
350                                 properties = MLT_TRANSITION_PROPERTIES( transition );
351                                 mlt_properties_inherit( properties, group );
352                         }
353                 }
354                 else if ( !strcmp( argv[ i ], "-blank" ) )
355                 {
356                         if ( producer != NULL && !mlt_producer_is_cut( producer ) )
357                                 mlt_playlist_append( playlist, producer );
358                         producer = NULL;
359                         mlt_playlist_blank( playlist, atof( argv[ ++ i ] ) );
360                 }
361                 else if ( !strcmp( argv[ i ], "-track" ) ||
362                                   !strcmp( argv[ i ], "-null-track" ) ||
363                                   !strcmp( argv[ i ], "-video-track" ) ||
364                                   !strcmp( argv[ i ], "-audio-track" ) ||
365                                   !strcmp( argv[ i ], "-hide-track" ) ||
366                                   !strcmp( argv[ i ], "-hide-video" ) ||
367                                   !strcmp( argv[ i ], "-hide-audio" ) )
368                 {
369                         if ( producer != NULL && !mlt_producer_is_cut( producer ) )
370                                 mlt_playlist_append( playlist, producer );
371                         producer = NULL;
372                         if ( !mlt_properties_get_int( MLT_PLAYLIST_PROPERTIES( playlist ), "_inigo_first" ) || 
373                                   mlt_producer_get_playtime( MLT_PLAYLIST_PRODUCER( playlist ) ) > 0 )
374                         {
375                                 mlt_multitrack_connect( multitrack, MLT_PLAYLIST_PRODUCER( playlist ), track ++ );
376                                 track_service( field, playlist, ( mlt_destructor )mlt_playlist_close );
377                                 playlist = mlt_playlist_init( );
378                         }
379                         if ( playlist != NULL )
380                         {
381                                 properties = MLT_PLAYLIST_PROPERTIES( playlist );
382                                 if ( !strcmp( argv[ i ], "-null-track" ) || !strcmp( argv[ i ], "-hide-track" ) )
383                                         mlt_properties_set_int( properties, "hide", 3 );
384                                 else if ( !strcmp( argv[ i ], "-audio-track" ) || !strcmp( argv[ i ], "-hide-video" ) )
385                                         mlt_properties_set_int( properties, "hide", 1 );
386                                 else if ( !strcmp( argv[ i ], "-video-track" ) || !strcmp( argv[ i ], "-hide-audio" ) )
387                                         mlt_properties_set_int( properties, "hide", 2 );
388                         }
389                 }
390                 else if ( strchr( argv[ i ], '=' ) && strstr( argv[ i ], "<?xml" ) != argv[ i ] )
391                 {
392                         mlt_properties_parse( properties, argv[ i ] );
393                 }
394                 else if ( argv[ i ][ 0 ] != '-' )
395                 {
396                         if ( producer != NULL && !mlt_producer_is_cut( producer ) )
397                                 mlt_playlist_append( playlist, producer );
398                         if ( title == NULL && strstr( argv[ i ], "<?xml" ) != argv[ i ] )
399                                 title = argv[ i ];
400                         producer = create_producer( profile, field, argv[ i ] );
401                         if ( producer != NULL )
402                         {
403                                 properties = MLT_PRODUCER_PROPERTIES( producer );
404                                 mlt_properties_inherit( properties, group );
405                         }
406                         else
407                         {
408                                 fprintf( stderr, "Failed to load \"%s\"\n", argv[ i ] );
409                         }
410                 }
411                 else
412                 {
413                         int backtrack = 0;
414                         if ( !strcmp( argv[ i ], "-serialise" ) ||
415                              !strcmp( argv[ i ], "-consumer" ) ||
416                              !strcmp( argv[ i ], "-profile" ) )
417                         {
418                                 i += 2;
419                                 backtrack = 1;
420                         }
421
422                         while ( argv[ i ] != NULL && strchr( argv[ i ], '=' ) )
423                         {
424                                 i ++;
425                                 backtrack = 1;
426                         }
427                         if ( backtrack )
428                                 i --;
429                 }
430         }
431
432         // Connect last producer to playlist
433         if ( producer != NULL && !mlt_producer_is_cut( producer ) )
434                 mlt_playlist_append( playlist, producer );
435
436         // Track the last playlist too
437         track_service( field, playlist, ( mlt_destructor )mlt_playlist_close );
438
439         // We must have a playlist to connect
440         if ( !mlt_properties_get_int( MLT_PLAYLIST_PROPERTIES( playlist ), "_inigo_first" ) || 
441                   mlt_producer_get_playtime( MLT_PLAYLIST_PRODUCER( playlist ) ) > 0 )
442                 mlt_multitrack_connect( multitrack, MLT_PLAYLIST_PRODUCER( playlist ), track );
443
444         mlt_producer prod = MLT_TRACTOR_PRODUCER( tractor );
445         mlt_producer_optimise( prod );
446         mlt_properties props = MLT_TRACTOR_PROPERTIES( tractor );
447         mlt_properties_set_data( props, "group", group, 0, ( mlt_destructor )mlt_properties_close, NULL );
448         mlt_properties_set_position( props, "length", mlt_producer_get_out( MLT_MULTITRACK_PRODUCER( multitrack ) ) + 1 );
449         mlt_producer_set_in_and_out( prod, 0, mlt_producer_get_out( MLT_MULTITRACK_PRODUCER( multitrack ) ) );
450         if ( title != NULL )
451                 mlt_properties_set( props, "title", strchr( title, '/' ) ? strrchr( title, '/' ) + 1 : title );
452
453         return prod;
454 }