]> git.sesse.net Git - mlt/blob - src/framework/mlt_property.c
added luma transition and new frame properties
[mlt] / src / framework / mlt_property.c
1 /*
2  * mlt_property.c -- property 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
23 #include "mlt_property.h"
24
25 #include <stdio.h>
26 #include <stdlib.h>
27 #include <string.h>
28
29 /** Construct and uninitialised property.
30 */
31
32 mlt_property mlt_property_init( )
33 {
34         return calloc( sizeof( struct mlt_property_s ), 1 );
35 }
36
37 /** Clear a property.
38 */
39
40 void mlt_property_clear( mlt_property this )
41 {
42         // Special case data handling
43         if ( this->types & mlt_prop_data && this->destructor != NULL )
44                 this->destructor( this->data );
45
46         // Special case string handling
47         if ( this->types & mlt_prop_string )
48                 free( this->prop_string );
49
50         // We can wipe it now.
51         memset( this, 0, sizeof( struct mlt_property_s ) );
52 }
53
54 /** Set an int on this property.
55 */
56
57 int mlt_property_set_int( mlt_property this, int value )
58 {
59         mlt_property_clear( this );
60         this->types = mlt_prop_int;
61         this->prop_int = value;
62         return 0;
63 }
64
65 /** Set a double on this property.
66 */
67
68 int mlt_property_set_double( mlt_property this, double value )
69 {
70         mlt_property_clear( this );
71         this->types = mlt_prop_double;
72         this->prop_double = value;
73         return 0;
74 }
75
76 /** Set a timecode on this property.
77 */
78
79 int mlt_property_set_timecode( mlt_property this, mlt_timecode value )
80 {
81         mlt_property_clear( this );
82         this->types = mlt_prop_timecode;
83         this->prop_timecode = value;
84         return 0;
85 }
86
87 /** Set a string on this property.
88 */
89
90 int mlt_property_set_string( mlt_property this, char *value )
91 {
92         mlt_property_clear( this );
93         this->types = mlt_prop_string;
94         if ( value != NULL )
95                 this->prop_string = strdup( value );
96         return this->prop_string != NULL;
97 }
98
99 /** Set a data on this property.
100 */
101
102 int mlt_property_set_data( mlt_property this, void *value, int length, mlt_destructor destructor, mlt_serialiser serialiser )
103 {
104         if ( this->data == value )
105                 this->destructor = NULL;
106         mlt_property_clear( this );
107         this->types = mlt_prop_data;
108         this->data = value;
109         this->length = length;
110         this->destructor = destructor;
111         this->serialiser = serialiser;
112         return 0;
113 }
114
115 /** Get an int from this property.
116 */
117
118 int mlt_property_get_int( mlt_property this )
119 {
120         if ( this->types & mlt_prop_int )
121                 return this->prop_int;
122         else if ( this->types & mlt_prop_double )
123                 return ( int )this->prop_double;
124         else if ( this->types & mlt_prop_timecode )
125                 return ( int )this->prop_timecode;
126         else if ( this->types & mlt_prop_string )
127                 return atoi( this->prop_string );
128         return 0;
129 }
130
131 /** Get a double from this property.
132 */
133
134 double mlt_property_get_double( mlt_property this )
135 {
136         if ( this->types & mlt_prop_double )
137                 return this->prop_double;
138         else if ( this->types & mlt_prop_int )
139                 return ( double )this->prop_int;
140         else if ( this->types & mlt_prop_timecode )
141                 return ( double )this->prop_timecode;
142         else if ( this->types & mlt_prop_string )
143                 return atof( this->prop_string );
144         return 0;
145 }
146
147 /** Get a timecode from this property.
148 */
149
150 mlt_timecode mlt_property_get_timecode( mlt_property this )
151 {
152         if ( this->types & mlt_prop_timecode )
153                 return this->prop_timecode;
154         else if ( this->types & mlt_prop_int )
155                 return ( mlt_timecode )this->prop_int;
156         else if ( this->types & mlt_prop_double )
157                 return ( mlt_timecode )this->prop_double;
158         else if ( this->types & mlt_prop_string )
159                 return ( mlt_timecode )atof( this->prop_string );
160         return 0;
161 }
162
163 /** Get a string from this property.
164 */
165
166 char *mlt_property_get_string( mlt_property this )
167 {
168         // Construct a string if need be
169         if ( ! ( this->types & mlt_prop_string ) )
170         {
171                 if ( this->types & mlt_prop_int )
172                 {
173                         this->types |= mlt_prop_string;
174                         this->prop_string = malloc( 32 );
175                         sprintf( this->prop_string, "%d", this->prop_int );
176                 }
177                 else if ( this->types & mlt_prop_double )
178                 {
179                         this->types |= mlt_prop_string;
180                         this->prop_string = malloc( 32 );
181                         sprintf( this->prop_string, "%e", this->prop_double );
182                 }
183                 else if ( this->types & mlt_prop_timecode )
184                 {
185                         this->types |= mlt_prop_string;
186                         this->prop_string = malloc( 32 );
187                         sprintf( this->prop_string, "%e", this->prop_timecode );
188                 }
189                 else if ( this->types & mlt_prop_data && this->serialiser != NULL )
190                 {
191                         this->types |= mlt_prop_string;
192                         this->prop_string = this->serialiser( this->data, this->length );
193                 }
194         }
195
196         // Return the string (may be NULL)
197         return this->prop_string;
198 }
199
200 /** Get a data and associated length.
201 */
202
203 void *mlt_property_get_data( mlt_property this, int *length )
204 {
205         // Assign length if not NULL
206         if ( length != NULL )
207                 *length = this->length;
208
209         // Return the data (note: there is no conversion here)
210         return this->data;
211 }
212
213 /** Close this property.
214 */
215
216 void mlt_property_close( mlt_property this )
217 {
218         mlt_property_clear( this );
219         free( this );
220 }
221
222