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