2 * transition_composite.c -- compose one image over another using alpha channel
3 * Copyright (C) 2003-2004 Ushodaya Enterprises Limited
4 * Author: Dan Dennedy <dan@dennedy.org>
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.
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.
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.
21 #include "transition_composite.h"
22 #include <framework/mlt_frame.h>
37 int nw; // normalised width
38 int nh; // normalised height
39 int sw; // scaled width, not including consumer scale based upon w/nw
40 int sh; // scaled height, not including consumer scale based upon h/nh
45 int halign; // horizontal alignment: 0=left, 1=center, 2=right
46 int valign; // vertical alignment: 0=top, 1=middle, 2=bottom
48 struct geometry_s *next;
51 /** Parse a value from a geometry string.
54 static float parse_value( char **ptr, int normalisation, char delim, float defaults )
56 float value = defaults;
58 if ( *ptr != NULL && **ptr != '\0' )
61 value = strtod( *ptr, &end );
65 value = ( value / 100.0 ) * normalisation;
66 while ( *end == delim || *end == '%' )
75 /** Parse a geometry property string with the syntax X,Y:WxH:MIX. Any value can be
76 expressed as a percentage by appending a % after the value, otherwise values are
77 assumed to be relative to the normalised dimensions of the consumer.
80 static void geometry_parse( struct geometry_s *geometry, struct geometry_s *defaults, char *property, int nw, int nh )
82 // Assign normalised width and height
86 // Assign from defaults if available
87 if ( defaults != NULL )
89 geometry->x = defaults->x;
90 geometry->y = defaults->y;
91 geometry->w = geometry->sw = defaults->w;
92 geometry->h = geometry->sh = defaults->h;
93 geometry->distort = defaults->distort;
94 geometry->mix = defaults->mix;
95 defaults->next = geometry;
102 // Parse the geomtry string
103 if ( property != NULL && strcmp( property, "" ) )
105 char *ptr = property;
106 geometry->x = parse_value( &ptr, nw, ',', geometry->x );
107 geometry->y = parse_value( &ptr, nh, ':', geometry->y );
108 geometry->w = geometry->sw = parse_value( &ptr, nw, 'x', geometry->w );
109 geometry->h = geometry->sh = parse_value( &ptr, nh, ':', geometry->h );
112 geometry->distort = 1;
117 geometry->mix = parse_value( &ptr, 100, ' ', geometry->mix );
121 /** Calculate real geometry.
124 static void geometry_calculate( struct geometry_s *output, struct geometry_s *in, float position )
126 // Search in for position
127 struct geometry_s *out = in->next;
129 if ( position >= 1.0 )
131 int section = floor( position );
133 if ( section % 2 == 1 )
134 position = 1.0 - position;
137 while ( out->next != NULL )
139 if ( position >= in->position && position < out->position )
146 position = ( position - in->position ) / ( out->position - in->position );
148 // Calculate this frames geometry
151 output->x = in->x + ( out->x - in->x ) * position + 0.5;
152 output->y = in->y + ( out->y - in->y ) * position + 0.5;
153 output->w = in->w + ( out->w - in->w ) * position;
154 output->h = in->h + ( out->h - in->h ) * position;
155 output->mix = in->mix + ( out->mix - in->mix ) * position;
156 output->distort = in->distort;
159 void transition_destroy_keys( void *arg )
161 struct geometry_s *ptr = arg;
162 struct geometry_s *next = NULL;
164 while ( ptr != NULL )
172 static struct geometry_s *transition_parse_keys( mlt_transition this, int normalised_width, int normalised_height )
174 // Loop variable for property interrogation
177 // Get the properties of the transition
178 mlt_properties properties = mlt_transition_properties( this );
180 // Get the in and out position
181 mlt_position in = mlt_transition_get_in( this );
182 mlt_position out = mlt_transition_get_out( this );
185 struct geometry_s *start = calloc( 1, sizeof( struct geometry_s ) );
187 // Create the end (we always need two entries)
188 struct geometry_s *end = calloc( 1, sizeof( struct geometry_s ) );
191 struct geometry_s *ptr = start;
193 // Parse the start property
194 geometry_parse( start, NULL, mlt_properties_get( properties, "start" ), normalised_width, normalised_height );
196 // Parse the keys in between
197 for ( i = 0; i < mlt_properties_count( properties ); i ++ )
199 // Get the name of the property
200 char *name = mlt_properties_get_name( properties, i );
202 // Check that it's valid
203 if ( !strncmp( name, "key[", 4 ) )
205 // Get the value of the property
206 char *value = mlt_properties_get_value( properties, i );
208 // Determine the frame number
209 int frame = atoi( name + 4 );
211 // Determine the position
214 if ( frame >= 0 && frame < ( out - in ) )
215 position = ( float )frame / ( float )( out - in + 1 );
216 else if ( frame < 0 && - frame < ( out - in ) )
217 position = ( float )( out - in + frame ) / ( float )( out - in + 1 );
219 // For now, we'll exclude all keys received out of order
220 if ( position > ptr->position )
222 // Create a new geometry
223 struct geometry_s *temp = calloc( 1, sizeof( struct geometry_s ) );
225 // Parse and add to the list
226 geometry_parse( temp, ptr, value, normalised_width, normalised_height );
228 // Assign the position
229 temp->position = position;
231 // Allow the next to be appended after this one
236 fprintf( stderr, "Key out of order - skipping %s\n", name );
242 geometry_parse( end, ptr, mlt_properties_get( properties, "end" ), normalised_width, normalised_height );
244 end->position = ( float )( out - in ) / ( float )( out - in + 1 );
248 // Assign to properties to ensure we get destroyed
249 mlt_properties_set_data( properties, "geometries", start, 0, transition_destroy_keys, NULL );
254 /** Parse the alignment properties into the geometry.
257 static int alignment_parse( char* align )
261 if ( align == NULL );
262 else if ( isdigit( align[ 0 ] ) )
264 else if ( align[ 0 ] == 'c' || align[ 0 ] == 'm' )
266 else if ( align[ 0 ] == 'r' || align[ 0 ] == 'b' )
272 /** Adjust position according to scaled size and alignment properties.
275 static void alignment_calculate( struct geometry_s *geometry )
277 geometry->x += ( geometry->w - geometry->sw ) * geometry->halign / 2 + 0.5;
278 geometry->y += ( geometry->h - geometry->sh ) * geometry->valign / 2 + 0.5;
281 /** Calculate the position for this frame.
284 static inline float position_calculate( mlt_transition this, mlt_frame frame )
286 // Get the in and out position
287 mlt_position in = mlt_transition_get_in( this );
288 mlt_position out = mlt_transition_get_out( this );
291 mlt_position position = mlt_frame_get_position( frame );
294 return ( float )( position - in ) / ( float )( out - in + 1 );
297 /** Calculate the field delta for this frame - position between two frames.
300 static inline float delta_calculate( mlt_transition this, mlt_frame frame )
302 // Get the in and out position
303 mlt_position in = mlt_transition_get_in( this );
304 mlt_position out = mlt_transition_get_out( this );
306 // Get the position of the frame
307 mlt_position position = mlt_frame_get_position( frame );
310 float x = ( float )( position - in ) / ( float )( out - in + 1 );
311 float y = ( float )( position + 1 - in ) / ( float )( out - in + 1 );
313 return ( y - x ) / 2.0;
316 static int get_value( mlt_properties properties, char *preferred, char *fallback )
318 int value = mlt_properties_get_int( properties, preferred );
320 value = mlt_properties_get_int( properties, fallback );
324 /** Composite function.
327 static int composite_yuv( uint8_t *p_dest, int width_dest, int height_dest, int bpp, uint8_t *p_src, int width_src, int height_src, uint8_t *p_alpha, struct geometry_s geometry, int field )
331 int x_src = 0, y_src = 0;
332 int32_t weight = ( 1 << 16 ) * ( geometry.mix / 100 );
333 int stride_src = width_src * bpp;
334 int stride_dest = width_dest * bpp;
336 // Adjust to consumer scale
337 int x = geometry.x * width_dest / geometry.nw + 0.5;
338 int y = geometry.y * height_dest / geometry.nh + 0.5;
343 // optimization points - no work to do
344 if ( width_src <= 0 || height_src <= 0 )
347 if ( ( x < 0 && -x >= width_src ) || ( y < 0 && -y >= height_src ) )
350 // crop overlay off the left edge of frame
358 // crop overlay beyond right edge of frame
359 else if ( x + width_src > width_dest )
360 width_src = width_dest - x;
362 // crop overlay off the top edge of the frame
368 // crop overlay below bottom edge of frame
369 else if ( y + height_src > height_dest )
370 height_src = height_dest - y;
372 // offset pointer into overlay buffer based on cropping
373 p_src += x_src * bpp + y_src * stride_src;
375 // offset pointer into frame buffer based upon positive coordinates only!
376 p_dest += ( x < 0 ? 0 : x ) * bpp + ( y < 0 ? 0 : y ) * stride_dest;
378 // offset pointer into alpha channel based upon cropping
380 p_alpha += x_src + y_src * stride_src / bpp;
382 // Assuming lower field first
383 // Special care is taken to make sure the b_frame is aligned to the correct field.
384 // field 0 = lower field and y should be odd (y is 0-based).
385 // field 1 = upper field and y should be even.
386 if ( ( field > -1 ) && ( y % 2 == field ) )
388 //fprintf( stderr, "field %d y %d\n", field, y );
389 if ( ( field == 1 && y < height_dest - 1 ) || ( field == 0 && y == 0 ) )
390 p_dest += stride_dest;
392 p_dest -= stride_dest;
395 // On the second field, use the other lines from b_frame
400 p_alpha += stride_src / bpp;
407 uint8_t *z = p_alpha;
411 int step = ( field > -1 ) ? 2 : 1;
413 stride_src = stride_src * step;
414 int alpha_stride = stride_src / bpp;
415 stride_dest = stride_dest * step;
417 // now do the compositing only to cropped extents
418 for ( i = 0; i < height_src; i += step )
425 for ( j = 0; j < width_src; j ++ )
427 a = ( z == NULL ) ? 255 : *z ++;
428 value = ( weight * ( a + 1 ) ) >> 8;
429 *o ++ = ( *p++ * value + *q++ * ( ( 1 << 16 ) - value ) ) >> 16;
430 *o ++ = ( *p++ * value + *q++ * ( ( 1 << 16 ) - value ) ) >> 16;
434 p_dest += stride_dest;
436 p_alpha += alpha_stride;
443 /** Get the properly sized image from b_frame.
446 static int get_b_frame_image( mlt_transition this, mlt_frame b_frame, uint8_t **image, int *width, int *height, struct geometry_s *geometry )
449 mlt_image_format format = mlt_image_yuv422;
451 // Get the properties objects
452 mlt_properties b_props = mlt_frame_properties( b_frame );
453 mlt_properties properties = mlt_transition_properties( this );
455 // ???: Not getting the logic of this...
456 geometry->sw = geometry->w;
457 geometry->sh = geometry->h;
459 if ( mlt_properties_get( properties, "distort" ) == NULL && geometry->distort == 0 )
461 // Adjust b_frame pixel aspect
462 int normalised_width = geometry->w;
463 int normalised_height = geometry->h;
464 int real_width = get_value( b_props, "real_width", "width" );
465 int real_height = get_value( b_props, "real_height", "height" );
466 double input_ar = mlt_frame_get_aspect_ratio( b_frame );
467 double output_ar = mlt_properties_get_double( b_props, "consumer_aspect_ratio" );
468 int scaled_width = real_width;
469 int scaled_height = real_height;
470 double output_sar = ( double ) geometry->nw / geometry->nh / output_ar;
472 // If the output is fat pixels (NTSC) then stretch our input horizontally
473 // derived from: output_sar / input_sar * real_width
474 scaled_width = output_sar * real_height * input_ar;
476 // Now ensure that our images fit in the normalised frame
477 if ( scaled_width > normalised_width )
479 scaled_height = scaled_height * normalised_width / scaled_width;
480 scaled_width = normalised_width;
482 if ( scaled_height > normalised_height )
484 scaled_width = scaled_width * normalised_height / scaled_height;
485 scaled_height = normalised_height;
488 // Now apply the fill
489 // TODO: Should combine fill/distort in one property
490 if ( mlt_properties_get( properties, "fill" ) != NULL )
492 scaled_width = ( geometry->w / scaled_width ) * scaled_width;
493 scaled_height = ( geometry->h / scaled_height ) * scaled_height;
496 // Save the new scaled dimensions
497 geometry->sw = scaled_width;
498 geometry->sh = scaled_height;
501 // We want to ensure that we bypass resize now...
502 mlt_properties_set( b_props, "distort", "true" );
504 // Take into consideration alignment for optimisation
505 alignment_calculate( geometry );
507 // Adjust to consumer scale
508 int x = geometry->x * *width / geometry->nw + 0.5;
509 int y = geometry->y * *height / geometry->nh + 0.5;
510 *width = geometry->sw * *width / geometry->nw;
511 *height = geometry->sh * *height / geometry->nh;
515 // optimization points - no work to do
516 if ( *width <= 0 || *height <= 0 )
519 if ( ( x < 0 && -x >= *width ) || ( y < 0 && -y >= *height ) )
522 ret = mlt_frame_get_image( b_frame, image, &format, width, height, 1 );
528 static uint8_t *transition_get_alpha_mask( mlt_frame this )
530 // Obtain properties of frame
531 mlt_properties properties = mlt_frame_properties( this );
533 // Return the alpha mask
534 return mlt_properties_get_data( properties, "alpha", NULL );
540 static int transition_get_image( mlt_frame a_frame, uint8_t **image, mlt_image_format *format, int *width, int *height, int writable )
542 // Get the b frame from the stack
543 mlt_frame b_frame = mlt_frame_pop_frame( a_frame );
545 // This compositer is yuv422 only
546 *format = mlt_image_yuv422;
548 // Get the transition from the a frame
549 mlt_transition this = mlt_frame_pop_service( a_frame );
551 // Get the image from the a frame
552 mlt_frame_get_image( a_frame, image, format, width, height, 1 );
554 if ( b_frame != NULL )
556 // Get the properties of the a frame
557 mlt_properties a_props = mlt_frame_properties( a_frame );
559 // Get the properties of the b frame
560 mlt_properties b_props = mlt_frame_properties( b_frame );
562 // Get the properties from the transition
563 mlt_properties properties = mlt_transition_properties( this );
565 // Structures for geometry
566 struct geometry_s result;
567 struct geometry_s *start = mlt_properties_get_data( properties, "geometries", NULL );
569 // Calculate the position
570 float position = mlt_properties_get_double( b_props, "relative_position" );
571 float delta = delta_calculate( this, a_frame );
573 // Now parse the geometries
576 // Obtain the normalised width and height from the a_frame
577 int normalised_width = mlt_properties_get_int( a_props, "normalised_width" );
578 int normalised_height = mlt_properties_get_int( a_props, "normalised_height" );
580 // Parse the transitions properties
581 start = transition_parse_keys( this, normalised_width, normalised_height );
584 // Since we are the consumer of the b_frame, we must pass along these
585 // consumer properties from the a_frame
586 mlt_properties_set_double( b_props, "consumer_aspect_ratio", mlt_properties_get_double( a_props, "consumer_aspect_ratio" ) );
587 mlt_properties_set_double( b_props, "consumer_scale", mlt_properties_get_double( a_props, "consumer_scale" ) );
589 // Do the calculation
590 geometry_calculate( &result, start, position );
592 // Now parse the alignment
593 result.halign = alignment_parse( mlt_properties_get( properties, "halign" ) );
594 result.valign = alignment_parse( mlt_properties_get( properties, "valign" ) );
596 // Get the image from the b frame
597 uint8_t *image_b = NULL;
598 int width_b = *width;
599 int height_b = *height;
601 if ( get_b_frame_image( this, b_frame, &image_b, &width_b, &height_b, &result ) == 0 )
603 uint8_t *dest = *image;
604 uint8_t *src = image_b;
606 uint8_t *alpha = mlt_frame_get_alpha_mask( b_frame );
607 int progressive = mlt_properties_get_int( a_props, "progressive" ) ||
608 mlt_properties_get_int( a_props, "consumer_progressive" ) ||
609 mlt_properties_get_int( properties, "progressive" );
612 for ( field = 0; field < ( progressive ? 1 : 2 ); field++ )
614 // Assume lower field (0) first
615 float field_position = position + field * delta;
617 // Do the calculation if we need to
618 geometry_calculate( &result, start, field_position );
621 alignment_calculate( &result );
623 // Composite the b_frame on the a_frame
624 composite_yuv( dest, *width, *height, bpp, src, width_b, height_b, alpha, result, progressive ? -1 : field );
632 /** Composition transition processing.
635 static mlt_frame composite_process( mlt_transition this, mlt_frame a_frame, mlt_frame b_frame )
637 // Propogate the transition properties to the b frame
638 mlt_properties_set_double( mlt_frame_properties( b_frame ), "relative_position", position_calculate( this, a_frame ) );
639 mlt_frame_push_service( a_frame, this );
640 mlt_frame_push_get_image( a_frame, transition_get_image );
641 mlt_frame_push_frame( a_frame, b_frame );
645 /** Constructor for the filter.
648 mlt_transition transition_composite_init( char *arg )
650 mlt_transition this = calloc( sizeof( struct mlt_transition_s ), 1 );
651 if ( this != NULL && mlt_transition_init( this, NULL ) == 0 )
653 this->process = composite_process;
654 mlt_properties_set( mlt_transition_properties( this ), "start", arg != NULL ? arg : "85%,5%:10%x10%" );