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