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