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