+/** Scale 16bit greyscale luma map using nearest neighbor.
+*/
+
+static inline void
+scale_luma ( uint16_t *dest_buf, int dest_width, int dest_height, const uint16_t *src_buf, int src_width, int src_height, int invert )
+{
+ register int i, j;
+ register int x_step = ( src_width << 16 ) / dest_width;
+ register int y_step = ( src_height << 16 ) / dest_height;
+ register int x, y = 0;
+
+ for ( i = 0; i < dest_height; i++ )
+ {
+ const uint16_t *src = src_buf + ( y >> 16 ) * src_width;
+ x = 0;
+
+ for ( j = 0; j < dest_width; j++ )
+ {
+ *dest_buf++ = src[ x >> 16 ] ^ invert;
+ x += x_step;
+ }
+ y += y_step;
+ }
+}
+
+static uint16_t* get_luma( mlt_transition self, mlt_properties properties, int width, int height )
+{
+ // The cached luma map information
+ int luma_width = mlt_properties_get_int( properties, "_luma.width" );
+ int luma_height = mlt_properties_get_int( properties, "_luma.height" );
+ uint16_t *luma_bitmap = mlt_properties_get_data( properties, "_luma.bitmap", NULL );
+ int invert = mlt_properties_get_int( properties, "luma_invert" );
+
+ // If the filename property changed, reload the map
+ char *resource = mlt_properties_get( properties, "luma" );
+
+ char temp[ 512 ];
+
+ if ( luma_width == 0 || luma_height == 0 )
+ {
+ luma_width = width;
+ luma_height = height;
+ }
+
+ if ( resource && resource[0] && strchr( resource, '%' ) )
+ {
+ // TODO: Clean up quick and dirty compressed/existence check
+ FILE *test;
+ sprintf( temp, "%s/lumas/%s/%s", mlt_environment( "MLT_DATA" ), mlt_environment( "MLT_NORMALISATION" ), strchr( resource, '%' ) + 1 );
+ test = fopen( temp, "r" );
+ if ( test == NULL )
+ strcat( temp, ".png" );
+ else
+ fclose( test );
+ resource = temp;
+ }
+
+ if ( resource && resource[0] )
+ {
+ char *old_luma = mlt_properties_get( properties, "_luma" );
+ int old_invert = mlt_properties_get_int( properties, "_luma_invert" );
+
+ if ( invert != old_invert || ( old_luma && old_luma[0] && strcmp( resource, old_luma ) ) )
+ {
+ mlt_properties_set_data( properties, "_luma.orig_bitmap", NULL, 0, NULL, NULL );
+ luma_bitmap = NULL;
+ }
+ }
+ else {
+ char *old_luma = mlt_properties_get( properties, "_luma" );
+ if ( old_luma && old_luma[0] )
+ {
+ mlt_properties_set_data( properties, "_luma.orig_bitmap", NULL, 0, NULL, NULL );
+ luma_bitmap = NULL;
+ mlt_properties_set( properties, "_luma", NULL);
+ }
+ }
+
+ if ( resource && resource[0] && ( luma_bitmap == NULL || luma_width != width || luma_height != height ) )
+ {
+ uint16_t *orig_bitmap = mlt_properties_get_data( properties, "_luma.orig_bitmap", NULL );
+ luma_width = mlt_properties_get_int( properties, "_luma.orig_width" );
+ luma_height = mlt_properties_get_int( properties, "_luma.orig_height" );
+
+ // Load the original luma once
+ if ( orig_bitmap == NULL )
+ {
+ char *extension = strrchr( resource, '.' );
+
+ // See if it is a PGM
+ if ( extension != NULL && strcmp( extension, ".pgm" ) == 0 )
+ {
+ // Open PGM
+ FILE *f = fopen( resource, "rb" );
+ if ( f != NULL )
+ {
+ // Load from PGM
+ luma_read_pgm( f, &orig_bitmap, &luma_width, &luma_height );
+ fclose( f );
+
+ // Remember the original size for subsequent scaling
+ mlt_properties_set_data( properties, "_luma.orig_bitmap", orig_bitmap, luma_width * luma_height * 2, mlt_pool_release, NULL );
+ mlt_properties_set_int( properties, "_luma.orig_width", luma_width );
+ mlt_properties_set_int( properties, "_luma.orig_height", luma_height );
+ }
+ }
+ else
+ {
+ // Get the factory producer service
+ char *factory = mlt_properties_get( properties, "factory" );
+
+ // Create the producer
+ mlt_profile profile = mlt_service_profile( MLT_TRANSITION_SERVICE( self ) );
+ mlt_producer producer = mlt_factory_producer( profile, factory, resource );
+
+ // If we have one
+ if ( producer != NULL )
+ {
+ // Get the producer properties
+ mlt_properties producer_properties = MLT_PRODUCER_PROPERTIES( producer );
+
+ // Ensure that we loop
+ mlt_properties_set( producer_properties, "eof", "loop" );
+
+ // Now pass all producer. properties on the transition down
+ mlt_properties_pass( producer_properties, properties, "luma." );
+
+ // We will get the alpha frame from the producer
+ mlt_frame luma_frame = NULL;
+
+ // Get the luma frame
+ if ( mlt_service_get_frame( MLT_PRODUCER_SERVICE( producer ), &luma_frame, 0 ) == 0 )
+ {
+ uint8_t *luma_image;
+ mlt_image_format luma_format = mlt_image_yuv422;
+
+ // Get image from the luma producer
+ mlt_properties_set( MLT_FRAME_PROPERTIES( luma_frame ), "rescale.interp", "none" );
+ mlt_frame_get_image( luma_frame, &luma_image, &luma_format, &luma_width, &luma_height, 0 );
+
+ // Generate the luma map
+ if ( luma_image != NULL && luma_format == mlt_image_yuv422 )
+ luma_read_yuv422( luma_image, &orig_bitmap, luma_width, luma_height );
+
+ // Remember the original size for subsequent scaling
+ mlt_properties_set_data( properties, "_luma.orig_bitmap", orig_bitmap, luma_width * luma_height * 2, mlt_pool_release, NULL );
+ mlt_properties_set_int( properties, "_luma.orig_width", luma_width );
+ mlt_properties_set_int( properties, "_luma.orig_height", luma_height );
+
+ // Cleanup the luma frame
+ mlt_frame_close( luma_frame );
+ }
+
+ // Cleanup the luma producer
+ mlt_producer_close( producer );
+ }
+ }
+ }
+ // Scale luma map
+ luma_bitmap = mlt_pool_alloc( width * height * sizeof( uint16_t ) );
+ scale_luma( luma_bitmap, width, height, orig_bitmap, luma_width, luma_height, invert * ( ( 1 << 16 ) - 1 ) );
+
+ // Remember the scaled luma size to prevent unnecessary scaling
+ mlt_properties_set_int( properties, "_luma.width", width );
+ mlt_properties_set_int( properties, "_luma.height", height );
+ mlt_properties_set_data( properties, "_luma.bitmap", luma_bitmap, width * height * 2, mlt_pool_release, NULL );
+ mlt_properties_set( properties, "_luma", resource );
+ mlt_properties_set_int( properties, "_luma_invert", invert );
+ }
+ return luma_bitmap;
+}
+
+/** Get the properly sized image from b_frame.
+*/
+
+static int get_b_frame_image( mlt_transition self, mlt_frame b_frame, uint8_t **image, int *width, int *height, struct geometry_s *geometry )
+{
+ int ret = 0;
+ mlt_image_format format = mlt_image_yuv422;
+
+ // Get the properties objects
+ mlt_properties b_props = MLT_FRAME_PROPERTIES( b_frame );
+ mlt_properties properties = MLT_TRANSITION_PROPERTIES( self );
+ uint8_t resize_alpha = mlt_properties_get_int( b_props, "resize_alpha" );
+ double output_ar = mlt_profile_sar( mlt_service_profile( MLT_TRANSITION_SERVICE(self) ) );
+
+ // Do not scale if we are cropping - the compositing rectangle can crop the b image
+ // TODO: Use the animatable w and h of the crop geometry to scale independently of crop rectangle
+ if ( mlt_properties_get( properties, "crop" ) )
+ {
+ int real_width = get_value( b_props, "meta.media.width", "width" );
+ int real_height = get_value( b_props, "meta.media.height", "height" );
+ double input_ar = mlt_properties_get_double( b_props, "aspect_ratio" );
+ int scaled_width = rint( ( input_ar == 0.0 ? output_ar : input_ar ) / output_ar * real_width );
+ int scaled_height = real_height;
+ geometry->sw = scaled_width;
+ geometry->sh = scaled_height;
+ }
+ // Normalise aspect ratios and scale preserving aspect ratio
+ else if ( mlt_properties_get_int( properties, "aligned" ) && mlt_properties_get_int( properties, "distort" ) == 0 && mlt_properties_get_int( b_props, "distort" ) == 0 && geometry->item.distort == 0 )
+ {
+ // Adjust b_frame pixel aspect
+ int normalised_width = geometry->item.w;
+ int normalised_height = geometry->item.h;
+ int real_width = get_value( b_props, "meta.media.width", "width" );
+ int real_height = get_value( b_props, "meta.media.height", "height" );
+ double input_ar = mlt_properties_get_double( b_props, "aspect_ratio" );
+ int scaled_width = rint( ( input_ar == 0.0 ? output_ar : input_ar ) / output_ar * real_width );
+ int scaled_height = real_height;
+// fprintf(stderr, "%s: scaled %dx%d norm %dx%d real %dx%d output_ar %f\n", __FILE__,
+// scaled_width, scaled_height, normalised_width, normalised_height, real_width, real_height,
+// output_ar);
+
+ // Now ensure that our images fit in the normalised frame
+ if ( scaled_width > normalised_width )
+ {
+ scaled_height = rint( scaled_height * normalised_width / scaled_width );
+ scaled_width = normalised_width;
+ }
+ if ( scaled_height > normalised_height )
+ {
+ scaled_width = rint( scaled_width * normalised_height / scaled_height );
+ scaled_height = normalised_height;
+ }
+
+ // Honour the fill request - this will scale the image to fill width or height while maintaining a/r
+ // ????: Shouln't this be the default behaviour?
+ if ( mlt_properties_get_int( properties, "fill" ) && scaled_width > 0 && scaled_height > 0 )
+ {
+ if ( scaled_height < normalised_height && scaled_width * normalised_height / scaled_height <= normalised_width )
+ {
+ scaled_width = rint( scaled_width * normalised_height / scaled_height );
+ scaled_height = normalised_height;
+ }
+ else if ( scaled_width < normalised_width && scaled_height * normalised_width / scaled_width < normalised_height )
+ {
+ scaled_height = rint( scaled_height * normalised_width / scaled_width );
+ scaled_width = normalised_width;
+ }
+ }
+
+ // Save the new scaled dimensions
+ geometry->sw = scaled_width;
+ geometry->sh = scaled_height;
+ }
+ else
+ {
+ geometry->sw = geometry->item.w;
+ geometry->sh = geometry->item.h;
+ }
+
+ // We want to ensure that we bypass resize now...
+ if ( resize_alpha == 0 )
+ mlt_properties_set_int( b_props, "distort", mlt_properties_get_int( properties, "distort" ) );
+
+ // If we're not aligned, we want a non-transparent background
+ if ( mlt_properties_get_int( properties, "aligned" ) == 0 )
+ mlt_properties_set_int( b_props, "resize_alpha", 255 );
+
+ // Take into consideration alignment for optimisation (titles are a special case)
+ if ( !mlt_properties_get_int( properties, "titles" ) &&
+ mlt_properties_get( properties, "crop" ) == NULL )
+ alignment_calculate( geometry );
+
+ // Adjust to consumer scale
+ *width = rint( geometry->sw * *width / geometry->nw );
+ *width -= *width % 2; // coerce to even width for yuv422
+ *height = rint( geometry->sh * *height / geometry->nh );
+// fprintf(stderr, "%s: scaled %dx%d norm %dx%d resize %dx%d\n", __FILE__,
+// geometry->sw, geometry->sh, geometry->nw, geometry->nh, *width, *height);
+
+ ret = mlt_frame_get_image( b_frame, image, &format, width, height, 1 );
+
+ // composite_yuv uses geometry->sw to determine source stride, which
+ // should equal the image width if not using crop property.
+ if ( !mlt_properties_get( properties, "crop" ) )
+ geometry->sw = *width;
+
+ // Set the frame back
+ mlt_properties_set_int( b_props, "resize_alpha", resize_alpha );
+
+ return ret && image != NULL;
+}
+
+static void crop_calculate( mlt_transition self, mlt_properties properties, struct geometry_s *result, double position )
+{
+ // Initialize panning info
+ result->x_src = 0;
+ result->y_src = 0;
+ if ( mlt_properties_get( properties, "crop" ) )
+ {
+ mlt_geometry crop = mlt_properties_get_data( properties, "crop_geometry", NULL );
+ if ( !crop )
+ {
+ crop = mlt_geometry_init();
+ mlt_position length = mlt_transition_get_length( self );
+ double cycle = mlt_properties_get_double( properties, "cycle" );
+
+ // Allow a geometry repeat cycle
+ if ( cycle >= 1 )
+ length = cycle;
+ else if ( cycle > 0 )
+ length *= cycle;
+ mlt_geometry_parse( crop, mlt_properties_get( properties, "crop" ), length, result->sw, result->sh );
+ mlt_properties_set_data( properties, "crop_geometry", crop, 0, (mlt_destructor)mlt_geometry_close, NULL );
+ }
+
+ // Repeat processing
+ int length = mlt_geometry_get_length( crop );
+ int mirror_off = mlt_properties_get_int( properties, "mirror_off" );
+ int repeat_off = mlt_properties_get_int( properties, "repeat_off" );
+ if ( !repeat_off && position >= length && length != 0 )
+ {
+ int section = position / length;
+ position -= section * length;
+ if ( !mirror_off && section % 2 == 1 )
+ position = length - position;
+ }
+
+ // Compute the pan
+ struct mlt_geometry_item_s crop_item;
+ mlt_geometry_fetch( crop, &crop_item, position );
+ result->x_src = rint( crop_item.x );
+ result->y_src = rint( crop_item.y );
+ }
+}
+
+static mlt_geometry composite_calculate( mlt_transition self, struct geometry_s *result, mlt_frame a_frame, double position )
+{
+ // Get the properties from the transition
+ mlt_properties properties = MLT_TRANSITION_PROPERTIES( self );
+
+ // Get the properties from the frame
+ mlt_properties a_props = MLT_FRAME_PROPERTIES( a_frame );
+
+ // Structures for geometry
+ mlt_geometry start = mlt_properties_get_data( properties, "geometries", NULL );
+
+ // Obtain the normalised width and height from the a_frame
+ mlt_profile profile = mlt_service_profile( MLT_TRANSITION_SERVICE( self ) );
+ int normalised_width = profile->width;
+ int normalised_height = profile->height;
+
+ char *name = mlt_properties_get( properties, "_unique_id" );
+ char key[ 256 ];
+
+ sprintf( key, "%s.in", name );
+ if ( mlt_properties_get( a_props, key ) )
+ {
+ sscanf( mlt_properties_get( a_props, key ), "%f %f %f %f %f %d %d", &result->item.x, &result->item.y, &result->item.w, &result->item.h, &result->item.mix, &result->nw, &result->nh );
+ }
+ else
+ {
+ // Now parse the geometries
+ if ( start == NULL )
+ {
+ // Parse the transitions properties
+ start = transition_parse_keys( self, normalised_width, normalised_height );
+
+ // Assign to properties to ensure we get destroyed
+ mlt_properties_set_data( properties, "geometries", start, 0, ( mlt_destructor )mlt_geometry_close, NULL );
+ }
+ else
+ {
+ mlt_position length = mlt_transition_get_length( self );
+ double cycle = mlt_properties_get_double( properties, "cycle" );
+ if ( cycle > 1 )
+ length = cycle;
+ else if ( cycle > 0 )
+ length *= cycle;
+ mlt_geometry_refresh( start, mlt_properties_get( properties, "geometry" ), length, normalised_width, normalised_height );
+ }
+
+ // Do the calculation
+ geometry_calculate( self, result, position );
+
+ // Assign normalised info
+ result->nw = normalised_width;
+ result->nh = normalised_height;
+ }
+
+ // Now parse the alignment
+ result->halign = alignment_parse( mlt_properties_get( properties, "halign" ) );
+ result->valign = alignment_parse( mlt_properties_get( properties, "valign" ) );
+
+ crop_calculate( self, properties, result, position );
+
+ return start;
+}
+
+mlt_frame composite_copy_region( mlt_transition self, mlt_frame a_frame, mlt_position frame_position )
+{
+ // Create a frame to return
+ mlt_frame b_frame = mlt_frame_init( MLT_TRANSITION_SERVICE( self ) );
+
+ // Get the properties of the a frame
+ mlt_properties a_props = MLT_FRAME_PROPERTIES( a_frame );
+
+ // Get the properties of the b frame
+ mlt_properties b_props = MLT_FRAME_PROPERTIES( b_frame );
+
+ // Get the position
+ int position = position_calculate( self, frame_position );
+
+ // Get the unique id of the transition
+ char *name = mlt_properties_get( MLT_TRANSITION_PROPERTIES( self ), "_unique_id" );
+ char key[ 256 ];
+
+ // Destination image
+ uint8_t *dest = NULL;
+
+ // Get the image and dimensions
+ uint8_t *image = NULL;
+ int width = mlt_properties_get_int( a_props, "width" );
+ int height = mlt_properties_get_int( a_props, "height" );
+ mlt_image_format format = mlt_image_yuv422;
+
+ mlt_frame_get_image( a_frame, &image, &format, &width, &height, 0 );
+ if ( !image )
+ return b_frame;
+
+ // Pointers for copy operation
+ uint8_t *p;
+
+ // Coordinates
+ int w = 0;
+ int h = 0;
+ int x = 0;
+ int y = 0;
+
+ int ss = 0;
+ int ds = 0;
+
+ // Will need to know region to copy
+ struct geometry_s result;
+
+ // Calculate the region now
+ composite_calculate( self, &result, a_frame, position );
+
+ // Need to scale down to actual dimensions
+ x = rint( result.item.x * width / result.nw );
+ y = rint( result.item.y * height / result.nh );
+ w = rint( result.item.w * width / result.nw );
+ h = rint( result.item.h * height / result.nh );
+
+ if ( x % 2 )
+ {
+ x --;
+ w ++;
+ }
+
+ // Store the key
+ sprintf( key, "%s.in=%d %d %d %d %f %d %d", name, x, y, w, h, result.item.mix, width, height );
+ mlt_properties_parse( a_props, key );
+ sprintf( key, "%s.out=%d %d %d %d %f %d %d", name, x, y, w, h, result.item.mix, width, height );
+ mlt_properties_parse( a_props, key );
+
+ ds = w * 2;
+ ss = width * 2;
+
+ // Now we need to create a new destination image
+ dest = mlt_pool_alloc( w * h * 2 );
+
+ // Assign to the new frame
+ mlt_frame_set_image( b_frame, dest, w * h * 2, mlt_pool_release );
+ mlt_properties_set_int( b_props, "width", w );
+ mlt_properties_set_int( b_props, "height", h );
+ mlt_properties_set_int( b_props, "format", format );
+
+ if ( y < 0 )
+ {
+ dest += ( ds * -y );
+ h += y;
+ y = 0;
+ }
+
+ if ( y + h > height )
+ h -= ( y + h - height );
+
+ if ( x < 0 )
+ {
+ dest += -x * 2;
+ w += x;
+ x = 0;
+ }
+
+ if ( w > 0 && h > 0 )
+ {
+ // Copy the region of the image
+ p = image + y * ss + x * 2;
+
+ while ( h -- )
+ {
+ memcpy( dest, p, w * 2 );
+ dest += ds;
+ p += ss;
+ }
+ }
+
+ // Assign this position to the b frame
+ mlt_frame_set_position( b_frame, frame_position );
+ mlt_properties_set_int( b_props, "distort", 1 );
+
+ // Return the frame
+ return b_frame;
+}
+