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