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() */
33 #include <vlc/decoder.h>
35 #include "vlc_filter.h"
37 /*****************************************************************************
39 *****************************************************************************/
40 static int Create ( vlc_object_t * );
41 static void Destroy ( vlc_object_t * );
42 static picture_t *Filter ( filter_t *, picture_t * );
43 static void RenderBlur ( filter_sys_t *, picture_t *, picture_t * );
44 static void Copy ( filter_t *, uint8_t **, picture_t * );
46 /*****************************************************************************
48 *****************************************************************************/
49 #define FACTOR_TEXT N_("Blur factor (1-127)")
50 #define FACTOR_LONGTEXT N_("The degree of blurring from 1 to 127.")
52 #define PERSISTANT_TEXT N_("Keep inifinte memory of past images")
53 #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\".")
55 #define FILTER_PREFIX "blur-"
58 set_shortname( _("Motion blur") );
59 set_description( _("Motion blur filter") );
60 set_capability( "video filter2", 0 );
61 set_category( CAT_VIDEO );
62 set_subcategory( SUBCAT_VIDEO_VFILTER );
64 add_integer_with_range( FILTER_PREFIX "factor", 80, 1, 127, NULL,
65 FACTOR_TEXT, FACTOR_LONGTEXT, VLC_FALSE );
66 add_bool( FILTER_PREFIX "persistant", 0, NULL, PERSISTANT_TEXT,
67 PERSISTANT_LONGTEXT, VLC_FALSE );
69 add_shortcut( "blur" );
71 set_callbacks( Create, Destroy );
74 static const char *ppsz_filter_options[] = {
75 "factor", "persistant", NULL
78 /*****************************************************************************
80 *****************************************************************************/
84 vlc_bool_t b_persistant;
90 /*****************************************************************************
92 *****************************************************************************/
93 static int Create( vlc_object_t *p_this )
95 filter_t *p_filter = (filter_t *)p_this;
97 /* Allocate structure */
98 p_filter->p_sys = malloc( sizeof( filter_sys_t ) );
99 if( p_filter->p_sys == NULL )
101 msg_Err( p_filter, "out of memory" );
105 p_filter->pf_video_filter = Filter;
107 config_ChainParse( p_filter, FILTER_PREFIX, ppsz_filter_options,
110 var_Create( p_filter, FILTER_PREFIX "factor",
111 VLC_VAR_INTEGER | VLC_VAR_DOINHERIT );
112 p_filter->p_sys->i_factor =
113 var_GetInteger( p_filter, FILTER_PREFIX "factor" );
114 var_Create( p_filter, FILTER_PREFIX "persistant",
115 VLC_VAR_BOOL | VLC_VAR_DOINHERIT );
116 p_filter->p_sys->b_persistant =
117 var_GetBool( p_filter, FILTER_PREFIX "persistant" );
119 printf("factor: %d\npersitant: %d\n", p_filter->p_sys->i_factor, p_filter->p_sys->b_persistant );
121 p_filter->p_sys->pp_planes = NULL;
122 p_filter->p_sys->i_planes = 0;
127 /*****************************************************************************
129 *****************************************************************************/
130 static void Destroy( vlc_object_t *p_this )
132 filter_t *p_filter = (filter_t *)p_this;
134 while( p_filter->p_sys->i_planes-- )
135 free( p_filter->p_sys->pp_planes[p_filter->p_sys->i_planes] );
136 free( p_filter->p_sys->pp_planes );
137 free( p_filter->p_sys );
140 #define RELEASE( pic ) \
141 if( pic ->pf_release ) \
142 pic ->pf_release( pic );
144 #define INITPIC( dst, src ) \
145 dst ->date = src ->date; \
146 dst ->b_force = src ->b_force; \
147 dst ->i_nb_fields = src ->i_nb_fields; \
148 dst ->b_progressive = src->b_progressive; \
149 dst ->b_top_field_first = src ->b_top_field_first;
151 /*****************************************************************************
153 *****************************************************************************/
154 static picture_t *Filter( filter_t *p_filter, picture_t *p_pic )
156 picture_t * p_outpic;
157 filter_sys_t *p_sys = p_filter->p_sys;
159 if( !p_pic ) return NULL;
161 p_outpic = p_filter->pf_vout_buffer_new( p_filter );
164 msg_Warn( p_filter, "can't get output picture" );
168 INITPIC( p_outpic, p_pic );
170 if( !p_sys->pp_planes )
172 /* initialise our picture buffer */
174 p_sys->i_planes = p_pic->i_planes;
176 (uint8_t**)malloc( p_sys->i_planes * sizeof( uint8_t * ) );
177 for( i_plane = 0; i_plane < p_pic->i_planes; i_plane++ )
179 p_sys->pp_planes[i_plane] = (uint8_t*)malloc(
180 p_pic->p[i_plane].i_pitch * p_pic->p[i_plane].i_visible_lines );
182 Copy( p_filter, p_sys->pp_planes, p_pic );
185 /* Get a new picture */
186 RenderBlur( p_sys, p_pic, p_outpic );
187 Copy( p_filter, p_sys->pp_planes, p_sys->b_persistant ? p_outpic : p_pic );
193 /*****************************************************************************
194 * RenderBlur: renders a blurred picture
195 *****************************************************************************/
196 static void RenderBlur( filter_sys_t *p_sys, picture_t *p_newpic,
197 picture_t *p_outpic )
200 int i_oldfactor = p_sys->i_factor;
201 int i_newfactor = 128 - i_oldfactor;
202 for( i_plane = 0; i_plane < p_outpic->i_planes; i_plane++ )
204 uint8_t *p_old, *p_new, *p_out, *p_out_end, *p_out_line_end;
205 const int i_pitch = p_outpic->p[i_plane].i_pitch;
206 const int i_visible_pitch = p_outpic->p[i_plane].i_visible_pitch;
207 const int i_visible_lines = p_outpic->p[i_plane].i_visible_lines;
209 p_out = p_outpic->p[i_plane].p_pixels;
210 p_new = p_newpic->p[i_plane].p_pixels;
211 p_old = p_sys->pp_planes[i_plane];
212 p_out_end = p_out + i_pitch * i_visible_lines;
213 while ( p_out < p_out_end )
215 p_out_line_end = p_out + i_visible_pitch;
217 while ( p_out < p_out_line_end )
219 *p_out++ = (((*p_old++) * i_oldfactor) +
220 ((*p_new++) * i_newfactor)) >> 7;
223 p_old += i_pitch - i_visible_pitch;
224 p_new += i_pitch - i_visible_pitch;
225 p_out += i_pitch - i_visible_pitch;
230 static void Copy( filter_t *p_filter, uint8_t **pp_planes, picture_t *p_pic )
233 for( i_plane = 0; i_plane < p_pic->i_planes; i_plane++ )
235 p_filter->p_libvlc->pf_memcpy(
236 p_filter->p_sys->pp_planes[i_plane], p_pic->p[i_plane].p_pixels,
237 p_pic->p[i_plane].i_pitch * p_pic->p[i_plane].i_visible_lines );