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