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