2 * /brief Crop Detection filter
4 * /author Zachary Drew, Copyright 2005
6 * inspired by mplayer's cropdetect filter
8 * Note: The goemetry generated is zero-indexed and is inclusive of the end values
11 * -filter crop_detect debug=1 // Visualize crop
12 * -filter crop_detect frequency=25 // Detect the crop once a second
13 * -filter crop_detect frequency=0 // Never detect unless the producer changes
14 * -filter crop_detect thresh=100 // Changes the threshold (default = 25)
16 * This program is free software; you can redistribute it and/or modify
17 * it under the terms of the GNU General Public License as published by
18 * the Free Software Foundation; either version 2 of the License, or
19 * (at your option) any later version.
21 * This program is distributed in the hope that it will be useful,
22 * but WITHOUT ANY WARRANTY; without even the implied warranty of
23 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
24 * GNU General Public License for more details.
26 * You should have received a copy of the GNU General Public License
27 * along with this program; if not, write to the Free Software Foundation,
28 * Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
32 #define DEFAULT_THRESH 20
34 #include <framework/mlt.h>
40 #include "arrow_code.h"
42 #define ABS(a) ((a) >= 0 ? (a) : (-(a)))
44 // Image stack(able) method
45 static int filter_get_image( mlt_frame this, uint8_t **image, mlt_image_format *format, int *width, int *height, int writable )
48 // Get the filter object and properties
49 mlt_filter filter = mlt_frame_pop_service( this );
50 mlt_properties properties = MLT_FILTER_PROPERTIES( filter );
53 int error = mlt_frame_get_image( this, image, format, width, height, 1 );
56 mlt_properties_debug( MLT_FRAME_PROPERTIES(this), "error after mlt_frame_get_image()", stderr );
60 // Parameter that describes how often to check for the crop
61 int frequency = mlt_properties_get_int( properties, "frequency");
63 // Producers may start with blank footage, by default we will skip, oh, 5 frames unless overridden
64 int skip = mlt_properties_get_int( properties, "skip");
67 mlt_geometry_item bounds = mlt_properties_get_data( properties, "bounds", NULL );
69 // Initialize if needed
70 if( bounds == NULL ) {
71 bounds = calloc( 1, sizeof( struct mlt_geometry_item_s ) );
74 mlt_properties_set_data( properties, "bounds", bounds, sizeof( struct mlt_geometry_item_s ), free, NULL );
77 // For periodic detection (with offset of 'skip')
78 if( frequency == 0 || (int)(mlt_frame_get_position(this)+skip) % frequency != 0)
81 mlt_properties_set_data( MLT_FRAME_PROPERTIES(this), "bounds", bounds, sizeof( struct mlt_geometry_item_s ), NULL, NULL );
87 // There is no way to detect a crop for sure, so make up an arbitrary one
88 int thresh = mlt_properties_get_int( properties, "thresh" );
90 *format = mlt_image_yuv422;
92 int ystride = 2 * *width;
94 int x, y, average_brightness, deviation; // Scratch variables
98 for( y = 0; y < *height/2; y++ ) {
100 average_brightness = 0;
102 q = *image + y*ystride;
103 for( x = 0; x < *width; x++ )
104 average_brightness += q[x*xstride];
106 average_brightness /= *width;
108 for( x = 0; x < *width; x++ )
109 deviation += abs(average_brightness - q[x*xstride]);
111 if( deviation*10 >= thresh * *width )
116 for( y = *height - 1; y >= *height/2; y-- ) {
118 average_brightness = 0;
120 q = *image + y*ystride;
121 for( x = 0; x < *width; x++ )
122 average_brightness += q[x*xstride];
124 average_brightness /= *width;
126 for( x = 0; x < *width; x++ )
127 deviation += abs(average_brightness - q[x*xstride]);
129 if( deviation*10 >= thresh * *width)
134 for( x = 0; x < *width/2; x++ ) {
136 average_brightness = 0;
138 q = *image + x*xstride;
139 for( y = 0; y < *height; y++ )
140 average_brightness += q[y*ystride];
142 average_brightness /= *height;
144 for( y = 0; y < *height; y++ )
145 deviation += abs(average_brightness - q[y*ystride]);
147 if( deviation*10 >= thresh * *width )
152 for( x = *width - 1; x >= *width/2; x-- ) {
154 average_brightness = 0;
156 q = *image + x*xstride;
157 for( y = 0; y < *height; y++ )
158 average_brightness += q[y*ystride];
160 average_brightness /= *height;
162 for( y = 0; y < *height; y++ )
163 deviation += abs(average_brightness - q[y*ystride]);
165 if( deviation*10 >= thresh * *width )
169 /* Debug: Draw arrows to show crop */
170 if( mlt_properties_get_int( properties, "debug") == 1 )
172 init_arrows( format, *width, *height );
174 draw_arrow(*image, bounds->x, *height/2, bounds->x+50, *height/2, 100);
175 draw_arrow(*image, *width/2, bounds->y, *width/2, bounds->y+50, 100);
176 draw_arrow(*image, bounds->w, *height/2, bounds->w-50, *height/2, 100);
177 draw_arrow(*image, *width/2, bounds->h, *width/2, bounds->h-50, 100);
178 draw_arrow(*image, bounds->x, bounds->y, bounds->x+40, bounds->y+30, 100);
179 draw_arrow(*image, bounds->x, bounds->h, bounds->x+40, bounds->h-30, 100);
180 draw_arrow(*image, bounds->w, bounds->y, bounds->w-40, bounds->y+30, 100);
181 draw_arrow(*image, bounds->w, bounds->h, bounds->w-40, bounds->h-30, 100);
184 // Convert to width and correct indexing
185 bounds->w -= bounds->x - 1;
186 bounds->h -= bounds->y - 1;
188 if( mlt_properties_get_int( properties, "debug") == 1 )
189 fprintf(stderr, "Top:%f Left:%f Width:%f Height:%f\n", bounds->y, bounds->x, bounds->w, bounds->h);
191 /* inject into frame */
192 mlt_properties_set_data( MLT_FRAME_PROPERTIES(this), "bounds", bounds, sizeof( struct mlt_geometry_item_s ), NULL, NULL );
199 /** Filter processing.
202 static mlt_frame filter_process( mlt_filter this, mlt_frame frame )
205 // Put the filter object somewhere we can find it
206 mlt_frame_push_service( frame, this);
208 // Push the frame filter
209 mlt_frame_push_get_image( frame, filter_get_image );
214 /** Constructor for the filter.
216 mlt_filter filter_crop_detect_init( mlt_profile profile, mlt_service_type type, const char *id, char *arg )
218 mlt_filter this = mlt_filter_new( );
221 this->process = filter_process;
224 mlt_properties_set_int( MLT_FILTER_PROPERTIES(this), "frequency", 1);
225 mlt_properties_set_int( MLT_FILTER_PROPERTIES(this), "thresh", 5);
226 mlt_properties_set_int( MLT_FILTER_PROPERTIES(this), "clip", 5);
227 mlt_properties_set_int( MLT_FILTER_PROPERTIES(this), "former_producer_id", -1);
234 /** This source code will self destruct in 5...4...3...