]> git.sesse.net Git - vlc/blob - modules/video_filter/motionblur.c
015ba58133b5ee2cf6c5a98c007cf3a4276bbfac
[vlc] / modules / video_filter / motionblur.c
1 /*****************************************************************************
2  * motion_blur.c : motion blur filter for vlc
3  *****************************************************************************
4  * Copyright (C) 2000, 2001, 2002, 2003 VideoLAN
5  * $Id$
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 static int  SendEvents( vlc_object_t *, char const *,
49                         vlc_value_t, vlc_value_t, void * );
50
51 /*****************************************************************************
52  * Module descriptor
53  *****************************************************************************/
54 #define MODE_TEXT N_("Blur factor (1-127)")
55 #define MODE_LONGTEXT N_("The degree of blurring from 1 to 127.")
56
57 vlc_module_begin();
58     set_description( _("Motion blur filter") );
59     set_capability( "video filter", 0 );
60
61     add_integer_with_range( "blur-factor", 80, 1, 127, NULL,
62         MODE_TEXT, MODE_LONGTEXT, VLC_FALSE );
63     
64     set_callbacks( Create, Destroy );
65 vlc_module_end();
66
67 /*****************************************************************************
68  * vout_sys_t: Deinterlace video output method descriptor
69  *****************************************************************************
70  * This structure is part of the video output thread descriptor.
71  * It describes the Deinterlace specific properties of an output thread.
72  *****************************************************************************/
73 struct vout_sys_t
74 {
75     int        i_factor;        /* Deinterlace mode */
76     vlc_bool_t b_double_rate; /* Shall we double the framerate? */
77
78     mtime_t    last_date;
79     mtime_t    next_date;
80
81     vout_thread_t *p_vout;
82     picture_t *p_lastpic;
83 };
84
85 /*****************************************************************************
86  * Control: control facility for the vout (forwards to child vout)
87  *****************************************************************************/
88 static int Control( vout_thread_t *p_vout, int i_query, va_list args )
89 {
90     return vout_vaControl( p_vout->p_sys->p_vout, i_query, args );
91 }
92
93 /*****************************************************************************
94  * Create: allocates Deinterlace video thread output method
95  *****************************************************************************
96  * This function allocates and initializes a Deinterlace vout method.
97  *****************************************************************************/
98 static int Create( vlc_object_t *p_this )
99 {
100     vout_thread_t *p_vout = (vout_thread_t *)p_this;
101
102     /* Allocate structure */
103     p_vout->p_sys = malloc( sizeof( vout_sys_t ) );
104     if( p_vout->p_sys == NULL )
105     {
106         msg_Err( p_vout, "out of memory" );
107         return VLC_ENOMEM;
108     }
109
110     p_vout->pf_init = Init;
111     p_vout->pf_end = End;
112     p_vout->pf_manage = NULL;
113     p_vout->pf_render = Render;
114     p_vout->pf_display = NULL;
115     p_vout->pf_control = Control;
116
117     p_vout->p_sys->i_factor = config_GetInt( p_vout, "blur-factor" );
118     p_vout->p_sys->b_double_rate = 0;
119     p_vout->p_sys->last_date = 0;
120     p_vout->p_sys->p_lastpic = NULL;
121
122     return VLC_SUCCESS;
123 }
124
125 /*****************************************************************************
126  * Init: initialize Deinterlace video thread output method
127  *****************************************************************************/
128 static int Init( vout_thread_t *p_vout )
129 {
130     int i_index;
131     picture_t *p_pic;
132
133     I_OUTPUTPICTURES = 0;
134
135     /* Initialize the output structure, full of directbuffers since we want
136      * the decoder to output directly to our structures. */
137     switch( p_vout->render.i_chroma )
138     {
139         case VLC_FOURCC('I','4','2','0'):
140         case VLC_FOURCC('I','Y','U','V'):
141         case VLC_FOURCC('Y','V','1','2'):
142         case VLC_FOURCC('I','4','2','2'):
143             p_vout->output.i_chroma = p_vout->render.i_chroma;
144             p_vout->output.i_width  = p_vout->render.i_width;
145             p_vout->output.i_height = p_vout->render.i_height;
146             p_vout->output.i_aspect = p_vout->render.i_aspect;
147             break;
148
149         default:
150             return VLC_EGENERIC; /* unknown chroma */
151             break;
152     }
153
154     msg_Dbg( p_vout, "spawning the real video output" );
155
156     switch( p_vout->render.i_chroma )
157     {
158     case VLC_FOURCC('I','4','2','0'):
159     case VLC_FOURCC('I','Y','U','V'):
160     case VLC_FOURCC('Y','V','1','2'):
161         p_vout->p_sys->p_vout = vout_Create( p_vout,
162                            p_vout->output.i_width, p_vout->output.i_height,
163                            p_vout->output.i_chroma, p_vout->output.i_aspect );
164         break;
165     default:
166         break;
167     }
168
169     /* Everything failed */
170     if( p_vout->p_sys->p_vout == NULL )
171     {
172         msg_Err( p_vout, "cannot open vout, aborting" );
173
174         return VLC_EGENERIC;
175     }
176
177     ALLOCATE_DIRECTBUFFERS( VOUT_MAX_PICTURES );
178
179     ADD_CALLBACKS( p_vout->p_sys->p_vout, SendEvents );
180
181     ADD_PARENT_CALLBACKS( SendEventsToChild );
182
183     return VLC_SUCCESS;
184 }
185
186 /*****************************************************************************
187  * End: terminate Deinterlace video thread output method
188  *****************************************************************************/
189 static void End( vout_thread_t *p_vout )
190 {
191     int i_index;
192
193     /* Free the fake output buffers we allocated */
194     for( i_index = I_OUTPUTPICTURES ; i_index ; )
195     {
196         i_index--;
197         free( PP_OUTPUTPICTURE[ i_index ]->p_data_orig );
198     }
199 }
200
201 /*****************************************************************************
202  * Destroy: destroy Deinterlace video thread output method
203  *****************************************************************************
204  * Terminate an output method created by DeinterlaceCreateOutputMethod
205  *****************************************************************************/
206 static void Destroy( vlc_object_t *p_this )
207 {
208     vout_thread_t *p_vout = (vout_thread_t *)p_this;
209
210     if( p_vout->p_sys->p_vout )
211     {
212         DEL_CALLBACKS( p_vout->p_sys->p_vout, SendEvents );
213         vlc_object_detach( p_vout->p_sys->p_vout );
214         vout_Destroy( p_vout->p_sys->p_vout );
215     }
216
217     DEL_PARENT_CALLBACKS( SendEventsToChild );
218
219     free( p_vout->p_sys );
220 }
221
222 /*****************************************************************************
223  * Render: displays previously rendered output
224  *****************************************************************************
225  * This function send the currently rendered image to Deinterlace image,
226  * waits until it is displayed and switch the two rendering buffers, preparing
227  * next frame.
228  *****************************************************************************/
229 static void Render ( vout_thread_t *p_vout, picture_t *p_pic )
230 {
231     picture_t * p_outpic;
232     while( ( p_outpic = vout_CreatePicture( p_vout->p_sys->p_vout, 0, 0, 0 ) )
233            == NULL )
234     {
235         if( p_vout->b_die || p_vout->b_error )
236         {
237             return;
238         }
239         msleep( VOUT_OUTMEM_SLEEP );
240     }
241     vout_DatePicture( p_vout, p_outpic, p_pic->date );
242
243     if ( p_vout->p_sys->p_lastpic == NULL )
244     {
245         /* Get a new picture */
246         while( ( p_vout->p_sys->p_lastpic =
247                  vout_CreatePicture( p_vout->p_sys->p_vout, 0, 0, 0 ) )
248                == NULL )
249         {
250             if( p_vout->b_die || p_vout->b_error )
251             {
252                 return;
253             }
254             msleep( VOUT_OUTMEM_SLEEP );
255         }
256         CopyPicture( p_vout, p_vout->p_sys->p_lastpic, p_pic );
257         CopyPicture( p_vout, p_outpic, p_pic );
258         vout_DisplayPicture( p_vout->p_sys->p_vout, p_outpic );
259         return;
260     }
261
262     /* Get a new picture */
263     RenderBlur( p_vout, p_vout->p_sys->p_lastpic, p_pic, p_outpic );
264     vout_DestroyPicture( p_vout, p_vout->p_sys->p_lastpic );
265
266
267     /* Get a new picture */
268     while( ( p_vout->p_sys->p_lastpic =
269              vout_CreatePicture( p_vout->p_sys->p_vout, 0, 0, 0 ) )
270            == NULL )
271     {
272         if( p_vout->b_die || p_vout->b_error )
273         {
274             return;
275         }
276         msleep( VOUT_OUTMEM_SLEEP );
277     }
278     CopyPicture( p_vout, p_vout->p_sys->p_lastpic, p_outpic );
279     vout_DisplayPicture( p_vout->p_sys->p_vout, p_outpic );
280 }
281
282 /* FIXME: this is a verbatim copy from src/video_output/vout_pictures.c */
283 /* XXX: the order is fucked up!! */
284 static void CopyPicture( vout_thread_t * p_vout,
285                          picture_t *p_dest, picture_t *p_src )
286 {
287     int i;
288
289     for( i = 0; i < p_src->i_planes ; i++ )
290     {
291         if( p_src->p[i].i_pitch == p_dest->p[i].i_pitch )
292         {
293             /* There are margins, but with the same width : perfect ! */
294             p_vout->p_vlc->pf_memcpy(
295                          p_dest->p[i].p_pixels, p_src->p[i].p_pixels,
296                          p_src->p[i].i_pitch * p_src->p[i].i_visible_lines );
297         }
298         else
299         {
300             /* We need to proceed line by line */
301             uint8_t *p_in = p_src->p[i].p_pixels;
302             uint8_t *p_out = p_dest->p[i].p_pixels;
303             int i_line;
304
305             for( i_line = p_src->p[i].i_visible_lines; i_line--; )
306             {
307                 p_vout->p_vlc->pf_memcpy( p_out, p_in,
308                                           p_src->p[i].i_visible_pitch );
309                 p_in += p_src->p[i].i_pitch;
310                 p_out += p_dest->p[i].i_pitch;
311             }
312         }
313     }
314 }
315
316 /*****************************************************************************
317  * RenderBlur: renders a blurred picture
318  *****************************************************************************/
319 static void RenderBlur( vout_thread_t *p_vout, picture_t *p_oldpic,
320                         picture_t *p_newpic, picture_t *p_outpic )
321 {
322     int i_plane;
323     int i_oldfactor = p_vout->p_sys->i_factor;
324     int i_newfactor = 128 - p_vout->p_sys->i_factor;
325     for( i_plane = 0; i_plane < p_outpic->i_planes; i_plane++ )
326     {
327         uint8_t *p_old, *p_new, *p_out, *p_out_end, *p_out_line_end;
328         p_out = p_outpic->p[i_plane].p_pixels;
329         p_new = p_newpic->p[i_plane].p_pixels;
330         p_old = p_oldpic->p[i_plane].p_pixels;
331         p_out_end = p_out + p_outpic->p[i_plane].i_pitch *
332                              p_outpic->p[i_plane].i_visible_lines;
333         while ( p_out < p_out_end )
334         {
335             p_out_line_end = p_out + p_outpic->p[i_plane].i_visible_pitch;
336
337             while ( p_out < p_out_line_end )
338             {
339                 *p_out++ = (((*p_old++) * i_oldfactor) +
340                             ((*p_new++) * i_newfactor)) >> 7;
341
342 //                *p_out++ = (*p_old++ >> 1) + (*p_new++ >> 1);
343             }
344
345             p_old += p_oldpic->p[i_plane].i_pitch
346                       - p_oldpic->p[i_plane].i_visible_pitch;
347             p_new += p_newpic->p[i_plane].i_pitch
348                       - p_newpic->p[i_plane].i_visible_pitch;
349             p_out += p_outpic->p[i_plane].i_pitch
350                       - p_outpic->p[i_plane].i_visible_pitch;
351         }
352     }
353 }
354
355 /*****************************************************************************
356  * SendEvents: forward mouse and keyboard events to the parent p_vout
357  *****************************************************************************/
358 static int SendEvents( vlc_object_t *p_this, char const *psz_var,
359                        vlc_value_t oldval, vlc_value_t newval, void *p_data )
360 {
361     var_Set( (vlc_object_t *)p_data, psz_var, newval );
362
363     return VLC_SUCCESS;
364 }
365
366 /*****************************************************************************
367  * SendEventsToChild: forward events to the child/children vout
368  *****************************************************************************/
369 static int SendEventsToChild( vlc_object_t *p_this, char const *psz_var,
370                        vlc_value_t oldval, vlc_value_t newval, void *p_data )
371 {
372     vout_thread_t *p_vout = (vout_thread_t *)p_this;
373     var_Set( p_vout->p_sys->p_vout, psz_var, newval );
374     return VLC_SUCCESS;
375 }