]> git.sesse.net Git - mlt/blob - src/modules/inigo/producer_inigo.c
ed20ea3b1a2e3d340e0a01ec21464110bde5aa45
[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 static mlt_producer parse_inigo( 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 mlt_producer create_producer( char *file )
62 {
63         mlt_producer result = NULL;
64
65         // 1st Line preferences
66         if ( strstr( file, ".inigo" ) )
67                 result = parse_inigo( file );
68         else if ( strstr( file, ".mpg" ) )
69                 result = mlt_factory_producer( "mcmpeg", file );
70         else if ( strstr( file, ".mpeg" ) )
71                 result = mlt_factory_producer( "mcmpeg", file );
72         else if ( strstr( file, ".dv" ) )
73                 result = mlt_factory_producer( "mcdv", file );
74         else if ( strstr( file, ".dif" ) )
75                 result = mlt_factory_producer( "mcdv", file );
76         else if ( strstr( file, ".jpg" ) )
77                 result = mlt_factory_producer( "pixbuf", file );
78         else if ( strstr( file, ".JPG" ) )
79                 result = mlt_factory_producer( "pixbuf", file );
80         else if ( strstr( file, ".jpeg" ) )
81                 result = mlt_factory_producer( "pixbuf", file );
82         else if ( strstr( file, ".png" ) )
83                 result = mlt_factory_producer( "pixbuf", file );
84         else if ( strstr( file, ".txt" ) )
85                 result = mlt_factory_producer( "pango", file );
86         else if ( strstr( file, ".westley" ) )
87                 result = mlt_factory_producer( "westley", file );
88
89         // 2nd Line fallbacks
90         if ( result == NULL && strstr( file, ".dv" ) )
91                 result = mlt_factory_producer( "libdv", file );
92         else if ( result == NULL && strstr( file, ".dif" ) )
93                 result = mlt_factory_producer( "libdv", file );
94
95         // 3rd line fallbacks 
96         if ( result == NULL )
97                 result = mlt_factory_producer( "avformat", file );
98
99         return result;
100 }
101
102 static void track_service( mlt_field field, void *service, mlt_destructor destructor )
103 {
104         mlt_properties properties = mlt_field_properties( field );
105         int registered = mlt_properties_get_int( properties, "registered" );
106         char *key = mlt_properties_get( properties, "registered" );
107         mlt_properties_set_data( properties, key, service, 0, destructor, NULL );
108         mlt_properties_set_int( properties, "registered", ++ registered );
109 }
110
111 static mlt_filter create_filter( mlt_field field, char *id, int track )
112 {
113         char *arg = strchr( id, ':' );
114         if ( arg != NULL )
115                 *arg ++ = '\0';
116         mlt_filter filter = mlt_factory_filter( id, arg );
117         if ( filter != NULL )
118         {
119                 mlt_field_plant_filter( field, filter, track );
120                 track_service( field, filter, ( mlt_destructor )mlt_filter_close );
121         }
122         return filter;
123 }
124
125 static mlt_transition create_transition( mlt_field field, char *id, int track )
126 {
127         char *arg = strchr( id, ':' );
128         if ( arg != NULL )
129                 *arg ++ = '\0';
130         mlt_transition transition = mlt_factory_transition( id, arg );
131         if ( transition != NULL )
132         {
133                 mlt_field_plant_transition( field, transition, track, track + 1 );
134                 track_service( field, transition, ( mlt_destructor )mlt_transition_close );
135         }
136         return transition;
137 }
138
139 mlt_producer producer_inigo_init( char **argv )
140 {
141         int i;
142         int track = 0;
143         mlt_producer producer = NULL;
144         mlt_playlist playlist = mlt_playlist_init( );
145         mlt_properties group = mlt_properties_new( );
146         mlt_properties properties = group;
147         mlt_field field = mlt_field_init( );
148         mlt_properties field_properties = mlt_field_properties( field );
149         mlt_multitrack multitrack = mlt_field_multitrack( field );
150
151         // We need to track the number of registered filters
152         mlt_properties_set_int( field_properties, "registered", 0 );
153
154         // Parse the arguments
155         for ( i = 0; argv[ i ] != NULL; i ++ )
156         {
157                 if ( !strcmp( argv[ i ], "-group" ) )
158                 {
159                         if ( mlt_properties_count( group ) != 0 )
160                         {
161                                 mlt_properties_close( group );
162                                 group = mlt_properties_new( );
163                         }
164                         if ( group != NULL )
165                                 properties = group;
166                 }
167                 else if ( !strcmp( argv[ i ], "-filter" ) )
168                 {
169                         mlt_filter filter = create_filter( field, argv[ ++ i ], track );
170                         if ( filter != NULL )
171                         {
172                                 properties = mlt_filter_properties( filter );
173                                 mlt_properties_inherit( properties, group );
174                         }
175                 }
176                 else if ( !strcmp( argv[ i ], "-transition" ) )
177                 {
178                         mlt_transition transition = create_transition( field, argv[ ++ i ], track );
179                         if ( transition != NULL )
180                         {
181                                 properties = mlt_transition_properties( transition );
182                                 mlt_properties_inherit( properties, group );
183                         }
184                 }
185                 else if ( !strcmp( argv[ i ], "-blank" ) )
186                 {
187                         if ( producer != NULL )
188                                 mlt_playlist_append( playlist, producer );
189                         producer = NULL;
190                         mlt_playlist_blank( playlist, atof( argv[ ++ i ] ) );
191                 }
192                 else if ( !strcmp( argv[ i ], "-track" ) )
193                 {
194                         if ( producer != NULL )
195                                 mlt_playlist_append( playlist, producer );
196                         producer = NULL;
197                         mlt_multitrack_connect( multitrack, mlt_playlist_producer( playlist ), track ++ );
198                         playlist = mlt_playlist_init( );
199                 }
200                 else if ( strstr( argv[ i ], "=" ) )
201                 {
202                         mlt_properties_parse( properties, argv[ i ] );
203                 }
204                 else if ( argv[ i ][ 0 ] != '-' )
205                 {
206                         if ( producer != NULL )
207                                 mlt_playlist_append( playlist, producer );
208                         producer = create_producer( argv[ i ] );
209                         if ( producer != NULL )
210                         {
211                                 properties = mlt_producer_properties( producer );
212                                 mlt_properties_inherit( properties, group );
213                         }
214                 }
215                 else
216                 {
217                         if ( !strcmp( argv[ i ], "-serialise" ) )
218                                 i += 2;
219                         else if ( !strcmp( argv[ i ], "-consumer" ) )
220                                 i += 2;
221
222                         while ( argv[ i ] != NULL && strchr( argv[ i ], '=' ) )
223                                 i ++;
224
225                         i --;
226                 }
227         }
228
229         // Connect producer to playlist
230         if ( producer != NULL )
231                 mlt_playlist_append( playlist, producer );
232
233         // We must have a producer at this point
234         if ( mlt_playlist_count( playlist ) > 0 )
235         {
236                 // Connect multitrack to producer
237                 mlt_multitrack_connect( multitrack, mlt_playlist_producer( playlist ), track );
238         }
239
240         mlt_tractor tractor = mlt_field_tractor( field );
241         mlt_producer prod = mlt_tractor_producer( tractor );
242         mlt_properties props = mlt_tractor_properties( tractor );
243         mlt_properties_set_data( props, "multitrack", multitrack, 0, NULL, NULL );
244         mlt_properties_set_data( props, "field", field, 0, NULL, NULL );
245         mlt_properties_set_data( props, "group", group, 0, NULL, NULL );
246         mlt_properties_set_position( props, "length", mlt_producer_get_out( mlt_multitrack_producer( multitrack ) ) + 1 );
247         mlt_producer_set_in_and_out( prod, 0, mlt_producer_get_out( mlt_multitrack_producer( multitrack ) ) );
248         mlt_properties_set_double( props, "fps", mlt_producer_get_fps( mlt_multitrack_producer( multitrack ) ) );
249
250         return mlt_tractor_producer( tractor );
251 }
252