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