Optimisations (part 0), pixel v percentage, reworked aspect ratio calcs, ante/post...
[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
51 if ( strcmp( interps, "nearest" ) == 0 )
52 interp = PIXOPS_INTERP_NEAREST;
53 else if ( strcmp( interps, "tiles" ) == 0 )
54 interp = PIXOPS_INTERP_TILES;
55 else if ( strcmp( interps, "hyper" ) == 0 )
56 interp = PIXOPS_INTERP_HYPER;
57
58 // If real_width/height exist, we want that as minimum information
59 if ( mlt_properties_get_int( properties, "real_width" ) )
60 {
61 iwidth = mlt_properties_get_int( properties, "real_width" );
62 iheight = mlt_properties_get_int( properties, "real_height" );
63 }
64
65 // Let the producer know what we are actually requested to obtain
66 mlt_properties_set_int( properties, "rescale_width", *width );
67 mlt_properties_set_int( properties, "rescale_height", *height );
68
69 // Get the image as requested
70 mlt_frame_get_image( this, &input, format, &iwidth, &iheight, writable );
71
72 if ( input != NULL )
73 {
74 // If width and height are correct, don't do anything
75 if ( *format == mlt_image_yuv422 && strcmp( interps, "none" ) && ( iwidth != owidth || iheight != oheight ) )
76 {
77 // Create the output image
78 // IRRIGATE ME
79 uint8_t *output = malloc( owidth * ( oheight + 1 ) * 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 + 1 ) * 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
99 // Create the yuv image
100 // IRRIGATE ME
101 uint8_t *output = malloc( owidth * ( oheight + 1 ) * 2 );
102
103 if ( strcmp( interps, "none" ) && ( iwidth != owidth || iheight != oheight ) )
104 {
105 GdkPixbuf *pixbuf = gdk_pixbuf_new_from_data( input, GDK_COLORSPACE_RGB,
106 ( *format == mlt_image_rgb24a ), 8, iwidth, iheight,
107 iwidth * bpp, NULL, NULL );
108
109 GdkPixbuf *scaled = gdk_pixbuf_scale_simple( pixbuf, owidth, oheight, interp );
110 g_object_unref( pixbuf );
111
112 // Extract YUV422 and alpha
113 if ( bpp == 4 )
114 {
115 // Allocate the alpha mask
116 // IRRIGATE ME
117 uint8_t *alpha = malloc( owidth * ( oheight + 1 ) );
118
119 // Convert the image and extract alpha
120 mlt_convert_rgb24a_to_yuv422( gdk_pixbuf_get_pixels( scaled ),
121 owidth, oheight,
122 gdk_pixbuf_get_rowstride( scaled ),
123 output, alpha );
124
125 mlt_properties_set_data( properties, "alpha", alpha, owidth * ( oheight + 1 ), free, NULL );
126 }
127 else
128 {
129 // No alpha to extract
130 mlt_convert_rgb24_to_yuv422( gdk_pixbuf_get_pixels( scaled ),
131 owidth, oheight,
132 gdk_pixbuf_get_rowstride( scaled ),
133 output );
134 }
135 g_object_unref( scaled );
136 }
137 else
138 {
139 // Extract YUV422 and alpha
140 if ( bpp == 4 )
141 {
142 // Allocate the alpha mask
143 // IRRIGATE ME
144 uint8_t *alpha = malloc( owidth * ( oheight + 1 ) );
145
146 // Convert the image and extract alpha
147 mlt_convert_rgb24a_to_yuv422( input,
148 owidth, oheight,
149 owidth * 4,
150 output, alpha );
151
152 mlt_properties_set_data( properties, "alpha", alpha, owidth * ( oheight + 1 ), free, NULL );
153 }
154 else
155 {
156 // No alpha to extract
157 mlt_convert_rgb24_to_yuv422( input,
158 owidth, oheight,
159 owidth * 3,
160 output );
161 }
162 }
163
164 // Now update the frame
165 mlt_properties_set_data( properties, "image", output, owidth * ( oheight + 1 ) * 2, free, NULL );
166 mlt_properties_set_int( properties, "width", owidth );
167 mlt_properties_set_int( properties, "height", oheight );
168
169 // Return the output
170 *format = mlt_image_yuv422;
171 *width = owidth;
172 *height = oheight;
173 *image = output;
174 }
175 else
176 *image = input;
177 }
178 else
179 *image = input;
180
181 return 0;
182 }
183
184 static uint8_t *producer_get_alpha_mask( mlt_frame this )
185 {
186 // Obtain properties of frame
187 mlt_properties properties = mlt_frame_properties( this );
188
189 // Return the alpha mask
190 return mlt_properties_get_data( properties, "alpha", NULL );
191 }
192
193 /** Filter processing.
194 */
195
196 static mlt_frame filter_process( mlt_filter this, mlt_frame frame )
197 {
198 mlt_frame_push_get_image( frame, filter_get_image );
199 mlt_properties_set( mlt_frame_properties( frame ), "rescale.interp",
200 mlt_properties_get( mlt_filter_properties( this ), "interpolation" ) );
201
202 // Set alpha call back
203 frame->get_alpha_mask = producer_get_alpha_mask;
204 return frame;
205 }
206
207 /** Constructor for the filter.
208 */
209
210 mlt_filter filter_rescale_init( char *arg )
211 {
212 mlt_filter this = calloc( sizeof( struct mlt_filter_s ), 1 );
213 if ( mlt_filter_init( this, this ) == 0 )
214 {
215 this->process = filter_process;
216 if ( arg != NULL )
217 mlt_properties_set( mlt_filter_properties( this ), "interpolation", arg );
218 else
219 mlt_properties_set( mlt_filter_properties( this ), "interpolation", "bilinear" );
220 }
221 return this;
222 }
223