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>
35 int nw; // normalised width
36 int nh; // normalised height
37 int sw; // scaled width, not including consumer scale based upon w/nw
38 int sh; // scaled height, not including consumer scale based upon h/nh
43 int halign; // horizontal alignment: 0=left, 1=center, 2=right
44 int valign; // vertical alignment: 0=top, 1=middle, 2=bottom
47 /** Parse a value from a geometry string.
50 static float parse_value( char **ptr, int normalisation, char delim, float defaults )
52 float value = defaults;
54 if ( *ptr != NULL && **ptr != '\0' )
57 value = strtod( *ptr, &end );
61 value = ( value / 100.0 ) * normalisation;
62 while ( *end == delim || *end == '%' )
71 /** Parse a geometry property string with the syntax X,Y:WxH:MIX. Any value can be
72 expressed as a percentage by appending a % after the value, otherwise values are
73 assumed to be relative to the normalised dimensions of the consumer.
76 static void geometry_parse( struct geometry_s *geometry, struct geometry_s *defaults, char *property, int nw, int nh )
78 memset( geometry, 0, sizeof( struct geometry_s ) );
80 // Assign normalised width and height
84 // Assign from defaults if available
85 if ( defaults != NULL )
87 geometry->x = defaults->x;
88 geometry->y = defaults->y;
89 geometry->w = geometry->sw = defaults->w;
90 geometry->h = geometry->sh = defaults->h;
91 geometry->mix = defaults->mix;
98 // Parse the geomtry string
99 if ( property != NULL )
101 char *ptr = property;
102 geometry->x = parse_value( &ptr, nw, ',', geometry->x );
103 geometry->y = parse_value( &ptr, nh, ':', geometry->y );
104 geometry->w = geometry->sw = parse_value( &ptr, nw, 'x', geometry->w );
105 geometry->h = geometry->sh = parse_value( &ptr, nh, ':', geometry->h );
106 geometry->mix = parse_value( &ptr, 100, ' ', geometry->mix );
110 /** Calculate real geometry.
113 static void geometry_calculate( struct geometry_s *output, struct geometry_s *in, struct geometry_s *out, float position )
115 // Calculate this frames geometry
118 output->x = in->x + ( out->x - in->x ) * position + 0.5;
119 output->y = in->y + ( out->y - in->y ) * position + 0.5;
120 output->w = in->w + ( out->w - in->w ) * position;
121 output->h = in->h + ( out->h - in->h ) * position;
122 output->mix = in->mix + ( out->mix - in->mix ) * position;
123 if ( output->mix > 100 )
124 fprintf( stderr, "%f = %f + ( %f - %f ) * %f\n", output->mix, in->mix, out->mix, in->mix, position );
127 /** Parse the alignment properties into the geometry.
130 static int alignment_parse( char* align )
134 if ( align == NULL );
135 else if ( isdigit( align[ 0 ] ) )
137 else if ( align[ 0 ] == 'c' || align[ 0 ] == 'm' )
139 else if ( align[ 0 ] == 'r' || align[ 0 ] == 'b' )
145 /** Adjust position according to scaled size and alignment properties.
148 static void alignment_calculate( struct geometry_s *geometry )
150 geometry->x += ( geometry->w - geometry->sw ) * geometry->halign / 2 + 0.5;
151 geometry->y += ( geometry->h - geometry->sh ) * geometry->valign / 2 + 0.5;
154 /** Calculate the position for this frame.
157 static inline float position_calculate( mlt_transition this, mlt_frame frame )
159 // Get the in and out position
160 mlt_position in = mlt_transition_get_in( this );
161 mlt_position out = mlt_transition_get_out( this );
164 mlt_position position = mlt_frame_get_position( frame );
167 return ( float )( position - in ) / ( float )( out - in + 1 );
170 /** Calculate the field delta for this frame - position between two frames.
173 static inline float delta_calculate( mlt_transition this, mlt_frame frame )
175 // Get the in and out position
176 mlt_position in = mlt_transition_get_in( this );
177 mlt_position out = mlt_transition_get_out( this );
179 // Get the position of the frame
180 mlt_position position = mlt_frame_get_position( frame );
183 float x = ( float )( position - in ) / ( float )( out - in + 1 );
184 float y = ( float )( position + 1 - in ) / ( float )( out - in + 1 );
186 return ( y - x ) / 2.0;
189 static int get_value( mlt_properties properties, char *preferred, char *fallback )
191 int value = mlt_properties_get_int( properties, preferred );
193 value = mlt_properties_get_int( properties, fallback );
197 /** Composite function.
200 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 )
204 int x_src = 0, y_src = 0;
205 int32_t weight = ( 1 << 16 ) * ( geometry.mix / 100 );
206 if ( geometry.mix > 100 )
207 fprintf( stderr, "%f %d\n", geometry.mix, weight );
208 int stride_src = width_src * bpp;
209 int stride_dest = width_dest * bpp;
211 // Adjust to consumer scale
212 int x = geometry.x * width_dest / geometry.nw + 0.5;
213 int y = geometry.y * height_dest / geometry.nh + 0.5;
218 // optimization points - no work to do
219 if ( width_src <= 0 || height_src <= 0 )
222 if ( ( x < 0 && -x >= width_src ) || ( y < 0 && -y >= height_src ) )
225 // crop overlay off the left edge of frame
233 // crop overlay beyond right edge of frame
234 else if ( x + width_src > width_dest )
235 width_src = width_dest - x;
237 // crop overlay off the top edge of the frame
243 // crop overlay below bottom edge of frame
244 else if ( y + height_src > height_dest )
245 height_src = height_dest - y;
247 // offset pointer into overlay buffer based on cropping
248 p_src += x_src * bpp + y_src * stride_src;
250 // offset pointer into frame buffer based upon positive coordinates only!
251 p_dest += ( x < 0 ? 0 : x ) * bpp + ( y < 0 ? 0 : y ) * stride_dest;
253 // offset pointer into alpha channel based upon cropping
255 p_alpha += x_src + y_src * stride_src / bpp;
257 // Assuming lower field first
258 // Special care is taken to make sure the b_frame is aligned to the correct field.
259 // field 0 = lower field and y should be odd (y is 0-based).
260 // field 1 = upper field and y should be even.
261 if ( ( field > -1 ) && ( y % 2 == field ) )
263 //fprintf( stderr, "field %d y %d\n", field, y );
264 if ( ( field == 1 && y < height_dest - 1 ) || ( field == 0 && y == 0 ) )
265 p_dest += stride_dest;
267 p_dest -= stride_dest;
270 // On the second field, use the other lines from b_frame
275 p_alpha += stride_src / bpp;
282 uint8_t *z = p_alpha;
286 int step = ( field > -1 ) ? 2 : 1;
288 stride_src = stride_src * step;
289 int alpha_stride = stride_src / bpp;
290 stride_dest = stride_dest * step;
292 // now do the compositing only to cropped extents
293 for ( i = 0; i < height_src; i += step )
300 for ( j = 0; j < width_src; j ++ )
302 a = ( z == NULL ) ? 255 : *z ++;
303 value = ( weight * ( a + 1 ) ) >> 8;
304 *o ++ = ( *p++ * value + *q++ * ( ( 1 << 16 ) - value ) ) >> 16;
305 *o ++ = ( *p++ * value + *q++ * ( ( 1 << 16 ) - value ) ) >> 16;
309 p_dest += stride_dest;
311 p_alpha += alpha_stride;
318 /** Get the properly sized image from b_frame.
321 static int get_b_frame_image( mlt_transition this, mlt_frame b_frame, uint8_t **image, int *width, int *height, struct geometry_s *geometry )
324 mlt_image_format format = mlt_image_yuv422;
326 // Initialise the scaled dimensions from the computed
327 geometry->sw = geometry->w;
328 geometry->sh = geometry->h;
330 // Compute the dimensioning rectangle
331 mlt_properties b_props = mlt_frame_properties( b_frame );
332 mlt_properties properties = mlt_transition_properties( this );
334 if ( mlt_properties_get( properties, "distort" ) == NULL )
336 // Adjust b_frame pixel aspect
337 int normalised_width = geometry->w;
338 int normalised_height = geometry->h;
339 int real_width = get_value( b_props, "real_width", "width" );
340 int real_height = get_value( b_props, "real_height", "height" );
341 int scaled_width = real_width;
342 int scaled_height = real_height;
344 // Now ensure that our images fit in the normalised frame
345 if ( scaled_width > normalised_width )
347 scaled_height = scaled_height * normalised_width / scaled_width;
348 scaled_width = normalised_width;
350 if ( scaled_height > normalised_height )
352 scaled_width = scaled_width * normalised_height / scaled_height;
353 scaled_height = normalised_height;
356 // Now we need to align to the geometry
357 if ( scaled_width <= geometry->w && scaled_height <= geometry->h )
359 // Save the new scaled dimensions
360 geometry->sw = scaled_width;
361 geometry->sh = scaled_height;
365 // We want to ensure that we bypass resize now...
366 mlt_properties_set( b_props, "distort", "true" );
368 // Take into consideration alignment for optimisation
369 alignment_calculate( geometry );
371 // Adjust to consumer scale
372 int x = geometry->x * *width / geometry->nw + 0.5;
373 int y = geometry->y * *height / geometry->nh + 0.5;
374 *width = geometry->sw * *width / geometry->nw;
375 *height = geometry->sh * *height / geometry->nh;
379 // optimization points - no work to do
380 if ( *width <= 0 || *height <= 0 )
383 if ( ( x < 0 && -x >= *width ) || ( y < 0 && -y >= *height ) )
386 ret = mlt_frame_get_image( b_frame, image, &format, width, height, 1 );
392 static uint8_t *transition_get_alpha_mask( mlt_frame this )
394 // Obtain properties of frame
395 mlt_properties properties = mlt_frame_properties( this );
397 // Return the alpha mask
398 return mlt_properties_get_data( properties, "alpha", NULL );
404 static int transition_get_image( mlt_frame a_frame, uint8_t **image, mlt_image_format *format, int *width, int *height, int writable )
406 // Get the b frame from the stack
407 mlt_frame b_frame = mlt_frame_pop_frame( a_frame );
409 // This compositer is yuv422 only
410 *format = mlt_image_yuv422;
412 // Get the transition from the a frame
413 mlt_transition this = mlt_frame_pop_service( a_frame );
415 // Get the image from the a frame
416 mlt_frame_get_image( a_frame, image, format, width, height, 1 );
418 if ( b_frame != NULL )
420 // Get the properties of the a frame
421 mlt_properties a_props = mlt_frame_properties( a_frame );
423 // Get the properties of the b frame
424 mlt_properties b_props = mlt_frame_properties( b_frame );
426 // Get the properties from the transition
427 mlt_properties properties = mlt_transition_properties( this );
429 // Structures for geometry
430 struct geometry_s result;
431 struct geometry_s start;
432 struct geometry_s end;
434 // Calculate the position
435 float position = mlt_properties_get_double( b_props, "relative_position" );
436 float delta = delta_calculate( this, a_frame );
438 // Obtain the normalised width and height from the a_frame
439 int normalised_width = mlt_properties_get_int( a_props, "normalised_width" );
440 int normalised_height = mlt_properties_get_int( a_props, "normalised_height" );
442 // Now parse the geometries
443 geometry_parse( &start, NULL, mlt_properties_get( properties, "start" ), normalised_width, normalised_height );
444 geometry_parse( &end, &start, mlt_properties_get( properties, "end" ), normalised_width, normalised_height );
446 // Now parse the alignment
447 result.halign = alignment_parse( mlt_properties_get( properties, "halign" ) );
448 result.valign = alignment_parse( mlt_properties_get( properties, "valign" ) );
450 // Since we are the consumer of the b_frame, we must pass along these
451 // consumer properties from the a_frame
452 mlt_properties_set_double( b_props, "consumer_aspect_ratio", mlt_properties_get_double( a_props, "consumer_aspect_ratio" ) );
453 mlt_properties_set_double( b_props, "consumer_scale", mlt_properties_get_double( a_props, "consumer_scale" ) );
455 // Do the calculation
456 geometry_calculate( &result, &start, &end, position );
458 // Get the image from the b frame
460 int width_b = *width;
461 int height_b = *height;
463 if ( get_b_frame_image( this, b_frame, &image_b, &width_b, &height_b, &result ) == 0 )
465 uint8_t *dest = *image;
466 uint8_t *src = image_b;
468 uint8_t *alpha = mlt_frame_get_alpha_mask( b_frame );
469 int progressive = mlt_properties_get_int( a_props, "progressive" ) ||
470 mlt_properties_get_int( a_props, "consumer_progressive" ) ||
471 mlt_properties_get_int( properties, "progressive" );
474 for ( field = 0; field < ( progressive ? 1 : 2 ); field++ )
476 // Assume lower field (0) first
477 float field_position = position + field * delta;
479 // Do the calculation
480 geometry_calculate( &result, &start, &end, field_position );
483 alignment_calculate( &result );
485 // Composite the b_frame on the a_frame
486 composite_yuv( dest, *width, *height, bpp, src, width_b, height_b, alpha, result, progressive ? -1 : field );
494 /** Composition transition processing.
497 static mlt_frame composite_process( mlt_transition this, mlt_frame a_frame, mlt_frame b_frame )
499 // Propogate the transition properties to the b frame
500 mlt_properties_set_double( mlt_frame_properties( b_frame ), "relative_position", position_calculate( this, a_frame ) );
501 mlt_frame_push_service( a_frame, this );
502 mlt_frame_push_get_image( a_frame, transition_get_image );
503 mlt_frame_push_frame( a_frame, b_frame );
507 /** Constructor for the filter.
510 mlt_transition transition_composite_init( char *arg )
512 mlt_transition this = calloc( sizeof( struct mlt_transition_s ), 1 );
513 if ( this != NULL && mlt_transition_init( this, NULL ) == 0 )
515 this->process = composite_process;
516 mlt_properties_set( mlt_transition_properties( this ), "start", arg != NULL ? arg : "85%,5%:10%x10%" );
517 mlt_properties_set( mlt_transition_properties( this ), "end", "" );