rc/framework/mlt_frame.c
[melted] / src / modules / core / filter_resize.c
index 6b29d98..bd8841b 100644 (file)
 #include <string.h>
 #include <stdlib.h>
 
-static int get_value( mlt_properties properties, char *preferred, char *fallback )
-{
-       int value = mlt_properties_get_int( properties, preferred );
-       if ( value == 0 )
-               value = mlt_properties_get_int( properties, fallback );
-       return value;
-}
-
 /** Do it :-).
 */
 
 static int filter_get_image( mlt_frame this, uint8_t **image, mlt_image_format *format, int *width, int *height, int writable )
 {
+       int error = 0;
+
        // Get the properties from the frame
-       mlt_properties properties = mlt_frame_properties( this );
+       mlt_properties properties = MLT_FRAME_PROPERTIES( this );
+
+       // Pop the top of stack now
+       mlt_filter filter = mlt_frame_pop_service( this );
+
+       // Retrieve the aspect ratio
+       double aspect_ratio = mlt_deque_pop_back_double( MLT_FRAME_IMAGE_STACK( this ) );
 
        // Assign requested width/height from our subordinate
        int owidth = *width;
        int oheight = *height;
 
+       // 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( properties, "distort" ) == NULL )
+       if ( mlt_properties_get_int( properties, "distort" ) == 0 )
        {
-               // Now do additional calcs based on real_width/height etc
+               // 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 = get_value( properties, "real_width", "width" );
-               int real_height = get_value( properties, "real_height", "height" );
-               double input_ar = mlt_frame_get_aspect_ratio( this );
-               double output_ar = mlt_properties_get_double( properties, "consumer_aspect_ratio" );
-               int scaled_width = ( input_ar > output_ar ? input_ar / output_ar : output_ar / input_ar ) * real_width;
-               int scaled_height = ( input_ar > output_ar ? input_ar / output_ar : output_ar / input_ar ) * real_height;
-
-               // Now ensure that our images fit in the normalised frame
+               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, "normalised %dx%d output %dx%d %f %f\n", normalised_width, normalised_height, owidth, oheight, ( float )output_ar, ( float )mlt_properties_get_double( properties, "consumer_aspect_ratio" ) * owidth / oheight );
+
+               // Optimised for the input_ar > output_ar case (e.g. widescreen on standard)
+               int scaled_width = input_ar / output_ar * normalised_width + 0.5;
+               int scaled_height = normalised_height;
+
+               // Now ensure that our images fit in the output frame
                if ( scaled_width > normalised_width )
                {
-                       scaled_height = scaled_height * normalised_width / scaled_width;
                        scaled_width = normalised_width;
-               }
-               if ( scaled_height > normalised_height )
-               {
-                       scaled_width = scaled_width * normalised_height / scaled_height;
-                       scaled_height = normalised_height;
+                       scaled_height = output_ar / input_ar * normalised_height + 0.5;
                }
 
-               if ( input_ar == output_ar && scaled_height == normalised_height )
-               {
-                       scaled_width = normalised_width;
-               }
-               else if ( ( real_height * 2 ) == normalised_height )
-               {
-                       scaled_width = normalised_width;
-                       scaled_height = normalised_height;
-               }
-       
                // Now calculate the actual image size that we want
                owidth = scaled_width * owidth / normalised_width;
                oheight = 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
-       mlt_frame_get_image( this, image, format, &owidth, &oheight, writable );
-       
-       // Correct field order if needed
-       if ( mlt_properties_get_int( properties, "top_field_first" ) == 1 )
+       error = mlt_frame_get_image( this, image, format, &owidth, &oheight, writable );
+
+       // We only know how to process yuv422 at the moment
+       if ( error == 0 && *format == mlt_image_yuv422 && *image != NULL )
        {
-               // Get the input image, width and height
-               int size;
-               uint8_t *image = mlt_properties_get_data( properties, "image", &size );
+               // Get the requested scale operation
+               char *op = mlt_properties_get( MLT_FILTER_PROPERTIES( filter ), "scale" );
 
-               // Keep the original image around to be destroyed on frame close
-               mlt_properties_rename( properties, "image", "original_image" );
+               // Correct field order if needed
+               if ( mlt_properties_get_int( properties, "top_field_first" ) == 1 )
+               {
+                       // Get the input image, width and height
+                       int size;
+                       uint8_t *image = mlt_properties_get_data( properties, "image", &size );
 
-               // Offset the image pointer by one line
-               image += owidth * 2;
-               size -= owidth * 2;
+                       // Keep the original image around to be destroyed on frame close
+                       mlt_properties_rename( properties, "image", "original_image" );
 
-               // Set the new image pointer with no destructor
-               mlt_properties_set_data( properties, "image", image, size, NULL, NULL );
+                       // Duplicate the last line in the field to avoid artifact
+                       memcpy( image + oheight * owidth * 2, image + oheight * owidth * 2 - owidth * 4, owidth * 2 );
 
-               // Set the normalised field order
-               mlt_properties_set_int( properties, "top_field_first", 0 );
-       }
+                       // Offset the image pointer by one line
+                       image += owidth * 2;
+                       size -= owidth * 2;
+                       
+                       // Set the new image pointer with no destructor
+                       mlt_properties_set_data( properties, "image", image, size, NULL, NULL );
 
-       if ( !strcmp( mlt_properties_get( properties, "resize.scale" ), "affine" ) )
-               *image = mlt_frame_rescale_yuv422( this, *width, *height );
-       else if ( strcmp( mlt_properties_get( properties, "resize.scale" ), "none" ) != 0 )
-               *image = mlt_frame_resize_yuv422( this, *width, *height );
-       else
-       {
-               *width = owidth;
-               *height = oheight;
+                       // Set the normalised field order
+                       mlt_properties_set_int( properties, "top_field_first", 0 );
+               }
+
+               if ( !strcmp( op, "affine" ) )
+               {
+                       *image = mlt_frame_rescale_yuv422( this, *width, *height );
+               }
+               else if ( strcmp( op, "none" ) != 0 )
+               {
+                       *image = mlt_frame_resize_yuv422( this, *width, *height );
+               }
+               else
+               {
+                       *width = owidth;
+                       *height = oheight;
+               }
        }
-       return 0;
+
+       return error;
 }
 
 /** Filter processing.
@@ -135,8 +155,15 @@ static int filter_get_image( mlt_frame this, uint8_t **image, mlt_image_format *
 
 static mlt_frame filter_process( mlt_filter this, mlt_frame frame )
 {
+       // Store the aspect ratio reported by the source
+       mlt_deque_push_back_double( MLT_FRAME_IMAGE_STACK( frame ), mlt_frame_get_aspect_ratio( frame ) );
+
+       // Push this on to the service stack
+       mlt_frame_push_service( frame, this );
+
+       // Push the get_image method on to the stack
        mlt_frame_push_get_image( frame, filter_get_image );
-       mlt_properties_set( mlt_frame_properties( frame ), "resize.scale", mlt_properties_get( mlt_filter_properties( this ), "scale" ) );
+
        return frame;
 }
 
@@ -149,11 +176,7 @@ mlt_filter filter_resize_init( char *arg )
        if ( mlt_filter_init( this, this ) == 0 )
        {
                this->process = filter_process;
-               if ( arg != NULL )
-                       mlt_properties_set( mlt_filter_properties( this ), "scale", arg );
-               else
-                       mlt_properties_set( mlt_filter_properties( this ), "scale", "off" );
+               mlt_properties_set( MLT_FILTER_PROPERTIES( this ), "scale", arg == NULL ? "off" : arg );
        }
        return this;
 }
-