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