f061038c5efa0046865c0ff81fc015eb6f54d26c
[melted] / src / modules / gtk2 / filter_rescale.c
1 /*
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>
5 *
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.
10 *
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.
15 *
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.
19 */
20
21 #include "filter_rescale.h"
22 #include "pixops.h"
23
24 #include <framework/mlt_frame.h>
25
26 #include <stdio.h>
27 #include <string.h>
28 #include <stdlib.h>
29 #include <gdk-pixbuf/gdk-pixbuf.h>
30
31 /** Do it :-).
32 */
33
34 static int filter_get_image( mlt_frame this, uint8_t **image, mlt_image_format *format, int *width, int *height, int writable )
35 {
36 if ( *width == 0 )
37 *width = 720;
38 if ( *height == 0 )
39 *height = 576;
40
41 mlt_properties properties = mlt_frame_properties( this );
42 int iwidth = *width;
43 int iheight = *height;
44 int owidth = *width;
45 int oheight = *height;
46 uint8_t *input = NULL;
47
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;
56
57 mlt_frame_get_image( this, &input, format, &iwidth, &iheight, 0 );
58
59 #if 0
60 // Determine maximum size within the aspect ratio:
61 double i_aspect_ratio = mlt_frame_get_aspect_ratio( this );
62 // TODO: this needs to be provided q
63 #define o_aspect_ratio ( double )( 4.0 / 3.0 )
64
65 if ( ( owidth * i_aspect_ratio * o_aspect_ratio ) > owidth )
66 oheight *= o_aspect_ratio / i_aspect_ratio;
67 else
68 owidth *= i_aspect_ratio * o_aspect_ratio;
69
70 fprintf( stderr, "rescale: from %dx%d (%f) to %dx%d\n", iwidth, iheight, i_aspect_ratio, owidth, oheight );
71 #endif
72
73 // If width and height are correct, don't do anything
74 if ( strcmp( interps, "none" ) && input != NULL && ( iwidth != owidth || iheight != oheight ) )
75 {
76 if ( *format == mlt_image_yuv422 )
77 {
78 // Create the output image
79 uint8_t *output = malloc( owidth * oheight * 2 );
80
81 // Calculate strides
82 int istride = iwidth * 2;
83 int ostride = owidth * 2;
84
85 yuv422_scale_simple( output, owidth, oheight, ostride, input, iwidth, iheight, istride, interp );
86
87 // Now update the frame
88 mlt_properties_set_data( properties, "image", output, owidth * oheight * 2, free, NULL );
89 mlt_properties_set_int( properties, "width", owidth );
90 mlt_properties_set_int( properties, "height", oheight );
91
92 // Return the output
93 *image = output;
94 }
95 else if ( *format == mlt_image_rgb24 || *format == mlt_image_rgb24a )
96 {
97 int bpp = (*format == mlt_image_rgb24a ? 4 : 3 );
98 GdkPixbuf *pixbuf = gdk_pixbuf_new_from_data( input, GDK_COLORSPACE_RGB,
99 (*format == mlt_image_rgb24a), 24, iwidth, iheight,
100 iwidth * bpp, NULL, NULL );
101 GdkPixbuf *scaled = gdk_pixbuf_scale_simple( pixbuf, owidth, oheight, interp );
102
103 // Create the output image
104 uint8_t *output = malloc( owidth * oheight * bpp );
105
106 int i;
107 for ( i = 0; i < oheight; i++ )
108 memcpy( output + i * owidth * bpp,
109 gdk_pixbuf_get_pixels( scaled ) + i * gdk_pixbuf_get_rowstride( scaled ),
110 gdk_pixbuf_get_width( scaled ) * bpp );
111
112 g_object_unref( pixbuf );
113 g_object_unref( scaled );
114
115 // Now update the frame
116 mlt_properties_set_data( properties, "image", output, owidth * oheight * bpp, free, NULL );
117 mlt_properties_set_int( properties, "width", owidth );
118 mlt_properties_set_int( properties, "height", oheight );
119
120 // Return the output
121 *image = output;
122 }
123 }
124 else
125 *image = input;
126
127 return 0;
128 }
129
130 /** Filter processing.
131 */
132
133 static mlt_frame filter_process( mlt_filter this, mlt_frame frame )
134 {
135 mlt_frame_push_get_image( frame, filter_get_image );
136 mlt_properties_set( mlt_frame_properties( frame ), "rescale.interp",
137 mlt_properties_get( mlt_filter_properties( this ), "interpolation" ) );
138 return frame;
139 }
140
141 /** Constructor for the filter.
142 */
143
144 mlt_filter filter_rescale_init( char *arg )
145 {
146 mlt_filter this = calloc( sizeof( struct mlt_filter_s ), 1 );
147 if ( mlt_filter_init( this, this ) == 0 )
148 {
149 this->process = filter_process;
150 if ( arg != NULL )
151 mlt_properties_set( mlt_filter_properties( this ), "interpolation", arg );
152 else
153 mlt_properties_set( mlt_filter_properties( this ), "interpolation", "bilinear" );
154 }
155 return this;
156 }
157