]> git.sesse.net Git - mlt/blobdiff - src/modules/core/filter_resize.c
Fix calloc() parameter ordering
[mlt] / src / modules / core / filter_resize.c
index 2d84b8f250f42ffd8cbdf3d8f4d342160161a010..d3e4ca344632738e9bdfe7cf8e3838bb7d75ef30 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;
@@ -132,14 +123,14 @@ static void resize_image( uint8_t *output, int owidth, int oheight, uint8_t *inp
        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 );
 
@@ -147,7 +138,7 @@ static uint8_t *frame_resize_image( mlt_frame this, int owidth, int oheight, int
        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" );
 
@@ -158,19 +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
                if ( alpha && alpha_size >= iwidth * iheight )
                {
                        alpha = resize_alpha( alpha, owidth, oheight, iwidth, iheight, alpha_value );
                        if ( alpha )
-                       {
-                               mlt_properties_set_data( properties, "alpha", alpha, owidth * oheight, ( mlt_destructor )mlt_pool_release, NULL );
-                               this->get_alpha_mask = NULL;
-                       }
+                               mlt_frame_set_alpha( frame, alpha, owidth * oheight, mlt_pool_release );
                }
 
                // Return the output
@@ -183,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
@@ -209,7 +197,7 @@ 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 );
@@ -217,21 +205,21 @@ static int filter_get_image( mlt_frame this, uint8_t **image, mlt_image_format *
        // 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);
@@ -252,7 +240,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 );
@@ -264,64 +252,13 @@ static int filter_get_image( mlt_frame this, uint8_t **image, mlt_image_format *
        // Now get the image
        if ( *format == mlt_image_yuv422 )
                owidth -= owidth % 2;
-       error = mlt_frame_get_image( this, image, format, &owidth, &oheight, writable );
+       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 &&
-                    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;
-               }
+               mlt_image_format_size( *format, owidth, oheight, &bpp );
+               *image = frame_resize_image( frame, *width, *height, bpp );
        }
 
        return error;
@@ -330,13 +267,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 );
@@ -349,11 +286,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;
 }