fix filename self-references
[vlc.git] / modules / video_filter / motiondetect.c
1 /*****************************************************************************
2  * motiondetect.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     p_outpic = filter_NewPicture( p_filter );
172     if( !p_outpic )
173     {
174         picture_Release( p_inpic );
175         return NULL;
176     }
177     picture_Copy( p_outpic, p_inpic );
178
179     if( !p_sys->b_old )
180     {
181         picture_Copy( p_sys->p_old, p_inpic );
182         picture_Release( p_inpic );
183         p_sys->b_old = true;
184         return p_outpic;
185     }
186
187     /**
188      * Substract Y planes
189      */
190     for( y = 0; y < p_fmt->i_height; y++ )
191     {
192         for( x = 0; x < p_fmt->i_width; x++ )
193             p_buf2[y*p_fmt->i_width+x] = abs( p_inpix[y*i_src_pitch+x] - p_oldpix[y*i_old_pitch+x] );
194     }
195
196     int i_chroma_dx;
197     int i_chroma_dy;
198     switch( p_inpic->format.i_chroma )
199     {
200         case VLC_CODEC_I420:
201         case VLC_CODEC_J420:
202         case VLC_CODEC_YV12:
203             i_chroma_dx = 2;
204             i_chroma_dy = 2;
205             break;
206
207         case VLC_CODEC_I422:
208         case VLC_CODEC_J422:
209             i_chroma_dx = 2;
210             i_chroma_dy = 1;
211             break;
212
213         default:
214             msg_Warn( p_filter, "Not taking chroma into account" );
215             i_chroma_dx = 0;
216             i_chroma_dy = 0;
217             break;
218     }
219
220     if( i_chroma_dx != 0 && i_chroma_dy != 0 )
221     {
222         const uint8_t *p_inpix_u = p_inpic->p[U_PLANE].p_pixels;
223         const uint8_t *p_inpix_v = p_inpic->p[V_PLANE].p_pixels;
224         const int i_src_pitch_u = p_inpic->p[U_PLANE].i_pitch;
225         const int i_src_pitch_v = p_inpic->p[V_PLANE].i_pitch;
226
227         const uint8_t *p_oldpix_u = p_sys->p_old->p[U_PLANE].p_pixels;
228         const uint8_t *p_oldpix_v = p_sys->p_old->p[V_PLANE].p_pixels;
229         const int i_old_pitch_u = p_sys->p_old->p[U_PLANE].i_pitch;
230         const int i_old_pitch_v = p_sys->p_old->p[V_PLANE].i_pitch;
231
232         for( y = 0; y < p_fmt->i_height/i_chroma_dy; y++ )
233         {
234             for( x = 0; x < p_fmt->i_width/i_chroma_dx; x ++ )
235             {
236                 const int d = abs( p_inpix_u[y*i_src_pitch_u+x] - p_oldpix_u[y*i_old_pitch_u+x] ) +
237                               abs( p_inpix_v[y*i_src_pitch_v+x] - p_oldpix_v[y*i_old_pitch_v+x] );
238                 int i, j;
239
240                 for( j = 0; j < i_chroma_dy; j++ )
241                 {
242                     for( i = 0; i < i_chroma_dx; i++ )
243                         p_buf2[i_chroma_dy*p_fmt->i_width*j + i_chroma_dx*i] = d;
244                 }
245             }
246         }
247     }
248
249     /**
250      * Get the areas where movement was detected
251      */
252     p_sys->i_colors = FindShapes( p_buf2, p_buf, p_fmt->i_width, p_fmt->i_width, p_fmt->i_height,
253                                   p_sys->colors, p_sys->color_x_min, p_sys->color_x_max, p_sys->color_y_min, p_sys->color_y_max );
254
255     /**
256      * Count final number of shapes
257      * Draw rectangles (there can be more than 1 moving shape in 1 rectangle)
258      */
259     Draw( p_filter, p_outpic->p[Y_PLANE].p_pixels, p_outpic->p[Y_PLANE].i_pitch, 1 );
260
261     /**
262      * We're done. Lets keep a copy of the picture
263      * TODO we may just picture_Release with a latency of 1 if the filters/vout
264      * handle it correctly */
265     picture_Copy( p_sys->p_old, p_inpic );
266
267     picture_Release( p_inpic );
268     return p_outpic;
269 }
270
271 /*****************************************************************************
272  * Filter YUV Packed
273  *****************************************************************************/
274 static picture_t *FilterPacked( filter_t *p_filter, picture_t *p_inpic )
275 {
276     filter_sys_t *p_sys = p_filter->p_sys;
277     const video_format_t *p_fmt = &p_filter->fmt_in.video;
278     picture_t *p_outpic;
279
280     const uint8_t *p_inpix = p_inpic->p[Y_PLANE].p_pixels;
281     const int i_src_pitch = p_inpic->p[Y_PLANE].i_pitch;
282
283     uint8_t *p_oldpix   = p_sys->p_old->p[Y_PLANE].p_pixels;
284     const int i_old_pitch = p_sys->p_old->p[Y_PLANE].i_pitch;
285     uint32_t *p_buf = p_sys->p_buf;
286     uint32_t *p_buf2= p_sys->p_buf2;
287
288     int i_y_offset, i_u_offset, i_v_offset;
289
290     unsigned x, y;
291
292     if( GetPackedYuvOffsets( p_fmt->i_chroma,
293                              &i_y_offset, &i_u_offset, &i_v_offset ) )
294     {
295         msg_Warn( p_filter, "Unsupported input chroma (%4.4s)",
296                   (char*)&p_fmt->i_chroma );
297         return p_inpic;
298     }
299
300     if( !p_sys->b_old )
301     {
302         picture_Copy( p_sys->p_old, p_inpic );
303         p_sys->b_old = true;
304         return p_inpic;
305     }
306
307     p_outpic = filter_NewPicture( p_filter );
308     if( !p_outpic )
309     {
310         picture_Release( p_inpic );
311         return NULL;
312     }
313     picture_Copy( p_outpic, p_inpic );
314
315     /* Substract all planes at once */
316
317     for( y = 0; y < p_fmt->i_height; y++ )
318     {
319         for( x = 0; x < p_fmt->i_width; x+=2 )
320         {
321             int i;
322             int d;
323
324             d = abs( p_inpix[y*i_src_pitch+2*x+i_u_offset] - p_oldpix[y*i_old_pitch+2*x+i_u_offset] ) +
325                 abs( p_inpix[y*i_src_pitch+2*x+i_v_offset] - p_oldpix[y*i_old_pitch+2*x+i_v_offset] );
326
327             for( i = 0; i < 2; i++ )
328                 p_buf2[y*p_fmt->i_width+x+i] =
329                     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;
330         }
331     }
332
333     /**
334      * Get the areas where movement was detected
335      */
336     p_sys->i_colors = FindShapes( p_buf2, p_buf, p_fmt->i_width, p_fmt->i_width, p_fmt->i_height,
337                                   p_sys->colors, p_sys->color_x_min, p_sys->color_x_max, p_sys->color_y_min, p_sys->color_y_max );
338
339     /**
340      * Count final number of shapes
341      * Draw rectangles (there can be more than 1 moving shape in 1 rectangle)
342      */
343     Draw( p_filter, &p_outpic->p[Y_PLANE].p_pixels[i_y_offset], p_outpic->p[Y_PLANE].i_pitch, 2 );
344
345     /**
346      * We're done. Lets keep a copy of the picture
347      * TODO we may just picture_Release with a latency of 1 if the filters/vout
348      * handle it correctly */
349     picture_Copy( p_sys->p_old, p_inpic );
350
351     picture_Release( p_inpic );
352     return p_outpic;
353 }
354
355
356
357 /*****************************************************************************
358  * Gaussian Convolution
359  *****************************************************************************
360  *    Gaussian convolution ( sigma == 1.4 )
361  *
362  *    |  2  4  5  4  2  |   |  2  4  4  4  2 |
363  *    |  4  9 12  9  4  |   |  4  8 12  8  4 |
364  *    |  5 12 15 12  5  | ~ |  4 12 16 12  4 |
365  *    |  4  9 12  9  4  |   |  4  8 12  8  4 |
366  *    |  2  4  5  4  2  |   |  2  4  4  4  2 |
367  *****************************************************************************/
368 static void GaussianConvolution( uint32_t *p_inpix, uint32_t *p_smooth,
369                                  int i_src_pitch, int i_num_lines,
370                                  int i_src_visible )
371 {
372     int x,y;
373
374     /* A bit overkill but ... simpler */
375     memset( p_smooth, 0, sizeof(*p_smooth) * i_src_pitch * i_num_lines );
376
377     for( y = 2; y < i_num_lines - 2; y++ )
378     {
379         for( x = 2; x < i_src_visible - 2; x++ )
380         {
381             p_smooth[y*i_src_visible+x] = (uint32_t)(
382               /* 2 rows up */
383                 ( p_inpix[(y-2)*i_src_pitch+x-2] )
384               + ((p_inpix[(y-2)*i_src_pitch+x-1]
385               +   p_inpix[(y-2)*i_src_pitch+x]
386               +   p_inpix[(y-2)*i_src_pitch+x+1])<<1 )
387               + ( p_inpix[(y-2)*i_src_pitch+x+2] )
388               /* 1 row up */
389               + ((p_inpix[(y-1)*i_src_pitch+x-2]
390               + ( p_inpix[(y-1)*i_src_pitch+x-1]<<1 )
391               + ( p_inpix[(y-1)*i_src_pitch+x]*3 )
392               + ( p_inpix[(y-1)*i_src_pitch+x+1]<<1 )
393               +   p_inpix[(y-1)*i_src_pitch+x+2]
394               /* */
395               +   p_inpix[y*i_src_pitch+x-2]
396               + ( p_inpix[y*i_src_pitch+x-1]*3 )
397               + ( p_inpix[y*i_src_pitch+x]<<2 )
398               + ( p_inpix[y*i_src_pitch+x+1]*3 )
399               +   p_inpix[y*i_src_pitch+x+2]
400               /* 1 row down */
401               +   p_inpix[(y+1)*i_src_pitch+x-2]
402               + ( p_inpix[(y+1)*i_src_pitch+x-1]<<1 )
403               + ( p_inpix[(y+1)*i_src_pitch+x]*3 )
404               + ( p_inpix[(y+1)*i_src_pitch+x+1]<<1 )
405               +   p_inpix[(y+1)*i_src_pitch+x+2] )<<1 )
406               /* 2 rows down */
407               + ( p_inpix[(y+2)*i_src_pitch+x-2] )
408               + ((p_inpix[(y+2)*i_src_pitch+x-1]
409               +   p_inpix[(y+2)*i_src_pitch+x]
410               +   p_inpix[(y+2)*i_src_pitch+x+1])<<1 )
411               + ( p_inpix[(y+2)*i_src_pitch+x+2] )
412               ) >> 6 /* 115 */;
413         }
414     }
415 }
416
417 /*****************************************************************************
418  *
419  *****************************************************************************/
420 static int FindShapes( uint32_t *p_diff, uint32_t *p_smooth,
421                        int i_pitch, int i_visible, int i_lines,
422                        int *colors,
423                        int *color_x_min, int *color_x_max,
424                        int *color_y_min, int *color_y_max )
425 {
426     int last = 1;
427     int i, j;
428
429     /**
430      * Apply some smoothing to remove noise
431      */
432     GaussianConvolution( p_diff, p_smooth, i_pitch, i_lines, i_visible );
433
434     /**
435      * Label the shapes and build the labels dependencies list
436      */
437     for( j = 0; j < i_pitch; j++ )
438     {
439         p_smooth[j] = 0;
440         p_smooth[(i_lines-1)*i_pitch+j] = 0;
441     }
442     for( i = 1; i < i_lines-1; i++ )
443     {
444         p_smooth[i*i_pitch] = 0;
445         for( j = 1; j < i_pitch-1; j++ )
446         {
447             if( p_smooth[i*i_pitch+j] > 15 )
448             {
449                 if( p_smooth[(i-1)*i_pitch+j-1] )
450                 {
451                     p_smooth[i*i_pitch+j] = p_smooth[(i-1)*i_pitch+j-1];
452                 }
453                 else if( p_smooth[(i-1)*i_pitch+j] )
454                     p_smooth[i*i_pitch+j] = p_smooth[(i-1)*i_pitch+j];
455                 else if( p_smooth[i*i_pitch+j-1] )
456                     p_smooth[i*i_pitch+j] = p_smooth[i*i_pitch+j-1];
457                 else
458                 {
459                     if( last < NUM_COLORS )
460                     {
461                         p_smooth[i*i_pitch+j] = last;
462                         colors[last] = last;
463                         last++;
464                     }
465                 }
466                 #define CHECK( A ) \
467                 if( p_smooth[A] && p_smooth[A] != p_smooth[i*i_pitch+j] ) \
468                 { \
469                     if( p_smooth[A] < p_smooth[i*i_pitch+j] ) \
470                         colors[p_smooth[i*i_pitch+j]] = p_smooth[A]; \
471                     else \
472                         colors[p_smooth[A]] = p_smooth[i*i_pitch+j]; \
473                 }
474                 CHECK( i*i_pitch+j-1 );
475                 CHECK( (i-1)*i_pitch+j-1 );
476                 CHECK( (i-1)*i_pitch+j );
477                 CHECK( (i-1)*i_pitch+j+1 );
478                 #undef CHECK
479             }
480             else
481             {
482                 p_smooth[i*i_pitch+j] = 0;
483             }
484         }
485         p_smooth[i*i_pitch+j] = 0;
486     }
487
488     /**
489      * Initialise empty rectangle list
490      */
491     for( i = 1; i < last; i++ )
492     {
493         color_x_min[i] = -1;
494         color_x_max[i] = -1;
495         color_y_min[i] = -1;
496         color_y_max[i] = -1;
497     }
498
499     /**
500      * Compute rectangle coordinates
501      */
502     for( i = 0; i < i_pitch * i_lines; i++ )
503     {
504         if( p_smooth[i] )
505         {
506             while( colors[p_smooth[i]] != (int)p_smooth[i] )
507                 p_smooth[i] = colors[p_smooth[i]];
508             if( color_x_min[p_smooth[i]] == -1 )
509             {
510                 color_x_min[p_smooth[i]] =
511                 color_x_max[p_smooth[i]] = i % i_pitch;
512                 color_y_min[p_smooth[i]] =
513                 color_y_max[p_smooth[i]] = i / i_pitch;
514             }
515             else
516             {
517                 int x = i % i_pitch, y = i / i_pitch;
518                 if( x < color_x_min[p_smooth[i]] )
519                     color_x_min[p_smooth[i]] = x;
520                 if( x > color_x_max[p_smooth[i]] )
521                     color_x_max[p_smooth[i]] = x;
522                 if( y < color_y_min[p_smooth[i]] )
523                     color_y_min[p_smooth[i]] = y;
524                 if( y > color_y_max[p_smooth[i]] )
525                     color_y_max[p_smooth[i]] = y;
526             }
527         }
528     }
529
530     /**
531      * Merge overlaping rectangles
532      */
533     for( i = 1; i < last; i++ )
534     {
535         if( colors[i] != i ) continue;
536         if( color_x_min[i] == -1 ) continue;
537         for( j = i+1; j < last; j++ )
538         {
539             if( colors[j] != j ) continue;
540             if( color_x_min[j] == -1 ) continue;
541             if( __MAX( color_x_min[i], color_x_min[j] ) < __MIN( color_x_max[i], color_x_max[j] ) &&
542                 __MAX( color_y_min[i], color_y_min[j] ) < __MIN( color_y_max[i], color_y_max[j] ) )
543             {
544                 color_x_min[i] = __MIN( color_x_min[i], color_x_min[j] );
545                 color_x_max[i] = __MAX( color_x_max[i], color_x_max[j] );
546                 color_y_min[i] = __MIN( color_y_min[i], color_y_min[j] );
547                 color_y_max[i] = __MAX( color_y_max[i], color_y_max[j] );
548                 color_x_min[j] = -1;
549                 j = 0;
550             }
551         }
552     }
553
554     return last;
555 }
556
557 static void Draw( filter_t *p_filter, uint8_t *p_pix, int i_pix_pitch, int i_pix_size )
558 {
559     filter_sys_t *p_sys = p_filter->p_sys;
560     int i, j;
561
562     for( i = 1, j = 0; i < p_sys->i_colors; i++ )
563     {
564         int x, y;
565
566         if( p_sys->colors[i] != i )
567             continue;
568
569         const int color_x_min = p_sys->color_x_min[i];
570         const int color_x_max = p_sys->color_x_max[i];
571         const int color_y_min = p_sys->color_y_min[i];
572         const int color_y_max = p_sys->color_y_max[i];
573
574         if( color_x_min == -1 )
575             continue;
576         if( ( color_y_max - color_y_min ) * ( color_x_max - color_x_min ) < 16 )
577             continue;
578
579         j++;
580
581         y = color_y_min;
582         for( x = color_x_min; x <= color_x_max; x++ )
583             p_pix[y*i_pix_pitch+x*i_pix_size] = 0xff;
584
585         y = color_y_max;
586         for( x = color_x_min; x <= color_x_max; x++ )
587             p_pix[y*i_pix_pitch+x*i_pix_size] = 0xff;
588
589         x = color_x_min;
590         for( y = color_y_min; y <= color_y_max; y++ )
591             p_pix[y*i_pix_pitch+x*i_pix_size] = 0xff;
592
593         x = color_x_max;
594         for( y = color_y_min; y <= color_y_max; y++ )
595             p_pix[y*i_pix_pitch+x*i_pix_size] = 0xff;
596     }
597     msg_Dbg( p_filter, "Counted %d moving shapes.", j );
598 }