3 * Copyright (C) 2014 Luca Barbato <lu_zero@gentoo.org>
5 * This file is part of FFmpeg.
7 * FFmpeg 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 * FFmpeg 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 FFmpeg; 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 #if CONFIG_LIBXCB_SHAPE
37 #include <xcb/shape.h>
40 #include "libavutil/internal.h"
41 #include "libavutil/mathematics.h"
42 #include "libavutil/opt.h"
43 #include "libavutil/parseutils.h"
44 #include "libavutil/time.h"
46 #include "libavformat/avformat.h"
47 #include "libavformat/internal.h"
49 typedef struct XCBGrabContext {
52 xcb_connection_t *conn;
56 xcb_shm_seg_t segment;
72 const char *video_size;
73 const char *framerate;
78 #define FOLLOW_CENTER -1
80 #define OFFSET(x) offsetof(XCBGrabContext, x)
81 #define D AV_OPT_FLAG_DECODING_PARAM
82 static const AVOption options[] = {
83 { "x", "Initial x coordinate.", OFFSET(x), AV_OPT_TYPE_INT, { .i64 = 0 }, 0, INT_MAX, D },
84 { "y", "Initial y coordinate.", OFFSET(y), AV_OPT_TYPE_INT, { .i64 = 0 }, 0, INT_MAX, D },
85 { "grab_x", "Initial x coordinate.", OFFSET(x), AV_OPT_TYPE_INT, { .i64 = 0 }, 0, INT_MAX, D },
86 { "grab_y", "Initial y coordinate.", OFFSET(y), AV_OPT_TYPE_INT, { .i64 = 0 }, 0, INT_MAX, D },
87 { "video_size", "A string describing frame size, such as 640x480 or hd720.", OFFSET(video_size), AV_OPT_TYPE_STRING, {.str = "vga" }, 0, 0, D },
88 { "framerate", "", OFFSET(framerate), AV_OPT_TYPE_STRING, {.str = "ntsc" }, 0, 0, D },
89 { "draw_mouse", "Draw the mouse pointer.", OFFSET(draw_mouse), AV_OPT_TYPE_INT, { .i64 = 1 }, 0, 1, D },
90 { "follow_mouse", "Move the grabbing region when the mouse pointer reaches within specified amount of pixels to the edge of region.",
91 OFFSET(follow_mouse), AV_OPT_TYPE_INT, { .i64 = 0 }, FOLLOW_CENTER, INT_MAX, D, "follow_mouse" },
92 { "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" },
93 { "show_region", "Show the grabbing region.", OFFSET(show_region), AV_OPT_TYPE_INT, { .i64 = 0 }, 0, 1, D },
94 { "region_border", "Set the region border thickness.", OFFSET(region_border), AV_OPT_TYPE_INT, { .i64 = 3 }, 1, 128, D },
98 static const AVClass xcbgrab_class = {
99 .class_name = "xcbgrab indev",
100 .item_name = av_default_item_name,
102 .version = LIBAVUTIL_VERSION_INT,
103 .category = AV_CLASS_CATEGORY_DEVICE_VIDEO_INPUT,
106 static int xcbgrab_reposition(AVFormatContext *s,
107 xcb_query_pointer_reply_t *p,
108 xcb_get_geometry_reply_t *geo)
110 XCBGrabContext *c = s->priv_data;
111 int x = c->x, y = c->y;
112 int w = c->width, h = c->height, f = c->follow_mouse;
121 if (f == FOLLOW_CENTER) {
126 int right = x + w - f;
128 int bottom = y + h + f;
131 } else if (p_x < left) {
136 } else if (p_y < top) {
141 c->x = FFMIN(FFMAX(0, x), geo->width - w);
142 c->y = FFMIN(FFMAX(0, y), geo->height - h);
147 static int xcbgrab_frame(AVFormatContext *s, AVPacket *pkt)
149 XCBGrabContext *c = s->priv_data;
150 xcb_get_image_cookie_t iq;
151 xcb_get_image_reply_t *img;
152 xcb_drawable_t drawable = c->screen->root;
156 iq = xcb_get_image(c->conn, XCB_IMAGE_FORMAT_Z_PIXMAP, drawable,
157 c->x, c->y, c->width, c->height, ~0);
159 img = xcb_get_image_reply(c->conn, iq, NULL);
161 return AVERROR(EAGAIN);
163 data = xcb_get_image_data(img);
164 length = xcb_get_image_data_length(img);
166 ret = av_new_packet(pkt, length);
169 memcpy(pkt->data, data, length);
176 static void wait_frame(AVFormatContext *s, AVPacket *pkt)
178 XCBGrabContext *c = s->priv_data;
179 int64_t curtime, delay;
180 int64_t frame_time = av_rescale_q(1, c->time_base, AV_TIME_BASE_Q);
182 c->time_frame += frame_time;
185 curtime = av_gettime();
186 delay = c->time_frame - curtime;
195 #if CONFIG_LIBXCB_SHM
196 static int check_shm(xcb_connection_t *conn)
198 xcb_shm_query_version_cookie_t cookie = xcb_shm_query_version(conn);
199 xcb_shm_query_version_reply_t *reply;
201 reply = xcb_shm_query_version_reply(conn, cookie, NULL);
210 static void dealloc_shm(void *unused, uint8_t *data)
215 static int xcbgrab_frame_shm(AVFormatContext *s, AVPacket *pkt)
217 XCBGrabContext *c = s->priv_data;
218 xcb_shm_get_image_cookie_t iq;
219 xcb_shm_get_image_reply_t *img;
220 xcb_drawable_t drawable = c->screen->root;
222 int size = c->frame_size + FF_INPUT_BUFFER_PADDING_SIZE;
223 int id = shmget(IPC_PRIVATE, size, IPC_CREAT | 0777);
224 xcb_generic_error_t *e = NULL;
228 int err = AVERROR(errno);
229 av_strerror(err, errbuf, sizeof(errbuf));
230 av_log(s, AV_LOG_ERROR, "Cannot get %d bytes of shared memory: %s.\n",
235 xcb_shm_attach(c->conn, c->segment, id, 0);
237 iq = xcb_shm_get_image(c->conn, drawable,
238 c->x, c->y, c->width, c->height, ~0,
239 XCB_IMAGE_FORMAT_Z_PIXMAP, c->segment, 0);
241 xcb_shm_detach(c->conn, c->segment);
243 img = xcb_shm_get_image_reply(c->conn, iq, &e);
248 av_log(s, AV_LOG_ERROR,
249 "Cannot get the image data "
250 "event_error: response_type:%u error_code:%u "
251 "sequence:%u resource_id:%u minor_code:%u major_code:%u.\n",
252 e->response_type, e->error_code,
253 e->sequence, e->resource_id, e->minor_code, e->major_code);
255 shmctl(id, IPC_RMID, 0);
256 return AVERROR(EACCES);
261 data = shmat(id, NULL, 0);
262 shmctl(id, IPC_RMID, 0);
264 if ((intptr_t)data == -1)
265 return AVERROR(errno);
267 pkt->buf = av_buffer_create(data, size, dealloc_shm, NULL, 0);
270 return AVERROR(ENOMEM);
273 pkt->data = pkt->buf->data;
274 pkt->size = c->frame_size;
278 #endif /* CONFIG_LIBXCB_SHM */
280 #if CONFIG_LIBXCB_XFIXES
281 static int check_xfixes(xcb_connection_t *conn)
283 xcb_xfixes_query_version_cookie_t cookie;
284 xcb_xfixes_query_version_reply_t *reply;
286 cookie = xcb_xfixes_query_version(conn, XCB_XFIXES_MAJOR_VERSION,
287 XCB_XFIXES_MINOR_VERSION);
288 reply = xcb_xfixes_query_version_reply(conn, cookie, NULL);
297 #define BLEND(target, source, alpha) \
298 (target) + ((source) * (255 - (alpha)) + 255 / 2) / 255
300 static void xcbgrab_draw_mouse(AVFormatContext *s, AVPacket *pkt,
301 xcb_query_pointer_reply_t *p,
302 xcb_get_geometry_reply_t *geo)
304 XCBGrabContext *gr = s->priv_data;
306 uint8_t *image = pkt->data;
307 int stride = gr->bpp / 8;
308 xcb_xfixes_get_cursor_image_cookie_t cc;
309 xcb_xfixes_get_cursor_image_reply_t *ci;
310 int cx, cy, x, y, w, h, c_off, i_off;
312 cc = xcb_xfixes_get_cursor_image(gr->conn);
313 ci = xcb_xfixes_get_cursor_image_reply(gr->conn, cc, NULL);
317 cursor = xcb_xfixes_get_cursor_image_cursor_image(ci);
321 cx = ci->x - ci->xhot;
322 cy = ci->y - ci->yhot;
324 x = FFMAX(cx, gr->x);
325 y = FFMAX(cy, gr->y);
327 w = FFMIN(cx + ci->width, gr->x + gr->width) - x;
328 h = FFMIN(cy + ci->height, gr->y + gr->height) - y;
333 cursor += (y - cy) * ci->width;
334 image += (y - gr->y) * gr->width * stride;
336 for (y = 0; y < h; y++) {
338 image += i_off * stride;
339 for (x = 0; x < w; x++, cursor++, image += stride) {
343 g = (*cursor >> 8) & 0xff;
344 b = (*cursor >> 16) & 0xff;
345 a = (*cursor >> 24) & 0xff;
355 image[0] = BLEND(r, image[0], a);
356 image[1] = BLEND(g, image[1], a);
357 image[2] = BLEND(b, image[2], a);
361 cursor += ci->width - w - c_off;
362 image += (gr->width - w - i_off) * stride;
367 #endif /* CONFIG_LIBXCB_XFIXES */
369 static void xcbgrab_update_region(AVFormatContext *s)
371 XCBGrabContext *c = s->priv_data;
372 const uint32_t args[] = { c->x - c->region_border,
373 c->y - c->region_border };
375 xcb_configure_window(c->conn,
377 XCB_CONFIG_WINDOW_X | XCB_CONFIG_WINDOW_Y,
381 static int xcbgrab_read_packet(AVFormatContext *s, AVPacket *pkt)
383 XCBGrabContext *c = s->priv_data;
384 xcb_query_pointer_cookie_t pc;
385 xcb_get_geometry_cookie_t gc;
386 xcb_query_pointer_reply_t *p = NULL;
387 xcb_get_geometry_reply_t *geo = NULL;
392 if (c->follow_mouse || c->draw_mouse) {
393 pc = xcb_query_pointer(c->conn, c->screen->root);
394 gc = xcb_get_geometry(c->conn, c->screen->root);
395 p = xcb_query_pointer_reply(c->conn, pc, NULL);
396 geo = xcb_get_geometry_reply(c->conn, gc, NULL);
399 if (c->follow_mouse && p->same_screen)
400 xcbgrab_reposition(s, p, geo);
403 xcbgrab_update_region(s);
405 #if CONFIG_LIBXCB_SHM
406 if (c->has_shm && xcbgrab_frame_shm(s, pkt) < 0)
410 ret = xcbgrab_frame(s, pkt);
412 #if CONFIG_LIBXCB_XFIXES
413 if (c->draw_mouse && p->same_screen)
414 xcbgrab_draw_mouse(s, pkt, p, geo);
423 static av_cold int xcbgrab_read_close(AVFormatContext *s)
425 XCBGrabContext *ctx = s->priv_data;
427 xcb_disconnect(ctx->conn);
432 static xcb_screen_t *get_screen(const xcb_setup_t *setup, int screen_num)
434 xcb_screen_iterator_t it = xcb_setup_roots_iterator(setup);
435 xcb_screen_t *screen = NULL;
437 for (; it.rem > 0; xcb_screen_next (&it)) {
449 static int pixfmt_from_pixmap_format(AVFormatContext *s, int depth,
452 XCBGrabContext *c = s->priv_data;
453 const xcb_setup_t *setup = xcb_get_setup(c->conn);
454 const xcb_format_t *fmt = xcb_setup_pixmap_formats(setup);
455 int length = xcb_setup_pixmap_formats_length(setup);
460 if (fmt->depth == depth) {
463 if (fmt->bits_per_pixel == 32)
464 *pix_fmt = AV_PIX_FMT_0RGB;
467 if (fmt->bits_per_pixel == 32)
468 *pix_fmt = AV_PIX_FMT_0RGB32;
469 else if (fmt->bits_per_pixel == 24)
470 *pix_fmt = AV_PIX_FMT_RGB24;
473 if (fmt->bits_per_pixel == 16)
474 *pix_fmt = AV_PIX_FMT_RGB565;
477 if (fmt->bits_per_pixel == 16)
478 *pix_fmt = AV_PIX_FMT_RGB555;
481 if (fmt->bits_per_pixel == 8)
482 *pix_fmt = AV_PIX_FMT_RGB8;
488 c->bpp = fmt->bits_per_pixel;
489 c->frame_size = c->width * c->height * fmt->bits_per_pixel / 8;
495 av_log(s, AV_LOG_ERROR, "Pixmap format not mappable.\n");
497 return AVERROR_PATCHWELCOME;
500 static int create_stream(AVFormatContext *s)
502 XCBGrabContext *c = s->priv_data;
503 AVStream *st = avformat_new_stream(s, NULL);
504 xcb_get_geometry_cookie_t gc;
505 xcb_get_geometry_reply_t *geo;
509 return AVERROR(ENOMEM);
511 ret = av_parse_video_size(&c->width, &c->height, c->video_size);
515 ret = av_parse_video_rate(&st->avg_frame_rate, c->framerate);
519 avpriv_set_pts_info(st, 64, 1, 1000000);
521 gc = xcb_get_geometry(c->conn, c->screen->root);
522 geo = xcb_get_geometry_reply(c->conn, gc, NULL);
524 c->width = FFMIN(geo->width, c->width);
525 c->height = FFMIN(geo->height, c->height);
526 c->time_base = (AVRational){ st->avg_frame_rate.den,
527 st->avg_frame_rate.num };
528 c->time_frame = av_gettime();
530 st->codec->codec_type = AVMEDIA_TYPE_VIDEO;
531 st->codec->codec_id = AV_CODEC_ID_RAWVIDEO;
532 st->codec->width = c->width;
533 st->codec->height = c->height;
534 st->codec->time_base = c->time_base;
536 ret = pixfmt_from_pixmap_format(s, geo->depth, &st->codec->pix_fmt);
543 static void draw_rectangle(AVFormatContext *s)
545 XCBGrabContext *c = s->priv_data;
546 xcb_gcontext_t gc = xcb_generate_id(c->conn);
547 uint32_t mask = XCB_GC_FOREGROUND |
552 uint32_t values[] = { c->screen->black_pixel,
553 c->screen->white_pixel,
555 XCB_LINE_STYLE_DOUBLE_DASH,
556 XCB_FILL_STYLE_SOLID };
557 xcb_rectangle_t r = { 1, 1,
558 c->width + c->region_border * 2 - 3,
559 c->height + c->region_border * 2 - 3 };
561 xcb_create_gc(c->conn, gc, c->window, mask, values);
563 xcb_poly_rectangle(c->conn, c->window, gc, 1, &r);
566 static void setup_window(AVFormatContext *s)
568 XCBGrabContext *c = s->priv_data;
569 uint32_t mask = XCB_CW_OVERRIDE_REDIRECT | XCB_CW_EVENT_MASK;
570 uint32_t values[] = { 1,
571 XCB_EVENT_MASK_EXPOSURE |
572 XCB_EVENT_MASK_STRUCTURE_NOTIFY };
573 xcb_rectangle_t rect = { 0, 0, c->width, c->height };
575 c->window = xcb_generate_id(c->conn);
577 xcb_create_window(c->conn, XCB_COPY_FROM_PARENT,
580 c->x - c->region_border,
581 c->y - c->region_border,
582 c->width + c->region_border * 2,
583 c->height + c->region_border * 2,
585 XCB_WINDOW_CLASS_INPUT_OUTPUT,
586 XCB_COPY_FROM_PARENT,
589 #if CONFIG_LIBXCB_SHAPE
590 xcb_shape_rectangles(c->conn, XCB_SHAPE_SO_SUBTRACT,
591 XCB_SHAPE_SK_BOUNDING, XCB_CLIP_ORDERING_UNSORTED,
593 c->region_border, c->region_border,
597 xcb_map_window(c->conn, c->window);
602 static av_cold int xcbgrab_read_header(AVFormatContext *s)
604 XCBGrabContext *c = s->priv_data;
606 const xcb_setup_t *setup;
607 char *display_name = av_strdup(s->filename);
610 return AVERROR(ENOMEM);
612 if (!sscanf(s->filename, "%[^+]+%d,%d", display_name, &c->x, &c->y)) {
614 sscanf(s->filename, "+%d,%d", &c->x, &c->y);
617 c->conn = xcb_connect(display_name[0] ? display_name : NULL, &screen_num);
618 av_freep(&display_name);
620 if ((ret = xcb_connection_has_error(c->conn))) {
621 av_log(s, AV_LOG_ERROR, "Cannot open display %s, error %d.\n",
622 s->filename[0] ? s->filename : "default", ret);
626 setup = xcb_get_setup(c->conn);
628 c->screen = get_screen(setup, screen_num);
630 av_log(s, AV_LOG_ERROR, "The screen %d does not exist.\n",
632 xcbgrab_read_close(s);
636 ret = create_stream(s);
639 xcbgrab_read_close(s);
643 #if CONFIG_LIBXCB_SHM
644 if ((c->has_shm = check_shm(c->conn)))
645 c->segment = xcb_generate_id(c->conn);
648 #if CONFIG_LIBXCB_XFIXES
650 if (!(c->draw_mouse = check_xfixes(c->conn))) {
651 av_log(s, AV_LOG_WARNING,
652 "XFixes not available, cannot draw the mouse.\n");
655 avpriv_report_missing_feature(s, "%d bits per pixel screen",
668 AVInputFormat ff_x11grab_xcb_demuxer = {
670 .long_name = NULL_IF_CONFIG_SMALL("X11 screen capture, using XCB"),
671 .priv_data_size = sizeof(XCBGrabContext),
672 .read_header = xcbgrab_read_header,
673 .read_packet = xcbgrab_read_packet,
674 .read_close = xcbgrab_read_close,
675 .flags = AVFMT_NOFILE,
676 .priv_class = &xcbgrab_class,