Merge ../mlt
[melted] / src / modules / core / filter_rescale.c
index 1c02a4a..25ca5fc 100644 (file)
@@ -3,35 +3,37 @@
  * 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>
 #include <string.h>
 #include <stdlib.h>
+#include <math.h>
 
 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
-       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" );
@@ -44,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 )
        {
@@ -61,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
                {
@@ -89,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
                        {
@@ -108,19 +120,45 @@ static int filter_scale( mlt_frame this, uint8_t **image, mlt_image_format iform
        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 *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 );
+       }
+}
+
 /** Do it :-).
 */
 
 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 );
@@ -133,7 +171,7 @@ static int filter_get_image( mlt_frame this, uint8_t **image, mlt_image_format *
        }
 
        // There can be problems with small images - avoid them (by hacking - gah)
-       if ( *width >= 2 && *height >= 6 )
+       if ( *width >= 6 && *height >= 6 )
        {
                int iwidth = *width;
                int iheight = *height;
@@ -145,7 +183,7 @@ static int filter_get_image( mlt_frame this, uint8_t **image, mlt_image_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 );
                }
        
@@ -165,13 +203,18 @@ 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" );
        
@@ -239,15 +282,6 @@ static int filter_get_image( mlt_frame this, uint8_t **image, mlt_image_format *
        return 0;
 }
 
-static uint8_t *producer_get_alpha_mask( mlt_frame this )
-{
-       // Obtain properties of frame
-       mlt_properties properties = mlt_frame_properties( this );
-
-       // Return the alpha mask
-       return mlt_properties_get_data( properties, "alpha", NULL );
-}
-
 /** Filter processing.
 */
 
@@ -259,16 +293,13 @@ static mlt_frame filter_process( mlt_filter this, mlt_frame frame )
        // Push the get image method
        mlt_frame_push_service( frame, filter_get_image );
 
-       // Set alpha call back
-       frame->get_alpha_mask = producer_get_alpha_mask;
-
        return 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( );
@@ -277,7 +308,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;