]> git.sesse.net Git - mlt/blobdiff - src/modules/core/filter_resize.c
Remove unused string.h include
[mlt] / src / modules / core / filter_resize.c
index e57c90ff47518a65e60341aeeac6f541b3740412..06e5b9a0001566d40fd89b3293ecfa0456b1d2be 100644 (file)
 
 #include <framework/mlt_filter.h>
 #include <framework/mlt_frame.h>
+#include <framework/mlt_profile.h>
 
 #include <stdio.h>
 #include <string.h>
 #include <stdlib.h>
 #include <math.h>
 
-/** Swapbytes inline.
-*/
-
-static inline void swap_bytes( uint8_t *upper, uint8_t *lower )
-{
-       uint8_t t = *lower;
-       *lower = *upper;
-       *upper = t;
-}
-
 static uint8_t *resize_alpha( uint8_t *input, int owidth, int oheight, int iwidth, int iheight, uint8_t alpha_value )
 {
        uint8_t *output = NULL;
@@ -118,7 +109,7 @@ static void resize_image( uint8_t *output, int owidth, int oheight, uint8_t *inp
        while ( iheight -- )
        {
                // We're in the input range for this row.
-               memcpy( out_line, in_line, iwidth * bpp );
+               memcpy( out_line, in_line, istride );
 
                // Move to next input line
                in_line += istride;
@@ -128,24 +119,26 @@ static void resize_image( uint8_t *output, int owidth, int oheight, uint8_t *inp
        }
 }
 
-/** A resizing function for yuv422 frames - this does not rescale, but simply
-       resizes. It assumes yuv422 images available on the frame so use with care.
+/** A padding function for frames - this does not rescale, but simply
+       resizes.
 */
 
-static uint8_t *frame_resize_image( mlt_frame this, int owidth, int oheight, int bpp )
+static uint8_t *frame_resize_image( mlt_frame frame, int owidth, int oheight, int bpp )
 {
        // Get properties
-       mlt_properties properties = MLT_FRAME_PROPERTIES( this );
+       mlt_properties properties = MLT_FRAME_PROPERTIES( frame );
 
        // Get the input image, width and height
        uint8_t *input = mlt_properties_get_data( properties, "image", NULL );
-       uint8_t *alpha = mlt_frame_get_alpha_mask( this );
+       uint8_t *alpha = mlt_frame_get_alpha_mask( frame );
+       int alpha_size = 0;
+       mlt_properties_get_data( properties, "alpha", &alpha_size );
 
        int iwidth = mlt_properties_get_int( properties, "width" );
        int iheight = mlt_properties_get_int( properties, "height" );
 
        // If width and height are correct, don't do anything
-       if ( iwidth != owidth || iheight != oheight )
+       if ( iwidth < owidth || iheight < oheight )
        {
                uint8_t alpha_value = mlt_properties_get_int( properties, "resize_alpha" );
 
@@ -156,16 +149,14 @@ static uint8_t *frame_resize_image( mlt_frame this, int owidth, int oheight, int
                resize_image( output, owidth, oheight, input, iwidth, iheight, bpp );
 
                // Now update the frame
-               mlt_properties_set_data( properties, "image", output, owidth * ( oheight + 1 ) * bpp, ( mlt_destructor )mlt_pool_release, NULL );
-               mlt_properties_set_int( properties, "width", owidth );
-               mlt_properties_set_int( properties, "height", oheight );
+               mlt_frame_set_image( frame, output, owidth * ( oheight + 1 ) * bpp, mlt_pool_release );
 
                // We should resize the alpha too
-               alpha = resize_alpha( alpha, owidth, oheight, iwidth, iheight, alpha_value );
-               if ( alpha != NULL )
+               if ( alpha && alpha_size >= iwidth * iheight )
                {
-                       mlt_properties_set_data( properties, "alpha", alpha, owidth * oheight, ( mlt_destructor )mlt_pool_release, NULL );
-                       this->get_alpha_mask = NULL;
+                       alpha = resize_alpha( alpha, owidth, oheight, iwidth, iheight, alpha_value );
+                       if ( alpha )
+                               mlt_frame_set_alpha( frame, alpha, owidth * oheight, mlt_pool_release );
                }
 
                // Return the output
@@ -178,24 +169,26 @@ static uint8_t *frame_resize_image( mlt_frame this, int owidth, int oheight, int
 /** Do it :-).
 */
 
-static int filter_get_image( mlt_frame this, uint8_t **image, mlt_image_format *format, int *width, int *height, int writable )
+static int filter_get_image( mlt_frame frame, 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( frame );
 
        // Pop the top of stack now
-       mlt_filter filter = mlt_frame_pop_service( this );
+       mlt_filter filter = mlt_frame_pop_service( frame );
+       mlt_profile profile = mlt_service_profile( MLT_FILTER_SERVICE( filter ) );
 
        // Retrieve the aspect ratio
-       double aspect_ratio = mlt_deque_pop_back_double( MLT_FRAME_IMAGE_STACK( this ) );
+       double aspect_ratio = mlt_deque_pop_back_double( MLT_FRAME_IMAGE_STACK( frame ) );
+       double consumer_aspect = mlt_profile_sar( mlt_service_profile( MLT_FILTER_SERVICE( filter ) ) );
 
        // Correct Width/height if necessary
        if ( *width == 0 || *height == 0 )
        {
-               *width = mlt_properties_get_int( properties, "normalised_width" );
-               *height = mlt_properties_get_int( properties, "normalised_height" );
+               *width = profile->width;
+               *height = profile->height;
        }
 
        // Assign requested width/height from our subordinate
@@ -204,29 +197,36 @@ static int filter_get_image( mlt_frame this, uint8_t **image, mlt_image_format *
 
        // 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" );
+               aspect_ratio = consumer_aspect;
 
        // Reset the aspect ratio
        mlt_properties_set_double( properties, "aspect_ratio", aspect_ratio );
 
+       // XXX: This is a hack, but it forces the force_full_luma to apply by doing a RGB
+       // conversion because range scaling only occurs on YUV->RGB. And we do it here,
+       // after the deinterlace filter, which only operates in YUV to avoid a YUV->RGB->YUV->?.
+       // Instead, it will go YUV->RGB->?.
+       if ( mlt_properties_get_int( properties, "force_full_luma" ) )
+               *format = mlt_image_rgb24a;
+
        // 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 );
+               return mlt_frame_get_image( frame, 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" );
+               int normalised_width = profile->width;
+               int normalised_height = profile->height;
+               int real_width = mlt_properties_get_int( properties, "meta.media.width" );
+               int real_height = mlt_properties_get_int( properties, "meta.media.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;
+               double output_ar = consumer_aspect * 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);
@@ -247,7 +247,7 @@ static int filter_get_image( mlt_frame this, uint8_t **image, mlt_image_format *
                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_frame_set_aspect_ratio( frame, consumer_aspect );
        }
 
        mlt_properties_set_int( properties, "distort", 0 );
@@ -257,63 +257,15 @@ static int filter_get_image( mlt_frame this, uint8_t **image, mlt_image_format *
        mlt_properties_set_int( properties, "resize_height", *height );
 
        // Now get the image
-       error = mlt_frame_get_image( this, image, format, &owidth, &oheight, writable );
+       if ( *format == mlt_image_yuv422 )
+               owidth -= owidth % 2;
+       error = mlt_frame_get_image( frame, 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 )
-               {
-                       // Get the input image, width and height
-                       int size;
-                       uint8_t *image = mlt_properties_get_data( properties, "image", &size );
-                       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;
-               }
+               mlt_image_format_size( *format, owidth, oheight, &bpp );
+               *image = frame_resize_image( frame, *width, *height, bpp );
        }
 
        return error;
@@ -322,13 +274,13 @@ static int filter_get_image( mlt_frame this, uint8_t **image, mlt_image_format *
 /** Filter processing.
 */
 
-static mlt_frame filter_process( mlt_filter this, mlt_frame frame )
+static mlt_frame filter_process( mlt_filter filter, 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 );
+       mlt_frame_push_service( frame, filter );
 
        // Push the get_image method on to the stack
        mlt_frame_push_get_image( frame, filter_get_image );
@@ -341,11 +293,10 @@ static mlt_frame filter_process( mlt_filter this, mlt_frame frame )
 
 mlt_filter filter_resize_init( mlt_profile profile, mlt_service_type type, const char *id, char *arg )
 {
-       mlt_filter this = calloc( sizeof( struct mlt_filter_s ), 1 );
-       if ( mlt_filter_init( this, this ) == 0 )
+       mlt_filter filter = calloc( 1, sizeof( struct mlt_filter_s ) );
+       if ( mlt_filter_init( filter, filter ) == 0 )
        {
-               this->process = filter_process;
-               mlt_properties_set( MLT_FILTER_PROPERTIES( this ), "scale", arg == NULL ? "off" : arg );
+               filter->process = filter_process;
        }
-       return this;
+       return filter;
 }