]> git.sesse.net Git - mlt/blob - src/modules/core/filter_resize.c
627d9e0b933c1ee943f0d7dbb2c1d577bf36552c
[mlt] / src / modules / core / filter_resize.c
1 /*
2  * filter_resize.c -- resizing filter
3  * Copyright (C) 2003-2004 Ushodaya Enterprises Limited
4  * Author: Charles Yates <charles.yates@pandora.be>
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_resize.h"
22
23 #include <framework/mlt_frame.h>
24
25 #include <stdio.h>
26 #include <string.h>
27 #include <stdlib.h>
28
29 /** Do it :-).
30 */
31
32 static int filter_get_image( mlt_frame this, uint8_t **image, mlt_image_format *format, int *width, int *height, int writable )
33 {
34         int error = 0;
35
36         // Get the properties from the frame
37         mlt_properties properties = mlt_frame_properties( this );
38
39         // Pop the top of stack now
40         mlt_filter filter = mlt_frame_pop_service( this );
41
42         // Assign requested width/height from our subordinate
43         int owidth = *width;
44         int oheight = *height;
45
46         // Hmmm...
47         char *rescale = mlt_properties_get( properties, "rescale.interp" );
48         if ( rescale != NULL && !strcmp( rescale, "none" ) )
49                 return mlt_frame_get_image( this, image, format, width, height, writable );
50
51         if ( mlt_properties_get( properties, "distort" ) == NULL )
52         {
53                 // Normalise the input and out display aspect
54                 int normalised_width = mlt_properties_get_int( properties, "normalised_width" );
55                 int normalised_height = mlt_properties_get_int( properties, "normalised_height" );
56                 int real_width = mlt_properties_get_int( properties, "real_width" );
57                 int real_height = mlt_properties_get_int( properties, "real_height" );
58                 if ( real_width == 0 )
59                         real_width = mlt_properties_get_int( properties, "width" );
60                 if ( real_height == 0 )
61                         real_height = mlt_properties_get_int( properties, "height" );
62                 double input_ar = mlt_frame_get_aspect_ratio( this ) * real_width / real_height;
63                 double output_ar = mlt_properties_get_double( properties, "consumer_aspect_ratio" ) * owidth / oheight;
64                 
65                 //fprintf( stderr, "normalised %dx%d output %dx%d %f %f\n", normalised_width, normalised_height, owidth, oheight, ( float )output_ar, ( float )mlt_properties_get_double( properties, "consumer_aspect_ratio" ) * owidth / oheight );
66
67                 // Optimised for the input_ar > output_ar case (e.g. widescreen on standard)
68                 int scaled_width = input_ar / output_ar * normalised_width + 0.5;
69                 int scaled_height = normalised_height;
70
71                 // Now ensure that our images fit in the output frame
72                 if ( scaled_width > normalised_width )
73                 {
74                         scaled_width = normalised_width;
75                         scaled_height = output_ar / input_ar * normalised_height + 0.5;
76                 }
77         
78                 // Now calculate the actual image size that we want
79                 owidth = scaled_width * owidth / normalised_width;
80                 oheight = scaled_height * oheight / normalised_height;
81
82                 // Tell frame we have conformed the aspect to the consumer
83                 mlt_frame_set_aspect_ratio( this, mlt_properties_get_double( properties, "consumer_aspect_ratio" ) );
84         }
85
86         // Now pass on the calculations down the line
87         mlt_properties_set_int( properties, "resize_width", *width );
88         mlt_properties_set_int( properties, "resize_height", *height );
89
90         // Now get the image
91         error = mlt_frame_get_image( this, image, format, &owidth, &oheight, writable );
92
93         // We only know how to process yuv422 at the moment
94         if ( error == 0 && *format == mlt_image_yuv422 )
95         {
96                 // Get the requested scale operation
97                 char *op = mlt_properties_get( mlt_filter_properties( filter ), "scale" );
98
99                 // Correct field order if needed
100                 if ( mlt_properties_get_int( properties, "top_field_first" ) == 1 )
101                 {
102                         // Get the input image, width and height
103                         int size;
104                         uint8_t *image = mlt_properties_get_data( properties, "image", &size );
105
106                         // Keep the original image around to be destroyed on frame close
107                         mlt_properties_rename( properties, "image", "original_image" );
108
109                         // Duplicate the last line in the field to avoid artifact
110                         memcpy( image + oheight * owidth * 2, image + oheight * owidth * 2 - owidth * 4, owidth * 2 );
111
112                         // Offset the image pointer by one line
113                         image += owidth * 2;
114                         size -= owidth * 2;
115                         
116                         // Set the new image pointer with no destructor
117                         mlt_properties_set_data( properties, "image", image, size, NULL, NULL );
118
119                         // Set the normalised field order
120                         mlt_properties_set_int( properties, "top_field_first", 0 );
121                 }
122
123                 if ( !strcmp( op, "affine" ) )
124                 {
125                         *image = mlt_frame_rescale_yuv422( this, *width, *height );
126                 }
127                 else if ( strcmp( op, "none" ) != 0 )
128                 {
129                         *image = mlt_frame_resize_yuv422( this, *width, *height );
130                 }
131                 else
132                 {
133                         *width = owidth;
134                         *height = oheight;
135                 }
136         }
137
138         return error;
139 }
140
141 /** Filter processing.
142 */
143
144 static mlt_frame filter_process( mlt_filter this, mlt_frame frame )
145 {
146         // Push this on to the service stack
147         mlt_frame_push_service( frame, this );
148
149         // Push the get_image method on to the stack
150         mlt_frame_push_get_image( frame, filter_get_image );
151
152         return frame;
153 }
154
155 /** Constructor for the filter.
156 */
157
158 mlt_filter filter_resize_init( char *arg )
159 {
160         mlt_filter this = calloc( sizeof( struct mlt_filter_s ), 1 );
161         if ( mlt_filter_init( this, this ) == 0 )
162         {
163                 this->process = filter_process;
164                 mlt_properties_set( mlt_filter_properties( this ), "scale", arg == NULL ? "off" : arg );
165         }
166         return this;
167 }