e1ebb7a41b8006b576bf7e6a2af3f41fda93160e
[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 #include <framework/mlt_factory.h>
26
27 #include <stdio.h>
28 #include <string.h>
29 #include <stdlib.h>
30 #include <gdk-pixbuf/gdk-pixbuf.h>
31
32 static int filter_scale( mlt_frame this, uint8_t **image, mlt_image_format iformat, mlt_image_format oformat, int iwidth, int iheight, int owidth, int oheight )
33 {
34 // Get the properties
35 mlt_properties properties = mlt_frame_properties( this );
36
37 // Get the requested interpolation method
38 char *interps = mlt_properties_get( properties, "rescale.interp" );
39
40 // Convert to the GTK flag
41 int interp = PIXOPS_INTERP_BILINEAR;
42
43 if ( strcmp( interps, "nearest" ) == 0 )
44 interp = PIXOPS_INTERP_NEAREST;
45 else if ( strcmp( interps, "tiles" ) == 0 )
46 interp = PIXOPS_INTERP_TILES;
47 else if ( strcmp( interps, "hyper" ) == 0 )
48 interp = PIXOPS_INTERP_HYPER;
49
50 // Carry out the rescaling
51 if ( iformat == mlt_image_yuv422 && oformat == mlt_image_yuv422 )
52 {
53 // Create the output image
54 uint8_t *output = mlt_pool_alloc( owidth * ( oheight + 1 ) * 2 );
55
56 // Calculate strides
57 int istride = iwidth * 2;
58 int ostride = owidth * 2;
59
60 yuv422_scale_simple( output, owidth, oheight, ostride, *image, iwidth, iheight, istride, interp );
61
62 // Now update the frame
63 mlt_properties_set_data( properties, "image", output, owidth * ( oheight + 1 ) * 2, ( mlt_destructor )mlt_pool_release, NULL );
64 mlt_properties_set_int( properties, "width", owidth );
65 mlt_properties_set_int( properties, "height", oheight );
66
67 // Return the output
68 *image = output;
69 }
70 else if ( iformat == mlt_image_rgb24 || iformat == mlt_image_rgb24a )
71 {
72 int bpp = (iformat == mlt_image_rgb24a ? 4 : 3 );
73
74 // Create the yuv image
75 uint8_t *output = mlt_pool_alloc( owidth * ( oheight + 1 ) * 2 );
76
77 if ( strcmp( interps, "none" ) && ( iwidth != owidth || iheight != oheight ) )
78 {
79 GdkPixbuf *pixbuf = gdk_pixbuf_new_from_data( *image, GDK_COLORSPACE_RGB,
80 ( iformat == mlt_image_rgb24a ), 8, iwidth, iheight,
81 iwidth * bpp, NULL, NULL );
82
83 GdkPixbuf *scaled = gdk_pixbuf_scale_simple( pixbuf, owidth, oheight, interp );
84 g_object_unref( pixbuf );
85
86 // Extract YUV422 and alpha
87 if ( bpp == 4 )
88 {
89 // Allocate the alpha mask
90 uint8_t *alpha = mlt_pool_alloc( owidth * ( oheight + 1 ) );
91
92 // Convert the image and extract alpha
93 mlt_convert_rgb24a_to_yuv422( gdk_pixbuf_get_pixels( scaled ), owidth, oheight, gdk_pixbuf_get_rowstride( scaled ), output, alpha );
94
95 mlt_properties_set_data( properties, "alpha", alpha, owidth * ( oheight + 1 ), ( mlt_destructor )mlt_pool_release, NULL );
96 }
97 else
98 {
99 // No alpha to extract
100 mlt_convert_rgb24_to_yuv422( gdk_pixbuf_get_pixels( scaled ), owidth, oheight, gdk_pixbuf_get_rowstride( scaled ), output );
101 }
102 g_object_unref( scaled );
103 }
104 else
105 {
106 // Extract YUV422 and alpha
107 if ( bpp == 4 )
108 {
109 // Allocate the alpha mask
110 uint8_t *alpha = mlt_pool_alloc( owidth * ( oheight + 1 ) );
111
112 // Convert the image and extract alpha
113 mlt_convert_rgb24a_to_yuv422( *image, owidth, oheight, owidth * 4, output, alpha );
114
115 mlt_properties_set_data( properties, "alpha", alpha, owidth * ( oheight + 1 ), ( mlt_destructor )mlt_pool_release, NULL );
116 }
117 else
118 {
119 // No alpha to extract
120 mlt_convert_rgb24_to_yuv422( *image, owidth, oheight, owidth * 3, output );
121 }
122 }
123
124 // Now update the frame
125 mlt_properties_set_data( properties, "image", output, owidth * ( oheight + 1 ) * 2, ( mlt_destructor )mlt_pool_release, NULL );
126 mlt_properties_set_int( properties, "width", owidth );
127 mlt_properties_set_int( properties, "height", oheight );
128
129 *image = output;
130 }
131
132 return 0;
133 }
134
135 /** Constructor for the filter.
136 */
137
138 mlt_filter filter_rescale_init( char *arg )
139 {
140 // Create a new scaler
141 mlt_filter this = mlt_factory_filter( "rescale", arg );
142
143 // If successful, then initialise it
144 if ( this != NULL )
145 {
146 // Get the properties
147 mlt_properties properties = mlt_filter_properties( this );
148
149 // Set the inerpolation
150 mlt_properties_set( properties, "interpolation", arg == NULL ? "bilinear" : arg );
151
152 // Set the method
153 mlt_properties_set_data( properties, "method", filter_scale, 0, NULL, NULL );
154 }
155
156 return this;
157 }
158