]> git.sesse.net Git - vlc/blob - modules/video_filter/motiondetect.c
79946d989a678581cd66c165711ecf47bbd9b8fb
[vlc] / modules / video_filter / motiondetect.c
1 /*****************************************************************************
2  * motiondetec.c : Second version of a motion detection plugin.
3  *****************************************************************************
4  * Copyright (C) 2000-2008 the VideoLAN team
5  * $Id$
6  *
7  * Authors: Antoine Cellerier <dionoea -at- videolan -dot- org>
8  *
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.
13  *
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.
18  *
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  *****************************************************************************/
23
24 /*****************************************************************************
25  * Preamble
26  *****************************************************************************/
27
28 #ifdef HAVE_CONFIG_H
29 # include "config.h"
30 #endif
31
32 #include <vlc_common.h>
33 #include <vlc_plugin.h>
34 #include <vlc_sout.h>
35
36 #include <vlc_filter.h>
37 #include "filter_picture.h"
38
39 /*****************************************************************************
40  * Module descriptor
41  *****************************************************************************/
42 static int  Create    ( vlc_object_t * );
43 static void Destroy   ( vlc_object_t * );
44
45 #define FILTER_PREFIX "motiondetect-"
46
47 vlc_module_begin ()
48     set_description( N_("Motion detect video filter") )
49     set_shortname( N_( "Motion Detect" ))
50     set_category( CAT_VIDEO )
51     set_subcategory( SUBCAT_VIDEO_VFILTER )
52     set_capability( "video filter2", 0 )
53
54     add_shortcut( "motion" )
55     set_callbacks( Create, Destroy )
56 vlc_module_end ()
57
58
59 /*****************************************************************************
60  * Local prototypes
61  *****************************************************************************/
62 static picture_t *Filter( filter_t *, picture_t * );
63 static picture_t *FilterPacked( filter_t *, picture_t * );
64 static void GaussianConvolution( uint32_t *, uint32_t *, int, int, int );
65 static int FindShapes( uint32_t *, uint32_t *, int, int, int,
66                        int *, int *, int *, int *, int *);
67 static void Draw( filter_t *p_filter, uint8_t *p_pix, int i_pix_pitch, int i_pix_size );
68 #define NUM_COLORS (5000)
69
70 struct filter_sys_t
71 {
72     bool b_old;
73     picture_t *p_old;
74     uint32_t *p_buf;
75     uint32_t *p_buf2;
76
77     /* */
78     int i_colors;
79     int colors[NUM_COLORS];
80     int color_x_min[NUM_COLORS];
81     int color_x_max[NUM_COLORS];
82     int color_y_min[NUM_COLORS];
83     int color_y_max[NUM_COLORS];
84 };
85
86 /*****************************************************************************
87  * Create
88  *****************************************************************************/
89 static int Create( vlc_object_t *p_this )
90 {
91     filter_t *p_filter = (filter_t *)p_this;
92     const video_format_t *p_fmt = &p_filter->fmt_in.video;
93     filter_sys_t *p_sys;
94
95     switch( p_fmt->i_chroma )
96     {
97         CASE_PLANAR_YUV
98             p_filter->pf_video_filter = Filter;
99             break;
100
101         CASE_PACKED_YUV_422
102             p_filter->pf_video_filter = FilterPacked;
103             break;
104
105         default:
106             msg_Err( p_filter, "Unsupported input chroma (%4.4s)",
107                      (char*)&(p_fmt->i_chroma) );
108             return VLC_EGENERIC;
109     }
110
111     /* Allocate structure */
112     p_filter->p_sys = p_sys = malloc( sizeof( filter_sys_t ) );
113     if( p_filter->p_sys == NULL )
114         return VLC_ENOMEM;
115
116     p_sys->b_old = false;
117     p_sys->p_old = picture_NewFromFormat( p_fmt );
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) );
120
121     if( !p_sys->p_old || !p_sys->p_buf || !p_sys->p_buf2 )
122     {
123         free( p_sys->p_buf2 );
124         free( p_sys->p_buf );
125         if( p_sys->p_old )
126             picture_Release( p_sys->p_old );
127         return VLC_ENOMEM;
128     }
129
130     return VLC_SUCCESS;
131 }
132
133 /*****************************************************************************
134  * Destroy
135  *****************************************************************************/
136 static void Destroy( vlc_object_t *p_this )
137 {
138     filter_t *p_filter = (filter_t *)p_this;
139     filter_sys_t *p_sys = p_filter->p_sys;
140
141     free( p_sys->p_buf2 );
142     free( p_sys->p_buf );
143     picture_Release( p_sys->p_old );
144     free( p_sys );
145 }
146
147
148 /*****************************************************************************
149  * Filter YUV Planar
150  *****************************************************************************/
151 static picture_t *Filter( filter_t *p_filter, picture_t *p_inpic )
152 {
153     filter_sys_t *p_sys = p_filter->p_sys;
154     const video_format_t *p_fmt = &p_filter->fmt_in.video;
155
156     picture_t *p_outpic;
157
158     uint8_t *p_oldpix   = p_sys->p_old->p[Y_PLANE].p_pixels;
159     const int i_old_pitch = p_sys->p_old->p[Y_PLANE].i_pitch;
160     uint32_t *p_buf = p_sys->p_buf;
161     uint32_t *p_buf2= p_sys->p_buf2;
162
163     unsigned x, y;
164
165     if( !p_inpic )
166         return NULL;
167
168     const uint8_t *p_inpix = p_inpic->p[Y_PLANE].p_pixels;
169     const int i_src_pitch = p_inpic->p[Y_PLANE].i_pitch;
170
171     if( !p_sys->b_old )
172     {
173         picture_Copy( p_sys->p_old, p_inpic );
174         p_sys->b_old = true;
175         return p_inpic;
176     }
177
178     p_outpic = filter_NewPicture( p_filter );
179     if( !p_outpic )
180     {
181         picture_Release( p_inpic );
182         return NULL;
183     }
184     picture_Copy( p_outpic, p_inpic );
185
186     /**
187      * Substract Y planes
188      */
189     for( y = 0; y < p_fmt->i_height; y++ )
190     {
191         for( x = 0; x < p_fmt->i_width; x++ )
192             p_buf2[y*p_fmt->i_width+x] = abs( p_inpix[y*i_src_pitch+x] - p_oldpix[y*i_old_pitch+x] );
193     }
194
195     int i_chroma_dx;
196     int i_chroma_dy;
197     switch( p_inpic->format.i_chroma )
198     {
199         case VLC_CODEC_I420:
200         case VLC_CODEC_J420:
201         case VLC_CODEC_YV12:
202             i_chroma_dx = 2;
203             i_chroma_dy = 2;
204             break;
205
206         case VLC_CODEC_I422:
207         case VLC_CODEC_J422:
208             i_chroma_dx = 2;
209             i_chroma_dy = 1;
210             break;
211
212         default:
213             msg_Warn( p_filter, "Not taking chroma into account" );
214             i_chroma_dx = 0;
215             i_chroma_dy = 0;
216             break;
217     }
218
219     if( i_chroma_dx != 0 && i_chroma_dy != 0 )
220     {
221         const uint8_t *p_inpix_u = p_inpic->p[U_PLANE].p_pixels;
222         const uint8_t *p_inpix_v = p_inpic->p[V_PLANE].p_pixels;
223         const int i_src_pitch_u = p_inpic->p[U_PLANE].i_pitch;
224         const int i_src_pitch_v = p_inpic->p[V_PLANE].i_pitch;
225
226         const uint8_t *p_oldpix_u = p_sys->p_old->p[U_PLANE].p_pixels;
227         const uint8_t *p_oldpix_v = p_sys->p_old->p[V_PLANE].p_pixels;
228         const int i_old_pitch_u = p_sys->p_old->p[U_PLANE].i_pitch;
229         const int i_old_pitch_v = p_sys->p_old->p[V_PLANE].i_pitch;
230
231         for( y = 0; y < p_fmt->i_height/i_chroma_dy; y++ )
232         {
233             for( x = 0; x < p_fmt->i_width/i_chroma_dx; x ++ )
234             {
235                 const int d = abs( p_inpix_u[y*i_src_pitch_u+x] - p_oldpix_u[y*i_old_pitch_u+x] ) +
236                               abs( p_inpix_v[y*i_src_pitch_v+x] - p_oldpix_v[y*i_old_pitch_v+x] );
237                 int i, j;
238
239                 for( j = 0; j < i_chroma_dy; j++ )
240                 {
241                     for( i = 0; i < i_chroma_dx; i++ )
242                         p_buf2[i_chroma_dy*p_fmt->i_width*j + i_chroma_dx*i] = d;
243                 }
244             }
245         }
246     }
247
248     /**
249      * Get the areas where movement was detected
250      */
251     p_sys->i_colors = FindShapes( p_buf2, p_buf, p_fmt->i_width, p_fmt->i_width, p_fmt->i_height,
252                                   p_sys->colors, p_sys->color_x_min, p_sys->color_x_max, p_sys->color_y_min, p_sys->color_y_max );
253
254     /**
255      * Count final number of shapes
256      * Draw rectangles (there can be more than 1 moving shape in 1 rectangle)
257      */
258     Draw( p_filter, p_outpic->p[Y_PLANE].p_pixels, p_outpic->p[Y_PLANE].i_pitch, 1 );
259
260     /**
261      * We're done. Lets keep a copy of the picture
262      * TODO we may just picture_Release with a latency of 1 if the filters/vout
263      * handle it correctly */
264     picture_Copy( p_sys->p_old, p_inpic );
265
266     picture_Release( p_inpic );
267     return p_outpic;
268 }
269
270 /*****************************************************************************
271  * Filter YUV Packed
272  *****************************************************************************/
273 static picture_t *FilterPacked( filter_t *p_filter, picture_t *p_inpic )
274 {
275     filter_sys_t *p_sys = p_filter->p_sys;
276     const video_format_t *p_fmt = &p_filter->fmt_in.video;
277     picture_t *p_outpic;
278
279     const uint8_t *p_inpix = p_inpic->p[Y_PLANE].p_pixels;
280     const int i_src_pitch = p_inpic->p[Y_PLANE].i_pitch;
281
282     uint8_t *p_oldpix   = p_sys->p_old->p[Y_PLANE].p_pixels;
283     const int i_old_pitch = p_sys->p_old->p[Y_PLANE].i_pitch;
284     uint32_t *p_buf = p_sys->p_buf;
285     uint32_t *p_buf2= p_sys->p_buf2;
286
287     int i_y_offset, i_u_offset, i_v_offset;
288
289     unsigned x, y;
290
291     if( GetPackedYuvOffsets( p_fmt->i_chroma,
292                              &i_y_offset, &i_u_offset, &i_v_offset ) )
293     {
294         msg_Warn( p_filter, "Unsupported input chroma (%4.4s)",
295                   (char*)&p_fmt->i_chroma );
296         return p_inpic;
297     }
298
299     if( !p_sys->b_old )
300     {
301         picture_Copy( p_sys->p_old, p_inpic );
302         p_sys->b_old = true;
303         return p_inpic;
304     }
305
306     p_outpic = filter_NewPicture( p_filter );
307     if( !p_outpic )
308     {
309         picture_Release( p_inpic );
310         return NULL;
311     }
312     picture_Copy( p_outpic, p_inpic );
313
314     /* Substract all planes at once */
315
316     for( y = 0; y < p_fmt->i_height; y++ )
317     {
318         for( x = 0; x < p_fmt->i_width; x+=2 )
319         {
320             int i;
321             int d;
322
323             d = abs( p_inpix[y*i_src_pitch+2*x+i_u_offset] - p_oldpix[y*i_old_pitch+2*x+i_u_offset] ) +
324                 abs( p_inpix[y*i_src_pitch+2*x+i_v_offset] - p_oldpix[y*i_old_pitch+2*x+i_v_offset] );
325
326             for( i = 0; i < 2; i++ )
327                 p_buf2[y*p_fmt->i_width+x+i] =
328                     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;
329         }
330     }
331
332     /**
333      * Get the areas where movement was detected
334      */
335     p_sys->i_colors = FindShapes( p_buf2, p_buf, p_fmt->i_width, p_fmt->i_width, p_fmt->i_height,
336                                   p_sys->colors, p_sys->color_x_min, p_sys->color_x_max, p_sys->color_y_min, p_sys->color_y_max );
337
338     /**
339      * Count final number of shapes
340      * Draw rectangles (there can be more than 1 moving shape in 1 rectangle)
341      */
342     Draw( p_filter, &p_outpic->p[Y_PLANE].p_pixels[i_y_offset], p_outpic->p[Y_PLANE].i_pitch, 2 );
343
344     /**
345      * We're done. Lets keep a copy of the picture
346      * TODO we may just picture_Release with a latency of 1 if the filters/vout
347      * handle it correctly */
348     picture_Copy( p_sys->p_old, p_inpic );
349
350     picture_Release( p_inpic );
351     return p_outpic;
352 }
353
354
355
356 /*****************************************************************************
357  * Gaussian Convolution
358  *****************************************************************************
359  *    Gaussian convolution ( sigma == 1.4 )
360  *
361  *    |  2  4  5  4  2  |   |  2  4  4  4  2 |
362  *    |  4  9 12  9  4  |   |  4  8 12  8  4 |
363  *    |  5 12 15 12  5  | ~ |  4 12 16 12  4 |
364  *    |  4  9 12  9  4  |   |  4  8 12  8  4 |
365  *    |  2  4  5  4  2  |   |  2  4  4  4  2 |
366  *****************************************************************************/
367 static void GaussianConvolution( uint32_t *p_inpix, uint32_t *p_smooth,
368                                  int i_src_pitch, int i_num_lines,
369                                  int i_src_visible )
370 {
371     int x,y;
372
373     /* A bit overkill but ... simpler */
374     memset( p_smooth, 0, sizeof(*p_smooth) * i_src_pitch * i_num_lines );
375
376     for( y = 2; y < i_num_lines - 2; y++ )
377     {
378         for( x = 2; x < i_src_visible - 2; x++ )
379         {
380             p_smooth[y*i_src_visible+x] = (uint32_t)(
381               /* 2 rows up */
382                 ( p_inpix[(y-2)*i_src_pitch+x-2] )
383               + ((p_inpix[(y-2)*i_src_pitch+x-1]
384               +   p_inpix[(y-2)*i_src_pitch+x]
385               +   p_inpix[(y-2)*i_src_pitch+x+1])<<1 )
386               + ( p_inpix[(y-2)*i_src_pitch+x+2] )
387               /* 1 row up */
388               + ((p_inpix[(y-1)*i_src_pitch+x-2]
389               + ( p_inpix[(y-1)*i_src_pitch+x-1]<<1 )
390               + ( p_inpix[(y-1)*i_src_pitch+x]*3 )
391               + ( p_inpix[(y-1)*i_src_pitch+x+1]<<1 )
392               +   p_inpix[(y-1)*i_src_pitch+x+2]
393               /* */
394               +   p_inpix[y*i_src_pitch+x-2]
395               + ( p_inpix[y*i_src_pitch+x-1]*3 )
396               + ( p_inpix[y*i_src_pitch+x]<<2 )
397               + ( p_inpix[y*i_src_pitch+x+1]*3 )
398               +   p_inpix[y*i_src_pitch+x+2]
399               /* 1 row down */
400               +   p_inpix[(y+1)*i_src_pitch+x-2]
401               + ( p_inpix[(y+1)*i_src_pitch+x-1]<<1 )
402               + ( p_inpix[(y+1)*i_src_pitch+x]*3 )
403               + ( p_inpix[(y+1)*i_src_pitch+x+1]<<1 )
404               +   p_inpix[(y+1)*i_src_pitch+x+2] )<<1 )
405               /* 2 rows down */
406               + ( p_inpix[(y+2)*i_src_pitch+x-2] )
407               + ((p_inpix[(y+2)*i_src_pitch+x-1]
408               +   p_inpix[(y+2)*i_src_pitch+x]
409               +   p_inpix[(y+2)*i_src_pitch+x+1])<<1 )
410               + ( p_inpix[(y+2)*i_src_pitch+x+2] )
411               ) >> 6 /* 115 */;
412         }
413     }
414 }
415
416 /*****************************************************************************
417  *
418  *****************************************************************************/
419 static int FindShapes( uint32_t *p_diff, uint32_t *p_smooth,
420                        int i_pitch, int i_visible, int i_lines,
421                        int *colors,
422                        int *color_x_min, int *color_x_max,
423                        int *color_y_min, int *color_y_max )
424 {
425     int last = 1;
426     int i, j;
427
428     /**
429      * Apply some smoothing to remove noise
430      */
431     GaussianConvolution( p_diff, p_smooth, i_pitch, i_lines, i_visible );
432
433     /**
434      * Label the shapes and build the labels dependencies list
435      */
436     for( j = 0; j < i_pitch; j++ )
437     {
438         p_smooth[j] = 0;
439         p_smooth[(i_lines-1)*i_pitch+j] = 0;
440     }
441     for( i = 1; i < i_lines-1; i++ )
442     {
443         p_smooth[i*i_pitch] = 0;
444         for( j = 1; j < i_pitch-1; j++ )
445         {
446             if( p_smooth[i*i_pitch+j] > 15 )
447             {
448                 if( p_smooth[(i-1)*i_pitch+j-1] )
449                 {
450                     p_smooth[i*i_pitch+j] = p_smooth[(i-1)*i_pitch+j-1];
451                 }
452                 else if( p_smooth[(i-1)*i_pitch+j] )
453                     p_smooth[i*i_pitch+j] = p_smooth[(i-1)*i_pitch+j];
454                 else if( p_smooth[i*i_pitch+j-1] )
455                     p_smooth[i*i_pitch+j] = p_smooth[i*i_pitch+j-1];
456                 else
457                 {
458                     if( last < NUM_COLORS )
459                     {
460                         p_smooth[i*i_pitch+j] = last;
461                         colors[last] = last;
462                         last++;
463                     }
464                 }
465                 #define CHECK( A ) \
466                 if( p_smooth[A] && p_smooth[A] != p_smooth[i*i_pitch+j] ) \
467                 { \
468                     if( p_smooth[A] < p_smooth[i*i_pitch+j] ) \
469                         colors[p_smooth[i*i_pitch+j]] = p_smooth[A]; \
470                     else \
471                         colors[p_smooth[A]] = p_smooth[i*i_pitch+j]; \
472                 }
473                 CHECK( i*i_pitch+j-1 );
474                 CHECK( (i-1)*i_pitch+j-1 );
475                 CHECK( (i-1)*i_pitch+j );
476                 CHECK( (i-1)*i_pitch+j+1 );
477                 #undef CHECK
478             }
479             else
480             {
481                 p_smooth[i*i_pitch+j] = 0;
482             }
483         }
484         p_smooth[i*i_pitch+j] = 0;
485     }
486
487     /**
488      * Initialise empty rectangle list
489      */
490     for( i = 1; i < last; i++ )
491     {
492         color_x_min[i] = -1;
493         color_x_max[i] = -1;
494         color_y_min[i] = -1;
495         color_y_max[i] = -1;
496     }
497
498     /**
499      * Compute rectangle coordinates
500      */
501     for( i = 0; i < i_pitch * i_lines; i++ )
502     {
503         if( p_smooth[i] )
504         {
505             while( colors[p_smooth[i]] != (int)p_smooth[i] )
506                 p_smooth[i] = colors[p_smooth[i]];
507             if( color_x_min[p_smooth[i]] == -1 )
508             {
509                 color_x_min[p_smooth[i]] =
510                 color_x_max[p_smooth[i]] = i % i_pitch;
511                 color_y_min[p_smooth[i]] =
512                 color_y_max[p_smooth[i]] = i / i_pitch;
513             }
514             else
515             {
516                 int x = i % i_pitch, y = i / i_pitch;
517                 if( x < color_x_min[p_smooth[i]] )
518                     color_x_min[p_smooth[i]] = x;
519                 if( x > color_x_max[p_smooth[i]] )
520                     color_x_max[p_smooth[i]] = x;
521                 if( y < color_y_min[p_smooth[i]] )
522                     color_y_min[p_smooth[i]] = y;
523                 if( y > color_y_max[p_smooth[i]] )
524                     color_y_max[p_smooth[i]] = y;
525             }
526         }
527     }
528
529     /**
530      * Merge overlaping rectangles
531      */
532     for( i = 1; i < last; i++ )
533     {
534         if( colors[i] != i ) continue;
535         if( color_x_min[i] == -1 ) continue;
536         for( j = i+1; j < last; j++ )
537         {
538             if( colors[j] != j ) continue;
539             if( color_x_min[j] == -1 ) continue;
540             if( __MAX( color_x_min[i], color_x_min[j] ) < __MIN( color_x_max[i], color_x_max[j] ) &&
541                 __MAX( color_y_min[i], color_y_min[j] ) < __MIN( color_y_max[i], color_y_max[j] ) )
542             {
543                 color_x_min[i] = __MIN( color_x_min[i], color_x_min[j] );
544                 color_x_max[i] = __MAX( color_x_max[i], color_x_max[j] );
545                 color_y_min[i] = __MIN( color_y_min[i], color_y_min[j] );
546                 color_y_max[i] = __MAX( color_y_max[i], color_y_max[j] );
547                 color_x_min[j] = -1;
548                 j = 0;
549             }
550         }
551     }
552
553     return last;
554 }
555
556 static void Draw( filter_t *p_filter, uint8_t *p_pix, int i_pix_pitch, int i_pix_size )
557 {
558     filter_sys_t *p_sys = p_filter->p_sys;
559     int i, j;
560
561     for( i = 1, j = 0; i < p_sys->i_colors; i++ )
562     {
563         int x, y;
564
565         if( p_sys->colors[i] != i )
566             continue;
567
568         const int color_x_min = p_sys->color_x_min[i];
569         const int color_x_max = p_sys->color_x_max[i];
570         const int color_y_min = p_sys->color_y_min[i];
571         const int color_y_max = p_sys->color_y_max[i];
572
573         if( color_x_min == -1 )
574             continue;
575         if( ( color_y_max - color_y_min ) * ( color_x_max - color_x_min ) < 16 )
576             continue;
577
578         j++;
579
580         y = color_y_min;
581         for( x = color_x_min; x <= color_x_max; x++ )
582             p_pix[y*i_pix_pitch+x*i_pix_size] = 0xff;
583
584         y = color_y_max;
585         for( x = color_x_min; x <= color_x_max; x++ )
586             p_pix[y*i_pix_pitch+x*i_pix_size] = 0xff;
587
588         x = color_x_min;
589         for( y = color_y_min; y <= color_y_max; y++ )
590             p_pix[y*i_pix_pitch+x*i_pix_size] = 0xff;
591
592         x = color_x_max;
593         for( y = color_y_min; y <= color_y_max; y++ )
594             p_pix[y*i_pix_pitch+x*i_pix_size] = 0xff;
595     }
596     msg_Dbg( p_filter, "Counted %d moving shapes.", j );
597 }