Optimisations (part 0), pixel v percentage, reworked aspect ratio calcs, ante/post...
[melted] / 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 static int get_value( mlt_properties properties, char *preferred, char *fallback )
30 {
31 int value = mlt_properties_get_int( properties, preferred );
32 if ( value == 0 )
33 value = mlt_properties_get_int( properties, fallback );
34 return value;
35 }
36
37 /** Do it :-).
38 */
39
40 static int filter_get_image( mlt_frame this, uint8_t **image, mlt_image_format *format, int *width, int *height, int writable )
41 {
42 // Get the properties from the frame
43 mlt_properties properties = mlt_frame_properties( this );
44
45 // Assign requested width/height from our subordinate
46 int owidth = *width;
47 int oheight = *height;
48
49 if ( mlt_properties_get( properties, "distort" ) == NULL )
50 {
51 // Now do additional calcs based on real_width/height etc
52 int normalised_width = mlt_properties_get_int( properties, "normalised_width" );
53 int normalised_height = mlt_properties_get_int( properties, "normalised_height" );
54 int real_width = get_value( properties, "real_width", "width" );
55 int real_height = get_value( properties, "real_height", "height" );
56 double input_ar = mlt_frame_get_aspect_ratio( this );
57 double output_ar = mlt_properties_get_double( properties, "consumer_aspect_ratio" );
58 int scaled_width = ( input_ar > output_ar ? input_ar / output_ar : output_ar / input_ar ) * real_width;
59 int scaled_height = ( input_ar > output_ar ? input_ar / output_ar : output_ar / input_ar ) * real_height;
60
61 // Now ensure that our images fit in the normalised frame
62 if ( scaled_width > normalised_width )
63 {
64 scaled_height = scaled_height * normalised_width / scaled_width;
65 scaled_width = normalised_width;
66 }
67 if ( scaled_height > normalised_height )
68 {
69 scaled_width = scaled_width * normalised_height / scaled_height;
70 scaled_height = normalised_height;
71 }
72
73 if ( input_ar == output_ar && scaled_height == normalised_height )
74 scaled_width = normalised_width;
75
76 // Now calculate the actual image size that we want
77 owidth = scaled_width * owidth / normalised_width;
78 oheight = scaled_height * oheight / normalised_height;
79 }
80
81 // Now pass on the calculations down the line
82 mlt_properties_set_int( properties, "resize_width", *width );
83 mlt_properties_set_int( properties, "resize_height", *height );
84
85 // Now get the image
86 mlt_frame_get_image( this, image, format, &owidth, &oheight, writable );
87
88 // Correct field order if needed
89 if ( mlt_properties_get_int( properties, "top_field_first" ) == 1 )
90 {
91 // Get the input image, width and height
92 int size;
93 uint8_t *image = mlt_properties_get_data( properties, "image", &size );
94
95 // Keep the original image around to be destroyed on frame close
96 mlt_properties_rename( properties, "image", "original_image" );
97
98 // Offset the image pointer by one line
99 image += owidth * 2;
100 size -= owidth * 2;
101
102 // Set the new image pointer with no destructor
103 mlt_properties_set_data( properties, "image", image, size, NULL, NULL );
104
105 // Set the normalised field order
106 mlt_properties_set_int( properties, "top_field_first", 0 );
107 }
108
109 if ( !strcmp( mlt_properties_get( properties, "resize.scale" ), "affine" ) )
110 *image = mlt_frame_rescale_yuv422( this, *width, *height );
111 else if ( strcmp( mlt_properties_get( properties, "resize.scale" ), "none" ) != 0 )
112 *image = mlt_frame_resize_yuv422( this, *width, *height );
113 else
114 {
115 *width = owidth;
116 *height = oheight;
117 }
118 return 0;
119 }
120
121 /** Filter processing.
122 */
123
124 static mlt_frame filter_process( mlt_filter this, mlt_frame frame )
125 {
126 mlt_frame_push_get_image( frame, filter_get_image );
127 mlt_properties_set( mlt_frame_properties( frame ), "resize.scale", mlt_properties_get( mlt_filter_properties( this ), "scale" ) );
128 return frame;
129 }
130
131 /** Constructor for the filter.
132 */
133
134 mlt_filter filter_resize_init( char *arg )
135 {
136 mlt_filter this = calloc( sizeof( struct mlt_filter_s ), 1 );
137 if ( mlt_filter_init( this, this ) == 0 )
138 {
139 this->process = filter_process;
140 if ( arg != NULL )
141 mlt_properties_set( mlt_filter_properties( this ), "scale", arg );
142 else
143 mlt_properties_set( mlt_filter_properties( this ), "scale", "off" );
144 }
145 return this;
146 }
147