]> git.sesse.net Git - vlc/blob - modules/video_filter/ball.c
New video filter : augmented reality. Add a moving ball to the video and handle bounc...
[vlc] / modules / video_filter / ball.c
1 /*****************************************************************************
2  * ball.c : Augmented reality ball video filter module
3  *****************************************************************************
4  * Copyright (C) 2000-2009 the VideoLAN team
5  *
6  * Author: Adrien Maglo <magsoft@videolan.org>
7  *
8  * The Canny edge detection algorithm comes from gradient.c which was
9  * writen by:
10  *         Samuel Hocevar <sam@zoy.org>
11  *         Antoine Cellerier <dionoea -at- videolan -dot- org>
12  *
13  * This program is free software; you can redistribute it and/or modify
14  * it under the terms of the GNU General Public License as published by
15  * the Free Software Foundation; either version 2 of the License, or
16  * (at your option) any later version.
17  *
18  * This program is distributed in the hope that it will be useful,
19  * but WITHOUT ANY WARRANTY; without even the implied warranty of
20  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
21  * GNU General Public License for more details.
22  *
23  * You should have received a copy of the GNU General Public License
24  * along with this program; if not, write to the Free Software
25  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
26  *****************************************************************************/
27
28
29 /*****************************************************************************
30  * Preamble
31  *****************************************************************************/
32
33 #ifdef HAVE_CONFIG_H
34 # include "config.h"
35 #endif
36
37 #include <math.h> /* sin(), cos(), asin() */
38
39 #include <vlc_common.h>
40 #include <vlc_plugin.h>
41 #include <vlc_sout.h>
42 #include <vlc_vout.h>
43
44 #include "vlc_filter.h"
45 #include "filter_picture.h"
46 #include "vlc_image.h"
47
48 enum { RED, GREEN, BLUE, WHITE };
49
50 typedef struct
51 {
52     uint8_t comp1;
53     uint8_t comp2;
54     uint8_t comp3;
55 }COLOR;
56
57 static COLOR colorList[4];
58
59 #define COLORS_RGB \
60     colorList[RED].comp1 = 255; colorList[RED].comp2 = 0;        \
61                                 colorList[RED].comp3 = 0;        \
62     colorList[GREEN].comp1 = 0; colorList[GREEN].comp2 = 255;    \
63                                colorList[GREEN].comp3 = 0;       \
64     colorList[BLUE].comp1 = 0; colorList[BLUE].comp2 = 0;        \
65                                colorList[BLUE].comp3 = 255;      \
66     colorList[WHITE].comp1 = 255; colorList[WHITE].comp2 = 255;  \
67                                   colorList[WHITE].comp3 = 255;
68
69 #define COLORS_YUV \
70     colorList[RED].comp1 = 82; colorList[RED].comp2 = 240;        \
71                                 colorList[RED].comp3 = 90;        \
72     colorList[GREEN].comp1 = 145; colorList[GREEN].comp2 = 34;    \
73                                colorList[GREEN].comp3 = 54 ;      \
74     colorList[BLUE].comp1 = 41; colorList[BLUE].comp2 = 146;      \
75                                colorList[BLUE].comp3 = 240;       \
76     colorList[WHITE].comp1 = 255; colorList[WHITE].comp2 = 128;   \
77                                   colorList[WHITE].comp3 = 128;
78
79
80 /*****************************************************************************
81  * Local prototypes
82  *****************************************************************************/
83 static int  Create    ( vlc_object_t * );
84 static void Destroy   ( vlc_object_t * );
85
86 static picture_t *Filter( filter_t *, picture_t * );
87
88 static void drawBall( filter_sys_t *p_sys, picture_t *p_outpic );
89 static void drawPixelRGB24( filter_sys_t *p_sys, picture_t *p_outpic,
90                             uint8_t R, uint8_t G, uint8_t B,
91                             int x, int y, bool b_skip );
92 static void drawPixelI420( filter_sys_t *p_sys, picture_t *p_outpic,
93                            uint8_t Y, uint8_t U, uint8_t V,
94                            int x, int y, bool b_skip );
95 static void drawPixelPacked( filter_sys_t *p_sys, picture_t *p_outpic,
96                              uint8_t Y, uint8_t U, uint8_t V,
97                              int x, int y, bool b_skip );
98
99 static void FilterBall( filter_t *, picture_t *, picture_t * );
100 static int ballCallback( vlc_object_t *, char const *,
101                          vlc_value_t, vlc_value_t,
102                          void * );
103 static int getBallColor( vlc_object_t *p_this, char const *psz_newval );
104
105
106 /*****************************************************************************
107  * Module descriptor
108  *****************************************************************************/
109 #define BALL_COLOR_TEXT N_("Ball color")
110 #define BALL_COLOR_LONGTEXT N_("Ball color, one of \"red\", \"blue\" and \"green\".")
111
112 #define EDGE_VISIBLE_TEXT N_("Edge visible")
113 #define EDGE_VISIBLE_LONGTEXT N_("Set edge visibility.")
114
115 #define BALL_SPEED_TEXT N_("Ball speed")
116 #define BALL_SPEED_LONGTEXT N_("Set ball speed, the displacement value \
117                                 in number of pixels by frame.")
118
119 #define BALL_SIZE_TEXT N_("Ball size")
120 #define BALL_SIZE_LONGTEXT N_("Set ball size giving its radius in number \
121                                 of pixels")
122
123 #define GRAD_THRESH_TEXT N_("Gradient threshold")
124 #define GRAD_THRESH_LONGTEXT N_("Set gradient threshold for edge computation.")
125
126 #define FILTER_PREFIX "ball-"
127
128 static const char *const mode_list[] = { "red", "green", "blue", "white" };
129 static const char *const mode_list_text[] = { N_("Red"), N_("Green"),
130                                               N_("Blue"), N_("White") };
131
132 vlc_module_begin ()
133     set_description( N_("Ball video filter") )
134     set_shortname( N_( "Ball" ))
135     set_capability( "video filter2", 0 )
136     set_category( CAT_VIDEO )
137     set_subcategory( SUBCAT_VIDEO_VFILTER )
138
139     add_string( FILTER_PREFIX "ball-color", "ball-color", NULL,
140                 BALL_COLOR_TEXT, BALL_COLOR_LONGTEXT, false )
141     change_string_list( mode_list, mode_list_text, 0 )
142
143     add_integer_with_range( FILTER_PREFIX "ball-speed", 4, 1, 15, NULL,
144                             BALL_SPEED_TEXT, BALL_SPEED_LONGTEXT, false )
145
146     add_integer_with_range( FILTER_PREFIX "ball-size", 10, 5, 30, NULL,
147                             BALL_SIZE_TEXT, BALL_SIZE_LONGTEXT, false )
148
149     add_integer_with_range( FILTER_PREFIX "gradient-threshold", 40, 1, 200, NULL,
150                             GRAD_THRESH_TEXT, GRAD_THRESH_LONGTEXT, false )
151
152     add_bool( FILTER_PREFIX "edge-visible", 1, NULL,
153               EDGE_VISIBLE_TEXT, EDGE_VISIBLE_LONGTEXT, true )
154
155     add_shortcut( "ball" )
156     set_callbacks( Create, Destroy )
157 vlc_module_end ()
158
159 static const char *const ppsz_filter_options[] = {
160     "ball-color", "ball-speed", "ball-size",
161     "gradient-threshold", "edge-visible", NULL
162 };
163
164
165 /*****************************************************************************
166 * filter_sys_t: Distort video output method descriptor
167 *****************************************************************************
168 * This structure is part of the video output thread descriptor.
169 * It describes the Distort specific properties of an output thread.
170  *****************************************************************************/
171 struct filter_sys_t
172 {
173     vlc_mutex_t lock;
174
175     int ballColor;
176
177     image_handler_t *p_image;
178
179     /* Ball position */
180     int i_ball_x;
181     int i_ball_y;
182
183     int i_ballSpeed;
184
185     int i_ballSize;
186
187     bool b_edgeVisible;
188
189     /* Offsets for YUV packed chroma */
190     int i_y_offset;
191     int i_u_offset;
192     int i_v_offset;
193
194     /* Gradient values */
195     uint32_t *p_smooth;
196     int32_t *p_grad_x;
197     int32_t *p_grad_y;
198
199     /* Gradient threshold */
200     int i_gradThresh;
201
202     /* Motion vectors */
203     float f_lastVect_x;
204     float f_lastVect_y;
205
206     float f_newVect_x;
207     float f_newVect_y;
208
209     float f_contVect_x;
210     float f_contVect_y;
211
212     /* Pointer on drawing function */
213     void ( *drawingPixelFunction )( filter_sys_t *, picture_t *,
214                                     uint8_t, uint8_t, uint8_t,
215                                     int, int, bool );
216 };
217
218
219 /*****************************************************************************
220 * Create: allocates Distort video thread output method
221 *****************************************************************************
222 * This function allocates and initializes a Distort vout method.
223 *****************************************************************************/
224 static int Create( vlc_object_t *p_this )
225 {
226     filter_t *p_filter = (filter_t *)p_this;
227     char *psz_method;
228
229     /* Allocate structure */
230     p_filter->p_sys = malloc( sizeof( filter_sys_t ) );
231     if( p_filter->p_sys == NULL )
232         return VLC_ENOMEM;
233
234     switch( p_filter->fmt_in.video.i_chroma )
235     {
236         case VLC_CODEC_I420:
237         case VLC_CODEC_J420:
238             p_filter->p_sys->drawingPixelFunction = drawPixelI420;
239             COLORS_YUV
240             break;
241         CASE_PACKED_YUV_422
242             p_filter->p_sys->drawingPixelFunction = drawPixelPacked;
243             COLORS_YUV
244             GetPackedYuvOffsets( p_filter->fmt_in.video.i_chroma,
245                                  &p_filter->p_sys->i_y_offset,
246                                  &p_filter->p_sys->i_u_offset,
247                                  &p_filter->p_sys->i_v_offset );
248             break;
249         case VLC_CODEC_RGB24:
250             p_filter->p_sys->drawingPixelFunction = drawPixelRGB24;
251             COLORS_RGB
252             break;
253         default:
254             msg_Err( p_filter, "Unsupported input chroma (%4s)",
255                      (char*)&(p_filter->fmt_in.video.i_chroma) );
256             return VLC_EGENERIC;
257     }
258
259     p_filter->p_sys->p_image = image_HandlerCreate( p_filter );
260     if( p_filter->p_sys->p_image == NULL )
261         return VLC_EGENERIC;
262
263     p_filter->pf_video_filter = Filter;
264
265     config_ChainParse( p_filter, FILTER_PREFIX, ppsz_filter_options,
266                        p_filter->p_cfg );
267
268     if( !(psz_method =
269         var_CreateGetNonEmptyStringCommand( p_filter,
270                                             FILTER_PREFIX "ball-color" ) ) )
271     {
272         msg_Err( p_filter, "configuration variable "
273                  FILTER_PREFIX "ball-color empty" );
274         p_filter->p_sys->ballColor = RED;
275     }
276     else
277         p_filter->p_sys->ballColor = getBallColor( p_this, psz_method );
278
279     free( psz_method );
280
281     p_filter->p_sys->i_ballSize =
282             var_CreateGetIntegerCommand( p_filter, FILTER_PREFIX "ball-size" );
283     p_filter->p_sys->i_ballSpeed =
284             var_CreateGetIntegerCommand( p_filter, FILTER_PREFIX "ball-speed" );
285     p_filter->p_sys->b_edgeVisible =
286             var_CreateGetBoolCommand( p_filter, FILTER_PREFIX "edge-visible" );
287     p_filter->p_sys->i_gradThresh =
288             var_CreateGetIntegerCommand( p_filter, FILTER_PREFIX "gradient-threshold" );
289
290     vlc_mutex_init( &p_filter->p_sys->lock );
291
292     var_AddCallback( p_filter, FILTER_PREFIX "ball-color",
293                      ballCallback, p_filter->p_sys );
294     var_AddCallback( p_filter, FILTER_PREFIX "ball-size",
295                      ballCallback, p_filter->p_sys );
296     var_AddCallback( p_filter, FILTER_PREFIX "ball-speed",
297                      ballCallback, p_filter->p_sys );
298     var_AddCallback( p_filter, FILTER_PREFIX "edge-visible",
299                      ballCallback, p_filter->p_sys );
300
301     p_filter->p_sys->p_smooth = NULL;
302     p_filter->p_sys->p_grad_x = NULL;
303     p_filter->p_sys->p_grad_y = NULL;
304
305     p_filter->p_sys->i_ball_x = 100;
306     p_filter->p_sys->i_ball_y = 100;
307
308     p_filter->p_sys->f_lastVect_x = 0;
309     p_filter->p_sys->f_lastVect_y = -1;
310
311     return VLC_SUCCESS;
312 }
313
314
315 /*****************************************************************************
316 * Destroy: destroy Distort video thread output method
317 *****************************************************************************
318 * Terminate an output method created by DistortCreateOutputMethod
319  *****************************************************************************/
320 static void Destroy( vlc_object_t *p_this )
321 {
322     filter_t *p_filter = (filter_t *)p_this;
323     filter_sys_t *p_sys = p_filter->p_sys;
324
325     vlc_mutex_destroy( &p_sys->lock );
326
327     image_HandlerDelete( p_sys->p_image );
328
329     free( p_sys->p_smooth );
330     free( p_sys->p_grad_x );
331     free( p_sys->p_grad_y );
332
333     free( p_sys );
334 }
335
336
337 /*****************************************************************************
338 * Render: displays previously rendered output
339 *****************************************************************************
340 * This function send the currently rendered image to Distort image, waits
341 * until it is displayed and switch the two rendering buffers, preparing next
342 * frame.
343  *****************************************************************************/
344 static picture_t *Filter( filter_t *p_filter, picture_t *p_pic )
345 {
346     picture_t *p_outpic;
347
348     if( !p_pic ) return NULL;
349
350     p_outpic = filter_NewPicture( p_filter );
351     if( !p_outpic )
352     {
353         picture_Release( p_pic );
354         return NULL;
355     }
356
357     vlc_mutex_lock( &p_filter->p_sys->lock );
358     FilterBall( p_filter, p_pic, p_outpic );
359     vlc_mutex_unlock( &p_filter->p_sys->lock );
360
361     return CopyInfoAndRelease( p_outpic, p_pic );
362 }
363
364
365 /*****************************************************************************
366 * Drawing functions
367 *****************************************************************************/
368
369 static void drawBall( filter_sys_t *p_sys, picture_t *p_outpic )
370 {
371     int x = p_sys->i_ball_x;
372     int y = p_sys->i_ball_y;
373     int size = p_sys->i_ballSize;
374
375     const int i_width = p_outpic->p[0].i_visible_pitch;
376     const int i_height = p_outpic->p[0].i_visible_lines;
377
378     for( int j = y - size; j <= y + size; j++ )
379     {
380         bool b_skip = ( x - size ) % 2;
381         for( int i = x - size; i <= x + size; i++ )
382         {
383             /* Draw the pixel if it is inside the disk
384                and check we don't write out the frame. */
385             if( ( i - x ) * ( i - x ) + ( j - y ) * ( j - y ) <= size * size
386                 && i >= 0 && i < i_width
387                 && j >= 0 && j < i_height )
388             {
389                 ( *p_sys->drawingPixelFunction )( p_sys, p_outpic,
390                                     colorList[ p_sys->ballColor ].comp1,
391                                     colorList[ p_sys->ballColor ].comp2,
392                                     colorList[ p_sys->ballColor ].comp3,
393                                     i, j, b_skip );
394             }
395             b_skip = !b_skip;
396         }
397     }
398 }
399
400
401 static void drawPixelRGB24( filter_sys_t *p_sys, picture_t *p_outpic,
402                             uint8_t R, uint8_t G, uint8_t B,
403                             int x, int y, bool b_skip )
404 {
405     VLC_UNUSED( p_sys );
406     VLC_UNUSED( b_skip );
407     uint8_t *p_pixel = p_outpic->p[0].p_pixels
408                        + p_outpic->p[0].i_pitch
409                        * x + 3 * y;
410     *p_pixel = B;
411     *++p_pixel = G;
412     *++p_pixel = R;
413 }
414
415
416 static void drawPixelI420( filter_sys_t *p_sys, picture_t *p_outpic,
417                            uint8_t Y, uint8_t U, uint8_t V,
418                            int x, int y, bool b_skip )
419 {
420     VLC_UNUSED( p_sys );
421     *( p_outpic->p[0].p_pixels + p_outpic->p[0].i_pitch * y + x ) = Y;
422     if( !b_skip )
423     {
424         *( p_outpic->p[2].p_pixels + p_outpic->p[2].i_pitch
425                                      * ( y / 2 ) + x / 2 ) = U;
426         *( p_outpic->p[1].p_pixels + p_outpic->p[1].i_pitch
427                                      * ( y / 2 ) + x / 2 ) = V;
428     }
429 }
430
431
432 static void drawPixelPacked( filter_sys_t *p_sys, picture_t *p_outpic,
433                            uint8_t Y, uint8_t U, uint8_t V,
434                            int x, int y, bool b_skip )
435 {
436     uint8_t *p_pixel = p_outpic->p[0].p_pixels
437                        + p_outpic->p[0].i_pitch * y + x * 2;
438     *( p_pixel + p_sys->i_y_offset ) = Y;
439     if( !b_skip )
440     {
441         *( p_pixel + p_sys->i_u_offset ) = U;
442         *( p_pixel + p_sys->i_v_offset ) = V;
443     }
444 }
445
446
447 /*****************************************************************************
448 * Nomalize vector
449 *****************************************************************************
450 * Modify its value to set its norm to 1 and keep its direction.
451  *****************************************************************************/
452 static void NormalizeVector( float *vect_x, float *vect_y )
453 {
454     float norm = sqrt( *vect_x * *vect_x + *vect_y * *vect_y );
455     if( *vect_x != 0 || *vect_y != 0 )
456     {
457         *vect_x /= norm;
458         *vect_y /= norm;
459     }
460 }
461
462
463 /*****************************************************************************
464 * Gaussian Convolution
465 *****************************************************************************
466 *    Gaussian convolution ( sigma == 1.4 )
467 *
468 *    |  2  4  5  4  2  |   |  2  4  4  4  2 |
469 *    |  4  9 12  9  4  |   |  4  8 12  8  4 |
470 *    |  5 12 15 12  5  | ~ |  4 12 16 12  4 |
471 *    |  4  9 12  9  4  |   |  4  8 12  8  4 |
472 *    |  2  4  5  4  2  |   |  2  4  4  4  2 |
473  *****************************************************************************/
474 static void GaussianConvolution( picture_t *p_inpic, uint32_t *p_smooth )
475 {
476     const uint8_t *p_inpix = p_inpic->p[Y_PLANE].p_pixels;
477     const int i_src_pitch = p_inpic->p[Y_PLANE].i_pitch;
478     const int i_src_visible = p_inpic->p[Y_PLANE].i_visible_pitch;
479     const int i_numLines = p_inpic->p[Y_PLANE].i_visible_lines;
480
481     int x,y;
482     for( y = 2; y < i_numLines - 2; y++ )
483     {
484         for( x = 2; x < i_src_visible - 2; x++ )
485         {
486             p_smooth[y*i_src_visible+x] = (uint32_t)(
487                     /* 2 rows up */
488                     ( p_inpix[(y-2)*i_src_pitch+x-2] )
489                     + ((p_inpix[(y-2)*i_src_pitch+x-1]
490                     +   p_inpix[(y-2)*i_src_pitch+x]
491                     +   p_inpix[(y-2)*i_src_pitch+x+1])<<1 )
492                     + ( p_inpix[(y-2)*i_src_pitch+x+2] )
493                     /* 1 row up */
494                     + ((p_inpix[(y-1)*i_src_pitch+x-2]
495                     + ( p_inpix[(y-1)*i_src_pitch+x-1]<<1 )
496                     + ( p_inpix[(y-1)*i_src_pitch+x]*3 )
497                     + ( p_inpix[(y-1)*i_src_pitch+x+1]<<1 )
498                     +   p_inpix[(y-1)*i_src_pitch+x+2]
499                     /* */
500                     +   p_inpix[y*i_src_pitch+x-2]
501                     + ( p_inpix[y*i_src_pitch+x-1]*3 )
502                     + ( p_inpix[y*i_src_pitch+x]<<2 )
503                     + ( p_inpix[y*i_src_pitch+x+1]*3 )
504                     +   p_inpix[y*i_src_pitch+x+2]
505                     /* 1 row down */
506                     +   p_inpix[(y+1)*i_src_pitch+x-2]
507                     + ( p_inpix[(y+1)*i_src_pitch+x-1]<<1 )
508                     + ( p_inpix[(y+1)*i_src_pitch+x]*3 )
509                     + ( p_inpix[(y+1)*i_src_pitch+x+1]<<1 )
510                     +   p_inpix[(y+1)*i_src_pitch+x+2] )<<1 )
511                     /* 2 rows down */
512                     + ( p_inpix[(y+2)*i_src_pitch+x-2] )
513                     + ((p_inpix[(y+2)*i_src_pitch+x-1]
514                     +   p_inpix[(y+2)*i_src_pitch+x]
515                     +   p_inpix[(y+2)*i_src_pitch+x+1])<<1 )
516                     + ( p_inpix[(y+2)*i_src_pitch+x+2] )
517                                                     ) >> 6 /* 115 */;
518         }
519     }
520 }
521
522
523 /*****************************************************************************
524  * FilterBall: Augmented reality ball video filter
525  *****************************************************************************
526  * The edge detection part comes from gradient.c video filter module.
527  * The Canny edge detection algorithm is used :
528  * http://fourier.eng.hmc.edu/e161/lectures/canny/node1.html
529  * (well ... the implementation isn't really the canny algorithm ... but some
530  * ideas are the same)
531  *****************************************************************************/
532 static void FilterBall( filter_t *p_filter, picture_t *p_inpic,
533                            picture_t *p_outpic )
534 {
535     int x, y;
536     filter_sys_t *p_sys = p_filter->p_sys;
537
538     uint32_t *p_smooth;
539     int32_t *p_grad_x;
540     int32_t *p_grad_y;
541
542     picture_t *p_converted;
543     video_format_t fmt_comp;
544
545     switch( p_filter->fmt_in.video.i_chroma )
546     {
547         case VLC_CODEC_RGB24:
548         CASE_PACKED_YUV_422
549             fmt_comp.i_width = p_filter->fmt_in.video.i_width;
550             fmt_comp.i_height = p_filter->fmt_in.video.i_height;
551             fmt_comp.i_chroma = VLC_FOURCC('G','R','E','Y');
552             fmt_comp.i_visible_width = fmt_comp.i_width;
553             fmt_comp.i_visible_height = fmt_comp.i_height;
554
555             p_converted = image_Convert( p_filter->p_sys->p_image, p_inpic,
556                                          &(p_filter->fmt_in.video),
557                                          &fmt_comp );
558             if( !p_converted )
559                 return;
560
561             break;
562
563         default:
564             p_converted = p_inpic;
565             break;
566     }
567
568     const int i_numCols = p_converted->p[0].i_visible_pitch;
569     const int i_numLines = p_converted->p[0].i_visible_lines;
570
571     if( !p_filter->p_sys->p_smooth )
572         p_filter->p_sys->p_smooth =
573                 (uint32_t *)malloc( i_numLines * i_numCols
574                                     * sizeof(uint32_t));
575     p_smooth = p_filter->p_sys->p_smooth;
576
577     if( !p_filter->p_sys->p_grad_x )
578         p_filter->p_sys->p_grad_x =
579                 (int32_t *)malloc( i_numLines * i_numCols
580                                    * sizeof(int32_t));
581     p_grad_x = p_filter->p_sys->p_grad_x;
582
583     if( !p_filter->p_sys->p_grad_y )
584         p_filter->p_sys->p_grad_y =
585                 (int32_t *)malloc( i_numLines * i_numCols
586                                    * sizeof(int32_t));
587     p_grad_y = p_filter->p_sys->p_grad_y;
588
589     if( !p_smooth || !p_grad_x || !p_grad_y ) return;
590
591     vlc_memcpy( p_outpic->p[0].p_pixels, p_inpic->p[0].p_pixels,
592                 p_outpic->p[0].i_lines * p_outpic->p[0].i_pitch );
593     vlc_memcpy( p_outpic->p[1].p_pixels, p_inpic->p[1].p_pixels,
594                 p_outpic->p[1].i_lines * p_outpic->p[1].i_pitch );
595     vlc_memcpy( p_outpic->p[2].p_pixels, p_inpic->p[2].p_pixels,
596                 p_outpic->p[2].i_lines * p_outpic->p[2].i_pitch );
597
598     GaussianConvolution( p_converted, p_smooth );
599
600     /* Compute the picture Sobel gradient
601        | -1 0 1 |     |  1  2  1 |
602        | -2 0 2 | and |  0  0  0 |
603        | -1 0 1 |     | -1 -2 -1 | */
604
605     for( y = 1; y < i_numLines - 1; y++ )
606     {
607         for( x = 1; x < i_numCols - 1; x++ )
608         {
609
610             p_grad_x[ y * i_numCols + x ] =
611                     ( p_smooth[(y-1)*i_numCols+x-1]
612                     - p_smooth[(y+1)*i_numCols+x-1] )
613                     + ( ( p_smooth[(y-1)*i_numCols+x]
614                     - p_smooth[(y+1)*i_numCols+x] ) <<1 )
615                     + ( p_smooth[(y-1)*i_numCols+x+1]
616                     - p_smooth[(y+1)*i_numCols+x+1] );
617             p_grad_y[ y * i_numCols + x ] =
618                     ( p_smooth[(y-1)*i_numCols+x-1]
619                     - p_smooth[(y-1)*i_numCols+x+1] )
620                     + ( ( p_smooth[y*i_numCols+x-1]
621                     - p_smooth[y*i_numCols+x+1] ) <<1 )
622                     + ( p_smooth[(y+1)*i_numCols+x-1]
623                     - p_smooth[(y+1)*i_numCols+x+1] );
624         }
625     }
626
627     if( p_sys->b_edgeVisible )
628     {
629         /* Display the edges. */
630         for( y = 1; y < i_numLines - 1; y++ )
631         {
632             for( x = 1; x < i_numCols - 1; x++ )
633             {
634                 if( abs( p_grad_x[ y * i_numCols + x ] )
635                     + abs( p_grad_y[ y * i_numCols + x ] )
636                     > p_sys->i_gradThresh )
637                 {
638                     ( *p_sys->drawingPixelFunction )( p_sys, p_outpic,
639                                                       colorList[ WHITE ].comp1,
640                                                       colorList[ WHITE ].comp2,
641                                                       colorList[ WHITE ].comp3,
642                                                       x, y, 0 );
643                 }
644             }
645         }
646     }
647
648     int i_motion;
649
650     float *pf_lastVect_x = &p_sys->f_lastVect_x;
651     float *pf_lastVect_y = &p_sys->f_lastVect_y;
652
653     float f_newVect_x = 0;
654     float f_newVect_y = 0;
655     float f_contVect_x = 0;
656     float f_contVect_y = 0;
657
658     int nb_collisions = 0;
659
660     bool bounce = false;
661
662     /* Test collisions for each pixel the ball will cover in its
663        motion. */
664     for ( i_motion = 0; i_motion <= p_sys->i_ballSpeed && !bounce; i_motion++ )
665     {
666         /* Compute next ball position */
667         x = roundf( (float)p_sys->i_ball_x
668                     + *pf_lastVect_x * (float)i_motion );
669         y = roundf( (float)p_sys->i_ball_y
670                     + *pf_lastVect_y * (float)i_motion );
671
672         for( int i = x - p_sys->i_ballSize; i <= x + p_sys->i_ballSize; i++ )
673         {
674             for( int j = y - p_sys->i_ballSize;
675                  j <= y + p_sys->i_ballSize; j++ )
676             {
677                 /* Test the pixel if it is inside the disk and check we don't
678                 write out the frame. */
679                 if( ( i - x ) * ( i - x ) + ( j - y ) * ( j - y )
680                     == p_sys->i_ballSize * p_sys->i_ballSize
681                     && j <= i_numLines - 1 && x <= i_numCols - 1
682                     && j >= 0 && i >= 0 )
683                 {
684                     /* Test firstly the picture limit collisions. */
685                     if( i <= 2 )
686                     {
687                         f_contVect_x = x - i;
688                         f_contVect_y = 0;
689                         x++;
690                         bounce = true;
691                         nb_collisions = 1;
692                         goto endLoop;
693                     }
694                     if( j <= 2 )
695                     {
696                         f_contVect_x = 0;
697                         f_contVect_y = y - j;
698                         y++;
699                         bounce = true;
700                         nb_collisions = 1;
701                         goto endLoop;
702                     }
703                     if( j >= i_numLines - 3 )
704                     {
705                         f_contVect_x = 0;
706                         f_contVect_y = y - j;
707                         y--;
708                         bounce = true;
709                         nb_collisions = 1;
710                         goto endLoop;
711                     }
712                     if( i >= i_numCols - 3 )
713                     {
714                         f_contVect_x = x - i;
715                         f_contVect_y = 0;
716                         x--;
717                         bounce = true;
718                         nb_collisions = 1;
719                         goto endLoop;
720                     }
721                     /* Test the collisions with edges. */
722                     if( abs( p_grad_x[ j * i_numCols + i ] )
723                         + abs( p_grad_y[ j * i_numCols + i ] )
724                         > p_sys->i_gradThresh )
725                     {
726                         f_contVect_x += x - i;
727                         f_contVect_y += y - j;
728                         nb_collisions++;
729                         bounce = true;
730                     }
731                 }
732             }
733         }
734     }
735
736     endLoop:
737
738     if( bounce )
739     {
740         /* Compute normal vector. */
741         f_contVect_x /= nb_collisions;
742         f_contVect_y /= nb_collisions;
743         NormalizeVector( &f_contVect_x, &f_contVect_y );
744
745         /* Compute the new vector after the bounce. */
746         float cosinus = *pf_lastVect_x * f_contVect_x
747                         + *pf_lastVect_y * f_contVect_y;
748         f_newVect_x = *pf_lastVect_x - 2 * cosinus * f_contVect_x;
749         f_newVect_y = *pf_lastVect_y - 2 * cosinus * f_contVect_y;
750         NormalizeVector( &f_newVect_x, &f_newVect_y );
751
752         *pf_lastVect_x = f_newVect_x;
753         *pf_lastVect_y = f_newVect_y;
754
755         p_sys->i_ball_x = x;
756         p_sys->i_ball_y = y;
757
758         /* Test if next pixel is outside the frame limits.
759            If it is the case, then the ball is blocked until it can move. */
760         x = roundf( (float)x + *pf_lastVect_x );
761         y = roundf( (float)y + *pf_lastVect_y );
762         if( x - p_sys->i_ballSize < 2
763             || x + p_sys->i_ballSize > i_numCols - 3
764             || y - p_sys->i_ballSize < 2
765             || y + p_sys->i_ballSize > i_numLines - 3 )
766         {
767             *pf_lastVect_x = 0;
768             *pf_lastVect_y = 0;
769         }
770         else
771             /* After a bouce, the first ball motion is always one pixel. */
772             i_motion = p_sys->i_ballSpeed - 1;
773     }
774     else
775         i_motion = 0;
776
777     /* Compute next ball position. */
778     p_sys->i_ball_x = roundf( (float)p_sys->i_ball_x + *pf_lastVect_x
779                               * (float)( p_sys->i_ballSpeed - i_motion ) );
780     p_sys->i_ball_y = roundf( p_sys->i_ball_y + *pf_lastVect_y
781                               * (float)( p_sys->i_ballSpeed - i_motion ) );
782
783     /* Draw the ball */
784     drawBall( p_sys, p_outpic );
785
786     switch( p_filter->fmt_in.video.i_chroma )
787     {
788         case VLC_CODEC_RGB24:
789         CASE_PACKED_YUV_422
790             picture_Release( p_converted );
791         default:
792             break;
793     }
794 }
795
796
797 /*****************************************************************************
798  * ballCallback
799  *****************************************************************************
800  * filter parameter modification callback
801  *****************************************************************************/
802 static int ballCallback( vlc_object_t *p_this, char const *psz_var,
803                              vlc_value_t oldval, vlc_value_t newval,
804                              void *p_data )
805 {
806     VLC_UNUSED(oldval);
807     filter_sys_t *p_sys = (filter_sys_t *)p_data;
808     msg_Err( p_this, "Test" );
809
810     vlc_mutex_lock( &p_sys->lock );
811     if( !strcmp( psz_var, FILTER_PREFIX "ball-color" ) )
812     {
813         p_sys->ballColor = getBallColor( p_this, newval.psz_string );
814     }
815     else if( !strcmp( psz_var, FILTER_PREFIX "ball-size" ) )
816     {
817         p_sys->i_ballSize = newval.i_int;
818     }
819     else if( !strcmp( psz_var, FILTER_PREFIX "ball-speed" ) )
820     {
821         p_sys->i_ballSpeed = newval.i_int;
822     }
823     else if( !strcmp( psz_var, FILTER_PREFIX "edge-visible" ) )
824     {
825         p_sys->b_edgeVisible = newval.b_bool;
826     }
827     else if( !strcmp( psz_var, FILTER_PREFIX "gradient-threshold" ) )
828     {
829         p_sys->i_gradThresh = newval.i_int;
830     }
831     vlc_mutex_unlock( &p_sys->lock );
832
833     return VLC_SUCCESS;
834 }
835
836
837 /*****************************************************************************
838  * ballCallback
839  *****************************************************************************
840  * Get and assign the ball color value
841  *****************************************************************************/
842 static int getBallColor( vlc_object_t *p_this, char const *psz_newval )
843 {
844     int ret;
845     if( !strcmp( psz_newval, "red" ) )
846         ret = RED;
847     else if( !strcmp( psz_newval, "blue" ) )
848         ret = BLUE;
849     else if( !strcmp( psz_newval, "green" ) )
850         ret = GREEN;
851     else if( !strcmp( psz_newval, "white" ) )
852         ret = WHITE;
853     else
854     {
855         msg_Err( p_this, "no valid ball color provided (%s)", psz_newval );
856         ret = RED;
857     }
858     return ret;
859 }