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