]> git.sesse.net Git - mlt/blobdiff - src/modules/core/filter_crop.c
Remove unused string.h include
[mlt] / src / modules / core / filter_crop.c
index 14edfedcfe05619d0f76b7f3182abf48a99a42c6..8f3b7765042315e8e4f13afbc2329c428b63d3f5 100644 (file)
@@ -21,6 +21,7 @@
 #include <framework/mlt_filter.h>
 #include <framework/mlt_frame.h>
 #include <framework/mlt_log.h>
+#include <framework/mlt_profile.h>
 
 #include <stdio.h>
 #include <string.h>
 
 static void crop( uint8_t *src, uint8_t *dest, int bpp, int width, int height, int left, int right, int top, int bottom )
 {
-       int stride = ( width - left - right ) * bpp;
+       int src_stride = ( width  ) * bpp;
+       int dest_stride = ( width - left - right ) * bpp;
        int y      = height - top - bottom + 1;
-       uint8_t *s = &src[ ( ( top * width ) + left ) * bpp ];
+       src += top * src_stride + left * bpp;
 
        while ( --y )
        {
-               memcpy( dest, sstride );
-               dest += stride;
-               s += stride + ( right + left ) * bpp;
+               memcpy( dest, src, dest_stride );
+               dest += dest_stride;
+               src += src_stride;
        }
 }
 
 /** 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;
+       mlt_profile profile = mlt_frame_pop_service( frame );
 
        // Get the properties from the frame
-       mlt_properties properties = MLT_FRAME_PROPERTIES( this );
+       mlt_properties properties = MLT_FRAME_PROPERTIES( frame );
 
        // 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;
        }
 
        int left    = mlt_properties_get_int( properties, "crop.left" );
@@ -63,8 +66,15 @@ static int filter_get_image( mlt_frame this, uint8_t **image, mlt_image_format *
        int top     = mlt_properties_get_int( properties, "crop.top" );
        int bottom  = mlt_properties_get_int( properties, "crop.bottom" );
 
+       // Request the image at its original resolution
+       if ( left || right || top || bottom )
+       {
+               mlt_properties_set_int( properties, "rescale_width", mlt_properties_get_int( properties, "crop.original_width" ) );
+               mlt_properties_set_int( properties, "rescale_height", mlt_properties_get_int( properties, "crop.original_height" ) );
+       }
+
        // Now get the image
-       error = mlt_frame_get_image( this, image, format, width, height, writable );
+       error = mlt_frame_get_image( frame, image, format, width, height, writable );
 
        int owidth  = *width - left - right;
        int oheight = *height - top - bottom;
@@ -76,57 +86,43 @@ static int filter_get_image( mlt_frame this, uint8_t **image, mlt_image_format *
        {
                int bpp;
 
-               switch ( *format )
+               // Subsampled YUV is messy and less precise.
+               if ( *format == mlt_image_yuv422 && frame->convert_image )
                {
-                       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 crop 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" ) );
-                       mlt_properties_set_int( properties, "meta.top_field_first", 0 );
+                       mlt_image_format requested_format = mlt_image_rgb24;
+                       frame->convert_image( frame, image, format, requested_format );
                }
+       
+               mlt_log_debug( NULL, "[filter crop] %s %dx%d -> %dx%d\n", mlt_image_format_name(*format),
+                                *width, *height, owidth, oheight);
 
                if ( top % 2 )
                        mlt_properties_set_int( properties, "top_field_first", !mlt_properties_get_int( properties, "top_field_first" ) );
                
                // Create the output image
-               uint8_t *output = mlt_pool_alloc( owidth * ( oheight + 1 ) * bpp );
+               int size = mlt_image_format_size( *format, owidth, oheight, &bpp );
+               uint8_t *output = mlt_pool_alloc( size );
                if ( output )
                {
                        // Call the generic resize
                        crop( *image, output, bpp, *width, *height, left, right, top, bottom );
 
                        // Now update the frame
+                       mlt_frame_set_image( frame, output, size, mlt_pool_release );
                        *image = output;
-                       mlt_properties_set_data( properties, "image", output, owidth * ( oheight + 1 ) * 2, ( mlt_destructor )mlt_pool_release, NULL );
-                       mlt_properties_set_int( properties, "width", owidth );
-                       mlt_properties_set_int( properties, "height", oheight );
                }
 
                // We should resize the alpha too
-               uint8_t *alpha = mlt_frame_get_alpha_mask( this );
-               if ( alpha != NULL )
+               uint8_t *alpha = mlt_frame_get_alpha_mask( frame );
+               int alpha_size = 0;
+               mlt_properties_get_data( properties, "alpha", &alpha_size );
+               if ( alpha && alpha_size >= ( *width * *height ) )
                {
                        uint8_t *newalpha = mlt_pool_alloc( owidth * oheight );
                        if ( newalpha )
                        {
                                crop( alpha, newalpha, 1, *width, *height, left, right, top, bottom );
-                               mlt_properties_set_data( properties, "alpha", newalpha, owidth * oheight, ( mlt_destructor )mlt_pool_release, NULL );
-                               this->get_alpha_mask = NULL;
+                               mlt_frame_set_alpha( frame, newalpha, owidth * oheight, mlt_pool_release );
                        }
                }
                *width = owidth;
@@ -139,26 +135,68 @@ 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 )
 {
-       if ( mlt_properties_get_int( MLT_FILTER_PROPERTIES( this ), "active" ) )
+       if ( mlt_properties_get_int( MLT_FILTER_PROPERTIES( filter ), "active" ) )
        {
                // Push the get_image method on to the stack
+               mlt_frame_push_service( frame, mlt_service_profile( MLT_FILTER_SERVICE( filter ) ) );
                mlt_frame_push_get_image( frame, filter_get_image );
        }
        else
        {
-               mlt_properties filter_props = MLT_FILTER_PROPERTIES( this );
+               mlt_properties filter_props = MLT_FILTER_PROPERTIES( filter );
                mlt_properties frame_props = MLT_FRAME_PROPERTIES( frame );
                int left   = mlt_properties_get_int( filter_props, "left" );
                int right  = mlt_properties_get_int( filter_props, "right" );
                int top    = mlt_properties_get_int( filter_props, "top" );
                int bottom = mlt_properties_get_int( filter_props, "bottom" );
-               int width  = mlt_properties_get_int( frame_props, "real_width" );
-               int height = mlt_properties_get_int( frame_props, "real_height" );
+               int width  = mlt_properties_get_int( frame_props, "meta.media.width" );
+               int height = mlt_properties_get_int( frame_props, "meta.media.height" );
+               int use_profile = mlt_properties_get_int( filter_props, "use_profile" );
+               mlt_profile profile = mlt_service_profile( MLT_FILTER_SERVICE( filter ) );
+
+               if ( use_profile )
+               {
+                       top = top * height / profile->height;
+                       bottom = bottom * height / profile->height;
+                       left = left * width / profile->width;
+                       right = right * width / profile->width;
+               }
+               if ( mlt_properties_get_int( filter_props, "center" ) )
+               {
+                       double aspect_ratio = mlt_frame_get_aspect_ratio( frame );
+                       if ( aspect_ratio == 0.0 )
+                               aspect_ratio = mlt_profile_sar( profile );
+                       double input_ar = aspect_ratio * width / height;
+                       double output_ar = mlt_profile_dar( mlt_service_profile( MLT_FILTER_SERVICE(filter) ) );
+                       int bias = mlt_properties_get_int( filter_props, "center_bias" );
+                       
+                       if ( input_ar > output_ar )
+                       {
+                               left = right = ( width - rint( output_ar * height / aspect_ratio ) ) / 2;
+                               if ( abs(bias) > left )
+                                       bias = bias < 0 ? -left : left;
+                               else if ( use_profile )
+                                       bias = bias * width / profile->width;
+                               left -= bias;
+                               right += bias;
+                       }
+                       else
+                       {
+                               top = bottom = ( height - rint( aspect_ratio * width / output_ar ) ) / 2;
+                               if ( abs(bias) > top )
+                                       bias = bias < 0 ? -top : top;
+                               else if ( use_profile )
+                                       bias = bias * height / profile->height;
+                               top -= bias;
+                               bottom += bias;
+                       }
+               }               
 
-               left  -= left % 2;
-               right -= right % 2;
+               // Coerce the output to an even width because subsampled YUV with odd widths is too
+               // risky for downstream processing to handle correctly.
+               left += ( width - left - right ) & 1;
                if ( width - left - right < 8 )
                        left = right = 0;
                if ( height - top - bottom < 8 )
@@ -167,8 +205,10 @@ static mlt_frame filter_process( mlt_filter this, mlt_frame frame )
                mlt_properties_set_int( frame_props, "crop.right", right );
                mlt_properties_set_int( frame_props, "crop.top", top );
                mlt_properties_set_int( frame_props, "crop.bottom", bottom );
-               mlt_properties_set_int( frame_props, "real_width", width - left - right );
-               mlt_properties_set_int( frame_props, "real_height", height - top - bottom );
+               mlt_properties_set_int( frame_props, "crop.original_width", width );
+               mlt_properties_set_int( frame_props, "crop.original_height", height );
+               mlt_properties_set_int( frame_props, "meta.media.width", width - left - right );
+               mlt_properties_set_int( frame_props, "meta.media.height", height - top - bottom );
        }
        return frame;
 }
@@ -178,12 +218,12 @@ static mlt_frame filter_process( mlt_filter this, mlt_frame frame )
 
 mlt_filter filter_crop_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;
+               filter->process = filter_process;
                if ( arg )
-                       mlt_properties_set_int( MLT_FILTER_PROPERTIES( this ), "active", atoi( arg ) );
+                       mlt_properties_set_int( MLT_FILTER_PROPERTIES( filter ), "active", atoi( arg ) );
        }
-       return this;
+       return filter;
 }