Main Page | Modules | Class Hierarchy | Class List | Directories | File List | Class Members | File Members | Related Pages

motiondetect.c

00001 /*****************************************************************************
00002  * motiondetect.c : Motion detect video effect plugin for vlc
00003  *****************************************************************************
00004  * Copyright (C) 2005 the VideoLAN team
00005  * $Id: motiondetect.c 12968 2005-10-25 19:24:21Z gbazin $
00006  *
00007  * Authors: Jérôme Decoodt <[email protected]>
00008  *
00009  * This program is free software; you can redistribute it and/or modify
00010  * it under the terms of the GNU General Public License as published by
00011  * the Free Software Foundation; either version 2 of the License, or
00012  * (at your option) any later version.
00013  *
00014  * This program is distributed in the hope that it will be useful,
00015  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00016  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00017  * GNU General Public License for more details.
00018  *
00019  * You should have received a copy of the GNU General Public License
00020  * along with this program; if not, write to the Free Software
00021  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111, USA.
00022  *****************************************************************************/
00023 
00024 /*****************************************************************************
00025  * Preamble
00026  *****************************************************************************/
00027 #include <stdlib.h>                                      /* malloc(), free() */
00028 #include <string.h>
00029 
00030 #include <vlc/vlc.h>
00031 #include <vlc/vout.h>
00032 #include <vlc/intf.h>
00033 
00034 #include "filter_common.h"
00035 
00036 /*****************************************************************************
00037  * Local prototypes
00038  *****************************************************************************/
00039 static int  Create    ( vlc_object_t * );
00040 static void Destroy   ( vlc_object_t * );
00041 
00042 static int  Init      ( vout_thread_t * );
00043 static void End       ( vout_thread_t * );
00044 static void Render    ( vout_thread_t *, picture_t * );
00045 static void MotionDetect( vout_thread_t *p_vout, picture_t *p_inpic,
00046                                                  picture_t *p_outpic );
00047 
00048 static int  SendEvents   ( vlc_object_t *, char const *,
00049                            vlc_value_t, vlc_value_t, void * );
00050 
00051 /*****************************************************************************
00052  * Module descriptor
00053  *****************************************************************************/
00054 #define DESC_TEXT N_("Description file")
00055 #define DESC_LONGTEXT N_("Description file, file containing simple playlist")
00056 #define HISTORY_TEXT N_("History parameter")
00057 #define HISTORY_LONGTEXT N_("History parameter, number of frames used for detection")
00058 
00059 vlc_module_begin();
00060     set_description( _("Motion detect video filter") );
00061     set_shortname( N_( "Motion detect" ));
00062     set_category( CAT_VIDEO );
00063     set_subcategory( SUBCAT_VIDEO_VFILTER );
00064     set_capability( "video filter", 0 );
00065 
00066     add_integer( "motiondetect-history", 1, NULL, HISTORY_TEXT,
00067                                 HISTORY_LONGTEXT, VLC_FALSE );
00068     add_string( "motiondetect-description", "motiondetect", NULL, DESC_TEXT,
00069                                 DESC_LONGTEXT, VLC_FALSE );
00070 
00071     set_callbacks( Create, Destroy );
00072 vlc_module_end();
00073 
00074 /*****************************************************************************
00075  * vout_sys_t: Motion detect video output method descriptor
00076  *****************************************************************************
00077  * This structure is part of the video output thread descriptor.
00078  * It describes the Motion detect specific properties of an output thread.
00079  *****************************************************************************/
00080 typedef struct area_t
00081 {
00082     int i_x1, i_y1;
00083     int i_x2, i_y2;
00084     int i_matches;
00085     int i_level;
00086     int i_downspeed, i_upspeed;
00087     char *psz_mrl;
00088 } area_t;
00089 
00090 struct vout_sys_t
00091 {
00092     vout_thread_t *p_vout;
00093     playlist_t *p_playlist;
00094 
00095     uint8_t *p_bufferY;
00096     int i_stack;
00097     area_t** pp_areas;
00098     int i_areas;
00099     int i_history;
00100 };
00101 
00102 /*****************************************************************************
00103  * Control: control facility for the vout (forwards to child vout)
00104  *****************************************************************************/
00105 static int Control( vout_thread_t *p_vout, int i_query, va_list args )
00106 {
00107     return vout_vaControl( p_vout->p_sys->p_vout, i_query, args );
00108 }
00109 
00110 /*****************************************************************************
00111  * Create: allocates Distort video thread output method
00112  *****************************************************************************
00113  * This function allocates and initializes a Distort vout method.
00114  *****************************************************************************/
00115 static int Create( vlc_object_t *p_this )
00116 {
00117     vout_thread_t *p_vout = (vout_thread_t *)p_this;
00118     char *psz_descfilename;
00119     char buffer[256];
00120     int x1, x2, y1, y2, i_level, i_downspeed, i_upspeed, i;
00121     area_t *p_area;
00122     FILE * p_file;
00123 
00124     /* Allocate structure */
00125     p_vout->p_sys = malloc( sizeof( vout_sys_t ) );
00126     if( p_vout->p_sys == NULL )
00127     {
00128         msg_Err( p_vout, "out of memory" );
00129         return VLC_ENOMEM;
00130     }
00131 
00132     p_vout->pf_init = Init;
00133     p_vout->pf_end = End;
00134     p_vout->pf_manage = NULL;
00135     p_vout->pf_render = Render;
00136     p_vout->pf_display = NULL;
00137     p_vout->pf_control = Control;
00138 
00139     memset( p_vout->p_sys, 0, sizeof( vout_sys_t ) );
00140 
00141     p_vout->p_sys->i_history = config_GetInt( p_vout,
00142                                               "motiondetect-history" );
00143 
00144     if( !(psz_descfilename = config_GetPsz( p_vout,
00145                                             "motiondetect-description" ) ) )
00146     {
00147         free( p_vout->p_sys );
00148         return VLC_EGENERIC;
00149     }
00150 
00151     p_vout->p_sys->p_playlist = vlc_object_find( p_this, VLC_OBJECT_PLAYLIST,
00152                                                  FIND_ANYWHERE );
00153     if( !p_vout->p_sys->p_playlist )
00154     {
00155          msg_Err( p_vout, "playlist not found" );
00156          free( p_vout->p_sys );
00157          return VLC_EGENERIC;
00158     }
00159 
00160     /* Parse description file and allocate areas */
00161     p_file = fopen( psz_descfilename, "r" );
00162     if( !p_file )
00163     {
00164         msg_Err( p_this, "Failed to open descritpion file %s",
00165                             psz_descfilename );
00166         free( psz_descfilename );
00167         free( p_vout->p_sys );
00168         return VLC_EGENERIC;
00169     }
00170     p_vout->p_sys->i_areas = 0;
00171     while( fscanf( p_file, "%d,%d,%d,%d,%d,%d,%d,",
00172                             &x1, &y1, &x2, &y2, &i_level,
00173                             &i_downspeed, &i_upspeed ) == 7 )
00174     {
00175         for( i = 0 ; i < 255 ; i++ )
00176         {
00177             fread( buffer + i, 1, 1, p_file );
00178             if( *( buffer + i ) == '\n' )
00179                 break;
00180         }
00181         *( buffer + i )  = 0;
00182         p_vout->p_sys->i_areas++;
00183         p_vout->p_sys->pp_areas = realloc( p_vout->p_sys->pp_areas,
00184                                         p_vout->p_sys->i_areas * 
00185                                                     sizeof( area_t ) );
00186         if( !p_vout->p_sys->pp_areas )
00187             /*FIXME: clean this... */
00188             return VLC_ENOMEM;
00189         p_area = malloc( sizeof( area_t ) );
00190         if( !p_area )
00191             break;
00192 
00193         p_area->i_x1 = x1;
00194         p_area->i_x2 = x2;
00195         p_area->i_y1 = y1;
00196         p_area->i_y2 = y2;
00197         p_area->i_matches = 0;
00198         p_area->i_level = i_level;
00199         p_area->i_downspeed = i_downspeed;
00200         p_area->i_upspeed = i_upspeed;
00201 
00202         p_area->psz_mrl = strdup(buffer);
00203         p_vout->p_sys->pp_areas[p_vout->p_sys->i_areas-1] = p_area;
00204     }
00205     fclose( p_file );
00206     return VLC_SUCCESS;
00207 }
00208 
00209 /*****************************************************************************
00210  * Init: initialize Motion detect video thread output method
00211  *****************************************************************************/
00212 static int Init( vout_thread_t *p_vout )
00213 {
00214     int i_index;
00215     picture_t *p_pic;
00216     video_format_t fmt = {0};
00217 
00218     I_OUTPUTPICTURES = 0;
00219 
00220     /* Initialize the output structure */
00221     p_vout->output.i_chroma = p_vout->render.i_chroma;
00222     p_vout->output.i_width  = p_vout->render.i_width;
00223     p_vout->output.i_height = p_vout->render.i_height;
00224     p_vout->output.i_aspect = p_vout->render.i_aspect;
00225     p_vout->fmt_out = p_vout->fmt_in;
00226     fmt = p_vout->fmt_out;
00227 
00228     /* Try to open the real video output */
00229     msg_Dbg( p_vout, "spawning the real video output" );
00230 
00231     p_vout->p_sys->p_vout = vout_Create( p_vout, &fmt );
00232 
00233     /* Everything failed */
00234     if( p_vout->p_sys->p_vout == NULL )
00235     {
00236         msg_Err( p_vout, "cannot open vout, aborting" );
00237         return VLC_EGENERIC;
00238     }
00239 
00240     ALLOCATE_DIRECTBUFFERS( VOUT_MAX_PICTURES );
00241 
00242     ADD_CALLBACKS( p_vout->p_sys->p_vout, SendEvents );
00243 
00244     ADD_PARENT_CALLBACKS( SendEventsToChild );
00245 
00246     return VLC_SUCCESS;
00247 }
00248 
00249 /*****************************************************************************
00250  * End: terminate Motion detect video thread output method
00251  *****************************************************************************/
00252 static void End( vout_thread_t *p_vout )
00253 {
00254     int i_index;
00255 
00256     /* Free the fake output buffers we allocated */
00257     for( i_index = I_OUTPUTPICTURES ; i_index ; )
00258     {
00259         i_index--;
00260         free( PP_OUTPUTPICTURE[ i_index ]->p_data_orig );
00261     }
00262 }
00263 
00264 /*****************************************************************************
00265  * Destroy: destroy Motion detect video thread output method
00266  *****************************************************************************
00267  * Terminate an output method created by DistortCreateOutputMethod
00268  *****************************************************************************/
00269 static void Destroy( vlc_object_t *p_this )
00270 {
00271     vout_thread_t *p_vout = (vout_thread_t *)p_this;
00272     int i;
00273 
00274     if( p_vout->p_sys->p_vout )
00275     {
00276         DEL_CALLBACKS( p_vout->p_sys->p_vout, SendEvents );
00277         vlc_object_detach( p_vout->p_sys->p_vout );
00278         vout_Destroy( p_vout->p_sys->p_vout );
00279     }
00280 
00281     DEL_PARENT_CALLBACKS( SendEventsToChild );
00282 
00283     for( i = 0 ; i < p_vout->p_sys->i_areas ; i++ )
00284     {
00285         free( p_vout->p_sys->pp_areas[i]->psz_mrl );
00286         free( p_vout->p_sys->pp_areas[i] );
00287     }
00288 
00289     free( p_vout->p_sys->pp_areas );
00290     free( p_vout->p_sys );
00291 }
00292 
00293 /*****************************************************************************
00294  * Render: displays previously rendered output
00295  *****************************************************************************
00296  * This function send the currently rendered image to Distort image, waits
00297  * until it is displayed and switch the two rendering buffers, preparing next
00298  * frame.
00299  *****************************************************************************/
00300 static void Render( vout_thread_t *p_vout, picture_t *p_pic )
00301 {
00302     picture_t *p_outpic;
00303 
00304     /* This is a new frame. Get a structure from the video_output. */
00305     while( ( p_outpic = vout_CreatePicture( p_vout->p_sys->p_vout, 0, 0, 0 ) )
00306               == NULL )
00307     {
00308         if( p_vout->b_die || p_vout->b_error )
00309         {
00310             return;
00311         }
00312         msleep( VOUT_OUTMEM_SLEEP );
00313     }
00314 
00315     vout_DatePicture( p_vout->p_sys->p_vout, p_outpic, p_pic->date );
00316 
00317     MotionDetect( p_vout, p_pic, p_outpic );
00318 
00319     vout_DisplayPicture( p_vout->p_sys->p_vout, p_outpic );
00320 }
00321 
00322 /*****************************************************************************
00323  * MotionDetect: calculates new matches
00324  *****************************************************************************/
00325 static void MotionDetect( vout_thread_t *p_vout, picture_t *p_inpic,
00326                                                  picture_t *p_outpic )
00327 {
00328 #define pp_curent_area p_vout->p_sys->pp_areas[i_area]
00329 
00330     int i_index, i_index_col, i_area;
00331     for( i_index = 0 ; i_index < p_inpic->i_planes ; i_index++ )
00332     {
00333         int i_line, i_num_lines, i_offset, i_size, i_diff;
00334         uint8_t *p_in, *p_out, *p_last_in, *p_buffer;
00335 
00336         p_in = p_inpic->p[i_index].p_pixels;
00337         p_out = p_outpic->p[i_index].p_pixels;
00338 
00339         i_num_lines = p_inpic->p[i_index].i_visible_lines;
00340         i_size = p_inpic->p[i_index].i_lines * p_inpic->p[i_index].i_pitch;
00341 
00342         p_vout->p_vlc->pf_memcpy( p_out, p_in, i_size );
00343         switch( i_index )
00344         {
00345         case Y_PLANE:
00346             p_buffer = p_vout->p_sys->p_bufferY;
00347 
00348             if( p_buffer == NULL)
00349             {
00350                 p_buffer = malloc( p_vout->p_sys->i_history * i_size);
00351                 memset( p_buffer, 0, p_vout->p_sys->i_history * i_size );
00352 
00353                 p_vout->p_sys->p_bufferY = p_buffer;
00354                 p_vout->p_sys->i_stack = 0;
00355             }
00356 
00357             i_offset = i_size * p_vout->p_sys->i_stack;
00358             p_last_in = p_buffer + i_offset;
00359             for( i_area = 0 ; i_area < p_vout->p_sys->i_areas ; i_area++)
00360             {
00361                 int i_tmp = 0, i_nb_pixels;
00362                 p_last_in = p_buffer + i_offset;
00363                 p_in = p_inpic->p[i_index].p_pixels;
00364                 p_out = p_outpic->p[i_index].p_pixels;
00365                 if( ( pp_curent_area->i_y1 > p_inpic->p[i_index].i_lines ) ||
00366                     ( pp_curent_area->i_x1 > p_inpic->p[i_index].i_pitch ) )
00367                     continue;
00368                 if( ( pp_curent_area->i_y2 > p_inpic->p[i_index].i_lines ) )
00369                     pp_curent_area->i_y2 = p_inpic->p[i_index].i_lines;
00370                 if( ( pp_curent_area->i_x2 > p_inpic->p[i_index].i_pitch ) )
00371                     pp_curent_area->i_x2 = p_inpic->p[i_index].i_pitch;
00372 
00373                 for( i_line = pp_curent_area->i_y1 ;
00374                      i_line < pp_curent_area->i_y2 ; i_line++ )
00375                 {
00376                     for( i_index_col = pp_curent_area->i_x1 ;
00377                          i_index_col < pp_curent_area->i_x2 ; i_index_col++ )
00378                     {
00379                         i_diff = (*(p_last_in + i_index_col +
00380                                         i_line * p_inpic->p[i_index].i_pitch)
00381                                 - *(p_in      + i_index_col +
00382                                         i_line * p_inpic->p[i_index].i_pitch));
00383                         if( i_diff < 0)
00384                             i_diff = -i_diff;
00385 
00386                         if( i_diff > pp_curent_area->i_level )
00387                             i_tmp += pp_curent_area->i_upspeed;
00388 
00389                         *( p_out + i_index_col + i_line *
00390                                         p_inpic->p[i_index].i_pitch ) =
00391                                                 pp_curent_area->i_matches;
00392                     }
00393                 }
00394                 i_nb_pixels = ( pp_curent_area->i_y2 - pp_curent_area->i_y1 ) *
00395                               ( pp_curent_area->i_x2 - pp_curent_area->i_x1 );
00396                 pp_curent_area->i_matches += i_tmp / i_nb_pixels -
00397                                     pp_curent_area->i_downspeed;
00398                 if( pp_curent_area->i_matches < 0)
00399                     pp_curent_area->i_matches = 0;
00400                 if( pp_curent_area->i_matches > 255)
00401                 {
00402                     playlist_item_t *p_item = playlist_ItemNew( p_vout,
00403                                         (const char*)pp_curent_area->psz_mrl,
00404                                         pp_curent_area->psz_mrl );
00405                     msg_Dbg( p_vout, "Area(%d) matched, going to %s\n", i_area,
00406                                         pp_curent_area->psz_mrl );
00407                     playlist_Control( p_vout->p_sys->p_playlist,
00408                                         PLAYLIST_ITEMPLAY, p_item );
00409                     pp_curent_area->i_matches = 0;
00410                 }
00411             }
00412             p_last_in = p_buffer + i_offset;
00413             p_in = p_inpic->p[i_index].p_pixels;
00414             p_out = p_outpic->p[i_index].p_pixels;
00415 
00416             p_vout->p_vlc->pf_memcpy( p_last_in, p_in, i_size );
00417             break;
00418         default:
00419             break;
00420         }
00421     }
00422     p_vout->p_sys->i_stack++;
00423     if( p_vout->p_sys->i_stack >= p_vout->p_sys->i_history )
00424         p_vout->p_sys->i_stack = 0;
00425 #undef pp_curent_area
00426 }
00427 
00428 /*****************************************************************************
00429  * SendEvents: forward mouse and keyboard events to the parent p_vout
00430  *****************************************************************************/
00431 static int SendEvents( vlc_object_t *p_this, char const *psz_var,
00432                        vlc_value_t oldval, vlc_value_t newval, void *p_data )
00433 {
00434     var_Set( (vlc_object_t *)p_data, psz_var, newval );
00435 
00436     return VLC_SUCCESS;
00437 }
00438 
00439 /*****************************************************************************
00440  * SendEventsToChild: forward events to the child/children vout
00441  *****************************************************************************/
00442 static int SendEventsToChild( vlc_object_t *p_this, char const *psz_var,
00443                        vlc_value_t oldval, vlc_value_t newval, void *p_data )
00444 {
00445     vout_thread_t *p_vout = (vout_thread_t *)p_this;
00446     var_Set( p_vout->p_sys->p_vout, psz_var, newval );
00447     return VLC_SUCCESS;
00448 }

Generated on Tue Dec 20 10:14:53 2005 for vlc-0.8.4a by  doxygen 1.4.2