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