]> git.sesse.net Git - vlc/blob - src/video_output/video_widgets.c
335edefc03cb008dc674a16fe3d0ecd3ee5cd731
[vlc] / src / video_output / video_widgets.c
1 /*****************************************************************************
2  * video_widgets.c : OSD widgets manipulation functions
3  *****************************************************************************
4  * Copyright (C) 2004-2010 the VideoLAN team
5  * $Id$
6  *
7  * Author: Yoann Peronneau <yoann@videolan.org>
8  *
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.
13  *
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.
18  *
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  *****************************************************************************/
23
24 /*****************************************************************************
25  * Preamble
26  *****************************************************************************/
27 #ifdef HAVE_CONFIG_H
28 # include "config.h"
29 #endif
30 #include <assert.h>
31
32 #include <vlc_common.h>
33 #include <vlc_vout.h>
34 #include <vlc_vout_osd.h>
35
36 #include <vlc_filter.h>
37
38 /* TODO remove access to private vout data */
39 #include "vout_internal.h"
40
41 #define STYLE_EMPTY 0
42 #define STYLE_FILLED 1
43
44 /*****************************************************************************
45  * Draws a rectangle at the given position in the subpic.
46  * It may be filled (fill == STYLE_FILLED) or empty (fill == STYLE_EMPTY).
47  *****************************************************************************/
48 static void DrawRect( subpicture_t *p_subpic, int i_x1, int i_y1,
49                       int i_x2, int i_y2, short fill )
50 {
51     int x, y;
52     uint8_t *p_a = p_subpic->p_region->p_picture->A_PIXELS;
53     int i_pitch = p_subpic->p_region->p_picture->Y_PITCH;
54
55     if( fill == STYLE_FILLED )
56     {
57         for( y = i_y1; y <= i_y2; y++ )
58         {
59             for( x = i_x1; x <= i_x2; x++ )
60             {
61                 p_a[ x + i_pitch * y ] = 0xff;
62             }
63         }
64     }
65     else
66     {
67         for( y = i_y1; y <= i_y2; y++ )
68         {
69             p_a[ i_x1 + i_pitch * y ] = 0xff;
70             p_a[ i_x2 + i_pitch * y ] = 0xff;
71         }
72         for( x = i_x1; x <= i_x2; x++ )
73         {
74             p_a[ x + i_pitch * i_y1 ] = 0xff;
75             p_a[ x + i_pitch * i_y2 ] = 0xff;
76         }
77     }
78 }
79
80 /*****************************************************************************
81  * Draws a triangle at the given position in the subpic.
82  * It may be filled (fill == STYLE_FILLED) or empty (fill == STYLE_EMPTY).
83  *****************************************************************************/
84 static void DrawTriangle( subpicture_t *p_subpic, int i_x1, int i_y1,
85                           int i_x2, int i_y2, short fill )
86 {
87     int x, y, i_mid, h;
88     uint8_t *p_a = p_subpic->p_region->p_picture->A_PIXELS;
89     int i_pitch = p_subpic->p_region->p_picture->Y_PITCH;
90
91     i_mid = i_y1 + ( ( i_y2 - i_y1 ) >> 1 );
92
93     if( i_x2 >= i_x1 )
94     {
95         if( fill == STYLE_FILLED )
96         {
97             for( y = i_y1; y <= i_mid; y++ )
98             {
99                 h = y - i_y1;
100                 for( x = i_x1; x <= i_x1 + h && x <= i_x2; x++ )
101                 {
102                     p_a[ x + i_pitch * y ] = 0xff;
103                     p_a[ x + i_pitch * ( i_y2 - h ) ] = 0xff;
104                 }
105             }
106         }
107         else
108         {
109             for( y = i_y1; y <= i_mid; y++ )
110             {
111                 h = y - i_y1;
112                 p_a[ i_x1 + i_pitch * y ] = 0xff;
113                 p_a[ i_x1 + h + i_pitch * y ] = 0xff;
114                 p_a[ i_x1 + i_pitch * ( i_y2 - h ) ] = 0xff;
115                 p_a[ i_x1 + h + i_pitch * ( i_y2 - h ) ] = 0xff;
116             }
117         }
118     }
119     else
120     {
121         if( fill == STYLE_FILLED )
122         {
123             for( y = i_y1; y <= i_mid; y++ )
124             {
125                 h = y - i_y1;
126                 for( x = i_x1; x >= i_x1 - h && x >= i_x2; x-- )
127                 {
128                     p_a[ x + i_pitch * y ] = 0xff;
129                     p_a[ x + i_pitch * ( i_y2 - h ) ] = 0xff;
130                 }
131             }
132         }
133         else
134         {
135             for( y = i_y1; y <= i_mid; y++ )
136             {
137                 h = y - i_y1;
138                 p_a[ i_x1 + i_pitch * y ] = 0xff;
139                 p_a[ i_x1 - h + i_pitch * y ] = 0xff;
140                 p_a[ i_x1 + i_pitch * ( i_y2 - h ) ] = 0xff;
141                 p_a[ i_x1 - h + i_pitch * ( i_y2 - h ) ] = 0xff;
142             }
143         }
144     }
145 }
146
147 /*****************************************************************************
148  * Create Picture: creates subpicture region and picture
149  *****************************************************************************/
150 static int CreatePicture( subpicture_t *p_subpic,
151                           int i_x, int i_y, int i_width, int i_height )
152 {
153     uint8_t *p_y, *p_u, *p_v, *p_a;
154     video_format_t fmt;
155     int i_pitch;
156
157     /* Create a new subpicture region */
158     memset( &fmt, 0, sizeof(video_format_t) );
159     fmt.i_chroma = VLC_CODEC_YUVA;
160     fmt.i_width = fmt.i_visible_width = i_width;
161     fmt.i_height = fmt.i_visible_height = i_height;
162     fmt.i_x_offset = fmt.i_y_offset = 0;
163     p_subpic->p_region = subpicture_region_New( &fmt );
164     if( !p_subpic->p_region )
165         return VLC_EGENERIC;
166
167     p_subpic->p_region->i_x = i_x;
168     p_subpic->p_region->i_y = i_y;
169     p_y = p_subpic->p_region->p_picture->Y_PIXELS;
170     p_u = p_subpic->p_region->p_picture->U_PIXELS;
171     p_v = p_subpic->p_region->p_picture->V_PIXELS;
172     p_a = p_subpic->p_region->p_picture->A_PIXELS;
173     i_pitch = p_subpic->p_region->p_picture->Y_PITCH;
174
175     /* Initialize the region pixels (only the alpha will be changed later) */
176     memset( p_y, 0xff, i_pitch * p_subpic->p_region->fmt.i_height );
177     memset( p_u, 0x80, i_pitch * p_subpic->p_region->fmt.i_height );
178     memset( p_v, 0x80, i_pitch * p_subpic->p_region->fmt.i_height );
179     memset( p_a, 0x00, i_pitch * p_subpic->p_region->fmt.i_height );
180
181     return VLC_SUCCESS;
182 }
183
184 /*****************************************************************************
185  * Creates and initializes an OSD widget.
186  *****************************************************************************/
187 static subpicture_t *CreateWidget( int i_channel )
188 {
189     subpicture_t *p_subpic;
190     mtime_t i_now = mdate();
191
192     /* Create and initialize a subpicture */
193     p_subpic = subpicture_New( NULL );
194     if( p_subpic == NULL ) return NULL;
195
196     p_subpic->i_channel = i_channel;
197     p_subpic->i_start = i_now;
198     p_subpic->i_stop = i_now + 1200000;
199     p_subpic->b_ephemer = true;
200     p_subpic->b_fade = true;
201
202     return p_subpic;
203 }
204
205 /*****************************************************************************
206  * Displays an OSD slider.
207  * Types are: OSD_HOR_SLIDER and OSD_VERT_SLIDER.
208  *****************************************************************************/
209 static int OsdSlider( spu_t *p_spu,
210                       int i_render_width, int i_render_height,
211                       int i_margin_left, int i_margin_bottom,
212                       int i_channel, int i_position, short i_type )
213 {
214     subpicture_t *p_subpic;
215     int i_x_margin, i_y_margin, i_x, i_y, i_width, i_height;
216
217     p_subpic = CreateWidget( i_channel );
218     if( p_subpic == NULL )
219     {
220         return VLC_EGENERIC;
221     }
222
223     i_y_margin = i_render_height / 10;
224     i_x_margin = i_y_margin + i_margin_left;
225     i_y_margin += i_margin_bottom;
226
227     if( i_type == OSD_HOR_SLIDER )
228     {
229         i_width = i_render_width - 2 * i_x_margin;
230         i_height = i_render_height / 20;
231         i_x = i_x_margin;
232         i_y = i_render_height - i_y_margin - i_height;
233     }
234     else
235     {
236         i_width = i_render_width / 40;
237         i_height = i_render_height - 2 * i_y_margin;
238         i_x = i_render_width - i_x_margin - i_width;
239         i_y = i_y_margin;
240     }
241
242     /* Create subpicture region and picture */
243     CreatePicture( p_subpic, i_x, i_y, i_width, i_height );
244
245     if( i_type == OSD_HOR_SLIDER )
246     {
247         int i_x_pos = ( i_width - 2 ) * i_position / 100;
248         DrawRect( p_subpic, i_x_pos - 1, 2, i_x_pos + 1,
249                   i_height - 3, STYLE_FILLED );
250         DrawRect( p_subpic, 0, 0, i_width - 1, i_height - 1, STYLE_EMPTY );
251     }
252     else if( i_type == OSD_VERT_SLIDER )
253     {
254         int i_y_pos = i_height / 2;
255         DrawRect( p_subpic, 2, i_height - ( i_height - 2 ) * i_position / 100,
256                   i_width - 3, i_height - 3, STYLE_FILLED );
257         DrawRect( p_subpic, 1, i_y_pos, 1, i_y_pos, STYLE_FILLED );
258         DrawRect( p_subpic, i_width - 2, i_y_pos,
259                   i_width - 2, i_y_pos, STYLE_FILLED );
260         DrawRect( p_subpic, 0, 0, i_width - 1, i_height - 1, STYLE_EMPTY );
261     }
262
263     spu_DisplaySubpicture( p_spu, p_subpic );
264
265     return VLC_SUCCESS;
266 }
267
268 /*****************************************************************************
269  * Displays an OSD icon.
270  * Types are: OSD_PLAY_ICON, OSD_PAUSE_ICON, OSD_SPEAKER_ICON, OSD_MUTE_ICON
271  *****************************************************************************/
272 static int OsdIcon( spu_t *p_spu,
273                     int i_render_width, int i_render_height, int i_margin_right,
274                     int i_margin_top, int i_channel, short i_type )
275 {
276     subpicture_t *p_subpic;
277     int i_x_margin, i_y_margin, i_x, i_y, i_width, i_height;
278
279     p_subpic = CreateWidget( i_channel );
280     if( p_subpic == NULL )
281     {
282         return VLC_EGENERIC;
283     }
284
285     i_y_margin = i_render_height / 15;
286     i_x_margin = i_y_margin + i_margin_right;
287     i_y_margin += i_margin_top;
288     i_width = i_render_width / 20;
289     i_height = i_width;
290     i_x = i_render_width - i_x_margin - i_width;
291     i_y = i_y_margin;
292
293     /* Create subpicture region and picture */
294     CreatePicture( p_subpic, i_x, i_y, i_width, i_height );
295
296     if( i_type == OSD_PAUSE_ICON )
297     {
298         int i_bar_width = i_width / 3;
299         DrawRect( p_subpic, 0, 0, i_bar_width - 1, i_height -1, STYLE_FILLED );
300         DrawRect( p_subpic, i_width - i_bar_width, 0,
301                   i_width - 1, i_height - 1, STYLE_FILLED );
302     }
303     else if( i_type == OSD_PLAY_ICON )
304     {
305         int i_mid = i_height >> 1;
306         int i_delta = ( i_width - i_mid ) >> 1;
307         int i_y2 = ( ( i_height - 1 ) >> 1 ) * 2;
308         DrawTriangle( p_subpic, i_delta, 0, i_width - i_delta, i_y2,
309                       STYLE_FILLED );
310     }
311     else if( i_type == OSD_SPEAKER_ICON || i_type == OSD_MUTE_ICON )
312     {
313         int i_mid = i_height >> 1;
314         int i_delta = ( i_width - i_mid ) >> 1;
315         int i_y2 = ( ( i_height - 1 ) >> 1 ) * 2;
316         DrawRect( p_subpic, i_delta, i_mid / 2, i_width - i_delta,
317                   i_height - 1 - i_mid / 2, STYLE_FILLED );
318         DrawTriangle( p_subpic, i_width - i_delta, 0, i_delta, i_y2,
319                       STYLE_FILLED );
320         if( i_type == OSD_MUTE_ICON )
321         {
322             uint8_t *p_a = p_subpic->p_region->p_picture->A_PIXELS;
323             int i_pitch = p_subpic->p_region->p_picture->Y_PITCH;
324             int i;
325             for( i = 1; i < i_pitch; i++ )
326             {
327                 int k = i + ( i_height - i - 1 ) * i_pitch;
328                 p_a[ k ] = 0xff - p_a[ k ];
329             }
330         }
331     }
332
333     spu_DisplaySubpicture( p_spu, p_subpic );
334
335     return VLC_SUCCESS;
336 }
337 /*****************************************************************************
338  * Displays an OSD slider.
339  * Types are: OSD_HOR_SLIDER and OSD_VERT_SLIDER.
340  *****************************************************************************/
341 void vout_OSDSlider( vout_thread_t *p_vout, int i_channel, int i_position,
342                      short i_type )
343 {
344     if( !var_InheritBool( p_vout, "osd" ) || i_position < 0 )
345         return;
346
347     OsdSlider( vout_GetSpu( p_vout ),
348                p_vout->p->fmt_render.i_width,
349                p_vout->p->fmt_render.i_height,
350                p_vout->p->fmt_in.i_x_offset,
351                p_vout->p->fmt_in.i_height - p_vout->p->fmt_in.i_visible_height
352                                           - p_vout->p->fmt_in.i_y_offset,
353                i_channel, i_position, i_type );
354 }
355
356 /*****************************************************************************
357  * Displays an OSD icon.
358  * Types are: OSD_PLAY_ICON, OSD_PAUSE_ICON, OSD_SPEAKER_ICON, OSD_MUTE_ICON
359  *****************************************************************************/
360 void vout_OSDIcon( vout_thread_t *p_vout, int i_channel, short i_type )
361 {
362     if( !var_InheritBool( p_vout, "osd" ) )
363         return;
364
365     OsdIcon( vout_GetSpu( p_vout ),
366              p_vout->p->fmt_render.i_width,
367              p_vout->p->fmt_render.i_height,
368              p_vout->p->fmt_in.i_width - p_vout->p->fmt_in.i_visible_width
369                                        - p_vout->p->fmt_in.i_x_offset,
370              p_vout->p->fmt_in.i_y_offset,
371              i_channel, i_type );
372 }
373