Optimisations (part 0), pixel v percentage, reworked aspect ratio calcs, ante/post...
[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 while ( count -- )
109 {
110 char *value = mlt_properties_get_value( that, count );
111 if ( value != NULL )
112 {
113 char *name = mlt_properties_get_name( that, count );
114 mlt_properties_set( this, name, value );
115 }
116 }
117 return 0;
118 }
119
120 /** Locate a property by name
121 */
122
123 static mlt_property mlt_properties_find( mlt_properties this, char *name )
124 {
125 mlt_property value = NULL;
126 property_list *list = this->private;
127 int i = 0;
128
129 // Locate the item
130 for ( i = 0; value == NULL && i < list->count; i ++ )
131 if ( !strcmp( list->name[ i ], name ) )
132 value = list->value[ i ];
133
134 return value;
135 }
136
137 /** Add a new property.
138 */
139
140 static mlt_property mlt_properties_add( mlt_properties this, char *name )
141 {
142 property_list *list = this->private;
143
144 // Check that we have space and resize if necessary
145 if ( list->count == list->size )
146 {
147 list->size += 10;
148 list->name = realloc( list->name, list->size * sizeof( char * ) );
149 list->value = realloc( list->value, list->size * sizeof( mlt_property ) );
150 }
151
152 // Assign name/value pair
153 list->name[ list->count ] = strdup( name );
154 list->value[ list->count ] = mlt_property_init( );
155
156 // Return and increment count accordingly
157 return list->value[ list->count ++ ];
158 }
159
160 /** Fetch a property by name - this includes add if not found.
161 */
162
163 static mlt_property mlt_properties_fetch( mlt_properties this, char *name )
164 {
165 // Try to find an existing property first
166 mlt_property property = mlt_properties_find( this, name );
167
168 // If it wasn't found, create one
169 if ( property == NULL )
170 property = mlt_properties_add( this, name );
171
172 // Return the property
173 return property;
174 }
175
176 /** Set the property.
177 */
178
179 int mlt_properties_set( mlt_properties this, char *name, char *value )
180 {
181 int error = 1;
182
183 // Fetch the property to work with
184 mlt_property property = mlt_properties_fetch( this, name );
185
186 // Set it if not NULL
187 if ( property != NULL )
188 {
189 error = mlt_property_set_string( property, value );
190 mlt_properties_do_mirror( this, name );
191 }
192
193 return error;
194 }
195
196 /** Get a string value by name.
197 */
198
199 char *mlt_properties_get( mlt_properties this, char *name )
200 {
201 mlt_property value = mlt_properties_find( this, name );
202 return value == NULL ? NULL : mlt_property_get_string( value );
203 }
204
205 /** Get a name by index.
206 */
207
208 char *mlt_properties_get_name( mlt_properties this, int index )
209 {
210 property_list *list = this->private;
211 if ( index >= 0 && index < list->count )
212 return list->name[ index ];
213 return NULL;
214 }
215
216 /** Get a string value by index.
217 */
218
219 char *mlt_properties_get_value( mlt_properties this, int index )
220 {
221 property_list *list = this->private;
222 if ( index >= 0 && index < list->count )
223 return mlt_property_get_string( list->value[ index ] );
224 return NULL;
225 }
226
227 /** Return the number of items in the list.
228 */
229
230 int mlt_properties_count( mlt_properties this )
231 {
232 property_list *list = this->private;
233 return list->count;
234 }
235
236 /** Set a value by parsing a name=value string
237 */
238
239 int mlt_properties_parse( mlt_properties this, char *namevalue )
240 {
241 char *name = strdup( namevalue );
242 char *value = strdup( namevalue );
243 int error = 0;
244
245 if ( strchr( name, '=' ) )
246 {
247 *( strchr( name, '=' ) ) = '\0';
248 strcpy( value, strchr( value, '=' ) + 1 );
249 }
250 else
251 {
252 strcpy( value, "" );
253 }
254
255 if ( strlen( value ) > 1 && value[ 0 ] == '\"' )
256 {
257 strcpy( value, value + 1 );
258 if ( value[ strlen( value ) - 1 ] == '\"' )
259 value[ strlen( value ) - 1 ] = '\0';
260 }
261
262 error = mlt_properties_set( this, name, value );
263
264 free( name );
265 free( value );
266
267 return error;
268 }
269
270 /** Get a value associated to the name.
271 */
272
273 int mlt_properties_get_int( mlt_properties this, char *name )
274 {
275 mlt_property value = mlt_properties_find( this, name );
276 return value == NULL ? 0 : mlt_property_get_int( value );
277 }
278
279 /** Set a value associated to the name.
280 */
281
282 int mlt_properties_set_int( mlt_properties this, char *name, int value )
283 {
284 int error = 1;
285
286 // Fetch the property to work with
287 mlt_property property = mlt_properties_fetch( this, name );
288
289 // Set it if not NULL
290 if ( property != NULL )
291 {
292 error = mlt_property_set_int( property, value );
293 mlt_properties_do_mirror( this, name );
294 }
295
296 return error;
297 }
298
299 /** Get a value associated to the name.
300 */
301
302 double mlt_properties_get_double( mlt_properties this, char *name )
303 {
304 mlt_property value = mlt_properties_find( this, name );
305 return value == NULL ? 0 : mlt_property_get_double( value );
306 }
307
308 /** Set a value associated to the name.
309 */
310
311 int mlt_properties_set_double( mlt_properties this, char *name, double value )
312 {
313 int error = 1;
314
315 // Fetch the property to work with
316 mlt_property property = mlt_properties_fetch( this, name );
317
318 // Set it if not NULL
319 if ( property != NULL )
320 {
321 error = mlt_property_set_double( property, value );
322 mlt_properties_do_mirror( this, name );
323 }
324
325 return error;
326 }
327
328 /** Get a value associated to the name.
329 */
330
331 mlt_position mlt_properties_get_position( mlt_properties this, char *name )
332 {
333 mlt_property value = mlt_properties_find( this, name );
334 return value == NULL ? 0 : mlt_property_get_position( value );
335 }
336
337 /** Set a value associated to the name.
338 */
339
340 int mlt_properties_set_position( mlt_properties this, char *name, mlt_position value )
341 {
342 int error = 1;
343
344 // Fetch the property to work with
345 mlt_property property = mlt_properties_fetch( this, name );
346
347 // Set it if not NULL
348 if ( property != NULL )
349 {
350 error = mlt_property_set_position( property, value );
351 mlt_properties_do_mirror( this, name );
352 }
353
354 return error;
355 }
356
357 /** Get a value associated to the name.
358 */
359
360 void *mlt_properties_get_data( mlt_properties this, char *name, int *length )
361 {
362 mlt_property value = mlt_properties_find( this, name );
363 return value == NULL ? NULL : mlt_property_get_data( value, length );
364 }
365
366 /** Set a value associated to the name.
367 */
368
369 int mlt_properties_set_data( mlt_properties this, char *name, void *value, int length, mlt_destructor destroy, mlt_serialiser serialise )
370 {
371 int error = 1;
372
373 // Fetch the property to work with
374 mlt_property property = mlt_properties_fetch( this, name );
375
376 // Set it if not NULL
377 if ( property != NULL )
378 error = mlt_property_set_data( property, value, length, destroy, serialise );
379
380 return error;
381 }
382
383 /** Rename a property.
384 */
385
386 int mlt_properties_rename( mlt_properties this, char *source, char *dest )
387 {
388 mlt_property value = mlt_properties_find( this, dest );
389
390 if ( value == NULL )
391 {
392 property_list *list = this->private;
393 int i = 0;
394
395 // Locate the item
396 for ( i = 0; i < list->count; i ++ )
397 {
398 if ( !strcmp( list->name[ i ], source ) )
399 {
400 free( list->name[ i ] );
401 list->name[ i ] = strdup( dest );
402 break;
403 }
404 }
405 }
406
407 return value != NULL;
408 }
409
410 /** Dump the properties.
411 */
412
413 void mlt_properties_dump( mlt_properties this, FILE *output )
414 {
415 property_list *list = this->private;
416 int i = 0;
417 for ( i = 0; i < list->count; i ++ )
418 fprintf( stderr, "%s = %s\n", list->name[ i ], mlt_properties_get( this, list->name[ i ] ) );
419 }
420
421 /** Close the list.
422 */
423
424 void mlt_properties_close( mlt_properties this )
425 {
426 property_list *list = this->private;
427 int index = 0;
428
429 // Clean up names and values
430 for ( index = list->count - 1; index >= 0; index -- )
431 {
432 free( list->name[ index ] );
433 mlt_property_close( list->value[ index ] );
434 }
435
436 // Clear up the list
437 free( list->name );
438 free( list->value );
439 free( list );
440
441 // Free this now if this has no child
442 if ( this->child == NULL )
443 free( this );
444 }
445