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