]> git.sesse.net Git - vlc/blob - modules/video_filter/mosaic.c
shame on me ... *cough* *cough*
[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,j;
538                 int i_lines = p_converted->p[ A_PLANE ].i_lines;
539                 int i_pitch = p_converted->p[ A_PLANE ].i_pitch;
540                 uint8_t *p_a = p_converted->p[ A_PLANE ].p_pixels;
541                 uint8_t *p_at = malloc( i_lines * i_pitch * sizeof( uint8_t ) );
542                 uint8_t *p_u = p_converted->p[ U_PLANE ].p_pixels;
543                 uint8_t *p_v = p_converted->p[ V_PLANE ].p_pixels;
544                 uint8_t umin, umax, vmin, vmax;
545                 umin = p_sys->i_bsu - p_sys->i_bsut >= 0x00 ?
546                        p_sys->i_bsu - p_sys->i_bsut : 0x00;
547                 umax = p_sys->i_bsu + p_sys->i_bsut <= 0xff ?
548                        p_sys->i_bsu + p_sys->i_bsut : 0xff;
549                 vmin = p_sys->i_bsv - p_sys->i_bsvt >= 0x00 ?
550                        p_sys->i_bsv - p_sys->i_bsvt : 0x00;
551                 vmax = p_sys->i_bsv + p_sys->i_bsvt <= 0xff ?
552                        p_sys->i_bsv + p_sys->i_bsvt : 0xff;
553
554                 for( i = 0; i < i_lines*i_pitch; i++ )
555                 {
556                     if(    p_u[i] < umax
557                         && p_u[i] > umin
558                         && p_v[i] < vmax
559                         && p_v[i] > vmin )
560                     {
561                         p_at[i] = 0x00;
562                     }
563                     else
564                     {
565                         p_at[i] = 0xff;
566                     }
567                 }
568                 /* Gaussian convolution to make it look cleaner */
569                 memset( p_a, 0, 2 * i_pitch );
570                 for( i = 2; i < i_lines - 2; i++ )
571                 {
572                     p_a[i*i_pitch] = 0x00;
573                     p_a[i*i_pitch+1] = 0x00;
574                     for( j = 2; j < i_pitch - 2; j ++ )
575                     {
576                         p_a[i*i_pitch+j] = (uint8_t)((
577                           /* 2 rows up */
578                             ( p_at[(i-2)*i_pitch+j-2]<<1 )
579                           + ( p_at[(i-2)*i_pitch+j-1]<<2 )
580                           + ( p_at[(i-2)*i_pitch+j]<<2 )
581                           + ( p_at[(i-2)*i_pitch+j+1]<<2 )
582                           + ( p_at[(i-2)*i_pitch+j+2]<<1 )
583                           /* 1 row up */
584                           + ( p_at[(i-1)*i_pitch+j-1]<<3 )
585                           + ( p_at[(i-1)*i_pitch+j-2]<<2 )
586                           + ( p_at[(i-1)*i_pitch+j]*12 )
587                           + ( p_at[(i-1)*i_pitch+j+1]<<3 )
588                           + ( p_at[(i-1)*i_pitch+j+2]<<2 )
589                           /* */
590                           + ( p_at[i*i_pitch+j-2]<<2 )
591                           + ( p_at[i*i_pitch+j-1]*12 )
592                           + ( p_at[i*i_pitch+j]<<4 )
593                           + ( p_at[i*i_pitch+j+1]*12 )
594                           + ( p_at[i*i_pitch+j+2]<<2 )
595                           /* 1 row down */
596                           + ( p_at[(i+1)*i_pitch+j-2]<<2 )
597                           + ( p_at[(i+1)*i_pitch+j-1]<<3 )
598                           + ( p_at[(i+1)*i_pitch+j]*12 )
599                           + ( p_at[(i+1)*i_pitch+j+1]<<3 )
600                           + ( p_at[(i+1)*i_pitch+j+2]<<2 )
601                           /* 2 rows down */
602                           + ( p_at[(i+2)*i_pitch+j-2]<<1 )
603                           + ( p_at[(i+2)*i_pitch+j-1]<<2 )
604                           + ( p_at[(i+2)*i_pitch+j]<<2 )
605                           + ( p_at[(i+2)*i_pitch+j+1]<<2 )
606                           + ( p_at[(i+2)*i_pitch+j+2]<<1 )
607                           )/152);
608                           if( p_a[i*i_pitch+j] < 0xbf ) p_a[i*i_pitch+j] = 0x00;
609                     }
610                 }
611                 free( p_at );
612             }
613         }
614         else
615         {
616             p_converted = p_es->p_picture;
617             p_converted->i_refcount++;
618             fmt_in.i_width = fmt_out.i_width = p_converted->format.i_width;
619             fmt_in.i_height = fmt_out.i_height = p_converted->format.i_height;
620             fmt_in.i_chroma = fmt_out.i_chroma = p_converted->format.i_chroma;
621             fmt_out.i_visible_width = fmt_out.i_width;
622             fmt_out.i_visible_height = fmt_out.i_height;
623         }
624
625         p_region = p_spu->pf_make_region( VLC_OBJECT(p_filter), &fmt_out,
626                                           p_converted );
627         if( !p_region )
628         {
629             msg_Err( p_filter, "cannot allocate SPU region" );
630             p_filter->pf_sub_buffer_del( p_filter, p_spu );
631             vlc_mutex_unlock( &p_sys->lock );
632             vlc_mutex_unlock( p_sys->p_lock );
633             return p_spu;
634         }
635
636         /* HACK ALERT : let's fix the pointers to avoid picture duplication.
637          * This is necessary because p_region->picture is not a pointer
638          * as it ought to be. */
639         if( !p_sys->b_keep )
640         {
641             free( p_converted );
642         }
643         else
644         {
645             /* Keep a pointer to the original picture (and its refcount...). */
646             p_region->picture.p_sys = (picture_sys_t *)p_converted;
647             p_region->picture.pf_release = MosaicReleasePicture;
648         }
649
650         if( p_sys->b_ar || p_sys->b_keep ) /* keep aspect ratio */
651         {
652             /* center the video in the dedicated rectangle */
653             p_region->i_x = p_sys->i_xoffset
654                         + i_col * ( p_sys->i_width / p_sys->i_cols )
655                         + ( i_col * p_sys->i_vborder ) / p_sys->i_cols
656                         + ( ( ( p_sys->i_width
657                                  - ( p_sys->i_cols - 1 ) * p_sys->i_vborder )
658                             / p_sys->i_cols ) - fmt_out.i_width ) / 2;
659             p_region->i_y = p_sys->i_yoffset
660                         + i_row * ( p_sys->i_height / p_sys->i_rows )
661                         + ( i_row * p_sys->i_hborder ) / p_sys->i_rows
662                         + ( ( ( p_sys->i_height
663                                  - ( p_sys->i_rows - 1 ) * p_sys->i_hborder )
664                             / p_sys->i_rows ) - fmt_out.i_height ) / 2;
665         }
666         else
667         {
668             /* we don't have to center the video since it takes the
669             whole rectangle area */
670             p_region->i_x = p_sys->i_xoffset
671                             + i_col * ( p_sys->i_width / p_sys->i_cols )
672                             + ( i_col * p_sys->i_vborder ) / p_sys->i_cols;
673             p_region->i_y = p_sys->i_yoffset
674                         + i_row * ( p_sys->i_height / p_sys->i_rows )
675                         + ( i_row * p_sys->i_hborder ) / p_sys->i_rows;
676         }
677
678         if( p_region_prev == NULL )
679         {
680             p_spu->p_region = p_region;
681         }
682         else
683         {
684             p_region_prev->p_next = p_region;
685         }
686
687         p_region_prev = p_region;
688     }
689
690     vlc_mutex_unlock( p_sys->p_lock );
691     vlc_mutex_unlock( &p_sys->lock );
692
693     return p_spu;
694 }
695
696 /*****************************************************************************
697 * Callback to update params on the fly
698 *****************************************************************************/
699 static int MosaicCallback( vlc_object_t *p_this, char const *psz_var,
700                             vlc_value_t oldval, vlc_value_t newval,
701                             void *p_data )
702 {
703     filter_sys_t *p_sys = (filter_sys_t *) p_data;
704     if( !strcmp( psz_var, "mosaic-alpha" ) )
705     {
706         vlc_mutex_lock( &p_sys->lock );
707         msg_Dbg( p_this, "Changing alpha from %d/255 to %d/255",
708                          p_sys->i_alpha, newval.i_int);
709         p_sys->i_alpha = __MIN( __MAX( newval.i_int, 0 ), 255 );
710         vlc_mutex_unlock( &p_sys->lock );
711     }
712     else if( !strcmp( psz_var, "mosaic-height" ) )
713     {
714         vlc_mutex_lock( &p_sys->lock );
715         msg_Dbg( p_this, "Changing height from %dpx to %dpx",
716                           p_sys->i_height, newval.i_int );
717         p_sys->i_height = __MAX( newval.i_int, 0 );
718         vlc_mutex_unlock( &p_sys->lock );
719     }
720     else if( !strcmp( psz_var, "mosaic-width" ) )
721     {
722         vlc_mutex_lock( &p_sys->lock );
723         msg_Dbg( p_this, "Changing width from %dpx to %dpx",
724                          p_sys->i_width, newval.i_int );
725         p_sys->i_width = __MAX( newval.i_int, 0 );
726         vlc_mutex_unlock( &p_sys->lock );
727     }
728     else if( !strcmp( psz_var, "mosaic-xoffset" ) )
729     {
730         vlc_mutex_lock( &p_sys->lock );
731         msg_Dbg( p_this, "Changing x offset from %dpx to %dpx",
732                          p_sys->i_xoffset, newval.i_int );
733         p_sys->i_xoffset = __MAX( newval.i_int, 0 );
734         vlc_mutex_unlock( &p_sys->lock );
735     }
736     else if( !strcmp( psz_var, "mosaic-yoffset" ) )
737     {
738         vlc_mutex_lock( &p_sys->lock );
739         msg_Dbg( p_this, "Changing y offset from %dpx to %dpx",
740                          p_sys->i_yoffset, newval.i_int );
741         p_sys->i_yoffset = __MAX( newval.i_int, 0 );
742         vlc_mutex_unlock( &p_sys->lock );
743     }
744     else if( !strcmp( psz_var, "mosaic-align" ) )
745     {
746         int i_old = 0, i_new = 0;
747         vlc_mutex_lock( &p_sys->lock );
748         newval.i_int = __MIN( __MAX( newval.i_int, 0 ), 10 );
749         if( newval.i_int == 3 || newval.i_int == 7 )
750             newval.i_int = 5;
751         while( pi_align_values[i_old] != p_sys->i_align ) i_old++;
752         while( pi_align_values[i_new] != newval.i_int ) i_new++;
753         msg_Dbg( p_this, "Changing alignment from %d (%s) to %d (%s)",
754                      p_sys->i_align, ppsz_align_descriptions[i_old],
755                      newval.i_int, ppsz_align_descriptions[i_new] );
756         p_sys->i_align = newval.i_int;
757         vlc_mutex_unlock( &p_sys->lock );
758     }
759     else if( !strcmp( psz_var, "mosaic-vborder" ) )
760     {
761         vlc_mutex_lock( &p_sys->lock );
762         msg_Dbg( p_this, "Changing vertical border from %dpx to %dpx",
763                          p_sys->i_vborder, newval.i_int );
764         p_sys->i_vborder = __MAX( newval.i_int, 0 );
765         vlc_mutex_unlock( &p_sys->lock );
766     }
767     else if( !strcmp( psz_var, "mosaic-hborder" ) )
768     {
769         vlc_mutex_lock( &p_sys->lock );
770         msg_Dbg( p_this, "Changing horizontal border from %dpx to %dpx",
771                          p_sys->i_vborder, newval.i_int );
772         p_sys->i_hborder = __MAX( newval.i_int, 0 );
773         vlc_mutex_unlock( &p_sys->lock );
774     }
775     else if( !strcmp( psz_var, "mosaic-position" ) )
776     {
777         if( newval.i_int > 1 || newval.i_int < 0 )
778         {
779             msg_Err( p_this, "Position is either 0 (auto) or 1 (fixed)" );
780         }
781         else
782         {
783             vlc_mutex_lock( &p_sys->lock );
784             msg_Dbg( p_this, "Changing position method from %d (%s) to %d (%s)",
785                              p_sys->i_position, ppsz_pos_descriptions[p_sys->i_position],
786                              newval.i_int, ppsz_pos_descriptions[newval.i_int]);
787             p_sys->i_position = newval.i_int;
788             vlc_mutex_unlock( &p_sys->lock );
789         }
790     }
791     else if( !strcmp( psz_var, "mosaic-rows" ) )
792     {
793         vlc_mutex_lock( &p_sys->lock );
794         msg_Dbg( p_this, "Changing number of rows from %d to %d",
795                          p_sys->i_rows, newval.i_int );
796         p_sys->i_rows = __MAX( newval.i_int, 1 );
797         vlc_mutex_unlock( &p_sys->lock );
798     }
799     else if( !strcmp( psz_var, "mosaic-cols" ) )
800     {
801         vlc_mutex_lock( &p_sys->lock );
802         msg_Dbg( p_this, "Changing number of columns from %d to %d",
803                          p_sys->i_cols, newval.i_int );
804         p_sys->i_cols = __MAX( newval.i_int, 1 );
805         vlc_mutex_unlock( &p_sys->lock );
806     }
807     else if( !strcmp( psz_var, "mosaic-keep-aspect-ratio" ) )
808     {
809         vlc_mutex_lock( &p_sys->lock );
810         if( newval.i_int )
811         {
812             msg_Dbg( p_this, "Keep aspect ratio" );
813             p_sys->b_ar = 1;
814         }
815         else
816         {
817             msg_Dbg( p_this, "Don't keep aspect ratio" );
818             p_sys->b_ar = 0;
819         }
820         vlc_mutex_unlock( &p_sys->lock );
821     }
822     return VLC_SUCCESS;
823 }