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 * );
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 PERSISTANT_TEXT N_("Keep inifinte memory of past images")
52 #define PERSISTANT_LONGTEXT N_("Use \"Result = ( new image * ( 128 - factor ) + factor * old result ) / 128\" instead of \"Result = ( new image * ( 128 - factor ) + factor * old image ) / 128\".")
54 #define FILTER_PREFIX "blur-"
57 set_shortname( _("Motion blur") );
58 set_description( _("Motion blur filter") );
59 set_capability( "video filter2", 0 );
60 set_category( CAT_VIDEO );
61 set_subcategory( SUBCAT_VIDEO_VFILTER );
63 add_integer_with_range( FILTER_PREFIX "factor", 80, 1, 127, NULL,
64 FACTOR_TEXT, FACTOR_LONGTEXT, VLC_FALSE );
65 add_bool( FILTER_PREFIX "persistant", 0, NULL, PERSISTANT_TEXT,
66 PERSISTANT_LONGTEXT, VLC_FALSE );
68 add_shortcut( "blur" );
70 set_callbacks( Create, Destroy );
73 static const char *ppsz_filter_options[] = {
74 "factor", "persistant", NULL
77 /*****************************************************************************
79 *****************************************************************************/
83 vlc_bool_t b_persistant;
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 )
100 msg_Err( p_filter, "out of memory" );
104 p_filter->pf_video_filter = Filter;
106 config_ChainParse( p_filter, FILTER_PREFIX, ppsz_filter_options,
109 var_Create( p_filter, FILTER_PREFIX "factor",
110 VLC_VAR_INTEGER | VLC_VAR_DOINHERIT );
111 p_filter->p_sys->i_factor =
112 var_GetInteger( p_filter, FILTER_PREFIX "factor" );
113 var_Create( p_filter, FILTER_PREFIX "persistant",
114 VLC_VAR_BOOL | VLC_VAR_DOINHERIT );
115 p_filter->p_sys->b_persistant =
116 var_GetBool( p_filter, FILTER_PREFIX "persistant" );
118 p_filter->p_sys->pp_planes = NULL;
119 p_filter->p_sys->i_planes = 0;
124 /*****************************************************************************
126 *****************************************************************************/
127 static void Destroy( vlc_object_t *p_this )
129 filter_t *p_filter = (filter_t *)p_this;
131 while( p_filter->p_sys->i_planes-- )
132 free( p_filter->p_sys->pp_planes[p_filter->p_sys->i_planes] );
133 free( p_filter->p_sys->pp_planes );
134 free( p_filter->p_sys );
137 #define RELEASE( pic ) \
138 if( pic ->pf_release ) \
139 pic ->pf_release( pic );
141 #define INITPIC( dst, src ) \
142 dst ->date = src ->date; \
143 dst ->b_force = src ->b_force; \
144 dst ->i_nb_fields = src ->i_nb_fields; \
145 dst ->b_progressive = src->b_progressive; \
146 dst ->b_top_field_first = src ->b_top_field_first;
148 /*****************************************************************************
150 *****************************************************************************/
151 static picture_t *Filter( filter_t *p_filter, picture_t *p_pic )
153 picture_t * p_outpic;
154 filter_sys_t *p_sys = p_filter->p_sys;
156 if( !p_pic ) return NULL;
158 p_outpic = p_filter->pf_vout_buffer_new( p_filter );
161 msg_Warn( p_filter, "can't get output picture" );
165 INITPIC( p_outpic, p_pic );
167 if( !p_sys->pp_planes )
169 /* initialise our picture buffer */
171 p_sys->i_planes = p_pic->i_planes;
173 (uint8_t**)malloc( p_sys->i_planes * sizeof( uint8_t * ) );
174 for( i_plane = 0; i_plane < p_pic->i_planes; i_plane++ )
176 p_sys->pp_planes[i_plane] = (uint8_t*)malloc(
177 p_pic->p[i_plane].i_pitch * p_pic->p[i_plane].i_visible_lines );
179 Copy( p_filter, p_sys->pp_planes, p_pic );
182 /* Get a new picture */
183 RenderBlur( p_sys, p_pic, p_outpic );
184 Copy( p_filter, p_sys->pp_planes, p_sys->b_persistant ? p_outpic : p_pic );
190 /*****************************************************************************
191 * RenderBlur: renders a blurred picture
192 *****************************************************************************/
193 static void RenderBlur( filter_sys_t *p_sys, picture_t *p_newpic,
194 picture_t *p_outpic )
197 int i_oldfactor = p_sys->i_factor;
198 int i_newfactor = 128 - i_oldfactor;
199 for( i_plane = 0; i_plane < p_outpic->i_planes; i_plane++ )
201 uint8_t *p_old, *p_new, *p_out, *p_out_end, *p_out_line_end;
202 const int i_pitch = p_outpic->p[i_plane].i_pitch;
203 const int i_visible_pitch = p_outpic->p[i_plane].i_visible_pitch;
204 const int i_visible_lines = p_outpic->p[i_plane].i_visible_lines;
206 p_out = p_outpic->p[i_plane].p_pixels;
207 p_new = p_newpic->p[i_plane].p_pixels;
208 p_old = p_sys->pp_planes[i_plane];
209 p_out_end = p_out + i_pitch * i_visible_lines;
210 while ( p_out < p_out_end )
212 p_out_line_end = p_out + i_visible_pitch;
214 while ( p_out < p_out_line_end )
216 *p_out++ = (((*p_old++) * i_oldfactor) +
217 ((*p_new++) * i_newfactor)) >> 7;
220 p_old += i_pitch - i_visible_pitch;
221 p_new += i_pitch - i_visible_pitch;
222 p_out += i_pitch - i_visible_pitch;
227 static void Copy( filter_t *p_filter, uint8_t **pp_planes, picture_t *p_pic )
230 for( i_plane = 0; i_plane < p_pic->i_planes; i_plane++ )
232 p_filter->p_libvlc->pf_memcpy(
233 p_filter->p_sys->pp_planes[i_plane], p_pic->p[i_plane].p_pixels,
234 p_pic->p[i_plane].i_pitch * p_pic->p[i_plane].i_visible_lines );