]> git.sesse.net Git - mlt/blob - src/modules/gtk2/filter_rescale.c
Memory pooling part 2 and other optimisations
[mlt] / 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                         uint8_t *output = mlt_pool_alloc( owidth * ( oheight + 1 ) * 2 );
79
80                         // Calculate strides
81                         int istride = iwidth * 2;
82                         int ostride = owidth * 2;
83
84                         yuv422_scale_simple( output, owidth, oheight, ostride, input, iwidth, iheight, istride, interp );
85                 
86                         // Now update the frame
87                         mlt_properties_set_data( properties, "image", output, owidth * ( oheight + 1 ) * 2, ( mlt_destructor )mlt_pool_release, NULL );
88                         mlt_properties_set_int( properties, "width", owidth );
89                         mlt_properties_set_int( properties, "height", oheight );
90
91                         // Return the output
92                         *image = output;
93                 }
94                 else if ( *format == mlt_image_rgb24 || *format == mlt_image_rgb24a )
95                 {
96                         int bpp = (*format == mlt_image_rgb24a ? 4 : 3 );
97                         
98                         // Create the yuv image
99                         uint8_t *output = mlt_pool_alloc( owidth * ( oheight + 1 ) * 2 );
100
101                         if ( strcmp( interps, "none" ) && ( iwidth != owidth || iheight != oheight ) )
102                         {
103                                 GdkPixbuf *pixbuf = gdk_pixbuf_new_from_data( input, GDK_COLORSPACE_RGB,
104                                         ( *format == mlt_image_rgb24a ), 8, iwidth, iheight,
105                                         iwidth * bpp, NULL, NULL );
106
107                                 GdkPixbuf *scaled = gdk_pixbuf_scale_simple( pixbuf, owidth, oheight, interp );
108                                 g_object_unref( pixbuf );
109                                 
110                                 // Extract YUV422 and alpha
111                                 if ( bpp == 4 )
112                                 {
113                                         // Allocate the alpha mask
114                                         uint8_t *alpha = mlt_pool_alloc( owidth * ( oheight + 1 ) );
115
116                                         // Convert the image and extract alpha
117                                         mlt_convert_rgb24a_to_yuv422( gdk_pixbuf_get_pixels( scaled ),
118                                                                                   owidth, oheight,
119                                                                                   gdk_pixbuf_get_rowstride( scaled ),
120                                                                                   output, alpha );
121
122                                         mlt_properties_set_data( properties, "alpha", alpha, owidth * ( oheight + 1 ), ( mlt_destructor )mlt_pool_release, NULL );
123                                 }
124                                 else
125                                 {
126                                         // No alpha to extract
127                                         mlt_convert_rgb24_to_yuv422( gdk_pixbuf_get_pixels( scaled ),
128                                                                                  owidth, oheight,
129                                                                                  gdk_pixbuf_get_rowstride( scaled ),
130                                                                                  output );
131                                 }
132                                 g_object_unref( scaled );
133                         }
134                         else
135                         {
136                                 // Extract YUV422 and alpha
137                                 if ( bpp == 4 )
138                                 {
139                                         // Allocate the alpha mask
140                                         uint8_t *alpha = mlt_pool_alloc( owidth * ( oheight + 1 ) );
141
142                                         // Convert the image and extract alpha
143                                         mlt_convert_rgb24a_to_yuv422( input,
144                                                                                   owidth, oheight,
145                                                                                   owidth * 4,
146                                                                                   output, alpha );
147
148                                         mlt_properties_set_data( properties, "alpha", alpha, owidth * ( oheight + 1 ), ( mlt_destructor )mlt_pool_release, NULL );
149                                 }
150                                 else
151                                 {
152                                         // No alpha to extract
153                                         mlt_convert_rgb24_to_yuv422( input,
154                                                                                  owidth, oheight,
155                                                                                  owidth * 3,
156                                                                                  output );
157                                 }
158                         }
159
160                         // Now update the frame
161                         mlt_properties_set_data( properties, "image", output, owidth * ( oheight + 1 ) * 2, ( mlt_destructor )mlt_pool_release, NULL );
162                         mlt_properties_set_int( properties, "width", owidth );
163                         mlt_properties_set_int( properties, "height", oheight );
164
165                         // Return the output
166                         *format = mlt_image_yuv422;
167                         *width = owidth;
168                         *height = oheight;
169                         *image = output;
170                 }
171                 else
172                         *image = input;
173         }
174         else
175                 *image = input;
176                 
177         return 0;
178 }
179
180 static uint8_t *producer_get_alpha_mask( mlt_frame this )
181 {
182         // Obtain properties of frame
183         mlt_properties properties = mlt_frame_properties( this );
184
185         // Return the alpha mask
186         return mlt_properties_get_data( properties, "alpha", NULL );
187 }
188
189 /** Filter processing.
190 */
191
192 static mlt_frame filter_process( mlt_filter this, mlt_frame frame )
193 {
194         mlt_frame_push_get_image( frame, filter_get_image );
195         mlt_properties_set( mlt_frame_properties( frame ), "rescale.interp",
196                 mlt_properties_get( mlt_filter_properties( this ), "interpolation" ) );
197                 
198         // Set alpha call back
199         frame->get_alpha_mask = producer_get_alpha_mask;
200         return frame;
201 }
202
203 /** Constructor for the filter.
204 */
205
206 mlt_filter filter_rescale_init( char *arg )
207 {
208         mlt_filter this = calloc( sizeof( struct mlt_filter_s ), 1 );
209         if ( mlt_filter_init( this, this ) == 0 )
210         {
211                 this->process = filter_process;
212                 if ( arg != NULL )
213                         mlt_properties_set( mlt_filter_properties( this ), "interpolation", arg );
214                 else
215                         mlt_properties_set( mlt_filter_properties( this ), "interpolation", "bilinear" );
216         }
217         return this;
218 }
219