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
)
42 mlt_properties properties
= mlt_frame_properties( this );
44 int iheight
= *height
;
46 int oheight
= *height
;
47 uint8_t *input
= NULL
;
48 char *interps
= mlt_properties_get( properties
, "rescale.interp" );
49 int interp
= PIXOPS_INTERP_BILINEAR
;
50 double i_aspect_ratio
= mlt_frame_get_aspect_ratio( this );
51 double o_aspect_ratio
= mlt_properties_get_double( properties
, "consumer_aspect_ratio" );
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 mlt_frame_get_image( this, &input
, format
, &iwidth
, &iheight
, writable
);
62 if ( o_aspect_ratio
!= 0 && o_aspect_ratio
!= i_aspect_ratio
&& mlt_properties_get( properties
, "distort" ) == NULL
)
64 int temp_width
= i_aspect_ratio
/ o_aspect_ratio
* owidth
+ 0.5;
66 // Determine maximum size within the aspect ratio:
67 if ( temp_width
> owidth
)
68 if ( i_aspect_ratio
> o_aspect_ratio
)
69 oheight
= o_aspect_ratio
/ i_aspect_ratio
* oheight
+ 0.5;
71 oheight
= i_aspect_ratio
/ o_aspect_ratio
* oheight
+ 0.5;
75 // Tell frame we have conformed the aspect to the consumer
76 mlt_frame_set_aspect_ratio( this, o_aspect_ratio
);
78 //fprintf( stderr, "rescale: from %dx%d (aspect %f) to %dx%d (aspect %f)\n", iwidth, iheight, i_aspect_ratio, owidth, oheight, o_aspect_ratio );
82 // If width and height are correct, don't do anything
83 if ( *format
== mlt_image_yuv422
&& strcmp( interps
, "none" ) && ( iwidth
!= owidth
|| iheight
!= oheight
) )
85 // Create the output image
87 uint8_t *output
= malloc( owidth
* ( oheight
+ 1 ) * 2 );
90 int istride
= iwidth
* 2;
91 int ostride
= owidth
* 2;
93 yuv422_scale_simple( output
, owidth
, oheight
, ostride
, input
, iwidth
, iheight
, istride
, interp
);
95 // Now update the frame
96 mlt_properties_set_data( properties
, "image", output
, owidth
* ( oheight
+ 1 ) * 2, free
, NULL
);
97 mlt_properties_set_int( properties
, "width", owidth
);
98 mlt_properties_set_int( properties
, "height", oheight
);
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
109 uint8_t *output
= malloc( owidth
* ( oheight
+ 1 ) * 2 );
111 if ( strcmp( interps
, "none" ) && ( iwidth
!= owidth
|| iheight
!= oheight
) )
113 GdkPixbuf
*pixbuf
= gdk_pixbuf_new_from_data( input
, GDK_COLORSPACE_RGB
,
114 ( *format
== mlt_image_rgb24a
), 8, iwidth
, iheight
,
115 iwidth
* bpp
, NULL
, NULL
);
117 GdkPixbuf
*scaled
= gdk_pixbuf_scale_simple( pixbuf
, owidth
, oheight
, interp
);
118 g_object_unref( pixbuf
);
120 // Extract YUV422 and alpha
123 // Allocate the alpha mask
125 uint8_t *alpha
= malloc( owidth
* ( oheight
+ 1 ) );
127 // Convert the image and extract alpha
128 mlt_convert_rgb24a_to_yuv422( gdk_pixbuf_get_pixels( scaled
),
130 gdk_pixbuf_get_rowstride( scaled
),
133 mlt_properties_set_data( properties
, "alpha", alpha
, owidth
* ( oheight
+ 1 ), free
, NULL
);
137 // No alpha to extract
138 mlt_convert_rgb24_to_yuv422( gdk_pixbuf_get_pixels( scaled
),
140 gdk_pixbuf_get_rowstride( scaled
),
143 g_object_unref( scaled
);
147 // Extract YUV422 and alpha
150 // Allocate the alpha mask
152 uint8_t *alpha
= malloc( owidth
* ( oheight
+ 1 ) );
154 // Convert the image and extract alpha
155 mlt_convert_rgb24a_to_yuv422( input
,
160 mlt_properties_set_data( properties
, "alpha", alpha
, owidth
* ( oheight
+ 1 ), free
, NULL
);
164 // No alpha to extract
165 mlt_convert_rgb24_to_yuv422( input
,
172 // Now update the frame
173 mlt_properties_set_data( properties
, "image", output
, owidth
* ( oheight
+ 1 ) * 2, free
, NULL
);
174 mlt_properties_set_int( properties
, "width", owidth
);
175 mlt_properties_set_int( properties
, "height", oheight
);
191 static uint8_t *producer_get_alpha_mask( mlt_frame
this )
193 // Obtain properties of frame
194 mlt_properties properties
= mlt_frame_properties( this );
196 // Return the alpha mask
197 return mlt_properties_get_data( properties
, "alpha", NULL
);
200 /** Filter processing.
203 static mlt_frame
filter_process( mlt_filter
this, mlt_frame frame
)
205 mlt_frame_push_get_image( frame
, filter_get_image
);
206 mlt_properties_set( mlt_frame_properties( frame
), "rescale.interp",
207 mlt_properties_get( mlt_filter_properties( this ), "interpolation" ) );
209 // Set alpha call back
210 frame
->get_alpha_mask
= producer_get_alpha_mask
;
214 /** Constructor for the filter.
217 mlt_filter
filter_rescale_init( char *arg
)
219 mlt_filter
this = calloc( sizeof( struct mlt_filter_s
), 1 );
220 if ( mlt_filter_init( this, this ) == 0 )
222 this->process
= filter_process
;
224 mlt_properties_set( mlt_filter_properties( this ), "interpolation", arg
);
226 mlt_properties_set( mlt_filter_properties( this ), "interpolation", "bilinear" );