]> git.sesse.net Git - vlc/blob - modules/video_filter/mosaic.c
input options whitelisting, step 2 (refs #1371)
[vlc] / modules / video_filter / mosaic.c
1 /*****************************************************************************
2  * mosaic.c : Mosaic video plugin for vlc
3  *****************************************************************************
4  * Copyright (C) 2004-2007 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 <vlc/vlc.h>
29 #include <vlc_vout.h>
30
31 #include <math.h>
32
33 #ifdef HAVE_LIMITS_H
34 #   include <limits.h> /* INT_MAX */
35 #endif
36
37 #include "vlc_filter.h"
38 #include "vlc_image.h"
39
40 #include "mosaic.h"
41
42 #define BLANK_DELAY I64C(1000000)
43
44 /*****************************************************************************
45  * Local prototypes
46  *****************************************************************************/
47 static int  CreateFilter    ( vlc_object_t * );
48 static void DestroyFilter   ( vlc_object_t * );
49 static subpicture_t *Filter ( filter_t *, mtime_t );
50
51 static int MosaicCallback   ( vlc_object_t *, char const *, vlc_value_t,
52                               vlc_value_t, void * );
53
54 /*****************************************************************************
55  * filter_sys_t : filter descriptor
56  *****************************************************************************/
57 struct filter_sys_t
58 {
59     vlc_mutex_t lock;         /* Internal filter lock */
60     vlc_mutex_t *p_lock;      /* Pointer to mosaic bridge lock */
61
62     image_handler_t *p_image;
63
64     int i_position;           /* Mosaic positioning method */
65     vlc_bool_t b_ar;          /* Do we keep the aspect ratio ? */
66     vlc_bool_t b_keep;        /* Do we keep the original picture format ? */
67     int i_width, i_height;    /* Mosaic height and width */
68     int i_cols, i_rows;       /* Mosaic rows and cols */
69     int i_align;              /* Mosaic alignment in background video */
70     int i_xoffset, i_yoffset; /* Top left corner offset */
71     int i_borderw, i_borderh; /* Border width/height between miniatures */
72     int i_alpha;              /* Subfilter alpha blending */
73
74     char **ppsz_order;        /* List of picture-ids */
75     int i_order_length;
76
77     int *pi_x_offsets;        /* List of substreams x offsets */
78     int *pi_y_offsets;        /* List of substreams y offsets */
79     int i_offsets_length;
80
81     mtime_t i_delay;
82 };
83
84 /*****************************************************************************
85  * Module descriptor
86  *****************************************************************************/
87 #define ALPHA_TEXT N_("Transparency")
88 #define ALPHA_LONGTEXT N_( \
89         "Transparency of the mosaic foreground pictures. " \
90         "0 means transparent, 255 opaque (default)." )
91
92 #define HEIGHT_TEXT N_("Height")
93 #define HEIGHT_LONGTEXT N_( "Total height of the mosaic, in pixels." )
94 #define WIDTH_TEXT N_("Width")
95 #define WIDTH_LONGTEXT N_( "Total width of the mosaic, in pixels." )
96
97 #define XOFFSET_TEXT N_("Top left corner X coordinate")
98 #define XOFFSET_LONGTEXT N_( \
99         "X Coordinate of the top-left corner of the mosaic.")
100 #define YOFFSET_TEXT N_("Top left corner Y coordinate")
101 #define YOFFSET_LONGTEXT N_( \
102         "Y Coordinate of the top-left corner of the mosaic.")
103
104 #define BORDERW_TEXT N_("Border width")
105 #define BORDERW_LONGTEXT N_( \
106         "Width in pixels of the border between miniatures." )
107 #define BORDERH_TEXT N_("Border height")
108 #define BORDERH_LONGTEXT N_( \
109         "Height in pixels of the border between miniatures." )
110
111 #define ALIGN_TEXT N_("Mosaic alignment" )
112 #define ALIGN_LONGTEXT N_( \
113         "You can enforce the mosaic alignment on the video " \
114         "(0=center, 1=left, 2=right, 4=top, 8=bottom, you can " \
115         "also use combinations of these values, eg 6 = top-right).")
116
117 #define POS_TEXT N_("Positioning method")
118 #define POS_LONGTEXT N_( \
119         "Positioning method for the mosaic. auto: " \
120         "automatically choose the best number of rows and columns. " \
121         "fixed: use the user-defined number of rows and columns. " \
122         "offsets: use the user-defined offsets for each image." )
123
124 #define ROWS_TEXT N_("Number of rows")
125 #define ROWS_LONGTEXT N_( \
126         "Number of image rows in the mosaic (only used if " \
127         "positionning method is set to \"fixed\")." )
128
129 #define COLS_TEXT N_("Number of columns")
130 #define COLS_LONGTEXT N_( \
131         "Number of image columns in the mosaic (only used if " \
132         "positionning method is set to \"fixed\"." )
133
134 #define AR_TEXT N_("Keep aspect ratio")
135 #define AR_LONGTEXT N_( \
136         "Keep the original aspect ratio when resizing " \
137         "mosaic elements." )
138 #define KEEP_TEXT N_("Keep original size")
139 #define KEEP_LONGTEXT N_( \
140         "Keep the original size of mosaic elements." )
141
142 #define ORDER_TEXT N_("Elements order" )
143 #define ORDER_LONGTEXT N_( \
144         "You can enforce the order of the elements on " \
145         "the mosaic. You must give a comma-separated list of picture ID(s)." \
146         "These IDs are assigned in the \"mosaic-bridge\" module." )
147
148 #define OFFSETS_TEXT N_("Offsets in order" )
149 #define OFFSETS_LONGTEXT N_( \
150         "You can enforce the (x,y) offsets of the elements on the mosaic " \
151         "(only used if positioning method is set to \"offsets\"). You " \
152         "must give a comma-separated list of coordinates (eg: 10,10,150,10)." )
153
154 #define DELAY_TEXT N_("Delay")
155 #define DELAY_LONGTEXT N_( \
156         "Pictures coming from the mosaic elements will be delayed " \
157         "according to this value (in milliseconds). For high " \
158         "values you will need to raise caching at input.")
159
160 enum
161 {
162     position_auto = 0, position_fixed = 1, position_offsets = 2
163 };
164 static int pi_pos_values[] = { 0, 1, 2 };
165 static const char *ppsz_pos_descriptions[] =
166     { N_("auto"), N_("fixed"), N_("offsets") };
167
168 static int pi_align_values[] = { 0, 1, 2, 4, 8, 5, 6, 9, 10 };
169 static const char *ppsz_align_descriptions[] =
170      { N_("Center"), N_("Left"), N_("Right"), N_("Top"), N_("Bottom"),
171      N_("Top-Left"), N_("Top-Right"), N_("Bottom-Left"), N_("Bottom-Right") };
172
173 #define CFG_PREFIX "mosaic-"
174
175 vlc_module_begin();
176     set_description( _("Mosaic video sub filter") );
177     set_shortname( _("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( CFG_PREFIX "alpha", 255, NULL,
184                  ALPHA_TEXT, ALPHA_LONGTEXT, VLC_FALSE );
185         change_safe();
186
187     add_integer( CFG_PREFIX "height", 100, NULL,
188                  HEIGHT_TEXT, HEIGHT_LONGTEXT, VLC_FALSE );
189         change_safe();
190     add_integer( CFG_PREFIX "width", 100, NULL,
191                  WIDTH_TEXT, WIDTH_LONGTEXT, VLC_FALSE );
192         change_safe();
193
194     add_integer( CFG_PREFIX "align", 5, NULL,
195                  ALIGN_TEXT, ALIGN_LONGTEXT, VLC_TRUE);
196         change_safe();
197         change_integer_list( pi_align_values, ppsz_align_descriptions, 0 );
198
199     add_integer( CFG_PREFIX "xoffset", 0, NULL,
200                  XOFFSET_TEXT, XOFFSET_LONGTEXT, VLC_TRUE );
201         change_safe();
202     add_integer( CFG_PREFIX "yoffset", 0, NULL,
203                  YOFFSET_TEXT, YOFFSET_LONGTEXT, VLC_TRUE );
204         change_safe();
205
206     add_integer( CFG_PREFIX "borderw", 0, NULL,
207                  BORDERW_TEXT, BORDERW_LONGTEXT, VLC_TRUE );
208         change_safe();
209         add_deprecated_alias( CFG_PREFIX "vborder" );
210     add_integer( CFG_PREFIX "borderh", 0, NULL,
211                  BORDERH_TEXT, BORDERH_LONGTEXT, VLC_TRUE );
212         change_safe();
213         add_deprecated_alias( CFG_PREFIX "hborder" );
214
215     add_integer( CFG_PREFIX "position", 0, NULL,
216                  POS_TEXT, POS_LONGTEXT, VLC_FALSE );
217         change_safe();
218         change_integer_list( pi_pos_values, ppsz_pos_descriptions, 0 );
219     add_integer( CFG_PREFIX "rows", 2, NULL,
220                  ROWS_TEXT, ROWS_LONGTEXT, VLC_FALSE );
221         change_safe();
222     add_integer( CFG_PREFIX "cols", 2, NULL,
223                  COLS_TEXT, COLS_LONGTEXT, VLC_FALSE );
224         change_safe();
225
226     add_bool( CFG_PREFIX "keep-aspect-ratio", 0, NULL,
227               AR_TEXT, AR_LONGTEXT, VLC_FALSE );
228         change_safe();
229     add_bool( CFG_PREFIX "keep-picture", 0, NULL,
230               KEEP_TEXT, KEEP_LONGTEXT, VLC_FALSE );
231         change_safe();
232
233     add_string( CFG_PREFIX "order", "", NULL,
234                 ORDER_TEXT, ORDER_LONGTEXT, VLC_FALSE );
235         change_safe();
236
237     add_string( CFG_PREFIX "offsets", "", NULL,
238                 OFFSETS_TEXT, OFFSETS_LONGTEXT, VLC_FALSE );
239         change_safe();
240
241     add_integer( CFG_PREFIX "delay", 0, NULL, DELAY_TEXT, DELAY_LONGTEXT,
242                  VLC_FALSE );
243         change_safe();
244 vlc_module_end();
245
246 static const char *ppsz_filter_options[] = {
247     "alpha", "height", "width", "align", "xoffset", "yoffset",
248     "borderw", "borderh", "position", "rows", "cols",
249     "keep-aspect-ratio", "keep-picture", "order", "offsets",
250     "delay", NULL
251 };
252
253 /*****************************************************************************
254  * mosaic_ParseSetOffsets:
255  * parse the "--mosaic-offsets x1,y1,x2,y2,x3,y3" parameter
256  * and set the corresponding struct filter_sys_t entries.
257  *****************************************************************************/
258 #define mosaic_ParseSetOffsets( a, b, c ) \
259       __mosaic_ParseSetOffsets( VLC_OBJECT( a ), b, c )
260 static void __mosaic_ParseSetOffsets( vlc_object_t *p_this,
261                                       filter_sys_t *p_sys,
262                                       char *psz_offsets )
263 {
264     if( *psz_offsets )
265     {
266         char *psz_end = NULL;
267         int i_index = 0;
268         do
269         {
270             i_index++;
271
272             p_sys->pi_x_offsets =
273                 realloc( p_sys->pi_x_offsets, i_index * sizeof(int) );
274             p_sys->pi_x_offsets[i_index - 1] = atoi( psz_offsets );
275             psz_end = strchr( psz_offsets, ',' );
276             psz_offsets = psz_end + 1;
277
278             p_sys->pi_y_offsets =
279                 realloc( p_sys->pi_y_offsets, i_index * sizeof(int) );
280             p_sys->pi_y_offsets[i_index - 1] = atoi( psz_offsets );
281             psz_end = strchr( psz_offsets, ',' );
282             psz_offsets = psz_end + 1;
283
284             msg_Dbg( p_this, CFG_PREFIX "offset: id %d, x=%d, y=%d",
285                      i_index, p_sys->pi_x_offsets[i_index - 1],
286                               p_sys->pi_y_offsets[i_index - 1]  );
287
288         } while( psz_end );
289         p_sys->i_offsets_length = i_index;
290     }
291 }
292
293 /*****************************************************************************
294  * CreateFiler: allocate mosaic video filter
295  *****************************************************************************/
296 static int CreateFilter( vlc_object_t *p_this )
297 {
298     filter_t *p_filter = (filter_t *)p_this;
299     filter_sys_t *p_sys;
300     vlc_object_t *p_libvlc = VLC_OBJECT( p_filter->p_libvlc );
301     char *psz_order;
302     char *psz_offsets;
303     int i_index;
304     vlc_value_t val;
305
306     /* The mosaic thread is more important than the decoder threads */
307     vlc_thread_set_priority( p_this, VLC_THREAD_PRIORITY_OUTPUT );
308
309     /* Allocate structure */
310     p_sys = p_filter->p_sys = malloc( sizeof( filter_sys_t ) );
311     if( p_sys == NULL )
312     {
313         msg_Err( p_filter, "out of memory" );
314         return VLC_ENOMEM;
315     }
316
317     p_filter->pf_sub_filter = Filter;
318
319     vlc_mutex_init( p_filter, &p_sys->lock );
320     vlc_mutex_lock( &p_sys->lock );
321
322     var_Create( p_libvlc, "mosaic-lock", VLC_VAR_MUTEX );
323     var_Get( p_libvlc, "mosaic-lock", &val );
324     p_sys->p_lock = val.p_address;
325
326     config_ChainParse( p_filter, CFG_PREFIX, ppsz_filter_options,
327                        p_filter->p_cfg );
328
329 #define GET_VAR( name, min, max )                                           \
330     p_sys->i_##name = __MIN( max, __MAX( min,                               \
331         var_CreateGetIntegerCommand( p_filter, CFG_PREFIX #name ) ) );      \
332     var_AddCallback( p_filter, CFG_PREFIX #name, MosaicCallback, p_sys );
333
334     GET_VAR( width, 0, INT_MAX );
335     GET_VAR( height, 0, INT_MAX );
336     GET_VAR( xoffset, 0, INT_MAX );
337     GET_VAR( yoffset, 0, INT_MAX );
338
339     GET_VAR( align, 0, 10 );
340     if( p_sys->i_align == 3 || p_sys->i_align == 7 )
341         p_sys->i_align = 5;
342
343     GET_VAR( borderw, 0, INT_MAX );
344     GET_VAR( borderh, 0, INT_MAX );
345     GET_VAR( rows, 1, INT_MAX );
346     GET_VAR( cols, 1, INT_MAX );
347     GET_VAR( alpha, 0, 255 );
348     GET_VAR( position, 0, 2 );
349     GET_VAR( delay, 100, INT_MAX );
350     p_sys->i_delay *= 1000;
351
352     p_sys->b_ar = var_CreateGetBoolCommand( p_filter,
353                                             CFG_PREFIX "keep-aspect-ratio" );
354     var_AddCallback( p_filter, CFG_PREFIX "keep-aspect-ratio", MosaicCallback,
355                      p_sys );
356
357     p_sys->b_keep = var_CreateGetBoolCommand( p_filter,
358                                               CFG_PREFIX "keep-picture" );
359     if ( !p_sys->b_keep )
360     {
361         p_sys->p_image = image_HandlerCreate( p_filter );
362     }
363
364     p_sys->i_order_length = 0;
365     p_sys->ppsz_order = NULL;
366     psz_order = var_CreateGetStringCommand( p_filter, CFG_PREFIX "order" );
367     var_AddCallback( p_filter, CFG_PREFIX "order", MosaicCallback, p_sys );
368
369     if( *psz_order )
370     {
371         char *psz_end = NULL;
372         i_index = 0;
373         do
374         {
375             psz_end = strchr( psz_order, ',' );
376             i_index++;
377             p_sys->ppsz_order = realloc( p_sys->ppsz_order,
378                                          i_index * sizeof(char *) );
379             p_sys->ppsz_order[i_index - 1] = strndup( psz_order,
380                                            psz_end - psz_order );
381             psz_order = psz_end+1;
382         } while( psz_end );
383         p_sys->i_order_length = i_index;
384     }
385
386     /* Manage specific offsets for substreams */
387     psz_offsets = var_CreateGetStringCommand( p_filter, CFG_PREFIX "offsets" );
388     p_sys->i_offsets_length = 0;
389     p_sys->pi_x_offsets = NULL;
390     p_sys->pi_y_offsets = NULL;
391     mosaic_ParseSetOffsets( p_filter, p_sys, psz_offsets );
392     var_AddCallback( p_filter, CFG_PREFIX "offsets", MosaicCallback, p_sys );
393
394     vlc_mutex_unlock( &p_sys->lock );
395
396     return VLC_SUCCESS;
397 }
398
399 /*****************************************************************************
400  * DestroyFilter: destroy mosaic video filter
401  *****************************************************************************/
402 static void DestroyFilter( vlc_object_t *p_this )
403 {
404     filter_t *p_filter = (filter_t*)p_this;
405     filter_sys_t *p_sys = p_filter->p_sys;
406     int i_index;
407
408     vlc_mutex_lock( &p_sys->lock );
409
410     if( !p_sys->b_keep )
411     {
412         image_HandlerDelete( p_sys->p_image );
413     }
414
415     if( p_sys->i_order_length )
416     {
417         for( i_index = 0; i_index < p_sys->i_order_length; i_index++ )
418         {
419             free( p_sys->ppsz_order[i_index] );
420         }
421         free( p_sys->ppsz_order );
422     }
423     if( p_sys->i_offsets_length )
424     {
425         free( p_sys->pi_x_offsets );
426         free( p_sys->pi_y_offsets );
427         p_sys->i_offsets_length = 0;
428     }
429
430     vlc_mutex_unlock( &p_sys->lock );
431     vlc_mutex_destroy( &p_sys->lock );
432     free( p_sys );
433 }
434
435 /*****************************************************************************
436  * MosaicReleasePicture : Hack to avoid picture duplication
437  *****************************************************************************/
438 static void MosaicReleasePicture( picture_t *p_picture )
439 {
440     picture_t *p_original_pic = (picture_t *)p_picture->p_sys;
441
442     p_original_pic->pf_release( p_original_pic );
443 }
444
445 /*****************************************************************************
446  * Filter
447  *****************************************************************************/
448 static subpicture_t *Filter( filter_t *p_filter, mtime_t date )
449 {
450     filter_sys_t *p_sys = p_filter->p_sys;
451     bridge_t *p_bridge;
452
453     subpicture_t *p_spu;
454
455     int i_index, i_real_index, i_row, i_col;
456     int i_greatest_real_index_used = p_sys->i_order_length - 1;
457
458     unsigned int col_inner_width, row_inner_height;
459
460     subpicture_region_t *p_region;
461     subpicture_region_t *p_region_prev = NULL;
462
463     /* Allocate the subpicture internal data. */
464     p_spu = p_filter->pf_sub_buffer_new( p_filter );
465     if( !p_spu )
466     {
467         return NULL;
468     }
469
470     /* Initialize subpicture */
471     p_spu->i_channel = 0;
472     p_spu->i_start  = date;
473     p_spu->i_stop = 0;
474     p_spu->b_ephemer = VLC_TRUE;
475     p_spu->i_alpha = p_sys->i_alpha;
476     p_spu->i_flags = p_sys->i_align;
477     p_spu->b_absolute = VLC_FALSE;
478
479     vlc_mutex_lock( &p_sys->lock );
480     vlc_mutex_lock( p_sys->p_lock );
481
482     p_bridge = GetBridge( p_filter );
483     if ( p_bridge == NULL )
484     {
485         vlc_mutex_unlock( p_sys->p_lock );
486         vlc_mutex_unlock( &p_sys->lock );
487         return p_spu;
488     }
489
490     if ( p_sys->i_position == position_offsets )
491     {
492         /* If we have either too much or not enough offsets, fall-back
493          * to automatic positioning. */
494         if ( p_sys->i_offsets_length != p_sys->i_order_length )
495         {
496             msg_Err( p_filter,
497                      "Number of specified offsets (%d) does not match number "
498                      "of input substreams in mosaic-order (%d), falling back "
499                      "to mosaic-position=0",
500                      p_sys->i_offsets_length, p_sys->i_order_length );
501             p_sys->i_position = position_auto;
502         }
503     }
504
505     if ( p_sys->i_position == position_auto )
506     {
507         int i_numpics = p_sys->i_order_length; /* keep slots and all */
508         for ( i_index = 0; i_index < p_bridge->i_es_num; i_index++ )
509         {
510             bridged_es_t *p_es = p_bridge->pp_es[i_index];
511             if ( !p_es->b_empty )
512             {
513                 i_numpics ++;
514                 if( p_sys->i_order_length && p_es->psz_id != 0 )
515                 {
516                     /* We also want to leave slots for images given in
517                      * mosaic-order that are not available in p_vout_picture */
518                     int i;
519                     for( i = 0; i < p_sys->i_order_length ; i++ )
520                     {
521                         if( !strcmp( p_sys->ppsz_order[i], p_es->psz_id ) )
522                         {
523                             i_numpics--;
524                             break;
525                         }
526                     }
527
528                 }
529             }
530         }
531         p_sys->i_rows = ceil(sqrt( (double)i_numpics ));
532         p_sys->i_cols = ( i_numpics % p_sys->i_rows == 0 ?
533                             i_numpics / p_sys->i_rows :
534                             i_numpics / p_sys->i_rows + 1 );
535     }
536
537     col_inner_width  = ( ( p_sys->i_width - ( p_sys->i_cols - 1 )
538                        * p_sys->i_borderw ) / p_sys->i_cols );
539     row_inner_height = ( ( p_sys->i_height - ( p_sys->i_rows - 1 )
540                        * p_sys->i_borderh ) / p_sys->i_rows );
541
542     i_real_index = 0;
543
544     for ( i_index = 0; i_index < p_bridge->i_es_num; i_index++ )
545     {
546         bridged_es_t *p_es = p_bridge->pp_es[i_index];
547         video_format_t fmt_in, fmt_out;
548         picture_t *p_converted;
549
550         memset( &fmt_in, 0, sizeof( video_format_t ) );
551         memset( &fmt_out, 0, sizeof( video_format_t ) );
552
553         if ( p_es->b_empty )
554             continue;
555
556         while ( p_es->p_picture != NULL
557                  && p_es->p_picture->date + p_sys->i_delay < date )
558         {
559             if ( p_es->p_picture->p_next != NULL )
560             {
561                 picture_t *p_next = p_es->p_picture->p_next;
562                 p_es->p_picture->pf_release( p_es->p_picture );
563                 p_es->p_picture = p_next;
564             }
565             else if ( p_es->p_picture->date + p_sys->i_delay + BLANK_DELAY <
566                         date )
567             {
568                 /* Display blank */
569                 p_es->p_picture->pf_release( p_es->p_picture );
570                 p_es->p_picture = NULL;
571                 p_es->pp_last = &p_es->p_picture;
572                 break;
573             }
574             else
575             {
576                 msg_Dbg( p_filter, "too late picture for %s (" I64Fd ")",
577                          p_es->psz_id,
578                          date - p_es->p_picture->date - p_sys->i_delay );
579                 break;
580             }
581         }
582
583         if ( p_es->p_picture == NULL )
584             continue;
585
586         if ( p_sys->i_order_length == 0 )
587         {
588             i_real_index++;
589         }
590         else
591         {
592             int i;
593             for ( i = 0; i <= p_sys->i_order_length; i++ )
594             {
595                 if ( i == p_sys->i_order_length ) break;
596                 if ( strcmp( p_es->psz_id, p_sys->ppsz_order[i] ) == 0 )
597                 {
598                     i_real_index = i;
599                     break;
600                 }
601             }
602             if ( i == p_sys->i_order_length )
603                 i_real_index = ++i_greatest_real_index_used;
604         }
605         i_row = ( i_real_index / p_sys->i_cols ) % p_sys->i_rows;
606         i_col = i_real_index % p_sys->i_cols ;
607
608         if ( !p_sys->b_keep )
609         {
610             /* Convert the images */
611             fmt_in.i_chroma = p_es->p_picture->format.i_chroma;
612             fmt_in.i_height = p_es->p_picture->format.i_height;
613             fmt_in.i_width = p_es->p_picture->format.i_width;
614
615             fmt_out.i_chroma = VLC_FOURCC('I','4','2','0');
616             fmt_out.i_width = col_inner_width;
617             fmt_out.i_height = row_inner_height;
618
619             if( p_sys->b_ar ) /* keep aspect ratio */
620             {
621                 if( (float)fmt_out.i_width / (float)fmt_out.i_height
622                       > (float)fmt_in.i_width / (float)fmt_in.i_height )
623                 {
624                     fmt_out.i_width = ( fmt_out.i_height * fmt_in.i_width )
625                                          / fmt_in.i_height;
626                 }
627                 else
628                 {
629                     fmt_out.i_height = ( fmt_out.i_width * fmt_in.i_height )
630                                         / fmt_in.i_width;
631                 }
632              }
633
634             fmt_out.i_visible_width = fmt_out.i_width;
635             fmt_out.i_visible_height = fmt_out.i_height;
636
637             p_converted = image_Convert( p_sys->p_image, p_es->p_picture,
638                                          &fmt_in, &fmt_out );
639             if( !p_converted )
640             {
641                 msg_Warn( p_filter,
642                            "image resizing and chroma conversion failed" );
643                 continue;
644             }
645         }
646         else
647         {
648             p_converted = p_es->p_picture;
649             p_converted->i_refcount++;
650             fmt_in.i_width = fmt_out.i_width = p_converted->format.i_width;
651             fmt_in.i_height = fmt_out.i_height = p_converted->format.i_height;
652             fmt_in.i_chroma = fmt_out.i_chroma = p_converted->format.i_chroma;
653             fmt_out.i_visible_width = fmt_out.i_width;
654             fmt_out.i_visible_height = fmt_out.i_height;
655         }
656
657         p_region = p_spu->pf_make_region( VLC_OBJECT(p_filter), &fmt_out,
658                                           p_converted );
659         if( !p_region )
660         {
661             msg_Err( p_filter, "cannot allocate SPU region" );
662             p_filter->pf_sub_buffer_del( p_filter, p_spu );
663             vlc_mutex_unlock( &p_sys->lock );
664             vlc_mutex_unlock( p_sys->p_lock );
665             return p_spu;
666         }
667
668         /* HACK ALERT: let's fix the pointers to avoid picture duplication.
669          * This is necessary because p_region->picture is not a pointer
670          * as it ought to be. */
671         if( !p_sys->b_keep )
672         {
673             free( p_converted );
674         }
675         else
676         {
677             /* Keep a pointer to the original picture (and its refcount...). */
678             p_region->picture.p_sys = (picture_sys_t *)p_converted;
679             p_region->picture.pf_release = MosaicReleasePicture;
680         }
681
682         if( p_sys->i_position == position_offsets )
683         {
684             p_region->i_x = p_sys->pi_x_offsets[i_real_index];
685         }
686         else if( fmt_out.i_width > col_inner_width ||
687             p_sys->b_ar || p_sys->b_keep )
688         {
689             /* we don't have to center the video since it takes the
690             whole rectangle area or it's larger than the rectangle */
691             p_region->i_x = p_sys->i_xoffset
692                         + i_col * ( p_sys->i_width / p_sys->i_cols )
693                         + ( i_col * p_sys->i_borderw ) / p_sys->i_cols;
694         }
695         else
696         {
697             /* center the video in the dedicated rectangle */
698             p_region->i_x = p_sys->i_xoffset
699                     + i_col * ( p_sys->i_width / p_sys->i_cols )
700                     + ( i_col * p_sys->i_borderw ) / p_sys->i_cols
701                     + ( col_inner_width - fmt_out.i_width ) / 2;
702         }
703
704         if( p_sys->i_position == position_offsets )
705         {
706             p_region->i_y = p_sys->pi_y_offsets[i_real_index];
707         }
708         else if( fmt_out.i_height < row_inner_height
709             || p_sys->b_ar || p_sys->b_keep )
710         {
711             /* we don't have to center the video since it takes the
712             whole rectangle area or it's taller than the rectangle */
713             p_region->i_y = p_sys->i_yoffset
714                     + i_row * ( p_sys->i_height / p_sys->i_rows )
715                     + ( i_row * p_sys->i_borderh ) / p_sys->i_rows;
716         }
717         else
718         {
719             /* center the video in the dedicated rectangle */
720             p_region->i_y = p_sys->i_yoffset
721                     + i_row * ( p_sys->i_height / p_sys->i_rows )
722                     + ( i_row * p_sys->i_borderh ) / p_sys->i_rows
723                     + ( row_inner_height - fmt_out.i_height ) / 2;
724         }
725         p_region->i_align = p_sys->i_align;
726
727         if( p_region_prev == NULL )
728         {
729             p_spu->p_region = p_region;
730         }
731         else
732         {
733             p_region_prev->p_next = p_region;
734         }
735
736         p_region_prev = p_region;
737     }
738
739     vlc_mutex_unlock( p_sys->p_lock );
740     vlc_mutex_unlock( &p_sys->lock );
741
742     return p_spu;
743 }
744
745 /*****************************************************************************
746 * Callback to update params on the fly
747 *****************************************************************************/
748 static int MosaicCallback( vlc_object_t *p_this, char const *psz_var,
749                             vlc_value_t oldval, vlc_value_t newval,
750                             void *p_data )
751 {
752     filter_sys_t *p_sys = (filter_sys_t *) p_data;
753
754 #define VAR_IS( a ) !strcmp( psz_var, CFG_PREFIX a )
755     if( VAR_IS( "alpha" ) )
756     {
757         vlc_mutex_lock( &p_sys->lock );
758         msg_Dbg( p_this, "changing alpha from %d/255 to %d/255",
759                          p_sys->i_alpha, newval.i_int);
760         p_sys->i_alpha = __MIN( __MAX( newval.i_int, 0 ), 255 );
761         vlc_mutex_unlock( &p_sys->lock );
762     }
763     else if( VAR_IS( "height" ) )
764     {
765         vlc_mutex_lock( &p_sys->lock );
766         msg_Dbg( p_this, "changing height from %dpx to %dpx",
767                           p_sys->i_height, newval.i_int );
768         p_sys->i_height = __MAX( newval.i_int, 0 );
769         vlc_mutex_unlock( &p_sys->lock );
770     }
771     else if( VAR_IS( "width" ) )
772     {
773         vlc_mutex_lock( &p_sys->lock );
774         msg_Dbg( p_this, "changing width from %dpx to %dpx",
775                          p_sys->i_width, newval.i_int );
776         p_sys->i_width = __MAX( newval.i_int, 0 );
777         vlc_mutex_unlock( &p_sys->lock );
778     }
779     else if( VAR_IS( "xoffset" ) )
780     {
781         vlc_mutex_lock( &p_sys->lock );
782         msg_Dbg( p_this, "changing x offset from %dpx to %dpx",
783                          p_sys->i_xoffset, newval.i_int );
784         p_sys->i_xoffset = __MAX( newval.i_int, 0 );
785         vlc_mutex_unlock( &p_sys->lock );
786     }
787     else if( VAR_IS( "yoffset" ) )
788     {
789         vlc_mutex_lock( &p_sys->lock );
790         msg_Dbg( p_this, "changing y offset from %dpx to %dpx",
791                          p_sys->i_yoffset, newval.i_int );
792         p_sys->i_yoffset = __MAX( newval.i_int, 0 );
793         vlc_mutex_unlock( &p_sys->lock );
794     }
795     else if( VAR_IS( "align" ) )
796     {
797         int i_old = 0, i_new = 0;
798         vlc_mutex_lock( &p_sys->lock );
799         newval.i_int = __MIN( __MAX( newval.i_int, 0 ), 10 );
800         if( newval.i_int == 3 || newval.i_int == 7 )
801             newval.i_int = 5;
802         while( pi_align_values[i_old] != p_sys->i_align ) i_old++;
803         while( pi_align_values[i_new] != newval.i_int ) i_new++;
804         msg_Dbg( p_this, "changing alignment from %d (%s) to %d (%s)",
805                      p_sys->i_align, ppsz_align_descriptions[i_old],
806                      newval.i_int, ppsz_align_descriptions[i_new] );
807         p_sys->i_align = newval.i_int;
808         vlc_mutex_unlock( &p_sys->lock );
809     }
810     else if( VAR_IS( "borderw" ) )
811     {
812         vlc_mutex_lock( &p_sys->lock );
813         msg_Dbg( p_this, "changing border width from %dpx to %dpx",
814                          p_sys->i_borderw, newval.i_int );
815         p_sys->i_borderw = __MAX( newval.i_int, 0 );
816         vlc_mutex_unlock( &p_sys->lock );
817     }
818     else if( VAR_IS( "borderh" ) )
819     {
820         vlc_mutex_lock( &p_sys->lock );
821         msg_Dbg( p_this, "changing border height from %dpx to %dpx",
822                          p_sys->i_borderh, newval.i_int );
823         p_sys->i_borderh = __MAX( newval.i_int, 0 );
824         vlc_mutex_unlock( &p_sys->lock );
825     }
826     else if( VAR_IS( "position" ) )
827     {
828         if( newval.i_int > 2 || newval.i_int < 0 )
829         {
830             msg_Err( p_this,
831                      "Position is either 0 (%s), 1 (%s) or 2 (%s)",
832                      ppsz_pos_descriptions[0],
833                      ppsz_pos_descriptions[1],
834                      ppsz_pos_descriptions[2] );
835         }
836         else
837         {
838             vlc_mutex_lock( &p_sys->lock );
839             msg_Dbg( p_this, "changing position method from %d (%s) to %d (%s)",
840                     p_sys->i_position, ppsz_pos_descriptions[p_sys->i_position],
841                     newval.i_int, ppsz_pos_descriptions[newval.i_int]);
842             p_sys->i_position = newval.i_int;
843             vlc_mutex_unlock( &p_sys->lock );
844         }
845     }
846     else if( VAR_IS( "rows" ) )
847     {
848         vlc_mutex_lock( &p_sys->lock );
849         msg_Dbg( p_this, "changing number of rows from %d to %d",
850                          p_sys->i_rows, newval.i_int );
851         p_sys->i_rows = __MAX( newval.i_int, 1 );
852         vlc_mutex_unlock( &p_sys->lock );
853     }
854     else if( VAR_IS( "cols" ) )
855     {
856         vlc_mutex_lock( &p_sys->lock );
857         msg_Dbg( p_this, "changing number of columns from %d to %d",
858                          p_sys->i_cols, newval.i_int );
859         p_sys->i_cols = __MAX( newval.i_int, 1 );
860         vlc_mutex_unlock( &p_sys->lock );
861     }
862     else if( VAR_IS( "order" ) )
863     {
864         char *psz_order;
865         int i_index;
866         vlc_mutex_lock( &p_sys->lock );
867         msg_Dbg( p_this, "Changing mosaic order to %s", newval.psz_string );
868
869         psz_order = newval.psz_string;
870
871         while( p_sys->i_order_length-- )
872         {
873             free( p_sys->ppsz_order[p_sys->i_order_length] );
874         }
875         free( p_sys->ppsz_order );
876         p_sys->ppsz_order = NULL;
877
878         if( *psz_order )
879         {
880             char *psz_end = NULL;
881             i_index = 0;
882             do
883             {
884                 psz_end = strchr( psz_order, ',' );
885                 i_index++;
886                 p_sys->ppsz_order = realloc( p_sys->ppsz_order,
887                                     i_index * sizeof(char *) );
888                 p_sys->ppsz_order[i_index - 1] = strndup( psz_order,
889                                            psz_end - psz_order );
890                 psz_order = psz_end+1;
891             } while( psz_end );
892             p_sys->i_order_length = i_index;
893         }
894
895         vlc_mutex_unlock( &p_sys->lock );
896     }
897     else if( VAR_IS( "offsets" ) )
898     {
899         vlc_mutex_lock( &p_sys->lock );
900         msg_Info( p_this, "Changing mosaic-offsets to %s", newval.psz_string );
901
902         if( p_sys->i_offsets_length != 0 )
903         {
904             p_sys->i_offsets_length = 0;
905             free( p_sys->pi_x_offsets );
906             free( p_sys->pi_y_offsets );
907             p_sys->pi_x_offsets = NULL;
908             p_sys->pi_y_offsets = NULL;
909         }
910
911         mosaic_ParseSetOffsets( p_this, p_sys, newval.psz_string );
912
913         vlc_mutex_unlock( &p_sys->lock );
914     }
915     else if( VAR_IS( "keep-aspect-ratio" ) )
916     {
917         vlc_mutex_lock( &p_sys->lock );
918         if( newval.i_int )
919         {
920             msg_Dbg( p_this, "keeping aspect ratio" );
921             p_sys->b_ar = 1;
922         }
923         else
924         {
925             msg_Dbg( p_this, "won't keep aspect ratio" );
926             p_sys->b_ar = 0;
927         }
928         vlc_mutex_unlock( &p_sys->lock );
929     }
930     else if( VAR_IS( "keep-picture" ) )
931     {
932         vlc_mutex_lock( &p_sys->lock );
933         p_sys->b_keep = newval.b_bool;
934         if ( !p_sys->b_keep && !p_sys->p_image )
935         {
936             p_sys->p_image = image_HandlerCreate( p_this );
937         }
938         vlc_mutex_unlock( &p_sys->lock );
939     }
940
941     return VLC_SUCCESS;
942 }