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