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 static picture_t *Filter( filter_t *, picture_t * );
47 static picture_t *FilterPacked( filter_t *, picture_t * );
48 static void GaussianConvolution( uint32_t *, uint32_t *, int, int, int );
49 static int FindShapes( uint32_t *, uint32_t *, int, int, int,
50 int *, int *, int *, int *, int *);
52 #define NUM_COLORS 5000
54 /*****************************************************************************
56 *****************************************************************************/
58 #define FILTER_PREFIX "motiondetect-"
61 set_description( N_("Motion detect video filter") );
62 set_shortname( N_( "Motion Detect" ));
63 set_capability( "video filter2", 0 );
64 set_category( CAT_VIDEO );
65 set_subcategory( SUBCAT_VIDEO_VFILTER );
67 add_shortcut( "motion" );
68 set_callbacks( Create, Destroy );
81 /*****************************************************************************
83 *****************************************************************************/
84 static int Create( vlc_object_t *p_this )
86 filter_t *p_filter = (filter_t *)p_this;
88 switch( p_filter->fmt_in.video.i_chroma )
91 p_filter->pf_video_filter = Filter;
95 p_filter->pf_video_filter = FilterPacked;
99 msg_Err( p_filter, "Unsupported input chroma (%4s)",
100 (char*)&(p_filter->fmt_in.video.i_chroma) );
104 /* Allocate structure */
105 p_filter->p_sys = malloc( sizeof( filter_sys_t ) );
106 if( p_filter->p_sys == NULL )
109 p_filter->p_sys->p_oldpix = NULL;
110 p_filter->p_sys->p_buf = NULL;
112 vlc_mutex_init( &p_filter->p_sys->lock );
117 /*****************************************************************************
119 *****************************************************************************/
120 static void Destroy( vlc_object_t *p_this )
122 filter_t *p_filter = (filter_t *)p_this;
124 free( p_filter->p_sys->p_oldpix );
125 free( p_filter->p_sys->p_buf );
127 vlc_mutex_destroy( &p_filter->p_sys->lock );
129 free( p_filter->p_sys );
133 /*****************************************************************************
135 *****************************************************************************/
136 static picture_t *Filter( filter_t *p_filter, picture_t *p_inpic )
139 filter_sys_t *p_sys = p_filter->p_sys;
141 const uint8_t *p_inpix = p_inpic->p[Y_PLANE].p_pixels;
142 const int i_src_pitch = p_inpic->p[Y_PLANE].i_pitch;
143 const int i_src_visible = p_inpic->p[Y_PLANE].i_visible_pitch;
144 const int i_num_lines = p_inpic->p[Y_PLANE].i_visible_lines;
146 const uint8_t *p_inpix_u = p_inpic->p[U_PLANE].p_pixels;
147 const uint8_t *p_inpix_v = p_inpic->p[V_PLANE].p_pixels;
148 const int i_src_pitch_u = p_inpic->p[U_PLANE].i_pitch;
149 const int i_num_lines_u = p_inpic->p[U_PLANE].i_visible_lines;
161 if( !p_inpic ) return NULL;
163 if( !p_sys->p_oldpix || !p_sys->p_buf )
165 free( p_sys->p_oldpix );
166 free( p_sys->p_buf );
167 p_sys->p_oldpix = malloc( i_src_pitch * i_num_lines );
168 p_sys->p_oldpix_u = malloc( i_src_pitch_u * i_num_lines_u );
169 p_sys->p_oldpix_v = malloc( i_src_pitch_u * i_num_lines_u );
170 p_sys->p_buf = malloc( sizeof( uint32_t ) * i_src_pitch * i_num_lines );
171 p_sys->p_buf2 = malloc( sizeof( uint32_t ) * i_src_pitch * i_num_lines);
172 vlc_memcpy( p_sys->p_oldpix, p_inpix, i_src_pitch * i_num_lines );
173 vlc_memcpy( p_sys->p_oldpix_u, p_inpix_u, i_src_pitch_u * i_num_lines_u );
174 vlc_memcpy( p_sys->p_oldpix_v, p_inpix_v, i_src_pitch_u * i_num_lines_u );
177 p_oldpix = p_sys->p_oldpix;
178 p_oldpix_u = p_sys->p_oldpix_u;
179 p_oldpix_v = p_sys->p_oldpix_v;
180 p_buf = p_sys->p_buf;
181 p_buf2 = p_sys->p_buf2;
183 p_outpic = p_filter->pf_vout_buffer_new( p_filter );
186 msg_Warn( p_filter, "can't get output picture" );
187 picture_Release( p_inpic );
191 p_outpix = p_outpic->p[Y_PLANE].p_pixels;
192 vlc_memcpy( p_outpic->p[Y_PLANE].p_pixels, p_inpic->p[Y_PLANE].p_pixels,
193 p_inpic->p[Y_PLANE].i_pitch * p_inpic->p[Y_PLANE].i_visible_lines );
194 vlc_memcpy( p_outpic->p[U_PLANE].p_pixels, p_inpic->p[U_PLANE].p_pixels,
195 p_inpic->p[U_PLANE].i_pitch * p_inpic->p[U_PLANE].i_visible_lines );
196 vlc_memcpy( p_outpic->p[V_PLANE].p_pixels, p_inpic->p[V_PLANE].p_pixels,
197 p_inpic->p[V_PLANE].i_pitch * p_inpic->p[V_PLANE].i_visible_lines );
199 vlc_mutex_lock( &p_filter->p_sys->lock );
204 for( i = 0; i < i_src_pitch * i_num_lines; i++ )
206 if( p_inpix[i] > p_oldpix[i] )
208 p_buf2[i] = p_inpix[i] - p_oldpix[i];
212 p_buf2[i] = p_oldpix[i] - p_inpix[i];
218 switch( p_inpic->format.i_chroma )
220 case VLC_FOURCC('I','4','2','0'):
221 case VLC_FOURCC('I','Y','U','V'):
222 case VLC_FOURCC('J','4','2','0'):
223 case VLC_FOURCC('Y','V','1','2'):
227 case VLC_FOURCC('I','4','2','2'):
228 case VLC_FOURCC('J','4','2','2'):
234 msg_Warn( p_filter, "Not taking chroma into account" );
240 for( line = 0; line < i_num_lines_u; line++ )
242 for( col = 0; col < i_src_pitch_u; col ++ )
245 i = line * i_src_pitch_u + col;
246 if( p_inpix_u[i] > p_oldpix_u[i] )
248 diff = p_inpix_u[i] - p_oldpix_u[i];
252 diff = p_oldpix_u[i] - p_inpix_u[i];
254 if( p_inpix_v[i] > p_oldpix_v[i] )
256 diff += p_inpix_v[i] - p_oldpix_v[i];
260 diff += p_oldpix_v[i] - p_inpix_v[i];
265 p_buf2[2*line*i_src_pitch+2*col] += diff;
266 p_buf2[2*line*i_src_pitch+2*col+1] += diff;
267 p_buf2[(2*line+1)*i_src_pitch+2*col] += diff;
268 p_buf2[(2*line+1)*i_src_pitch+2*col+1] += diff;
272 p_buf2[line*i_src_pitch+2*col] += diff;
273 p_buf2[line*i_src_pitch+2*col+1] += diff;
281 * Get the areas where movement was detected
283 int colors[NUM_COLORS];
284 int color_x_min[NUM_COLORS];
285 int color_x_max[NUM_COLORS];
286 int color_y_min[NUM_COLORS];
287 int color_y_max[NUM_COLORS];
289 last = FindShapes( p_buf2, p_buf, i_src_pitch, i_src_visible, i_num_lines,
290 colors, color_x_min, color_x_max, color_y_min, color_y_max );
293 * Count final number of shapes
294 * Draw rectangles (there can be more than 1 moving shape in 1 rectangle)
297 for( i = 1; i < last; i++ )
299 if( colors[i] == i && color_x_min[i] != -1 )
301 if( ( color_y_max[i] - color_y_min[i] ) * ( color_x_max[i] - color_x_min[i] ) < 16 ) continue;
305 for( x = color_x_min[i]; x <= color_x_max[i]; x++ )
307 p_outpix[y*i_src_pitch+x] = 0xff;
310 for( x = color_x_min[i]; x <= color_x_max[i]; x++ )
312 p_outpix[y*i_src_pitch+x] = 0xff;
315 for( y = color_y_min[i]; y <= color_y_max[i]; y++ )
317 p_outpix[y*i_src_pitch+x] = 0xff;
320 for( y = color_y_min[i]; y <= color_y_max[i]; y++ )
322 p_outpix[y*i_src_pitch+x] = 0xff;
326 msg_Dbg( p_filter, "Counted %d moving shapes.", j);
329 * We're done. Lets keep a copy of the picture
331 vlc_memcpy( p_oldpix, p_inpix, i_src_pitch * i_num_lines );
332 vlc_memcpy( p_oldpix_u, p_inpix_u, i_src_pitch_u * i_num_lines_u );
333 vlc_memcpy( p_oldpix_v, p_inpix_v, i_src_pitch_u * i_num_lines_u );
335 vlc_mutex_unlock( &p_filter->p_sys->lock );
337 return CopyInfoAndRelease( p_outpic, p_inpic );
340 /*****************************************************************************
342 *****************************************************************************/
343 static picture_t *FilterPacked( filter_t *p_filter, picture_t *p_inpic )
346 filter_sys_t *p_sys = p_filter->p_sys;
348 int i_y_offset, i_u_offset, i_v_offset;
350 const uint8_t *p_inpix;
351 const uint8_t *p_inpix_u;
352 const uint8_t *p_inpix_v;
369 if( GetPackedYuvOffsets( p_inpic->format.i_chroma, &i_y_offset,
370 &i_u_offset, &i_v_offset ) != VLC_SUCCESS )
372 msg_Warn( p_filter, "Unsupported input chroma (%4s)",
373 (char*)&(p_inpic->format.i_chroma) );
374 picture_Release( p_inpic );
378 p_inpix = p_inpic->p->p_pixels+i_y_offset;
379 p_inpix_u = p_inpic->p->p_pixels+i_u_offset;
380 p_inpix_v = p_inpic->p->p_pixels+i_v_offset;
382 i_src_pitch = p_inpic->p->i_pitch;
383 i_src_visible = p_inpic->p->i_visible_pitch;
384 i_num_lines = p_inpic->p->i_visible_lines;
386 if( !p_sys->p_oldpix || !p_sys->p_buf )
388 free( p_sys->p_oldpix );
389 free( p_sys->p_buf );
390 p_sys->p_oldpix = malloc( i_src_pitch * i_num_lines );
391 p_sys->p_buf = malloc( sizeof( uint32_t ) * i_src_pitch * i_num_lines / 2 );
392 p_sys->p_buf2 = malloc( sizeof( uint32_t ) * i_src_pitch * i_num_lines / 2 );
393 vlc_memcpy( p_sys->p_oldpix, p_inpic->p->p_pixels,
394 i_src_pitch * i_num_lines );
398 p_outpic = p_filter->pf_vout_buffer_new( p_filter );
401 msg_Warn( p_filter, "can't get output picture" );
402 picture_Release( p_inpic );
406 p_outpix = p_outpic->p->p_pixels+i_y_offset;
407 vlc_memcpy( p_outpic->p->p_pixels, p_inpic->p->p_pixels,
408 p_inpic->p->i_pitch * p_inpic->p->i_visible_lines );
410 p_oldpix = p_sys->p_oldpix+i_y_offset;
411 p_oldpix_u = p_sys->p_oldpix+i_u_offset;
412 p_oldpix_v = p_sys->p_oldpix+i_v_offset;
413 p_buf = p_sys->p_buf;
414 p_buf2 = p_sys->p_buf2;
416 vlc_mutex_lock( &p_filter->p_sys->lock );
421 for( i = 0; i < i_src_pitch * i_num_lines; i+=2 )
423 if( p_inpix[i] > p_oldpix[i] )
425 p_buf2[i>>1] = p_inpix[i] - p_oldpix[i];
429 p_buf2[i>>1] = p_oldpix[i] - p_inpix[i];
434 for( i = 0; i < i_src_pitch * i_num_lines; i+=4 )
437 if( p_inpix_u[i] > p_oldpix_u[i] )
439 diff = p_inpix_u[i] - p_oldpix_u[i];
443 diff = p_oldpix_u[i] - p_inpix_u[i];
445 if( p_inpix_v[i] > p_oldpix_v[i] )
447 diff += p_inpix_v[i] - p_oldpix_v[i];
451 diff += p_oldpix_v[i] - p_inpix_v[i];
454 p_buf2[(i>>2)<<1] += diff;
455 p_buf2[((i>>2)<<1)+1] += diff;
460 * Get the areas where movement was detected
463 int color_x_min[5000];
464 int color_x_max[5000];
465 int color_y_min[5000];
466 int color_y_max[5000];
468 last = FindShapes( p_buf2, p_buf, i_src_pitch/2, i_src_visible/2,
469 i_num_lines, colors, color_x_min, color_x_max,
470 color_y_min, color_y_max );
473 * Count final number of shapes
474 * Draw rectangles (there can be more than 1 moving shape in 1 rectangle)
477 for( i = 1; i < last; i++ )
479 if( colors[i] == i && color_x_min[i] != -1 )
481 if( ( color_y_max[i] - color_y_min[i] ) * ( color_x_max[i] - color_x_min[i] ) < 16 ) continue;
483 printf("Shape: (%d,%d) (%d,%d)\n", color_y_max[i], color_y_min[i], color_x_max[i], color_x_min[i] );
486 for( x = color_x_min[i]*2; x <= color_x_max[i]*2; x+=2 )
488 p_outpix[y*i_src_pitch+x] = 0xff;
491 for( x = color_x_min[i]*2; x <= color_x_max[i]*2; x+=2 )
493 p_outpix[y*i_src_pitch+x] = 0xff;
495 x = color_x_min[i]*2;
496 for( y = color_y_min[i]; y <= color_y_max[i]; y++ )
498 p_outpix[y*i_src_pitch+x] = 0xff;
500 x = color_x_max[i]*2;
501 for( y = color_y_min[i]; y <= color_y_max[i]; y++ )
503 p_outpix[y*i_src_pitch+x] = 0xff;
507 msg_Dbg( p_filter, "Counted %d moving shapes.", j);
510 * We're done. Lets keep a copy of the picture
512 vlc_memcpy( p_sys->p_oldpix, p_inpic->p->p_pixels, i_src_pitch * i_num_lines );
514 vlc_mutex_unlock( &p_filter->p_sys->lock );
516 return CopyInfoAndRelease( p_outpic, p_inpic );
520 /*****************************************************************************
521 * Gaussian Convolution
522 *****************************************************************************
523 * Gaussian convolution ( sigma == 1.4 )
525 * | 2 4 5 4 2 | | 2 4 4 4 2 |
526 * | 4 9 12 9 4 | | 4 8 12 8 4 |
527 * | 5 12 15 12 5 | ~ | 4 12 16 12 4 |
528 * | 4 9 12 9 4 | | 4 8 12 8 4 |
529 * | 2 4 5 4 2 | | 2 4 4 4 2 |
530 *****************************************************************************/
531 static void GaussianConvolution( uint32_t *p_inpix, uint32_t *p_smooth,
532 int i_src_pitch, int i_num_lines,
536 for( y = 2; y < i_num_lines - 2; y++ )
538 for( x = 2; x < i_src_visible - 2; x++ )
540 p_smooth[y*i_src_visible+x] = (uint32_t)(
542 ( p_inpix[(y-2)*i_src_pitch+x-2] )
543 + ((p_inpix[(y-2)*i_src_pitch+x-1]
544 + p_inpix[(y-2)*i_src_pitch+x]
545 + p_inpix[(y-2)*i_src_pitch+x+1])<<1 )
546 + ( p_inpix[(y-2)*i_src_pitch+x+2] )
548 + ((p_inpix[(y-1)*i_src_pitch+x-2]
549 + ( p_inpix[(y-1)*i_src_pitch+x-1]<<1 )
550 + ( p_inpix[(y-1)*i_src_pitch+x]*3 )
551 + ( p_inpix[(y-1)*i_src_pitch+x+1]<<1 )
552 + p_inpix[(y-1)*i_src_pitch+x+2]
554 + p_inpix[y*i_src_pitch+x-2]
555 + ( p_inpix[y*i_src_pitch+x-1]*3 )
556 + ( p_inpix[y*i_src_pitch+x]<<2 )
557 + ( p_inpix[y*i_src_pitch+x+1]*3 )
558 + p_inpix[y*i_src_pitch+x+2]
560 + p_inpix[(y+1)*i_src_pitch+x-2]
561 + ( p_inpix[(y+1)*i_src_pitch+x-1]<<1 )
562 + ( p_inpix[(y+1)*i_src_pitch+x]*3 )
563 + ( p_inpix[(y+1)*i_src_pitch+x+1]<<1 )
564 + p_inpix[(y+1)*i_src_pitch+x+2] )<<1 )
566 + ( p_inpix[(y+2)*i_src_pitch+x-2] )
567 + ((p_inpix[(y+2)*i_src_pitch+x-1]
568 + p_inpix[(y+2)*i_src_pitch+x]
569 + p_inpix[(y+2)*i_src_pitch+x+1])<<1 )
570 + ( p_inpix[(y+2)*i_src_pitch+x+2] )
576 /*****************************************************************************
578 *****************************************************************************/
579 static int FindShapes( uint32_t *p_diff, uint32_t *p_smooth,
580 int i_pitch, int i_visible, int i_lines,
582 int *color_x_min, int *color_x_max,
583 int *color_y_min, int *color_y_max )
589 * Apply some smoothing to remove noise
591 GaussianConvolution( p_diff, p_smooth, i_pitch, i_visible, i_lines );
594 * Label the shapes and build the labels dependencies list
596 for( j = 0; j < i_pitch; j++ )
599 p_smooth[(i_lines-1)*i_pitch+j] = 0;
601 for( i = 1; i < i_lines-1; i++ )
603 p_smooth[i*i_pitch] = 0;
604 for( j = 1; j < i_pitch-1; j++ )
606 if( p_smooth[i*i_pitch+j] > 15 )
608 if( p_smooth[(i-1)*i_pitch+j-1] )
610 p_smooth[i*i_pitch+j] = p_smooth[(i-1)*i_pitch+j-1];
612 else if( p_smooth[(i-1)*i_pitch+j] )
613 p_smooth[i*i_pitch+j] = p_smooth[(i-1)*i_pitch+j];
614 else if( p_smooth[i*i_pitch+j-1] )
615 p_smooth[i*i_pitch+j] = p_smooth[i*i_pitch+j-1];
620 p_smooth[i*i_pitch+j] = last;
626 if( p_smooth[A] && p_smooth[A] != p_smooth[i*i_pitch+j] ) \
628 if( p_smooth[A] < p_smooth[i*i_pitch+j] ) \
629 colors[p_smooth[i*i_pitch+j]] = p_smooth[A]; \
631 colors[p_smooth[A]] = p_smooth[i*i_pitch+j]; \
633 CHECK( i*i_pitch+j-1 );
634 CHECK( (i-1)*i_pitch+j-1 );
635 CHECK( (i-1)*i_pitch+j );
636 CHECK( (i-1)*i_pitch+j+1 );
641 p_smooth[i*i_pitch+j] = 0;
644 p_smooth[i*i_pitch+j] = 0;
648 * Initialise empty rectangle list
650 for( i = 1; i < last; i++ )
659 * Compute rectangle coordinates
661 for( i = 0; i < i_pitch * i_lines; i++ )
665 while( colors[p_smooth[i]] != (int)p_smooth[i] )
666 p_smooth[i] = colors[p_smooth[i]];
667 if( color_x_min[p_smooth[i]] == -1 )
669 color_x_min[p_smooth[i]] =
670 color_x_max[p_smooth[i]] = i % i_pitch;
671 color_y_min[p_smooth[i]] =
672 color_y_max[p_smooth[i]] = i / i_pitch;
676 int x = i % i_pitch, y = i / i_pitch;
677 if( x < color_x_min[p_smooth[i]] )
678 color_x_min[p_smooth[i]] = x;
679 if( x > color_x_max[p_smooth[i]] )
680 color_x_max[p_smooth[i]] = x;
681 if( y < color_y_min[p_smooth[i]] )
682 color_y_min[p_smooth[i]] = y;
683 if( y > color_y_max[p_smooth[i]] )
684 color_y_max[p_smooth[i]] = y;
690 * Merge overlaping rectangles
692 for( i = 1; i < last; i++ )
694 if( colors[i] != i ) continue;
695 if( color_x_min[i] == -1 ) continue;
696 for( j = i+1; j < last; j++ )
698 if( colors[j] != j ) continue;
699 if( color_x_min[j] == -1 ) continue;
700 if( __MAX( color_x_min[i], color_x_min[j] ) < __MIN( color_x_max[i], color_x_max[j] ) &&
701 __MAX( color_y_min[i], color_y_min[j] ) < __MIN( color_y_max[i], color_y_max[j] ) )
703 color_x_min[i] = __MIN( color_x_min[i], color_x_min[j] );
704 color_x_max[i] = __MAX( color_x_max[i], color_x_max[j] );
705 color_y_min[i] = __MIN( color_y_min[i], color_y_min[j] );
706 color_y_max[i] = __MAX( color_y_max[i], color_y_max[j] );