]> git.sesse.net Git - mlt/blobdiff - src/modules/core/filter_rescale.c
Massive refactoring of image conversion.
[mlt] / src / modules / core / filter_rescale.c
index e606a17461d0f08b1c46cd9b9dfb14f092fb46bb..540e0ca6ff9f1fa0497ded099c1b2437f0d58e94 100644 (file)
  * Copyright (C) 2003-2004 Ushodaya Enterprises Limited
  * Author: Dan Dennedy <dan@dennedy.org>
  *
- * This program is free software; you can redistribute it and/or modify
- * it under the terms of the GNU General Public License as published by
- * the Free Software Foundation; either version 2 of the License, or
- * (at your option) any later version.
+ * This library is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 2.1 of the License, or (at your option) any later version.
  *
- * This program is distributed in the hope that it will be useful,
+ * This library is distributed in the hope that it will be useful,
  * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
- * GNU General Public License for more details.
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+ * Lesser General Public License for more details.
  *
- * You should have received a copy of the GNU General Public License
- * along with this program; if not, write to the Free Software Foundation,
- * Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this library; if not, write to the Free Software
+ * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
  */
 
-#include "filter_rescale.h"
-
+#include <framework/mlt_filter.h>
 #include <framework/mlt_frame.h>
+#include <framework/mlt_log.h>
 
 #include <stdio.h>
 #include <string.h>
 #include <stdlib.h>
+#include <math.h>
+
+/** virtual function declaration for an image scaler
+ *
+ * image scaler implementations are expected to support the following in and out formats:
+ * yuv422 -> yuv422
+ * rgb24 -> rgb24
+ * rgb24a -> rgb24a
+ * rgb24 -> yuv422
+ * rgb24a -> yuv422
+ */
 
-typedef int ( *image_scaler )( mlt_frame this, uint8_t **image, mlt_image_format iformat, mlt_image_format oformat, int iwidth, int iheight, int owidth, int oheight );
+typedef int ( *image_scaler )( mlt_frame this, uint8_t **image, mlt_image_format *format, int iwidth, int iheight, int owidth, int oheight );
 
-static int filter_scale( mlt_frame this, uint8_t **image, mlt_image_format iformat, mlt_image_format oformat, int iwidth, int iheight, int owidth, int oheight )
+static int filter_scale( mlt_frame this, uint8_t **image, mlt_image_format *format, int iwidth, int iheight, int owidth, int oheight )
 {
        // Get the properties
-       mlt_properties properties = mlt_frame_properties( this );
+       mlt_properties properties = MLT_FRAME_PROPERTIES( this );
 
-       // Get the rescaling interpolsation
-       char *interps = mlt_properties_get( properties, "rescale.interp" );
+       // Convert the image to yuv422
+       *format = mlt_image_yuv422;
+       mlt_frame_get_image( this, image, format, &iwidth, &iheight, 0 );
 
-       // Carry out the rescaling
-       if ( iformat == mlt_image_yuv422 && oformat == mlt_image_yuv422 )
-       {
-               // Scale the frame
-               mlt_frame_rescale_yuv422( this, owidth, oheight );
-               
-               // Return the output
-               *image = mlt_properties_get_data( properties, "image", NULL );
-       }
-       else if ( iformat == mlt_image_rgb24 || iformat == mlt_image_rgb24a )
-       {
-               int bpp = (iformat == mlt_image_rgb24a ? 4 : 3 );
-                       
-               // Create the yuv image
-               uint8_t *output = mlt_pool_alloc( iwidth * ( iheight + 1 ) * 2 );
+       // Create the output image
+       uint8_t *output = mlt_pool_alloc( owidth * ( oheight + 1 ) * 2 );
 
-               if ( strcmp( interps, "none" ) && ( iwidth != owidth || iheight != oheight ) )
-               {
-                       // Extract YUV422 and alpha
-                       if ( bpp == 4 )
-                       {
-                               // Allocate the alpha mask
-                               uint8_t *alpha = mlt_pool_alloc( iwidth * ( iheight + 1 ) );
+       // Calculate strides
+       int istride = iwidth * 2;
+       int ostride = owidth * 2;
+       iwidth = iwidth - ( iwidth % 4 );
 
-                               // Convert the image and extract alpha
-                               mlt_convert_rgb24a_to_yuv422( *image, iwidth, iheight, iwidth * 2, output, alpha );
+       // Derived coordinates
+       int dy, dx;
 
-                               mlt_properties_set_data( properties, "alpha", alpha, iwidth * ( iheight + 1 ), ( mlt_destructor )mlt_pool_release, NULL );
-                       }
-                       else
-                       {
-                               // No alpha to extract
-                               mlt_convert_rgb24_to_yuv422( *image, iwidth, iheight, iwidth * 2, output );
-                       }
+       // Calculate ranges
+       int out_x_range = owidth / 2;
+       int out_y_range = oheight / 2;
+       int in_x_range = iwidth / 2;
+       int in_y_range = iheight / 2;
 
-                       // Scale the frame
-                       mlt_frame_rescale_yuv422( this, owidth, oheight );
-               
-                       // Return the output
-                       *image = mlt_properties_get_data( properties, "image", NULL );
-               }
-               else
-               {
-                       // Extract YUV422 and alpha
-                       if ( bpp == 4 )
-                       {
-                               // Allocate the alpha mask
-                               uint8_t *alpha = mlt_pool_alloc( owidth * ( oheight + 1 ) );
+       // Output pointers
+       register uint8_t *out_line = output;
+       register uint8_t *out_ptr;
 
-                               // Convert the image and extract alpha
-                               mlt_convert_rgb24a_to_yuv422( *image, owidth, oheight, owidth * 4, output, alpha );
+       // Calculate a middle pointer
+       uint8_t *in_middle = *image + istride * in_y_range + in_x_range * 2;
+       uint8_t *in_line;
 
-                               mlt_properties_set_data( properties, "alpha", alpha, owidth * ( oheight + 1 ), ( mlt_destructor )mlt_pool_release, NULL );
-                       }
-                       else
-                       {
-                               // No alpha to extract
-                               mlt_convert_rgb24_to_yuv422( *image, owidth, oheight, owidth * 3, output );
-                       }
-               }
+       // Generate the affine transform scaling values
+       register int scale_width = ( iwidth << 16 ) / owidth;
+       register int scale_height = ( iheight << 16 ) / oheight;
+       register int base = 0;
 
-               // Now update the frame
-               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 );
+       int outer = out_x_range * scale_width;
+       int bottom = out_y_range * scale_height;
 
-               *image = output;
+       // Loop for the entirety of our output height.
+       for ( dy = - bottom; dy < bottom; dy += scale_height )
+       {
+               // Start at the beginning of the line
+               out_ptr = out_line;
+
+               // Pointer to the middle of the input line
+               in_line = in_middle + ( dy >> 16 ) * istride;
+
+               // Loop for the entirety of our output row.
+               for ( dx = - outer; dx < outer; dx += scale_width )
+               {
+                       base = dx >> 15;
+                       base &= 0xfffffffe;
+                       *out_ptr ++ = *( in_line + base );
+                       base &= 0xfffffffc;
+                       *out_ptr ++ = *( in_line + base + 1 );
+                       dx += scale_width;
+                       base = dx >> 15;
+                       base &= 0xfffffffe;
+                       *out_ptr ++ = *( in_line + base );
+                       base &= 0xfffffffc;
+                       *out_ptr ++ = *( in_line + base + 3 );
+               }
+               // Move to next output line
+               out_line += ostride;
        }
+       // Now update the frame
+       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 );
 
+       *image = output;
        return 0;
 }
 
 static void scale_alpha( mlt_frame this, int iwidth, int iheight, int owidth, int oheight )
 {
+       // Scale the alpha
+       uint8_t *output = NULL;
        uint8_t *input = mlt_frame_get_alpha_mask( this );
-       
+
        if ( input != NULL )
        {
-               uint8_t *output = mlt_pool_alloc( owidth * oheight );
-
-               // Derived coordinates
-               int dy, dx;
-
-       // Calculate ranges
-       int out_x_range = owidth / 2;
-       int out_y_range = oheight / 2;
-       int in_x_range = iwidth / 2;
-       int in_y_range = iheight / 2;
-
-       // Output pointers
-       register uint8_t *out_line = output;
-       register uint8_t *out_ptr;
-
-       // Calculate a middle pointer
-       uint8_t *in_middle = input + iwidth * in_y_range + in_x_range;
-       uint8_t *in_line;
-
-               // Generate the affine transform scaling values
-               register int scale_width = ( iwidth << 16 ) / owidth;
-               register int scale_height = ( iheight << 16 ) / oheight;
-               register int base = 0;
-
-               int outer = out_x_range * scale_width;
-               int bottom = out_y_range * scale_height;
-
-       // Loop for the entirety of our output height.
-       for ( dy = - bottom; dy < bottom; dy += scale_height )
-       {
-               // Start at the beginning of the line
-               out_ptr = out_line;
-       
-               // Pointer to the middle of the input line
-               in_line = in_middle + ( dy >> 16 ) * iwidth;
-
-               // Loop for the entirety of our output row.
-               for ( dx = - outer; dx < outer; dx += scale_width )
-               {
-                               base = dx >> 15;
-                               *out_ptr ++ = *( in_line + base );
-               }
-
-               // Move to next output line
-               out_line += owidth;
-       }
-
-               this->get_alpha_mask = NULL;
-               mlt_properties_set_data( mlt_frame_properties( this ), "alpha", output, 0, mlt_pool_release, NULL );
+               uint8_t *out_line;
+               int x, y;
+               int ox = ( iwidth << 10 ) / owidth;
+               int oy = ( iheight << 10 ) / oheight;
+
+               output = mlt_pool_alloc( owidth * oheight );
+               out_line = output;
+
+               // Loop for the entirety of our output height.
+               for ( y = 0; y < oheight; y ++ )
+                       for ( x = 0; x < owidth; x ++ )
+                               *out_line ++ = *( input + ( ( 512 + ( y * oy * iwidth ) + x * ox ) >> 10 ) );
+
+               // Set it back on the frame
+               mlt_properties_set_data( MLT_FRAME_PROPERTIES( this ), "alpha", output, owidth * oheight, mlt_pool_release, NULL );
        }
 }
 
@@ -172,13 +150,13 @@ static void scale_alpha( mlt_frame this, int iwidth, int iheight, int owidth, in
 static int filter_get_image( mlt_frame this, uint8_t **image, mlt_image_format *format, int *width, int *height, int writable )
 {
        // Get the frame properties
-       mlt_properties properties = mlt_frame_properties( this );
+       mlt_properties properties = MLT_FRAME_PROPERTIES( this );
 
        // Get the filter from the stack
        mlt_filter filter = mlt_frame_pop_service( this );
 
        // Get the filter properties
-       mlt_properties filter_properties = mlt_filter_properties( filter );
+       mlt_properties filter_properties = MLT_FILTER_PROPERTIES( filter );
 
        // Get the image scaler method
        image_scaler scaler_method = mlt_properties_get_data( filter_properties, "method", NULL );
@@ -198,12 +176,11 @@ static int filter_get_image( mlt_frame this, uint8_t **image, mlt_image_format *
                int owidth = *width;
                int oheight = *height;
                char *interps = mlt_properties_get( properties, "rescale.interp" );
-               int wanted_format = *format;
 
                // Default from the scaler if not specifed on the frame
                if ( interps == NULL )
                {
-                       interps = mlt_properties_get( mlt_filter_properties( filter ), "interpolation" );
+                       interps = mlt_properties_get( MLT_FILTER_PROPERTIES( filter ), "interpolation" );
                        mlt_properties_set( properties, "rescale.interp", interps );
                }
        
@@ -215,7 +192,7 @@ static int filter_get_image( mlt_frame this, uint8_t **image, mlt_image_format *
                }
        
                // Let the producer know what we are actually requested to obtain
-               if ( *format == mlt_image_yuv422 && strcmp( interps, "none" ) )
+               if ( strcmp( interps, "none" ) )
                {
                        mlt_properties_set_int( properties, "rescale_width", *width );
                        mlt_properties_set_int( properties, "rescale_height", *height );
@@ -223,64 +200,40 @@ static int filter_get_image( mlt_frame this, uint8_t **image, mlt_image_format *
                else
                {
                        // When no scaling is requested, revert the requested dimensions if possible
-                       mlt_properties_set_int( properties, "rescale_width", ( iwidth / 2 ) * 2 );
+                       mlt_properties_set_int( properties, "rescale_width", iwidth );
                        mlt_properties_set_int( properties, "rescale_height", iheight );
                }
-       
+
+               // Deinterlace if height is changing to prevent fields mixing on interpolation
+               // One exception: non-interpolated, integral scaling
+               if ( iheight != oheight && ( strcmp( interps, "nearest" ) || ( iheight % oheight != 0 ) ) )
+                       mlt_properties_set_int( properties, "consumer_deinterlace", 1 );
+
                // Get the image as requested
                mlt_frame_get_image( this, image, format, &iwidth, &iheight, writable );
 
                // Get rescale interpretation again, in case the producer wishes to override scaling
                interps = mlt_properties_get( properties, "rescale.interp" );
        
-               if ( *image != NULL && ( *format != mlt_image_yuv422 || ( iwidth != owidth || iheight != oheight ) ) )
+               if ( *image && strcmp( interps, "none" ) && ( iwidth != owidth || iheight != oheight ) )
                {
-                       // If the colour space is correct and scaling is off, do nothing
-                       if ( *format == mlt_image_yuv422 && !strcmp( interps, "none" ) )
-                       {
-                               *width = iwidth;
-                               *height = iheight;
-                       }
-                       else if ( *format == mlt_image_yuv422 )
-                       {
-                               // Call the local scaler
-                               scaler_method( this, image, *format, mlt_image_yuv422, iwidth, iheight, owidth, oheight );
-                               *width = owidth;
-                               *height = oheight;
+                       mlt_log_debug( MLT_FILTER_SERVICE( filter ), "%dx%d -> %dx%d (%s)\n",
+                               iwidth, iheight, owidth, oheight, mlt_image_format_name( *format ) );
 
-                               // Scale the alpha
-                               scale_alpha( this, iwidth, iheight, owidth, oheight );
-                       }
-                       else if ( *format == mlt_image_rgb24 && wanted_format == mlt_image_rgb24 )
+                       // If valid colorspace
+                       if ( *format == mlt_image_yuv422 || *format == mlt_image_rgb24 ||
+                            *format == mlt_image_rgb24a || *format == mlt_image_opengl )
                        {
-                               // Call the local scaler
-                               scaler_method( this, image, *format, mlt_image_rgb24, iwidth, iheight, owidth, oheight );
-
-                               // Return the output
+                               // Call the virtual function
+                               scaler_method( this, image, format, iwidth, iheight, owidth, oheight );
                                *width = owidth;
                                *height = oheight;
-
-                               // Scale the alpha
-                               scale_alpha( this, iwidth, iheight, owidth, oheight );
                        }
-                       else if ( *format == mlt_image_rgb24 || *format == mlt_image_rgb24a )
-                       {
-                               // Call the local scaler
-                               scaler_method( this, image, *format, mlt_image_yuv422, iwidth, iheight, owidth, oheight );
-
-                               // Return the output
-                               *format = mlt_image_yuv422;
-                               *width = owidth;
-                               *height = oheight;
-
-                               // Scale the alpha
+                       // Scale the alpha channel only if exists and not correct size
+                       int alpha_size = 0;
+                       mlt_properties_get_data( properties, "alpha", &alpha_size );
+                       if ( alpha_size > 0 && alpha_size != ( owidth * oheight ) && alpha_size != ( owidth * ( oheight + 1 ) ) )
                                scale_alpha( this, iwidth, iheight, owidth, oheight );
-                       }
-                       else
-                       {
-                               *width = iwidth;
-                               *height = iheight;
-                       }
                }
                else
                {
@@ -323,7 +276,7 @@ static mlt_frame filter_process( mlt_filter this, mlt_frame frame )
 /** Constructor for the filter.
 */
 
-mlt_filter filter_rescale_init( char *arg )
+mlt_filter filter_rescale_init( mlt_profile profile, mlt_service_type type, const char *id, char *arg )
 {
        // Create a new scaler
        mlt_filter this = mlt_filter_new( );
@@ -332,7 +285,7 @@ mlt_filter filter_rescale_init( char *arg )
        if ( this != NULL )
        {
                // Get the properties
-               mlt_properties properties = mlt_filter_properties( this );
+               mlt_properties properties = MLT_FILTER_PROPERTIES( this );
 
                // Set the process method
                this->process = filter_process;