1 /*****************************************************************************
2 * mosaic.c : Mosaic video plugin for vlc
3 *****************************************************************************
4 * Copyright (C) 2004-2005 the VideoLAN team
7 * Authors: Antoine Cellerier <dionoea@via.ecp.fr>
8 * Christophe Massiot <massiot@via.ecp.fr>
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.
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.
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 *****************************************************************************/
25 /*****************************************************************************
27 *****************************************************************************/
29 #include <stdlib.h> /* malloc(), free() */
37 # include <limits.h> /* INT_MAX */
40 #include "vlc_filter.h"
41 #include "vlc_image.h"
45 #define BLANK_DELAY I64C(1000000)
47 /*****************************************************************************
49 *****************************************************************************/
50 static int CreateFilter ( vlc_object_t * );
51 static void DestroyFilter ( vlc_object_t * );
53 static subpicture_t *Filter( filter_t *, mtime_t );
55 static int MosaicCallback( vlc_object_t *, char const *, vlc_value_t,
56 vlc_value_t, void * );
58 /*****************************************************************************
59 * filter_sys_t : filter descriptor
60 *****************************************************************************/
66 image_handler_t *p_image;
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 */
79 vlc_bool_t b_bs; /* Bluescreen vars */
80 int i_bsu, i_bsv, i_bsut, i_bsvt;
82 char **ppsz_order; /* list of picture-id */
85 int *pi_x_offsets; /* list of substreams x offsets */
86 int *pi_y_offsets; /* list of substreams y offsets */
92 /*****************************************************************************
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)." )
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." )
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.")
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." )
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).")
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." )
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\")." )
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\"." )
142 #define AR_TEXT N_("Keep aspect ratio")
143 #define AR_LONGTEXT N_( \
144 "Keep the original aspect ratio when resizing " \
146 #define KEEP_TEXT N_("Keep original size")
147 #define KEEP_LONGTEXT N_( \
148 "Keep the original size of mosaic elements." )
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." )
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)." )
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.")
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)." )
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 " \
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 " \
194 static int pi_pos_values[] = { 0, 1, 2 };
195 static const char * ppsz_pos_descriptions[] =
196 { N_("auto"), N_("fixed"), N_("offsets") };
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") };
203 #define CFG_PREFIX "mosaic-"
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 );
213 add_integer( CFG_PREFIX "alpha", 255, NULL,
214 ALPHA_TEXT, ALPHA_LONGTEXT, VLC_FALSE );
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 );
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 );
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 );
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 );
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 );
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 );
250 add_string( CFG_PREFIX "order", "", NULL,
251 ORDER_TEXT, ORDER_LONGTEXT, VLC_FALSE );
253 add_string( CFG_PREFIX "offsets", "", NULL,
254 OFFSETS_TEXT, OFFSETS_LONGTEXT, VLC_FALSE );
256 add_integer( CFG_PREFIX "delay", 0, NULL, DELAY_TEXT, DELAY_LONGTEXT,
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,
269 add_integer_with_range( CFG_PREFIX "bsvt", 17, 0, 255, NULL,
270 BLUESCREENVTOL_TEXT, BLUESCREENVTOL_LONGTEXT,
273 var_Create( p_module->p_libvlc_global, "mosaic-lock", VLC_VAR_MUTEX );
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
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 )
290 if( psz_offsets[0] != 0 )
292 char *psz_end = NULL;
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;
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;
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] );
310 } while( NULL != psz_end );
311 p_sys->i_offsets_length = i_index;
315 /*****************************************************************************
316 * CreateFiler: allocate mosaic video filter
317 *****************************************************************************/
318 static int CreateFilter( vlc_object_t *p_this )
320 filter_t *p_filter = (filter_t *)p_this;
322 libvlc_global_data_t *p_libvlc_global = p_filter->p_libvlc_global;
328 /* The mosaic thread is more important than the decoder threads */
329 vlc_thread_set_priority( p_this, VLC_THREAD_PRIORITY_OUTPUT );
331 /* Allocate structure */
332 p_sys = p_filter->p_sys = malloc( sizeof( filter_sys_t ) );
335 msg_Err( p_filter, "out of memory" );
339 p_filter->pf_sub_filter = Filter;
342 vlc_mutex_init( p_filter, &p_sys->lock );
343 vlc_mutex_lock( &p_sys->lock );
345 var_Get( p_libvlc_global, "mosaic-lock", &val );
346 p_sys->p_lock = val.p_address;
348 config_ChainParse( p_filter, CFG_PREFIX, ppsz_filter_options,
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 );
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 );
361 GET_VAR( align, 0, 10 );
362 if( p_sys->i_align == 3 || p_sys->i_align == 7 )
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;
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,
379 p_sys->b_keep = var_CreateGetBool( p_filter, CFG_PREFIX "keep-picture" );
380 if ( !p_sys->b_keep )
382 p_sys->p_image = image_HandlerCreate( p_filter );
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 );
390 if( psz_order[0] != 0 )
392 char *psz_end = NULL;
396 psz_end = strchr( psz_order, ',' );
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;
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 );
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 )
424 msg_Warn( p_filter, CFG_PREFIX "keep-picture needs to be disabled for"
425 " bluescreen to work" );
428 vlc_mutex_unlock( &p_sys->lock );
433 /*****************************************************************************
434 * DestroyFilter: destroy mosaic video filter
435 *****************************************************************************/
436 static void DestroyFilter( vlc_object_t *p_this )
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;
443 vlc_mutex_lock( &p_sys->lock );
447 image_HandlerDelete( p_sys->p_image );
450 if( p_sys->i_order_length )
452 for( i_index = 0; i_index < p_sys->i_order_length; i_index++ )
454 free( p_sys->ppsz_order[i_index] );
456 free( p_sys->ppsz_order );
458 if( p_sys->i_offsets_length )
460 free( p_sys->pi_x_offsets );
461 free( p_sys->pi_y_offsets );
462 p_sys->i_offsets_length = 0;
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" );
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" );
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 );
490 /*****************************************************************************
491 * MosaicReleasePicture : Hack to avoid picture duplication
492 *****************************************************************************/
493 static void MosaicReleasePicture( picture_t *p_picture )
495 picture_t *p_original_pic = (picture_t *)p_picture->p_sys;
497 p_original_pic->pf_release( p_original_pic );
500 /*****************************************************************************
502 *****************************************************************************/
503 static subpicture_t *Filter( filter_t *p_filter, mtime_t date )
505 filter_sys_t *p_sys = p_filter->p_sys;
510 int i_index, i_real_index, i_row, i_col;
511 int i_greatest_real_index_used = p_sys->i_order_length - 1;
513 unsigned int col_inner_width, row_inner_height;
515 subpicture_region_t *p_region;
516 subpicture_region_t *p_region_prev = NULL;
518 /* Allocate the subpicture internal data. */
519 p_spu = p_filter->pf_sub_buffer_new( p_filter );
525 /* Initialize subpicture */
526 p_spu->i_channel = 0;
527 p_spu->i_start = date;
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;
534 vlc_mutex_lock( &p_sys->lock );
535 vlc_mutex_lock( p_sys->p_lock );
537 p_bridge = GetBridge( p_filter );
538 if ( p_bridge == NULL )
540 vlc_mutex_unlock( p_sys->p_lock );
541 vlc_mutex_unlock( &p_sys->lock );
545 if ( p_sys->i_position == 2 ) /* user-defined offsets for positioning */
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 )
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;
556 if ( p_sys->i_position == 0 ) /* use automatic positioning */
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++ )
561 bridged_es_t *p_es = p_bridge->pp_es[i_index];
562 if ( !p_es->b_empty )
565 if( p_sys->i_order_length && p_es->psz_id != 0 )
567 /* We also want to leave slots for images given in
568 * mosaic-order that are not available in p_vout_picture */
570 for( i = 0; i < p_sys->i_order_length ; i++ )
572 if( !strcmp( p_sys->ppsz_order[i], p_es->psz_id ) )
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 );
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 );
595 for ( i_index = 0; i_index < p_bridge->i_es_num; i_index++ )
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;
601 memset( &fmt_in, 0, sizeof( video_format_t ) );
602 memset( &fmt_out, 0, sizeof( video_format_t ) );
607 while ( p_es->p_picture != NULL
608 && p_es->p_picture->date + p_sys->i_delay < date )
610 if ( p_es->p_picture->p_next != NULL )
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;
616 else if ( p_es->p_picture->date + p_sys->i_delay + BLANK_DELAY <
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;
627 msg_Dbg( p_filter, "too late picture for %s (" I64Fd ")",
629 date - p_es->p_picture->date - p_sys->i_delay );
634 if ( p_es->p_picture == NULL )
637 if ( p_sys->i_order_length == 0 )
644 for ( i = 0; i <= p_sys->i_order_length; i++ )
646 if ( i == p_sys->i_order_length ) break;
647 if ( strcmp( p_es->psz_id, p_sys->ppsz_order[i] ) == 0 )
653 if ( i == p_sys->i_order_length )
654 i_real_index = ++i_greatest_real_index_used;
656 i_row = ( i_real_index / p_sys->i_cols ) % p_sys->i_rows;
657 i_col = i_real_index % p_sys->i_cols ;
659 if ( !p_sys->b_keep )
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;
667 fmt_out.i_chroma = VLC_FOURCC('Y','U','V','A');
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;
673 if( p_sys->b_ar ) /* keep aspect ratio */
675 if( (float)fmt_out.i_width / (float)fmt_out.i_height
676 > (float)fmt_in.i_width / (float)fmt_in.i_height )
678 fmt_out.i_width = ( fmt_out.i_height * fmt_in.i_width )
683 fmt_out.i_height = ( fmt_out.i_width * fmt_in.i_height )
688 fmt_out.i_visible_width = fmt_out.i_width;
689 fmt_out.i_visible_height = fmt_out.i_height;
691 p_converted = image_Convert( p_sys->p_image, p_es->p_picture,
696 "image resizing and chroma conversion failed" );
700 /* Bluescreen stuff */
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;
720 for( i = 0; i < i_lines*i_pitch; i++ )
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++ )
738 p_a[i*i_pitch] = 0x00;
739 p_a[i*i_pitch+1] = 0x00;
740 for( j = 2; j < i_pitch - 2; j ++ )
742 p_a[i*i_pitch+j] = (uint8_t)((
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 )
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 )
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 )
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 )
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 )
774 if( p_a[i*i_pitch+j] < 0xbf ) p_a[i*i_pitch+j] = 0x00;
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;
791 p_region = p_spu->pf_make_region( VLC_OBJECT(p_filter), &fmt_out,
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 );
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. */
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;
816 if( p_sys->i_position == 2 ) /* user-defined offset */
818 p_region->i_x = p_sys->pi_x_offsets[i_real_index];
820 else if( fmt_out.i_width > col_inner_width ||
821 p_sys->b_ar || p_sys->b_keep )
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;
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;
838 if( p_sys->i_position == 2 ) /* user-defined offset */
840 p_region->i_y = p_sys->pi_y_offsets[i_real_index];
842 else if( fmt_out.i_height < row_inner_height
843 || p_sys->b_ar || p_sys->b_keep )
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;
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;
860 if( p_region_prev == NULL )
862 p_spu->p_region = p_region;
866 p_region_prev->p_next = p_region;
869 p_region_prev = p_region;
872 vlc_mutex_unlock( p_sys->p_lock );
873 vlc_mutex_unlock( &p_sys->lock );
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,
885 filter_sys_t *p_sys = (filter_sys_t *) p_data;
887 #define VAR_IS( a ) !strcmp( psz_var, CFG_PREFIX a )
888 if( VAR_IS( "alpha" ) )
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 );
896 else if( VAR_IS( "height" ) )
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 );
904 else if( VAR_IS( "width" ) )
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 );
912 else if( VAR_IS( "xoffset" ) )
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 );
920 else if( VAR_IS( "yoffset" ) )
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 );
928 else if( VAR_IS( "align" ) )
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 )
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 );
943 else if( VAR_IS( "borderw" ) )
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 );
951 else if( VAR_IS( "borderh" ) )
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 );
959 else if( VAR_IS( "position" ) )
961 if( newval.i_int > 1 || newval.i_int < 0 )
963 msg_Err( p_this, "Position is either 0 (auto) or 1 (fixed)" );
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 );
975 else if( VAR_IS( "rows" ) )
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 );
983 else if( VAR_IS( "cols" ) )
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 );
991 else if( VAR_IS( "order" ) )
995 vlc_mutex_lock( &p_sys->lock );
996 msg_Dbg( p_this, "Changing mosaic order to %s", newval.psz_string );
998 p_sys->i_order_length = 0;
999 p_sys->ppsz_order = NULL;
1000 psz_order = newval.psz_string;
1002 while( p_sys->i_order_length-- )
1005 printf("%d\n", p_sys->ppsz_order);
1007 free( p_sys->ppsz_order );
1009 if( psz_order[0] != 0 )
1011 char *psz_end = NULL;
1015 psz_end = strchr( psz_order, ',' );
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;
1026 vlc_mutex_unlock( &p_sys->lock );
1028 else if( VAR_IS( "offsets" ) )
1030 vlc_mutex_lock( &p_sys->lock );
1031 msg_Info( p_this, "Changing mosaic-offsets to %s", newval.psz_string );
1033 if( p_sys->i_offsets_length != 0 )
1035 free( p_sys->pi_x_offsets );
1036 free( p_sys->pi_y_offsets );
1038 p_sys->i_offsets_length = 0;
1040 mosaic_ParseSetOffsets( (vlc_object_t *) p_this, p_sys, newval.psz_string );
1042 vlc_mutex_unlock( &p_sys->lock );
1044 else if( VAR_IS( "keep-aspect-ratio" ) )
1046 vlc_mutex_lock( &p_sys->lock );
1049 msg_Dbg( p_this, "keeping aspect ratio" );
1054 msg_Dbg( p_this, "won't keep aspect ratio" );
1057 vlc_mutex_unlock( &p_sys->lock );
1059 else if( VAR_IS( "bs" ) )
1061 vlc_mutex_lock( &p_sys->lock );
1062 p_sys->b_bs = newval.b_bool;
1063 vlc_mutex_unlock( &p_sys->lock );
1065 else if( VAR_IS( "bsu" ) )
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 );
1071 else if( VAR_IS( "bsv" ) )
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 );
1077 else if( VAR_IS( "bsut" ) )
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 );
1083 else if( VAR_IS( "bsvt" ) )
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 );