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 )
64 mlt_property points[4];
65 animation_node prev = current->prev;
66 animation_node next = current->next;
68 while ( prev && !prev->item.is_key ) prev = prev->prev;
69 while ( next && !next->item.is_key ) next = next->next;
72 current->item.is_key = 1;
73 points[0] = prev->prev? prev->prev->item.property : prev->item.property;
74 points[1] = prev->item.property;
75 points[2] = next->item.property;
76 points[3] = next->next? next->next->item.property : next->item.property;
77 progress = current->item.frame - prev->item.frame;
78 progress /= next->item.frame - prev->item.frame;
79 mlt_property_interpolate( current->item.property, points, progress,
80 self->fps, self->locale, current->item.keyframe_type );
83 // Move to the next item
84 current = current->next;
89 static int mlt_animation_drop( mlt_animation self, animation_node node )
91 if ( node == self->nodes )
93 self->nodes = node->next;
95 self->nodes->prev = NULL;
96 self->nodes->item.is_key = 1;
99 else if ( node->next && node->prev )
101 node->prev->next = node->next;
102 node->next->prev = node->prev;
104 else if ( node->next )
106 node->next->prev = node->prev;
108 else if ( node->prev )
110 node->prev->next = node->next;
112 mlt_property_close( node->item.property );
118 static void mlt_animation_clean( mlt_animation self )
123 while ( self->nodes )
124 mlt_animation_drop( self, self->nodes );
127 int mlt_animation_parse(mlt_animation self, const char *data, int length, double fps, locale_t locale )
131 struct mlt_animation_item_s item;
132 mlt_tokeniser tokens = mlt_tokeniser_init( );
134 // Clean the existing geometry
135 mlt_animation_clean( self );
137 // Update the info on the data
139 self->data = strdup( data );
140 self->length = length;
142 self->locale = locale;
143 item.property = mlt_property_init();
147 mlt_tokeniser_parse_new( tokens, (char*) data, ";" );
149 // Iterate through each token
150 for ( i = 0; i < mlt_tokeniser_count( tokens ); i++ )
152 char *value = mlt_tokeniser_get_string( tokens, i );
154 // If no data in keyframe, drop it (trailing semicolon)
155 if ( !value || !strcmp( value, "" ) )
159 item.frame = item.is_key = 0;
161 // Now parse the item
162 mlt_animation_parse_item( self, &item, value );
164 // Now insert into place
165 mlt_animation_insert( self, &item );
167 mlt_animation_interpolate( self );
170 mlt_tokeniser_close( tokens );
171 mlt_property_close( item.property );
176 // Conditionally refresh in case of a change
177 int mlt_animation_refresh(mlt_animation self, const char *data, int length)
179 if ( ( length != self->length )|| ( data && ( !self->data || strcmp( data, self->data ) ) ) )
180 return mlt_animation_parse( self, data, length, self->fps, self->locale );
184 int mlt_animation_get_length( mlt_animation self )
192 void mlt_animation_set_length( mlt_animation self, int length )
195 self->length = length;
198 int mlt_animation_parse_item( mlt_animation self, mlt_animation_item item, const char *value )
202 if ( value && strcmp( value, "" ) )
204 // Determine if a position has been specified
205 if ( strchr( value, '=' ) )
209 #if defined(__GLIBC__) || defined(__DARWIN__)
211 temp = strtod_l( value, &p, self->locale );
214 temp = strtod( value, &p );
215 // If p == value then it is likely a time clock or time code.
216 if ( temp > -1.0 && temp < 1.0 && p != value )
218 // Parse a relative time (-1, 1).
219 item->frame = temp * self->length;
223 // Parse an absolute time value.
224 mlt_property_set_string( item->property, value );
225 item->frame = mlt_property_get_int( item->property, self->fps, self->locale );
228 // The character preceeding the equal sign indicates interpolation method.
229 p = strchr( value, '=' ) - 1;
230 if ( p[0] == '|' || p[0] == '!' )
231 item->keyframe_type = mlt_keyframe_discrete;
232 else if ( p[0] == '~' )
233 item->keyframe_type = mlt_keyframe_smooth;
235 item->keyframe_type = mlt_keyframe_linear;
239 // Special case - frame < 0
240 if ( item->frame < 0 )
241 item->frame += self->length;
243 // Set remainder of string as item value.
244 mlt_property_set_string( item->property, value );
255 // Fetch a geometry item for an absolute position
256 int mlt_animation_get_item( mlt_animation self, mlt_animation_item item, int position )
259 // Need to find the nearest keyframe to the position specifed
260 animation_node node = self->nodes;
262 // Iterate through the keyframes until we reach last or have
263 while ( node && node->next && position >= node->next->item.frame )
268 item->keyframe_type = node->item.keyframe_type;
270 // Position is before the first keyframe.
271 if ( position < node->item.frame )
274 mlt_property_pass( item->property, node->item.property );
277 else if ( position == node->item.frame )
279 item->is_key = node->item.is_key;
280 mlt_property_pass( item->property, node->item.property );
282 // Position is after the last keyframe.
283 else if ( !node->next )
286 mlt_property_pass( item->property, node->item.property );
288 // Interpolation needed.
292 mlt_property points[4];
293 points[0] = node->prev? node->prev->item.property : node->item.property;
294 points[1] = node->item.property;
295 points[2] = node->next->item.property;
296 points[3] = node->next->next? node->next->next->item.property : node->next->item.property;
297 progress = position - node->item.frame;
298 progress /= node->next->item.frame - node->item.frame;
299 mlt_property_interpolate( item->property, points, progress,
300 self->fps, self->locale, item->keyframe_type );
306 item->frame = item->is_key = 0;
309 item->frame = position;
314 // Specify an animation item at an absolute position
315 int mlt_animation_insert( mlt_animation self, mlt_animation_item item )
318 animation_node node = calloc( 1, sizeof( *node ) );
319 node->item.frame = item->frame;
320 node->item.is_key = 1;
321 node->item.keyframe_type = item->keyframe_type;
322 node->item.property = mlt_property_init();
323 mlt_property_pass( node->item.property, item->property );
325 // Determine if we need to insert or append to the list, or if it's a new list
328 // Get the first item
329 animation_node current = self->nodes;
331 // Locate an existing nearby item
332 while ( current->next && item->frame > current->item.frame )
333 current = current->next;
335 if ( item->frame < current->item.frame )
337 if ( current == self->nodes )
340 current->prev->next = node;
341 node->next = current;
342 node->prev = current->prev;
343 current->prev = node;
345 else if ( item->frame > current->item.frame )
348 current->next->prev = node;
349 node->next = current->next;
350 node->prev = current;
351 current->next = node;
355 // Update matching node.
356 current->item.frame = item->frame;
357 current->item.is_key = 1;
358 current->item.keyframe_type = item->keyframe_type;
359 mlt_property_close( current->item.property );
360 current->item.property = node->item.property;
366 // Set the first item
373 // Remove the keyframe at the specified position
374 int mlt_animation_remove( mlt_animation self, int position )
377 animation_node node = self->nodes;
379 while ( node && position != node->item.frame )
382 if ( node && position == node->item.frame )
383 error = mlt_animation_drop( self, node );
388 // Get the keyfame at the position or the next following
389 int mlt_animation_next_key( mlt_animation self, mlt_animation_item item, int position )
391 animation_node node = self->nodes;
393 while ( node && position > node->item.frame )
398 item->frame = node->item.frame;
399 item->is_key = node->item.is_key;
400 item->keyframe_type = node->item.keyframe_type;
401 mlt_property_pass( item->property, node->item.property );
404 return ( node == NULL );
407 // Get the keyframe at the position or the previous key
408 int mlt_animation_prev_key( mlt_animation self, mlt_animation_item item, int position )
410 animation_node node = self->nodes;
412 while ( node && node->next && position >= node->next->item.frame )
417 item->frame = node->item.frame;
418 item->is_key = node->item.is_key;
419 item->keyframe_type = node->item.keyframe_type;
420 mlt_property_pass( item->property, node->item.property );
423 return ( node == NULL );
426 char *mlt_animation_serialize_cut( mlt_animation self, int in, int out )
428 struct mlt_animation_item_s item;
429 char *ret = malloc( 1000 );
433 item.property = mlt_property_init();
437 out = mlt_animation_get_length( self );
449 // If it's the first frame, then it's not necessarily a key
450 if ( item.frame == in )
452 if ( mlt_animation_get_item( self, &item, item.frame ) )
455 // If the first keyframe is larger than the current position
456 // then do nothing here
457 if ( self->nodes->item.frame > item.frame )
463 // To ensure correct seeding
466 // Typically, we move from keyframe to keyframe
467 else if ( item.frame < out )
469 if ( mlt_animation_next_key( self, &item, item.frame ) )
472 // Special case - crop at the out point
473 if ( item.frame > out )
474 mlt_animation_get_item( self, &item, out );
476 // We've handled the last keyframe
482 // Determine length of string to be appended.
483 if ( item.frame - in != 0 )
486 item_len += strlen( mlt_property_get_string_l( item.property, self->locale ) );
488 // Reallocate return string to be long enough.
489 while ( used + item_len + 2 > size ) // +2 for ';' and NULL
492 ret = realloc( ret, size );
495 // Append item delimiter (;) if needed.
496 if ( ret && used > 0 )
503 // Append keyframe time and keyframe/value delimiter (=).
505 switch (item.keyframe_type) {
506 case mlt_keyframe_discrete:
509 case mlt_keyframe_smooth:
516 sprintf( ret + used, "%d%s=", item.frame - in, s );
518 // Append item value.
520 strcat( ret, mlt_property_get_string_l( item.property, self->locale ) );
521 used = strlen( ret );
526 mlt_property_close( item.property );
531 // Serialise the current geometry
532 char *mlt_animation_serialize( mlt_animation self )
534 char *ret = mlt_animation_serialize_cut( self, 0, self->length );
541 return strdup( ret );
544 // Close the geometry
545 void mlt_animation_close( mlt_animation self )
549 mlt_animation_clean( self );