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