]> git.sesse.net Git - vlc/blob - modules/video_filter/motionblur.c
caa28709b5c2c47674eddf1bf797f55f6aeadfb6
[vlc] / modules / video_filter / motionblur.c
1 /*****************************************************************************
2  * motion_blur.c : motion blur filter for vlc
3  *****************************************************************************
4  * Copyright (C) 2000, 2001, 2002, 2003 the VideoLAN team
5  * $Id$
6  *
7  * Authors: Sigmund Augdal Helberg <dnumgis@videolan.org>
8  *          Antoine Cellerier <dionoea &t videolan d.t org>
9  *
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.
14  *
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.
19  *
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  *****************************************************************************/
24
25 /*****************************************************************************
26  * Preamble
27  *****************************************************************************/
28
29 #include <vlc/vlc.h>
30 #include <vlc_sout.h>
31 #include <vlc_vout.h>
32 #include <vlc_filter.h>
33
34 /*****************************************************************************
35  * Local protypes
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 * );
44
45 /*****************************************************************************
46  * Module descriptor
47  *****************************************************************************/
48 #define FACTOR_TEXT N_("Blur factor (1-127)")
49 #define FACTOR_LONGTEXT N_("The degree of blurring from 1 to 127.")
50
51 #define FILTER_PREFIX "blur-"
52
53 vlc_module_begin();
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 );
59
60     add_integer_with_range( FILTER_PREFIX "factor", 80, 1, 127, NULL,
61                             FACTOR_TEXT, FACTOR_LONGTEXT, VLC_FALSE );
62         change_safe();
63
64     add_shortcut( "blur" );
65
66     set_callbacks( Create, Destroy );
67 vlc_module_end();
68
69 static const char *ppsz_filter_options[] = {
70     "factor", NULL
71 };
72
73 /*****************************************************************************
74  * filter_sys_t
75  *****************************************************************************/
76 struct filter_sys_t
77 {
78     int        i_factor;
79
80     uint8_t  **pp_planes;
81     int        i_planes;
82 };
83
84 /*****************************************************************************
85  * Create
86  *****************************************************************************/
87 static int Create( vlc_object_t *p_this )
88 {
89     filter_t *p_filter = (filter_t *)p_this;
90
91     /* Allocate structure */
92     p_filter->p_sys = malloc( sizeof( filter_sys_t ) );
93     if( p_filter->p_sys == NULL )
94     {
95         msg_Err( p_filter, "out of memory" );
96         return VLC_ENOMEM;
97     }
98
99     p_filter->pf_video_filter = Filter;
100
101     config_ChainParse( p_filter, FILTER_PREFIX, ppsz_filter_options,
102                        p_filter->p_cfg );
103
104     p_filter->p_sys->i_factor =
105         var_CreateGetIntegerCommand( p_filter, FILTER_PREFIX "factor" );
106     var_AddCallback( p_filter, FILTER_PREFIX "factor",
107                      MotionBlurCallback, p_filter->p_sys );
108
109     p_filter->p_sys->pp_planes = NULL;
110     p_filter->p_sys->i_planes = 0;
111
112     return VLC_SUCCESS;
113 }
114
115 /*****************************************************************************
116  * Destroy
117  *****************************************************************************/
118 static void Destroy( vlc_object_t *p_this )
119 {
120     filter_t *p_filter = (filter_t *)p_this;
121
122     while( p_filter->p_sys->i_planes-- )
123         free( p_filter->p_sys->pp_planes[p_filter->p_sys->i_planes] );
124     free( p_filter->p_sys->pp_planes );
125     free( p_filter->p_sys );
126 }
127
128 #define RELEASE( pic ) \
129         if( pic ->pf_release ) \
130             pic ->pf_release( pic );
131
132 #define INITPIC( dst, src ) \
133     dst ->date = src ->date; \
134     dst ->b_force = src ->b_force; \
135     dst ->i_nb_fields = src ->i_nb_fields; \
136     dst ->b_progressive = src->b_progressive; \
137     dst ->b_top_field_first = src ->b_top_field_first;
138
139 /*****************************************************************************
140  * Filter
141  *****************************************************************************/
142 static picture_t *Filter( filter_t *p_filter, picture_t *p_pic )
143 {
144     picture_t * p_outpic;
145     filter_sys_t *p_sys = p_filter->p_sys;
146
147     if( !p_pic ) return NULL;
148
149     p_outpic = p_filter->pf_vout_buffer_new( p_filter );
150     if( !p_outpic )
151     {
152         msg_Warn( p_filter, "can't get output picture" );
153         RELEASE( p_pic );
154         return NULL;
155     }
156     INITPIC( p_outpic, p_pic );
157
158     if( !p_sys->pp_planes )
159     {
160         /* initialise our picture buffer */
161         int i_plane;
162         p_sys->i_planes = p_pic->i_planes;
163         p_sys->pp_planes =
164             (uint8_t**)malloc( p_sys->i_planes * sizeof( uint8_t * ) );
165         for( i_plane = 0; i_plane < p_pic->i_planes; i_plane++ )
166         {
167             p_sys->pp_planes[i_plane] = (uint8_t*)malloc(
168                 p_pic->p[i_plane].i_pitch * p_pic->p[i_plane].i_visible_lines );
169         }
170         Copy( p_filter, p_sys->pp_planes, p_pic );
171     }
172
173     /* Get a new picture */
174     RenderBlur( p_sys, p_pic, p_outpic );
175     Copy( p_filter, p_sys->pp_planes, p_outpic );
176
177     RELEASE( p_pic );
178     return p_outpic;
179 }
180
181 /*****************************************************************************
182  * RenderBlur: renders a blurred picture
183  *****************************************************************************/
184 static void RenderBlur( filter_sys_t *p_sys, picture_t *p_newpic,
185                         picture_t *p_outpic )
186 {
187     int i_plane;
188     int i_oldfactor = p_sys->i_factor;
189     int i_newfactor = 128 - i_oldfactor;
190     for( i_plane = 0; i_plane < p_outpic->i_planes; i_plane++ )
191     {
192         uint8_t *p_old, *p_new, *p_out, *p_out_end, *p_out_line_end;
193         const int i_pitch = p_outpic->p[i_plane].i_pitch;
194         const int i_visible_pitch = p_outpic->p[i_plane].i_visible_pitch;
195         const int i_visible_lines = p_outpic->p[i_plane].i_visible_lines;
196
197         p_out = p_outpic->p[i_plane].p_pixels;
198         p_new = p_newpic->p[i_plane].p_pixels;
199         p_old = p_sys->pp_planes[i_plane];
200         p_out_end = p_out + i_pitch * i_visible_lines;
201         while ( p_out < p_out_end )
202         {
203             p_out_line_end = p_out + i_visible_pitch;
204
205             while ( p_out < p_out_line_end )
206             {
207                 *p_out++ = (((*p_old++) * i_oldfactor) +
208                             ((*p_new++) * i_newfactor)) >> 7;
209             }
210
211             p_old += i_pitch - i_visible_pitch;
212             p_new += i_pitch - i_visible_pitch;
213             p_out += i_pitch - i_visible_pitch;
214         }
215     }
216 }
217
218 static void Copy( filter_t *p_filter, uint8_t **pp_planes, picture_t *p_pic )
219 {
220     int i_plane;
221     for( i_plane = 0; i_plane < p_pic->i_planes; i_plane++ )
222     {
223         p_filter->p_libvlc->pf_memcpy(
224             p_filter->p_sys->pp_planes[i_plane], p_pic->p[i_plane].p_pixels,
225             p_pic->p[i_plane].i_pitch * p_pic->p[i_plane].i_visible_lines );
226     }
227 }
228
229 static int MotionBlurCallback( vlc_object_t *p_this, char const *psz_var,
230                                vlc_value_t oldval, vlc_value_t newval,
231                                void *p_data )
232 {
233     filter_sys_t *p_sys = (filter_sys_t *)p_data;
234     if( !strcmp( psz_var, FILTER_PREFIX "factor" ) )
235         p_sys->i_factor = __MIN( 127, __MAX( 1, newval.i_int ) );
236     return VLC_SUCCESS;
237 }