]> git.sesse.net Git - vlc/blob - modules/video_filter/motionblur.c
* ./toolbox: toolbox --update-po now automatically extracts strings from
[vlc] / modules / video_filter / motionblur.c
1 /*****************************************************************************
2  * motion_blur.c : motion blur filter for vlc
3  *****************************************************************************
4  * Copyright (C) 2000, 2001 VideoLAN
5  * $Id: motionblur.c,v 1.5 2003/01/09 17:47:05 sam Exp $
6  *
7  * Authors: Sigmund Augdal <sigmunau@idi.ntnu.no>
8  *
9  * This program is free software; you can redistribute it and/or modify
10  * it under the terms of the GNU General Public License as published by
11  * the Free Software Foundation; either version 2 of the License, or
12  * (at your option) any later version.
13  *
14  * This program is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17  * GNU General Public License for more details.
18  *
19  * You should have received a copy of the GNU General Public License
20  * along with this program; if not, write to the Free Software
21  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111, USA.
22  *****************************************************************************/
23
24 /*****************************************************************************
25  * Preamble
26  *****************************************************************************/
27 #include <stdlib.h>                                      /* malloc(), free() */
28 #include <string.h>
29
30 #include <vlc/vlc.h>
31 #include <vlc/vout.h>
32
33 #include "filter_common.h"
34
35 /*****************************************************************************
36  * Local protypes
37  *****************************************************************************/
38 static int  Create    ( vlc_object_t * );
39 static void Destroy   ( vlc_object_t * );
40
41 static int  Init      ( vout_thread_t * );
42 static void End       ( vout_thread_t * );
43 static void Render    ( vout_thread_t *, picture_t * );
44
45 static void RenderBlur    ( vout_thread_t *, picture_t *, picture_t *, picture_t * );
46 static void CopyPicture ( vout_thread_t*, picture_t *, picture_t * );
47
48 /*****************************************************************************
49  * Module descriptor
50  *****************************************************************************/
51 #define MODE_TEXT N_("Blur factor")
52 #define MODE_LONGTEXT N_("The degree of blurring from 1 to 127")
53
54 vlc_module_begin();
55     add_category_hint( N_("Miscellaneous"), NULL );
56     add_integer( "blur-factor", 80, NULL, MODE_TEXT, MODE_LONGTEXT );
57     set_description( _("Motion blur filter") );
58     set_capability( "video filter", 0 );
59     set_callbacks( Create, Destroy );
60 vlc_module_end();
61
62 /*****************************************************************************
63  * vout_sys_t: Deinterlace video output method descriptor
64  *****************************************************************************
65  * This structure is part of the video output thread descriptor.
66  * It describes the Deinterlace specific properties of an output thread.
67  *****************************************************************************/
68 struct vout_sys_t
69 {
70     int        i_factor;        /* Deinterlace mode */
71     vlc_bool_t b_double_rate; /* Shall we double the framerate? */
72
73     mtime_t    last_date;
74     mtime_t    next_date;
75
76     vout_thread_t *p_vout;
77     picture_t *p_lastpic;
78 };
79
80 /*****************************************************************************
81  * Create: allocates Deinterlace video thread output method
82  *****************************************************************************
83  * This function allocates and initializes a Deinterlace vout method.
84  *****************************************************************************/
85 static int Create( vlc_object_t *p_this )
86 {
87     vout_thread_t *p_vout = (vout_thread_t *)p_this;
88
89     /* Allocate structure */
90     p_vout->p_sys = malloc( sizeof( vout_sys_t ) );
91     if( p_vout->p_sys == NULL )
92     {
93         msg_Err( p_vout, "out of memory" );
94         return VLC_ENOMEM;
95     }
96
97     p_vout->pf_init = Init;
98     p_vout->pf_end = End;
99     p_vout->pf_manage = NULL;
100     p_vout->pf_render = Render;
101     p_vout->pf_display = NULL;
102
103     p_vout->p_sys->i_factor = config_GetInt( p_vout, "blur-factor" );
104     p_vout->p_sys->b_double_rate = 0;
105     p_vout->p_sys->last_date = 0;
106     p_vout->p_sys->p_lastpic = NULL;
107
108     return VLC_SUCCESS;
109 }
110
111 /*****************************************************************************
112  * Init: initialize Deinterlace video thread output method
113  *****************************************************************************/
114 static int Init( vout_thread_t *p_vout )
115 {
116     int i_index;
117     picture_t *p_pic;
118
119     I_OUTPUTPICTURES = 0;
120
121     /* Initialize the output structure, full of directbuffers since we want
122      * the decoder to output directly to our structures. */
123     switch( p_vout->render.i_chroma )
124     {
125         case VLC_FOURCC('I','4','2','0'):
126         case VLC_FOURCC('I','Y','U','V'):
127         case VLC_FOURCC('Y','V','1','2'):
128         case VLC_FOURCC('I','4','2','2'):
129             p_vout->output.i_chroma = p_vout->render.i_chroma;
130             p_vout->output.i_width  = p_vout->render.i_width;
131             p_vout->output.i_height = p_vout->render.i_height;
132             p_vout->output.i_aspect = p_vout->render.i_aspect;
133             break;
134
135         default:
136             return VLC_EGENERIC; /* unknown chroma */
137             break;
138     }
139
140     msg_Dbg( p_vout, "spawning the real video output" );
141
142     switch( p_vout->render.i_chroma )
143     {
144     case VLC_FOURCC('I','4','2','0'):
145     case VLC_FOURCC('I','Y','U','V'):
146     case VLC_FOURCC('Y','V','1','2'):
147         p_vout->p_sys->p_vout = vout_Create( p_vout,
148                            p_vout->output.i_width, p_vout->output.i_height,
149                            p_vout->output.i_chroma, p_vout->output.i_aspect );
150         break;
151     default:
152         break;
153     }
154
155     /* Everything failed */
156     if( p_vout->p_sys->p_vout == NULL )
157     {
158         msg_Err( p_vout, "cannot open vout, aborting" );
159
160         return VLC_EGENERIC;
161     }
162
163     ALLOCATE_DIRECTBUFFERS( VOUT_MAX_PICTURES );
164
165     return VLC_SUCCESS;
166 }
167
168 /*****************************************************************************
169  * End: terminate Deinterlace video thread output method
170  *****************************************************************************/
171 static void End( vout_thread_t *p_vout )
172 {
173     int i_index;
174
175     /* Free the fake output buffers we allocated */
176     for( i_index = I_OUTPUTPICTURES ; i_index ; )
177     {
178         i_index--;
179         free( PP_OUTPUTPICTURE[ i_index ]->p_data_orig );
180     }
181 }
182
183 /*****************************************************************************
184  * Destroy: destroy Deinterlace video thread output method
185  *****************************************************************************
186  * Terminate an output method created by DeinterlaceCreateOutputMethod
187  *****************************************************************************/
188 static void Destroy( vlc_object_t *p_this )
189 {
190     vout_thread_t *p_vout = (vout_thread_t *)p_this;
191
192     vout_Destroy( p_vout->p_sys->p_vout );
193
194     free( p_vout->p_sys );
195 }
196
197 /*****************************************************************************
198  * Render: displays previously rendered output
199  *****************************************************************************
200  * This function send the currently rendered image to Deinterlace image,
201  * waits until it is displayed and switch the two rendering buffers, preparing
202  * next frame.
203  *****************************************************************************/
204 static void Render ( vout_thread_t *p_vout, picture_t *p_pic )
205 {
206     picture_t * p_outpic;
207     while( ( p_outpic = vout_CreatePicture( p_vout->p_sys->p_vout, 0, 0, 0 ) )
208            == NULL )
209     {
210         if( p_vout->b_die || p_vout->b_error )
211         {
212             return;
213         }
214         msleep( VOUT_OUTMEM_SLEEP );
215     }
216     vout_DatePicture( p_vout, p_outpic, p_pic->date );
217
218     if ( p_vout->p_sys->p_lastpic == NULL )
219     {
220         /* Get a new picture */
221         while( ( p_vout->p_sys->p_lastpic =
222                  vout_CreatePicture( p_vout->p_sys->p_vout, 0, 0, 0 ) )
223                == NULL )
224         {
225             if( p_vout->b_die || p_vout->b_error )
226             {
227                 return;
228             }
229             msleep( VOUT_OUTMEM_SLEEP );
230         }
231         CopyPicture( p_vout, p_vout->p_sys->p_lastpic, p_pic );
232         CopyPicture( p_vout, p_outpic, p_pic );
233         vout_DisplayPicture( p_vout->p_sys->p_vout, p_outpic );
234         return;
235     }
236
237     /* Get a new picture */
238     RenderBlur( p_vout, p_vout->p_sys->p_lastpic, p_pic, p_outpic );
239     vout_DestroyPicture( p_vout, p_vout->p_sys->p_lastpic );
240
241
242     /* Get a new picture */
243     while( ( p_vout->p_sys->p_lastpic =
244              vout_CreatePicture( p_vout->p_sys->p_vout, 0, 0, 0 ) )
245            == NULL )
246     {
247         if( p_vout->b_die || p_vout->b_error )
248         {
249             return;
250         }
251         msleep( VOUT_OUTMEM_SLEEP );
252     }
253     CopyPicture( p_vout, p_vout->p_sys->p_lastpic, p_outpic );
254     vout_DisplayPicture( p_vout->p_sys->p_vout, p_outpic );
255 }
256
257 /* FIXME: this is a verbatim copy from src/video_output/vout_pictures.c */
258 /* XXX: the order is fucked up!! */
259 static void CopyPicture( vout_thread_t * p_vout,
260                          picture_t *p_dest, picture_t *p_src )
261 {
262     int i;
263
264     for( i = 0; i < p_src->i_planes ; i++ )
265     {
266         if( p_src->p[i].i_pitch == p_dest->p[i].i_pitch )
267         {
268             /* There are margins, but with the same width : perfect ! */
269             p_vout->p_vlc->pf_memcpy(
270                          p_dest->p[i].p_pixels, p_src->p[i].p_pixels,
271                          p_src->p[i].i_pitch * p_src->p[i].i_lines );
272         }
273         else
274         {
275             /* We need to proceed line by line */
276             uint8_t *p_in = p_src->p[i].p_pixels;
277             uint8_t *p_out = p_dest->p[i].p_pixels;
278             int i_line;
279
280             for( i_line = p_src->p[i].i_lines; i_line--; )
281             {
282                 p_vout->p_vlc->pf_memcpy( p_out, p_in,
283                                           p_src->p[i].i_visible_pitch );
284                 p_in += p_src->p[i].i_pitch;
285                 p_out += p_dest->p[i].i_pitch;
286             }
287         }
288     }
289 }
290
291 /*****************************************************************************
292  * RenderBlur: renders a blurred picture
293  *****************************************************************************/
294 static void RenderBlur( vout_thread_t *p_vout, picture_t *p_oldpic,
295                         picture_t *p_newpic, picture_t *p_outpic )
296 {
297     int i_plane;
298     int i_oldfactor = p_vout->p_sys->i_factor;
299     int i_newfactor = 128 - p_vout->p_sys->i_factor;
300     for( i_plane = 0; i_plane < p_outpic->i_planes; i_plane++ )
301     {
302         uint8_t *p_old, *p_new, *p_out, *p_out_end, *p_out_line_end;
303         p_out = p_outpic->p[i_plane].p_pixels;
304         p_new = p_newpic->p[i_plane].p_pixels;
305         p_old = p_oldpic->p[i_plane].p_pixels;
306         p_out_end = p_out + p_outpic->p[i_plane].i_pitch *
307                              p_outpic->p[i_plane].i_lines;
308         while ( p_out < p_out_end )
309         {
310             p_out_line_end = p_out + p_outpic->p[i_plane].i_visible_pitch;
311
312             while ( p_out < p_out_line_end )
313             {
314                 *p_out++ = (((*p_old++) * i_oldfactor) +
315                             ((*p_new++) * i_newfactor)) >> 7;
316
317 //                *p_out++ = (*p_old++ >> 1) + (*p_new++ >> 1);
318             }
319
320             p_old += p_oldpic->p[i_plane].i_pitch
321                       - p_oldpic->p[i_plane].i_visible_pitch;
322             p_new += p_newpic->p[i_plane].i_pitch
323                       - p_newpic->p[i_plane].i_visible_pitch;
324             p_out += p_outpic->p[i_plane].i_pitch
325                       - p_outpic->p[i_plane].i_visible_pitch;
326         }
327     }
328 }
329