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