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