1 /*****************************************************************************
2 * motiondetec.c : Second version of a motion detection plugin.
3 *****************************************************************************
4 * Copyright (C) 2000-2008 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 *****************************************************************************/
32 #include <vlc_common.h>
33 #include <vlc_plugin.h>
37 #include "vlc_filter.h"
38 #include "filter_picture.h"
40 /*****************************************************************************
42 *****************************************************************************/
43 static int Create ( vlc_object_t * );
44 static void Destroy ( vlc_object_t * );
46 #define FILTER_PREFIX "motiondetect-"
49 set_description( N_("Motion detect video filter") )
50 set_shortname( N_( "Motion Detect" ))
51 set_category( CAT_VIDEO )
52 set_subcategory( SUBCAT_VIDEO_VFILTER )
53 set_capability( "video filter2", 0 )
55 add_shortcut( "motion" )
56 set_callbacks( Create, Destroy )
60 /*****************************************************************************
62 *****************************************************************************/
63 static picture_t *Filter( filter_t *, picture_t * );
64 static picture_t *FilterPacked( filter_t *, picture_t * );
65 static void GaussianConvolution( uint32_t *, uint32_t *, int, int, int );
66 static int FindShapes( uint32_t *, uint32_t *, int, int, int,
67 int *, int *, int *, int *, int *);
68 static void Draw( filter_t *p_filter, uint8_t *p_pix, int i_pix_pitch, int i_pix_size );
69 #define NUM_COLORS (5000)
80 int colors[NUM_COLORS];
81 int color_x_min[NUM_COLORS];
82 int color_x_max[NUM_COLORS];
83 int color_y_min[NUM_COLORS];
84 int color_y_max[NUM_COLORS];
87 /*****************************************************************************
89 *****************************************************************************/
90 static int Create( vlc_object_t *p_this )
92 filter_t *p_filter = (filter_t *)p_this;
93 const video_format_t *p_fmt = &p_filter->fmt_in.video;
96 switch( p_fmt->i_chroma )
99 p_filter->pf_video_filter = Filter;
103 p_filter->pf_video_filter = FilterPacked;
107 msg_Err( p_filter, "Unsupported input chroma (%4s)",
108 (char*)&(p_fmt->i_chroma) );
112 /* Allocate structure */
113 p_filter->p_sys = p_sys = malloc( sizeof( filter_sys_t ) );
114 if( p_filter->p_sys == NULL )
117 p_sys->b_old = false;
118 p_sys->p_old = picture_New( p_fmt->i_chroma,
119 p_fmt->i_width, p_fmt->i_height, 0 );
120 p_sys->p_buf = calloc( p_fmt->i_width * p_fmt->i_height, sizeof(*p_sys->p_buf) );
121 p_sys->p_buf2 = calloc( p_fmt->i_width * p_fmt->i_height, sizeof(*p_sys->p_buf) );
123 if( !p_sys->p_old || !p_sys->p_buf || !p_sys->p_buf2 )
125 free( p_sys->p_buf2 );
126 free( p_sys->p_buf );
128 picture_Release( p_sys->p_old );
135 /*****************************************************************************
137 *****************************************************************************/
138 static void Destroy( vlc_object_t *p_this )
140 filter_t *p_filter = (filter_t *)p_this;
141 filter_sys_t *p_sys = p_filter->p_sys;
143 free( p_sys->p_buf2 );
144 free( p_sys->p_buf );
145 picture_Release( p_sys->p_old );
150 /*****************************************************************************
152 *****************************************************************************/
153 static picture_t *Filter( filter_t *p_filter, picture_t *p_inpic )
155 filter_sys_t *p_sys = p_filter->p_sys;
156 const video_format_t *p_fmt = &p_filter->fmt_in.video;
160 const uint8_t *p_inpix = p_inpic->p[Y_PLANE].p_pixels;
161 const int i_src_pitch = p_inpic->p[Y_PLANE].i_pitch;
163 uint8_t *p_oldpix = p_sys->p_old->p[Y_PLANE].p_pixels;
164 const int i_old_pitch = p_sys->p_old->p[Y_PLANE].i_pitch;
165 uint32_t *p_buf = p_sys->p_buf;
166 uint32_t *p_buf2= p_sys->p_buf2;
175 picture_Copy( p_sys->p_old, p_inpic );
180 p_outpic = filter_NewPicture( p_filter );
183 picture_Release( p_inpic );
186 picture_Copy( p_outpic, p_inpic );
191 for( y = 0; y < p_fmt->i_height; y++ )
193 for( x = 0; x < p_fmt->i_width; x++ )
194 p_buf2[y*p_fmt->i_width+x] = abs( p_inpix[y*i_src_pitch+x] - p_oldpix[y*i_old_pitch+x] );
199 switch( p_inpic->format.i_chroma )
201 case VLC_FOURCC('I','4','2','0'):
202 case VLC_FOURCC('I','Y','U','V'):
203 case VLC_FOURCC('J','4','2','0'):
204 case VLC_FOURCC('Y','V','1','2'):
209 case VLC_FOURCC('I','4','2','2'):
210 case VLC_FOURCC('J','4','2','2'):
216 msg_Warn( p_filter, "Not taking chroma into account" );
222 if( i_chroma_dx != 0 && i_chroma_dy != 0 )
224 const uint8_t *p_inpix_u = p_inpic->p[U_PLANE].p_pixels;
225 const uint8_t *p_inpix_v = p_inpic->p[V_PLANE].p_pixels;
226 const int i_src_pitch_u = p_inpic->p[U_PLANE].i_pitch;
227 const int i_src_pitch_v = p_inpic->p[V_PLANE].i_pitch;
229 const uint8_t *p_oldpix_u = p_sys->p_old->p[U_PLANE].p_pixels;
230 const uint8_t *p_oldpix_v = p_sys->p_old->p[V_PLANE].p_pixels;
231 const int i_old_pitch_u = p_sys->p_old->p[U_PLANE].i_pitch;
232 const int i_old_pitch_v = p_sys->p_old->p[V_PLANE].i_pitch;
234 for( y = 0; y < p_fmt->i_height/i_chroma_dy; y++ )
236 for( x = 0; x < p_fmt->i_width/i_chroma_dx; x ++ )
238 const int d = abs( p_inpix_u[y*i_src_pitch_u+x] - p_oldpix_u[y*i_old_pitch_u+x] ) +
239 abs( p_inpix_v[y*i_src_pitch_v+x] - p_oldpix_v[y*i_old_pitch_v+x] );
242 for( j = 0; j < i_chroma_dy; j++ )
244 for( i = 0; i < i_chroma_dx; i++ )
245 p_buf2[i_chroma_dy*p_fmt->i_width*j + i_chroma_dx*i] = d;
252 * Get the areas where movement was detected
254 p_sys->i_colors = FindShapes( p_buf2, p_buf, p_fmt->i_width, p_fmt->i_width, p_fmt->i_height,
255 p_sys->colors, p_sys->color_x_min, p_sys->color_x_max, p_sys->color_y_min, p_sys->color_y_max );
258 * Count final number of shapes
259 * Draw rectangles (there can be more than 1 moving shape in 1 rectangle)
261 Draw( p_filter, p_outpic->p[Y_PLANE].p_pixels, p_outpic->p[Y_PLANE].i_pitch, 1 );
264 * We're done. Lets keep a copy of the picture
265 * TODO we may just picture_Release with a latency of 1 if the filters/vout
266 * handle it correctly */
267 picture_Copy( p_sys->p_old, p_inpic );
269 picture_Release( p_inpic );
273 /*****************************************************************************
275 *****************************************************************************/
276 static picture_t *FilterPacked( filter_t *p_filter, picture_t *p_inpic )
278 filter_sys_t *p_sys = p_filter->p_sys;
279 const video_format_t *p_fmt = &p_filter->fmt_in.video;
282 const uint8_t *p_inpix = p_inpic->p[Y_PLANE].p_pixels;
283 const int i_src_pitch = p_inpic->p[Y_PLANE].i_pitch;
285 uint8_t *p_oldpix = p_sys->p_old->p[Y_PLANE].p_pixels;
286 const int i_old_pitch = p_sys->p_old->p[Y_PLANE].i_pitch;
287 uint32_t *p_buf = p_sys->p_buf;
288 uint32_t *p_buf2= p_sys->p_buf2;
290 int i_y_offset, i_u_offset, i_v_offset;
294 if( GetPackedYuvOffsets( p_fmt->i_chroma,
295 &i_y_offset, &i_u_offset, &i_v_offset ) )
297 msg_Warn( p_filter, "Unsupported input chroma (%4s)",
298 (char*)&p_fmt->i_chroma );
304 picture_Copy( p_sys->p_old, p_inpic );
309 p_outpic = filter_NewPicture( p_filter );
312 picture_Release( p_inpic );
315 picture_Copy( p_outpic, p_inpic );
317 /* Substract all planes at once */
319 for( y = 0; y < p_fmt->i_height; y++ )
321 for( x = 0; x < p_fmt->i_width; x+=2 )
326 d = abs( p_inpix[y*i_src_pitch+2*x+i_u_offset] - p_oldpix[y*i_old_pitch+2*x+i_u_offset] ) +
327 abs( p_inpix[y*i_src_pitch+2*x+i_v_offset] - p_oldpix[y*i_old_pitch+2*x+i_v_offset] );
329 for( i = 0; i < 2; i++ )
330 p_buf2[y*p_fmt->i_width+x+i] =
331 abs( p_inpix[y*i_src_pitch+2*(x+i)+i_y_offset] - p_oldpix[y*i_old_pitch+2*(x+i)+i_y_offset] ) + d;
336 * Get the areas where movement was detected
338 p_sys->i_colors = FindShapes( p_buf2, p_buf, p_fmt->i_width, p_fmt->i_width, p_fmt->i_height,
339 p_sys->colors, p_sys->color_x_min, p_sys->color_x_max, p_sys->color_y_min, p_sys->color_y_max );
342 * Count final number of shapes
343 * Draw rectangles (there can be more than 1 moving shape in 1 rectangle)
345 Draw( p_filter, &p_outpic->p[Y_PLANE].p_pixels[i_y_offset], p_outpic->p[Y_PLANE].i_pitch, 2 );
348 * We're done. Lets keep a copy of the picture
349 * TODO we may just picture_Release with a latency of 1 if the filters/vout
350 * handle it correctly */
351 picture_Copy( p_sys->p_old, p_inpic );
353 picture_Release( p_inpic );
359 /*****************************************************************************
360 * Gaussian Convolution
361 *****************************************************************************
362 * Gaussian convolution ( sigma == 1.4 )
364 * | 2 4 5 4 2 | | 2 4 4 4 2 |
365 * | 4 9 12 9 4 | | 4 8 12 8 4 |
366 * | 5 12 15 12 5 | ~ | 4 12 16 12 4 |
367 * | 4 9 12 9 4 | | 4 8 12 8 4 |
368 * | 2 4 5 4 2 | | 2 4 4 4 2 |
369 *****************************************************************************/
370 static void GaussianConvolution( uint32_t *p_inpix, uint32_t *p_smooth,
371 int i_src_pitch, int i_num_lines,
376 /* A bit overkill but ... simpler */
377 memset( p_smooth, 0, sizeof(*p_smooth) * i_src_pitch * i_num_lines );
379 for( y = 2; y < i_num_lines - 2; y++ )
381 for( x = 2; x < i_src_visible - 2; x++ )
383 p_smooth[y*i_src_visible+x] = (uint32_t)(
385 ( p_inpix[(y-2)*i_src_pitch+x-2] )
386 + ((p_inpix[(y-2)*i_src_pitch+x-1]
387 + p_inpix[(y-2)*i_src_pitch+x]
388 + p_inpix[(y-2)*i_src_pitch+x+1])<<1 )
389 + ( p_inpix[(y-2)*i_src_pitch+x+2] )
391 + ((p_inpix[(y-1)*i_src_pitch+x-2]
392 + ( p_inpix[(y-1)*i_src_pitch+x-1]<<1 )
393 + ( p_inpix[(y-1)*i_src_pitch+x]*3 )
394 + ( p_inpix[(y-1)*i_src_pitch+x+1]<<1 )
395 + p_inpix[(y-1)*i_src_pitch+x+2]
397 + p_inpix[y*i_src_pitch+x-2]
398 + ( p_inpix[y*i_src_pitch+x-1]*3 )
399 + ( p_inpix[y*i_src_pitch+x]<<2 )
400 + ( p_inpix[y*i_src_pitch+x+1]*3 )
401 + p_inpix[y*i_src_pitch+x+2]
403 + p_inpix[(y+1)*i_src_pitch+x-2]
404 + ( p_inpix[(y+1)*i_src_pitch+x-1]<<1 )
405 + ( p_inpix[(y+1)*i_src_pitch+x]*3 )
406 + ( p_inpix[(y+1)*i_src_pitch+x+1]<<1 )
407 + p_inpix[(y+1)*i_src_pitch+x+2] )<<1 )
409 + ( p_inpix[(y+2)*i_src_pitch+x-2] )
410 + ((p_inpix[(y+2)*i_src_pitch+x-1]
411 + p_inpix[(y+2)*i_src_pitch+x]
412 + p_inpix[(y+2)*i_src_pitch+x+1])<<1 )
413 + ( p_inpix[(y+2)*i_src_pitch+x+2] )
419 /*****************************************************************************
421 *****************************************************************************/
422 static int FindShapes( uint32_t *p_diff, uint32_t *p_smooth,
423 int i_pitch, int i_visible, int i_lines,
425 int *color_x_min, int *color_x_max,
426 int *color_y_min, int *color_y_max )
432 * Apply some smoothing to remove noise
434 GaussianConvolution( p_diff, p_smooth, i_pitch, i_lines, i_visible );
437 * Label the shapes and build the labels dependencies list
439 for( j = 0; j < i_pitch; j++ )
442 p_smooth[(i_lines-1)*i_pitch+j] = 0;
444 for( i = 1; i < i_lines-1; i++ )
446 p_smooth[i*i_pitch] = 0;
447 for( j = 1; j < i_pitch-1; j++ )
449 if( p_smooth[i*i_pitch+j] > 15 )
451 if( p_smooth[(i-1)*i_pitch+j-1] )
453 p_smooth[i*i_pitch+j] = p_smooth[(i-1)*i_pitch+j-1];
455 else if( p_smooth[(i-1)*i_pitch+j] )
456 p_smooth[i*i_pitch+j] = p_smooth[(i-1)*i_pitch+j];
457 else if( p_smooth[i*i_pitch+j-1] )
458 p_smooth[i*i_pitch+j] = p_smooth[i*i_pitch+j-1];
461 if( last < NUM_COLORS )
463 p_smooth[i*i_pitch+j] = last;
469 if( p_smooth[A] && p_smooth[A] != p_smooth[i*i_pitch+j] ) \
471 if( p_smooth[A] < p_smooth[i*i_pitch+j] ) \
472 colors[p_smooth[i*i_pitch+j]] = p_smooth[A]; \
474 colors[p_smooth[A]] = p_smooth[i*i_pitch+j]; \
476 CHECK( i*i_pitch+j-1 );
477 CHECK( (i-1)*i_pitch+j-1 );
478 CHECK( (i-1)*i_pitch+j );
479 CHECK( (i-1)*i_pitch+j+1 );
484 p_smooth[i*i_pitch+j] = 0;
487 p_smooth[i*i_pitch+j] = 0;
491 * Initialise empty rectangle list
493 for( i = 1; i < last; i++ )
502 * Compute rectangle coordinates
504 for( i = 0; i < i_pitch * i_lines; i++ )
508 while( colors[p_smooth[i]] != (int)p_smooth[i] )
509 p_smooth[i] = colors[p_smooth[i]];
510 if( color_x_min[p_smooth[i]] == -1 )
512 color_x_min[p_smooth[i]] =
513 color_x_max[p_smooth[i]] = i % i_pitch;
514 color_y_min[p_smooth[i]] =
515 color_y_max[p_smooth[i]] = i / i_pitch;
519 int x = i % i_pitch, y = i / i_pitch;
520 if( x < color_x_min[p_smooth[i]] )
521 color_x_min[p_smooth[i]] = x;
522 if( x > color_x_max[p_smooth[i]] )
523 color_x_max[p_smooth[i]] = x;
524 if( y < color_y_min[p_smooth[i]] )
525 color_y_min[p_smooth[i]] = y;
526 if( y > color_y_max[p_smooth[i]] )
527 color_y_max[p_smooth[i]] = y;
533 * Merge overlaping rectangles
535 for( i = 1; i < last; i++ )
537 if( colors[i] != i ) continue;
538 if( color_x_min[i] == -1 ) continue;
539 for( j = i+1; j < last; j++ )
541 if( colors[j] != j ) continue;
542 if( color_x_min[j] == -1 ) continue;
543 if( __MAX( color_x_min[i], color_x_min[j] ) < __MIN( color_x_max[i], color_x_max[j] ) &&
544 __MAX( color_y_min[i], color_y_min[j] ) < __MIN( color_y_max[i], color_y_max[j] ) )
546 color_x_min[i] = __MIN( color_x_min[i], color_x_min[j] );
547 color_x_max[i] = __MAX( color_x_max[i], color_x_max[j] );
548 color_y_min[i] = __MIN( color_y_min[i], color_y_min[j] );
549 color_y_max[i] = __MAX( color_y_max[i], color_y_max[j] );
559 static void Draw( filter_t *p_filter, uint8_t *p_pix, int i_pix_pitch, int i_pix_size )
561 filter_sys_t *p_sys = p_filter->p_sys;
564 for( i = 1, j = 0; i < p_sys->i_colors; i++ )
568 if( p_sys->colors[i] != i )
571 const int color_x_min = p_sys->color_x_min[i];
572 const int color_x_max = p_sys->color_x_max[i];
573 const int color_y_min = p_sys->color_y_min[i];
574 const int color_y_max = p_sys->color_y_max[i];
576 if( color_x_min == -1 )
578 if( ( color_y_max - color_y_min ) * ( color_x_max - color_x_min ) < 16 )
584 for( x = color_x_min; x <= color_x_max; x++ )
585 p_pix[y*i_pix_pitch+x*i_pix_size] = 0xff;
588 for( x = color_x_min; x <= color_x_max; x++ )
589 p_pix[y*i_pix_pitch+x*i_pix_size] = 0xff;
592 for( y = color_y_min; y <= color_y_max; y++ )
593 p_pix[y*i_pix_pitch+x*i_pix_size] = 0xff;
596 for( y = color_y_min; y <= color_y_max; y++ )
597 p_pix[y*i_pix_pitch+x*i_pix_size] = 0xff;
599 msg_Dbg( p_filter, "Counted %d moving shapes.", j );