2 * mlt_animation.c -- provides the property animation API
3 * Copyright (C) 2004-2013 Ushodaya Enterprises Limited
4 * Author: Charles Yates <charles.yates@pandora.be>
5 * Author: Dan Dennedy <dan@dennedy.org>
7 * This library is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Lesser General Public
9 * License as published by the Free Software Foundation; either
10 * version 2.1 of the License, or (at your option) any later version.
12 * This library is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with this library; if not, write to the Free Software
19 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
22 #include "mlt_animation.h"
23 #include "mlt_tokeniser.h"
24 #include "mlt_profile.h"
30 typedef struct animation_node_s *animation_node;
31 struct animation_node_s
33 struct mlt_animation_item_s item;
34 animation_node next, prev;
37 struct mlt_animation_s
46 // Create a new geometry structure
47 mlt_animation mlt_animation_new( )
49 mlt_animation self = calloc( 1, sizeof( *self ) );
53 void mlt_animation_interpolate( mlt_animation self )
55 // Parse all items to ensure non-keyframes are calculated correctly.
58 animation_node current = self->nodes;
61 if ( !current->item.is_key )
63 animation_node prev = current->prev;
64 animation_node next = current->next;
66 while ( prev && !prev->item.is_key ) prev = prev->prev;
67 while ( next && !next->item.is_key ) next = next->next;
70 current->item.is_key = 1;
71 mlt_property_interpolate( current->item.property,
72 prev->item.property, next->item.property,
73 current->item.frame - prev->item.frame,
74 next->item.frame - prev->item.frame,
75 self->fps, self->locale );
78 // Move to the next item
79 current = current->next;
84 static int mlt_animation_drop( mlt_animation self, animation_node node )
86 if ( node == self->nodes )
88 self->nodes = node->next;
90 self->nodes->prev = NULL;
91 self->nodes->item.is_key = 1;
94 else if ( node->next && node->prev )
96 node->prev->next = node->next;
97 node->next->prev = node->prev;
99 else if ( node->next )
101 node->next->prev = node->prev;
103 else if ( node->prev )
105 node->prev->next = node->next;
107 mlt_property_close( node->item.property );
113 static void mlt_animation_clean( mlt_animation self )
118 while ( self->nodes )
119 mlt_animation_drop( self, self->nodes );
122 int mlt_animation_parse(mlt_animation self, const char *data, int length, double fps, locale_t locale )
126 struct mlt_animation_item_s item;
127 mlt_tokeniser tokens = mlt_tokeniser_init( );
129 // Clean the existing geometry
130 mlt_animation_clean( self );
132 // Update the info on the data
134 self->data = strdup( data );
135 self->length = length;
137 self->locale = locale;
138 item.property = mlt_property_init();
142 mlt_tokeniser_parse_new( tokens, (char*) data, ";" );
144 // Iterate through each token
145 for ( i = 0; i < mlt_tokeniser_count( tokens ); i++ )
147 char *value = mlt_tokeniser_get_string( tokens, i );
149 // If no data in keyframe, drop it (trailing semicolon)
150 if ( !value || !strcmp( value, "" ) )
154 item.frame = item.is_key = 0;
156 // Now parse the item
157 mlt_animation_parse_item( self, &item, value );
159 // Now insert into place
160 mlt_animation_insert( self, &item );
162 mlt_animation_interpolate( self );
165 mlt_tokeniser_close( tokens );
166 mlt_property_close( item.property );
171 // Conditionally refresh in case of a change
172 int mlt_animation_refresh(mlt_animation self, const char *data, int length)
174 if ( ( length != self->length )|| ( data && ( !self->data || strcmp( data, self->data ) ) ) )
175 return mlt_animation_parse( self, data, length, self->fps, self->locale );
179 int mlt_animation_get_length( mlt_animation self )
187 void mlt_animation_set_length( mlt_animation self, int length )
190 self->length = length;
193 int mlt_animation_parse_item( mlt_animation self, mlt_animation_item item, const char *value )
197 if ( value && strcmp( value, "" ) )
199 // Determine if a position has been specified
200 if ( strchr( value, '=' ) )
204 #if defined(__GLIBC__) || defined(__DARWIN__)
206 temp = strtod_l( value, &p, self->locale );
209 temp = strtod( value, &p );
210 // If p == value then it is likely a time clock or time code.
211 if ( temp > -1.0 && temp < 1.0 && p != value )
213 // Parse a relative time (-1, 1).
214 item->frame = temp * self->length;
218 // Parse an absolute time value.
219 mlt_property_set_string( item->property, value );
220 item->frame = mlt_property_get_int( item->property, self->fps, self->locale );
222 value = strchr( value, '=' ) + 1;
224 // TODO the character preceeding the equal sign indicates method of interpolation.
227 // Special case - frame < 0
228 if ( item->frame < 0 )
229 item->frame += self->length;
231 // Obtain the current value at this position - this allows new
232 // frames to be created which don't specify all values
233 mlt_animation_get_item( self, item, item->frame );
235 // Set remainder of string as item value.
236 mlt_property_set_string( item->property, value );
247 // Fetch a geometry item for an absolute position
248 int mlt_animation_get_item( mlt_animation self, mlt_animation_item item, int position )
251 // Need to find the nearest keyframe to the position specifed
252 animation_node node = self->nodes;
254 // Iterate through the keyframes until we reach last or have
255 while ( node && node->next && position >= node->next->item.frame )
260 // Position is before the first keyframe.
261 if ( position < node->item.frame )
264 mlt_property_pass( item->property, node->item.property );
267 else if ( position == node->item.frame )
269 item->is_key = node->item.is_key;
270 mlt_property_pass( item->property, node->item.property );
272 // Position is after the last keyframe.
273 else if ( !node->next )
276 mlt_property_pass( item->property, node->item.property );
278 // Interpolation needed.
282 mlt_property_interpolate( item->property, node->item.property, node->next->item.property,
283 position - node->item.frame, node->next->item.frame - node->item.frame,
284 self->fps, self->locale );
289 item->frame = item->is_key = 0;
292 item->frame = position;
297 // Specify an animation item at an absolute position
298 int mlt_animation_insert( mlt_animation self, mlt_animation_item item )
301 animation_node node = calloc( 1, sizeof( *node ) );
302 node->item.frame = item->frame;
303 node->item.is_key = 1;
304 node->item.property = mlt_property_init();
305 mlt_property_pass( node->item.property, item->property );
307 // Determine if we need to insert or append to the list, or if it's a new list
310 // Get the first item
311 animation_node current = self->nodes;
313 // Locate an existing nearby item
314 while ( current->next && item->frame > current->item.frame )
315 current = current->next;
317 if ( item->frame < current->item.frame )
319 if ( current == self->nodes )
322 current->prev->next = node;
323 node->next = current;
324 node->prev = current->prev;
325 current->prev = node;
327 else if ( item->frame > current->item.frame )
330 current->next->prev = node;
331 node->next = current->next;
332 node->prev = current;
333 current->next = node;
337 // Update matching node.
338 current->item.frame = item->frame;
339 current->item.is_key = 1;
340 mlt_property_close( current->item.property );
341 current->item.property = node->item.property;
347 // Set the first item
354 // Remove the keyframe at the specified position
355 int mlt_animation_remove( mlt_animation self, int position )
358 animation_node node = self->nodes;
360 while ( node && position != node->item.frame )
363 if ( node && position == node->item.frame )
364 error = mlt_animation_drop( self, node );
369 // Get the keyfame at the position or the next following
370 int mlt_animation_next_key( mlt_animation self, mlt_animation_item item, int position )
372 animation_node node = self->nodes;
374 while ( node && position > node->item.frame )
379 item->frame = node->item.frame;
380 item->is_key = node->item.is_key;
381 mlt_property_pass( item->property, node->item.property );
384 return ( node == NULL );
387 // Get the keyframe at the position or the previous key
388 int mlt_animation_prev_key( mlt_animation self, mlt_animation_item item, int position )
390 animation_node node = self->nodes;
392 while ( node && node->next && position >= node->next->item.frame )
397 item->frame = node->item.frame;
398 item->is_key = node->item.is_key;
399 mlt_property_pass( item->property, node->item.property );
402 return ( node == NULL );
405 char *mlt_animation_serialize_cut( mlt_animation self, int in, int out )
407 struct mlt_animation_item_s item;
408 char *ret = malloc( 1000 );
412 item.property = mlt_property_init();
416 out = mlt_animation_get_length( self );
428 // If it's the first frame, then it's not necessarily a key
429 if ( item.frame == in )
431 if ( mlt_animation_get_item( self, &item, item.frame ) )
434 // If the first keyframe is larger than the current position
435 // then do nothing here
436 if ( self->nodes->item.frame > item.frame )
442 // To ensure correct seeding
445 // Typically, we move from keyframe to keyframe
446 else if ( item.frame < out )
448 if ( mlt_animation_next_key( self, &item, item.frame ) )
451 // Special case - crop at the out point
452 if ( item.frame > out )
453 mlt_animation_get_item( self, &item, out );
455 // We've handled the last keyframe
461 // Determine length of string to be appended.
462 if ( item.frame - in != 0 )
465 item_len += strlen( mlt_property_get_string_l( item.property, self->locale ) );
467 // Reallocate return string to be long enough.
468 while ( used + item_len + 2 > size ) // +2 for ';' and NULL
471 ret = realloc( ret, size );
474 // Append item delimiter (;) if needed.
475 if ( ret && used > 0 )
482 // Append keyframe time and keyframe/value delimiter (=).
483 if ( item.frame - in != 0 )
484 sprintf( ret + used, "%d=", item.frame - in );
485 // Append item value.
487 strcat( ret, mlt_property_get_string_l( item.property, self->locale ) );
488 used = strlen( ret );
493 mlt_property_close( item.property );
498 // Serialise the current geometry
499 char *mlt_animation_serialize( mlt_animation self )
501 char *ret = mlt_animation_serialize_cut( self, 0, self->length );
508 return strdup( ret );
511 // Close the geometry
512 void mlt_animation_close( mlt_animation self )
516 mlt_animation_clean( self );