]> git.sesse.net Git - vlc/blob - modules/video_filter/mosaic.c
438125e6918af88cfa96ec81868248f631072a68
[vlc] / modules / video_filter / mosaic.c
1 /*****************************************************************************
2  * mosaic.c : Mosaic video plugin for vlc
3  *****************************************************************************
4  * Copyright (C) 2004-2008 the VideoLAN team
5  * $Id$
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
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 #ifdef HAVE_CONFIG_H
29 # include "config.h"
30 #endif
31
32 #include <vlc_common.h>
33 #include <vlc_plugin.h>
34 #include <vlc_vout.h>
35
36 #include <math.h>
37 #include <limits.h> /* INT_MAX */
38
39 #include "vlc_filter.h"
40 #include "vlc_image.h"
41
42 #include "mosaic.h"
43
44 #define BLANK_DELAY INT64_C(1000000)
45
46 /*****************************************************************************
47  * Local prototypes
48  *****************************************************************************/
49 static int  CreateFilter    ( vlc_object_t * );
50 static void DestroyFilter   ( vlc_object_t * );
51 static subpicture_t *Filter ( filter_t *, mtime_t );
52
53 static int MosaicCallback   ( vlc_object_t *, char const *, vlc_value_t,
54                               vlc_value_t, void * );
55
56 /*****************************************************************************
57  * filter_sys_t : filter descriptor
58  *****************************************************************************/
59 struct filter_sys_t
60 {
61     vlc_mutex_t lock;         /* Internal filter lock */
62     vlc_mutex_t *p_lock;      /* Pointer to mosaic bridge lock */
63
64     image_handler_t *p_image;
65
66     int i_position;           /* Mosaic positioning method */
67     bool b_ar;          /* Do we keep the aspect ratio ? */
68     bool b_keep;        /* Do we keep the original picture format ? */
69     int i_width, i_height;    /* Mosaic height and width */
70     int i_cols, i_rows;       /* Mosaic rows and cols */
71     int i_align;              /* Mosaic alignment in background video */
72     int i_xoffset, i_yoffset; /* Top left corner offset */
73     int i_borderw, i_borderh; /* Border width/height between miniatures */
74     int i_alpha;              /* Subfilter alpha blending */
75
76     char **ppsz_order;        /* List of picture-ids */
77     int i_order_length;
78
79     int *pi_x_offsets;        /* List of substreams x offsets */
80     int *pi_y_offsets;        /* List of substreams y offsets */
81     int i_offsets_length;
82
83     mtime_t i_delay;
84 };
85
86 /*****************************************************************************
87  * Module descriptor
88  *****************************************************************************/
89 #define ALPHA_TEXT N_("Transparency")
90 #define ALPHA_LONGTEXT N_( \
91         "Transparency of the mosaic foreground pictures. " \
92         "0 means transparent, 255 opaque (default)." )
93
94 #define HEIGHT_TEXT N_("Height")
95 #define HEIGHT_LONGTEXT N_( "Total height of the mosaic, in pixels." )
96 #define WIDTH_TEXT N_("Width")
97 #define WIDTH_LONGTEXT N_( "Total width of the mosaic, in pixels." )
98
99 #define XOFFSET_TEXT N_("Top left corner X coordinate")
100 #define XOFFSET_LONGTEXT N_( \
101         "X Coordinate of the top-left corner of the mosaic.")
102 #define YOFFSET_TEXT N_("Top left corner Y coordinate")
103 #define YOFFSET_LONGTEXT N_( \
104         "Y Coordinate of the top-left corner of the mosaic.")
105
106 #define BORDERW_TEXT N_("Border width")
107 #define BORDERW_LONGTEXT N_( \
108         "Width in pixels of the border between miniatures." )
109 #define BORDERH_TEXT N_("Border height")
110 #define BORDERH_LONGTEXT N_( \
111         "Height in pixels of the border between miniatures." )
112
113 #define ALIGN_TEXT N_("Mosaic alignment" )
114 #define ALIGN_LONGTEXT N_( \
115         "You can enforce the mosaic alignment on the video " \
116         "(0=center, 1=left, 2=right, 4=top, 8=bottom, you can " \
117         "also use combinations of these values, eg 6 = top-right).")
118
119 #define POS_TEXT N_("Positioning method")
120 #define POS_LONGTEXT N_( \
121         "Positioning method for the mosaic. auto: " \
122         "automatically choose the best number of rows and columns. " \
123         "fixed: use the user-defined number of rows and columns. " \
124         "offsets: use the user-defined offsets for each image." )
125
126 #define ROWS_TEXT N_("Number of rows")
127 #define ROWS_LONGTEXT N_( \
128         "Number of image rows in the mosaic (only used if " \
129         "positionning method is set to \"fixed\")." )
130
131 #define COLS_TEXT N_("Number of columns")
132 #define COLS_LONGTEXT N_( \
133         "Number of image columns in the mosaic (only used if " \
134         "positionning method is set to \"fixed\"." )
135
136 #define AR_TEXT N_("Keep aspect ratio")
137 #define AR_LONGTEXT N_( \
138         "Keep the original aspect ratio when resizing " \
139         "mosaic elements." )
140 #define KEEP_TEXT N_("Keep original size")
141 #define KEEP_LONGTEXT N_( \
142         "Keep the original size of mosaic elements." )
143
144 #define ORDER_TEXT N_("Elements order" )
145 #define ORDER_LONGTEXT N_( \
146         "You can enforce the order of the elements on " \
147         "the mosaic. You must give a comma-separated list of picture ID(s)." \
148         "These IDs are assigned in the \"mosaic-bridge\" module." )
149
150 #define OFFSETS_TEXT N_("Offsets in order" )
151 #define OFFSETS_LONGTEXT N_( \
152         "You can enforce the (x,y) offsets of the elements on the mosaic " \
153         "(only used if positioning method is set to \"offsets\"). You " \
154         "must give a comma-separated list of coordinates (eg: 10,10,150,10)." )
155
156 #define DELAY_TEXT N_("Delay")
157 #define DELAY_LONGTEXT N_( \
158         "Pictures coming from the mosaic elements will be delayed " \
159         "according to this value (in milliseconds). For high " \
160         "values you will need to raise caching at input.")
161
162 enum
163 {
164     position_auto = 0, position_fixed = 1, position_offsets = 2
165 };
166 static const int pi_pos_values[] = { 0, 1, 2 };
167 static const char *const ppsz_pos_descriptions[] =
168     { N_("auto"), N_("fixed"), N_("offsets") };
169
170 static const int pi_align_values[] = { 0, 1, 2, 4, 8, 5, 6, 9, 10 };
171 static const char *const ppsz_align_descriptions[] =
172      { N_("Center"), N_("Left"), N_("Right"), N_("Top"), N_("Bottom"),
173      N_("Top-Left"), N_("Top-Right"), N_("Bottom-Left"), N_("Bottom-Right") };
174
175 #define CFG_PREFIX "mosaic-"
176
177 vlc_module_begin ()
178     set_description( N_("Mosaic video sub filter") )
179     set_shortname( N_("Mosaic") )
180     set_category( CAT_VIDEO )
181     set_subcategory( SUBCAT_VIDEO_SUBPIC)
182     set_capability( "sub filter", 0 )
183     set_callbacks( CreateFilter, DestroyFilter )
184
185     add_integer_with_range( CFG_PREFIX "alpha", 255, 0, 255, NULL,
186                             ALPHA_TEXT, ALPHA_LONGTEXT, false );
187
188     add_integer( CFG_PREFIX "height", 100, NULL,
189                  HEIGHT_TEXT, HEIGHT_LONGTEXT, false );
190     add_integer( CFG_PREFIX "width", 100, NULL,
191                  WIDTH_TEXT, WIDTH_LONGTEXT, false );
192
193     add_integer( CFG_PREFIX "align", 5, NULL,
194                  ALIGN_TEXT, ALIGN_LONGTEXT, true);
195         change_integer_list( pi_align_values, ppsz_align_descriptions, NULL );
196
197     add_integer( CFG_PREFIX "xoffset", 0, NULL,
198                  XOFFSET_TEXT, XOFFSET_LONGTEXT, true );
199     add_integer( CFG_PREFIX "yoffset", 0, NULL,
200                  YOFFSET_TEXT, YOFFSET_LONGTEXT, true );
201
202     add_integer( CFG_PREFIX "borderw", 0, NULL,
203                  BORDERW_TEXT, BORDERW_LONGTEXT, true );
204         add_deprecated_alias( CFG_PREFIX "vborder" )
205     add_integer( CFG_PREFIX "borderh", 0, NULL,
206                  BORDERH_TEXT, BORDERH_LONGTEXT, true );
207         add_deprecated_alias( CFG_PREFIX "hborder" )
208
209     add_integer( CFG_PREFIX "position", 0, NULL,
210                  POS_TEXT, POS_LONGTEXT, false );
211         change_integer_list( pi_pos_values, ppsz_pos_descriptions, NULL );
212     add_integer( CFG_PREFIX "rows", 2, NULL,
213                  ROWS_TEXT, ROWS_LONGTEXT, false );
214     add_integer( CFG_PREFIX "cols", 2, NULL,
215                  COLS_TEXT, COLS_LONGTEXT, false );
216
217     add_bool( CFG_PREFIX "keep-aspect-ratio", 0, NULL,
218               AR_TEXT, AR_LONGTEXT, false );
219     add_bool( CFG_PREFIX "keep-picture", 0, NULL,
220               KEEP_TEXT, KEEP_LONGTEXT, false );
221
222     add_string( CFG_PREFIX "order", "", NULL,
223                 ORDER_TEXT, ORDER_LONGTEXT, false );
224
225     add_string( CFG_PREFIX "offsets", "", NULL,
226                 OFFSETS_TEXT, OFFSETS_LONGTEXT, false );
227
228     add_integer( CFG_PREFIX "delay", 0, NULL, DELAY_TEXT, DELAY_LONGTEXT,
229                  false );
230 vlc_module_end ()
231
232 static const char *const ppsz_filter_options[] = {
233     "alpha", "height", "width", "align", "xoffset", "yoffset",
234     "borderw", "borderh", "position", "rows", "cols",
235     "keep-aspect-ratio", "keep-picture", "order", "offsets",
236     "delay", NULL
237 };
238
239 /*****************************************************************************
240  * mosaic_ParseSetOffsets:
241  * parse the "--mosaic-offsets x1,y1,x2,y2,x3,y3" parameter
242  * and set the corresponding struct filter_sys_t entries.
243  *****************************************************************************/
244 #define mosaic_ParseSetOffsets( a, b, c ) \
245       __mosaic_ParseSetOffsets( VLC_OBJECT( a ), b, c )
246 static void __mosaic_ParseSetOffsets( vlc_object_t *p_this,
247                                       filter_sys_t *p_sys,
248                                       char *psz_offsets )
249 {
250     if( *psz_offsets )
251     {
252         char *psz_end = NULL;
253         int i_index = 0;
254         do
255         {
256             i_index++;
257
258             p_sys->pi_x_offsets =
259                 realloc( p_sys->pi_x_offsets, i_index * sizeof(int) );
260             p_sys->pi_x_offsets[i_index - 1] = atoi( psz_offsets );
261             psz_end = strchr( psz_offsets, ',' );
262             psz_offsets = psz_end + 1;
263
264             p_sys->pi_y_offsets =
265                 realloc( p_sys->pi_y_offsets, i_index * sizeof(int) );
266             p_sys->pi_y_offsets[i_index - 1] = atoi( psz_offsets );
267             psz_end = strchr( psz_offsets, ',' );
268             psz_offsets = psz_end + 1;
269
270             msg_Dbg( p_this, CFG_PREFIX "offset: id %d, x=%d, y=%d",
271                      i_index, p_sys->pi_x_offsets[i_index - 1],
272                               p_sys->pi_y_offsets[i_index - 1]  );
273
274         } while( psz_end );
275         p_sys->i_offsets_length = i_index;
276     }
277 }
278
279 /*****************************************************************************
280  * CreateFiler: allocate mosaic video filter
281  *****************************************************************************/
282 static int CreateFilter( vlc_object_t *p_this )
283 {
284     filter_t *p_filter = (filter_t *)p_this;
285     filter_sys_t *p_sys;
286     vlc_object_t *p_libvlc = VLC_OBJECT( p_filter->p_libvlc );
287     char *psz_order, *_psz_order;
288     char *psz_offsets;
289     int i_index;
290     vlc_value_t val;
291
292     /* The mosaic thread is more important than the decoder threads */
293     vlc_thread_set_priority( p_this, VLC_THREAD_PRIORITY_OUTPUT );
294
295     /* Allocate structure */
296     p_sys = p_filter->p_sys = malloc( sizeof( filter_sys_t ) );
297     if( p_sys == NULL )
298         return VLC_ENOMEM;
299
300     p_filter->pf_sub_filter = Filter;
301
302     vlc_mutex_init( &p_sys->lock );
303     vlc_mutex_lock( &p_sys->lock );
304
305     var_Create( p_libvlc, "mosaic-lock", VLC_VAR_MUTEX );
306     var_Get( p_libvlc, "mosaic-lock", &val );
307     p_sys->p_lock = val.p_address;
308
309     config_ChainParse( p_filter, CFG_PREFIX, ppsz_filter_options,
310                        p_filter->p_cfg );
311
312 #define GET_VAR( name, min, max )                                           \
313     p_sys->i_##name = __MIN( max, __MAX( min,                               \
314         var_CreateGetIntegerCommand( p_filter, CFG_PREFIX #name ) ) );      \
315     var_AddCallback( p_filter, CFG_PREFIX #name, MosaicCallback, p_sys );
316
317     GET_VAR( width, 0, INT_MAX );
318     GET_VAR( height, 0, INT_MAX );
319     GET_VAR( xoffset, 0, INT_MAX );
320     GET_VAR( yoffset, 0, INT_MAX );
321
322     GET_VAR( align, 0, 10 );
323     if( p_sys->i_align == 3 || p_sys->i_align == 7 )
324         p_sys->i_align = 5;
325
326     GET_VAR( borderw, 0, INT_MAX );
327     GET_VAR( borderh, 0, INT_MAX );
328     GET_VAR( rows, 1, INT_MAX );
329     GET_VAR( cols, 1, INT_MAX );
330     GET_VAR( alpha, 0, 255 );
331     GET_VAR( position, 0, 2 );
332     GET_VAR( delay, 100, INT_MAX );
333     p_sys->i_delay *= 1000;
334
335     p_sys->b_ar = var_CreateGetBoolCommand( p_filter,
336                                             CFG_PREFIX "keep-aspect-ratio" );
337     var_AddCallback( p_filter, CFG_PREFIX "keep-aspect-ratio", MosaicCallback,
338                      p_sys );
339
340     p_sys->b_keep = var_CreateGetBoolCommand( p_filter,
341                                               CFG_PREFIX "keep-picture" );
342     if ( !p_sys->b_keep )
343     {
344         p_sys->p_image = image_HandlerCreate( p_filter );
345     }
346
347     p_sys->i_order_length = 0;
348     p_sys->ppsz_order = NULL;
349     psz_order = var_CreateGetStringCommand( p_filter, CFG_PREFIX "order" );
350     _psz_order = psz_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     free( _psz_order );
371
372     /* Manage specific offsets for substreams */
373     psz_offsets = var_CreateGetStringCommand( p_filter, CFG_PREFIX "offsets" );
374     p_sys->i_offsets_length = 0;
375     p_sys->pi_x_offsets = NULL;
376     p_sys->pi_y_offsets = NULL;
377     mosaic_ParseSetOffsets( p_filter, p_sys, psz_offsets );
378     free( psz_offsets );
379     var_AddCallback( p_filter, CFG_PREFIX "offsets", MosaicCallback, p_sys );
380
381     vlc_mutex_unlock( &p_sys->lock );
382
383     return VLC_SUCCESS;
384 }
385
386 /*****************************************************************************
387  * DestroyFilter: destroy mosaic video filter
388  *****************************************************************************/
389 static void DestroyFilter( vlc_object_t *p_this )
390 {
391     filter_t *p_filter = (filter_t*)p_this;
392     filter_sys_t *p_sys = p_filter->p_sys;
393     int i_index;
394
395     vlc_mutex_lock( &p_sys->lock );
396
397     if( !p_sys->b_keep )
398     {
399         image_HandlerDelete( p_sys->p_image );
400     }
401
402     if( p_sys->i_order_length )
403     {
404         for( i_index = 0; i_index < p_sys->i_order_length; i_index++ )
405         {
406             free( p_sys->ppsz_order[i_index] );
407         }
408         free( p_sys->ppsz_order );
409     }
410     if( p_sys->i_offsets_length )
411     {
412         free( p_sys->pi_x_offsets );
413         free( p_sys->pi_y_offsets );
414         p_sys->i_offsets_length = 0;
415     }
416
417     vlc_mutex_unlock( &p_sys->lock );
418     vlc_mutex_destroy( &p_sys->lock );
419     free( p_sys );
420 }
421
422 /*****************************************************************************
423  * MosaicReleasePicture : Hack to avoid picture duplication
424  *****************************************************************************/
425 static void MosaicReleasePicture( picture_t *p_picture )
426 {
427     picture_t *p_original_pic = (picture_t *)p_picture->p_sys;
428
429     picture_Release( p_original_pic );
430 }
431
432 /*****************************************************************************
433  * Filter
434  *****************************************************************************/
435 static subpicture_t *Filter( filter_t *p_filter, mtime_t date )
436 {
437     filter_sys_t *p_sys = p_filter->p_sys;
438     bridge_t *p_bridge;
439
440     subpicture_t *p_spu;
441
442     int i_index, i_real_index, i_row, i_col;
443     int i_greatest_real_index_used = p_sys->i_order_length - 1;
444
445     unsigned int col_inner_width, row_inner_height;
446
447     subpicture_region_t *p_region;
448     subpicture_region_t *p_region_prev = NULL;
449
450     /* Allocate the subpicture internal data. */
451     p_spu = filter_NewSubpicture( p_filter );
452     if( !p_spu )
453         return NULL;
454
455     /* Initialize subpicture */
456     p_spu->i_channel = 0;
457     p_spu->i_start  = date;
458     p_spu->i_stop = 0;
459     p_spu->b_ephemer = true;
460     p_spu->i_alpha = p_sys->i_alpha;
461     p_spu->b_absolute = 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                 picture_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                 picture_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 (%"PRId64 ")",
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             if( fmt_in.i_chroma == VLC_FOURCC('Y','U','V','A') ||
600                 fmt_in.i_chroma == VLC_FOURCC('R','G','B','A') )
601                 fmt_out.i_chroma = VLC_FOURCC('Y','U','V','A');
602             else
603                 fmt_out.i_chroma = VLC_FOURCC('I','4','2','0');
604             fmt_out.i_width = col_inner_width;
605             fmt_out.i_height = row_inner_height;
606
607             if( p_sys->b_ar ) /* keep aspect ratio */
608             {
609                 if( (float)fmt_out.i_width / (float)fmt_out.i_height
610                       > (float)fmt_in.i_width / (float)fmt_in.i_height )
611                 {
612                     fmt_out.i_width = ( fmt_out.i_height * fmt_in.i_width )
613                                          / fmt_in.i_height;
614                 }
615                 else
616                 {
617                     fmt_out.i_height = ( fmt_out.i_width * fmt_in.i_height )
618                                         / fmt_in.i_width;
619                 }
620              }
621
622             fmt_out.i_visible_width = fmt_out.i_width;
623             fmt_out.i_visible_height = fmt_out.i_height;
624
625             p_converted = image_Convert( p_sys->p_image, p_es->p_picture,
626                                          &fmt_in, &fmt_out );
627             if( !p_converted )
628             {
629                 msg_Warn( p_filter,
630                            "image resizing and chroma conversion failed" );
631                 continue;
632             }
633         }
634         else
635         {
636             p_converted = p_es->p_picture;
637             fmt_in.i_width = fmt_out.i_width = p_converted->format.i_width;
638             fmt_in.i_height = fmt_out.i_height = p_converted->format.i_height;
639             fmt_in.i_chroma = fmt_out.i_chroma = p_converted->format.i_chroma;
640             fmt_out.i_visible_width = fmt_out.i_width;
641             fmt_out.i_visible_height = fmt_out.i_height;
642         }
643
644         p_region = subpicture_region_New( &fmt_out );
645         /* FIXME the copy is probably not needed anymore */
646         if( p_region )
647             picture_Copy( p_region->p_picture, p_converted );
648         if( !p_sys->b_keep )
649             picture_Release( p_converted );
650
651         if( !p_region )
652         {
653             msg_Err( p_filter, "cannot allocate SPU region" );
654             p_filter->pf_sub_buffer_del( p_filter, p_spu );
655             vlc_mutex_unlock( &p_sys->lock );
656             vlc_mutex_unlock( p_sys->p_lock );
657             return p_spu;
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         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     VLC_UNUSED(oldval);
737     filter_sys_t *p_sys = (filter_sys_t *) p_data;
738
739 #define VAR_IS( a ) !strcmp( psz_var, CFG_PREFIX a )
740     if( VAR_IS( "alpha" ) )
741     {
742         vlc_mutex_lock( &p_sys->lock );
743         msg_Dbg( p_this, "changing alpha from %d/255 to %d/255",
744                          p_sys->i_alpha, newval.i_int);
745         p_sys->i_alpha = __MIN( __MAX( newval.i_int, 0 ), 255 );
746         vlc_mutex_unlock( &p_sys->lock );
747     }
748     else if( VAR_IS( "height" ) )
749     {
750         vlc_mutex_lock( &p_sys->lock );
751         msg_Dbg( p_this, "changing height from %dpx to %dpx",
752                           p_sys->i_height, newval.i_int );
753         p_sys->i_height = __MAX( newval.i_int, 0 );
754         vlc_mutex_unlock( &p_sys->lock );
755     }
756     else if( VAR_IS( "width" ) )
757     {
758         vlc_mutex_lock( &p_sys->lock );
759         msg_Dbg( p_this, "changing width from %dpx to %dpx",
760                          p_sys->i_width, newval.i_int );
761         p_sys->i_width = __MAX( newval.i_int, 0 );
762         vlc_mutex_unlock( &p_sys->lock );
763     }
764     else if( VAR_IS( "xoffset" ) )
765     {
766         vlc_mutex_lock( &p_sys->lock );
767         msg_Dbg( p_this, "changing x offset from %dpx to %dpx",
768                          p_sys->i_xoffset, newval.i_int );
769         p_sys->i_xoffset = __MAX( newval.i_int, 0 );
770         vlc_mutex_unlock( &p_sys->lock );
771     }
772     else if( VAR_IS( "yoffset" ) )
773     {
774         vlc_mutex_lock( &p_sys->lock );
775         msg_Dbg( p_this, "changing y offset from %dpx to %dpx",
776                          p_sys->i_yoffset, newval.i_int );
777         p_sys->i_yoffset = __MAX( newval.i_int, 0 );
778         vlc_mutex_unlock( &p_sys->lock );
779     }
780     else if( VAR_IS( "align" ) )
781     {
782         int i_old = 0, i_new = 0;
783         vlc_mutex_lock( &p_sys->lock );
784         newval.i_int = __MIN( __MAX( newval.i_int, 0 ), 10 );
785         if( newval.i_int == 3 || newval.i_int == 7 )
786             newval.i_int = 5;
787         while( pi_align_values[i_old] != p_sys->i_align ) i_old++;
788         while( pi_align_values[i_new] != newval.i_int ) i_new++;
789         msg_Dbg( p_this, "changing alignment from %d (%s) to %d (%s)",
790                      p_sys->i_align, ppsz_align_descriptions[i_old],
791                      newval.i_int, ppsz_align_descriptions[i_new] );
792         p_sys->i_align = newval.i_int;
793         vlc_mutex_unlock( &p_sys->lock );
794     }
795     else if( VAR_IS( "borderw" ) )
796     {
797         vlc_mutex_lock( &p_sys->lock );
798         msg_Dbg( p_this, "changing border width from %dpx to %dpx",
799                          p_sys->i_borderw, newval.i_int );
800         p_sys->i_borderw = __MAX( newval.i_int, 0 );
801         vlc_mutex_unlock( &p_sys->lock );
802     }
803     else if( VAR_IS( "borderh" ) )
804     {
805         vlc_mutex_lock( &p_sys->lock );
806         msg_Dbg( p_this, "changing border height from %dpx to %dpx",
807                          p_sys->i_borderh, newval.i_int );
808         p_sys->i_borderh = __MAX( newval.i_int, 0 );
809         vlc_mutex_unlock( &p_sys->lock );
810     }
811     else if( VAR_IS( "position" ) )
812     {
813         if( newval.i_int > 2 || newval.i_int < 0 )
814         {
815             msg_Err( p_this,
816                      "Position is either 0 (%s), 1 (%s) or 2 (%s)",
817                      ppsz_pos_descriptions[0],
818                      ppsz_pos_descriptions[1],
819                      ppsz_pos_descriptions[2] );
820         }
821         else
822         {
823             vlc_mutex_lock( &p_sys->lock );
824             msg_Dbg( p_this, "changing position method from %d (%s) to %d (%s)",
825                     p_sys->i_position, ppsz_pos_descriptions[p_sys->i_position],
826                     newval.i_int, ppsz_pos_descriptions[newval.i_int]);
827             p_sys->i_position = newval.i_int;
828             vlc_mutex_unlock( &p_sys->lock );
829         }
830     }
831     else if( VAR_IS( "rows" ) )
832     {
833         vlc_mutex_lock( &p_sys->lock );
834         msg_Dbg( p_this, "changing number of rows from %d to %d",
835                          p_sys->i_rows, newval.i_int );
836         p_sys->i_rows = __MAX( newval.i_int, 1 );
837         vlc_mutex_unlock( &p_sys->lock );
838     }
839     else if( VAR_IS( "cols" ) )
840     {
841         vlc_mutex_lock( &p_sys->lock );
842         msg_Dbg( p_this, "changing number of columns from %d to %d",
843                          p_sys->i_cols, newval.i_int );
844         p_sys->i_cols = __MAX( newval.i_int, 1 );
845         vlc_mutex_unlock( &p_sys->lock );
846     }
847     else if( VAR_IS( "order" ) )
848     {
849         char *psz_order;
850         int i_index;
851         vlc_mutex_lock( &p_sys->lock );
852         msg_Dbg( p_this, "Changing mosaic order to %s", newval.psz_string );
853
854         psz_order = newval.psz_string;
855
856         while( p_sys->i_order_length-- )
857         {
858             free( p_sys->ppsz_order[p_sys->i_order_length] );
859         }
860         free( p_sys->ppsz_order );
861         p_sys->ppsz_order = NULL;
862
863         if( *psz_order )
864         {
865             char *psz_end = NULL;
866             i_index = 0;
867             do
868             {
869                 psz_end = strchr( psz_order, ',' );
870                 i_index++;
871                 p_sys->ppsz_order = realloc( p_sys->ppsz_order,
872                                     i_index * sizeof(char *) );
873                 p_sys->ppsz_order[i_index - 1] = strndup( psz_order,
874                                            psz_end - psz_order );
875                 psz_order = psz_end+1;
876             } while( psz_end );
877             p_sys->i_order_length = i_index;
878         }
879
880         vlc_mutex_unlock( &p_sys->lock );
881     }
882     else if( VAR_IS( "offsets" ) )
883     {
884         vlc_mutex_lock( &p_sys->lock );
885         msg_Info( p_this, "Changing mosaic-offsets to %s", newval.psz_string );
886
887         if( p_sys->i_offsets_length != 0 )
888         {
889             p_sys->i_offsets_length = 0;
890             free( p_sys->pi_x_offsets );
891             free( p_sys->pi_y_offsets );
892             p_sys->pi_x_offsets = NULL;
893             p_sys->pi_y_offsets = NULL;
894         }
895
896         mosaic_ParseSetOffsets( p_this, p_sys, newval.psz_string );
897
898         vlc_mutex_unlock( &p_sys->lock );
899     }
900     else if( VAR_IS( "keep-aspect-ratio" ) )
901     {
902         vlc_mutex_lock( &p_sys->lock );
903         if( newval.i_int )
904         {
905             msg_Dbg( p_this, "keeping aspect ratio" );
906             p_sys->b_ar = 1;
907         }
908         else
909         {
910             msg_Dbg( p_this, "won't keep aspect ratio" );
911             p_sys->b_ar = 0;
912         }
913         vlc_mutex_unlock( &p_sys->lock );
914     }
915     else if( VAR_IS( "keep-picture" ) )
916     {
917         vlc_mutex_lock( &p_sys->lock );
918         p_sys->b_keep = newval.b_bool;
919         if ( !p_sys->b_keep && !p_sys->p_image )
920         {
921             p_sys->p_image = image_HandlerCreate( p_this );
922         }
923         vlc_mutex_unlock( &p_sys->lock );
924     }
925
926     return VLC_SUCCESS;
927 }