]> git.sesse.net Git - vlc/blob - modules/video_filter/mosaic.c
Remove unused variable.
[vlc] / modules / video_filter / mosaic.c
1 /*****************************************************************************
2  * mosaic.c : Mosaic video plugin for vlc
3  *****************************************************************************
4  * Copyright (C) 2004-2007 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 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;         /* Internal filter lock */
63     vlc_mutex_t *p_lock;      /* Pointer to mosaic bridge lock */
64
65     image_handler_t *p_image;
66
67     int i_position;           /* Mosaic positioning method */
68     vlc_bool_t b_ar;          /* Do we keep the aspect ratio ? */
69     vlc_bool_t b_keep;        /* Do we keep the original picture format ? */
70     int i_width, i_height;    /* Mosaic height and width */
71     int i_cols, i_rows;       /* Mosaic rows and cols */
72     int i_align;              /* Mosaic alignment in background video */
73     int i_xoffset, i_yoffset; /* Top left corner offset */
74     int i_borderw, i_borderh; /* Border width/height between miniatures */
75     int i_alpha;              /* Subfilter alpha blending */
76
77     char **ppsz_order;        /* List of picture-ids */
78     int i_order_length;
79
80     int *pi_x_offsets;        /* List of substreams x offsets */
81     int *pi_y_offsets;        /* List of substreams y offsets */
82     int i_offsets_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_( \
92         "Transparency of the mosaic foreground pictures. " \
93         "0 means transparent, 255 opaque (default)." )
94
95 #define HEIGHT_TEXT N_("Height")
96 #define HEIGHT_LONGTEXT N_( "Total height of the mosaic, in pixels." )
97 #define WIDTH_TEXT N_("Width")
98 #define WIDTH_LONGTEXT N_( "Total width of the mosaic, in pixels." )
99
100 #define XOFFSET_TEXT N_("Top left corner X coordinate")
101 #define XOFFSET_LONGTEXT N_( \
102         "X Coordinate of the top-left corner of the mosaic.")
103 #define YOFFSET_TEXT N_("Top left corner Y coordinate")
104 #define YOFFSET_LONGTEXT N_( \
105         "Y Coordinate of the top-left corner of the mosaic.")
106
107 #define BORDERW_TEXT N_("Border width")
108 #define BORDERW_LONGTEXT N_( \
109         "Width in pixels of the border between miniatures." )
110 #define BORDERH_TEXT N_("Border height")
111 #define BORDERH_LONGTEXT N_( \
112         "Height in pixels of the border between miniatures." )
113
114 #define ALIGN_TEXT N_("Mosaic alignment" )
115 #define ALIGN_LONGTEXT N_( \
116         "You can enforce the mosaic alignment on the video " \
117         "(0=center, 1=left, 2=right, 4=top, 8=bottom, you can " \
118         "also use combinations of these values, eg 6 = top-right).")
119
120 #define POS_TEXT N_("Positioning method")
121 #define POS_LONGTEXT N_( \
122         "Positioning method for the mosaic. auto: " \
123         "automatically choose the best number of rows and columns. " \
124         "fixed: use the user-defined number of rows and columns. " \
125         "offsets: use the user-defined offsets for each image." )
126
127 #define ROWS_TEXT N_("Number of rows")
128 #define ROWS_LONGTEXT N_( \
129         "Number of image rows in the mosaic (only used if " \
130         "positionning method is set to \"fixed\")." )
131
132 #define COLS_TEXT N_("Number of columns")
133 #define COLS_LONGTEXT N_( \
134         "Number of image columns in the mosaic (only used if " \
135         "positionning method is set to \"fixed\"." )
136
137 #define AR_TEXT N_("Keep aspect ratio")
138 #define AR_LONGTEXT N_( \
139         "Keep the original aspect ratio when resizing " \
140         "mosaic elements." )
141 #define KEEP_TEXT N_("Keep original size")
142 #define KEEP_LONGTEXT N_( \
143         "Keep the original size of mosaic elements." )
144
145 #define ORDER_TEXT N_("Elements order" )
146 #define ORDER_LONGTEXT N_( \
147         "You can enforce the order of the elements on " \
148         "the mosaic. You must give a comma-separated list of picture ID(s)." \
149         "These IDs are assigned in the \"mosaic-bridge\" module." )
150
151 #define OFFSETS_TEXT N_("Offsets in order" )
152 #define OFFSETS_LONGTEXT N_( \
153         "You can enforce the (x,y) offsets of the elements on the mosaic " \
154         "(only used if positioning method is set to \"offsets\"). You " \
155         "must give a comma-separated list of coordinates (eg: 10,10,150,10)." )
156
157 #define DELAY_TEXT N_("Delay")
158 #define DELAY_LONGTEXT N_( \
159         "Pictures coming from the mosaic elements will be delayed " \
160         "according to this value (in milliseconds). For high " \
161         "values you will need to raise caching at input.")
162
163 enum
164 {
165     position_auto = 0, position_fixed = 1, position_offsets = 2
166 };
167 static int pi_pos_values[] = { 0, 1, 2 };
168 static const char *ppsz_pos_descriptions[] =
169     { N_("auto"), N_("fixed"), N_("offsets") };
170
171 static int pi_align_values[] = { 0, 1, 2, 4, 8, 5, 6, 9, 10 };
172 static const char *ppsz_align_descriptions[] =
173      { N_("Center"), N_("Left"), N_("Right"), N_("Top"), N_("Bottom"),
174      N_("Top-Left"), N_("Top-Right"), N_("Bottom-Left"), N_("Bottom-Right") };
175
176 #define CFG_PREFIX "mosaic-"
177
178 vlc_module_begin();
179     set_description( _("Mosaic video sub filter") );
180     set_shortname( _("Mosaic") );
181     set_category( CAT_VIDEO );
182     set_subcategory( SUBCAT_VIDEO_SUBPIC);
183     set_capability( "sub filter", 0 );
184     set_callbacks( CreateFilter, DestroyFilter );
185
186     add_integer( CFG_PREFIX "alpha", 255, NULL,
187                  ALPHA_TEXT, ALPHA_LONGTEXT, VLC_FALSE );
188
189     add_integer( CFG_PREFIX "height", 100, NULL,
190                  HEIGHT_TEXT, HEIGHT_LONGTEXT, VLC_FALSE );
191     add_integer( CFG_PREFIX "width", 100, NULL,
192                  WIDTH_TEXT, WIDTH_LONGTEXT, VLC_FALSE );
193
194     add_integer( CFG_PREFIX "align", 5, NULL,
195                  ALIGN_TEXT, ALIGN_LONGTEXT, VLC_TRUE);
196         change_integer_list( pi_align_values, ppsz_align_descriptions, 0 );
197
198     add_integer( CFG_PREFIX "xoffset", 0, NULL,
199                  XOFFSET_TEXT, XOFFSET_LONGTEXT, VLC_TRUE );
200     add_integer( CFG_PREFIX "yoffset", 0, NULL,
201                  YOFFSET_TEXT, YOFFSET_LONGTEXT, VLC_TRUE );
202
203     add_integer( CFG_PREFIX "borderw", 0, NULL,
204                  BORDERW_TEXT, BORDERW_LONGTEXT, VLC_TRUE );
205         add_deprecated( CFG_PREFIX "vborder", VLC_FALSE );
206     add_integer( CFG_PREFIX "borderh", 0, NULL,
207                  BORDERH_TEXT, BORDERH_LONGTEXT, VLC_TRUE );
208         add_deprecated( CFG_PREFIX "hborder", VLC_FALSE );
209
210     add_integer( CFG_PREFIX "position", 0, NULL,
211                  POS_TEXT, POS_LONGTEXT, VLC_FALSE );
212         change_integer_list( pi_pos_values, ppsz_pos_descriptions, 0 );
213     add_integer( CFG_PREFIX "rows", 2, NULL,
214                  ROWS_TEXT, ROWS_LONGTEXT, VLC_FALSE );
215     add_integer( CFG_PREFIX "cols", 2, NULL,
216                  COLS_TEXT, COLS_LONGTEXT, VLC_FALSE );
217
218     add_bool( CFG_PREFIX "keep-aspect-ratio", 0, NULL,
219               AR_TEXT, AR_LONGTEXT, VLC_FALSE );
220     add_bool( CFG_PREFIX "keep-picture", 0, NULL,
221               KEEP_TEXT, KEEP_LONGTEXT, VLC_FALSE );
222
223     add_string( CFG_PREFIX "order", "", NULL,
224                 ORDER_TEXT, ORDER_LONGTEXT, VLC_FALSE );
225
226     add_string( CFG_PREFIX "offsets", "", NULL,
227                 OFFSETS_TEXT, OFFSETS_LONGTEXT, VLC_FALSE );
228
229     add_integer( CFG_PREFIX "delay", 0, NULL, DELAY_TEXT, DELAY_LONGTEXT,
230                  VLC_FALSE );
231
232     var_Create( p_module->p_libvlc_global, "mosaic-lock", VLC_VAR_MUTEX );
233 vlc_module_end();
234
235 static const char *ppsz_filter_options[] = {
236     "alpha", "height", "width", "align", "xoffset", "yoffset",
237     "borderw", "borderh", "position", "rows", "cols",
238     "keep-aspect-ratio", "keep-picture", "order", "offsets",
239     "delay", NULL
240 };
241
242 /*****************************************************************************
243  * mosaic_ParseSetOffsets:
244  * parse the "--mosaic-offsets x1,y1,x2,y2,x3,y3" parameter
245  * and set the corresponding struct filter_sys_t entries.
246  *****************************************************************************/
247 #define mosaic_ParseSetOffsets( a, b, c ) \
248       __mosaic_ParseSetOffsets( VLC_OBJECT( a ), b, c )
249 static void __mosaic_ParseSetOffsets( vlc_object_t *p_this,
250                                       filter_sys_t *p_sys,
251                                       char *psz_offsets )
252 {
253     if( *psz_offsets )
254     {
255         char *psz_end = NULL;
256         int i_index = 0;
257         do
258         {
259             i_index++;
260
261             p_sys->pi_x_offsets =
262                 realloc( p_sys->pi_x_offsets, i_index * sizeof(int) );
263             p_sys->pi_x_offsets[i_index - 1] = atoi( psz_offsets );
264             psz_end = strchr( psz_offsets, ',' );
265             psz_offsets = psz_end + 1;
266
267             p_sys->pi_y_offsets =
268                 realloc( p_sys->pi_y_offsets, i_index * sizeof(int) );
269             p_sys->pi_y_offsets[i_index - 1] = atoi( psz_offsets );
270             psz_end = strchr( psz_offsets, ',' );
271             psz_offsets = psz_end + 1;
272
273             msg_Dbg( p_this, CFG_PREFIX "offset: id %d, x=%d, y=%d",
274                      i_index, p_sys->pi_x_offsets[i_index - 1],
275                               p_sys->pi_y_offsets[i_index - 1]  );
276
277         } while( psz_end );
278         p_sys->i_offsets_length = i_index;
279     }
280 }
281
282 /*****************************************************************************
283  * CreateFiler: allocate mosaic video filter
284  *****************************************************************************/
285 static int CreateFilter( vlc_object_t *p_this )
286 {
287     filter_t *p_filter = (filter_t *)p_this;
288     filter_sys_t *p_sys;
289     libvlc_global_data_t *p_libvlc_global = p_filter->p_libvlc_global;
290     char *psz_order;
291     char *psz_offsets;
292     int i_index;
293     vlc_value_t val;
294
295     /* The mosaic thread is more important than the decoder threads */
296     vlc_thread_set_priority( p_this, VLC_THREAD_PRIORITY_OUTPUT );
297
298     /* Allocate structure */
299     p_sys = p_filter->p_sys = malloc( sizeof( filter_sys_t ) );
300     if( p_sys == NULL )
301     {
302         msg_Err( p_filter, "out of memory" );
303         return VLC_ENOMEM;
304     }
305
306     p_filter->pf_sub_filter = Filter;
307
308     vlc_mutex_init( p_filter, &p_sys->lock );
309     vlc_mutex_lock( &p_sys->lock );
310
311     var_Get( p_libvlc_global, "mosaic-lock", &val );
312     p_sys->p_lock = val.p_address;
313
314     config_ChainParse( p_filter, CFG_PREFIX, ppsz_filter_options,
315                        p_filter->p_cfg );
316
317 #define GET_VAR( name, min, max )                                           \
318     p_sys->i_##name = __MIN( max, __MAX( min,                               \
319         var_CreateGetIntegerCommand( p_filter, CFG_PREFIX #name ) ) );      \
320     var_AddCallback( p_filter, CFG_PREFIX #name, MosaicCallback, p_sys );
321
322     GET_VAR( width, 0, INT_MAX );
323     GET_VAR( height, 0, INT_MAX );
324     GET_VAR( xoffset, 0, INT_MAX );
325     GET_VAR( yoffset, 0, INT_MAX );
326
327     GET_VAR( align, 0, 10 );
328     if( p_sys->i_align == 3 || p_sys->i_align == 7 )
329         p_sys->i_align = 5;
330
331     GET_VAR( borderw, 0, INT_MAX );
332     GET_VAR( borderh, 0, INT_MAX );
333     GET_VAR( rows, 1, INT_MAX );
334     GET_VAR( cols, 1, INT_MAX );
335     GET_VAR( alpha, 0, 255 );
336     GET_VAR( position, 0, 2 );
337     GET_VAR( delay, 100, INT_MAX );
338     p_sys->i_delay *= 1000;
339
340     p_sys->b_ar = var_CreateGetBoolCommand( p_filter,
341                                             CFG_PREFIX "keep-aspect-ratio" );
342     var_AddCallback( p_filter, CFG_PREFIX "keep-aspect-ratio", MosaicCallback,
343                      p_sys );
344
345     p_sys->b_keep = var_CreateGetBoolCommand( p_filter,
346                                               CFG_PREFIX "keep-picture" );
347     if ( !p_sys->b_keep )
348     {
349         p_sys->p_image = image_HandlerCreate( p_filter );
350     }
351
352     p_sys->i_order_length = 0;
353     p_sys->ppsz_order = NULL;
354     psz_order = var_CreateGetStringCommand( p_filter, CFG_PREFIX "order" );
355     var_AddCallback( p_filter, CFG_PREFIX "order", MosaicCallback, p_sys );
356
357     if( *psz_order )
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( psz_end );
371         p_sys->i_order_length = i_index;
372     }
373
374     /* Manage specific offsets for substreams */
375     psz_offsets = var_CreateGetStringCommand( p_filter, CFG_PREFIX "offsets" );
376     p_sys->i_offsets_length = 0;
377     p_sys->pi_x_offsets = NULL;
378     p_sys->pi_y_offsets = NULL;
379     mosaic_ParseSetOffsets( p_filter, p_sys, psz_offsets );
380     var_AddCallback( p_filter, CFG_PREFIX "offsets", MosaicCallback, p_sys );
381
382     vlc_mutex_unlock( &p_sys->lock );
383
384     return VLC_SUCCESS;
385 }
386
387 /*****************************************************************************
388  * DestroyFilter: destroy mosaic video filter
389  *****************************************************************************/
390 static void DestroyFilter( vlc_object_t *p_this )
391 {
392     filter_t *p_filter = (filter_t*)p_this;
393     filter_sys_t *p_sys = p_filter->p_sys;
394     int i_index;
395
396     vlc_mutex_lock( &p_sys->lock );
397
398     if( !p_sys->b_keep )
399     {
400         image_HandlerDelete( p_sys->p_image );
401     }
402
403     if( p_sys->i_order_length )
404     {
405         for( i_index = 0; i_index < p_sys->i_order_length; i_index++ )
406         {
407             free( p_sys->ppsz_order[i_index] );
408         }
409         free( p_sys->ppsz_order );
410     }
411     if( p_sys->i_offsets_length )
412     {
413         free( p_sys->pi_x_offsets );
414         free( p_sys->pi_y_offsets );
415         p_sys->i_offsets_length = 0;
416     }
417
418     vlc_mutex_unlock( &p_sys->lock );
419     vlc_mutex_destroy( &p_sys->lock );
420     free( p_sys );
421 }
422
423 /*****************************************************************************
424  * MosaicReleasePicture : Hack to avoid picture duplication
425  *****************************************************************************/
426 static void MosaicReleasePicture( picture_t *p_picture )
427 {
428     picture_t *p_original_pic = (picture_t *)p_picture->p_sys;
429
430     p_original_pic->pf_release( p_original_pic );
431 }
432
433 /*****************************************************************************
434  * Filter
435  *****************************************************************************/
436 static subpicture_t *Filter( filter_t *p_filter, mtime_t date )
437 {
438     filter_sys_t *p_sys = p_filter->p_sys;
439     bridge_t *p_bridge;
440
441     subpicture_t *p_spu;
442
443     int i_index, i_real_index, i_row, i_col;
444     int i_greatest_real_index_used = p_sys->i_order_length - 1;
445
446     unsigned int col_inner_width, row_inner_height;
447
448     subpicture_region_t *p_region;
449     subpicture_region_t *p_region_prev = NULL;
450
451     /* Allocate the subpicture internal data. */
452     p_spu = p_filter->pf_sub_buffer_new( p_filter );
453     if( !p_spu )
454     {
455         return NULL;
456     }
457
458     /* Initialize subpicture */
459     p_spu->i_channel = 0;
460     p_spu->i_start  = date;
461     p_spu->i_stop = 0;
462     p_spu->b_ephemer = VLC_TRUE;
463     p_spu->i_alpha = p_sys->i_alpha;
464     p_spu->i_flags = p_sys->i_align;
465     p_spu->b_absolute = VLC_FALSE;
466
467     vlc_mutex_lock( &p_sys->lock );
468     vlc_mutex_lock( p_sys->p_lock );
469
470     p_bridge = GetBridge( p_filter );
471     if ( p_bridge == NULL )
472     {
473         vlc_mutex_unlock( p_sys->p_lock );
474         vlc_mutex_unlock( &p_sys->lock );
475         return p_spu;
476     }
477
478     if ( p_sys->i_position == position_offsets )
479     {
480         /* If we have either too much or not enough offsets, fall-back
481          * to automatic positioning. */
482         if ( p_sys->i_offsets_length != p_sys->i_order_length )
483         {
484             msg_Err( p_filter,
485                      "Number of specified offsets (%d) does not match number "
486                      "of input substreams in mosaic-order (%d), falling back "
487                      "to mosaic-position=0",
488                      p_sys->i_offsets_length, p_sys->i_order_length );
489             p_sys->i_position = position_auto;
490         }
491     }
492
493     if ( p_sys->i_position == position_auto )
494     {
495         int i_numpics = p_sys->i_order_length; /* keep slots and all */
496         for ( i_index = 0; i_index < p_bridge->i_es_num; i_index++ )
497         {
498             bridged_es_t *p_es = p_bridge->pp_es[i_index];
499             if ( !p_es->b_empty )
500             {
501                 i_numpics ++;
502                 if( p_sys->i_order_length && p_es->psz_id != 0 )
503                 {
504                     /* We also want to leave slots for images given in
505                      * mosaic-order that are not available in p_vout_picture */
506                     int i;
507                     for( i = 0; i < p_sys->i_order_length ; i++ )
508                     {
509                         if( !strcmp( p_sys->ppsz_order[i], p_es->psz_id ) )
510                         {
511                             i_numpics--;
512                             break;
513                         }
514                     }
515
516                 }
517             }
518         }
519         p_sys->i_rows = ceil(sqrt( (double)i_numpics ));
520         p_sys->i_cols = ( i_numpics % p_sys->i_rows == 0 ?
521                             i_numpics / p_sys->i_rows :
522                             i_numpics / p_sys->i_rows + 1 );
523     }
524
525     col_inner_width  = ( ( p_sys->i_width - ( p_sys->i_cols - 1 )
526                        * p_sys->i_borderw ) / p_sys->i_cols );
527     row_inner_height = ( ( p_sys->i_height - ( p_sys->i_rows - 1 )
528                        * p_sys->i_borderh ) / p_sys->i_rows );
529
530     i_real_index = 0;
531
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         video_format_t fmt_in, fmt_out;
536         picture_t *p_converted;
537
538         memset( &fmt_in, 0, sizeof( video_format_t ) );
539         memset( &fmt_out, 0, sizeof( video_format_t ) );
540
541         if ( p_es->b_empty )
542             continue;
543
544         while ( p_es->p_picture != NULL
545                  && p_es->p_picture->date + p_sys->i_delay < date )
546         {
547             if ( p_es->p_picture->p_next != NULL )
548             {
549                 picture_t *p_next = p_es->p_picture->p_next;
550                 p_es->p_picture->pf_release( p_es->p_picture );
551                 p_es->p_picture = p_next;
552             }
553             else if ( p_es->p_picture->date + p_sys->i_delay + BLANK_DELAY <
554                         date )
555             {
556                 /* Display blank */
557                 p_es->p_picture->pf_release( p_es->p_picture );
558                 p_es->p_picture = NULL;
559                 p_es->pp_last = &p_es->p_picture;
560                 break;
561             }
562             else
563             {
564                 msg_Dbg( p_filter, "too late picture for %s (" I64Fd ")",
565                          p_es->psz_id,
566                          date - p_es->p_picture->date - p_sys->i_delay );
567                 break;
568             }
569         }
570
571         if ( p_es->p_picture == NULL )
572             continue;
573
574         if ( p_sys->i_order_length == 0 )
575         {
576             i_real_index++;
577         }
578         else
579         {
580             int i;
581             for ( i = 0; i <= p_sys->i_order_length; i++ )
582             {
583                 if ( i == p_sys->i_order_length ) break;
584                 if ( strcmp( p_es->psz_id, p_sys->ppsz_order[i] ) == 0 )
585                 {
586                     i_real_index = i;
587                     break;
588                 }
589             }
590             if ( i == p_sys->i_order_length )
591                 i_real_index = ++i_greatest_real_index_used;
592         }
593         i_row = ( i_real_index / p_sys->i_cols ) % p_sys->i_rows;
594         i_col = i_real_index % p_sys->i_cols ;
595
596         if ( !p_sys->b_keep )
597         {
598             /* Convert the images */
599             fmt_in.i_chroma = p_es->p_picture->format.i_chroma;
600             fmt_in.i_height = p_es->p_picture->format.i_height;
601             fmt_in.i_width = p_es->p_picture->format.i_width;
602
603             fmt_out.i_chroma = VLC_FOURCC('I','4','2','0');
604             fmt_out.i_width = col_inner_width;
605             fmt_out.i_height = row_inner_height;
606
607             if( p_sys->b_ar ) /* keep aspect ratio */
608             {
609                 if( (float)fmt_out.i_width / (float)fmt_out.i_height
610                       > (float)fmt_in.i_width / (float)fmt_in.i_height )
611                 {
612                     fmt_out.i_width = ( fmt_out.i_height * fmt_in.i_width )
613                                          / fmt_in.i_height;
614                 }
615                 else
616                 {
617                     fmt_out.i_height = ( fmt_out.i_width * fmt_in.i_height )
618                                         / fmt_in.i_width;
619                 }
620              }
621
622             fmt_out.i_visible_width = fmt_out.i_width;
623             fmt_out.i_visible_height = fmt_out.i_height;
624
625             p_converted = image_Convert( p_sys->p_image, p_es->p_picture,
626                                          &fmt_in, &fmt_out );
627             if( !p_converted )
628             {
629                 msg_Warn( p_filter,
630                            "image resizing and chroma conversion failed" );
631                 continue;
632             }
633         }
634         else
635         {
636             p_converted = p_es->p_picture;
637             p_converted->i_refcount++;
638             fmt_in.i_width = fmt_out.i_width = p_converted->format.i_width;
639             fmt_in.i_height = fmt_out.i_height = p_converted->format.i_height;
640             fmt_in.i_chroma = fmt_out.i_chroma = p_converted->format.i_chroma;
641             fmt_out.i_visible_width = fmt_out.i_width;
642             fmt_out.i_visible_height = fmt_out.i_height;
643         }
644
645         p_region = p_spu->pf_make_region( VLC_OBJECT(p_filter), &fmt_out,
646                                           p_converted );
647         if( !p_region )
648         {
649             msg_Err( p_filter, "cannot allocate SPU region" );
650             p_filter->pf_sub_buffer_del( p_filter, p_spu );
651             vlc_mutex_unlock( &p_sys->lock );
652             vlc_mutex_unlock( p_sys->p_lock );
653             return p_spu;
654         }
655
656         /* HACK ALERT: let's fix the pointers to avoid picture duplication.
657          * This is necessary because p_region->picture is not a pointer
658          * as it ought to be. */
659         if( !p_sys->b_keep )
660         {
661             free( p_converted );
662         }
663         else
664         {
665             /* Keep a pointer to the original picture (and its refcount...). */
666             p_region->picture.p_sys = (picture_sys_t *)p_converted;
667             p_region->picture.pf_release = MosaicReleasePicture;
668         }
669
670         if( p_sys->i_position == position_offsets )
671         {
672             p_region->i_x = p_sys->pi_x_offsets[i_real_index];
673         }
674         else if( fmt_out.i_width > col_inner_width ||
675             p_sys->b_ar || p_sys->b_keep )
676         {
677             /* we don't have to center the video since it takes the
678             whole rectangle area or it's larger than the rectangle */
679             p_region->i_x = p_sys->i_xoffset
680                         + i_col * ( p_sys->i_width / p_sys->i_cols )
681                         + ( i_col * p_sys->i_borderw ) / p_sys->i_cols;
682         }
683         else
684         {
685             /* center the video in the dedicated rectangle */
686             p_region->i_x = p_sys->i_xoffset
687                     + i_col * ( p_sys->i_width / p_sys->i_cols )
688                     + ( i_col * p_sys->i_borderw ) / p_sys->i_cols
689                     + ( col_inner_width - fmt_out.i_width ) / 2;
690         }
691
692         if( p_sys->i_position == position_offsets )
693         {
694             p_region->i_y = p_sys->pi_y_offsets[i_real_index];
695         }
696         else if( fmt_out.i_height < row_inner_height
697             || p_sys->b_ar || p_sys->b_keep )
698         {
699             /* we don't have to center the video since it takes the
700             whole rectangle area or it's taller than the rectangle */
701             p_region->i_y = p_sys->i_yoffset
702                     + i_row * ( p_sys->i_height / p_sys->i_rows )
703                     + ( i_row * p_sys->i_borderh ) / p_sys->i_rows;
704         }
705         else
706         {
707             /* center the video in the dedicated rectangle */
708             p_region->i_y = p_sys->i_yoffset
709                     + i_row * ( p_sys->i_height / p_sys->i_rows )
710                     + ( i_row * p_sys->i_borderh ) / p_sys->i_rows
711                     + ( row_inner_height - fmt_out.i_height ) / 2;
712         }
713
714         if( p_region_prev == NULL )
715         {
716             p_spu->p_region = p_region;
717         }
718         else
719         {
720             p_region_prev->p_next = p_region;
721         }
722
723         p_region_prev = p_region;
724     }
725
726     vlc_mutex_unlock( p_sys->p_lock );
727     vlc_mutex_unlock( &p_sys->lock );
728
729     return p_spu;
730 }
731
732 /*****************************************************************************
733 * Callback to update params on the fly
734 *****************************************************************************/
735 static int MosaicCallback( vlc_object_t *p_this, char const *psz_var,
736                             vlc_value_t oldval, vlc_value_t newval,
737                             void *p_data )
738 {
739     filter_sys_t *p_sys = (filter_sys_t *) p_data;
740
741 #define VAR_IS( a ) !strcmp( psz_var, CFG_PREFIX a )
742     if( VAR_IS( "alpha" ) )
743     {
744         vlc_mutex_lock( &p_sys->lock );
745         msg_Dbg( p_this, "changing alpha from %d/255 to %d/255",
746                          p_sys->i_alpha, newval.i_int);
747         p_sys->i_alpha = __MIN( __MAX( newval.i_int, 0 ), 255 );
748         vlc_mutex_unlock( &p_sys->lock );
749     }
750     else if( VAR_IS( "height" ) )
751     {
752         vlc_mutex_lock( &p_sys->lock );
753         msg_Dbg( p_this, "changing height from %dpx to %dpx",
754                           p_sys->i_height, newval.i_int );
755         p_sys->i_height = __MAX( newval.i_int, 0 );
756         vlc_mutex_unlock( &p_sys->lock );
757     }
758     else if( VAR_IS( "width" ) )
759     {
760         vlc_mutex_lock( &p_sys->lock );
761         msg_Dbg( p_this, "changing width from %dpx to %dpx",
762                          p_sys->i_width, newval.i_int );
763         p_sys->i_width = __MAX( newval.i_int, 0 );
764         vlc_mutex_unlock( &p_sys->lock );
765     }
766     else if( VAR_IS( "xoffset" ) )
767     {
768         vlc_mutex_lock( &p_sys->lock );
769         msg_Dbg( p_this, "changing x offset from %dpx to %dpx",
770                          p_sys->i_xoffset, newval.i_int );
771         p_sys->i_xoffset = __MAX( newval.i_int, 0 );
772         vlc_mutex_unlock( &p_sys->lock );
773     }
774     else if( VAR_IS( "yoffset" ) )
775     {
776         vlc_mutex_lock( &p_sys->lock );
777         msg_Dbg( p_this, "changing y offset from %dpx to %dpx",
778                          p_sys->i_yoffset, newval.i_int );
779         p_sys->i_yoffset = __MAX( newval.i_int, 0 );
780         vlc_mutex_unlock( &p_sys->lock );
781     }
782     else if( VAR_IS( "align" ) )
783     {
784         int i_old = 0, i_new = 0;
785         vlc_mutex_lock( &p_sys->lock );
786         newval.i_int = __MIN( __MAX( newval.i_int, 0 ), 10 );
787         if( newval.i_int == 3 || newval.i_int == 7 )
788             newval.i_int = 5;
789         while( pi_align_values[i_old] != p_sys->i_align ) i_old++;
790         while( pi_align_values[i_new] != newval.i_int ) i_new++;
791         msg_Dbg( p_this, "changing alignment from %d (%s) to %d (%s)",
792                      p_sys->i_align, ppsz_align_descriptions[i_old],
793                      newval.i_int, ppsz_align_descriptions[i_new] );
794         p_sys->i_align = newval.i_int;
795         vlc_mutex_unlock( &p_sys->lock );
796     }
797     else if( VAR_IS( "borderw" ) )
798     {
799         vlc_mutex_lock( &p_sys->lock );
800         msg_Dbg( p_this, "changing border width from %dpx to %dpx",
801                          p_sys->i_borderw, newval.i_int );
802         p_sys->i_borderw = __MAX( newval.i_int, 0 );
803         vlc_mutex_unlock( &p_sys->lock );
804     }
805     else if( VAR_IS( "borderh" ) )
806     {
807         vlc_mutex_lock( &p_sys->lock );
808         msg_Dbg( p_this, "changing border height from %dpx to %dpx",
809                          p_sys->i_borderh, newval.i_int );
810         p_sys->i_borderh = __MAX( newval.i_int, 0 );
811         vlc_mutex_unlock( &p_sys->lock );
812     }
813     else if( VAR_IS( "position" ) )
814     {
815         if( newval.i_int > 2 || newval.i_int < 0 )
816         {
817             msg_Err( p_this,
818                      "Position is either 0 (%s), 1 (%s) or 2 (%s)",
819                      ppsz_pos_descriptions[0],
820                      ppsz_pos_descriptions[1],
821                      ppsz_pos_descriptions[2] );
822         }
823         else
824         {
825             vlc_mutex_lock( &p_sys->lock );
826             msg_Dbg( p_this, "changing position method from %d (%s) to %d (%s)",
827                     p_sys->i_position, ppsz_pos_descriptions[p_sys->i_position],
828                     newval.i_int, ppsz_pos_descriptions[newval.i_int]);
829             p_sys->i_position = newval.i_int;
830             vlc_mutex_unlock( &p_sys->lock );
831         }
832     }
833     else if( VAR_IS( "rows" ) )
834     {
835         vlc_mutex_lock( &p_sys->lock );
836         msg_Dbg( p_this, "changing number of rows from %d to %d",
837                          p_sys->i_rows, newval.i_int );
838         p_sys->i_rows = __MAX( newval.i_int, 1 );
839         vlc_mutex_unlock( &p_sys->lock );
840     }
841     else if( VAR_IS( "cols" ) )
842     {
843         vlc_mutex_lock( &p_sys->lock );
844         msg_Dbg( p_this, "changing number of columns from %d to %d",
845                          p_sys->i_cols, newval.i_int );
846         p_sys->i_cols = __MAX( newval.i_int, 1 );
847         vlc_mutex_unlock( &p_sys->lock );
848     }
849     else if( VAR_IS( "order" ) )
850     {
851         char *psz_order;
852         int i_index;
853         vlc_mutex_lock( &p_sys->lock );
854         msg_Dbg( p_this, "Changing mosaic order to %s", newval.psz_string );
855
856         psz_order = newval.psz_string;
857
858         while( p_sys->i_order_length-- )
859         {
860             free( p_sys->ppsz_order[p_sys->i_order_length] );
861         }
862         free( p_sys->ppsz_order );
863         p_sys->ppsz_order = NULL;
864
865         if( *psz_order )
866         {
867             char *psz_end = NULL;
868             i_index = 0;
869             do
870             {
871                 psz_end = strchr( psz_order, ',' );
872                 i_index++;
873                 p_sys->ppsz_order = realloc( p_sys->ppsz_order,
874                                     i_index * sizeof(char *) );
875                 p_sys->ppsz_order[i_index - 1] = strndup( psz_order,
876                                            psz_end - psz_order );
877                 psz_order = psz_end+1;
878             } while( psz_end );
879             p_sys->i_order_length = i_index;
880         }
881
882         vlc_mutex_unlock( &p_sys->lock );
883     }
884     else if( VAR_IS( "offsets" ) )
885     {
886         vlc_mutex_lock( &p_sys->lock );
887         msg_Info( p_this, "Changing mosaic-offsets to %s", newval.psz_string );
888
889         if( p_sys->i_offsets_length != 0 )
890         {
891             free( p_sys->pi_x_offsets );
892             free( p_sys->pi_y_offsets );
893         }
894         p_sys->i_offsets_length = 0;
895
896         mosaic_ParseSetOffsets( p_this, p_sys, newval.psz_string );
897
898         vlc_mutex_unlock( &p_sys->lock );
899     }
900     else if( VAR_IS( "keep-aspect-ratio" ) )
901     {
902         vlc_mutex_lock( &p_sys->lock );
903         if( newval.i_int )
904         {
905             msg_Dbg( p_this, "keeping aspect ratio" );
906             p_sys->b_ar = 1;
907         }
908         else
909         {
910             msg_Dbg( p_this, "won't keep aspect ratio" );
911             p_sys->b_ar = 0;
912         }
913         vlc_mutex_unlock( &p_sys->lock );
914     }
915     else if( VAR_IS( "keep-picture" ) )
916     {
917         vlc_mutex_lock( &p_sys->lock );
918         p_sys->b_keep = newval.b_bool;
919         if ( !p_sys->b_keep && !p_sys->p_image )
920         {
921             p_sys->p_image = image_HandlerCreate( p_this );
922         }
923         vlc_mutex_unlock( &p_sys->lock );
924     }
925
926     return VLC_SUCCESS;
927 }