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