Merge ../mlt
[melted] / src / modules / core / filter_rescale.c
index 19b087f..25ca5fc 100644 (file)
@@ -3,23 +3,22 @@
  * 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 <stdio.h>
@@ -29,6 +28,8 @@
 
 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 );
 
+static void scale_alpha( mlt_frame this, 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 )
 {
        // Get the properties
@@ -45,6 +46,12 @@ static int filter_scale( mlt_frame this, uint8_t **image, mlt_image_format iform
                
                // Return the output
                *image = mlt_properties_get_data( properties, "image", NULL );
+
+               // 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 ) )
+                       scale_alpha( this, iwidth, iheight, owidth, oheight );
        }
        else if ( iformat == mlt_image_rgb24 || iformat == mlt_image_rgb24a )
        {
@@ -62,21 +69,23 @@ static int filter_scale( mlt_frame this, uint8_t **image, mlt_image_format iform
                                uint8_t *alpha = mlt_pool_alloc( iwidth * ( iheight + 1 ) );
 
                                // Convert the image and extract alpha
-                               mlt_convert_rgb24a_to_yuv422( *image, iwidth, iheight, iwidth * 2, output, alpha );
+                               mlt_convert_rgb24a_to_yuv422( *image, iwidth, iheight, iwidth * 4, output, alpha );
 
                                mlt_properties_set_data( properties, "alpha", alpha, iwidth * ( iheight + 1 ), ( mlt_destructor )mlt_pool_release, NULL );
+
+                               scale_alpha( this, iwidth, iheight, owidth, oheight );
                        }
                        else
                        {
                                // No alpha to extract
-                               mlt_convert_rgb24_to_yuv422( *image, iwidth, iheight, iwidth * 2, output );
+                               mlt_convert_rgb24_to_yuv422( *image, iwidth, iheight, iwidth * 3, output );
                        }
 
+                       mlt_properties_set_data( properties, "image", output, iwidth * ( iheight + 1 ) * 2, ( mlt_destructor )mlt_pool_release, NULL );
+
                        // Scale the frame
-                       mlt_frame_rescale_yuv422( this, owidth, oheight );
-               
-                       // Return the output
-                       *image = mlt_properties_get_data( properties, "image", NULL );
+                       output = mlt_frame_rescale_yuv422( this, owidth, oheight );
+
                }
                else
                {
@@ -90,6 +99,8 @@ static int filter_scale( mlt_frame this, uint8_t **image, mlt_image_format iform
                                mlt_convert_rgb24a_to_yuv422( *image, owidth, oheight, owidth * 4, output, alpha );
 
                                mlt_properties_set_data( properties, "alpha", alpha, owidth * ( oheight + 1 ), ( mlt_destructor )mlt_pool_release, NULL );
+
+                               scale_alpha( this, iwidth, iheight, owidth, oheight );
                        }
                        else
                        {
@@ -111,6 +122,7 @@ static int filter_scale( mlt_frame this, uint8_t **image, mlt_image_format iform
 
 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 );
 
@@ -194,7 +206,12 @@ static int filter_get_image( mlt_frame this, uint8_t **image, mlt_image_format *
                        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 );
 
@@ -215,9 +232,6 @@ static int filter_get_image( mlt_frame this, uint8_t **image, mlt_image_format *
                                scaler_method( this, image, *format, mlt_image_yuv422, iwidth, iheight, owidth, oheight );
                                *width = owidth;
                                *height = oheight;
-
-                               // Scale the alpha
-                               scale_alpha( this, iwidth, iheight, owidth, oheight );
                        }
                        else if ( *format == mlt_image_rgb24 && wanted_format == mlt_image_rgb24 )
                        {
@@ -227,9 +241,6 @@ static int filter_get_image( mlt_frame this, uint8_t **image, mlt_image_format *
                                // Return the output
                                *width = owidth;
                                *height = oheight;
-
-                               // Scale the alpha
-                               scale_alpha( this, iwidth, iheight, owidth, oheight );
                        }
                        else if ( *format == mlt_image_rgb24 || *format == mlt_image_rgb24a )
                        {
@@ -240,9 +251,6 @@ static int filter_get_image( mlt_frame this, uint8_t **image, mlt_image_format *
                                *format = mlt_image_yuv422;
                                *width = owidth;
                                *height = oheight;
-
-                               // Scale the alpha
-                               scale_alpha( this, iwidth, iheight, owidth, oheight );
                        }
                        else
                        {
@@ -252,8 +260,6 @@ static int filter_get_image( mlt_frame this, uint8_t **image, mlt_image_format *
                }
                else
                {
-                               // Scale the alpha
-                               scale_alpha( this, iwidth, iheight, owidth, oheight );
                        *width = iwidth;
                        *height = iheight;
                }
@@ -293,7 +299,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( );