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