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