]> git.sesse.net Git - mlt/blobdiff - src/modules/gtk2/filter_rescale.c
Merge branch 'master' of dennedy.org:git/mltframework.org/mlt
[mlt] / src / modules / gtk2 / filter_rescale.c
index 31ebac4acdc31272b1e33ca6981f0f36bdc01c3b..e7b574cadd51e0f51cad6b82a3e56bfd532f3d6e 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 "pixops.h"
 
+#include <framework/mlt_filter.h>
 #include <framework/mlt_frame.h>
+#include <framework/mlt_factory.h>
 
 #include <stdio.h>
 #include <string.h>
 #include <stdlib.h>
 #include <gdk-pixbuf/gdk-pixbuf.h>
 
-/** 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_scale( mlt_frame this, uint8_t **image, mlt_image_format *format, int iwidth, int iheight, int owidth, int oheight )
 {
-       if ( *width == 0 )
-               *width = 720;
-       if ( *height == 0 )
-               *height = 576;
-
-       mlt_properties properties = mlt_frame_properties( this );
-       int iwidth = *width;
-       int iheight = *height;
-       int owidth = *width;
-       int oheight = *height;
-       uint8_t *input = NULL;
+       // Get the properties
+       mlt_properties properties = MLT_FRAME_PROPERTIES( this );
+
+       // Get the requested interpolation method
        char *interps = mlt_properties_get( properties, "rescale.interp" );
+
+       // Convert to the GTK flag
        int interp = PIXOPS_INTERP_BILINEAR;
-       double i_aspect_ratio = mlt_frame_get_aspect_ratio( this );
-       double o_aspect_ratio = mlt_properties_get_double( properties, "consumer_aspect_ratio" );
-       
+
        if ( strcmp( interps, "nearest" ) == 0 )
                interp = PIXOPS_INTERP_NEAREST;
        else if ( strcmp( interps, "tiles" ) == 0 )
                interp = PIXOPS_INTERP_TILES;
-       else if ( strcmp( interps, "hyper" ) == 0 )
+       else if ( strcmp( interps, "hyper" ) == 0 || strcmp( interps, "bicubic" ) == 0 )
                interp = PIXOPS_INTERP_HYPER;
 
-       mlt_frame_get_image( this, &input, format, &iwidth, &iheight, 0 );
+       int bpp;
+       int size = mlt_image_format_size( *format, owidth, oheight, &bpp );
 
-       if ( o_aspect_ratio != 0 && o_aspect_ratio != i_aspect_ratio && mlt_properties_get_int( properties, "distort" ) == 0 )
+       // Carry out the rescaling
+       switch ( *format )
        {
-               // Determine maximum size within the aspect ratio:
-
-               if ( ( owidth * i_aspect_ratio * o_aspect_ratio ) > owidth )
-                       oheight *= o_aspect_ratio / i_aspect_ratio;
-               else
-                       owidth *= i_aspect_ratio * o_aspect_ratio;
-               //fprintf( stderr, "rescale: from %dx%d (aspect %f) to %dx%d (aspect %f)\n", iwidth, iheight, i_aspect_ratio, owidth, oheight, o_aspect_ratio );
-       }
-       
-       // If width and height are correct, don't do anything
-       if ( strcmp( interps, "none" ) && input != NULL && ( iwidth != owidth || iheight != oheight ) )
+       case mlt_image_yuv422:
        {
-               if ( *format == mlt_image_yuv422 )
-               {
-                       // Create the output image
-                       uint8_t *output = malloc( owidth * oheight * 2 );
+               // Create the output image
+               uint8_t *output = mlt_pool_alloc( size );
 
-                       // Calculate strides
-                       int istride = iwidth * 2;
-                       int ostride = owidth * 2;
+               // Calculate strides
+               int istride = iwidth * 2;
+               int ostride = owidth * 2;
 
-                       yuv422_scale_simple( output, owidth, oheight, ostride, input, iwidth, iheight, istride, interp );
+               yuv422_scale_simple( output, owidth, oheight, ostride, *image, iwidth, iheight, istride, interp );
                
-                       // Now update the frame
-                       mlt_properties_set_data( properties, "image", output, owidth * oheight * 2, free, NULL );
-                       mlt_properties_set_int( properties, "width", owidth );
-                       mlt_properties_set_int( properties, "height", oheight );
+               // Now update the frame
+               mlt_frame_set_image( this, output, size, mlt_pool_release );
 
-                       // Return the output
-                       *image = output;
-               }
-               else if ( *format == mlt_image_rgb24 || *format == mlt_image_rgb24a )
+               // Return the output
+               *image = output;
+               break;
+       }
+       case mlt_image_rgb24:
+       case mlt_image_rgb24a:
+       case mlt_image_opengl:
+       {
+               // Create the output image
+               uint8_t *output = mlt_pool_alloc( size );
+
+               if ( strcmp( interps, "none" ) && ( iwidth != owidth || iheight != oheight ) )
                {
-                       int bpp = (*format == mlt_image_rgb24a ? 4 : 3 );
-                       GdkPixbuf *pixbuf = gdk_pixbuf_new_from_data( input, GDK_COLORSPACE_RGB,
-                               (*format == mlt_image_rgb24a), 24, iwidth, iheight,
+                       GdkPixbuf *pixbuf = gdk_pixbuf_new_from_data( *image, GDK_COLORSPACE_RGB,
+                               ( *format == mlt_image_rgb24a || *format == mlt_image_opengl ), 8, iwidth, iheight,
                                iwidth * bpp, NULL, NULL );
                        GdkPixbuf *scaled = gdk_pixbuf_scale_simple( pixbuf, owidth, oheight, interp );
+                       g_object_unref( pixbuf );
 
-                       // Create the output image
-                       uint8_t *output = malloc( owidth * oheight * bpp );
-
-                       int i;
-                       for ( i = 0; i < oheight; i++ )
-                               memcpy( output + i * owidth * bpp,
-                                               gdk_pixbuf_get_pixels( scaled ) + i * gdk_pixbuf_get_rowstride( scaled ),
-                                               gdk_pixbuf_get_width( scaled ) * bpp );
+                       int src_stride = gdk_pixbuf_get_rowstride( scaled );
+                       int dst_stride = owidth * bpp;
+                       if ( src_stride != dst_stride )
+                       {
+                               int y = oheight;
+                               uint8_t *src = gdk_pixbuf_get_pixels( scaled );
+                               uint8_t *dst = output;
+                               while ( y-- )
+                               {
+                                       memcpy( dst, src, dst_stride );
+                                       dst += dst_stride;
+                                       src += src_stride;
+                               }
+                       }
+                       else
+                       {
+                               memcpy( output, gdk_pixbuf_get_pixels( scaled ), owidth * oheight * bpp );
+                       }
 
-                       g_object_unref( pixbuf );
                        g_object_unref( scaled );
-                       
-                       // Now update the frame
-                       mlt_properties_set_data( properties, "image", output, owidth * oheight * bpp, free, NULL );
-                       mlt_properties_set_int( properties, "width", owidth );
-                       mlt_properties_set_int( properties, "height", oheight );
 
+                       // Now update the frame
+                       mlt_frame_set_image( this, output, size, mlt_pool_release );
+       
                        // Return the output
                        *image = output;
                }
+               break;
+       }
+       default:
+               break;
        }
-       else
-               *image = input;
-               
-       return 0;
-}
-
-/** Filter processing.
-*/
 
-static mlt_frame filter_process( mlt_filter this, mlt_frame frame )
-{
-       mlt_frame_push_get_image( frame, filter_get_image );
-       mlt_properties_set( mlt_frame_properties( frame ), "rescale.interp",
-               mlt_properties_get( mlt_filter_properties( this ), "interpolation" ) );
-       return frame;
+       return 0;
 }
 
 /** Constructor for the filter.
 */
 
-mlt_filter filter_rescale_init( char *arg )
+mlt_filter filter_rescale_init( mlt_profile profile, char *arg )
 {
-       mlt_filter this = calloc( sizeof( struct mlt_filter_s ), 1 );
-       if ( mlt_filter_init( this, this ) == 0 )
+       // Create a new scaler
+       mlt_filter this = mlt_factory_filter( profile, "rescale", arg );
+
+       // If successful, then initialise it
+       if ( this != NULL )
        {
-               this->process = filter_process;
-               if ( arg != NULL )
-                       mlt_properties_set( mlt_filter_properties( this ), "interpolation", arg );
-               else
-                       mlt_properties_set( mlt_filter_properties( this ), "interpolation", "bilinear" );
+               // Get the properties
+               mlt_properties properties = MLT_FILTER_PROPERTIES( this );
+
+               // Set the inerpolation
+               mlt_properties_set( properties, "interpolation", arg == NULL ? "bilinear" : arg );
+
+               // Set the method
+               mlt_properties_set_data( properties, "method", filter_scale, 0, NULL, NULL );
        }
+
        return this;
 }