]> git.sesse.net Git - mlt/blob - src/modules/melt/producer_melt.c
Fix XML and melt producers producer_avformat cache size.
[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         mlt_producer result = mlt_factory_producer( profile, NULL, 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_melt_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 ), "_melt_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 ( argv[ i + 1 ] == NULL && (
149                         !strcmp( argv[ i ], "-attach" ) ||
150                         !strcmp( argv[ i ], "-attach-cut" ) ||
151                         !strcmp( argv[ i ], "-attach-track" ) ||
152                         !strcmp( argv[ i ], "-attach-clip" ) ||
153                         !strcmp( argv[ i ], "-repeat" ) ||
154                         !strcmp( argv[ i ], "-split" ) ||
155                         !strcmp( argv[ i ], "-join" ) ||
156                         !strcmp( argv[ i ], "-mixer" ) ||
157                         !strcmp( argv[ i ], "-mix" ) ||
158                         !strcmp( argv[ i ], "-filter" ) ||
159                         !strcmp( argv[ i ], "-transition" ) ||
160                         !strcmp( argv[ i ], "-blank" ) ) )
161                 {
162                         fprintf( stderr, "Argument missing for %s.\n", argv[ i ] );
163                         break;
164                 }
165                 if ( !strcmp( argv[ i ], "-group" ) )
166                 {
167                         if ( mlt_properties_count( group ) != 0 )
168                         {
169                                 mlt_properties_close( group );
170                                 group = mlt_properties_new( );
171                         }
172                         if ( group != NULL )
173                                 properties = group;
174                 }
175                 else if ( !strcmp( argv[ i ], "-attach" ) || 
176                                   !strcmp( argv[ i ], "-attach-cut" ) ||
177                                   !strcmp( argv[ i ], "-attach-track" ) ||
178                                   !strcmp( argv[ i ], "-attach-clip" ) )
179                 {
180                         int type = !strcmp( argv[ i ], "-attach" ) ? 0 : 
181                                            !strcmp( argv[ i ], "-attach-cut" ) ? 1 : 
182                                            !strcmp( argv[ i ], "-attach-track" ) ? 2 : 3;
183                         mlt_filter filter = create_attach( profile, field, argv[ ++ i ], track );
184                         if ( producer != NULL && !mlt_producer_is_cut( producer ) )
185                         {
186                                 mlt_playlist_clip_info info;
187                                 mlt_playlist_append( playlist, producer );
188                                 mlt_playlist_get_clip_info( playlist, &info, mlt_playlist_count( playlist ) - 1 );
189                                 producer = info.cut;
190                         }
191
192                         if ( type == 1 || type == 2 )
193                         {
194                                 mlt_playlist_clip_info info;
195                                 mlt_playlist_get_clip_info( playlist, &info, mlt_playlist_count( playlist ) - 1 );
196                                 producer = info.cut;
197                         }
198
199                         if ( filter != NULL && mlt_playlist_count( playlist ) > 0 )
200                         {
201                                 if ( type == 0 )
202                                         mlt_service_attach( ( mlt_service )properties, filter );
203                                 else if ( type == 1 )
204                                         mlt_service_attach( ( mlt_service )producer, filter );
205                                 else if ( type == 2 )
206                                         mlt_service_attach( ( mlt_service )playlist, filter );
207                                 else if ( type == 3 )
208                                         mlt_service_attach( ( mlt_service )mlt_producer_cut_parent( producer ), filter );
209
210                                 properties = MLT_FILTER_PROPERTIES( filter );
211                                 mlt_properties_inherit( properties, group );
212                         }
213                         else if ( filter != NULL )
214                         {
215                                 mlt_service_attach( ( mlt_service )playlist, filter );
216                                 properties = MLT_FILTER_PROPERTIES( filter );
217                                 mlt_properties_inherit( properties, group );
218                         }
219                 }
220                 else if ( !strcmp( argv[ i ], "-repeat" ) )
221                 {
222                         int repeat = atoi( argv[ ++ i ] );
223                         if ( producer != NULL && !mlt_producer_is_cut( producer ) )
224                                 mlt_playlist_append( playlist, producer );
225                         producer = NULL;
226                         if ( mlt_playlist_count( playlist ) > 0 )
227                         {
228                                 mlt_playlist_clip_info info;
229                                 mlt_playlist_repeat_clip( playlist, mlt_playlist_count( playlist ) - 1, repeat );
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 ], "-split" ) )
236                 {
237                         int split = atoi( argv[ ++ i ] );
238                         if ( producer != NULL && !mlt_producer_is_cut( producer ) )
239                                 mlt_playlist_append( playlist, producer );
240                         producer = NULL;
241                         if ( mlt_playlist_count( playlist ) > 0 )
242                         {
243                                 mlt_playlist_clip_info info;
244                                 mlt_playlist_get_clip_info( playlist, &info, mlt_playlist_count( playlist ) - 1 );
245                                 split = split < 0 ? info.frame_out + split : split;
246                                 mlt_playlist_split( playlist, mlt_playlist_count( playlist ) - 1, split );
247                                 mlt_playlist_get_clip_info( playlist, &info, mlt_playlist_count( playlist ) - 1 );
248                                 producer = info.cut;
249                                 properties = MLT_PRODUCER_PROPERTIES( producer );
250                         }
251                 }
252                 else if ( !strcmp( argv[ i ], "-swap" ) )
253                 {
254                         if ( producer != NULL && !mlt_producer_is_cut( producer ) )
255                                 mlt_playlist_append( playlist, producer );
256                         producer = NULL;
257                         if ( mlt_playlist_count( playlist ) >= 2 )
258                         {
259                                 mlt_playlist_clip_info info;
260                                 mlt_playlist_move( playlist, mlt_playlist_count( playlist ) - 2, mlt_playlist_count( playlist ) - 1 );
261                                 mlt_playlist_get_clip_info( playlist, &info, mlt_playlist_count( playlist ) - 1 );
262                                 producer = info.cut;
263                                 properties = MLT_PRODUCER_PROPERTIES( producer );
264                         }
265                 }
266                 else if ( !strcmp( argv[ i ], "-join" ) )
267                 {
268                         int clips = atoi( argv[ ++ i ] );
269                         if ( producer != NULL && !mlt_producer_is_cut( producer ) )
270                                 mlt_playlist_append( playlist, producer );
271                         producer = NULL;
272                         if ( mlt_playlist_count( playlist ) > 0 )
273                         {
274                                 mlt_playlist_clip_info info;
275                                 int clip = clips <= 0 ? 0 : mlt_playlist_count( playlist ) - clips - 1;
276                                 if ( clip < 0 ) clip = 0;
277                                 if ( clip >= mlt_playlist_count( playlist ) ) clip = mlt_playlist_count( playlist ) - 2;
278                                 if ( clips < 0 ) clips =  mlt_playlist_count( playlist ) - 1;
279                                 mlt_playlist_join( playlist, clip, clips, 0 );
280                                 mlt_playlist_get_clip_info( playlist, &info, mlt_playlist_count( playlist ) - 1 );
281                                 producer = info.cut;
282                                 properties = MLT_PRODUCER_PROPERTIES( producer );
283                         }
284                 }
285                 else if ( !strcmp( argv[ i ], "-remove" ) )
286                 {
287                         if ( producer != NULL && !mlt_producer_is_cut( producer ) )
288                                 mlt_playlist_append( playlist, producer );
289                         producer = NULL;
290                         if ( mlt_playlist_count( playlist ) > 0 )
291                         {
292                                 mlt_playlist_clip_info info;
293                                 mlt_playlist_remove( playlist, mlt_playlist_count( playlist ) - 1 );
294                                 mlt_playlist_get_clip_info( playlist, &info, mlt_playlist_count( playlist ) - 1 );
295                                 producer = info.cut;
296                                 properties = MLT_PRODUCER_PROPERTIES( producer );
297                         }
298                 }
299                 else if ( !strcmp( argv[ i ], "-mix" ) )
300                 {
301                         int length = atoi( argv[ ++ i ] );
302                         if ( producer != NULL && !mlt_producer_is_cut( producer ) )
303                                 mlt_playlist_append( playlist, producer );
304                         producer = NULL;
305                         if ( mlt_playlist_count( playlist ) >= 2 )
306                         {
307                                 if ( mlt_playlist_mix( playlist, mlt_playlist_count( playlist ) - 2, length, NULL ) == 0 )
308                                 {
309                                         mlt_playlist_clip_info info;
310                                         mlt_playlist_get_clip_info( playlist, &info, mlt_playlist_count( playlist ) - 1 );
311                                         if ( mlt_properties_get_data( ( mlt_properties )info.producer, "mlt_mix", NULL ) == NULL )
312                                                 mlt_playlist_get_clip_info( playlist, &info, mlt_playlist_count( playlist ) - 2 );
313                                         mix = ( mlt_tractor )mlt_properties_get_data( ( mlt_properties )info.producer, "mlt_mix", NULL );
314                                         properties = NULL;
315                                 }
316                                 else
317                                 {
318                                         fprintf( stderr, "Mix failed?\n" );
319                                 }
320                         }
321                         else
322                         {
323                                 fprintf( stderr, "Invalid position for a mix...\n" );
324                         }
325                 }
326                 else if ( !strcmp( argv[ i ], "-mixer" ) )
327                 {
328                         if ( mix != NULL )
329                         {
330                                 char *id = strdup( argv[ ++ i ] );
331                                 char *arg = strchr( id, ':' );
332                                 mlt_field field = mlt_tractor_field( mix );
333                                 mlt_transition transition = NULL;
334                                 if ( arg != NULL )
335                                         *arg ++ = '\0';
336                                 transition = mlt_factory_transition( profile, id, arg );
337                                 if ( transition != NULL )
338                                 {
339                                         properties = MLT_TRANSITION_PROPERTIES( transition );
340                                         mlt_properties_inherit( properties, group );
341                                         mlt_field_plant_transition( field, transition, 0, 1 );
342                                         mlt_properties_set_position( properties, "in", 0 );
343                                         mlt_properties_set_position( properties, "out", mlt_producer_get_out( ( mlt_producer )mix ) );
344                                         mlt_transition_close( transition );
345                                 }
346                                 free( id );
347                         }
348                         else
349                         {
350                                 fprintf( stderr, "Invalid mixer...\n" );
351                         }
352                 }
353                 else if ( !strcmp( argv[ i ], "-filter" ) )
354                 {
355                         mlt_filter filter = create_filter( profile, field, argv[ ++ i ], track );
356                         if ( filter != NULL )
357                         {
358                                 properties = MLT_FILTER_PROPERTIES( filter );
359                                 mlt_properties_inherit( properties, group );
360                         }
361                 }
362                 else if ( !strcmp( argv[ i ], "-transition" ) )
363                 {
364                         mlt_transition transition = create_transition( profile, field, argv[ ++ i ], track - 1 );
365                         if ( transition != NULL )
366                         {
367                                 properties = MLT_TRANSITION_PROPERTIES( transition );
368                                 mlt_properties_inherit( properties, group );
369                         }
370                 }
371                 else if ( !strcmp( argv[ i ], "-blank" ) )
372                 {
373                         if ( producer != NULL && !mlt_producer_is_cut( producer ) )
374                                 mlt_playlist_append( playlist, producer );
375                         producer = NULL;
376                         mlt_playlist_blank( playlist, atof( argv[ ++ i ] ) );
377                 }
378                 else if ( !strcmp( argv[ i ], "-track" ) ||
379                                   !strcmp( argv[ i ], "-null-track" ) ||
380                                   !strcmp( argv[ i ], "-video-track" ) ||
381                                   !strcmp( argv[ i ], "-audio-track" ) ||
382                                   !strcmp( argv[ i ], "-hide-track" ) ||
383                                   !strcmp( argv[ i ], "-hide-video" ) ||
384                                   !strcmp( argv[ i ], "-hide-audio" ) )
385                 {
386                         if ( producer != NULL && !mlt_producer_is_cut( producer ) )
387                                 mlt_playlist_append( playlist, producer );
388                         producer = NULL;
389                         if ( !mlt_properties_get_int( MLT_PLAYLIST_PROPERTIES( playlist ), "_melt_first" ) || 
390                                   mlt_producer_get_playtime( MLT_PLAYLIST_PRODUCER( playlist ) ) > 0 )
391                         {
392                                 mlt_multitrack_connect( multitrack, MLT_PLAYLIST_PRODUCER( playlist ), track ++ );
393                                 track_service( field, playlist, ( mlt_destructor )mlt_playlist_close );
394                                 playlist = mlt_playlist_init( );
395                         }
396                         if ( playlist != NULL )
397                         {
398                                 properties = MLT_PLAYLIST_PROPERTIES( playlist );
399                                 if ( !strcmp( argv[ i ], "-null-track" ) || !strcmp( argv[ i ], "-hide-track" ) )
400                                         mlt_properties_set_int( properties, "hide", 3 );
401                                 else if ( !strcmp( argv[ i ], "-audio-track" ) || !strcmp( argv[ i ], "-hide-video" ) )
402                                         mlt_properties_set_int( properties, "hide", 1 );
403                                 else if ( !strcmp( argv[ i ], "-video-track" ) || !strcmp( argv[ i ], "-hide-audio" ) )
404                                         mlt_properties_set_int( properties, "hide", 2 );
405                         }
406                 }
407                 else if ( strchr( argv[ i ], '=' ) && strstr( argv[ i ], "<?xml" ) != argv[ i ] &&
408                         // Prevent interpreting URL with parameters as a property.
409                         // This does not support property names containing a colon.
410                         ( !strchr( argv[ i ], ':' ) || strchr( argv[ i ], ':' ) > strchr( argv[ i ], '=' ) ) )
411                 {
412                         mlt_properties_parse( properties, argv[ i ] );
413                 }
414                 else if ( argv[ i ][ 0 ] != '-' )
415                 {
416                         if ( producer != NULL && !mlt_producer_is_cut( producer ) )
417                                 mlt_playlist_append( playlist, producer );
418                         if ( title == NULL && strstr( argv[ i ], "<?xml" ) != argv[ i ] )
419                                 title = argv[ i ];
420                         producer = create_producer( profile, field, argv[ i ] );
421                         if ( producer != NULL )
422                         {
423                                 properties = MLT_PRODUCER_PROPERTIES( producer );
424                                 mlt_properties_inherit( properties, group );
425                         }
426                         else
427                         {
428                                 fprintf( stderr, "Failed to load \"%s\"\n", argv[ i ] );
429                         }
430                 }
431                 else
432                 {
433                         int backtrack = 0;
434                         if ( !strcmp( argv[ i ], "-serialise" ) ||
435                              !strcmp( argv[ i ], "-consumer" ) ||
436                              !strcmp( argv[ i ], "-profile" ) )
437                         {
438                                 i += 2;
439                                 backtrack = 1;
440                         }
441
442                         while ( argv[ i ] != NULL && strchr( argv[ i ], '=' ) )
443                         {
444                                 i ++;
445                                 backtrack = 1;
446                         }
447                         if ( backtrack )
448                                 i --;
449                 }
450         }
451
452         // Set the size of the producer_avformat cache to the number of tracks.
453         if ( track > mlt_service_cache_get_size( MLT_PLAYLIST_SERVICE( playlist ), "producer_avformat" ) )
454                 mlt_service_cache_set_size( MLT_PLAYLIST_SERVICE( playlist ), "producer_avformat", track + 2 );
455
456         // Connect last producer to playlist
457         if ( producer != NULL && !mlt_producer_is_cut( producer ) )
458                 mlt_playlist_append( playlist, producer );
459
460         // Track the last playlist too
461         track_service( field, playlist, ( mlt_destructor )mlt_playlist_close );
462
463         // We must have a playlist to connect
464         if ( !mlt_properties_get_int( MLT_PLAYLIST_PROPERTIES( playlist ), "_melt_first" ) || 
465                   mlt_producer_get_playtime( MLT_PLAYLIST_PRODUCER( playlist ) ) > 0 )
466                 mlt_multitrack_connect( multitrack, MLT_PLAYLIST_PRODUCER( playlist ), track );
467
468         mlt_producer prod = MLT_TRACTOR_PRODUCER( tractor );
469         mlt_producer_optimise( prod );
470         mlt_properties props = MLT_TRACTOR_PROPERTIES( tractor );
471         mlt_properties_set_data( props, "group", group, 0, ( mlt_destructor )mlt_properties_close, NULL );
472         mlt_properties_set_position( props, "length", mlt_producer_get_out( MLT_MULTITRACK_PRODUCER( multitrack ) ) + 1 );
473         mlt_producer_set_in_and_out( prod, 0, mlt_producer_get_out( MLT_MULTITRACK_PRODUCER( multitrack ) ) );
474         if ( title != NULL )
475                 mlt_properties_set( props, "title", strchr( title, '/' ) ? strrchr( title, '/' ) + 1 : title );
476
477         return prod;
478 }