1 /*****************************************************************************
2 * motion_blur.c : motion blur filter for vlc
3 *****************************************************************************
4 * Copyright (C) 2000, 2001, 2002, 2003 VideoLAN
7 * Authors: Sigmund Augdal <sigmunau@idi.ntnu.no>
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.
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.
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 *****************************************************************************/
24 /*****************************************************************************
26 *****************************************************************************/
27 #include <stdlib.h> /* malloc(), free() */
33 #include "filter_common.h"
35 /*****************************************************************************
37 *****************************************************************************/
38 static int Create ( vlc_object_t * );
39 static void Destroy ( vlc_object_t * );
41 static int Init ( vout_thread_t * );
42 static void End ( vout_thread_t * );
43 static void Render ( vout_thread_t *, picture_t * );
45 static void RenderBlur ( vout_thread_t *, picture_t *, picture_t *, picture_t * );
46 static void CopyPicture ( vout_thread_t*, picture_t *, picture_t * );
48 static int SendEvents( vlc_object_t *, char const *,
49 vlc_value_t, vlc_value_t, void * );
51 /*****************************************************************************
53 *****************************************************************************/
54 #define MODE_TEXT N_("Blur factor (1-127)")
55 #define MODE_LONGTEXT N_("The degree of blurring from 1 to 127.")
58 set_description( _("Motion blur filter") );
59 set_capability( "video filter", 0 );
61 add_integer_with_range( "blur-factor", 80, 1, 127, NULL,
62 MODE_TEXT, MODE_LONGTEXT, VLC_FALSE );
64 set_callbacks( Create, Destroy );
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 *****************************************************************************/
75 int i_factor; /* Deinterlace mode */
76 vlc_bool_t b_double_rate; /* Shall we double the framerate? */
81 vout_thread_t *p_vout;
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 )
90 return vout_vaControl( p_vout->p_sys->p_vout, i_query, args );
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 )
100 vout_thread_t *p_vout = (vout_thread_t *)p_this;
102 /* Allocate structure */
103 p_vout->p_sys = malloc( sizeof( vout_sys_t ) );
104 if( p_vout->p_sys == NULL )
106 msg_Err( p_vout, "out of memory" );
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;
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;
125 /*****************************************************************************
126 * Init: initialize Deinterlace video thread output method
127 *****************************************************************************/
128 static int Init( vout_thread_t *p_vout )
132 video_format_t fmt = {0};
134 I_OUTPUTPICTURES = 0;
136 /* Initialize the output structure, full of directbuffers since we want
137 * the decoder to output directly to our structures. */
138 switch( p_vout->render.i_chroma )
140 case VLC_FOURCC('I','4','2','0'):
141 case VLC_FOURCC('I','Y','U','V'):
142 case VLC_FOURCC('Y','V','1','2'):
143 case VLC_FOURCC('I','4','2','2'):
144 p_vout->output.i_chroma = p_vout->render.i_chroma;
145 p_vout->output.i_width = p_vout->render.i_width;
146 p_vout->output.i_height = p_vout->render.i_height;
147 p_vout->output.i_aspect = p_vout->render.i_aspect;
151 return VLC_EGENERIC; /* unknown chroma */
155 msg_Dbg( p_vout, "spawning the real video output" );
157 fmt.i_width = fmt.i_visible_width = p_vout->output.i_width;
158 fmt.i_height = fmt.i_visible_height = p_vout->output.i_height;
159 fmt.i_x_offset = fmt.i_y_offset = 0;
160 fmt.i_chroma = p_vout->output.i_chroma;
161 fmt.i_aspect = p_vout->output.i_aspect;
162 fmt.i_sar_num = p_vout->output.i_aspect * fmt.i_height / fmt.i_width;
163 fmt.i_sar_den = VOUT_ASPECT_FACTOR;
165 switch( p_vout->render.i_chroma )
167 case VLC_FOURCC('I','4','2','0'):
168 case VLC_FOURCC('I','Y','U','V'):
169 case VLC_FOURCC('Y','V','1','2'):
170 p_vout->p_sys->p_vout = vout_Create( p_vout, &fmt );
176 /* Everything failed */
177 if( p_vout->p_sys->p_vout == NULL )
179 msg_Err( p_vout, "cannot open vout, aborting" );
184 ALLOCATE_DIRECTBUFFERS( VOUT_MAX_PICTURES );
186 ADD_CALLBACKS( p_vout->p_sys->p_vout, SendEvents );
188 ADD_PARENT_CALLBACKS( SendEventsToChild );
193 /*****************************************************************************
194 * End: terminate Deinterlace video thread output method
195 *****************************************************************************/
196 static void End( vout_thread_t *p_vout )
200 /* Free the fake output buffers we allocated */
201 for( i_index = I_OUTPUTPICTURES ; i_index ; )
204 free( PP_OUTPUTPICTURE[ i_index ]->p_data_orig );
208 /*****************************************************************************
209 * Destroy: destroy Deinterlace video thread output method
210 *****************************************************************************
211 * Terminate an output method created by DeinterlaceCreateOutputMethod
212 *****************************************************************************/
213 static void Destroy( vlc_object_t *p_this )
215 vout_thread_t *p_vout = (vout_thread_t *)p_this;
217 if( p_vout->p_sys->p_vout )
219 DEL_CALLBACKS( p_vout->p_sys->p_vout, SendEvents );
220 vlc_object_detach( p_vout->p_sys->p_vout );
221 vout_Destroy( p_vout->p_sys->p_vout );
224 DEL_PARENT_CALLBACKS( SendEventsToChild );
226 free( p_vout->p_sys );
229 /*****************************************************************************
230 * Render: displays previously rendered output
231 *****************************************************************************
232 * This function send the currently rendered image to Deinterlace image,
233 * waits until it is displayed and switch the two rendering buffers, preparing
235 *****************************************************************************/
236 static void Render ( vout_thread_t *p_vout, picture_t *p_pic )
238 picture_t * p_outpic;
239 while( ( p_outpic = vout_CreatePicture( p_vout->p_sys->p_vout, 0, 0, 0 ) )
242 if( p_vout->b_die || p_vout->b_error )
246 msleep( VOUT_OUTMEM_SLEEP );
248 vout_DatePicture( p_vout, p_outpic, p_pic->date );
250 if ( p_vout->p_sys->p_lastpic == NULL )
252 /* Get a new picture */
253 while( ( p_vout->p_sys->p_lastpic =
254 vout_CreatePicture( p_vout->p_sys->p_vout, 0, 0, 0 ) )
257 if( p_vout->b_die || p_vout->b_error )
261 msleep( VOUT_OUTMEM_SLEEP );
263 CopyPicture( p_vout, p_vout->p_sys->p_lastpic, p_pic );
264 CopyPicture( p_vout, p_outpic, p_pic );
265 vout_DisplayPicture( p_vout->p_sys->p_vout, p_outpic );
269 /* Get a new picture */
270 RenderBlur( p_vout, p_vout->p_sys->p_lastpic, p_pic, p_outpic );
271 vout_DestroyPicture( p_vout, p_vout->p_sys->p_lastpic );
274 /* Get a new picture */
275 while( ( p_vout->p_sys->p_lastpic =
276 vout_CreatePicture( p_vout->p_sys->p_vout, 0, 0, 0 ) )
279 if( p_vout->b_die || p_vout->b_error )
283 msleep( VOUT_OUTMEM_SLEEP );
285 CopyPicture( p_vout, p_vout->p_sys->p_lastpic, p_outpic );
286 vout_DisplayPicture( p_vout->p_sys->p_vout, p_outpic );
289 /* FIXME: this is a verbatim copy from src/video_output/vout_pictures.c */
290 /* XXX: the order is fucked up!! */
291 static void CopyPicture( vout_thread_t * p_vout,
292 picture_t *p_dest, picture_t *p_src )
296 for( i = 0; i < p_src->i_planes ; i++ )
298 if( p_src->p[i].i_pitch == p_dest->p[i].i_pitch )
300 /* There are margins, but with the same width : perfect ! */
301 p_vout->p_vlc->pf_memcpy(
302 p_dest->p[i].p_pixels, p_src->p[i].p_pixels,
303 p_src->p[i].i_pitch * p_src->p[i].i_visible_lines );
307 /* We need to proceed line by line */
308 uint8_t *p_in = p_src->p[i].p_pixels;
309 uint8_t *p_out = p_dest->p[i].p_pixels;
312 for( i_line = p_src->p[i].i_visible_lines; i_line--; )
314 p_vout->p_vlc->pf_memcpy( p_out, p_in,
315 p_src->p[i].i_visible_pitch );
316 p_in += p_src->p[i].i_pitch;
317 p_out += p_dest->p[i].i_pitch;
323 /*****************************************************************************
324 * RenderBlur: renders a blurred picture
325 *****************************************************************************/
326 static void RenderBlur( vout_thread_t *p_vout, picture_t *p_oldpic,
327 picture_t *p_newpic, picture_t *p_outpic )
330 int i_oldfactor = p_vout->p_sys->i_factor;
331 int i_newfactor = 128 - p_vout->p_sys->i_factor;
332 for( i_plane = 0; i_plane < p_outpic->i_planes; i_plane++ )
334 uint8_t *p_old, *p_new, *p_out, *p_out_end, *p_out_line_end;
335 p_out = p_outpic->p[i_plane].p_pixels;
336 p_new = p_newpic->p[i_plane].p_pixels;
337 p_old = p_oldpic->p[i_plane].p_pixels;
338 p_out_end = p_out + p_outpic->p[i_plane].i_pitch *
339 p_outpic->p[i_plane].i_visible_lines;
340 while ( p_out < p_out_end )
342 p_out_line_end = p_out + p_outpic->p[i_plane].i_visible_pitch;
344 while ( p_out < p_out_line_end )
346 *p_out++ = (((*p_old++) * i_oldfactor) +
347 ((*p_new++) * i_newfactor)) >> 7;
349 // *p_out++ = (*p_old++ >> 1) + (*p_new++ >> 1);
352 p_old += p_oldpic->p[i_plane].i_pitch
353 - p_oldpic->p[i_plane].i_visible_pitch;
354 p_new += p_newpic->p[i_plane].i_pitch
355 - p_newpic->p[i_plane].i_visible_pitch;
356 p_out += p_outpic->p[i_plane].i_pitch
357 - p_outpic->p[i_plane].i_visible_pitch;
362 /*****************************************************************************
363 * SendEvents: forward mouse and keyboard events to the parent p_vout
364 *****************************************************************************/
365 static int SendEvents( vlc_object_t *p_this, char const *psz_var,
366 vlc_value_t oldval, vlc_value_t newval, void *p_data )
368 var_Set( (vlc_object_t *)p_data, psz_var, newval );
373 /*****************************************************************************
374 * SendEventsToChild: forward events to the child/children vout
375 *****************************************************************************/
376 static int SendEventsToChild( vlc_object_t *p_this, char const *psz_var,
377 vlc_value_t oldval, vlc_value_t newval, void *p_data )
379 vout_thread_t *p_vout = (vout_thread_t *)p_this;
380 var_Set( p_vout->p_sys->p_vout, psz_var, newval );