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>
33 int nw; // normalised width
34 int nh; // normalised height
35 int sw; // scaled width, not including consumer scale based upon w/nw
36 int sh; // scaled height, not including consumer scale based upon h/nh
42 int halign; // horizontal alignment: 0=left, 1=center, 2=right
43 int valign; // vertical alignment: 0=top, 1=middle, 2=bottom
46 /** Parse a value from a geometry string.
49 static float parse_value( char **ptr, int normalisation, char delim, float defaults )
51 float value = defaults;
53 if ( *ptr != NULL && **ptr != '\0' )
56 value = strtod( *ptr, &end );
60 value = ( value / 100.0 ) * normalisation;
61 while ( *end == delim || *end == '%' )
70 /** Parse a geometry property string with the syntax X,Y:WxH:MIX. Any value can be
71 expressed as a percentage by appending a % after the value, otherwise values are
72 assumed to be relative to the normalised dimensions of the consumer.
75 static void geometry_parse( struct geometry_s *geometry, struct geometry_s *defaults, char *property, int nw, int nh )
77 // Assign normalised width and height
81 // Assign from defaults if available
82 if ( defaults != NULL )
84 geometry->x = defaults->x;
85 geometry->y = defaults->y;
86 geometry->w = geometry->sw = defaults->w;
87 geometry->h = geometry->sh = defaults->h;
88 geometry->mix = defaults->mix;
95 // Parse the geomtry string
96 if ( property != NULL )
99 geometry->x = parse_value( &ptr, nw, ',', geometry->x );
100 geometry->y = parse_value( &ptr, nh, ':', geometry->y );
101 geometry->w = geometry->sw = parse_value( &ptr, nw, 'x', geometry->w );
102 geometry->h = geometry->sh = parse_value( &ptr, nh, ':', geometry->h );
103 geometry->mix = parse_value( &ptr, 100, ' ', geometry->mix );
107 /** Calculate real geometry.
110 static void geometry_calculate( struct geometry_s *output, struct geometry_s *in, struct geometry_s *out, float position )
112 // Calculate this frames geometry
115 output->x = in->x + ( out->x - in->x ) * position + 0.5;
116 output->y = in->y + ( out->y - in->y ) * position + 0.5;
117 output->w = in->w + ( out->w - in->w ) * position;
118 output->h = in->h + ( out->h - in->h ) * position;
119 output->mix = in->mix + ( out->mix - in->mix ) * position;
122 /** Parse the alignment properties into the geometry.
125 static int alignment_parse( char* align )
129 if ( align == NULL );
130 else if ( isdigit( align[ 0 ] ) )
132 else if ( align[ 0 ] == 'c' || align[ 0 ] == 'm' )
134 else if ( align[ 0 ] == 'r' || align[ 0 ] == 'b' )
140 /** Adjust position according to scaled size and alignment properties.
143 static void alignment_calculate( struct geometry_s *geometry )
145 geometry->x += ( geometry->w - geometry->sw ) * geometry->halign / 2 + 0.5;
146 geometry->y += ( geometry->h - geometry->sh ) * geometry->valign / 2 + 0.5;
149 /** Calculate the position for this frame.
152 static float position_calculate( mlt_transition this, mlt_frame frame )
154 // Get the in and out position
155 mlt_position in = mlt_transition_get_in( this );
156 mlt_position out = mlt_transition_get_out( this );
158 // Get the position of the frame
159 mlt_position position = mlt_frame_get_position( frame );
162 return ( float )( position - in ) / ( float )( out - in + 1 );
165 /** Calculate the field delta for this frame - position between two frames.
168 static float delta_calculate( mlt_transition this, mlt_frame frame )
170 // Get the in and out position
171 mlt_position in = mlt_transition_get_in( this );
172 mlt_position out = mlt_transition_get_out( this );
174 // Get the position of the frame
175 mlt_position position = mlt_frame_get_position( frame );
178 float x = ( float )( position - in ) / ( float )( out - in + 1 );
180 float y = ( float )( position - in ) / ( float )( out - in + 1 );
182 return ( y - x ) / 2.0;
185 static int get_value( mlt_properties properties, char *preferred, char *fallback )
187 int value = mlt_properties_get_int( properties, preferred );
189 value = mlt_properties_get_int( properties, fallback );
193 /** Composite function.
196 static int composite_yuv( uint8_t *p_dest, int width_dest, int height_dest, uint8_t *p_src, int width_src, int height_src, uint8_t *p_alpha, struct geometry_s geometry, int field )
200 int x_src = 0, y_src = 0;
201 float weight = geometry.mix / 100;
202 int stride_src = width_src * 2;
203 int stride_dest = width_dest * 2;
205 // Adjust to consumer scale
206 int x = geometry.x * width_dest / geometry.nw + 0.5;
207 int y = geometry.y * height_dest / geometry.nh + 0.5;
211 // optimization points - no work to do
212 if ( width_src <= 0 || height_src <= 0 )
215 if ( ( x < 0 && -x >= width_src ) || ( y < 0 && -y >= height_src ) )
218 // crop overlay off the left edge of frame
226 // crop overlay beyond right edge of frame
227 else if ( x + width_src > width_dest )
228 width_src = width_dest - x;
230 // crop overlay off the top edge of the frame
236 // crop overlay below bottom edge of frame
237 else if ( y + height_src > height_dest )
238 height_src = height_dest - y;
240 // offset pointer into overlay buffer based on cropping
241 p_src += x_src * 2 + y_src * stride_src;
243 // offset pointer into frame buffer based upon positive, even coordinates only!
244 p_dest += ( x < 0 ? 0 : x ) * 2 + ( y < 0 ? 0 : y ) * stride_dest;
246 // offset pointer into alpha channel based upon cropping
248 p_alpha += x_src + y_src * stride_src / 2;
250 // Assuming lower field first
251 // Special care is taken to make sure the b_frame is aligned to the correct field.
252 // field 0 = lower field and y should be odd (y is 0-based).
253 // field 1 = upper field and y should be even.
254 if ( ( field > -1 ) && ( y % 2 == field ) )
257 p_dest += stride_dest;
259 p_dest -= stride_dest;
262 // On the second field, use the other lines from b_frame
267 p_alpha += stride_src / 2;
274 uint8_t *z = p_alpha;
280 int step = ( field > -1 ) ? 2 : 1;
282 // now do the compositing only to cropped extents
283 for ( i = 0; i < height_src; i += step )
285 p = &p_src[ i * stride_src ];
286 q = &p_dest[ i * stride_dest ];
287 o = &p_dest[ i * stride_dest ];
289 z = &p_alpha[ i * stride_src / 2 ];
291 for ( j = 0; j < width_src; j ++ )
295 a = ( z == NULL ) ? 255 : *z ++;
296 value = ( weight * ( float ) a / 255.0 );
297 *o ++ = (uint8_t)( Y * value + *q++ * ( 1 - value ) );
298 *o ++ = (uint8_t)( UV * value + *q++ * ( 1 - value ) );
306 /** Get the properly sized image from b_frame.
309 static int get_b_frame_image( mlt_frame b_frame, uint8_t **image, int *width, int *height, struct geometry_s *geometry )
312 mlt_image_format format = mlt_image_yuv422;
314 // Initialise the scaled dimensions from the computed
315 geometry->sw = geometry->w;
316 geometry->sh = geometry->h;
318 // Compute the dimensioning rectangle
319 mlt_properties b_props = mlt_frame_properties( b_frame );
320 mlt_transition this = mlt_properties_get_data( b_props, "transition_composite", NULL );
321 mlt_properties properties = mlt_transition_properties( this );
323 if ( mlt_properties_get( properties, "distort" ) == NULL )
325 // Now do additional calcs based on real_width/height etc
326 int normalised_width = geometry->w;
327 int normalised_height = geometry->h;
328 //int real_width = get_value( b_props, "real_width", "width" );
329 int real_height = get_value( b_props, "real_height", "height" );
330 double input_ar = mlt_frame_get_aspect_ratio( b_frame );
331 double output_ar = mlt_properties_get_double( b_props, "consumer_aspect_ratio" );
332 //int scaled_width = ( input_ar > output_ar ? input_ar / output_ar : output_ar / input_ar ) * real_width;
333 //int scaled_height = ( input_ar > output_ar ? input_ar / output_ar : output_ar / input_ar ) * real_height;
334 int scaled_width = ( float )geometry->nw / geometry->nh / output_ar * real_height * input_ar;
335 int scaled_height = real_height;
336 //fprintf( stderr, "composite: real %dx%d scaled %dx%d normalised %dx%d\n", real_width, real_height, scaled_width, scaled_height, normalised_width, normalised_height );
338 // Now ensure that our images fit in the normalised frame
339 if ( scaled_width > normalised_width )
341 scaled_height = scaled_height * normalised_width / scaled_width;
342 scaled_width = normalised_width;
344 if ( scaled_height > normalised_height )
346 scaled_width = scaled_width * normalised_height / scaled_height;
347 scaled_height = normalised_height;
351 if ( scaled_height == normalised_height )
352 scaled_width = normalised_width;
354 // Now we need to align to the geometry
355 if ( scaled_width <= geometry->w && scaled_height <= geometry->h )
357 // Save the new scaled dimensions
358 geometry->sw = scaled_width;
359 geometry->sh = scaled_height;
361 mlt_properties_set( b_props, "distort", "true" );
365 mlt_properties_set( b_props, "distort", "true" );
370 // We want to ensure that we bypass resize now...
371 mlt_properties_set( b_props, "distort", "true" );
374 // Take into consideration alignment for optimisation
375 alignment_calculate( geometry );
377 // Adjust to consumer scale
378 int x = geometry->x * *width / geometry->nw + 0.5;
379 int y = geometry->y * *height / geometry->nh + 0.5;
380 *width = geometry->sw * *width / geometry->nw;
381 *height = geometry->sh * *height / geometry->nh;
385 //fprintf( stderr, "composite calculated %d,%d:%dx%d\n", x, y, *width, *height );
387 // optimization points - no work to do
388 if ( *width <= 0 || *height <= 0 )
391 if ( ( x < 0 && -x >= *width ) || ( y < 0 && -y >= *height ) )
394 ret = mlt_frame_get_image( b_frame, image, &format, width, height, 1 /* writable */ );
403 static int transition_get_image( mlt_frame a_frame, uint8_t **image, mlt_image_format *format, int *width, int *height, int writable )
405 // Get the b frame from the stack
406 mlt_frame b_frame = mlt_frame_pop_frame( a_frame );
408 // This compositer is yuv422 only
409 *format = mlt_image_yuv422;
411 // Get the image from the a frame
412 mlt_frame_get_image( a_frame, image, format, width, height, 1 );
414 if ( b_frame != NULL )
416 // Get the properties of the a frame
417 mlt_properties a_props = mlt_frame_properties( a_frame );
419 // Get the properties of the b frame
420 mlt_properties b_props = mlt_frame_properties( b_frame );
422 // Get the transition from the b frame
423 mlt_transition this = mlt_properties_get_data( b_props, "transition_composite", NULL );
425 // Get the properties from the transition
426 mlt_properties properties = mlt_transition_properties( this );
428 // Structures for geometry
429 struct geometry_s result;
430 struct geometry_s start;
431 struct geometry_s end;
433 // Calculate the position
434 float position = position_calculate( this, a_frame );
435 float delta = delta_calculate( this, a_frame );
437 // Obtain the normalised width and height from the a_frame
438 int normalised_width = mlt_properties_get_int( a_props, "normalised_width" );
439 int normalised_height = mlt_properties_get_int( a_props, "normalised_height" );
441 // Now parse the geometries
442 geometry_parse( &start, NULL, mlt_properties_get( properties, "start" ), normalised_width, normalised_height );
443 geometry_parse( &end, &start, mlt_properties_get( properties, "end" ), normalised_width, normalised_height );
445 // Now parse the alignment
446 result.halign = alignment_parse( mlt_properties_get( properties, "halign" ) );
447 result.valign = alignment_parse( mlt_properties_get( properties, "valign" ) );
449 // Since we are the consumer of the b_frame, we must pass along these
450 // consumer properties from the a_frame
451 mlt_properties_set_double( b_props, "consumer_aspect_ratio", mlt_properties_get_double( a_props, "consumer_aspect_ratio" ) );
452 mlt_properties_set_double( b_props, "consumer_scale", mlt_properties_get_double( a_props, "consumer_scale" ) );
454 // Do the calculation
455 geometry_calculate( &result, &start, &end, position );
457 // Get the image from the b frame
459 int width_b = *width;
460 int height_b = *height;
462 if ( get_b_frame_image( b_frame, &image_b, &width_b, &height_b, &result ) == 0 )
464 uint8_t *alpha = mlt_frame_get_alpha_mask( b_frame );
465 int progressive = mlt_properties_get_int( a_props, "progressive" ) ||
466 mlt_properties_get_int( a_props, "consumer_progressive" ) ||
467 mlt_properties_get_int( properties, "progressive" );
470 for ( field = 0; field < ( progressive ? 1 : 2 ); field++ )
472 // Assume lower field (0) first
473 float field_position = position + field * delta;
475 // Do the calculation
476 geometry_calculate( &result, &start, &end, field_position );
479 alignment_calculate( &result );
481 // Composite the b_frame on the a_frame
482 composite_yuv( *image, *width, *height, image_b, width_b, height_b, alpha, result, progressive ? -1 : field );
490 /** Composition transition processing.
493 static mlt_frame composite_process( mlt_transition this, mlt_frame a_frame, mlt_frame b_frame )
495 // Propogate the transition properties to the b frame
496 mlt_properties b_props = mlt_frame_properties( b_frame );
497 mlt_properties_set_data( b_props, "transition_composite", this, 0, NULL, NULL );
498 mlt_frame_push_get_image( a_frame, transition_get_image );
499 mlt_frame_push_frame( a_frame, b_frame );
503 /** Constructor for the filter.
506 mlt_transition transition_composite_init( char *arg )
508 mlt_transition this = calloc( sizeof( struct mlt_transition_s ), 1 );
509 if ( this != NULL && mlt_transition_init( this, NULL ) == 0 )
511 this->process = composite_process;
512 mlt_properties_set( mlt_transition_properties( this ), "start", arg != NULL ? arg : "85%,5%:10%x10%" );
513 mlt_properties_set( mlt_transition_properties( this ), "end", "" );