+ Meta override for field order misreporting/errors in encoders
[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 #include <math.h>
29
30 /** Do it :-).
31 */
32
33 static int filter_get_image( mlt_frame this, uint8_t **image, mlt_image_format *format, int *width, int *height, int writable )
34 {
35 int error = 0;
36
37 // Get the properties from the frame
38 mlt_properties properties = MLT_FRAME_PROPERTIES( this );
39
40 // Pop the top of stack now
41 mlt_filter filter = mlt_frame_pop_service( this );
42
43 // Retrieve the aspect ratio
44 double aspect_ratio = mlt_deque_pop_back_double( MLT_FRAME_IMAGE_STACK( this ) );
45
46 // Assign requested width/height from our subordinate
47 int owidth = *width;
48 int oheight = *height;
49
50 // Check for the special case - no aspect ratio means no problem :-)
51 if ( aspect_ratio == 0.0 )
52 aspect_ratio = mlt_properties_get_double( properties, "consumer_aspect_ratio" );
53
54 // Reset the aspect ratio
55 mlt_properties_set_double( properties, "aspect_ratio", aspect_ratio );
56
57 // Hmmm...
58 char *rescale = mlt_properties_get( properties, "rescale.interp" );
59 if ( rescale != NULL && !strcmp( rescale, "none" ) )
60 return mlt_frame_get_image( this, image, format, width, height, writable );
61
62 if ( mlt_properties_get_int( properties, "distort" ) == 0 )
63 {
64 // Normalise the input and out display aspect
65 int normalised_width = mlt_properties_get_int( properties, "normalised_width" );
66 int normalised_height = mlt_properties_get_int( properties, "normalised_height" );
67 int real_width = mlt_properties_get_int( properties, "real_width" );
68 int real_height = mlt_properties_get_int( properties, "real_height" );
69 if ( real_width == 0 )
70 real_width = mlt_properties_get_int( properties, "width" );
71 if ( real_height == 0 )
72 real_height = mlt_properties_get_int( properties, "height" );
73 double input_ar = aspect_ratio * real_width / real_height;
74 double output_ar = mlt_properties_get_double( properties, "consumer_aspect_ratio" ) * owidth / oheight;
75
76 //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 );
77
78 // Optimised for the input_ar > output_ar case (e.g. widescreen on standard)
79 int scaled_width = rint( 0.5 + ( input_ar * normalised_width ) / output_ar );
80 int scaled_height = normalised_height;
81
82 // Now ensure that our images fit in the output frame
83 if ( scaled_width > normalised_width )
84 {
85 scaled_width = normalised_width;
86 scaled_height = rint( 0.5 + ( output_ar * normalised_height ) / input_ar );
87 }
88
89 // Now calculate the actual image size that we want
90 owidth = rint( 0.5 + scaled_width * owidth / normalised_width );
91 oheight = rint( 0.5 + scaled_height * oheight / normalised_height );
92
93 // Tell frame we have conformed the aspect to the consumer
94 mlt_frame_set_aspect_ratio( this, mlt_properties_get_double( properties, "consumer_aspect_ratio" ) );
95 }
96
97 mlt_properties_set_int( properties, "distort", 0 );
98
99 // Now pass on the calculations down the line
100 mlt_properties_set_int( properties, "resize_width", *width );
101 mlt_properties_set_int( properties, "resize_height", *height );
102
103 // Now get the image
104 error = mlt_frame_get_image( this, image, format, &owidth, &oheight, writable );
105
106 // We only know how to process yuv422 at the moment
107 if ( error == 0 && *format == mlt_image_yuv422 && *image != NULL )
108 {
109 // Get the requested scale operation
110 char *op = mlt_properties_get( MLT_FILTER_PROPERTIES( filter ), "scale" );
111
112 // Correct field order if needed
113 if ( mlt_properties_get_int( properties, "top_field_first" ) == 1 || mlt_properties_get_int( properties, "meta.top_field_first" ) == 1 )
114 {
115 // Get the input image, width and height
116 int size;
117 uint8_t *image = mlt_properties_get_data( properties, "image", &size );
118
119 // Keep the original image around to be destroyed on frame close
120 mlt_properties_rename( properties, "image", "original_image" );
121
122 // Duplicate the last line in the field to avoid artifact
123 memcpy( image + oheight * owidth * 2, image + oheight * owidth * 2 - owidth * 4, owidth * 2 );
124
125 // Offset the image pointer by one line
126 image += owidth * 2;
127 size -= owidth * 2;
128
129 // Set the new image pointer with no destructor
130 mlt_properties_set_data( properties, "image", image, size, NULL, NULL );
131
132 // Set the normalised field order
133 mlt_properties_set_int( properties, "top_field_first", 0 );
134 }
135
136 if ( !strcmp( op, "affine" ) )
137 {
138 *image = mlt_frame_rescale_yuv422( this, *width, *height );
139 }
140 else if ( strcmp( op, "none" ) != 0 )
141 {
142 *image = mlt_frame_resize_yuv422( this, *width, *height );
143 }
144 else
145 {
146 *width = owidth;
147 *height = oheight;
148 }
149 }
150
151 return error;
152 }
153
154 /** Filter processing.
155 */
156
157 static mlt_frame filter_process( mlt_filter this, mlt_frame frame )
158 {
159 // Store the aspect ratio reported by the source
160 mlt_deque_push_back_double( MLT_FRAME_IMAGE_STACK( frame ), mlt_frame_get_aspect_ratio( frame ) );
161
162 // Push this on to the service stack
163 mlt_frame_push_service( frame, this );
164
165 // Push the get_image method on to the stack
166 mlt_frame_push_get_image( frame, filter_get_image );
167
168 return frame;
169 }
170
171 /** Constructor for the filter.
172 */
173
174 mlt_filter filter_resize_init( char *arg )
175 {
176 mlt_filter this = calloc( sizeof( struct mlt_filter_s ), 1 );
177 if ( mlt_filter_init( this, this ) == 0 )
178 {
179 this->process = filter_process;
180 mlt_properties_set( MLT_FILTER_PROPERTIES( this ), "scale", arg == NULL ? "off" : arg );
181 }
182 return this;
183 }