fezzik now accepts service:resource and strips \'avformat:\' before fallback avformat...
[melted] / src / modules / fezzik / producer_fezzik.c
1 /*
2 * producer_fezzik.c -- a normalising filter
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_fezzik.h"
22
23 #include <stdio.h>
24 #include <stdlib.h>
25 #include <string.h>
26
27 #include <framework/mlt.h>
28
29 static void track_service( mlt_tractor tractor, void *service, mlt_destructor destructor )
30 {
31 mlt_properties properties = mlt_tractor_properties( tractor );
32 int registered = mlt_properties_get_int( properties, "_registered" );
33 char *key = mlt_properties_get( properties, "_registered" );
34 char *real = malloc( strlen( key ) + 2 );
35 sprintf( real, "_%s", key );
36 mlt_properties_set_data( properties, real, service, 0, destructor, NULL );
37 mlt_properties_set_int( properties, "_registered", ++ registered );
38 free( real );
39 }
40
41 static mlt_producer create_producer( char *file )
42 {
43 mlt_producer result = NULL;
44
45 // 1st Line preferences
46 if ( strstr( file, ".inigo" ) )
47 result = mlt_factory_producer( "inigo_file", file );
48 else if ( strstr( file, ".mpg" ) )
49 result = mlt_factory_producer( "mcmpeg", file );
50 else if ( strstr( file, ".mpeg" ) )
51 result = mlt_factory_producer( "mcmpeg", file );
52 else if ( strstr( file, ".dv" ) )
53 result = mlt_factory_producer( "mcdv", file );
54 else if ( strstr( file, ".dif" ) )
55 result = mlt_factory_producer( "mcdv", file );
56 else if ( strstr( file, ".jpg" ) )
57 result = mlt_factory_producer( "pixbuf", file );
58 else if ( strstr( file, ".JPG" ) )
59 result = mlt_factory_producer( "pixbuf", file );
60 else if ( strstr( file, ".jpeg" ) )
61 result = mlt_factory_producer( "pixbuf", file );
62 else if ( strstr( file, ".png" ) )
63 result = mlt_factory_producer( "pixbuf", file );
64 else if ( strstr( file, ".svg" ) )
65 result = mlt_factory_producer( "pixbuf", file );
66 else if ( strstr( file, ".txt" ) )
67 result = mlt_factory_producer( "pango", file );
68 else if ( strstr( file, ".westley" ) )
69 result = mlt_factory_producer( "westley", file );
70 else if ( strstr( file, ".ogg" ) )
71 result = mlt_factory_producer( "vorbis", file );
72
73 // 2nd Line fallbacks
74 if ( result == NULL )
75 {
76 if ( strstr( file, ".dv" ) )
77 result = mlt_factory_producer( "libdv", file );
78 else if ( strstr( file, ".dif" ) )
79 result = mlt_factory_producer( "libdv", file );
80 }
81
82 // 3rd line fallbacks
83 if ( result == NULL )
84 result = mlt_factory_producer( "avformat", file +
85 ( strncmp( file, "avformat:", 9 ) ? 0 : 9 ) );
86
87 // 4th - allow explicit construction
88 if ( result == NULL )
89 {
90 char *arg = strchr( file, ':' );
91 if ( arg )
92 {
93 arg[0] = 0;
94 arg++;
95 }
96 result = mlt_factory_producer( file, arg );
97 }
98
99 return result;
100 }
101
102 static mlt_service create_filter( mlt_tractor tractor, mlt_service last, char *effect )
103 {
104 char *id = strdup( effect );
105 char *arg = strchr( id, ':' );
106 if ( arg != NULL )
107 *arg ++ = '\0';
108 mlt_filter filter = mlt_factory_filter( id, arg );
109 if ( filter != NULL )
110 {
111 mlt_filter_connect( filter, last, 0 );
112 track_service( tractor, filter, ( mlt_destructor )mlt_filter_close );
113 last = mlt_filter_service( filter );
114 }
115 free( id );
116 return last;
117 }
118
119 mlt_producer producer_fezzik_init( char *arg )
120 {
121 // Create the producer that the tractor will contain
122 mlt_producer producer = NULL;
123 if ( arg != NULL )
124 producer = create_producer( arg );
125
126 // Build the tractor if we have a producer and it isn't already westley'd :-)
127 if ( producer != NULL && mlt_properties_get( mlt_producer_properties( producer ), "westley" ) == NULL )
128 {
129 // Construct the tractor
130 mlt_tractor tractor = mlt_tractor_init( );
131
132 // Sanity check
133 if ( tractor != NULL )
134 {
135 // Extract the tractor properties
136 mlt_properties properties = mlt_tractor_properties( tractor );
137
138 // Our producer will be the last service
139 mlt_service last = mlt_producer_service( producer );
140
141 // Set the registered count
142 mlt_properties_set_int( properties, "_registered", 0 );
143
144 // Register our producer for seeking in the tractor
145 mlt_properties_set_data( properties, "producer", producer, 0, ( mlt_destructor )mlt_producer_close, NULL );
146
147 // Now attach normalising filters
148 last = create_filter( tractor, last, "deinterlace" );
149 last = create_filter( tractor, last, "rescale" );
150 last = create_filter( tractor, last, "resize" );
151 last = create_filter( tractor, last, "resample" );
152
153 // Connect the tractor to the last
154 mlt_tractor_connect( tractor, last );
155
156 // Finally, inherit properties from producer
157 mlt_properties_inherit( properties, mlt_producer_properties( producer ) );
158
159 // Now make sure we don't lose our inherited identity
160 mlt_properties_set_int( properties, "_mlt_service_hidden", 1 );
161
162 // This is a temporary hack to ensure that westley doesn't dig too deep
163 // and fezzik doesn't overdo it with throwing rocks...
164 mlt_properties_set( properties, "westley", "was here" );
165
166 // We need to ensure that all further properties are mirrored in the producer
167 mlt_properties_mirror( properties, mlt_producer_properties( producer ) );
168
169 // Now, we return the producer of the tractor
170 producer = mlt_tractor_producer( tractor );
171 }
172 }
173
174 // Return the tractor's producer
175 return producer;
176 }