]> git.sesse.net Git - vlc/blob - modules/video_filter/motionblur.c
Remove printf
[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     p_filter->p_sys->pp_planes = NULL;
119     p_filter->p_sys->i_planes = 0;
120
121     return VLC_SUCCESS;
122 }
123
124 /*****************************************************************************
125  * Destroy
126  *****************************************************************************/
127 static void Destroy( vlc_object_t *p_this )
128 {
129     filter_t *p_filter = (filter_t *)p_this;
130
131     while( p_filter->p_sys->i_planes-- )
132         free( p_filter->p_sys->pp_planes[p_filter->p_sys->i_planes] );
133     free( p_filter->p_sys->pp_planes );
134     free( p_filter->p_sys );
135 }
136
137 #define RELEASE( pic ) \
138         if( pic ->pf_release ) \
139             pic ->pf_release( pic );
140
141 #define INITPIC( dst, src ) \
142     dst ->date = src ->date; \
143     dst ->b_force = src ->b_force; \
144     dst ->i_nb_fields = src ->i_nb_fields; \
145     dst ->b_progressive = src->b_progressive; \
146     dst ->b_top_field_first = src ->b_top_field_first;
147
148 /*****************************************************************************
149  * Filter
150  *****************************************************************************/
151 static picture_t *Filter( filter_t *p_filter, picture_t *p_pic )
152 {
153     picture_t * p_outpic;
154     filter_sys_t *p_sys = p_filter->p_sys;
155
156     if( !p_pic ) return NULL;
157
158     p_outpic = p_filter->pf_vout_buffer_new( p_filter );
159     if( !p_outpic )
160     {
161         msg_Warn( p_filter, "can't get output picture" );
162         RELEASE( p_pic );
163         return NULL;
164     }
165     INITPIC( p_outpic, p_pic );
166
167     if( !p_sys->pp_planes )
168     {
169         /* initialise our picture buffer */
170         int i_plane;
171         p_sys->i_planes = p_pic->i_planes;
172         p_sys->pp_planes =
173             (uint8_t**)malloc( p_sys->i_planes * sizeof( uint8_t * ) );
174         for( i_plane = 0; i_plane < p_pic->i_planes; i_plane++ )
175         {
176             p_sys->pp_planes[i_plane] = (uint8_t*)malloc(
177                 p_pic->p[i_plane].i_pitch * p_pic->p[i_plane].i_visible_lines );
178         }
179         Copy( p_filter, p_sys->pp_planes, p_pic );
180     }
181
182     /* Get a new picture */
183     RenderBlur( p_sys, p_pic, p_outpic );
184     Copy( p_filter, p_sys->pp_planes, p_sys->b_persistant ? p_outpic : p_pic );
185
186     RELEASE( p_pic );
187     return p_outpic;
188 }
189
190 /*****************************************************************************
191  * RenderBlur: renders a blurred picture
192  *****************************************************************************/
193 static void RenderBlur( filter_sys_t *p_sys, picture_t *p_newpic,
194                         picture_t *p_outpic )
195 {
196     int i_plane;
197     int i_oldfactor = p_sys->i_factor;
198     int i_newfactor = 128 - i_oldfactor;
199     for( i_plane = 0; i_plane < p_outpic->i_planes; i_plane++ )
200     {
201         uint8_t *p_old, *p_new, *p_out, *p_out_end, *p_out_line_end;
202         const int i_pitch = p_outpic->p[i_plane].i_pitch;
203         const int i_visible_pitch = p_outpic->p[i_plane].i_visible_pitch;
204         const int i_visible_lines = p_outpic->p[i_plane].i_visible_lines;
205
206         p_out = p_outpic->p[i_plane].p_pixels;
207         p_new = p_newpic->p[i_plane].p_pixels;
208         p_old = p_sys->pp_planes[i_plane];
209         p_out_end = p_out + i_pitch * i_visible_lines;
210         while ( p_out < p_out_end )
211         {
212             p_out_line_end = p_out + i_visible_pitch;
213
214             while ( p_out < p_out_line_end )
215             {
216                 *p_out++ = (((*p_old++) * i_oldfactor) +
217                             ((*p_new++) * i_newfactor)) >> 7;
218             }
219
220             p_old += i_pitch - i_visible_pitch;
221             p_new += i_pitch - i_visible_pitch;
222             p_out += i_pitch - i_visible_pitch;
223         }
224     }
225 }
226
227 static void Copy( filter_t *p_filter, uint8_t **pp_planes, picture_t *p_pic )
228 {
229     int i_plane;
230     for( i_plane = 0; i_plane < p_pic->i_planes; i_plane++ )
231     {
232         p_filter->p_libvlc->pf_memcpy(
233             p_filter->p_sys->pp_planes[i_plane], p_pic->p[i_plane].p_pixels,
234             p_pic->p[i_plane].i_pitch * p_pic->p[i_plane].i_visible_lines );
235     }
236 }