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