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 *****************************************************************************/
32 #include <vlc_filter.h>
34 /*****************************************************************************
36 *****************************************************************************/
37 static int Create ( vlc_object_t * );
38 static void Destroy ( vlc_object_t * );
39 static picture_t *Filter ( filter_t *, picture_t * );
40 static void RenderBlur ( filter_sys_t *, picture_t *, picture_t * );
41 static void Copy ( filter_t *, uint8_t **, picture_t * );
42 static int MotionBlurCallback( vlc_object_t *, char const *,
43 vlc_value_t, vlc_value_t, void * );
45 /*****************************************************************************
47 *****************************************************************************/
48 #define FACTOR_TEXT N_("Blur factor (1-127)")
49 #define FACTOR_LONGTEXT N_("The degree of blurring from 1 to 127.")
51 #define FILTER_PREFIX "blur-"
54 set_shortname( _("Motion blur") );
55 set_description( _("Motion blur filter") );
56 set_capability( "video filter2", 0 );
57 set_category( CAT_VIDEO );
58 set_subcategory( SUBCAT_VIDEO_VFILTER );
60 add_integer_with_range( FILTER_PREFIX "factor", 80, 1, 127, NULL,
61 FACTOR_TEXT, FACTOR_LONGTEXT, VLC_FALSE );
63 add_shortcut( "blur" );
65 set_callbacks( Create, Destroy );
68 static const char *ppsz_filter_options[] = {
72 /*****************************************************************************
74 *****************************************************************************/
83 /*****************************************************************************
85 *****************************************************************************/
86 static int Create( vlc_object_t *p_this )
88 filter_t *p_filter = (filter_t *)p_this;
90 /* Allocate structure */
91 p_filter->p_sys = malloc( sizeof( filter_sys_t ) );
92 if( p_filter->p_sys == NULL )
94 msg_Err( p_filter, "out of memory" );
98 p_filter->pf_video_filter = Filter;
100 config_ChainParse( p_filter, FILTER_PREFIX, ppsz_filter_options,
103 p_filter->p_sys->i_factor =
104 var_CreateGetIntegerCommand( p_filter, FILTER_PREFIX "factor" );
105 var_AddCallback( p_filter, FILTER_PREFIX "factor",
106 MotionBlurCallback, p_filter->p_sys );
108 p_filter->p_sys->pp_planes = NULL;
109 p_filter->p_sys->i_planes = 0;
114 /*****************************************************************************
116 *****************************************************************************/
117 static void Destroy( vlc_object_t *p_this )
119 filter_t *p_filter = (filter_t *)p_this;
121 while( p_filter->p_sys->i_planes-- )
122 free( p_filter->p_sys->pp_planes[p_filter->p_sys->i_planes] );
123 free( p_filter->p_sys->pp_planes );
124 free( p_filter->p_sys );
127 #define RELEASE( pic ) \
128 if( pic ->pf_release ) \
129 pic ->pf_release( pic );
131 #define INITPIC( dst, src ) \
132 dst ->date = src ->date; \
133 dst ->b_force = src ->b_force; \
134 dst ->i_nb_fields = src ->i_nb_fields; \
135 dst ->b_progressive = src->b_progressive; \
136 dst ->b_top_field_first = src ->b_top_field_first;
138 /*****************************************************************************
140 *****************************************************************************/
141 static picture_t *Filter( filter_t *p_filter, picture_t *p_pic )
143 picture_t * p_outpic;
144 filter_sys_t *p_sys = p_filter->p_sys;
146 if( !p_pic ) return NULL;
148 p_outpic = p_filter->pf_vout_buffer_new( p_filter );
151 msg_Warn( p_filter, "can't get output picture" );
155 INITPIC( p_outpic, p_pic );
157 if( !p_sys->pp_planes )
159 /* initialise our picture buffer */
161 p_sys->i_planes = p_pic->i_planes;
163 (uint8_t**)malloc( p_sys->i_planes * sizeof( uint8_t * ) );
164 for( i_plane = 0; i_plane < p_pic->i_planes; i_plane++ )
166 p_sys->pp_planes[i_plane] = (uint8_t*)malloc(
167 p_pic->p[i_plane].i_pitch * p_pic->p[i_plane].i_visible_lines );
169 Copy( p_filter, p_sys->pp_planes, p_pic );
172 /* Get a new picture */
173 RenderBlur( p_sys, p_pic, p_outpic );
174 Copy( p_filter, p_sys->pp_planes, p_outpic );
180 /*****************************************************************************
181 * RenderBlur: renders a blurred picture
182 *****************************************************************************/
183 static void RenderBlur( filter_sys_t *p_sys, picture_t *p_newpic,
184 picture_t *p_outpic )
187 int i_oldfactor = p_sys->i_factor;
188 int i_newfactor = 128 - i_oldfactor;
189 for( i_plane = 0; i_plane < p_outpic->i_planes; i_plane++ )
191 uint8_t *p_old, *p_new, *p_out, *p_out_end, *p_out_line_end;
192 const int i_pitch = p_outpic->p[i_plane].i_pitch;
193 const int i_visible_pitch = p_outpic->p[i_plane].i_visible_pitch;
194 const int i_visible_lines = p_outpic->p[i_plane].i_visible_lines;
196 p_out = p_outpic->p[i_plane].p_pixels;
197 p_new = p_newpic->p[i_plane].p_pixels;
198 p_old = p_sys->pp_planes[i_plane];
199 p_out_end = p_out + i_pitch * i_visible_lines;
200 while ( p_out < p_out_end )
202 p_out_line_end = p_out + i_visible_pitch;
204 while ( p_out < p_out_line_end )
206 *p_out++ = (((*p_old++) * i_oldfactor) +
207 ((*p_new++) * i_newfactor)) >> 7;
210 p_old += i_pitch - i_visible_pitch;
211 p_new += i_pitch - i_visible_pitch;
212 p_out += i_pitch - i_visible_pitch;
217 static void Copy( filter_t *p_filter, uint8_t **pp_planes, picture_t *p_pic )
220 for( i_plane = 0; i_plane < p_pic->i_planes; i_plane++ )
222 p_filter->p_libvlc->pf_memcpy(
223 p_filter->p_sys->pp_planes[i_plane], p_pic->p[i_plane].p_pixels,
224 p_pic->p[i_plane].i_pitch * p_pic->p[i_plane].i_visible_lines );
228 static int MotionBlurCallback( vlc_object_t *p_this, char const *psz_var,
229 vlc_value_t oldval, vlc_value_t newval,
232 filter_sys_t *p_sys = (filter_sys_t *)p_data;
233 if( !strcmp( psz_var, FILTER_PREFIX "factor" ) )
234 p_sys->i_factor = __MIN( 127, __MAX( 1, newval.i_int ) );