1 /*****************************************************************************
2 * osd_widgets.c : OSD widgets manipulation functions
3 *****************************************************************************
4 * Copyright (C) 2004-2007 the VideoLAN team
7 * Author: Yoann Peronneau <yoann@videolan.org>
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., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
22 *****************************************************************************/
24 /*****************************************************************************
26 *****************************************************************************/
31 #include <vlc_common.h>
34 #include <vlc_filter.h>
37 #define STYLE_FILLED 1
39 /*****************************************************************************
41 *****************************************************************************/
42 static void DrawRect( subpicture_t *, int, int, int, int, short );
43 static void DrawTriangle( subpicture_t *, int, int, int, int, short );
44 static int CreatePicture( spu_t *, subpicture_t *, int, int, int, int );
45 static subpicture_t *osd_CreateWidget( spu_t *, int );
47 /*****************************************************************************
48 * Draws a rectangle at the given position in the subpic.
49 * It may be filled (fill == STYLE_FILLED) or empty (fill == STYLE_EMPTY).
50 *****************************************************************************/
51 static void DrawRect( subpicture_t *p_subpic, int i_x1, int i_y1,
52 int i_x2, int i_y2, short fill )
55 uint8_t *p_a = p_subpic->p_region->p_picture->A_PIXELS;
56 int i_pitch = p_subpic->p_region->p_picture->Y_PITCH;
58 if( fill == STYLE_FILLED )
60 for( y = i_y1; y <= i_y2; y++ )
62 for( x = i_x1; x <= i_x2; x++ )
64 p_a[ x + i_pitch * y ] = 0xff;
70 for( y = i_y1; y <= i_y2; y++ )
72 p_a[ i_x1 + i_pitch * y ] = 0xff;
73 p_a[ i_x2 + i_pitch * y ] = 0xff;
75 for( x = i_x1; x <= i_x2; x++ )
77 p_a[ x + i_pitch * i_y1 ] = 0xff;
78 p_a[ x + i_pitch * i_y2 ] = 0xff;
83 /*****************************************************************************
84 * Draws a triangle at the given position in the subpic.
85 * It may be filled (fill == STYLE_FILLED) or empty (fill == STYLE_EMPTY).
86 *****************************************************************************/
87 static void DrawTriangle( subpicture_t *p_subpic, int i_x1, int i_y1,
88 int i_x2, int i_y2, short fill )
91 uint8_t *p_a = p_subpic->p_region->p_picture->A_PIXELS;
92 int i_pitch = p_subpic->p_region->p_picture->Y_PITCH;
94 i_mid = i_y1 + ( ( i_y2 - i_y1 ) >> 1 );
98 if( fill == STYLE_FILLED )
100 for( y = i_y1; y <= i_mid; y++ )
103 for( x = i_x1; x <= i_x1 + h && x <= i_x2; x++ )
105 p_a[ x + i_pitch * y ] = 0xff;
106 p_a[ x + i_pitch * ( i_y2 - h ) ] = 0xff;
112 for( y = i_y1; y <= i_mid; y++ )
115 p_a[ i_x1 + i_pitch * y ] = 0xff;
116 p_a[ i_x1 + h + i_pitch * y ] = 0xff;
117 p_a[ i_x1 + i_pitch * ( i_y2 - h ) ] = 0xff;
118 p_a[ i_x1 + h + i_pitch * ( i_y2 - h ) ] = 0xff;
124 if( fill == STYLE_FILLED )
126 for( y = i_y1; y <= i_mid; y++ )
129 for( x = i_x1; x >= i_x1 - h && x >= i_x2; x-- )
131 p_a[ x + i_pitch * y ] = 0xff;
132 p_a[ x + i_pitch * ( i_y2 - h ) ] = 0xff;
138 for( y = i_y1; y <= i_mid; y++ )
141 p_a[ i_x1 + i_pitch * y ] = 0xff;
142 p_a[ i_x1 - h + i_pitch * y ] = 0xff;
143 p_a[ i_x1 + i_pitch * ( i_y2 - h ) ] = 0xff;
144 p_a[ i_x1 - h + i_pitch * ( i_y2 - h ) ] = 0xff;
150 /*****************************************************************************
151 * Create Picture: creates subpicture region and picture
152 *****************************************************************************/
153 static int CreatePicture( spu_t *p_spu, subpicture_t *p_subpic,
154 int i_x, int i_y, int i_width, int i_height )
156 uint8_t *p_y, *p_u, *p_v, *p_a;
160 /* Create a new subpicture region */
161 memset( &fmt, 0, sizeof(video_format_t) );
162 fmt.i_chroma = VLC_CODEC_YUVA;
163 fmt.i_width = fmt.i_visible_width = i_width;
164 fmt.i_height = fmt.i_visible_height = i_height;
165 fmt.i_x_offset = fmt.i_y_offset = 0;
166 p_subpic->p_region = subpicture_region_New( &fmt );
167 if( !p_subpic->p_region )
169 msg_Err( p_spu, "cannot allocate SPU region" );
173 p_subpic->p_region->i_x = i_x;
174 p_subpic->p_region->i_y = i_y;
175 p_y = p_subpic->p_region->p_picture->Y_PIXELS;
176 p_u = p_subpic->p_region->p_picture->U_PIXELS;
177 p_v = p_subpic->p_region->p_picture->V_PIXELS;
178 p_a = p_subpic->p_region->p_picture->A_PIXELS;
179 i_pitch = p_subpic->p_region->p_picture->Y_PITCH;
181 /* Initialize the region pixels (only the alpha will be changed later) */
182 memset( p_y, 0xff, i_pitch * p_subpic->p_region->fmt.i_height );
183 memset( p_u, 0x80, i_pitch * p_subpic->p_region->fmt.i_height );
184 memset( p_v, 0x80, i_pitch * p_subpic->p_region->fmt.i_height );
185 memset( p_a, 0x00, i_pitch * p_subpic->p_region->fmt.i_height );
190 /*****************************************************************************
191 * Creates and initializes an OSD widget.
192 *****************************************************************************/
193 subpicture_t *osd_CreateWidget( spu_t *p_spu, int i_channel )
195 subpicture_t *p_subpic;
196 mtime_t i_now = mdate();
200 /* Create and initialize a subpicture */
201 p_subpic = subpicture_New();
202 if( p_subpic == NULL ) return NULL;
204 p_subpic->i_channel = i_channel;
205 p_subpic->i_start = i_now;
206 p_subpic->i_stop = i_now + 1200000;
207 p_subpic->b_ephemer = true;
208 p_subpic->b_fade = true;
213 /*****************************************************************************
214 * Displays an OSD slider.
215 * Types are: OSD_HOR_SLIDER and OSD_VERT_SLIDER.
216 *****************************************************************************/
217 int osd_Slider( vlc_object_t *p_this, spu_t *p_spu,
218 int i_render_width, int i_render_height,
219 int i_margin_left, int i_margin_bottom,
220 int i_channel, int i_position, short i_type )
222 subpicture_t *p_subpic;
223 int i_x_margin, i_y_margin, i_x, i_y, i_width, i_height;
226 p_subpic = osd_CreateWidget( p_spu, i_channel );
227 if( p_subpic == NULL )
232 i_y_margin = i_render_height / 10;
233 i_x_margin = i_y_margin + i_margin_left;
234 i_y_margin += i_margin_bottom;
236 if( i_type == OSD_HOR_SLIDER )
238 i_width = i_render_width - 2 * i_x_margin;
239 i_height = i_render_height / 20;
241 i_y = i_render_height - i_y_margin - i_height;
245 i_width = i_render_width / 40;
246 i_height = i_render_height - 2 * i_y_margin;
247 i_x = i_render_width - i_x_margin - i_width;
251 /* Create subpicture region and picture */
252 CreatePicture( p_spu, p_subpic, i_x, i_y, i_width, i_height );
254 if( i_type == OSD_HOR_SLIDER )
256 int i_x_pos = ( i_width - 2 ) * i_position / 100;
257 DrawRect( p_subpic, i_x_pos - 1, 2, i_x_pos + 1,
258 i_height - 3, STYLE_FILLED );
259 DrawRect( p_subpic, 0, 0, i_width - 1, i_height - 1, STYLE_EMPTY );
261 else if( i_type == OSD_VERT_SLIDER )
263 int i_y_pos = i_height / 2;
264 DrawRect( p_subpic, 2, i_height - ( i_height - 2 ) * i_position / 100,
265 i_width - 3, i_height - 3, STYLE_FILLED );
266 DrawRect( p_subpic, 1, i_y_pos, 1, i_y_pos, STYLE_FILLED );
267 DrawRect( p_subpic, i_width - 2, i_y_pos,
268 i_width - 2, i_y_pos, STYLE_FILLED );
269 DrawRect( p_subpic, 0, 0, i_width - 1, i_height - 1, STYLE_EMPTY );
272 spu_DisplaySubpicture( p_spu, p_subpic );
277 /*****************************************************************************
278 * Displays an OSD icon.
279 * Types are: OSD_PLAY_ICON, OSD_PAUSE_ICON, OSD_SPEAKER_ICON, OSD_MUTE_ICON
280 *****************************************************************************/
281 int osd_Icon( vlc_object_t *p_this, spu_t *p_spu,
282 int i_render_width, int i_render_height, int i_margin_right,
283 int i_margin_top, int i_channel, short i_type )
285 subpicture_t *p_subpic;
286 int i_x_margin, i_y_margin, i_x, i_y, i_width, i_height;
289 p_subpic = osd_CreateWidget( p_spu, i_channel );
290 if( p_subpic == NULL )
295 i_y_margin = i_render_height / 15;
296 i_x_margin = i_y_margin + i_margin_right;
297 i_y_margin += i_margin_top;
298 i_width = i_render_width / 20;
300 i_x = i_render_width - i_x_margin - i_width;
303 /* Create subpicture region and picture */
304 CreatePicture( p_spu, p_subpic, i_x, i_y, i_width, i_height );
306 if( i_type == OSD_PAUSE_ICON )
308 int i_bar_width = i_width / 3;
309 DrawRect( p_subpic, 0, 0, i_bar_width - 1, i_height -1, STYLE_FILLED );
310 DrawRect( p_subpic, i_width - i_bar_width, 0,
311 i_width - 1, i_height - 1, STYLE_FILLED );
313 else if( i_type == OSD_PLAY_ICON )
315 int i_mid = i_height >> 1;
316 int i_delta = ( i_width - i_mid ) >> 1;
317 int i_y2 = ( ( i_height - 1 ) >> 1 ) * 2;
318 DrawTriangle( p_subpic, i_delta, 0, i_width - i_delta, i_y2,
321 else if( i_type == OSD_SPEAKER_ICON || i_type == OSD_MUTE_ICON )
323 int i_mid = i_height >> 1;
324 int i_delta = ( i_width - i_mid ) >> 1;
325 int i_y2 = ( ( i_height - 1 ) >> 1 ) * 2;
326 DrawRect( p_subpic, i_delta, i_mid / 2, i_width - i_delta,
327 i_height - 1 - i_mid / 2, STYLE_FILLED );
328 DrawTriangle( p_subpic, i_width - i_delta, 0, i_delta, i_y2,
330 if( i_type == OSD_MUTE_ICON )
332 uint8_t *p_a = p_subpic->p_region->p_picture->A_PIXELS;
333 int i_pitch = p_subpic->p_region->p_picture->Y_PITCH;
335 for( i = 1; i < i_pitch; i++ )
337 int k = i + ( i_height - i - 1 ) * i_pitch;
338 p_a[ k ] = 0xff - p_a[ k ];
343 spu_DisplaySubpicture( p_spu, p_subpic );