f9789a6efc254c4d16b37286da2d4b401a2e702f
[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
32 /** Do it :-).
33 */
34
35 static int filter_get_image( mlt_frame this, uint8_t **image, mlt_image_format *format, int *width, int *height, int writable )
36 {
37 if ( *width == 0 )
38 *width = 720;
39 if ( *height == 0 )
40 *height = 576;
41 if ( *width < 2 || *height < 6 )
42 return 1;
43
44 mlt_properties properties = mlt_frame_properties( this );
45 int iwidth = *width;
46 int iheight = *height;
47 int owidth = *width;
48 int oheight = *height;
49 uint8_t *input = NULL;
50 char *interps = mlt_properties_get( properties, "rescale.interp" );
51 int interp = PIXOPS_INTERP_BILINEAR;
52
53 // If real_width/height exist, we want that as minimum information
54 if ( mlt_properties_get_int( properties, "real_width" ) )
55 {
56 iwidth = mlt_properties_get_int( properties, "real_width" );
57 iheight = mlt_properties_get_int( properties, "real_height" );
58 }
59
60 // Let the producer know what we are actually requested to obtain
61 if ( *format == mlt_image_yuv422 && strcmp( interps, "none" ) )
62 {
63 mlt_properties_set_int( properties, "rescale_width", *width );
64 mlt_properties_set_int( properties, "rescale_height", *height );
65 }
66 else
67 {
68 // When no scaling is requested, revert the requested dimensions if possible
69 mlt_properties_set_int( properties, "rescale_width", ( iwidth / 2 ) * 2 );
70 mlt_properties_set_int( properties, "rescale_height", ( iheight / 2 ) * 2 );
71 }
72
73 // Get the image as requested
74 mlt_frame_get_image( this, &input, format, &iwidth, &iheight, writable );
75
76 // Get rescale interpretation again, in case the producer wishes to override scaling
77 interps = mlt_properties_get( properties, "rescale.interp" );
78
79 if ( strcmp( interps, "nearest" ) == 0 )
80 interp = PIXOPS_INTERP_NEAREST;
81 else if ( strcmp( interps, "tiles" ) == 0 )
82 interp = PIXOPS_INTERP_TILES;
83 else if ( strcmp( interps, "hyper" ) == 0 )
84 interp = PIXOPS_INTERP_HYPER;
85
86 if ( input != NULL )
87 {
88 // If width and height are correct, don't do anything
89 if ( *format == mlt_image_yuv422 && strcmp( interps, "none" ) && ( iwidth != owidth || iheight != oheight ) )
90 {
91 // Create the output image
92 uint8_t *output = mlt_pool_alloc( owidth * ( oheight + 1 ) * 2 );
93
94 // Calculate strides
95 int istride = iwidth * 2;
96 int ostride = owidth * 2;
97
98 yuv422_scale_simple( output, owidth, oheight, ostride, input, iwidth, iheight, istride, interp );
99
100 // Now update the frame
101 mlt_properties_set_data( properties, "image", output, owidth * ( oheight + 1 ) * 2, ( mlt_destructor )mlt_pool_release, NULL );
102 mlt_properties_set_int( properties, "width", owidth );
103 mlt_properties_set_int( properties, "height", oheight );
104
105 // Return the output
106 *image = output;
107 }
108 else if ( *format == mlt_image_yuv422 && !strcmp( interps, "none" ) )
109 {
110 // Do nothing
111 *width = iwidth;
112 *height = iheight;
113 *image = input;
114 }
115 else if ( *format == mlt_image_rgb24 || *format == mlt_image_rgb24a )
116 {
117 int bpp = (*format == mlt_image_rgb24a ? 4 : 3 );
118
119 // Create the yuv image
120 uint8_t *output = mlt_pool_alloc( owidth * ( oheight + 1 ) * 2 );
121
122 if ( strcmp( interps, "none" ) && ( iwidth != owidth || iheight != oheight ) )
123 {
124 GdkPixbuf *pixbuf = gdk_pixbuf_new_from_data( input, GDK_COLORSPACE_RGB,
125 ( *format == mlt_image_rgb24a ), 8, iwidth, iheight,
126 iwidth * bpp, NULL, NULL );
127
128 GdkPixbuf *scaled = gdk_pixbuf_scale_simple( pixbuf, owidth, oheight, interp );
129 g_object_unref( pixbuf );
130
131 // Extract YUV422 and alpha
132 if ( bpp == 4 )
133 {
134 // Allocate the alpha mask
135 uint8_t *alpha = mlt_pool_alloc( owidth * ( oheight + 1 ) );
136
137 // Convert the image and extract alpha
138 mlt_convert_rgb24a_to_yuv422( gdk_pixbuf_get_pixels( scaled ),
139 owidth, oheight,
140 gdk_pixbuf_get_rowstride( scaled ),
141 output, alpha );
142
143 mlt_properties_set_data( properties, "alpha", alpha, owidth * ( oheight + 1 ), ( mlt_destructor )mlt_pool_release, NULL );
144 }
145 else
146 {
147 // No alpha to extract
148 mlt_convert_rgb24_to_yuv422( gdk_pixbuf_get_pixels( scaled ),
149 owidth, oheight,
150 gdk_pixbuf_get_rowstride( scaled ),
151 output );
152 }
153 g_object_unref( scaled );
154 }
155 else
156 {
157 // Extract YUV422 and alpha
158 if ( bpp == 4 )
159 {
160 // Allocate the alpha mask
161 uint8_t *alpha = mlt_pool_alloc( owidth * ( oheight + 1 ) );
162
163 // Convert the image and extract alpha
164 mlt_convert_rgb24a_to_yuv422( input,
165 owidth, oheight,
166 owidth * 4,
167 output, alpha );
168
169 mlt_properties_set_data( properties, "alpha", alpha, owidth * ( oheight + 1 ), ( mlt_destructor )mlt_pool_release, NULL );
170 }
171 else
172 {
173 // No alpha to extract
174 mlt_convert_rgb24_to_yuv422( input,
175 owidth, oheight,
176 owidth * 3,
177 output );
178 }
179 }
180
181 // Now update the frame
182 mlt_properties_set_data( properties, "image", output, owidth * ( oheight + 1 ) * 2, ( mlt_destructor )mlt_pool_release, NULL );
183 mlt_properties_set_int( properties, "width", owidth );
184 mlt_properties_set_int( properties, "height", oheight );
185
186 // Return the output
187 *format = mlt_image_yuv422;
188 *width = owidth;
189 *height = oheight;
190 *image = output;
191 }
192 else
193 *image = input;
194 }
195 else
196 *image = input;
197
198 return 0;
199 }
200
201 static uint8_t *producer_get_alpha_mask( mlt_frame this )
202 {
203 // Obtain properties of frame
204 mlt_properties properties = mlt_frame_properties( this );
205
206 // Return the alpha mask
207 return mlt_properties_get_data( properties, "alpha", NULL );
208 }
209
210 /** Filter processing.
211 */
212
213 static mlt_frame filter_process( mlt_filter this, mlt_frame frame )
214 {
215 mlt_frame_push_get_image( frame, filter_get_image );
216 mlt_properties_set( mlt_frame_properties( frame ), "rescale.interp",
217 mlt_properties_get( mlt_filter_properties( this ), "interpolation" ) );
218
219 // Set alpha call back
220 frame->get_alpha_mask = producer_get_alpha_mask;
221 return frame;
222 }
223
224 /** Constructor for the filter.
225 */
226
227 mlt_filter filter_rescale_init( char *arg )
228 {
229 mlt_filter this = mlt_filter_new( );
230 if ( this != NULL )
231 {
232 this->process = filter_process;
233 mlt_properties_set( mlt_filter_properties( this ), "interpolation", arg == NULL ? "bilinear" : arg );
234 }
235 return this;
236 }