]> git.sesse.net Git - vlc/blob - src/video_output/video_widgets.c
sftp: change item b_net
[vlc] / src / video_output / video_widgets.c
1 /*****************************************************************************
2  * video_widgets.c : OSD widgets manipulation functions
3  *****************************************************************************
4  * Copyright (C) 2004-2010 VLC authors and VideoLAN
5  * $Id$
6  *
7  * Author: Yoann Peronneau <yoann@videolan.org>
8  *         Laurent Aimar <fenrir _AT_ videolan _DOT_ org>
9  *
10  * This program is free software; you can redistribute it and/or modify it
11  * under the terms of the GNU Lesser General Public License as published by
12  * the Free Software Foundation; either version 2.1 of the License, or
13  * (at your option) any later version.
14  *
15  * This program is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18  * GNU Lesser General Public License for more details.
19  *
20  * You should have received a copy of the GNU Lesser General Public License
21  * along with this program; if not, write to the Free Software Foundation,
22  * Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
23  *****************************************************************************/
24
25 /*****************************************************************************
26  * Preamble
27  *****************************************************************************/
28 #ifdef HAVE_CONFIG_H
29 # include "config.h"
30 #endif
31 #include <assert.h>
32
33 #include <vlc_common.h>
34 #include <vlc_vout.h>
35 #include <vlc_vout_osd.h>
36
37 #include <vlc_filter.h>
38
39 #define STYLE_EMPTY 0
40 #define STYLE_FILLED 1
41
42 /**
43  * Draws a rectangle at the given position in the region.
44  * It may be filled (fill == STYLE_FILLED) or empty (fill == STYLE_EMPTY).
45  */
46 static void DrawRect(subpicture_region_t *r, int fill,
47                      int x1, int y1, int x2, int y2)
48 {
49     uint8_t *p    = r->p_picture->p->p_pixels;
50     int     pitch = r->p_picture->p->i_pitch;
51
52     if (fill == STYLE_FILLED) {
53         for (int y = y1; y <= y2; y++) {
54             for (int x = x1; x <= x2; x++)
55                 p[x + pitch * y] = 1;
56         }
57     } else {
58         for (int y = y1; y <= y2; y++) {
59             p[x1 + pitch * y] = 1;
60             p[x2 + pitch * y] = 1;
61         }
62         for (int x = x1; x <= x2; x++) {
63             p[x + pitch * y1] = 1;
64             p[x + pitch * y2] = 1;
65         }
66     }
67 }
68
69 /**
70  * Draws a triangle at the given position in the region.
71  * It may be filled (fill == STYLE_FILLED) or empty (fill == STYLE_EMPTY).
72  */
73 static void DrawTriangle(subpicture_region_t *r, int fill,
74                          int x1, int y1, int x2, int y2)
75 {
76     uint8_t *p    = r->p_picture->p->p_pixels;
77     int     pitch = r->p_picture->p->i_pitch;
78     const int mid = y1 + (y2 - y1) / 2;
79
80     /* TODO factorize it */
81     if (x2 >= x1) {
82         if (fill == STYLE_FILLED) {
83             for (int y = y1; y <= mid; y++) {
84                 int h = y - y1;
85                 for (int x = x1; x <= x1 + h && x <= x2; x++) {
86                     p[x + pitch * y         ] = 1;
87                     p[x + pitch * (y2 - h)] = 1;
88                 }
89             }
90         } else {
91             for (int y = y1; y <= mid; y++) {
92                 int h = y - y1;
93                 p[x1 +     pitch * y         ] = 1;
94                 p[x1 + h + pitch * y         ] = 1;
95                 p[x1 +     pitch * (y2 - h)] = 1;
96                 p[x1 + h + pitch * (y2 - h)] = 1;
97             }
98         }
99     } else {
100         if( fill == STYLE_FILLED) {
101             for (int y = y1; y <= mid; y++) {
102                 int h = y - y1;
103                 for (int x = x1; x >= x1 - h && x >= x2; x--) {
104                     p[x + pitch * y       ] = 1;
105                     p[x + pitch * (y2 - h)] = 1;
106                 }
107             }
108         } else {
109             for (int y = y1; y <= mid; y++) {
110                 int h = y - y1;
111                 p[ x1 +     pitch * y       ] = 1;
112                 p[ x1 - h + pitch * y       ] = 1;
113                 p[ x1 +     pitch * (y2 - h)] = 1;
114                 p[ x1 - h + pitch * (y2 - h)] = 1;
115             }
116         }
117     }
118 }
119
120 /**
121  * Create a region with a white transparent picture.
122  */
123 static subpicture_region_t *OSDRegion(int x, int y, int width, int height)
124 {
125     video_palette_t palette = {
126         .i_entries = 2,
127         .palette = {
128             [0] = { 0xff, 0x80, 0x80, 0x00 },
129             [1] = { 0xff, 0x80, 0x80, 0xff },
130         },
131     };
132
133     video_format_t fmt;
134     video_format_Init(&fmt, VLC_CODEC_YUVP);
135     fmt.i_width          =
136     fmt.i_visible_width  = width;
137     fmt.i_height         =
138     fmt.i_visible_height = height;
139     fmt.i_sar_num        = 1;
140     fmt.i_sar_den        = 1;
141     fmt.p_palette        = &palette;
142
143     subpicture_region_t *r = subpicture_region_New(&fmt);
144     if (!r)
145         return NULL;
146     r->i_x = x;
147     r->i_y = y;
148     memset(r->p_picture->p->p_pixels, 0, r->p_picture->p->i_pitch * height);
149
150     return r;
151 }
152
153 /**
154  * Create the region for an OSD slider.
155  * Types are: OSD_HOR_SLIDER and OSD_VERT_SLIDER.
156  */
157 static subpicture_region_t *OSDSlider(int type, int position,
158                                       const video_format_t *fmt)
159 {
160     const int size = __MAX(fmt->i_visible_width, fmt->i_visible_height);
161     const int margin = size * 0.10;
162
163     int x, y;
164     int width, height;
165     if (type == OSD_HOR_SLIDER) {
166         width  = __MAX(fmt->i_visible_width - 2 * margin, 1);
167         height = __MAX(fmt->i_visible_height * 0.05,      1);
168         x      = __MIN(fmt->i_x_offset + margin, fmt->i_visible_width - width);
169         y      = __MAX(fmt->i_y_offset + fmt->i_visible_height - margin, 0);
170     } else {
171         width  = __MAX(fmt->i_visible_width * 0.025,       1);
172         height = __MAX(fmt->i_visible_height - 2 * margin, 1);
173         x      = __MAX(fmt->i_x_offset + fmt->i_visible_width - margin, 0);
174         y      = __MIN(fmt->i_y_offset + margin, fmt->i_visible_height - height);
175     }
176
177     subpicture_region_t *r = OSDRegion(x, y, width, height);
178     if( !r)
179         return NULL;
180
181     if (type == OSD_HOR_SLIDER) {
182         int pos_x = (width - 2) * position / 100;
183         DrawRect(r, STYLE_FILLED, pos_x - 1, 2, pos_x + 1, height - 3);
184         DrawRect(r, STYLE_EMPTY,  0,         0, width - 1, height - 1);
185     } else {
186         int pos_mid = height / 2;
187         int pos_y   = height - (height - 2) * position / 100;
188         DrawRect(r, STYLE_FILLED, 2,         pos_y,   width - 3, height - 3);
189         DrawRect(r, STYLE_FILLED, 1,         pos_mid, 1,         pos_mid   );
190         DrawRect(r, STYLE_FILLED, width - 2, pos_mid, width - 2, pos_mid   );
191         DrawRect(r, STYLE_EMPTY,  0,         0,       width - 1, height - 1);
192     }
193     return r;
194 }
195
196 /**
197  * Create the region for an OSD slider.
198  * Types are: OSD_PLAY_ICON, OSD_PAUSE_ICON, OSD_SPEAKER_ICON, OSD_MUTE_ICON
199  */
200 static subpicture_region_t *OSDIcon(int type, const video_format_t *fmt)
201 {
202     const float size_ratio   = 0.05;
203     const float margin_ratio = 0.07;
204
205     const int size   = __MAX(fmt->i_visible_width, fmt->i_visible_height);
206     const int width  = size * size_ratio;
207     const int height = size * size_ratio;
208     const int x      = fmt->i_x_offset + fmt->i_visible_width - margin_ratio * size - width;
209     const int y      = fmt->i_y_offset                        + margin_ratio * size;
210
211     subpicture_region_t *r = OSDRegion(__MAX(x, 0),
212                                        __MIN(y, (int)fmt->i_visible_height - height),
213                                        width, height);
214     if (!r)
215         return NULL;
216
217     if (type == OSD_PAUSE_ICON) {
218         int bar_width = width / 3;
219         DrawRect(r, STYLE_FILLED, 0, 0, bar_width - 1, height -1);
220         DrawRect(r, STYLE_FILLED, width - bar_width, 0, width - 1, height - 1);
221     } else if (type == OSD_PLAY_ICON) {
222         int mid   = height >> 1;
223         int delta = (width - mid) >> 1;
224         int y2    = ((height - 1) >> 1) * 2;
225         DrawTriangle(r, STYLE_FILLED, delta, 0, width - delta, y2);
226     } else {
227         int mid   = height >> 1;
228         int delta = (width - mid) >> 1;
229         int y2    = ((height - 1) >> 1) * 2;
230         DrawRect(r, STYLE_FILLED, delta, mid / 2, width - delta, height - 1 - mid / 2);
231         DrawTriangle(r, STYLE_FILLED, width - delta, 0, delta, y2);
232         if (type == OSD_MUTE_ICON) {
233             uint8_t *a    = r->p_picture->A_PIXELS;
234             int     pitch = r->p_picture->A_PITCH;
235             for (int i = 1; i < pitch; i++) {
236                 int k = i + (height - i - 1) * pitch;
237                 a[k] = 0xff - a[k];
238             }
239         }
240     }
241     return r;
242 }
243
244 struct subpicture_updater_sys_t {
245     int type;
246     int position;
247 };
248
249 static int OSDWidgetValidate(subpicture_t *subpic,
250                            bool has_src_changed, const video_format_t *fmt_src,
251                            bool has_dst_changed, const video_format_t *fmt_dst,
252                            mtime_t ts)
253 {
254     VLC_UNUSED(subpic); VLC_UNUSED(ts);
255     VLC_UNUSED(fmt_src); VLC_UNUSED(has_src_changed);
256     VLC_UNUSED(fmt_dst);
257
258     if (!has_dst_changed)
259         return VLC_SUCCESS;
260     return VLC_EGENERIC;
261 }
262
263 static void OSDWidgetUpdate(subpicture_t *subpic,
264                           const video_format_t *fmt_src,
265                           const video_format_t *fmt_dst,
266                           mtime_t ts)
267 {
268     subpicture_updater_sys_t *sys = subpic->updater.p_sys;
269     VLC_UNUSED(fmt_src); VLC_UNUSED(ts);
270
271     video_format_t fmt = *fmt_dst;
272     fmt.i_width         = fmt.i_width         * fmt.i_sar_num / fmt.i_sar_den;
273     fmt.i_visible_width = fmt.i_visible_width * fmt.i_sar_num / fmt.i_sar_den;
274     fmt.i_x_offset      = fmt.i_x_offset      * fmt.i_sar_num / fmt.i_sar_den;
275     fmt.i_sar_num       = 1;
276     fmt.i_sar_den       = 1;
277
278     subpic->i_original_picture_width  = fmt.i_visible_width;
279     subpic->i_original_picture_height = fmt.i_visible_height;
280     if (sys->type == OSD_HOR_SLIDER || sys->type == OSD_VERT_SLIDER)
281         subpic->p_region = OSDSlider(sys->type, sys->position, &fmt);
282     else
283         subpic->p_region = OSDIcon(sys->type, &fmt);
284 }
285
286 static void OSDWidgetDestroy(subpicture_t *subpic)
287 {
288     free(subpic->updater.p_sys);
289 }
290
291 static void OSDWidget(vout_thread_t *vout, int channel, int type, int position)
292 {
293     if (!var_InheritBool(vout, "osd"))
294         return;
295     if (type == OSD_HOR_SLIDER || type == OSD_VERT_SLIDER)
296         position = VLC_CLIP(position, 0, 100);
297
298     subpicture_updater_sys_t *sys = malloc(sizeof(*sys));
299     if (!sys)
300         return;
301     sys->type     = type;
302     sys->position = position;
303
304     subpicture_updater_t updater = {
305         .pf_validate = OSDWidgetValidate,
306         .pf_update   = OSDWidgetUpdate,
307         .pf_destroy  = OSDWidgetDestroy,
308         .p_sys       = sys,
309     };
310     subpicture_t *subpic = subpicture_New(&updater);
311     if (!subpic) {
312         free(sys);
313         return;
314     }
315
316     subpic->i_channel  = channel;
317     subpic->i_start    = mdate();
318     subpic->i_stop     = subpic->i_start + 1200000;
319     subpic->b_ephemer  = true;
320     subpic->b_absolute = true;
321     subpic->b_fade     = true;
322
323     vout_PutSubpicture(vout, subpic);
324 }
325
326 void vout_OSDSlider(vout_thread_t *vout, int channel, int position, short type)
327 {
328     OSDWidget(vout, channel, type, position);
329 }
330
331 void vout_OSDIcon(vout_thread_t *vout, int channel, short type )
332 {
333     OSDWidget(vout, channel, type, 0);
334 }
335