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