1 /*****************************************************************************
2 * motiondetect.c : Second version of a motion detection plugin.
3 *****************************************************************************
4 * Copyright (C) 2000-2008 VLC authors and VideoLAN
6 * Authors: Antoine Cellerier <dionoea -at- videolan -dot- org>
8 * This program is free software; you can redistribute it and/or modify it
9 * under the terms of the GNU Lesser General Public License as published by
10 * the Free Software Foundation; either version 2.1 of the License, or
11 * (at your option) any later version.
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU Lesser General Public License for more details.
18 * You should have received a copy of the GNU Lesser General Public License
19 * along with this program; if not, write to the Free Software Foundation,
20 * Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
21 *****************************************************************************/
23 /*****************************************************************************
25 *****************************************************************************/
31 #include <vlc_common.h>
32 #include <vlc_plugin.h>
34 #include <vlc_filter.h>
35 #include <vlc_picture.h>
36 #include "filter_picture.h"
38 /*****************************************************************************
40 *****************************************************************************/
41 static int Create ( vlc_object_t
* );
42 static void Destroy ( vlc_object_t
* );
44 #define FILTER_PREFIX "motiondetect-"
47 set_description( N_("Motion detect video filter") )
48 set_shortname( N_( "Motion Detect" ))
49 set_category( CAT_VIDEO
)
50 set_subcategory( SUBCAT_VIDEO_VFILTER
)
51 set_capability( "video filter", 0 )
53 add_shortcut( "motion" )
54 set_callbacks( Create
, Destroy
)
58 /*****************************************************************************
60 *****************************************************************************/
61 static picture_t
*Filter( filter_t
*, picture_t
* );
62 static void GaussianConvolution( uint32_t *, uint32_t *, int, int, int );
63 static int FindShapes( uint32_t *, uint32_t *, int, int, int,
64 int *, int *, int *, int *, int *);
65 static void Draw( filter_t
*p_filter
, uint8_t *p_pix
, int i_pix_pitch
, int i_pix_size
);
66 #define NUM_COLORS (5000)
77 int colors
[NUM_COLORS
];
78 int color_x_min
[NUM_COLORS
];
79 int color_x_max
[NUM_COLORS
];
80 int color_y_min
[NUM_COLORS
];
81 int color_y_max
[NUM_COLORS
];
84 /*****************************************************************************
86 *****************************************************************************/
87 static int Create( vlc_object_t
*p_this
)
89 filter_t
*p_filter
= (filter_t
*)p_this
;
90 const video_format_t
*p_fmt
= &p_filter
->fmt_in
.video
;
94 switch( p_fmt
->i_chroma
)
101 is_yuv_planar
= false;
105 msg_Err( p_filter
, "Unsupported input chroma (%4.4s)",
106 (char*)&(p_fmt
->i_chroma
) );
109 p_filter
->pf_video_filter
= Filter
;
111 /* Allocate structure */
112 p_filter
->p_sys
= p_sys
= malloc( sizeof( filter_sys_t
) );
113 if( p_filter
->p_sys
== NULL
)
116 p_sys
->is_yuv_planar
= is_yuv_planar
;
118 p_sys
->p_buf
= calloc( p_fmt
->i_width
* p_fmt
->i_height
, sizeof(*p_sys
->p_buf
) );
119 p_sys
->p_buf2
= calloc( p_fmt
->i_width
* p_fmt
->i_height
, sizeof(*p_sys
->p_buf
) );
121 if( !p_sys
->p_buf
|| !p_sys
->p_buf2
)
123 free( p_sys
->p_buf2
);
124 free( p_sys
->p_buf
);
131 /*****************************************************************************
133 *****************************************************************************/
134 static void Destroy( vlc_object_t
*p_this
)
136 filter_t
*p_filter
= (filter_t
*)p_this
;
137 filter_sys_t
*p_sys
= p_filter
->p_sys
;
139 free( p_sys
->p_buf2
);
140 free( p_sys
->p_buf
);
142 picture_Release( p_sys
->p_old
);
147 /*****************************************************************************
148 * Filter YUV Planar/Packed
149 *****************************************************************************/
150 static void PreparePlanar( filter_t
*p_filter
, picture_t
*p_inpic
)
152 filter_sys_t
*p_sys
= p_filter
->p_sys
;
153 const video_format_t
*p_fmt
= &p_filter
->fmt_in
.video
;
155 uint8_t *p_oldpix
= p_sys
->p_old
->p
[Y_PLANE
].p_pixels
;
156 const int i_old_pitch
= p_sys
->p_old
->p
[Y_PLANE
].i_pitch
;
158 const uint8_t *p_inpix
= p_inpic
->p
[Y_PLANE
].p_pixels
;
159 const int i_src_pitch
= p_inpic
->p
[Y_PLANE
].i_pitch
;
164 for( unsigned y
= 0; y
< p_fmt
->i_height
; y
++ )
166 for( unsigned x
= 0; x
< p_fmt
->i_width
; x
++ )
167 p_sys
->p_buf2
[y
*p_fmt
->i_width
+x
] = abs( p_inpix
[y
*i_src_pitch
+x
] - p_oldpix
[y
*i_old_pitch
+x
] );
172 switch( p_inpic
->format
.i_chroma
)
188 msg_Warn( p_filter
, "Not taking chroma into account" );
192 const uint8_t *p_inpix_u
= p_inpic
->p
[U_PLANE
].p_pixels
;
193 const uint8_t *p_inpix_v
= p_inpic
->p
[V_PLANE
].p_pixels
;
194 const int i_src_pitch_u
= p_inpic
->p
[U_PLANE
].i_pitch
;
195 const int i_src_pitch_v
= p_inpic
->p
[V_PLANE
].i_pitch
;
197 const uint8_t *p_oldpix_u
= p_sys
->p_old
->p
[U_PLANE
].p_pixels
;
198 const uint8_t *p_oldpix_v
= p_sys
->p_old
->p
[V_PLANE
].p_pixels
;
199 const int i_old_pitch_u
= p_sys
->p_old
->p
[U_PLANE
].i_pitch
;
200 const int i_old_pitch_v
= p_sys
->p_old
->p
[V_PLANE
].i_pitch
;
202 for( unsigned y
= 0; y
< p_fmt
->i_height
/i_chroma_dy
; y
++ )
204 for( unsigned x
= 0; x
< p_fmt
->i_width
/i_chroma_dx
; x
++ )
206 const int d
= abs( p_inpix_u
[y
*i_src_pitch_u
+x
] - p_oldpix_u
[y
*i_old_pitch_u
+x
] ) +
207 abs( p_inpix_v
[y
*i_src_pitch_v
+x
] - p_oldpix_v
[y
*i_old_pitch_v
+x
] );
209 for( int j
= 0; j
< i_chroma_dy
; j
++ )
211 for( int i
= 0; i
< i_chroma_dx
; i
++ )
212 p_sys
->p_buf2
[i_chroma_dy
*p_fmt
->i_width
*j
+ i_chroma_dx
*i
] = d
;
218 static int PreparePacked( filter_t
*p_filter
, picture_t
*p_inpic
, int *pi_pix_offset
)
220 filter_sys_t
*p_sys
= p_filter
->p_sys
;
221 const video_format_t
*p_fmt
= &p_filter
->fmt_in
.video
;
223 int i_y_offset
, i_u_offset
, i_v_offset
;
224 if( GetPackedYuvOffsets( p_fmt
->i_chroma
,
225 &i_y_offset
, &i_u_offset
, &i_v_offset
) )
227 msg_Warn( p_filter
, "Unsupported input chroma (%4.4s)",
228 (char*)&p_fmt
->i_chroma
);
231 *pi_pix_offset
= i_y_offset
;
233 /* Substract all planes at once */
234 uint8_t *p_oldpix
= p_sys
->p_old
->p
[Y_PLANE
].p_pixels
;
235 const int i_old_pitch
= p_sys
->p_old
->p
[Y_PLANE
].i_pitch
;
237 const uint8_t *p_inpix
= p_inpic
->p
[Y_PLANE
].p_pixels
;
238 const int i_src_pitch
= p_inpic
->p
[Y_PLANE
].i_pitch
;
240 for( unsigned y
= 0; y
< p_fmt
->i_height
; y
++ )
242 for( unsigned x
= 0; x
< p_fmt
->i_width
; x
+=2 )
245 d
= abs( p_inpix
[y
*i_src_pitch
+2*x
+i_u_offset
] - p_oldpix
[y
*i_old_pitch
+2*x
+i_u_offset
] ) +
246 abs( p_inpix
[y
*i_src_pitch
+2*x
+i_v_offset
] - p_oldpix
[y
*i_old_pitch
+2*x
+i_v_offset
] );
248 for( int i
= 0; i
< 2; i
++ )
249 p_sys
->p_buf2
[y
*p_fmt
->i_width
+x
+i
] =
250 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
;
256 static picture_t
*Filter( filter_t
*p_filter
, picture_t
*p_inpic
)
258 filter_sys_t
*p_sys
= p_filter
->p_sys
;
263 picture_t
*p_outpic
= filter_NewPicture( p_filter
);
266 picture_Release( p_inpic
);
269 picture_Copy( p_outpic
, p_inpic
);
273 p_sys
->p_old
= picture_Hold( p_inpic
);
279 if( p_sys
->is_yuv_planar
)
281 PreparePlanar( p_filter
, p_inpic
);
287 if( PreparePacked( p_filter
, p_inpic
, &i_pix_offset
) )
293 * Get the areas where movement was detected
295 const video_format_t
*p_fmt
= &p_filter
->fmt_in
.video
;
296 p_sys
->i_colors
= FindShapes( p_sys
->p_buf2
, p_sys
->p_buf
, p_fmt
->i_width
, p_fmt
->i_width
, p_fmt
->i_height
,
297 p_sys
->colors
, p_sys
->color_x_min
, p_sys
->color_x_max
, p_sys
->color_y_min
, p_sys
->color_y_max
);
300 * Count final number of shapes
301 * Draw rectangles (there can be more than 1 moving shape in 1 rectangle)
303 Draw( p_filter
, &p_outpic
->p
[Y_PLANE
].p_pixels
[i_pix_offset
], p_outpic
->p
[Y_PLANE
].i_pitch
, i_pix_size
);
305 /* We're done. Lets keep a copy of the picture */
306 picture_Release( p_sys
->p_old
);
307 p_sys
->p_old
= picture_Hold( p_inpic
);
310 picture_Release( p_inpic
);
315 /*****************************************************************************
316 * Gaussian Convolution
317 *****************************************************************************
318 * Gaussian convolution ( sigma == 1.4 )
320 * | 2 4 5 4 2 | | 2 4 4 4 2 |
321 * | 4 9 12 9 4 | | 4 8 12 8 4 |
322 * | 5 12 15 12 5 | ~ | 4 12 16 12 4 |
323 * | 4 9 12 9 4 | | 4 8 12 8 4 |
324 * | 2 4 5 4 2 | | 2 4 4 4 2 |
325 *****************************************************************************/
326 static void GaussianConvolution( uint32_t *p_inpix
, uint32_t *p_smooth
,
327 int i_src_pitch
, int i_num_lines
,
330 /* A bit overkill but ... simpler */
331 memset( p_smooth
, 0, sizeof(*p_smooth
) * i_src_pitch
* i_num_lines
);
333 for( int y
= 2; y
< i_num_lines
- 2; y
++ )
335 for( int x
= 2; x
< i_src_visible
- 2; x
++ )
337 p_smooth
[y
*i_src_visible
+x
] = (uint32_t)(
339 ( p_inpix
[(y
-2)*i_src_pitch
+x
-2] )
340 + ((p_inpix
[(y
-2)*i_src_pitch
+x
-1]
341 + p_inpix
[(y
-2)*i_src_pitch
+x
]
342 + p_inpix
[(y
-2)*i_src_pitch
+x
+1])<<1 )
343 + ( p_inpix
[(y
-2)*i_src_pitch
+x
+2] )
345 + ((p_inpix
[(y
-1)*i_src_pitch
+x
-2]
346 + ( p_inpix
[(y
-1)*i_src_pitch
+x
-1]<<1 )
347 + ( p_inpix
[(y
-1)*i_src_pitch
+x
]*3 )
348 + ( p_inpix
[(y
-1)*i_src_pitch
+x
+1]<<1 )
349 + p_inpix
[(y
-1)*i_src_pitch
+x
+2]
351 + p_inpix
[y
*i_src_pitch
+x
-2]
352 + ( p_inpix
[y
*i_src_pitch
+x
-1]*3 )
353 + ( p_inpix
[y
*i_src_pitch
+x
]<<2 )
354 + ( p_inpix
[y
*i_src_pitch
+x
+1]*3 )
355 + p_inpix
[y
*i_src_pitch
+x
+2]
357 + p_inpix
[(y
+1)*i_src_pitch
+x
-2]
358 + ( p_inpix
[(y
+1)*i_src_pitch
+x
-1]<<1 )
359 + ( p_inpix
[(y
+1)*i_src_pitch
+x
]*3 )
360 + ( p_inpix
[(y
+1)*i_src_pitch
+x
+1]<<1 )
361 + p_inpix
[(y
+1)*i_src_pitch
+x
+2] )<<1 )
363 + ( p_inpix
[(y
+2)*i_src_pitch
+x
-2] )
364 + ((p_inpix
[(y
+2)*i_src_pitch
+x
-1]
365 + p_inpix
[(y
+2)*i_src_pitch
+x
]
366 + p_inpix
[(y
+2)*i_src_pitch
+x
+1])<<1 )
367 + ( p_inpix
[(y
+2)*i_src_pitch
+x
+2] )
373 /*****************************************************************************
375 *****************************************************************************/
376 static int FindShapes( uint32_t *p_diff
, uint32_t *p_smooth
,
377 int i_pitch
, int i_visible
, int i_lines
,
379 int *color_x_min
, int *color_x_max
,
380 int *color_y_min
, int *color_y_max
)
385 * Apply some smoothing to remove noise
387 GaussianConvolution( p_diff
, p_smooth
, i_pitch
, i_lines
, i_visible
);
390 * Label the shapes and build the labels dependencies list
392 for( int j
= 0; j
< i_pitch
; j
++ )
395 p_smooth
[(i_lines
-1)*i_pitch
+j
] = 0;
397 for( int i
= 1; i
< i_lines
-1; i
++ )
400 p_smooth
[i
*i_pitch
] = 0;
401 for( j
= 1; j
< i_pitch
-1; j
++ )
403 if( p_smooth
[i
*i_pitch
+j
] > 15 )
405 if( p_smooth
[(i
-1)*i_pitch
+j
-1] )
407 p_smooth
[i
*i_pitch
+j
] = p_smooth
[(i
-1)*i_pitch
+j
-1];
409 else if( p_smooth
[(i
-1)*i_pitch
+j
] )
410 p_smooth
[i
*i_pitch
+j
] = p_smooth
[(i
-1)*i_pitch
+j
];
411 else if( p_smooth
[i
*i_pitch
+j
-1] )
412 p_smooth
[i
*i_pitch
+j
] = p_smooth
[i
*i_pitch
+j
-1];
415 if( last
< NUM_COLORS
)
417 p_smooth
[i
*i_pitch
+j
] = last
;
423 if( p_smooth[A] && p_smooth[A] != p_smooth[i*i_pitch+j] ) \
425 if( p_smooth[A] < p_smooth[i*i_pitch+j] ) \
426 colors[p_smooth[i*i_pitch+j]] = p_smooth[A]; \
428 colors[p_smooth[A]] = p_smooth[i*i_pitch+j]; \
430 CHECK( i
*i_pitch
+j
-1 );
431 CHECK( (i
-1)*i_pitch
+j
-1 );
432 CHECK( (i
-1)*i_pitch
+j
);
433 CHECK( (i
-1)*i_pitch
+j
+1 );
438 p_smooth
[i
*i_pitch
+j
] = 0;
441 p_smooth
[i
*i_pitch
+j
] = 0;
445 * Initialise empty rectangle list
447 for( int i
= 1; i
< last
; i
++ )
456 * Compute rectangle coordinates
458 for( int i
= 0; i
< i_pitch
* i_lines
; i
++ )
462 while( colors
[p_smooth
[i
]] != (int)p_smooth
[i
] )
463 p_smooth
[i
] = colors
[p_smooth
[i
]];
464 if( color_x_min
[p_smooth
[i
]] == -1 )
466 color_x_min
[p_smooth
[i
]] =
467 color_x_max
[p_smooth
[i
]] = i
% i_pitch
;
468 color_y_min
[p_smooth
[i
]] =
469 color_y_max
[p_smooth
[i
]] = i
/ i_pitch
;
473 int x
= i
% i_pitch
, y
= i
/ i_pitch
;
474 if( x
< color_x_min
[p_smooth
[i
]] )
475 color_x_min
[p_smooth
[i
]] = x
;
476 if( x
> color_x_max
[p_smooth
[i
]] )
477 color_x_max
[p_smooth
[i
]] = x
;
478 if( y
< color_y_min
[p_smooth
[i
]] )
479 color_y_min
[p_smooth
[i
]] = y
;
480 if( y
> color_y_max
[p_smooth
[i
]] )
481 color_y_max
[p_smooth
[i
]] = y
;
487 * Merge overlaping rectangles
489 for( int i
= 1; i
< last
; i
++ )
491 if( colors
[i
] != i
) continue;
492 if( color_x_min
[i
] == -1 ) continue;
493 for( int j
= i
+1; j
< last
; j
++ )
495 if( colors
[j
] != j
) continue;
496 if( color_x_min
[j
] == -1 ) continue;
497 if( __MAX( color_x_min
[i
], color_x_min
[j
] ) < __MIN( color_x_max
[i
], color_x_max
[j
] ) &&
498 __MAX( color_y_min
[i
], color_y_min
[j
] ) < __MIN( color_y_max
[i
], color_y_max
[j
] ) )
500 color_x_min
[i
] = __MIN( color_x_min
[i
], color_x_min
[j
] );
501 color_x_max
[i
] = __MAX( color_x_max
[i
], color_x_max
[j
] );
502 color_y_min
[i
] = __MIN( color_y_min
[i
], color_y_min
[j
] );
503 color_y_max
[i
] = __MAX( color_y_max
[i
], color_y_max
[j
] );
513 static void Draw( filter_t
*p_filter
, uint8_t *p_pix
, int i_pix_pitch
, int i_pix_size
)
515 filter_sys_t
*p_sys
= p_filter
->p_sys
;
519 for( int i
= 1; i
< p_sys
->i_colors
; i
++ )
523 if( p_sys
->colors
[i
] != i
)
526 const int color_x_min
= p_sys
->color_x_min
[i
];
527 const int color_x_max
= p_sys
->color_x_max
[i
];
528 const int color_y_min
= p_sys
->color_y_min
[i
];
529 const int color_y_max
= p_sys
->color_y_max
[i
];
531 if( color_x_min
== -1 )
533 if( ( color_y_max
- color_y_min
) * ( color_x_max
- color_x_min
) < 16 )
539 for( x
= color_x_min
; x
<= color_x_max
; x
++ )
540 p_pix
[y
*i_pix_pitch
+x
*i_pix_size
] = 0xff;
543 for( x
= color_x_min
; x
<= color_x_max
; x
++ )
544 p_pix
[y
*i_pix_pitch
+x
*i_pix_size
] = 0xff;
547 for( y
= color_y_min
; y
<= color_y_max
; y
++ )
548 p_pix
[y
*i_pix_pitch
+x
*i_pix_size
] = 0xff;
551 for( y
= color_y_min
; y
<= color_y_max
; y
++ )
552 p_pix
[y
*i_pix_pitch
+x
*i_pix_size
] = 0xff;
554 msg_Dbg( p_filter
, "Counted %d moving shapes.", j
);