fourcc: add VLC_CODEC_VAAPI_420_10BPP fallbacks
[vlc.git] / src / video_output / video_widgets.c
blob201f90773c51c56eeaec6b724f323f62584579a7
1 /*****************************************************************************
2 * video_widgets.c : OSD widgets manipulation functions
3 *****************************************************************************
4 * Copyright (C) 2004-2010 VLC authors and VideoLAN
5 * $Id$
7 * Author: Yoann Peronneau <yoann@videolan.org>
8 * Laurent Aimar <fenrir _AT_ videolan _DOT_ org>
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.
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.
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 *****************************************************************************/
25 /*****************************************************************************
26 * Preamble
27 *****************************************************************************/
28 #ifdef HAVE_CONFIG_H
29 # include "config.h"
30 #endif
31 #include <assert.h>
33 #include <vlc_common.h>
34 #include <vlc_vout.h>
35 #include <vlc_vout_osd.h>
37 #include <vlc_filter.h>
39 #define STYLE_EMPTY 0
40 #define STYLE_FILLED 1
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).
46 static void DrawRect(subpicture_region_t *r, int fill,
47 int x1, int y1, int x2, int y2)
49 uint8_t *p = r->p_picture->p->p_pixels;
50 int pitch = r->p_picture->p->i_pitch;
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;
57 } else {
58 for (int y = y1; y <= y2; y++) {
59 p[x1 + pitch * y] = 1;
60 p[x2 + pitch * y] = 1;
62 for (int x = x1; x <= x2; x++) {
63 p[x + pitch * y1] = 1;
64 p[x + pitch * y2] = 1;
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).
73 static void DrawTriangle(subpicture_region_t *r, int fill,
74 int x1, int y1, int x2, int y2)
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;
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;
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;
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;
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;
121 * Create a region with a white transparent picture.
123 static subpicture_region_t *OSDRegion(int x, int y, int width, int height)
125 video_palette_t palette = {
126 .i_entries = 2,
127 .palette = {
128 [0] = { 0xff, 0x80, 0x80, 0x00 },
129 [1] = { 0xff, 0x80, 0x80, 0xff },
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;
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);
150 return r;
154 * Create the region for an OSD slider.
155 * Types are: OSD_HOR_SLIDER and OSD_VERT_SLIDER.
157 static subpicture_region_t *OSDSlider(int type, int position,
158 const video_format_t *fmt)
160 const int size = __MAX(fmt->i_visible_width, fmt->i_visible_height);
161 const int margin = size * 0.10;
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);
177 subpicture_region_t *r = OSDRegion(x, y, width, height);
178 if( !r)
179 return NULL;
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);
193 return r;
197 * Create the region for an OSD slider.
198 * Types are: OSD_PLAY_ICON, OSD_PAUSE_ICON, OSD_SPEAKER_ICON, OSD_MUTE_ICON
200 static subpicture_region_t *OSDIcon(int type, const video_format_t *fmt)
202 const float size_ratio = 0.05;
203 const float margin_ratio = 0.07;
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;
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;
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];
241 return r;
244 struct subpicture_updater_sys_t {
245 int type;
246 int position;
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)
254 VLC_UNUSED(subpic); VLC_UNUSED(ts);
255 VLC_UNUSED(fmt_src); VLC_UNUSED(has_src_changed);
256 VLC_UNUSED(fmt_dst);
258 if (!has_dst_changed)
259 return VLC_SUCCESS;
260 return VLC_EGENERIC;
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)
268 subpicture_updater_sys_t *sys = subpic->updater.p_sys;
269 VLC_UNUSED(fmt_src); VLC_UNUSED(ts);
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;
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);
286 static void OSDWidgetDestroy(subpicture_t *subpic)
288 free(subpic->updater.p_sys);
291 static void OSDWidget(vout_thread_t *vout, int channel, int type, int position)
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);
298 subpicture_updater_sys_t *sys = malloc(sizeof(*sys));
299 if (!sys)
300 return;
301 sys->type = type;
302 sys->position = position;
304 subpicture_updater_t updater = {
305 .pf_validate = OSDWidgetValidate,
306 .pf_update = OSDWidgetUpdate,
307 .pf_destroy = OSDWidgetDestroy,
308 .p_sys = sys,
310 subpicture_t *subpic = subpicture_New(&updater);
311 if (!subpic) {
312 free(sys);
313 return;
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;
323 vout_PutSubpicture(vout, subpic);
326 void vout_OSDSlider(vout_thread_t *vout, int channel, int position, short type)
328 OSDWidget(vout, channel, type, position);
331 void vout_OSDIcon(vout_thread_t *vout, int channel, short type )
333 OSDWidget(vout, channel, type, 0);