1 /*****************************************************************************
2  * mosaic.c : Mosaic video plugin for vlc
3  *****************************************************************************
4  * Copyright (C) 2004-2008 VLC authors and VideoLAN
5  * $Id: b76166414a0d0b9b8edeccb28ef44c0695a9a8b5 $
6  *
7  * Authors: Antoine Cellerier <dionoea at videolan dot org>
8  *          Christophe Massiot <massiot@via.ecp.fr>
9  *
10  * This program is free software; you can redistribute it and/or modify it
11  * under the terms of the GNU Lesser General Public License as published by
12  * the Free Software Foundation; either version 2.1 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 Lesser General Public License for more details.
19  *
20  * You should have received a copy of the GNU Lesser General Public License
21  * along with this program; if not, write to the Free Software Foundation,
22  * Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
23 *****************************************************************************/
24 
25 /*****************************************************************************
26  * Preamble
27  *****************************************************************************/
28 #ifdef HAVE_CONFIG_H
29 # include "config.h"
30 #endif
31 
32 #include <math.h>
33 #include <limits.h> /* INT_MAX */
34 
35 #include <vlc_common.h>
36 #include <vlc_plugin.h>
37 #include <vlc_filter.h>
38 #include <vlc_image.h>
39 #include <vlc_subpicture.h>
40 
41 #include "mosaic.h"
42 
43 #define BLANK_DELAY INT64_C(1000000)
44 
45 /*****************************************************************************
46  * Local prototypes
47  *****************************************************************************/
48 static int  CreateFilter    ( vlc_object_t * );
49 static void DestroyFilter   ( vlc_object_t * );
50 static subpicture_t *Filter ( filter_t *, mtime_t );
51 
52 static int MosaicCallback   ( vlc_object_t *, char const *, vlc_value_t,
53                               vlc_value_t, void * );
54 
55 /*****************************************************************************
56  * filter_sys_t : filter descriptor
57  *****************************************************************************/
58 struct filter_sys_t
59 {
60     vlc_mutex_t lock;         /* Internal filter lock */
61 
62     image_handler_t *p_image;
63 
64     int i_position;           /* Mosaic positioning method */
65     bool b_ar;          /* Do we keep the aspect ratio ? */
66     bool 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         "positioning 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         "positioning 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 const int pi_pos_values[] = { 0, 1, 2 };
165 static const char *const ppsz_pos_descriptions[] =
166     { N_("auto"), N_("fixed"), N_("offsets") };
167 
168 static const int pi_align_values[] = { 0, 1, 2, 4, 8, 5, 6, 9, 10 };
169 static const char *const 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( N_("Mosaic video sub source") )
177     set_shortname( N_("Mosaic") )
178     set_category( CAT_VIDEO )
179     set_subcategory( SUBCAT_VIDEO_SUBPIC)
180     set_capability( "sub source", 0 )
181     set_callbacks( CreateFilter, DestroyFilter )
182 
183     add_integer_with_range( CFG_PREFIX "alpha", 255, 0, 255,
184                             ALPHA_TEXT, ALPHA_LONGTEXT, false )
185 
186     add_integer( CFG_PREFIX "height", 100,
187                  HEIGHT_TEXT, HEIGHT_LONGTEXT, false )
188     add_integer( CFG_PREFIX "width", 100,
189                  WIDTH_TEXT, WIDTH_LONGTEXT, false )
190 
191     add_integer( CFG_PREFIX "align", 5,
192                  ALIGN_TEXT, ALIGN_LONGTEXT, true)
193         change_integer_list( pi_align_values, ppsz_align_descriptions )
194 
195     add_integer( CFG_PREFIX "xoffset", 0,
196                  XOFFSET_TEXT, XOFFSET_LONGTEXT, true )
197     add_integer( CFG_PREFIX "yoffset", 0,
198                  YOFFSET_TEXT, YOFFSET_LONGTEXT, true )
199 
200     add_integer( CFG_PREFIX "borderw", 0,
201                  BORDERW_TEXT, BORDERW_LONGTEXT, true )
202     add_integer( CFG_PREFIX "borderh", 0,
203                  BORDERH_TEXT, BORDERH_LONGTEXT, true )
204 
205     add_integer( CFG_PREFIX "position", 0,
206                  POS_TEXT, POS_LONGTEXT, false )
207         change_integer_list( pi_pos_values, ppsz_pos_descriptions )
208     add_integer( CFG_PREFIX "rows", 2,
209                  ROWS_TEXT, ROWS_LONGTEXT, false )
210     add_integer( CFG_PREFIX "cols", 2,
211                  COLS_TEXT, COLS_LONGTEXT, false )
212 
213     add_bool( CFG_PREFIX "keep-aspect-ratio", false,
214               AR_TEXT, AR_LONGTEXT, false )
215     add_bool( CFG_PREFIX "keep-picture", false,
216               KEEP_TEXT, KEEP_LONGTEXT, false )
217 
218     add_string( CFG_PREFIX "order", "",
219                 ORDER_TEXT, ORDER_LONGTEXT, false )
220 
221     add_string( CFG_PREFIX "offsets", "",
222                 OFFSETS_TEXT, OFFSETS_LONGTEXT, false )
223 
224     add_integer( CFG_PREFIX "delay", 0, DELAY_TEXT, DELAY_LONGTEXT,
225                  false )
226 vlc_module_end ()
227 
228 static const char *const ppsz_filter_options[] = {
229     "alpha", "height", "width", "align", "xoffset", "yoffset",
230     "borderw", "borderh", "position", "rows", "cols",
231     "keep-aspect-ratio", "keep-picture", "order", "offsets",
232     "delay", NULL
233 };
234 
235 /*****************************************************************************
236  * mosaic_ParseSetOffsets:
237  * parse the "--mosaic-offsets x1,y1,x2,y2,x3,y3" parameter
238  * and set the corresponding struct filter_sys_t entries.
239  *****************************************************************************/
mosaic_ParseSetOffsets(vlc_object_t * p_this,filter_sys_t * p_sys,char * psz_offsets)240 static void mosaic_ParseSetOffsets( vlc_object_t *p_this,
241                                       filter_sys_t *p_sys,
242                                       char *psz_offsets )
243 {
244     if( *psz_offsets )
245     {
246         char *psz_end = NULL;
247         int i_index = 0;
248         do
249         {
250             i_index++;
251 
252             p_sys->pi_x_offsets = xrealloc( p_sys->pi_x_offsets,
253                                                    i_index * sizeof(int) );
254             p_sys->pi_x_offsets[i_index - 1] = atoi( psz_offsets );
255             psz_end = strchr( psz_offsets, ',' );
256             psz_offsets = psz_end + 1;
257 
258             p_sys->pi_y_offsets = xrealloc( p_sys->pi_y_offsets,
259                                                    i_index * sizeof(int) );
260             p_sys->pi_y_offsets[i_index - 1] = atoi( psz_offsets );
261             psz_end = strchr( psz_offsets, ',' );
262             psz_offsets = psz_end + 1;
263 
264             msg_Dbg( p_this, CFG_PREFIX "offset: id %d, x=%d, y=%d",
265                      i_index, p_sys->pi_x_offsets[i_index - 1],
266                               p_sys->pi_y_offsets[i_index - 1]  );
267 
268         } while( psz_end );
269         p_sys->i_offsets_length = i_index;
270     }
271 }
272 #define mosaic_ParseSetOffsets( a, b, c ) \
273             mosaic_ParseSetOffsets( VLC_OBJECT( a ), b, c )
274 
275 /*****************************************************************************
276  * CreateFiler: allocate mosaic video filter
277  *****************************************************************************/
CreateFilter(vlc_object_t * p_this)278 static int CreateFilter( vlc_object_t *p_this )
279 {
280     filter_t *p_filter = (filter_t *)p_this;
281     filter_sys_t *p_sys;
282     char *psz_order, *_psz_order;
283     char *psz_offsets;
284     int i_index;
285     int i_command;
286 
287     /* Allocate structure */
288     p_sys = p_filter->p_sys = malloc( sizeof( filter_sys_t ) );
289     if( p_sys == NULL )
290         return VLC_ENOMEM;
291 
292     p_filter->pf_sub_source = Filter;
293 
294     vlc_mutex_init( &p_sys->lock );
295     vlc_mutex_lock( &p_sys->lock );
296 
297     config_ChainParse( p_filter, CFG_PREFIX, ppsz_filter_options,
298                        p_filter->p_cfg );
299 
300 #define GET_VAR( name, min, max )                                           \
301     i_command = var_CreateGetIntegerCommand( p_filter, CFG_PREFIX #name );  \
302     p_sys->i_##name = VLC_CLIP( i_command, min, max );                \
303     var_AddCallback( p_filter, CFG_PREFIX #name, MosaicCallback, p_sys );
304 
305     GET_VAR( width, 0, INT_MAX );
306     GET_VAR( height, 0, INT_MAX );
307     GET_VAR( xoffset, 0, INT_MAX );
308     GET_VAR( yoffset, 0, INT_MAX );
309 
310     GET_VAR( align, 0, 10 );
311     if( p_sys->i_align == 3 || p_sys->i_align == 7 )
312         p_sys->i_align = 5;
313 
314     GET_VAR( borderw, 0, INT_MAX );
315     GET_VAR( borderh, 0, INT_MAX );
316     GET_VAR( rows, 1, INT_MAX );
317     GET_VAR( cols, 1, INT_MAX );
318     GET_VAR( alpha, 0, 255 );
319     GET_VAR( position, 0, 2 );
320     GET_VAR( delay, 100, INT_MAX );
321 #undef GET_VAR
322     p_sys->i_delay *= 1000;
323 
324     p_sys->b_ar = var_CreateGetBoolCommand( p_filter,
325                                             CFG_PREFIX "keep-aspect-ratio" );
326     var_AddCallback( p_filter, CFG_PREFIX "keep-aspect-ratio", MosaicCallback,
327                      p_sys );
328 
329     p_sys->b_keep = var_CreateGetBoolCommand( p_filter,
330                                               CFG_PREFIX "keep-picture" );
331     if ( !p_sys->b_keep )
332     {
333         p_sys->p_image = image_HandlerCreate( p_filter );
334     }
335 
336     p_sys->i_order_length = 0;
337     p_sys->ppsz_order = NULL;
338     psz_order = var_CreateGetStringCommand( p_filter, CFG_PREFIX "order" );
339     _psz_order = psz_order;
340     var_AddCallback( p_filter, CFG_PREFIX "order", MosaicCallback, p_sys );
341 
342     if( *psz_order )
343     {
344         char *psz_end = NULL;
345         i_index = 0;
346         do
347         {
348             psz_end = strchr( psz_order, ',' );
349             i_index++;
350             p_sys->ppsz_order = xrealloc( p_sys->ppsz_order,
351                                                  i_index * sizeof(char *) );
352             p_sys->ppsz_order[i_index - 1] = strndup( psz_order,
353                                            psz_end - psz_order );
354             psz_order = psz_end+1;
355         } while( psz_end );
356         p_sys->i_order_length = i_index;
357     }
358 
359     free( _psz_order );
360 
361     /* Manage specific offsets for substreams */
362     psz_offsets = var_CreateGetStringCommand( p_filter, CFG_PREFIX "offsets" );
363     p_sys->i_offsets_length = 0;
364     p_sys->pi_x_offsets = NULL;
365     p_sys->pi_y_offsets = NULL;
366     mosaic_ParseSetOffsets( p_filter, p_sys, psz_offsets );
367     free( psz_offsets );
368     var_AddCallback( p_filter, CFG_PREFIX "offsets", MosaicCallback, p_sys );
369 
370     vlc_mutex_unlock( &p_sys->lock );
371 
372     return VLC_SUCCESS;
373 }
374 
375 /*****************************************************************************
376  * DestroyFilter: destroy mosaic video filter
377  *****************************************************************************/
DestroyFilter(vlc_object_t * p_this)378 static void DestroyFilter( vlc_object_t *p_this )
379 {
380     filter_t *p_filter = (filter_t*)p_this;
381     filter_sys_t *p_sys = p_filter->p_sys;
382 
383 #define DEL_CB( name ) \
384     var_DelCallback( p_filter, CFG_PREFIX #name, MosaicCallback, p_sys )
385     DEL_CB( width );
386     DEL_CB( height );
387     DEL_CB( xoffset );
388     DEL_CB( yoffset );
389 
390     DEL_CB( align );
391 
392     DEL_CB( borderw );
393     DEL_CB( borderh );
394     DEL_CB( rows );
395     DEL_CB( cols );
396     DEL_CB( alpha );
397     DEL_CB( position );
398     DEL_CB( delay );
399 
400     DEL_CB( keep-aspect-ratio );
401     DEL_CB( order );
402 #undef DEL_CB
403 
404     if( !p_sys->b_keep )
405     {
406         image_HandlerDelete( p_sys->p_image );
407     }
408 
409     if( p_sys->i_order_length )
410     {
411         for( int i_index = 0; i_index < p_sys->i_order_length; i_index++ )
412         {
413             free( p_sys->ppsz_order[i_index] );
414         }
415         free( p_sys->ppsz_order );
416     }
417     if( p_sys->i_offsets_length )
418     {
419         free( p_sys->pi_x_offsets );
420         free( p_sys->pi_y_offsets );
421         p_sys->i_offsets_length = 0;
422     }
423 
424     vlc_mutex_destroy( &p_sys->lock );
425     free( p_sys );
426 }
427 
428 /*****************************************************************************
429  * Filter
430  *****************************************************************************/
Filter(filter_t * p_filter,mtime_t date)431 static subpicture_t *Filter( filter_t *p_filter, mtime_t date )
432 {
433     filter_sys_t *p_sys = p_filter->p_sys;
434     bridge_t *p_bridge;
435 
436     int i_real_index, i_row, i_col;
437     int i_greatest_real_index_used = p_sys->i_order_length - 1;
438 
439     unsigned int col_inner_width, row_inner_height;
440 
441     subpicture_region_t *p_region;
442     subpicture_region_t *p_region_prev = NULL;
443 
444     /* Allocate the subpicture internal data. */
445     subpicture_t *p_spu = filter_NewSubpicture( p_filter );
446     if( !p_spu )
447         return NULL;
448 
449     /* Initialize subpicture */
450     p_spu->i_channel = 0;
451     p_spu->i_start  = date;
452     p_spu->i_stop = 0;
453     p_spu->b_ephemer = true;
454     p_spu->i_alpha = p_sys->i_alpha;
455     p_spu->b_absolute = false;
456 
457     p_spu->i_original_picture_width = p_sys->i_width;
458     p_spu->i_original_picture_height = p_sys->i_height;
459 
460     vlc_mutex_lock( &p_sys->lock );
461     vlc_global_lock( VLC_MOSAIC_MUTEX );
462 
463     p_bridge = GetBridge( p_filter );
464     if ( p_bridge == NULL )
465     {
466         vlc_global_unlock( VLC_MOSAIC_MUTEX );
467         vlc_mutex_unlock( &p_sys->lock );
468         return p_spu;
469     }
470 
471     if ( p_sys->i_position == position_offsets )
472     {
473         /* If we have either too much or not enough offsets, fall-back
474          * to automatic positioning. */
475         if ( p_sys->i_offsets_length != p_sys->i_order_length )
476         {
477             msg_Err( p_filter,
478                      "Number of specified offsets (%d) does not match number "
479                      "of input substreams in mosaic-order (%d), falling back "
480                      "to mosaic-position=0",
481                      p_sys->i_offsets_length, p_sys->i_order_length );
482             p_sys->i_position = position_auto;
483         }
484     }
485 
486     if ( p_sys->i_position == position_auto )
487     {
488         int i_numpics = p_sys->i_order_length; /* keep slots and all */
489         for( int i_index = 0; i_index < p_bridge->i_es_num; i_index++ )
490         {
491             bridged_es_t *p_es = p_bridge->pp_es[i_index];
492             if ( !p_es->b_empty )
493             {
494                 i_numpics ++;
495                 if( p_sys->i_order_length && p_es->psz_id != NULL )
496                 {
497                     /* We also want to leave slots for images given in
498                      * mosaic-order that are not available in p_vout_picture */
499                     for( int i = 0; i < p_sys->i_order_length ; i++ )
500                     {
501                         if( !strcmp( p_sys->ppsz_order[i], p_es->psz_id ) )
502                         {
503                             i_numpics--;
504                             break;
505                         }
506                     }
507 
508                 }
509             }
510         }
511         p_sys->i_rows = ceil(sqrt( (double)i_numpics ));
512         p_sys->i_cols = ( i_numpics % p_sys->i_rows == 0 ?
513                             i_numpics / p_sys->i_rows :
514                             i_numpics / p_sys->i_rows + 1 );
515     }
516 
517     col_inner_width  = ( ( p_sys->i_width - ( p_sys->i_cols - 1 )
518                        * p_sys->i_borderw ) / p_sys->i_cols );
519     row_inner_height = ( ( p_sys->i_height - ( p_sys->i_rows - 1 )
520                        * p_sys->i_borderh ) / p_sys->i_rows );
521 
522     i_real_index = 0;
523 
524     for( int i_index = 0; i_index < p_bridge->i_es_num; i_index++ )
525     {
526         bridged_es_t *p_es = p_bridge->pp_es[i_index];
527         video_format_t fmt_in, fmt_out;
528         picture_t *p_converted;
529 
530         if ( p_es->b_empty )
531             continue;
532 
533         while ( p_es->p_picture != NULL
534                  && p_es->p_picture->date + p_sys->i_delay < date )
535         {
536             if ( p_es->p_picture->p_next != NULL )
537             {
538                 picture_t *p_next = p_es->p_picture->p_next;
539                 picture_Release( p_es->p_picture );
540                 p_es->p_picture = p_next;
541             }
542             else if ( p_es->p_picture->date + p_sys->i_delay + BLANK_DELAY <
543                         date )
544             {
545                 /* Display blank */
546                 picture_Release( p_es->p_picture );
547                 p_es->p_picture = NULL;
548                 p_es->pp_last = &p_es->p_picture;
549                 break;
550             }
551             else
552             {
553                 msg_Dbg( p_filter, "too late picture for %s (%"PRId64 ")",
554                          p_es->psz_id,
555                          date - p_es->p_picture->date - p_sys->i_delay );
556                 break;
557             }
558         }
559 
560         if ( p_es->p_picture == NULL )
561             continue;
562 
563         if ( p_sys->i_order_length == 0 )
564         {
565             i_real_index++;
566         }
567         else
568         {
569             int i;
570             for ( i = 0; i <= p_sys->i_order_length; i++ )
571             {
572                 if ( i == p_sys->i_order_length ) break;
573                 if ( strcmp( p_es->psz_id, p_sys->ppsz_order[i] ) == 0 )
574                 {
575                     i_real_index = i;
576                     break;
577                 }
578             }
579             if ( i == p_sys->i_order_length )
580                 i_real_index = ++i_greatest_real_index_used;
581         }
582         i_row = ( i_real_index / p_sys->i_cols ) % p_sys->i_rows;
583         i_col = i_real_index % p_sys->i_cols ;
584 
585         video_format_Init( &fmt_in, 0 );
586         video_format_Init( &fmt_out, 0 );
587 
588         if ( !p_sys->b_keep )
589         {
590             /* Convert the images */
591             fmt_in.i_chroma = p_es->p_picture->format.i_chroma;
592             fmt_in.i_height = p_es->p_picture->format.i_height;
593             fmt_in.i_width = p_es->p_picture->format.i_width;
594 
595             if( fmt_in.i_chroma == VLC_CODEC_YUVA ||
596                 fmt_in.i_chroma == VLC_CODEC_RGBA )
597                 fmt_out.i_chroma = VLC_CODEC_YUVA;
598             else
599                 fmt_out.i_chroma = VLC_CODEC_I420;
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                 video_format_Clean( &fmt_in );
628                 video_format_Clean( &fmt_out );
629                 continue;
630             }
631         }
632         else
633         {
634             p_converted = p_es->p_picture;
635             fmt_in.i_width = fmt_out.i_width = p_converted->format.i_width;
636             fmt_in.i_height = fmt_out.i_height = p_converted->format.i_height;
637             fmt_in.i_chroma = fmt_out.i_chroma = p_converted->format.i_chroma;
638             fmt_out.i_visible_width = fmt_out.i_width;
639             fmt_out.i_visible_height = fmt_out.i_height;
640         }
641 
642         p_region = subpicture_region_New( &fmt_out );
643         /* FIXME the copy is probably not needed anymore */
644         if( p_region )
645             picture_Copy( p_region->p_picture, p_converted );
646         if( !p_sys->b_keep )
647             picture_Release( p_converted );
648 
649         if( !p_region )
650         {
651             video_format_Clean( &fmt_in );
652             video_format_Clean( &fmt_out );
653             msg_Err( p_filter, "cannot allocate SPU region" );
654             subpicture_Delete( p_spu );
655             vlc_global_unlock( VLC_MOSAIC_MUTEX );
656             vlc_mutex_unlock( &p_sys->lock );
657             return NULL;
658         }
659 
660         if( p_es->i_x >= 0 && p_es->i_y >= 0 )
661         {
662             p_region->i_x = p_es->i_x;
663             p_region->i_y = p_es->i_y;
664         }
665         else if( p_sys->i_position == position_offsets )
666         {
667             p_region->i_x = p_sys->pi_x_offsets[i_real_index];
668             p_region->i_y = p_sys->pi_y_offsets[i_real_index];
669         }
670         else
671         {
672             if( fmt_out.i_width > col_inner_width ||
673                 p_sys->b_ar || p_sys->b_keep )
674             {
675                 /* we don't have to center the video since it takes the
676                 whole rectangle area or it's larger than the rectangle */
677                 p_region->i_x = p_sys->i_xoffset
678                             + i_col * ( p_sys->i_width / p_sys->i_cols )
679                             + ( i_col * p_sys->i_borderw ) / p_sys->i_cols;
680             }
681             else
682             {
683                 /* center the video in the dedicated rectangle */
684                 p_region->i_x = p_sys->i_xoffset
685                         + i_col * ( p_sys->i_width / p_sys->i_cols )
686                         + ( i_col * p_sys->i_borderw ) / p_sys->i_cols
687                         + ( col_inner_width - fmt_out.i_width ) / 2;
688             }
689 
690             if( fmt_out.i_height > row_inner_height
691                 || p_sys->b_ar || p_sys->b_keep )
692             {
693                 /* we don't have to center the video since it takes the
694                 whole rectangle area or it's taller than the rectangle */
695                 p_region->i_y = p_sys->i_yoffset
696                         + i_row * ( p_sys->i_height / p_sys->i_rows )
697                         + ( i_row * p_sys->i_borderh ) / p_sys->i_rows;
698             }
699             else
700             {
701                 /* center the video in the dedicated rectangle */
702                 p_region->i_y = p_sys->i_yoffset
703                         + i_row * ( p_sys->i_height / p_sys->i_rows )
704                         + ( i_row * p_sys->i_borderh ) / p_sys->i_rows
705                         + ( row_inner_height - fmt_out.i_height ) / 2;
706             }
707         }
708         p_region->i_align = p_sys->i_align;
709         p_region->i_alpha = p_es->i_alpha;
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         video_format_Clean( &fmt_in );
721         video_format_Clean( &fmt_out );
722 
723         p_region_prev = p_region;
724     }
725 
726     vlc_global_unlock( VLC_MOSAIC_MUTEX );
727     vlc_mutex_unlock( &p_sys->lock );
728 
729     return p_spu;
730 }
731 
732 /*****************************************************************************
733 * Callback to update params on the fly
734 *****************************************************************************/
MosaicCallback(vlc_object_t * p_this,char const * psz_var,vlc_value_t oldval,vlc_value_t newval,void * p_data)735 static int MosaicCallback( vlc_object_t *p_this, char const *psz_var,
736                             vlc_value_t oldval, vlc_value_t newval,
737                             void *p_data )
738 {
739     VLC_UNUSED(oldval);
740     filter_sys_t *p_sys = (filter_sys_t *) p_data;
741 
742 #define VAR_IS( a ) !strcmp( psz_var, CFG_PREFIX a )
743     if( VAR_IS( "alpha" ) )
744     {
745         vlc_mutex_lock( &p_sys->lock );
746         msg_Dbg( p_this, "changing alpha from %d/255 to %d/255",
747                          p_sys->i_alpha, (int)newval.i_int);
748         p_sys->i_alpha = VLC_CLIP( newval.i_int, 0, 255 );
749         vlc_mutex_unlock( &p_sys->lock );
750     }
751     else if( VAR_IS( "height" ) )
752     {
753         vlc_mutex_lock( &p_sys->lock );
754         msg_Dbg( p_this, "changing height from %dpx to %dpx",
755                           p_sys->i_height, (int)newval.i_int );
756         p_sys->i_height = __MAX( newval.i_int, 0 );
757         vlc_mutex_unlock( &p_sys->lock );
758     }
759     else if( VAR_IS( "width" ) )
760     {
761         vlc_mutex_lock( &p_sys->lock );
762         msg_Dbg( p_this, "changing width from %dpx to %dpx",
763                          p_sys->i_width, (int)newval.i_int );
764         p_sys->i_width = __MAX( newval.i_int, 0 );
765         vlc_mutex_unlock( &p_sys->lock );
766     }
767     else if( VAR_IS( "xoffset" ) )
768     {
769         vlc_mutex_lock( &p_sys->lock );
770         msg_Dbg( p_this, "changing x offset from %dpx to %dpx",
771                          p_sys->i_xoffset, (int)newval.i_int );
772         p_sys->i_xoffset = __MAX( newval.i_int, 0 );
773         vlc_mutex_unlock( &p_sys->lock );
774     }
775     else if( VAR_IS( "yoffset" ) )
776     {
777         vlc_mutex_lock( &p_sys->lock );
778         msg_Dbg( p_this, "changing y offset from %dpx to %dpx",
779                          p_sys->i_yoffset, (int)newval.i_int );
780         p_sys->i_yoffset = __MAX( newval.i_int, 0 );
781         vlc_mutex_unlock( &p_sys->lock );
782     }
783     else if( VAR_IS( "align" ) )
784     {
785         int i_old = 0, i_new = 0;
786         vlc_mutex_lock( &p_sys->lock );
787         newval.i_int = VLC_CLIP( newval.i_int, 0, 10 );
788         if( newval.i_int == 3 || newval.i_int == 7 )
789             newval.i_int = 5;
790         while( pi_align_values[i_old] != p_sys->i_align ) i_old++;
791         while( pi_align_values[i_new] != newval.i_int ) i_new++;
792         msg_Dbg( p_this, "changing alignment from %d (%s) to %d (%s)",
793                      p_sys->i_align, ppsz_align_descriptions[i_old],
794                      (int)newval.i_int, ppsz_align_descriptions[i_new] );
795         p_sys->i_align = newval.i_int;
796         vlc_mutex_unlock( &p_sys->lock );
797     }
798     else if( VAR_IS( "borderw" ) )
799     {
800         vlc_mutex_lock( &p_sys->lock );
801         msg_Dbg( p_this, "changing border width from %dpx to %dpx",
802                          p_sys->i_borderw, (int)newval.i_int );
803         p_sys->i_borderw = __MAX( newval.i_int, 0 );
804         vlc_mutex_unlock( &p_sys->lock );
805     }
806     else if( VAR_IS( "borderh" ) )
807     {
808         vlc_mutex_lock( &p_sys->lock );
809         msg_Dbg( p_this, "changing border height from %dpx to %dpx",
810                          p_sys->i_borderh, (int)newval.i_int );
811         p_sys->i_borderh = __MAX( newval.i_int, 0 );
812         vlc_mutex_unlock( &p_sys->lock );
813     }
814     else if( VAR_IS( "position" ) )
815     {
816         if( newval.i_int > 2 || newval.i_int < 0 )
817         {
818             msg_Err( p_this,
819                      "Position is either 0 (%s), 1 (%s) or 2 (%s)",
820                      ppsz_pos_descriptions[0],
821                      ppsz_pos_descriptions[1],
822                      ppsz_pos_descriptions[2] );
823         }
824         else
825         {
826             vlc_mutex_lock( &p_sys->lock );
827             msg_Dbg( p_this, "changing position method from %d (%s) to %d (%s)",
828                     p_sys->i_position, ppsz_pos_descriptions[p_sys->i_position],
829                      (int)newval.i_int, ppsz_pos_descriptions[newval.i_int]);
830             p_sys->i_position = newval.i_int;
831             vlc_mutex_unlock( &p_sys->lock );
832         }
833     }
834     else if( VAR_IS( "rows" ) )
835     {
836         vlc_mutex_lock( &p_sys->lock );
837         msg_Dbg( p_this, "changing number of rows from %d to %d",
838                          p_sys->i_rows, (int)newval.i_int );
839         p_sys->i_rows = __MAX( newval.i_int, 1 );
840         vlc_mutex_unlock( &p_sys->lock );
841     }
842     else if( VAR_IS( "cols" ) )
843     {
844         vlc_mutex_lock( &p_sys->lock );
845         msg_Dbg( p_this, "changing number of columns from %d to %d",
846                          p_sys->i_cols, (int)newval.i_int );
847         p_sys->i_cols = __MAX( newval.i_int, 1 );
848         vlc_mutex_unlock( &p_sys->lock );
849     }
850     else if( VAR_IS( "order" ) )
851     {
852         char *psz_order;
853         int i_index;
854         vlc_mutex_lock( &p_sys->lock );
855         msg_Dbg( p_this, "Changing mosaic order to %s", newval.psz_string );
856 
857         psz_order = newval.psz_string;
858 
859         while( p_sys->i_order_length-- )
860         {
861             free( p_sys->ppsz_order[p_sys->i_order_length] );
862         }
863         free( p_sys->ppsz_order );
864         p_sys->ppsz_order = NULL;
865 
866         if( *psz_order )
867         {
868             char *psz_end = NULL;
869             i_index = 0;
870             do
871             {
872                 psz_end = strchr( psz_order, ',' );
873                 i_index++;
874                 p_sys->ppsz_order = xrealloc( p_sys->ppsz_order,
875                                                    i_index * sizeof(char *) );
876                 p_sys->ppsz_order[i_index - 1] = strndup( psz_order,
877                                            psz_end - psz_order );
878                 psz_order = psz_end+1;
879             } while( psz_end );
880             p_sys->i_order_length = i_index;
881         }
882 
883         vlc_mutex_unlock( &p_sys->lock );
884     }
885     else if( VAR_IS( "offsets" ) )
886     {
887         vlc_mutex_lock( &p_sys->lock );
888         msg_Info( p_this, "Changing mosaic-offsets to %s", newval.psz_string );
889 
890         if( p_sys->i_offsets_length != 0 )
891         {
892             p_sys->i_offsets_length = 0;
893             free( p_sys->pi_x_offsets );
894             free( p_sys->pi_y_offsets );
895             p_sys->pi_x_offsets = NULL;
896             p_sys->pi_y_offsets = NULL;
897         }
898 
899         mosaic_ParseSetOffsets( p_this, p_sys, newval.psz_string );
900 
901         vlc_mutex_unlock( &p_sys->lock );
902     }
903     else if( VAR_IS( "keep-aspect-ratio" ) )
904     {
905         vlc_mutex_lock( &p_sys->lock );
906         if( newval.i_int )
907         {
908             msg_Dbg( p_this, "keeping aspect ratio" );
909             p_sys->b_ar = 1;
910         }
911         else
912         {
913             msg_Dbg( p_this, "won't keep aspect ratio" );
914             p_sys->b_ar = 0;
915         }
916         vlc_mutex_unlock( &p_sys->lock );
917     }
918     else if( VAR_IS( "keep-picture" ) )
919     {
920         vlc_mutex_lock( &p_sys->lock );
921         p_sys->b_keep = newval.b_bool;
922         if ( !p_sys->b_keep && !p_sys->p_image )
923         {
924             p_sys->p_image = image_HandlerCreate( p_this );
925         }
926         vlc_mutex_unlock( &p_sys->lock );
927     }
928 
929     return VLC_SUCCESS;
930 }
931