]> git.sesse.net Git - mlt/blob - src/modules/core/filter_crop.c
3c45e24d03e1df6575d8650294f2fb278c341b78
[mlt] / src / modules / core / filter_crop.c
1 /*
2  * filter_crop.c -- cropping filter
3  * Copyright (C) 2009 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 #include <framework/mlt_log.h>
24 #include <framework/mlt_profile.h>
25
26 #include <stdio.h>
27 #include <string.h>
28 #include <stdlib.h>
29 #include <math.h>
30
31 static void crop( uint8_t *src, uint8_t *dest, int bpp, int width, int height, int left, int right, int top, int bottom )
32 {
33         int src_stride = ( width  ) * bpp;
34         int dest_stride = ( width - left - right ) * bpp;
35         int y      = height - top - bottom + 1;
36         src += top * src_stride + left * bpp;
37
38         while ( --y )
39         {
40                 memcpy( dest, src, dest_stride );
41                 dest += dest_stride;
42                 src += src_stride;
43         }
44 }
45
46 /** Do it :-).
47 */
48
49 static int filter_get_image( mlt_frame this, uint8_t **image, mlt_image_format *format, int *width, int *height, int writable )
50 {
51         int error = 0;
52
53         // Get the properties from the frame
54         mlt_properties properties = MLT_FRAME_PROPERTIES( this );
55
56         // Correct Width/height if necessary
57         if ( *width == 0 || *height == 0 )
58         {
59                 *width  = mlt_properties_get_int( properties, "normalised_width" );
60                 *height = mlt_properties_get_int( properties, "normalised_height" );
61         }
62
63         int left    = mlt_properties_get_int( properties, "crop.left" );
64         int right   = mlt_properties_get_int( properties, "crop.right" );
65         int top     = mlt_properties_get_int( properties, "crop.top" );
66         int bottom  = mlt_properties_get_int( properties, "crop.bottom" );
67
68         // Request the image at its original resolution
69         if ( left || right || top || bottom )
70         {
71                 mlt_properties_set_int( properties, "rescale_width", mlt_properties_get_int( properties, "crop.original_width" ) );
72                 mlt_properties_set_int( properties, "rescale_height", mlt_properties_get_int( properties, "crop.original_height" ) );
73         }
74
75         // Now get the image
76         error = mlt_frame_get_image( this, image, format, width, height, writable );
77
78         int owidth  = *width - left - right;
79         int oheight = *height - top - bottom;
80         owidth = owidth < 0 ? 0 : owidth;
81         oheight = oheight < 0 ? 0 : oheight;
82
83         if ( ( owidth != *width || oheight != *height ) &&
84                 error == 0 && *image != NULL && owidth > 0 && oheight > 0 )
85         {
86                 int bpp;
87
88                 // Subsampled YUV is messy and less precise.
89                 if ( *format == mlt_image_yuv422 && this->convert_image )
90                 {
91                         mlt_image_format requested_format = mlt_image_rgb24;
92                         this->convert_image( this, image, format, requested_format );
93                 }
94         
95                 mlt_log_debug( NULL, "[filter crop] %s %dx%d -> %dx%d\n", mlt_image_format_name(*format),
96                                  *width, *height, owidth, oheight);
97
98                 // Provides a manual override for misreported field order
99                 if ( mlt_properties_get( properties, "meta.top_field_first" ) )
100                 {
101                         mlt_properties_set_int( properties, "top_field_first", mlt_properties_get_int( properties, "meta.top_field_first" ) );
102                         mlt_properties_set_int( properties, "meta.top_field_first", 0 );
103                 }
104
105                 if ( top % 2 )
106                         mlt_properties_set_int( properties, "top_field_first", !mlt_properties_get_int( properties, "top_field_first" ) );
107                 
108                 // Create the output image
109                 int size = mlt_image_format_size( *format, owidth, oheight, &bpp );
110                 uint8_t *output = mlt_pool_alloc( size );
111                 if ( output )
112                 {
113                         // Call the generic resize
114                         crop( *image, output, bpp, *width, *height, left, right, top, bottom );
115
116                         // Now update the frame
117                         mlt_frame_set_image( this, output, size, mlt_pool_release );
118                         *image = output;
119                 }
120
121                 // We should resize the alpha too
122                 uint8_t *alpha = mlt_frame_get_alpha_mask( this );
123                 int alpha_size = 0;
124                 mlt_properties_get_data( properties, "alpha", &alpha_size );
125                 if ( alpha && alpha_size >= ( *width * *height ) )
126                 {
127                         uint8_t *newalpha = mlt_pool_alloc( owidth * oheight );
128                         if ( newalpha )
129                         {
130                                 crop( alpha, newalpha, 1, *width, *height, left, right, top, bottom );
131                                 mlt_frame_set_alpha( this, newalpha, owidth * oheight, mlt_pool_release );
132                                 this->get_alpha_mask = NULL;
133                         }
134                 }
135                 *width = owidth;
136                 *height = oheight;
137         }
138
139         return error;
140 }
141
142 /** Filter processing.
143 */
144
145 static mlt_frame filter_process( mlt_filter this, mlt_frame frame )
146 {
147         if ( mlt_properties_get_int( MLT_FILTER_PROPERTIES( this ), "active" ) )
148         {
149                 // Push the get_image method on to the stack
150                 mlt_frame_push_get_image( frame, filter_get_image );
151         }
152         else
153         {
154                 mlt_properties filter_props = MLT_FILTER_PROPERTIES( this );
155                 mlt_properties frame_props = MLT_FRAME_PROPERTIES( frame );
156                 int left   = mlt_properties_get_int( filter_props, "left" );
157                 int right  = mlt_properties_get_int( filter_props, "right" );
158                 int top    = mlt_properties_get_int( filter_props, "top" );
159                 int bottom = mlt_properties_get_int( filter_props, "bottom" );
160                 int width  = mlt_properties_get_int( frame_props, "real_width" );
161                 int height = mlt_properties_get_int( frame_props, "real_height" );
162                 int use_profile = mlt_properties_get_int( filter_props, "use_profile" );
163                 mlt_profile profile = mlt_service_profile( MLT_FILTER_SERVICE( this ) );
164
165                 if ( use_profile )
166                 {
167                         top = top * height / profile->height;
168                         bottom = bottom * height / profile->height;
169                         left = left * width / profile->width;
170                         right = right * width / profile->width;
171                 }
172                 if ( mlt_properties_get_int( filter_props, "center" ) )
173                 {
174                         double aspect_ratio = mlt_frame_get_aspect_ratio( frame );
175                         if ( aspect_ratio == 0.0 )
176                                 aspect_ratio = mlt_properties_get_double( frame_props, "consumer_aspect_ratio" );
177                         double input_ar = aspect_ratio * width / height;
178                         double output_ar = mlt_profile_dar( mlt_service_profile( MLT_FILTER_SERVICE(this) ) );
179                         int bias = mlt_properties_get_int( filter_props, "center_bias" );
180                         
181                         if ( input_ar > output_ar )
182                         {
183                                 left = right = ( width - rint( output_ar * height / aspect_ratio ) ) / 2;
184                                 if ( abs(bias) > left )
185                                         bias = bias < 0 ? -left : left;
186                                 else if ( use_profile )
187                                         bias = bias * width / profile->width;
188                                 left -= bias;
189                                 right += bias;
190                         }
191                         else
192                         {
193                                 top = bottom = ( height - rint( aspect_ratio * width / output_ar ) ) / 2;
194                                 if ( abs(bias) > top )
195                                         bias = bias < 0 ? -top : top;
196                                 else if ( use_profile )
197                                         bias = bias * height / profile->height;
198                                 top -= bias;
199                                 bottom += bias;
200                         }
201                 }               
202
203                 // Coerce the output to an even width because subsampled YUV with odd widths is too
204                 // risky for downstream processing to handle correctly.
205                 left += ( width - left - right ) & 1;
206                 if ( width - left - right < 8 )
207                         left = right = 0;
208                 if ( height - top - bottom < 8 )
209                         top = bottom = 0;
210                 mlt_properties_set_int( frame_props, "crop.left", left );
211                 mlt_properties_set_int( frame_props, "crop.right", right );
212                 mlt_properties_set_int( frame_props, "crop.top", top );
213                 mlt_properties_set_int( frame_props, "crop.bottom", bottom );
214                 mlt_properties_set_int( frame_props, "crop.original_width", width );
215                 mlt_properties_set_int( frame_props, "crop.original_height", height );
216                 mlt_properties_set_int( frame_props, "real_width", width - left - right );
217                 mlt_properties_set_int( frame_props, "real_height", height - top - bottom );
218         }
219         return frame;
220 }
221
222 /** Constructor for the filter.
223 */
224
225 mlt_filter filter_crop_init( mlt_profile profile, mlt_service_type type, const char *id, char *arg )
226 {
227         mlt_filter this = calloc( sizeof( struct mlt_filter_s ), 1 );
228         if ( mlt_filter_init( this, this ) == 0 )
229         {
230                 this->process = filter_process;
231                 if ( arg )
232                         mlt_properties_set_int( MLT_FILTER_PROPERTIES( this ), "active", atoi( arg ) );
233         }
234         return this;
235 }