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 *****************************************************************************/
28 #include <stdlib.h> /* malloc(), free() */
36 # include <limits.h> /* INT_MAX */
39 #include "vlc_filter.h"
40 #include "vlc_image.h"
44 #define BLANK_DELAY I64C(1000000)
46 /*****************************************************************************
48 *****************************************************************************/
49 static int CreateFilter ( vlc_object_t * );
50 static void DestroyFilter ( vlc_object_t * );
52 static subpicture_t *Filter( filter_t *, mtime_t );
54 static int MosaicCallback( vlc_object_t *, char const *, vlc_value_t,
55 vlc_value_t, void * );
57 /*****************************************************************************
58 * filter_sys_t : filter descriptor
59 *****************************************************************************/
65 image_handler_t *p_image;
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 */
78 vlc_bool_t b_bs; /* Bluescreen vars */
79 int i_bsu, i_bsv, i_bsut, i_bsvt;
81 char **ppsz_order; /* list of picture-id */
84 int *pi_x_offsets; /* list of substreams x offsets */
85 int *pi_y_offsets; /* list of substreams y offsets */
91 /*****************************************************************************
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)." )
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." )
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." )
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).")
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." )
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\"." )
134 #define AR_TEXT N_("Keep aspect ratio")
135 #define AR_LONGTEXT N_("Keep the original aspect ratio when resizing " \
137 #define KEEP_TEXT N_("Keep original size")
138 #define KEEP_LONGTEXT N_("Keep the original size of mosaic elements." )
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." )
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)." )
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.")
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)." )
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 " \
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 " \
176 static int pi_pos_values[] = { 0, 1, 2 };
177 static char * ppsz_pos_descriptions[] =
178 { N_("auto"), N_("fixed"), N_("offsets") };
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") };
187 set_description( N_("Mosaic video sub filter") );
188 set_shortname( N_("Mosaic") );
189 set_category( CAT_VIDEO );
190 set_subcategory( SUBCAT_VIDEO_SUBPIC);
191 set_capability( "sub filter", 0 );
192 set_callbacks( CreateFilter, DestroyFilter );
194 add_integer( "mosaic-alpha", 255, NULL, ALPHA_TEXT, ALPHA_LONGTEXT, VLC_FALSE );
195 add_integer( "mosaic-height", 100, NULL, HEIGHT_TEXT, HEIGHT_LONGTEXT, VLC_FALSE );
196 add_integer( "mosaic-width", 100, NULL, WIDTH_TEXT, WIDTH_LONGTEXT, VLC_FALSE );
197 add_integer( "mosaic-align", 5, NULL, ALIGN_TEXT, ALIGN_LONGTEXT, VLC_TRUE);
198 change_integer_list( pi_align_values, ppsz_align_descriptions, 0 );
199 add_integer( "mosaic-xoffset", 0, NULL, XOFFSET_TEXT, XOFFSET_LONGTEXT, VLC_TRUE );
200 add_integer( "mosaic-yoffset", 0, NULL, YOFFSET_TEXT, YOFFSET_LONGTEXT, VLC_TRUE );
201 add_integer( "mosaic-vborder", 0, NULL, VBORDER_TEXT, VBORDER_LONGTEXT, VLC_TRUE );
202 add_integer( "mosaic-hborder", 0, NULL, HBORDER_TEXT, HBORDER_LONGTEXT, VLC_TRUE );
204 add_integer( "mosaic-position", 0, NULL, POS_TEXT, POS_LONGTEXT, VLC_FALSE );
205 change_integer_list( pi_pos_values, ppsz_pos_descriptions, 0 );
206 add_integer( "mosaic-rows", 2, NULL, ROWS_TEXT, ROWS_LONGTEXT, VLC_FALSE );
207 add_integer( "mosaic-cols", 2, NULL, COLS_TEXT, COLS_LONGTEXT, VLC_FALSE );
208 add_bool( "mosaic-keep-aspect-ratio", 0, NULL, AR_TEXT, AR_LONGTEXT, VLC_FALSE );
209 add_bool( "mosaic-keep-picture", 0, NULL, KEEP_TEXT, KEEP_LONGTEXT, VLC_FALSE );
210 add_string( "mosaic-order", "", NULL, ORDER_TEXT, ORDER_LONGTEXT, VLC_FALSE );
211 add_string( "mosaic-offsets", "", NULL, OFFSETS_TEXT, OFFSETS_LONGTEXT, VLC_FALSE );
213 add_integer( "mosaic-delay", 0, NULL, DELAY_TEXT, DELAY_LONGTEXT,
216 add_bool( "mosaic-bs", 0, NULL, BLUESCREEN_TEXT,
217 BLUESCREEN_LONGTEXT, VLC_FALSE );
218 add_integer( "mosaic-bsu", 120, NULL, BLUESCREENU_TEXT,
219 BLUESCREENU_LONGTEXT, VLC_FALSE );
220 add_integer( "mosaic-bsv", 90, NULL, BLUESCREENV_TEXT,
221 BLUESCREENV_LONGTEXT, VLC_FALSE );
222 add_integer( "mosaic-bsut", 17, NULL, BLUESCREENUTOL_TEXT,
223 BLUESCREENUTOL_LONGTEXT, VLC_FALSE );
224 add_integer( "mosaic-bsvt", 17, NULL, BLUESCREENVTOL_TEXT,
225 BLUESCREENVTOL_LONGTEXT, VLC_FALSE );
227 var_Create( p_module->p_libvlc_global, "mosaic-lock", VLC_VAR_MUTEX );
231 /*****************************************************************************
232 * mosaic_ParseSetOffsets:
233 * parse the "--mosaic-offsets x1,y1,x2,y2,x3,y3" parameter
234 * and set the corresponding struct filter_sys_t entries.
235 *****************************************************************************/
236 void mosaic_ParseSetOffsets( vlc_object_t *p_this, filter_sys_t *p_sys, char *psz_offsets )
238 if( psz_offsets[0] != 0 )
240 char *psz_end = NULL;
246 p_sys->pi_x_offsets = realloc( p_sys->pi_x_offsets, i_index * sizeof(int) );
247 p_sys->pi_x_offsets[i_index - 1] = atoi( psz_offsets );
248 psz_end = strchr( psz_offsets, ',' );
249 psz_offsets = psz_end + 1;
251 p_sys->pi_y_offsets = realloc( p_sys->pi_y_offsets, i_index * sizeof(int) );
252 p_sys->pi_y_offsets[i_index - 1] = atoi( psz_offsets );
253 psz_end = strchr( psz_offsets, ',' );
254 psz_offsets = psz_end + 1;
256 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] );
258 } while( NULL != psz_end );
259 p_sys->i_offsets_length = i_index;
263 /*****************************************************************************
264 * CreateFiler: allocate mosaic video filter
265 *****************************************************************************/
266 static int CreateFilter( vlc_object_t *p_this )
268 filter_t *p_filter = (filter_t *)p_this;
270 libvlc_global_data_t *p_libvlc_global = p_filter->p_libvlc_global;
276 /* The mosaic thread is more important than the decoder threads */
277 vlc_thread_set_priority( p_this, VLC_THREAD_PRIORITY_OUTPUT );
279 /* Allocate structure */
280 p_sys = p_filter->p_sys = malloc( sizeof( filter_sys_t ) );
283 msg_Err( p_filter, "out of memory" );
287 p_filter->pf_sub_filter = Filter;
290 vlc_mutex_init( p_filter, &p_sys->lock );
291 vlc_mutex_lock( &p_sys->lock );
293 var_Get( p_libvlc_global, "mosaic-lock", &val );
294 p_sys->p_lock = val.p_address;
296 #define GET_VAR( name, min, max ) \
297 p_sys->i_##name = __MIN( max, __MAX( min, \
298 var_CreateGetInteger( p_filter, "mosaic-" #name ) ) ); \
299 var_Destroy( p_filter, "mosaic-" #name ); \
300 var_Create( p_libvlc_global, "mosaic-" #name, VLC_VAR_INTEGER ); \
301 var_SetInteger( p_libvlc_global, "mosaic-" #name, p_sys->i_##name ); \
302 var_AddCallback( p_libvlc_global, "mosaic-" #name, MosaicCallback, p_sys );
304 GET_VAR( width, 0, INT_MAX );
305 GET_VAR( height, 0, INT_MAX );
306 GET_VAR( xoffset, 0, INT_MAX );
307 GET_VAR( yoffset, 0, INT_MAX );
309 p_sys->i_align = __MIN( 10, __MAX( 0, var_CreateGetInteger( p_filter, "mosaic-align" ) ) );
310 if( p_sys->i_align == 3 || p_sys->i_align == 7 )
312 var_Destroy( p_filter, "mosaic-align" );
313 var_Create( p_libvlc_global, "mosaic-align", VLC_VAR_INTEGER );
314 var_SetInteger( p_libvlc_global, "mosaic-align", p_sys->i_align );
315 var_AddCallback( p_libvlc_global, "mosaic-align", MosaicCallback, p_sys );
317 GET_VAR( vborder, 0, INT_MAX );
318 GET_VAR( hborder, 0, INT_MAX );
319 GET_VAR( rows, 1, INT_MAX );
320 GET_VAR( cols, 1, INT_MAX );
321 GET_VAR( alpha, 0, 255 );
322 GET_VAR( position, 0, 2 );
323 GET_VAR( delay, 100, INT_MAX );
324 p_sys->i_delay *= 1000;
326 p_sys->b_ar = var_CreateGetBool( p_filter, "mosaic-keep-aspect-ratio" );
327 var_Destroy( p_filter, "mosaic-keep-aspect-ratio" );
328 var_Create( p_libvlc_global, "mosaic-keep-aspect-ratio", VLC_VAR_INTEGER );
329 var_SetBool( p_libvlc_global, "mosaic-keep-aspect-ratio", p_sys->b_ar );
330 var_AddCallback( p_libvlc_global, "mosaic-keep-aspect-ratio", MosaicCallback,
333 p_sys->b_keep = var_CreateGetBool( p_filter, "mosaic-keep-picture" );
334 if ( !p_sys->b_keep )
336 p_sys->p_image = image_HandlerCreate( p_filter );
339 p_sys->i_order_length = 0;
340 p_sys->ppsz_order = NULL;
341 psz_order = var_CreateGetString( p_filter, "mosaic-order" );
343 var_Create( p_libvlc_global, "mosaic-order", VLC_VAR_STRING);
344 var_AddCallback( p_libvlc_global, "mosaic-order", MosaicCallback, p_sys );
346 if( psz_order[0] != 0 )
348 char *psz_end = NULL;
352 psz_end = strchr( psz_order, ',' );
354 p_sys->ppsz_order = realloc( p_sys->ppsz_order,
355 i_index * sizeof(char *) );
356 p_sys->ppsz_order[i_index - 1] = strndup( psz_order,
357 psz_end - psz_order );
358 psz_order = psz_end+1;
359 } while( NULL != psz_end );
360 p_sys->i_order_length = i_index;
363 /* Manage specific offsets for substreams */
364 psz_offsets = var_CreateGetString( p_filter, "mosaic-offsets" );
365 var_Destroy( p_filter, "mosaic-offsets" );
366 p_sys->i_offsets_length = 0;
367 p_sys->pi_x_offsets = NULL;
368 p_sys->pi_y_offsets = NULL;
369 mosaic_ParseSetOffsets( (vlc_object_t *) p_filter, p_sys, psz_offsets );
370 var_Create( p_libvlc_global, "mosaic-offsets", VLC_VAR_STRING);
371 var_SetString( p_libvlc_global, "mosaic-offsets", psz_offsets );
372 var_AddCallback( p_libvlc_global, "mosaic-offsets", MosaicCallback, p_sys );
374 /* Bluescreen specific stuff */
375 GET_VAR( bsu, 0x00, 0xff );
376 GET_VAR( bsv, 0x00, 0xff );
377 GET_VAR( bsut, 0x00, 0xff );
378 GET_VAR( bsvt, 0x00, 0xff );
379 p_sys->b_bs = var_CreateGetBool( p_filter, "mosaic-bs" );
380 var_Destroy( p_filter, "mosaic-bs" );
381 var_Create( p_libvlc_global, "mosaic-bs", VLC_VAR_INTEGER );
382 var_SetBool( p_libvlc_global, "mosaic-bs", p_sys->b_bs );
383 var_AddCallback( p_libvlc_global, "mosaic-bs", MosaicCallback, p_sys );
384 if( p_sys->b_bs && p_sys->b_keep )
386 msg_Warn( p_filter, "mosaic-keep-picture needs to be disabled for"
387 " bluescreen to work" );
390 vlc_mutex_unlock( &p_sys->lock );
395 /*****************************************************************************
396 * DestroyFilter: destroy mosaic video filter
397 *****************************************************************************/
398 static void DestroyFilter( vlc_object_t *p_this )
400 filter_t *p_filter = (filter_t*)p_this;
401 filter_sys_t *p_sys = p_filter->p_sys;
402 libvlc_global_data_t *p_libvlc_global = p_filter->p_libvlc_global;
405 vlc_mutex_lock( &p_sys->lock );
409 image_HandlerDelete( p_sys->p_image );
412 if( p_sys->i_order_length )
414 for( i_index = 0; i_index < p_sys->i_order_length; i_index++ )
416 free( p_sys->ppsz_order[i_index] );
418 free( p_sys->ppsz_order );
420 if( p_sys->i_offsets_length )
422 free( p_sys->pi_x_offsets );
423 free( p_sys->pi_y_offsets );
424 p_sys->i_offsets_length = 0;
426 var_Destroy( p_libvlc_global, "mosaic-offsets" );
427 var_Destroy( p_libvlc_global, "mosaic-alpha" );
428 var_Destroy( p_libvlc_global, "mosaic-height" );
429 var_Destroy( p_libvlc_global, "mosaic-align" );
430 var_Destroy( p_libvlc_global, "mosaic-width" );
431 var_Destroy( p_libvlc_global, "mosaic-xoffset" );
432 var_Destroy( p_libvlc_global, "mosaic-yoffset" );
433 var_Destroy( p_libvlc_global, "mosaic-vborder" );
434 var_Destroy( p_libvlc_global, "mosaic-hborder" );
435 var_Destroy( p_libvlc_global, "mosaic-position" );
436 var_Destroy( p_libvlc_global, "mosaic-rows" );
437 var_Destroy( p_libvlc_global, "mosaic-cols" );
438 var_Destroy( p_libvlc_global, "mosaic-keep-aspect-ratio" );
440 var_Destroy( p_libvlc_global, "mosaic-bsu" );
441 var_Destroy( p_libvlc_global, "mosaic-bsv" );
442 var_Destroy( p_libvlc_global, "mosaic-bsut" );
443 var_Destroy( p_libvlc_global, "mosaic-bsvt" );
444 var_Destroy( p_libvlc_global, "mosaic-bs" );
446 if( p_sys->p_pic ) p_sys->p_pic->pf_release( p_sys->p_pic );
447 vlc_mutex_unlock( &p_sys->lock );
448 vlc_mutex_destroy( &p_sys->lock );
452 /*****************************************************************************
453 * MosaicReleasePicture : Hack to avoid picture duplication
454 *****************************************************************************/
455 static void MosaicReleasePicture( picture_t *p_picture )
457 picture_t *p_original_pic = (picture_t *)p_picture->p_sys;
459 p_original_pic->pf_release( p_original_pic );
462 /*****************************************************************************
464 *****************************************************************************/
465 static subpicture_t *Filter( filter_t *p_filter, mtime_t date )
467 filter_sys_t *p_sys = p_filter->p_sys;
472 int i_index, i_real_index, i_row, i_col;
473 int i_greatest_real_index_used = p_sys->i_order_length - 1;
475 unsigned int col_inner_width, row_inner_height;
477 subpicture_region_t *p_region;
478 subpicture_region_t *p_region_prev = NULL;
480 /* Allocate the subpicture internal data. */
481 p_spu = p_filter->pf_sub_buffer_new( p_filter );
487 /* Initialize subpicture */
488 p_spu->i_channel = 0;
489 p_spu->i_start = date;
491 p_spu->b_ephemer = VLC_TRUE;
492 p_spu->i_alpha = p_sys->i_alpha;
493 p_spu->i_flags = p_sys->i_align;
494 p_spu->b_absolute = VLC_FALSE;
496 vlc_mutex_lock( &p_sys->lock );
497 vlc_mutex_lock( p_sys->p_lock );
499 p_bridge = GetBridge( p_filter );
500 if ( p_bridge == NULL )
502 vlc_mutex_unlock( p_sys->p_lock );
503 vlc_mutex_unlock( &p_sys->lock );
507 if ( p_sys->i_position == 2 ) /* user-defined offsets for positioning */
509 /* If we have either too much or not enough offsets, fall-back
510 * to automatic positioning. */
511 if ( p_sys->i_offsets_length != p_sys->i_order_length )
513 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 );
514 p_sys->i_position = 0;
518 if ( p_sys->i_position == 0 ) /* use automatic positioning */
520 int i_numpics = p_sys->i_order_length; /* keep slots and all */
521 for ( i_index = 0; i_index < p_bridge->i_es_num; i_index++ )
523 bridged_es_t *p_es = p_bridge->pp_es[i_index];
524 if ( !p_es->b_empty )
527 if( p_sys->i_order_length && p_es->psz_id != 0 )
529 /* We also want to leave slots for images given in
530 * mosaic-order that are not available in p_vout_picture */
532 for( i = 0; i < p_sys->i_order_length ; i++ )
534 if( !strcmp( p_sys->ppsz_order[i], p_es->psz_id ) )
544 p_sys->i_rows = ((int)ceil(sqrt( (float)i_numpics )));
545 p_sys->i_cols = ( i_numpics % p_sys->i_rows == 0 ?
546 i_numpics / p_sys->i_rows :
547 i_numpics / p_sys->i_rows + 1 );
550 col_inner_width = ( ( p_sys->i_width - ( p_sys->i_cols - 1 )
551 * p_sys->i_vborder ) / p_sys->i_cols );
552 row_inner_height = ( ( p_sys->i_height - ( p_sys->i_rows - 1 )
553 * p_sys->i_hborder ) / p_sys->i_rows );
557 for ( i_index = 0; i_index < p_bridge->i_es_num; i_index++ )
559 bridged_es_t *p_es = p_bridge->pp_es[i_index];
560 video_format_t fmt_in = {0}, fmt_out = {0};
561 picture_t *p_converted;
566 while ( p_es->p_picture != NULL
567 && p_es->p_picture->date + p_sys->i_delay < date )
569 if ( p_es->p_picture->p_next != NULL )
571 picture_t *p_next = p_es->p_picture->p_next;
572 p_es->p_picture->pf_release( p_es->p_picture );
573 p_es->p_picture = p_next;
575 else if ( p_es->p_picture->date + p_sys->i_delay + BLANK_DELAY <
579 p_es->p_picture->pf_release( p_es->p_picture );
580 p_es->p_picture = NULL;
581 p_es->pp_last = &p_es->p_picture;
586 msg_Dbg( p_filter, "too late picture for %s (" I64Fd ")",
588 date - p_es->p_picture->date - p_sys->i_delay );
593 if ( p_es->p_picture == NULL )
596 if ( p_sys->i_order_length == 0 )
603 for ( i = 0; i <= p_sys->i_order_length; i++ )
605 if ( i == p_sys->i_order_length ) break;
606 if ( strcmp( p_es->psz_id, p_sys->ppsz_order[i] ) == 0 )
612 if ( i == p_sys->i_order_length )
613 i_real_index = ++i_greatest_real_index_used;
615 i_row = ( i_real_index / p_sys->i_cols ) % p_sys->i_rows;
616 i_col = i_real_index % p_sys->i_cols ;
618 if ( !p_sys->b_keep )
620 /* Convert the images */
621 fmt_in.i_chroma = p_es->p_picture->format.i_chroma;
622 fmt_in.i_height = p_es->p_picture->format.i_height;
623 fmt_in.i_width = p_es->p_picture->format.i_width;
625 fmt_out.i_chroma = VLC_FOURCC('Y','U','V','A');
626 fmt_out.i_width = col_inner_width;
627 fmt_out.i_height = row_inner_height;
629 if( p_sys->b_ar ) /* keep aspect ratio */
631 if( (float)fmt_out.i_width / (float)fmt_out.i_height
632 > (float)fmt_in.i_width / (float)fmt_in.i_height )
634 fmt_out.i_width = ( fmt_out.i_height * fmt_in.i_width )
639 fmt_out.i_height = ( fmt_out.i_width * fmt_in.i_height )
644 fmt_out.i_visible_width = fmt_out.i_width;
645 fmt_out.i_visible_height = fmt_out.i_height;
647 p_converted = image_Convert( p_sys->p_image, p_es->p_picture,
652 "image resizing and chroma conversion failed" );
656 /* Bluescreen stuff */
660 int i_lines = p_converted->p[ A_PLANE ].i_lines;
661 int i_pitch = p_converted->p[ A_PLANE ].i_pitch;
662 uint8_t *p_a = p_converted->p[ A_PLANE ].p_pixels;
663 uint8_t *p_at = malloc( i_lines * i_pitch * sizeof( uint8_t ) );
664 uint8_t *p_u = p_converted->p[ U_PLANE ].p_pixels;
665 uint8_t *p_v = p_converted->p[ V_PLANE ].p_pixels;
666 uint8_t umin, umax, vmin, vmax;
667 umin = p_sys->i_bsu - p_sys->i_bsut >= 0x00 ?
668 p_sys->i_bsu - p_sys->i_bsut : 0x00;
669 umax = p_sys->i_bsu + p_sys->i_bsut <= 0xff ?
670 p_sys->i_bsu + p_sys->i_bsut : 0xff;
671 vmin = p_sys->i_bsv - p_sys->i_bsvt >= 0x00 ?
672 p_sys->i_bsv - p_sys->i_bsvt : 0x00;
673 vmax = p_sys->i_bsv + p_sys->i_bsvt <= 0xff ?
674 p_sys->i_bsv + p_sys->i_bsvt : 0xff;
676 for( i = 0; i < i_lines*i_pitch; i++ )
690 /* Gaussian convolution to make it look cleaner */
691 memset( p_a, 0, 2 * i_pitch );
692 for( i = 2; i < i_lines - 2; i++ )
694 p_a[i*i_pitch] = 0x00;
695 p_a[i*i_pitch+1] = 0x00;
696 for( j = 2; j < i_pitch - 2; j ++ )
698 p_a[i*i_pitch+j] = (uint8_t)((
700 ( p_at[(i-2)*i_pitch+j-2]<<1 )
701 + ( p_at[(i-2)*i_pitch+j-1]<<2 )
702 + ( p_at[(i-2)*i_pitch+j]<<2 )
703 + ( p_at[(i-2)*i_pitch+j+1]<<2 )
704 + ( p_at[(i-2)*i_pitch+j+2]<<1 )
706 + ( p_at[(i-1)*i_pitch+j-1]<<3 )
707 + ( p_at[(i-1)*i_pitch+j-2]<<2 )
708 + ( p_at[(i-1)*i_pitch+j]*12 )
709 + ( p_at[(i-1)*i_pitch+j+1]<<3 )
710 + ( p_at[(i-1)*i_pitch+j+2]<<2 )
712 + ( p_at[i*i_pitch+j-2]<<2 )
713 + ( p_at[i*i_pitch+j-1]*12 )
714 + ( p_at[i*i_pitch+j]<<4 )
715 + ( p_at[i*i_pitch+j+1]*12 )
716 + ( p_at[i*i_pitch+j+2]<<2 )
718 + ( p_at[(i+1)*i_pitch+j-2]<<2 )
719 + ( p_at[(i+1)*i_pitch+j-1]<<3 )
720 + ( p_at[(i+1)*i_pitch+j]*12 )
721 + ( p_at[(i+1)*i_pitch+j+1]<<3 )
722 + ( p_at[(i+1)*i_pitch+j+2]<<2 )
724 + ( p_at[(i+2)*i_pitch+j-2]<<1 )
725 + ( p_at[(i+2)*i_pitch+j-1]<<2 )
726 + ( p_at[(i+2)*i_pitch+j]<<2 )
727 + ( p_at[(i+2)*i_pitch+j+1]<<2 )
728 + ( p_at[(i+2)*i_pitch+j+2]<<1 )
730 if( p_a[i*i_pitch+j] < 0xbf ) p_a[i*i_pitch+j] = 0x00;
738 p_converted = p_es->p_picture;
739 p_converted->i_refcount++;
740 fmt_in.i_width = fmt_out.i_width = p_converted->format.i_width;
741 fmt_in.i_height = fmt_out.i_height = p_converted->format.i_height;
742 fmt_in.i_chroma = fmt_out.i_chroma = p_converted->format.i_chroma;
743 fmt_out.i_visible_width = fmt_out.i_width;
744 fmt_out.i_visible_height = fmt_out.i_height;
747 p_region = p_spu->pf_make_region( VLC_OBJECT(p_filter), &fmt_out,
751 msg_Err( p_filter, "cannot allocate SPU region" );
752 p_filter->pf_sub_buffer_del( p_filter, p_spu );
753 vlc_mutex_unlock( &p_sys->lock );
754 vlc_mutex_unlock( p_sys->p_lock );
758 /* HACK ALERT : let's fix the pointers to avoid picture duplication.
759 * This is necessary because p_region->picture is not a pointer
760 * as it ought to be. */
767 /* Keep a pointer to the original picture (and its refcount...). */
768 p_region->picture.p_sys = (picture_sys_t *)p_converted;
769 p_region->picture.pf_release = MosaicReleasePicture;
772 if( p_sys->i_position == 2 ) /* user-defined offset */
774 p_region->i_x = p_sys->pi_x_offsets[i_real_index];
776 else if( fmt_out.i_width > col_inner_width ||
777 p_sys->b_ar || p_sys->b_keep )
779 /* we don't have to center the video since it takes the
780 whole rectangle area or it's larger than the rectangle */
781 p_region->i_x = p_sys->i_xoffset
782 + i_col * ( p_sys->i_width / p_sys->i_cols )
783 + ( i_col * p_sys->i_vborder ) / p_sys->i_cols;
787 /* center the video in the dedicated rectangle */
788 p_region->i_x = p_sys->i_xoffset
789 + i_col * ( p_sys->i_width / p_sys->i_cols )
790 + ( i_col * p_sys->i_vborder ) / p_sys->i_cols
791 + ( col_inner_width - fmt_out.i_width ) / 2;
794 if( p_sys->i_position == 2 ) /* user-defined offset */
796 p_region->i_y = p_sys->pi_y_offsets[i_real_index];
798 else if( fmt_out.i_height < row_inner_height
799 || p_sys->b_ar || p_sys->b_keep )
801 /* we don't have to center the video since it takes the
802 whole rectangle area or it's taller than the rectangle */
803 p_region->i_y = p_sys->i_yoffset
804 + i_row * ( p_sys->i_height / p_sys->i_rows )
805 + ( i_row * p_sys->i_hborder ) / p_sys->i_rows;
809 /* center the video in the dedicated rectangle */
810 p_region->i_y = p_sys->i_yoffset
811 + i_row * ( p_sys->i_height / p_sys->i_rows )
812 + ( i_row * p_sys->i_hborder ) / p_sys->i_rows
813 + ( row_inner_height - fmt_out.i_height ) / 2;
816 if( p_region_prev == NULL )
818 p_spu->p_region = p_region;
822 p_region_prev->p_next = p_region;
825 p_region_prev = p_region;
828 vlc_mutex_unlock( p_sys->p_lock );
829 vlc_mutex_unlock( &p_sys->lock );
834 /*****************************************************************************
835 * Callback to update params on the fly
836 *****************************************************************************/
837 static int MosaicCallback( vlc_object_t *p_this, char const *psz_var,
838 vlc_value_t oldval, vlc_value_t newval,
841 filter_sys_t *p_sys = (filter_sys_t *) p_data;
842 if( !strcmp( psz_var, "mosaic-alpha" ) )
844 vlc_mutex_lock( &p_sys->lock );
845 msg_Dbg( p_this, "changing alpha from %d/255 to %d/255",
846 p_sys->i_alpha, newval.i_int);
847 p_sys->i_alpha = __MIN( __MAX( newval.i_int, 0 ), 255 );
848 vlc_mutex_unlock( &p_sys->lock );
850 else if( !strcmp( psz_var, "mosaic-height" ) )
852 vlc_mutex_lock( &p_sys->lock );
853 msg_Dbg( p_this, "changing height from %dpx to %dpx",
854 p_sys->i_height, newval.i_int );
855 p_sys->i_height = __MAX( newval.i_int, 0 );
856 vlc_mutex_unlock( &p_sys->lock );
858 else if( !strcmp( psz_var, "mosaic-width" ) )
860 vlc_mutex_lock( &p_sys->lock );
861 msg_Dbg( p_this, "changing width from %dpx to %dpx",
862 p_sys->i_width, newval.i_int );
863 p_sys->i_width = __MAX( newval.i_int, 0 );
864 vlc_mutex_unlock( &p_sys->lock );
866 else if( !strcmp( psz_var, "mosaic-xoffset" ) )
868 vlc_mutex_lock( &p_sys->lock );
869 msg_Dbg( p_this, "changing x offset from %dpx to %dpx",
870 p_sys->i_xoffset, newval.i_int );
871 p_sys->i_xoffset = __MAX( newval.i_int, 0 );
872 vlc_mutex_unlock( &p_sys->lock );
874 else if( !strcmp( psz_var, "mosaic-yoffset" ) )
876 vlc_mutex_lock( &p_sys->lock );
877 msg_Dbg( p_this, "changing y offset from %dpx to %dpx",
878 p_sys->i_yoffset, newval.i_int );
879 p_sys->i_yoffset = __MAX( newval.i_int, 0 );
880 vlc_mutex_unlock( &p_sys->lock );
882 else if( !strcmp( psz_var, "mosaic-align" ) )
884 int i_old = 0, i_new = 0;
885 vlc_mutex_lock( &p_sys->lock );
886 newval.i_int = __MIN( __MAX( newval.i_int, 0 ), 10 );
887 if( newval.i_int == 3 || newval.i_int == 7 )
889 while( pi_align_values[i_old] != p_sys->i_align ) i_old++;
890 while( pi_align_values[i_new] != newval.i_int ) i_new++;
891 msg_Dbg( p_this, "changing alignment from %d (%s) to %d (%s)",
892 p_sys->i_align, ppsz_align_descriptions[i_old],
893 newval.i_int, ppsz_align_descriptions[i_new] );
894 p_sys->i_align = newval.i_int;
895 vlc_mutex_unlock( &p_sys->lock );
897 else if( !strcmp( psz_var, "mosaic-vborder" ) )
899 vlc_mutex_lock( &p_sys->lock );
900 msg_Dbg( p_this, "changing vertical border from %dpx to %dpx",
901 p_sys->i_vborder, newval.i_int );
902 p_sys->i_vborder = __MAX( newval.i_int, 0 );
903 vlc_mutex_unlock( &p_sys->lock );
905 else if( !strcmp( psz_var, "mosaic-hborder" ) )
907 vlc_mutex_lock( &p_sys->lock );
908 msg_Dbg( p_this, "changing horizontal border from %dpx to %dpx",
909 p_sys->i_vborder, newval.i_int );
910 p_sys->i_hborder = __MAX( newval.i_int, 0 );
911 vlc_mutex_unlock( &p_sys->lock );
913 else if( !strcmp( psz_var, "mosaic-position" ) )
915 if( newval.i_int > 1 || newval.i_int < 0 )
917 msg_Err( p_this, "Position is either 0 (auto) or 1 (fixed)" );
921 vlc_mutex_lock( &p_sys->lock );
922 msg_Dbg( p_this, "changing position method from %d (%s) to %d (%s)",
923 p_sys->i_position, ppsz_pos_descriptions[p_sys->i_position],
924 newval.i_int, ppsz_pos_descriptions[newval.i_int]);
925 p_sys->i_position = newval.i_int;
926 vlc_mutex_unlock( &p_sys->lock );
929 else if( !strcmp( psz_var, "mosaic-rows" ) )
931 vlc_mutex_lock( &p_sys->lock );
932 msg_Dbg( p_this, "changing number of rows from %d to %d",
933 p_sys->i_rows, newval.i_int );
934 p_sys->i_rows = __MAX( newval.i_int, 1 );
935 vlc_mutex_unlock( &p_sys->lock );
937 else if( !strcmp( psz_var, "mosaic-cols" ) )
939 vlc_mutex_lock( &p_sys->lock );
940 msg_Dbg( p_this, "changing number of columns from %d to %d",
941 p_sys->i_cols, newval.i_int );
942 p_sys->i_cols = __MAX( newval.i_int, 1 );
943 vlc_mutex_unlock( &p_sys->lock );
945 else if( !strcmp( psz_var, "mosaic-order" ) )
949 vlc_mutex_lock( &p_sys->lock );
950 msg_Dbg( p_this, "Changing mosaic order to %s", newval.psz_string );
952 p_sys->i_order_length = 0;
953 p_sys->ppsz_order = NULL;
954 psz_order = newval.psz_string;
956 while( p_sys->i_order_length-- )
959 printf("%d\n", p_sys->ppsz_order);
961 free( p_sys->ppsz_order );
963 if( psz_order[0] != 0 )
965 char *psz_end = NULL;
969 psz_end = strchr( psz_order, ',' );
971 p_sys->ppsz_order = realloc( p_sys->ppsz_order,
972 i_index * sizeof(char *) );
973 p_sys->ppsz_order[i_index - 1] = strndup( psz_order,
974 psz_end - psz_order );
975 psz_order = psz_end+1;
976 } while( NULL != psz_end );
977 p_sys->i_order_length = i_index;
980 vlc_mutex_unlock( &p_sys->lock );
982 else if( !strcmp( psz_var, "mosaic-offsets" ) )
984 vlc_mutex_lock( &p_sys->lock );
985 msg_Info( p_this, "Changing mosaic-offsets to %s", newval.psz_string );
987 if( p_sys->i_offsets_length != 0 )
989 free( p_sys->pi_x_offsets );
990 free( p_sys->pi_y_offsets );
992 p_sys->i_offsets_length = 0;
994 mosaic_ParseSetOffsets( (vlc_object_t *) p_this, p_sys, newval.psz_string );
996 vlc_mutex_unlock( &p_sys->lock );
998 else if( !strcmp( psz_var, "mosaic-keep-aspect-ratio" ) )
1000 vlc_mutex_lock( &p_sys->lock );
1003 msg_Dbg( p_this, "keeping aspect ratio" );
1008 msg_Dbg( p_this, "won't keep aspect ratio" );
1011 vlc_mutex_unlock( &p_sys->lock );