bug fixes
[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
42 mlt_properties properties = mlt_frame_properties( this );
43 int iwidth = *width;
44 int iheight = *height;
45 int owidth = *width;
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" );
52
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;
59
60 mlt_frame_get_image( this, &input, format, &iwidth, &iheight, writable );
61
62 if ( o_aspect_ratio != 0 && o_aspect_ratio != i_aspect_ratio && mlt_properties_get( properties, "distort" ) == NULL )
63 {
64 int temp_width = i_aspect_ratio / o_aspect_ratio * owidth + 0.5;
65
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;
70 else
71 oheight = i_aspect_ratio / o_aspect_ratio * oheight + 0.5;
72 else
73 owidth = temp_width;
74
75 // Tell frame we have conformed the aspect to the consumer
76 mlt_frame_set_aspect_ratio( this, o_aspect_ratio );
77 }
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 );
79
80 if ( input != NULL )
81 {
82 // If width and height are correct, don't do anything
83 if ( *format == mlt_image_yuv422 && strcmp( interps, "none" ) && ( iwidth != owidth || iheight != oheight ) )
84 {
85 // Create the output image
86 // IRRIGATE ME
87 uint8_t *output = malloc( owidth * ( oheight + 1 ) * 2 );
88
89 // Calculate strides
90 int istride = iwidth * 2;
91 int ostride = owidth * 2;
92
93 yuv422_scale_simple( output, owidth, oheight, ostride, input, iwidth, iheight, istride, interp );
94
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 );
99
100 // Return the output
101 *image = output;
102 }
103 else if ( *format == mlt_image_rgb24 || *format == mlt_image_rgb24a )
104 {
105 int bpp = (*format == mlt_image_rgb24a ? 4 : 3 );
106
107 // Create the yuv image
108 // IRRIGATE ME
109 uint8_t *output = malloc( owidth * ( oheight + 1 ) * 2 );
110
111 if ( strcmp( interps, "none" ) && ( iwidth != owidth || iheight != oheight ) )
112 {
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 );
116
117 GdkPixbuf *scaled = gdk_pixbuf_scale_simple( pixbuf, owidth, oheight, interp );
118 g_object_unref( pixbuf );
119
120 // Extract YUV422 and alpha
121 if ( bpp == 4 )
122 {
123 // Allocate the alpha mask
124 // IRRIGATE ME
125 uint8_t *alpha = malloc( owidth * ( oheight + 1 ) );
126
127 // Convert the image and extract alpha
128 mlt_convert_rgb24a_to_yuv422( gdk_pixbuf_get_pixels( scaled ),
129 owidth, oheight,
130 gdk_pixbuf_get_rowstride( scaled ),
131 output, alpha );
132
133 mlt_properties_set_data( properties, "alpha", alpha, owidth * ( oheight + 1 ), free, NULL );
134 }
135 else
136 {
137 // No alpha to extract
138 mlt_convert_rgb24_to_yuv422( gdk_pixbuf_get_pixels( scaled ),
139 owidth, oheight,
140 gdk_pixbuf_get_rowstride( scaled ),
141 output );
142 }
143 g_object_unref( scaled );
144 }
145 else
146 {
147 // Extract YUV422 and alpha
148 if ( bpp == 4 )
149 {
150 // Allocate the alpha mask
151 // IRRIGATE ME
152 uint8_t *alpha = malloc( owidth * ( oheight + 1 ) );
153
154 // Convert the image and extract alpha
155 mlt_convert_rgb24a_to_yuv422( input,
156 owidth, oheight,
157 owidth * 4,
158 output, alpha );
159
160 mlt_properties_set_data( properties, "alpha", alpha, owidth * ( oheight + 1 ), free, NULL );
161 }
162 else
163 {
164 // No alpha to extract
165 mlt_convert_rgb24_to_yuv422( input,
166 owidth, oheight,
167 owidth * 3,
168 output );
169 }
170 }
171
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 );
176
177 // Return the output
178 *format = mlt_image_yuv422;
179 *width = owidth;
180 *height = oheight;
181 *image = output;
182 }
183 else
184 *image = input;
185 }
186 else
187 *image = input;
188
189 return 0;
190 }
191
192 static uint8_t *producer_get_alpha_mask( mlt_frame this )
193 {
194 // Obtain properties of frame
195 mlt_properties properties = mlt_frame_properties( this );
196
197 // Return the alpha mask
198 return mlt_properties_get_data( properties, "alpha", NULL );
199 }
200
201 /** Filter processing.
202 */
203
204 static mlt_frame filter_process( mlt_filter this, mlt_frame frame )
205 {
206 mlt_frame_push_get_image( frame, filter_get_image );
207 mlt_properties_set( mlt_frame_properties( frame ), "rescale.interp",
208 mlt_properties_get( mlt_filter_properties( this ), "interpolation" ) );
209
210 // Set alpha call back
211 frame->get_alpha_mask = producer_get_alpha_mask;
212 return frame;
213 }
214
215 /** Constructor for the filter.
216 */
217
218 mlt_filter filter_rescale_init( char *arg )
219 {
220 mlt_filter this = calloc( sizeof( struct mlt_filter_s ), 1 );
221 if ( mlt_filter_init( this, this ) == 0 )
222 {
223 this->process = filter_process;
224 if ( arg != NULL )
225 mlt_properties_set( mlt_filter_properties( this ), "interpolation", arg );
226 else
227 mlt_properties_set( mlt_filter_properties( this ), "interpolation", "bilinear" );
228 }
229 return this;
230 }
231