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 "libavformat/avformat.h"
41 #include "libavformat/internal.h"
43 #include "libavutil/mathematics.h"
44 #include "libavutil/opt.h"
45 #include "libavutil/parseutils.h"
46 #include "libavutil/time.h"
48 typedef struct XCBGrabContext
{
51 xcb_connection_t
*conn
;
55 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 { "video_size", "A string describing frame size, such as 640x480 or hd720.", OFFSET(video_size
), AV_OPT_TYPE_STRING
, {.str
= "vga" }, 0, 0, D
},
86 { "framerate", "", OFFSET(framerate
), AV_OPT_TYPE_STRING
, {.str
= "ntsc" }, 0, 0, D
},
87 { "draw_mouse", "Draw the mouse pointer.", OFFSET(draw_mouse
), AV_OPT_TYPE_INT
, { .i64
= 1 }, 0, 1, D
},
88 { "follow_mouse", "Move the grabbing region when the mouse pointer reaches within specified amount of pixels to the edge of region.",
89 OFFSET(follow_mouse
), AV_OPT_TYPE_INT
, { .i64
= 0 }, FOLLOW_CENTER
, INT_MAX
, D
, "follow_mouse" },
90 { "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" },
91 { "show_region", "Show the grabbing region.", OFFSET(show_region
), AV_OPT_TYPE_INT
, { .i64
= 0 }, 0, 1, D
},
92 { "region_border", "Set the region border thickness.", OFFSET(region_border
), AV_OPT_TYPE_INT
, { .i64
= 3 }, 1, 128, D
},
96 static const AVClass xcbgrab_class
= {
97 .class_name
= "xcbgrab indev",
98 .item_name
= av_default_item_name
,
100 .version
= LIBAVUTIL_VERSION_INT
,
101 .category
= AV_CLASS_CATEGORY_DEVICE_VIDEO_INPUT
,
104 static int xcbgrab_reposition(AVFormatContext
*s
,
105 xcb_query_pointer_reply_t
*p
,
106 xcb_get_geometry_reply_t
*geo
)
108 XCBGrabContext
*c
= s
->priv_data
;
109 int x
= c
->x
, y
= c
->y
, p_x
= p
->win_x
, p_y
= p
->win_y
;
110 int w
= c
->width
, h
= c
->height
, f
= c
->follow_mouse
;
115 if (f
== FOLLOW_CENTER
) {
120 int right
= x
+ w
- f
;
122 int bottom
= y
+ h
+ f
;
125 } else if (p_x
< left
) {
130 } else if (p_y
< top
) {
135 c
->x
= FFMIN(FFMAX(0, x
), geo
->width
- w
);
136 c
->y
= FFMIN(FFMAX(0, y
), geo
->height
- h
);
141 static int xcbgrab_frame(AVFormatContext
*s
, AVPacket
*pkt
)
143 XCBGrabContext
*c
= s
->priv_data
;
144 xcb_get_image_cookie_t iq
;
145 xcb_get_image_reply_t
*img
;
146 xcb_drawable_t drawable
= c
->screen
->root
;
150 iq
= xcb_get_image(c
->conn
, XCB_IMAGE_FORMAT_Z_PIXMAP
, drawable
,
151 c
->x
, c
->y
, c
->width
, c
->height
, ~0);
153 img
= xcb_get_image_reply(c
->conn
, iq
, NULL
);
155 return AVERROR(EAGAIN
);
157 data
= xcb_get_image_data(img
);
158 length
= xcb_get_image_data_length(img
);
160 ret
= av_new_packet(pkt
, length
);
163 memcpy(pkt
->data
, data
, length
);
170 static void wait_frame(AVFormatContext
*s
, AVPacket
*pkt
)
172 XCBGrabContext
*c
= s
->priv_data
;
173 int64_t curtime
, delay
;
174 int64_t frame_time
= av_rescale_q(1, c
->time_base
, AV_TIME_BASE_Q
);
176 c
->time_frame
+= frame_time
;
179 curtime
= av_gettime();
180 delay
= c
->time_frame
- curtime
;
189 #if CONFIG_LIBXCB_SHM
190 static int check_shm(xcb_connection_t
*conn
)
192 xcb_shm_query_version_cookie_t cookie
= xcb_shm_query_version(conn
);
193 xcb_shm_query_version_reply_t
*reply
;
195 reply
= xcb_shm_query_version_reply(conn
, cookie
, NULL
);
204 static void dealloc_shm(void *unused
, uint8_t *data
)
209 static int xcbgrab_frame_shm(AVFormatContext
*s
, AVPacket
*pkt
)
211 XCBGrabContext
*c
= s
->priv_data
;
212 xcb_shm_get_image_cookie_t iq
;
213 xcb_shm_get_image_reply_t
*img
;
214 xcb_drawable_t drawable
= c
->screen
->root
;
216 int size
= c
->frame_size
+ FF_INPUT_BUFFER_PADDING_SIZE
;
217 int id
= shmget(IPC_PRIVATE
, size
, IPC_CREAT
| 0777);
218 xcb_generic_error_t
*e
= NULL
;
222 int err
= AVERROR(errno
);
223 av_strerror(err
, errbuf
, sizeof(errbuf
));
224 av_log(s
, AV_LOG_ERROR
, "Cannot get %d bytes of shared memory: %s.\n",
229 xcb_shm_attach(c
->conn
, c
->segment
, id
, 0);
231 iq
= xcb_shm_get_image(c
->conn
, drawable
,
232 c
->x
, c
->y
, c
->width
, c
->height
, ~0,
233 XCB_IMAGE_FORMAT_Z_PIXMAP
, c
->segment
, 0);
235 xcb_shm_detach(c
->conn
, c
->segment
);
237 img
= xcb_shm_get_image_reply(c
->conn
, iq
, &e
);
242 av_log(s
, AV_LOG_ERROR
,
243 "Cannot get the image data "
244 "event_error: response_type:%u error_code:%u "
245 "sequence:%u resource_id:%u minor_code:%u major_code:%u.\n",
246 e
->response_type
, e
->error_code
,
247 e
->sequence
, e
->resource_id
, e
->minor_code
, e
->major_code
);
249 shmctl(id
, IPC_RMID
, 0);
250 return AVERROR(EACCES
);
255 data
= shmat(id
, NULL
, 0);
256 shmctl(id
, IPC_RMID
, 0);
258 if ((intptr_t)data
== -1)
259 return AVERROR(errno
);
261 pkt
->buf
= av_buffer_create(data
, size
, dealloc_shm
, NULL
, 0);
264 return AVERROR(ENOMEM
);
267 pkt
->data
= pkt
->buf
->data
;
268 pkt
->size
= c
->frame_size
;
272 #endif /* CONFIG_LIBXCB_SHM */
274 #if CONFIG_LIBXCB_XFIXES
275 static int check_xfixes(xcb_connection_t
*conn
)
277 xcb_xfixes_query_version_cookie_t cookie
;
278 xcb_xfixes_query_version_reply_t
*reply
;
280 cookie
= xcb_xfixes_query_version(conn
, XCB_XFIXES_MAJOR_VERSION
,
281 XCB_XFIXES_MINOR_VERSION
);
282 reply
= xcb_xfixes_query_version_reply(conn
, cookie
, NULL
);
291 #define BLEND(target, source, alpha) \
292 (target) + ((source) * (255 - (alpha)) + 255 / 2) / 255
294 static void xcbgrab_draw_mouse(AVFormatContext
*s
, AVPacket
*pkt
,
295 xcb_query_pointer_reply_t
*p
,
296 xcb_get_geometry_reply_t
*geo
)
298 XCBGrabContext
*gr
= s
->priv_data
;
300 uint8_t *image
= pkt
->data
;
301 int stride
= gr
->bpp
/ 8;
302 xcb_xfixes_get_cursor_image_cookie_t cc
;
303 xcb_xfixes_get_cursor_image_reply_t
*ci
;
304 int cx
, cy
, x
, y
, w
, h
, c_off
, i_off
;
306 cc
= xcb_xfixes_get_cursor_image(gr
->conn
);
307 ci
= xcb_xfixes_get_cursor_image_reply(gr
->conn
, cc
, NULL
);
311 cursor
= xcb_xfixes_get_cursor_image_cursor_image(ci
);
315 cx
= ci
->x
- ci
->xhot
;
316 cy
= ci
->y
- ci
->yhot
;
318 x
= FFMAX(cx
, gr
->x
);
319 y
= FFMAX(cy
, gr
->y
);
321 w
= FFMIN(cx
+ ci
->width
, gr
->x
+ gr
->width
) - x
;
322 h
= FFMIN(cy
+ ci
->height
, gr
->y
+ gr
->height
) - y
;
327 cursor
+= (y
- cy
) * ci
->width
;
328 image
+= (y
- gr
->y
) * gr
->width
* stride
;
330 for (y
= 0; y
< h
; y
++) {
332 image
+= i_off
* stride
;
333 for (x
= 0; x
< w
; x
++, cursor
++, image
+= stride
) {
337 g
= (*cursor
>> 8) & 0xff;
338 b
= (*cursor
>> 16) & 0xff;
339 a
= (*cursor
>> 24) & 0xff;
349 image
[0] = BLEND(r
, image
[0], a
);
350 image
[1] = BLEND(g
, image
[1], a
);
351 image
[2] = BLEND(b
, image
[2], a
);
355 cursor
+= ci
->width
- w
- c_off
;
356 image
+= (gr
->width
- w
- i_off
) * stride
;
361 #endif /* CONFIG_LIBXCB_XFIXES */
363 static void xcbgrab_update_region(AVFormatContext
*s
)
365 XCBGrabContext
*c
= s
->priv_data
;
366 const uint32_t args
[] = { c
->x
- c
->region_border
,
367 c
->y
- c
->region_border
};
369 xcb_configure_window(c
->conn
,
371 XCB_CONFIG_WINDOW_X
| XCB_CONFIG_WINDOW_Y
,
375 static int xcbgrab_read_packet(AVFormatContext
*s
, AVPacket
*pkt
)
377 XCBGrabContext
*c
= s
->priv_data
;
378 xcb_query_pointer_cookie_t pc
;
379 xcb_get_geometry_cookie_t gc
;
380 xcb_query_pointer_reply_t
*p
= NULL
;
381 xcb_get_geometry_reply_t
*geo
= NULL
;
386 if (c
->follow_mouse
|| c
->draw_mouse
) {
387 pc
= xcb_query_pointer(c
->conn
, c
->screen
->root
);
388 gc
= xcb_get_geometry(c
->conn
, c
->screen
->root
);
389 p
= xcb_query_pointer_reply(c
->conn
, pc
, NULL
);
390 geo
= xcb_get_geometry_reply(c
->conn
, gc
, NULL
);
393 if (c
->follow_mouse
&& p
->same_screen
)
394 xcbgrab_reposition(s
, p
, geo
);
397 xcbgrab_update_region(s
);
399 #if CONFIG_LIBXCB_SHM
400 if (c
->has_shm
&& xcbgrab_frame_shm(s
, pkt
) < 0)
404 ret
= xcbgrab_frame(s
, pkt
);
406 #if CONFIG_LIBXCB_XFIXES
407 if (c
->draw_mouse
&& p
->same_screen
)
408 xcbgrab_draw_mouse(s
, pkt
, p
, geo
);
417 static av_cold
int xcbgrab_read_close(AVFormatContext
*s
)
419 XCBGrabContext
*ctx
= s
->priv_data
;
421 xcb_disconnect(ctx
->conn
);
426 static xcb_screen_t
*get_screen(const xcb_setup_t
*setup
, int screen_num
)
428 xcb_screen_iterator_t it
= xcb_setup_roots_iterator(setup
);
429 xcb_screen_t
*screen
= NULL
;
431 for (; it
.rem
> 0; xcb_screen_next (&it
)) {
443 static int pixfmt_from_pixmap_format(AVFormatContext
*s
, int depth
,
446 XCBGrabContext
*c
= s
->priv_data
;
447 const xcb_setup_t
*setup
= xcb_get_setup(c
->conn
);
448 const xcb_format_t
*fmt
= xcb_setup_pixmap_formats(setup
);
449 int length
= xcb_setup_pixmap_formats_length(setup
);
454 if (fmt
->depth
== depth
) {
457 if (fmt
->bits_per_pixel
== 32)
458 *pix_fmt
= AV_PIX_FMT_ARGB
;
461 if (fmt
->bits_per_pixel
== 32)
462 *pix_fmt
= AV_PIX_FMT_RGB32
;
463 else if (fmt
->bits_per_pixel
== 24)
464 *pix_fmt
= AV_PIX_FMT_RGB24
;
467 if (fmt
->bits_per_pixel
== 16)
468 *pix_fmt
= AV_PIX_FMT_RGB565
;
471 if (fmt
->bits_per_pixel
== 16)
472 *pix_fmt
= AV_PIX_FMT_RGB555
;
475 if (fmt
->bits_per_pixel
== 8)
476 *pix_fmt
= AV_PIX_FMT_RGB8
;
482 c
->bpp
= fmt
->bits_per_pixel
;
483 c
->frame_size
= c
->width
* c
->height
* fmt
->bits_per_pixel
/ 8;
489 av_log(s
, AV_LOG_ERROR
, "Pixmap format not mappable.\n");
491 return AVERROR_PATCHWELCOME
;
494 static int create_stream(AVFormatContext
*s
)
496 XCBGrabContext
*c
= s
->priv_data
;
497 AVStream
*st
= avformat_new_stream(s
, NULL
);
498 xcb_get_geometry_cookie_t gc
;
499 xcb_get_geometry_reply_t
*geo
;
503 return AVERROR(ENOMEM
);
505 ret
= av_parse_video_size(&c
->width
, &c
->height
, c
->video_size
);
509 ret
= av_parse_video_rate(&st
->avg_frame_rate
, c
->framerate
);
513 avpriv_set_pts_info(st
, 64, 1, 1000000);
515 gc
= xcb_get_geometry(c
->conn
, c
->screen
->root
);
516 geo
= xcb_get_geometry_reply(c
->conn
, gc
, NULL
);
518 c
->width
= FFMIN(geo
->width
, c
->width
);
519 c
->height
= FFMIN(geo
->height
, c
->height
);
520 c
->time_base
= (AVRational
){ st
->avg_frame_rate
.den
,
521 st
->avg_frame_rate
.num
};
522 c
->time_frame
= av_gettime();
524 st
->codec
->codec_type
= AVMEDIA_TYPE_VIDEO
;
525 st
->codec
->codec_id
= AV_CODEC_ID_RAWVIDEO
;
526 st
->codec
->width
= c
->width
;
527 st
->codec
->height
= c
->height
;
528 st
->codec
->time_base
= c
->time_base
;
530 ret
= pixfmt_from_pixmap_format(s
, geo
->depth
, &st
->codec
->pix_fmt
);
537 static void draw_rectangle(AVFormatContext
*s
)
539 XCBGrabContext
*c
= s
->priv_data
;
540 xcb_gcontext_t gc
= xcb_generate_id(c
->conn
);
541 uint32_t mask
= XCB_GC_FOREGROUND
|
546 uint32_t values
[] = { c
->screen
->black_pixel
,
547 c
->screen
->white_pixel
,
549 XCB_LINE_STYLE_DOUBLE_DASH
,
550 XCB_FILL_STYLE_SOLID
};
551 xcb_rectangle_t r
= { 1, 1,
552 c
->width
+ c
->region_border
* 2 - 3,
553 c
->height
+ c
->region_border
* 2 - 3 };
555 xcb_create_gc(c
->conn
, gc
, c
->window
, mask
, values
);
557 xcb_poly_rectangle(c
->conn
, c
->window
, gc
, 1, &r
);
560 static void setup_window(AVFormatContext
*s
)
562 XCBGrabContext
*c
= s
->priv_data
;
563 uint32_t mask
= XCB_CW_OVERRIDE_REDIRECT
| XCB_CW_EVENT_MASK
;
564 uint32_t values
[] = { 1,
565 XCB_EVENT_MASK_EXPOSURE
|
566 XCB_EVENT_MASK_STRUCTURE_NOTIFY
};
567 xcb_rectangle_t rect
= { c
->x
, c
->y
, c
->width
, c
->height
};
569 c
->window
= xcb_generate_id(c
->conn
);
571 xcb_create_window(c
->conn
, XCB_COPY_FROM_PARENT
,
574 c
->x
- c
->region_border
,
575 c
->y
- c
->region_border
,
576 c
->width
+ c
->region_border
* 2,
577 c
->height
+ c
->region_border
* 2,
579 XCB_WINDOW_CLASS_INPUT_OUTPUT
,
580 XCB_COPY_FROM_PARENT
,
583 #if CONFIG_LIBXCB_SHAPE
584 xcb_shape_rectangles(c
->conn
, XCB_SHAPE_SO_SUBTRACT
,
585 XCB_SHAPE_SK_BOUNDING
, XCB_CLIP_ORDERING_UNSORTED
,
587 c
->region_border
, c
->region_border
,
591 xcb_map_window(c
->conn
, c
->window
);
596 static av_cold
int xcbgrab_read_header(AVFormatContext
*s
)
598 XCBGrabContext
*c
= s
->priv_data
;
600 const xcb_setup_t
*setup
;
601 char *display_name
= av_strdup(s
->filename
);
604 return AVERROR(ENOMEM
);
606 if (!sscanf(s
->filename
, "%[^+]+%d,%d", display_name
, &c
->x
, &c
->y
)) {
608 sscanf(s
->filename
, "+%d,%d", &c
->x
, &c
->y
);
611 c
->conn
= xcb_connect(display_name
, &screen_num
);
612 av_freep(&display_name
);
613 if ((ret
= xcb_connection_has_error(c
->conn
))) {
614 av_log(s
, AV_LOG_ERROR
, "Cannot open display %s, error %d.\n",
615 (*s
->filename
) ? s
->filename
: "default", ret
);
618 setup
= xcb_get_setup(c
->conn
);
620 c
->screen
= get_screen(setup
, screen_num
);
622 av_log(s
, AV_LOG_ERROR
, "The screen %d does not exist.\n",
624 xcbgrab_read_close(s
);
628 #if CONFIG_LIBXCB_SHM
629 c
->segment
= xcb_generate_id(c
->conn
);
632 ret
= create_stream(s
);
635 xcbgrab_read_close(s
);
639 #if CONFIG_LIBXCB_SHM
640 c
->has_shm
= check_shm(c
->conn
);
643 #if CONFIG_LIBXCB_XFIXES
645 if (!(c
->draw_mouse
= check_xfixes(c
->conn
))) {
646 av_log(s
, AV_LOG_WARNING
,
647 "XFixes not available, cannot draw the mouse.\n");
650 avpriv_report_missing_feature(s
, "%d bits per pixel screen",
663 AVInputFormat ff_x11grab_xcb_demuxer
= {
665 .long_name
= NULL_IF_CONFIG_SMALL("X11 screen capture, using XCB"),
666 .priv_data_size
= sizeof(XCBGrabContext
),
667 .read_header
= xcbgrab_read_header
,
668 .read_packet
= xcbgrab_read_packet
,
669 .read_close
= xcbgrab_read_close
,
670 .flags
= AVFMT_NOFILE
,
671 .priv_class
= &xcbgrab_class
,