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.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_position position
)
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 return ( float )( position
- in
) / ( float )( out
- in
+ 1 );
298 /** Calculate the field delta for this frame - position between two frames.
301 static inline float delta_calculate( mlt_transition
this, mlt_frame frame
)
303 // Get the in and out position
304 mlt_position in
= mlt_transition_get_in( this );
305 mlt_position out
= mlt_transition_get_out( this );
307 // Get the position of the frame
308 mlt_position position
= mlt_frame_get_position( frame
);
311 float x
= ( float )( position
- in
) / ( float )( out
- in
+ 1 );
312 float y
= ( float )( position
+ 1 - in
) / ( float )( out
- in
+ 1 );
314 return ( y
- x
) / 2.0;
317 static int get_value( mlt_properties properties
, char *preferred
, char *fallback
)
319 int value
= mlt_properties_get_int( properties
, preferred
);
321 value
= mlt_properties_get_int( properties
, fallback
);
325 /** A linear threshold determination function.
328 static inline int32_t linearstep( int32_t edge1
, int32_t edge2
, int32_t a
)
336 return ( ( a
- edge1
) << 16 ) / ( edge2
- edge1
);
339 /** A smoother, non-linear threshold determination function.
342 static inline int32_t smoothstep( int32_t edge1
, int32_t edge2
, uint32_t a
)
350 a
= ( ( a
- edge1
) << 16 ) / ( edge2
- edge1
);
352 return ( ( ( a
* a
) >> 16 ) * ( ( 3 << 16 ) - ( 2 * a
) ) ) >> 16;
355 /** Load the luma map from PGM stream.
358 static void luma_read_pgm( FILE *f
, uint16_t **map
, int *width
, int *height
)
360 uint8_t *data
= NULL
;
372 // get the magic code
373 if ( fgets( line
, 127, f
) == NULL
)
377 while ( sscanf( line
, " #%s", comment
) > 0 )
378 if ( fgets( line
, 127, f
) == NULL
)
381 if ( line
[0] != 'P' || line
[1] != '5' )
384 // skip white space and see if a new line must be fetched
385 for ( i
= 2; i
< 127 && line
[i
] != '\0' && isspace( line
[i
] ); i
++ );
386 if ( ( line
[i
] == '\0' || line
[i
] == '#' ) && fgets( line
, 127, f
) == NULL
)
390 while ( sscanf( line
, " #%s", comment
) > 0 )
391 if ( fgets( line
, 127, f
) == NULL
)
394 // get the dimensions
395 if ( line
[0] == 'P' )
396 i
= sscanf( line
, "P5 %d %d %d", width
, height
, &maxval
);
398 i
= sscanf( line
, "%d %d %d", width
, height
, &maxval
);
400 // get the height value, if not yet
403 if ( fgets( line
, 127, f
) == NULL
)
407 while ( sscanf( line
, " #%s", comment
) > 0 )
408 if ( fgets( line
, 127, f
) == NULL
)
411 i
= sscanf( line
, "%d", height
);
418 // get the maximum gray value, if not yet
421 if ( fgets( line
, 127, f
) == NULL
)
425 while ( sscanf( line
, " #%s", comment
) > 0 )
426 if ( fgets( line
, 127, f
) == NULL
)
429 i
= sscanf( line
, "%d", &maxval
);
434 // determine if this is one or two bytes per pixel
435 bpp
= maxval
> 255 ?
2 : 1;
437 // allocate temporary storage for the raw data
438 data
= mlt_pool_alloc( *width
* *height
* bpp
);
443 if ( fread( data
, *width
* *height
* bpp
, 1, f
) != 1 )
446 // allocate the luma bitmap
447 *map
= p
= (uint16_t*)mlt_pool_alloc( *width
* *height
* sizeof( uint16_t ) );
451 // proces the raw data into the luma bitmap
452 for ( i
= 0; i
< *width
* *height
* bpp
; i
+= bpp
)
455 *p
++ = data
[ i
] << 8;
457 *p
++ = ( data
[ i
] << 8 ) + data
[ i
+1 ];
464 mlt_pool_release( data
);
467 /** Generate a luma map from any YUV image.
470 static void luma_read_yuv422( uint8_t *image
, uint16_t **map
, int width
, int height
)
474 // allocate the luma bitmap
475 uint16_t *p
= *map
= ( uint16_t* )mlt_pool_alloc( width
* height
* sizeof( uint16_t ) );
479 // proces the image data into the luma bitmap
480 for ( i
= 0; i
< width
* height
* 2; i
+= 2 )
481 *p
++ = ( image
[ i
] - 16 ) * 299; // 299 = 65535 / 219
484 /** Composite function.
487 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
, uint16_t *p_luma
, int32_t softness
)
491 int x_src
= 0, y_src
= 0;
492 int32_t weight
= ( 1 << 16 ) * ( geometry
.mix
/ 100 );
493 int stride_src
= width_src
* bpp
;
494 int stride_dest
= width_dest
* bpp
;
496 // Adjust to consumer scale
497 int x
= geometry
.x
* width_dest
/ geometry
.nw
;
498 int y
= geometry
.y
* height_dest
/ geometry
.nh
;
501 width_src
&= 0xfffffffe;
503 // optimization points - no work to do
504 if ( width_src
<= 0 || height_src
<= 0 )
507 if ( ( x
< 0 && -x
>= width_src
) || ( y
< 0 && -y
>= height_src
) )
510 // crop overlay off the left edge of frame
518 // crop overlay beyond right edge of frame
519 else if ( x
+ width_src
> width_dest
)
520 width_src
= width_dest
- x
;
522 // crop overlay off the top edge of the frame
528 // crop overlay below bottom edge of frame
529 else if ( y
+ height_src
> height_dest
)
530 height_src
= height_dest
- y
;
532 // offset pointer into overlay buffer based on cropping
533 p_src
+= x_src
* bpp
+ y_src
* stride_src
;
535 // offset pointer into frame buffer based upon positive coordinates only!
536 p_dest
+= ( x
< 0 ?
0 : x
) * bpp
+ ( y
< 0 ?
0 : y
) * stride_dest
;
538 // offset pointer into alpha channel based upon cropping
540 p_alpha
+= x_src
+ y_src
* stride_src
/ bpp
;
542 // offset pointer into luma channel based upon cropping
544 p_luma
+= x_src
+ y_src
* stride_src
/ bpp
;
546 // Assuming lower field first
547 // Special care is taken to make sure the b_frame is aligned to the correct field.
548 // field 0 = lower field and y should be odd (y is 0-based).
549 // field 1 = upper field and y should be even.
550 if ( ( field
> -1 ) && ( y
% 2 == field
) )
552 //fprintf( stderr, "field %d y %d\n", field, y );
553 if ( ( field
== 1 && y
< height_dest
- 1 ) || ( field
== 0 && y
== 0 ) )
554 p_dest
+= stride_dest
;
556 p_dest
-= stride_dest
;
559 // On the second field, use the other lines from b_frame
564 p_alpha
+= stride_src
/ bpp
;
571 uint16_t *l
= p_luma
;
572 uint8_t *z
= p_alpha
;
575 int32_t current_weight
;
577 int step
= ( field
> -1 ) ?
2 : 1;
579 stride_src
= stride_src
* step
;
580 int alpha_stride
= stride_src
/ bpp
;
581 stride_dest
= stride_dest
* step
;
583 // now do the compositing only to cropped extents
584 for ( i
= 0; i
< height_src
; i
+= step
)
592 for ( j
= 0; j
< width_src
; j
++ )
594 a
= ( z
== NULL
) ?
255 : *z
++;
595 current_weight
= ( l
== NULL
) ? weight
: linearstep( l
[ j
], l
[ j
] + softness
, weight
);
596 value
= ( current_weight
* ( a
+ 1 ) ) >> 8;
597 *o
++ = ( *p
++ * value
+ *q
++ * ( ( 1 << 16 ) - value
) ) >> 16;
598 *o
++ = ( *p
++ * value
+ *q
++ * ( ( 1 << 16 ) - value
) ) >> 16;
602 p_dest
+= stride_dest
;
604 p_alpha
+= alpha_stride
;
606 p_luma
+= alpha_stride
;
612 static uint16_t* get_luma( mlt_properties properties
, int width
, int height
)
614 // The cached luma map information
615 int luma_width
= mlt_properties_get_int( properties
, "_luma.width" );
616 int luma_height
= mlt_properties_get_int( properties
, "_luma.height" );
617 uint16_t *luma_bitmap
= mlt_properties_get_data( properties
, "_luma.bitmap", NULL
);
619 // If the filename property changed, reload the map
620 char *resource
= mlt_properties_get( properties
, "luma" );
622 if ( luma_bitmap
== NULL
&& resource
!= NULL
)
624 char *extension
= extension
= strrchr( resource
, '.' );
626 // See if it is a PGM
627 if ( extension
!= NULL
&& strcmp( extension
, ".pgm" ) == 0 )
630 FILE *f
= fopen( resource
, "r" );
634 luma_read_pgm( f
, &luma_bitmap
, &luma_width
, &luma_height
);
637 // Set the transition properties
638 mlt_properties_set_int( properties
, "_luma.width", luma_width
);
639 mlt_properties_set_int( properties
, "_luma.height", luma_height
);
640 mlt_properties_set_data( properties
, "_luma.bitmap", luma_bitmap
, luma_width
* luma_height
* 2, mlt_pool_release
, NULL
);
645 // Get the factory producer service
646 char *factory
= mlt_properties_get( properties
, "factory" );
648 // Create the producer
649 mlt_producer producer
= mlt_factory_producer( factory
, resource
);
652 if ( producer
!= NULL
)
654 // Get the producer properties
655 mlt_properties producer_properties
= mlt_producer_properties( producer
);
657 // Ensure that we loop
658 mlt_properties_set( producer_properties
, "eof", "loop" );
660 // Now pass all producer. properties on the transition down
661 mlt_properties_pass( producer_properties
, properties
, "luma." );
663 // We will get the alpha frame from the producer
664 mlt_frame luma_frame
= NULL
;
666 // Get the luma frame
667 if ( mlt_service_get_frame( mlt_producer_service( producer
), &luma_frame
, 0 ) == 0 )
670 mlt_image_format luma_format
= mlt_image_yuv422
;
672 // Request a luma image the size of transition image request
674 luma_height
= height
;
676 // Get image from the luma producer
677 mlt_properties_set( mlt_frame_properties( luma_frame
), "distort", "true" );
678 mlt_frame_get_image( luma_frame
, &luma_image
, &luma_format
, &luma_width
, &luma_height
, 0 );
680 // Generate the luma map
681 if ( luma_image
!= NULL
&& luma_format
== mlt_image_yuv422
)
683 luma_read_yuv422( luma_image
, &luma_bitmap
, luma_width
, luma_height
);
685 // Set the transition properties
686 mlt_properties_set_int( properties
, "_luma.width", luma_width
);
687 mlt_properties_set_int( properties
, "_luma.height", luma_height
);
688 mlt_properties_set_data( properties
, "_luma.bitmap", luma_bitmap
, luma_width
* luma_height
* 2, mlt_pool_release
, NULL
);
691 // Cleanup the luma frame
692 mlt_frame_close( luma_frame
);
695 // Cleanup the luma producer
696 mlt_producer_close( producer
);
703 /** Get the properly sized image from b_frame.
706 static int get_b_frame_image( mlt_transition
this, mlt_frame b_frame
, uint8_t **image
, int *width
, int *height
, struct geometry_s
*geometry
)
709 mlt_image_format format
= mlt_image_yuv422
;
711 // Get the properties objects
712 mlt_properties b_props
= mlt_frame_properties( b_frame
);
713 mlt_properties properties
= mlt_transition_properties( this );
715 if ( mlt_properties_get( properties
, "distort" ) == NULL
&& geometry
->distort
== 0 )
717 // Adjust b_frame pixel aspect
718 int normalised_width
= geometry
->w
;
719 int normalised_height
= geometry
->h
;
720 int real_width
= get_value( b_props
, "real_width", "width" );
721 int real_height
= get_value( b_props
, "real_height", "height" );
722 double input_ar
= mlt_frame_get_aspect_ratio( b_frame
);
723 double output_ar
= mlt_properties_get_double( b_props
, "consumer_aspect_ratio" );
724 int scaled_width
= input_ar
/ output_ar
* real_width
;
725 int scaled_height
= real_height
;
727 // Now ensure that our images fit in the normalised frame
728 if ( scaled_width
> normalised_width
)
730 scaled_height
= scaled_height
* normalised_width
/ scaled_width
;
731 scaled_width
= normalised_width
;
733 if ( scaled_height
> normalised_height
)
735 scaled_width
= scaled_width
* normalised_height
/ scaled_height
;
736 scaled_height
= normalised_height
;
739 // Now apply the fill
740 // TODO: Should combine fill/distort in one property
741 if ( mlt_properties_get( properties
, "fill" ) != NULL
)
743 scaled_width
= ( geometry
->w
/ scaled_width
) * scaled_width
;
744 scaled_height
= ( geometry
->h
/ scaled_height
) * scaled_height
;
747 // Save the new scaled dimensions
748 geometry
->sw
= scaled_width
;
749 geometry
->sh
= scaled_height
;
753 geometry
->sw
= geometry
->w
;
754 geometry
->sh
= geometry
->h
;
757 // We want to ensure that we bypass resize now...
758 mlt_properties_set( b_props
, "distort", "true" );
760 // Take into consideration alignment for optimisation
761 alignment_calculate( geometry
);
763 // Adjust to consumer scale
764 int x
= geometry
->x
* *width
/ geometry
->nw
;
765 int y
= geometry
->y
* *height
/ geometry
->nh
;
766 *width
= geometry
->sw
* *width
/ geometry
->nw
;
767 *height
= geometry
->sh
* *height
/ geometry
->nh
;
771 // optimization points - no work to do
772 if ( *width
<= 0 || *height
<= 0 )
775 if ( ( x
< 0 && -x
>= *width
) || ( y
< 0 && -y
>= *height
) )
778 ret
= mlt_frame_get_image( b_frame
, image
, &format
, width
, height
, 1 );
784 struct geometry_s
*composite_calculate( struct geometry_s
*result
, mlt_transition
this, mlt_frame a_frame
, float position
)
786 // Get the properties from the transition
787 mlt_properties properties
= mlt_transition_properties( this );
789 // Get the properties from the frame
790 mlt_properties a_props
= mlt_frame_properties( a_frame
);
792 // Structures for geometry
793 struct geometry_s
*start
= mlt_properties_get_data( properties
, "geometries", NULL
);
795 // Now parse the geometries
798 // Obtain the normalised width and height from the a_frame
799 int normalised_width
= mlt_properties_get_int( a_props
, "normalised_width" );
800 int normalised_height
= mlt_properties_get_int( a_props
, "normalised_height" );
802 // Parse the transitions properties
803 start
= transition_parse_keys( this, normalised_width
, normalised_height
);
806 // Do the calculation
807 geometry_calculate( result
, start
, position
);
809 // Now parse the alignment
810 result
->halign
= alignment_parse( mlt_properties_get( properties
, "halign" ) );
811 result
->valign
= alignment_parse( mlt_properties_get( properties
, "valign" ) );
816 mlt_frame
composite_copy_region( mlt_transition
this, mlt_frame a_frame
, mlt_position frame_position
)
818 // Create a frame to return
819 mlt_frame b_frame
= mlt_frame_init( );
821 // Get the properties of the a frame
822 mlt_properties a_props
= mlt_frame_properties( a_frame
);
824 // Get the properties of the b frame
825 mlt_properties b_props
= mlt_frame_properties( b_frame
);
828 float position
= position_calculate( this, frame_position
);
831 uint8_t *dest
= NULL
;
833 // Get the image and dimensions
834 uint8_t *image
= mlt_properties_get_data( a_props
, "image", NULL
);
835 int width
= mlt_properties_get_int( a_props
, "width" );
836 int height
= mlt_properties_get_int( a_props
, "height" );
838 // Pointers for copy operation
849 // Will need to know region to copy
850 struct geometry_s result
;
852 // Calculate the region now
853 composite_calculate( &result
, this, a_frame
, position
);
855 // Need to scale down to actual dimensions
856 x
= result
.x
* width
/ result
.nw
;
857 y
= result
.y
* height
/ result
.nh
;
858 w
= result
.w
* width
/ result
.nw
;
859 h
= result
.h
* height
/ result
.nh
;
864 // Now we need to create a new destination image
865 dest
= mlt_pool_alloc( w
* h
* 2 );
867 // Copy the region of the image
868 p
= image
+ y
* width
* 2 + x
* 2;
870 r
= dest
+ w
* h
* 2;
874 memcpy( q
, p
, w
* 2 );
879 // Assign to the new frame
880 mlt_properties_set_data( b_props
, "image", dest
, w
* h
* 2, mlt_pool_release
, NULL
);
881 mlt_properties_set_int( b_props
, "width", w
);
882 mlt_properties_set_int( b_props
, "height", h
);
884 // Assign this position to the b frame
885 mlt_frame_set_position( b_frame
, frame_position
);
894 static int transition_get_image( mlt_frame a_frame
, uint8_t **image
, mlt_image_format
*format
, int *width
, int *height
, int writable
)
896 // Get the b frame from the stack
897 mlt_frame b_frame
= mlt_frame_pop_frame( a_frame
);
899 // Get the transition from the a frame
900 mlt_transition
this = mlt_frame_pop_service( a_frame
);
902 // This compositer is yuv422 only
903 *format
= mlt_image_yuv422
;
905 // Get the image from the a frame
906 mlt_frame_get_image( a_frame
, image
, format
, width
, height
, 1 );
908 if ( b_frame
!= NULL
)
910 // Get the properties of the a frame
911 mlt_properties a_props
= mlt_frame_properties( a_frame
);
913 // Get the properties of the b frame
914 mlt_properties b_props
= mlt_frame_properties( b_frame
);
916 // Get the properties from the transition
917 mlt_properties properties
= mlt_transition_properties( this );
919 // Structures for geometry
920 struct geometry_s result
;
922 // Calculate the position
923 float position
= mlt_properties_get_double( b_props
, "relative_position" );
924 float delta
= delta_calculate( this, a_frame
);
926 // Do the calculation
927 struct geometry_s
*start
= composite_calculate( &result
, this, a_frame
, position
);
929 // Optimisation - no compositing required
930 if ( result
.mix
== 0 )
933 // Since we are the consumer of the b_frame, we must pass along these
934 // consumer properties from the a_frame
935 mlt_properties_set_double( b_props
, "consumer_aspect_ratio", mlt_properties_get_double( a_props
, "consumer_aspect_ratio" ) );
937 // Get the image from the b frame
938 uint8_t *image_b
= NULL
;
939 int width_b
= *width
;
940 int height_b
= *height
;
942 if ( get_b_frame_image( this, b_frame
, &image_b
, &width_b
, &height_b
, &result
) == 0 )
944 uint8_t *dest
= *image
;
945 uint8_t *src
= image_b
;
947 uint8_t *alpha
= mlt_frame_get_alpha_mask( b_frame
);
948 int progressive
= mlt_properties_get_int( a_props
, "progressive" ) ||
949 mlt_properties_get_int( a_props
, "consumer_progressive" ) ||
950 mlt_properties_get_int( properties
, "progressive" );
953 int32_t luma_softness
= mlt_properties_get_double( properties
, "softness" ) * ( 1 << 16 );
954 uint16_t *luma_bitmap
= get_luma( properties
, width_b
, height_b
);
956 for ( field
= 0; field
< ( progressive ?
1 : 2 ); field
++ )
958 // Assume lower field (0) first
959 float field_position
= position
+ field
* delta
;
961 // Do the calculation if we need to
962 geometry_calculate( &result
, start
, field_position
);
965 alignment_calculate( &result
);
967 // Composite the b_frame on the a_frame
968 composite_yuv( dest
, *width
, *height
, bpp
, src
, width_b
, height_b
, alpha
, result
, progressive ?
-1 : field
, luma_bitmap
, luma_softness
);
976 /** Composition transition processing.
979 static mlt_frame
composite_process( mlt_transition
this, mlt_frame a_frame
, mlt_frame b_frame
)
981 // Propogate the transition properties to the b frame
982 mlt_properties_set_double( mlt_frame_properties( b_frame
), "relative_position", position_calculate( this, mlt_frame_get_position( a_frame
) ) );
983 mlt_frame_push_service( a_frame
, this );
984 mlt_frame_push_get_image( a_frame
, transition_get_image
);
985 mlt_frame_push_frame( a_frame
, b_frame
);
989 /** Constructor for the filter.
992 mlt_transition
transition_composite_init( char *arg
)
994 mlt_transition
this = calloc( sizeof( struct mlt_transition_s
), 1 );
995 if ( this != NULL
&& mlt_transition_init( this, NULL
) == 0 )
997 this->process
= composite_process
;
998 mlt_properties_set( mlt_transition_properties( this ), "start", arg
!= NULL ? arg
: "85%,5%:10%x10%" );
1001 mlt_properties_set( mlt_transition_properties( this ), "factory", "fezzik" );