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