]> git.sesse.net Git - vlc/blob - src/video_output/video_widgets.c
Use var_Inherit* instead of var_CreateGet*.
[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  *         Laurent Aimar <fenrir _AT_ videolan _DOT_ org>
9  *
10  * This program is free software; you can redistribute it and/or modify
11  * it under the terms of the GNU General Public License as published by
12  * the Free Software Foundation; either version 2 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 General Public License for more details.
19  *
20  * You should have received a copy of the GNU General Public License
21  * along with this program; if not, write to the Free Software
22  * Foundation, 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 *a    = r->p_picture->A_PIXELS;
50     int     pitch = r->p_picture->A_PITCH;
51
52     if (fill == STYLE_FILLED) {
53         for (int y = y1; y <= y2; y++) {
54             for (int x = x1; x <= x2; x++)
55                 a[x + pitch * y] = 0xff;
56         }
57     } else {
58         for (int y = y1; y <= y2; y++) {
59             a[x1 + pitch * y] = 0xff;
60             a[x2 + pitch * y] = 0xff;
61         }
62         for (int x = x1; x <= x2; x++) {
63             a[x + pitch * y1] = 0xff;
64             a[x + pitch * y2] = 0xff;
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 *a    = r->p_picture->A_PIXELS;
77     int     pitch = r->p_picture->A_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                     a[x + pitch * y         ] = 0xff;
87                     a[x + pitch * (y2 - h)] = 0xff;
88                 }
89             }
90         } else {
91             for (int y = y1; y <= mid; y++) {
92                 int h = y - y1;
93                 a[x1 +     pitch * y         ] = 0xff;
94                 a[x1 + h + pitch * y         ] = 0xff;
95                 a[x1 +     pitch * (y2 - h)] = 0xff;
96                 a[x1 + h + pitch * (y2 - h)] = 0xff;
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                     a[x + pitch * y       ] = 0xff;
105                     a[x + pitch * (y2 - h)] = 0xff;
106                 }
107             }
108         } else {
109             for (int y = y1; y <= mid; y++) {
110                 int h = y - y1;
111                 a[ x1 +     pitch * y       ] = 0xff;
112                 a[ x1 - h + pitch * y       ] = 0xff;
113                 a[ x1 +     pitch * (y2 - h)] = 0xff;
114                 a[ x1 - h + pitch * (y2 - h)] = 0xff;
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_format_t fmt;
126     video_format_Init(&fmt, VLC_CODEC_YUVA);
127     fmt.i_width          =
128     fmt.i_visible_width  = width;
129     fmt.i_height         =
130     fmt.i_visible_height = height;
131     fmt.i_sar_num        = 0;
132     fmt.i_sar_den        = 1;
133
134     subpicture_region_t *r = subpicture_region_New(&fmt);
135     if (!r)
136         return NULL;
137     r->i_x = x;
138     r->i_y = y;
139
140     for (int i = 0; i < r->p_picture->i_planes; i++) {
141         plane_t *p = &r->p_picture->p[i];
142         int colors[PICTURE_PLANE_MAX] = {
143             0xff, 0x80, 0x80, 0x00
144         };
145         memset(p->p_pixels, colors[i], p->i_pitch * height);
146     }
147     return r;
148 }
149
150 /**
151  * Create the region for an OSD slider.
152  * Types are: OSD_HOR_SLIDER and OSD_VERT_SLIDER.
153  */
154 static subpicture_region_t *OSDSlider(int type, int position,
155                                       const video_format_t *fmt)
156 {
157     const int size = __MAX(fmt->i_visible_width, fmt->i_visible_height);
158     const int margin = size * 0.10;
159
160     int x, y;
161     int width, height;
162     if (type == OSD_HOR_SLIDER) {
163         width  = __MAX(fmt->i_visible_width - 2 * margin, 1);
164         height = __MAX(fmt->i_visible_height * 0.05,      1);
165         x      = __MIN(fmt->i_x_offset + margin, fmt->i_visible_width - width);
166         y      = __MAX(fmt->i_y_offset + fmt->i_visible_height - margin, 0);
167     } else {
168         width  = __MAX(fmt->i_visible_width * 0.025,       1);
169         height = __MAX(fmt->i_visible_height - 2 * margin, 1);
170         x      = __MAX(fmt->i_x_offset + fmt->i_visible_width - margin, 0);
171         y      = __MIN(fmt->i_y_offset + margin, fmt->i_visible_height - height);
172     }
173
174     subpicture_region_t *r = OSDRegion(x, y, width, height);
175     if( !r)
176         return NULL;
177
178     if (type == OSD_HOR_SLIDER) {
179         int pos_x = (width - 2) * position / 100;
180         DrawRect(r, STYLE_FILLED, pos_x - 1, 2, pos_x + 1, height - 3);
181         DrawRect(r, STYLE_EMPTY,  0,         0, width - 1, height - 1);
182     } else {
183         int pos_mid = height / 2;
184         int pos_y   = height - (height - 2) * position / 100;
185         DrawRect(r, STYLE_FILLED, 2,         pos_y,   width - 3, height - 3);
186         DrawRect(r, STYLE_FILLED, 1,         pos_mid, 1,         pos_mid   );
187         DrawRect(r, STYLE_FILLED, width - 2, pos_mid, width - 2, pos_mid   );
188         DrawRect(r, STYLE_EMPTY,  0,         0,       width - 1, height - 1);
189     }
190     return r;
191 }
192
193 /**
194  * Create the region for an OSD slider.
195  * Types are: OSD_PLAY_ICON, OSD_PAUSE_ICON, OSD_SPEAKER_ICON, OSD_MUTE_ICON
196  */
197 static subpicture_region_t *OSDIcon(int type, const video_format_t *fmt)
198 {
199     const float size_ratio   = 0.05;
200     const float margin_ratio = 0.07;
201
202     const int size   = __MAX(fmt->i_visible_width, fmt->i_visible_height);
203     const int width  = size * size_ratio;
204     const int height = size * size_ratio;
205     const int x      = fmt->i_x_offset + fmt->i_visible_width - margin_ratio * size - width;
206     const int y      = fmt->i_y_offset                        + margin_ratio * size;
207
208     subpicture_region_t *r = OSDRegion(__MAX(x, 0),
209                                        __MIN(y, (int)fmt->i_visible_height - height),
210                                        width, height);
211     if (!r)
212         return NULL;
213
214     if (type == OSD_PAUSE_ICON) {
215         int bar_width = width / 3;
216         DrawRect(r, STYLE_FILLED, 0, 0, bar_width - 1, height -1);
217         DrawRect(r, STYLE_FILLED, width - bar_width, 0, width - 1, height - 1);
218     } else if (type == OSD_PLAY_ICON) {
219         int mid   = height >> 1;
220         int delta = (width - mid) >> 1;
221         int y2    = ((height - 1) >> 1) * 2;
222         DrawTriangle(r, STYLE_FILLED, delta, 0, width - delta, y2);
223     } else {
224         int mid   = height >> 1;
225         int delta = (width - mid) >> 1;
226         int y2    = ((height - 1) >> 1) * 2;
227         DrawRect(r, STYLE_FILLED, delta, mid / 2, width - delta, height - 1 - mid / 2);
228         DrawTriangle(r, STYLE_FILLED, width - delta, 0, delta, y2);
229         if (type == OSD_MUTE_ICON) {
230             uint8_t *a    = r->p_picture->A_PIXELS;
231             int     pitch = r->p_picture->A_PITCH;
232             for (int i = 1; i < pitch; i++) {
233                 int k = i + (height - i - 1) * pitch;
234                 a[k] = 0xff - a[k];
235             }
236         }
237     }
238     return r;
239 }
240
241 struct subpicture_updater_sys_t {
242     int type;
243     int position;
244 };
245
246 static int OSDWidgetValidate(subpicture_t *subpic,
247                            bool has_src_changed, const video_format_t *fmt_src,
248                            bool has_dst_changed, const video_format_t *fmt_dst,
249                            mtime_t ts)
250 {
251     VLC_UNUSED(subpic); VLC_UNUSED(ts); VLC_UNUSED(fmt_src);
252     VLC_UNUSED(has_dst_changed); VLC_UNUSED(fmt_dst);
253
254     if (!has_src_changed && !has_dst_changed)
255         return VLC_SUCCESS;
256     return VLC_EGENERIC;
257 }
258
259 static void OSDWidgetUpdate(subpicture_t *subpic,
260                           const video_format_t *fmt_src,
261                           const video_format_t *fmt_dst,
262                           mtime_t ts)
263 {
264     subpicture_updater_sys_t *sys = subpic->updater.p_sys;
265     VLC_UNUSED(fmt_dst); VLC_UNUSED(ts);
266
267     subpic->i_original_picture_width  = fmt_src->i_width;
268     subpic->i_original_picture_height = fmt_src->i_height;
269     if (sys->type == OSD_HOR_SLIDER || sys->type == OSD_VERT_SLIDER)
270         subpic->p_region = OSDSlider(sys->type, sys->position, fmt_src);
271     else
272         subpic->p_region = OSDIcon(sys->type, fmt_src);
273 }
274
275 static void OSDWidgetDestroy(subpicture_t *subpic)
276 {
277     free(subpic->updater.p_sys);
278 }
279
280 static void OSDWidget(vout_thread_t *vout, int channel, int type, int position)
281 {
282     if (!var_InheritBool(vout, "osd"))
283         return;
284     if (type == OSD_HOR_SLIDER || type == OSD_VERT_SLIDER)
285         position = __MIN(__MAX(position, 0), 100);
286
287     subpicture_updater_sys_t *sys = malloc(sizeof(*sys));
288     if (!sys)
289         return;
290     sys->type     = type;
291     sys->position = position;
292
293     subpicture_updater_t updater = {
294         .pf_validate = OSDWidgetValidate,
295         .pf_update   = OSDWidgetUpdate,
296         .pf_destroy  = OSDWidgetDestroy,
297         .p_sys       = sys,
298     };
299     subpicture_t *subpic = subpicture_New(&updater);
300     if (!subpic) {
301         free(sys);
302         return;
303     }
304
305     subpic->i_channel  = channel;
306     subpic->i_start    = mdate();
307     subpic->i_stop     = subpic->i_start + 1200000;
308     subpic->b_ephemer  = true;
309     subpic->b_absolute = true;
310     subpic->b_fade     = true;
311
312     vout_PutSubpicture(vout, subpic);
313 }
314
315 void vout_OSDSlider(vout_thread_t *vout, int channel, int position, short type)
316 {
317     OSDWidget(vout, channel, type, position);
318 }
319
320 void vout_OSDIcon(vout_thread_t *vout, int channel, short type )
321 {
322     OSDWidget(vout, channel, type, 0);
323 }
324