27 #if CONFIG_LIBXCB_XFIXES 28 #include <xcb/xfixes.h> 36 #if CONFIG_LIBXCB_SHAPE 37 #include <xcb/shape.h> 78 #define FOLLOW_CENTER -1 80 #define OFFSET(x) offsetof(XCBGrabContext, x) 81 #define D AV_OPT_FLAG_DECODING_PARAM 90 {
"follow_mouse",
"Move the grabbing region when the mouse pointer reaches within specified amount of pixels to the edge of region.",
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" },
107 xcb_query_pointer_reply_t *p,
108 xcb_get_geometry_reply_t *geo)
111 int x = c->
x,
y = c->
y;
126 int right = x + w -
f;
128 int bottom =
y +
h -
f;
131 }
else if (p_x < left) {
136 }
else if (p_y < top) {
155 xcb_get_image_cookie_t iq;
156 xcb_get_image_reply_t *
img;
157 xcb_drawable_t drawable = c->
screen->root;
158 xcb_generic_error_t *e =
NULL;
162 iq = xcb_get_image(c->
conn, XCB_IMAGE_FORMAT_Z_PIXMAP, drawable,
165 img = xcb_get_image_reply(c->
conn, iq, &e);
169 "Cannot get the image data " 170 "event_error: response_type:%u error_code:%u " 171 "sequence:%u resource_id:%u minor_code:%u major_code:%u.\n",
172 e->response_type, e->error_code,
173 e->sequence, e->resource_id, e->minor_code, e->major_code);
181 data = xcb_get_image_data(img);
182 length = xcb_get_image_data_length(img);
201 int64_t curtime, delay;
216 #if CONFIG_LIBXCB_SHM 217 static int check_shm(xcb_connection_t *
conn)
219 xcb_shm_query_version_cookie_t cookie = xcb_shm_query_version(conn);
220 xcb_shm_query_version_reply_t *reply;
222 reply = xcb_shm_query_version_reply(conn, cookie,
NULL);
231 static void free_shm_buffer(
void *opaque,
uint8_t *
data)
238 xcb_connection_t *
conn = opaque;
244 id = shmget(IPC_PRIVATE, size, IPC_CREAT | 0777);
248 segment = xcb_generate_id(conn);
249 xcb_shm_attach(conn, segment,
id, 0);
250 data = shmat(
id,
NULL, 0);
251 shmctl(
id, IPC_RMID, 0);
252 if ((intptr_t)data == -1 || !data)
255 ref =
av_buffer_create(data, size, free_shm_buffer, (
void *)(ptrdiff_t)segment, 0);
265 xcb_shm_get_image_cookie_t iq;
266 xcb_shm_get_image_reply_t *
img;
267 xcb_drawable_t drawable = c->
screen->root;
268 xcb_generic_error_t *e =
NULL;
279 iq = xcb_shm_get_image(c->
conn, drawable,
281 XCB_IMAGE_FORMAT_Z_PIXMAP, segment, 0);
282 img = xcb_shm_get_image_reply(c->
conn, iq, &e);
288 "Cannot get the image data " 289 "event_error: response_type:%u error_code:%u " 290 "sequence:%u resource_id:%u minor_code:%u major_code:%u.\n",
291 e->response_type, e->error_code,
292 e->sequence, e->resource_id, e->minor_code, e->major_code);
311 #if CONFIG_LIBXCB_XFIXES 312 static int check_xfixes(xcb_connection_t *
conn)
314 xcb_xfixes_query_version_cookie_t cookie;
315 xcb_xfixes_query_version_reply_t *reply;
317 cookie = xcb_xfixes_query_version(conn, XCB_XFIXES_MAJOR_VERSION,
318 XCB_XFIXES_MINOR_VERSION);
319 reply = xcb_xfixes_query_version_reply(conn, cookie,
NULL);
328 #define BLEND(target, source, alpha) \ 329 (target) + ((source) * (255 - (alpha)) + 255 / 2) / 255 332 xcb_query_pointer_reply_t *p,
333 xcb_get_geometry_reply_t *geo)
339 xcb_xfixes_get_cursor_image_cookie_t cc;
340 xcb_xfixes_get_cursor_image_reply_t *ci;
341 int cx, cy,
x,
y,
w,
h, c_off, i_off;
343 cc = xcb_xfixes_get_cursor_image(gr->
conn);
344 ci = xcb_xfixes_get_cursor_image_reply(gr->
conn, cc,
NULL);
348 cursor = xcb_xfixes_get_cursor_image_cursor_image(ci);
352 cx = ci->x - ci->xhot;
353 cy = ci->y - ci->yhot;
364 cursor += (y - cy) * ci->width;
367 for (y = 0; y <
h; y++) {
370 for (x = 0; x <
w; x++, cursor++, image +=
stride) {
374 g = (*cursor >> 8) & 0xff;
375 b = (*cursor >> 16) & 0xff;
376 a = (*cursor >> 24) & 0xff;
386 image[0] = BLEND(r, image[0], a);
387 image[1] = BLEND(g, image[1], a);
388 image[2] = BLEND(b, image[2], a);
392 cursor += ci->width - w - c_off;
393 image += (gr->
width - w - i_off) * stride;
406 xcb_configure_window(c->
conn,
408 XCB_CONFIG_WINDOW_X | XCB_CONFIG_WINDOW_Y,
415 xcb_query_pointer_cookie_t pc;
416 xcb_get_geometry_cookie_t gc;
417 xcb_query_pointer_reply_t *p =
NULL;
418 xcb_get_geometry_reply_t *geo =
NULL;
425 pc = xcb_query_pointer(c->
conn, c->
screen->root);
426 gc = xcb_get_geometry(c->
conn, c->
screen->root);
427 p = xcb_query_pointer_reply(c->
conn, pc,
NULL);
428 geo = xcb_get_geometry_reply(c->
conn, gc,
NULL);
437 #if CONFIG_LIBXCB_SHM 438 if (c->
has_shm && xcbgrab_frame_shm(s, pkt) < 0) {
448 #if CONFIG_LIBXCB_XFIXES 449 if (ret >= 0 && c->
draw_mouse && p->same_screen)
450 xcbgrab_draw_mouse(s, pkt, p, geo);
463 #if CONFIG_LIBXCB_SHM 467 xcb_disconnect(ctx->
conn);
472 static xcb_screen_t *
get_screen(
const xcb_setup_t *setup,
int screen_num)
474 xcb_screen_iterator_t it = xcb_setup_roots_iterator(setup);
477 for (; it.rem > 0; xcb_screen_next (&it)) {
493 const xcb_setup_t *setup = xcb_get_setup(c->
conn);
494 const xcb_format_t *fmt = xcb_setup_pixmap_formats(setup);
495 int length = xcb_setup_pixmap_formats_length(setup);
500 if (fmt->depth == depth) {
503 if (fmt->bits_per_pixel == 32)
507 if (fmt->bits_per_pixel == 32)
509 else if (fmt->bits_per_pixel == 24)
513 if (fmt->bits_per_pixel == 16)
517 if (fmt->bits_per_pixel == 16)
521 if (fmt->bits_per_pixel == 8)
528 *bpp = fmt->bits_per_pixel;
543 xcb_get_geometry_cookie_t gc;
544 xcb_get_geometry_reply_t *geo;
545 int64_t frame_size_bits;
557 gc = xcb_get_geometry(c->
conn, c->
screen->root);
558 geo = xcb_get_geometry_reply(c->
conn, gc,
NULL);
563 c->
width = geo->width;
567 if (c->
x + c->
width > geo->width ||
568 c->
y + c->
height > geo->height) {
570 "Capture area %dx%d at position %d.%d " 571 "outside the screen size %dx%d\n",
574 geo->width, geo->height);
596 #if CONFIG_LIBXCB_SHM 598 c->
conn, allocate_shm_buffer,
NULL);
615 xcb_gcontext_t gc = xcb_generate_id(c->
conn);
616 uint32_t
mask = XCB_GC_FOREGROUND |
621 uint32_t values[] = { c->
screen->black_pixel,
624 XCB_LINE_STYLE_DOUBLE_DASH,
625 XCB_FILL_STYLE_SOLID };
626 xcb_rectangle_t
r = { 1, 1,
630 xcb_create_gc(c->
conn, gc, c->
window, mask, values);
632 xcb_poly_rectangle(c->
conn, c->
window, gc, 1, &r);
638 uint32_t
mask = XCB_CW_OVERRIDE_REDIRECT | XCB_CW_EVENT_MASK;
639 uint32_t values[] = { 1,
640 XCB_EVENT_MASK_EXPOSURE |
641 XCB_EVENT_MASK_STRUCTURE_NOTIFY };
646 xcb_create_window(c->
conn, XCB_COPY_FROM_PARENT,
654 XCB_WINDOW_CLASS_INPUT_OUTPUT,
655 XCB_COPY_FROM_PARENT,
658 #if CONFIG_LIBXCB_SHAPE 659 xcb_shape_rectangles(c->
conn, XCB_SHAPE_SO_SUBTRACT,
660 XCB_SHAPE_SK_BOUNDING, XCB_CLIP_ORDERING_UNSORTED,
675 const xcb_setup_t *setup;
681 if (!sscanf(s->
url,
"%[^+]+%d,%d", display_name, &c->
x, &c->
y)) {
683 if(sscanf(s->
url,
"+%d,%d", &c->
x, &c->
y) != 2) {
689 c->
conn = xcb_connect(display_name[0] ? display_name :
NULL, &screen_num);
692 if ((ret = xcb_connection_has_error(c->
conn))) {
694 s->
url[0] ? s->
url :
"default", ret);
698 setup = xcb_get_setup(c->
conn);
715 #if CONFIG_LIBXCB_SHM 719 #if CONFIG_LIBXCB_XFIXES 723 "XFixes not available, cannot draw the mouse.\n");
747 .priv_class = &xcbgrab_class,
static xcb_screen_t * get_screen(const xcb_setup_t *setup, int screen_num)
AVInputFormat ff_xcbgrab_demuxer
void * av_buffer_pool_buffer_get_opaque(AVBufferRef *ref)
Query the original opaque parameter of an allocated buffer in the pool.
static enum AVPixelFormat pix_fmt
static av_cold int xcbgrab_read_header(AVFormatContext *s)
void av_buffer_unref(AVBufferRef **buf)
Free a given reference and automatically free the buffer if there are no more references to it...
int av_parse_video_rate(AVRational *rate, const char *arg)
Parse str and store the detected values in *rate.
static int create_stream(AVFormatContext *s)
static void xcbgrab_update_region(AVFormatContext *s)
#define AV_LOG_WARNING
Something somehow does not look correct.
#define LIBAVUTIL_VERSION_INT
packed RGB 8:8:8, 24bpp, RGBRGB...
static void draw_rectangle(AVFormatContext *s)
enum AVCodecID codec_id
Specific type of the encoded data (the codec used).
const char * av_default_item_name(void *ptr)
Return the context name.
int av_usleep(unsigned usec)
Sleep for a period of time.
const char * class_name
The name of the class; usually it is the same name as the context structure type to which the AVClass...
int64_t duration
Duration of this packet in AVStream->time_base units, 0 if unknown.
AVStream * avformat_new_stream(AVFormatContext *s, const AVCodec *c)
Add a new stream to a media file.
static const AVClass xcbgrab_class
static int pixfmt_from_pixmap_format(AVFormatContext *s, int depth, int *pix_fmt, int *bpp)
static int xcbgrab_frame(AVFormatContext *s, AVPacket *pkt)
static av_cold int read_close(AVFormatContext *ctx)
int64_t bit_rate
The average bitrate of the encoded data (in bits per second).
int64_t av_rescale_q(int64_t a, AVRational bq, AVRational cq)
Rescale a 64-bit integer by 2 rational numbers.
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
static const uint16_t mask[17]
#define NULL_IF_CONFIG_SMALL(x)
Return NULL if CONFIG_SMALL is true, otherwise the argument without modification. ...
char * url
input or output URL.
enum AVMediaType codec_type
General type of the encoded data.
AVBufferRef * buf
A reference to the reference-counted buffer where the packet data is stored.
AVBufferRef * av_buffer_create(uint8_t *data, int size, void(*free)(void *opaque, uint8_t *data), void *opaque, int flags)
Create an AVBuffer from an existing array.
AVRational avg_frame_rate
Average framerate.
common internal API header
int64_t av_rescale(int64_t a, int64_t b, int64_t c)
Rescale a 64-bit integer with rounding to nearest.
static const AVOption options[]
AVBufferPool * av_buffer_pool_init2(int size, void *opaque, AVBufferRef *(*alloc)(void *opaque, int size), void(*pool_free)(void *opaque))
Allocate and initialize a buffer pool with a more complex allocator.
static int read_header(FFV1Context *f)
int64_t av_gettime(void)
Get the current time in microseconds.
#define AVERROR_PATCHWELCOME
Not yet implemented in FFmpeg, patches welcome.
static int read_packet(void *opaque, uint8_t *buf, int buf_size)
char * av_strdup(const char *s)
Duplicate a string.
static int64_t wait_frame(AVFormatContext *s, AVPacket *pkt)
#define AV_TIME_BASE_Q
Internal time base represented as fractional value.
uint8_t * data
The data buffer.
Describe the class of an AVClass context structure.
void av_buffer_pool_uninit(AVBufferPool **ppool)
Mark the pool as being available for freeing.
static int xcbgrab_read_packet(AVFormatContext *s, AVPacket *pkt)
Rational number (pair of numerator and denominator).
offset must point to two consecutive integers
static void xcbgrab_image_reply_free(void *opaque, uint8_t *data)
static void setup_window(AVFormatContext *s)
void avpriv_report_missing_feature(void *avc, const char *msg,...) av_printf_format(2
Log a generic warning message about a missing feature.
#define flags(name, subs,...)
static int xcbgrab_reposition(AVFormatContext *s, xcb_query_pointer_reply_t *p, xcb_get_geometry_reply_t *geo)
A reference to a data buffer.
static int ref[MAX_W *MAX_W]
static av_cold int xcbgrab_read_close(AVFormatContext *s)
packed RGB 3:3:2, 8bpp, (msb)2R 3G 3B(lsb)
#define AV_PIX_FMT_RGB555
void av_init_packet(AVPacket *pkt)
Initialize optional fields of a packet with default values.
#define AV_INPUT_BUFFER_PADDING_SIZE
Required number of additionally allocated bytes at the end of the input bitstream for decoding...
void * priv_data
Format private data.
#define AV_PIX_FMT_RGB565
int64_t dts
Decompression timestamp in AVStream->time_base units; the time at which the packet is decompressed...
AVCodecParameters * codecpar
Codec parameters associated with this stream.
AVBufferRef * av_buffer_pool_get(AVBufferPool *pool)
Allocate a new AVBuffer, reusing an old buffer from the pool when available.
packed RGB 8:8:8, 32bpp, XRGBXRGB... X=unused/undefined
#define AVERROR_EXTERNAL
Generic error in an external library.
This structure stores compressed data.
int64_t pts
Presentation timestamp in AVStream->time_base units; the time at which the decompressed packet will b...
#define AV_PIX_FMT_0RGB32