1 /*****************************************************************************
2 * mosaic.c : Mosaic video plugin for vlc
3 *****************************************************************************
4 * Copyright (C) 2004-2005 VideoLAN
7 * Authors: Antoine Cellerier <dionoea@via.ecp.fr>
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version.
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
19 * You should have received a copy of the GNU General Public License
20 * along with this program; if not, write to the Free Software
21 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111, USA.
22 *****************************************************************************/
24 /*****************************************************************************
26 *****************************************************************************/
27 #include <stdlib.h> /* malloc(), free() */
34 #include "vlc_filter.h"
35 #include "vlc_input.h"
37 #include "vlc_image.h"
39 /*****************************************************************************
41 *****************************************************************************/
43 static int CreateFilter ( vlc_object_t * );
44 static void DestroyFilter ( vlc_object_t * );
46 static subpicture_t *Filter( filter_t *, mtime_t );
48 /*****************************************************************************
49 * filter_sys_t : filter desriptor
50 *****************************************************************************/
52 #include "../video_output/picture.h"
57 image_handler_t *p_image;
58 image_handler_t *p_image2;
61 int i_pos; /* mosaic positioning method */
62 int i_width, i_height; /* mosaic height and width */
63 int i_cols, i_rows; /* mosaic rows and cols */
64 int i_xoffset, i_yoffset; /* top left corner offset */
65 int i_vborder, i_hborder; /* border width/height between miniatures */
66 int i_alpha; /* subfilter alpha blending */
70 /*****************************************************************************
72 *****************************************************************************/
74 #define ALPHA_TEXT N_("Mosaic alpha blending (0 -> 255)")
75 #define ALPHA_LONGTEXT N_("Mosaic alpha blending (0 -> 255). default is 255")
77 #define HEIGHT_TEXT N_("Mosaic height in pixels")
78 #define WIDTH_TEXT N_("Mosaic width in pixels")
79 #define XOFFSET_TEXT N_("Mosaic top left corner x coordinate")
80 #define YOFFSET_TEXT N_("Mosaic top left corner y coordinate")
81 #define VBORDER_TEXT N_("Mosaic vertical border width in pixels")
82 #define HBORDER_TEXT N_("Mosaic horizontal border width in pixels")
84 #define POS_TEXT N_("Mosaic positioning method")
85 #define ROWS_TEXT N_("Mosaic number of rows")
86 #define COLS_TEXT N_("Mosaic number of columns")
88 static int pi_pos_values[] = { 0, 1 };
89 static char * ppsz_pos_descriptions[] =
90 { N_("auto"), N_("fixed") };
94 set_description( _("Mosaic video sub filter") );
95 set_shortname( N_("Mosaic") );
96 set_capability( "sub filter", 0 );
97 set_category( CAT_VIDEO );
98 set_subcategory( SUBCAT_VIDEO_SUBPIC );
99 set_callbacks( CreateFilter, DestroyFilter );
101 add_integer( "mosaic-alpha", 255, NULL, ALPHA_TEXT, ALPHA_LONGTEXT, VLC_FALSE );
102 add_integer( "mosaic-height", 100, NULL, HEIGHT_TEXT, HEIGHT_TEXT, VLC_FALSE );
103 add_integer( "mosaic-width", 100, NULL, WIDTH_TEXT, WIDTH_TEXT, VLC_FALSE );
104 add_integer( "mosaic-xoffset", 0, NULL, XOFFSET_TEXT, XOFFSET_TEXT, VLC_FALSE );
105 add_integer( "mosaic-yoffset", 0, NULL, YOFFSET_TEXT, YOFFSET_TEXT, VLC_FALSE );
106 add_integer( "mosaic-vborder", 0, NULL, VBORDER_TEXT, VBORDER_TEXT, VLC_FALSE );
107 add_integer( "mosaic-hborder", 0, NULL, HBORDER_TEXT, HBORDER_TEXT, VLC_FALSE );
109 add_integer( "mosaic-position", 0, NULL, POS_TEXT, POS_TEXT, VLC_TRUE );
110 change_integer_list( pi_pos_values, ppsz_pos_descriptions, 0 );
111 add_integer( "mosaic-rows", 2, NULL, ROWS_TEXT, ROWS_TEXT, VLC_FALSE );
112 add_integer( "mosaic-cols", 2, NULL, COLS_TEXT, COLS_TEXT, VLC_FALSE );
116 /*****************************************************************************
117 * CreateFiler: allocates mosaic video filter
118 *****************************************************************************/
120 static int CreateFilter( vlc_object_t *p_this )
122 filter_t *p_filter = (filter_t *)p_this;
125 /* Allocate structure */
126 p_sys = p_filter->p_sys = malloc( sizeof( filter_sys_t ) );
129 msg_Err( p_filter, "out of memory" );
132 p_sys->p_image = image_HandlerCreate( p_filter );
133 p_sys->p_image2 = image_HandlerCreate( p_filter );
135 p_filter->pf_sub_filter = Filter;
138 p_sys->i_width = __MAX( 0, config_GetInt( p_filter, "mosaic-width" ) );
139 p_sys->i_height = __MAX( 0, config_GetInt( p_filter, "mosaic-height" ) );
141 p_sys->i_xoffset = __MAX( 0, config_GetInt( p_filter, "mosaic-xoffset" ) );
142 p_sys->i_yoffset = __MAX( 0, config_GetInt( p_filter, "mosaic-yoffset" ) );
144 p_sys->i_vborder = __MAX( 0, config_GetInt( p_filter, "mosaic-vborder" ) );
145 p_sys->i_hborder = __MAX( 0, config_GetInt( p_filter, "mosaic-hborder" ) );
147 p_sys->i_rows = __MAX( 1, config_GetInt( p_filter, "mosaic-rows") );
148 p_sys->i_cols = __MAX( 1, config_GetInt( p_filter, "mosaic-cols") );
150 p_sys->i_alpha = config_GetInt( p_filter, "mosaic-alpha" );
151 p_sys->i_alpha = __MIN( 255, __MAX( 0, p_sys->i_alpha ) );
153 p_sys->i_pos = config_GetInt( p_filter, "mosaic-position" );
154 if( p_sys->i_pos > 1 || p_sys->i_pos < 0 ) p_sys->i_pos = 0;
159 /*****************************************************************************
160 * DestroyFilter: destroy mosaic video filter
161 *****************************************************************************/
163 static void DestroyFilter( vlc_object_t *p_this )
165 filter_t *p_filter = (filter_t*)p_this;
166 filter_sys_t *p_sys = p_filter->p_sys;
168 image_HandlerDelete( p_sys->p_image );
169 image_HandlerDelete( p_sys->p_image2 );
171 if( p_sys->p_pic ) p_sys->p_pic->pf_release( p_sys->p_pic );
175 /*****************************************************************************
177 *****************************************************************************/
179 static subpicture_t *Filter( filter_t *p_filter, mtime_t date )
182 filter_sys_t *p_sys = p_filter->p_sys;
185 libvlc_t *p_libvlc = p_filter->p_libvlc;
189 subpicture_region_t *p_region;
190 subpicture_region_t *p_region_prev = NULL;
192 struct picture_vout_t *p_picture_vout;
194 if( var_Get( p_libvlc, "p_picture_vout", &val ) )
199 /* Allocate the subpicture internal data. */
200 p_spu = p_filter->pf_sub_buffer_new( p_filter );
206 p_picture_vout = val.p_address;
208 vlc_mutex_lock( &p_picture_vout->lock );
210 if( p_sys->i_pos == 0 ) /* use automatic positioning */
214 i_index < p_picture_vout->i_picture_num ;
217 if( p_picture_vout->p_pic[i_index].i_status
218 == PICTURE_VOUT_E_OCCUPIED ) {
222 p_sys->i_rows = ((int)ceil(sqrt( (float)i_numpics )));
223 p_sys->i_cols = ( i_numpics%p_sys->i_rows == 0 ?
224 i_numpics/p_sys->i_rows :
225 i_numpics/p_sys->i_rows + 1 );
228 for( i_index = 0 ; i_index < p_picture_vout->i_picture_num ; i_index ++ )
231 video_format_t fmt_in = {0}, fmt_middle = {0}, fmt_out = {0};
233 picture_t *p_converted, *p_middle;
235 if( p_picture_vout->p_pic[i_index].p_picture == NULL )
240 if( p_picture_vout->p_pic[i_index].i_status
241 == PICTURE_VOUT_E_AVAILABLE )
243 msg_Dbg( p_filter, "Picture Vout Element is empty");
248 /* Convert the images */
249 /* fprintf (stderr, "Input image %ix%i %4.4s\n",
250 p_picture_vout->p_pic[i_index].p_picture->format.i_width,
251 p_picture_vout->p_pic[i_index].p_picture->format.i_height,
252 (char *)&p_picture_vout->p_pic[i_index].p_picture->format.i_chroma );*/
254 fmt_in.i_chroma = p_picture_vout->p_pic[i_index].
255 p_picture->format.i_chroma;
256 fmt_in.i_height = p_picture_vout->p_pic[i_index].
257 p_picture->format.i_height;
258 fmt_in.i_width = p_picture_vout->p_pic[i_index].
259 p_picture->format.i_width;
262 fmt_out.i_chroma = VLC_FOURCC('Y','U','V','A');
263 fmt_out.i_width = fmt_in.i_width *( p_sys->i_width / p_sys->i_cols ) / fmt_in.i_width;
264 fmt_out.i_height = fmt_in.i_height*( p_sys->i_height / p_sys->i_rows ) / fmt_in.i_height;
265 fmt_out.i_visible_width = fmt_out.i_width;
266 fmt_out.i_visible_height = fmt_out.i_height;
268 fmt_middle.i_chroma = fmt_in.i_chroma;
269 fmt_middle.i_visible_width = fmt_middle.i_width = fmt_out.i_width;
270 fmt_middle.i_visible_height = fmt_middle.i_height = fmt_out.i_height;
272 p_middle = image_Convert( p_sys->p_image,
273 p_picture_vout->p_pic[i_index].p_picture, &fmt_in, &fmt_middle );
276 vlc_mutex_unlock( &p_picture_vout->lock );
280 p_converted = image_Convert( p_sys->p_image2,
281 p_middle, &fmt_middle, &fmt_out );
284 vlc_mutex_unlock( &p_picture_vout->lock );
288 /* fprintf( stderr, "Converted %ix%i %4.4s\n", p_converted->format.i_width, p_converted->format.i_height, (char *)&p_converted->format.i_chroma);*/
291 p_region = p_spu->pf_create_region( VLC_OBJECT(p_filter), &fmt_out);
294 msg_Err( p_filter, "cannot allocate SPU region" );
295 p_filter->pf_sub_buffer_del( p_filter, p_spu );
296 vlc_mutex_unlock( &p_picture_vout->lock );
300 p_region->i_x = p_sys->i_xoffset + ( i_index % p_sys->i_cols )
301 * ( p_sys->i_width / p_sys->i_cols + p_sys->i_vborder );
302 p_region->i_y = p_sys->i_yoffset
303 + ( ( i_index / p_sys->i_cols ) % p_sys->i_rows )
304 * ( p_sys->i_height / p_sys->i_rows + p_sys->i_hborder );
307 if( p_region_prev == NULL ){
308 p_spu->p_region = p_region;
310 p_region_prev->p_next = p_region;
313 p_region_prev = p_region;
315 vout_CopyPicture( p_filter, &p_region->picture, p_converted );
317 p_middle->pf_release( p_middle );
318 p_converted->pf_release( p_converted );
321 vlc_mutex_unlock( &p_picture_vout->lock );
323 /* Initialize subpicture */
324 p_spu->i_channel = 0;
325 p_spu->i_start = date;
327 p_spu->b_ephemer = VLC_TRUE;
328 p_spu->i_alpha = p_sys->i_alpha;