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