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>
34 static int filter_get_image( mlt_frame
this, uint8_t **image
, mlt_image_format
*format
, int *width
, int *height
, int writable
)
41 mlt_properties properties
= mlt_frame_properties( this );
43 int iheight
= *height
;
45 int oheight
= *height
;
46 uint8_t *input
= NULL
;
48 char *interps
= mlt_properties_get( properties
, "rescale.interp" );
49 int interp
= PIXOPS_INTERP_BILINEAR
;
50 if ( strcmp( interps
, "nearest" ) == 0 )
51 interp
= PIXOPS_INTERP_NEAREST
;
52 else if ( strcmp( interps
, "tiles" ) == 0 )
53 interp
= PIXOPS_INTERP_TILES
;
54 else if ( strcmp( interps
, "hyper" ) == 0 )
55 interp
= PIXOPS_INTERP_HYPER
;
57 mlt_frame_get_image( this, &input
, format
, &iwidth
, &iheight
, 0 );
60 // Determine maximum size within the aspect ratio:
61 double aspect_ratio
= mlt_frame_get_aspect_ratio( this );
62 // TODO: these need to be provided
63 double dsar
= oheight
< 576 ?
(8.0 / 9.0) : (48.0 / 45.0);
64 double ssar
= iheight
< 576 ?
(8.0 / 9.0) : (48.0 / 45.0);
66 if ( ( (double) owidth
* dsar
/ iwidth
* ssar
) < ( (double) oheight
/ iheight
) )
67 oheight
= (double) owidth
* dsar
/ aspect_ratio
;
69 owidth
= (int)( ( aspect_ratio
* oheight
/ dsar
) + 0.5 ) >> 1 << 1;
71 fprintf( stderr
, "rescale: from %dx%d (%f) to %dx%d\n", iwidth
, iheight
, aspect_ratio
, owidth
, oheight
);
74 // If width and height are correct, don't do anything
75 if ( input
!= NULL
&& ( iwidth
!= owidth
|| iheight
!= oheight
) )
77 if ( *format
== mlt_image_yuv422
)
79 // Create the output image
80 uint8_t *output
= malloc( owidth
* oheight
* 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
* 2, free
, NULL
);
90 mlt_properties_set_int( properties
, "width", owidth
);
91 mlt_properties_set_int( properties
, "height", oheight
);
96 else if ( *format
== mlt_image_rgb24
|| *format
== mlt_image_rgb24a
)
98 int bpp
= (*format
== mlt_image_rgb24a ?
4 : 3 );
99 GdkPixbuf
*pixbuf
= gdk_pixbuf_new_from_data( input
, GDK_COLORSPACE_RGB
,
100 (*format
== mlt_image_rgb24a
), 24, iwidth
, iheight
,
101 iwidth
* bpp
, NULL
, NULL
);
102 GdkPixbuf
*scaled
= gdk_pixbuf_scale_simple( pixbuf
, owidth
, oheight
, interp
);
104 // Create the output image
105 uint8_t *output
= malloc( owidth
* oheight
* bpp
);
108 for ( i
= 0; i
< oheight
; i
++ )
109 memcpy( output
+ i
* owidth
* bpp
,
110 gdk_pixbuf_get_pixels( scaled
) + i
* gdk_pixbuf_get_rowstride( scaled
),
111 gdk_pixbuf_get_width( scaled
) * bpp
);
113 g_object_unref( pixbuf
);
114 g_object_unref( scaled
);
116 // Now update the frame
117 mlt_properties_set_data( properties
, "image", output
, owidth
* oheight
* bpp
, free
, NULL
);
118 mlt_properties_set_int( properties
, "width", owidth
);
119 mlt_properties_set_int( properties
, "height", oheight
);
131 /** Filter processing.
134 static mlt_frame
filter_process( mlt_filter
this, mlt_frame frame
)
136 mlt_frame_push_get_image( frame
, filter_get_image
);
137 mlt_properties_set( mlt_frame_properties( frame
), "rescale.interp",
138 mlt_properties_get( mlt_filter_properties( this ), "interpolation" ) );
142 /** Constructor for the filter.
145 mlt_filter
filter_rescale_init( char *arg
)
147 mlt_filter
this = calloc( sizeof( struct mlt_filter_s
), 1 );
148 if ( mlt_filter_init( this, this ) == 0 )
150 this->process
= filter_process
;
152 mlt_properties_set( mlt_filter_properties( this ), "interpolation", arg
);
154 mlt_properties_set( mlt_filter_properties( this ), "interpolation", "bilinear" );