]> git.sesse.net Git - vlc/blob - modules/video_filter/mosaic.c
Merge branch '1.0'
[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
35 #include <math.h>
36 #include <limits.h> /* INT_MAX */
37
38 #include "vlc_filter.h"
39 #include "vlc_image.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     vlc_mutex_t *p_lock;      /* Pointer to mosaic bridge lock */
62
63     image_handler_t *p_image;
64
65     int i_position;           /* Mosaic positioning method */
66     bool b_ar;          /* Do we keep the aspect ratio ? */
67     bool b_keep;        /* Do we keep the original picture format ? */
68     int i_width, i_height;    /* Mosaic height and width */
69     int i_cols, i_rows;       /* Mosaic rows and cols */
70     int i_align;              /* Mosaic alignment in background video */
71     int i_xoffset, i_yoffset; /* Top left corner offset */
72     int i_borderw, i_borderh; /* Border width/height between miniatures */
73     int i_alpha;              /* Subfilter alpha blending */
74
75     char **ppsz_order;        /* List of picture-ids */
76     int i_order_length;
77
78     int *pi_x_offsets;        /* List of substreams x offsets */
79     int *pi_y_offsets;        /* List of substreams y offsets */
80     int i_offsets_length;
81
82     mtime_t i_delay;
83 };
84
85 /*****************************************************************************
86  * Module descriptor
87  *****************************************************************************/
88 #define ALPHA_TEXT N_("Transparency")
89 #define ALPHA_LONGTEXT N_( \
90         "Transparency of the mosaic foreground pictures. " \
91         "0 means transparent, 255 opaque (default)." )
92
93 #define HEIGHT_TEXT N_("Height")
94 #define HEIGHT_LONGTEXT N_( "Total height of the mosaic, in pixels." )
95 #define WIDTH_TEXT N_("Width")
96 #define WIDTH_LONGTEXT N_( "Total width of the mosaic, in pixels." )
97
98 #define XOFFSET_TEXT N_("Top left corner X coordinate")
99 #define XOFFSET_LONGTEXT N_( \
100         "X Coordinate of the top-left corner of the mosaic.")
101 #define YOFFSET_TEXT N_("Top left corner Y coordinate")
102 #define YOFFSET_LONGTEXT N_( \
103         "Y Coordinate of the top-left corner of the mosaic.")
104
105 #define BORDERW_TEXT N_("Border width")
106 #define BORDERW_LONGTEXT N_( \
107         "Width in pixels of the border between miniatures." )
108 #define BORDERH_TEXT N_("Border height")
109 #define BORDERH_LONGTEXT N_( \
110         "Height in pixels of the border between miniatures." )
111
112 #define ALIGN_TEXT N_("Mosaic alignment" )
113 #define ALIGN_LONGTEXT N_( \
114         "You can enforce the mosaic alignment on the video " \
115         "(0=center, 1=left, 2=right, 4=top, 8=bottom, you can " \
116         "also use combinations of these values, eg 6 = top-right).")
117
118 #define POS_TEXT N_("Positioning method")
119 #define POS_LONGTEXT N_( \
120         "Positioning method for the mosaic. auto: " \
121         "automatically choose the best number of rows and columns. " \
122         "fixed: use the user-defined number of rows and columns. " \
123         "offsets: use the user-defined offsets for each image." )
124
125 #define ROWS_TEXT N_("Number of rows")
126 #define ROWS_LONGTEXT N_( \
127         "Number of image rows in the mosaic (only used if " \
128         "positionning method is set to \"fixed\")." )
129
130 #define COLS_TEXT N_("Number of columns")
131 #define COLS_LONGTEXT N_( \
132         "Number of image columns in the mosaic (only used if " \
133         "positionning method is set to \"fixed\"." )
134
135 #define AR_TEXT N_("Keep aspect ratio")
136 #define AR_LONGTEXT N_( \
137         "Keep the original aspect ratio when resizing " \
138         "mosaic elements." )
139 #define KEEP_TEXT N_("Keep original size")
140 #define KEEP_LONGTEXT N_( \
141         "Keep the original size of mosaic elements." )
142
143 #define ORDER_TEXT N_("Elements order" )
144 #define ORDER_LONGTEXT N_( \
145         "You can enforce the order of the elements on " \
146         "the mosaic. You must give a comma-separated list of picture ID(s)." \
147         "These IDs are assigned in the \"mosaic-bridge\" module." )
148
149 #define OFFSETS_TEXT N_("Offsets in order" )
150 #define OFFSETS_LONGTEXT N_( \
151         "You can enforce the (x,y) offsets of the elements on the mosaic " \
152         "(only used if positioning method is set to \"offsets\"). You " \
153         "must give a comma-separated list of coordinates (eg: 10,10,150,10)." )
154
155 #define DELAY_TEXT N_("Delay")
156 #define DELAY_LONGTEXT N_( \
157         "Pictures coming from the mosaic elements will be delayed " \
158         "according to this value (in milliseconds). For high " \
159         "values you will need to raise caching at input.")
160
161 enum
162 {
163     position_auto = 0, position_fixed = 1, position_offsets = 2
164 };
165 static const int pi_pos_values[] = { 0, 1, 2 };
166 static const char *const ppsz_pos_descriptions[] =
167     { N_("auto"), N_("fixed"), N_("offsets") };
168
169 static const int pi_align_values[] = { 0, 1, 2, 4, 8, 5, 6, 9, 10 };
170 static const char *const ppsz_align_descriptions[] =
171      { N_("Center"), N_("Left"), N_("Right"), N_("Top"), N_("Bottom"),
172      N_("Top-Left"), N_("Top-Right"), N_("Bottom-Left"), N_("Bottom-Right") };
173
174 #define CFG_PREFIX "mosaic-"
175
176 vlc_module_begin ()
177     set_description( N_("Mosaic video sub filter") )
178     set_shortname( N_("Mosaic") )
179     set_category( CAT_VIDEO )
180     set_subcategory( SUBCAT_VIDEO_SUBPIC)
181     set_capability( "sub filter", 0 )
182     set_callbacks( CreateFilter, DestroyFilter )
183
184     add_integer_with_range( CFG_PREFIX "alpha", 255, 0, 255, NULL,
185                             ALPHA_TEXT, ALPHA_LONGTEXT, false )
186
187     add_integer( CFG_PREFIX "height", 100, NULL,
188                  HEIGHT_TEXT, HEIGHT_LONGTEXT, false )
189     add_integer( CFG_PREFIX "width", 100, NULL,
190                  WIDTH_TEXT, WIDTH_LONGTEXT, false )
191
192     add_integer( CFG_PREFIX "align", 5, NULL,
193                  ALIGN_TEXT, ALIGN_LONGTEXT, true)
194         change_integer_list( pi_align_values, ppsz_align_descriptions, NULL )
195
196     add_integer( CFG_PREFIX "xoffset", 0, NULL,
197                  XOFFSET_TEXT, XOFFSET_LONGTEXT, true )
198     add_integer( CFG_PREFIX "yoffset", 0, NULL,
199                  YOFFSET_TEXT, YOFFSET_LONGTEXT, true )
200
201     add_integer( CFG_PREFIX "borderw", 0, NULL,
202                  BORDERW_TEXT, BORDERW_LONGTEXT, true )
203         add_deprecated_alias( CFG_PREFIX "vborder" )
204     add_integer( CFG_PREFIX "borderh", 0, NULL,
205                  BORDERH_TEXT, BORDERH_LONGTEXT, true )
206         add_deprecated_alias( CFG_PREFIX "hborder" )
207
208     add_integer( CFG_PREFIX "position", 0, NULL,
209                  POS_TEXT, POS_LONGTEXT, false )
210         change_integer_list( pi_pos_values, ppsz_pos_descriptions, NULL )
211     add_integer( CFG_PREFIX "rows", 2, NULL,
212                  ROWS_TEXT, ROWS_LONGTEXT, false )
213     add_integer( CFG_PREFIX "cols", 2, NULL,
214                  COLS_TEXT, COLS_LONGTEXT, false )
215
216     add_bool( CFG_PREFIX "keep-aspect-ratio", 0, NULL,
217               AR_TEXT, AR_LONGTEXT, false )
218     add_bool( CFG_PREFIX "keep-picture", 0, NULL,
219               KEEP_TEXT, KEEP_LONGTEXT, false )
220
221     add_string( CFG_PREFIX "order", "", NULL,
222                 ORDER_TEXT, ORDER_LONGTEXT, false )
223
224     add_string( CFG_PREFIX "offsets", "", NULL,
225                 OFFSETS_TEXT, OFFSETS_LONGTEXT, false )
226
227     add_integer( CFG_PREFIX "delay", 0, NULL, DELAY_TEXT, DELAY_LONGTEXT,
228                  false )
229 vlc_module_end ()
230
231 static const char *const ppsz_filter_options[] = {
232     "alpha", "height", "width", "align", "xoffset", "yoffset",
233     "borderw", "borderh", "position", "rows", "cols",
234     "keep-aspect-ratio", "keep-picture", "order", "offsets",
235     "delay", NULL
236 };
237
238 /*****************************************************************************
239  * mosaic_ParseSetOffsets:
240  * parse the "--mosaic-offsets x1,y1,x2,y2,x3,y3" parameter
241  * and set the corresponding struct filter_sys_t entries.
242  *****************************************************************************/
243 #define mosaic_ParseSetOffsets( a, b, c ) \
244       __mosaic_ParseSetOffsets( VLC_OBJECT( a ), b, c )
245 static void __mosaic_ParseSetOffsets( vlc_object_t *p_this,
246                                       filter_sys_t *p_sys,
247                                       char *psz_offsets )
248 {
249     if( *psz_offsets )
250     {
251         char *psz_end = NULL;
252         int i_index = 0;
253         do
254         {
255             i_index++;
256
257             p_sys->pi_x_offsets =
258                 realloc( p_sys->pi_x_offsets, i_index * sizeof(int) );
259             p_sys->pi_x_offsets[i_index - 1] = atoi( psz_offsets );
260             psz_end = strchr( psz_offsets, ',' );
261             psz_offsets = psz_end + 1;
262
263             p_sys->pi_y_offsets =
264                 realloc( p_sys->pi_y_offsets, i_index * sizeof(int) );
265             p_sys->pi_y_offsets[i_index - 1] = atoi( psz_offsets );
266             psz_end = strchr( psz_offsets, ',' );
267             psz_offsets = psz_end + 1;
268
269             msg_Dbg( p_this, CFG_PREFIX "offset: id %d, x=%d, y=%d",
270                      i_index, p_sys->pi_x_offsets[i_index - 1],
271                               p_sys->pi_y_offsets[i_index - 1]  );
272
273         } while( psz_end );
274         p_sys->i_offsets_length = i_index;
275     }
276 }
277
278 /*****************************************************************************
279  * CreateFiler: allocate mosaic video filter
280  *****************************************************************************/
281 static int CreateFilter( vlc_object_t *p_this )
282 {
283     filter_t *p_filter = (filter_t *)p_this;
284     filter_sys_t *p_sys;
285     vlc_object_t *p_libvlc = VLC_OBJECT( p_filter->p_libvlc );
286     char *psz_order, *_psz_order;
287     char *psz_offsets;
288     int i_index;
289     vlc_value_t val;
290     int i_command;
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     i_command = var_CreateGetIntegerCommand( p_filter, CFG_PREFIX #name );  \
314     p_sys->i_##name = __MIN( max, __MAX( min, i_command ) );                \
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; /* FIXME: NOT THREAD SAFE w.r.t. callback */
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 #undef GET_VAR
334     p_sys->i_delay *= 1000; /* FIXME: NOT THREAD SAFE w.r.t. callback */
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     _psz_order = psz_order;
352     var_AddCallback( p_filter, CFG_PREFIX "order", MosaicCallback, p_sys );
353
354     if( *psz_order )
355     {
356         char *psz_end = NULL;
357         i_index = 0;
358         do
359         {
360             psz_end = strchr( psz_order, ',' );
361             i_index++;
362             p_sys->ppsz_order = realloc( p_sys->ppsz_order,
363                                          i_index * sizeof(char *) );
364             p_sys->ppsz_order[i_index - 1] = strndup( psz_order,
365                                            psz_end - psz_order );
366             psz_order = psz_end+1;
367         } while( psz_end );
368         p_sys->i_order_length = i_index;
369     }
370
371     free( _psz_order );
372
373     /* Manage specific offsets for substreams */
374     psz_offsets = var_CreateGetStringCommand( p_filter, CFG_PREFIX "offsets" );
375     p_sys->i_offsets_length = 0;
376     p_sys->pi_x_offsets = NULL;
377     p_sys->pi_y_offsets = NULL;
378     mosaic_ParseSetOffsets( p_filter, p_sys, psz_offsets );
379     free( psz_offsets );
380     var_AddCallback( p_filter, CFG_PREFIX "offsets", MosaicCallback, p_sys );
381
382     vlc_mutex_unlock( &p_sys->lock );
383
384     return VLC_SUCCESS;
385 }
386
387 /*****************************************************************************
388  * DestroyFilter: destroy mosaic video filter
389  *****************************************************************************/
390 static void DestroyFilter( vlc_object_t *p_this )
391 {
392     filter_t *p_filter = (filter_t*)p_this;
393     filter_sys_t *p_sys = p_filter->p_sys;
394
395     /* FIXME: destroy callbacks first! */
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( int 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_destroy( &p_sys->lock );
418     free( p_sys );
419 }
420
421 /*****************************************************************************
422  * MosaicReleasePicture : Hack to avoid picture duplication
423  *****************************************************************************/
424 static void MosaicReleasePicture( picture_t *p_picture )
425 {
426     picture_t *p_original_pic = (picture_t *)p_picture->p_sys;
427
428     picture_Release( p_original_pic );
429 }
430
431 /*****************************************************************************
432  * Filter
433  *****************************************************************************/
434 static subpicture_t *Filter( filter_t *p_filter, mtime_t date )
435 {
436     filter_sys_t *p_sys = p_filter->p_sys;
437     bridge_t *p_bridge;
438
439     subpicture_t *p_spu;
440
441     int i_index, i_real_index, i_row, i_col;
442     int i_greatest_real_index_used = p_sys->i_order_length - 1;
443
444     unsigned int col_inner_width, row_inner_height;
445
446     subpicture_region_t *p_region;
447     subpicture_region_t *p_region_prev = NULL;
448
449     /* Allocate the subpicture internal data. */
450     p_spu = filter_NewSubpicture( p_filter );
451     if( !p_spu )
452         return NULL;
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 = true;
459     p_spu->i_alpha = p_sys->i_alpha;
460     p_spu->b_absolute = false;
461
462     vlc_mutex_lock( &p_sys->lock );
463     vlc_mutex_lock( p_sys->p_lock );
464
465     p_bridge = GetBridge( p_filter );
466     if ( p_bridge == NULL )
467     {
468         vlc_mutex_unlock( p_sys->p_lock );
469         vlc_mutex_unlock( &p_sys->lock );
470         return p_spu;
471     }
472
473     if ( p_sys->i_position == position_offsets )
474     {
475         /* If we have either too much or not enough offsets, fall-back
476          * to automatic positioning. */
477         if ( p_sys->i_offsets_length != p_sys->i_order_length )
478         {
479             msg_Err( p_filter,
480                      "Number of specified offsets (%d) does not match number "
481                      "of input substreams in mosaic-order (%d), falling back "
482                      "to mosaic-position=0",
483                      p_sys->i_offsets_length, p_sys->i_order_length );
484             p_sys->i_position = position_auto;
485         }
486     }
487
488     if ( p_sys->i_position == position_auto )
489     {
490         int i_numpics = p_sys->i_order_length; /* keep slots and all */
491         for ( i_index = 0; i_index < p_bridge->i_es_num; i_index++ )
492         {
493             bridged_es_t *p_es = p_bridge->pp_es[i_index];
494             if ( !p_es->b_empty )
495             {
496                 i_numpics ++;
497                 if( p_sys->i_order_length && p_es->psz_id != NULL )
498                 {
499                     /* We also want to leave slots for images given in
500                      * mosaic-order that are not available in p_vout_picture */
501                     int i;
502                     for( i = 0; i < p_sys->i_order_length ; i++ )
503                     {
504                         if( !strcmp( p_sys->ppsz_order[i], p_es->psz_id ) )
505                         {
506                             i_numpics--;
507                             break;
508                         }
509                     }
510
511                 }
512             }
513         }
514         p_sys->i_rows = ceil(sqrt( (double)i_numpics ));
515         p_sys->i_cols = ( i_numpics % p_sys->i_rows == 0 ?
516                             i_numpics / p_sys->i_rows :
517                             i_numpics / p_sys->i_rows + 1 );
518     }
519
520     col_inner_width  = ( ( p_sys->i_width - ( p_sys->i_cols - 1 )
521                        * p_sys->i_borderw ) / p_sys->i_cols );
522     row_inner_height = ( ( p_sys->i_height - ( p_sys->i_rows - 1 )
523                        * p_sys->i_borderh ) / p_sys->i_rows );
524
525     i_real_index = 0;
526
527     for ( i_index = 0; i_index < p_bridge->i_es_num; i_index++ )
528     {
529         bridged_es_t *p_es = p_bridge->pp_es[i_index];
530         video_format_t fmt_in, fmt_out;
531         picture_t *p_converted;
532
533         memset( &fmt_in, 0, sizeof( video_format_t ) );
534         memset( &fmt_out, 0, sizeof( video_format_t ) );
535
536         if ( p_es->b_empty )
537             continue;
538
539         while ( p_es->p_picture != NULL
540                  && p_es->p_picture->date + p_sys->i_delay < date )
541         {
542             if ( p_es->p_picture->p_next != NULL )
543             {
544                 picture_t *p_next = p_es->p_picture->p_next;
545                 picture_Release( p_es->p_picture );
546                 p_es->p_picture = p_next;
547             }
548             else if ( p_es->p_picture->date + p_sys->i_delay + BLANK_DELAY <
549                         date )
550             {
551                 /* Display blank */
552                 picture_Release( p_es->p_picture );
553                 p_es->p_picture = NULL;
554                 p_es->pp_last = &p_es->p_picture;
555                 break;
556             }
557             else
558             {
559                 msg_Dbg( p_filter, "too late picture for %s (%"PRId64 ")",
560                          p_es->psz_id,
561                          date - p_es->p_picture->date - p_sys->i_delay );
562                 break;
563             }
564         }
565
566         if ( p_es->p_picture == NULL )
567             continue;
568
569         if ( p_sys->i_order_length == 0 )
570         {
571             i_real_index++;
572         }
573         else
574         {
575             int i;
576             for ( i = 0; i <= p_sys->i_order_length; i++ )
577             {
578                 if ( i == p_sys->i_order_length ) break;
579                 if ( strcmp( p_es->psz_id, p_sys->ppsz_order[i] ) == 0 )
580                 {
581                     i_real_index = i;
582                     break;
583                 }
584             }
585             if ( i == p_sys->i_order_length )
586                 i_real_index = ++i_greatest_real_index_used;
587         }
588         i_row = ( i_real_index / p_sys->i_cols ) % p_sys->i_rows;
589         i_col = i_real_index % p_sys->i_cols ;
590
591         if ( !p_sys->b_keep )
592         {
593             /* Convert the images */
594             fmt_in.i_chroma = p_es->p_picture->format.i_chroma;
595             fmt_in.i_height = p_es->p_picture->format.i_height;
596             fmt_in.i_width = p_es->p_picture->format.i_width;
597
598             if( fmt_in.i_chroma == VLC_CODEC_YUVA ||
599                 fmt_in.i_chroma == VLC_CODEC_RGBA )
600                 fmt_out.i_chroma = VLC_CODEC_YUVA;
601             else
602                 fmt_out.i_chroma = VLC_CODEC_I420;
603             fmt_out.i_width = col_inner_width;
604             fmt_out.i_height = row_inner_height;
605
606             if( p_sys->b_ar ) /* keep aspect ratio */
607             {
608                 if( (float)fmt_out.i_width / (float)fmt_out.i_height
609                       > (float)fmt_in.i_width / (float)fmt_in.i_height )
610                 {
611                     fmt_out.i_width = ( fmt_out.i_height * fmt_in.i_width )
612                                          / fmt_in.i_height;
613                 }
614                 else
615                 {
616                     fmt_out.i_height = ( fmt_out.i_width * fmt_in.i_height )
617                                         / fmt_in.i_width;
618                 }
619              }
620
621             fmt_out.i_visible_width = fmt_out.i_width;
622             fmt_out.i_visible_height = fmt_out.i_height;
623
624             p_converted = image_Convert( p_sys->p_image, p_es->p_picture,
625                                          &fmt_in, &fmt_out );
626             if( !p_converted )
627             {
628                 msg_Warn( p_filter,
629                            "image resizing and chroma conversion failed" );
630                 continue;
631             }
632         }
633         else
634         {
635             p_converted = p_es->p_picture;
636             fmt_in.i_width = fmt_out.i_width = p_converted->format.i_width;
637             fmt_in.i_height = fmt_out.i_height = p_converted->format.i_height;
638             fmt_in.i_chroma = fmt_out.i_chroma = p_converted->format.i_chroma;
639             fmt_out.i_visible_width = fmt_out.i_width;
640             fmt_out.i_visible_height = fmt_out.i_height;
641         }
642
643         p_region = subpicture_region_New( &fmt_out );
644         /* FIXME the copy is probably not needed anymore */
645         if( p_region )
646             picture_Copy( p_region->p_picture, p_converted );
647         if( !p_sys->b_keep )
648             picture_Release( p_converted );
649
650         if( !p_region )
651         {
652             msg_Err( p_filter, "cannot allocate SPU region" );
653             p_filter->pf_sub_buffer_del( p_filter, p_spu );
654             vlc_mutex_unlock( &p_sys->lock );
655             vlc_mutex_unlock( p_sys->p_lock );
656             return p_spu;
657         }
658
659         if( p_es->i_x >= 0 && p_es->i_y >= 0 )
660         {
661             p_region->i_x = p_es->i_x;
662             p_region->i_y = p_es->i_y;
663         }
664         else if( p_sys->i_position == position_offsets )
665         {
666             p_region->i_x = p_sys->pi_x_offsets[i_real_index];
667             p_region->i_y = p_sys->pi_y_offsets[i_real_index];
668         }
669         else
670         {
671             if( fmt_out.i_width > col_inner_width ||
672                 p_sys->b_ar || p_sys->b_keep )
673             {
674                 /* we don't have to center the video since it takes the
675                 whole rectangle area or it's larger than the rectangle */
676                 p_region->i_x = p_sys->i_xoffset
677                             + i_col * ( p_sys->i_width / p_sys->i_cols )
678                             + ( i_col * p_sys->i_borderw ) / p_sys->i_cols;
679             }
680             else
681             {
682                 /* center the video in the dedicated rectangle */
683                 p_region->i_x = p_sys->i_xoffset
684                         + i_col * ( p_sys->i_width / p_sys->i_cols )
685                         + ( i_col * p_sys->i_borderw ) / p_sys->i_cols
686                         + ( col_inner_width - fmt_out.i_width ) / 2;
687             }
688
689             if( fmt_out.i_height > row_inner_height
690                 || p_sys->b_ar || p_sys->b_keep )
691             {
692                 /* we don't have to center the video since it takes the
693                 whole rectangle area or it's taller than the rectangle */
694                 p_region->i_y = p_sys->i_yoffset
695                         + i_row * ( p_sys->i_height / p_sys->i_rows )
696                         + ( i_row * p_sys->i_borderh ) / p_sys->i_rows;
697             }
698             else
699             {
700                 /* center the video in the dedicated rectangle */
701                 p_region->i_y = p_sys->i_yoffset
702                         + i_row * ( p_sys->i_height / p_sys->i_rows )
703                         + ( i_row * p_sys->i_borderh ) / p_sys->i_rows
704                         + ( row_inner_height - fmt_out.i_height ) / 2;
705             }
706         }
707         p_region->i_align = p_sys->i_align;
708         p_region->i_alpha = p_es->i_alpha;
709
710         if( p_region_prev == NULL )
711         {
712             p_spu->p_region = p_region;
713         }
714         else
715         {
716             p_region_prev->p_next = p_region;
717         }
718
719         p_region_prev = p_region;
720     }
721
722     vlc_mutex_unlock( p_sys->p_lock );
723     vlc_mutex_unlock( &p_sys->lock );
724
725     return p_spu;
726 }
727
728 /*****************************************************************************
729 * Callback to update params on the fly
730 *****************************************************************************/
731 static int MosaicCallback( vlc_object_t *p_this, char const *psz_var,
732                             vlc_value_t oldval, vlc_value_t newval,
733                             void *p_data )
734 {
735     VLC_UNUSED(oldval);
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 }