+
+ // Check for the special case - no aspect ratio means no problem :-)
+ if ( aspect_ratio == 0.0 )
+ aspect_ratio = mlt_properties_get_double( properties, "consumer_aspect_ratio" );
+
+ // Reset the aspect ratio
+ mlt_properties_set_double( properties, "aspect_ratio", aspect_ratio );
+
+ // Hmmm...
+ char *rescale = mlt_properties_get( properties, "rescale.interp" );
+ if ( rescale != NULL && !strcmp( rescale, "none" ) )
+ return mlt_frame_get_image( this, image, format, width, height, writable );
+
+ if ( mlt_properties_get_int( properties, "distort" ) == 0 )
+ {
+ // Normalise the input and out display aspect
+ int normalised_width = mlt_properties_get_int( properties, "normalised_width" );
+ int normalised_height = mlt_properties_get_int( properties, "normalised_height" );
+ int real_width = mlt_properties_get_int( properties, "real_width" );
+ int real_height = mlt_properties_get_int( properties, "real_height" );
+ if ( real_width == 0 )
+ real_width = mlt_properties_get_int( properties, "width" );
+ if ( real_height == 0 )
+ real_height = mlt_properties_get_int( properties, "height" );
+ double input_ar = aspect_ratio * real_width / real_height;
+ double output_ar = mlt_properties_get_double( properties, "consumer_aspect_ratio" ) * owidth / oheight;
+
+// fprintf( stderr, "real %dx%d normalised %dx%d output %dx%d sar %f in-dar %f out-dar %f\n",
+// real_width, real_height, normalised_width, normalised_height, owidth, oheight, aspect_ratio, input_ar, output_ar);
+
+ // Optimised for the input_ar > output_ar case (e.g. widescreen on standard)
+ int scaled_width = rint( ( input_ar * normalised_width ) / output_ar );
+ int scaled_height = normalised_height;
+
+ // Now ensure that our images fit in the output frame
+ if ( scaled_width > normalised_width )
+ {
+ scaled_width = normalised_width;
+ scaled_height = rint( ( output_ar * normalised_height ) / input_ar );
+ }
+
+ // Now calculate the actual image size that we want
+ owidth = rint( scaled_width * owidth / normalised_width );
+ oheight = rint( scaled_height * oheight / normalised_height );
+
+ // Tell frame we have conformed the aspect to the consumer
+ mlt_frame_set_aspect_ratio( this, mlt_properties_get_double( properties, "consumer_aspect_ratio" ) );
+ }
+
+ mlt_properties_set_int( properties, "distort", 0 );
+
+ // Now pass on the calculations down the line
+ mlt_properties_set_int( properties, "resize_width", *width );
+ mlt_properties_set_int( properties, "resize_height", *height );
+
+ // Now get the image
+ if ( *format == mlt_image_yuv422 )
+ owidth -= owidth % 2;
+ error = mlt_frame_get_image( this, image, format, &owidth, &oheight, writable );
+
+ if ( error == 0 && *image )
+ {
+ // Get the requested scale operation
+ char *op = mlt_properties_get( MLT_FILTER_PROPERTIES( filter ), "scale" );
+ int bpp;
+
+ switch ( *format )
+ {
+ case mlt_image_yuv422:
+ bpp = 2;
+ break;
+ case mlt_image_rgb24:
+ bpp = 3;
+ break;
+ case mlt_image_rgb24a:
+ case mlt_image_opengl:
+ bpp = 4;
+ break;
+ default:
+ // XXX: we only know how to resize packed formats
+ return 1;
+ }
+
+ // Provides a manual override for misreported field order
+ if ( mlt_properties_get( properties, "meta.top_field_first" ) )
+ mlt_properties_set_int( properties, "top_field_first", mlt_properties_get_int( properties, "meta.top_field_first" ) );
+
+ // Correct field order if needed
+ if ( mlt_properties_get_int( properties, "top_field_first" ) == 1 &&
+ mlt_properties_get( properties, "progressive" ) &&
+ mlt_properties_get_int( properties, "progressive" ) == 0 )
+ {
+ // Get the input image, width and height
+ int size = owidth * oheight * bpp;
+ uint8_t *ptr = *image + owidth * bpp;
+ memmove( ptr, *image, size - owidth * bpp );
+
+ // Set the normalised field order
+ mlt_properties_set_int( properties, "top_field_first", 0 );
+ mlt_properties_set_int( properties, "meta.top_field_first", 0 );
+ }
+
+ if ( !strcmp( op, "affine" ) )
+ {
+ // TODO: Determine where this is needed and find a different way
+ // *image = mlt_frame_rescale_image( this, *width, *height, bpp );
+ }
+ else if ( strcmp( op, "none" ) != 0 )
+ {
+ *image = frame_resize_image( this, *width, *height, bpp );
+ }
+ else
+ {
+ *width = owidth;
+ *height = oheight;
+ }
+ }
+
+ return error;