]> git.sesse.net Git - mlt/blob - src/modules/melt/producer_melt.c
Add an automatic profile feature to melt.
[mlt] / src / modules / melt / producer_melt.c
1 /*
2  * producer_melt.c -- load from melt command line syntax
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_melt_init( mlt_profile profile, mlt_service_type type, const char *id, char **argv );
28
29 mlt_producer producer_melt_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_melt_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         char *filedup;
73         if ( profile->description && strncmp( file, "consumer:", 9 ) && !strcmp( profile->description, "consumer:" ) )
74         {
75                 filedup = calloc( 1, strlen( file ) + strlen( profile->description ) + 1 );
76                 strcat( filedup, profile->description );
77                 strcat( filedup, file );
78         }
79         else
80         {
81                 filedup = strdup( file );
82         }
83         mlt_producer result = mlt_factory_producer( profile, NULL, filedup );
84         free( filedup );
85
86         if ( result != NULL )
87                 track_service( field, result, ( mlt_destructor )mlt_producer_close );
88
89         return result;
90 }
91
92 static mlt_filter create_attach( mlt_profile profile, mlt_field field, char *id, int track )
93 {
94         char *temp = strdup( id );
95         char *arg = strchr( temp, ':' );
96         if ( arg != NULL )
97                 *arg ++ = '\0';
98         mlt_filter filter = mlt_factory_filter( profile, temp, arg );
99         if ( filter != NULL )
100                 track_service( field, filter, ( mlt_destructor )mlt_filter_close );
101         free( temp );
102         return filter;
103 }
104
105 static mlt_filter create_filter( mlt_profile profile, mlt_field field, char *id, int track )
106 {
107         char *temp = strdup( id );
108         char *arg = strchr( temp, ':' );
109         if ( arg != NULL )
110                 *arg ++ = '\0';
111         mlt_filter filter = mlt_factory_filter( profile, temp, arg );
112         if ( filter != NULL )
113         {
114                 mlt_field_plant_filter( field, filter, track );
115                 track_service( field, filter, ( mlt_destructor )mlt_filter_close );
116         }
117         free( temp );
118         return filter;
119 }
120
121 static mlt_transition create_transition( mlt_profile profile, mlt_field field, char *id, int track )
122 {
123         char *arg = strchr( id, ':' );
124         if ( arg != NULL )
125                 *arg ++ = '\0';
126         mlt_transition transition = mlt_factory_transition( profile, id, arg );
127         if ( transition != NULL )
128         {
129                 mlt_field_plant_transition( field, transition, track, track + 1 );
130                 track_service( field, transition, ( mlt_destructor )mlt_transition_close );
131         }
132         return transition;
133 }
134
135 mlt_producer producer_melt_init( mlt_profile profile, mlt_service_type type, const char *id, char **argv )
136 {
137         int i;
138         int track = 0;
139         mlt_producer producer = NULL;
140         mlt_tractor mix = NULL;
141         mlt_playlist playlist = mlt_playlist_init( );
142         mlt_properties group = mlt_properties_new( );
143         mlt_tractor tractor = mlt_tractor_new( );
144         mlt_properties properties = MLT_TRACTOR_PROPERTIES( tractor );
145         mlt_field field = mlt_tractor_field( tractor );
146         mlt_properties field_properties = mlt_field_properties( field );
147         mlt_multitrack multitrack = mlt_tractor_multitrack( tractor );
148         char *title = NULL;
149
150         // Assistance for template construction (allows -track usage to specify the first track)
151         mlt_properties_set_int( MLT_PLAYLIST_PROPERTIES( playlist ), "_melt_first", 1 );
152
153         // We need to track the number of registered filters
154         mlt_properties_set_int( field_properties, "registered", 0 );
155
156         // Parse the arguments
157         if ( argv )
158         for ( i = 0; argv[ i ] != NULL; i ++ )
159         {
160                 if ( !strcmp( argv[ i ], "-group" ) )
161                 {
162                         if ( mlt_properties_count( group ) != 0 )
163                         {
164                                 mlt_properties_close( group );
165                                 group = mlt_properties_new( );
166                         }
167                         if ( group != NULL )
168                                 properties = group;
169                 }
170                 else if ( !strcmp( argv[ i ], "-attach" ) || 
171                                   !strcmp( argv[ i ], "-attach-cut" ) ||
172                                   !strcmp( argv[ i ], "-attach-track" ) ||
173                                   !strcmp( argv[ i ], "-attach-clip" ) )
174                 {
175                         int type = !strcmp( argv[ i ], "-attach" ) ? 0 : 
176                                            !strcmp( argv[ i ], "-attach-cut" ) ? 1 : 
177                                            !strcmp( argv[ i ], "-attach-track" ) ? 2 : 3;
178                         mlt_filter filter = create_attach( profile, field, argv[ ++ i ], track );
179                         if ( producer != NULL && !mlt_producer_is_cut( producer ) )
180                         {
181                                 mlt_playlist_clip_info info;
182                                 mlt_playlist_append( playlist, producer );
183                                 mlt_playlist_get_clip_info( playlist, &info, mlt_playlist_count( playlist ) - 1 );
184                                 producer = info.cut;
185                         }
186
187                         if ( type == 1 || type == 2 )
188                         {
189                                 mlt_playlist_clip_info info;
190                                 mlt_playlist_get_clip_info( playlist, &info, mlt_playlist_count( playlist ) - 1 );
191                                 producer = info.cut;
192                         }
193
194                         if ( filter != NULL && mlt_playlist_count( playlist ) > 0 )
195                         {
196                                 if ( type == 0 )
197                                         mlt_service_attach( ( mlt_service )properties, filter );
198                                 else if ( type == 1 )
199                                         mlt_service_attach( ( mlt_service )producer, filter );
200                                 else if ( type == 2 )
201                                         mlt_service_attach( ( mlt_service )playlist, filter );
202                                 else if ( type == 3 )
203                                         mlt_service_attach( ( mlt_service )mlt_producer_cut_parent( producer ), filter );
204
205                                 properties = MLT_FILTER_PROPERTIES( filter );
206                                 mlt_properties_inherit( properties, group );
207                         }
208                         else if ( filter != NULL )
209                         {
210                                 mlt_service_attach( ( mlt_service )playlist, filter );
211                                 properties = MLT_FILTER_PROPERTIES( filter );
212                                 mlt_properties_inherit( properties, group );
213                         }
214                 }
215                 else if ( !strcmp( argv[ i ], "-repeat" ) )
216                 {
217                         int repeat = atoi( argv[ ++ i ] );
218                         if ( producer != NULL && !mlt_producer_is_cut( producer ) )
219                                 mlt_playlist_append( playlist, producer );
220                         producer = NULL;
221                         if ( mlt_playlist_count( playlist ) > 0 )
222                         {
223                                 mlt_playlist_clip_info info;
224                                 mlt_playlist_repeat_clip( playlist, mlt_playlist_count( playlist ) - 1, repeat );
225                                 mlt_playlist_get_clip_info( playlist, &info, mlt_playlist_count( playlist ) - 1 );
226                                 producer = info.cut;
227                                 properties = MLT_PRODUCER_PROPERTIES( producer );
228                         }
229                 }
230                 else if ( !strcmp( argv[ i ], "-split" ) )
231                 {
232                         int split = atoi( argv[ ++ i ] );
233                         if ( producer != NULL && !mlt_producer_is_cut( producer ) )
234                                 mlt_playlist_append( playlist, producer );
235                         producer = NULL;
236                         if ( mlt_playlist_count( playlist ) > 0 )
237                         {
238                                 mlt_playlist_clip_info info;
239                                 mlt_playlist_get_clip_info( playlist, &info, mlt_playlist_count( playlist ) - 1 );
240                                 split = split < 0 ? info.frame_out + split : split;
241                                 mlt_playlist_split( playlist, mlt_playlist_count( playlist ) - 1, split );
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 ], "-swap" ) )
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 ) >= 2 )
253                         {
254                                 mlt_playlist_clip_info info;
255                                 mlt_playlist_move( playlist, mlt_playlist_count( playlist ) - 2, 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 ], "-join" ) )
262                 {
263                         int clips = 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 ) > 0 )
268                         {
269                                 mlt_playlist_clip_info info;
270                                 int clip = clips <= 0 ? 0 : mlt_playlist_count( playlist ) - clips - 1;
271                                 if ( clip < 0 ) clip = 0;
272                                 if ( clip >= mlt_playlist_count( playlist ) ) clip = mlt_playlist_count( playlist ) - 2;
273                                 if ( clips < 0 ) clips =  mlt_playlist_count( playlist ) - 1;
274                                 mlt_playlist_join( playlist, clip, clips, 0 );
275                                 mlt_playlist_get_clip_info( playlist, &info, mlt_playlist_count( playlist ) - 1 );
276                                 producer = info.cut;
277                                 properties = MLT_PRODUCER_PROPERTIES( producer );
278                         }
279                 }
280                 else if ( !strcmp( argv[ i ], "-remove" ) )
281                 {
282                         if ( producer != NULL && !mlt_producer_is_cut( producer ) )
283                                 mlt_playlist_append( playlist, producer );
284                         producer = NULL;
285                         if ( mlt_playlist_count( playlist ) > 0 )
286                         {
287                                 mlt_playlist_clip_info info;
288                                 mlt_playlist_remove( playlist, mlt_playlist_count( playlist ) - 1 );
289                                 mlt_playlist_get_clip_info( playlist, &info, mlt_playlist_count( playlist ) - 1 );
290                                 producer = info.cut;
291                                 properties = MLT_PRODUCER_PROPERTIES( producer );
292                         }
293                 }
294                 else if ( !strcmp( argv[ i ], "-mix" ) )
295                 {
296                         int length = atoi( argv[ ++ i ] );
297                         if ( producer != NULL && !mlt_producer_is_cut( producer ) )
298                                 mlt_playlist_append( playlist, producer );
299                         producer = NULL;
300                         if ( mlt_playlist_count( playlist ) >= 2 )
301                         {
302                                 if ( mlt_playlist_mix( playlist, mlt_playlist_count( playlist ) - 2, length, NULL ) == 0 )
303                                 {
304                                         mlt_playlist_clip_info info;
305                                         mlt_playlist_get_clip_info( playlist, &info, mlt_playlist_count( playlist ) - 1 );
306                                         if ( mlt_properties_get_data( ( mlt_properties )info.producer, "mlt_mix", NULL ) == NULL )
307                                                 mlt_playlist_get_clip_info( playlist, &info, mlt_playlist_count( playlist ) - 2 );
308                                         mix = ( mlt_tractor )mlt_properties_get_data( ( mlt_properties )info.producer, "mlt_mix", NULL );
309                                         properties = NULL;
310                                 }
311                                 else
312                                 {
313                                         fprintf( stderr, "Mix failed?\n" );
314                                 }
315                         }
316                         else
317                         {
318                                 fprintf( stderr, "Invalid position for a mix...\n" );
319                         }
320                 }
321                 else if ( !strcmp( argv[ i ], "-mixer" ) )
322                 {
323                         if ( mix != NULL )
324                         {
325                                 char *id = strdup( argv[ ++ i ] );
326                                 char *arg = strchr( id, ':' );
327                                 mlt_field field = mlt_tractor_field( mix );
328                                 mlt_transition transition = NULL;
329                                 if ( arg != NULL )
330                                         *arg ++ = '\0';
331                                 transition = mlt_factory_transition( profile, id, arg );
332                                 if ( transition != NULL )
333                                 {
334                                         properties = MLT_TRANSITION_PROPERTIES( transition );
335                                         mlt_properties_inherit( properties, group );
336                                         mlt_field_plant_transition( field, transition, 0, 1 );
337                                         mlt_properties_set_position( properties, "in", 0 );
338                                         mlt_properties_set_position( properties, "out", mlt_producer_get_out( ( mlt_producer )mix ) );
339                                         mlt_transition_close( transition );
340                                 }
341                                 free( id );
342                         }
343                         else
344                         {
345                                 fprintf( stderr, "Invalid mixer...\n" );
346                         }
347                 }
348                 else if ( !strcmp( argv[ i ], "-filter" ) )
349                 {
350                         mlt_filter filter = create_filter( profile, field, argv[ ++ i ], track );
351                         if ( filter != NULL )
352                         {
353                                 properties = MLT_FILTER_PROPERTIES( filter );
354                                 mlt_properties_inherit( properties, group );
355                         }
356                 }
357                 else if ( !strcmp( argv[ i ], "-transition" ) )
358                 {
359                         mlt_transition transition = create_transition( profile, field, argv[ ++ i ], track - 1 );
360                         if ( transition != NULL )
361                         {
362                                 properties = MLT_TRANSITION_PROPERTIES( transition );
363                                 mlt_properties_inherit( properties, group );
364                         }
365                 }
366                 else if ( !strcmp( argv[ i ], "-blank" ) )
367                 {
368                         if ( producer != NULL && !mlt_producer_is_cut( producer ) )
369                                 mlt_playlist_append( playlist, producer );
370                         producer = NULL;
371                         mlt_playlist_blank( playlist, atof( argv[ ++ i ] ) );
372                 }
373                 else if ( !strcmp( argv[ i ], "-track" ) ||
374                                   !strcmp( argv[ i ], "-null-track" ) ||
375                                   !strcmp( argv[ i ], "-video-track" ) ||
376                                   !strcmp( argv[ i ], "-audio-track" ) ||
377                                   !strcmp( argv[ i ], "-hide-track" ) ||
378                                   !strcmp( argv[ i ], "-hide-video" ) ||
379                                   !strcmp( argv[ i ], "-hide-audio" ) )
380                 {
381                         if ( producer != NULL && !mlt_producer_is_cut( producer ) )
382                                 mlt_playlist_append( playlist, producer );
383                         producer = NULL;
384                         if ( !mlt_properties_get_int( MLT_PLAYLIST_PROPERTIES( playlist ), "_melt_first" ) || 
385                                   mlt_producer_get_playtime( MLT_PLAYLIST_PRODUCER( playlist ) ) > 0 )
386                         {
387                                 mlt_multitrack_connect( multitrack, MLT_PLAYLIST_PRODUCER( playlist ), track ++ );
388                                 track_service( field, playlist, ( mlt_destructor )mlt_playlist_close );
389                                 playlist = mlt_playlist_init( );
390                         }
391                         if ( playlist != NULL )
392                         {
393                                 properties = MLT_PLAYLIST_PROPERTIES( playlist );
394                                 if ( !strcmp( argv[ i ], "-null-track" ) || !strcmp( argv[ i ], "-hide-track" ) )
395                                         mlt_properties_set_int( properties, "hide", 3 );
396                                 else if ( !strcmp( argv[ i ], "-audio-track" ) || !strcmp( argv[ i ], "-hide-video" ) )
397                                         mlt_properties_set_int( properties, "hide", 1 );
398                                 else if ( !strcmp( argv[ i ], "-video-track" ) || !strcmp( argv[ i ], "-hide-audio" ) )
399                                         mlt_properties_set_int( properties, "hide", 2 );
400                         }
401                 }
402                 else if ( strchr( argv[ i ], '=' ) && strstr( argv[ i ], "<?xml" ) != argv[ i ] )
403                 {
404                         mlt_properties_parse( properties, argv[ i ] );
405                 }
406                 else if ( argv[ i ][ 0 ] != '-' )
407                 {
408                         if ( producer != NULL && !mlt_producer_is_cut( producer ) )
409                                 mlt_playlist_append( playlist, producer );
410                         if ( title == NULL && strstr( argv[ i ], "<?xml" ) != argv[ i ] )
411                                 title = argv[ i ];
412                         producer = create_producer( profile, field, argv[ i ] );
413                         if ( producer != NULL )
414                         {
415                                 properties = MLT_PRODUCER_PROPERTIES( producer );
416                                 mlt_properties_inherit( properties, group );
417                         }
418                         else
419                         {
420                                 fprintf( stderr, "Failed to load \"%s\"\n", argv[ i ] );
421                         }
422                 }
423                 else
424                 {
425                         int backtrack = 0;
426                         if ( !strcmp( argv[ i ], "-serialise" ) ||
427                              !strcmp( argv[ i ], "-consumer" ) ||
428                              !strcmp( argv[ i ], "-profile" ) )
429                         {
430                                 i += 2;
431                                 backtrack = 1;
432                         }
433
434                         while ( argv[ i ] != NULL && strchr( argv[ i ], '=' ) )
435                         {
436                                 i ++;
437                                 backtrack = 1;
438                         }
439                         if ( backtrack )
440                                 i --;
441                 }
442         }
443
444         // Connect last producer to playlist
445         if ( producer != NULL && !mlt_producer_is_cut( producer ) )
446                 mlt_playlist_append( playlist, producer );
447
448         // Track the last playlist too
449         track_service( field, playlist, ( mlt_destructor )mlt_playlist_close );
450
451         // We must have a playlist to connect
452         if ( !mlt_properties_get_int( MLT_PLAYLIST_PROPERTIES( playlist ), "_melt_first" ) || 
453                   mlt_producer_get_playtime( MLT_PLAYLIST_PRODUCER( playlist ) ) > 0 )
454                 mlt_multitrack_connect( multitrack, MLT_PLAYLIST_PRODUCER( playlist ), track );
455
456         mlt_producer prod = MLT_TRACTOR_PRODUCER( tractor );
457         mlt_producer_optimise( prod );
458         mlt_properties props = MLT_TRACTOR_PROPERTIES( tractor );
459         mlt_properties_set_data( props, "group", group, 0, ( mlt_destructor )mlt_properties_close, NULL );
460         mlt_properties_set_position( props, "length", mlt_producer_get_out( MLT_MULTITRACK_PRODUCER( multitrack ) ) + 1 );
461         mlt_producer_set_in_and_out( prod, 0, mlt_producer_get_out( MLT_MULTITRACK_PRODUCER( multitrack ) ) );
462         if ( title != NULL )
463                 mlt_properties_set( props, "title", strchr( title, '/' ) ? strrchr( title, '/' ) + 1 : title );
464
465         return prod;
466 }