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