]> git.sesse.net Git - vlc/blob - modules/video_filter/motiondetect.c
Don't reinvent max/min for the nth time.
[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
28 #ifdef HAVE_CONFIG_H
29 # include "config.h"
30 #endif
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_num_lines_u = p_inpic->p[U_PLANE].i_visible_lines;
141
142     uint8_t *p_oldpix;
143     uint8_t *p_oldpix_u;
144     uint8_t *p_oldpix_v;
145     uint8_t *p_outpix;
146     uint32_t *p_buf;
147     uint32_t *p_buf2;
148
149     int i,j;
150     int last;
151
152     if( !p_inpic ) return NULL;
153
154     p_outpic = p_filter->pf_vout_buffer_new( p_filter );
155     if( !p_outpic )
156     {
157         msg_Warn( p_filter, "can't get output picture" );
158         if( p_inpic->pf_release )
159             p_inpic->pf_release( p_inpic );
160         return NULL;
161     }
162
163     p_outpix = p_outpic->p[Y_PLANE].p_pixels;
164     p_filter->p_libvlc->pf_memcpy( p_outpic->p[U_PLANE].p_pixels,
165                                    p_inpic->p[U_PLANE].p_pixels,
166         p_inpic->p[U_PLANE].i_pitch * p_inpic->p[U_PLANE].i_visible_lines );
167     p_filter->p_libvlc->pf_memcpy( p_outpic->p[V_PLANE].p_pixels,
168                                    p_inpic->p[V_PLANE].p_pixels,
169         p_inpic->p[V_PLANE].i_pitch * p_inpic->p[V_PLANE].i_visible_lines );
170
171     if( !p_sys->p_oldpix || !p_sys->p_buf )
172     {
173         free( p_sys->p_oldpix );
174         free( p_sys->p_buf );
175         p_sys->p_oldpix = malloc( i_src_pitch * i_num_lines );
176         p_sys->p_oldpix_u = malloc( i_src_pitch_u * i_num_lines_u );
177         p_sys->p_oldpix_v = malloc( i_src_pitch_u * i_num_lines_u );
178         p_sys->p_buf = malloc( sizeof( uint32_t ) * i_src_pitch * i_num_lines );
179         p_sys->p_buf2 = malloc( sizeof( uint32_t ) * i_src_pitch * i_num_lines);
180         return p_inpic;
181     }
182     p_oldpix = p_sys->p_oldpix;
183     p_oldpix_u = p_sys->p_oldpix_u;
184     p_oldpix_v = p_sys->p_oldpix_v;
185     p_buf = p_sys->p_buf;
186     p_buf2 = p_sys->p_buf2;
187
188     vlc_mutex_lock( &p_filter->p_sys->lock );
189
190     /**
191      * Substract Y planes
192      */
193     for( i = 0; i < i_src_pitch * i_num_lines; i++ )
194     {
195         if( p_inpix[i] > p_oldpix[i] )
196         {
197             p_buf2[i] = p_inpix[i] - p_oldpix[i];
198         }
199         else
200         {
201             p_buf2[i] = p_oldpix[i] - p_inpix[i];
202         }
203     }
204     int line;
205     int col;
206     int format;
207     switch( p_inpic->format.i_chroma )
208     {
209         case VLC_FOURCC('I','4','2','0'):
210         case VLC_FOURCC('I','Y','U','V'):
211         case VLC_FOURCC('J','4','2','0'):
212         case VLC_FOURCC('Y','V','1','2'):
213             format = 1;
214             break;
215
216         case VLC_FOURCC('I','4','2','2'):
217         case VLC_FOURCC('J','4','2','2'):
218             format = 2;
219             break;
220
221         default:
222             format = 0;
223             msg_Warn( p_filter, "Not taking chroma into account" );
224             break;
225     }
226
227     //format = 0;
228     if( format )
229     {
230         for( line = 0; line < i_num_lines_u; line++ )
231         {
232             for( col = 0; col < i_src_pitch_u; col ++ )
233             {
234                 int diff;
235                 i = line * i_src_pitch_u + col;
236                 if( p_inpix_u[i] > p_oldpix_u[i] )
237                 {
238                     diff = p_inpix_u[i] - p_oldpix_u[i];
239                 }
240                 else
241                 {
242                     diff = p_oldpix_u[i] - p_inpix_u[i];
243                 }
244                 if( p_inpix_v[i] > p_oldpix_v[i] )
245                 {
246                     diff += p_inpix_v[i] - p_oldpix_v[i];
247                 }
248                 else
249                 {
250                     diff += p_oldpix_v[i] - p_inpix_v[i];
251                 }
252                 switch( format )
253                 {
254                     case 1:
255                         p_buf2[2*line*i_src_pitch+2*col] += diff;
256                         p_buf2[2*line*i_src_pitch+2*col+1] += diff;
257                         p_buf2[(2*line+1)*i_src_pitch+2*col] += diff;
258                         p_buf2[(2*line+1)*i_src_pitch+2*col+1] += diff;
259                         break;
260
261                     case 2:
262                         p_buf2[line*i_src_pitch+2*col] += diff;
263                         p_buf2[line*i_src_pitch+2*col+1] += diff;
264                         break;
265                 }
266             }
267         }
268     }
269
270     /**
271      * Apply some smoothing to remove noise
272      */
273     GaussianConvolution( p_buf2, p_buf, i_src_pitch, i_num_lines, i_src_visible );
274
275     /**
276      * Copy luminance plane
277      */
278     for( i = 0; i < i_src_pitch * i_num_lines; i++ )
279     {
280         p_outpix[i] = p_inpix[i];
281     }
282
283     /**
284      * Label the shapes ans build the labels dependencies list
285      */
286     last = 1;
287     int colors[5000];
288     int color_x_min[5000];
289     int color_x_max[5000];
290     int color_y_min[5000];
291     int color_y_max[5000];
292
293     for( j = 0; j < i_src_pitch; j++ )
294     {
295         p_buf[j] = 0;
296         p_buf[(i_num_lines-1)*i_src_pitch+j] = 0;
297     }
298     for( i = 1; i < i_num_lines-1; i++ )
299     {
300         p_buf[i*i_src_pitch] = 0;
301         for( j = 1; j < i_src_pitch-1; j++ )
302         {
303             if( p_buf[i*i_src_pitch+j] > 15 )
304             {
305                 if( p_buf[(i-1)*i_src_pitch+j-1] )
306                 {
307                     p_buf[i*i_src_pitch+j] = p_buf[(i-1)*i_src_pitch+j-1];
308                 }
309                 else if( p_buf[(i-1)*i_src_pitch+j] )
310                     p_buf[i*i_src_pitch+j] = p_buf[(i-1)*i_src_pitch+j];
311                 else if( p_buf[i*i_src_pitch+j-1] )
312                     p_buf[i*i_src_pitch+j] = p_buf[i*i_src_pitch+j-1];
313                 else
314                 {
315                     p_buf[i*i_src_pitch+j] = last;
316                     colors[last] = last;
317                     last++;
318                 }
319                 #define CHECK( A ) \
320                 if( p_buf[A] && p_buf[A] != p_buf[i*i_src_pitch+j] ) \
321                 { \
322                     if( p_buf[A] < p_buf[i*i_src_pitch+j] ) \
323                         colors[p_buf[i*i_src_pitch+j]] = p_buf[A]; \
324                     else \
325                         colors[p_buf[A]] = p_buf[i*i_src_pitch+j]; \
326                 }
327                 CHECK( i*i_src_pitch+j-1 );
328                 CHECK( (i-1)*i_src_pitch+j-1 );
329                 CHECK( (i-1)*i_src_pitch+j );
330                 CHECK( (i-1)*i_src_pitch+j+1 );
331             }
332             else
333             {
334                 p_buf[i*i_src_pitch+j] = 0;
335             }
336         }
337         p_buf[i*i_src_pitch+j] = 0;
338     }
339
340     /**
341      * Initialise empty rectangle list
342      */
343     for( i = 1; i < last; i++ )
344     {
345         color_x_min[i] = -1;
346         color_x_max[i] = -1;
347         color_y_min[i] = -1;
348         color_y_max[i] = -1;
349     }
350
351     /**
352      * Compute rectangle coordinates
353      */
354     for( i = 0; i < i_src_pitch * i_num_lines; i++ )
355     {
356         if( p_buf[i] )
357         {
358             while( colors[p_buf[i]] != p_buf[i] )
359                 p_buf[i] = colors[p_buf[i]];
360             if( color_x_min[p_buf[i]] == -1 )
361             {
362                 color_x_min[p_buf[i]] =
363                 color_x_max[p_buf[i]] = i % i_src_pitch;
364                 color_y_min[p_buf[i]] =
365                 color_y_max[p_buf[i]] = i / i_src_pitch;
366             }
367             else
368             {
369                 int x = i % i_src_pitch, y = i / i_src_pitch;
370                 if( x < color_x_min[p_buf[i]] )
371                     color_x_min[p_buf[i]] = x;
372                 if( x > color_x_max[p_buf[i]] )
373                     color_x_max[p_buf[i]] = x;
374                 if( y < color_y_min[p_buf[i]] )
375                     color_y_min[p_buf[i]] = y;
376                 if( y > color_y_max[p_buf[i]] )
377                     color_y_max[p_buf[i]] = y;
378             }
379         }
380     }
381
382     /**
383      * Merge overlaping rectangles
384      */
385     for( i = 1; i < last; i++ )
386     {
387         if( colors[i] != i ) continue;
388         if( color_x_min[i] == -1 ) continue;
389         for( j = i+1; j < last; j++ )
390         {
391             if( colors[j] != j ) continue;
392             if( color_x_min[j] == -1 ) continue;
393             if( __MAX( color_x_min[i], color_x_min[j] ) < __MIN( color_x_max[i], color_x_max[j] ) &&
394                 __MAX( color_y_min[i], color_y_min[j] ) < __MIN( color_y_max[i], color_y_max[j] ) )
395             {
396                 color_x_min[i] = __MIN( color_x_min[i], color_x_min[j] );
397                 color_x_max[i] = __MAX( color_x_max[i], color_x_max[j] );
398                 color_y_min[i] = __MIN( color_y_min[i], color_y_min[j] );
399                 color_y_max[i] = __MAX( color_y_max[i], color_y_max[j] );
400                 color_x_min[j] = -1;
401                 j = 0;
402             }
403         }
404     }
405
406     /**
407      * Count final number of shapes
408      * Draw rectangles (there can be more than 1 moving shape in 1 rectangle)
409      */
410     j = 0;
411     for( i = 1; i < last; i++ )
412     {
413         if( colors[i] == i && color_x_min[i] != -1 )
414         {
415             if( ( color_y_max[i] - color_y_min[i] ) * ( color_x_max[i] - color_x_min[i] ) < 16 ) continue;
416             j++;
417             int x, y;
418             y = color_y_min[i];
419             for( x = color_x_min[i]; x <= color_x_max[i]; x++ )
420             {
421                 p_outpix[y*i_src_pitch+x] = 0xff;
422             }
423             y = color_y_max[i];
424             for( x = color_x_min[i]; x <= color_x_max[i]; x++ )
425             {
426                 p_outpix[y*i_src_pitch+x] = 0xff;
427             }
428             x = color_x_min[i];
429             for( y = color_y_min[i]; y <= color_y_max[i]; y++ )
430             {
431                 p_outpix[y*i_src_pitch+x] = 0xff;
432             }
433             x = color_x_max[i];
434             for( y = color_y_min[i]; y <= color_y_max[i]; y++ )
435             {
436                 p_outpix[y*i_src_pitch+x] = 0xff;
437             }
438         }
439     }
440     msg_Dbg( p_filter, "Counted %d moving shapes.", j);
441
442     /**
443      * We're done. Lets keep a copy of the picture
444      */
445     p_filter->p_libvlc->pf_memcpy( p_oldpix, p_inpix,
446                                    i_src_pitch * i_num_lines );
447     p_filter->p_libvlc->pf_memcpy( p_oldpix_u, p_inpix_u,
448                                    i_src_pitch_u * i_num_lines_u );
449     p_filter->p_libvlc->pf_memcpy( p_oldpix_v, p_inpix_v,
450                                    i_src_pitch_u * i_num_lines_u );
451
452     vlc_mutex_unlock( &p_filter->p_sys->lock );
453
454     /* misc stuff */
455     p_outpic->date = p_inpic->date;
456     p_outpic->b_force = p_inpic->b_force;
457     p_outpic->i_nb_fields = p_inpic->i_nb_fields;
458     p_outpic->b_progressive = p_inpic->b_progressive;
459     p_outpic->b_top_field_first = p_inpic->b_top_field_first;
460
461     if( p_inpic->pf_release )
462         p_inpic->pf_release( p_inpic );
463
464     return p_outpic;
465 }
466
467
468 /*****************************************************************************
469  * Gaussian Convolution
470  *****************************************************************************
471  *    Gaussian convolution ( sigma == 1.4 )
472  *
473  *    |  2  4  5  4  2  |   |  2  4  4  4  2 |
474  *    |  4  9 12  9  4  |   |  4  8 12  8  4 |
475  *    |  5 12 15 12  5  | ~ |  4 12 16 12  4 |
476  *    |  4  9 12  9  4  |   |  4  8 12  8  4 |
477  *    |  2  4  5  4  2  |   |  2  4  4  4  2 |
478  *****************************************************************************/
479 static void GaussianConvolution( uint32_t *p_inpix, uint32_t *p_smooth,
480                                  int i_src_pitch, int i_num_lines,
481                                  int i_src_visible )
482 {
483 /*    const uint8_t *p_inpix = p_inpic->p[Y_PLANE].p_pixels;
484     const int i_src_pitch = p_inpic->p[Y_PLANE].i_pitch;
485     const int i_src_visible = p_inpic->p[Y_PLANE].i_visible_pitch;
486     const int i_num_lines = p_inpic->p[Y_PLANE].i_visible_lines;*/
487
488     int x,y;
489     for( y = 2; y < i_num_lines - 2; y++ )
490     {
491         for( x = 2; x < i_src_visible - 2; x++ )
492         {
493             p_smooth[y*i_src_visible+x] = (uint32_t)(
494               /* 2 rows up */
495                 ( p_inpix[(y-2)*i_src_pitch+x-2] )
496               + ((p_inpix[(y-2)*i_src_pitch+x-1]
497               +   p_inpix[(y-2)*i_src_pitch+x]
498               +   p_inpix[(y-2)*i_src_pitch+x+1])<<1 )
499               + ( p_inpix[(y-2)*i_src_pitch+x+2] )
500               /* 1 row up */
501               + ((p_inpix[(y-1)*i_src_pitch+x-2]
502               + ( p_inpix[(y-1)*i_src_pitch+x-1]<<1 )
503               + ( p_inpix[(y-1)*i_src_pitch+x]*3 )
504               + ( p_inpix[(y-1)*i_src_pitch+x+1]<<1 )
505               +   p_inpix[(y-1)*i_src_pitch+x+2]
506               /* */
507               +   p_inpix[y*i_src_pitch+x-2]
508               + ( p_inpix[y*i_src_pitch+x-1]*3 )
509               + ( p_inpix[y*i_src_pitch+x]<<2 )
510               + ( p_inpix[y*i_src_pitch+x+1]*3 )
511               +   p_inpix[y*i_src_pitch+x+2]
512               /* 1 row down */
513               +   p_inpix[(y+1)*i_src_pitch+x-2]
514               + ( p_inpix[(y+1)*i_src_pitch+x-1]<<1 )
515               + ( p_inpix[(y+1)*i_src_pitch+x]*3 )
516               + ( p_inpix[(y+1)*i_src_pitch+x+1]<<1 )
517               +   p_inpix[(y+1)*i_src_pitch+x+2] )<<1 )
518               /* 2 rows down */
519               + ( p_inpix[(y+2)*i_src_pitch+x-2] )
520               + ((p_inpix[(y+2)*i_src_pitch+x-1]
521               +   p_inpix[(y+2)*i_src_pitch+x]
522               +   p_inpix[(y+2)*i_src_pitch+x+1])<<1 )
523               + ( p_inpix[(y+2)*i_src_pitch+x+2] )
524               ) >> 6 /* 115 */;
525         }
526     }
527 }