* 2nd review of video* and control/telnet.c
[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_("Transparency")
91 #define ALPHA_LONGTEXT N_("Transparency of the mosaic foreground pictures. " \
92         "0 means transparent, 255 opaque (default)." )
93
94 #define HEIGHT_TEXT N_("Height")
95 #define HEIGHT_LONGTEXT N_( "Total height of the mosaic, in pixels." )
96 #define WIDTH_TEXT N_("Width")
97 #define WIDTH_LONGTEXT N_( "Total width of the mosaic, in pixels." )
98
99 #define XOFFSET_TEXT N_("Top left corner X coordinate")
100 #define XOFFSET_LONGTEXT N_("X Coordinate of the top-left corner of the mosaic.")
101 #define YOFFSET_TEXT N_("Top left corner Y coordinate")
102 #define YOFFSET_LONGTEXT N_("Y Coordinate of the top-left corner of the mosaic.")
103 #define VBORDER_TEXT N_("Vertical border width")
104 #define VBORDER_LONGTEXT N_( "Width in pixels of the border than can be "\
105     "drawn vertically around the mosaic." )
106 #define HBORDER_TEXT N_("Horizontal border width")
107 #define HBORDER_LONGTEXT N_( "Width in pixels of the border than can "\
108     "be drawn horizontally around the mosaic." )
109
110 #define ALIGN_TEXT N_("Mosaic alignment" )
111 #define ALIGN_LONGTEXT N_( \
112   "You can enforce the mosaic alignment on the video " \
113   "(0=center, 1=left, 2=right, 4=top, 8=bottom, you can " \
114   "also use combinations of these values, eg 6 = top-right).")
115
116 #define POS_TEXT N_("Positioning method")
117 #define POS_LONGTEXT N_("Positioning method for the mosaic. auto: " \
118         "automatically choose the best number of rows and columns. " \
119         "fixed: use the user-defined number of rows and columns.")
120
121 #define ROWS_TEXT N_("Number of rows")
122 #define ROWS_LONGTEXT N_("Number of image rows in the mosaic (only used if "\
123         "positionning method is set to \"fixed\"." )
124 #define COLS_TEXT N_("Number of columns")
125 #define COLS_LONGTEXT N_("Number of image columns in the mosaic (only used if "\
126         "positionning method is set to \"fixed\"." )
127
128 #define AR_TEXT N_("Keep aspect ratio")
129 #define AR_LONGTEXT N_("Keep the original aspect ratio when resizing " \
130         "mosaic elements." )
131 #define KEEP_TEXT N_("Keep original size")
132 #define KEEP_LONGTEXT N_("Keep the original size of mosaic elements." )
133
134 #define ORDER_TEXT N_("Elements order" )
135 #define ORDER_LONGTEXT N_( "You can enforce the order of the elements on " \
136         "the mosaic. You must give a comma-separated list of picture ID(s)." \
137         "These IDs are assigned in the \"mosaic-bridge\" module." )
138
139 #define DELAY_TEXT N_("Delay")
140 #define DELAY_LONGTEXT N_("Pictures coming from the mosaic elements " \
141         "will be delayed according to this value (in milliseconds). For high " \
142         "values you will need to raise caching at input.")
143
144 #define BLUESCREEN_TEXT N_("Bluescreen" )
145 #define BLUESCREEN_LONGTEXT N_( "This effect, also known as \"greenscreen\" "\
146    "or \"chroma key\" blends the \"blue parts\" of the foreground images of " \
147    "the mosaic on the background (like wheather forecast presenters). You " \
148    "can choose the \"key\" color for blending (blue by default)." )
149
150 #define BLUESCREENU_TEXT N_("Bluescreen U value")
151 #define BLUESCREENU_LONGTEXT N_("\"U\" value for the bluescreen key color " \
152         "(in YUV values). From 0 to 255. Defaults to 120 for blue." )
153 #define BLUESCREENV_TEXT N_("Bluescreen V value")
154 #define BLUESCREENV_LONGTEXT N_("\"V\" value for the bluescreen key color " \
155         "(in YUV values). From 0 to 255. Defaults to 90 for blue." )
156 #define BLUESCREENUTOL_TEXT N_("Bluescreen U tolerance")
157 #define BLUESCREENUTOL_LONGTEXT N_("Tolerance of the bluescreen blender " \
158         "on color variations for the U plane. A value between 10 and 20 " \
159         "seems sensible." )
160 #define BLUESCREENVTOL_TEXT N_("Bluescreen V tolerance")
161 #define BLUESCREENVTOL_LONGTEXT N_("Tolerance of the bluescreen blender " \
162         "on color variations for the V plane. A value between 10 and 20 " \
163         "seems sensible." )
164
165 static int pi_pos_values[] = { 0, 1 };
166 static char * ppsz_pos_descriptions[] =
167 { N_("auto"), N_("fixed") };
168
169 static int pi_align_values[] = { 0, 1, 2, 4, 8, 5, 6, 9, 10 };
170 static char *ppsz_align_descriptions[] =
171      { N_("Center"), N_("Left"), N_("Right"), N_("Top"), N_("Bottom"),
172      N_("Top-Left"), N_("Top-Right"), N_("Bottom-Left"), N_("Bottom-Right") };
173
174
175 vlc_module_begin();
176     set_description( N_("Mosaic video sub filter") );
177     set_shortname( N_("Mosaic") );
178     set_category( CAT_VIDEO );
179     set_subcategory( SUBCAT_VIDEO_SUBPIC);
180     set_capability( "sub filter", 0 );
181     set_callbacks( CreateFilter, DestroyFilter );
182
183     add_integer( "mosaic-alpha", 255, NULL, ALPHA_TEXT, ALPHA_LONGTEXT, VLC_FALSE );
184     add_integer( "mosaic-height", 100, NULL, HEIGHT_TEXT, HEIGHT_LONGTEXT, VLC_FALSE );
185     add_integer( "mosaic-width", 100, NULL, WIDTH_TEXT, WIDTH_LONGTEXT, VLC_FALSE );
186     add_integer( "mosaic-align", 5, NULL, ALIGN_TEXT, ALIGN_LONGTEXT, VLC_TRUE);
187         change_integer_list( pi_align_values, ppsz_align_descriptions, 0 );
188     add_integer( "mosaic-xoffset", 0, NULL, XOFFSET_TEXT, XOFFSET_LONGTEXT, VLC_TRUE );
189     add_integer( "mosaic-yoffset", 0, NULL, YOFFSET_TEXT, YOFFSET_LONGTEXT, VLC_TRUE );
190     add_integer( "mosaic-vborder", 0, NULL, VBORDER_TEXT, VBORDER_LONGTEXT, VLC_TRUE );
191     add_integer( "mosaic-hborder", 0, NULL, HBORDER_TEXT, HBORDER_LONGTEXT, VLC_TRUE );
192
193     add_integer( "mosaic-position", 0, NULL, POS_TEXT, POS_LONGTEXT, VLC_FALSE );
194         change_integer_list( pi_pos_values, ppsz_pos_descriptions, 0 );
195     add_integer( "mosaic-rows", 2, NULL, ROWS_TEXT, ROWS_LONGTEXT, VLC_FALSE );
196     add_integer( "mosaic-cols", 2, NULL, COLS_TEXT, COLS_LONGTEXT, VLC_FALSE );
197     add_bool( "mosaic-keep-aspect-ratio", 0, NULL, AR_TEXT, AR_LONGTEXT, VLC_FALSE );
198     add_bool( "mosaic-keep-picture", 0, NULL, KEEP_TEXT, KEEP_LONGTEXT, VLC_FALSE );
199     add_string( "mosaic-order", "", NULL, ORDER_TEXT, ORDER_LONGTEXT, VLC_FALSE );
200
201     add_integer( "mosaic-delay", 0, NULL, DELAY_TEXT, DELAY_LONGTEXT,
202                  VLC_FALSE );
203
204     add_bool( "mosaic-bs", 0, NULL, BLUESCREEN_TEXT,
205               BLUESCREEN_LONGTEXT, VLC_FALSE );
206     add_integer( "mosaic-bsu", 120, NULL, BLUESCREENU_TEXT,
207                  BLUESCREENU_LONGTEXT, VLC_FALSE );
208     add_integer( "mosaic-bsv", 90, NULL, BLUESCREENV_TEXT,
209                  BLUESCREENV_LONGTEXT, VLC_FALSE );
210     add_integer( "mosaic-bsut", 17, NULL, BLUESCREENUTOL_TEXT,
211                  BLUESCREENUTOL_LONGTEXT, VLC_FALSE );
212     add_integer( "mosaic-bsvt", 17, NULL, BLUESCREENVTOL_TEXT,
213                  BLUESCREENVTOL_LONGTEXT, VLC_FALSE );
214
215     var_Create( p_module->p_libvlc, "mosaic-lock", VLC_VAR_MUTEX );
216 vlc_module_end();
217
218
219 /*****************************************************************************
220  * CreateFiler: allocate mosaic video filter
221  *****************************************************************************/
222 static int CreateFilter( vlc_object_t *p_this )
223 {
224     filter_t *p_filter = (filter_t *)p_this;
225     filter_sys_t *p_sys;
226     libvlc_t *p_libvlc = p_filter->p_libvlc;
227     char *psz_order;
228     int i_index;
229     vlc_value_t val;
230
231     /* The mosaic thread is more important than the decoder threads */
232     vlc_thread_set_priority( p_this, VLC_THREAD_PRIORITY_OUTPUT );
233
234     /* Allocate structure */
235     p_sys = p_filter->p_sys = malloc( sizeof( filter_sys_t ) );
236     if( p_sys == NULL )
237     {
238         msg_Err( p_filter, "out of memory" );
239         return VLC_ENOMEM;
240     }
241
242     p_filter->pf_sub_filter = Filter;
243     p_sys->p_pic = NULL;
244
245     vlc_mutex_init( p_filter, &p_sys->lock );
246     vlc_mutex_lock( &p_sys->lock );
247
248     var_Get( p_libvlc, "mosaic-lock", &val );
249     p_sys->p_lock = val.p_address;
250
251 #define GET_VAR( name, min, max )                                           \
252     p_sys->i_##name = __MIN( max, __MAX( min,                               \
253                 var_CreateGetInteger( p_filter, "mosaic-" #name ) ) );      \
254     var_Destroy( p_filter, "mosaic-" #name );                               \
255     var_Create( p_libvlc, "mosaic-" #name, VLC_VAR_INTEGER );               \
256     var_SetInteger( p_libvlc, "mosaic-" #name, p_sys->i_##name );           \
257     var_AddCallback( p_libvlc, "mosaic-" #name, MosaicCallback, p_sys );
258
259     GET_VAR( width, 0, INT_MAX );
260     GET_VAR( height, 0, INT_MAX );
261     GET_VAR( xoffset, 0, INT_MAX );
262     GET_VAR( yoffset, 0, INT_MAX );
263
264     p_sys->i_align = __MIN( 10, __MAX( 0,  var_CreateGetInteger( p_filter, "mosaic-align" ) ) );
265     if( p_sys->i_align == 3 || p_sys->i_align == 7 )
266         p_sys->i_align = 5;
267     var_Destroy( p_filter, "mosaic-align" );
268     var_Create( p_libvlc, "mosaic-align", VLC_VAR_INTEGER );
269     var_SetInteger( p_libvlc, "mosaic-align", p_sys->i_align );
270     var_AddCallback( p_libvlc, "mosaic-align", MosaicCallback, p_sys );
271
272     GET_VAR( vborder, 0, INT_MAX );
273     GET_VAR( hborder, 0, INT_MAX );
274     GET_VAR( rows, 1, INT_MAX );
275     GET_VAR( cols, 1, INT_MAX );
276     GET_VAR( alpha, 0, 255 );
277     GET_VAR( position, 0, 1 );
278     GET_VAR( delay, 100, INT_MAX );
279     p_sys->i_delay *= 1000;
280
281     p_sys->b_ar = var_CreateGetBool( p_filter, "mosaic-keep-aspect-ratio" );
282     var_Destroy( p_filter, "mosaic-keep-aspect-ratio" );
283     var_Create( p_libvlc, "mosaic-keep-aspect-ratio", VLC_VAR_INTEGER );
284     var_SetBool( p_libvlc, "mosaic-keep-aspect-ratio", p_sys->b_ar );
285     var_AddCallback( p_libvlc, "mosaic-keep-aspect-ratio", MosaicCallback,
286                      p_sys );
287
288     p_sys->b_keep = var_CreateGetBool( p_filter, "mosaic-keep-picture" );
289     if ( !p_sys->b_keep )
290     {
291         p_sys->p_image = image_HandlerCreate( p_filter );
292     }
293
294     p_sys->i_order_length = 0;
295     p_sys->ppsz_order = NULL;
296     psz_order = var_CreateGetString( p_filter, "mosaic-order" );
297
298     if( psz_order[0] != 0 )
299     {
300         char *psz_end = NULL;
301         i_index = 0;
302         do
303         {
304             psz_end = strchr( psz_order, ',' );
305             i_index++;
306             p_sys->ppsz_order = realloc( p_sys->ppsz_order,
307                                          i_index * sizeof(char *) );
308             p_sys->ppsz_order[i_index - 1] = strndup( psz_order,
309                                            psz_end - psz_order );
310             psz_order = psz_end+1;
311         } while( NULL !=  psz_end );
312         p_sys->i_order_length = i_index;
313     }
314
315     /* Bluescreen specific stuff */
316     GET_VAR( bsu, 0x00, 0xff );
317     GET_VAR( bsv, 0x00, 0xff );
318     GET_VAR( bsut, 0x00, 0xff );
319     GET_VAR( bsvt, 0x00, 0xff );
320     p_sys->b_bs = var_CreateGetBool( p_filter, "mosaic-bs" );
321     var_Destroy( p_filter, "mosaic-bs" );
322     var_Create( p_libvlc, "mosaic-bs", VLC_VAR_INTEGER );
323     var_SetBool( p_libvlc, "mosaic-bs", p_sys->b_bs );
324     var_AddCallback( p_libvlc, "mosaic-bs", MosaicCallback, p_sys );
325     if( p_sys->b_bs && p_sys->b_keep )
326     {
327         msg_Warn( p_filter, "mosaic-keep-picture needs to be disabled for"
328                             " bluescreen to work" );
329     }
330
331     vlc_mutex_unlock( &p_sys->lock );
332
333     return VLC_SUCCESS;
334 }
335
336 /*****************************************************************************
337  * DestroyFilter: destroy mosaic video filter
338  *****************************************************************************/
339 static void DestroyFilter( vlc_object_t *p_this )
340 {
341     filter_t *p_filter = (filter_t*)p_this;
342     filter_sys_t *p_sys = p_filter->p_sys;
343     libvlc_t *p_libvlc = p_filter->p_libvlc;
344     int i_index;
345
346     vlc_mutex_lock( &p_sys->lock );
347
348     if( !p_sys->b_keep )
349     {
350         image_HandlerDelete( p_sys->p_image );
351     }
352
353     if( p_sys->i_order_length )
354     {
355         for( i_index = 0; i_index < p_sys->i_order_length; i_index++ )
356         {
357             free( p_sys->ppsz_order[i_index] );
358         }
359         free( p_sys->ppsz_order );
360     }
361
362     var_Destroy( p_libvlc, "mosaic-alpha" );
363     var_Destroy( p_libvlc, "mosaic-height" );
364     var_Destroy( p_libvlc, "mosaic-align" );
365     var_Destroy( p_libvlc, "mosaic-width" );
366     var_Destroy( p_libvlc, "mosaic-xoffset" );
367     var_Destroy( p_libvlc, "mosaic-yoffset" );
368     var_Destroy( p_libvlc, "mosaic-vborder" );
369     var_Destroy( p_libvlc, "mosaic-hborder" );
370     var_Destroy( p_libvlc, "mosaic-position" );
371     var_Destroy( p_libvlc, "mosaic-rows" );
372     var_Destroy( p_libvlc, "mosaic-cols" );
373     var_Destroy( p_libvlc, "mosaic-keep-aspect-ratio" );
374
375     var_Destroy( p_libvlc, "mosaic-bsu" );
376     var_Destroy( p_libvlc, "mosaic-bsv" );
377     var_Destroy( p_libvlc, "mosaic-bsut" );
378     var_Destroy( p_libvlc, "mosaic-bsvt" );
379     var_Destroy( p_libvlc, "mosaic-bs" );
380
381     if( p_sys->p_pic ) p_sys->p_pic->pf_release( p_sys->p_pic );
382     vlc_mutex_unlock( &p_sys->lock );
383     vlc_mutex_destroy( &p_sys->lock );
384     free( p_sys );
385 }
386
387 /*****************************************************************************
388  * MosaicReleasePicture : Hack to avoid picture duplication
389  *****************************************************************************/
390 static void MosaicReleasePicture( picture_t *p_picture )
391 {
392     picture_t *p_original_pic = (picture_t *)p_picture->p_sys;
393
394     p_original_pic->pf_release( p_original_pic );
395 }
396
397 /*****************************************************************************
398  * Filter
399  *****************************************************************************/
400 static subpicture_t *Filter( filter_t *p_filter, mtime_t date )
401 {
402     filter_sys_t *p_sys = p_filter->p_sys;
403     bridge_t *p_bridge;
404
405     subpicture_t *p_spu;
406
407     int i_index, i_real_index, i_row, i_col;
408     int i_greatest_real_index_used = p_sys->i_order_length - 1;
409
410     unsigned int col_inner_width, row_inner_height;
411
412     subpicture_region_t *p_region;
413     subpicture_region_t *p_region_prev = NULL;
414
415     /* Allocate the subpicture internal data. */
416     p_spu = p_filter->pf_sub_buffer_new( p_filter );
417     if( !p_spu )
418     {
419         return NULL;
420     }
421
422     /* Initialize subpicture */
423     p_spu->i_channel = 0;
424     p_spu->i_start  = date;
425     p_spu->i_stop = 0;
426     p_spu->b_ephemer = VLC_TRUE;
427     p_spu->i_alpha = p_sys->i_alpha;
428     p_spu->i_flags = p_sys->i_align;
429     p_spu->b_absolute = VLC_FALSE;
430
431     vlc_mutex_lock( &p_sys->lock );
432     vlc_mutex_lock( p_sys->p_lock );
433
434     p_bridge = GetBridge( p_filter );
435     if ( p_bridge == NULL )
436     {
437         vlc_mutex_unlock( p_sys->p_lock );
438         vlc_mutex_unlock( &p_sys->lock );
439         return p_spu;
440     }
441
442     if ( p_sys->i_position == 0 ) /* use automatic positioning */
443     {
444         int i_numpics = p_sys->i_order_length; /* keep slots and all */
445         for ( i_index = 0; i_index < p_bridge->i_es_num; i_index++ )
446         {
447             bridged_es_t *p_es = p_bridge->pp_es[i_index];
448             if ( !p_es->b_empty )
449             {
450                 i_numpics ++;
451                 if( p_sys->i_order_length && p_es->psz_id != 0 )
452                 {
453                     /* We also want to leave slots for images given in
454                      * mosaic-order that are not available in p_vout_picture */
455                     int i;
456                     for( i = 0; i < p_sys->i_order_length ; i++ )
457                     {
458                         if( !strcmp( p_sys->ppsz_order[i], p_es->psz_id ) )
459                         {
460                             i_numpics--;
461                             break;
462                         }
463                     }
464
465                 }
466             }
467         }
468         p_sys->i_rows = ((int)ceil(sqrt( (float)i_numpics )));
469         p_sys->i_cols = ( i_numpics % p_sys->i_rows == 0 ?
470                             i_numpics / p_sys->i_rows :
471                             i_numpics / p_sys->i_rows + 1 );
472     }
473
474     col_inner_width  = ( ( p_sys->i_width - ( p_sys->i_cols - 1 )
475                        * p_sys->i_vborder ) / p_sys->i_cols );
476     row_inner_height = ( ( p_sys->i_height - ( p_sys->i_rows - 1 )
477                        * p_sys->i_hborder ) / p_sys->i_rows );
478
479     i_real_index = 0;
480
481     for ( i_index = 0; i_index < p_bridge->i_es_num; i_index++ )
482     {
483         bridged_es_t *p_es = p_bridge->pp_es[i_index];
484         video_format_t fmt_in = {0}, fmt_out = {0};
485         picture_t *p_converted;
486
487         if ( p_es->b_empty )
488             continue;
489
490         while ( p_es->p_picture != NULL
491                  && p_es->p_picture->date + p_sys->i_delay < date )
492         {
493             if ( p_es->p_picture->p_next != NULL )
494             {
495                 picture_t *p_next = p_es->p_picture->p_next;
496                 p_es->p_picture->pf_release( p_es->p_picture );
497                 p_es->p_picture = p_next;
498             }
499             else if ( p_es->p_picture->date + p_sys->i_delay + BLANK_DELAY <
500                         date )
501             {
502                 /* Display blank */
503                 p_es->p_picture->pf_release( p_es->p_picture );
504                 p_es->p_picture = NULL;
505                 p_es->pp_last = &p_es->p_picture;
506                 break;
507             }
508             else
509             {
510                 msg_Dbg( p_filter, "too late picture for %s (" I64Fd ")",
511                          p_es->psz_id,
512                          date - p_es->p_picture->date - p_sys->i_delay );
513                 break;
514             }
515         }
516
517         if ( p_es->p_picture == NULL )
518             continue;
519
520         if ( p_sys->i_order_length == 0 )
521         {
522             i_real_index++;
523         }
524         else
525         {
526             int i;
527             for ( i = 0; i <= p_sys->i_order_length; i++ )
528             {
529                 if ( i == p_sys->i_order_length ) break;
530                 if ( strcmp( p_es->psz_id, p_sys->ppsz_order[i] ) == 0 )
531                 {
532                     i_real_index = i;
533                     break;
534                 }
535             }
536             if ( i == p_sys->i_order_length )
537                 i_real_index = ++i_greatest_real_index_used;
538         }
539         i_row = ( i_real_index / p_sys->i_cols ) % p_sys->i_rows;
540         i_col = i_real_index % p_sys->i_cols ;
541
542         if ( !p_sys->b_keep )
543         {
544             /* Convert the images */
545             fmt_in.i_chroma = p_es->p_picture->format.i_chroma;
546             fmt_in.i_height = p_es->p_picture->format.i_height;
547             fmt_in.i_width = p_es->p_picture->format.i_width;
548
549             fmt_out.i_chroma = VLC_FOURCC('Y','U','V','A');
550             fmt_out.i_width = col_inner_width;
551             fmt_out.i_height = row_inner_height;
552
553             if( p_sys->b_ar ) /* keep aspect ratio */
554             {
555                 if( (float)fmt_out.i_width / (float)fmt_out.i_height
556                       > (float)fmt_in.i_width / (float)fmt_in.i_height )
557                 {
558                     fmt_out.i_width = ( fmt_out.i_height * fmt_in.i_width )
559                                          / fmt_in.i_height;
560                 }
561                 else
562                 {
563                     fmt_out.i_height = ( fmt_out.i_width * fmt_in.i_height )
564                                         / fmt_in.i_width;
565                 }
566              }
567
568             fmt_out.i_visible_width = fmt_out.i_width;
569             fmt_out.i_visible_height = fmt_out.i_height;
570
571             p_converted = image_Convert( p_sys->p_image, p_es->p_picture,
572                                          &fmt_in, &fmt_out );
573             if( !p_converted )
574             {
575                 msg_Warn( p_filter,
576                            "image resizing and chroma conversion failed" );
577                 continue;
578             }
579
580             /* Bluescreen stuff */
581             if( p_sys->b_bs )
582             {
583                 int i,j;
584                 int i_lines = p_converted->p[ A_PLANE ].i_lines;
585                 int i_pitch = p_converted->p[ A_PLANE ].i_pitch;
586                 uint8_t *p_a = p_converted->p[ A_PLANE ].p_pixels;
587                 uint8_t *p_at = malloc( i_lines * i_pitch * sizeof( uint8_t ) );
588                 uint8_t *p_u = p_converted->p[ U_PLANE ].p_pixels;
589                 uint8_t *p_v = p_converted->p[ V_PLANE ].p_pixels;
590                 uint8_t umin, umax, vmin, vmax;
591                 umin = p_sys->i_bsu - p_sys->i_bsut >= 0x00 ?
592                        p_sys->i_bsu - p_sys->i_bsut : 0x00;
593                 umax = p_sys->i_bsu + p_sys->i_bsut <= 0xff ?
594                        p_sys->i_bsu + p_sys->i_bsut : 0xff;
595                 vmin = p_sys->i_bsv - p_sys->i_bsvt >= 0x00 ?
596                        p_sys->i_bsv - p_sys->i_bsvt : 0x00;
597                 vmax = p_sys->i_bsv + p_sys->i_bsvt <= 0xff ?
598                        p_sys->i_bsv + p_sys->i_bsvt : 0xff;
599
600                 for( i = 0; i < i_lines*i_pitch; i++ )
601                 {
602                     if(    p_u[i] < umax
603                         && p_u[i] > umin
604                         && p_v[i] < vmax
605                         && p_v[i] > vmin )
606                     {
607                         p_at[i] = 0x00;
608                     }
609                     else
610                     {
611                         p_at[i] = 0xff;
612                     }
613                 }
614                 /* Gaussian convolution to make it look cleaner */
615                 memset( p_a, 0, 2 * i_pitch );
616                 for( i = 2; i < i_lines - 2; i++ )
617                 {
618                     p_a[i*i_pitch] = 0x00;
619                     p_a[i*i_pitch+1] = 0x00;
620                     for( j = 2; j < i_pitch - 2; j ++ )
621                     {
622                         p_a[i*i_pitch+j] = (uint8_t)((
623                           /* 2 rows up */
624                             ( p_at[(i-2)*i_pitch+j-2]<<1 )
625                           + ( p_at[(i-2)*i_pitch+j-1]<<2 )
626                           + ( p_at[(i-2)*i_pitch+j]<<2 )
627                           + ( p_at[(i-2)*i_pitch+j+1]<<2 )
628                           + ( p_at[(i-2)*i_pitch+j+2]<<1 )
629                           /* 1 row up */
630                           + ( p_at[(i-1)*i_pitch+j-1]<<3 )
631                           + ( p_at[(i-1)*i_pitch+j-2]<<2 )
632                           + ( p_at[(i-1)*i_pitch+j]*12 )
633                           + ( p_at[(i-1)*i_pitch+j+1]<<3 )
634                           + ( p_at[(i-1)*i_pitch+j+2]<<2 )
635                           /* */
636                           + ( p_at[i*i_pitch+j-2]<<2 )
637                           + ( p_at[i*i_pitch+j-1]*12 )
638                           + ( p_at[i*i_pitch+j]<<4 )
639                           + ( p_at[i*i_pitch+j+1]*12 )
640                           + ( p_at[i*i_pitch+j+2]<<2 )
641                           /* 1 row down */
642                           + ( p_at[(i+1)*i_pitch+j-2]<<2 )
643                           + ( p_at[(i+1)*i_pitch+j-1]<<3 )
644                           + ( p_at[(i+1)*i_pitch+j]*12 )
645                           + ( p_at[(i+1)*i_pitch+j+1]<<3 )
646                           + ( p_at[(i+1)*i_pitch+j+2]<<2 )
647                           /* 2 rows down */
648                           + ( p_at[(i+2)*i_pitch+j-2]<<1 )
649                           + ( p_at[(i+2)*i_pitch+j-1]<<2 )
650                           + ( p_at[(i+2)*i_pitch+j]<<2 )
651                           + ( p_at[(i+2)*i_pitch+j+1]<<2 )
652                           + ( p_at[(i+2)*i_pitch+j+2]<<1 )
653                           )/152);
654                           if( p_a[i*i_pitch+j] < 0xbf ) p_a[i*i_pitch+j] = 0x00;
655                     }
656                 }
657                 free( p_at );
658             }
659         }
660         else
661         {
662             p_converted = p_es->p_picture;
663             p_converted->i_refcount++;
664             fmt_in.i_width = fmt_out.i_width = p_converted->format.i_width;
665             fmt_in.i_height = fmt_out.i_height = p_converted->format.i_height;
666             fmt_in.i_chroma = fmt_out.i_chroma = p_converted->format.i_chroma;
667             fmt_out.i_visible_width = fmt_out.i_width;
668             fmt_out.i_visible_height = fmt_out.i_height;
669         }
670
671         p_region = p_spu->pf_make_region( VLC_OBJECT(p_filter), &fmt_out,
672                                           p_converted );
673         if( !p_region )
674         {
675             msg_Err( p_filter, "cannot allocate SPU region" );
676             p_filter->pf_sub_buffer_del( p_filter, p_spu );
677             vlc_mutex_unlock( &p_sys->lock );
678             vlc_mutex_unlock( p_sys->p_lock );
679             return p_spu;
680         }
681
682         /* HACK ALERT : let's fix the pointers to avoid picture duplication.
683          * This is necessary because p_region->picture is not a pointer
684          * as it ought to be. */
685         if( !p_sys->b_keep )
686         {
687             free( p_converted );
688         }
689         else
690         {
691             /* Keep a pointer to the original picture (and its refcount...). */
692             p_region->picture.p_sys = (picture_sys_t *)p_converted;
693             p_region->picture.pf_release = MosaicReleasePicture;
694         }
695
696         if( fmt_out.i_width > col_inner_width ||
697             p_sys->b_ar || p_sys->b_keep )
698         {
699             /* we don't have to center the video since it takes the
700             whole rectangle area or it's larger than the rectangle */
701             p_region->i_x = p_sys->i_xoffset
702                         + i_col * ( p_sys->i_width / p_sys->i_cols )
703                         + ( i_col * p_sys->i_vborder ) / p_sys->i_cols;
704         }
705         else
706         {
707             /* center the video in the dedicated rectangle */
708             p_region->i_x = p_sys->i_xoffset
709                     + i_col * ( p_sys->i_width / p_sys->i_cols )
710                     + ( i_col * p_sys->i_vborder ) / p_sys->i_cols
711                     + ( col_inner_width - fmt_out.i_width ) / 2;
712         }
713
714         if( fmt_out.i_height < row_inner_height
715             || p_sys->b_ar || p_sys->b_keep )
716         {
717             /* we don't have to center the video since it takes the
718             whole rectangle area or it's taller than the rectangle */
719             p_region->i_y = p_sys->i_yoffset
720                     + i_row * ( p_sys->i_height / p_sys->i_rows )
721                     + ( i_row * p_sys->i_hborder ) / p_sys->i_rows;
722         }
723         else
724         {
725             /* center the video in the dedicated rectangle */
726             p_region->i_y = p_sys->i_yoffset
727                     + i_row * ( p_sys->i_height / p_sys->i_rows )
728                     + ( i_row * p_sys->i_hborder ) / p_sys->i_rows
729                     + ( row_inner_height - fmt_out.i_height ) / 2;
730         }
731
732         if( p_region_prev == NULL )
733         {
734             p_spu->p_region = p_region;
735         }
736         else
737         {
738             p_region_prev->p_next = p_region;
739         }
740
741         p_region_prev = p_region;
742     }
743
744     vlc_mutex_unlock( p_sys->p_lock );
745     vlc_mutex_unlock( &p_sys->lock );
746
747     return p_spu;
748 }
749
750 /*****************************************************************************
751 * Callback to update params on the fly
752 *****************************************************************************/
753 static int MosaicCallback( vlc_object_t *p_this, char const *psz_var,
754                             vlc_value_t oldval, vlc_value_t newval,
755                             void *p_data )
756 {
757     filter_sys_t *p_sys = (filter_sys_t *) p_data;
758     if( !strcmp( psz_var, "mosaic-alpha" ) )
759     {
760         vlc_mutex_lock( &p_sys->lock );
761         msg_Dbg( p_this, "changing alpha from %d/255 to %d/255",
762                          p_sys->i_alpha, newval.i_int);
763         p_sys->i_alpha = __MIN( __MAX( newval.i_int, 0 ), 255 );
764         vlc_mutex_unlock( &p_sys->lock );
765     }
766     else if( !strcmp( psz_var, "mosaic-height" ) )
767     {
768         vlc_mutex_lock( &p_sys->lock );
769         msg_Dbg( p_this, "changing height from %dpx to %dpx",
770                           p_sys->i_height, newval.i_int );
771         p_sys->i_height = __MAX( newval.i_int, 0 );
772         vlc_mutex_unlock( &p_sys->lock );
773     }
774     else if( !strcmp( psz_var, "mosaic-width" ) )
775     {
776         vlc_mutex_lock( &p_sys->lock );
777         msg_Dbg( p_this, "changing width from %dpx to %dpx",
778                          p_sys->i_width, newval.i_int );
779         p_sys->i_width = __MAX( newval.i_int, 0 );
780         vlc_mutex_unlock( &p_sys->lock );
781     }
782     else if( !strcmp( psz_var, "mosaic-xoffset" ) )
783     {
784         vlc_mutex_lock( &p_sys->lock );
785         msg_Dbg( p_this, "changing x offset from %dpx to %dpx",
786                          p_sys->i_xoffset, newval.i_int );
787         p_sys->i_xoffset = __MAX( newval.i_int, 0 );
788         vlc_mutex_unlock( &p_sys->lock );
789     }
790     else if( !strcmp( psz_var, "mosaic-yoffset" ) )
791     {
792         vlc_mutex_lock( &p_sys->lock );
793         msg_Dbg( p_this, "changing y offset from %dpx to %dpx",
794                          p_sys->i_yoffset, newval.i_int );
795         p_sys->i_yoffset = __MAX( newval.i_int, 0 );
796         vlc_mutex_unlock( &p_sys->lock );
797     }
798     else if( !strcmp( psz_var, "mosaic-align" ) )
799     {
800         int i_old = 0, i_new = 0;
801         vlc_mutex_lock( &p_sys->lock );
802         newval.i_int = __MIN( __MAX( newval.i_int, 0 ), 10 );
803         if( newval.i_int == 3 || newval.i_int == 7 )
804             newval.i_int = 5;
805         while( pi_align_values[i_old] != p_sys->i_align ) i_old++;
806         while( pi_align_values[i_new] != newval.i_int ) i_new++;
807         msg_Dbg( p_this, "changing alignment from %d (%s) to %d (%s)",
808                      p_sys->i_align, ppsz_align_descriptions[i_old],
809                      newval.i_int, ppsz_align_descriptions[i_new] );
810         p_sys->i_align = newval.i_int;
811         vlc_mutex_unlock( &p_sys->lock );
812     }
813     else if( !strcmp( psz_var, "mosaic-vborder" ) )
814     {
815         vlc_mutex_lock( &p_sys->lock );
816         msg_Dbg( p_this, "changing vertical border from %dpx to %dpx",
817                          p_sys->i_vborder, newval.i_int );
818         p_sys->i_vborder = __MAX( newval.i_int, 0 );
819         vlc_mutex_unlock( &p_sys->lock );
820     }
821     else if( !strcmp( psz_var, "mosaic-hborder" ) )
822     {
823         vlc_mutex_lock( &p_sys->lock );
824         msg_Dbg( p_this, "changing horizontal border from %dpx to %dpx",
825                          p_sys->i_vborder, newval.i_int );
826         p_sys->i_hborder = __MAX( newval.i_int, 0 );
827         vlc_mutex_unlock( &p_sys->lock );
828     }
829     else if( !strcmp( psz_var, "mosaic-position" ) )
830     {
831         if( newval.i_int > 1 || newval.i_int < 0 )
832         {
833             msg_Err( p_this, "Position is either 0 (auto) or 1 (fixed)" );
834         }
835         else
836         {
837             vlc_mutex_lock( &p_sys->lock );
838             msg_Dbg( p_this, "changing position method from %d (%s) to %d (%s)",
839                              p_sys->i_position, ppsz_pos_descriptions[p_sys->i_position],
840                              newval.i_int, ppsz_pos_descriptions[newval.i_int]);
841             p_sys->i_position = newval.i_int;
842             vlc_mutex_unlock( &p_sys->lock );
843         }
844     }
845     else if( !strcmp( psz_var, "mosaic-rows" ) )
846     {
847         vlc_mutex_lock( &p_sys->lock );
848         msg_Dbg( p_this, "changing number of rows from %d to %d",
849                          p_sys->i_rows, newval.i_int );
850         p_sys->i_rows = __MAX( newval.i_int, 1 );
851         vlc_mutex_unlock( &p_sys->lock );
852     }
853     else if( !strcmp( psz_var, "mosaic-cols" ) )
854     {
855         vlc_mutex_lock( &p_sys->lock );
856         msg_Dbg( p_this, "changing number of columns from %d to %d",
857                          p_sys->i_cols, newval.i_int );
858         p_sys->i_cols = __MAX( newval.i_int, 1 );
859         vlc_mutex_unlock( &p_sys->lock );
860     }
861     else if( !strcmp( psz_var, "mosaic-keep-aspect-ratio" ) )
862     {
863         vlc_mutex_lock( &p_sys->lock );
864         if( newval.i_int )
865         {
866             msg_Dbg( p_this, "keeping aspect ratio" );
867             p_sys->b_ar = 1;
868         }
869         else
870         {
871             msg_Dbg( p_this, "won't keep aspect ratio" );
872             p_sys->b_ar = 0;
873         }
874         vlc_mutex_unlock( &p_sys->lock );
875     }
876     return VLC_SUCCESS;
877 }