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>
41 /** Parse a value from a geometry string.
44 static float parse_value( char **ptr, int normalisation, char delim, float defaults )
46 float value = defaults;
48 if ( *ptr != NULL && **ptr != '\0' )
51 value = strtod( *ptr, &end );
55 value = ( value / 100.0 ) * normalisation;
56 while ( *end == delim || *end == '%' )
65 /** Parse a geometry property string with the syntax X,Y:WxH:MIX. Any value can be
66 expressed as a percentage by appending a % after the value, otherwise values are
67 assumed to be relative to the normalised dimensions of the consumer.
70 static void geometry_parse( struct geometry_s *geometry, struct geometry_s *defaults, char *property, int nw, int nh )
72 // Assign normalised width and height
76 // Assign from defaults if available
77 if ( defaults != NULL )
79 geometry->x = defaults->x;
80 geometry->y = defaults->y;
81 geometry->w = defaults->w;
82 geometry->h = defaults->h;
83 geometry->mix = defaults->mix;
90 // Parse the geomtry string
91 if ( property != NULL )
94 geometry->x = parse_value( &ptr, nw, ',', geometry->x );
95 geometry->y = parse_value( &ptr, nh, ':', geometry->y );
96 geometry->w = parse_value( &ptr, nw, 'x', geometry->w );
97 geometry->h = parse_value( &ptr, nh, ':', geometry->h );
98 geometry->mix = parse_value( &ptr, 100, ' ', geometry->mix );
102 /** Calculate real geometry.
105 static void geometry_calculate( struct geometry_s *output, struct geometry_s *in, struct geometry_s *out, float position )
107 // Calculate this frames geometry
110 output->x = in->x + ( out->x - in->x ) * position;
111 output->y = in->y + ( out->y - in->y ) * position;
112 output->w = in->w + ( out->w - in->w ) * position;
113 output->h = in->h + ( out->h - in->h ) * position;
114 output->mix = in->mix + ( out->mix - in->mix ) * position;
117 /** Calculate the position for this frame.
120 static float position_calculate( mlt_transition this, mlt_frame frame )
122 // Get the in and out position
123 mlt_position in = mlt_transition_get_in( this );
124 mlt_position out = mlt_transition_get_out( this );
126 // Get the position of the frame
127 mlt_position position = mlt_frame_get_position( frame );
130 return ( float )( position - in ) / ( float )( out - in + 1 );
133 static int get_value( mlt_properties properties, char *preferred, char *fallback )
135 int value = mlt_properties_get_int( properties, preferred );
137 value = mlt_properties_get_int( properties, fallback );
141 /** Composite function.
144 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 )
148 int x_src = 0, y_src = 0;
149 float weight = geometry.mix / 100;
150 int x = ( geometry.x * width_dest ) / geometry.nw;
151 int y = ( geometry.y * height_dest ) / geometry.nh;
152 int stride_src = width_src * 2;
153 int stride_dest = width_dest * 2;
157 // optimization points - no work to do
158 if ( width_src <= 0 || height_src <= 0 )
161 if ( ( x < 0 && -x >= width_src ) || ( y < 0 && -y >= height_src ) )
164 // crop overlay off the left edge of frame
172 // crop overlay beyond right edge of frame
173 else if ( x + width_src > width_dest )
174 width_src = width_dest - x;
176 // crop overlay off the top edge of the frame
182 // crop overlay below bottom edge of frame
183 else if ( y + height_src > height_dest )
184 height_src = height_dest - y;
186 // offset pointer into overlay buffer based on cropping
187 p_src += x_src * 2 + y_src * stride_src;
189 // offset pointer into frame buffer based upon positive, even coordinates only!
190 p_dest += ( x < 0 ? 0 : x ) * 2 + ( y < 0 ? 0 : y ) * stride_dest;
192 // offset pointer into alpha channel based upon cropping
194 p_alpha += x_src + y_src * stride_src / 2;
199 uint8_t *z = p_alpha;
206 // now do the compositing only to cropped extents
207 for ( i = 0; i < height_src; i++ )
214 for ( j = 0; j < width_src; j ++ )
218 a = ( z == NULL ) ? 255 : *z ++;
219 value = ( weight * ( float ) a / 255.0 );
220 *o ++ = (uint8_t)( Y * value + *q++ * ( 1 - value ) );
221 *o ++ = (uint8_t)( UV * value + *q++ * ( 1 - value ) );
225 p_dest += stride_dest;
227 p_alpha += stride_src / 2;
234 /** Get the properly sized image from b_frame.
237 static int get_b_frame_image( mlt_frame b_frame, uint8_t **image, int *width, int *height, struct geometry_s *geometry )
240 mlt_image_format format = mlt_image_yuv422;
242 // Compute the dimensioning rectangle
243 mlt_properties b_props = mlt_frame_properties( b_frame );
244 mlt_transition this = mlt_properties_get_data( b_props, "transition_composite", NULL );
245 mlt_properties properties = mlt_transition_properties( this );
247 if ( mlt_properties_get( properties, "distort" ) == NULL )
249 // Now do additional calcs based on real_width/height etc
250 //int normalised_width = mlt_properties_get_int( b_props, "normalised_width" );
251 //int normalised_height = mlt_properties_get_int( b_props, "normalised_height" );
252 int normalised_width = geometry->w;
253 int normalised_height = geometry->h;
254 int real_width = get_value( b_props, "real_width", "width" );
255 int real_height = get_value( b_props, "real_height", "height" );
256 double input_ar = mlt_frame_get_aspect_ratio( b_frame );
257 double output_ar = mlt_properties_get_double( b_props, "consumer_aspect_ratio" );
258 int scaled_width = ( input_ar > output_ar ? input_ar / output_ar : output_ar / input_ar ) * real_width;
259 int scaled_height = ( input_ar > output_ar ? input_ar / output_ar : output_ar / input_ar ) * real_height;
261 // Now ensure that our images fit in the normalised frame
262 if ( scaled_width > normalised_width )
264 scaled_height = scaled_height * normalised_width / scaled_width;
265 scaled_width = normalised_width;
267 if ( scaled_height > normalised_height )
269 scaled_width = scaled_width * normalised_height / scaled_height;
270 scaled_height = normalised_height;
274 if ( scaled_height == normalised_height )
275 scaled_width = normalised_width;
277 // Now we need to align to the geometry
278 if ( scaled_width <= geometry->w && scaled_height <= geometry->h )
280 // TODO: Should take into account requested alignment here...
281 // Assume centred alignment for now
283 geometry->x = geometry->x + ( geometry->w - scaled_width ) / 2;
284 geometry->y = geometry->y + ( geometry->h - scaled_height ) / 2;
285 geometry->w = scaled_width;
286 geometry->h = scaled_height;
287 mlt_properties_set( b_props, "distort", "true" );
291 mlt_properties_set( b_props, "distort", "true" );
296 // We want to ensure that we bypass resize now...
297 mlt_properties_set( b_props, "distort", "true" );
300 int x = ( geometry->x * *width ) / geometry->nw;
301 int y = ( geometry->y * *height ) / geometry->nh;
302 *width = ( geometry->w * *width ) / geometry->nw;
303 *height = ( geometry->h * *height ) / geometry->nh;
307 // optimization points - no work to do
308 if ( *width <= 0 || *height <= 0 )
311 if ( ( x < 0 && -x >= *width ) || ( y < 0 && -y >= *height ) )
314 ret = mlt_frame_get_image( b_frame, image, &format, width, height, 1 /* writable */ );
323 static int transition_get_image( mlt_frame a_frame, uint8_t **image, mlt_image_format *format, int *width, int *height, int writable )
325 // Get the b frame from the stack
326 mlt_frame b_frame = mlt_frame_pop_frame( a_frame );
328 // This compositer is yuv422 only
329 *format = mlt_image_yuv422;
331 // Get the image from the a frame
332 mlt_frame_get_image( a_frame, image, format, width, height, 1 );
334 if ( b_frame != NULL )
336 // Get the properties of the a frame
337 mlt_properties a_props = mlt_frame_properties( a_frame );
339 // Get the properties of the b frame
340 mlt_properties b_props = mlt_frame_properties( b_frame );
342 // Get the transition from the b frame
343 mlt_transition this = mlt_properties_get_data( b_props, "transition_composite", NULL );
345 // Get the properties from the transition
346 mlt_properties properties = mlt_transition_properties( this );
348 // Structures for geometry
349 struct geometry_s result;
350 struct geometry_s start;
351 struct geometry_s end;
353 // Calculate the position
354 float position = position_calculate( this, a_frame );
356 // Obtain the normalised width and height from the a_frame
357 int normalised_width = mlt_properties_get_int( a_props, "normalised_width" );
358 int normalised_height = mlt_properties_get_int( a_props, "normalised_height" );
360 // Now parse the geometries
361 geometry_parse( &start, NULL, mlt_properties_get( properties, "start" ), normalised_width, normalised_height );
362 geometry_parse( &end, &start, mlt_properties_get( properties, "end" ), normalised_width, normalised_height );
364 // Do the calculation
365 geometry_calculate( &result, &start, &end, position );
367 // Since we are the consumer of the b_frame, we must pass along these
368 // consumer properties from the a_frame
369 mlt_properties_set_double( b_props, "consumer_aspect_ratio", mlt_properties_get_double( a_props, "consumer_aspect_ratio" ) );
370 mlt_properties_set_double( b_props, "consumer_scale", mlt_properties_get_double( a_props, "consumer_scale" ) );
372 // Get the image from the b frame
374 int width_b = *width;
375 int height_b = *height;
376 if ( get_b_frame_image( b_frame, &image_b, &width_b, &height_b, &result ) == 0 )
378 uint8_t *alpha = mlt_frame_get_alpha_mask( b_frame );
380 // Composite the b_frame on the a_frame
381 composite_yuv( *image, *width, *height, image_b, width_b, height_b, alpha, result );
388 /** Composition transition processing.
391 static mlt_frame composite_process( mlt_transition this, mlt_frame a_frame, mlt_frame b_frame )
393 // Propogate the transition properties to the b frame
394 mlt_properties b_props = mlt_frame_properties( b_frame );
395 mlt_properties_set_data( b_props, "transition_composite", this, 0, NULL, NULL );
396 mlt_frame_push_get_image( a_frame, transition_get_image );
397 mlt_frame_push_frame( a_frame, b_frame );
401 /** Constructor for the filter.
404 mlt_transition transition_composite_init( char *arg )
406 mlt_transition this = calloc( sizeof( struct mlt_transition_s ), 1 );
407 if ( this != NULL && mlt_transition_init( this, NULL ) == 0 )
409 this->process = composite_process;
410 mlt_properties_set( mlt_transition_properties( this ), "start", arg != NULL ? arg : "85%,5%:10%x10%" );
411 mlt_properties_set( mlt_transition_properties( this ), "end", "" );