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