Fix unsigned int vs int compilation warning.
[vlc.git] / modules / video_filter / mosaic.c
1 /*****************************************************************************
2  * mosaic.c : Mosaic video plugin for vlc
3  *****************************************************************************
4  * Copyright (C) 2004-2005 the VideoLAN team
5  * $Id$
6  *
7  * Authors: Antoine Cellerier <dionoea@via.ecp.fr>
8  *          Christophe Massiot <massiot@via.ecp.fr>
9  *
10  * This program is free software; you can redistribute it and/or modify
11  * it under the terms of the GNU General Public License as published by
12  * the Free Software Foundation; either version 2 of the License, or
13  * (at your option) any later version.
14  *
15  * This program is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18  * GNU General Public License for more details.
19  *
20  * You should have received a copy of the GNU General Public License
21  * along with this program; if not, write to the Free Software
22  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
23 *****************************************************************************/
24
25 /*****************************************************************************
26  * Preamble
27  *****************************************************************************/
28 #include <stdlib.h>                                      /* malloc(), free() */
29 #include <string.h>
30 #include <math.h>
31
32 #include <vlc/vlc.h>
33 #include <vlc/vout.h>
34
35 #ifdef HAVE_LIMITS_H
36 #   include <limits.h> /* INT_MAX */
37 #endif
38
39 #include "vlc_filter.h"
40 #include "vlc_image.h"
41
42 #include "mosaic.h"
43
44 #define BLANK_DELAY I64C(1000000)
45
46 /*****************************************************************************
47  * Local prototypes
48  *****************************************************************************/
49 static int  CreateFilter    ( vlc_object_t * );
50 static void DestroyFilter   ( vlc_object_t * );
51
52 static subpicture_t *Filter( filter_t *, mtime_t );
53
54 static int MosaicCallback( vlc_object_t *, char const *, vlc_value_t,
55                            vlc_value_t, void * );
56
57 /*****************************************************************************
58  * filter_sys_t : filter descriptor
59  *****************************************************************************/
60 struct filter_sys_t
61 {
62     vlc_mutex_t lock;
63     vlc_mutex_t *p_lock;
64
65     image_handler_t *p_image;
66     picture_t *p_pic;
67
68     int i_position; /* mosaic positioning method */
69     vlc_bool_t b_ar; /* do we keep the aspect ratio ? */
70     vlc_bool_t b_keep; /* do we keep the original picture format ? */
71     int i_width, i_height; /* mosaic height and width */
72     int i_cols, i_rows; /* mosaic rows and cols */
73     int i_align; /* mosaic alignment in background video */
74     int i_xoffset, i_yoffset; /* top left corner offset */
75     int i_vborder, i_hborder; /* border width/height between miniatures */
76     int i_alpha; /* subfilter alpha blending */
77
78     vlc_bool_t b_bs; /* Bluescreen vars */
79     int i_bsu, i_bsv, i_bsut, i_bsvt;
80
81     char **ppsz_order; /* list of picture-id */
82     int i_order_length;
83
84     mtime_t i_delay;
85 };
86
87 /*****************************************************************************
88  * Module descriptor
89  *****************************************************************************/
90 #define ALPHA_TEXT N_("Alpha blending")
91 #define ALPHA_LONGTEXT N_("Alpha blending (0 -> 255). Default is 255")
92
93 #define HEIGHT_TEXT N_("Height in pixels")
94 #define WIDTH_TEXT N_("Width in pixels")
95 #define XOFFSET_TEXT N_("Top left corner x coordinate")
96 #define YOFFSET_TEXT N_("Top left corner y coordinate")
97 #define VBORDER_TEXT N_("Vertical border width in pixels")
98 #define HBORDER_TEXT N_("Horizontal border width in pixels")
99 #define ALIGN_TEXT N_("Mosaic alignment")
100
101 #define POS_TEXT N_("Positioning method")
102 #define POS_LONGTEXT N_("Positioning method. auto: automatically choose " \
103         "the best number of rows and columns. fixed: use the user-defined " \
104         "number of rows and columns.")
105 #define ROWS_TEXT N_("Number of rows")
106 #define COLS_TEXT N_("Number of columns")
107 #define AR_TEXT N_("Keep aspect ratio when resizing")
108 #define KEEP_TEXT N_("Keep original size")
109
110 #define ORDER_TEXT N_("Order as a comma separated list of picture-id(s)")
111
112 #define DELAY_TEXT N_("Delay")
113 #define DELAY_LONGTEXT N_("Pictures coming from the picture video outputs " \
114         "will be delayed accordingly (in milliseconds). For high " \
115         "values you will need to raise file-caching and others.")
116
117 #define BLUESCREEN_TEXT N_("Enable bluescreen (aka greenscreen or chroma key) video background replacing")
118 #define BLUESCREENU_TEXT N_("Bluescreen chroma key U (0-255)")
119 #define BLUESCREENV_TEXT N_("Bluescreen chroma key V (0-255)")
120 #define BLUESCREENUTOL_TEXT N_("Bluescreen chroma key U tolerance")
121 #define BLUESCREENVTOL_TEXT N_("Bluescreen chroma key V tolerance")
122
123 static int pi_pos_values[] = { 0, 1 };
124 static char * ppsz_pos_descriptions[] =
125 { N_("auto"), N_("fixed") };
126
127 static int pi_align_values[] = { 0, 1, 2, 4, 8, 5, 6, 9, 10 };
128 static char *ppsz_align_descriptions[] =
129      { N_("Center"), N_("Left"), N_("Right"), N_("Top"), N_("Bottom"),
130      N_("Top-Left"), N_("Top-Right"), N_("Bottom-Left"), N_("Bottom-Right") };
131
132
133 vlc_module_begin();
134     set_description( N_("Mosaic video sub filter") );
135     set_shortname( N_("Mosaic") );
136     set_category( CAT_VIDEO );
137     set_subcategory( SUBCAT_VIDEO_SUBPIC);
138     set_capability( "sub filter", 0 );
139     set_callbacks( CreateFilter, DestroyFilter );
140
141     add_integer( "mosaic-alpha", 255, NULL, ALPHA_TEXT, ALPHA_LONGTEXT, VLC_FALSE );
142     add_integer( "mosaic-height", 100, NULL, HEIGHT_TEXT, HEIGHT_TEXT, VLC_FALSE );
143     add_integer( "mosaic-width", 100, NULL, WIDTH_TEXT, WIDTH_TEXT, VLC_FALSE );
144     add_integer( "mosaic-align", 5, NULL, ALIGN_TEXT, ALIGN_TEXT, VLC_TRUE);
145         change_integer_list( pi_align_values, ppsz_align_descriptions, 0 );
146     add_integer( "mosaic-xoffset", 0, NULL, XOFFSET_TEXT, XOFFSET_TEXT, VLC_TRUE );
147     add_integer( "mosaic-yoffset", 0, NULL, YOFFSET_TEXT, YOFFSET_TEXT, VLC_TRUE );
148     add_integer( "mosaic-vborder", 0, NULL, VBORDER_TEXT, VBORDER_TEXT, VLC_TRUE );
149     add_integer( "mosaic-hborder", 0, NULL, HBORDER_TEXT, HBORDER_TEXT, VLC_TRUE );
150
151     add_integer( "mosaic-position", 0, NULL, POS_TEXT, POS_LONGTEXT, VLC_FALSE );
152         change_integer_list( pi_pos_values, ppsz_pos_descriptions, 0 );
153     add_integer( "mosaic-rows", 2, NULL, ROWS_TEXT, ROWS_TEXT, VLC_FALSE );
154     add_integer( "mosaic-cols", 2, NULL, COLS_TEXT, COLS_TEXT, VLC_FALSE );
155     add_bool( "mosaic-keep-aspect-ratio", 0, NULL, AR_TEXT, AR_TEXT, VLC_FALSE );
156     add_bool( "mosaic-keep-picture", 0, NULL, KEEP_TEXT, KEEP_TEXT, VLC_FALSE );
157     add_string( "mosaic-order", "", NULL, ORDER_TEXT, ORDER_TEXT, VLC_FALSE );
158
159     add_integer( "mosaic-delay", 0, NULL, DELAY_TEXT, DELAY_LONGTEXT,
160                  VLC_FALSE );
161
162     add_bool( "mosaic-bs", 0, NULL, BLUESCREEN_TEXT,
163               BLUESCREEN_TEXT, VLC_FALSE );
164     add_integer( "mosaic-bsu", 120, NULL, BLUESCREENU_TEXT,
165                  BLUESCREENU_TEXT, VLC_FALSE );
166     add_integer( "mosaic-bsv", 90, NULL, BLUESCREENV_TEXT,
167                  BLUESCREENV_TEXT, VLC_FALSE );
168     add_integer( "mosaic-bsut", 17, NULL, BLUESCREENUTOL_TEXT,
169                  BLUESCREENUTOL_TEXT, VLC_FALSE );
170     add_integer( "mosaic-bsvt", 17, NULL, BLUESCREENVTOL_TEXT,
171                  BLUESCREENVTOL_TEXT, VLC_FALSE );
172
173     var_Create( p_module->p_libvlc, "mosaic-lock", VLC_VAR_MUTEX );
174 vlc_module_end();
175
176
177 /*****************************************************************************
178  * CreateFiler: allocate mosaic video filter
179  *****************************************************************************/
180 static int CreateFilter( vlc_object_t *p_this )
181 {
182     filter_t *p_filter = (filter_t *)p_this;
183     filter_sys_t *p_sys;
184     libvlc_t *p_libvlc = p_filter->p_libvlc;
185     char *psz_order;
186     int i_index;
187     vlc_value_t val;
188
189     /* The mosaic thread is more important than the decoder threads */
190     vlc_thread_set_priority( p_this, VLC_THREAD_PRIORITY_OUTPUT );
191
192     /* Allocate structure */
193     p_sys = p_filter->p_sys = malloc( sizeof( filter_sys_t ) );
194     if( p_sys == NULL )
195     {
196         msg_Err( p_filter, "out of memory" );
197         return VLC_ENOMEM;
198     }
199
200     p_filter->pf_sub_filter = Filter;
201     p_sys->p_pic = NULL;
202
203     vlc_mutex_init( p_filter, &p_sys->lock );
204     vlc_mutex_lock( &p_sys->lock );
205
206     var_Get( p_libvlc, "mosaic-lock", &val );
207     p_sys->p_lock = val.p_address;
208
209 #define GET_VAR( name, min, max )                                           \
210     p_sys->i_##name = __MIN( max, __MAX( min,                               \
211                 var_CreateGetInteger( p_filter, "mosaic-" #name ) ) );      \
212     var_Destroy( p_filter, "mosaic-" #name );                               \
213     var_Create( p_libvlc, "mosaic-" #name, VLC_VAR_INTEGER );               \
214     var_SetInteger( p_libvlc, "mosaic-" #name, p_sys->i_##name );           \
215     var_AddCallback( p_libvlc, "mosaic-" #name, MosaicCallback, p_sys );
216
217     GET_VAR( width, 0, INT_MAX );
218     GET_VAR( height, 0, INT_MAX );
219     GET_VAR( xoffset, 0, INT_MAX );
220     GET_VAR( yoffset, 0, INT_MAX );
221
222     p_sys->i_align = __MIN( 10, __MAX( 0,  var_CreateGetInteger( p_filter, "mosaic-align" ) ) );
223     if( p_sys->i_align == 3 || p_sys->i_align == 7 )
224         p_sys->i_align = 5;
225     var_Destroy( p_filter, "mosaic-align" );
226     var_Create( p_libvlc, "mosaic-align", VLC_VAR_INTEGER );
227     var_SetInteger( p_libvlc, "mosaic-align", p_sys->i_align );
228     var_AddCallback( p_libvlc, "mosaic-align", MosaicCallback, p_sys );
229
230     GET_VAR( vborder, 0, INT_MAX );
231     GET_VAR( hborder, 0, INT_MAX );
232     GET_VAR( rows, 1, INT_MAX );
233     GET_VAR( cols, 1, INT_MAX );
234     GET_VAR( alpha, 0, 255 );
235     GET_VAR( position, 0, 1 );
236     GET_VAR( delay, 100, INT_MAX );
237     p_sys->i_delay *= 1000;
238
239     p_sys->b_ar = var_CreateGetBool( p_filter, "mosaic-keep-aspect-ratio" );
240     var_Destroy( p_filter, "mosaic-keep-aspect-ratio" );
241     var_Create( p_libvlc, "mosaic-keep-aspect-ratio", VLC_VAR_INTEGER );
242     var_SetBool( p_libvlc, "mosaic-keep-aspect-ratio", p_sys->b_ar );
243     var_AddCallback( p_libvlc, "mosaic-keep-aspect-ratio", MosaicCallback,
244                      p_sys );
245
246     p_sys->b_keep = var_CreateGetBool( p_filter, "mosaic-keep-picture" );
247     if ( !p_sys->b_keep )
248     {
249         p_sys->p_image = image_HandlerCreate( p_filter );
250     }
251
252     p_sys->i_order_length = 0;
253     p_sys->ppsz_order = NULL;
254     psz_order = var_CreateGetString( p_filter, "mosaic-order" );
255
256     if( psz_order[0] != 0 )
257     {
258         char *psz_end = NULL;
259         i_index = 0;
260         do
261         {
262             psz_end = strchr( psz_order, ',' );
263             i_index++;
264             p_sys->ppsz_order = realloc( p_sys->ppsz_order,
265                                          i_index * sizeof(char *) );
266             p_sys->ppsz_order[i_index - 1] = strndup( psz_order,
267                                            psz_end - psz_order );
268             psz_order = psz_end+1;
269         } while( NULL !=  psz_end );
270         p_sys->i_order_length = i_index;
271     }
272
273     /* Bluescreen specific stuff */
274     GET_VAR( bsu, 0x00, 0xff );
275     GET_VAR( bsv, 0x00, 0xff );
276     GET_VAR( bsut, 0x00, 0xff );
277     GET_VAR( bsvt, 0x00, 0xff );
278     p_sys->b_bs = var_CreateGetBool( p_filter, "mosaic-bs" );
279     var_Destroy( p_filter, "mosaic-bs" );
280     var_Create( p_libvlc, "mosaic-bs", VLC_VAR_INTEGER );
281     var_SetBool( p_libvlc, "mosaic-bs", p_sys->b_bs );
282     var_AddCallback( p_libvlc, "mosaic-bs", MosaicCallback, p_sys );
283     if( p_sys->b_bs && p_sys->b_keep )
284     {
285         msg_Warn( p_filter, "mosaic-keep-picture needs to be disabled for"
286                             " bluescreen to work" );
287     }
288
289     vlc_mutex_unlock( &p_sys->lock );
290
291     return VLC_SUCCESS;
292 }
293
294 /*****************************************************************************
295  * DestroyFilter: destroy mosaic video filter
296  *****************************************************************************/
297 static void DestroyFilter( vlc_object_t *p_this )
298 {
299     filter_t *p_filter = (filter_t*)p_this;
300     filter_sys_t *p_sys = p_filter->p_sys;
301     libvlc_t *p_libvlc = p_filter->p_libvlc;
302     int i_index;
303
304     vlc_mutex_lock( &p_sys->lock );
305
306     if( !p_sys->b_keep )
307     {
308         image_HandlerDelete( p_sys->p_image );
309     }
310
311     if( p_sys->i_order_length )
312     {
313         for( i_index = 0; i_index < p_sys->i_order_length; i_index++ )
314         {
315             free( p_sys->ppsz_order[i_index] );
316         }
317         free( p_sys->ppsz_order );
318     }
319
320     var_Destroy( p_libvlc, "mosaic-alpha" );
321     var_Destroy( p_libvlc, "mosaic-height" );
322     var_Destroy( p_libvlc, "mosaic-align" );
323     var_Destroy( p_libvlc, "mosaic-width" );
324     var_Destroy( p_libvlc, "mosaic-xoffset" );
325     var_Destroy( p_libvlc, "mosaic-yoffset" );
326     var_Destroy( p_libvlc, "mosaic-vborder" );
327     var_Destroy( p_libvlc, "mosaic-hborder" );
328     var_Destroy( p_libvlc, "mosaic-position" );
329     var_Destroy( p_libvlc, "mosaic-rows" );
330     var_Destroy( p_libvlc, "mosaic-cols" );
331     var_Destroy( p_libvlc, "mosaic-keep-aspect-ratio" );
332
333     var_Destroy( p_libvlc, "mosaic-bsu" );
334     var_Destroy( p_libvlc, "mosaic-bsv" );
335     var_Destroy( p_libvlc, "mosaic-bsut" );
336     var_Destroy( p_libvlc, "mosaic-bsvt" );
337     var_Destroy( p_libvlc, "mosaic-bs" );
338
339     if( p_sys->p_pic ) p_sys->p_pic->pf_release( p_sys->p_pic );
340     vlc_mutex_unlock( &p_sys->lock );
341     vlc_mutex_destroy( &p_sys->lock );
342     free( p_sys );
343 }
344
345 /*****************************************************************************
346  * MosaicReleasePicture : Hack to avoid picture duplication
347  *****************************************************************************/
348 static void MosaicReleasePicture( picture_t *p_picture )
349 {
350     picture_t *p_original_pic = (picture_t *)p_picture->p_sys;
351
352     p_original_pic->pf_release( p_original_pic );
353 }
354
355 /*****************************************************************************
356  * Filter
357  *****************************************************************************/
358 static subpicture_t *Filter( filter_t *p_filter, mtime_t date )
359 {
360     filter_sys_t *p_sys = p_filter->p_sys;
361     bridge_t *p_bridge;
362
363     subpicture_t *p_spu;
364
365     int i_index, i_real_index, i_row, i_col;
366     int i_greatest_real_index_used = p_sys->i_order_length - 1;
367
368     unsigned int col_inner_width, row_inner_height;
369
370     subpicture_region_t *p_region;
371     subpicture_region_t *p_region_prev = NULL;
372
373     /* Allocate the subpicture internal data. */
374     p_spu = p_filter->pf_sub_buffer_new( p_filter );
375     if( !p_spu )
376     {
377         return NULL;
378     }
379
380     /* Initialize subpicture */
381     p_spu->i_channel = 0;
382     p_spu->i_start  = date;
383     p_spu->i_stop = 0;
384     p_spu->b_ephemer = VLC_TRUE;
385     p_spu->i_alpha = p_sys->i_alpha;
386     p_spu->i_flags = p_sys->i_align;
387     p_spu->b_absolute = VLC_FALSE;
388
389     vlc_mutex_lock( &p_sys->lock );
390     vlc_mutex_lock( p_sys->p_lock );
391
392     p_bridge = GetBridge( p_filter );
393     if ( p_bridge == NULL )
394     {
395         vlc_mutex_unlock( p_sys->p_lock );
396         vlc_mutex_unlock( &p_sys->lock );
397         return p_spu;
398     }
399
400     if ( p_sys->i_position == 0 ) /* use automatic positioning */
401     {
402         int i_numpics = p_sys->i_order_length; /* keep slots and all */
403         for ( i_index = 0; i_index < p_bridge->i_es_num; i_index++ )
404         {
405             bridged_es_t *p_es = p_bridge->pp_es[i_index];
406             if ( !p_es->b_empty )
407             {
408                 i_numpics ++;
409                 if( p_sys->i_order_length && p_es->psz_id != 0 )
410                 {
411                     /* We also want to leave slots for images given in
412                      * mosaic-order that are not available in p_vout_picture */
413                     int i;
414                     for( i = 0; i < p_sys->i_order_length ; i++ )
415                     {
416                         if( !strcmp( p_sys->ppsz_order[i], p_es->psz_id ) )
417                         {
418                             i_numpics--;
419                             break;
420                         }
421                     }
422
423                 }
424             }
425         }
426         p_sys->i_rows = ((int)ceil(sqrt( (float)i_numpics )));
427         p_sys->i_cols = ( i_numpics % p_sys->i_rows == 0 ?
428                             i_numpics / p_sys->i_rows :
429                             i_numpics / p_sys->i_rows + 1 );
430     }
431
432     col_inner_width  = ( ( p_sys->i_width - ( p_sys->i_cols - 1 )
433                        * p_sys->i_vborder ) / p_sys->i_cols );
434     row_inner_height = ( ( p_sys->i_height - ( p_sys->i_rows - 1 )
435                        * p_sys->i_hborder ) / p_sys->i_rows );
436
437     i_real_index = 0;
438
439     for ( i_index = 0; i_index < p_bridge->i_es_num; i_index++ )
440     {
441         bridged_es_t *p_es = p_bridge->pp_es[i_index];
442         video_format_t fmt_in = {0}, fmt_out = {0};
443         picture_t *p_converted;
444
445         if ( p_es->b_empty )
446             continue;
447
448         while ( p_es->p_picture != NULL
449                  && p_es->p_picture->date + p_sys->i_delay < date )
450         {
451             if ( p_es->p_picture->p_next != NULL )
452             {
453                 picture_t *p_next = p_es->p_picture->p_next;
454                 p_es->p_picture->pf_release( p_es->p_picture );
455                 p_es->p_picture = p_next;
456             }
457             else if ( p_es->p_picture->date + p_sys->i_delay + BLANK_DELAY <
458                         date )
459             {
460                 /* Display blank */
461                 p_es->p_picture->pf_release( p_es->p_picture );
462                 p_es->p_picture = NULL;
463                 p_es->pp_last = &p_es->p_picture;
464                 break;
465             }
466             else
467             {
468                 msg_Dbg( p_filter, "too late picture for %s (" I64Fd ")",
469                          p_es->psz_id,
470                          date - p_es->p_picture->date - p_sys->i_delay );
471                 break;
472             }
473         }
474
475         if ( p_es->p_picture == NULL )
476             continue;
477
478         if ( p_sys->i_order_length == 0 )
479         {
480             i_real_index++;
481         }
482         else
483         {
484             int i;
485             for ( i = 0; i <= p_sys->i_order_length; i++ )
486             {
487                 if ( i == p_sys->i_order_length ) break;
488                 if ( strcmp( p_es->psz_id, p_sys->ppsz_order[i] ) == 0 )
489                 {
490                     i_real_index = i;
491                     break;
492                 }
493             }
494             if ( i == p_sys->i_order_length )
495                 i_real_index = ++i_greatest_real_index_used;
496         }
497         i_row = ( i_real_index / p_sys->i_cols ) % p_sys->i_rows;
498         i_col = i_real_index % p_sys->i_cols ;
499
500         if ( !p_sys->b_keep )
501         {
502             /* Convert the images */
503             fmt_in.i_chroma = p_es->p_picture->format.i_chroma;
504             fmt_in.i_height = p_es->p_picture->format.i_height;
505             fmt_in.i_width = p_es->p_picture->format.i_width;
506
507             fmt_out.i_chroma = VLC_FOURCC('Y','U','V','A');
508             fmt_out.i_width = col_inner_width;
509             fmt_out.i_height = row_inner_height;
510
511             if( p_sys->b_ar ) /* keep aspect ratio */
512             {
513                 if( (float)fmt_out.i_width / (float)fmt_out.i_height
514                       > (float)fmt_in.i_width / (float)fmt_in.i_height )
515                 {
516                     fmt_out.i_width = ( fmt_out.i_height * fmt_in.i_width )
517                                          / fmt_in.i_height;
518                 }
519                 else
520                 {
521                     fmt_out.i_height = ( fmt_out.i_width * fmt_in.i_height )
522                                         / fmt_in.i_width;
523                 }
524              }
525
526             fmt_out.i_visible_width = fmt_out.i_width;
527             fmt_out.i_visible_height = fmt_out.i_height;
528
529             p_converted = image_Convert( p_sys->p_image, p_es->p_picture,
530                                          &fmt_in, &fmt_out );
531             if( !p_converted )
532             {
533                 msg_Warn( p_filter,
534                            "image resizing and chroma conversion failed" );
535                 continue;
536             }
537
538             /* Bluescreen stuff */
539             if( p_sys->b_bs )
540             {
541                 int i,j;
542                 int i_lines = p_converted->p[ A_PLANE ].i_lines;
543                 int i_pitch = p_converted->p[ A_PLANE ].i_pitch;
544                 uint8_t *p_a = p_converted->p[ A_PLANE ].p_pixels;
545                 uint8_t *p_at = malloc( i_lines * i_pitch * sizeof( uint8_t ) );
546                 uint8_t *p_u = p_converted->p[ U_PLANE ].p_pixels;
547                 uint8_t *p_v = p_converted->p[ V_PLANE ].p_pixels;
548                 uint8_t umin, umax, vmin, vmax;
549                 umin = p_sys->i_bsu - p_sys->i_bsut >= 0x00 ?
550                        p_sys->i_bsu - p_sys->i_bsut : 0x00;
551                 umax = p_sys->i_bsu + p_sys->i_bsut <= 0xff ?
552                        p_sys->i_bsu + p_sys->i_bsut : 0xff;
553                 vmin = p_sys->i_bsv - p_sys->i_bsvt >= 0x00 ?
554                        p_sys->i_bsv - p_sys->i_bsvt : 0x00;
555                 vmax = p_sys->i_bsv + p_sys->i_bsvt <= 0xff ?
556                        p_sys->i_bsv + p_sys->i_bsvt : 0xff;
557
558                 for( i = 0; i < i_lines*i_pitch; i++ )
559                 {
560                     if(    p_u[i] < umax
561                         && p_u[i] > umin
562                         && p_v[i] < vmax
563                         && p_v[i] > vmin )
564                     {
565                         p_at[i] = 0x00;
566                     }
567                     else
568                     {
569                         p_at[i] = 0xff;
570                     }
571                 }
572                 /* Gaussian convolution to make it look cleaner */
573                 memset( p_a, 0, 2 * i_pitch );
574                 for( i = 2; i < i_lines - 2; i++ )
575                 {
576                     p_a[i*i_pitch] = 0x00;
577                     p_a[i*i_pitch+1] = 0x00;
578                     for( j = 2; j < i_pitch - 2; j ++ )
579                     {
580                         p_a[i*i_pitch+j] = (uint8_t)((
581                           /* 2 rows up */
582                             ( p_at[(i-2)*i_pitch+j-2]<<1 )
583                           + ( p_at[(i-2)*i_pitch+j-1]<<2 )
584                           + ( p_at[(i-2)*i_pitch+j]<<2 )
585                           + ( p_at[(i-2)*i_pitch+j+1]<<2 )
586                           + ( p_at[(i-2)*i_pitch+j+2]<<1 )
587                           /* 1 row up */
588                           + ( p_at[(i-1)*i_pitch+j-1]<<3 )
589                           + ( p_at[(i-1)*i_pitch+j-2]<<2 )
590                           + ( p_at[(i-1)*i_pitch+j]*12 )
591                           + ( p_at[(i-1)*i_pitch+j+1]<<3 )
592                           + ( p_at[(i-1)*i_pitch+j+2]<<2 )
593                           /* */
594                           + ( p_at[i*i_pitch+j-2]<<2 )
595                           + ( p_at[i*i_pitch+j-1]*12 )
596                           + ( p_at[i*i_pitch+j]<<4 )
597                           + ( p_at[i*i_pitch+j+1]*12 )
598                           + ( p_at[i*i_pitch+j+2]<<2 )
599                           /* 1 row down */
600                           + ( p_at[(i+1)*i_pitch+j-2]<<2 )
601                           + ( p_at[(i+1)*i_pitch+j-1]<<3 )
602                           + ( p_at[(i+1)*i_pitch+j]*12 )
603                           + ( p_at[(i+1)*i_pitch+j+1]<<3 )
604                           + ( p_at[(i+1)*i_pitch+j+2]<<2 )
605                           /* 2 rows down */
606                           + ( p_at[(i+2)*i_pitch+j-2]<<1 )
607                           + ( p_at[(i+2)*i_pitch+j-1]<<2 )
608                           + ( p_at[(i+2)*i_pitch+j]<<2 )
609                           + ( p_at[(i+2)*i_pitch+j+1]<<2 )
610                           + ( p_at[(i+2)*i_pitch+j+2]<<1 )
611                           )/152);
612                           if( p_a[i*i_pitch+j] < 0xbf ) p_a[i*i_pitch+j] = 0x00;
613                     }
614                 }
615                 free( p_at );
616             }
617         }
618         else
619         {
620             p_converted = p_es->p_picture;
621             p_converted->i_refcount++;
622             fmt_in.i_width = fmt_out.i_width = p_converted->format.i_width;
623             fmt_in.i_height = fmt_out.i_height = p_converted->format.i_height;
624             fmt_in.i_chroma = fmt_out.i_chroma = p_converted->format.i_chroma;
625             fmt_out.i_visible_width = fmt_out.i_width;
626             fmt_out.i_visible_height = fmt_out.i_height;
627         }
628
629         p_region = p_spu->pf_make_region( VLC_OBJECT(p_filter), &fmt_out,
630                                           p_converted );
631         if( !p_region )
632         {
633             msg_Err( p_filter, "cannot allocate SPU region" );
634             p_filter->pf_sub_buffer_del( p_filter, p_spu );
635             vlc_mutex_unlock( &p_sys->lock );
636             vlc_mutex_unlock( p_sys->p_lock );
637             return p_spu;
638         }
639
640         /* HACK ALERT : let's fix the pointers to avoid picture duplication.
641          * This is necessary because p_region->picture is not a pointer
642          * as it ought to be. */
643         if( !p_sys->b_keep )
644         {
645             free( p_converted );
646         }
647         else
648         {
649             /* Keep a pointer to the original picture (and its refcount...). */
650             p_region->picture.p_sys = (picture_sys_t *)p_converted;
651             p_region->picture.pf_release = MosaicReleasePicture;
652         }
653
654         if( fmt_out.i_width > col_inner_width ||
655             p_sys->b_ar || p_sys->b_keep )
656         {
657             /* we don't have to center the video since it takes the
658             whole rectangle area or it's larger than the rectangle */
659             p_region->i_x = p_sys->i_xoffset
660                         + i_col * ( p_sys->i_width / p_sys->i_cols )
661                         + ( i_col * p_sys->i_vborder ) / p_sys->i_cols;
662         }
663         else
664         {
665             /* center the video in the dedicated rectangle */
666             p_region->i_x = p_sys->i_xoffset
667                     + i_col * ( p_sys->i_width / p_sys->i_cols )
668                     + ( i_col * p_sys->i_vborder ) / p_sys->i_cols
669                     + ( col_inner_width - fmt_out.i_width ) / 2;
670         }
671
672         if( fmt_out.i_height < row_inner_height
673             || p_sys->b_ar || p_sys->b_keep )
674         {
675             /* we don't have to center the video since it takes the
676             whole rectangle area or it's taller than the rectangle */
677             p_region->i_y = p_sys->i_yoffset
678                     + i_row * ( p_sys->i_height / p_sys->i_rows )
679                     + ( i_row * p_sys->i_hborder ) / p_sys->i_rows;
680         }
681         else
682         {
683             /* center the video in the dedicated rectangle */
684             p_region->i_y = p_sys->i_yoffset
685                     + i_row * ( p_sys->i_height / p_sys->i_rows )
686                     + ( i_row * p_sys->i_hborder ) / p_sys->i_rows
687                     + ( row_inner_height - fmt_out.i_height ) / 2;
688         }
689
690         if( p_region_prev == NULL )
691         {
692             p_spu->p_region = p_region;
693         }
694         else
695         {
696             p_region_prev->p_next = p_region;
697         }
698
699         p_region_prev = p_region;
700     }
701
702     vlc_mutex_unlock( p_sys->p_lock );
703     vlc_mutex_unlock( &p_sys->lock );
704
705     return p_spu;
706 }
707
708 /*****************************************************************************
709 * Callback to update params on the fly
710 *****************************************************************************/
711 static int MosaicCallback( vlc_object_t *p_this, char const *psz_var,
712                             vlc_value_t oldval, vlc_value_t newval,
713                             void *p_data )
714 {
715     filter_sys_t *p_sys = (filter_sys_t *) p_data;
716     if( !strcmp( psz_var, "mosaic-alpha" ) )
717     {
718         vlc_mutex_lock( &p_sys->lock );
719         msg_Dbg( p_this, "Changing alpha from %d/255 to %d/255",
720                          p_sys->i_alpha, newval.i_int);
721         p_sys->i_alpha = __MIN( __MAX( newval.i_int, 0 ), 255 );
722         vlc_mutex_unlock( &p_sys->lock );
723     }
724     else if( !strcmp( psz_var, "mosaic-height" ) )
725     {
726         vlc_mutex_lock( &p_sys->lock );
727         msg_Dbg( p_this, "Changing height from %dpx to %dpx",
728                           p_sys->i_height, newval.i_int );
729         p_sys->i_height = __MAX( newval.i_int, 0 );
730         vlc_mutex_unlock( &p_sys->lock );
731     }
732     else if( !strcmp( psz_var, "mosaic-width" ) )
733     {
734         vlc_mutex_lock( &p_sys->lock );
735         msg_Dbg( p_this, "Changing width from %dpx to %dpx",
736                          p_sys->i_width, newval.i_int );
737         p_sys->i_width = __MAX( newval.i_int, 0 );
738         vlc_mutex_unlock( &p_sys->lock );
739     }
740     else if( !strcmp( psz_var, "mosaic-xoffset" ) )
741     {
742         vlc_mutex_lock( &p_sys->lock );
743         msg_Dbg( p_this, "Changing x offset from %dpx to %dpx",
744                          p_sys->i_xoffset, newval.i_int );
745         p_sys->i_xoffset = __MAX( newval.i_int, 0 );
746         vlc_mutex_unlock( &p_sys->lock );
747     }
748     else if( !strcmp( psz_var, "mosaic-yoffset" ) )
749     {
750         vlc_mutex_lock( &p_sys->lock );
751         msg_Dbg( p_this, "Changing y offset from %dpx to %dpx",
752                          p_sys->i_yoffset, newval.i_int );
753         p_sys->i_yoffset = __MAX( newval.i_int, 0 );
754         vlc_mutex_unlock( &p_sys->lock );
755     }
756     else if( !strcmp( psz_var, "mosaic-align" ) )
757     {
758         int i_old = 0, i_new = 0;
759         vlc_mutex_lock( &p_sys->lock );
760         newval.i_int = __MIN( __MAX( newval.i_int, 0 ), 10 );
761         if( newval.i_int == 3 || newval.i_int == 7 )
762             newval.i_int = 5;
763         while( pi_align_values[i_old] != p_sys->i_align ) i_old++;
764         while( pi_align_values[i_new] != newval.i_int ) i_new++;
765         msg_Dbg( p_this, "Changing alignment from %d (%s) to %d (%s)",
766                      p_sys->i_align, ppsz_align_descriptions[i_old],
767                      newval.i_int, ppsz_align_descriptions[i_new] );
768         p_sys->i_align = newval.i_int;
769         vlc_mutex_unlock( &p_sys->lock );
770     }
771     else if( !strcmp( psz_var, "mosaic-vborder" ) )
772     {
773         vlc_mutex_lock( &p_sys->lock );
774         msg_Dbg( p_this, "Changing vertical border from %dpx to %dpx",
775                          p_sys->i_vborder, newval.i_int );
776         p_sys->i_vborder = __MAX( newval.i_int, 0 );
777         vlc_mutex_unlock( &p_sys->lock );
778     }
779     else if( !strcmp( psz_var, "mosaic-hborder" ) )
780     {
781         vlc_mutex_lock( &p_sys->lock );
782         msg_Dbg( p_this, "Changing horizontal border from %dpx to %dpx",
783                          p_sys->i_vborder, newval.i_int );
784         p_sys->i_hborder = __MAX( newval.i_int, 0 );
785         vlc_mutex_unlock( &p_sys->lock );
786     }
787     else if( !strcmp( psz_var, "mosaic-position" ) )
788     {
789         if( newval.i_int > 1 || newval.i_int < 0 )
790         {
791             msg_Err( p_this, "Position is either 0 (auto) or 1 (fixed)" );
792         }
793         else
794         {
795             vlc_mutex_lock( &p_sys->lock );
796             msg_Dbg( p_this, "Changing position method from %d (%s) to %d (%s)",
797                              p_sys->i_position, ppsz_pos_descriptions[p_sys->i_position],
798                              newval.i_int, ppsz_pos_descriptions[newval.i_int]);
799             p_sys->i_position = newval.i_int;
800             vlc_mutex_unlock( &p_sys->lock );
801         }
802     }
803     else if( !strcmp( psz_var, "mosaic-rows" ) )
804     {
805         vlc_mutex_lock( &p_sys->lock );
806         msg_Dbg( p_this, "Changing number of rows from %d to %d",
807                          p_sys->i_rows, newval.i_int );
808         p_sys->i_rows = __MAX( newval.i_int, 1 );
809         vlc_mutex_unlock( &p_sys->lock );
810     }
811     else if( !strcmp( psz_var, "mosaic-cols" ) )
812     {
813         vlc_mutex_lock( &p_sys->lock );
814         msg_Dbg( p_this, "Changing number of columns from %d to %d",
815                          p_sys->i_cols, newval.i_int );
816         p_sys->i_cols = __MAX( newval.i_int, 1 );
817         vlc_mutex_unlock( &p_sys->lock );
818     }
819     else if( !strcmp( psz_var, "mosaic-keep-aspect-ratio" ) )
820     {
821         vlc_mutex_lock( &p_sys->lock );
822         if( newval.i_int )
823         {
824             msg_Dbg( p_this, "Keep aspect ratio" );
825             p_sys->b_ar = 1;
826         }
827         else
828         {
829             msg_Dbg( p_this, "Don't keep aspect ratio" );
830             p_sys->b_ar = 0;
831         }
832         vlc_mutex_unlock( &p_sys->lock );
833     }
834     return VLC_SUCCESS;
835 }