]> git.sesse.net Git - vlc/blob - modules/video_filter/motionblur.c
livehttp: don't remove segment-count to be less than number of segments requested
[vlc] / modules / video_filter / motionblur.c
1 /*****************************************************************************
2  * motionblur.c : motion blur filter for vlc
3  *****************************************************************************
4  * Copyright (C) 2000, 2001, 2002, 2003 VLC authors and VideoLAN
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 it
11  * under the terms of the GNU Lesser General Public License as published by
12  * the Free Software Foundation; either version 2.1 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 Lesser General Public License for more details.
19  *
20  * You should have received a copy of the GNU Lesser General Public License
21  * along with this program; if not, write to the Free Software Foundation,
22  * 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_filter.h>
37 #include <vlc_atomic.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 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,
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     picture_t *p_tmp;
83     bool      b_first;
84     atomic_int i_factor;
85 };
86
87 /*****************************************************************************
88  * Create
89  *****************************************************************************/
90 static int Create( vlc_object_t *p_this )
91 {
92     filter_t *p_filter = (filter_t *)p_this;
93
94     /* Allocate structure */
95     p_filter->p_sys = malloc( sizeof( filter_sys_t ) );
96     if( p_filter->p_sys == NULL )
97         return VLC_ENOMEM;
98
99     p_filter->p_sys->p_tmp = picture_NewFromFormat( &p_filter->fmt_in.video );
100     if( !p_filter->p_sys->p_tmp )
101     {
102         free( p_filter->p_sys );
103         return VLC_ENOMEM;
104     }
105     p_filter->p_sys->b_first = true;
106
107     p_filter->pf_video_filter = Filter;
108
109     config_ChainParse( p_filter, FILTER_PREFIX, ppsz_filter_options,
110                        p_filter->p_cfg );
111
112     atomic_init( &p_filter->p_sys->i_factor,
113              var_CreateGetIntegerCommand( p_filter, FILTER_PREFIX "factor" ) );
114     var_AddCallback( p_filter, FILTER_PREFIX "factor",
115                      MotionBlurCallback, p_filter->p_sys );
116
117
118     return VLC_SUCCESS;
119 }
120
121 /*****************************************************************************
122  * Destroy
123  *****************************************************************************/
124 static void Destroy( vlc_object_t *p_this )
125 {
126     filter_t *p_filter = (filter_t *)p_this;
127
128     var_DelCallback( p_filter, FILTER_PREFIX "factor",
129                      MotionBlurCallback, p_filter->p_sys );
130
131     picture_Release( p_filter->p_sys->p_tmp );
132     free( p_filter->p_sys );
133 }
134
135 /*****************************************************************************
136  * Filter
137  *****************************************************************************/
138 static picture_t *Filter( filter_t *p_filter, picture_t *p_pic )
139 {
140     picture_t * p_outpic;
141     filter_sys_t *p_sys = p_filter->p_sys;
142
143     if( !p_pic ) return NULL;
144
145     p_outpic = filter_NewPicture( p_filter );
146     if( !p_outpic )
147     {
148         picture_Release( p_pic );
149         return NULL;
150     }
151
152     if( p_sys->b_first )
153     {
154         picture_CopyPixels( p_sys->p_tmp, p_pic );
155         p_sys->b_first = false;
156     }
157
158     /* Get a new picture */
159     RenderBlur( p_sys, p_pic, p_outpic );
160
161     picture_CopyPixels( p_sys->p_tmp, p_outpic );
162
163     return CopyInfoAndRelease( p_outpic, p_pic );
164 }
165
166 /*****************************************************************************
167  * RenderBlur: renders a blurred picture
168  *****************************************************************************/
169 static void RenderBlur( filter_sys_t *p_sys, picture_t *p_newpic,
170                         picture_t *p_outpic )
171 {
172     int i_plane;
173     const int i_oldfactor = atomic_load( &p_sys->i_factor );
174     int i_newfactor = 128 - i_oldfactor;
175
176     for( i_plane = 0; i_plane < p_outpic->i_planes; i_plane++ )
177     {
178         uint8_t *p_old, *p_new, *p_out, *p_out_end, *p_out_line_end;
179         const int i_visible_pitch = p_outpic->p[i_plane].i_visible_pitch;
180         const int i_visible_lines = p_outpic->p[i_plane].i_visible_lines;
181
182         p_out = p_outpic->p[i_plane].p_pixels;
183         p_new = p_newpic->p[i_plane].p_pixels;
184         p_old = p_sys->p_tmp->p[i_plane].p_pixels;
185         p_out_end = p_out + p_outpic->p[i_plane].i_pitch * i_visible_lines;
186         while ( p_out < p_out_end )
187         {
188             p_out_line_end = p_out + i_visible_pitch;
189
190             while ( p_out < p_out_line_end )
191             {
192                 *p_out++ = (((*p_old++) * i_oldfactor) +
193                             ((*p_new++) * i_newfactor)) >> 7;
194             }
195
196             p_old += p_sys->p_tmp->p[i_plane].i_pitch - i_visible_pitch;
197             p_new += p_newpic->p[i_plane].i_pitch     - i_visible_pitch;
198             p_out += p_outpic->p[i_plane].i_pitch     - i_visible_pitch;
199         }
200     }
201 }
202
203 static int MotionBlurCallback( vlc_object_t *p_this, char const *psz_var,
204                                vlc_value_t oldval, vlc_value_t newval,
205                                void *p_data )
206 {
207     VLC_UNUSED(p_this); VLC_UNUSED(psz_var); VLC_UNUSED(oldval);
208     filter_sys_t *p_sys = (filter_sys_t *)p_data;
209
210     atomic_store( &p_sys->i_factor, VLC_CLIP( newval.i_int, 1, 127 ) );
211     return VLC_SUCCESS;
212 }