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>
30 typedef void ( *composite_line_fn
)( uint8_t *dest
, uint8_t *src
, int width_src
, uint8_t *alpha
, int weight
, uint16_t *luma
, int softness
);
32 /* mmx function declarations */
34 void composite_line_yuv_mmx( uint8_t *dest
, uint8_t *src
, int width_src
, uint8_t *alpha
, int weight
, uint16_t *luma
, int softness
);
35 int composite_have_mmx( void );
45 int nw
; // normalised width
46 int nh
; // normalised height
47 int sw
; // scaled width, not including consumer scale based upon w/nw
48 int sh
; // scaled height, not including consumer scale based upon h/nh
53 int halign
; // horizontal alignment: 0=left, 1=center, 2=right
54 int valign
; // vertical alignment: 0=top, 1=middle, 2=bottom
56 struct geometry_s
*next
;
59 /** Parse a value from a geometry string.
62 static float parse_value( char **ptr
, int normalisation
, char delim
, float defaults
)
64 float value
= defaults
;
66 if ( *ptr
!= NULL
&& **ptr
!= '\0' )
69 value
= strtod( *ptr
, &end
);
73 value
= ( value
/ 100.0 ) * normalisation
;
74 while ( *end
== delim
|| *end
== '%' )
83 /** Parse a geometry property string with the syntax X,Y:WxH:MIX. Any value can be
84 expressed as a percentage by appending a % after the value, otherwise values are
85 assumed to be relative to the normalised dimensions of the consumer.
88 static void geometry_parse( struct geometry_s
*geometry
, struct geometry_s
*defaults
, char *property
, int nw
, int nh
)
90 // Assign normalised width and height
94 // Assign from defaults if available
95 if ( defaults
!= NULL
)
97 geometry
->x
= defaults
->x
;
98 geometry
->y
= defaults
->y
;
99 geometry
->w
= geometry
->sw
= defaults
->w
;
100 geometry
->h
= geometry
->sh
= defaults
->h
;
101 geometry
->distort
= defaults
->distort
;
102 geometry
->mix
= defaults
->mix
;
103 defaults
->next
= geometry
;
110 // Parse the geomtry string
111 if ( property
!= NULL
&& strcmp( property
, "" ) )
113 char *ptr
= property
;
114 geometry
->x
= parse_value( &ptr
, nw
, ',', geometry
->x
);
115 geometry
->y
= parse_value( &ptr
, nh
, ':', geometry
->y
);
116 geometry
->w
= geometry
->sw
= parse_value( &ptr
, nw
, 'x', geometry
->w
);
117 geometry
->h
= geometry
->sh
= parse_value( &ptr
, nh
, ':', geometry
->h
);
120 geometry
->distort
= 1;
125 geometry
->mix
= parse_value( &ptr
, 100, ' ', geometry
->mix
);
129 /** Calculate real geometry.
132 static void geometry_calculate( struct geometry_s
*output
, struct geometry_s
*in
, float position
)
134 // Search in for position
135 struct geometry_s
*out
= in
->next
;
137 if ( position
>= 1.0 )
139 int section
= floor( position
);
141 if ( section
% 2 == 1 )
142 position
= 1.0 - position
;
145 while ( out
->next
!= NULL
)
147 if ( position
>= in
->position
&& position
< out
->position
)
154 position
= ( position
- in
->position
) / ( out
->position
- in
->position
);
156 // Calculate this frames geometry
159 output
->x
= in
->x
+ ( out
->x
- in
->x
) * position
;
160 output
->y
= in
->y
+ ( out
->y
- in
->y
) * position
;
161 output
->w
= in
->w
+ ( out
->w
- in
->w
) * position
;
162 output
->h
= in
->h
+ ( out
->h
- in
->h
) * position
;
163 output
->mix
= in
->mix
+ ( out
->mix
- in
->mix
) * position
;
164 output
->distort
= in
->distort
;
166 // DRD> These break on negative values. I do not think they are needed
167 // since yuv_composite takes care of YUYV group alignment
168 //output->x = ( int )floor( output->x ) & 0xfffffffe;
169 //output->w = ( int )floor( output->w ) & 0xfffffffe;
170 //output->sw &= 0xfffffffe;
173 void transition_destroy_keys( void *arg
)
175 struct geometry_s
*ptr
= arg
;
176 struct geometry_s
*next
= NULL
;
178 while ( ptr
!= NULL
)
186 static struct geometry_s
*transition_parse_keys( mlt_transition
this, int normalised_width
, int normalised_height
)
188 // Loop variable for property interrogation
191 // Get the properties of the transition
192 mlt_properties properties
= mlt_transition_properties( this );
194 // Get the in and out position
195 mlt_position in
= mlt_transition_get_in( this );
196 mlt_position out
= mlt_transition_get_out( this );
199 struct geometry_s
*start
= calloc( 1, sizeof( struct geometry_s
) );
201 // Create the end (we always need two entries)
202 struct geometry_s
*end
= calloc( 1, sizeof( struct geometry_s
) );
205 struct geometry_s
*ptr
= start
;
207 // Parse the start property
208 geometry_parse( start
, NULL
, mlt_properties_get( properties
, "start" ), normalised_width
, normalised_height
);
210 // Parse the keys in between
211 for ( i
= 0; i
< mlt_properties_count( properties
); i
++ )
213 // Get the name of the property
214 char *name
= mlt_properties_get_name( properties
, i
);
216 // Check that it's valid
217 if ( !strncmp( name
, "key[", 4 ) )
219 // Get the value of the property
220 char *value
= mlt_properties_get_value( properties
, i
);
222 // Determine the frame number
223 int frame
= atoi( name
+ 4 );
225 // Determine the position
228 if ( frame
>= 0 && frame
< ( out
- in
) )
229 position
= ( float )frame
/ ( float )( out
- in
+ 1 );
230 else if ( frame
< 0 && - frame
< ( out
- in
) )
231 position
= ( float )( out
- in
+ frame
) / ( float )( out
- in
+ 1 );
233 // For now, we'll exclude all keys received out of order
234 if ( position
> ptr
->position
)
236 // Create a new geometry
237 struct geometry_s
*temp
= calloc( 1, sizeof( struct geometry_s
) );
239 // Parse and add to the list
240 geometry_parse( temp
, ptr
, value
, normalised_width
, normalised_height
);
242 // Assign the position
243 temp
->position
= position
;
245 // Allow the next to be appended after this one
250 fprintf( stderr
, "Key out of order - skipping %s\n", name
);
256 geometry_parse( end
, ptr
, mlt_properties_get( properties
, "end" ), normalised_width
, normalised_height
);
258 end
->position
= ( float )( out
- in
) / ( float )( out
- in
+ 1 );
262 // Assign to properties to ensure we get destroyed
263 mlt_properties_set_data( properties
, "geometries", start
, 0, transition_destroy_keys
, NULL
);
268 /** Parse the alignment properties into the geometry.
271 static int alignment_parse( char* align
)
275 if ( align
== NULL
);
276 else if ( isdigit( align
[ 0 ] ) )
278 else if ( align
[ 0 ] == 'c' || align
[ 0 ] == 'm' )
280 else if ( align
[ 0 ] == 'r' || align
[ 0 ] == 'b' )
286 /** Adjust position according to scaled size and alignment properties.
289 static void alignment_calculate( struct geometry_s
*geometry
)
291 geometry
->x
+= ( geometry
->w
- geometry
->sw
) * geometry
->halign
/ 2;
292 geometry
->y
+= ( geometry
->h
- geometry
->sh
) * geometry
->valign
/ 2;
295 /** Calculate the position for this frame.
298 static float position_calculate( mlt_transition
this, mlt_position position
)
300 // Get the in and out position
301 mlt_position in
= mlt_transition_get_in( this );
302 mlt_position out
= mlt_transition_get_out( this );
305 return ( float )( position
- in
) / ( float )( out
- in
+ 1 );
308 /** Calculate the field delta for this frame - position between two frames.
311 static inline float delta_calculate( mlt_transition
this, mlt_frame frame
)
313 // Get the in and out position
314 mlt_position in
= mlt_transition_get_in( this );
315 mlt_position out
= mlt_transition_get_out( this );
317 // Get the position of the frame
318 char *name
= mlt_properties_get( mlt_transition_properties( this ), "_unique_id" );
319 mlt_position position
= mlt_properties_get_position( mlt_frame_properties( frame
), name
);
322 float x
= ( float )( position
- in
) / ( float )( out
- in
+ 1 );
323 float y
= ( float )( position
+ 1 - in
) / ( float )( out
- in
+ 1 );
325 return ( y
- x
) / 2.0;
328 static int get_value( mlt_properties properties
, char *preferred
, char *fallback
)
330 int value
= mlt_properties_get_int( properties
, preferred
);
332 value
= mlt_properties_get_int( properties
, fallback
);
336 /** A linear threshold determination function.
339 static inline int32_t linearstep( int32_t edge1
, int32_t edge2
, int32_t a
)
347 return ( ( a
- edge1
) << 16 ) / ( edge2
- edge1
);
350 /** A smoother, non-linear threshold determination function.
353 static inline int32_t smoothstep( int32_t edge1
, int32_t edge2
, uint32_t a
)
361 a
= ( ( a
- edge1
) << 16 ) / ( edge2
- edge1
);
363 return ( ( ( a
* a
) >> 16 ) * ( ( 3 << 16 ) - ( 2 * a
) ) ) >> 16;
366 /** Load the luma map from PGM stream.
369 static void luma_read_pgm( FILE *f
, uint16_t **map
, int *width
, int *height
)
371 uint8_t *data
= NULL
;
383 // get the magic code
384 if ( fgets( line
, 127, f
) == NULL
)
388 while ( sscanf( line
, " #%s", comment
) > 0 )
389 if ( fgets( line
, 127, f
) == NULL
)
392 if ( line
[0] != 'P' || line
[1] != '5' )
395 // skip white space and see if a new line must be fetched
396 for ( i
= 2; i
< 127 && line
[i
] != '\0' && isspace( line
[i
] ); i
++ );
397 if ( ( line
[i
] == '\0' || line
[i
] == '#' ) && fgets( line
, 127, f
) == NULL
)
401 while ( sscanf( line
, " #%s", comment
) > 0 )
402 if ( fgets( line
, 127, f
) == NULL
)
405 // get the dimensions
406 if ( line
[0] == 'P' )
407 i
= sscanf( line
, "P5 %d %d %d", width
, height
, &maxval
);
409 i
= sscanf( line
, "%d %d %d", width
, height
, &maxval
);
411 // get the height value, if not yet
414 if ( fgets( line
, 127, f
) == NULL
)
418 while ( sscanf( line
, " #%s", comment
) > 0 )
419 if ( fgets( line
, 127, f
) == NULL
)
422 i
= sscanf( line
, "%d", height
);
429 // get the maximum gray value, if not yet
432 if ( fgets( line
, 127, f
) == NULL
)
436 while ( sscanf( line
, " #%s", comment
) > 0 )
437 if ( fgets( line
, 127, f
) == NULL
)
440 i
= sscanf( line
, "%d", &maxval
);
445 // determine if this is one or two bytes per pixel
446 bpp
= maxval
> 255 ?
2 : 1;
448 // allocate temporary storage for the raw data
449 data
= mlt_pool_alloc( *width
* *height
* bpp
);
454 if ( fread( data
, *width
* *height
* bpp
, 1, f
) != 1 )
457 // allocate the luma bitmap
458 *map
= p
= (uint16_t*)mlt_pool_alloc( *width
* *height
* sizeof( uint16_t ) );
462 // proces the raw data into the luma bitmap
463 for ( i
= 0; i
< *width
* *height
* bpp
; i
+= bpp
)
466 *p
++ = data
[ i
] << 8;
468 *p
++ = ( data
[ i
] << 8 ) + data
[ i
+ 1 ];
475 mlt_pool_release( data
);
478 /** Generate a luma map from any YUV image.
481 static void luma_read_yuv422( uint8_t *image
, uint16_t **map
, int width
, int height
)
485 // allocate the luma bitmap
486 uint16_t *p
= *map
= ( uint16_t* )mlt_pool_alloc( width
* height
* sizeof( uint16_t ) );
490 // proces the image data into the luma bitmap
491 for ( i
= 0; i
< width
* height
* 2; i
+= 2 )
492 *p
++ = ( image
[ i
] - 16 ) * 299; // 299 = 65535 / 219
496 /** Composite a source line over a destination line
500 void composite_line_yuv( uint8_t *dest
, uint8_t *src
, int width_src
, uint8_t *alpha
, int weight
, uint16_t *luma
, int softness
)
505 for ( j
= 0; j
< width_src
; j
++ )
507 a
= ( alpha
== NULL
) ?
255 : *alpha
++;
508 mix
= ( luma
== NULL
) ? weight
: linearstep( luma
[ j
], luma
[ j
] + softness
, weight
);
509 mix
= ( mix
* ( a
+ 1 ) ) >> 8;
510 *dest
= ( *src
++ * mix
+ *dest
* ( ( 1 << 16 ) - mix
) ) >> 16;
512 *dest
= ( *src
++ * mix
+ *dest
* ( ( 1 << 16 ) - mix
) ) >> 16;
517 /** Composite function.
520 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
, int field
, uint16_t *p_luma
, int32_t softness
, composite_line_fn line_fn
)
524 int x_src
= 0, y_src
= 0;
525 int32_t weight
= ( 1 << 16 ) * ( geometry
.mix
/ 100 );
526 int step
= ( field
> -1 ) ?
2 : 1;
528 int stride_src
= width_src
* bpp
;
529 int stride_dest
= width_dest
* bpp
;
531 // Adjust to consumer scale
532 int x
= geometry
.x
* width_dest
/ geometry
.nw
;
533 int y
= geometry
.y
* height_dest
/ geometry
.nh
;
535 // Align x to a full YUYV group
537 width_src
&= 0xfffffffe;
539 // optimization points - no work to do
540 if ( width_src
<= 0 || height_src
<= 0 )
543 if ( ( x
< 0 && -x
>= width_src
) || ( y
< 0 && -y
>= height_src
) )
546 // crop overlay off the left edge of frame
554 // crop overlay beyond right edge of frame
555 if ( x
+ width_src
> width_dest
)
556 width_src
= width_dest
- x
;
558 // crop overlay off the top edge of the frame
565 // crop overlay below bottom edge of frame
566 if ( y
+ height_src
> height_dest
)
567 height_src
= height_dest
- y
;
569 // offset pointer into overlay buffer based on cropping
570 p_src
+= x_src
* bpp
+ y_src
* stride_src
;
572 // offset pointer into frame buffer based upon positive coordinates only!
573 p_dest
+= ( x
< 0 ?
0 : x
) * bpp
+ ( y
< 0 ?
0 : y
) * stride_dest
;
575 // offset pointer into alpha channel based upon cropping
577 p_alpha
+= x_src
+ y_src
* stride_src
/ bpp
;
579 // offset pointer into luma channel based upon cropping
581 p_luma
+= x_src
+ y_src
* stride_src
/ bpp
;
583 // Assuming lower field first
584 // Special care is taken to make sure the b_frame is aligned to the correct field.
585 // field 0 = lower field and y should be odd (y is 0-based).
586 // field 1 = upper field and y should be even.
587 if ( ( field
> -1 ) && ( y
% 2 == field
) )
589 //fprintf( stderr, "field %d y %d\n", field, y );
590 if ( ( field
== 1 && y
< height_dest
- 1 ) || ( field
== 0 && y
== 0 ) )
591 p_dest
+= stride_dest
;
593 p_dest
-= stride_dest
;
596 // On the second field, use the other lines from b_frame
601 p_alpha
+= stride_src
/ bpp
;
607 int alpha_stride
= stride_src
/ bpp
;
609 if ( line_fn
== NULL
)
610 line_fn
= composite_line_yuv
;
612 // now do the compositing only to cropped extents
613 for ( i
= 0; i
< height_src
; i
+= step
)
615 line_fn( p_dest
, p_src
, width_src
, p_alpha
, weight
, p_luma
, softness
);
618 p_dest
+= stride_dest
;
620 p_alpha
+= alpha_stride
;
622 p_luma
+= alpha_stride
;
629 /** Scale 16bit greyscale luma map using nearest neighbor.
633 scale_luma ( uint16_t *dest_buf
, int dest_width
, int dest_height
, const uint16_t *src_buf
, int src_width
, int src_height
)
636 register int x_step
= ( src_width
<< 16 ) / dest_width
;
637 register int y_step
= ( src_height
<< 16 ) / dest_height
;
638 register int x
, y
= 0;
640 for ( i
= 0; i
< dest_height
; i
++ )
642 const uint16_t *src
= src_buf
+ ( y
>> 16 ) * src_width
;
645 for ( j
= 0; j
< dest_width
; j
++ )
647 *dest_buf
++ = src
[ x
>> 16 ];
654 static uint16_t* get_luma( mlt_properties properties
, int width
, int height
)
656 // The cached luma map information
657 int luma_width
= mlt_properties_get_int( properties
, "_luma.width" );
658 int luma_height
= mlt_properties_get_int( properties
, "_luma.height" );
659 uint16_t *luma_bitmap
= mlt_properties_get_data( properties
, "_luma.bitmap", NULL
);
661 // If the filename property changed, reload the map
662 char *resource
= mlt_properties_get( properties
, "luma" );
664 if ( resource
!= NULL
&& ( luma_bitmap
== NULL
|| luma_width
!= width
|| luma_height
!= height
) )
666 uint16_t *orig_bitmap
= mlt_properties_get_data( properties
, "_luma.orig_bitmap", NULL
);
667 luma_width
= mlt_properties_get_int( properties
, "_luma.orig_width" );
668 luma_height
= mlt_properties_get_int( properties
, "_luma.orig_height" );
670 // Load the original luma once
671 if ( orig_bitmap
== NULL
)
673 char *extension
= extension
= strrchr( resource
, '.' );
675 // See if it is a PGM
676 if ( extension
!= NULL
&& strcmp( extension
, ".pgm" ) == 0 )
679 FILE *f
= fopen( resource
, "r" );
683 luma_read_pgm( f
, &orig_bitmap
, &luma_width
, &luma_height
);
686 // Remember the original size for subsequent scaling
687 mlt_properties_set_data( properties
, "_luma.orig_bitmap", orig_bitmap
, luma_width
* luma_height
* 2, mlt_pool_release
, NULL
);
688 mlt_properties_set_int( properties
, "_luma.orig_width", luma_width
);
689 mlt_properties_set_int( properties
, "_luma.orig_height", luma_height
);
694 // Get the factory producer service
695 char *factory
= mlt_properties_get( properties
, "factory" );
697 // Create the producer
698 mlt_producer producer
= mlt_factory_producer( factory
, resource
);
701 if ( producer
!= NULL
)
703 // Get the producer properties
704 mlt_properties producer_properties
= mlt_producer_properties( producer
);
706 // Ensure that we loop
707 mlt_properties_set( producer_properties
, "eof", "loop" );
709 // Now pass all producer. properties on the transition down
710 mlt_properties_pass( producer_properties
, properties
, "luma." );
712 // We will get the alpha frame from the producer
713 mlt_frame luma_frame
= NULL
;
715 // Get the luma frame
716 if ( mlt_service_get_frame( mlt_producer_service( producer
), &luma_frame
, 0 ) == 0 )
719 mlt_image_format luma_format
= mlt_image_yuv422
;
721 // Get image from the luma producer
722 mlt_properties_set( mlt_frame_properties( luma_frame
), "rescale.interp", "none" );
723 mlt_frame_get_image( luma_frame
, &luma_image
, &luma_format
, &luma_width
, &luma_height
, 0 );
725 // Generate the luma map
726 if ( luma_image
!= NULL
&& luma_format
== mlt_image_yuv422
)
727 luma_read_yuv422( luma_image
, &orig_bitmap
, luma_width
, luma_height
);
729 // Remember the original size for subsequent scaling
730 mlt_properties_set_data( properties
, "_luma.orig_bitmap", orig_bitmap
, luma_width
* luma_height
* 2, mlt_pool_release
, NULL
);
731 mlt_properties_set_int( properties
, "_luma.orig_width", luma_width
);
732 mlt_properties_set_int( properties
, "_luma.orig_height", luma_height
);
734 // Cleanup the luma frame
735 mlt_frame_close( luma_frame
);
738 // Cleanup the luma producer
739 mlt_producer_close( producer
);
744 luma_bitmap
= mlt_pool_alloc( width
* height
* sizeof( uint16_t ) );
745 scale_luma( luma_bitmap
, width
, height
, orig_bitmap
, luma_width
, luma_height
);
747 // Remember the scaled luma size to prevent unnecessary scaling
748 mlt_properties_set_int( properties
, "_luma.width", width
);
749 mlt_properties_set_int( properties
, "_luma.height", height
);
750 mlt_properties_set_data( properties
, "_luma.bitmap", luma_bitmap
, width
* height
* 2, mlt_pool_release
, NULL
);
755 /** Get the properly sized image from b_frame.
758 static int get_b_frame_image( mlt_transition
this, mlt_frame b_frame
, uint8_t **image
, int *width
, int *height
, struct geometry_s
*geometry
)
761 mlt_image_format format
= mlt_image_yuv422
;
763 // Get the properties objects
764 mlt_properties b_props
= mlt_frame_properties( b_frame
);
765 mlt_properties properties
= mlt_transition_properties( this );
767 if ( mlt_properties_get( properties
, "distort" ) == NULL
&& geometry
->distort
== 0 )
769 // Adjust b_frame pixel aspect
770 int normalised_width
= geometry
->w
;
771 int normalised_height
= geometry
->h
;
772 int real_width
= get_value( b_props
, "real_width", "width" );
773 int real_height
= get_value( b_props
, "real_height", "height" );
774 double input_ar
= mlt_frame_get_aspect_ratio( b_frame
);
775 double output_ar
= mlt_properties_get_double( b_props
, "consumer_aspect_ratio" );
776 int scaled_width
= input_ar
/ output_ar
* real_width
;
777 int scaled_height
= real_height
;
779 // Now ensure that our images fit in the normalised frame
780 if ( scaled_width
> normalised_width
)
782 scaled_height
= scaled_height
* normalised_width
/ scaled_width
;
783 scaled_width
= normalised_width
;
785 if ( scaled_height
> normalised_height
)
787 scaled_width
= scaled_width
* normalised_height
/ scaled_height
;
788 scaled_height
= normalised_height
;
791 // Now apply the fill
792 // TODO: Should combine fill/distort in one property
793 if ( mlt_properties_get( properties
, "fill" ) != NULL
)
795 scaled_width
= ( geometry
->w
/ scaled_width
) * scaled_width
;
796 scaled_height
= ( geometry
->h
/ scaled_height
) * scaled_height
;
799 // Save the new scaled dimensions
800 geometry
->sw
= scaled_width
;
801 geometry
->sh
= scaled_height
;
805 geometry
->sw
= geometry
->w
;
806 geometry
->sh
= geometry
->h
;
809 // We want to ensure that we bypass resize now...
810 mlt_properties_set( b_props
, "distort", "true" );
812 // Take into consideration alignment for optimisation
813 alignment_calculate( geometry
);
815 // Adjust to consumer scale
816 int x
= geometry
->x
* *width
/ geometry
->nw
;
817 int y
= geometry
->y
* *height
/ geometry
->nh
;
818 *width
= geometry
->sw
* *width
/ geometry
->nw
;
819 *height
= geometry
->sh
* *height
/ geometry
->nh
;
823 // optimization points - no work to do
824 if ( *width
< 1 || *height
< 1 )
827 if ( ( x
< 0 && -x
>= *width
) || ( y
< 0 && -y
>= *height
) )
830 ret
= mlt_frame_get_image( b_frame
, image
, &format
, width
, height
, 1 );
836 struct geometry_s
*composite_calculate( struct geometry_s
*result
, mlt_transition
this, mlt_frame a_frame
, float position
)
838 // Get the properties from the transition
839 mlt_properties properties
= mlt_transition_properties( this );
841 // Get the properties from the frame
842 mlt_properties a_props
= mlt_frame_properties( a_frame
);
844 // Structures for geometry
845 struct geometry_s
*start
= mlt_properties_get_data( properties
, "geometries", NULL
);
847 // Now parse the geometries
850 // Obtain the normalised width and height from the a_frame
851 int normalised_width
= mlt_properties_get_int( a_props
, "normalised_width" );
852 int normalised_height
= mlt_properties_get_int( a_props
, "normalised_height" );
854 // Parse the transitions properties
855 start
= transition_parse_keys( this, normalised_width
, normalised_height
);
858 // Do the calculation
859 geometry_calculate( result
, start
, position
);
861 // Now parse the alignment
862 result
->halign
= alignment_parse( mlt_properties_get( properties
, "halign" ) );
863 result
->valign
= alignment_parse( mlt_properties_get( properties
, "valign" ) );
868 mlt_frame
composite_copy_region( mlt_transition
this, mlt_frame a_frame
, mlt_position frame_position
)
870 // Create a frame to return
871 mlt_frame b_frame
= mlt_frame_init( );
873 // Get the properties of the a frame
874 mlt_properties a_props
= mlt_frame_properties( a_frame
);
876 // Get the properties of the b frame
877 mlt_properties b_props
= mlt_frame_properties( b_frame
);
880 float position
= position_calculate( this, frame_position
);
883 uint8_t *dest
= NULL
;
885 // Get the image and dimensions
886 uint8_t *image
= mlt_properties_get_data( a_props
, "image", NULL
);
887 int width
= mlt_properties_get_int( a_props
, "width" );
888 int height
= mlt_properties_get_int( a_props
, "height" );
890 // Pointers for copy operation
901 // Will need to know region to copy
902 struct geometry_s result
;
904 // Calculate the region now
905 composite_calculate( &result
, this, a_frame
, position
);
907 // Need to scale down to actual dimensions
908 x
= result
.x
* width
/ result
.nw
;
909 y
= result
.y
* height
/ result
.nh
;
910 w
= result
.w
* width
/ result
.nw
;
911 h
= result
.h
* height
/ result
.nh
;
916 // Now we need to create a new destination image
917 dest
= mlt_pool_alloc( w
* h
* 2 );
919 // Copy the region of the image
920 p
= image
+ y
* width
* 2 + x
* 2;
922 r
= dest
+ w
* h
* 2;
926 memcpy( q
, p
, w
* 2 );
931 // Assign to the new frame
932 mlt_properties_set_data( b_props
, "image", dest
, w
* h
* 2, mlt_pool_release
, NULL
);
933 mlt_properties_set_int( b_props
, "width", w
);
934 mlt_properties_set_int( b_props
, "height", h
);
936 // Assign this position to the b frame
937 mlt_frame_set_position( b_frame
, frame_position
);
946 static int transition_get_image( mlt_frame a_frame
, uint8_t **image
, mlt_image_format
*format
, int *width
, int *height
, int writable
)
948 // Get the b frame from the stack
949 mlt_frame b_frame
= mlt_frame_pop_frame( a_frame
);
951 // Get the transition from the a frame
952 mlt_transition
this = mlt_frame_pop_service( a_frame
);
954 // This compositer is yuv422 only
955 *format
= mlt_image_yuv422
;
957 // Get the image from the a frame
958 mlt_frame_get_image( a_frame
, image
, format
, width
, height
, 1 );
960 if ( b_frame
!= NULL
)
962 // Get the properties of the a frame
963 mlt_properties a_props
= mlt_frame_properties( a_frame
);
965 // Get the properties of the b frame
966 mlt_properties b_props
= mlt_frame_properties( b_frame
);
968 // Get the properties from the transition
969 mlt_properties properties
= mlt_transition_properties( this );
971 // Structures for geometry
972 struct geometry_s result
;
974 // Calculate the position
975 float position
= mlt_properties_get_double( b_props
, "relative_position" );
976 float delta
= delta_calculate( this, a_frame
);
978 // Do the calculation
979 struct geometry_s
*start
= composite_calculate( &result
, this, a_frame
, position
);
981 // Optimisation - no compositing required
982 if ( result
.mix
== 0 || ( result
.w
== 0 && result
.h
== 0 ) )
985 // Since we are the consumer of the b_frame, we must pass along these
986 // consumer properties from the a_frame
987 mlt_properties_set_double( b_props
, "consumer_aspect_ratio", mlt_properties_get_double( a_props
, "consumer_aspect_ratio" ) );
989 // Get the image from the b frame
990 uint8_t *image_b
= NULL
;
991 int width_b
= *width
;
992 int height_b
= *height
;
994 if ( get_b_frame_image( this, b_frame
, &image_b
, &width_b
, &height_b
, &result
) == 0 )
996 uint8_t *dest
= *image
;
997 uint8_t *src
= image_b
;
998 uint8_t *alpha
= mlt_frame_get_alpha_mask( b_frame
);
1000 mlt_properties_get_int( a_props
, "consumer_progressive" ) ||
1001 mlt_properties_get_int( properties
, "progressive" );
1004 int32_t luma_softness
= mlt_properties_get_double( properties
, "softness" ) * ( 1 << 16 );
1005 uint16_t *luma_bitmap
= get_luma( properties
, width_b
, height_b
);
1006 composite_line_fn line_fn
= mlt_properties_get_int( properties
, "_MMX" ) ? composite_line_yuv_mmx
: composite_line_yuv
;
1008 for ( field
= 0; field
< ( progressive ?
1 : 2 ); field
++ )
1010 // Assume lower field (0) first
1011 float field_position
= position
+ field
* delta
;
1013 // Do the calculation if we need to
1014 geometry_calculate( &result
, start
, field_position
);
1017 alignment_calculate( &result
);
1019 // Composite the b_frame on the a_frame
1020 composite_yuv( dest
, *width
, *height
, src
, width_b
, height_b
, alpha
, result
, progressive ?
-1 : field
, luma_bitmap
, luma_softness
, line_fn
);
1028 /** Composition transition processing.
1031 static mlt_frame
composite_process( mlt_transition
this, mlt_frame a_frame
, mlt_frame b_frame
)
1033 // Get a unique name to store the frame position
1034 char *name
= mlt_properties_get( mlt_transition_properties( this ), "_unique_id" );
1036 // Assign the current position to the name
1037 mlt_properties_set_position( mlt_frame_properties( a_frame
), name
, mlt_frame_get_position( a_frame
) );
1039 // Propogate the transition properties to the b frame
1040 mlt_properties_set_double( mlt_frame_properties( b_frame
), "relative_position", position_calculate( this, mlt_frame_get_position( a_frame
) ) );
1042 mlt_frame_push_service( a_frame
, this );
1043 mlt_frame_push_frame( a_frame
, b_frame
);
1044 mlt_frame_push_get_image( a_frame
, transition_get_image
);
1048 /** Constructor for the filter.
1051 mlt_transition
transition_composite_init( char *arg
)
1053 mlt_transition
this = calloc( sizeof( struct mlt_transition_s
), 1 );
1054 if ( this != NULL
&& mlt_transition_init( this, NULL
) == 0 )
1056 mlt_properties properties
= mlt_transition_properties( this );
1058 this->process
= composite_process
;
1060 // Default starting motion and zoom
1061 mlt_properties_set( properties
, "start", arg
!= NULL ? arg
: "85%,5%:10%x10%" );
1064 mlt_properties_set( properties
, "factory", "fezzik" );
1067 //mlt_properties_set_int( properties, "_MMX", composite_have_mmx() );