composite aspect ratio fix (again ;-)), added fill compositing test case, filter...
[melted] / src / framework / mlt_properties.c
1 /*
2 * mlt_properties.c -- base properties class
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 "config.h"
22 #include "mlt_properties.h"
23 #include "mlt_property.h"
24
25 #include <stdio.h>
26 #include <stdlib.h>
27 #include <string.h>
28
29 /* ---------------- // Private Implementation // ---------------- */
30
31 /** Private implementation of the property list.
32 */
33
34 typedef struct
35 {
36 char **name;
37 mlt_property *value;
38 int count;
39 int size;
40 }
41 property_list;
42
43 /** Basic implementation.
44 */
45
46 int mlt_properties_init( mlt_properties this, void *child )
47 {
48 // NULL all methods
49 memset( this, 0, sizeof( struct mlt_properties_s ) );
50
51 // Assign the child of the object
52 this->child = child;
53
54 // Allocate the private structure
55 this->private = calloc( sizeof( property_list ), 1 );
56
57 return this->private == NULL;
58 }
59
60 /** Constructor for stand alone object.
61 */
62
63 mlt_properties mlt_properties_new( )
64 {
65 // Construct a standalone properties object
66 mlt_properties this = calloc( sizeof( struct mlt_properties_s ), 1 );
67
68 // Initialise this
69 mlt_properties_init( this, NULL );
70
71 // Return the pointer
72 return this;
73 }
74
75 /** Special case - when a container (such as fezzik) is protecting another
76 producer, we need to ensure that properties are passed through to the
77 real producer.
78 */
79
80 static void mlt_properties_do_mirror( mlt_properties this, char *name )
81 {
82 if ( strcmp( name, "in" ) && strcmp( name, "out" ) )
83 {
84 mlt_properties mirror = mlt_properties_get_data( this, "mlt_mirror", NULL );
85 if ( mirror != NULL )
86 {
87 char *value = mlt_properties_get( this, name );
88 if ( value != NULL )
89 mlt_properties_set( mirror, name, value );
90 }
91 }
92 }
93
94 /** Allow the specification of a mirror.
95 */
96
97 void mlt_properties_mirror( mlt_properties this, mlt_properties that )
98 {
99 mlt_properties_set_data( this, "mlt_mirror", that, 0, NULL, NULL );
100 }
101
102 /** Inherit all serialisable properties from that into this.
103 */
104
105 int mlt_properties_inherit( mlt_properties this, mlt_properties that )
106 {
107 int count = mlt_properties_count( that );
108 int i = 0;
109 for ( i = 0; i < count; i ++ )
110 {
111 char *value = mlt_properties_get_value( that, i );
112 if ( value != NULL )
113 {
114 char *name = mlt_properties_get_name( that, i );
115 mlt_properties_set( this, name, value );
116 }
117 }
118 return 0;
119 }
120
121 /** Pass all properties from 'that' that match the prefix to 'this' (excluding the prefix).
122 */
123
124 int mlt_properties_pass( mlt_properties this, mlt_properties that, char *prefix )
125 {
126 int count = mlt_properties_count( that );
127 int length = strlen( prefix );
128 int i = 0;
129 for ( i = 0; i < count; i ++ )
130 {
131 char *name = mlt_properties_get_name( that, i );
132 if ( !strncmp( name, prefix, length ) )
133 {
134 char *value = mlt_properties_get_value( that, i );
135 if ( value != NULL )
136 mlt_properties_set( this, name + length, value );
137 }
138 }
139 return 0;
140 }
141
142 /** Locate a property by name
143 */
144
145 static mlt_property mlt_properties_find( mlt_properties this, char *name )
146 {
147 mlt_property value = NULL;
148 property_list *list = this->private;
149 int i = 0;
150
151 // Locate the item
152 for ( i = 0; value == NULL && i < list->count; i ++ )
153 if ( !strcmp( list->name[ i ], name ) )
154 value = list->value[ i ];
155
156 return value;
157 }
158
159 /** Add a new property.
160 */
161
162 static mlt_property mlt_properties_add( mlt_properties this, char *name )
163 {
164 property_list *list = this->private;
165
166 // Check that we have space and resize if necessary
167 if ( list->count == list->size )
168 {
169 list->size += 50;
170 list->name = realloc( list->name, list->size * sizeof( char * ) );
171 list->value = realloc( list->value, list->size * sizeof( mlt_property ) );
172 }
173
174 // Assign name/value pair
175 list->name[ list->count ] = strdup( name );
176 list->value[ list->count ] = mlt_property_init( );
177
178 // Return and increment count accordingly
179 return list->value[ list->count ++ ];
180 }
181
182 /** Fetch a property by name - this includes add if not found.
183 */
184
185 static mlt_property mlt_properties_fetch( mlt_properties this, char *name )
186 {
187 // Try to find an existing property first
188 mlt_property property = mlt_properties_find( this, name );
189
190 // If it wasn't found, create one
191 if ( property == NULL )
192 property = mlt_properties_add( this, name );
193
194 // Return the property
195 return property;
196 }
197
198 /** Set the property.
199 */
200
201 int mlt_properties_set( mlt_properties this, char *name, char *value )
202 {
203 int error = 1;
204
205 // Fetch the property to work with
206 mlt_property property = mlt_properties_fetch( this, name );
207
208 // Set it if not NULL
209 if ( property != NULL )
210 {
211 error = mlt_property_set_string( property, value );
212 mlt_properties_do_mirror( this, name );
213 }
214
215 return error;
216 }
217
218 /** Get a string value by name.
219 */
220
221 char *mlt_properties_get( mlt_properties this, char *name )
222 {
223 mlt_property value = mlt_properties_find( this, name );
224 return value == NULL ? NULL : mlt_property_get_string( value );
225 }
226
227 /** Get a name by index.
228 */
229
230 char *mlt_properties_get_name( mlt_properties this, int index )
231 {
232 property_list *list = this->private;
233 if ( index >= 0 && index < list->count )
234 return list->name[ index ];
235 return NULL;
236 }
237
238 /** Get a string value by index.
239 */
240
241 char *mlt_properties_get_value( mlt_properties this, int index )
242 {
243 property_list *list = this->private;
244 if ( index >= 0 && index < list->count )
245 return mlt_property_get_string( list->value[ index ] );
246 return NULL;
247 }
248
249 /** Get a data value by index.
250 */
251
252 void *mlt_properties_get_data_at( mlt_properties this, int index, int *size )
253 {
254 property_list *list = this->private;
255 if ( index >= 0 && index < list->count )
256 return mlt_property_get_data( list->value[ index ], size );
257 return NULL;
258 }
259
260 /** Return the number of items in the list.
261 */
262
263 int mlt_properties_count( mlt_properties this )
264 {
265 property_list *list = this->private;
266 return list->count;
267 }
268
269 /** Set a value by parsing a name=value string
270 */
271
272 int mlt_properties_parse( mlt_properties this, char *namevalue )
273 {
274 char *name = strdup( namevalue );
275 char *value = strdup( namevalue );
276 int error = 0;
277
278 if ( strchr( name, '=' ) )
279 {
280 *( strchr( name, '=' ) ) = '\0';
281 strcpy( value, strchr( value, '=' ) + 1 );
282 }
283 else
284 {
285 strcpy( value, "" );
286 }
287
288 if ( strlen( value ) > 1 && value[ 0 ] == '\"' )
289 {
290 strcpy( value, value + 1 );
291 if ( value[ strlen( value ) - 1 ] == '\"' )
292 value[ strlen( value ) - 1 ] = '\0';
293 }
294
295 error = mlt_properties_set( this, name, value );
296
297 free( name );
298 free( value );
299
300 return error;
301 }
302
303 /** Get a value associated to the name.
304 */
305
306 int mlt_properties_get_int( mlt_properties this, char *name )
307 {
308 mlt_property value = mlt_properties_find( this, name );
309 return value == NULL ? 0 : mlt_property_get_int( value );
310 }
311
312 /** Set a value associated to the name.
313 */
314
315 int mlt_properties_set_int( mlt_properties this, char *name, int value )
316 {
317 int error = 1;
318
319 // Fetch the property to work with
320 mlt_property property = mlt_properties_fetch( this, name );
321
322 // Set it if not NULL
323 if ( property != NULL )
324 {
325 error = mlt_property_set_int( property, value );
326 mlt_properties_do_mirror( this, name );
327 }
328
329 return error;
330 }
331
332 /** Get a value associated to the name.
333 */
334
335 double mlt_properties_get_double( mlt_properties this, char *name )
336 {
337 mlt_property value = mlt_properties_find( this, name );
338 return value == NULL ? 0 : mlt_property_get_double( value );
339 }
340
341 /** Set a value associated to the name.
342 */
343
344 int mlt_properties_set_double( mlt_properties this, char *name, double value )
345 {
346 int error = 1;
347
348 // Fetch the property to work with
349 mlt_property property = mlt_properties_fetch( this, name );
350
351 // Set it if not NULL
352 if ( property != NULL )
353 {
354 error = mlt_property_set_double( property, value );
355 mlt_properties_do_mirror( this, name );
356 }
357
358 return error;
359 }
360
361 /** Get a value associated to the name.
362 */
363
364 mlt_position mlt_properties_get_position( mlt_properties this, char *name )
365 {
366 mlt_property value = mlt_properties_find( this, name );
367 return value == NULL ? 0 : mlt_property_get_position( value );
368 }
369
370 /** Set a value associated to the name.
371 */
372
373 int mlt_properties_set_position( mlt_properties this, char *name, mlt_position value )
374 {
375 int error = 1;
376
377 // Fetch the property to work with
378 mlt_property property = mlt_properties_fetch( this, name );
379
380 // Set it if not NULL
381 if ( property != NULL )
382 {
383 error = mlt_property_set_position( property, value );
384 mlt_properties_do_mirror( this, name );
385 }
386
387 return error;
388 }
389
390 /** Get a value associated to the name.
391 */
392
393 void *mlt_properties_get_data( mlt_properties this, char *name, int *length )
394 {
395 mlt_property value = mlt_properties_find( this, name );
396 return value == NULL ? NULL : mlt_property_get_data( value, length );
397 }
398
399 /** Set a value associated to the name.
400 */
401
402 int mlt_properties_set_data( mlt_properties this, char *name, void *value, int length, mlt_destructor destroy, mlt_serialiser serialise )
403 {
404 int error = 1;
405
406 // Fetch the property to work with
407 mlt_property property = mlt_properties_fetch( this, name );
408
409 // Set it if not NULL
410 if ( property != NULL )
411 error = mlt_property_set_data( property, value, length, destroy, serialise );
412
413 return error;
414 }
415
416 /** Rename a property.
417 */
418
419 int mlt_properties_rename( mlt_properties this, char *source, char *dest )
420 {
421 mlt_property value = mlt_properties_find( this, dest );
422
423 if ( value == NULL )
424 {
425 property_list *list = this->private;
426 int i = 0;
427
428 // Locate the item
429 for ( i = 0; i < list->count; i ++ )
430 {
431 if ( !strcmp( list->name[ i ], source ) )
432 {
433 free( list->name[ i ] );
434 list->name[ i ] = strdup( dest );
435 break;
436 }
437 }
438 }
439
440 return value != NULL;
441 }
442
443 /** Dump the properties.
444 */
445
446 void mlt_properties_dump( mlt_properties this, FILE *output )
447 {
448 property_list *list = this->private;
449 int i = 0;
450 for ( i = 0; i < list->count; i ++ )
451 fprintf( stderr, "%s = %s\n", list->name[ i ], mlt_properties_get( this, list->name[ i ] ) );
452 }
453
454 /** Close the list.
455 */
456
457 void mlt_properties_close( mlt_properties this )
458 {
459 property_list *list = this->private;
460 int index = 0;
461
462 // Clean up names and values
463 for ( index = list->count - 1; index >= 0; index -- )
464 {
465 free( list->name[ index ] );
466 mlt_property_close( list->value[ index ] );
467 }
468
469 // Clear up the list
470 free( list->name );
471 free( list->value );
472 free( list );
473
474 // Free this now if this has no child
475 if ( this->child == NULL )
476 free( this );
477 }
478