]> git.sesse.net Git - vlc/blob - modules/video_filter/mosaic.c
* mosaic.c: Bluescreen/Greenscreen/Chromakey video background replacement
[vlc] / 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     subpicture_region_t *p_region;
369     subpicture_region_t *p_region_prev = NULL;
370
371     /* Allocate the subpicture internal data. */
372     p_spu = p_filter->pf_sub_buffer_new( p_filter );
373     if( !p_spu )
374     {
375         return NULL;
376     }
377
378     /* Initialize subpicture */
379     p_spu->i_channel = 0;
380     p_spu->i_start  = date;
381     p_spu->i_stop = 0;
382     p_spu->b_ephemer = VLC_TRUE;
383     p_spu->i_alpha = p_sys->i_alpha;
384     p_spu->i_flags = p_sys->i_align;
385     p_spu->b_absolute = VLC_FALSE;
386
387     vlc_mutex_lock( &p_sys->lock );
388     vlc_mutex_lock( p_sys->p_lock );
389
390     p_bridge = GetBridge( p_filter );
391     if ( p_bridge == NULL )
392     {
393         vlc_mutex_unlock( p_sys->p_lock );
394         vlc_mutex_unlock( &p_sys->lock );
395         return p_spu;
396     }
397
398     if ( p_sys->i_position == 0 ) /* use automatic positioning */
399     {
400         int i_numpics = p_sys->i_order_length; /* keep slots and all */
401         for ( i_index = 0; i_index < p_bridge->i_es_num; i_index++ )
402         {
403             bridged_es_t *p_es = p_bridge->pp_es[i_index];
404             if ( !p_es->b_empty )
405             {
406                 i_numpics ++;
407                 if( p_sys->i_order_length && p_es->psz_id != 0 )
408                 {
409                     /* We also want to leave slots for images given in
410                      * mosaic-order that are not available in p_vout_picture */
411                     int i;
412                     for( i = 0; i < p_sys->i_order_length ; i++ )
413                     {
414                         if( !strcmp( p_sys->ppsz_order[i], p_es->psz_id ) )
415                         {
416                             i_numpics--;
417                             break;
418                         }
419                     }
420
421                 }
422             }
423         }
424         p_sys->i_rows = ((int)ceil(sqrt( (float)i_numpics )));
425         p_sys->i_cols = ( i_numpics % p_sys->i_rows == 0 ?
426                             i_numpics / p_sys->i_rows :
427                             i_numpics / p_sys->i_rows + 1 );
428     }
429
430     i_real_index = 0;
431
432     for ( i_index = 0; i_index < p_bridge->i_es_num; i_index++ )
433     {
434         bridged_es_t *p_es = p_bridge->pp_es[i_index];
435         video_format_t fmt_in = {0}, fmt_out = {0};
436         picture_t *p_converted;
437
438         if ( p_es->b_empty )
439             continue;
440
441         while ( p_es->p_picture != NULL
442                  && p_es->p_picture->date + p_sys->i_delay < date )
443         {
444             if ( p_es->p_picture->p_next != NULL )
445             {
446                 picture_t *p_next = p_es->p_picture->p_next;
447                 p_es->p_picture->pf_release( p_es->p_picture );
448                 p_es->p_picture = p_next;
449             }
450             else if ( p_es->p_picture->date + p_sys->i_delay + BLANK_DELAY <
451                         date )
452             {
453                 /* Display blank */
454                 p_es->p_picture->pf_release( p_es->p_picture );
455                 p_es->p_picture = NULL;
456                 p_es->pp_last = &p_es->p_picture;
457                 break;
458             }
459             else
460             {
461                 msg_Dbg( p_filter, "too late picture for %s (" I64Fd ")",
462                          p_es->psz_id,
463                          date - p_es->p_picture->date - p_sys->i_delay );
464                 break;
465             }
466         }
467
468         if ( p_es->p_picture == NULL )
469             continue;
470
471         if ( p_sys->i_order_length == 0 )
472         {
473             i_real_index++;
474         }
475         else
476         {
477             int i;
478             for ( i = 0; i <= p_sys->i_order_length; i++ )
479             {
480                 if ( i == p_sys->i_order_length ) break;
481                 if ( strcmp( p_es->psz_id, p_sys->ppsz_order[i] ) == 0 )
482                 {
483                     i_real_index = i;
484                     break;
485                 }
486             }
487             if ( i == p_sys->i_order_length )
488                 i_real_index = ++i_greatest_real_index_used;
489         }
490         i_row = ( i_real_index / p_sys->i_cols ) % p_sys->i_rows;
491         i_col = i_real_index % p_sys->i_cols ;
492
493         if ( !p_sys->b_keep )
494         {
495             /* Convert the images */
496             fmt_in.i_chroma = p_es->p_picture->format.i_chroma;
497             fmt_in.i_height = p_es->p_picture->format.i_height;
498             fmt_in.i_width = p_es->p_picture->format.i_width;
499
500             fmt_out.i_chroma = VLC_FOURCC('Y','U','V','A');
501             fmt_out.i_width = fmt_in.i_width *
502                 ( ( p_sys->i_width - ( p_sys->i_cols - 1 ) * p_sys->i_vborder )
503                   / p_sys->i_cols ) / fmt_in.i_width;
504             fmt_out.i_height = fmt_in.i_height *
505                 ( ( p_sys->i_height - ( p_sys->i_rows - 1 ) * p_sys->i_hborder )
506                   / p_sys->i_rows ) / fmt_in.i_height;
507             if( p_sys->b_ar ) /* keep aspect ratio */
508             {
509                 if( (float)fmt_out.i_width / (float)fmt_out.i_height
510                       > (float)fmt_in.i_width / (float)fmt_in.i_height )
511                 {
512                     fmt_out.i_width = ( fmt_out.i_height * fmt_in.i_width )
513                                          / fmt_in.i_height;
514                 }
515                 else
516                 {
517                     fmt_out.i_height = ( fmt_out.i_width * fmt_in.i_height )
518                                         / fmt_in.i_width;
519                 }
520              }
521
522             fmt_out.i_visible_width = fmt_out.i_width;
523             fmt_out.i_visible_height = fmt_out.i_height;
524
525             p_converted = image_Convert( p_sys->p_image, p_es->p_picture,
526                                          &fmt_in, &fmt_out );
527             if( !p_converted )
528             {
529                 msg_Warn( p_filter,
530                            "image resizing and chroma conversion failed" );
531                 continue;
532             }
533
534             /* Bluescreen stuff */
535             if( p_sys->b_bs )
536             {
537                 int i;
538                 uint8_t *p_a = p_converted->p[ A_PLANE ].p_pixels;
539                 uint8_t *p_u = p_converted->p[ U_PLANE ].p_pixels;
540                 uint8_t *p_v = p_converted->p[ V_PLANE ].p_pixels;
541                 int i_lines = p_converted->p[ A_PLANE ].i_lines;
542                 int i_pitch = p_converted->p[ A_PLANE ].i_pitch;
543                 uint8_t umin, umax, vmin, vmax;
544                 umin = p_sys->i_bsu - p_sys->i_bsut >= 0x00 ?
545                        p_sys->i_bsu - p_sys->i_bsut : 0x00;
546                 umax = p_sys->i_bsu + p_sys->i_bsut <= 0xff ?
547                        p_sys->i_bsu + p_sys->i_bsut : 0xff;
548                 vmin = p_sys->i_bsv - p_sys->i_bsvt >= 0x00 ?
549                        p_sys->i_bsv - p_sys->i_bsvt : 0x00;
550                 vmax = p_sys->i_bsv + p_sys->i_bsvt <= 0xff ?
551                        p_sys->i_bsv + p_sys->i_bsvt : 0xff;
552
553                 for( i = 0; i < i_lines*i_pitch; i++ )
554                 {
555                     if(    p_u[i] < umax
556                         && p_u[i] > umin
557                         && p_v[i] < vmax
558                         && p_v[i] > vmin )
559                     {
560                         p_a[i] = 0x00;
561                     }
562                 }
563             }
564         }
565         else
566         {
567             p_converted = p_es->p_picture;
568             p_converted->i_refcount++;
569             fmt_in.i_width = fmt_out.i_width = p_converted->format.i_width;
570             fmt_in.i_height = fmt_out.i_height = p_converted->format.i_height;
571             fmt_in.i_chroma = fmt_out.i_chroma = p_converted->format.i_chroma;
572             fmt_out.i_visible_width = fmt_out.i_width;
573             fmt_out.i_visible_height = fmt_out.i_height;
574         }
575
576         p_region = p_spu->pf_make_region( VLC_OBJECT(p_filter), &fmt_out,
577                                           p_converted );
578         if( !p_region )
579         {
580             msg_Err( p_filter, "cannot allocate SPU region" );
581             p_filter->pf_sub_buffer_del( p_filter, p_spu );
582             vlc_mutex_unlock( &p_sys->lock );
583             vlc_mutex_unlock( p_sys->p_lock );
584             return p_spu;
585         }
586
587         /* HACK ALERT : let's fix the pointers to avoid picture duplication.
588          * This is necessary because p_region->picture is not a pointer
589          * as it ought to be. */
590         if( !p_sys->b_keep )
591         {
592             free( p_converted );
593         }
594         else
595         {
596             /* Keep a pointer to the original picture (and its refcount...). */
597             p_region->picture.p_sys = (picture_sys_t *)p_converted;
598             p_region->picture.pf_release = MosaicReleasePicture;
599         }
600
601         if( p_sys->b_ar || p_sys->b_keep ) /* keep aspect ratio */
602         {
603             /* center the video in the dedicated rectangle */
604             p_region->i_x = p_sys->i_xoffset
605                         + i_col * ( p_sys->i_width / p_sys->i_cols )
606                         + ( i_col * p_sys->i_vborder ) / p_sys->i_cols
607                         + ( ( ( p_sys->i_width
608                                  - ( p_sys->i_cols - 1 ) * p_sys->i_vborder )
609                             / p_sys->i_cols ) - fmt_out.i_width ) / 2;
610             p_region->i_y = p_sys->i_yoffset
611                         + i_row * ( p_sys->i_height / p_sys->i_rows )
612                         + ( i_row * p_sys->i_hborder ) / p_sys->i_rows
613                         + ( ( ( p_sys->i_height
614                                  - ( p_sys->i_rows - 1 ) * p_sys->i_hborder )
615                             / p_sys->i_rows ) - fmt_out.i_height ) / 2;
616         }
617         else
618         {
619             /* we don't have to center the video since it takes the
620             whole rectangle area */
621             p_region->i_x = p_sys->i_xoffset
622                             + i_col * ( p_sys->i_width / p_sys->i_cols )
623                             + ( i_col * p_sys->i_vborder ) / p_sys->i_cols;
624             p_region->i_y = p_sys->i_yoffset
625                         + i_row * ( p_sys->i_height / p_sys->i_rows )
626                         + ( i_row * p_sys->i_hborder ) / p_sys->i_rows;
627         }
628
629         if( p_region_prev == NULL )
630         {
631             p_spu->p_region = p_region;
632         }
633         else
634         {
635             p_region_prev->p_next = p_region;
636         }
637
638         p_region_prev = p_region;
639     }
640
641     vlc_mutex_unlock( p_sys->p_lock );
642     vlc_mutex_unlock( &p_sys->lock );
643
644     return p_spu;
645 }
646
647 /*****************************************************************************
648 * Callback to update params on the fly
649 *****************************************************************************/
650 static int MosaicCallback( vlc_object_t *p_this, char const *psz_var,
651                             vlc_value_t oldval, vlc_value_t newval,
652                             void *p_data )
653 {
654     filter_sys_t *p_sys = (filter_sys_t *) p_data;
655     if( !strcmp( psz_var, "mosaic-alpha" ) )
656     {
657         vlc_mutex_lock( &p_sys->lock );
658         msg_Dbg( p_this, "Changing alpha from %d/255 to %d/255",
659                          p_sys->i_alpha, newval.i_int);
660         p_sys->i_alpha = __MIN( __MAX( newval.i_int, 0 ), 255 );
661         vlc_mutex_unlock( &p_sys->lock );
662     }
663     else if( !strcmp( psz_var, "mosaic-height" ) )
664     {
665         vlc_mutex_lock( &p_sys->lock );
666         msg_Dbg( p_this, "Changing height from %dpx to %dpx",
667                           p_sys->i_height, newval.i_int );
668         p_sys->i_height = __MAX( newval.i_int, 0 );
669         vlc_mutex_unlock( &p_sys->lock );
670     }
671     else if( !strcmp( psz_var, "mosaic-width" ) )
672     {
673         vlc_mutex_lock( &p_sys->lock );
674         msg_Dbg( p_this, "Changing width from %dpx to %dpx",
675                          p_sys->i_width, newval.i_int );
676         p_sys->i_width = __MAX( newval.i_int, 0 );
677         vlc_mutex_unlock( &p_sys->lock );
678     }
679     else if( !strcmp( psz_var, "mosaic-xoffset" ) )
680     {
681         vlc_mutex_lock( &p_sys->lock );
682         msg_Dbg( p_this, "Changing x offset from %dpx to %dpx",
683                          p_sys->i_xoffset, newval.i_int );
684         p_sys->i_xoffset = __MAX( newval.i_int, 0 );
685         vlc_mutex_unlock( &p_sys->lock );
686     }
687     else if( !strcmp( psz_var, "mosaic-yoffset" ) )
688     {
689         vlc_mutex_lock( &p_sys->lock );
690         msg_Dbg( p_this, "Changing y offset from %dpx to %dpx",
691                          p_sys->i_yoffset, newval.i_int );
692         p_sys->i_yoffset = __MAX( newval.i_int, 0 );
693         vlc_mutex_unlock( &p_sys->lock );
694     }
695     else if( !strcmp( psz_var, "mosaic-align" ) )
696     {
697         int i_old = 0, i_new = 0;
698         vlc_mutex_lock( &p_sys->lock );
699         newval.i_int = __MIN( __MAX( newval.i_int, 0 ), 10 );
700         if( newval.i_int == 3 || newval.i_int == 7 )
701             newval.i_int = 5;
702         while( pi_align_values[i_old] != p_sys->i_align ) i_old++;
703         while( pi_align_values[i_new] != newval.i_int ) i_new++;
704         msg_Dbg( p_this, "Changing alignment from %d (%s) to %d (%s)",
705                      p_sys->i_align, ppsz_align_descriptions[i_old],
706                      newval.i_int, ppsz_align_descriptions[i_new] );
707         p_sys->i_align = newval.i_int;
708         vlc_mutex_unlock( &p_sys->lock );
709     }
710     else if( !strcmp( psz_var, "mosaic-vborder" ) )
711     {
712         vlc_mutex_lock( &p_sys->lock );
713         msg_Dbg( p_this, "Changing vertical border from %dpx to %dpx",
714                          p_sys->i_vborder, newval.i_int );
715         p_sys->i_vborder = __MAX( newval.i_int, 0 );
716         vlc_mutex_unlock( &p_sys->lock );
717     }
718     else if( !strcmp( psz_var, "mosaic-hborder" ) )
719     {
720         vlc_mutex_lock( &p_sys->lock );
721         msg_Dbg( p_this, "Changing horizontal border from %dpx to %dpx",
722                          p_sys->i_vborder, newval.i_int );
723         p_sys->i_hborder = __MAX( newval.i_int, 0 );
724         vlc_mutex_unlock( &p_sys->lock );
725     }
726     else if( !strcmp( psz_var, "mosaic-position" ) )
727     {
728         if( newval.i_int > 1 || newval.i_int < 0 )
729         {
730             msg_Err( p_this, "Position is either 0 (auto) or 1 (fixed)" );
731         }
732         else
733         {
734             vlc_mutex_lock( &p_sys->lock );
735             msg_Dbg( p_this, "Changing position method from %d (%s) to %d (%s)",
736                              p_sys->i_position, ppsz_pos_descriptions[p_sys->i_position],
737                              newval.i_int, ppsz_pos_descriptions[newval.i_int]);
738             p_sys->i_position = newval.i_int;
739             vlc_mutex_unlock( &p_sys->lock );
740         }
741     }
742     else if( !strcmp( psz_var, "mosaic-rows" ) )
743     {
744         vlc_mutex_lock( &p_sys->lock );
745         msg_Dbg( p_this, "Changing number of rows from %d to %d",
746                          p_sys->i_rows, newval.i_int );
747         p_sys->i_rows = __MAX( newval.i_int, 1 );
748         vlc_mutex_unlock( &p_sys->lock );
749     }
750     else if( !strcmp( psz_var, "mosaic-cols" ) )
751     {
752         vlc_mutex_lock( &p_sys->lock );
753         msg_Dbg( p_this, "Changing number of columns from %d to %d",
754                          p_sys->i_cols, newval.i_int );
755         p_sys->i_cols = __MAX( newval.i_int, 1 );
756         vlc_mutex_unlock( &p_sys->lock );
757     }
758     else if( !strcmp( psz_var, "mosaic-keep-aspect-ratio" ) )
759     {
760         vlc_mutex_lock( &p_sys->lock );
761         if( newval.i_int )
762         {
763             msg_Dbg( p_this, "Keep aspect ratio" );
764             p_sys->b_ar = 1;
765         }
766         else
767         {
768             msg_Dbg( p_this, "Don't keep aspect ratio" );
769             p_sys->b_ar = 0;
770         }
771         vlc_mutex_unlock( &p_sys->lock );
772     }
773     return VLC_SUCCESS;
774 }