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 *****************************************************************************/
27 #include <stdlib.h> /* malloc(), free() */
30 #include <math.h> /* sin(), cos() */
36 #include "vlc_filter.h"
38 /*****************************************************************************
40 *****************************************************************************/
41 static int Create ( vlc_object_t * );
42 static void Destroy ( vlc_object_t * );
44 static picture_t *Filter( filter_t *, picture_t * );
45 static void GaussianConvolution( uint32_t *, uint32_t *, int, int, int );
47 /*****************************************************************************
49 *****************************************************************************/
51 #define FILTER_PREFIX "motiondetect-"
54 set_description( _("Motion detect video filter") );
55 set_shortname( _( "Motion Detect" ));
56 set_capability( "video filter2", 0 );
57 set_category( CAT_VIDEO );
58 set_subcategory( SUBCAT_VIDEO_VFILTER );
60 add_shortcut( "motion" );
61 set_callbacks( Create, Destroy );
65 static const char *ppsz_filter_options[] = {
80 /*****************************************************************************
82 *****************************************************************************/
83 static int Create( vlc_object_t *p_this )
85 filter_t *p_filter = (filter_t *)p_this;
87 /* Allocate structure */
88 p_filter->p_sys = malloc( sizeof( filter_sys_t ) );
89 if( p_filter->p_sys == NULL )
91 msg_Err( p_filter, "out of memory" );
95 p_filter->pf_video_filter = Filter;
97 p_filter->p_sys->p_oldpix = NULL;
98 p_filter->p_sys->p_buf = NULL;
101 config_ChainParse( p_filter, FILTER_PREFIX, ppsz_filter_options,
104 vlc_mutex_init( p_filter, &p_filter->p_sys->lock );
109 /*****************************************************************************
111 *****************************************************************************/
112 static void Destroy( vlc_object_t *p_this )
114 filter_t *p_filter = (filter_t *)p_this;
116 free( p_filter->p_sys->p_oldpix );
117 free( p_filter->p_sys->p_buf );
119 vlc_mutex_destroy( &p_filter->p_sys->lock );
121 free( p_filter->p_sys );
124 /*****************************************************************************
126 *****************************************************************************/
127 static picture_t *Filter( filter_t *p_filter, picture_t *p_inpic )
130 filter_sys_t *p_sys = p_filter->p_sys;
132 const uint8_t *p_inpix = p_inpic->p[Y_PLANE].p_pixels;
133 const int i_src_pitch = p_inpic->p[Y_PLANE].i_pitch;
134 const int i_src_visible = p_inpic->p[Y_PLANE].i_visible_pitch;
135 const int i_num_lines = p_inpic->p[Y_PLANE].i_visible_lines;
137 const uint8_t *p_inpix_u = p_inpic->p[U_PLANE].p_pixels;
138 const uint8_t *p_inpix_v = p_inpic->p[V_PLANE].p_pixels;
139 const int i_src_pitch_u = p_inpic->p[U_PLANE].i_pitch;
140 const int i_num_lines_u = p_inpic->p[U_PLANE].i_visible_lines;
152 if( !p_inpic ) return NULL;
154 p_outpic = p_filter->pf_vout_buffer_new( p_filter );
157 msg_Warn( p_filter, "can't get output picture" );
158 if( p_inpic->pf_release )
159 p_inpic->pf_release( p_inpic );
163 p_outpix = p_outpic->p[Y_PLANE].p_pixels;
165 p_filter->p_libvlc->pf_memset( p_outpic->p[U_PLANE].p_pixels, 0x80,
166 p_inpic->p[U_PLANE].i_pitch * p_inpic->p[U_PLANE].i_visible_lines );
167 p_filter->p_libvlc->pf_memset( p_outpic->p[V_PLANE].p_pixels, 0x80,
168 p_inpic->p[V_PLANE].i_pitch * p_inpic->p[V_PLANE].i_visible_lines );
170 p_filter->p_libvlc->pf_memcpy( p_outpic->p[U_PLANE].p_pixels,
171 p_inpic->p[U_PLANE].p_pixels,
172 p_inpic->p[U_PLANE].i_pitch * p_inpic->p[U_PLANE].i_visible_lines );
173 p_filter->p_libvlc->pf_memcpy( p_outpic->p[V_PLANE].p_pixels,
174 p_inpic->p[V_PLANE].p_pixels,
175 p_inpic->p[V_PLANE].i_pitch * p_inpic->p[V_PLANE].i_visible_lines );
178 if( !p_sys->p_oldpix || !p_sys->p_buf )
180 free( p_sys->p_oldpix );
181 free( p_sys->p_buf );
182 p_sys->p_oldpix = malloc( i_src_pitch * i_num_lines );
183 p_sys->p_oldpix_u = malloc( i_src_pitch_u * i_num_lines_u );
184 p_sys->p_oldpix_v = malloc( i_src_pitch_u * i_num_lines_u );
185 p_sys->p_buf = malloc( sizeof( uint32_t ) * i_src_pitch * i_num_lines );
186 p_sys->p_buf2 = malloc( sizeof( uint32_t ) * i_src_pitch * i_num_lines);
189 p_oldpix = p_sys->p_oldpix;
190 p_oldpix_u = p_sys->p_oldpix_u;
191 p_oldpix_v = p_sys->p_oldpix_v;
192 p_buf = p_sys->p_buf;
193 p_buf2 = p_sys->p_buf2;
195 vlc_mutex_lock( &p_filter->p_sys->lock );
200 for( i = 0; i < i_src_pitch * i_num_lines; i++ )
202 if( p_inpix[i] > p_oldpix[i] )
204 p_buf2[i] = p_inpix[i] - p_oldpix[i];
208 p_buf2[i] = p_oldpix[i] - p_inpix[i];
214 switch( p_inpic->format.i_chroma )
216 case VLC_FOURCC('I','4','2','0'):
217 case VLC_FOURCC('I','Y','U','V'):
218 case VLC_FOURCC('J','4','2','0'):
219 case VLC_FOURCC('Y','V','1','2'):
223 case VLC_FOURCC('I','4','2','2'):
224 case VLC_FOURCC('J','4','2','2'):
230 msg_Warn( p_filter, "Not taking chroma into account" );
236 for( line = 0; line < i_num_lines_u; line++ )
238 for( col = 0; col < i_src_pitch_u; col ++ )
241 i = line * i_src_pitch_u + col;
242 if( p_inpix_u[i] > p_oldpix_u[i] )
244 diff = p_inpix_u[i] - p_oldpix_u[i];
248 diff = p_oldpix_u[i] - p_inpix_u[i];
253 p_buf2[2*line*i_src_pitch+2*col] += diff;
254 p_buf2[2*line*i_src_pitch+2*col+1] += diff;
255 p_buf2[(2*line+1)*i_src_pitch+2*col] += diff;
256 p_buf2[(2*line+1)*i_src_pitch+2*col+1] += diff;
260 p_buf2[line*i_src_pitch+2*col] += diff;
261 p_buf2[line*i_src_pitch+2*col+1] += diff;
269 * Apply some smoothing to remove noise
272 GaussianConvolution( p_buf2, p_buf, i_src_pitch, i_num_lines, i_src_visible );
275 uint32_t *pouet = p_buf2;
280 * Copy luminance plane
282 for( i = 0; i < i_src_pitch * i_num_lines; i++ )
284 /*if( p_buf[i] > 25 )
288 else// if( p_buf[i] > 15 )*/
290 p_outpix[i] = p_inpix[i];
299 * Label the shapes ans build the labels dependencies list
303 int color_x_min[5000];
304 int color_x_max[5000];
305 int color_y_min[5000];
306 int color_y_max[5000];
308 for( j = 0; j < i_src_pitch; j++ )
311 p_buf[(i_num_lines-1)*i_src_pitch+j] = 0;
313 for( i = 1; i < i_num_lines-1; i++ )
315 p_buf[i*i_src_pitch] = 0;
316 for( j = 1; j < i_src_pitch-1; j++ )
318 if( p_buf[i*i_src_pitch+j] > 15 )
320 if( p_buf[(i-1)*i_src_pitch+j-1] )
322 p_buf[i*i_src_pitch+j] = p_buf[(i-1)*i_src_pitch+j-1];
324 else if( p_buf[(i-1)*i_src_pitch+j] )
325 p_buf[i*i_src_pitch+j] = p_buf[(i-1)*i_src_pitch+j];
326 else if( p_buf[i*i_src_pitch+j-1] )
327 p_buf[i*i_src_pitch+j] = p_buf[i*i_src_pitch+j-1];
330 p_buf[i*i_src_pitch+j] = last;
335 if( p_buf[A] && p_buf[A] != p_buf[i*i_src_pitch+j] ) \
337 if( p_buf[A] < p_buf[i*i_src_pitch+j] ) \
338 colors[p_buf[i*i_src_pitch+j]] = p_buf[A]; \
340 colors[p_buf[A]] = p_buf[i*i_src_pitch+j]; \
342 CHECK( i*i_src_pitch+j-1 );
343 CHECK( (i-1)*i_src_pitch+j-1 );
344 CHECK( (i-1)*i_src_pitch+j );
345 CHECK( (i-1)*i_src_pitch+j+1 );
349 p_buf[i*i_src_pitch+j] = 0;
352 p_buf[i*i_src_pitch+j] = 0;
356 * Initialise empty rectangle list
358 for( i = 1; i < last; i++ )
367 * Compute rectangle coordinates
369 for( i = 0; i < i_src_pitch * i_num_lines; i++ )
373 while( colors[p_buf[i]] != p_buf[i] )
374 p_buf[i] = colors[p_buf[i]];
375 //p_outpix[i] = /*(p_buf[i]%2) */ 0xff;
376 if( color_x_min[p_buf[i]] == -1 )
378 color_x_min[p_buf[i]] =
379 color_x_max[p_buf[i]] = i % i_src_pitch;
380 color_y_min[p_buf[i]] =
381 color_y_max[p_buf[i]] = i / i_src_pitch;
385 int x = i % i_src_pitch, y = i / i_src_pitch;
386 if( x < color_x_min[p_buf[i]] )
387 color_x_min[p_buf[i]] = x;
388 if( x > color_x_max[p_buf[i]] )
389 color_x_max[p_buf[i]] = x;
390 if( y < color_y_min[p_buf[i]] )
391 color_y_min[p_buf[i]] = y;
392 if( y > color_y_max[p_buf[i]] )
393 color_y_max[p_buf[i]] = y;
399 * Merge overlaping rectangles
401 for( i = 1; i < last; i++ )
403 if( colors[i] != i ) continue;
404 if( color_x_min[i] == -1 ) continue;
405 for( j = i+1; j < last; j++ )
407 if( colors[j] != j ) continue;
408 if( color_x_min[j] == -1 ) continue;
409 #define max( a, b ) ( a > b ? a : b )
410 #define min( a, b ) ( a < b ? a : b )
411 if( max( color_x_min[i], color_x_min[j] ) < min( color_x_max[i], color_x_max[j] ) && max( color_y_min[i], color_y_min[j] ) < min( color_y_max[i], color_y_max[j] ) )
413 color_x_min[i] = min( color_x_min[i], color_x_min[j] );
414 color_x_max[i] = max( color_x_max[i], color_x_max[j] );
415 color_y_min[i] = min( color_y_min[i], color_y_min[j] );
416 color_y_max[i] = max( color_y_max[i], color_y_max[j] );
424 * Count final number of shapes
425 * Draw rectangles (there can be more than 1 moving shape in 1 rectangle)
428 for( i = 1; i < last; i++ )
430 if( colors[i] == i && color_x_min[i] != -1 )
432 if( ( color_y_max[i] - color_y_min[i] ) * ( color_x_max[i] - color_x_min[i] ) < 16 ) continue;
437 for( x = color_x_min[i]; x <= color_x_max[i]; x++ )
439 p_outpix[y*i_src_pitch+x] = 0xff;
442 for( x = color_x_min[i]; x <= color_x_max[i]; x++ )
444 p_outpix[y*i_src_pitch+x] = 0xff;
447 for( y = color_y_min[i]; y <= color_y_max[i]; y++ )
449 p_outpix[y*i_src_pitch+x] = 0xff;
452 for( y = color_y_min[i]; y <= color_y_max[i]; y++ )
454 p_outpix[y*i_src_pitch+x] = 0xff;
457 for( x = color_x_min[i]; x <= color_x_max[i]; x++ )
459 for( y = color_y_min[i]; y <= color_y_max[i]; y++ )
461 p_outpix[y*i_src_pitch+x] = 0x0;
462 //p_inpix[y*i_src_pitch+x];
468 printf("counted %d moving shapes\n", j);
471 * We're done. Lets keep a copy of the Y plane
473 p_filter->p_libvlc->pf_memcpy( p_oldpix, p_inpix,
474 i_src_pitch * i_num_lines );
475 p_filter->p_libvlc->pf_memcpy( p_oldpix_u, p_inpix_u,
476 i_src_pitch_u * i_num_lines_u );
477 p_filter->p_libvlc->pf_memcpy( p_oldpix_v, p_inpix_v,
478 i_src_pitch_u * i_num_lines_u );
480 vlc_mutex_unlock( &p_filter->p_sys->lock );
483 p_outpic->date = p_inpic->date;
484 p_outpic->b_force = p_inpic->b_force;
485 p_outpic->i_nb_fields = p_inpic->i_nb_fields;
486 p_outpic->b_progressive = p_inpic->b_progressive;
487 p_outpic->b_top_field_first = p_inpic->b_top_field_first;
489 if( p_inpic->pf_release )
490 p_inpic->pf_release( p_inpic );
496 /*****************************************************************************
497 * Gaussian Convolution
498 *****************************************************************************
499 * Gaussian convolution ( sigma == 1.4 )
501 * | 2 4 5 4 2 | | 2 4 4 4 2 |
502 * | 4 9 12 9 4 | | 4 8 12 8 4 |
503 * | 5 12 15 12 5 | ~ | 4 12 16 12 4 |
504 * | 4 9 12 9 4 | | 4 8 12 8 4 |
505 * | 2 4 5 4 2 | | 2 4 4 4 2 |
506 *****************************************************************************/
507 static void GaussianConvolution( uint32_t *p_inpix, uint32_t *p_smooth,
508 int i_src_pitch, int i_num_lines,
511 /* const uint8_t *p_inpix = p_inpic->p[Y_PLANE].p_pixels;
512 const int i_src_pitch = p_inpic->p[Y_PLANE].i_pitch;
513 const int i_src_visible = p_inpic->p[Y_PLANE].i_visible_pitch;
514 const int i_num_lines = p_inpic->p[Y_PLANE].i_visible_lines;*/
517 for( y = 2; y < i_num_lines - 2; y++ )
519 for( x = 2; x < i_src_visible - 2; x++ )
521 p_smooth[y*i_src_visible+x] = (uint32_t)(
523 ( p_inpix[(y-2)*i_src_pitch+x-2] )
524 + ((p_inpix[(y-2)*i_src_pitch+x-1]
525 + p_inpix[(y-2)*i_src_pitch+x]
526 + p_inpix[(y-2)*i_src_pitch+x+1])<<1 )
527 + ( p_inpix[(y-2)*i_src_pitch+x+2] )
529 + ((p_inpix[(y-1)*i_src_pitch+x-2]
530 + ( p_inpix[(y-1)*i_src_pitch+x-1]<<1 )
531 + ( p_inpix[(y-1)*i_src_pitch+x]*3 )
532 + ( p_inpix[(y-1)*i_src_pitch+x+1]<<1 )
533 + p_inpix[(y-1)*i_src_pitch+x+2]
535 + p_inpix[y*i_src_pitch+x-2]
536 + ( p_inpix[y*i_src_pitch+x-1]*3 )
537 + ( p_inpix[y*i_src_pitch+x]<<2 )
538 + ( p_inpix[y*i_src_pitch+x+1]*3 )
539 + p_inpix[y*i_src_pitch+x+2]
541 + p_inpix[(y+1)*i_src_pitch+x-2]
542 + ( p_inpix[(y+1)*i_src_pitch+x-1]<<1 )
543 + ( p_inpix[(y+1)*i_src_pitch+x]*3 )
544 + ( p_inpix[(y+1)*i_src_pitch+x+1]<<1 )
545 + p_inpix[(y+1)*i_src_pitch+x+2] )<<1 )
547 + ( p_inpix[(y+2)*i_src_pitch+x-2] )
548 + ((p_inpix[(y+2)*i_src_pitch+x-1]
549 + p_inpix[(y+2)*i_src_pitch+x]
550 + p_inpix[(y+2)*i_src_pitch+x+1])<<1 )
551 + ( p_inpix[(y+2)*i_src_pitch+x+2] )