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 *****************************************************************************/
28 #include <stdlib.h> /* malloc(), free() */
34 #include <vlc_filter.h>
36 /*****************************************************************************
38 *****************************************************************************/
39 static int Create ( vlc_object_t * );
40 static void Destroy ( vlc_object_t * );
41 static picture_t *Filter ( filter_t *, picture_t * );
42 static void RenderBlur ( filter_sys_t *, picture_t *, picture_t * );
43 static void Copy ( filter_t *, uint8_t **, picture_t * );
44 static int MotionBlurCallback( vlc_object_t *, char const *,
45 vlc_value_t, vlc_value_t, void * );
47 /*****************************************************************************
49 *****************************************************************************/
50 #define FACTOR_TEXT N_("Blur factor (1-127)")
51 #define FACTOR_LONGTEXT N_("The degree of blurring from 1 to 127.")
53 #define FILTER_PREFIX "blur-"
56 set_shortname( _("Motion blur") );
57 set_description( _("Motion blur filter") );
58 set_capability( "video filter2", 0 );
59 set_category( CAT_VIDEO );
60 set_subcategory( SUBCAT_VIDEO_VFILTER );
62 add_integer_with_range( FILTER_PREFIX "factor", 80, 1, 127, NULL,
63 FACTOR_TEXT, FACTOR_LONGTEXT, VLC_FALSE );
65 add_shortcut( "blur" );
67 set_callbacks( Create, Destroy );
70 static const char *ppsz_filter_options[] = {
74 /*****************************************************************************
76 *****************************************************************************/
85 /*****************************************************************************
87 *****************************************************************************/
88 static int Create( vlc_object_t *p_this )
90 filter_t *p_filter = (filter_t *)p_this;
92 /* Allocate structure */
93 p_filter->p_sys = malloc( sizeof( filter_sys_t ) );
94 if( p_filter->p_sys == NULL )
96 msg_Err( p_filter, "out of memory" );
100 p_filter->pf_video_filter = Filter;
102 config_ChainParse( p_filter, FILTER_PREFIX, ppsz_filter_options,
105 p_filter->p_sys->i_factor =
106 var_CreateGetIntegerCommand( p_filter, FILTER_PREFIX "factor" );
107 var_AddCallback( p_filter, FILTER_PREFIX "factor",
108 MotionBlurCallback, p_filter->p_sys );
110 p_filter->p_sys->pp_planes = NULL;
111 p_filter->p_sys->i_planes = 0;
116 /*****************************************************************************
118 *****************************************************************************/
119 static void Destroy( vlc_object_t *p_this )
121 filter_t *p_filter = (filter_t *)p_this;
123 while( p_filter->p_sys->i_planes-- )
124 free( p_filter->p_sys->pp_planes[p_filter->p_sys->i_planes] );
125 free( p_filter->p_sys->pp_planes );
126 free( p_filter->p_sys );
129 #define RELEASE( pic ) \
130 if( pic ->pf_release ) \
131 pic ->pf_release( pic );
133 #define INITPIC( dst, src ) \
134 dst ->date = src ->date; \
135 dst ->b_force = src ->b_force; \
136 dst ->i_nb_fields = src ->i_nb_fields; \
137 dst ->b_progressive = src->b_progressive; \
138 dst ->b_top_field_first = src ->b_top_field_first;
140 /*****************************************************************************
142 *****************************************************************************/
143 static picture_t *Filter( filter_t *p_filter, picture_t *p_pic )
145 picture_t * p_outpic;
146 filter_sys_t *p_sys = p_filter->p_sys;
148 if( !p_pic ) return NULL;
150 p_outpic = p_filter->pf_vout_buffer_new( p_filter );
153 msg_Warn( p_filter, "can't get output picture" );
157 INITPIC( p_outpic, p_pic );
159 if( !p_sys->pp_planes )
161 /* initialise our picture buffer */
163 p_sys->i_planes = p_pic->i_planes;
165 (uint8_t**)malloc( p_sys->i_planes * sizeof( uint8_t * ) );
166 for( i_plane = 0; i_plane < p_pic->i_planes; i_plane++ )
168 p_sys->pp_planes[i_plane] = (uint8_t*)malloc(
169 p_pic->p[i_plane].i_pitch * p_pic->p[i_plane].i_visible_lines );
171 Copy( p_filter, p_sys->pp_planes, p_pic );
174 /* Get a new picture */
175 RenderBlur( p_sys, p_pic, p_outpic );
176 Copy( p_filter, p_sys->pp_planes, p_outpic );
182 /*****************************************************************************
183 * RenderBlur: renders a blurred picture
184 *****************************************************************************/
185 static void RenderBlur( filter_sys_t *p_sys, picture_t *p_newpic,
186 picture_t *p_outpic )
189 int i_oldfactor = p_sys->i_factor;
190 int i_newfactor = 128 - i_oldfactor;
191 for( i_plane = 0; i_plane < p_outpic->i_planes; i_plane++ )
193 uint8_t *p_old, *p_new, *p_out, *p_out_end, *p_out_line_end;
194 const int i_pitch = p_outpic->p[i_plane].i_pitch;
195 const int i_visible_pitch = p_outpic->p[i_plane].i_visible_pitch;
196 const int i_visible_lines = p_outpic->p[i_plane].i_visible_lines;
198 p_out = p_outpic->p[i_plane].p_pixels;
199 p_new = p_newpic->p[i_plane].p_pixels;
200 p_old = p_sys->pp_planes[i_plane];
201 p_out_end = p_out + i_pitch * i_visible_lines;
202 while ( p_out < p_out_end )
204 p_out_line_end = p_out + i_visible_pitch;
206 while ( p_out < p_out_line_end )
208 *p_out++ = (((*p_old++) * i_oldfactor) +
209 ((*p_new++) * i_newfactor)) >> 7;
212 p_old += i_pitch - i_visible_pitch;
213 p_new += i_pitch - i_visible_pitch;
214 p_out += i_pitch - i_visible_pitch;
219 static void Copy( filter_t *p_filter, uint8_t **pp_planes, picture_t *p_pic )
222 for( i_plane = 0; i_plane < p_pic->i_planes; i_plane++ )
224 p_filter->p_libvlc->pf_memcpy(
225 p_filter->p_sys->pp_planes[i_plane], p_pic->p[i_plane].p_pixels,
226 p_pic->p[i_plane].i_pitch * p_pic->p[i_plane].i_visible_lines );
230 static int MotionBlurCallback( vlc_object_t *p_this, char const *psz_var,
231 vlc_value_t oldval, vlc_value_t newval,
234 filter_sys_t *p_sys = (filter_sys_t *)p_data;
235 if( !strcmp( psz_var, FILTER_PREFIX "factor" ) )
236 p_sys->i_factor = __MIN( 127, __MAX( 1, newval.i_int ) );