]> git.sesse.net Git - vlc/blob - modules/video_filter/motionblur.c
macosx: fixed menubar appearance in fullscreen mode by partially reverting [46c93c9cc...
[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 #include "filter_picture.h"
39
40 /*****************************************************************************
41  * Local protypes
42  *****************************************************************************/
43 static int  Create       ( vlc_object_t * );
44 static void Destroy      ( vlc_object_t * );
45 static picture_t *Filter ( filter_t *, picture_t * );
46 static void RenderBlur   ( filter_sys_t *, picture_t *, picture_t * );
47 static void Copy         ( filter_t *, picture_t * );
48 static int MotionBlurCallback( vlc_object_t *, char const *,
49                                vlc_value_t, vlc_value_t, void * );
50
51 /*****************************************************************************
52  * Module descriptor
53  *****************************************************************************/
54 #define FACTOR_TEXT N_("Blur factor (1-127)")
55 #define FACTOR_LONGTEXT N_("The degree of blurring from 1 to 127.")
56
57 #define FILTER_PREFIX "blur-"
58
59 vlc_module_begin ()
60     set_shortname( N_("Motion blur") )
61     set_description( N_("Motion blur filter") )
62     set_capability( "video filter2", 0 )
63     set_category( CAT_VIDEO )
64     set_subcategory( SUBCAT_VIDEO_VFILTER )
65
66     add_integer_with_range( FILTER_PREFIX "factor", 80, 1, 127, NULL,
67                             FACTOR_TEXT, FACTOR_LONGTEXT, false )
68
69     add_shortcut( "blur" )
70
71     set_callbacks( Create, Destroy )
72 vlc_module_end ()
73
74 static const char *const ppsz_filter_options[] = {
75     "factor", NULL
76 };
77
78 /*****************************************************************************
79  * filter_sys_t
80  *****************************************************************************/
81 struct filter_sys_t
82 {
83     vlc_spinlock_t lock;
84     int        i_factor;
85
86     uint8_t  **pp_planes;
87     int        i_planes;
88 };
89
90 /*****************************************************************************
91  * Create
92  *****************************************************************************/
93 static int Create( vlc_object_t *p_this )
94 {
95     filter_t *p_filter = (filter_t *)p_this;
96
97     /* Allocate structure */
98     p_filter->p_sys = malloc( sizeof( filter_sys_t ) );
99     if( p_filter->p_sys == NULL )
100         return VLC_ENOMEM;
101
102     p_filter->pf_video_filter = Filter;
103
104     config_ChainParse( p_filter, FILTER_PREFIX, ppsz_filter_options,
105                        p_filter->p_cfg );
106
107     p_filter->p_sys->i_factor =
108         var_CreateGetIntegerCommand( p_filter, FILTER_PREFIX "factor" );
109     vlc_spin_init( &p_filter->p_sys->lock );
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     var_DelCallback( p_filter, FILTER_PREFIX "factor",
127                      MotionBlurCallback, p_filter->p_sys );
128     vlc_spin_destroy( &p_filter->p_sys->lock );
129
130     while( p_filter->p_sys->i_planes-- )
131         free( p_filter->p_sys->pp_planes[p_filter->p_sys->i_planes] );
132     free( p_filter->p_sys->pp_planes );
133     free( p_filter->p_sys );
134 }
135
136 /*****************************************************************************
137  * Filter
138  *****************************************************************************/
139 static picture_t *Filter( filter_t *p_filter, picture_t *p_pic )
140 {
141     picture_t * p_outpic;
142     filter_sys_t *p_sys = p_filter->p_sys;
143
144     if( !p_pic ) return NULL;
145
146     p_outpic = filter_NewPicture( p_filter );
147     if( !p_outpic )
148     {
149         picture_Release( p_pic );
150         return NULL;
151     }
152
153     if( !p_sys->pp_planes )
154     {
155         /* initialise our picture buffer */
156         int i_plane;
157         p_sys->i_planes = p_pic->i_planes;
158         p_sys->pp_planes =
159             (uint8_t**)malloc( p_sys->i_planes * sizeof( uint8_t * ) );
160         for( i_plane = 0; i_plane < p_pic->i_planes; i_plane++ )
161         {
162             p_sys->pp_planes[i_plane] = (uint8_t*)malloc(
163                 p_pic->p[i_plane].i_pitch * p_pic->p[i_plane].i_visible_lines );
164         }
165         Copy( p_filter, p_pic );
166     }
167
168     /* Get a new picture */
169     RenderBlur( p_sys, p_pic, p_outpic );
170     Copy( p_filter, p_outpic );
171
172     return CopyInfoAndRelease( p_outpic, p_pic );
173 }
174
175 /*****************************************************************************
176  * RenderBlur: renders a blurred picture
177  *****************************************************************************/
178 static void RenderBlur( filter_sys_t *p_sys, picture_t *p_newpic,
179                         picture_t *p_outpic )
180 {
181     int i_plane;
182     int i_oldfactor = p_sys->i_factor;
183     int i_newfactor = 128 - i_oldfactor;
184     for( i_plane = 0; i_plane < p_outpic->i_planes; i_plane++ )
185     {
186         uint8_t *p_old, *p_new, *p_out, *p_out_end, *p_out_line_end;
187         const int i_pitch = p_outpic->p[i_plane].i_pitch;
188         const int i_visible_pitch = p_outpic->p[i_plane].i_visible_pitch;
189         const int i_visible_lines = p_outpic->p[i_plane].i_visible_lines;
190
191         p_out = p_outpic->p[i_plane].p_pixels;
192         p_new = p_newpic->p[i_plane].p_pixels;
193         p_old = p_sys->pp_planes[i_plane];
194         p_out_end = p_out + i_pitch * i_visible_lines;
195         while ( p_out < p_out_end )
196         {
197             p_out_line_end = p_out + i_visible_pitch;
198
199             while ( p_out < p_out_line_end )
200             {
201                 *p_out++ = (((*p_old++) * i_oldfactor) +
202                             ((*p_new++) * i_newfactor)) >> 7;
203             }
204
205             p_old += i_pitch - i_visible_pitch;
206             p_new += i_pitch - i_visible_pitch;
207             p_out += i_pitch - i_visible_pitch;
208         }
209     }
210 }
211
212 static void Copy( filter_t *p_filter, picture_t *p_pic )
213 {
214     int i_plane;
215     for( i_plane = 0; i_plane < p_pic->i_planes; i_plane++ )
216     {
217         vlc_memcpy(
218             p_filter->p_sys->pp_planes[i_plane], p_pic->p[i_plane].p_pixels,
219             p_pic->p[i_plane].i_pitch * p_pic->p[i_plane].i_visible_lines );
220     }
221 }
222
223 static int MotionBlurCallback( vlc_object_t *p_this, char const *psz_var,
224                                vlc_value_t oldval, vlc_value_t newval,
225                                void *p_data )
226 {
227     VLC_UNUSED(p_this); VLC_UNUSED(oldval);
228     filter_sys_t *p_sys = (filter_sys_t *)p_data;
229
230     if( !strcmp( psz_var, FILTER_PREFIX "factor" ) )
231     {
232         vlc_spin_lock( &p_sys->lock );
233         p_sys->i_factor = __MIN( 127, __MAX( 1, newval.i_int ) );
234         vlc_spin_unlock( &p_sys->lock );
235     }
236     return VLC_SUCCESS;
237 }