]> git.sesse.net Git - mlt/blob - src/modules/core/filter_resize.c
Filter optimisations and cleanup part 1
[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 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         int error = 0;
43
44         // Get the properties from the frame
45         mlt_properties properties = mlt_frame_properties( this );
46
47         // Assign requested width/height from our subordinate
48         int owidth = *width;
49         int oheight = *height;
50
51         // Hmmm...
52         char *rescale = mlt_properties_get( properties, "rescale.interp" );
53         if ( rescale != NULL && !strcmp( rescale, "none" ) )
54                 return mlt_frame_get_image( this, image, format, width, height, writable );
55
56         if ( mlt_properties_get( properties, "distort" ) == NULL )
57         {
58                 // Normalise the input and out display aspect
59                 int normalised_width = mlt_properties_get_int( properties, "normalised_width" );
60                 int normalised_height = mlt_properties_get_int( properties, "normalised_height" );
61                 double input_ar = mlt_frame_get_aspect_ratio( this );
62                 double output_ar = mlt_properties_get_double( properties, "consumer_aspect_ratio" );
63                 
64                 // Optimised for the input_ar > output_ar case (e.g. widescreen on standard)
65                 int scaled_width = normalised_width;
66                 int scaled_height = output_ar / input_ar * normalised_height;
67
68                 // Now ensure that our images fit in the normalised frame
69                 if ( scaled_height > normalised_height )
70                 {
71                         scaled_width = input_ar / output_ar * normalised_width;
72                         scaled_height = normalised_height;
73                 }
74         
75                 // Now calculate the actual image size that we want
76                 owidth = scaled_width * owidth / normalised_width;
77                 oheight = scaled_height * oheight / normalised_height;
78
79                 // Tell frame we have conformed the aspect to the consumer
80                 mlt_frame_set_aspect_ratio( this, output_ar );
81         }
82
83         // Now pass on the calculations down the line
84         mlt_properties_set_int( properties, "resize_width", *width );
85         mlt_properties_set_int( properties, "resize_height", *height );
86
87         // Now get the image
88         error = mlt_frame_get_image( this, image, format, &owidth, &oheight, writable );
89
90         // We only know how to process yuv422 at the moment
91         if ( error == 0 && *format == mlt_image_yuv422 )
92         {
93                 // Correct field order if needed
94                 if ( mlt_properties_get_int( properties, "top_field_first" ) == 1 )
95                 {
96                         // Get the input image, width and height
97                         int size;
98                         uint8_t *image = mlt_properties_get_data( properties, "image", &size );
99
100                         // Keep the original image around to be destroyed on frame close
101                         mlt_properties_rename( properties, "image", "original_image" );
102
103                         // Offset the image pointer by one line
104                         image += owidth * 2;
105                         size -= owidth * 2;
106
107                         // Set the new image pointer with no destructor
108                         mlt_properties_set_data( properties, "image", image, size, NULL, NULL );
109
110                         // Set the normalised field order
111                         mlt_properties_set_int( properties, "top_field_first", 0 );
112                 }
113
114                 if ( !strcmp( mlt_properties_get( properties, "resize.scale" ), "affine" ) )
115                 {
116                         *image = mlt_frame_rescale_yuv422( this, *width, *height );
117                 }
118                 else if ( strcmp( mlt_properties_get( properties, "resize.scale" ), "none" ) != 0 )
119                 {
120                         *image = mlt_frame_resize_yuv422( this, *width, *height );
121                 }
122                 else
123                 {
124                         *width = owidth;
125                         *height = oheight;
126                 }
127         }
128
129         return 0;
130 }
131
132 /** Filter processing.
133 */
134
135 static mlt_frame filter_process( mlt_filter this, mlt_frame frame )
136 {
137         mlt_frame_push_get_image( frame, filter_get_image );
138         mlt_properties_set( mlt_frame_properties( frame ), "resize.scale", mlt_properties_get( mlt_filter_properties( this ), "scale" ) );
139         return frame;
140 }
141
142 /** Constructor for the filter.
143 */
144
145 mlt_filter filter_resize_init( char *arg )
146 {
147         mlt_filter this = calloc( sizeof( struct mlt_filter_s ), 1 );
148         if ( mlt_filter_init( this, this ) == 0 )
149         {
150                 this->process = filter_process;
151                 if ( arg != NULL )
152                         mlt_properties_set( mlt_filter_properties( this ), "scale", arg );
153                 else
154                         mlt_properties_set( mlt_filter_properties( this ), "scale", "off" );
155         }
156         return this;
157 }
158