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>
36 int nw
; // normalised width
37 int nh
; // normalised height
38 int sw
; // scaled width, not including consumer scale based upon w/nw
39 int sh
; // scaled height, not including consumer scale based upon h/nh
44 int halign
; // horizontal alignment: 0=left, 1=center, 2=right
45 int valign
; // vertical alignment: 0=top, 1=middle, 2=bottom
47 struct geometry_s
*next
;
50 /** Parse a value from a geometry string.
53 static float parse_value( char **ptr
, int normalisation
, char delim
, float defaults
)
55 float value
= defaults
;
57 if ( *ptr
!= NULL
&& **ptr
!= '\0' )
60 value
= strtod( *ptr
, &end
);
64 value
= ( value
/ 100.0 ) * normalisation
;
65 while ( *end
== delim
|| *end
== '%' )
74 /** Parse a geometry property string with the syntax X,Y:WxH:MIX. Any value can be
75 expressed as a percentage by appending a % after the value, otherwise values are
76 assumed to be relative to the normalised dimensions of the consumer.
79 static void geometry_parse( struct geometry_s
*geometry
, struct geometry_s
*defaults
, char *property
, int nw
, int nh
)
81 // Assign normalised width and height
85 // Assign from defaults if available
86 if ( defaults
!= NULL
)
88 geometry
->x
= defaults
->x
;
89 geometry
->y
= defaults
->y
;
90 geometry
->w
= geometry
->sw
= defaults
->w
;
91 geometry
->h
= geometry
->sh
= defaults
->h
;
92 geometry
->distort
= defaults
->distort
;
93 geometry
->mix
= defaults
->mix
;
94 defaults
->next
= geometry
;
101 // Parse the geomtry string
102 if ( property
!= NULL
&& strcmp( property
, "" ) )
104 char *ptr
= property
;
105 geometry
->x
= parse_value( &ptr
, nw
, ',', geometry
->x
);
106 geometry
->y
= parse_value( &ptr
, nh
, ':', geometry
->y
);
107 geometry
->w
= geometry
->sw
= parse_value( &ptr
, nw
, 'x', geometry
->w
);
108 geometry
->h
= geometry
->sh
= parse_value( &ptr
, nh
, ':', geometry
->h
);
111 geometry
->distort
= 1;
116 geometry
->mix
= parse_value( &ptr
, 100, ' ', geometry
->mix
);
120 /** Calculate real geometry.
123 static void geometry_calculate( struct geometry_s
*output
, struct geometry_s
*in
, float position
)
125 // Search in for position
126 struct geometry_s
*out
= in
->next
;
128 while ( out
->next
!= NULL
)
130 if ( position
>= in
->position
&& position
< out
->position
)
137 position
= ( position
- in
->position
) / ( out
->position
- in
->position
);
139 // Calculate this frames geometry
142 output
->x
= in
->x
+ ( out
->x
- in
->x
) * position
+ 0.5;
143 output
->y
= in
->y
+ ( out
->y
- in
->y
) * position
+ 0.5;
144 output
->w
= in
->w
+ ( out
->w
- in
->w
) * position
;
145 output
->h
= in
->h
+ ( out
->h
- in
->h
) * position
;
146 output
->mix
= in
->mix
+ ( out
->mix
- in
->mix
) * position
;
147 output
->distort
= in
->distort
;
150 /** Parse the alignment properties into the geometry.
153 static int alignment_parse( char* align
)
157 if ( align
== NULL
);
158 else if ( isdigit( align
[ 0 ] ) )
160 else if ( align
[ 0 ] == 'c' || align
[ 0 ] == 'm' )
162 else if ( align
[ 0 ] == 'r' || align
[ 0 ] == 'b' )
168 /** Adjust position according to scaled size and alignment properties.
171 static void alignment_calculate( struct geometry_s
*geometry
)
173 geometry
->x
+= ( geometry
->w
- geometry
->sw
) * geometry
->halign
/ 2 + 0.5;
174 geometry
->y
+= ( geometry
->h
- geometry
->sh
) * geometry
->valign
/ 2 + 0.5;
177 /** Calculate the position for this frame.
180 static inline float position_calculate( mlt_transition
this, mlt_frame frame
)
182 // Get the in and out position
183 mlt_position in
= mlt_transition_get_in( this );
184 mlt_position out
= mlt_transition_get_out( this );
187 mlt_position position
= mlt_frame_get_position( frame
);
190 return ( float )( position
- in
) / ( float )( out
- in
+ 1 );
193 /** Calculate the field delta for this frame - position between two frames.
196 static inline float delta_calculate( mlt_transition
this, mlt_frame frame
)
198 // Get the in and out position
199 mlt_position in
= mlt_transition_get_in( this );
200 mlt_position out
= mlt_transition_get_out( this );
202 // Get the position of the frame
203 mlt_position position
= mlt_frame_get_position( frame
);
206 float x
= ( float )( position
- in
) / ( float )( out
- in
+ 1 );
207 float y
= ( float )( position
+ 1 - in
) / ( float )( out
- in
+ 1 );
209 return ( y
- x
) / 2.0;
212 static int get_value( mlt_properties properties
, char *preferred
, char *fallback
)
214 int value
= mlt_properties_get_int( properties
, preferred
);
216 value
= mlt_properties_get_int( properties
, fallback
);
220 /** Composite function.
223 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
)
227 int x_src
= 0, y_src
= 0;
228 int32_t weight
= ( 1 << 16 ) * ( geometry
.mix
/ 100 );
229 int stride_src
= width_src
* bpp
;
230 int stride_dest
= width_dest
* bpp
;
232 // Adjust to consumer scale
233 int x
= geometry
.x
* width_dest
/ geometry
.nw
+ 0.5;
234 int y
= geometry
.y
* height_dest
/ geometry
.nh
+ 0.5;
239 // optimization points - no work to do
240 if ( width_src
<= 0 || height_src
<= 0 )
243 if ( ( x
< 0 && -x
>= width_src
) || ( y
< 0 && -y
>= height_src
) )
246 // crop overlay off the left edge of frame
254 // crop overlay beyond right edge of frame
255 else if ( x
+ width_src
> width_dest
)
256 width_src
= width_dest
- x
;
258 // crop overlay off the top edge of the frame
264 // crop overlay below bottom edge of frame
265 else if ( y
+ height_src
> height_dest
)
266 height_src
= height_dest
- y
;
268 // offset pointer into overlay buffer based on cropping
269 p_src
+= x_src
* bpp
+ y_src
* stride_src
;
271 // offset pointer into frame buffer based upon positive coordinates only!
272 p_dest
+= ( x
< 0 ?
0 : x
) * bpp
+ ( y
< 0 ?
0 : y
) * stride_dest
;
274 // offset pointer into alpha channel based upon cropping
276 p_alpha
+= x_src
+ y_src
* stride_src
/ bpp
;
278 // Assuming lower field first
279 // Special care is taken to make sure the b_frame is aligned to the correct field.
280 // field 0 = lower field and y should be odd (y is 0-based).
281 // field 1 = upper field and y should be even.
282 if ( ( field
> -1 ) && ( y
% 2 == field
) )
284 //fprintf( stderr, "field %d y %d\n", field, y );
285 if ( ( field
== 1 && y
< height_dest
- 1 ) || ( field
== 0 && y
== 0 ) )
286 p_dest
+= stride_dest
;
288 p_dest
-= stride_dest
;
291 // On the second field, use the other lines from b_frame
296 p_alpha
+= stride_src
/ bpp
;
303 uint8_t *z
= p_alpha
;
307 int step
= ( field
> -1 ) ?
2 : 1;
309 stride_src
= stride_src
* step
;
310 int alpha_stride
= stride_src
/ bpp
;
311 stride_dest
= stride_dest
* step
;
313 // now do the compositing only to cropped extents
314 for ( i
= 0; i
< height_src
; i
+= step
)
321 for ( j
= 0; j
< width_src
; j
++ )
323 a
= ( z
== NULL
) ?
255 : *z
++;
324 value
= ( weight
* ( a
+ 1 ) ) >> 8;
325 *o
++ = ( *p
++ * value
+ *q
++ * ( ( 1 << 16 ) - value
) ) >> 16;
326 *o
++ = ( *p
++ * value
+ *q
++ * ( ( 1 << 16 ) - value
) ) >> 16;
330 p_dest
+= stride_dest
;
332 p_alpha
+= alpha_stride
;
339 /** Get the properly sized image from b_frame.
342 static int get_b_frame_image( mlt_transition
this, mlt_frame b_frame
, uint8_t **image
, int *width
, int *height
, struct geometry_s
*geometry
)
345 mlt_image_format format
= mlt_image_yuv422
;
347 // Initialise the scaled dimensions from the computed
348 geometry
->sw
= geometry
->w
;
349 geometry
->sh
= geometry
->h
;
351 // Compute the dimensioning rectangle
352 mlt_properties b_props
= mlt_frame_properties( b_frame
);
353 mlt_properties properties
= mlt_transition_properties( this );
355 if ( mlt_properties_get( properties
, "distort" ) == NULL
&& geometry
->distort
== 0 )
357 // Adjust b_frame pixel aspect
358 int normalised_width
= geometry
->w
;
359 int normalised_height
= geometry
->h
;
360 int real_width
= get_value( b_props
, "real_width", "width" );
361 int real_height
= get_value( b_props
, "real_height", "height" );
362 int scaled_width
= real_width
;
363 int scaled_height
= real_height
;
365 // Now ensure that our images fit in the normalised frame
366 if ( scaled_width
> normalised_width
)
368 scaled_height
= scaled_height
* normalised_width
/ scaled_width
;
369 scaled_width
= normalised_width
;
371 if ( scaled_height
> normalised_height
)
373 scaled_width
= scaled_width
* normalised_height
/ scaled_height
;
374 scaled_height
= normalised_height
;
377 // Now we need to align to the geometry
378 if ( scaled_width
<= geometry
->w
&& scaled_height
<= geometry
->h
)
380 // Save the new scaled dimensions
381 geometry
->sw
= scaled_width
;
382 geometry
->sh
= scaled_height
;
386 // We want to ensure that we bypass resize now...
387 mlt_properties_set( b_props
, "distort", "true" );
389 // Take into consideration alignment for optimisation
390 alignment_calculate( geometry
);
392 // Adjust to consumer scale
393 int x
= geometry
->x
* *width
/ geometry
->nw
+ 0.5;
394 int y
= geometry
->y
* *height
/ geometry
->nh
+ 0.5;
395 *width
= geometry
->sw
* *width
/ geometry
->nw
;
396 *height
= geometry
->sh
* *height
/ geometry
->nh
;
400 // optimization points - no work to do
401 if ( *width
<= 0 || *height
<= 0 )
404 if ( ( x
< 0 && -x
>= *width
) || ( y
< 0 && -y
>= *height
) )
407 ret
= mlt_frame_get_image( b_frame
, image
, &format
, width
, height
, 1 );
413 static uint8_t *transition_get_alpha_mask( mlt_frame
this )
415 // Obtain properties of frame
416 mlt_properties properties
= mlt_frame_properties( this );
418 // Return the alpha mask
419 return mlt_properties_get_data( properties
, "alpha", NULL
);
422 void transition_destroy_keys( void *arg
)
424 struct geometry_s
*ptr
= arg
;
425 struct geometry_s
*next
= NULL
;
427 while ( ptr
!= NULL
)
435 static struct geometry_s
*transition_parse_keys( mlt_transition
this, int normalised_width
, int normalised_height
)
437 // Loop variable for property interrogation
440 // Get the properties of the transition
441 mlt_properties properties
= mlt_transition_properties( this );
443 // Get the in and out position
444 mlt_position in
= mlt_transition_get_in( this );
445 mlt_position out
= mlt_transition_get_out( this );
448 struct geometry_s
*start
= calloc( 1, sizeof( struct geometry_s
) );
450 // Create the end (we always need two entries)
451 struct geometry_s
*end
= calloc( 1, sizeof( struct geometry_s
) );
454 struct geometry_s
*ptr
= start
;
456 // Parse the start property
457 geometry_parse( start
, NULL
, mlt_properties_get( properties
, "start" ), normalised_width
, normalised_height
);
459 // Parse the keys in between
460 for ( i
= 0; i
< mlt_properties_count( properties
); i
++ )
462 // Get the name of the property
463 char *name
= mlt_properties_get_name( properties
, i
);
465 // Check that it's valid
466 if ( !strncmp( name
, "key[", 4 ) )
468 // Get the value of the property
469 char *value
= mlt_properties_get_value( properties
, i
);
471 // Determine the frame number
472 int frame
= atoi( name
+ 4 );
474 // Determine the position
477 if ( frame
>= 0 && frame
< ( out
- in
) )
478 position
= ( float )frame
/ ( float )( out
- in
+ 1 );
479 else if ( frame
< 0 && - frame
< ( out
- in
) )
480 position
= ( float )( out
- in
+ frame
) / ( float )( out
- in
+ 1 );
482 // For now, we'll exclude all keys received out of order
483 if ( position
> ptr
->position
)
485 // Create a new geometry
486 struct geometry_s
*temp
= calloc( 1, sizeof( struct geometry_s
) );
488 // Parse and add to the list
489 geometry_parse( temp
, ptr
, value
, normalised_width
, normalised_height
);
491 // Assign the position
492 temp
->position
= position
;
494 // Allow the next to be appended after this one
499 fprintf( stderr
, "Key out of order - skipping %s\n", name
);
505 geometry_parse( end
, ptr
, mlt_properties_get( properties
, "end" ), normalised_width
, normalised_height
);
506 end
->position
= ( float )( out
- in
) / ( float )( out
- in
+ 1 );
508 // Assign to properties to ensure we get destroyed
509 mlt_properties_set_data( properties
, "geometries", start
, 0, transition_destroy_keys
, NULL
);
517 static int transition_get_image( mlt_frame a_frame
, uint8_t **image
, mlt_image_format
*format
, int *width
, int *height
, int writable
)
519 // Get the b frame from the stack
520 mlt_frame b_frame
= mlt_frame_pop_frame( a_frame
);
522 // This compositer is yuv422 only
523 *format
= mlt_image_yuv422
;
525 // Get the transition from the a frame
526 mlt_transition
this = mlt_frame_pop_service( a_frame
);
528 // Get the image from the a frame
529 mlt_frame_get_image( a_frame
, image
, format
, width
, height
, 1 );
531 if ( b_frame
!= NULL
)
533 // Get the properties of the a frame
534 mlt_properties a_props
= mlt_frame_properties( a_frame
);
536 // Get the properties of the b frame
537 mlt_properties b_props
= mlt_frame_properties( b_frame
);
539 // Get the properties from the transition
540 mlt_properties properties
= mlt_transition_properties( this );
542 // Structures for geometry
543 struct geometry_s result
;
544 struct geometry_s
*start
= mlt_properties_get_data( properties
, "geometries", NULL
);
546 // Calculate the position
547 float position
= mlt_properties_get_double( b_props
, "relative_position" );
548 float delta
= delta_calculate( this, a_frame
);
550 // Now parse the geometries
553 // Obtain the normalised width and height from the a_frame
554 int normalised_width
= mlt_properties_get_int( a_props
, "normalised_width" );
555 int normalised_height
= mlt_properties_get_int( a_props
, "normalised_height" );
557 // Parse the transitions properties
558 start
= transition_parse_keys( this, normalised_width
, normalised_height
);
561 // Since we are the consumer of the b_frame, we must pass along these
562 // consumer properties from the a_frame
563 mlt_properties_set_double( b_props
, "consumer_aspect_ratio", mlt_properties_get_double( a_props
, "consumer_aspect_ratio" ) );
564 mlt_properties_set_double( b_props
, "consumer_scale", mlt_properties_get_double( a_props
, "consumer_scale" ) );
566 // Do the calculation
567 geometry_calculate( &result
, start
, position
);
569 // Now parse the alignment
570 result
.halign
= alignment_parse( mlt_properties_get( properties
, "halign" ) );
571 result
.valign
= alignment_parse( mlt_properties_get( properties
, "valign" ) );
573 // Get the image from the b frame
575 int width_b
= *width
;
576 int height_b
= *height
;
578 if ( get_b_frame_image( this, b_frame
, &image_b
, &width_b
, &height_b
, &result
) == 0 )
580 uint8_t *dest
= *image
;
581 uint8_t *src
= image_b
;
583 uint8_t *alpha
= mlt_frame_get_alpha_mask( b_frame
);
584 int progressive
= mlt_properties_get_int( a_props
, "progressive" ) ||
585 mlt_properties_get_int( a_props
, "consumer_progressive" ) ||
586 mlt_properties_get_int( properties
, "progressive" );
589 for ( field
= 0; field
< ( progressive ?
1 : 2 ); field
++ )
591 // Assume lower field (0) first
592 float field_position
= position
+ field
* delta
;
594 // Do the calculation if we need to
595 geometry_calculate( &result
, start
, field_position
);
598 alignment_calculate( &result
);
600 // Composite the b_frame on the a_frame
601 composite_yuv( dest
, *width
, *height
, bpp
, src
, width_b
, height_b
, alpha
, result
, progressive ?
-1 : field
);
609 /** Composition transition processing.
612 static mlt_frame
composite_process( mlt_transition
this, mlt_frame a_frame
, mlt_frame b_frame
)
614 // Propogate the transition properties to the b frame
615 mlt_properties_set_double( mlt_frame_properties( b_frame
), "relative_position", position_calculate( this, a_frame
) );
616 mlt_frame_push_service( a_frame
, this );
617 mlt_frame_push_get_image( a_frame
, transition_get_image
);
618 mlt_frame_push_frame( a_frame
, b_frame
);
622 /** Constructor for the filter.
625 mlt_transition
transition_composite_init( char *arg
)
627 mlt_transition
this = calloc( sizeof( struct mlt_transition_s
), 1 );
628 if ( this != NULL
&& mlt_transition_init( this, NULL
) == 0 )
630 this->process
= composite_process
;
631 mlt_properties_set( mlt_transition_properties( this ), "start", arg
!= NULL ? arg
: "85%,5%:10%x10%" );