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_ar; /* do we keep aspect ratio ? */
63 int i_width, i_height; /* mosaic height and width */
64 int i_cols, i_rows; /* mosaic rows and cols */
65 int i_xoffset, i_yoffset; /* top left corner offset */
66 int i_vborder, i_hborder; /* border width/height between miniatures */
67 int i_alpha; /* subfilter alpha blending */
71 /*****************************************************************************
73 *****************************************************************************/
75 #define ALPHA_TEXT N_("Mosaic alpha blending (0 -> 255)")
76 #define ALPHA_LONGTEXT N_("Mosaic alpha blending (0 -> 255). default is 255")
78 #define HEIGHT_TEXT N_("Mosaic height in pixels")
79 #define WIDTH_TEXT N_("Mosaic width in pixels")
80 #define XOFFSET_TEXT N_("Mosaic top left corner x coordinate")
81 #define YOFFSET_TEXT N_("Mosaic top left corner y coordinate")
82 #define VBORDER_TEXT N_("Mosaic vertical border width in pixels")
83 #define HBORDER_TEXT N_("Mosaic horizontal border width in pixels")
85 #define POS_TEXT N_("Mosaic positioning method")
86 #define ROWS_TEXT N_("Mosaic number of rows")
87 #define COLS_TEXT N_("Mosaic number of columns")
88 #define AR_TEXT N_("Keep aspect ratio when resizing")
90 static int pi_pos_values[] = { 0, 1 };
91 static char * ppsz_pos_descriptions[] =
92 { N_("auto"), N_("fixed") };
96 set_description( _("Mosaic video sub filter") );
97 set_shortname( N_("Mosaic") );
98 set_capability( "sub filter", 0 );
99 set_category( CAT_VIDEO );
100 set_subcategory( SUBCAT_VIDEO_SUBPIC );
101 set_callbacks( CreateFilter, DestroyFilter );
103 add_integer( "mosaic-alpha", 255, NULL, ALPHA_TEXT, ALPHA_LONGTEXT, VLC_FALSE );
104 add_integer( "mosaic-height", 100, NULL, HEIGHT_TEXT, HEIGHT_TEXT, VLC_FALSE );
105 add_integer( "mosaic-width", 100, NULL, WIDTH_TEXT, WIDTH_TEXT, VLC_FALSE );
106 add_integer( "mosaic-xoffset", 0, NULL, XOFFSET_TEXT, XOFFSET_TEXT, VLC_FALSE );
107 add_integer( "mosaic-yoffset", 0, NULL, YOFFSET_TEXT, YOFFSET_TEXT, VLC_FALSE );
108 add_integer( "mosaic-vborder", 0, NULL, VBORDER_TEXT, VBORDER_TEXT, VLC_FALSE );
109 add_integer( "mosaic-hborder", 0, NULL, HBORDER_TEXT, HBORDER_TEXT, VLC_FALSE );
111 add_integer( "mosaic-position", 0, NULL, POS_TEXT, POS_TEXT, VLC_TRUE );
112 change_integer_list( pi_pos_values, ppsz_pos_descriptions, 0 );
113 add_integer( "mosaic-rows", 2, NULL, ROWS_TEXT, ROWS_TEXT, VLC_FALSE );
114 add_integer( "mosaic-cols", 2, NULL, COLS_TEXT, COLS_TEXT, VLC_FALSE );
115 add_bool( "mosaic-keep-aspect-ratio", 0, NULL, AR_TEXT, AR_TEXT, VLC_FALSE );
119 /*****************************************************************************
120 * CreateFiler: allocates mosaic video filter
121 *****************************************************************************/
123 static int CreateFilter( vlc_object_t *p_this )
125 filter_t *p_filter = (filter_t *)p_this;
128 /* Allocate structure */
129 p_sys = p_filter->p_sys = malloc( sizeof( filter_sys_t ) );
132 msg_Err( p_filter, "out of memory" );
135 p_sys->p_image = image_HandlerCreate( p_filter );
136 p_sys->p_image2 = image_HandlerCreate( p_filter );
138 p_filter->pf_sub_filter = Filter;
141 p_sys->i_width = __MAX( 0, config_GetInt( p_filter, "mosaic-width" ) );
142 p_sys->i_height = __MAX( 0, config_GetInt( p_filter, "mosaic-height" ) );
144 p_sys->i_xoffset = __MAX( 0, config_GetInt( p_filter, "mosaic-xoffset" ) );
145 p_sys->i_yoffset = __MAX( 0, config_GetInt( p_filter, "mosaic-yoffset" ) );
147 p_sys->i_vborder = __MAX( 0, config_GetInt( p_filter, "mosaic-vborder" ) );
148 p_sys->i_hborder = __MAX( 0, config_GetInt( p_filter, "mosaic-hborder" ) );
150 p_sys->i_rows = __MAX( 1, config_GetInt( p_filter, "mosaic-rows") );
151 p_sys->i_cols = __MAX( 1, config_GetInt( p_filter, "mosaic-cols") );
153 p_sys->i_alpha = config_GetInt( p_filter, "mosaic-alpha" );
154 p_sys->i_alpha = __MIN( 255, __MAX( 0, p_sys->i_alpha ) );
156 p_sys->i_pos = config_GetInt( p_filter, "mosaic-position" );
157 if( p_sys->i_pos > 1 || p_sys->i_pos < 0 ) p_sys->i_pos = 0;
159 p_sys->i_ar = config_GetInt( p_filter, "mosaic-keep-aspect-ratio" );
164 /*****************************************************************************
165 * DestroyFilter: destroy mosaic video filter
166 *****************************************************************************/
168 static void DestroyFilter( vlc_object_t *p_this )
170 filter_t *p_filter = (filter_t*)p_this;
171 filter_sys_t *p_sys = p_filter->p_sys;
173 image_HandlerDelete( p_sys->p_image );
174 image_HandlerDelete( p_sys->p_image2 );
176 if( p_sys->p_pic ) p_sys->p_pic->pf_release( p_sys->p_pic );
180 /*****************************************************************************
182 *****************************************************************************/
184 static subpicture_t *Filter( filter_t *p_filter, mtime_t date )
187 filter_sys_t *p_sys = p_filter->p_sys;
190 libvlc_t *p_libvlc = p_filter->p_libvlc;
194 subpicture_region_t *p_region;
195 subpicture_region_t *p_region_prev = NULL;
197 struct picture_vout_t *p_picture_vout;
199 if( var_Get( p_libvlc, "p_picture_vout", &val ) )
204 /* Allocate the subpicture internal data. */
205 p_spu = p_filter->pf_sub_buffer_new( p_filter );
211 p_picture_vout = val.p_address;
213 vlc_mutex_lock( &p_picture_vout->lock );
215 if( p_sys->i_pos == 0 ) /* use automatic positioning */
219 i_index < p_picture_vout->i_picture_num ;
222 if( p_picture_vout->p_pic[i_index].i_status
223 == PICTURE_VOUT_E_OCCUPIED ) {
227 p_sys->i_rows = ((int)ceil(sqrt( (float)i_numpics )));
228 p_sys->i_cols = ( i_numpics%p_sys->i_rows == 0 ?
229 i_numpics/p_sys->i_rows :
230 i_numpics/p_sys->i_rows + 1 );
233 for( i_index = 0 ; i_index < p_picture_vout->i_picture_num ; i_index ++ )
236 video_format_t fmt_in = {0}, fmt_middle = {0}, fmt_out = {0};
238 picture_t *p_converted, *p_middle;
240 if( p_picture_vout->p_pic[i_index].p_picture == NULL )
245 if( p_picture_vout->p_pic[i_index].i_status
246 == PICTURE_VOUT_E_AVAILABLE )
248 msg_Dbg( p_filter, "Picture Vout Element is empty");
253 /* Convert the images */
254 /* fprintf (stderr, "Input image %ix%i %4.4s\n",
255 p_picture_vout->p_pic[i_index].p_picture->format.i_width,
256 p_picture_vout->p_pic[i_index].p_picture->format.i_height,
257 (char *)&p_picture_vout->p_pic[i_index].p_picture->format.i_chroma );*/
259 fmt_in.i_chroma = p_picture_vout->p_pic[i_index].
260 p_picture->format.i_chroma;
261 fmt_in.i_height = p_picture_vout->p_pic[i_index].
262 p_picture->format.i_height;
263 fmt_in.i_width = p_picture_vout->p_pic[i_index].
264 p_picture->format.i_width;
267 fmt_out.i_chroma = VLC_FOURCC('Y','U','V','A');
268 fmt_out.i_width = fmt_in.i_width *( p_sys->i_width / p_sys->i_cols ) / fmt_in.i_width;
269 fmt_out.i_height = fmt_in.i_height*( p_sys->i_height / p_sys->i_rows ) / fmt_in.i_height;
270 if( p_sys->i_ar ) /* keep aspect ratio */
272 if( (float)fmt_out.i_width/(float)fmt_out.i_height
273 > (float)fmt_in.i_width/(float)fmt_in.i_height )
275 fmt_out.i_width = ( fmt_out.i_height * fmt_in.i_width ) / fmt_in.i_height ;
277 fmt_out.i_height = ( fmt_out.i_width * fmt_in.i_height ) / fmt_in.i_width ;
281 fmt_out.i_visible_width = fmt_out.i_width;
282 fmt_out.i_visible_height = fmt_out.i_height;
284 fmt_middle.i_chroma = fmt_in.i_chroma;
285 fmt_middle.i_visible_width = fmt_middle.i_width = fmt_out.i_width;
286 fmt_middle.i_visible_height = fmt_middle.i_height = fmt_out.i_height;
288 p_middle = image_Convert( p_sys->p_image,
289 p_picture_vout->p_pic[i_index].p_picture, &fmt_in, &fmt_middle );
292 vlc_mutex_unlock( &p_picture_vout->lock );
296 p_converted = image_Convert( p_sys->p_image2,
297 p_middle, &fmt_middle, &fmt_out );
300 vlc_mutex_unlock( &p_picture_vout->lock );
304 /* fprintf( stderr, "Converted %ix%i %4.4s\n", p_converted->format.i_width, p_converted->format.i_height, (char *)&p_converted->format.i_chroma);*/
307 p_region = p_spu->pf_create_region( VLC_OBJECT(p_filter), &fmt_out);
310 msg_Err( p_filter, "cannot allocate SPU region" );
311 p_filter->pf_sub_buffer_del( p_filter, p_spu );
312 vlc_mutex_unlock( &p_picture_vout->lock );
316 p_region->i_x = p_sys->i_xoffset + ( i_index % p_sys->i_cols )
317 * ( p_sys->i_width / p_sys->i_cols + p_sys->i_vborder );
318 p_region->i_y = p_sys->i_yoffset
319 + ( ( i_index / p_sys->i_cols ) % p_sys->i_rows )
320 * ( p_sys->i_height / p_sys->i_rows + p_sys->i_hborder );
323 if( p_region_prev == NULL ){
324 p_spu->p_region = p_region;
326 p_region_prev->p_next = p_region;
329 p_region_prev = p_region;
331 vout_CopyPicture( p_filter, &p_region->picture, p_converted );
333 p_middle->pf_release( p_middle );
334 p_converted->pf_release( p_converted );
337 vlc_mutex_unlock( &p_picture_vout->lock );
339 /* Initialize subpicture */
340 p_spu->i_channel = 0;
341 p_spu->i_start = date;
343 p_spu->b_ephemer = VLC_TRUE;
344 p_spu->i_alpha = p_sys->i_alpha;