]> git.sesse.net Git - vlc/blob - modules/video_filter/motiondetect.c
26f4edd53f48be726f67b928c07a24e42263a109
[vlc] / modules / video_filter / motiondetect.c
1 /*****************************************************************************
2  * motiondetec.c : Second version of a motion detection plugin.
3  *****************************************************************************
4  * Copyright (C) 2000-2006 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 #include <stdlib.h>                                      /* malloc(), free() */
28 #include <string.h>
29
30 #include <math.h>                                            /* sin(), cos() */
31
32 #include <vlc/vlc.h>
33 #include <vlc_sout.h>
34 #include <vlc_vout.h>
35
36 #include "vlc_filter.h"
37
38 /*****************************************************************************
39  * Local prototypes
40  *****************************************************************************/
41 static int  Create    ( vlc_object_t * );
42 static void Destroy   ( vlc_object_t * );
43
44 static picture_t *Filter( filter_t *, picture_t * );
45 static void GaussianConvolution( uint32_t *, uint32_t *, int, int, int );
46
47 /*****************************************************************************
48  * Module descriptor
49  *****************************************************************************/
50
51 #define FILTER_PREFIX "motiondetect-"
52
53 vlc_module_begin();
54     set_description( _("Motion detect video filter") );
55     set_shortname( _( "Motion Detect" ));
56     set_capability( "video filter2", 0 );
57     set_category( CAT_VIDEO );
58     set_subcategory( SUBCAT_VIDEO_VFILTER );
59
60     add_shortcut( "motion" );
61     set_callbacks( Create, Destroy );
62 vlc_module_end();
63
64 #if 0
65 static const char *ppsz_filter_options[] = {
66     NULL
67 };
68 #endif
69
70 struct filter_sys_t
71 {
72     uint8_t *p_oldpix;
73     uint8_t *p_oldpix_u;
74     uint8_t *p_oldpix_v;
75     uint32_t *p_buf;
76     uint32_t *p_buf2;
77     vlc_mutex_t lock;
78 };
79
80 /*****************************************************************************
81  * Create
82  *****************************************************************************/
83 static int Create( vlc_object_t *p_this )
84 {
85     filter_t *p_filter = (filter_t *)p_this;
86
87     /* Allocate structure */
88     p_filter->p_sys = malloc( sizeof( filter_sys_t ) );
89     if( p_filter->p_sys == NULL )
90     {
91         msg_Err( p_filter, "out of memory" );
92         return VLC_ENOMEM;
93     }
94
95     p_filter->pf_video_filter = Filter;
96
97     p_filter->p_sys->p_oldpix = NULL;
98     p_filter->p_sys->p_buf = NULL;
99
100 #if 0
101     config_ChainParse( p_filter, FILTER_PREFIX, ppsz_filter_options,
102                    p_filter->p_cfg );
103 #endif
104     vlc_mutex_init( p_filter, &p_filter->p_sys->lock );
105
106     return VLC_SUCCESS;
107 }
108
109 /*****************************************************************************
110  * Destroy
111  *****************************************************************************/
112 static void Destroy( vlc_object_t *p_this )
113 {
114     filter_t *p_filter = (filter_t *)p_this;
115
116     free( p_filter->p_sys->p_oldpix );
117     free( p_filter->p_sys->p_buf );
118
119     vlc_mutex_destroy( &p_filter->p_sys->lock );
120
121     free( p_filter->p_sys );
122 }
123
124 /*****************************************************************************
125  * Render
126  *****************************************************************************/
127 static picture_t *Filter( filter_t *p_filter, picture_t *p_inpic )
128 {
129     picture_t *p_outpic;
130     filter_sys_t *p_sys = p_filter->p_sys;
131
132     const uint8_t *p_inpix = p_inpic->p[Y_PLANE].p_pixels;
133     const int i_src_pitch = p_inpic->p[Y_PLANE].i_pitch;
134     const int i_src_visible = p_inpic->p[Y_PLANE].i_visible_pitch;
135     const int i_num_lines = p_inpic->p[Y_PLANE].i_visible_lines;
136
137     const uint8_t *p_inpix_u = p_inpic->p[U_PLANE].p_pixels;
138     const uint8_t *p_inpix_v = p_inpic->p[V_PLANE].p_pixels;
139     const int i_src_pitch_u = p_inpic->p[U_PLANE].i_pitch;
140     const int i_src_visible_u = p_inpic->p[U_PLANE].i_visible_pitch;
141     const int i_num_lines_u = p_inpic->p[U_PLANE].i_visible_lines;
142
143     uint8_t *p_oldpix;
144     uint8_t *p_oldpix_u;
145     uint8_t *p_oldpix_v;
146     uint8_t *p_outpix;
147     uint32_t *p_buf;
148     uint32_t *p_buf2;
149
150     int i,j;
151     int last;
152
153     if( !p_inpic ) return NULL;
154
155     p_outpic = p_filter->pf_vout_buffer_new( p_filter );
156     if( !p_outpic )
157     {
158         msg_Warn( p_filter, "can't get output picture" );
159         if( p_inpic->pf_release )
160             p_inpic->pf_release( p_inpic );
161         return NULL;
162     }
163
164     p_outpix = p_outpic->p[Y_PLANE].p_pixels;
165 #if 0
166     p_filter->p_libvlc->pf_memset( p_outpic->p[U_PLANE].p_pixels, 0x80,
167         p_inpic->p[U_PLANE].i_pitch * p_inpic->p[U_PLANE].i_visible_lines );
168     p_filter->p_libvlc->pf_memset( p_outpic->p[V_PLANE].p_pixels, 0x80,
169         p_inpic->p[V_PLANE].i_pitch * p_inpic->p[V_PLANE].i_visible_lines );
170 #else
171     p_filter->p_libvlc->pf_memcpy( p_outpic->p[U_PLANE].p_pixels,
172                                    p_inpic->p[U_PLANE].p_pixels,
173         p_inpic->p[U_PLANE].i_pitch * p_inpic->p[U_PLANE].i_visible_lines );
174     p_filter->p_libvlc->pf_memcpy( p_outpic->p[V_PLANE].p_pixels,
175                                    p_inpic->p[V_PLANE].p_pixels,
176         p_inpic->p[V_PLANE].i_pitch * p_inpic->p[V_PLANE].i_visible_lines );
177 #endif
178
179     if( !p_sys->p_oldpix || !p_sys->p_buf )
180     {
181         free( p_sys->p_oldpix );
182         free( p_sys->p_buf );
183         p_sys->p_oldpix = malloc( i_src_pitch * i_num_lines );
184         p_sys->p_oldpix_u = malloc( i_src_pitch_u * i_num_lines_u );
185         p_sys->p_oldpix_v = malloc( i_src_pitch_u * i_num_lines_u );
186         p_sys->p_buf = malloc( sizeof( uint32_t ) * i_src_pitch * i_num_lines );
187         p_sys->p_buf2 = malloc( sizeof( uint32_t ) * i_src_pitch * i_num_lines);
188         return p_inpic;
189     }
190     p_oldpix = p_sys->p_oldpix;
191     p_oldpix_u = p_sys->p_oldpix_u;
192     p_oldpix_v = p_sys->p_oldpix_v;
193     p_buf = p_sys->p_buf;
194     p_buf2 = p_sys->p_buf2;
195
196     vlc_mutex_lock( &p_filter->p_sys->lock );
197
198     /**
199      * Substract Y planes
200      */
201     for( i = 0; i < i_src_pitch * i_num_lines; i++ )
202     {
203         if( p_inpix[i] > p_oldpix[i] )
204         {
205             p_buf2[i] = p_inpix[i] - p_oldpix[i];
206         }
207         else
208         {
209             p_buf2[i] = p_oldpix[i] - p_inpix[i];
210         }
211     }
212     int line;
213     int col;
214     int format;
215     switch( p_inpic->format.i_chroma )
216     {
217         case VLC_FOURCC('I','4','2','0'):
218         case VLC_FOURCC('I','Y','U','V'):
219         case VLC_FOURCC('J','4','2','0'):
220         case VLC_FOURCC('Y','V','1','2'):
221             format = 1;
222             break;
223
224         case VLC_FOURCC('I','4','2','2'):
225         case VLC_FOURCC('J','4','2','2'):
226             format = 2;
227             break;
228
229         default:
230             format = 0;
231             msg_Warn( p_filter, "Not taking chroma into account" );
232             break;
233     }
234     format = 0;
235     if( format )
236     {
237         for( line = 0; line < i_num_lines_u; line++ )
238         {
239             for( col = 0; col < i_src_pitch_u; col ++ )
240             {
241                 int diff;
242                 i = line * i_src_pitch_u + col;
243                 if( p_inpix_u[i] > p_oldpix_u[i] )
244                 {
245                     diff = p_inpix_u[i] - p_oldpix_u[i];
246                 }
247                 else
248                 {
249                     diff = p_oldpix_u[i] - p_inpix_u[i];
250                 }
251                 switch( format )
252                 {
253                     case 1:
254                         p_buf2[2*line*i_src_pitch+2*col] += diff;
255                         p_buf2[2*line*i_src_pitch+2*col+1] += diff;
256                         p_buf2[(2*line+1)*i_src_pitch+2*col] += diff;
257                         p_buf2[(2*line+1)*i_src_pitch+2*col+1] += diff;
258                         break;
259
260                     case2:
261                         p_buf2[line*i_src_pitch+2*col] += diff;
262                         p_buf2[line*i_src_pitch+2*col+1] += diff;
263                         break;
264                 }
265             }
266         }
267     }
268
269     /**
270      * Apply some smoothing to remove noise
271      */
272 #if 1
273     GaussianConvolution( p_buf2, p_buf, i_src_pitch, i_num_lines, i_src_visible );
274
275 #else
276     uint32_t *pouet = p_buf2;
277     p_buf2 = p_buf;
278     p_buf = pouet;
279 #endif
280     /**
281      * Copy luminance plane
282      */
283     for( i = 0; i < i_src_pitch * i_num_lines; i++ )
284     {
285         /*if( p_buf[i] > 25 )
286         {
287             //p_outpix[i] = 255;
288         }
289         else// if( p_buf[i] > 15 )*/
290         {
291             p_outpix[i] = p_inpix[i];
292         }
293         /*else
294         {
295             p_outpix[i] = 0;
296         }*/
297     }
298
299     /**
300      * Label the shapes ans build the labels dependencies list
301      */
302     last = 1;
303     int colors[5000];
304     int color_x_min[5000];
305     int color_x_max[5000];
306     int color_y_min[5000];
307     int color_y_max[5000];
308
309     for( j = 0; j < i_src_pitch; j++ )
310     {
311         p_buf[j] = 0;
312         p_buf[(i_num_lines-1)*i_src_pitch+j] = 0;
313     }
314     for( i = 1; i < i_num_lines-1; i++ )
315     {
316         p_buf[i*i_src_pitch] = 0;
317         for( j = 1; j < i_src_pitch-1; j++ )
318         {
319             if( p_buf[i*i_src_pitch+j] > 15 )
320             {
321                 if( p_buf[(i-1)*i_src_pitch+j-1] )
322                 {
323                     p_buf[i*i_src_pitch+j] = p_buf[(i-1)*i_src_pitch+j-1];
324                 }
325                 else if( p_buf[(i-1)*i_src_pitch+j] )
326                     p_buf[i*i_src_pitch+j] = p_buf[(i-1)*i_src_pitch+j];
327                 else if( p_buf[i*i_src_pitch+j-1] )
328                     p_buf[i*i_src_pitch+j] = p_buf[i*i_src_pitch+j-1];
329                 else
330                 {
331                     p_buf[i*i_src_pitch+j] = last;
332                     colors[last] = last;
333                     last++;
334                 }
335                 #define CHECK( A ) \
336                 if( p_buf[A] && p_buf[A] != p_buf[i*i_src_pitch+j] ) \
337                 { \
338                     if( p_buf[A] < p_buf[i*i_src_pitch+j] ) \
339                         colors[p_buf[i*i_src_pitch+j]] = p_buf[A]; \
340                     else \
341                         colors[p_buf[A]] = p_buf[i*i_src_pitch+j]; \
342                 }
343                 CHECK( i*i_src_pitch+j-1 );
344                 CHECK( (i-1)*i_src_pitch+j-1 );
345                 CHECK( (i-1)*i_src_pitch+j );
346                 CHECK( (i-1)*i_src_pitch+j+1 );
347             }
348             else
349             {
350                 p_buf[i*i_src_pitch+j] = 0;
351             }
352         }
353         p_buf[i*i_src_pitch+j] = 0;
354     }
355
356     /**
357      * Initialise empty rectangle list
358      */
359     for( i = 1; i < last; i++ )
360     {
361         color_x_min[i] = -1;
362         color_x_max[i] = -1;
363         color_y_min[i] = -1;
364         color_y_max[i] = -1;
365     }
366
367     /**
368      * Compute rectangle coordinates
369      */
370     for( i = 0; i < i_src_pitch * i_num_lines; i++ )
371     {
372         if( p_buf[i] )
373         {
374             while( colors[p_buf[i]] != p_buf[i] )
375                 p_buf[i] = colors[p_buf[i]];
376             //p_outpix[i] = /*(p_buf[i]%2) */ 0xff;
377             if( color_x_min[p_buf[i]] == -1 )
378             {
379                 color_x_min[p_buf[i]] =
380                 color_x_max[p_buf[i]] = i % i_src_pitch;
381                 color_y_min[p_buf[i]] =
382                 color_y_max[p_buf[i]] = i / i_src_pitch;
383             }
384             else
385             {
386                 int x = i % i_src_pitch, y = i / i_src_pitch;
387                 if( x < color_x_min[p_buf[i]] )
388                     color_x_min[p_buf[i]] = x;
389                 if( x > color_x_max[p_buf[i]] )
390                     color_x_max[p_buf[i]] = x;
391                 if( y < color_y_min[p_buf[i]] )
392                     color_y_min[p_buf[i]] = y;
393                 if( y > color_y_max[p_buf[i]] )
394                     color_y_max[p_buf[i]] = y;
395             }
396         }
397     }
398
399     /**
400      * Merge overlaping rectangles
401      */
402     for( i = 1; i < last; i++ )
403     {
404         if( colors[i] != i ) continue;
405         if( color_x_min[i] == -1 ) continue;
406         for( j = i+1; j < last; j++ )
407         {
408             if( colors[j] != j ) continue;
409             if( color_x_min[j] == -1 ) continue;
410 #define max( a, b ) ( a > b ? a : b )
411 #define min( a, b ) ( a < b ? a : b )
412             if( max( color_x_min[i], color_x_min[j] ) < min( color_x_max[i], color_x_max[j] ) && max( color_y_min[i], color_y_min[j] ) < min( color_y_max[i], color_y_max[j] ) )
413             {
414                 color_x_min[i] = min( color_x_min[i], color_x_min[j] );
415                 color_x_max[i] = max( color_x_max[i], color_x_max[j] );
416                 color_y_min[i] = min( color_y_min[i], color_y_min[j] );
417                 color_y_max[i] = max( color_y_max[i], color_y_max[j] );
418                 color_x_min[j] = -1;
419                 j = 0;
420             }
421         }
422     }
423
424     /**
425      * Count final number of shapes
426      * Draw rectangles (there can be more than 1 moving shape in 1 rectangle)
427      */
428     j = 0;
429     for( i = 1; i < last; i++ )
430     {
431         if( colors[i] == i && color_x_min[i] != -1 )
432         {
433             if( ( color_y_max[i] - color_y_min[i] ) * ( color_x_max[i] - color_x_min[i] ) < 16 ) continue;
434             j++;
435             int x, y;
436 #if 1
437             y = color_y_min[i];
438             for( x = color_x_min[i]; x <= color_x_max[i]; x++ )
439             {
440                 p_outpix[y*i_src_pitch+x] = 0xff;
441             }
442             y = color_y_max[i];
443             for( x = color_x_min[i]; x <= color_x_max[i]; x++ )
444             {
445                 p_outpix[y*i_src_pitch+x] = 0xff;
446             }
447             x = color_x_min[i];
448             for( y = color_y_min[i]; y <= color_y_max[i]; y++ )
449             {
450                 p_outpix[y*i_src_pitch+x] = 0xff;
451             }
452             x = color_x_max[i];
453             for( y = color_y_min[i]; y <= color_y_max[i]; y++ )
454             {
455                 p_outpix[y*i_src_pitch+x] = 0xff;
456             }
457 #else
458             for( x = color_x_min[i]; x <= color_x_max[i]; x++ )
459             {
460                 for( y = color_y_min[i]; y <= color_y_max[i]; y++ )
461                 {
462                     p_outpix[y*i_src_pitch+x] = 0x0;
463                         //p_inpix[y*i_src_pitch+x];
464                 }
465             }
466 #endif
467         }
468     }
469     printf("counted %d moving shapes\n", j);
470
471     /**
472      * We're done. Lets keep a copy of the Y plane
473      */
474     p_filter->p_libvlc->pf_memcpy( p_oldpix, p_inpix,
475                                    i_src_pitch * i_num_lines );
476     p_filter->p_libvlc->pf_memcpy( p_oldpix_u, p_inpix_u,
477                                    i_src_pitch_u * i_num_lines_u );
478     p_filter->p_libvlc->pf_memcpy( p_oldpix_v, p_inpix_v,
479                                    i_src_pitch_u * i_num_lines_u );
480
481     vlc_mutex_unlock( &p_filter->p_sys->lock );
482
483     /* misc stuff */
484     p_outpic->date = p_inpic->date;
485     p_outpic->b_force = p_inpic->b_force;
486     p_outpic->i_nb_fields = p_inpic->i_nb_fields;
487     p_outpic->b_progressive = p_inpic->b_progressive;
488     p_outpic->b_top_field_first = p_inpic->b_top_field_first;
489
490     if( p_inpic->pf_release )
491         p_inpic->pf_release( p_inpic );
492
493     return p_outpic;
494 }
495
496
497 /*****************************************************************************
498  * Gaussian Convolution
499  *****************************************************************************
500  *    Gaussian convolution ( sigma == 1.4 )
501  *
502  *    |  2  4  5  4  2  |   |  2  4  4  4  2 |
503  *    |  4  9 12  9  4  |   |  4  8 12  8  4 |
504  *    |  5 12 15 12  5  | ~ |  4 12 16 12  4 |
505  *    |  4  9 12  9  4  |   |  4  8 12  8  4 |
506  *    |  2  4  5  4  2  |   |  2  4  4  4  2 |
507  *****************************************************************************/
508 static void GaussianConvolution( uint32_t *p_inpix, uint32_t *p_smooth,
509                                  int i_src_pitch, int i_num_lines,
510                                  int i_src_visible )
511 {
512 /*    const uint8_t *p_inpix = p_inpic->p[Y_PLANE].p_pixels;
513     const int i_src_pitch = p_inpic->p[Y_PLANE].i_pitch;
514     const int i_src_visible = p_inpic->p[Y_PLANE].i_visible_pitch;
515     const int i_num_lines = p_inpic->p[Y_PLANE].i_visible_lines;*/
516
517     int x,y;
518     for( y = 2; y < i_num_lines - 2; y++ )
519     {
520         for( x = 2; x < i_src_visible - 2; x++ )
521         {
522             p_smooth[y*i_src_visible+x] = (uint32_t)(
523               /* 2 rows up */
524                 ( p_inpix[(y-2)*i_src_pitch+x-2] )
525               + ((p_inpix[(y-2)*i_src_pitch+x-1]
526               +   p_inpix[(y-2)*i_src_pitch+x]
527               +   p_inpix[(y-2)*i_src_pitch+x+1])<<1 )
528               + ( p_inpix[(y-2)*i_src_pitch+x+2] )
529               /* 1 row up */
530               + ((p_inpix[(y-1)*i_src_pitch+x-2]
531               + ( p_inpix[(y-1)*i_src_pitch+x-1]<<1 )
532               + ( p_inpix[(y-1)*i_src_pitch+x]*3 )
533               + ( p_inpix[(y-1)*i_src_pitch+x+1]<<1 )
534               +   p_inpix[(y-1)*i_src_pitch+x+2]
535               /* */
536               +   p_inpix[y*i_src_pitch+x-2]
537               + ( p_inpix[y*i_src_pitch+x-1]*3 )
538               + ( p_inpix[y*i_src_pitch+x]<<2 )
539               + ( p_inpix[y*i_src_pitch+x+1]*3 )
540               +   p_inpix[y*i_src_pitch+x+2]
541               /* 1 row down */
542               +   p_inpix[(y+1)*i_src_pitch+x-2]
543               + ( p_inpix[(y+1)*i_src_pitch+x-1]<<1 )
544               + ( p_inpix[(y+1)*i_src_pitch+x]*3 )
545               + ( p_inpix[(y+1)*i_src_pitch+x+1]<<1 )
546               +   p_inpix[(y+1)*i_src_pitch+x+2] )<<1 )
547               /* 2 rows down */
548               + ( p_inpix[(y+2)*i_src_pitch+x-2] )
549               + ((p_inpix[(y+2)*i_src_pitch+x-1]
550               +   p_inpix[(y+2)*i_src_pitch+x]
551               +   p_inpix[(y+2)*i_src_pitch+x+1])<<1 )
552               + ( p_inpix[(y+2)*i_src_pitch+x+2] )
553               ) >> 6 /* 115 */;
554         }
555     }
556 }