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 = rint( in->x + ( out->x - in->x ) * position + 0.5 );
163 output->y = rint( in->y + ( out->y - in->y ) * position + 0.5 );
164 output->w = rint( in->w + ( out->w - in->w ) * position + 0.5 );
165 output->h = rint( in->h + ( out->h - in->h ) * position + 0.5 );
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;
182 static void transition_destroy_keys( void *arg )
184 struct geometry_s *ptr = arg;
185 struct geometry_s *next = NULL;
187 while ( ptr != NULL )
195 static struct geometry_s *transition_parse_keys( mlt_transition this, int normalised_width, int normalised_height )
197 // Loop variable for property interrogation
200 // Get the properties of the transition
201 mlt_properties properties = mlt_transition_properties( this );
203 // Get the in and out position
204 mlt_position in = mlt_transition_get_in( this );
205 mlt_position out = mlt_transition_get_out( this );
208 struct geometry_s *start = calloc( 1, sizeof( struct geometry_s ) );
210 // Create the end (we always need two entries)
211 struct geometry_s *end = calloc( 1, sizeof( struct geometry_s ) );
214 struct geometry_s *ptr = start;
216 // Parse the start property
217 geometry_parse( start, NULL, mlt_properties_get( properties, "start" ), normalised_width, normalised_height );
219 // Parse the keys in between
220 for ( i = 0; i < mlt_properties_count( properties ); i ++ )
222 // Get the name of the property
223 char *name = mlt_properties_get_name( properties, i );
225 // Check that it's valid
226 if ( !strncmp( name, "key[", 4 ) )
228 // Get the value of the property
229 char *value = mlt_properties_get_value( properties, i );
231 // Determine the frame number
232 int frame = atoi( name + 4 );
234 // Determine the position
237 if ( frame >= 0 && frame < ( out - in ) )
238 position = ( float )frame / ( float )( out - in + 1 );
239 else if ( frame < 0 && - frame < ( out - in ) )
240 position = ( float )( out - in + frame ) / ( float )( out - in + 1 );
242 // For now, we'll exclude all keys received out of order
243 if ( position > ptr->position )
245 // Create a new geometry
246 struct geometry_s *temp = calloc( 1, sizeof( struct geometry_s ) );
248 // Parse and add to the list
249 geometry_parse( temp, ptr, value, normalised_width, normalised_height );
251 // Assign the position and frame
253 temp->position = position;
255 // Allow the next to be appended after this one
260 fprintf( stderr, "Key out of order - skipping %s\n", name );
266 geometry_parse( end, ptr, mlt_properties_get( properties, "end" ), normalised_width, normalised_height );
268 end->position = ( float )( out - in ) / ( float )( out - in + 1 );
275 /** Parse the alignment properties into the geometry.
278 static int alignment_parse( char* align )
282 if ( align == NULL );
283 else if ( isdigit( align[ 0 ] ) )
285 else if ( align[ 0 ] == 'c' || align[ 0 ] == 'm' )
287 else if ( align[ 0 ] == 'r' || align[ 0 ] == 'b' )
293 /** Adjust position according to scaled size and alignment properties.
296 static void alignment_calculate( struct geometry_s *geometry )
298 geometry->x += ( geometry->w - geometry->sw ) * geometry->halign / 2;
299 geometry->y += ( geometry->h - geometry->sh ) * geometry->valign / 2;
302 /** Calculate the position for this frame.
305 static float position_calculate( mlt_transition this, mlt_position position )
307 // Get the in and out position
308 mlt_position in = mlt_transition_get_in( this );
309 mlt_position out = mlt_transition_get_out( this );
312 return ( float )( position - in ) / ( float )( out - in + 1 );
315 /** Calculate the field delta for this frame - position between two frames.
318 static inline float delta_calculate( mlt_transition this, mlt_frame frame )
320 // Get the in and out position
321 mlt_position in = mlt_transition_get_in( this );
322 mlt_position out = mlt_transition_get_out( this );
324 // Get the position of the frame
325 char *name = mlt_properties_get( mlt_transition_properties( this ), "_unique_id" );
326 mlt_position position = mlt_properties_get_position( mlt_frame_properties( frame ), name );
329 float x = ( float )( position - in ) / ( float )( out - in + 1 );
330 float y = ( float )( position + 1 - in ) / ( float )( out - in + 1 );
332 return ( y - x ) / 2.0;
335 static int get_value( mlt_properties properties, char *preferred, char *fallback )
337 int value = mlt_properties_get_int( properties, preferred );
339 value = mlt_properties_get_int( properties, fallback );
343 /** A linear threshold determination function.
346 static inline int32_t linearstep( int32_t edge1, int32_t edge2, int32_t a )
354 return ( ( a - edge1 ) << 16 ) / ( edge2 - edge1 );
357 /** A smoother, non-linear threshold determination function.
360 static inline int32_t smoothstep( int32_t edge1, int32_t edge2, uint32_t a )
368 a = ( ( a - edge1 ) << 16 ) / ( edge2 - edge1 );
370 return ( ( ( a * a ) >> 16 ) * ( ( 3 << 16 ) - ( 2 * a ) ) ) >> 16;
373 /** Load the luma map from PGM stream.
376 static void luma_read_pgm( FILE *f, uint16_t **map, int *width, int *height )
378 uint8_t *data = NULL;
390 // get the magic code
391 if ( fgets( line, 127, f ) == NULL )
395 while ( sscanf( line, " #%s", comment ) > 0 )
396 if ( fgets( line, 127, f ) == NULL )
399 if ( line[0] != 'P' || line[1] != '5' )
402 // skip white space and see if a new line must be fetched
403 for ( i = 2; i < 127 && line[i] != '\0' && isspace( line[i] ); i++ );
404 if ( ( line[i] == '\0' || line[i] == '#' ) && fgets( line, 127, f ) == NULL )
408 while ( sscanf( line, " #%s", comment ) > 0 )
409 if ( fgets( line, 127, f ) == NULL )
412 // get the dimensions
413 if ( line[0] == 'P' )
414 i = sscanf( line, "P5 %d %d %d", width, height, &maxval );
416 i = sscanf( line, "%d %d %d", width, height, &maxval );
418 // get the height 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", height );
436 // get the maximum gray value, if not yet
439 if ( fgets( line, 127, f ) == NULL )
443 while ( sscanf( line, " #%s", comment ) > 0 )
444 if ( fgets( line, 127, f ) == NULL )
447 i = sscanf( line, "%d", &maxval );
452 // determine if this is one or two bytes per pixel
453 bpp = maxval > 255 ? 2 : 1;
455 // allocate temporary storage for the raw data
456 data = mlt_pool_alloc( *width * *height * bpp );
461 if ( fread( data, *width * *height * bpp, 1, f ) != 1 )
464 // allocate the luma bitmap
465 *map = p = (uint16_t*)mlt_pool_alloc( *width * *height * sizeof( uint16_t ) );
469 // proces the raw data into the luma bitmap
470 for ( i = 0; i < *width * *height * bpp; i += bpp )
473 *p++ = data[ i ] << 8;
475 *p++ = ( data[ i ] << 8 ) + data[ i + 1 ];
482 mlt_pool_release( data );
485 /** Generate a luma map from any YUV image.
488 static void luma_read_yuv422( uint8_t *image, uint16_t **map, int width, int height )
492 // allocate the luma bitmap
493 uint16_t *p = *map = ( uint16_t* )mlt_pool_alloc( width * height * sizeof( uint16_t ) );
497 // proces the image data into the luma bitmap
498 for ( i = 0; i < width * height * 2; i += 2 )
499 *p++ = ( image[ i ] - 16 ) * 299; // 299 = 65535 / 219
503 /** Composite a source line over a destination line
507 void composite_line_yuv( uint8_t *dest, uint8_t *src, int width_src, uint8_t *alpha, int weight, uint16_t *luma, int softness )
512 for ( j = 0; j < width_src; j ++ )
514 a = ( alpha == NULL ) ? 255 : *alpha ++;
515 mix = ( luma == NULL ) ? weight : linearstep( luma[ j ], luma[ j ] + softness, weight );
516 mix = ( mix * ( a + 1 ) ) >> 8;
517 *dest = ( *src++ * mix + *dest * ( ( 1 << 16 ) - mix ) ) >> 16;
519 *dest = ( *src++ * mix + *dest * ( ( 1 << 16 ) - mix ) ) >> 16;
524 /** Composite function.
527 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 )
531 int x_src = 0, y_src = 0;
532 int32_t weight = ( 1 << 16 ) * ( geometry.mix / 100 );
533 int step = ( field > -1 ) ? 2 : 1;
535 int stride_src = width_src * bpp;
536 int stride_dest = width_dest * bpp;
538 // Adjust to consumer scale
539 int x = geometry.x * width_dest / geometry.nw;
540 int y = geometry.y * height_dest / geometry.nh;
541 int uneven = ( x & 1 );
543 // optimization points - no work to do
544 if ( width_src <= 0 || height_src <= 0 )
547 if ( ( x < 0 && -x >= width_src ) || ( y < 0 && -y >= height_src ) )
550 // crop overlay off the left edge of frame
558 // crop overlay beyond right edge of frame
559 if ( x + width_src > width_dest )
560 width_src = width_dest - x;
562 // crop overlay off the top edge of the frame
570 // crop overlay below bottom edge of frame
571 if ( y + height_src > height_dest )
572 height_src = height_dest - y;
574 // offset pointer into overlay buffer based on cropping
575 p_src += x_src * bpp + y_src * stride_src;
577 // offset pointer into frame buffer based upon positive coordinates only!
578 p_dest += ( x < 0 ? 0 : x ) * bpp + ( y < 0 ? 0 : y ) * stride_dest;
580 // offset pointer into alpha channel based upon cropping
582 p_alpha += x_src + y_src * stride_src / bpp;
584 // offset pointer into luma channel based upon cropping
586 p_luma += x_src + y_src * stride_src / bpp;
588 // Assuming lower field first
589 // Special care is taken to make sure the b_frame is aligned to the correct field.
590 // field 0 = lower field and y should be odd (y is 0-based).
591 // field 1 = upper field and y should be even.
592 if ( ( field > -1 ) && ( y % 2 == field ) )
594 //fprintf( stderr, "field %d y %d\n", field, y );
595 if ( ( field == 1 && y < height_dest - 1 ) || ( field == 0 && y == 0 ) )
596 p_dest += stride_dest;
598 p_dest -= stride_dest;
601 // On the second field, use the other lines from b_frame
606 p_alpha += stride_src / bpp;
612 int alpha_stride = stride_src / bpp;
617 // now do the compositing only to cropped extents
618 if ( line_fn != NULL )
620 for ( i = 0; i < height_src; i += step )
622 line_fn( p_dest, p_src, width_src, p_alpha, weight, p_luma, softness );
625 p_dest += stride_dest;
627 p_alpha += alpha_stride;
629 p_luma += alpha_stride;
634 for ( i = 0; i < height_src; i += step )
636 composite_line_yuv( p_dest, p_src, width_src, p_alpha, weight, p_luma, softness );
639 p_dest += stride_dest;
641 p_alpha += alpha_stride;
643 p_luma += alpha_stride;
651 /** Scale 16bit greyscale luma map using nearest neighbor.
655 scale_luma ( uint16_t *dest_buf, int dest_width, int dest_height, const uint16_t *src_buf, int src_width, int src_height )
658 register int x_step = ( src_width << 16 ) / dest_width;
659 register int y_step = ( src_height << 16 ) / dest_height;
660 register int x, y = 0;
662 for ( i = 0; i < dest_height; i++ )
664 const uint16_t *src = src_buf + ( y >> 16 ) * src_width;
667 for ( j = 0; j < dest_width; j++ )
669 *dest_buf++ = src[ x >> 16 ];
676 static uint16_t* get_luma( mlt_properties properties, int width, int height )
678 // The cached luma map information
679 int luma_width = mlt_properties_get_int( properties, "_luma.width" );
680 int luma_height = mlt_properties_get_int( properties, "_luma.height" );
681 uint16_t *luma_bitmap = mlt_properties_get_data( properties, "_luma.bitmap", NULL );
683 // If the filename property changed, reload the map
684 char *resource = mlt_properties_get( properties, "luma" );
686 if ( resource != NULL && ( luma_bitmap == NULL || luma_width != width || luma_height != height ) )
688 uint16_t *orig_bitmap = mlt_properties_get_data( properties, "_luma.orig_bitmap", NULL );
689 luma_width = mlt_properties_get_int( properties, "_luma.orig_width" );
690 luma_height = mlt_properties_get_int( properties, "_luma.orig_height" );
692 // Load the original luma once
693 if ( orig_bitmap == NULL )
695 char *extension = extension = strrchr( resource, '.' );
697 // See if it is a PGM
698 if ( extension != NULL && strcmp( extension, ".pgm" ) == 0 )
701 FILE *f = fopen( resource, "r" );
705 luma_read_pgm( f, &orig_bitmap, &luma_width, &luma_height );
708 // Remember the original size for subsequent scaling
709 mlt_properties_set_data( properties, "_luma.orig_bitmap", orig_bitmap, luma_width * luma_height * 2, mlt_pool_release, NULL );
710 mlt_properties_set_int( properties, "_luma.orig_width", luma_width );
711 mlt_properties_set_int( properties, "_luma.orig_height", luma_height );
716 // Get the factory producer service
717 char *factory = mlt_properties_get( properties, "factory" );
719 // Create the producer
720 mlt_producer producer = mlt_factory_producer( factory, resource );
723 if ( producer != NULL )
725 // Get the producer properties
726 mlt_properties producer_properties = mlt_producer_properties( producer );
728 // Ensure that we loop
729 mlt_properties_set( producer_properties, "eof", "loop" );
731 // Now pass all producer. properties on the transition down
732 mlt_properties_pass( producer_properties, properties, "luma." );
734 // We will get the alpha frame from the producer
735 mlt_frame luma_frame = NULL;
737 // Get the luma frame
738 if ( mlt_service_get_frame( mlt_producer_service( producer ), &luma_frame, 0 ) == 0 )
741 mlt_image_format luma_format = mlt_image_yuv422;
743 // Get image from the luma producer
744 mlt_properties_set( mlt_frame_properties( luma_frame ), "rescale.interp", "none" );
745 mlt_frame_get_image( luma_frame, &luma_image, &luma_format, &luma_width, &luma_height, 0 );
747 // Generate the luma map
748 if ( luma_image != NULL && luma_format == mlt_image_yuv422 )
749 luma_read_yuv422( luma_image, &orig_bitmap, luma_width, luma_height );
751 // Remember the original size for subsequent scaling
752 mlt_properties_set_data( properties, "_luma.orig_bitmap", orig_bitmap, luma_width * luma_height * 2, mlt_pool_release, NULL );
753 mlt_properties_set_int( properties, "_luma.orig_width", luma_width );
754 mlt_properties_set_int( properties, "_luma.orig_height", luma_height );
756 // Cleanup the luma frame
757 mlt_frame_close( luma_frame );
760 // Cleanup the luma producer
761 mlt_producer_close( producer );
766 luma_bitmap = mlt_pool_alloc( width * height * sizeof( uint16_t ) );
767 scale_luma( luma_bitmap, width, height, orig_bitmap, luma_width, luma_height );
769 // Remember the scaled luma size to prevent unnecessary scaling
770 mlt_properties_set_int( properties, "_luma.width", width );
771 mlt_properties_set_int( properties, "_luma.height", height );
772 mlt_properties_set_data( properties, "_luma.bitmap", luma_bitmap, width * height * 2, mlt_pool_release, NULL );
777 /** Get the properly sized image from b_frame.
780 static int get_b_frame_image( mlt_transition this, mlt_frame b_frame, uint8_t **image, int *width, int *height, struct geometry_s *geometry )
783 mlt_image_format format = mlt_image_yuv422;
785 // Get the properties objects
786 mlt_properties b_props = mlt_frame_properties( b_frame );
787 mlt_properties properties = mlt_transition_properties( this );
789 if ( mlt_properties_get( properties, "distort" ) == NULL && mlt_properties_get( b_props, "distort" ) == NULL && geometry->distort == 0 )
791 // Adjust b_frame pixel aspect
792 int normalised_width = geometry->w;
793 int normalised_height = geometry->h;
794 int real_width = get_value( b_props, "real_width", "width" );
795 int real_height = get_value( b_props, "real_height", "height" );
796 double input_ar = mlt_frame_get_aspect_ratio( b_frame );
797 double output_ar = mlt_properties_get_double( b_props, "consumer_aspect_ratio" );
798 int scaled_width = input_ar / output_ar * real_width;
799 int scaled_height = real_height;
801 // Now ensure that our images fit in the normalised frame
802 if ( scaled_width > normalised_width )
804 scaled_height = scaled_height * normalised_width / scaled_width;
805 scaled_width = normalised_width;
807 if ( scaled_height > normalised_height )
809 scaled_width = scaled_width * normalised_height / scaled_height;
810 scaled_height = normalised_height;
813 // Now apply the fill
814 // TODO: Should combine fill/distort in one property
815 if ( mlt_properties_get( properties, "fill" ) != NULL )
817 scaled_width = ( geometry->w / scaled_width ) * scaled_width;
818 scaled_height = ( geometry->h / scaled_height ) * scaled_height;
821 // Save the new scaled dimensions
822 geometry->sw = scaled_width;
823 geometry->sh = scaled_height;
827 geometry->sw = geometry->w;
828 geometry->sh = geometry->h;
831 // We want to ensure that we bypass resize now...
832 mlt_properties_set( b_props, "distort", "true" );
834 // Take into consideration alignment for optimisation
835 alignment_calculate( geometry );
837 // Adjust to consumer scale
838 int x = geometry->x * *width / geometry->nw;
839 int y = geometry->y * *height / geometry->nh;
840 *width = geometry->sw * *width / geometry->nw;
841 *height = geometry->sh * *height / geometry->nh;
845 // optimization points - no work to do
846 if ( *width < 1 || *height < 1 )
849 if ( ( x < 0 && -x >= *width ) || ( y < 0 && -y >= *height ) )
852 ret = mlt_frame_get_image( b_frame, image, &format, width, height, 1 );
858 static struct geometry_s *composite_calculate( struct geometry_s *result, mlt_transition this, mlt_frame a_frame, float position )
860 // Get the properties from the transition
861 mlt_properties properties = mlt_transition_properties( this );
863 // Get the properties from the frame
864 mlt_properties a_props = mlt_frame_properties( a_frame );
866 // Structures for geometry
867 struct geometry_s *start = mlt_properties_get_data( properties, "geometries", NULL );
869 // Obtain the normalised width and height from the a_frame
870 int normalised_width = mlt_properties_get_int( a_props, "normalised_width" );
871 int normalised_height = mlt_properties_get_int( a_props, "normalised_height" );
873 // Now parse the geometries
874 if ( start == NULL || mlt_properties_get_int( properties, "refresh" ) || start->nw != normalised_width || start->nh != normalised_height )
876 // Parse the transitions properties
877 start = transition_parse_keys( this, normalised_width, normalised_height );
879 // Assign to properties to ensure we get destroyed
880 mlt_properties_set_data( properties, "geometries", start, 0, transition_destroy_keys, NULL );
881 mlt_properties_set_int( properties, "refresh", 0 );
884 // Do the calculation
885 geometry_calculate( result, start, position );
887 // Now parse the alignment
888 result->halign = alignment_parse( mlt_properties_get( properties, "halign" ) );
889 result->valign = alignment_parse( mlt_properties_get( properties, "valign" ) );
894 static inline void inline_memcpy( uint8_t *dest, uint8_t *src, int length )
896 uint8_t *end = src + length;
904 mlt_frame composite_copy_region( mlt_transition this, mlt_frame a_frame, mlt_position frame_position )
906 // Create a frame to return
907 mlt_frame b_frame = mlt_frame_init( );
909 // Get the properties of the a frame
910 mlt_properties a_props = mlt_frame_properties( a_frame );
912 // Get the properties of the b frame
913 mlt_properties b_props = mlt_frame_properties( b_frame );
916 float position = position_calculate( this, frame_position );
919 uint8_t *dest = NULL;
921 // Get the image and dimensions
922 uint8_t *image = mlt_properties_get_data( a_props, "image", NULL );
923 int width = mlt_properties_get_int( a_props, "width" );
924 int height = mlt_properties_get_int( a_props, "height" );
926 // Pointers for copy operation
937 // Will need to know region to copy
938 struct geometry_s result;
940 // Calculate the region now
941 composite_calculate( &result, this, a_frame, position );
943 // Need to scale down to actual dimensions
944 x = result.x * width / result.nw ;
945 y = result.y * height / result.nh;
946 w = result.w * width / result.nw;
947 h = result.h * height / result.nh;
955 if ( y + h > height )
961 // Now we need to create a new destination image
962 dest = mlt_pool_alloc( w * h * 2 );
964 // Copy the region of the image
965 p = image + y * width * 2 + x * 2;
967 r = dest + w * h * 2;
971 inline_memcpy( q, p, w * 2 );
976 // Assign to the new frame
977 mlt_properties_set_data( b_props, "image", dest, w * h * 2, mlt_pool_release, NULL );
978 mlt_properties_set_int( b_props, "width", w );
979 mlt_properties_set_int( b_props, "height", h );
981 // Assign this position to the b frame
982 mlt_frame_set_position( b_frame, frame_position );
983 mlt_properties_set( b_props, "distort", "true" );
992 static int transition_get_image( mlt_frame a_frame, uint8_t **image, mlt_image_format *format, int *width, int *height, int writable )
994 // Get the b frame from the stack
995 mlt_frame b_frame = mlt_frame_pop_frame( a_frame );
997 // Get the transition from the a frame
998 mlt_transition this = mlt_frame_pop_service( a_frame );
1000 // This compositer is yuv422 only
1001 *format = mlt_image_yuv422;
1003 // Get the image from the a frame
1004 mlt_frame_get_image( a_frame, image, format, width, height, 1 );
1006 // Get the properties from the transition
1007 mlt_properties properties = mlt_transition_properties( this );
1009 if ( b_frame != NULL )
1011 // Get the properties of the a frame
1012 mlt_properties a_props = mlt_frame_properties( a_frame );
1014 // Get the properties of the b frame
1015 mlt_properties b_props = mlt_frame_properties( b_frame );
1017 // Structures for geometry
1018 struct geometry_s result;
1020 // Calculate the position
1021 float position = mlt_properties_get_double( b_props, "relative_position" );
1022 float delta = delta_calculate( this, a_frame );
1024 // Do the calculation
1025 struct geometry_s *start = composite_calculate( &result, this, a_frame, position );
1027 // Get the image from the b frame
1028 uint8_t *image_b = NULL;
1029 int width_b = *width;
1030 int height_b = *height;
1032 // Optimisation - no compositing required
1033 if ( result.mix == 0 || ( result.w == 0 && result.h == 0 ) )
1036 // Need to keep the width/height of the a_frame on the b_frame for titling
1037 if ( mlt_properties_get( a_props, "dest_width" ) == NULL )
1039 mlt_properties_set_int( a_props, "dest_width", *width );
1040 mlt_properties_set_int( a_props, "dest_height", *height );
1041 mlt_properties_set_int( b_props, "dest_width", *width );
1042 mlt_properties_set_int( b_props, "dest_height", *height );
1046 mlt_properties_set_int( b_props, "dest_width", mlt_properties_get_int( a_props, "dest_width" ) );
1047 mlt_properties_set_int( b_props, "dest_height", mlt_properties_get_int( a_props, "dest_height" ) );
1050 // Since we are the consumer of the b_frame, we must pass along these
1051 // consumer properties from the a_frame
1052 mlt_properties_set_double( b_props, "consumer_deinterlace", mlt_properties_get_double( a_props, "consumer_deinterlace" ) );
1053 mlt_properties_set_double( b_props, "consumer_aspect_ratio", mlt_properties_get_double( a_props, "consumer_aspect_ratio" ) );
1054 mlt_properties_set_int( b_props, "normalised_width", mlt_properties_get_double( a_props, "normalised_width" ) );
1055 mlt_properties_set_int( b_props, "normalised_height", mlt_properties_get_double( a_props, "normalised_height" ) );
1057 // Special case for titling...
1058 if ( mlt_properties_get_int( properties, "titles" ) )
1060 if ( mlt_properties_get( b_props, "rescale.interp" ) == NULL )
1061 mlt_properties_set( b_props, "rescale.interp", "nearest" );
1062 mlt_properties_set( properties, "fill", NULL );
1063 width_b = mlt_properties_get_int( a_props, "dest_width" );
1064 height_b = mlt_properties_get_int( a_props, "dest_height" );
1067 if ( get_b_frame_image( this, b_frame, &image_b, &width_b, &height_b, &result ) == 0 )
1069 uint8_t *dest = *image;
1070 uint8_t *src = image_b;
1071 uint8_t *alpha = mlt_frame_get_alpha_mask( b_frame );
1073 mlt_properties_get_int( a_props, "consumer_deinterlace" ) ||
1074 mlt_properties_get_int( properties, "progressive" );
1077 int32_t luma_softness = mlt_properties_get_double( properties, "softness" ) * ( 1 << 16 );
1078 uint16_t *luma_bitmap = get_luma( properties, width_b, height_b );
1079 //composite_line_fn line_fn = mlt_properties_get_int( properties, "_MMX" ) ? composite_line_yuv_mmx : NULL;
1080 composite_line_fn line_fn = NULL;
1082 for ( field = 0; field < ( progressive ? 1 : 2 ); field++ )
1084 // Assume lower field (0) first
1085 float field_position = position + field * delta;
1087 // Do the calculation if we need to
1088 geometry_calculate( &result, start, field_position );
1091 alignment_calculate( &result );
1093 // Composite the b_frame on the a_frame
1094 composite_yuv( dest, *width, *height, src, width_b, height_b, alpha, result, progressive ? -1 : field, luma_bitmap, luma_softness, line_fn );
1102 /** Composition transition processing.
1105 static mlt_frame composite_process( mlt_transition this, mlt_frame a_frame, mlt_frame b_frame )
1107 // Get a unique name to store the frame position
1108 char *name = mlt_properties_get( mlt_transition_properties( this ), "_unique_id" );
1110 // Assign the current position to the name
1111 mlt_properties_set_position( mlt_frame_properties( a_frame ), name, mlt_frame_get_position( a_frame ) );
1113 // Propogate the transition properties to the b frame
1114 mlt_properties_set_double( mlt_frame_properties( b_frame ), "relative_position", position_calculate( this, mlt_frame_get_position( a_frame ) ) );
1116 mlt_frame_push_service( a_frame, this );
1117 mlt_frame_push_frame( a_frame, b_frame );
1118 mlt_frame_push_get_image( a_frame, transition_get_image );
1122 /** Constructor for the filter.
1125 mlt_transition transition_composite_init( char *arg )
1127 mlt_transition this = calloc( sizeof( struct mlt_transition_s ), 1 );
1128 if ( this != NULL && mlt_transition_init( this, NULL ) == 0 )
1130 mlt_properties properties = mlt_transition_properties( this );
1132 this->process = composite_process;
1134 // Default starting motion and zoom
1135 mlt_properties_set( properties, "start", arg != NULL ? arg : "85%,5%:10%x10%" );
1138 mlt_properties_set( properties, "factory", "fezzik" );
1141 //mlt_properties_set_int( properties, "_MMX", composite_have_mmx() );