]> git.sesse.net Git - vlc/blob - modules/video_filter/mosaic.c
Mosaic:
[vlc] / modules / video_filter / mosaic.c
1 /*****************************************************************************
2  * mosaic.c : Mosaic video plugin for vlc
3  *****************************************************************************
4  * Copyright (C) 2004-2005 the VideoLAN team
5  * $Id$
6  *
7  * Authors: Antoine Cellerier <dionoea@via.ecp.fr>
8  *          Christophe Massiot <massiot@via.ecp.fr>
9  *
10  * This program is free software; you can redistribute it and/or modify
11  * it under the terms of the GNU General Public License as published by
12  * the Free Software Foundation; either version 2 of the License, or
13  * (at your option) any later version.
14  *
15  * This program is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18  * GNU General Public License for more details.
19  *
20  * You should have received a copy of the GNU General Public License
21  * along with this program; if not, write to the Free Software
22  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
23 *****************************************************************************/
24
25 /*****************************************************************************
26  * Preamble
27  *****************************************************************************/
28 #define _GNU_SOURCE
29 #include <stdlib.h>                                      /* malloc(), free() */
30 #include <string.h>
31 #include <math.h>
32
33 #include <vlc/vlc.h>
34 #include <vlc_vout.h>
35
36 #ifdef HAVE_LIMITS_H
37 #   include <limits.h> /* INT_MAX */
38 #endif
39
40 #include "vlc_filter.h"
41 #include "vlc_image.h"
42
43 #include "mosaic.h"
44
45 #define BLANK_DELAY I64C(1000000)
46
47 /*****************************************************************************
48  * Local prototypes
49  *****************************************************************************/
50 static int  CreateFilter    ( vlc_object_t * );
51 static void DestroyFilter   ( vlc_object_t * );
52
53 static subpicture_t *Filter( filter_t *, mtime_t );
54
55 static int MosaicCallback( vlc_object_t *, char const *, vlc_value_t,
56                            vlc_value_t, void * );
57
58 /*****************************************************************************
59  * filter_sys_t : filter descriptor
60  *****************************************************************************/
61 struct filter_sys_t
62 {
63     vlc_mutex_t lock;
64     vlc_mutex_t *p_lock;
65
66     image_handler_t *p_image;
67     picture_t *p_pic;
68
69     int i_position; /* mosaic positioning method */
70     vlc_bool_t b_ar; /* do we keep the aspect ratio ? */
71     vlc_bool_t b_keep; /* do we keep the original picture format ? */
72     int i_width, i_height; /* mosaic height and width */
73     int i_cols, i_rows; /* mosaic rows and cols */
74     int i_align; /* mosaic alignment in background video */
75     int i_xoffset, i_yoffset; /* top left corner offset */
76     int i_borderw, i_borderh; /* border width/height between miniatures */
77     int i_alpha; /* subfilter alpha blending */
78
79     vlc_bool_t b_bs; /* Bluescreen vars */
80     int i_bsu, i_bsv, i_bsut, i_bsvt;
81
82     char **ppsz_order; /* list of picture-id */
83     int i_order_length;
84
85     int *pi_x_offsets; /* list of substreams x offsets */
86     int *pi_y_offsets; /* list of substreams y offsets */
87     int i_offsets_length;
88
89     mtime_t i_delay;
90 };
91
92 /*****************************************************************************
93  * Module descriptor
94  *****************************************************************************/
95 #define ALPHA_TEXT N_("Transparency")
96 #define ALPHA_LONGTEXT N_( \
97         "Transparency of the mosaic foreground pictures. " \
98         "0 means transparent, 255 opaque (default)." )
99
100 #define HEIGHT_TEXT N_("Height")
101 #define HEIGHT_LONGTEXT N_( "Total height of the mosaic, in pixels." )
102 #define WIDTH_TEXT N_("Width")
103 #define WIDTH_LONGTEXT N_( "Total width of the mosaic, in pixels." )
104
105 #define XOFFSET_TEXT N_("Top left corner X coordinate")
106 #define XOFFSET_LONGTEXT N_( \
107         "X Coordinate of the top-left corner of the mosaic.")
108 #define YOFFSET_TEXT N_("Top left corner Y coordinate")
109 #define YOFFSET_LONGTEXT N_( \
110         "Y Coordinate of the top-left corner of the mosaic.")
111
112 #define BORDERW_TEXT N_("Border width")
113 #define BORDERW_LONGTEXT N_( \
114         "Width in pixels of the border between miniatures." )
115 #define BORDERH_TEXT N_("Border height")
116 #define BORDERH_LONGTEXT N_( \
117         "Height in pixels of the border between miniatures." )
118
119 #define ALIGN_TEXT N_("Mosaic alignment" )
120 #define ALIGN_LONGTEXT N_( \
121         "You can enforce the mosaic alignment on the video " \
122         "(0=center, 1=left, 2=right, 4=top, 8=bottom, you can " \
123         "also use combinations of these values, eg 6 = top-right).")
124
125 #define POS_TEXT N_("Positioning method")
126 #define POS_LONGTEXT N_( \
127         "Positioning method for the mosaic. auto: " \
128         "automatically choose the best number of rows and columns. " \
129         "fixed: use the user-defined number of rows and columns. " \
130         "offsets: use the user-defined offsets for each image." )
131
132 #define ROWS_TEXT N_("Number of rows")
133 #define ROWS_LONGTEXT N_( \
134         "Number of image rows in the mosaic (only used if " \
135         "positionning method is set to \"fixed\")." )
136
137 #define COLS_TEXT N_("Number of columns")
138 #define COLS_LONGTEXT N_( \
139         "Number of image columns in the mosaic (only used if " \
140         "positionning method is set to \"fixed\"." )
141
142 #define AR_TEXT N_("Keep aspect ratio")
143 #define AR_LONGTEXT N_( \
144         "Keep the original aspect ratio when resizing " \
145         "mosaic elements." )
146 #define KEEP_TEXT N_("Keep original size")
147 #define KEEP_LONGTEXT N_( \
148         "Keep the original size of mosaic elements." )
149
150 #define ORDER_TEXT N_("Elements order" )
151 #define ORDER_LONGTEXT N_( \
152         "You can enforce the order of the elements on " \
153         "the mosaic. You must give a comma-separated list of picture ID(s)." \
154         "These IDs are assigned in the \"mosaic-bridge\" module." )
155
156 #define OFFSETS_TEXT N_("Offsets in order" )
157 #define OFFSETS_LONGTEXT N_( \
158         "You can enforce the (x,y) offsets of the elements on the mosaic " \
159         "(only used if positioning method is set to \"offsets\"). You " \
160         "must give a comma-separated list of coordinates (eg: 10,10,150,10)." )
161
162 #define DELAY_TEXT N_("Delay")
163 #define DELAY_LONGTEXT N_( \
164         "Pictures coming from the mosaic elements will be delayed " \
165         "according to this value (in milliseconds). For high " \
166         "values you will need to raise caching at input.")
167
168 #define BLUESCREEN_TEXT N_("Bluescreen" )
169 #define BLUESCREEN_LONGTEXT N_( \
170         "This effect, also known as \"greenscreen\" or \"chroma key\" blends " \
171         "the \"blue parts\" of the foreground images of the mosaic on the " \
172         "background (like weather forecast). You can choose the \"key\" " \
173         "color for blending (blue by default)." )
174
175 #define BLUESCREENU_TEXT N_("Bluescreen U value")
176 #define BLUESCREENU_LONGTEXT N_( \
177         "\"U\" value for the bluescreen key color " \
178         "(in YUV values). From 0 to 255. Defaults to 120 for blue." )
179 #define BLUESCREENV_TEXT N_("Bluescreen V value")
180 #define BLUESCREENV_LONGTEXT N_( \
181         "\"V\" value for the bluescreen key color " \
182         "(in YUV values). From 0 to 255. Defaults to 90 for blue." )
183 #define BLUESCREENUTOL_TEXT N_("Bluescreen U tolerance")
184 #define BLUESCREENUTOL_LONGTEXT N_( \
185         "Tolerance of the bluescreen blender " \
186         "on color variations for the U plane. A value between 10 and 20 " \
187         "seems sensible." )
188 #define BLUESCREENVTOL_TEXT N_("Bluescreen V tolerance")
189 #define BLUESCREENVTOL_LONGTEXT N_( \
190         "Tolerance of the bluescreen blender " \
191         "on color variations for the V plane. A value between 10 and 20 " \
192         "seems sensible." )
193
194 static int pi_pos_values[] = { 0, 1, 2 };
195 static const char * ppsz_pos_descriptions[] =
196 { N_("auto"), N_("fixed"), N_("offsets") };
197
198 static int pi_align_values[] = { 0, 1, 2, 4, 8, 5, 6, 9, 10 };
199 static const char *ppsz_align_descriptions[] =
200      { N_("Center"), N_("Left"), N_("Right"), N_("Top"), N_("Bottom"),
201      N_("Top-Left"), N_("Top-Right"), N_("Bottom-Left"), N_("Bottom-Right") };
202
203 #define CFG_PREFIX "mosaic-"
204
205 vlc_module_begin();
206     set_description( _("Mosaic video sub filter") );
207     set_shortname( _("Mosaic") );
208     set_category( CAT_VIDEO );
209     set_subcategory( SUBCAT_VIDEO_SUBPIC);
210     set_capability( "sub filter", 0 );
211     set_callbacks( CreateFilter, DestroyFilter );
212
213     add_integer( CFG_PREFIX "alpha", 255, NULL,
214                  ALPHA_TEXT, ALPHA_LONGTEXT, VLC_FALSE );
215
216     add_integer( CFG_PREFIX "height", 100, NULL,
217                  HEIGHT_TEXT, HEIGHT_LONGTEXT, VLC_FALSE );
218     add_integer( CFG_PREFIX "width", 100, NULL,
219                  WIDTH_TEXT, WIDTH_LONGTEXT, VLC_FALSE );
220
221     add_integer( CFG_PREFIX "align", 5, NULL,
222                  ALIGN_TEXT, ALIGN_LONGTEXT, VLC_TRUE);
223         change_integer_list( pi_align_values, ppsz_align_descriptions, 0 );
224
225     add_integer( CFG_PREFIX "xoffset", 0, NULL,
226                  XOFFSET_TEXT, XOFFSET_LONGTEXT, VLC_TRUE );
227     add_integer( CFG_PREFIX "yoffset", 0, NULL,
228                  YOFFSET_TEXT, YOFFSET_LONGTEXT, VLC_TRUE );
229
230     add_integer( CFG_PREFIX "borderw", 0, NULL,
231                  BORDERW_TEXT, BORDERW_LONGTEXT, VLC_TRUE );
232         add_deprecated( CFG_PREFIX "vborder", VLC_FALSE );
233     add_integer( CFG_PREFIX "borderh", 0, NULL,
234                  BORDERH_TEXT, BORDERH_LONGTEXT, VLC_TRUE );
235         add_deprecated( CFG_PREFIX "hborder", VLC_FALSE );
236
237     add_integer( CFG_PREFIX "position", 0, NULL,
238                  POS_TEXT, POS_LONGTEXT, VLC_FALSE );
239         change_integer_list( pi_pos_values, ppsz_pos_descriptions, 0 );
240     add_integer( CFG_PREFIX "rows", 2, NULL,
241                  ROWS_TEXT, ROWS_LONGTEXT, VLC_FALSE );
242     add_integer( CFG_PREFIX "cols", 2, NULL,
243                  COLS_TEXT, COLS_LONGTEXT, VLC_FALSE );
244
245     add_bool( CFG_PREFIX "keep-aspect-ratio", 0, NULL,
246               AR_TEXT, AR_LONGTEXT, VLC_FALSE );
247     add_bool( CFG_PREFIX "keep-picture", 0, NULL,
248               KEEP_TEXT, KEEP_LONGTEXT, VLC_FALSE );
249
250     add_string( CFG_PREFIX "order", "", NULL,
251                 ORDER_TEXT, ORDER_LONGTEXT, VLC_FALSE );
252
253     add_string( CFG_PREFIX "offsets", "", NULL,
254                 OFFSETS_TEXT, OFFSETS_LONGTEXT, VLC_FALSE );
255
256     add_integer( CFG_PREFIX "delay", 0, NULL, DELAY_TEXT, DELAY_LONGTEXT,
257                  VLC_FALSE );
258
259     set_section( N_("Bluescreen effect"), NULL );
260     add_bool( CFG_PREFIX "bs", 0, NULL, BLUESCREEN_TEXT,
261               BLUESCREEN_LONGTEXT, VLC_FALSE );
262     add_integer_with_range( CFG_PREFIX "bsu", 120, 0, 255, NULL,
263                             BLUESCREENU_TEXT, BLUESCREENU_LONGTEXT, VLC_FALSE );
264     add_integer_with_range( CFG_PREFIX "bsv", 90, 0, 255, NULL,
265                             BLUESCREENV_TEXT, BLUESCREENV_LONGTEXT, VLC_FALSE );
266     add_integer_with_range( CFG_PREFIX "bsut", 17, 0, 255, NULL,
267                             BLUESCREENUTOL_TEXT, BLUESCREENUTOL_LONGTEXT,
268                             VLC_FALSE );
269     add_integer_with_range( CFG_PREFIX "bsvt", 17, 0, 255, NULL,
270                             BLUESCREENVTOL_TEXT, BLUESCREENVTOL_LONGTEXT,
271                             VLC_FALSE );
272
273     var_Create( p_module->p_libvlc_global, "mosaic-lock", VLC_VAR_MUTEX );
274 vlc_module_end();
275
276 static const char *ppsz_filter_options[] = {
277     "alpha", "height", "width", "align", "xoffset", "yoffset",
278     "borderw", "borderh", "position", "rows", "cols",
279     "keep-aspect-ratio", "keep-picture", "order", "offsets",
280     "delay", "bs", "bsu", "bsv", "bsut", "bsvt", NULL
281 };
282
283 /*****************************************************************************
284  * mosaic_ParseSetOffsets:
285  * parse the "--mosaic-offsets x1,y1,x2,y2,x3,y3" parameter
286  * and set the corresponding struct filter_sys_t entries.
287  *****************************************************************************/
288 static void mosaic_ParseSetOffsets( vlc_object_t *p_this, filter_sys_t *p_sys, char *psz_offsets )
289 {
290     if( psz_offsets[0] != 0 )
291     {
292         char *psz_end = NULL;
293         int i_index = 0;
294         do
295         {
296             i_index++;
297
298             p_sys->pi_x_offsets = realloc( p_sys->pi_x_offsets, i_index * sizeof(int) );
299             p_sys->pi_x_offsets[i_index - 1] = atoi( psz_offsets );
300             psz_end = strchr( psz_offsets, ',' );
301             psz_offsets = psz_end + 1;
302
303             p_sys->pi_y_offsets = realloc( p_sys->pi_y_offsets, i_index * sizeof(int) );
304             p_sys->pi_y_offsets[i_index - 1] = atoi( psz_offsets );
305             psz_end = strchr( psz_offsets, ',' );
306             psz_offsets = psz_end + 1;
307
308             msg_Dbg( p_this, CFG_PREFIX "offset: id %d, x=%d, y=%d", i_index, p_sys->pi_x_offsets[i_index - 1], p_sys->pi_y_offsets[i_index - 1] );
309
310         } while( NULL != psz_end );
311         p_sys->i_offsets_length = i_index;
312     }
313 }
314
315 /*****************************************************************************
316  * CreateFiler: allocate mosaic video filter
317  *****************************************************************************/
318 static int CreateFilter( vlc_object_t *p_this )
319 {
320     filter_t *p_filter = (filter_t *)p_this;
321     filter_sys_t *p_sys;
322     libvlc_global_data_t *p_libvlc_global = p_filter->p_libvlc_global;
323     char *psz_order;
324     char *psz_offsets;
325     int i_index;
326     vlc_value_t val;
327
328     /* The mosaic thread is more important than the decoder threads */
329     vlc_thread_set_priority( p_this, VLC_THREAD_PRIORITY_OUTPUT );
330
331     /* Allocate structure */
332     p_sys = p_filter->p_sys = malloc( sizeof( filter_sys_t ) );
333     if( p_sys == NULL )
334     {
335         msg_Err( p_filter, "out of memory" );
336         return VLC_ENOMEM;
337     }
338
339     p_filter->pf_sub_filter = Filter;
340     p_sys->p_pic = NULL;
341
342     vlc_mutex_init( p_filter, &p_sys->lock );
343     vlc_mutex_lock( &p_sys->lock );
344
345     var_Get( p_libvlc_global, "mosaic-lock", &val );
346     p_sys->p_lock = val.p_address;
347
348     config_ChainParse( p_filter, CFG_PREFIX, ppsz_filter_options,
349                        p_filter->p_cfg );
350
351 #define GET_VAR( name, min, max )                                           \
352     p_sys->i_##name = __MIN( max, __MAX( min,                               \
353         var_CreateGetIntegerCommand( p_filter, CFG_PREFIX #name ) ) );      \
354     var_AddCallback( p_filter, CFG_PREFIX #name, MosaicCallback, p_sys );
355
356     GET_VAR( width, 0, INT_MAX );
357     GET_VAR( height, 0, INT_MAX );
358     GET_VAR( xoffset, 0, INT_MAX );
359     GET_VAR( yoffset, 0, INT_MAX );
360
361     GET_VAR( align, 0, 10 );
362     if( p_sys->i_align == 3 || p_sys->i_align == 7 )
363         p_sys->i_align = 5;
364
365     GET_VAR( borderw, 0, INT_MAX );
366     GET_VAR( borderh, 0, INT_MAX );
367     GET_VAR( rows, 1, INT_MAX );
368     GET_VAR( cols, 1, INT_MAX );
369     GET_VAR( alpha, 0, 255 );
370     GET_VAR( position, 0, 2 );
371     GET_VAR( delay, 100, INT_MAX );
372     p_sys->i_delay *= 1000;
373
374     p_sys->b_ar = var_CreateGetBoolCommand( p_filter,
375                                             CFG_PREFIX "keep-aspect-ratio" );
376     var_AddCallback( p_filter, CFG_PREFIX "keep-aspect-ratio", MosaicCallback,
377                      p_sys );
378
379     p_sys->b_keep = var_CreateGetBool( p_filter, CFG_PREFIX "keep-picture" );
380     if ( !p_sys->b_keep )
381     {
382         p_sys->p_image = image_HandlerCreate( p_filter );
383     }
384
385     p_sys->i_order_length = 0;
386     p_sys->ppsz_order = NULL;
387     psz_order = var_CreateGetStringCommand( p_filter, CFG_PREFIX "order" );
388     var_AddCallback( p_filter, CFG_PREFIX "order", MosaicCallback, p_sys );
389
390     if( psz_order[0] != 0 )
391     {
392         char *psz_end = NULL;
393         i_index = 0;
394         do
395         {
396             psz_end = strchr( psz_order, ',' );
397             i_index++;
398             p_sys->ppsz_order = realloc( p_sys->ppsz_order,
399                                          i_index * sizeof(char *) );
400             p_sys->ppsz_order[i_index - 1] = strndup( psz_order,
401                                            psz_end - psz_order );
402             psz_order = psz_end+1;
403         } while( NULL !=  psz_end );
404         p_sys->i_order_length = i_index;
405     }
406
407     /* Manage specific offsets for substreams */
408     psz_offsets = var_CreateGetStringCommand( p_filter, CFG_PREFIX "offsets" );
409     p_sys->i_offsets_length = 0;
410     p_sys->pi_x_offsets = NULL;
411     p_sys->pi_y_offsets = NULL;
412     mosaic_ParseSetOffsets( (vlc_object_t *) p_filter, p_sys, psz_offsets );
413     var_AddCallback( p_filter, CFG_PREFIX "offsets", MosaicCallback, p_sys );
414
415     /* Bluescreen specific stuff */
416     GET_VAR( bsu, 0x00, 0xff );
417     GET_VAR( bsv, 0x00, 0xff );
418     GET_VAR( bsut, 0x00, 0xff );
419     GET_VAR( bsvt, 0x00, 0xff );
420     p_sys->b_bs = var_CreateGetBoolCommand( p_filter, CFG_PREFIX "bs" );
421     var_AddCallback( p_filter, CFG_PREFIX "bs", MosaicCallback, p_sys );
422     if( p_sys->b_bs && p_sys->b_keep )
423     {
424         msg_Warn( p_filter, CFG_PREFIX "keep-picture needs to be disabled for"
425                             " bluescreen to work" );
426     }
427
428     vlc_mutex_unlock( &p_sys->lock );
429
430     return VLC_SUCCESS;
431 }
432
433 /*****************************************************************************
434  * DestroyFilter: destroy mosaic video filter
435  *****************************************************************************/
436 static void DestroyFilter( vlc_object_t *p_this )
437 {
438     filter_t *p_filter = (filter_t*)p_this;
439     filter_sys_t *p_sys = p_filter->p_sys;
440     libvlc_global_data_t *p_libvlc_global = p_filter->p_libvlc_global;
441     int i_index;
442
443     vlc_mutex_lock( &p_sys->lock );
444
445     if( !p_sys->b_keep )
446     {
447         image_HandlerDelete( p_sys->p_image );
448     }
449
450     if( p_sys->i_order_length )
451     {
452         for( i_index = 0; i_index < p_sys->i_order_length; i_index++ )
453         {
454             free( p_sys->ppsz_order[i_index] );
455         }
456         free( p_sys->ppsz_order );
457     }
458     if( p_sys->i_offsets_length )
459     {
460         free( p_sys->pi_x_offsets );
461         free( p_sys->pi_y_offsets );
462         p_sys->i_offsets_length = 0;
463     }
464     var_Destroy( p_libvlc_global, CFG_PREFIX "offsets" );
465     var_Destroy( p_libvlc_global, CFG_PREFIX "alpha" );
466     var_Destroy( p_libvlc_global, CFG_PREFIX "height" );
467     var_Destroy( p_libvlc_global, CFG_PREFIX "align" );
468     var_Destroy( p_libvlc_global, CFG_PREFIX "width" );
469     var_Destroy( p_libvlc_global, CFG_PREFIX "xoffset" );
470     var_Destroy( p_libvlc_global, CFG_PREFIX "yoffset" );
471     var_Destroy( p_libvlc_global, CFG_PREFIX "vborder" );
472     var_Destroy( p_libvlc_global, CFG_PREFIX "hborder" );
473     var_Destroy( p_libvlc_global, CFG_PREFIX "position" );
474     var_Destroy( p_libvlc_global, CFG_PREFIX "rows" );
475     var_Destroy( p_libvlc_global, CFG_PREFIX "cols" );
476     var_Destroy( p_libvlc_global, CFG_PREFIX "keep-aspect-ratio" );
477
478     var_Destroy( p_libvlc_global, CFG_PREFIX "bsu" );
479     var_Destroy( p_libvlc_global, CFG_PREFIX "bsv" );
480     var_Destroy( p_libvlc_global, CFG_PREFIX "bsut" );
481     var_Destroy( p_libvlc_global, CFG_PREFIX "bsvt" );
482     var_Destroy( p_libvlc_global, CFG_PREFIX "bs" );
483
484     if( p_sys->p_pic ) p_sys->p_pic->pf_release( p_sys->p_pic );
485     vlc_mutex_unlock( &p_sys->lock );
486     vlc_mutex_destroy( &p_sys->lock );
487     free( p_sys );
488 }
489
490 /*****************************************************************************
491  * MosaicReleasePicture : Hack to avoid picture duplication
492  *****************************************************************************/
493 static void MosaicReleasePicture( picture_t *p_picture )
494 {
495     picture_t *p_original_pic = (picture_t *)p_picture->p_sys;
496
497     p_original_pic->pf_release( p_original_pic );
498 }
499
500 /*****************************************************************************
501  * Filter
502  *****************************************************************************/
503 static subpicture_t *Filter( filter_t *p_filter, mtime_t date )
504 {
505     filter_sys_t *p_sys = p_filter->p_sys;
506     bridge_t *p_bridge;
507
508     subpicture_t *p_spu;
509
510     int i_index, i_real_index, i_row, i_col;
511     int i_greatest_real_index_used = p_sys->i_order_length - 1;
512
513     unsigned int col_inner_width, row_inner_height;
514
515     subpicture_region_t *p_region;
516     subpicture_region_t *p_region_prev = NULL;
517
518     /* Allocate the subpicture internal data. */
519     p_spu = p_filter->pf_sub_buffer_new( p_filter );
520     if( !p_spu )
521     {
522         return NULL;
523     }
524
525     /* Initialize subpicture */
526     p_spu->i_channel = 0;
527     p_spu->i_start  = date;
528     p_spu->i_stop = 0;
529     p_spu->b_ephemer = VLC_TRUE;
530     p_spu->i_alpha = p_sys->i_alpha;
531     p_spu->i_flags = p_sys->i_align;
532     p_spu->b_absolute = VLC_FALSE;
533
534     vlc_mutex_lock( &p_sys->lock );
535     vlc_mutex_lock( p_sys->p_lock );
536
537     p_bridge = GetBridge( p_filter );
538     if ( p_bridge == NULL )
539     {
540         vlc_mutex_unlock( p_sys->p_lock );
541         vlc_mutex_unlock( &p_sys->lock );
542         return p_spu;
543     }
544
545     if ( p_sys->i_position == 2 ) /* user-defined offsets for positioning */
546     {
547         /* If we have either too much or not enough offsets, fall-back
548          * to automatic positioning. */
549         if ( p_sys->i_offsets_length != p_sys->i_order_length )
550         {
551             msg_Err( p_filter, "Number of specified offsets (%d) does not match number of input substreams in mosaic-order (%d), falling back to mosaic-position=0", p_sys->i_offsets_length, p_sys->i_order_length );
552             p_sys->i_position = 0;
553         }
554     }
555
556     if ( p_sys->i_position == 0 ) /* use automatic positioning */
557     {
558         int i_numpics = p_sys->i_order_length; /* keep slots and all */
559         for ( i_index = 0; i_index < p_bridge->i_es_num; i_index++ )
560         {
561             bridged_es_t *p_es = p_bridge->pp_es[i_index];
562             if ( !p_es->b_empty )
563             {
564                 i_numpics ++;
565                 if( p_sys->i_order_length && p_es->psz_id != 0 )
566                 {
567                     /* We also want to leave slots for images given in
568                      * mosaic-order that are not available in p_vout_picture */
569                     int i;
570                     for( i = 0; i < p_sys->i_order_length ; i++ )
571                     {
572                         if( !strcmp( p_sys->ppsz_order[i], p_es->psz_id ) )
573                         {
574                             i_numpics--;
575                             break;
576                         }
577                     }
578
579                 }
580             }
581         }
582         p_sys->i_rows = ceil(sqrt( (double)i_numpics ));
583         p_sys->i_cols = ( i_numpics % p_sys->i_rows == 0 ?
584                             i_numpics / p_sys->i_rows :
585                             i_numpics / p_sys->i_rows + 1 );
586     }
587
588     col_inner_width  = ( ( p_sys->i_width - ( p_sys->i_cols - 1 )
589                        * p_sys->i_borderw ) / p_sys->i_cols );
590     row_inner_height = ( ( p_sys->i_height - ( p_sys->i_rows - 1 )
591                        * p_sys->i_borderh ) / p_sys->i_rows );
592
593     i_real_index = 0;
594
595     for ( i_index = 0; i_index < p_bridge->i_es_num; i_index++ )
596     {
597         bridged_es_t *p_es = p_bridge->pp_es[i_index];
598         video_format_t fmt_in, fmt_out;
599         picture_t *p_converted;
600
601         memset( &fmt_in, 0, sizeof( video_format_t ) );
602         memset( &fmt_out, 0, sizeof( video_format_t ) );
603
604         if ( p_es->b_empty )
605             continue;
606
607         while ( p_es->p_picture != NULL
608                  && p_es->p_picture->date + p_sys->i_delay < date )
609         {
610             if ( p_es->p_picture->p_next != NULL )
611             {
612                 picture_t *p_next = p_es->p_picture->p_next;
613                 p_es->p_picture->pf_release( p_es->p_picture );
614                 p_es->p_picture = p_next;
615             }
616             else if ( p_es->p_picture->date + p_sys->i_delay + BLANK_DELAY <
617                         date )
618             {
619                 /* Display blank */
620                 p_es->p_picture->pf_release( p_es->p_picture );
621                 p_es->p_picture = NULL;
622                 p_es->pp_last = &p_es->p_picture;
623                 break;
624             }
625             else
626             {
627                 msg_Dbg( p_filter, "too late picture for %s (" I64Fd ")",
628                          p_es->psz_id,
629                          date - p_es->p_picture->date - p_sys->i_delay );
630                 break;
631             }
632         }
633
634         if ( p_es->p_picture == NULL )
635             continue;
636
637         if ( p_sys->i_order_length == 0 )
638         {
639             i_real_index++;
640         }
641         else
642         {
643             int i;
644             for ( i = 0; i <= p_sys->i_order_length; i++ )
645             {
646                 if ( i == p_sys->i_order_length ) break;
647                 if ( strcmp( p_es->psz_id, p_sys->ppsz_order[i] ) == 0 )
648                 {
649                     i_real_index = i;
650                     break;
651                 }
652             }
653             if ( i == p_sys->i_order_length )
654                 i_real_index = ++i_greatest_real_index_used;
655         }
656         i_row = ( i_real_index / p_sys->i_cols ) % p_sys->i_rows;
657         i_col = i_real_index % p_sys->i_cols ;
658
659         if ( !p_sys->b_keep )
660         {
661             /* Convert the images */
662             fmt_in.i_chroma = p_es->p_picture->format.i_chroma;
663             fmt_in.i_height = p_es->p_picture->format.i_height;
664             fmt_in.i_width = p_es->p_picture->format.i_width;
665
666             if( p_sys->b_bs )
667                 fmt_out.i_chroma = VLC_FOURCC('Y','U','V','A');
668             else
669                 fmt_out.i_chroma = VLC_FOURCC('I','4','2','0');
670             fmt_out.i_width = col_inner_width;
671             fmt_out.i_height = row_inner_height;
672
673             if( p_sys->b_ar ) /* keep aspect ratio */
674             {
675                 if( (float)fmt_out.i_width / (float)fmt_out.i_height
676                       > (float)fmt_in.i_width / (float)fmt_in.i_height )
677                 {
678                     fmt_out.i_width = ( fmt_out.i_height * fmt_in.i_width )
679                                          / fmt_in.i_height;
680                 }
681                 else
682                 {
683                     fmt_out.i_height = ( fmt_out.i_width * fmt_in.i_height )
684                                         / fmt_in.i_width;
685                 }
686              }
687
688             fmt_out.i_visible_width = fmt_out.i_width;
689             fmt_out.i_visible_height = fmt_out.i_height;
690
691             p_converted = image_Convert( p_sys->p_image, p_es->p_picture,
692                                          &fmt_in, &fmt_out );
693             if( !p_converted )
694             {
695                 msg_Warn( p_filter,
696                            "image resizing and chroma conversion failed" );
697                 continue;
698             }
699
700             /* Bluescreen stuff */
701             if( p_sys->b_bs )
702             {
703                 int i,j;
704                 int i_lines = p_converted->p[ A_PLANE ].i_lines;
705                 int i_pitch = p_converted->p[ A_PLANE ].i_pitch;
706                 uint8_t *p_a = p_converted->p[ A_PLANE ].p_pixels;
707                 uint8_t *p_at = malloc( i_lines * i_pitch * sizeof( uint8_t ) );
708                 uint8_t *p_u = p_converted->p[ U_PLANE ].p_pixels;
709                 uint8_t *p_v = p_converted->p[ V_PLANE ].p_pixels;
710                 uint8_t umin, umax, vmin, vmax;
711                 umin = p_sys->i_bsu - p_sys->i_bsut >= 0x00 ?
712                        p_sys->i_bsu - p_sys->i_bsut : 0x00;
713                 umax = p_sys->i_bsu + p_sys->i_bsut <= 0xff ?
714                        p_sys->i_bsu + p_sys->i_bsut : 0xff;
715                 vmin = p_sys->i_bsv - p_sys->i_bsvt >= 0x00 ?
716                        p_sys->i_bsv - p_sys->i_bsvt : 0x00;
717                 vmax = p_sys->i_bsv + p_sys->i_bsvt <= 0xff ?
718                        p_sys->i_bsv + p_sys->i_bsvt : 0xff;
719
720                 for( i = 0; i < i_lines*i_pitch; i++ )
721                 {
722                     if(    p_u[i] < umax
723                         && p_u[i] > umin
724                         && p_v[i] < vmax
725                         && p_v[i] > vmin )
726                     {
727                         p_at[i] = 0x00;
728                     }
729                     else
730                     {
731                         p_at[i] = 0xff;
732                     }
733                 }
734                 /* Gaussian convolution to make it look cleaner */
735                 p_filter->p_libvlc->pf_memset( p_a, 0, 2 * i_pitch );
736                 for( i = 2; i < i_lines - 2; i++ )
737                 {
738                     p_a[i*i_pitch] = 0x00;
739                     p_a[i*i_pitch+1] = 0x00;
740                     for( j = 2; j < i_pitch - 2; j ++ )
741                     {
742                         p_a[i*i_pitch+j] = (uint8_t)((
743                           /* 2 rows up */
744                             ( p_at[(i-2)*i_pitch+j-2]<<1 )
745                           + ( p_at[(i-2)*i_pitch+j-1]<<2 )
746                           + ( p_at[(i-2)*i_pitch+j]<<2 )
747                           + ( p_at[(i-2)*i_pitch+j+1]<<2 )
748                           + ( p_at[(i-2)*i_pitch+j+2]<<1 )
749                           /* 1 row up */
750                           + ( p_at[(i-1)*i_pitch+j-1]<<3 )
751                           + ( p_at[(i-1)*i_pitch+j-2]<<2 )
752                           + ( p_at[(i-1)*i_pitch+j]*12 )
753                           + ( p_at[(i-1)*i_pitch+j+1]<<3 )
754                           + ( p_at[(i-1)*i_pitch+j+2]<<2 )
755                           /* */
756                           + ( p_at[i*i_pitch+j-2]<<2 )
757                           + ( p_at[i*i_pitch+j-1]*12 )
758                           + ( p_at[i*i_pitch+j]<<4 )
759                           + ( p_at[i*i_pitch+j+1]*12 )
760                           + ( p_at[i*i_pitch+j+2]<<2 )
761                           /* 1 row down */
762                           + ( p_at[(i+1)*i_pitch+j-2]<<2 )
763                           + ( p_at[(i+1)*i_pitch+j-1]<<3 )
764                           + ( p_at[(i+1)*i_pitch+j]*12 )
765                           + ( p_at[(i+1)*i_pitch+j+1]<<3 )
766                           + ( p_at[(i+1)*i_pitch+j+2]<<2 )
767                           /* 2 rows down */
768                           + ( p_at[(i+2)*i_pitch+j-2]<<1 )
769                           + ( p_at[(i+2)*i_pitch+j-1]<<2 )
770                           + ( p_at[(i+2)*i_pitch+j]<<2 )
771                           + ( p_at[(i+2)*i_pitch+j+1]<<2 )
772                           + ( p_at[(i+2)*i_pitch+j+2]<<1 )
773                           )/152);
774                           if( p_a[i*i_pitch+j] < 0xbf ) p_a[i*i_pitch+j] = 0x00;
775                     }
776                 }
777                 free( p_at );
778             }
779         }
780         else
781         {
782             p_converted = p_es->p_picture;
783             p_converted->i_refcount++;
784             fmt_in.i_width = fmt_out.i_width = p_converted->format.i_width;
785             fmt_in.i_height = fmt_out.i_height = p_converted->format.i_height;
786             fmt_in.i_chroma = fmt_out.i_chroma = p_converted->format.i_chroma;
787             fmt_out.i_visible_width = fmt_out.i_width;
788             fmt_out.i_visible_height = fmt_out.i_height;
789         }
790
791         p_region = p_spu->pf_make_region( VLC_OBJECT(p_filter), &fmt_out,
792                                           p_converted );
793         if( !p_region )
794         {
795             msg_Err( p_filter, "cannot allocate SPU region" );
796             p_filter->pf_sub_buffer_del( p_filter, p_spu );
797             vlc_mutex_unlock( &p_sys->lock );
798             vlc_mutex_unlock( p_sys->p_lock );
799             return p_spu;
800         }
801
802         /* HACK ALERT : let's fix the pointers to avoid picture duplication.
803          * This is necessary because p_region->picture is not a pointer
804          * as it ought to be. */
805         if( !p_sys->b_keep )
806         {
807             free( p_converted );
808         }
809         else
810         {
811             /* Keep a pointer to the original picture (and its refcount...). */
812             p_region->picture.p_sys = (picture_sys_t *)p_converted;
813             p_region->picture.pf_release = MosaicReleasePicture;
814         }
815
816         if( p_sys->i_position == 2 ) /* user-defined offset */
817         {
818             p_region->i_x = p_sys->pi_x_offsets[i_real_index];
819         }
820         else if( fmt_out.i_width > col_inner_width ||
821             p_sys->b_ar || p_sys->b_keep )
822         {
823             /* we don't have to center the video since it takes the
824             whole rectangle area or it's larger than the rectangle */
825             p_region->i_x = p_sys->i_xoffset
826                         + i_col * ( p_sys->i_width / p_sys->i_cols )
827                         + ( i_col * p_sys->i_borderw ) / p_sys->i_cols;
828         }
829         else
830         {
831             /* center the video in the dedicated rectangle */
832             p_region->i_x = p_sys->i_xoffset
833                     + i_col * ( p_sys->i_width / p_sys->i_cols )
834                     + ( i_col * p_sys->i_borderw ) / p_sys->i_cols
835                     + ( col_inner_width - fmt_out.i_width ) / 2;
836         }
837
838         if( p_sys->i_position == 2 ) /* user-defined offset */
839         {
840             p_region->i_y = p_sys->pi_y_offsets[i_real_index];
841         }
842         else if( fmt_out.i_height < row_inner_height
843             || p_sys->b_ar || p_sys->b_keep )
844         {
845             /* we don't have to center the video since it takes the
846             whole rectangle area or it's taller than the rectangle */
847             p_region->i_y = p_sys->i_yoffset
848                     + i_row * ( p_sys->i_height / p_sys->i_rows )
849                     + ( i_row * p_sys->i_borderh ) / p_sys->i_rows;
850         }
851         else
852         {
853             /* center the video in the dedicated rectangle */
854             p_region->i_y = p_sys->i_yoffset
855                     + i_row * ( p_sys->i_height / p_sys->i_rows )
856                     + ( i_row * p_sys->i_borderh ) / p_sys->i_rows
857                     + ( row_inner_height - fmt_out.i_height ) / 2;
858         }
859
860         if( p_region_prev == NULL )
861         {
862             p_spu->p_region = p_region;
863         }
864         else
865         {
866             p_region_prev->p_next = p_region;
867         }
868
869         p_region_prev = p_region;
870     }
871
872     vlc_mutex_unlock( p_sys->p_lock );
873     vlc_mutex_unlock( &p_sys->lock );
874
875     return p_spu;
876 }
877
878 /*****************************************************************************
879 * Callback to update params on the fly
880 *****************************************************************************/
881 static int MosaicCallback( vlc_object_t *p_this, char const *psz_var,
882                             vlc_value_t oldval, vlc_value_t newval,
883                             void *p_data )
884 {
885     filter_sys_t *p_sys = (filter_sys_t *) p_data;
886
887 #define VAR_IS( a ) !strcmp( psz_var, CFG_PREFIX a )
888     if( VAR_IS( "alpha" ) )
889     {
890         vlc_mutex_lock( &p_sys->lock );
891         msg_Dbg( p_this, "changing alpha from %d/255 to %d/255",
892                          p_sys->i_alpha, newval.i_int);
893         p_sys->i_alpha = __MIN( __MAX( newval.i_int, 0 ), 255 );
894         vlc_mutex_unlock( &p_sys->lock );
895     }
896     else if( VAR_IS( "height" ) )
897     {
898         vlc_mutex_lock( &p_sys->lock );
899         msg_Dbg( p_this, "changing height from %dpx to %dpx",
900                           p_sys->i_height, newval.i_int );
901         p_sys->i_height = __MAX( newval.i_int, 0 );
902         vlc_mutex_unlock( &p_sys->lock );
903     }
904     else if( VAR_IS( "width" ) )
905     {
906         vlc_mutex_lock( &p_sys->lock );
907         msg_Dbg( p_this, "changing width from %dpx to %dpx",
908                          p_sys->i_width, newval.i_int );
909         p_sys->i_width = __MAX( newval.i_int, 0 );
910         vlc_mutex_unlock( &p_sys->lock );
911     }
912     else if( VAR_IS( "xoffset" ) )
913     {
914         vlc_mutex_lock( &p_sys->lock );
915         msg_Dbg( p_this, "changing x offset from %dpx to %dpx",
916                          p_sys->i_xoffset, newval.i_int );
917         p_sys->i_xoffset = __MAX( newval.i_int, 0 );
918         vlc_mutex_unlock( &p_sys->lock );
919     }
920     else if( VAR_IS( "yoffset" ) )
921     {
922         vlc_mutex_lock( &p_sys->lock );
923         msg_Dbg( p_this, "changing y offset from %dpx to %dpx",
924                          p_sys->i_yoffset, newval.i_int );
925         p_sys->i_yoffset = __MAX( newval.i_int, 0 );
926         vlc_mutex_unlock( &p_sys->lock );
927     }
928     else if( VAR_IS( "align" ) )
929     {
930         int i_old = 0, i_new = 0;
931         vlc_mutex_lock( &p_sys->lock );
932         newval.i_int = __MIN( __MAX( newval.i_int, 0 ), 10 );
933         if( newval.i_int == 3 || newval.i_int == 7 )
934             newval.i_int = 5;
935         while( pi_align_values[i_old] != p_sys->i_align ) i_old++;
936         while( pi_align_values[i_new] != newval.i_int ) i_new++;
937         msg_Dbg( p_this, "changing alignment from %d (%s) to %d (%s)",
938                      p_sys->i_align, ppsz_align_descriptions[i_old],
939                      newval.i_int, ppsz_align_descriptions[i_new] );
940         p_sys->i_align = newval.i_int;
941         vlc_mutex_unlock( &p_sys->lock );
942     }
943     else if( VAR_IS( "borderw" ) )
944     {
945         vlc_mutex_lock( &p_sys->lock );
946         msg_Dbg( p_this, "changing border width from %dpx to %dpx",
947                          p_sys->i_borderw, newval.i_int );
948         p_sys->i_borderw = __MAX( newval.i_int, 0 );
949         vlc_mutex_unlock( &p_sys->lock );
950     }
951     else if( VAR_IS( "borderh" ) )
952     {
953         vlc_mutex_lock( &p_sys->lock );
954         msg_Dbg( p_this, "changing border height from %dpx to %dpx",
955                          p_sys->i_borderh, newval.i_int );
956         p_sys->i_borderh = __MAX( newval.i_int, 0 );
957         vlc_mutex_unlock( &p_sys->lock );
958     }
959     else if( VAR_IS( "position" ) )
960     {
961         if( newval.i_int > 1 || newval.i_int < 0 )
962         {
963             msg_Err( p_this, "Position is either 0 (auto) or 1 (fixed)" );
964         }
965         else
966         {
967             vlc_mutex_lock( &p_sys->lock );
968             msg_Dbg( p_this, "changing position method from %d (%s) to %d (%s)",
969                     p_sys->i_position, ppsz_pos_descriptions[p_sys->i_position],
970                     newval.i_int, ppsz_pos_descriptions[newval.i_int]);
971             p_sys->i_position = newval.i_int;
972             vlc_mutex_unlock( &p_sys->lock );
973         }
974     }
975     else if( VAR_IS( "rows" ) )
976     {
977         vlc_mutex_lock( &p_sys->lock );
978         msg_Dbg( p_this, "changing number of rows from %d to %d",
979                          p_sys->i_rows, newval.i_int );
980         p_sys->i_rows = __MAX( newval.i_int, 1 );
981         vlc_mutex_unlock( &p_sys->lock );
982     }
983     else if( VAR_IS( "cols" ) )
984     {
985         vlc_mutex_lock( &p_sys->lock );
986         msg_Dbg( p_this, "changing number of columns from %d to %d",
987                          p_sys->i_cols, newval.i_int );
988         p_sys->i_cols = __MAX( newval.i_int, 1 );
989         vlc_mutex_unlock( &p_sys->lock );
990     }
991     else if( VAR_IS( "order" ) )
992     {
993         char *psz_order;
994         int i_index;
995         vlc_mutex_lock( &p_sys->lock );
996         msg_Dbg( p_this, "Changing mosaic order to %s", newval.psz_string );
997
998         p_sys->i_order_length = 0;
999         p_sys->ppsz_order = NULL;
1000         psz_order = newval.psz_string;
1001
1002         while( p_sys->i_order_length-- )
1003         {
1004 #if 0
1005             printf("%d\n", p_sys->ppsz_order);
1006 #endif
1007             free( p_sys->ppsz_order );
1008         }
1009         if( psz_order[0] != 0 )
1010         {
1011             char *psz_end = NULL;
1012             i_index = 0;
1013             do
1014             {
1015                 psz_end = strchr( psz_order, ',' );
1016                 i_index++;
1017                 p_sys->ppsz_order = realloc( p_sys->ppsz_order,
1018                                     i_index * sizeof(char *) );
1019                 p_sys->ppsz_order[i_index - 1] = strndup( psz_order,
1020                                            psz_end - psz_order );
1021                 psz_order = psz_end+1;
1022             } while( NULL !=  psz_end );
1023             p_sys->i_order_length = i_index;
1024         }
1025
1026         vlc_mutex_unlock( &p_sys->lock );
1027     }
1028     else if( VAR_IS( "offsets" ) )
1029     {
1030         vlc_mutex_lock( &p_sys->lock );
1031         msg_Info( p_this, "Changing mosaic-offsets to %s", newval.psz_string );
1032
1033         if( p_sys->i_offsets_length != 0 )
1034         {
1035             free( p_sys->pi_x_offsets );
1036             free( p_sys->pi_y_offsets );
1037         }
1038         p_sys->i_offsets_length = 0;
1039
1040         mosaic_ParseSetOffsets( (vlc_object_t *) p_this, p_sys, newval.psz_string );
1041
1042         vlc_mutex_unlock( &p_sys->lock );
1043     }
1044     else if( VAR_IS( "keep-aspect-ratio" ) )
1045     {
1046         vlc_mutex_lock( &p_sys->lock );
1047         if( newval.i_int )
1048         {
1049             msg_Dbg( p_this, "keeping aspect ratio" );
1050             p_sys->b_ar = 1;
1051         }
1052         else
1053         {
1054             msg_Dbg( p_this, "won't keep aspect ratio" );
1055             p_sys->b_ar = 0;
1056         }
1057         vlc_mutex_unlock( &p_sys->lock );
1058     }
1059     else if( VAR_IS( "bs" ) )
1060     {
1061         vlc_mutex_lock( &p_sys->lock );
1062         p_sys->b_bs = newval.b_bool;
1063         vlc_mutex_unlock( &p_sys->lock );
1064     }
1065     else if( VAR_IS( "bsu" ) )
1066     {
1067         vlc_mutex_lock( &p_sys->lock );
1068         p_sys->i_bsu = __MAX( 0, __MIN( 255, newval.i_int ) );
1069         vlc_mutex_unlock( &p_sys->lock );
1070     }
1071     else if( VAR_IS( "bsv" ) )
1072     {
1073         vlc_mutex_lock( &p_sys->lock );
1074         p_sys->i_bsv = __MAX( 0, __MIN( 255, newval.i_int ) );
1075         vlc_mutex_unlock( &p_sys->lock );
1076     }
1077     else if( VAR_IS( "bsut" ) )
1078     {
1079         vlc_mutex_lock( &p_sys->lock );
1080         p_sys->i_bsut = __MAX( 0, __MIN( 255, newval.i_int ) );
1081         vlc_mutex_unlock( &p_sys->lock );
1082     }
1083     else if( VAR_IS( "bsvt" ) )
1084     {
1085         vlc_mutex_lock( &p_sys->lock );
1086         p_sys->i_bsvt = __MAX( 0, __MIN( 255, newval.i_int ) );
1087         vlc_mutex_unlock( &p_sys->lock );
1088     }
1089     return VLC_SUCCESS;
1090 }