1 /*****************************************************************************
2 * motion_blur.c : motion blur filter for vlc
3 *****************************************************************************
4 * Copyright (C) 2000, 2001, 2002, 2003 the VideoLAN team
7 * Authors: Sigmund Augdal Helberg <dnumgis@videolan.org>
8 * Antoine Cellerier <dionoea &t videolan d.t org>
10 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License as published by
12 * the Free Software Foundation; either version 2 of the License, or
13 * (at your option) any later version.
15 * This program is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU General Public License for more details.
20 * You should have received a copy of the GNU General Public License
21 * along with this program; if not, write to the Free Software
22 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
23 *****************************************************************************/
25 /*****************************************************************************
27 *****************************************************************************/
33 #include <vlc_common.h>
34 #include <vlc_plugin.h>
36 #include <vlc_filter.h>
37 #include "filter_picture.h"
39 /*****************************************************************************
41 *****************************************************************************/
42 static int Create ( vlc_object_t * );
43 static void Destroy ( vlc_object_t * );
44 static picture_t *Filter ( filter_t *, picture_t * );
45 static void RenderBlur ( filter_sys_t *, picture_t *, picture_t * );
46 static void Copy ( filter_t *, picture_t * );
47 static int MotionBlurCallback( vlc_object_t *, char const *,
48 vlc_value_t, vlc_value_t, void * );
50 /*****************************************************************************
52 *****************************************************************************/
53 #define FACTOR_TEXT N_("Blur factor (1-127)")
54 #define FACTOR_LONGTEXT N_("The degree of blurring from 1 to 127.")
56 #define FILTER_PREFIX "blur-"
59 set_shortname( N_("Motion blur") )
60 set_description( N_("Motion blur filter") )
61 set_capability( "video filter2", 0 )
62 set_category( CAT_VIDEO )
63 set_subcategory( SUBCAT_VIDEO_VFILTER )
65 add_integer_with_range( FILTER_PREFIX "factor", 80, 1, 127, NULL,
66 FACTOR_TEXT, FACTOR_LONGTEXT, false )
68 add_shortcut( "blur" )
70 set_callbacks( Create, Destroy )
73 static const char *const ppsz_filter_options[] = {
77 /*****************************************************************************
79 *****************************************************************************/
89 /*****************************************************************************
91 *****************************************************************************/
92 static int Create( vlc_object_t *p_this )
94 filter_t *p_filter = (filter_t *)p_this;
96 /* Allocate structure */
97 p_filter->p_sys = malloc( sizeof( filter_sys_t ) );
98 if( p_filter->p_sys == NULL )
101 p_filter->pf_video_filter = Filter;
103 config_ChainParse( p_filter, FILTER_PREFIX, ppsz_filter_options,
106 p_filter->p_sys->i_factor =
107 var_CreateGetIntegerCommand( p_filter, FILTER_PREFIX "factor" );
108 vlc_spin_init( &p_filter->p_sys->lock );
109 var_AddCallback( p_filter, FILTER_PREFIX "factor",
110 MotionBlurCallback, p_filter->p_sys );
112 p_filter->p_sys->pp_planes = NULL;
113 p_filter->p_sys->i_planes = 0;
118 /*****************************************************************************
120 *****************************************************************************/
121 static void Destroy( vlc_object_t *p_this )
123 filter_t *p_filter = (filter_t *)p_this;
125 var_DelCallback( p_filter, FILTER_PREFIX "factor",
126 MotionBlurCallback, p_filter->p_sys );
127 vlc_spin_destroy( &p_filter->p_sys->lock );
129 while( p_filter->p_sys->i_planes-- )
130 free( p_filter->p_sys->pp_planes[p_filter->p_sys->i_planes] );
131 free( p_filter->p_sys->pp_planes );
132 free( p_filter->p_sys );
135 /*****************************************************************************
137 *****************************************************************************/
138 static picture_t *Filter( filter_t *p_filter, picture_t *p_pic )
140 picture_t * p_outpic;
141 filter_sys_t *p_sys = p_filter->p_sys;
143 if( !p_pic ) return NULL;
145 p_outpic = filter_NewPicture( p_filter );
148 picture_Release( p_pic );
152 if( !p_sys->pp_planes )
154 /* initialise our picture buffer */
156 p_sys->i_planes = p_pic->i_planes;
158 (uint8_t**)malloc( p_sys->i_planes * sizeof( uint8_t * ) );
159 for( i_plane = 0; i_plane < p_pic->i_planes; i_plane++ )
161 p_sys->pp_planes[i_plane] = (uint8_t*)malloc(
162 p_pic->p[i_plane].i_pitch * p_pic->p[i_plane].i_visible_lines );
164 Copy( p_filter, p_pic );
167 /* Get a new picture */
168 RenderBlur( p_sys, p_pic, p_outpic );
169 Copy( p_filter, p_outpic );
171 return CopyInfoAndRelease( p_outpic, p_pic );
174 /*****************************************************************************
175 * RenderBlur: renders a blurred picture
176 *****************************************************************************/
177 static void RenderBlur( filter_sys_t *p_sys, picture_t *p_newpic,
178 picture_t *p_outpic )
181 vlc_spin_lock( &p_sys->lock );
182 const int i_oldfactor = p_sys->i_factor;
183 vlc_spin_unlock( &p_sys->lock );
184 int i_newfactor = 128 - i_oldfactor;
186 for( i_plane = 0; i_plane < p_outpic->i_planes; i_plane++ )
188 uint8_t *p_old, *p_new, *p_out, *p_out_end, *p_out_line_end;
189 const int i_pitch = p_outpic->p[i_plane].i_pitch;
190 const int i_visible_pitch = p_outpic->p[i_plane].i_visible_pitch;
191 const int i_visible_lines = p_outpic->p[i_plane].i_visible_lines;
193 p_out = p_outpic->p[i_plane].p_pixels;
194 p_new = p_newpic->p[i_plane].p_pixels;
195 p_old = p_sys->pp_planes[i_plane];
196 p_out_end = p_out + i_pitch * i_visible_lines;
197 while ( p_out < p_out_end )
199 p_out_line_end = p_out + i_visible_pitch;
201 while ( p_out < p_out_line_end )
203 *p_out++ = (((*p_old++) * i_oldfactor) +
204 ((*p_new++) * i_newfactor)) >> 7;
207 p_old += i_pitch - i_visible_pitch;
208 p_new += i_pitch - i_visible_pitch;
209 p_out += i_pitch - i_visible_pitch;
214 static void Copy( filter_t *p_filter, picture_t *p_pic )
217 for( i_plane = 0; i_plane < p_pic->i_planes; i_plane++ )
220 p_filter->p_sys->pp_planes[i_plane], p_pic->p[i_plane].p_pixels,
221 p_pic->p[i_plane].i_pitch * p_pic->p[i_plane].i_visible_lines );
225 static int MotionBlurCallback( vlc_object_t *p_this, char const *psz_var,
226 vlc_value_t oldval, vlc_value_t newval,
229 VLC_UNUSED(p_this); VLC_UNUSED(oldval);
230 filter_sys_t *p_sys = (filter_sys_t *)p_data;
232 if( !strcmp( psz_var, FILTER_PREFIX "factor" ) )
234 vlc_spin_lock( &p_sys->lock );
235 p_sys->i_factor = __MIN( 127, __MAX( 1, newval.i_int ) );
236 vlc_spin_unlock( &p_sys->lock );