📄 motiondetect.c
字号:
/***************************************************************************** * motiondetec.c : Second version of a motion detection plugin. ***************************************************************************** * Copyright (C) 2000-2008 the VideoLAN team * $Id$ * * Authors: Antoine Cellerier <dionoea -at- videolan -dot- org> * * This program is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation; either version 2 of the License, or * (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program; if not, write to the Free Software * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA. *****************************************************************************//***************************************************************************** * Preamble *****************************************************************************/#ifdef HAVE_CONFIG_H# include "config.h"#endif#include <vlc_common.h>#include <vlc_plugin.h>#include <vlc_sout.h>#include <vlc_vout.h>#include "vlc_filter.h"#include "filter_picture.h"/***************************************************************************** * Module descriptor *****************************************************************************/static int Create ( vlc_object_t * );static void Destroy ( vlc_object_t * );#define FILTER_PREFIX "motiondetect-"vlc_module_begin(); set_description( N_("Motion detect video filter") ); set_shortname( N_( "Motion Detect" )); set_category( CAT_VIDEO ); set_subcategory( SUBCAT_VIDEO_VFILTER ); set_capability( "video filter2", 0 ); add_shortcut( "motion" ); set_callbacks( Create, Destroy );vlc_module_end();/***************************************************************************** * Local prototypes *****************************************************************************/static picture_t *Filter( filter_t *, picture_t * );static picture_t *FilterPacked( filter_t *, picture_t * );static void GaussianConvolution( uint32_t *, uint32_t *, int, int, int );static int FindShapes( uint32_t *, uint32_t *, int, int, int, int *, int *, int *, int *, int *);static void Draw( filter_t *p_filter, uint8_t *p_pix, int i_pix_pitch, int i_pix_size );#define NUM_COLORS (5000)struct filter_sys_t{ bool b_old; picture_t *p_old; uint32_t *p_buf; uint32_t *p_buf2; /* */ int i_colors; int colors[NUM_COLORS]; int color_x_min[NUM_COLORS]; int color_x_max[NUM_COLORS]; int color_y_min[NUM_COLORS]; int color_y_max[NUM_COLORS];};/***************************************************************************** * Create *****************************************************************************/static int Create( vlc_object_t *p_this ){ filter_t *p_filter = (filter_t *)p_this; const video_format_t *p_fmt = &p_filter->fmt_in.video; filter_sys_t *p_sys; switch( p_fmt->i_chroma ) { CASE_PLANAR_YUV p_filter->pf_video_filter = Filter; break; CASE_PACKED_YUV_422 p_filter->pf_video_filter = FilterPacked; break; default: msg_Err( p_filter, "Unsupported input chroma (%4s)", (char*)&(p_fmt->i_chroma) ); return VLC_EGENERIC; } /* Allocate structure */ p_filter->p_sys = p_sys = malloc( sizeof( filter_sys_t ) ); if( p_filter->p_sys == NULL ) return VLC_ENOMEM; p_sys->b_old = false; p_sys->p_old = picture_New( p_fmt->i_chroma, p_fmt->i_width, p_fmt->i_height, 0 ); p_sys->p_buf = calloc( p_fmt->i_width * p_fmt->i_height, sizeof(*p_sys->p_buf) ); p_sys->p_buf2 = calloc( p_fmt->i_width * p_fmt->i_height, sizeof(*p_sys->p_buf) ); if( !p_sys->p_old || !p_sys->p_buf || !p_sys->p_buf2 ) { free( p_sys->p_buf2 ); free( p_sys->p_buf ); if( p_sys->p_old ) picture_Release( p_sys->p_old ); return VLC_ENOMEM; } return VLC_SUCCESS;}/***************************************************************************** * Destroy *****************************************************************************/static void Destroy( vlc_object_t *p_this ){ filter_t *p_filter = (filter_t *)p_this; filter_sys_t *p_sys = p_filter->p_sys; free( p_sys->p_buf2 ); free( p_sys->p_buf ); picture_Release( p_sys->p_old ); free( p_sys );}/***************************************************************************** * Filter YUV Planar *****************************************************************************/static picture_t *Filter( filter_t *p_filter, picture_t *p_inpic ){ filter_sys_t *p_sys = p_filter->p_sys; const video_format_t *p_fmt = &p_filter->fmt_in.video; picture_t *p_outpic; const uint8_t *p_inpix = p_inpic->p[Y_PLANE].p_pixels; const int i_src_pitch = p_inpic->p[Y_PLANE].i_pitch; uint8_t *p_oldpix = p_sys->p_old->p[Y_PLANE].p_pixels; const int i_old_pitch = p_sys->p_old->p[Y_PLANE].i_pitch; uint32_t *p_buf = p_sys->p_buf; uint32_t *p_buf2= p_sys->p_buf2; unsigned x, y; if( !p_inpic ) return NULL; if( !p_sys->b_old ) { picture_Copy( p_sys->p_old, p_inpic ); p_sys->b_old = true; return p_inpic; } p_outpic = filter_NewPicture( p_filter ); if( !p_outpic ) { picture_Release( p_inpic ); return NULL; } picture_Copy( p_outpic, p_inpic ); /** * Substract Y planes */ for( y = 0; y < p_fmt->i_height; y++ ) { for( x = 0; x < p_fmt->i_width; x++ ) p_buf2[y*p_fmt->i_width+x] = abs( p_inpix[y*i_src_pitch+x] - p_oldpix[y*i_old_pitch+x] ); } int i_chroma_dx; int i_chroma_dy; switch( p_inpic->format.i_chroma ) { case VLC_FOURCC('I','4','2','0'): case VLC_FOURCC('I','Y','U','V'): case VLC_FOURCC('J','4','2','0'): case VLC_FOURCC('Y','V','1','2'): i_chroma_dx = 2; i_chroma_dy = 2; break; case VLC_FOURCC('I','4','2','2'): case VLC_FOURCC('J','4','2','2'): i_chroma_dx = 2; i_chroma_dy = 1; break; default: msg_Warn( p_filter, "Not taking chroma into account" ); i_chroma_dx = 0; i_chroma_dy = 0; break; } if( i_chroma_dx != 0 && i_chroma_dy != 0 ) { const uint8_t *p_inpix_u = p_inpic->p[U_PLANE].p_pixels; const uint8_t *p_inpix_v = p_inpic->p[V_PLANE].p_pixels; const int i_src_pitch_u = p_inpic->p[U_PLANE].i_pitch; const int i_src_pitch_v = p_inpic->p[V_PLANE].i_pitch; const uint8_t *p_oldpix_u = p_sys->p_old->p[U_PLANE].p_pixels; const uint8_t *p_oldpix_v = p_sys->p_old->p[V_PLANE].p_pixels; const int i_old_pitch_u = p_sys->p_old->p[U_PLANE].i_pitch; const int i_old_pitch_v = p_sys->p_old->p[V_PLANE].i_pitch; for( y = 0; y < p_fmt->i_height/i_chroma_dy; y++ ) { for( x = 0; x < p_fmt->i_width/i_chroma_dx; x ++ ) { const int d = abs( p_inpix_u[y*i_src_pitch_u+x] - p_oldpix_u[y*i_old_pitch_u+x] ) + abs( p_inpix_v[y*i_src_pitch_v+x] - p_oldpix_v[y*i_old_pitch_v+x] ); int i, j; for( j = 0; j < i_chroma_dy; j++ ) { for( i = 0; i < i_chroma_dx; i++ ) p_buf2[i_chroma_dy*p_fmt->i_width*j + i_chroma_dx*i] = d; } } } } /** * Get the areas where movement was detected */ p_sys->i_colors = FindShapes( p_buf2, p_buf, p_fmt->i_width, p_fmt->i_width, p_fmt->i_height, p_sys->colors, p_sys->color_x_min, p_sys->color_x_max, p_sys->color_y_min, p_sys->color_y_max ); /** * Count final number of shapes * Draw rectangles (there can be more than 1 moving shape in 1 rectangle) */ Draw( p_filter, p_outpic->p[Y_PLANE].p_pixels, p_outpic->p[Y_PLANE].i_pitch, 1 ); /** * We're done. Lets keep a copy of the picture * TODO we may just picture_Release with a latency of 1 if the filters/vout * handle it correctly */ picture_Copy( p_sys->p_old, p_inpic ); picture_Release( p_inpic ); return p_outpic;}/***************************************************************************** * Filter YUV Packed *****************************************************************************/static picture_t *FilterPacked( filter_t *p_filter, picture_t *p_inpic ){ filter_sys_t *p_sys = p_filter->p_sys; const video_format_t *p_fmt = &p_filter->fmt_in.video; picture_t *p_outpic; const uint8_t *p_inpix = p_inpic->p[Y_PLANE].p_pixels; const int i_src_pitch = p_inpic->p[Y_PLANE].i_pitch; uint8_t *p_oldpix = p_sys->p_old->p[Y_PLANE].p_pixels; const int i_old_pitch = p_sys->p_old->p[Y_PLANE].i_pitch; uint32_t *p_buf = p_sys->p_buf; uint32_t *p_buf2= p_sys->p_buf2; int i_y_offset, i_u_offset, i_v_offset; unsigned x, y; if( GetPackedYuvOffsets( p_fmt->i_chroma, &i_y_offset, &i_u_offset, &i_v_offset ) ) { msg_Warn( p_filter, "Unsupported input chroma (%4s)", (char*)&p_fmt->i_chroma ); return p_inpic; }
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -