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