]> git.sesse.net Git - mlt/blob - src/modules/core/filter_rescale.c
framework: remove global profile, rather share one mlt_profile across a service netwo...
[mlt] / src / modules / core / 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 library is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Lesser General Public
8  * License as published by the Free Software Foundation; either
9  * version 2.1 of the License, or (at your option) any later version.
10  *
11  * This library 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 GNU
14  * Lesser General Public License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public
17  * License along with this library; if not, write to the Free Software
18  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
19  */
20
21 #include <framework/mlt_filter.h>
22 #include <framework/mlt_frame.h>
23
24 #include <stdio.h>
25 #include <string.h>
26 #include <stdlib.h>
27 #include <math.h>
28
29 typedef int ( *image_scaler )( mlt_frame this, uint8_t **image, mlt_image_format iformat, mlt_image_format oformat, int iwidth, int iheight, int owidth, int oheight );
30
31 static void scale_alpha( mlt_frame this, int iwidth, int iheight, int owidth, int oheight );
32
33 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 )
34 {
35         // Get the properties
36         mlt_properties properties = MLT_FRAME_PROPERTIES( this );
37
38         // Get the rescaling interpolsation
39         char *interps = mlt_properties_get( properties, "rescale.interp" );
40
41         // Carry out the rescaling
42         if ( iformat == mlt_image_yuv422 && oformat == mlt_image_yuv422 )
43         {
44                 // Scale the frame
45                 mlt_frame_rescale_yuv422( this, owidth, oheight );
46                 
47                 // Return the output
48                 *image = mlt_properties_get_data( properties, "image", NULL );
49
50                 // Scale the alpha channel only if exists and not correct size
51                 int alpha_size = 0;
52                 mlt_properties_get_data( properties, "alpha", &alpha_size );
53                 if ( alpha_size > 0 && alpha_size != ( owidth * oheight ) )
54                         scale_alpha( this, iwidth, iheight, owidth, oheight );
55         }
56         else if ( iformat == mlt_image_rgb24 || iformat == mlt_image_rgb24a )
57         {
58                 int bpp = (iformat == mlt_image_rgb24a ? 4 : 3 );
59                         
60                 // Create the yuv image
61                 uint8_t *output = mlt_pool_alloc( iwidth * ( iheight + 1 ) * 2 );
62
63                 if ( strcmp( interps, "none" ) && ( iwidth != owidth || iheight != oheight ) )
64                 {
65                         // Extract YUV422 and alpha
66                         if ( bpp == 4 )
67                         {
68                                 // Allocate the alpha mask
69                                 uint8_t *alpha = mlt_pool_alloc( iwidth * ( iheight + 1 ) );
70
71                                 // Convert the image and extract alpha
72                                 mlt_convert_rgb24a_to_yuv422( *image, iwidth, iheight, iwidth * 4, output, alpha );
73
74                                 mlt_properties_set_data( properties, "alpha", alpha, iwidth * ( iheight + 1 ), ( mlt_destructor )mlt_pool_release, NULL );
75
76                                 scale_alpha( this, iwidth, iheight, owidth, oheight );
77                         }
78                         else
79                         {
80                                 // No alpha to extract
81                                 mlt_convert_rgb24_to_yuv422( *image, iwidth, iheight, iwidth * 3, output );
82                         }
83
84                         mlt_properties_set_data( properties, "image", output, iwidth * ( iheight + 1 ) * 2, ( mlt_destructor )mlt_pool_release, NULL );
85
86                         // Scale the frame
87                         output = mlt_frame_rescale_yuv422( this, owidth, oheight );
88
89                 }
90                 else
91                 {
92                         // Extract YUV422 and alpha
93                         if ( bpp == 4 )
94                         {
95                                 // Allocate the alpha mask
96                                 uint8_t *alpha = mlt_pool_alloc( owidth * ( oheight + 1 ) );
97
98                                 // Convert the image and extract alpha
99                                 mlt_convert_rgb24a_to_yuv422( *image, owidth, oheight, owidth * 4, output, alpha );
100
101                                 mlt_properties_set_data( properties, "alpha", alpha, owidth * ( oheight + 1 ), ( mlt_destructor )mlt_pool_release, NULL );
102
103                                 scale_alpha( this, iwidth, iheight, owidth, oheight );
104                         }
105                         else
106                         {
107                                 // No alpha to extract
108                                 mlt_convert_rgb24_to_yuv422( *image, owidth, oheight, owidth * 3, output );
109                         }
110                 }
111
112                 // Now update the frame
113                 mlt_properties_set_data( properties, "image", output, owidth * ( oheight + 1 ) * 2, ( mlt_destructor )mlt_pool_release, NULL );
114                 mlt_properties_set_int( properties, "width", owidth );
115                 mlt_properties_set_int( properties, "height", oheight );
116
117                 *image = output;
118         }
119
120         return 0;
121 }
122
123 static void scale_alpha( mlt_frame this, int iwidth, int iheight, int owidth, int oheight )
124 {
125         // Scale the alpha
126         uint8_t *output = NULL;
127         uint8_t *input = mlt_frame_get_alpha_mask( this );
128
129         if ( input != NULL )
130         {
131                 uint8_t *out_line;
132                 int x, y;
133                 int ox = ( iwidth << 10 ) / owidth;
134                 int oy = ( iheight << 10 ) / oheight;
135
136                 output = mlt_pool_alloc( owidth * oheight );
137                 out_line = output;
138
139                 // Loop for the entirety of our output height.
140                 for ( y = 0; y < oheight; y ++ )
141                         for ( x = 0; x < owidth; x ++ )
142                                 *out_line ++ = *( input + ( ( 512 + ( y * oy * iwidth ) + x * ox ) >> 10 ) );
143
144                 // Set it back on the frame
145                 mlt_properties_set_data( MLT_FRAME_PROPERTIES( this ), "alpha", output, owidth * oheight, mlt_pool_release, NULL );
146         }
147 }
148
149 /** Do it :-).
150 */
151
152 static int filter_get_image( mlt_frame this, uint8_t **image, mlt_image_format *format, int *width, int *height, int writable )
153 {
154         // Get the frame properties
155         mlt_properties properties = MLT_FRAME_PROPERTIES( this );
156
157         // Get the filter from the stack
158         mlt_filter filter = mlt_frame_pop_service( this );
159
160         // Get the filter properties
161         mlt_properties filter_properties = MLT_FILTER_PROPERTIES( filter );
162
163         // Get the image scaler method
164         image_scaler scaler_method = mlt_properties_get_data( filter_properties, "method", NULL );
165
166         // Correct Width/height if necessary
167         if ( *width == 0 || *height == 0 )
168         {
169                 *width = mlt_properties_get_int( properties, "normalised_width" );
170                 *height = mlt_properties_get_int( properties, "normalised_height" );
171         }
172
173         // There can be problems with small images - avoid them (by hacking - gah)
174         if ( *width >= 6 && *height >= 6 )
175         {
176                 int iwidth = *width;
177                 int iheight = *height;
178                 int owidth = *width;
179                 int oheight = *height;
180                 char *interps = mlt_properties_get( properties, "rescale.interp" );
181                 int wanted_format = *format;
182
183                 // Default from the scaler if not specifed on the frame
184                 if ( interps == NULL )
185                 {
186                         interps = mlt_properties_get( MLT_FILTER_PROPERTIES( filter ), "interpolation" );
187                         mlt_properties_set( properties, "rescale.interp", interps );
188                 }
189         
190                 // If real_width/height exist, we want that as minimum information
191                 if ( mlt_properties_get_int( properties, "real_width" ) )
192                 {
193                         iwidth = mlt_properties_get_int( properties, "real_width" );
194                         iheight = mlt_properties_get_int( properties, "real_height" );
195                 }
196         
197                 // Let the producer know what we are actually requested to obtain
198                 if ( *format == mlt_image_yuv422 && strcmp( interps, "none" ) )
199                 {
200                         mlt_properties_set_int( properties, "rescale_width", *width );
201                         mlt_properties_set_int( properties, "rescale_height", *height );
202                 }
203                 else
204                 {
205                         // When no scaling is requested, revert the requested dimensions if possible
206                         mlt_properties_set_int( properties, "rescale_width", iwidth );
207                         mlt_properties_set_int( properties, "rescale_height", iheight );
208                 }
209         
210                 // Get the image as requested
211                 mlt_frame_get_image( this, image, format, &iwidth, &iheight, writable );
212
213                 // Get rescale interpretation again, in case the producer wishes to override scaling
214                 interps = mlt_properties_get( properties, "rescale.interp" );
215         
216                 if ( *image != NULL && ( *format != mlt_image_yuv422 || ( iwidth != owidth || iheight != oheight ) ) )
217                 {
218                         // If the colour space is correct and scaling is off, do nothing
219                         if ( *format == mlt_image_yuv422 && !strcmp( interps, "none" ) )
220                         {
221                                 *width = iwidth;
222                                 *height = iheight;
223                         }
224                         else if ( *format == mlt_image_yuv422 )
225                         {
226                                 // Call the local scaler
227                                 scaler_method( this, image, *format, mlt_image_yuv422, iwidth, iheight, owidth, oheight );
228                                 *width = owidth;
229                                 *height = oheight;
230                         }
231                         else if ( *format == mlt_image_rgb24 && wanted_format == mlt_image_rgb24 )
232                         {
233                                 // Call the local scaler
234                                 scaler_method( this, image, *format, mlt_image_rgb24, iwidth, iheight, owidth, oheight );
235
236                                 // Return the output
237                                 *width = owidth;
238                                 *height = oheight;
239                         }
240                         else if ( *format == mlt_image_rgb24 || *format == mlt_image_rgb24a )
241                         {
242                                 // Call the local scaler
243                                 scaler_method( this, image, *format, mlt_image_yuv422, iwidth, iheight, owidth, oheight );
244
245                                 // Return the output
246                                 *format = mlt_image_yuv422;
247                                 *width = owidth;
248                                 *height = oheight;
249                         }
250                         else
251                         {
252                                 *width = iwidth;
253                                 *height = iheight;
254                         }
255                 }
256                 else
257                 {
258                         *width = iwidth;
259                         *height = iheight;
260                 }
261         }
262         else
263         {
264                 // Store the requested width/height
265                 int iwidth = *width;
266                 int iheight = *height;
267
268                 // Get the image as requested
269                 mlt_frame_get_image( this, image, format, &iwidth, &iheight, writable );
270
271                 // Too small - for now just assign as though we got what we wanted
272                 *width = iwidth;
273                 *height = iheight;
274         }
275
276
277         return 0;
278 }
279
280 /** Filter processing.
281 */
282
283 static mlt_frame filter_process( mlt_filter this, mlt_frame frame )
284 {
285         // Push the filter
286         mlt_frame_push_service( frame, this );
287
288         // Push the get image method
289         mlt_frame_push_service( frame, filter_get_image );
290
291         return frame;
292 }
293
294 /** Constructor for the filter.
295 */
296
297 mlt_filter filter_rescale_init( mlt_profile profile, mlt_service_type type, const char *id, char *arg )
298 {
299         // Create a new scaler
300         mlt_filter this = mlt_filter_new( );
301
302         // If successful, then initialise it
303         if ( this != NULL )
304         {
305                 // Get the properties
306                 mlt_properties properties = MLT_FILTER_PROPERTIES( this );
307
308                 // Set the process method
309                 this->process = filter_process;
310
311                 // Set the inerpolation
312                 mlt_properties_set( properties, "interpolation", arg == NULL ? "bilinear" : arg );
313
314                 // Set the method
315                 mlt_properties_set_data( properties, "method", filter_scale, 0, NULL, NULL );
316         }
317
318         return this;
319 }
320