3 * Copyright (C) 2014 Luca Barbato <lu_zero@gentoo.org>
5 * This file is part of Libav.
7 * Libav is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Lesser General Public
9 * License as published by the Free Software Foundation; either
10 * version 2.1 of the License, or (at your option) any later version.
12 * Libav is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with Libav; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
27 #if CONFIG_LIBXCB_XFIXES
28 #include <xcb/xfixes.h>
36 #include "libavformat/avformat.h"
37 #include "libavformat/internal.h"
39 #include "libavutil/mathematics.h"
40 #include "libavutil/opt.h"
41 #include "libavutil/parseutils.h"
42 #include "libavutil/time.h"
44 typedef struct XCBGrabContext {
47 xcb_connection_t *conn;
50 xcb_shm_seg_t segment;
66 const char *video_size;
67 const char *framerate;
72 #define FOLLOW_CENTER -1
74 #define OFFSET(x) offsetof(XCBGrabContext, x)
75 #define D AV_OPT_FLAG_DECODING_PARAM
76 static const AVOption options[] = {
77 { "x", "Initial x coordinate.", OFFSET(x), AV_OPT_TYPE_INT, { .i64 = 0 }, 0, INT_MAX, D },
78 { "y", "Initial y coordinate.", OFFSET(y), AV_OPT_TYPE_INT, { .i64 = 0 }, 0, INT_MAX, D },
79 { "video_size", "A string describing frame size, such as 640x480 or hd720.", OFFSET(video_size), AV_OPT_TYPE_STRING, {.str = "vga" }, 0, 0, D },
80 { "framerate", "", OFFSET(framerate), AV_OPT_TYPE_STRING, {.str = "ntsc" }, 0, 0, D },
81 { "draw_mouse", "Draw the mouse pointer.", OFFSET(draw_mouse), AV_OPT_TYPE_INT, { .i64 = 1 }, 0, 1, D },
82 { "follow_mouse", "Move the grabbing region when the mouse pointer reaches within specified amount of pixels to the edge of region.",
83 OFFSET(follow_mouse), AV_OPT_TYPE_INT, { .i64 = 0 }, FOLLOW_CENTER, INT_MAX, D, "follow_mouse" },
84 { "centered", "Keep the mouse pointer at the center of grabbing region when following.", 0, AV_OPT_TYPE_CONST, { .i64 = -1 }, INT_MIN, INT_MAX, D, "follow_mouse" },
85 { "show_region", "Show the grabbing region.", OFFSET(show_region), AV_OPT_TYPE_INT, { .i64 = 0 }, 0, 1, D },
86 { "region_border", "Set the region border thickness.", OFFSET(region_border), AV_OPT_TYPE_INT, { .i64 = 3 }, 1, 128, D },
90 static const AVClass xcbgrab_class = {
91 .class_name = "xcbgrab indev",
92 .item_name = av_default_item_name,
94 .version = LIBAVUTIL_VERSION_INT,
97 static int xcbgrab_reposition(AVFormatContext *s,
98 xcb_query_pointer_reply_t *p,
99 xcb_get_geometry_reply_t *geo)
101 XCBGrabContext *c = s->priv_data;
102 int x = c->x, y = c->y, p_x = p->win_x, p_y = p->win_y;
103 int w = c->width, h = c->height, f = c->follow_mouse;
108 if (f == FOLLOW_CENTER) {
113 int right = x + w - f;
115 int bottom = y + h + f;
118 } else if (p_x < left) {
123 } else if (p_y < top) {
128 c->x = FFMIN(FFMAX(0, x), geo->width - w);
129 c->y = FFMIN(FFMAX(0, y), geo->height - h);
134 static int xcbgrab_frame(AVFormatContext *s, AVPacket *pkt)
136 XCBGrabContext *c = s->priv_data;
137 xcb_get_image_cookie_t iq;
138 xcb_get_image_reply_t *img;
139 xcb_drawable_t drawable = c->screen->root;
143 iq = xcb_get_image(c->conn, XCB_IMAGE_FORMAT_Z_PIXMAP, drawable,
144 c->x, c->y, c->width, c->height, ~0);
146 img = xcb_get_image_reply(c->conn, iq, NULL);
148 return AVERROR(EAGAIN);
150 data = xcb_get_image_data(img);
151 length = xcb_get_image_data_length(img);
153 ret = av_new_packet(pkt, length);
156 memcpy(pkt->data, data, length);
163 static void wait_frame(AVFormatContext *s, AVPacket *pkt)
165 XCBGrabContext *c = s->priv_data;
166 int64_t curtime, delay;
167 int64_t frame_time = av_rescale_q(1, c->time_base, AV_TIME_BASE_Q);
169 c->time_frame += frame_time;
172 curtime = av_gettime();
173 delay = c->time_frame - curtime;
182 #if CONFIG_LIBXCB_SHM
183 static int check_shm(xcb_connection_t *conn)
185 xcb_shm_query_version_cookie_t cookie = xcb_shm_query_version(conn);
186 xcb_shm_query_version_reply_t *reply;
188 reply = xcb_shm_query_version_reply(conn, cookie, NULL);
197 static void dealloc_shm(void *unused, uint8_t *data)
202 static int xcbgrab_frame_shm(AVFormatContext *s, AVPacket *pkt)
204 XCBGrabContext *c = s->priv_data;
205 xcb_shm_get_image_cookie_t iq;
206 xcb_shm_get_image_reply_t *img;
207 xcb_drawable_t drawable = c->screen->root;
209 int size = c->frame_size + FF_INPUT_BUFFER_PADDING_SIZE;
210 int id = shmget(IPC_PRIVATE, size, IPC_CREAT | 0777);
211 xcb_generic_error_t *e = NULL;
215 int err = AVERROR(errno);
216 av_strerror(err, errbuf, sizeof(errbuf));
217 av_log(s, AV_LOG_ERROR, "Cannot get %d bytes of shared memory: %s.\n",
222 xcb_shm_attach(c->conn, c->segment, id, 0);
224 iq = xcb_shm_get_image(c->conn, drawable,
225 c->x, c->y, c->width, c->height, ~0,
226 XCB_IMAGE_FORMAT_Z_PIXMAP, c->segment, 0);
228 xcb_shm_detach(c->conn, c->segment);
230 img = xcb_shm_get_image_reply(c->conn, iq, &e);
235 av_log(s, AV_LOG_ERROR,
236 "Cannot get the image data "
237 "event_error: response_type:%u error_code:%u "
238 "sequence:%u resource_id:%u minor_code:%u major_code:%u.\n",
239 e->response_type, e->error_code,
240 e->sequence, e->resource_id, e->minor_code, e->major_code);
242 shmctl(id, IPC_RMID, 0);
243 return AVERROR(EACCES);
248 data = shmat(id, NULL, 0);
249 shmctl(id, IPC_RMID, 0);
251 if ((intptr_t)data == -1)
252 return AVERROR(errno);
254 pkt->buf = av_buffer_create(data, size, dealloc_shm, NULL, 0);
257 return AVERROR(ENOMEM);
260 pkt->data = pkt->buf->data;
261 pkt->size = c->frame_size;
265 #endif /* CONFIG_LIBXCB_SHM */
267 #if CONFIG_LIBXCB_XFIXES
268 static int check_xfixes(xcb_connection_t *conn)
270 xcb_xfixes_query_version_cookie_t cookie;
271 xcb_xfixes_query_version_reply_t *reply;
273 cookie = xcb_xfixes_query_version(conn, XCB_XFIXES_MAJOR_VERSION,
274 XCB_XFIXES_MINOR_VERSION);
275 reply = xcb_xfixes_query_version_reply(conn, cookie, NULL);
284 #define BLEND(target, source, alpha) \
285 (target) + ((source) * (255 - (alpha)) + 255 / 2) / 255
287 static void xcbgrab_draw_mouse(AVFormatContext *s, AVPacket *pkt,
288 xcb_query_pointer_reply_t *p,
289 xcb_get_geometry_reply_t *geo)
291 XCBGrabContext *gr = s->priv_data;
293 uint8_t *image = pkt->data;
294 int stride = gr->bpp / 8;
295 xcb_xfixes_get_cursor_image_cookie_t cc;
296 xcb_xfixes_get_cursor_image_reply_t *ci;
297 int cx, cy, x, y, w, h, c_off, i_off;
299 cc = xcb_xfixes_get_cursor_image(gr->conn);
300 ci = xcb_xfixes_get_cursor_image_reply(gr->conn, cc, NULL);
304 cursor = xcb_xfixes_get_cursor_image_cursor_image(ci);
308 cx = ci->x - ci->xhot;
309 cy = ci->y - ci->yhot;
311 x = FFMAX(cx, gr->x);
312 y = FFMAX(cy, gr->y);
314 w = FFMIN(cx + ci->width, gr->x + gr->width) - x;
315 h = FFMIN(cy + ci->height, gr->y + gr->height) - y;
320 cursor += (y - cy) * ci->width;
321 image += (y - gr->y) * gr->width * stride;
323 for (y = 0; y < h; y++) {
325 image += i_off * stride;
326 for (x = 0; x < w; x++, cursor++, image += stride) {
330 g = (*cursor >> 8) & 0xff;
331 b = (*cursor >> 16) & 0xff;
332 a = (*cursor >> 24) & 0xff;
342 image[0] = BLEND(r, image[0], a);
343 image[1] = BLEND(g, image[1], a);
344 image[2] = BLEND(b, image[2], a);
348 cursor += ci->width - w - c_off;
349 image += (gr->width - w - i_off) * stride;
354 #endif /* CONFIG_LIBXCB_XFIXES */
356 static void xcbgrab_update_region(AVFormatContext *s)
358 XCBGrabContext *c = s->priv_data;
359 const uint32_t args[] = { c->x - c->region_border,
360 c->y - c->region_border };
362 xcb_configure_window(c->conn,
364 XCB_CONFIG_WINDOW_X | XCB_CONFIG_WINDOW_Y,
368 static int xcbgrab_read_packet(AVFormatContext *s, AVPacket *pkt)
370 XCBGrabContext *c = s->priv_data;
371 xcb_query_pointer_cookie_t pc;
372 xcb_get_geometry_cookie_t gc;
373 xcb_query_pointer_reply_t *p = NULL;
374 xcb_get_geometry_reply_t *geo = NULL;
379 if (c->follow_mouse || c->draw_mouse) {
380 pc = xcb_query_pointer(c->conn, c->screen->root);
381 gc = xcb_get_geometry(c->conn, c->screen->root);
382 p = xcb_query_pointer_reply(c->conn, pc, NULL);
383 geo = xcb_get_geometry_reply(c->conn, gc, NULL);
386 if (c->follow_mouse && p->same_screen)
387 xcbgrab_reposition(s, p, geo);
390 xcbgrab_update_region(s);
392 #if CONFIG_LIBXCB_SHM
393 if (c->has_shm && xcbgrab_frame_shm(s, pkt) < 0)
397 ret = xcbgrab_frame(s, pkt);
399 #if CONFIG_LIBXCB_XFIXES
400 if (c->draw_mouse && p->same_screen)
401 xcbgrab_draw_mouse(s, pkt, p, geo);
410 static av_cold int xcbgrab_read_close(AVFormatContext *s)
412 XCBGrabContext *ctx = s->priv_data;
414 xcb_disconnect(ctx->conn);
419 static xcb_screen_t *get_screen(const xcb_setup_t *setup, int screen_num)
421 xcb_screen_iterator_t it = xcb_setup_roots_iterator(setup);
422 xcb_screen_t *screen = NULL;
424 for (; it.rem > 0; xcb_screen_next (&it)) {
436 static int pixfmt_from_pixmap_format(AVFormatContext *s, int depth,
439 XCBGrabContext *c = s->priv_data;
440 const xcb_setup_t *setup = xcb_get_setup(c->conn);
441 const xcb_format_t *fmt = xcb_setup_pixmap_formats(setup);
442 int length = xcb_setup_pixmap_formats_length(setup);
447 if (fmt->depth == depth) {
450 if (fmt->bits_per_pixel == 32)
451 *pix_fmt = AV_PIX_FMT_ARGB;
454 if (fmt->bits_per_pixel == 32)
455 *pix_fmt = AV_PIX_FMT_RGB32;
456 else if (fmt->bits_per_pixel == 24)
457 *pix_fmt = AV_PIX_FMT_RGB24;
460 if (fmt->bits_per_pixel == 16)
461 *pix_fmt = AV_PIX_FMT_RGB565;
464 if (fmt->bits_per_pixel == 16)
465 *pix_fmt = AV_PIX_FMT_RGB555;
468 if (fmt->bits_per_pixel == 8)
469 *pix_fmt = AV_PIX_FMT_RGB8;
475 c->bpp = fmt->bits_per_pixel;
476 c->frame_size = c->width * c->height * fmt->bits_per_pixel / 8;
482 av_log(s, AV_LOG_ERROR, "Pixmap format not mappable.\n");
484 return AVERROR_PATCHWELCOME;
487 static int create_stream(AVFormatContext *s)
489 XCBGrabContext *c = s->priv_data;
490 AVStream *st = avformat_new_stream(s, NULL);
491 const char *opts = strchr(s->filename, '+');
492 xcb_get_geometry_cookie_t gc;
493 xcb_get_geometry_reply_t *geo;
497 return AVERROR(ENOMEM);
499 ret = av_parse_video_size(&c->width, &c->height, c->video_size);
503 ret = av_parse_video_rate(&st->avg_frame_rate, c->framerate);
508 sscanf(opts, "%d,%d", &c->x, &c->y);
510 avpriv_set_pts_info(st, 64, 1, 1000000);
512 gc = xcb_get_geometry(c->conn, c->screen->root);
513 geo = xcb_get_geometry_reply(c->conn, gc, NULL);
515 c->width = FFMIN(geo->width, c->width);
516 c->height = FFMIN(geo->height, c->height);
517 c->time_base = (AVRational){ st->avg_frame_rate.den,
518 st->avg_frame_rate.num };
519 c->time_frame = av_gettime();
521 st->codec->codec_type = AVMEDIA_TYPE_VIDEO;
522 st->codec->codec_id = AV_CODEC_ID_RAWVIDEO;
523 st->codec->width = c->width;
524 st->codec->height = c->height;
525 st->codec->time_base = c->time_base;
527 ret = pixfmt_from_pixmap_format(s, geo->depth, &st->codec->pix_fmt);
534 static void draw_rectangle(AVFormatContext *s)
536 XCBGrabContext *c = s->priv_data;
537 xcb_gcontext_t gc = xcb_generate_id(c->conn);
538 uint32_t mask = XCB_GC_FOREGROUND |
543 uint32_t values[] = { c->screen->black_pixel,
544 c->screen->white_pixel,
546 XCB_LINE_STYLE_DOUBLE_DASH,
547 XCB_FILL_STYLE_SOLID };
548 xcb_rectangle_t r = { 1, 1,
549 c->width + c->region_border * 2 - 3,
550 c->height + c->region_border * 2 - 3 };
552 xcb_create_gc(c->conn, gc, c->window, mask, values);
554 xcb_poly_rectangle(c->conn, c->window, gc, 1, &r);
557 static void setup_window(AVFormatContext *s)
559 XCBGrabContext *c = s->priv_data;
560 uint32_t mask = XCB_CW_OVERRIDE_REDIRECT | XCB_CW_EVENT_MASK;
561 uint32_t values[] = { 1,
562 XCB_EVENT_MASK_EXPOSURE |
563 XCB_EVENT_MASK_STRUCTURE_NOTIFY };
564 xcb_rectangle_t rect = { c->x, c->y, c->width, c->height };
566 c->window = xcb_generate_id(c->conn);
568 xcb_create_window(c->conn, XCB_COPY_FROM_PARENT,
571 c->x - c->region_border,
572 c->y - c->region_border,
573 c->width + c->region_border * 2,
574 c->height + c->region_border * 2,
576 XCB_WINDOW_CLASS_INPUT_OUTPUT,
577 XCB_COPY_FROM_PARENT,
580 xcb_shape_rectangles(c->conn, XCB_SHAPE_SO_SUBTRACT,
581 XCB_SHAPE_SK_BOUNDING, XCB_CLIP_ORDERING_UNSORTED,
583 c->region_border, c->region_border,
586 xcb_map_window(c->conn, c->window);
591 static av_cold int xcbgrab_read_header(AVFormatContext *s)
593 XCBGrabContext *c = s->priv_data;
595 const xcb_setup_t *setup;
597 c->conn = xcb_connect(s->filename, &screen_num);
598 if ((ret = xcb_connection_has_error(c->conn))) {
599 av_log(s, AV_LOG_ERROR, "Cannot open display %s, error %d.\n",
600 s->filename ? s->filename : "default", ret);
603 setup = xcb_get_setup(c->conn);
605 c->screen = get_screen(setup, screen_num);
607 av_log(s, AV_LOG_ERROR, "The screen %d does not exist.\n",
609 xcbgrab_read_close(s);
613 c->segment = xcb_generate_id(c->conn);
615 ret = create_stream(s);
618 xcbgrab_read_close(s);
622 #if CONFIG_LIBXCB_SHM
623 c->has_shm = check_shm(c->conn);
626 #if CONFIG_LIBXCB_XFIXES
628 if (!(c->draw_mouse = check_xfixes(c->conn))) {
629 av_log(s, AV_LOG_WARNING,
630 "XFixes not available, cannot draw the mouse.\n");
633 avpriv_report_missing_feature(s, "%d bits per pixel screen",
646 AVInputFormat ff_x11grab_xcb_demuxer = {
648 .long_name = NULL_IF_CONFIG_SMALL("X11 screen capture, using XCB"),
649 .priv_data_size = sizeof(XCBGrabContext),
650 .read_header = xcbgrab_read_header,
651 .read_packet = xcbgrab_read_packet,
652 .read_close = xcbgrab_read_close,
653 .flags = AVFMT_NOFILE,
654 .priv_class = &xcbgrab_class,