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