1 /**
2 * /brief Crop Detection filter
3 *
4 * /author Zachary Drew, Copyright 2005
5 *
6 * inspired by mplayer's cropdetect filter
7 *
8 * Note: The goemetry generated is zero-indexed and is inclusive of the end values
9 *
10 * Options:
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)
15 *
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.
20 *
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.
25 *
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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
29 */
30
31 #define DEBUG
32 #define DEFAULT_THRESH 20
33
34 #include <framework/mlt.h>
35
36 #include <stdio.h>
37 #include <stdlib.h>
38 #include <math.h>
39 #include <string.h>
40 #include "arrow_code.h"
41
42 #define ABS(a) ((a) >= 0 ? (a) : (-(a)))
43
44 // Image stack(able) method
filter_get_image(mlt_frame this,uint8_t ** image,mlt_image_format * format,int * width,int * height,int writable)45 static int filter_get_image( mlt_frame this, uint8_t **image, mlt_image_format *format, int *width, int *height, int writable )
46 {
47
48 // Get the filter object and properties
49 mlt_filter filter = mlt_frame_pop_service( this );
50 mlt_properties properties = MLT_FILTER_PROPERTIES( filter );
51 mlt_profile profile = mlt_service_profile(MLT_FILTER_SERVICE(filter));
52
53 // Disable consumer scaling
54 if (profile && profile->width && profile->height) {
55 *width = profile->width;
56 *height = profile->height;
57 }
58
59 // Get the new image
60 int error = mlt_frame_get_image( this, image, format, width, height, 1 );
61
62 if( error != 0 ) {
63 mlt_properties_debug( MLT_FRAME_PROPERTIES(this), "error after mlt_frame_get_image()", stderr );
64 return error;
65 }
66
67 // Parameter that describes how often to check for the crop
68 int frequency = mlt_properties_get_int( properties, "frequency");
69
70 // Producers may start with blank footage, by default we will skip, oh, 5 frames unless overridden
71 int skip = mlt_properties_get_int( properties, "skip");
72
73 mlt_service_lock( MLT_FILTER_SERVICE( filter ) );
74
75 // The result
76 mlt_geometry_item bounds = mlt_properties_get_data( properties, "bounds", NULL );
77
78 // Initialize if needed
79 if( bounds == NULL ) {
80 bounds = calloc( 1, sizeof( struct mlt_geometry_item_s ) );
81 bounds->w = *width;
82 bounds->h = *height;
83 mlt_properties_set_data( properties, "bounds", bounds, sizeof( struct mlt_geometry_item_s ), free, NULL );
84 }
85
86 // For periodic detection (with offset of 'skip')
87 if( frequency == 0 || (int)(mlt_filter_get_position(filter, this)+skip) % frequency != 0)
88 {
89 // Inject in stream
90 mlt_properties_set_data( MLT_FRAME_PROPERTIES(this), "bounds", bounds, sizeof( struct mlt_geometry_item_s ), NULL, NULL );
91
92 return 0;
93 }
94
95
96 // There is no way to detect a crop for sure, so make up an arbitrary one
97 int thresh = mlt_properties_get_int( properties, "thresh" );
98
99 *format = mlt_image_yuv422;
100 int xstride = 2;
101 int ystride = 2 * *width;
102
103 int x, y, average_brightness, deviation; // Scratch variables
104 uint8_t *q;
105
106 // Top crop
107 for( y = 0; y < *height/2; y++ ) {
108 bounds->y = y;
109 average_brightness = 0;
110 deviation = 0;
111 q = *image + y*ystride;
112 for( x = 0; x < *width; x++ )
113 average_brightness += q[x*xstride];
114
115 average_brightness /= *width;
116
117 for( x = 0; x < *width; x++ )
118 deviation += abs(average_brightness - q[x*xstride]);
119
120 if( deviation*10 >= thresh * *width )
121 break;
122 }
123
124 // Bottom crop
125 for( y = *height - 1; y >= *height/2; y-- ) {
126 bounds->h = y;
127 average_brightness = 0;
128 deviation = 0;
129 q = *image + y*ystride;
130 for( x = 0; x < *width; x++ )
131 average_brightness += q[x*xstride];
132
133 average_brightness /= *width;
134
135 for( x = 0; x < *width; x++ )
136 deviation += abs(average_brightness - q[x*xstride]);
137
138 if( deviation*10 >= thresh * *width)
139 break;
140 }
141
142 // Left crop
143 for( x = 0; x < *width/2; x++ ) {
144 bounds->x = x;
145 average_brightness = 0;
146 deviation = 0;
147 q = *image + x*xstride;
148 for( y = 0; y < *height; y++ )
149 average_brightness += q[y*ystride];
150
151 average_brightness /= *height;
152
153 for( y = 0; y < *height; y++ )
154 deviation += abs(average_brightness - q[y*ystride]);
155
156 if( deviation*10 >= thresh * *width )
157 break;
158 }
159
160 // Right crop
161 for( x = *width - 1; x >= *width/2; x-- ) {
162 bounds->w = x;
163 average_brightness = 0;
164 deviation = 0;
165 q = *image + x*xstride;
166 for( y = 0; y < *height; y++ )
167 average_brightness += q[y*ystride];
168
169 average_brightness /= *height;
170
171 for( y = 0; y < *height; y++ )
172 deviation += abs(average_brightness - q[y*ystride]);
173
174 if( deviation*10 >= thresh * *width )
175 break;
176 }
177
178 /* Debug: Draw arrows to show crop */
179 if( mlt_properties_get_int( properties, "debug") == 1 )
180 {
181 init_arrows( format, *width, *height );
182
183 draw_arrow(*image, bounds->x, *height/2, bounds->x+50, *height/2, 100);
184 draw_arrow(*image, *width/2, bounds->y, *width/2, bounds->y+50, 100);
185 draw_arrow(*image, bounds->w, *height/2, bounds->w-50, *height/2, 100);
186 draw_arrow(*image, *width/2, bounds->h, *width/2, bounds->h-50, 100);
187 draw_arrow(*image, bounds->x, bounds->y, bounds->x+40, bounds->y+30, 100);
188 draw_arrow(*image, bounds->x, bounds->h, bounds->x+40, bounds->h-30, 100);
189 draw_arrow(*image, bounds->w, bounds->y, bounds->w-40, bounds->y+30, 100);
190 draw_arrow(*image, bounds->w, bounds->h, bounds->w-40, bounds->h-30, 100);
191 }
192
193 // Convert to width and correct indexing
194 bounds->w -= bounds->x - 1;
195 bounds->h -= bounds->y - 1;
196
197 if( mlt_properties_get_int( properties, "debug") == 1 )
198 fprintf(stderr, "Top:%f Left:%f Width:%f Height:%f\n", bounds->y, bounds->x, bounds->w, bounds->h);
199
200 /* inject into frame */
201 mlt_properties_set_data( MLT_FRAME_PROPERTIES(this), "bounds", bounds, sizeof( struct mlt_geometry_item_s ), NULL, NULL );
202
203 mlt_service_unlock( MLT_FILTER_SERVICE( filter ) );
204
205 return error;
206 }
207
208
209
210 /** Filter processing.
211 */
212
filter_process(mlt_filter this,mlt_frame frame)213 static mlt_frame filter_process( mlt_filter this, mlt_frame frame )
214 {
215
216 // Put the filter object somewhere we can find it
217 mlt_frame_push_service( frame, this);
218
219 // Push the frame filter
220 mlt_frame_push_get_image( frame, filter_get_image );
221
222 return frame;
223 }
224
225 /** Constructor for the filter.
226 */
filter_crop_detect_init(mlt_profile profile,mlt_service_type type,const char * id,char * arg)227 mlt_filter filter_crop_detect_init( mlt_profile profile, mlt_service_type type, const char *id, char *arg )
228 {
229 mlt_filter this = mlt_filter_new( );
230 if ( this != NULL )
231 {
232 this->process = filter_process;
233
234 /* defaults */
235 mlt_properties_set_int( MLT_FILTER_PROPERTIES(this), "frequency", 1);
236 mlt_properties_set_int( MLT_FILTER_PROPERTIES(this), "thresh", 5);
237 mlt_properties_set_int( MLT_FILTER_PROPERTIES(this), "clip", 5);
238 mlt_properties_set_int( MLT_FILTER_PROPERTIES(this), "former_producer_id", -1);
239
240 }
241
242 return this;
243 }
244
245 /** This source code will self destruct in 5...4...3...
246 */
247
248