]> git.sesse.net Git - vlc/blob - modules/video_filter/motiondetect.c
34bd4cff947bd25ec705b226450ab6e180789822
[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     video_format_t fmt = {0};
215
216     I_OUTPUTPICTURES = 0;
217
218     /* Initialize the output structure */
219     p_vout->output.i_chroma = p_vout->render.i_chroma;
220     p_vout->output.i_width  = p_vout->render.i_width;
221     p_vout->output.i_height = p_vout->render.i_height;
222     p_vout->output.i_aspect = p_vout->render.i_aspect;
223
224     fmt.i_width = fmt.i_visible_width = p_vout->render.i_width;
225     fmt.i_height = fmt.i_visible_height = p_vout->render.i_height;
226     fmt.i_x_offset = fmt.i_y_offset = 0;
227     fmt.i_chroma = p_vout->render.i_chroma;
228     fmt.i_aspect = p_vout->render.i_aspect;
229     fmt.i_sar_num = p_vout->render.i_aspect * fmt.i_height / fmt.i_width;
230     fmt.i_sar_den = VOUT_ASPECT_FACTOR;
231
232     /* Try to open the real video output */
233     msg_Dbg( p_vout, "spawning the real video output" );
234
235     p_vout->p_sys->p_vout = vout_Create( p_vout, &fmt );
236
237     /* Everything failed */
238     if( p_vout->p_sys->p_vout == NULL )
239     {
240         msg_Err( p_vout, "cannot open vout, aborting" );
241         return VLC_EGENERIC;
242     }
243
244     ALLOCATE_DIRECTBUFFERS( VOUT_MAX_PICTURES );
245
246     ADD_CALLBACKS( p_vout->p_sys->p_vout, SendEvents );
247
248     ADD_PARENT_CALLBACKS( SendEventsToChild );
249
250     return VLC_SUCCESS;
251 }
252
253 /*****************************************************************************
254  * End: terminate Motion detect video thread output method
255  *****************************************************************************/
256 static void End( vout_thread_t *p_vout )
257 {
258     int i_index;
259
260     /* Free the fake output buffers we allocated */
261     for( i_index = I_OUTPUTPICTURES ; i_index ; )
262     {
263         i_index--;
264         free( PP_OUTPUTPICTURE[ i_index ]->p_data_orig );
265     }
266 }
267
268 /*****************************************************************************
269  * Destroy: destroy Motion detect video thread output method
270  *****************************************************************************
271  * Terminate an output method created by DistortCreateOutputMethod
272  *****************************************************************************/
273 static void Destroy( vlc_object_t *p_this )
274 {
275     vout_thread_t *p_vout = (vout_thread_t *)p_this;
276     int i;
277
278     if( p_vout->p_sys->p_vout )
279     {
280         DEL_CALLBACKS( p_vout->p_sys->p_vout, SendEvents );
281         vlc_object_detach( p_vout->p_sys->p_vout );
282         vout_Destroy( p_vout->p_sys->p_vout );
283     }
284
285     DEL_PARENT_CALLBACKS( SendEventsToChild );
286
287     for( i = 0 ; i < p_vout->p_sys->i_areas ; i++ )
288     {
289         free( p_vout->p_sys->pp_areas[i]->psz_mrl );
290         free( p_vout->p_sys->pp_areas[i] );
291     }
292
293     free( p_vout->p_sys->pp_areas );
294     free( p_vout->p_sys );
295 }
296
297 /*****************************************************************************
298  * Render: displays previously rendered output
299  *****************************************************************************
300  * This function send the currently rendered image to Distort image, waits
301  * until it is displayed and switch the two rendering buffers, preparing next
302  * frame.
303  *****************************************************************************/
304 static void Render( vout_thread_t *p_vout, picture_t *p_pic )
305 {
306     picture_t *p_outpic;
307
308     /* This is a new frame. Get a structure from the video_output. */
309     while( ( p_outpic = vout_CreatePicture( p_vout->p_sys->p_vout, 0, 0, 0 ) )
310               == NULL )
311     {
312         if( p_vout->b_die || p_vout->b_error )
313         {
314             return;
315         }
316         msleep( VOUT_OUTMEM_SLEEP );
317     }
318
319     vout_DatePicture( p_vout->p_sys->p_vout, p_outpic, p_pic->date );
320
321     MotionDetect( p_vout, p_pic, p_outpic );
322
323     vout_DisplayPicture( p_vout->p_sys->p_vout, p_outpic );
324 }
325
326 /*****************************************************************************
327  * MotionDetect: calculates new matches
328  *****************************************************************************/
329 static void MotionDetect( vout_thread_t *p_vout, picture_t *p_inpic,
330                                                  picture_t *p_outpic )
331 {
332 #define pp_curent_area p_vout->p_sys->pp_areas[i_area]
333
334     int i_index, i_index_col, i_area;
335     for( i_index = 0 ; i_index < p_inpic->i_planes ; i_index++ )
336     {
337         int i_line, i_num_lines, i_offset, i_size, i_diff;
338         uint8_t *p_in, *p_out, *p_last_in, *p_buffer;
339
340         p_in = p_inpic->p[i_index].p_pixels;
341         p_out = p_outpic->p[i_index].p_pixels;
342
343         i_num_lines = p_inpic->p[i_index].i_visible_lines;
344         i_size = p_inpic->p[i_index].i_lines * p_inpic->p[i_index].i_pitch;
345
346         p_vout->p_vlc->pf_memcpy( p_out, p_in, i_size );
347         switch( i_index )
348         {
349         case Y_PLANE:
350             p_buffer = p_vout->p_sys->p_bufferY;
351
352             if( p_buffer == NULL)
353             {
354                 p_buffer = malloc( p_vout->p_sys->i_history * i_size);
355                 memset( p_buffer, 0, p_vout->p_sys->i_history * i_size );
356
357                 p_vout->p_sys->p_bufferY = p_buffer;
358                 p_vout->p_sys->i_stack = 0;
359             }
360
361             i_offset = i_size * p_vout->p_sys->i_stack;
362             p_last_in = p_buffer + i_offset;
363             for( i_area = 0 ; i_area < p_vout->p_sys->i_areas ; i_area++)
364             {
365                 int i_tmp = 0, i_nb_pixels;
366                 p_last_in = p_buffer + i_offset;
367                 p_in = p_inpic->p[i_index].p_pixels;
368                 p_out = p_outpic->p[i_index].p_pixels;
369                 if( ( pp_curent_area->i_y1 > p_inpic->p[i_index].i_lines ) ||
370                     ( pp_curent_area->i_x1 > p_inpic->p[i_index].i_pitch ) )
371                     continue;
372                 if( ( pp_curent_area->i_y2 > p_inpic->p[i_index].i_lines ) )
373                     pp_curent_area->i_y2 = p_inpic->p[i_index].i_lines;
374                 if( ( pp_curent_area->i_x2 > p_inpic->p[i_index].i_pitch ) )
375                     pp_curent_area->i_x2 = p_inpic->p[i_index].i_pitch;
376
377                 for( i_line = pp_curent_area->i_y1 ;
378                      i_line < pp_curent_area->i_y2 ; i_line++ )
379                 {
380                     for( i_index_col = pp_curent_area->i_x1 ;
381                          i_index_col < pp_curent_area->i_x2 ; i_index_col++ )
382                     {
383                         i_diff = (*(p_last_in + i_index_col +
384                                         i_line * p_inpic->p[i_index].i_pitch)
385                                 - *(p_in      + i_index_col +
386                                         i_line * p_inpic->p[i_index].i_pitch));
387                         if( i_diff < 0)
388                             i_diff = -i_diff;
389
390                         if( i_diff > pp_curent_area->i_level )
391                             i_tmp += pp_curent_area->i_upspeed;
392
393                         *( p_out + i_index_col + i_line *
394                                         p_inpic->p[i_index].i_pitch ) =
395                                                 pp_curent_area->i_matches;
396                     }
397                 }
398                 i_nb_pixels = ( pp_curent_area->i_y2 - pp_curent_area->i_y1 ) *
399                               ( pp_curent_area->i_x2 - pp_curent_area->i_x1 );
400                 pp_curent_area->i_matches += i_tmp / i_nb_pixels -
401                                     pp_curent_area->i_downspeed;
402                 if( pp_curent_area->i_matches < 0)
403                     pp_curent_area->i_matches = 0;
404                 if( pp_curent_area->i_matches > 255)
405                 {
406                     playlist_item_t *p_item = playlist_ItemNew( p_vout,
407                                         (const char*)pp_curent_area->psz_mrl,
408                                         pp_curent_area->psz_mrl );
409                     msg_Dbg( p_vout, "Area(%d) matched, going to %s\n", i_area,
410                                         pp_curent_area->psz_mrl );
411                     playlist_Control( p_vout->p_sys->p_playlist,
412                                         PLAYLIST_ITEMPLAY, p_item );
413                     pp_curent_area->i_matches = 0;
414                 }
415             }
416             p_last_in = p_buffer + i_offset;
417             p_in = p_inpic->p[i_index].p_pixels;
418             p_out = p_outpic->p[i_index].p_pixels;
419
420             p_vout->p_vlc->pf_memcpy( p_last_in, p_in, i_size );
421             break;
422         default:
423             break;
424         }
425     }
426     p_vout->p_sys->i_stack++;
427     if( p_vout->p_sys->i_stack >= p_vout->p_sys->i_history )
428         p_vout->p_sys->i_stack = 0;
429 #undef pp_curent_area
430 }
431
432 /*****************************************************************************
433  * SendEvents: forward mouse and keyboard events to the parent p_vout
434  *****************************************************************************/
435 static int SendEvents( vlc_object_t *p_this, char const *psz_var,
436                        vlc_value_t oldval, vlc_value_t newval, void *p_data )
437 {
438     var_Set( (vlc_object_t *)p_data, psz_var, newval );
439
440     return VLC_SUCCESS;
441 }
442
443 /*****************************************************************************
444  * SendEventsToChild: forward events to the child/children vout
445  *****************************************************************************/
446 static int SendEventsToChild( vlc_object_t *p_this, char const *psz_var,
447                        vlc_value_t oldval, vlc_value_t newval, void *p_data )
448 {
449     vout_thread_t *p_vout = (vout_thread_t *)p_this;
450     var_Set( p_vout->p_sys->p_vout, psz_var, newval );
451     return VLC_SUCCESS;
452 }