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