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