]> git.sesse.net Git - mlt/blob - src/modules/inigo/producer_inigo.c
35e84ad1364553c972873f447180000d15d604aa
[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_filter( 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         {
88                 mlt_field_plant_filter( field, filter, track );
89                 track_service( field, filter, ( mlt_destructor )mlt_filter_close );
90         }
91         return filter;
92 }
93
94 static mlt_transition create_transition( mlt_field field, char *id, int track )
95 {
96         char *arg = strchr( id, ':' );
97         if ( arg != NULL )
98                 *arg ++ = '\0';
99         mlt_transition transition = mlt_factory_transition( id, arg );
100         if ( transition != NULL )
101         {
102                 mlt_field_plant_transition( field, transition, track, track + 1 );
103                 track_service( field, transition, ( mlt_destructor )mlt_transition_close );
104         }
105         return transition;
106 }
107
108 mlt_producer producer_inigo_init( char **argv )
109 {
110         int i;
111         int track = 0;
112         mlt_producer producer = NULL;
113         mlt_playlist playlist = mlt_playlist_init( );
114         mlt_properties group = mlt_properties_new( );
115         mlt_properties properties = group;
116         mlt_field field = mlt_field_init( );
117         mlt_properties field_properties = mlt_field_properties( field );
118         mlt_multitrack multitrack = mlt_field_multitrack( field );
119
120         // We need to track the number of registered filters
121         mlt_properties_set_int( field_properties, "registered", 0 );
122
123         // Parse the arguments
124         for ( i = 0; argv[ i ] != NULL; i ++ )
125         {
126                 if ( !strcmp( argv[ i ], "-group" ) )
127                 {
128                         if ( mlt_properties_count( group ) != 0 )
129                         {
130                                 mlt_properties_close( group );
131                                 group = mlt_properties_new( );
132                         }
133                         if ( group != NULL )
134                                 properties = group;
135                 }
136                 else if ( !strcmp( argv[ i ], "-filter" ) )
137                 {
138                         mlt_filter filter = create_filter( field, argv[ ++ i ], track );
139                         if ( filter != NULL )
140                         {
141                                 properties = mlt_filter_properties( filter );
142                                 mlt_properties_inherit( properties, group );
143                         }
144                 }
145                 else if ( !strcmp( argv[ i ], "-transition" ) )
146                 {
147                         mlt_transition transition = create_transition( field, argv[ ++ i ], track - 1 );
148                         if ( transition != NULL )
149                         {
150                                 properties = mlt_transition_properties( transition );
151                                 mlt_properties_inherit( properties, group );
152                         }
153                 }
154                 else if ( !strcmp( argv[ i ], "-blank" ) )
155                 {
156                         if ( producer != NULL )
157                                 mlt_playlist_append( playlist, producer );
158                         producer = NULL;
159                         mlt_playlist_blank( playlist, atof( argv[ ++ i ] ) );
160                 }
161                 else if ( !strcmp( argv[ i ], "-track" ) ||
162                                   !strcmp( argv[ i ], "-hide-track" ) ||
163                                   !strcmp( argv[ i ], "-hide-video" ) ||
164                                   !strcmp( argv[ i ], "-hide-audio" ) )
165                 {
166                         if ( producer != NULL )
167                                 mlt_playlist_append( playlist, producer );
168                         producer = NULL;
169                         mlt_multitrack_connect( multitrack, mlt_playlist_producer( playlist ), track ++ );
170                         track_service( field, playlist, ( mlt_destructor )mlt_playlist_close );
171                         playlist = mlt_playlist_init( );
172                         if ( playlist != NULL )
173                         {
174                                 properties = mlt_playlist_properties( playlist );
175                                 if ( !strcmp( argv[ i ], "-hide-track" ) )
176                                         mlt_properties_set_int( properties, "hide", 3 );
177                                 else if ( !strcmp( argv[ i ], "-hide-video" ) )
178                                         mlt_properties_set_int( properties, "hide", 1 );
179                                 else if ( !strcmp( argv[ i ], "-hide-audio" ) )
180                                         mlt_properties_set_int( properties, "hide", 2 );
181                         }
182                 }
183                 else if ( strchr( argv[ i ], '=' ) )
184                 {
185                         mlt_properties_parse( properties, argv[ i ] );
186                 }
187                 else if ( argv[ i ][ 0 ] != '-' )
188                 {
189                         if ( producer != NULL )
190                                 mlt_playlist_append( playlist, producer );
191                         producer = create_producer( field, argv[ i ] );
192                         if ( producer != NULL )
193                         {
194                                 properties = mlt_producer_properties( producer );
195                                 mlt_properties_inherit( properties, group );
196                         }
197                 }
198                 else
199                 {
200                         if ( !strcmp( argv[ i ], "-serialise" ) )
201                                 i += 2;
202                         else if ( !strcmp( argv[ i ], "-consumer" ) )
203                                 i += 2;
204
205                         while ( argv[ i ] != NULL && strchr( argv[ i ], '=' ) )
206                                 i ++;
207
208                         i --;
209                 }
210         }
211
212         // Connect last producer to playlist
213         if ( producer != NULL )
214                 mlt_playlist_append( playlist, producer );
215
216         // Track the last playlist too
217         track_service( field, playlist, ( mlt_destructor )mlt_playlist_close );
218
219         // We must have a playlist to connect
220         if ( mlt_playlist_count( playlist ) > 0 )
221                 mlt_multitrack_connect( multitrack, mlt_playlist_producer( playlist ), track );
222
223         mlt_tractor tractor = mlt_field_tractor( field );
224         mlt_producer prod = mlt_tractor_producer( tractor );
225         mlt_properties props = mlt_tractor_properties( tractor );
226         mlt_properties_set_data( props, "multitrack", multitrack, 0, ( mlt_destructor )mlt_multitrack_close, NULL );
227         mlt_properties_set_data( props, "field", field, 0, ( mlt_destructor )mlt_field_close, NULL );
228         mlt_properties_set_data( props, "group", group, 0, ( mlt_destructor )mlt_properties_close, NULL );
229         mlt_properties_set_position( props, "length", mlt_producer_get_out( mlt_multitrack_producer( multitrack ) ) + 1 );
230         mlt_producer_set_in_and_out( prod, 0, mlt_producer_get_out( mlt_multitrack_producer( multitrack ) ) );
231         mlt_properties_set_double( props, "fps", mlt_producer_get_fps( mlt_multitrack_producer( multitrack ) ) );
232
233         return mlt_tractor_producer( tractor );
234 }