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