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 );
46 int nw
; // normalised width
47 int nh
; // normalised height
48 int sw
; // scaled width, not including consumer scale based upon w/nw
49 int sh
; // scaled height, not including consumer scale based upon h/nh
54 int halign
; // horizontal alignment: 0=left, 1=center, 2=right
55 int valign
; // vertical alignment: 0=top, 1=middle, 2=bottom
57 struct geometry_s
*next
;
60 /** Parse a value from a geometry string.
63 static float parse_value( char **ptr
, int normalisation
, char delim
, float defaults
)
65 float value
= defaults
;
67 if ( *ptr
!= NULL
&& **ptr
!= '\0' )
70 value
= strtod( *ptr
, &end
);
74 value
= ( value
/ 100.0 ) * normalisation
;
75 while ( *end
== delim
|| *end
== '%' )
84 /** Parse a geometry property string with the syntax X,Y:WxH:MIX. Any value can be
85 expressed as a percentage by appending a % after the value, otherwise values are
86 assumed to be relative to the normalised dimensions of the consumer.
89 static void geometry_parse( struct geometry_s
*geometry
, struct geometry_s
*defaults
, char *property
, int nw
, int nh
)
91 // Assign normalised width and height
95 // Assign from defaults if available
96 if ( defaults
!= NULL
)
98 geometry
->x
= defaults
->x
;
99 geometry
->y
= defaults
->y
;
100 geometry
->w
= geometry
->sw
= defaults
->w
;
101 geometry
->h
= geometry
->sh
= defaults
->h
;
102 geometry
->distort
= defaults
->distort
;
103 geometry
->mix
= defaults
->mix
;
104 defaults
->next
= geometry
;
111 // Parse the geomtry string
112 if ( property
!= NULL
&& strcmp( property
, "" ) )
114 char *ptr
= property
;
115 geometry
->x
= parse_value( &ptr
, nw
, ',', geometry
->x
);
116 geometry
->y
= parse_value( &ptr
, nh
, ':', geometry
->y
);
117 geometry
->w
= geometry
->sw
= parse_value( &ptr
, nw
, 'x', geometry
->w
);
118 geometry
->h
= geometry
->sh
= parse_value( &ptr
, nh
, ':', geometry
->h
);
121 geometry
->distort
= 1;
126 geometry
->mix
= parse_value( &ptr
, 100, ' ', geometry
->mix
);
130 /** Calculate real geometry.
133 static void geometry_calculate( struct geometry_s
*output
, struct geometry_s
*in
, float position
)
135 // Search in for position
136 struct geometry_s
*out
= in
->next
;
138 if ( position
>= 1.0 )
140 int section
= floor( position
);
142 if ( section
% 2 == 1 )
143 position
= 1.0 - position
;
146 while ( out
->next
!= NULL
)
148 if ( position
>= in
->position
&& position
< out
->position
)
155 position
= ( position
- in
->position
) / ( out
->position
- in
->position
);
157 // Calculate this frames geometry
158 if ( in
->frame
!= out
->frame
- 1 )
162 output
->x
= in
->x
+ ( out
->x
- in
->x
) * position
;
163 output
->y
= in
->y
+ ( out
->y
- in
->y
) * position
;
164 output
->w
= in
->w
+ ( out
->w
- in
->w
) * position
;
165 output
->h
= in
->h
+ ( out
->h
- in
->h
) * position
;
166 output
->mix
= in
->mix
+ ( out
->mix
- in
->mix
) * position
;
167 output
->distort
= in
->distort
;
171 output
->nw
= out
->nw
;
172 output
->nh
= out
->nh
;
177 output
->mix
= out
->mix
;
178 output
->distort
= out
->distort
;
181 // DRD> These break on negative values. I do not think they are needed
182 // since yuv_composite takes care of YUYV group alignment
183 //output->x = ( int )floor( output->x ) & 0xfffffffe;
184 //output->w = ( int )floor( output->w ) & 0xfffffffe;
185 //output->sw &= 0xfffffffe;
188 void transition_destroy_keys( void *arg
)
190 struct geometry_s
*ptr
= arg
;
191 struct geometry_s
*next
= NULL
;
193 while ( ptr
!= NULL
)
201 static struct geometry_s
*transition_parse_keys( mlt_transition
this, int normalised_width
, int normalised_height
)
203 // Loop variable for property interrogation
206 // Get the properties of the transition
207 mlt_properties properties
= mlt_transition_properties( this );
209 // Get the in and out position
210 mlt_position in
= mlt_transition_get_in( this );
211 mlt_position out
= mlt_transition_get_out( this );
214 struct geometry_s
*start
= calloc( 1, sizeof( struct geometry_s
) );
216 // Create the end (we always need two entries)
217 struct geometry_s
*end
= calloc( 1, sizeof( struct geometry_s
) );
220 struct geometry_s
*ptr
= start
;
222 // Parse the start property
223 geometry_parse( start
, NULL
, mlt_properties_get( properties
, "start" ), normalised_width
, normalised_height
);
225 // Parse the keys in between
226 for ( i
= 0; i
< mlt_properties_count( properties
); i
++ )
228 // Get the name of the property
229 char *name
= mlt_properties_get_name( properties
, i
);
231 // Check that it's valid
232 if ( !strncmp( name
, "key[", 4 ) )
234 // Get the value of the property
235 char *value
= mlt_properties_get_value( properties
, i
);
237 // Determine the frame number
238 int frame
= atoi( name
+ 4 );
240 // Determine the position
243 if ( frame
>= 0 && frame
< ( out
- in
) )
244 position
= ( float )frame
/ ( float )( out
- in
+ 1 );
245 else if ( frame
< 0 && - frame
< ( out
- in
) )
246 position
= ( float )( out
- in
+ frame
) / ( float )( out
- in
+ 1 );
248 // For now, we'll exclude all keys received out of order
249 if ( position
> ptr
->position
)
251 // Create a new geometry
252 struct geometry_s
*temp
= calloc( 1, sizeof( struct geometry_s
) );
254 // Parse and add to the list
255 geometry_parse( temp
, ptr
, value
, normalised_width
, normalised_height
);
257 // Assign the position and frame
259 temp
->position
= position
;
261 // Allow the next to be appended after this one
266 fprintf( stderr
, "Key out of order - skipping %s\n", name
);
272 geometry_parse( end
, ptr
, mlt_properties_get( properties
, "end" ), normalised_width
, normalised_height
);
274 end
->position
= ( float )( out
- in
) / ( float )( out
- in
+ 1 );
281 /** Parse the alignment properties into the geometry.
284 static int alignment_parse( char* align
)
288 if ( align
== NULL
);
289 else if ( isdigit( align
[ 0 ] ) )
291 else if ( align
[ 0 ] == 'c' || align
[ 0 ] == 'm' )
293 else if ( align
[ 0 ] == 'r' || align
[ 0 ] == 'b' )
299 /** Adjust position according to scaled size and alignment properties.
302 static void alignment_calculate( struct geometry_s
*geometry
)
304 geometry
->x
+= ( geometry
->w
- geometry
->sw
) * geometry
->halign
/ 2;
305 geometry
->y
+= ( geometry
->h
- geometry
->sh
) * geometry
->valign
/ 2;
308 /** Calculate the position for this frame.
311 static float position_calculate( mlt_transition
this, mlt_position position
)
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 );
318 return ( float )( position
- in
) / ( float )( out
- in
+ 1 );
321 /** Calculate the field delta for this frame - position between two frames.
324 static inline float delta_calculate( mlt_transition
this, mlt_frame frame
)
326 // Get the in and out position
327 mlt_position in
= mlt_transition_get_in( this );
328 mlt_position out
= mlt_transition_get_out( this );
330 // Get the position of the frame
331 char *name
= mlt_properties_get( mlt_transition_properties( this ), "_unique_id" );
332 mlt_position position
= mlt_properties_get_position( mlt_frame_properties( frame
), name
);
335 float x
= ( float )( position
- in
) / ( float )( out
- in
+ 1 );
336 float y
= ( float )( position
+ 1 - in
) / ( float )( out
- in
+ 1 );
338 return ( y
- x
) / 2.0;
341 static int get_value( mlt_properties properties
, char *preferred
, char *fallback
)
343 int value
= mlt_properties_get_int( properties
, preferred
);
345 value
= mlt_properties_get_int( properties
, fallback
);
349 /** A linear threshold determination function.
352 static inline int32_t linearstep( int32_t edge1
, int32_t edge2
, int32_t a
)
360 return ( ( a
- edge1
) << 16 ) / ( edge2
- edge1
);
363 /** A smoother, non-linear threshold determination function.
366 static inline int32_t smoothstep( int32_t edge1
, int32_t edge2
, uint32_t a
)
374 a
= ( ( a
- edge1
) << 16 ) / ( edge2
- edge1
);
376 return ( ( ( a
* a
) >> 16 ) * ( ( 3 << 16 ) - ( 2 * a
) ) ) >> 16;
379 /** Load the luma map from PGM stream.
382 static void luma_read_pgm( FILE *f
, uint16_t **map
, int *width
, int *height
)
384 uint8_t *data
= NULL
;
396 // get the magic code
397 if ( fgets( line
, 127, f
) == NULL
)
401 while ( sscanf( line
, " #%s", comment
) > 0 )
402 if ( fgets( line
, 127, f
) == NULL
)
405 if ( line
[0] != 'P' || line
[1] != '5' )
408 // skip white space and see if a new line must be fetched
409 for ( i
= 2; i
< 127 && line
[i
] != '\0' && isspace( line
[i
] ); i
++ );
410 if ( ( line
[i
] == '\0' || line
[i
] == '#' ) && fgets( line
, 127, f
) == NULL
)
414 while ( sscanf( line
, " #%s", comment
) > 0 )
415 if ( fgets( line
, 127, f
) == NULL
)
418 // get the dimensions
419 if ( line
[0] == 'P' )
420 i
= sscanf( line
, "P5 %d %d %d", width
, height
, &maxval
);
422 i
= sscanf( line
, "%d %d %d", width
, height
, &maxval
);
424 // get the height value, if not yet
427 if ( fgets( line
, 127, f
) == NULL
)
431 while ( sscanf( line
, " #%s", comment
) > 0 )
432 if ( fgets( line
, 127, f
) == NULL
)
435 i
= sscanf( line
, "%d", height
);
442 // get the maximum gray value, if not yet
445 if ( fgets( line
, 127, f
) == NULL
)
449 while ( sscanf( line
, " #%s", comment
) > 0 )
450 if ( fgets( line
, 127, f
) == NULL
)
453 i
= sscanf( line
, "%d", &maxval
);
458 // determine if this is one or two bytes per pixel
459 bpp
= maxval
> 255 ?
2 : 1;
461 // allocate temporary storage for the raw data
462 data
= mlt_pool_alloc( *width
* *height
* bpp
);
467 if ( fread( data
, *width
* *height
* bpp
, 1, f
) != 1 )
470 // allocate the luma bitmap
471 *map
= p
= (uint16_t*)mlt_pool_alloc( *width
* *height
* sizeof( uint16_t ) );
475 // proces the raw data into the luma bitmap
476 for ( i
= 0; i
< *width
* *height
* bpp
; i
+= bpp
)
479 *p
++ = data
[ i
] << 8;
481 *p
++ = ( data
[ i
] << 8 ) + data
[ i
+ 1 ];
488 mlt_pool_release( data
);
491 /** Generate a luma map from any YUV image.
494 static void luma_read_yuv422( uint8_t *image
, uint16_t **map
, int width
, int height
)
498 // allocate the luma bitmap
499 uint16_t *p
= *map
= ( uint16_t* )mlt_pool_alloc( width
* height
* sizeof( uint16_t ) );
503 // proces the image data into the luma bitmap
504 for ( i
= 0; i
< width
* height
* 2; i
+= 2 )
505 *p
++ = ( image
[ i
] - 16 ) * 299; // 299 = 65535 / 219
509 /** Composite a source line over a destination line
513 void composite_line_yuv( uint8_t *dest
, uint8_t *src
, int width_src
, uint8_t *alpha
, int weight
, uint16_t *luma
, int softness
)
518 for ( j
= 0; j
< width_src
; j
++ )
520 a
= ( alpha
== NULL
) ?
255 : *alpha
++;
521 mix
= ( luma
== NULL
) ? weight
: linearstep( luma
[ j
], luma
[ j
] + softness
, weight
);
522 mix
= ( mix
* ( a
+ 1 ) ) >> 8;
523 *dest
= ( *src
++ * mix
+ *dest
* ( ( 1 << 16 ) - mix
) ) >> 16;
525 *dest
= ( *src
++ * mix
+ *dest
* ( ( 1 << 16 ) - mix
) ) >> 16;
530 /** Composite function.
533 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
)
537 int x_src
= 0, y_src
= 0;
538 int32_t weight
= ( 1 << 16 ) * ( geometry
.mix
/ 100 );
539 int step
= ( field
> -1 ) ?
2 : 1;
541 int stride_src
= width_src
* bpp
;
542 int stride_dest
= width_dest
* bpp
;
544 // Adjust to consumer scale
545 int x
= geometry
.x
* width_dest
/ geometry
.nw
;
546 int y
= geometry
.y
* height_dest
/ geometry
.nh
;
548 // Align x to a full YUYV group
550 width_src
= ( width_src
| 1 ) ^
1;
552 // optimization points - no work to do
553 if ( width_src
<= 0 || height_src
<= 0 )
556 if ( ( x
< 0 && -x
>= width_src
) || ( y
< 0 && -y
>= height_src
) )
559 // crop overlay off the left edge of frame
567 // crop overlay beyond right edge of frame
568 if ( x
+ width_src
> width_dest
)
569 width_src
= width_dest
- x
;
571 // crop overlay off the top edge of the frame
579 // crop overlay below bottom edge of frame
580 if ( y
+ height_src
> height_dest
)
581 height_src
= height_dest
- y
;
583 // offset pointer into overlay buffer based on cropping
584 p_src
+= x_src
* bpp
+ y_src
* stride_src
;
586 // offset pointer into frame buffer based upon positive coordinates only!
587 p_dest
+= ( x
< 0 ?
0 : x
) * bpp
+ ( y
< 0 ?
0 : y
) * stride_dest
;
589 // offset pointer into alpha channel based upon cropping
591 p_alpha
+= x_src
+ y_src
* stride_src
/ bpp
;
593 // offset pointer into luma channel based upon cropping
595 p_luma
+= x_src
+ y_src
* stride_src
/ bpp
;
597 // Assuming lower field first
598 // Special care is taken to make sure the b_frame is aligned to the correct field.
599 // field 0 = lower field and y should be odd (y is 0-based).
600 // field 1 = upper field and y should be even.
601 if ( ( field
> -1 ) && ( y
% 2 == field
) )
603 //fprintf( stderr, "field %d y %d\n", field, y );
604 if ( ( field
== 1 && y
< height_dest
- 1 ) || ( field
== 0 && y
== 0 ) )
605 p_dest
+= stride_dest
;
607 p_dest
-= stride_dest
;
610 // On the second field, use the other lines from b_frame
615 p_alpha
+= stride_src
/ bpp
;
621 int alpha_stride
= stride_src
/ bpp
;
623 // now do the compositing only to cropped extents
624 if ( line_fn
!= NULL
)
626 for ( i
= 0; i
< height_src
; i
+= step
)
628 line_fn( p_dest
, p_src
, width_src
, p_alpha
, weight
, p_luma
, softness
);
631 p_dest
+= stride_dest
;
633 p_alpha
+= alpha_stride
;
635 p_luma
+= alpha_stride
;
640 for ( i
= 0; i
< height_src
; i
+= step
)
642 composite_line_yuv( p_dest
, p_src
, width_src
, p_alpha
, weight
, p_luma
, softness
);
645 p_dest
+= stride_dest
;
647 p_alpha
+= alpha_stride
;
649 p_luma
+= alpha_stride
;
657 /** Scale 16bit greyscale luma map using nearest neighbor.
661 scale_luma ( uint16_t *dest_buf
, int dest_width
, int dest_height
, const uint16_t *src_buf
, int src_width
, int src_height
)
664 register int x_step
= ( src_width
<< 16 ) / dest_width
;
665 register int y_step
= ( src_height
<< 16 ) / dest_height
;
666 register int x
, y
= 0;
668 for ( i
= 0; i
< dest_height
; i
++ )
670 const uint16_t *src
= src_buf
+ ( y
>> 16 ) * src_width
;
673 for ( j
= 0; j
< dest_width
; j
++ )
675 *dest_buf
++ = src
[ x
>> 16 ];
682 static uint16_t* get_luma( mlt_properties properties
, int width
, int height
)
684 // The cached luma map information
685 int luma_width
= mlt_properties_get_int( properties
, "_luma.width" );
686 int luma_height
= mlt_properties_get_int( properties
, "_luma.height" );
687 uint16_t *luma_bitmap
= mlt_properties_get_data( properties
, "_luma.bitmap", NULL
);
689 // If the filename property changed, reload the map
690 char *resource
= mlt_properties_get( properties
, "luma" );
692 if ( resource
!= NULL
&& ( luma_bitmap
== NULL
|| luma_width
!= width
|| luma_height
!= height
) )
694 uint16_t *orig_bitmap
= mlt_properties_get_data( properties
, "_luma.orig_bitmap", NULL
);
695 luma_width
= mlt_properties_get_int( properties
, "_luma.orig_width" );
696 luma_height
= mlt_properties_get_int( properties
, "_luma.orig_height" );
698 // Load the original luma once
699 if ( orig_bitmap
== NULL
)
701 char *extension
= extension
= strrchr( resource
, '.' );
703 // See if it is a PGM
704 if ( extension
!= NULL
&& strcmp( extension
, ".pgm" ) == 0 )
707 FILE *f
= fopen( resource
, "r" );
711 luma_read_pgm( f
, &orig_bitmap
, &luma_width
, &luma_height
);
714 // Remember the original size for subsequent scaling
715 mlt_properties_set_data( properties
, "_luma.orig_bitmap", orig_bitmap
, luma_width
* luma_height
* 2, mlt_pool_release
, NULL
);
716 mlt_properties_set_int( properties
, "_luma.orig_width", luma_width
);
717 mlt_properties_set_int( properties
, "_luma.orig_height", luma_height
);
722 // Get the factory producer service
723 char *factory
= mlt_properties_get( properties
, "factory" );
725 // Create the producer
726 mlt_producer producer
= mlt_factory_producer( factory
, resource
);
729 if ( producer
!= NULL
)
731 // Get the producer properties
732 mlt_properties producer_properties
= mlt_producer_properties( producer
);
734 // Ensure that we loop
735 mlt_properties_set( producer_properties
, "eof", "loop" );
737 // Now pass all producer. properties on the transition down
738 mlt_properties_pass( producer_properties
, properties
, "luma." );
740 // We will get the alpha frame from the producer
741 mlt_frame luma_frame
= NULL
;
743 // Get the luma frame
744 if ( mlt_service_get_frame( mlt_producer_service( producer
), &luma_frame
, 0 ) == 0 )
747 mlt_image_format luma_format
= mlt_image_yuv422
;
749 // Get image from the luma producer
750 mlt_properties_set( mlt_frame_properties( luma_frame
), "rescale.interp", "none" );
751 mlt_frame_get_image( luma_frame
, &luma_image
, &luma_format
, &luma_width
, &luma_height
, 0 );
753 // Generate the luma map
754 if ( luma_image
!= NULL
&& luma_format
== mlt_image_yuv422
)
755 luma_read_yuv422( luma_image
, &orig_bitmap
, luma_width
, luma_height
);
757 // Remember the original size for subsequent scaling
758 mlt_properties_set_data( properties
, "_luma.orig_bitmap", orig_bitmap
, luma_width
* luma_height
* 2, mlt_pool_release
, NULL
);
759 mlt_properties_set_int( properties
, "_luma.orig_width", luma_width
);
760 mlt_properties_set_int( properties
, "_luma.orig_height", luma_height
);
762 // Cleanup the luma frame
763 mlt_frame_close( luma_frame
);
766 // Cleanup the luma producer
767 mlt_producer_close( producer
);
772 luma_bitmap
= mlt_pool_alloc( width
* height
* sizeof( uint16_t ) );
773 scale_luma( luma_bitmap
, width
, height
, orig_bitmap
, luma_width
, luma_height
);
775 // Remember the scaled luma size to prevent unnecessary scaling
776 mlt_properties_set_int( properties
, "_luma.width", width
);
777 mlt_properties_set_int( properties
, "_luma.height", height
);
778 mlt_properties_set_data( properties
, "_luma.bitmap", luma_bitmap
, width
* height
* 2, mlt_pool_release
, NULL
);
783 /** Get the properly sized image from b_frame.
786 static int get_b_frame_image( mlt_transition
this, mlt_frame b_frame
, uint8_t **image
, int *width
, int *height
, struct geometry_s
*geometry
)
789 mlt_image_format format
= mlt_image_yuv422
;
791 // Get the properties objects
792 mlt_properties b_props
= mlt_frame_properties( b_frame
);
793 mlt_properties properties
= mlt_transition_properties( this );
795 if ( mlt_properties_get( properties
, "distort" ) == NULL
&& geometry
->distort
== 0 )
797 // Adjust b_frame pixel aspect
798 int normalised_width
= geometry
->w
;
799 int normalised_height
= geometry
->h
;
800 int real_width
= get_value( b_props
, "real_width", "width" );
801 int real_height
= get_value( b_props
, "real_height", "height" );
802 double input_ar
= mlt_frame_get_aspect_ratio( b_frame
);
803 double output_ar
= mlt_properties_get_double( b_props
, "consumer_aspect_ratio" );
804 int scaled_width
= input_ar
/ output_ar
* real_width
;
805 int scaled_height
= real_height
;
807 // Now ensure that our images fit in the normalised frame
808 if ( scaled_width
> normalised_width
)
810 scaled_height
= scaled_height
* normalised_width
/ scaled_width
;
811 scaled_width
= normalised_width
;
813 if ( scaled_height
> normalised_height
)
815 scaled_width
= scaled_width
* normalised_height
/ scaled_height
;
816 scaled_height
= normalised_height
;
819 // Now apply the fill
820 // TODO: Should combine fill/distort in one property
821 if ( mlt_properties_get( properties
, "fill" ) != NULL
)
823 scaled_width
= ( geometry
->w
/ scaled_width
) * scaled_width
;
824 scaled_height
= ( geometry
->h
/ scaled_height
) * scaled_height
;
827 // Save the new scaled dimensions
828 geometry
->sw
= scaled_width
;
829 geometry
->sh
= scaled_height
;
833 geometry
->sw
= geometry
->w
;
834 geometry
->sh
= geometry
->h
;
837 // We want to ensure that we bypass resize now...
838 mlt_properties_set( b_props
, "distort", "true" );
840 // Take into consideration alignment for optimisation
841 alignment_calculate( geometry
);
843 // Adjust to consumer scale
844 int x
= geometry
->x
* *width
/ geometry
->nw
;
845 int y
= geometry
->y
* *height
/ geometry
->nh
;
846 *width
= geometry
->sw
* *width
/ geometry
->nw
;
847 *height
= geometry
->sh
* *height
/ geometry
->nh
;
851 // optimization points - no work to do
852 if ( *width
< 1 || *height
< 1 )
855 if ( ( x
< 0 && -x
>= *width
) || ( y
< 0 && -y
>= *height
) )
858 ret
= mlt_frame_get_image( b_frame
, image
, &format
, width
, height
, 1 );
864 static struct geometry_s
*composite_calculate( struct geometry_s
*result
, mlt_transition
this, mlt_frame a_frame
, float position
)
866 // Get the properties from the transition
867 mlt_properties properties
= mlt_transition_properties( this );
869 // Get the properties from the frame
870 mlt_properties a_props
= mlt_frame_properties( a_frame
);
872 // Structures for geometry
873 struct geometry_s
*start
= mlt_properties_get_data( properties
, "geometries", NULL
);
875 // Now parse the geometries
876 if ( start
== NULL
|| mlt_properties_get_int( properties
, "refresh" ) )
878 // Obtain the normalised width and height from the a_frame
879 int normalised_width
= mlt_properties_get_int( a_props
, "normalised_width" );
880 int normalised_height
= mlt_properties_get_int( a_props
, "normalised_height" );
882 // Parse the transitions properties
883 start
= transition_parse_keys( this, normalised_width
, normalised_height
);
885 // Assign to properties to ensure we get destroyed
886 mlt_properties_set_data( properties
, "geometries", start
, 0, transition_destroy_keys
, NULL
);
887 mlt_properties_set_int( properties
, "refresh", 0 );
890 // Do the calculation
891 geometry_calculate( result
, start
, position
);
893 // Now parse the alignment
894 result
->halign
= alignment_parse( mlt_properties_get( properties
, "halign" ) );
895 result
->valign
= alignment_parse( mlt_properties_get( properties
, "valign" ) );
900 static inline void inline_memcpy( uint8_t *dest
, uint8_t *src
, int length
)
902 uint8_t *end
= src
+ length
;
910 mlt_frame
composite_copy_region( mlt_transition
this, mlt_frame a_frame
, mlt_position frame_position
)
912 // Create a frame to return
913 mlt_frame b_frame
= mlt_frame_init( );
915 // Get the properties of the a frame
916 mlt_properties a_props
= mlt_frame_properties( a_frame
);
918 // Get the properties of the b frame
919 mlt_properties b_props
= mlt_frame_properties( b_frame
);
922 float position
= position_calculate( this, frame_position
);
925 uint8_t *dest
= NULL
;
927 // Get the image and dimensions
928 uint8_t *image
= mlt_properties_get_data( a_props
, "image", NULL
);
929 int width
= mlt_properties_get_int( a_props
, "width" );
930 int height
= mlt_properties_get_int( a_props
, "height" );
932 // Pointers for copy operation
943 // Will need to know region to copy
944 struct geometry_s result
;
946 // Calculate the region now
947 composite_calculate( &result
, this, a_frame
, position
);
949 // Need to scale down to actual dimensions
950 x
= result
.x
* width
/ result
.nw
;
951 y
= result
.y
* height
/ result
.nh
;
952 w
= result
.w
* width
/ result
.nw
;
953 h
= result
.h
* height
/ result
.nh
;
961 if ( y
+ h
> height
)
967 // Now we need to create a new destination image
968 dest
= mlt_pool_alloc( w
* h
* 2 );
970 // Copy the region of the image
971 p
= image
+ y
* width
* 2 + x
* 2;
973 r
= dest
+ w
* h
* 2;
977 inline_memcpy( q
, p
, w
* 2 );
982 // Assign to the new frame
983 mlt_properties_set_data( b_props
, "image", dest
, w
* h
* 2, mlt_pool_release
, NULL
);
984 mlt_properties_set_int( b_props
, "width", w
);
985 mlt_properties_set_int( b_props
, "height", h
);
987 // Assign this position to the b frame
988 mlt_frame_set_position( b_frame
, frame_position
);
997 static int transition_get_image( mlt_frame a_frame
, uint8_t **image
, mlt_image_format
*format
, int *width
, int *height
, int writable
)
999 // Get the b frame from the stack
1000 mlt_frame b_frame
= mlt_frame_pop_frame( a_frame
);
1002 // Get the transition from the a frame
1003 mlt_transition
this = mlt_frame_pop_service( a_frame
);
1005 // This compositer is yuv422 only
1006 *format
= mlt_image_yuv422
;
1008 // Get the image from the a frame
1009 mlt_frame_get_image( a_frame
, image
, format
, width
, height
, 1 );
1011 // Get the properties from the transition
1012 mlt_properties properties
= mlt_transition_properties( this );
1014 if ( b_frame
!= NULL
)
1016 // Get the properties of the a frame
1017 mlt_properties a_props
= mlt_frame_properties( a_frame
);
1019 // Get the properties of the b frame
1020 mlt_properties b_props
= mlt_frame_properties( b_frame
);
1022 // Structures for geometry
1023 struct geometry_s result
;
1025 // Calculate the position
1026 float position
= mlt_properties_get_double( b_props
, "relative_position" );
1027 float delta
= delta_calculate( this, a_frame
);
1029 // Do the calculation
1030 struct geometry_s
*start
= composite_calculate( &result
, this, a_frame
, position
);
1032 // Optimisation - no compositing required
1033 if ( result
.mix
== 0 || ( result
.w
== 0 && result
.h
== 0 ) )
1036 // Since we are the consumer of the b_frame, we must pass along these
1037 // consumer properties from the a_frame
1038 mlt_properties_set_double( b_props
, "consumer_aspect_ratio", mlt_properties_get_double( a_props
, "consumer_aspect_ratio" ) );
1040 // Get the image from the b frame
1041 uint8_t *image_b
= NULL
;
1042 int width_b
= *width
;
1043 int height_b
= *height
;
1045 if ( get_b_frame_image( this, b_frame
, &image_b
, &width_b
, &height_b
, &result
) == 0 )
1047 uint8_t *dest
= *image
;
1048 uint8_t *src
= image_b
;
1049 uint8_t *alpha
= mlt_frame_get_alpha_mask( b_frame
);
1051 mlt_properties_get_int( a_props
, "consumer_progressive" ) ||
1052 mlt_properties_get_int( properties
, "progressive" );
1055 int32_t luma_softness
= mlt_properties_get_double( properties
, "softness" ) * ( 1 << 16 );
1056 uint16_t *luma_bitmap
= get_luma( properties
, width_b
, height_b
);
1057 //composite_line_fn line_fn = mlt_properties_get_int( properties, "_MMX" ) ? composite_line_yuv_mmx : NULL;
1058 composite_line_fn line_fn
= NULL
;
1060 for ( field
= 0; field
< ( progressive ?
1 : 2 ); field
++ )
1062 // Assume lower field (0) first
1063 float field_position
= position
+ field
* delta
;
1065 // Do the calculation if we need to
1066 geometry_calculate( &result
, start
, field_position
);
1069 alignment_calculate( &result
);
1071 // Composite the b_frame on the a_frame
1072 composite_yuv( dest
, *width
, *height
, src
, width_b
, height_b
, alpha
, result
, progressive ?
-1 : field
, luma_bitmap
, luma_softness
, line_fn
);
1080 /** Composition transition processing.
1083 static mlt_frame
composite_process( mlt_transition
this, mlt_frame a_frame
, mlt_frame b_frame
)
1085 // Get a unique name to store the frame position
1086 char *name
= mlt_properties_get( mlt_transition_properties( this ), "_unique_id" );
1088 // Assign the current position to the name
1089 mlt_properties_set_position( mlt_frame_properties( a_frame
), name
, mlt_frame_get_position( a_frame
) );
1091 // Propogate the transition properties to the b frame
1092 mlt_properties_set_double( mlt_frame_properties( b_frame
), "relative_position", position_calculate( this, mlt_frame_get_position( a_frame
) ) );
1094 mlt_frame_push_service( a_frame
, this );
1095 mlt_frame_push_frame( a_frame
, b_frame
);
1096 mlt_frame_push_get_image( a_frame
, transition_get_image
);
1100 /** Constructor for the filter.
1103 mlt_transition
transition_composite_init( char *arg
)
1105 mlt_transition
this = calloc( sizeof( struct mlt_transition_s
), 1 );
1106 if ( this != NULL
&& mlt_transition_init( this, NULL
) == 0 )
1108 mlt_properties properties
= mlt_transition_properties( this );
1110 this->process
= composite_process
;
1112 // Default starting motion and zoom
1113 mlt_properties_set( properties
, "start", arg
!= NULL ? arg
: "85%,5%:10%x10%" );
1116 mlt_properties_set( properties
, "factory", "fezzik" );
1119 //mlt_properties_set_int( properties, "_MMX", composite_have_mmx() );