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