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
;
152 output
->y
= in
->y
+ ( out
->y
- in
->y
) * position
;
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
;
158 output
->x
= ( int )floor( output
->x
) & 0xfffffffe;
159 output
->w
= ( int )floor( output
->w
) & 0xfffffffe;
160 output
->sw
&= 0xfffffffe;
163 void transition_destroy_keys( void *arg
)
165 struct geometry_s
*ptr
= arg
;
166 struct geometry_s
*next
= NULL
;
168 while ( ptr
!= NULL
)
176 static struct geometry_s
*transition_parse_keys( mlt_transition
this, int normalised_width
, int normalised_height
)
178 // Loop variable for property interrogation
181 // Get the properties of the transition
182 mlt_properties properties
= mlt_transition_properties( this );
184 // Get the in and out position
185 mlt_position in
= mlt_transition_get_in( this );
186 mlt_position out
= mlt_transition_get_out( this );
189 struct geometry_s
*start
= calloc( 1, sizeof( struct geometry_s
) );
191 // Create the end (we always need two entries)
192 struct geometry_s
*end
= calloc( 1, sizeof( struct geometry_s
) );
195 struct geometry_s
*ptr
= start
;
197 // Parse the start property
198 geometry_parse( start
, NULL
, mlt_properties_get( properties
, "start" ), normalised_width
, normalised_height
);
200 // Parse the keys in between
201 for ( i
= 0; i
< mlt_properties_count( properties
); i
++ )
203 // Get the name of the property
204 char *name
= mlt_properties_get_name( properties
, i
);
206 // Check that it's valid
207 if ( !strncmp( name
, "key[", 4 ) )
209 // Get the value of the property
210 char *value
= mlt_properties_get_value( properties
, i
);
212 // Determine the frame number
213 int frame
= atoi( name
+ 4 );
215 // Determine the position
218 if ( frame
>= 0 && frame
< ( out
- in
) )
219 position
= ( float )frame
/ ( float )( out
- in
+ 1 );
220 else if ( frame
< 0 && - frame
< ( out
- in
) )
221 position
= ( float )( out
- in
+ frame
) / ( float )( out
- in
+ 1 );
223 // For now, we'll exclude all keys received out of order
224 if ( position
> ptr
->position
)
226 // Create a new geometry
227 struct geometry_s
*temp
= calloc( 1, sizeof( struct geometry_s
) );
229 // Parse and add to the list
230 geometry_parse( temp
, ptr
, value
, normalised_width
, normalised_height
);
232 // Assign the position
233 temp
->position
= position
;
235 // Allow the next to be appended after this one
240 fprintf( stderr
, "Key out of order - skipping %s\n", name
);
246 geometry_parse( end
, ptr
, mlt_properties_get( properties
, "end" ), normalised_width
, normalised_height
);
248 end
->position
= ( float )( out
- in
) / ( float )( out
- in
+ 1 );
252 // Assign to properties to ensure we get destroyed
253 mlt_properties_set_data( properties
, "geometries", start
, 0, transition_destroy_keys
, NULL
);
258 /** Parse the alignment properties into the geometry.
261 static int alignment_parse( char* align
)
265 if ( align
== NULL
);
266 else if ( isdigit( align
[ 0 ] ) )
268 else if ( align
[ 0 ] == 'c' || align
[ 0 ] == 'm' )
270 else if ( align
[ 0 ] == 'r' || align
[ 0 ] == 'b' )
276 /** Adjust position according to scaled size and alignment properties.
279 static void alignment_calculate( struct geometry_s
*geometry
)
281 geometry
->x
+= ( geometry
->w
- geometry
->sw
) * geometry
->halign
/ 2;
282 geometry
->y
+= ( geometry
->h
- geometry
->sh
) * geometry
->valign
/ 2;
285 /** Calculate the position for this frame.
288 static float position_calculate( mlt_transition
this, mlt_frame frame
)
290 // Get the in and out position
291 mlt_position in
= mlt_transition_get_in( this );
292 mlt_position out
= mlt_transition_get_out( this );
295 mlt_position position
= mlt_frame_get_position( frame
);
298 return ( float )( position
- in
) / ( float )( out
- in
+ 1 );
301 /** Calculate the field delta for this frame - position between two frames.
304 static inline float delta_calculate( mlt_transition
this, mlt_frame frame
)
306 // Get the in and out position
307 mlt_position in
= mlt_transition_get_in( this );
308 mlt_position out
= mlt_transition_get_out( this );
310 // Get the position of the frame
311 mlt_position position
= mlt_frame_get_position( frame
);
314 float x
= ( float )( position
- in
) / ( float )( out
- in
+ 1 );
315 float y
= ( float )( position
+ 1 - in
) / ( float )( out
- in
+ 1 );
317 return ( y
- x
) / 2.0;
320 static int get_value( mlt_properties properties
, char *preferred
, char *fallback
)
322 int value
= mlt_properties_get_int( properties
, preferred
);
324 value
= mlt_properties_get_int( properties
, fallback
);
328 /** Composite function.
331 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
)
335 int x_src
= 0, y_src
= 0;
336 int32_t weight
= ( 1 << 16 ) * ( geometry
.mix
/ 100 );
337 int stride_src
= width_src
* bpp
;
338 int stride_dest
= width_dest
* bpp
;
340 // Adjust to consumer scale
341 int x
= geometry
.x
* width_dest
/ geometry
.nw
;
342 int y
= geometry
.y
* height_dest
/ geometry
.nh
;
345 width_src
&= 0xfffffffe;
347 // optimization points - no work to do
348 if ( width_src
<= 0 || height_src
<= 0 )
351 if ( ( x
< 0 && -x
>= width_src
) || ( y
< 0 && -y
>= height_src
) )
354 // crop overlay off the left edge of frame
362 // crop overlay beyond right edge of frame
363 else if ( x
+ width_src
> width_dest
)
364 width_src
= width_dest
- x
;
366 // crop overlay off the top edge of the frame
372 // crop overlay below bottom edge of frame
373 else if ( y
+ height_src
> height_dest
)
374 height_src
= height_dest
- y
;
376 // offset pointer into overlay buffer based on cropping
377 p_src
+= x_src
* bpp
+ y_src
* stride_src
;
379 // offset pointer into frame buffer based upon positive coordinates only!
380 p_dest
+= ( x
< 0 ?
0 : x
) * bpp
+ ( y
< 0 ?
0 : y
) * stride_dest
;
382 // offset pointer into alpha channel based upon cropping
384 p_alpha
+= x_src
+ y_src
* stride_src
/ bpp
;
386 // Assuming lower field first
387 // Special care is taken to make sure the b_frame is aligned to the correct field.
388 // field 0 = lower field and y should be odd (y is 0-based).
389 // field 1 = upper field and y should be even.
390 if ( ( field
> -1 ) && ( y
% 2 == field
) )
392 //fprintf( stderr, "field %d y %d\n", field, y );
393 if ( ( field
== 1 && y
< height_dest
- 1 ) || ( field
== 0 && y
== 0 ) )
394 p_dest
+= stride_dest
;
396 p_dest
-= stride_dest
;
399 // On the second field, use the other lines from b_frame
404 p_alpha
+= stride_src
/ bpp
;
411 uint8_t *z
= p_alpha
;
415 int step
= ( field
> -1 ) ?
2 : 1;
417 stride_src
= stride_src
* step
;
418 int alpha_stride
= stride_src
/ bpp
;
419 stride_dest
= stride_dest
* step
;
421 // now do the compositing only to cropped extents
422 for ( i
= 0; i
< height_src
; i
+= step
)
429 for ( j
= 0; j
< width_src
; j
++ )
431 a
= ( z
== NULL
) ?
255 : *z
++;
432 value
= ( weight
* ( a
+ 1 ) ) >> 8;
433 *o
++ = ( *p
++ * value
+ *q
++ * ( ( 1 << 16 ) - value
) ) >> 16;
434 *o
++ = ( *p
++ * value
+ *q
++ * ( ( 1 << 16 ) - value
) ) >> 16;
438 p_dest
+= stride_dest
;
440 p_alpha
+= alpha_stride
;
447 /** Get the properly sized image from b_frame.
450 static int get_b_frame_image( mlt_transition
this, mlt_frame b_frame
, uint8_t **image
, int *width
, int *height
, struct geometry_s
*geometry
)
453 mlt_image_format format
= mlt_image_yuv422
;
455 // Get the properties objects
456 mlt_properties b_props
= mlt_frame_properties( b_frame
);
457 mlt_properties properties
= mlt_transition_properties( this );
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
;
502 geometry
->sw
= geometry
->w
;
503 geometry
->sh
= geometry
->h
;
506 // We want to ensure that we bypass resize now...
507 mlt_properties_set( b_props
, "distort", "true" );
509 // Take into consideration alignment for optimisation
510 alignment_calculate( geometry
);
512 // Adjust to consumer scale
513 int x
= geometry
->x
* *width
/ geometry
->nw
;
514 int y
= geometry
->y
* *height
/ geometry
->nh
;
515 *width
= geometry
->sw
* *width
/ geometry
->nw
;
516 *height
= geometry
->sh
* *height
/ geometry
->nh
;
520 // optimization points - no work to do
521 if ( *width
<= 0 || *height
<= 0 )
524 if ( ( x
< 0 && -x
>= *width
) || ( y
< 0 && -y
>= *height
) )
527 ret
= mlt_frame_get_image( b_frame
, image
, &format
, width
, height
, 1 );
533 struct geometry_s
*composite_calculate( struct geometry_s
*result
, mlt_transition
this, mlt_frame a_frame
, float position
)
535 // Get the properties from the transition
536 mlt_properties properties
= mlt_transition_properties( this );
538 // Get the properties from the frame
539 mlt_properties a_props
= mlt_frame_properties( a_frame
);
541 // Structures for geometry
542 struct geometry_s
*start
= mlt_properties_get_data( properties
, "geometries", NULL
);
544 // Now parse the geometries
547 // Obtain the normalised width and height from the a_frame
548 int normalised_width
= mlt_properties_get_int( a_props
, "normalised_width" );
549 int normalised_height
= mlt_properties_get_int( a_props
, "normalised_height" );
551 // Parse the transitions properties
552 start
= transition_parse_keys( this, normalised_width
, normalised_height
);
555 // Do the calculation
556 geometry_calculate( result
, start
, position
);
558 // Now parse the alignment
559 result
->halign
= alignment_parse( mlt_properties_get( properties
, "halign" ) );
560 result
->valign
= alignment_parse( mlt_properties_get( properties
, "valign" ) );
565 mlt_frame
composite_copy_region( mlt_transition
this, mlt_frame a_frame
)
567 // Create a frame to return
568 mlt_frame b_frame
= mlt_frame_init( );
570 // Get the properties of the a frame
571 mlt_properties a_props
= mlt_frame_properties( a_frame
);
573 // Get the properties of the b frame
574 mlt_properties b_props
= mlt_frame_properties( b_frame
);
577 float position
= position_calculate( this, a_frame
);
580 uint8_t *dest
= NULL
;
582 // Get the image and dimensions
583 uint8_t *image
= mlt_properties_get_data( a_props
, "image", NULL
);
584 int width
= mlt_properties_get_int( a_props
, "width" );
585 int height
= mlt_properties_get_int( a_props
, "height" );
587 // Pointers for copy operation
598 // Will need to know region to copy
599 struct geometry_s result
;
601 // Calculate the region now
602 composite_calculate( &result
, this, a_frame
, position
);
604 // Need to scale down to actual dimensions
605 x
= result
.x
* width
/ result
.nw
;
606 y
= result
.y
* height
/ result
.nh
;
607 w
= result
.w
* width
/ result
.nw
;
608 h
= result
.h
* height
/ result
.nh
;
613 // Now we need to create a new destination image
614 dest
= mlt_pool_alloc( w
* h
* 2 );
616 // Copy the region of the image
617 p
= image
+ y
* width
* 2 + x
* 2;
619 r
= dest
+ w
* h
* 2;
623 memcpy( q
, p
, w
* 2 );
628 // Assign to the new frame
629 mlt_properties_set_data( b_props
, "image", dest
, w
* h
* 2, mlt_pool_release
, NULL
);
630 mlt_properties_set_int( b_props
, "width", w
);
631 mlt_properties_set_int( b_props
, "height", h
);
640 static int transition_get_image( mlt_frame a_frame
, uint8_t **image
, mlt_image_format
*format
, int *width
, int *height
, int writable
)
642 // Get the b frame from the stack
643 mlt_frame b_frame
= mlt_frame_pop_frame( a_frame
);
645 // Get the transition from the a frame
646 mlt_transition
this = mlt_frame_pop_service( a_frame
);
648 // This compositer is yuv422 only
649 *format
= mlt_image_yuv422
;
651 // Get the image from the a frame
652 mlt_frame_get_image( a_frame
, image
, format
, width
, height
, 1 );
654 if ( b_frame
!= NULL
)
656 // Get the properties of the a frame
657 mlt_properties a_props
= mlt_frame_properties( a_frame
);
659 // Get the properties of the b frame
660 mlt_properties b_props
= mlt_frame_properties( b_frame
);
662 // Get the properties from the transition
663 mlt_properties properties
= mlt_transition_properties( this );
665 // Structures for geometry
666 struct geometry_s result
;
668 // Calculate the position
669 float position
= mlt_properties_get_double( b_props
, "relative_position" );
670 float delta
= delta_calculate( this, a_frame
);
672 // Do the calculation
673 struct geometry_s
*start
= composite_calculate( &result
, this, a_frame
, position
);
675 // Since we are the consumer of the b_frame, we must pass along these
676 // consumer properties from the a_frame
677 mlt_properties_set_double( b_props
, "consumer_aspect_ratio", mlt_properties_get_double( a_props
, "consumer_aspect_ratio" ) );
678 mlt_properties_set_double( b_props
, "consumer_scale", mlt_properties_get_double( a_props
, "consumer_scale" ) );
680 // Get the image from the b frame
681 uint8_t *image_b
= NULL
;
682 int width_b
= *width
;
683 int height_b
= *height
;
685 if ( get_b_frame_image( this, b_frame
, &image_b
, &width_b
, &height_b
, &result
) == 0 )
687 uint8_t *dest
= *image
;
688 uint8_t *src
= image_b
;
690 uint8_t *alpha
= mlt_frame_get_alpha_mask( b_frame
);
691 int progressive
= mlt_properties_get_int( a_props
, "progressive" ) ||
692 mlt_properties_get_int( a_props
, "consumer_progressive" ) ||
693 mlt_properties_get_int( properties
, "progressive" );
696 for ( field
= 0; field
< ( progressive ?
1 : 2 ); field
++ )
698 // Assume lower field (0) first
699 float field_position
= position
+ field
* delta
;
701 // Do the calculation if we need to
702 geometry_calculate( &result
, start
, field_position
);
705 alignment_calculate( &result
);
707 // Composite the b_frame on the a_frame
708 composite_yuv( dest
, *width
, *height
, bpp
, src
, width_b
, height_b
, alpha
, result
, progressive ?
-1 : field
);
716 /** Composition transition processing.
719 static mlt_frame
composite_process( mlt_transition
this, mlt_frame a_frame
, mlt_frame b_frame
)
721 // Propogate the transition properties to the b frame
722 mlt_properties_set_double( mlt_frame_properties( b_frame
), "relative_position", position_calculate( this, a_frame
) );
723 mlt_frame_push_service( a_frame
, this );
724 mlt_frame_push_get_image( a_frame
, transition_get_image
);
725 mlt_frame_push_frame( a_frame
, b_frame
);
729 /** Constructor for the filter.
732 mlt_transition
transition_composite_init( char *arg
)
734 mlt_transition
this = calloc( sizeof( struct mlt_transition_s
), 1 );
735 if ( this != NULL
&& mlt_transition_init( this, NULL
) == 0 )
737 this->process
= composite_process
;
738 mlt_properties_set( mlt_transition_properties( this ), "start", arg
!= NULL ? arg
: "85%,5%:10%x10%" );