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