]> git.sesse.net Git - vlc/blob - modules/video_filter/mosaic.c
Revert the so-called whitelisting commits that are actually blacklisting
[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
186     add_integer( CFG_PREFIX "height", 100, NULL,
187                  HEIGHT_TEXT, HEIGHT_LONGTEXT, VLC_FALSE );
188     add_integer( CFG_PREFIX "width", 100, NULL,
189                  WIDTH_TEXT, WIDTH_LONGTEXT, VLC_FALSE );
190
191     add_integer( CFG_PREFIX "align", 5, NULL,
192                  ALIGN_TEXT, ALIGN_LONGTEXT, VLC_TRUE);
193         change_integer_list( pi_align_values, ppsz_align_descriptions, 0 );
194
195     add_integer( CFG_PREFIX "xoffset", 0, NULL,
196                  XOFFSET_TEXT, XOFFSET_LONGTEXT, VLC_TRUE );
197     add_integer( CFG_PREFIX "yoffset", 0, NULL,
198                  YOFFSET_TEXT, YOFFSET_LONGTEXT, VLC_TRUE );
199
200     add_integer( CFG_PREFIX "borderw", 0, NULL,
201                  BORDERW_TEXT, BORDERW_LONGTEXT, VLC_TRUE );
202         add_deprecated_alias( CFG_PREFIX "vborder" );
203     add_integer( CFG_PREFIX "borderh", 0, NULL,
204                  BORDERH_TEXT, BORDERH_LONGTEXT, VLC_TRUE );
205         add_deprecated_alias( CFG_PREFIX "hborder" );
206
207     add_integer( CFG_PREFIX "position", 0, NULL,
208                  POS_TEXT, POS_LONGTEXT, VLC_FALSE );
209         change_integer_list( pi_pos_values, ppsz_pos_descriptions, 0 );
210     add_integer( CFG_PREFIX "rows", 2, NULL,
211                  ROWS_TEXT, ROWS_LONGTEXT, VLC_FALSE );
212     add_integer( CFG_PREFIX "cols", 2, NULL,
213                  COLS_TEXT, COLS_LONGTEXT, VLC_FALSE );
214
215     add_bool( CFG_PREFIX "keep-aspect-ratio", 0, NULL,
216               AR_TEXT, AR_LONGTEXT, VLC_FALSE );
217     add_bool( CFG_PREFIX "keep-picture", 0, NULL,
218               KEEP_TEXT, KEEP_LONGTEXT, VLC_FALSE );
219
220     add_string( CFG_PREFIX "order", "", NULL,
221                 ORDER_TEXT, ORDER_LONGTEXT, VLC_FALSE );
222
223     add_string( CFG_PREFIX "offsets", "", NULL,
224                 OFFSETS_TEXT, OFFSETS_LONGTEXT, VLC_FALSE );
225
226     add_integer( CFG_PREFIX "delay", 0, NULL, DELAY_TEXT, DELAY_LONGTEXT,
227                  VLC_FALSE );
228 vlc_module_end();
229
230 static const char *ppsz_filter_options[] = {
231     "alpha", "height", "width", "align", "xoffset", "yoffset",
232     "borderw", "borderh", "position", "rows", "cols",
233     "keep-aspect-ratio", "keep-picture", "order", "offsets",
234     "delay", NULL
235 };
236
237 /*****************************************************************************
238  * mosaic_ParseSetOffsets:
239  * parse the "--mosaic-offsets x1,y1,x2,y2,x3,y3" parameter
240  * and set the corresponding struct filter_sys_t entries.
241  *****************************************************************************/
242 #define mosaic_ParseSetOffsets( a, b, c ) \
243       __mosaic_ParseSetOffsets( VLC_OBJECT( a ), b, c )
244 static void __mosaic_ParseSetOffsets( vlc_object_t *p_this,
245                                       filter_sys_t *p_sys,
246                                       char *psz_offsets )
247 {
248     if( *psz_offsets )
249     {
250         char *psz_end = NULL;
251         int i_index = 0;
252         do
253         {
254             i_index++;
255
256             p_sys->pi_x_offsets =
257                 realloc( p_sys->pi_x_offsets, i_index * sizeof(int) );
258             p_sys->pi_x_offsets[i_index - 1] = atoi( psz_offsets );
259             psz_end = strchr( psz_offsets, ',' );
260             psz_offsets = psz_end + 1;
261
262             p_sys->pi_y_offsets =
263                 realloc( p_sys->pi_y_offsets, i_index * sizeof(int) );
264             p_sys->pi_y_offsets[i_index - 1] = atoi( psz_offsets );
265             psz_end = strchr( psz_offsets, ',' );
266             psz_offsets = psz_end + 1;
267
268             msg_Dbg( p_this, CFG_PREFIX "offset: id %d, x=%d, y=%d",
269                      i_index, p_sys->pi_x_offsets[i_index - 1],
270                               p_sys->pi_y_offsets[i_index - 1]  );
271
272         } while( psz_end );
273         p_sys->i_offsets_length = i_index;
274     }
275 }
276
277 /*****************************************************************************
278  * CreateFiler: allocate mosaic video filter
279  *****************************************************************************/
280 static int CreateFilter( vlc_object_t *p_this )
281 {
282     filter_t *p_filter = (filter_t *)p_this;
283     filter_sys_t *p_sys;
284     vlc_object_t *p_libvlc = VLC_OBJECT( p_filter->p_libvlc );
285     char *psz_order;
286     char *psz_offsets;
287     int i_index;
288     vlc_value_t val;
289
290     /* The mosaic thread is more important than the decoder threads */
291     vlc_thread_set_priority( p_this, VLC_THREAD_PRIORITY_OUTPUT );
292
293     /* Allocate structure */
294     p_sys = p_filter->p_sys = malloc( sizeof( filter_sys_t ) );
295     if( p_sys == NULL )
296     {
297         msg_Err( p_filter, "out of memory" );
298         return VLC_ENOMEM;
299     }
300
301     p_filter->pf_sub_filter = Filter;
302
303     vlc_mutex_init( p_filter, &p_sys->lock );
304     vlc_mutex_lock( &p_sys->lock );
305
306     var_Create( p_libvlc, "mosaic-lock", VLC_VAR_MUTEX );
307     var_Get( p_libvlc, "mosaic-lock", &val );
308     p_sys->p_lock = val.p_address;
309
310     config_ChainParse( p_filter, CFG_PREFIX, ppsz_filter_options,
311                        p_filter->p_cfg );
312
313 #define GET_VAR( name, min, max )                                           \
314     p_sys->i_##name = __MIN( max, __MAX( min,                               \
315         var_CreateGetIntegerCommand( p_filter, CFG_PREFIX #name ) ) );      \
316     var_AddCallback( p_filter, CFG_PREFIX #name, MosaicCallback, p_sys );
317
318     GET_VAR( width, 0, INT_MAX );
319     GET_VAR( height, 0, INT_MAX );
320     GET_VAR( xoffset, 0, INT_MAX );
321     GET_VAR( yoffset, 0, INT_MAX );
322
323     GET_VAR( align, 0, 10 );
324     if( p_sys->i_align == 3 || p_sys->i_align == 7 )
325         p_sys->i_align = 5;
326
327     GET_VAR( borderw, 0, INT_MAX );
328     GET_VAR( borderh, 0, INT_MAX );
329     GET_VAR( rows, 1, INT_MAX );
330     GET_VAR( cols, 1, INT_MAX );
331     GET_VAR( alpha, 0, 255 );
332     GET_VAR( position, 0, 2 );
333     GET_VAR( delay, 100, INT_MAX );
334     p_sys->i_delay *= 1000;
335
336     p_sys->b_ar = var_CreateGetBoolCommand( p_filter,
337                                             CFG_PREFIX "keep-aspect-ratio" );
338     var_AddCallback( p_filter, CFG_PREFIX "keep-aspect-ratio", MosaicCallback,
339                      p_sys );
340
341     p_sys->b_keep = var_CreateGetBoolCommand( p_filter,
342                                               CFG_PREFIX "keep-picture" );
343     if ( !p_sys->b_keep )
344     {
345         p_sys->p_image = image_HandlerCreate( p_filter );
346     }
347
348     p_sys->i_order_length = 0;
349     p_sys->ppsz_order = NULL;
350     psz_order = var_CreateGetStringCommand( p_filter, CFG_PREFIX "order" );
351     var_AddCallback( p_filter, CFG_PREFIX "order", MosaicCallback, p_sys );
352
353     if( *psz_order )
354     {
355         char *psz_end = NULL;
356         i_index = 0;
357         do
358         {
359             psz_end = strchr( psz_order, ',' );
360             i_index++;
361             p_sys->ppsz_order = realloc( p_sys->ppsz_order,
362                                          i_index * sizeof(char *) );
363             p_sys->ppsz_order[i_index - 1] = strndup( psz_order,
364                                            psz_end - psz_order );
365             psz_order = psz_end+1;
366         } while( psz_end );
367         p_sys->i_order_length = i_index;
368     }
369
370     /* Manage specific offsets for substreams */
371     psz_offsets = var_CreateGetStringCommand( p_filter, CFG_PREFIX "offsets" );
372     p_sys->i_offsets_length = 0;
373     p_sys->pi_x_offsets = NULL;
374     p_sys->pi_y_offsets = NULL;
375     mosaic_ParseSetOffsets( p_filter, p_sys, psz_offsets );
376     var_AddCallback( p_filter, CFG_PREFIX "offsets", MosaicCallback, p_sys );
377
378     vlc_mutex_unlock( &p_sys->lock );
379
380     return VLC_SUCCESS;
381 }
382
383 /*****************************************************************************
384  * DestroyFilter: destroy mosaic video filter
385  *****************************************************************************/
386 static void DestroyFilter( vlc_object_t *p_this )
387 {
388     filter_t *p_filter = (filter_t*)p_this;
389     filter_sys_t *p_sys = p_filter->p_sys;
390     int i_index;
391
392     vlc_mutex_lock( &p_sys->lock );
393
394     if( !p_sys->b_keep )
395     {
396         image_HandlerDelete( p_sys->p_image );
397     }
398
399     if( p_sys->i_order_length )
400     {
401         for( i_index = 0; i_index < p_sys->i_order_length; i_index++ )
402         {
403             free( p_sys->ppsz_order[i_index] );
404         }
405         free( p_sys->ppsz_order );
406     }
407     if( p_sys->i_offsets_length )
408     {
409         free( p_sys->pi_x_offsets );
410         free( p_sys->pi_y_offsets );
411         p_sys->i_offsets_length = 0;
412     }
413
414     vlc_mutex_unlock( &p_sys->lock );
415     vlc_mutex_destroy( &p_sys->lock );
416     free( p_sys );
417 }
418
419 /*****************************************************************************
420  * MosaicReleasePicture : Hack to avoid picture duplication
421  *****************************************************************************/
422 static void MosaicReleasePicture( picture_t *p_picture )
423 {
424     picture_t *p_original_pic = (picture_t *)p_picture->p_sys;
425
426     p_original_pic->pf_release( p_original_pic );
427 }
428
429 /*****************************************************************************
430  * Filter
431  *****************************************************************************/
432 static subpicture_t *Filter( filter_t *p_filter, mtime_t date )
433 {
434     filter_sys_t *p_sys = p_filter->p_sys;
435     bridge_t *p_bridge;
436
437     subpicture_t *p_spu;
438
439     int i_index, i_real_index, i_row, i_col;
440     int i_greatest_real_index_used = p_sys->i_order_length - 1;
441
442     unsigned int col_inner_width, row_inner_height;
443
444     subpicture_region_t *p_region;
445     subpicture_region_t *p_region_prev = NULL;
446
447     /* Allocate the subpicture internal data. */
448     p_spu = p_filter->pf_sub_buffer_new( p_filter );
449     if( !p_spu )
450     {
451         return NULL;
452     }
453
454     /* Initialize subpicture */
455     p_spu->i_channel = 0;
456     p_spu->i_start  = date;
457     p_spu->i_stop = 0;
458     p_spu->b_ephemer = VLC_TRUE;
459     p_spu->i_alpha = p_sys->i_alpha;
460     p_spu->i_flags = p_sys->i_align;
461     p_spu->b_absolute = VLC_FALSE;
462
463     vlc_mutex_lock( &p_sys->lock );
464     vlc_mutex_lock( p_sys->p_lock );
465
466     p_bridge = GetBridge( p_filter );
467     if ( p_bridge == NULL )
468     {
469         vlc_mutex_unlock( p_sys->p_lock );
470         vlc_mutex_unlock( &p_sys->lock );
471         return p_spu;
472     }
473
474     if ( p_sys->i_position == position_offsets )
475     {
476         /* If we have either too much or not enough offsets, fall-back
477          * to automatic positioning. */
478         if ( p_sys->i_offsets_length != p_sys->i_order_length )
479         {
480             msg_Err( p_filter,
481                      "Number of specified offsets (%d) does not match number "
482                      "of input substreams in mosaic-order (%d), falling back "
483                      "to mosaic-position=0",
484                      p_sys->i_offsets_length, p_sys->i_order_length );
485             p_sys->i_position = position_auto;
486         }
487     }
488
489     if ( p_sys->i_position == position_auto )
490     {
491         int i_numpics = p_sys->i_order_length; /* keep slots and all */
492         for ( i_index = 0; i_index < p_bridge->i_es_num; i_index++ )
493         {
494             bridged_es_t *p_es = p_bridge->pp_es[i_index];
495             if ( !p_es->b_empty )
496             {
497                 i_numpics ++;
498                 if( p_sys->i_order_length && p_es->psz_id != 0 )
499                 {
500                     /* We also want to leave slots for images given in
501                      * mosaic-order that are not available in p_vout_picture */
502                     int i;
503                     for( i = 0; i < p_sys->i_order_length ; i++ )
504                     {
505                         if( !strcmp( p_sys->ppsz_order[i], p_es->psz_id ) )
506                         {
507                             i_numpics--;
508                             break;
509                         }
510                     }
511
512                 }
513             }
514         }
515         p_sys->i_rows = ceil(sqrt( (double)i_numpics ));
516         p_sys->i_cols = ( i_numpics % p_sys->i_rows == 0 ?
517                             i_numpics / p_sys->i_rows :
518                             i_numpics / p_sys->i_rows + 1 );
519     }
520
521     col_inner_width  = ( ( p_sys->i_width - ( p_sys->i_cols - 1 )
522                        * p_sys->i_borderw ) / p_sys->i_cols );
523     row_inner_height = ( ( p_sys->i_height - ( p_sys->i_rows - 1 )
524                        * p_sys->i_borderh ) / p_sys->i_rows );
525
526     i_real_index = 0;
527
528     for ( i_index = 0; i_index < p_bridge->i_es_num; i_index++ )
529     {
530         bridged_es_t *p_es = p_bridge->pp_es[i_index];
531         video_format_t fmt_in, fmt_out;
532         picture_t *p_converted;
533
534         memset( &fmt_in, 0, sizeof( video_format_t ) );
535         memset( &fmt_out, 0, sizeof( video_format_t ) );
536
537         if ( p_es->b_empty )
538             continue;
539
540         while ( p_es->p_picture != NULL
541                  && p_es->p_picture->date + p_sys->i_delay < date )
542         {
543             if ( p_es->p_picture->p_next != NULL )
544             {
545                 picture_t *p_next = p_es->p_picture->p_next;
546                 p_es->p_picture->pf_release( p_es->p_picture );
547                 p_es->p_picture = p_next;
548             }
549             else if ( p_es->p_picture->date + p_sys->i_delay + BLANK_DELAY <
550                         date )
551             {
552                 /* Display blank */
553                 p_es->p_picture->pf_release( p_es->p_picture );
554                 p_es->p_picture = NULL;
555                 p_es->pp_last = &p_es->p_picture;
556                 break;
557             }
558             else
559             {
560                 msg_Dbg( p_filter, "too late picture for %s (" I64Fd ")",
561                          p_es->psz_id,
562                          date - p_es->p_picture->date - p_sys->i_delay );
563                 break;
564             }
565         }
566
567         if ( p_es->p_picture == NULL )
568             continue;
569
570         if ( p_sys->i_order_length == 0 )
571         {
572             i_real_index++;
573         }
574         else
575         {
576             int i;
577             for ( i = 0; i <= p_sys->i_order_length; i++ )
578             {
579                 if ( i == p_sys->i_order_length ) break;
580                 if ( strcmp( p_es->psz_id, p_sys->ppsz_order[i] ) == 0 )
581                 {
582                     i_real_index = i;
583                     break;
584                 }
585             }
586             if ( i == p_sys->i_order_length )
587                 i_real_index = ++i_greatest_real_index_used;
588         }
589         i_row = ( i_real_index / p_sys->i_cols ) % p_sys->i_rows;
590         i_col = i_real_index % p_sys->i_cols ;
591
592         if ( !p_sys->b_keep )
593         {
594             /* Convert the images */
595             fmt_in.i_chroma = p_es->p_picture->format.i_chroma;
596             fmt_in.i_height = p_es->p_picture->format.i_height;
597             fmt_in.i_width = p_es->p_picture->format.i_width;
598
599             fmt_out.i_chroma = VLC_FOURCC('I','4','2','0');
600             fmt_out.i_width = col_inner_width;
601             fmt_out.i_height = row_inner_height;
602
603             if( p_sys->b_ar ) /* keep aspect ratio */
604             {
605                 if( (float)fmt_out.i_width / (float)fmt_out.i_height
606                       > (float)fmt_in.i_width / (float)fmt_in.i_height )
607                 {
608                     fmt_out.i_width = ( fmt_out.i_height * fmt_in.i_width )
609                                          / fmt_in.i_height;
610                 }
611                 else
612                 {
613                     fmt_out.i_height = ( fmt_out.i_width * fmt_in.i_height )
614                                         / fmt_in.i_width;
615                 }
616              }
617
618             fmt_out.i_visible_width = fmt_out.i_width;
619             fmt_out.i_visible_height = fmt_out.i_height;
620
621             p_converted = image_Convert( p_sys->p_image, p_es->p_picture,
622                                          &fmt_in, &fmt_out );
623             if( !p_converted )
624             {
625                 msg_Warn( p_filter,
626                            "image resizing and chroma conversion failed" );
627                 continue;
628             }
629         }
630         else
631         {
632             p_converted = p_es->p_picture;
633             p_converted->i_refcount++;
634             fmt_in.i_width = fmt_out.i_width = p_converted->format.i_width;
635             fmt_in.i_height = fmt_out.i_height = p_converted->format.i_height;
636             fmt_in.i_chroma = fmt_out.i_chroma = p_converted->format.i_chroma;
637             fmt_out.i_visible_width = fmt_out.i_width;
638             fmt_out.i_visible_height = fmt_out.i_height;
639         }
640
641         p_region = p_spu->pf_make_region( VLC_OBJECT(p_filter), &fmt_out,
642                                           p_converted );
643         if( !p_region )
644         {
645             msg_Err( p_filter, "cannot allocate SPU region" );
646             p_filter->pf_sub_buffer_del( p_filter, p_spu );
647             vlc_mutex_unlock( &p_sys->lock );
648             vlc_mutex_unlock( p_sys->p_lock );
649             return p_spu;
650         }
651
652         /* HACK ALERT: let's fix the pointers to avoid picture duplication.
653          * This is necessary because p_region->picture is not a pointer
654          * as it ought to be. */
655         if( !p_sys->b_keep )
656         {
657             free( p_converted );
658         }
659         else
660         {
661             /* Keep a pointer to the original picture (and its refcount...). */
662             p_region->picture.p_sys = (picture_sys_t *)p_converted;
663             p_region->picture.pf_release = MosaicReleasePicture;
664         }
665
666         if( p_sys->i_position == position_offsets )
667         {
668             p_region->i_x = p_sys->pi_x_offsets[i_real_index];
669         }
670         else if( fmt_out.i_width > col_inner_width ||
671             p_sys->b_ar || p_sys->b_keep )
672         {
673             /* we don't have to center the video since it takes the
674             whole rectangle area or it's larger than the rectangle */
675             p_region->i_x = p_sys->i_xoffset
676                         + i_col * ( p_sys->i_width / p_sys->i_cols )
677                         + ( i_col * p_sys->i_borderw ) / p_sys->i_cols;
678         }
679         else
680         {
681             /* center the video in the dedicated rectangle */
682             p_region->i_x = p_sys->i_xoffset
683                     + i_col * ( p_sys->i_width / p_sys->i_cols )
684                     + ( i_col * p_sys->i_borderw ) / p_sys->i_cols
685                     + ( col_inner_width - fmt_out.i_width ) / 2;
686         }
687
688         if( p_sys->i_position == position_offsets )
689         {
690             p_region->i_y = p_sys->pi_y_offsets[i_real_index];
691         }
692         else if( fmt_out.i_height < row_inner_height
693             || p_sys->b_ar || p_sys->b_keep )
694         {
695             /* we don't have to center the video since it takes the
696             whole rectangle area or it's taller than the rectangle */
697             p_region->i_y = p_sys->i_yoffset
698                     + i_row * ( p_sys->i_height / p_sys->i_rows )
699                     + ( i_row * p_sys->i_borderh ) / p_sys->i_rows;
700         }
701         else
702         {
703             /* center the video in the dedicated rectangle */
704             p_region->i_y = p_sys->i_yoffset
705                     + i_row * ( p_sys->i_height / p_sys->i_rows )
706                     + ( i_row * p_sys->i_borderh ) / p_sys->i_rows
707                     + ( row_inner_height - fmt_out.i_height ) / 2;
708         }
709         p_region->i_align = p_sys->i_align;
710
711         if( p_region_prev == NULL )
712         {
713             p_spu->p_region = p_region;
714         }
715         else
716         {
717             p_region_prev->p_next = p_region;
718         }
719
720         p_region_prev = p_region;
721     }
722
723     vlc_mutex_unlock( p_sys->p_lock );
724     vlc_mutex_unlock( &p_sys->lock );
725
726     return p_spu;
727 }
728
729 /*****************************************************************************
730 * Callback to update params on the fly
731 *****************************************************************************/
732 static int MosaicCallback( vlc_object_t *p_this, char const *psz_var,
733                             vlc_value_t oldval, vlc_value_t newval,
734                             void *p_data )
735 {
736     filter_sys_t *p_sys = (filter_sys_t *) p_data;
737
738 #define VAR_IS( a ) !strcmp( psz_var, CFG_PREFIX a )
739     if( VAR_IS( "alpha" ) )
740     {
741         vlc_mutex_lock( &p_sys->lock );
742         msg_Dbg( p_this, "changing alpha from %d/255 to %d/255",
743                          p_sys->i_alpha, newval.i_int);
744         p_sys->i_alpha = __MIN( __MAX( newval.i_int, 0 ), 255 );
745         vlc_mutex_unlock( &p_sys->lock );
746     }
747     else if( VAR_IS( "height" ) )
748     {
749         vlc_mutex_lock( &p_sys->lock );
750         msg_Dbg( p_this, "changing height from %dpx to %dpx",
751                           p_sys->i_height, newval.i_int );
752         p_sys->i_height = __MAX( newval.i_int, 0 );
753         vlc_mutex_unlock( &p_sys->lock );
754     }
755     else if( VAR_IS( "width" ) )
756     {
757         vlc_mutex_lock( &p_sys->lock );
758         msg_Dbg( p_this, "changing width from %dpx to %dpx",
759                          p_sys->i_width, newval.i_int );
760         p_sys->i_width = __MAX( newval.i_int, 0 );
761         vlc_mutex_unlock( &p_sys->lock );
762     }
763     else if( VAR_IS( "xoffset" ) )
764     {
765         vlc_mutex_lock( &p_sys->lock );
766         msg_Dbg( p_this, "changing x offset from %dpx to %dpx",
767                          p_sys->i_xoffset, newval.i_int );
768         p_sys->i_xoffset = __MAX( newval.i_int, 0 );
769         vlc_mutex_unlock( &p_sys->lock );
770     }
771     else if( VAR_IS( "yoffset" ) )
772     {
773         vlc_mutex_lock( &p_sys->lock );
774         msg_Dbg( p_this, "changing y offset from %dpx to %dpx",
775                          p_sys->i_yoffset, newval.i_int );
776         p_sys->i_yoffset = __MAX( newval.i_int, 0 );
777         vlc_mutex_unlock( &p_sys->lock );
778     }
779     else if( VAR_IS( "align" ) )
780     {
781         int i_old = 0, i_new = 0;
782         vlc_mutex_lock( &p_sys->lock );
783         newval.i_int = __MIN( __MAX( newval.i_int, 0 ), 10 );
784         if( newval.i_int == 3 || newval.i_int == 7 )
785             newval.i_int = 5;
786         while( pi_align_values[i_old] != p_sys->i_align ) i_old++;
787         while( pi_align_values[i_new] != newval.i_int ) i_new++;
788         msg_Dbg( p_this, "changing alignment from %d (%s) to %d (%s)",
789                      p_sys->i_align, ppsz_align_descriptions[i_old],
790                      newval.i_int, ppsz_align_descriptions[i_new] );
791         p_sys->i_align = newval.i_int;
792         vlc_mutex_unlock( &p_sys->lock );
793     }
794     else if( VAR_IS( "borderw" ) )
795     {
796         vlc_mutex_lock( &p_sys->lock );
797         msg_Dbg( p_this, "changing border width from %dpx to %dpx",
798                          p_sys->i_borderw, newval.i_int );
799         p_sys->i_borderw = __MAX( newval.i_int, 0 );
800         vlc_mutex_unlock( &p_sys->lock );
801     }
802     else if( VAR_IS( "borderh" ) )
803     {
804         vlc_mutex_lock( &p_sys->lock );
805         msg_Dbg( p_this, "changing border height from %dpx to %dpx",
806                          p_sys->i_borderh, newval.i_int );
807         p_sys->i_borderh = __MAX( newval.i_int, 0 );
808         vlc_mutex_unlock( &p_sys->lock );
809     }
810     else if( VAR_IS( "position" ) )
811     {
812         if( newval.i_int > 2 || newval.i_int < 0 )
813         {
814             msg_Err( p_this,
815                      "Position is either 0 (%s), 1 (%s) or 2 (%s)",
816                      ppsz_pos_descriptions[0],
817                      ppsz_pos_descriptions[1],
818                      ppsz_pos_descriptions[2] );
819         }
820         else
821         {
822             vlc_mutex_lock( &p_sys->lock );
823             msg_Dbg( p_this, "changing position method from %d (%s) to %d (%s)",
824                     p_sys->i_position, ppsz_pos_descriptions[p_sys->i_position],
825                     newval.i_int, ppsz_pos_descriptions[newval.i_int]);
826             p_sys->i_position = newval.i_int;
827             vlc_mutex_unlock( &p_sys->lock );
828         }
829     }
830     else if( VAR_IS( "rows" ) )
831     {
832         vlc_mutex_lock( &p_sys->lock );
833         msg_Dbg( p_this, "changing number of rows from %d to %d",
834                          p_sys->i_rows, newval.i_int );
835         p_sys->i_rows = __MAX( newval.i_int, 1 );
836         vlc_mutex_unlock( &p_sys->lock );
837     }
838     else if( VAR_IS( "cols" ) )
839     {
840         vlc_mutex_lock( &p_sys->lock );
841         msg_Dbg( p_this, "changing number of columns from %d to %d",
842                          p_sys->i_cols, newval.i_int );
843         p_sys->i_cols = __MAX( newval.i_int, 1 );
844         vlc_mutex_unlock( &p_sys->lock );
845     }
846     else if( VAR_IS( "order" ) )
847     {
848         char *psz_order;
849         int i_index;
850         vlc_mutex_lock( &p_sys->lock );
851         msg_Dbg( p_this, "Changing mosaic order to %s", newval.psz_string );
852
853         psz_order = newval.psz_string;
854
855         while( p_sys->i_order_length-- )
856         {
857             free( p_sys->ppsz_order[p_sys->i_order_length] );
858         }
859         free( p_sys->ppsz_order );
860         p_sys->ppsz_order = NULL;
861
862         if( *psz_order )
863         {
864             char *psz_end = NULL;
865             i_index = 0;
866             do
867             {
868                 psz_end = strchr( psz_order, ',' );
869                 i_index++;
870                 p_sys->ppsz_order = realloc( p_sys->ppsz_order,
871                                     i_index * sizeof(char *) );
872                 p_sys->ppsz_order[i_index - 1] = strndup( psz_order,
873                                            psz_end - psz_order );
874                 psz_order = psz_end+1;
875             } while( psz_end );
876             p_sys->i_order_length = i_index;
877         }
878
879         vlc_mutex_unlock( &p_sys->lock );
880     }
881     else if( VAR_IS( "offsets" ) )
882     {
883         vlc_mutex_lock( &p_sys->lock );
884         msg_Info( p_this, "Changing mosaic-offsets to %s", newval.psz_string );
885
886         if( p_sys->i_offsets_length != 0 )
887         {
888             p_sys->i_offsets_length = 0;
889             free( p_sys->pi_x_offsets );
890             free( p_sys->pi_y_offsets );
891             p_sys->pi_x_offsets = NULL;
892             p_sys->pi_y_offsets = NULL;
893         }
894
895         mosaic_ParseSetOffsets( p_this, p_sys, newval.psz_string );
896
897         vlc_mutex_unlock( &p_sys->lock );
898     }
899     else if( VAR_IS( "keep-aspect-ratio" ) )
900     {
901         vlc_mutex_lock( &p_sys->lock );
902         if( newval.i_int )
903         {
904             msg_Dbg( p_this, "keeping aspect ratio" );
905             p_sys->b_ar = 1;
906         }
907         else
908         {
909             msg_Dbg( p_this, "won't keep aspect ratio" );
910             p_sys->b_ar = 0;
911         }
912         vlc_mutex_unlock( &p_sys->lock );
913     }
914     else if( VAR_IS( "keep-picture" ) )
915     {
916         vlc_mutex_lock( &p_sys->lock );
917         p_sys->b_keep = newval.b_bool;
918         if ( !p_sys->b_keep && !p_sys->p_image )
919         {
920             p_sys->p_image = image_HandlerCreate( p_this );
921         }
922         vlc_mutex_unlock( &p_sys->lock );
923     }
924
925     return VLC_SUCCESS;
926 }