1 /*****************************************************************************
2 * motiondetec.c : Second version of a motion detection plugin.
3 *****************************************************************************
4 * Copyright (C) 2000-2006 the VideoLAN team
7 * Authors: Antoine Cellerier <dionoea -at- videolan -dot- org>
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version.
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
19 * You should have received a copy of the GNU General Public License
20 * along with this program; if not, write to the Free Software
21 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
22 *****************************************************************************/
24 /*****************************************************************************
26 *****************************************************************************/
33 #include <vlc_plugin.h>
37 #include "vlc_filter.h"
39 /*****************************************************************************
41 *****************************************************************************/
42 static int Create ( vlc_object_t * );
43 static void Destroy ( vlc_object_t * );
45 static picture_t *Filter( filter_t *, picture_t * );
46 static void GaussianConvolution( uint32_t *, uint32_t *, int, int, int );
48 /*****************************************************************************
50 *****************************************************************************/
52 #define FILTER_PREFIX "motiondetect-"
55 set_description( _("Motion detect video filter") );
56 set_shortname( _( "Motion Detect" ));
57 set_capability( "video filter2", 0 );
58 set_category( CAT_VIDEO );
59 set_subcategory( SUBCAT_VIDEO_VFILTER );
61 add_shortcut( "motion" );
62 set_callbacks( Create, Destroy );
66 static const char *ppsz_filter_options[] = {
81 /*****************************************************************************
83 *****************************************************************************/
84 static int Create( vlc_object_t *p_this )
86 filter_t *p_filter = (filter_t *)p_this;
88 /* Allocate structure */
89 p_filter->p_sys = malloc( sizeof( filter_sys_t ) );
90 if( p_filter->p_sys == NULL )
92 msg_Err( p_filter, "out of memory" );
96 p_filter->pf_video_filter = Filter;
98 p_filter->p_sys->p_oldpix = NULL;
99 p_filter->p_sys->p_buf = NULL;
102 config_ChainParse( p_filter, FILTER_PREFIX, ppsz_filter_options,
105 vlc_mutex_init( &p_filter->p_sys->lock );
110 /*****************************************************************************
112 *****************************************************************************/
113 static void Destroy( vlc_object_t *p_this )
115 filter_t *p_filter = (filter_t *)p_this;
117 free( p_filter->p_sys->p_oldpix );
118 free( p_filter->p_sys->p_buf );
120 vlc_mutex_destroy( &p_filter->p_sys->lock );
122 free( p_filter->p_sys );
125 /*****************************************************************************
127 *****************************************************************************/
128 static picture_t *Filter( filter_t *p_filter, picture_t *p_inpic )
131 filter_sys_t *p_sys = p_filter->p_sys;
133 const uint8_t *p_inpix = p_inpic->p[Y_PLANE].p_pixels;
134 const int i_src_pitch = p_inpic->p[Y_PLANE].i_pitch;
135 const int i_src_visible = p_inpic->p[Y_PLANE].i_visible_pitch;
136 const int i_num_lines = p_inpic->p[Y_PLANE].i_visible_lines;
138 const uint8_t *p_inpix_u = p_inpic->p[U_PLANE].p_pixels;
139 const uint8_t *p_inpix_v = p_inpic->p[V_PLANE].p_pixels;
140 const int i_src_pitch_u = p_inpic->p[U_PLANE].i_pitch;
141 const int i_num_lines_u = p_inpic->p[U_PLANE].i_visible_lines;
153 if( !p_inpic ) return NULL;
155 p_outpic = p_filter->pf_vout_buffer_new( p_filter );
158 msg_Warn( p_filter, "can't get output picture" );
159 if( p_inpic->pf_release )
160 p_inpic->pf_release( p_inpic );
164 p_outpix = p_outpic->p[Y_PLANE].p_pixels;
165 vlc_memcpy( p_outpic->p[U_PLANE].p_pixels, p_inpic->p[U_PLANE].p_pixels,
166 p_inpic->p[U_PLANE].i_pitch * p_inpic->p[U_PLANE].i_visible_lines );
167 vlc_memcpy( p_outpic->p[V_PLANE].p_pixels, p_inpic->p[V_PLANE].p_pixels,
168 p_inpic->p[V_PLANE].i_pitch * p_inpic->p[V_PLANE].i_visible_lines );
170 if( !p_sys->p_oldpix || !p_sys->p_buf )
172 free( p_sys->p_oldpix );
173 free( p_sys->p_buf );
174 p_sys->p_oldpix = malloc( i_src_pitch * i_num_lines );
175 p_sys->p_oldpix_u = malloc( i_src_pitch_u * i_num_lines_u );
176 p_sys->p_oldpix_v = malloc( i_src_pitch_u * i_num_lines_u );
177 p_sys->p_buf = malloc( sizeof( uint32_t ) * i_src_pitch * i_num_lines );
178 p_sys->p_buf2 = malloc( sizeof( uint32_t ) * i_src_pitch * i_num_lines);
181 p_oldpix = p_sys->p_oldpix;
182 p_oldpix_u = p_sys->p_oldpix_u;
183 p_oldpix_v = p_sys->p_oldpix_v;
184 p_buf = p_sys->p_buf;
185 p_buf2 = p_sys->p_buf2;
187 vlc_mutex_lock( &p_filter->p_sys->lock );
192 for( i = 0; i < i_src_pitch * i_num_lines; i++ )
194 if( p_inpix[i] > p_oldpix[i] )
196 p_buf2[i] = p_inpix[i] - p_oldpix[i];
200 p_buf2[i] = p_oldpix[i] - p_inpix[i];
206 switch( p_inpic->format.i_chroma )
208 case VLC_FOURCC('I','4','2','0'):
209 case VLC_FOURCC('I','Y','U','V'):
210 case VLC_FOURCC('J','4','2','0'):
211 case VLC_FOURCC('Y','V','1','2'):
215 case VLC_FOURCC('I','4','2','2'):
216 case VLC_FOURCC('J','4','2','2'):
222 msg_Warn( p_filter, "Not taking chroma into account" );
229 for( line = 0; line < i_num_lines_u; line++ )
231 for( col = 0; col < i_src_pitch_u; col ++ )
234 i = line * i_src_pitch_u + col;
235 if( p_inpix_u[i] > p_oldpix_u[i] )
237 diff = p_inpix_u[i] - p_oldpix_u[i];
241 diff = p_oldpix_u[i] - p_inpix_u[i];
243 if( p_inpix_v[i] > p_oldpix_v[i] )
245 diff += p_inpix_v[i] - p_oldpix_v[i];
249 diff += p_oldpix_v[i] - p_inpix_v[i];
254 p_buf2[2*line*i_src_pitch+2*col] += diff;
255 p_buf2[2*line*i_src_pitch+2*col+1] += diff;
256 p_buf2[(2*line+1)*i_src_pitch+2*col] += diff;
257 p_buf2[(2*line+1)*i_src_pitch+2*col+1] += diff;
261 p_buf2[line*i_src_pitch+2*col] += diff;
262 p_buf2[line*i_src_pitch+2*col+1] += diff;
270 * Apply some smoothing to remove noise
272 GaussianConvolution( p_buf2, p_buf, i_src_pitch, i_num_lines, i_src_visible );
275 * Copy luminance plane
277 for( i = 0; i < i_src_pitch * i_num_lines; i++ )
279 p_outpix[i] = p_inpix[i];
283 * Label the shapes ans build the labels dependencies list
287 int color_x_min[5000];
288 int color_x_max[5000];
289 int color_y_min[5000];
290 int color_y_max[5000];
292 for( j = 0; j < i_src_pitch; j++ )
295 p_buf[(i_num_lines-1)*i_src_pitch+j] = 0;
297 for( i = 1; i < i_num_lines-1; i++ )
299 p_buf[i*i_src_pitch] = 0;
300 for( j = 1; j < i_src_pitch-1; j++ )
302 if( p_buf[i*i_src_pitch+j] > 15 )
304 if( p_buf[(i-1)*i_src_pitch+j-1] )
306 p_buf[i*i_src_pitch+j] = p_buf[(i-1)*i_src_pitch+j-1];
308 else if( p_buf[(i-1)*i_src_pitch+j] )
309 p_buf[i*i_src_pitch+j] = p_buf[(i-1)*i_src_pitch+j];
310 else if( p_buf[i*i_src_pitch+j-1] )
311 p_buf[i*i_src_pitch+j] = p_buf[i*i_src_pitch+j-1];
314 p_buf[i*i_src_pitch+j] = last;
319 if( p_buf[A] && p_buf[A] != p_buf[i*i_src_pitch+j] ) \
321 if( p_buf[A] < p_buf[i*i_src_pitch+j] ) \
322 colors[p_buf[i*i_src_pitch+j]] = p_buf[A]; \
324 colors[p_buf[A]] = p_buf[i*i_src_pitch+j]; \
326 CHECK( i*i_src_pitch+j-1 );
327 CHECK( (i-1)*i_src_pitch+j-1 );
328 CHECK( (i-1)*i_src_pitch+j );
329 CHECK( (i-1)*i_src_pitch+j+1 );
333 p_buf[i*i_src_pitch+j] = 0;
336 p_buf[i*i_src_pitch+j] = 0;
340 * Initialise empty rectangle list
342 for( i = 1; i < last; i++ )
351 * Compute rectangle coordinates
353 for( i = 0; i < i_src_pitch * i_num_lines; i++ )
357 while( colors[p_buf[i]] != p_buf[i] )
358 p_buf[i] = colors[p_buf[i]];
359 if( color_x_min[p_buf[i]] == -1 )
361 color_x_min[p_buf[i]] =
362 color_x_max[p_buf[i]] = i % i_src_pitch;
363 color_y_min[p_buf[i]] =
364 color_y_max[p_buf[i]] = i / i_src_pitch;
368 int x = i % i_src_pitch, y = i / i_src_pitch;
369 if( x < color_x_min[p_buf[i]] )
370 color_x_min[p_buf[i]] = x;
371 if( x > color_x_max[p_buf[i]] )
372 color_x_max[p_buf[i]] = x;
373 if( y < color_y_min[p_buf[i]] )
374 color_y_min[p_buf[i]] = y;
375 if( y > color_y_max[p_buf[i]] )
376 color_y_max[p_buf[i]] = y;
382 * Merge overlaping rectangles
384 for( i = 1; i < last; i++ )
386 if( colors[i] != i ) continue;
387 if( color_x_min[i] == -1 ) continue;
388 for( j = i+1; j < last; j++ )
390 if( colors[j] != j ) continue;
391 if( color_x_min[j] == -1 ) continue;
392 if( __MAX( color_x_min[i], color_x_min[j] ) < __MIN( color_x_max[i], color_x_max[j] ) &&
393 __MAX( color_y_min[i], color_y_min[j] ) < __MIN( color_y_max[i], color_y_max[j] ) )
395 color_x_min[i] = __MIN( color_x_min[i], color_x_min[j] );
396 color_x_max[i] = __MAX( color_x_max[i], color_x_max[j] );
397 color_y_min[i] = __MIN( color_y_min[i], color_y_min[j] );
398 color_y_max[i] = __MAX( color_y_max[i], color_y_max[j] );
406 * Count final number of shapes
407 * Draw rectangles (there can be more than 1 moving shape in 1 rectangle)
410 for( i = 1; i < last; i++ )
412 if( colors[i] == i && color_x_min[i] != -1 )
414 if( ( color_y_max[i] - color_y_min[i] ) * ( color_x_max[i] - color_x_min[i] ) < 16 ) continue;
418 for( x = color_x_min[i]; x <= color_x_max[i]; x++ )
420 p_outpix[y*i_src_pitch+x] = 0xff;
423 for( x = color_x_min[i]; x <= color_x_max[i]; x++ )
425 p_outpix[y*i_src_pitch+x] = 0xff;
428 for( y = color_y_min[i]; y <= color_y_max[i]; y++ )
430 p_outpix[y*i_src_pitch+x] = 0xff;
433 for( y = color_y_min[i]; y <= color_y_max[i]; y++ )
435 p_outpix[y*i_src_pitch+x] = 0xff;
439 msg_Dbg( p_filter, "Counted %d moving shapes.", j);
442 * We're done. Lets keep a copy of the picture
444 vlc_memcpy( p_oldpix, p_inpix, i_src_pitch * i_num_lines );
445 vlc_memcpy( p_oldpix_u, p_inpix_u, i_src_pitch_u * i_num_lines_u );
446 vlc_memcpy( p_oldpix_v, p_inpix_v, i_src_pitch_u * i_num_lines_u );
448 vlc_mutex_unlock( &p_filter->p_sys->lock );
451 p_outpic->date = p_inpic->date;
452 p_outpic->b_force = p_inpic->b_force;
453 p_outpic->i_nb_fields = p_inpic->i_nb_fields;
454 p_outpic->b_progressive = p_inpic->b_progressive;
455 p_outpic->b_top_field_first = p_inpic->b_top_field_first;
457 if( p_inpic->pf_release )
458 p_inpic->pf_release( p_inpic );
464 /*****************************************************************************
465 * Gaussian Convolution
466 *****************************************************************************
467 * Gaussian convolution ( sigma == 1.4 )
469 * | 2 4 5 4 2 | | 2 4 4 4 2 |
470 * | 4 9 12 9 4 | | 4 8 12 8 4 |
471 * | 5 12 15 12 5 | ~ | 4 12 16 12 4 |
472 * | 4 9 12 9 4 | | 4 8 12 8 4 |
473 * | 2 4 5 4 2 | | 2 4 4 4 2 |
474 *****************************************************************************/
475 static void GaussianConvolution( uint32_t *p_inpix, uint32_t *p_smooth,
476 int i_src_pitch, int i_num_lines,
479 /* const uint8_t *p_inpix = p_inpic->p[Y_PLANE].p_pixels;
480 const int i_src_pitch = p_inpic->p[Y_PLANE].i_pitch;
481 const int i_src_visible = p_inpic->p[Y_PLANE].i_visible_pitch;
482 const int i_num_lines = p_inpic->p[Y_PLANE].i_visible_lines;*/
485 for( y = 2; y < i_num_lines - 2; y++ )
487 for( x = 2; x < i_src_visible - 2; x++ )
489 p_smooth[y*i_src_visible+x] = (uint32_t)(
491 ( p_inpix[(y-2)*i_src_pitch+x-2] )
492 + ((p_inpix[(y-2)*i_src_pitch+x-1]
493 + p_inpix[(y-2)*i_src_pitch+x]
494 + p_inpix[(y-2)*i_src_pitch+x+1])<<1 )
495 + ( p_inpix[(y-2)*i_src_pitch+x+2] )
497 + ((p_inpix[(y-1)*i_src_pitch+x-2]
498 + ( p_inpix[(y-1)*i_src_pitch+x-1]<<1 )
499 + ( p_inpix[(y-1)*i_src_pitch+x]*3 )
500 + ( p_inpix[(y-1)*i_src_pitch+x+1]<<1 )
501 + p_inpix[(y-1)*i_src_pitch+x+2]
503 + p_inpix[y*i_src_pitch+x-2]
504 + ( p_inpix[y*i_src_pitch+x-1]*3 )
505 + ( p_inpix[y*i_src_pitch+x]<<2 )
506 + ( p_inpix[y*i_src_pitch+x+1]*3 )
507 + p_inpix[y*i_src_pitch+x+2]
509 + p_inpix[(y+1)*i_src_pitch+x-2]
510 + ( p_inpix[(y+1)*i_src_pitch+x-1]<<1 )
511 + ( p_inpix[(y+1)*i_src_pitch+x]*3 )
512 + ( p_inpix[(y+1)*i_src_pitch+x+1]<<1 )
513 + p_inpix[(y+1)*i_src_pitch+x+2] )<<1 )
515 + ( p_inpix[(y+2)*i_src_pitch+x-2] )
516 + ((p_inpix[(y+2)*i_src_pitch+x-1]
517 + p_inpix[(y+2)*i_src_pitch+x]
518 + p_inpix[(y+2)*i_src_pitch+x+1])<<1 )
519 + ( p_inpix[(y+2)*i_src_pitch+x+2] )