9906132a6161493555e4301d75aef89b6ca9463b
[vlc.git] / modules / video_filter / motiondetect.c
1 /*****************************************************************************
2  * motiondetect.c : Second version of a motion detection plugin.
3  *****************************************************************************
4  * Copyright (C) 2000-2008 VLC authors and VideoLAN
5  *
6  * Authors: Antoine Cellerier <dionoea -at- videolan -dot- org>
7  *
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.
12  *
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.
17  *
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  *****************************************************************************/
22
23 /*****************************************************************************
24  * Preamble
25  *****************************************************************************/
26
27 #ifdef HAVE_CONFIG_H
28 # include "config.h"
29 #endif
30
31 #include <vlc_common.h>
32 #include <vlc_plugin.h>
33 #include <vlc_sout.h>
34 #include <vlc_filter.h>
35 #include <vlc_picture.h>
36 #include "filter_picture.h"
37
38 /*****************************************************************************
39  * Module descriptor
40  *****************************************************************************/
41 static int  Create    ( vlc_object_t * );
42 static void Destroy   ( vlc_object_t * );
43
44 #define FILTER_PREFIX "motiondetect-"
45
46 vlc_module_begin ()
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 )
52
53     add_shortcut( "motion" )
54     set_callbacks( Create, Destroy )
55 vlc_module_end ()
56
57
58 /*****************************************************************************
59  * Local prototypes
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)
67
68 typedef struct
69 {
70     bool is_yuv_planar;
71     picture_t *p_old;
72     uint32_t *p_buf;
73     uint32_t *p_buf2;
74
75     /* */
76     int i_colors;
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];
82 } filter_sys_t;
83
84 /*****************************************************************************
85  * Create
86  *****************************************************************************/
87 static int Create( vlc_object_t *p_this )
88 {
89     filter_t *p_filter = (filter_t *)p_this;
90     const video_format_t *p_fmt = &p_filter->fmt_in.video;
91     filter_sys_t *p_sys;
92     bool is_yuv_planar;
93
94     switch( p_fmt->i_chroma )
95     {
96         CASE_PLANAR_YUV
97             is_yuv_planar = true;
98             break;
99
100         CASE_PACKED_YUV_422
101             is_yuv_planar = false;
102             break;
103
104         default:
105             msg_Err( p_filter, "Unsupported input chroma (%4.4s)",
106                      (char*)&(p_fmt->i_chroma) );
107             return VLC_EGENERIC;
108     }
109     p_filter->pf_video_filter = Filter;
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->is_yuv_planar = is_yuv_planar;
117     p_sys->p_old = NULL;
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_buf || !p_sys->p_buf2 )
122     {
123         free( p_sys->p_buf2 );
124         free( p_sys->p_buf );
125         return VLC_ENOMEM;
126     }
127
128     return VLC_SUCCESS;
129 }
130
131 /*****************************************************************************
132  * Destroy
133  *****************************************************************************/
134 static void Destroy( vlc_object_t *p_this )
135 {
136     filter_t *p_filter = (filter_t *)p_this;
137     filter_sys_t *p_sys = p_filter->p_sys;
138
139     free( p_sys->p_buf2 );
140     free( p_sys->p_buf );
141     if( p_sys->p_old )
142         picture_Release( p_sys->p_old );
143     free( p_sys );
144 }
145
146
147 /*****************************************************************************
148  * Filter YUV Planar/Packed
149  *****************************************************************************/
150 static void PreparePlanar( filter_t *p_filter, picture_t *p_inpic )
151 {
152     filter_sys_t *p_sys = p_filter->p_sys;
153     const video_format_t *p_fmt = &p_filter->fmt_in.video;
154
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;
157
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;
160
161     /**
162      * Substract Y planes
163      */
164     for( unsigned y = 0; y < p_fmt->i_height; y++ )
165     {
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] );
168     }
169
170     int i_chroma_dx;
171     int i_chroma_dy;
172     switch( p_inpic->format.i_chroma )
173     {
174         case VLC_CODEC_I420:
175         case VLC_CODEC_J420:
176         case VLC_CODEC_YV12:
177             i_chroma_dx = 2;
178             i_chroma_dy = 2;
179             break;
180
181         case VLC_CODEC_I422:
182         case VLC_CODEC_J422:
183             i_chroma_dx = 2;
184             i_chroma_dy = 1;
185             break;
186
187         default:
188             msg_Warn( p_filter, "Not taking chroma into account" );
189             return;
190     }
191
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;
196
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;
201
202     for( unsigned y = 0; y < p_fmt->i_height/i_chroma_dy; y++ )
203     {
204         for( unsigned x = 0; x < p_fmt->i_width/i_chroma_dx; x ++ )
205         {
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] );
208
209             for( int j = 0; j < i_chroma_dy; j++ )
210             {
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;
213             }
214         }
215     }
216 }
217
218 static int PreparePacked( filter_t *p_filter, picture_t *p_inpic, int *pi_pix_offset )
219 {
220     filter_sys_t *p_sys = p_filter->p_sys;
221     const video_format_t *p_fmt = &p_filter->fmt_in.video;
222
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 ) )
226     {
227         msg_Warn( p_filter, "Unsupported input chroma (%4.4s)",
228                   (char*)&p_fmt->i_chroma );
229         return VLC_EGENERIC;
230     }
231     *pi_pix_offset = i_y_offset;
232
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;
236
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;
239
240     for( unsigned y = 0; y < p_fmt->i_height; y++ )
241     {
242         for( unsigned x = 0; x < p_fmt->i_width; x+=2 )
243         {
244             int d;
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] );
247
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;
251         }
252     }
253     return VLC_SUCCESS;
254 }
255
256 static picture_t *Filter( filter_t *p_filter, picture_t *p_inpic )
257 {
258     filter_sys_t *p_sys = p_filter->p_sys;
259
260     if( !p_inpic )
261         return NULL;
262
263     picture_t *p_outpic = filter_NewPicture( p_filter );
264     if( !p_outpic )
265     {
266         picture_Release( p_inpic );
267         return NULL;
268     }
269     picture_Copy( p_outpic, p_inpic );
270
271     if( !p_sys->p_old )
272     {
273         p_sys->p_old = picture_Hold( p_inpic );
274         goto exit;
275     }
276
277     int i_pix_offset;
278     int i_pix_size;
279     if( p_sys->is_yuv_planar )
280     {
281         PreparePlanar( p_filter, p_inpic );
282         i_pix_offset = 0;
283         i_pix_size = 1;
284     }
285     else
286     {
287         if( PreparePacked( p_filter, p_inpic, &i_pix_offset ) )
288             goto exit;
289         i_pix_size = 2;
290     }
291
292     /**
293      * Get the areas where movement was detected
294      */
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 );
298
299     /**
300      * Count final number of shapes
301      * Draw rectangles (there can be more than 1 moving shape in 1 rectangle)
302      */
303     Draw( p_filter, &p_outpic->p[Y_PLANE].p_pixels[i_pix_offset], p_outpic->p[Y_PLANE].i_pitch, i_pix_size );
304
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 );
308
309 exit:
310     picture_Release( p_inpic );
311     return p_outpic;
312 }
313
314
315 /*****************************************************************************
316  * Gaussian Convolution
317  *****************************************************************************
318  *    Gaussian convolution ( sigma == 1.4 )
319  *
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,
328                                  int i_src_visible )
329 {
330     /* A bit overkill but ... simpler */
331     memset( p_smooth, 0, sizeof(*p_smooth) * i_src_pitch * i_num_lines );
332
333     for( int y = 2; y < i_num_lines - 2; y++ )
334     {
335         for( int x = 2; x < i_src_visible - 2; x++ )
336         {
337             p_smooth[y*i_src_visible+x] = (uint32_t)(
338               /* 2 rows up */
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] )
344               /* 1 row up */
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]
350               /* */
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]
356               /* 1 row down */
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 )
362               /* 2 rows down */
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] )
368               ) >> 6 /* 115 */;
369         }
370     }
371 }
372
373 /*****************************************************************************
374  *
375  *****************************************************************************/
376 static int FindShapes( uint32_t *p_diff, uint32_t *p_smooth,
377                        int i_pitch, int i_visible, int i_lines,
378                        int *colors,
379                        int *color_x_min, int *color_x_max,
380                        int *color_y_min, int *color_y_max )
381 {
382     int last = 1;
383
384     /**
385      * Apply some smoothing to remove noise
386      */
387     GaussianConvolution( p_diff, p_smooth, i_pitch, i_lines, i_visible );
388
389     /**
390      * Label the shapes and build the labels dependencies list
391      */
392     for( int j = 0; j < i_pitch; j++ )
393     {
394         p_smooth[j] = 0;
395         p_smooth[(i_lines-1)*i_pitch+j] = 0;
396     }
397     for( int i = 1; i < i_lines-1; i++ )
398     {
399         int j;
400         p_smooth[i*i_pitch] = 0;
401         for( j = 1; j < i_pitch-1; j++ )
402         {
403             if( p_smooth[i*i_pitch+j] > 15 )
404             {
405                 if( p_smooth[(i-1)*i_pitch+j-1] )
406                 {
407                     p_smooth[i*i_pitch+j] = p_smooth[(i-1)*i_pitch+j-1];
408                 }
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];
413                 else
414                 {
415                     if( last < NUM_COLORS )
416                     {
417                         p_smooth[i*i_pitch+j] = last;
418                         colors[last] = last;
419                         last++;
420                     }
421                 }
422                 #define CHECK( A ) \
423                 if( p_smooth[A] && p_smooth[A] != p_smooth[i*i_pitch+j] ) \
424                 { \
425                     if( p_smooth[A] < p_smooth[i*i_pitch+j] ) \
426                         colors[p_smooth[i*i_pitch+j]] = p_smooth[A]; \
427                     else \
428                         colors[p_smooth[A]] = p_smooth[i*i_pitch+j]; \
429                 }
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 );
434                 #undef CHECK
435             }
436             else
437             {
438                 p_smooth[i*i_pitch+j] = 0;
439             }
440         }
441         p_smooth[i*i_pitch+j] = 0;
442     }
443
444     /**
445      * Initialise empty rectangle list
446      */
447     for( int i = 1; i < last; i++ )
448     {
449         color_x_min[i] = -1;
450         color_x_max[i] = -1;
451         color_y_min[i] = -1;
452         color_y_max[i] = -1;
453     }
454
455     /**
456      * Compute rectangle coordinates
457      */
458     for( int i = 0; i < i_pitch * i_lines; i++ )
459     {
460         if( p_smooth[i] )
461         {
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 )
465             {
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;
470             }
471             else
472             {
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;
482             }
483         }
484     }
485
486     /**
487      * Merge overlaping rectangles
488      */
489     for( int i = 1; i < last; i++ )
490     {
491         if( colors[i] != i ) continue;
492         if( color_x_min[i] == -1 ) continue;
493         for( int j = i+1; j < last; j++ )
494         {
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] ) )
499             {
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] );
504                 color_x_min[j] = -1;
505                 j = 0;
506             }
507         }
508     }
509
510     return last;
511 }
512
513 static void Draw( filter_t *p_filter, uint8_t *p_pix, int i_pix_pitch, int i_pix_size )
514 {
515     filter_sys_t *p_sys = p_filter->p_sys;
516
517     int j = 0;
518
519     for( int i = 1; i < p_sys->i_colors; i++ )
520     {
521         int x, y;
522
523         if( p_sys->colors[i] != i )
524             continue;
525
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];
530
531         if( color_x_min == -1 )
532             continue;
533         if( ( color_y_max - color_y_min ) * ( color_x_max - color_x_min ) < 16 )
534             continue;
535
536         j++;
537
538         y = color_y_min;
539         for( x = color_x_min; x <= color_x_max; x++ )
540             p_pix[y*i_pix_pitch+x*i_pix_size] = 0xff;
541
542         y = color_y_max;
543         for( x = color_x_min; x <= color_x_max; x++ )
544             p_pix[y*i_pix_pitch+x*i_pix_size] = 0xff;
545
546         x = color_x_min;
547         for( y = color_y_min; y <= color_y_max; y++ )
548             p_pix[y*i_pix_pitch+x*i_pix_size] = 0xff;
549
550         x = color_x_max;
551         for( y = color_y_min; y <= color_y_max; y++ )
552             p_pix[y*i_pix_pitch+x*i_pix_size] = 0xff;
553     }
554     msg_Dbg( p_filter, "Counted %d moving shapes.", j );
555 }