]> git.sesse.net Git - vlc/blob - modules/video_filter/motiondetect.c
remove those two filers from the prefs in 0.8.2 since we don't have
[vlc] / modules / video_filter / motiondetect.c
1 /*****************************************************************************
2  * motiondetect.c : Motion detect video effect plugin for vlc
3  *****************************************************************************
4  * Copyright (C) 2005 VideoLAN
5  * $Id: $
6  *
7  * Authors: Jérôme Decoodt <djc@videolan.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., 59 Temple Place - Suite 330, Boston, MA  02111, 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/vout.h>
32 #include <vlc/intf.h>
33
34 #include "filter_common.h"
35
36 /*****************************************************************************
37  * Local prototypes
38  *****************************************************************************/
39 static int  Create    ( vlc_object_t * );
40 static void Destroy   ( vlc_object_t * );
41
42 static int  Init      ( vout_thread_t * );
43 static void End       ( vout_thread_t * );
44 static void Render    ( vout_thread_t *, picture_t * );
45 static void MotionDetect( vout_thread_t *p_vout, picture_t *p_inpic,
46                                                  picture_t *p_outpic );
47
48 static int  SendEvents   ( vlc_object_t *, char const *,
49                            vlc_value_t, vlc_value_t, void * );
50
51 /*****************************************************************************
52  * Module descriptor
53  *****************************************************************************/
54 #define DESC_TEXT N_("Description file")
55 #define DESC_LONGTEXT N_("Description file, file containing simple playlist")
56 #define HISTORY_TEXT N_("History parameter")
57 #define HISTORY_LONGTEXT N_("History parameter, number of frames used for detection")
58
59 vlc_module_begin();
60     set_description( _("Motion detect video filter") );
61     set_shortname( N_( "Motion detect" ));
62 /*  Leave this commented as long as the VLM intf in wx isn't available
63     set_category( CAT_VIDEO );
64     set_subcategory( SUBCAT_VIDEO_VFILTER );*/
65     set_capability( "video filter", 0 );
66
67     add_integer( "motiondetect-history", 1, NULL, HISTORY_TEXT,
68                                 HISTORY_LONGTEXT, VLC_FALSE );
69     add_string( "motiondetect-description", "motiondetect", NULL, DESC_TEXT,
70                                 DESC_LONGTEXT, VLC_FALSE );
71
72     set_callbacks( Create, Destroy );
73 vlc_module_end();
74
75 /*****************************************************************************
76  * vout_sys_t: Motion detect video output method descriptor
77  *****************************************************************************
78  * This structure is part of the video output thread descriptor.
79  * It describes the Motion detect specific properties of an output thread.
80  *****************************************************************************/
81 typedef struct area_t
82 {
83     int i_x1, i_y1;
84     int i_x2, i_y2;
85     int i_matches;
86     int i_level;
87     int i_downspeed, i_upspeed;
88     char *psz_mrl;
89 } area_t;
90
91 struct vout_sys_t
92 {
93     vout_thread_t *p_vout;
94     playlist_t *p_playlist;
95
96     uint8_t *p_bufferY;
97     int i_stack;
98     area_t** pp_areas;
99     int i_areas;
100     int i_history;
101 };
102
103 /*****************************************************************************
104  * Control: control facility for the vout (forwards to child vout)
105  *****************************************************************************/
106 static int Control( vout_thread_t *p_vout, int i_query, va_list args )
107 {
108     return vout_vaControl( p_vout->p_sys->p_vout, i_query, args );
109 }
110
111 /*****************************************************************************
112  * Create: allocates Distort video thread output method
113  *****************************************************************************
114  * This function allocates and initializes a Distort vout method.
115  *****************************************************************************/
116 static int Create( vlc_object_t *p_this )
117 {
118     vout_thread_t *p_vout = (vout_thread_t *)p_this;
119     char *psz_descfilename;
120     char buffer[256];
121     int x1, x2, y1, y2, i_level, i_downspeed, i_upspeed, i;
122     area_t *p_area;
123     FILE * p_file;
124
125     /* Allocate structure */
126     p_vout->p_sys = malloc( sizeof( vout_sys_t ) );
127     if( p_vout->p_sys == NULL )
128     {
129         msg_Err( p_vout, "out of memory" );
130         return VLC_ENOMEM;
131     }
132
133     p_vout->pf_init = Init;
134     p_vout->pf_end = End;
135     p_vout->pf_manage = NULL;
136     p_vout->pf_render = Render;
137     p_vout->pf_display = NULL;
138     p_vout->pf_control = Control;
139
140     memset( p_vout->p_sys, 0, sizeof( vout_sys_t ) );
141
142     p_vout->p_sys->i_history = config_GetInt( p_vout,
143                                               "motiondetect-history" );
144
145     if( !(psz_descfilename = config_GetPsz( p_vout,
146                                             "motiondetect-description" ) ) )
147     {
148         free( p_vout->p_sys );
149         return VLC_EGENERIC;
150     }
151
152     p_vout->p_sys->p_playlist = vlc_object_find( p_this, VLC_OBJECT_PLAYLIST,
153                                                  FIND_ANYWHERE );
154     if( !p_vout->p_sys->p_playlist )
155     {
156          msg_Err( p_vout, "playlist not found" );
157          free( p_vout->p_sys );
158          return VLC_EGENERIC;
159     }
160
161     /* Parse description file and allocate areas */
162     p_file = fopen( psz_descfilename, "r" );
163     if( !p_file )
164     {
165         msg_Err( p_this, "Failed to open descritpion file %s",
166                             psz_descfilename );
167         free( psz_descfilename );
168         free( p_vout->p_sys );
169         return VLC_EGENERIC;
170     }
171     p_vout->p_sys->i_areas = 0;
172     while( fscanf( p_file, "%d,%d,%d,%d,%d,%d,%d,",
173                             &x1, &y1, &x2, &y2, &i_level,
174                             &i_downspeed, &i_upspeed ) == 7 )
175     {
176         for( i = 0 ; i < 255 ; i++ )
177         {
178             fread( buffer + i, 1, 1, p_file );
179             if( *( buffer + i ) == '\n' )
180                 break;
181         }
182         *( buffer + i )  = 0;
183         p_vout->p_sys->i_areas++;
184         p_vout->p_sys->pp_areas = realloc( p_vout->p_sys->pp_areas,
185                                         p_vout->p_sys->i_areas * 
186                                                     sizeof( area_t ) );
187         if( !p_vout->p_sys->pp_areas )
188             /*FIXME: clean this... */
189             return VLC_ENOMEM;
190         p_area = malloc( sizeof( area_t ) );
191         if( !p_area )
192             break;
193
194         p_area->i_x1 = x1;
195         p_area->i_x2 = x2;
196         p_area->i_y1 = y1;
197         p_area->i_y2 = y2;
198         p_area->i_matches = 0;
199         p_area->i_level = i_level;
200         p_area->i_downspeed = i_downspeed;
201         p_area->i_upspeed = i_upspeed;
202
203         p_area->psz_mrl = strdup(buffer);
204         p_vout->p_sys->pp_areas[p_vout->p_sys->i_areas-1] = p_area;
205     }
206     fclose( p_file );
207     return VLC_SUCCESS;
208 }
209
210 /*****************************************************************************
211  * Init: initialize Motion detect video thread output method
212  *****************************************************************************/
213 static int Init( vout_thread_t *p_vout )
214 {
215     int i_index;
216     picture_t *p_pic;
217     video_format_t fmt = {0};
218
219     I_OUTPUTPICTURES = 0;
220
221     /* Initialize the output structure */
222     p_vout->output.i_chroma = p_vout->render.i_chroma;
223     p_vout->output.i_width  = p_vout->render.i_width;
224     p_vout->output.i_height = p_vout->render.i_height;
225     p_vout->output.i_aspect = p_vout->render.i_aspect;
226
227     fmt.i_width = fmt.i_visible_width = p_vout->render.i_width;
228     fmt.i_height = fmt.i_visible_height = p_vout->render.i_height;
229     fmt.i_x_offset = fmt.i_y_offset = 0;
230     fmt.i_chroma = p_vout->render.i_chroma;
231     fmt.i_aspect = p_vout->render.i_aspect;
232     fmt.i_sar_num = p_vout->render.i_aspect * fmt.i_height / fmt.i_width;
233     fmt.i_sar_den = VOUT_ASPECT_FACTOR;
234
235     /* Try to open the real video output */
236     msg_Dbg( p_vout, "spawning the real video output" );
237
238     p_vout->p_sys->p_vout = vout_Create( p_vout, &fmt );
239
240     /* Everything failed */
241     if( p_vout->p_sys->p_vout == NULL )
242     {
243         msg_Err( p_vout, "cannot open vout, aborting" );
244         return VLC_EGENERIC;
245     }
246
247     ALLOCATE_DIRECTBUFFERS( VOUT_MAX_PICTURES );
248
249     ADD_CALLBACKS( p_vout->p_sys->p_vout, SendEvents );
250
251     ADD_PARENT_CALLBACKS( SendEventsToChild );
252
253     return VLC_SUCCESS;
254 }
255
256 /*****************************************************************************
257  * End: terminate Motion detect video thread output method
258  *****************************************************************************/
259 static void End( vout_thread_t *p_vout )
260 {
261     int i_index;
262
263     /* Free the fake output buffers we allocated */
264     for( i_index = I_OUTPUTPICTURES ; i_index ; )
265     {
266         i_index--;
267         free( PP_OUTPUTPICTURE[ i_index ]->p_data_orig );
268     }
269 }
270
271 /*****************************************************************************
272  * Destroy: destroy Motion detect video thread output method
273  *****************************************************************************
274  * Terminate an output method created by DistortCreateOutputMethod
275  *****************************************************************************/
276 static void Destroy( vlc_object_t *p_this )
277 {
278     vout_thread_t *p_vout = (vout_thread_t *)p_this;
279     int i;
280
281     if( p_vout->p_sys->p_vout )
282     {
283         DEL_CALLBACKS( p_vout->p_sys->p_vout, SendEvents );
284         vlc_object_detach( p_vout->p_sys->p_vout );
285         vout_Destroy( p_vout->p_sys->p_vout );
286     }
287
288     DEL_PARENT_CALLBACKS( SendEventsToChild );
289
290     for( i = 0 ; i < p_vout->p_sys->i_areas ; i++ )
291     {
292         free( p_vout->p_sys->pp_areas[i]->psz_mrl );
293         free( p_vout->p_sys->pp_areas[i] );
294     }
295
296     free( p_vout->p_sys->pp_areas );
297     free( p_vout->p_sys );
298 }
299
300 /*****************************************************************************
301  * Render: displays previously rendered output
302  *****************************************************************************
303  * This function send the currently rendered image to Distort image, waits
304  * until it is displayed and switch the two rendering buffers, preparing next
305  * frame.
306  *****************************************************************************/
307 static void Render( vout_thread_t *p_vout, picture_t *p_pic )
308 {
309     picture_t *p_outpic;
310
311     /* This is a new frame. Get a structure from the video_output. */
312     while( ( p_outpic = vout_CreatePicture( p_vout->p_sys->p_vout, 0, 0, 0 ) )
313               == NULL )
314     {
315         if( p_vout->b_die || p_vout->b_error )
316         {
317             return;
318         }
319         msleep( VOUT_OUTMEM_SLEEP );
320     }
321
322     vout_DatePicture( p_vout->p_sys->p_vout, p_outpic, p_pic->date );
323
324     MotionDetect( p_vout, p_pic, p_outpic );
325
326     vout_DisplayPicture( p_vout->p_sys->p_vout, p_outpic );
327 }
328
329 /*****************************************************************************
330  * MotionDetect: calculates new matches
331  *****************************************************************************/
332 static void MotionDetect( vout_thread_t *p_vout, picture_t *p_inpic,
333                                                  picture_t *p_outpic )
334 {
335 #define pp_curent_area p_vout->p_sys->pp_areas[i_area]
336
337     int i_index, i_index_col, i_area;
338     for( i_index = 0 ; i_index < p_inpic->i_planes ; i_index++ )
339     {
340         int i_line, i_num_lines, i_offset, i_size, i_diff;
341         uint8_t *p_in, *p_out, *p_last_in, *p_buffer;
342
343         p_in = p_inpic->p[i_index].p_pixels;
344         p_out = p_outpic->p[i_index].p_pixels;
345
346         i_num_lines = p_inpic->p[i_index].i_visible_lines;
347         i_size = p_inpic->p[i_index].i_lines * p_inpic->p[i_index].i_pitch;
348
349         p_vout->p_vlc->pf_memcpy( p_out, p_in, i_size );
350         switch( i_index )
351         {
352         case Y_PLANE:
353             p_buffer = p_vout->p_sys->p_bufferY;
354
355             if( p_buffer == NULL)
356             {
357                 p_buffer = malloc( p_vout->p_sys->i_history * i_size);
358                 memset( p_buffer, 0, p_vout->p_sys->i_history * i_size );
359
360                 p_vout->p_sys->p_bufferY = p_buffer;
361                 p_vout->p_sys->i_stack = 0;
362             }
363
364             i_offset = i_size * p_vout->p_sys->i_stack;
365             p_last_in = p_buffer + i_offset;
366             for( i_area = 0 ; i_area < p_vout->p_sys->i_areas ; i_area++)
367             {
368                 int i_tmp = 0, i_nb_pixels;
369                 p_last_in = p_buffer + i_offset;
370                 p_in = p_inpic->p[i_index].p_pixels;
371                 p_out = p_outpic->p[i_index].p_pixels;
372                 if( ( pp_curent_area->i_y1 > p_inpic->p[i_index].i_lines ) ||
373                     ( pp_curent_area->i_x1 > p_inpic->p[i_index].i_pitch ) )
374                     continue;
375                 if( ( pp_curent_area->i_y2 > p_inpic->p[i_index].i_lines ) )
376                     pp_curent_area->i_y2 = p_inpic->p[i_index].i_lines;
377                 if( ( pp_curent_area->i_x2 > p_inpic->p[i_index].i_pitch ) )
378                     pp_curent_area->i_x2 = p_inpic->p[i_index].i_pitch;
379
380                 for( i_line = pp_curent_area->i_y1 ;
381                      i_line < pp_curent_area->i_y2 ; i_line++ )
382                 {
383                     for( i_index_col = pp_curent_area->i_x1 ;
384                          i_index_col < pp_curent_area->i_x2 ; i_index_col++ )
385                     {
386                         i_diff = (*(p_last_in + i_index_col +
387                                         i_line * p_inpic->p[i_index].i_pitch)
388                                 - *(p_in      + i_index_col +
389                                         i_line * p_inpic->p[i_index].i_pitch));
390                         if( i_diff < 0)
391                             i_diff = -i_diff;
392
393                         if( i_diff > pp_curent_area->i_level )
394                             i_tmp += pp_curent_area->i_upspeed;
395
396                         *( p_out + i_index_col + i_line *
397                                         p_inpic->p[i_index].i_pitch ) =
398                                                 pp_curent_area->i_matches;
399                     }
400                 }
401                 i_nb_pixels = ( pp_curent_area->i_y2 - pp_curent_area->i_y1 ) *
402                               ( pp_curent_area->i_x2 - pp_curent_area->i_x1 );
403                 pp_curent_area->i_matches += i_tmp / i_nb_pixels -
404                                     pp_curent_area->i_downspeed;
405                 if( pp_curent_area->i_matches < 0)
406                     pp_curent_area->i_matches = 0;
407                 if( pp_curent_area->i_matches > 255)
408                 {
409                     playlist_item_t *p_item = playlist_ItemNew( p_vout,
410                                         (const char*)pp_curent_area->psz_mrl,
411                                         pp_curent_area->psz_mrl );
412                     msg_Dbg( p_vout, "Area(%d) matched, going to %s\n", i_area,
413                                         pp_curent_area->psz_mrl );
414                     playlist_Control( p_vout->p_sys->p_playlist,
415                                         PLAYLIST_ITEMPLAY, p_item );
416                     pp_curent_area->i_matches = 0;
417                 }
418             }
419             p_last_in = p_buffer + i_offset;
420             p_in = p_inpic->p[i_index].p_pixels;
421             p_out = p_outpic->p[i_index].p_pixels;
422
423             p_vout->p_vlc->pf_memcpy( p_last_in, p_in, i_size );
424             break;
425         default:
426             break;
427         }
428     }
429     p_vout->p_sys->i_stack++;
430     if( p_vout->p_sys->i_stack >= p_vout->p_sys->i_history )
431         p_vout->p_sys->i_stack = 0;
432 #undef pp_curent_area
433 }
434
435 /*****************************************************************************
436  * SendEvents: forward mouse and keyboard events to the parent p_vout
437  *****************************************************************************/
438 static int SendEvents( vlc_object_t *p_this, char const *psz_var,
439                        vlc_value_t oldval, vlc_value_t newval, void *p_data )
440 {
441     var_Set( (vlc_object_t *)p_data, psz_var, newval );
442
443     return VLC_SUCCESS;
444 }
445
446 /*****************************************************************************
447  * SendEventsToChild: forward events to the child/children vout
448  *****************************************************************************/
449 static int SendEventsToChild( vlc_object_t *p_this, char const *psz_var,
450                        vlc_value_t oldval, vlc_value_t newval, void *p_data )
451 {
452     vout_thread_t *p_vout = (vout_thread_t *)p_this;
453     var_Set( p_vout->p_sys->p_vout, psz_var, newval );
454     return VLC_SUCCESS;
455 }