2 * filter_rescale.c -- scale the producer video frame size to match the consumer
3 * Copyright (C) 2003-2004 Ushodaya Enterprises Limited
4 * Author: Dan Dennedy <dan@dennedy.org>
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software Foundation,
18 * Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
21 #include "filter_rescale.h"
24 #include <framework/mlt_frame.h>
29 #include <gdk-pixbuf/gdk-pixbuf.h>
35 static int filter_get_image( mlt_frame
this, uint8_t **image
, mlt_image_format
*format
, int *width
, int *height
, int writable
)
41 if ( *width
< 2 || *height
< 6 )
44 mlt_properties properties
= mlt_frame_properties( this );
46 int iheight
= *height
;
48 int oheight
= *height
;
49 uint8_t *input
= NULL
;
50 char *interps
= mlt_properties_get( properties
, "rescale.interp" );
51 int interp
= PIXOPS_INTERP_BILINEAR
;
53 if ( strcmp( interps
, "nearest" ) == 0 )
54 interp
= PIXOPS_INTERP_NEAREST
;
55 else if ( strcmp( interps
, "tiles" ) == 0 )
56 interp
= PIXOPS_INTERP_TILES
;
57 else if ( strcmp( interps
, "hyper" ) == 0 )
58 interp
= PIXOPS_INTERP_HYPER
;
60 // If real_width/height exist, we want that as minimum information
61 if ( mlt_properties_get_int( properties
, "real_width" ) )
63 iwidth
= mlt_properties_get_int( properties
, "real_width" );
64 iheight
= mlt_properties_get_int( properties
, "real_height" );
67 // Let the producer know what we are actually requested to obtain
68 mlt_properties_set_int( properties
, "rescale_width", *width
);
69 mlt_properties_set_int( properties
, "rescale_height", *height
);
71 // Get the image as requested
72 mlt_frame_get_image( this, &input
, format
, &iwidth
, &iheight
, writable
);
76 // If width and height are correct, don't do anything
77 if ( *format
== mlt_image_yuv422
&& strcmp( interps
, "none" ) && ( iwidth
!= owidth
|| iheight
!= oheight
) )
79 // Create the output image
80 uint8_t *output
= mlt_pool_alloc( owidth
* ( oheight
+ 1 ) * 2 );
83 int istride
= iwidth
* 2;
84 int ostride
= owidth
* 2;
86 yuv422_scale_simple( output
, owidth
, oheight
, ostride
, input
, iwidth
, iheight
, istride
, interp
);
88 // Now update the frame
89 mlt_properties_set_data( properties
, "image", output
, owidth
* ( oheight
+ 1 ) * 2, ( mlt_destructor
)mlt_pool_release
, NULL
);
90 mlt_properties_set_int( properties
, "width", owidth
);
91 mlt_properties_set_int( properties
, "height", oheight
);
96 else if ( *format
== mlt_image_yuv422
&& !strcmp( interps
, "none" ) )
103 else if ( *format
== mlt_image_rgb24
|| *format
== mlt_image_rgb24a
)
105 int bpp
= (*format
== mlt_image_rgb24a ?
4 : 3 );
107 // Create the yuv image
108 uint8_t *output
= mlt_pool_alloc( owidth
* ( oheight
+ 1 ) * 2 );
110 if ( strcmp( interps
, "none" ) && ( iwidth
!= owidth
|| iheight
!= oheight
) )
112 GdkPixbuf
*pixbuf
= gdk_pixbuf_new_from_data( input
, GDK_COLORSPACE_RGB
,
113 ( *format
== mlt_image_rgb24a
), 8, iwidth
, iheight
,
114 iwidth
* bpp
, NULL
, NULL
);
116 GdkPixbuf
*scaled
= gdk_pixbuf_scale_simple( pixbuf
, owidth
, oheight
, interp
);
117 g_object_unref( pixbuf
);
119 // Extract YUV422 and alpha
122 // Allocate the alpha mask
123 uint8_t *alpha
= mlt_pool_alloc( owidth
* ( oheight
+ 1 ) );
125 // Convert the image and extract alpha
126 mlt_convert_rgb24a_to_yuv422( gdk_pixbuf_get_pixels( scaled
),
128 gdk_pixbuf_get_rowstride( scaled
),
131 mlt_properties_set_data( properties
, "alpha", alpha
, owidth
* ( oheight
+ 1 ), ( mlt_destructor
)mlt_pool_release
, NULL
);
135 // No alpha to extract
136 mlt_convert_rgb24_to_yuv422( gdk_pixbuf_get_pixels( scaled
),
138 gdk_pixbuf_get_rowstride( scaled
),
141 g_object_unref( scaled
);
145 // Extract YUV422 and alpha
148 // Allocate the alpha mask
149 uint8_t *alpha
= mlt_pool_alloc( owidth
* ( oheight
+ 1 ) );
151 // Convert the image and extract alpha
152 mlt_convert_rgb24a_to_yuv422( input
,
157 mlt_properties_set_data( properties
, "alpha", alpha
, owidth
* ( oheight
+ 1 ), ( mlt_destructor
)mlt_pool_release
, NULL
);
161 // No alpha to extract
162 mlt_convert_rgb24_to_yuv422( input
,
169 // Now update the frame
170 mlt_properties_set_data( properties
, "image", output
, owidth
* ( oheight
+ 1 ) * 2, ( mlt_destructor
)mlt_pool_release
, NULL
);
171 mlt_properties_set_int( properties
, "width", owidth
);
172 mlt_properties_set_int( properties
, "height", oheight
);
175 *format
= mlt_image_yuv422
;
189 static uint8_t *producer_get_alpha_mask( mlt_frame
this )
191 // Obtain properties of frame
192 mlt_properties properties
= mlt_frame_properties( this );
194 // Return the alpha mask
195 return mlt_properties_get_data( properties
, "alpha", NULL
);
198 /** Filter processing.
201 static mlt_frame
filter_process( mlt_filter
this, mlt_frame frame
)
203 mlt_frame_push_get_image( frame
, filter_get_image
);
204 mlt_properties_set( mlt_frame_properties( frame
), "rescale.interp",
205 mlt_properties_get( mlt_filter_properties( this ), "interpolation" ) );
207 // Set alpha call back
208 frame
->get_alpha_mask
= producer_get_alpha_mask
;
212 /** Constructor for the filter.
215 mlt_filter
filter_rescale_init( char *arg
)
217 mlt_filter
this = mlt_filter_new( );
220 this->process
= filter_process
;
221 mlt_properties_set( mlt_filter_properties( this ), "interpolation", arg
== NULL ?
"bilinear" : arg
);