1 /*****************************************************************************
2 * motiondetect.c : Motion detect video effect plugin for vlc
3 *****************************************************************************
4 * Copyright (C) 2005 VideoLAN
7 * Authors: Jérôme Decoodt <djc@videolan.org>
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.
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.
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 *****************************************************************************/
24 /*****************************************************************************
26 *****************************************************************************/
27 #include <stdlib.h> /* malloc(), free() */
34 #include "filter_common.h"
36 /*****************************************************************************
38 *****************************************************************************/
39 static int Create ( vlc_object_t * );
40 static void Destroy ( vlc_object_t * );
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 );
48 static int SendEvents ( vlc_object_t *, char const *,
49 vlc_value_t, vlc_value_t, void * );
51 /*****************************************************************************
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")
60 set_description( _("Motion detect video filter") );
61 set_shortname( N_( "Motion detect" ));
62 set_capability( "video filter", 0 );
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 );
69 set_callbacks( Create, Destroy );
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 *****************************************************************************/
84 int i_downspeed, i_upspeed;
90 vout_thread_t *p_vout;
91 playlist_t *p_playlist;
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 )
105 return vout_vaControl( p_vout->p_sys->p_vout, i_query, args );
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 )
115 vout_thread_t *p_vout = (vout_thread_t *)p_this;
116 char *psz_descfilename;
118 int x1, x2, y1, y2, i_level, i_downspeed, i_upspeed, i;
122 /* Allocate structure */
123 p_vout->p_sys = malloc( sizeof( vout_sys_t ) );
124 if( p_vout->p_sys == NULL )
126 msg_Err( p_vout, "out of memory" );
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;
137 memset( p_vout->p_sys, 0, sizeof( vout_sys_t ) );
139 p_vout->p_sys->i_history = config_GetInt( p_vout,
140 "motiondetect-history" );
142 if( !(psz_descfilename = config_GetPsz( p_vout,
143 "motiondetect-description" ) ) )
145 free( p_vout->p_sys );
149 p_vout->p_sys->p_playlist = vlc_object_find( p_this, VLC_OBJECT_PLAYLIST,
151 if( !p_vout->p_sys->p_playlist )
153 msg_Err( p_vout, "playlist not found" );
154 free( p_vout->p_sys );
158 /* Parse description file and allocate areas */
159 p_file = fopen( psz_descfilename, "r" );
162 msg_Err( p_this, "Failed to open descritpion file %s",
164 free( psz_descfilename );
165 free( p_vout->p_sys );
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 )
173 for( i = 0 ; i < 255 ; i++ )
175 fread( buffer + i, 1, 1, p_file );
176 if( *( buffer + i ) == '\n' )
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 *
184 if( !p_vout->p_sys->pp_areas )
185 /*FIXME: clean this... */
187 p_area = malloc( sizeof( area_t ) );
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;
200 p_area->psz_mrl = strdup(buffer);
201 p_vout->p_sys->pp_areas[p_vout->p_sys->i_areas-1] = p_area;
207 /*****************************************************************************
208 * Init: initialize Motion detect video thread output method
209 *****************************************************************************/
210 static int Init( vout_thread_t *p_vout )
215 I_OUTPUTPICTURES = 0;
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;
223 /* Try to open the real video output */
224 msg_Dbg( p_vout, "spawning the real video output" );
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 );
230 /* Everything failed */
231 if( p_vout->p_sys->p_vout == NULL )
233 msg_Err( p_vout, "cannot open vout, aborting" );
237 ALLOCATE_DIRECTBUFFERS( VOUT_MAX_PICTURES );
239 ADD_CALLBACKS( p_vout->p_sys->p_vout, SendEvents );
241 ADD_PARENT_CALLBACKS( SendEventsToChild );
246 /*****************************************************************************
247 * End: terminate Motion detect video thread output method
248 *****************************************************************************/
249 static void End( vout_thread_t *p_vout )
253 /* Free the fake output buffers we allocated */
254 for( i_index = I_OUTPUTPICTURES ; i_index ; )
257 free( PP_OUTPUTPICTURE[ i_index ]->p_data_orig );
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 )
268 vout_thread_t *p_vout = (vout_thread_t *)p_this;
271 if( p_vout->p_sys->p_vout )
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 );
278 DEL_PARENT_CALLBACKS( SendEventsToChild );
280 for( i = 0 ; i < p_vout->p_sys->i_areas ; i++ )
282 free( p_vout->p_sys->pp_areas[i]->psz_mrl );
283 free( p_vout->p_sys->pp_areas[i] );
286 free( p_vout->p_sys->pp_areas );
287 free( p_vout->p_sys );
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
296 *****************************************************************************/
297 static void Render( vout_thread_t *p_vout, picture_t *p_pic )
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 ) )
305 if( p_vout->b_die || p_vout->b_error )
309 msleep( VOUT_OUTMEM_SLEEP );
312 vout_DatePicture( p_vout->p_sys->p_vout, p_outpic, p_pic->date );
314 MotionDetect( p_vout, p_pic, p_outpic );
316 vout_DisplayPicture( p_vout->p_sys->p_vout, p_outpic );
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 )
325 #define pp_curent_area p_vout->p_sys->pp_areas[i_area]
327 int i_index, i_index_col, i_area;
328 for( i_index = 0 ; i_index < p_inpic->i_planes ; i_index++ )
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;
333 p_in = p_inpic->p[i_index].p_pixels;
334 p_out = p_outpic->p[i_index].p_pixels;
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;
339 p_vout->p_vlc->pf_memcpy( p_out, p_in, i_size );
343 p_buffer = p_vout->p_sys->p_bufferY;
345 if( p_buffer == NULL)
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 );
350 p_vout->p_sys->p_bufferY = p_buffer;
351 p_vout->p_sys->i_stack = 0;
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++)
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 ) )
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;
370 for( i_line = pp_curent_area->i_y1 ;
371 i_line < pp_curent_area->i_y2 ; i_line++ )
373 for( i_index_col = pp_curent_area->i_x1 ;
374 i_index_col < pp_curent_area->i_x2 ; i_index_col++ )
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));
383 if( i_diff > pp_curent_area->i_level )
384 i_tmp += pp_curent_area->i_upspeed;
386 *( p_out + i_index_col + i_line *
387 p_inpic->p[i_index].i_pitch ) =
388 pp_curent_area->i_matches;
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)
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;
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;
413 p_vout->p_vlc->pf_memcpy( p_last_in, p_in, i_size );
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
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 )
431 var_Set( (vlc_object_t *)p_data, psz_var, newval );
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 )
442 vout_thread_t *p_vout = (vout_thread_t *)p_this;
443 var_Set( p_vout->p_sys->p_vout, psz_var, newval );