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 {
54 xcb_connection_t *conn;
58 xcb_shm_seg_t segment;
74 const char *video_size;
75 const char *framerate;
80 #define FOLLOW_CENTER -1
82 #define OFFSET(x) offsetof(XCBGrabContext, x)
83 #define D AV_OPT_FLAG_DECODING_PARAM
84 static const AVOption options[] = {
85 { "x", "Initial x coordinate.", OFFSET(x), AV_OPT_TYPE_INT, { .i64 = 0 }, 0, INT_MAX, D },
86 { "y", "Initial y coordinate.", OFFSET(y), AV_OPT_TYPE_INT, { .i64 = 0 }, 0, INT_MAX, D },
87 { "grab_x", "Initial x coordinate.", OFFSET(x), AV_OPT_TYPE_INT, { .i64 = 0 }, 0, INT_MAX, D },
88 { "grab_y", "Initial y coordinate.", OFFSET(y), AV_OPT_TYPE_INT, { .i64 = 0 }, 0, INT_MAX, D },
89 { "video_size", "A string describing frame size, such as 640x480 or hd720.", OFFSET(video_size), AV_OPT_TYPE_STRING, {.str = "vga" }, 0, 0, D },
90 { "framerate", "", OFFSET(framerate), AV_OPT_TYPE_STRING, {.str = "ntsc" }, 0, 0, D },
91 { "draw_mouse", "Draw the mouse pointer.", OFFSET(draw_mouse), AV_OPT_TYPE_INT, { .i64 = 1 }, 0, 1, D },
92 { "follow_mouse", "Move the grabbing region when the mouse pointer reaches within specified amount of pixels to the edge of region.",
93 OFFSET(follow_mouse), AV_OPT_TYPE_INT, { .i64 = 0 }, FOLLOW_CENTER, INT_MAX, D, "follow_mouse" },
94 { "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" },
95 { "show_region", "Show the grabbing region.", OFFSET(show_region), AV_OPT_TYPE_INT, { .i64 = 0 }, 0, 1, D },
96 { "region_border", "Set the region border thickness.", OFFSET(region_border), AV_OPT_TYPE_INT, { .i64 = 3 }, 1, 128, D },
100 static const AVClass xcbgrab_class = {
101 .class_name = "xcbgrab indev",
102 .item_name = av_default_item_name,
104 .version = LIBAVUTIL_VERSION_INT,
105 .category = AV_CLASS_CATEGORY_DEVICE_VIDEO_INPUT,
108 static int xcbgrab_reposition(AVFormatContext *s,
109 xcb_query_pointer_reply_t *p,
110 xcb_get_geometry_reply_t *geo)
112 XCBGrabContext *c = s->priv_data;
113 int x = c->x, y = c->y;
114 int w = c->width, h = c->height, f = c->follow_mouse;
123 if (f == FOLLOW_CENTER) {
128 int right = x + w - f;
130 int bottom = y + h - f;
133 } else if (p_x < left) {
138 } else if (p_y < top) {
143 c->x = FFMIN(FFMAX(0, x), geo->width - w);
144 c->y = FFMIN(FFMAX(0, y), geo->height - h);
149 static int xcbgrab_frame(AVFormatContext *s, AVPacket *pkt)
151 XCBGrabContext *c = s->priv_data;
152 xcb_get_image_cookie_t iq;
153 xcb_get_image_reply_t *img;
154 xcb_drawable_t drawable = c->screen->root;
155 xcb_generic_error_t *e = NULL;
159 iq = xcb_get_image(c->conn, XCB_IMAGE_FORMAT_Z_PIXMAP, drawable,
160 c->x, c->y, c->width, c->height, ~0);
162 img = xcb_get_image_reply(c->conn, iq, &e);
165 av_log(s, AV_LOG_ERROR,
166 "Cannot get the image data "
167 "event_error: response_type:%u error_code:%u "
168 "sequence:%u resource_id:%u minor_code:%u major_code:%u.\n",
169 e->response_type, e->error_code,
170 e->sequence, e->resource_id, e->minor_code, e->major_code);
172 return AVERROR(EACCES);
176 return AVERROR(EAGAIN);
178 data = xcb_get_image_data(img);
179 length = xcb_get_image_data_length(img);
181 ret = av_new_packet(pkt, length);
184 memcpy(pkt->data, data, length);
191 static void wait_frame(AVFormatContext *s, AVPacket *pkt)
193 XCBGrabContext *c = s->priv_data;
194 int64_t curtime, delay;
195 int64_t frame_time = av_rescale_q(1, c->time_base, AV_TIME_BASE_Q);
197 c->time_frame += frame_time;
200 curtime = av_gettime();
201 delay = c->time_frame - curtime;
210 #if CONFIG_LIBXCB_SHM
211 static int check_shm(xcb_connection_t *conn)
213 xcb_shm_query_version_cookie_t cookie = xcb_shm_query_version(conn);
214 xcb_shm_query_version_reply_t *reply;
216 reply = xcb_shm_query_version_reply(conn, cookie, NULL);
225 static int allocate_shm(AVFormatContext *s)
227 XCBGrabContext *c = s->priv_data;
228 int size = c->frame_size + AV_INPUT_BUFFER_PADDING_SIZE;
234 id = shmget(IPC_PRIVATE, size, IPC_CREAT | 0777);
237 int err = AVERROR(errno);
238 av_strerror(err, errbuf, sizeof(errbuf));
239 av_log(s, AV_LOG_ERROR, "Cannot get %d bytes of shared memory: %s.\n",
243 xcb_shm_attach(c->conn, c->segment, id, 0);
244 data = shmat(id, NULL, 0);
245 shmctl(id, IPC_RMID, 0);
246 if ((intptr_t)data == -1 || !data)
247 return AVERROR(errno);
252 static int xcbgrab_frame_shm(AVFormatContext *s, AVPacket *pkt)
254 XCBGrabContext *c = s->priv_data;
255 xcb_shm_get_image_cookie_t iq;
256 xcb_shm_get_image_reply_t *img;
257 xcb_drawable_t drawable = c->screen->root;
258 xcb_generic_error_t *e = NULL;
261 ret = allocate_shm(s);
265 iq = xcb_shm_get_image(c->conn, drawable,
266 c->x, c->y, c->width, c->height, ~0,
267 XCB_IMAGE_FORMAT_Z_PIXMAP, c->segment, 0);
268 img = xcb_shm_get_image_reply(c->conn, iq, &e);
273 av_log(s, AV_LOG_ERROR,
274 "Cannot get the image data "
275 "event_error: response_type:%u error_code:%u "
276 "sequence:%u resource_id:%u minor_code:%u major_code:%u.\n",
277 e->response_type, e->error_code,
278 e->sequence, e->resource_id, e->minor_code, e->major_code);
281 return AVERROR(EACCES);
286 pkt->data = c->buffer;
287 pkt->size = c->frame_size;
291 #endif /* CONFIG_LIBXCB_SHM */
293 #if CONFIG_LIBXCB_XFIXES
294 static int check_xfixes(xcb_connection_t *conn)
296 xcb_xfixes_query_version_cookie_t cookie;
297 xcb_xfixes_query_version_reply_t *reply;
299 cookie = xcb_xfixes_query_version(conn, XCB_XFIXES_MAJOR_VERSION,
300 XCB_XFIXES_MINOR_VERSION);
301 reply = xcb_xfixes_query_version_reply(conn, cookie, NULL);
310 #define BLEND(target, source, alpha) \
311 (target) + ((source) * (255 - (alpha)) + 255 / 2) / 255
313 static void xcbgrab_draw_mouse(AVFormatContext *s, AVPacket *pkt,
314 xcb_query_pointer_reply_t *p,
315 xcb_get_geometry_reply_t *geo)
317 XCBGrabContext *gr = s->priv_data;
319 uint8_t *image = pkt->data;
320 int stride = gr->bpp / 8;
321 xcb_xfixes_get_cursor_image_cookie_t cc;
322 xcb_xfixes_get_cursor_image_reply_t *ci;
323 int cx, cy, x, y, w, h, c_off, i_off;
325 cc = xcb_xfixes_get_cursor_image(gr->conn);
326 ci = xcb_xfixes_get_cursor_image_reply(gr->conn, cc, NULL);
330 cursor = xcb_xfixes_get_cursor_image_cursor_image(ci);
334 cx = ci->x - ci->xhot;
335 cy = ci->y - ci->yhot;
337 x = FFMAX(cx, gr->x);
338 y = FFMAX(cy, gr->y);
340 w = FFMIN(cx + ci->width, gr->x + gr->width) - x;
341 h = FFMIN(cy + ci->height, gr->y + gr->height) - y;
346 cursor += (y - cy) * ci->width;
347 image += (y - gr->y) * gr->width * stride;
349 for (y = 0; y < h; y++) {
351 image += i_off * stride;
352 for (x = 0; x < w; x++, cursor++, image += stride) {
356 g = (*cursor >> 8) & 0xff;
357 b = (*cursor >> 16) & 0xff;
358 a = (*cursor >> 24) & 0xff;
368 image[0] = BLEND(r, image[0], a);
369 image[1] = BLEND(g, image[1], a);
370 image[2] = BLEND(b, image[2], a);
374 cursor += ci->width - w - c_off;
375 image += (gr->width - w - i_off) * stride;
380 #endif /* CONFIG_LIBXCB_XFIXES */
382 static void xcbgrab_update_region(AVFormatContext *s)
384 XCBGrabContext *c = s->priv_data;
385 const uint32_t args[] = { c->x - c->region_border,
386 c->y - c->region_border };
388 xcb_configure_window(c->conn,
390 XCB_CONFIG_WINDOW_X | XCB_CONFIG_WINDOW_Y,
394 static int xcbgrab_read_packet(AVFormatContext *s, AVPacket *pkt)
396 XCBGrabContext *c = s->priv_data;
397 xcb_query_pointer_cookie_t pc;
398 xcb_get_geometry_cookie_t gc;
399 xcb_query_pointer_reply_t *p = NULL;
400 xcb_get_geometry_reply_t *geo = NULL;
405 if (c->follow_mouse || c->draw_mouse) {
406 pc = xcb_query_pointer(c->conn, c->screen->root);
407 gc = xcb_get_geometry(c->conn, c->screen->root);
408 p = xcb_query_pointer_reply(c->conn, pc, NULL);
409 geo = xcb_get_geometry_reply(c->conn, gc, NULL);
412 if (c->follow_mouse && p->same_screen)
413 xcbgrab_reposition(s, p, geo);
416 xcbgrab_update_region(s);
418 #if CONFIG_LIBXCB_SHM
419 if (c->has_shm && xcbgrab_frame_shm(s, pkt) < 0)
423 ret = xcbgrab_frame(s, pkt);
425 #if CONFIG_LIBXCB_XFIXES
426 if (ret >= 0 && c->draw_mouse && p->same_screen)
427 xcbgrab_draw_mouse(s, pkt, p, geo);
436 static av_cold int xcbgrab_read_close(AVFormatContext *s)
438 XCBGrabContext *ctx = s->priv_data;
440 #if CONFIG_LIBXCB_SHM
446 xcb_disconnect(ctx->conn);
451 static xcb_screen_t *get_screen(const xcb_setup_t *setup, int screen_num)
453 xcb_screen_iterator_t it = xcb_setup_roots_iterator(setup);
454 xcb_screen_t *screen = NULL;
456 for (; it.rem > 0; xcb_screen_next (&it)) {
468 static int pixfmt_from_pixmap_format(AVFormatContext *s, int depth,
471 XCBGrabContext *c = s->priv_data;
472 const xcb_setup_t *setup = xcb_get_setup(c->conn);
473 const xcb_format_t *fmt = xcb_setup_pixmap_formats(setup);
474 int length = xcb_setup_pixmap_formats_length(setup);
479 if (fmt->depth == depth) {
482 if (fmt->bits_per_pixel == 32)
483 *pix_fmt = AV_PIX_FMT_0RGB;
486 if (fmt->bits_per_pixel == 32)
487 *pix_fmt = AV_PIX_FMT_0RGB32;
488 else if (fmt->bits_per_pixel == 24)
489 *pix_fmt = AV_PIX_FMT_RGB24;
492 if (fmt->bits_per_pixel == 16)
493 *pix_fmt = AV_PIX_FMT_RGB565;
496 if (fmt->bits_per_pixel == 16)
497 *pix_fmt = AV_PIX_FMT_RGB555;
500 if (fmt->bits_per_pixel == 8)
501 *pix_fmt = AV_PIX_FMT_RGB8;
507 c->bpp = fmt->bits_per_pixel;
508 c->frame_size = c->width * c->height * fmt->bits_per_pixel / 8;
514 avpriv_report_missing_feature(s, "Mapping this pixmap format");
516 return AVERROR_PATCHWELCOME;
519 static int create_stream(AVFormatContext *s)
521 XCBGrabContext *c = s->priv_data;
522 AVStream *st = avformat_new_stream(s, NULL);
523 xcb_get_geometry_cookie_t gc;
524 xcb_get_geometry_reply_t *geo;
528 return AVERROR(ENOMEM);
530 ret = av_parse_video_size(&c->width, &c->height, c->video_size);
534 ret = av_parse_video_rate(&st->avg_frame_rate, c->framerate);
538 avpriv_set_pts_info(st, 64, 1, 1000000);
540 gc = xcb_get_geometry(c->conn, c->screen->root);
541 geo = xcb_get_geometry_reply(c->conn, gc, NULL);
543 return AVERROR_EXTERNAL;
545 if (c->x + c->width > geo->width ||
546 c->y + c->height > geo->height) {
547 av_log(s, AV_LOG_ERROR,
548 "Capture area %dx%d at position %d.%d "
549 "outside the screen size %dx%d\n",
552 geo->width, geo->height);
554 return AVERROR(EINVAL);
557 c->time_base = (AVRational){ st->avg_frame_rate.den,
558 st->avg_frame_rate.num };
559 c->time_frame = av_gettime();
561 st->codecpar->codec_type = AVMEDIA_TYPE_VIDEO;
562 st->codecpar->codec_id = AV_CODEC_ID_RAWVIDEO;
563 st->codecpar->width = c->width;
564 st->codecpar->height = c->height;
566 ret = pixfmt_from_pixmap_format(s, geo->depth, &st->codecpar->format);
573 static void draw_rectangle(AVFormatContext *s)
575 XCBGrabContext *c = s->priv_data;
576 xcb_gcontext_t gc = xcb_generate_id(c->conn);
577 uint32_t mask = XCB_GC_FOREGROUND |
582 uint32_t values[] = { c->screen->black_pixel,
583 c->screen->white_pixel,
585 XCB_LINE_STYLE_DOUBLE_DASH,
586 XCB_FILL_STYLE_SOLID };
587 xcb_rectangle_t r = { 1, 1,
588 c->width + c->region_border * 2 - 3,
589 c->height + c->region_border * 2 - 3 };
591 xcb_create_gc(c->conn, gc, c->window, mask, values);
593 xcb_poly_rectangle(c->conn, c->window, gc, 1, &r);
596 static void setup_window(AVFormatContext *s)
598 XCBGrabContext *c = s->priv_data;
599 uint32_t mask = XCB_CW_OVERRIDE_REDIRECT | XCB_CW_EVENT_MASK;
600 uint32_t values[] = { 1,
601 XCB_EVENT_MASK_EXPOSURE |
602 XCB_EVENT_MASK_STRUCTURE_NOTIFY };
603 av_unused xcb_rectangle_t rect = { 0, 0, c->width, c->height };
605 c->window = xcb_generate_id(c->conn);
607 xcb_create_window(c->conn, XCB_COPY_FROM_PARENT,
610 c->x - c->region_border,
611 c->y - c->region_border,
612 c->width + c->region_border * 2,
613 c->height + c->region_border * 2,
615 XCB_WINDOW_CLASS_INPUT_OUTPUT,
616 XCB_COPY_FROM_PARENT,
619 #if CONFIG_LIBXCB_SHAPE
620 xcb_shape_rectangles(c->conn, XCB_SHAPE_SO_SUBTRACT,
621 XCB_SHAPE_SK_BOUNDING, XCB_CLIP_ORDERING_UNSORTED,
623 c->region_border, c->region_border,
627 xcb_map_window(c->conn, c->window);
632 static av_cold int xcbgrab_read_header(AVFormatContext *s)
634 XCBGrabContext *c = s->priv_data;
636 const xcb_setup_t *setup;
637 char *display_name = av_strdup(s->url);
640 return AVERROR(ENOMEM);
642 if (!sscanf(s->url, "%[^+]+%d,%d", display_name, &c->x, &c->y)) {
644 sscanf(s->url, "+%d,%d", &c->x, &c->y);
647 c->conn = xcb_connect(display_name[0] ? display_name : NULL, &screen_num);
648 av_freep(&display_name);
650 if ((ret = xcb_connection_has_error(c->conn))) {
651 av_log(s, AV_LOG_ERROR, "Cannot open display %s, error %d.\n",
652 s->url[0] ? s->url : "default", ret);
656 setup = xcb_get_setup(c->conn);
658 c->screen = get_screen(setup, screen_num);
660 av_log(s, AV_LOG_ERROR, "The screen %d does not exist.\n",
662 xcbgrab_read_close(s);
666 ret = create_stream(s);
669 xcbgrab_read_close(s);
673 #if CONFIG_LIBXCB_SHM
674 if ((c->has_shm = check_shm(c->conn)))
675 c->segment = xcb_generate_id(c->conn);
678 #if CONFIG_LIBXCB_XFIXES
680 if (!(c->draw_mouse = check_xfixes(c->conn))) {
681 av_log(s, AV_LOG_WARNING,
682 "XFixes not available, cannot draw the mouse.\n");
685 avpriv_report_missing_feature(s, "%d bits per pixel screen",
698 AVInputFormat ff_xcbgrab_demuxer = {
700 .long_name = NULL_IF_CONFIG_SMALL("X11 screen capture, using XCB"),
701 .priv_data_size = sizeof(XCBGrabContext),
702 .read_header = xcbgrab_read_header,
703 .read_packet = xcbgrab_read_packet,
704 .read_close = xcbgrab_read_close,
705 .flags = AVFMT_NOFILE,
706 .priv_class = &xcbgrab_class,