]> git.sesse.net Git - mlt/blob - src/modules/motion_est/filter_crop_detect.c
8165141a380ae20b2f29989096d06dbdb29936e9
[mlt] / src / modules / motion_est / filter_crop_detect.c
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., 59 Temple Place - Suite 330, Boston, MA 02111-1307, 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
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
52         // Get the new image
53         int error = mlt_frame_get_image( this, image, format, width, height, 1 );
54
55         if( error != 0 ) {
56                 mlt_properties_debug( MLT_FRAME_PROPERTIES(this), "error after mlt_frame_get_image()", stderr );
57                 return error;
58         }
59
60         // Parameter that describes how often to check for the crop
61         int frequency = mlt_properties_get_int( properties, "frequency");
62
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");
65
66         // The result
67         mlt_geometry_item bounds = mlt_properties_get_data( properties, "bounds", NULL );
68
69         // Initialize if needed
70         if( bounds == NULL ) {
71                 bounds = calloc( 1, sizeof( struct mlt_geometry_item_s ) );
72                 bounds->w = *width;
73                 bounds->h = *height;
74                 mlt_properties_set_data( properties, "bounds", bounds, sizeof( struct mlt_geometry_item_s ), free, NULL );
75         }
76
77         // For periodic detection (with offset of 'skip')
78         if( frequency == 0 || (int)(mlt_frame_get_position(this)+skip) % frequency  != 0)
79         {
80                 // Inject in stream 
81                 mlt_properties_set_data( MLT_FRAME_PROPERTIES(this), "bounds", bounds, sizeof( struct mlt_geometry_item_s ), NULL, NULL );
82
83                 return 0;
84         }
85         
86
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" );
89
90         int xstride, ystride;
91
92         switch( *format ) {
93                 case mlt_image_yuv422:
94                         xstride = 2;
95                         ystride = 2 * *width;
96                         break;
97                 default:
98                         fprintf(stderr, "image format not supported by filter_crop_detect\n");
99                         return -1;
100         }
101
102         int x, y, average_brightness, deviation; // Scratch variables
103         uint8_t *q;
104
105         // Top crop
106         for( y = 0; y < *height/2; y++ ) {
107                 bounds->y = y;
108                 average_brightness = 0;
109                 deviation = 0;
110                 q = *image + y*ystride;
111                 for( x = 0; x < *width; x++ )
112                         average_brightness += q[x*xstride];
113
114                 average_brightness /= *width;
115
116                 for( x = 0; x < *width; x++ )
117                         deviation += abs(average_brightness - q[x*xstride]);
118
119                 if( deviation*10 >= thresh * *width )
120                         break;
121         }
122
123         // Bottom crop
124         for( y = *height - 1; y >= *height/2; y-- ) {
125                 bounds->h = y;
126                 average_brightness = 0;
127                 deviation = 0;
128                 q = *image + y*ystride;
129                 for( x = 0; x < *width; x++ )
130                         average_brightness += q[x*xstride];
131
132                 average_brightness /= *width;
133
134                 for( x = 0; x < *width; x++ )
135                         deviation += abs(average_brightness - q[x*xstride]);
136
137                 if( deviation*10 >= thresh * *width)
138                         break;
139         }
140
141         // Left crop    
142         for( x = 0; x < *width/2; x++ ) {
143                 bounds->x = x;
144                 average_brightness = 0;
145                 deviation = 0;
146                 q = *image + x*xstride;
147                 for( y = 0; y < *height; y++ )
148                         average_brightness += q[y*ystride];
149
150                 average_brightness /= *height;
151
152                 for( y = 0; y < *height; y++ )
153                         deviation += abs(average_brightness - q[y*ystride]);
154
155                 if( deviation*10 >= thresh * *width )
156                         break;
157         }
158
159         // Right crop
160         for( x = *width - 1; x >= *width/2; x-- ) {
161                 bounds->w = x;
162                 average_brightness = 0;
163                 deviation = 0;
164                 q = *image + x*xstride;
165                 for( y = 0; y < *height; y++ )
166                         average_brightness += q[y*ystride];
167
168                 average_brightness /= *height;
169
170                 for( y = 0; y < *height; y++ )
171                         deviation += abs(average_brightness - q[y*ystride]);
172
173                 if( deviation*10 >= thresh * *width )
174                         break;
175         }
176
177         /* Debug: Draw arrows to show crop */
178         if( mlt_properties_get_int( properties, "debug") == 1 )
179         {
180                 init_arrows( format, *width, *height );
181
182                 draw_arrow(*image, bounds->x, *height/2, bounds->x+50, *height/2, 100);
183                 draw_arrow(*image, *width/2, bounds->y, *width/2, bounds->y+50, 100);
184                 draw_arrow(*image, bounds->w, *height/2, bounds->w-50, *height/2, 100);
185                 draw_arrow(*image, *width/2, bounds->h, *width/2, bounds->h-50, 100);
186                 draw_arrow(*image, bounds->x, bounds->y, bounds->x+40, bounds->y+30, 100);
187                 draw_arrow(*image, bounds->x, bounds->h, bounds->x+40, bounds->h-30, 100);
188                 draw_arrow(*image, bounds->w, bounds->y, bounds->w-40, bounds->y+30, 100);
189                 draw_arrow(*image, bounds->w, bounds->h, bounds->w-40, bounds->h-30, 100);
190         }
191
192         // Convert to width and correct indexing
193         bounds->w -= bounds->x - 1;
194         bounds->h -= bounds->y - 1;
195
196         if( mlt_properties_get_int( properties, "debug") == 1 )
197                 fprintf(stderr, "Top:%f Left:%f Width:%f Height:%f\n", bounds->y, bounds->x, bounds->w, bounds->h);
198
199         /* inject into frame */
200         mlt_properties_set_data( MLT_FRAME_PROPERTIES(this), "bounds", bounds, sizeof( struct mlt_geometry_item_s ), NULL, NULL );
201
202         return error;
203 }
204
205
206
207 /** Filter processing.
208 */
209
210 static mlt_frame filter_process( mlt_filter this, mlt_frame frame )
211 {
212
213         // Put the filter object somewhere we can find it
214         mlt_frame_push_service( frame, this);
215
216         // Push the frame filter
217         mlt_frame_push_get_image( frame, filter_get_image );
218
219         return frame;
220 }
221
222 /** Constructor for the filter.
223 */
224 mlt_filter filter_crop_detect_init( mlt_profile profile, mlt_service_type type, const char *id, char *arg )
225 {
226         mlt_filter this = mlt_filter_new( );
227         if ( this != NULL )
228         {
229                 this->process = filter_process;
230
231                 /* defaults */
232                 mlt_properties_set_int( MLT_FILTER_PROPERTIES(this), "frequency", 1);
233                 mlt_properties_set_int( MLT_FILTER_PROPERTIES(this), "thresh", 5);
234                 mlt_properties_set_int( MLT_FILTER_PROPERTIES(this), "clip", 5);
235                 mlt_properties_set_int( MLT_FILTER_PROPERTIES(this), "former_producer_id", -1);
236
237         }
238
239         return this;
240 }
241
242 /** This source code will self destruct in 5...4...3...
243 */
244