FFmpeg
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
x11grab.c
Go to the documentation of this file.
1 /*
2  * X11 video grab interface
3  *
4  * This file is part of FFmpeg.
5  *
6  * FFmpeg integration:
7  * Copyright (C) 2006 Clemens Fruhwirth <clemens@endorphin.org>
8  * Edouard Gomez <ed.gomez@free.fr>
9  *
10  * This file contains code from grab.c:
11  * Copyright (c) 2000-2001 Fabrice Bellard
12  *
13  * This file contains code from the xvidcap project:
14  * Copyright (C) 1997-1998 Rasca, Berlin
15  * 2003-2004 Karl H. Beckers, Frankfurt
16  *
17  * FFmpeg is free software; you can redistribute it and/or modify
18  * it under the terms of the GNU General Public License as published by
19  * the Free Software Foundation; either version 2 of the License, or
20  * (at your option) any later version.
21  *
22  * FFmpeg is distributed in the hope that it will be useful,
23  * but WITHOUT ANY WARRANTY; without even the implied warranty of
24  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
25  * GNU General Public License for more details.
26  *
27  * You should have received a copy of the GNU General Public License
28  * along with FFmpeg; if not, write to the Free Software
29  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
30  */
31 
32 /**
33  * @file
34  * X11 frame device demuxer
35  * @author Clemens Fruhwirth <clemens@endorphin.org>
36  * @author Edouard Gomez <ed.gomez@free.fr>
37  */
38 
39 #include "config.h"
40 
41 #include <time.h>
42 #include <sys/shm.h>
43 
44 #include <X11/cursorfont.h>
45 #include <X11/X.h>
46 #include <X11/Xlib.h>
47 #include <X11/Xlibint.h>
48 #include <X11/Xproto.h>
49 #include <X11/Xutil.h>
50 
51 #include <X11/extensions/shape.h>
52 #include <X11/extensions/Xfixes.h>
53 #include <X11/extensions/XShm.h>
54 
55 #include "libavutil/internal.h"
56 #include "libavutil/log.h"
57 #include "libavutil/opt.h"
58 #include "libavutil/parseutils.h"
59 #include "libavutil/time.h"
60 
61 #include "libavformat/internal.h"
62 
63 #include "avdevice.h"
64 
65 /** X11 device demuxer context */
66 typedef struct X11GrabContext {
67  const AVClass *class; /**< Class for private options. */
68  int frame_size; /**< Size in bytes of a grabbed frame */
69  AVRational time_base; /**< Time base */
70  int64_t time_frame; /**< Current time */
71 
72  int width; /**< Width of the grab frame */
73  int height; /**< Height of the grab frame */
74  int x_off; /**< Horizontal top-left corner coordinate */
75  int y_off; /**< Vertical top-left corner coordinate */
76 
77  Display *dpy; /**< X11 display from which x11grab grabs frames */
78  XImage *image; /**< X11 image holding the grab */
79  int use_shm; /**< !0 when using XShm extension */
80  XShmSegmentInfo shminfo; /**< When using XShm, keeps track of XShm infos */
81  int draw_mouse; /**< Set by a private option. */
82  int follow_mouse; /**< Set by a private option. */
83  int show_region; /**< set by a private option. */
84  AVRational framerate; /**< Set by a private option. */
86  uint32_t palette[256];
87 
88  Cursor c;
89  Window region_win; /**< This is used by show_region option. */
91 
92 #define REGION_WIN_BORDER 3
93 
94 /**
95  * Draw grabbing region window
96  *
97  * @param s x11grab context
98  */
100 {
101  Display *dpy = s->dpy;
102  Window win = s->region_win;
103  int screen = DefaultScreen(dpy);
104  GC gc = XCreateGC(dpy, win, 0, 0);
105 
106  XSetForeground(dpy, gc, WhitePixel(dpy, screen));
107  XSetBackground(dpy, gc, BlackPixel(dpy, screen));
108  XSetLineAttributes(dpy, gc, REGION_WIN_BORDER, LineDoubleDash, 0, 0);
109  XDrawRectangle(dpy, win, gc, 1, 1,
110  (s->width + REGION_WIN_BORDER * 2) - 1 * 2 - 1,
111  (s->height + REGION_WIN_BORDER * 2) - 1 * 2 - 1);
112  XFreeGC(dpy, gc);
113 }
114 
115 /**
116  * Initialize grabbing region window
117  *
118  * @param s x11grab context
119  */
121 {
122  Display *dpy = s->dpy;
123  XRectangle rect;
124  XSetWindowAttributes attribs = { .override_redirect = True };
125  int screen = DefaultScreen(dpy);
126 
127  s->region_win = XCreateWindow(dpy, RootWindow(dpy, screen),
130  s->width + REGION_WIN_BORDER * 2,
131  s->height + REGION_WIN_BORDER * 2,
132  0, CopyFromParent,
133  InputOutput, CopyFromParent,
134  CWOverrideRedirect, &attribs);
135  rect.x = 0;
136  rect.y = 0;
137  rect.width = s->width;
138  rect.height = s->height;
139  XShapeCombineRectangles(dpy, s->region_win,
140  ShapeBounding, REGION_WIN_BORDER, REGION_WIN_BORDER,
141  &rect, 1, ShapeSubtract, 0);
142  XMapWindow(dpy, s->region_win);
143  XSelectInput(dpy, s->region_win, ExposureMask | StructureNotifyMask);
145 }
146 
147 static int setup_shm(AVFormatContext *s, Display *dpy, XImage **image)
148 {
149  X11GrabContext *g = s->priv_data;
150  int scr = XDefaultScreen(dpy);
151  XImage *img = XShmCreateImage(dpy, DefaultVisual(dpy, scr),
152  DefaultDepth(dpy, scr), ZPixmap, NULL,
153  &g->shminfo, g->width, g->height);
154 
155  g->shminfo.shmid = shmget(IPC_PRIVATE, img->bytes_per_line * img->height,
156  IPC_CREAT | 0777);
157 
158  if (g->shminfo.shmid == -1) {
159  av_log(s, AV_LOG_ERROR, "Cannot get shared memory!\n");
160  return AVERROR(ENOMEM);
161  }
162 
163  g->shminfo.shmaddr = img->data = shmat(g->shminfo.shmid, 0, 0);
164  g->shminfo.readOnly = False;
165 
166  if (!XShmAttach(dpy, &g->shminfo)) {
167  av_log(s, AV_LOG_ERROR, "Failed to attach shared memory!\n");
168  /* needs some better error subroutine :) */
169  return AVERROR(EIO);
170  }
171 
172  *image = img;
173  return 0;
174 }
175 
176 static int setup_mouse(Display *dpy, int screen)
177 {
178  int ev_ret, ev_err;
179 
180  if (XFixesQueryExtension(dpy, &ev_ret, &ev_err)) {
181  Window root = RootWindow(dpy, screen);
182  XFixesSelectCursorInput(dpy, root, XFixesDisplayCursorNotifyMask);
183  return 0;
184  }
185 
186  return AVERROR(ENOSYS);
187 }
188 
189 static int pixfmt_from_image(AVFormatContext *s, XImage *image, int *pix_fmt)
190 {
191  av_log(s, AV_LOG_DEBUG,
192  "Image r 0x%.6lx g 0x%.6lx b 0x%.6lx and depth %i\n",
193  image->red_mask,
194  image->green_mask,
195  image->blue_mask,
196  image->bits_per_pixel);
197 
198  *pix_fmt = AV_PIX_FMT_NONE;
199 
200  switch (image->bits_per_pixel) {
201  case 8:
202  *pix_fmt = AV_PIX_FMT_PAL8;
203  break;
204  case 16:
205  if (image->red_mask == 0xf800 &&
206  image->green_mask == 0x07e0 &&
207  image->blue_mask == 0x001f) {
208  *pix_fmt = AV_PIX_FMT_RGB565;
209  } else if (image->red_mask == 0x7c00 &&
210  image->green_mask == 0x03e0 &&
211  image->blue_mask == 0x001f) {
212  *pix_fmt = AV_PIX_FMT_RGB555;
213  }
214  break;
215  case 24:
216  if (image->red_mask == 0xff0000 &&
217  image->green_mask == 0x00ff00 &&
218  image->blue_mask == 0x0000ff) {
219  *pix_fmt = AV_PIX_FMT_BGR24;
220  } else if (image->red_mask == 0x0000ff &&
221  image->green_mask == 0x00ff00 &&
222  image->blue_mask == 0xff0000) {
223  *pix_fmt = AV_PIX_FMT_RGB24;
224  }
225  break;
226  case 32:
227  if (image->red_mask == 0xff0000 &&
228  image->green_mask == 0x00ff00 &&
229  image->blue_mask == 0x0000ff ) {
230  *pix_fmt = AV_PIX_FMT_0RGB32;
231  }
232  break;
233  }
234  if (*pix_fmt == AV_PIX_FMT_NONE) {
235  av_log(s, AV_LOG_ERROR,
236  "XImages with RGB mask 0x%.6lx 0x%.6lx 0x%.6lx and depth %i "
237  "are currently not supported.\n",
238  image->red_mask,
239  image->green_mask,
240  image->blue_mask,
241  image->bits_per_pixel);
242 
243  return AVERROR_PATCHWELCOME;
244  }
245 
246  return 0;
247 }
248 
249 /**
250  * Initialize the x11 grab device demuxer (public device demuxer API).
251  *
252  * @param s1 Context from avformat core
253  * @return <ul>
254  * <li>AVERROR(ENOMEM) no memory left</li>
255  * <li>AVERROR(EIO) other failure case</li>
256  * <li>0 success</li>
257  * </ul>
258  */
260 {
261  X11GrabContext *x11grab = s1->priv_data;
262  Display *dpy;
263  AVStream *st = NULL;
264  XImage *image;
265  int x_off = 0, y_off = 0, ret = 0, screen, use_shm = 0;
266  char *dpyname, *offset;
267  Colormap color_map;
268  XColor color[256];
269  int i;
270 
271  dpyname = av_strdup(s1->filename);
272  if (!dpyname)
273  goto out;
274 
275  offset = strchr(dpyname, '+');
276  if (offset) {
277  sscanf(offset, "%d,%d", &x_off, &y_off);
278  if (strstr(offset, "nomouse")) {
280  "'nomouse' specification in argument is deprecated: "
281  "use 'draw_mouse' option with value 0 instead\n");
282  x11grab->draw_mouse = 0;
283  }
284  *offset = 0;
285  }
286 
287  av_log(s1, AV_LOG_INFO,
288  "device: %s -> display: %s x: %d y: %d width: %d height: %d\n",
289  s1->filename, dpyname, x_off, y_off, x11grab->width, x11grab->height);
290 
291  dpy = XOpenDisplay(dpyname);
292  av_freep(&dpyname);
293  if (!dpy) {
294  av_log(s1, AV_LOG_ERROR, "Could not open X display.\n");
295  ret = AVERROR(EIO);
296  goto out;
297  }
298 
299  st = avformat_new_stream(s1, NULL);
300  if (!st) {
301  ret = AVERROR(ENOMEM);
302  goto out;
303  }
304  avpriv_set_pts_info(st, 64, 1, 1000000); /* 64 bits pts in us */
305 
306  screen = DefaultScreen(dpy);
307 
308  if (x11grab->follow_mouse) {
309  int screen_w, screen_h;
310  Window w;
311 
312  screen_w = DisplayWidth(dpy, screen);
313  screen_h = DisplayHeight(dpy, screen);
314  XQueryPointer(dpy, RootWindow(dpy, screen), &w, &w, &x_off, &y_off,
315  &ret, &ret, &ret);
316  x_off -= x11grab->width / 2;
317  y_off -= x11grab->height / 2;
318  x_off = av_clip(x_off, 0, screen_w - x11grab->width);
319  y_off = av_clip(y_off, 0, screen_h - x11grab->height);
320  av_log(s1, AV_LOG_INFO,
321  "followmouse is enabled, resetting grabbing region to x: %d y: %d\n",
322  x_off, y_off);
323  }
324 
325  if (x11grab->use_shm) {
326  use_shm = XShmQueryExtension(dpy);
327  av_log(s1, AV_LOG_INFO,
328  "shared memory extension %sfound\n", use_shm ? "" : "not ");
329  }
330 
331  if (use_shm && setup_shm(s1, dpy, &image) < 0) {
332  av_log(s1, AV_LOG_WARNING, "Falling back to XGetImage\n");
333  use_shm = 0;
334  }
335 
336  if (!use_shm) {
337  image = XGetImage(dpy, RootWindow(dpy, screen),
338  x_off, y_off,
339  x11grab->width, x11grab->height,
340  AllPlanes, ZPixmap);
341  }
342 
343  if (x11grab->draw_mouse && setup_mouse(dpy, screen) < 0) {
345  "XFixes not available, cannot draw the mouse cursor\n");
346  x11grab->draw_mouse = 0;
347  }
348 
349  x11grab->frame_size = x11grab->width * x11grab->height * image->bits_per_pixel / 8;
350  x11grab->dpy = dpy;
351  x11grab->time_base = av_inv_q(x11grab->framerate);
352  x11grab->time_frame = av_gettime() / av_q2d(x11grab->time_base);
353  x11grab->x_off = x_off;
354  x11grab->y_off = y_off;
355  x11grab->image = image;
356  x11grab->use_shm = use_shm;
357 
358  ret = pixfmt_from_image(s1, image, &st->codecpar->format);
359  if (ret < 0)
360  goto out;
361 
362  if (st->codecpar->format == AV_PIX_FMT_PAL8) {
363  color_map = DefaultColormap(dpy, screen);
364  for (i = 0; i < 256; ++i)
365  color[i].pixel = i;
366  XQueryColors(dpy, color_map, color, 256);
367  for (i = 0; i < 256; ++i)
368  x11grab->palette[i] = (color[i].red & 0xFF00) << 8 |
369  (color[i].green & 0xFF00) |
370  (color[i].blue & 0xFF00) >> 8;
371  x11grab->palette_changed = 1;
372  }
373 
374 
377  st->codecpar->width = x11grab->width;
378  st->codecpar->height = x11grab->height;
379  st->codecpar->bit_rate = x11grab->frame_size * 1 / av_q2d(x11grab->time_base) * 8;
380 
381  st->avg_frame_rate = av_inv_q(x11grab->time_base);
382 
383 out:
384  av_free(dpyname);
385  return ret;
386 }
387 
388 /**
389  * Paint a mouse pointer in an X11 image.
390  *
391  * @param image image to paint the mouse pointer to
392  * @param s context used to retrieve original grabbing rectangle
393  * coordinates
394  */
395 static void paint_mouse_pointer(XImage *image, AVFormatContext *s1)
396 {
397  X11GrabContext *s = s1->priv_data;
398  int x_off = s->x_off;
399  int y_off = s->y_off;
400  int width = s->width;
401  int height = s->height;
402  Display *dpy = s->dpy;
403  XFixesCursorImage *xcim;
404  int x, y;
405  int line, column;
406  int to_line, to_column;
407  int pixstride = image->bits_per_pixel >> 3;
408  /* Warning: in its insanity, xlib provides unsigned image data through a
409  * char* pointer, so we have to make it uint8_t to make things not break.
410  * Anyone who performs further investigation of the xlib API likely risks
411  * permanent brain damage. */
412  uint8_t *pix = image->data;
413  Window root;
414  XSetWindowAttributes attr;
415 
416  /* Code doesn't currently support 16-bit or PAL8 */
417  if (image->bits_per_pixel != 24 && image->bits_per_pixel != 32)
418  return;
419 
420  if (!s->c)
421  s->c = XCreateFontCursor(dpy, XC_left_ptr);
422  root = DefaultRootWindow(dpy);
423  attr.cursor = s->c;
424  XChangeWindowAttributes(dpy, root, CWCursor, &attr);
425 
426  xcim = XFixesGetCursorImage(dpy);
427  if (!xcim) {
429  "XFixesGetCursorImage failed\n");
430  return;
431  }
432 
433  x = xcim->x - xcim->xhot;
434  y = xcim->y - xcim->yhot;
435 
436  to_line = FFMIN((y + xcim->height), (height + y_off));
437  to_column = FFMIN((x + xcim->width), (width + x_off));
438 
439  for (line = FFMAX(y, y_off); line < to_line; line++) {
440  for (column = FFMAX(x, x_off); column < to_column; column++) {
441  int xcim_addr = (line - y) * xcim->width + column - x;
442  int image_addr = ((line - y_off) * width + column - x_off) * pixstride;
443  int r = (uint8_t)(xcim->pixels[xcim_addr] >> 0);
444  int g = (uint8_t)(xcim->pixels[xcim_addr] >> 8);
445  int b = (uint8_t)(xcim->pixels[xcim_addr] >> 16);
446  int a = (uint8_t)(xcim->pixels[xcim_addr] >> 24);
447 
448  if (a == 255) {
449  pix[image_addr + 0] = r;
450  pix[image_addr + 1] = g;
451  pix[image_addr + 2] = b;
452  } else if (a) {
453  /* pixel values from XFixesGetCursorImage come premultiplied by alpha */
454  pix[image_addr + 0] = r + (pix[image_addr + 0] * (255 - a) + 255 / 2) / 255;
455  pix[image_addr + 1] = g + (pix[image_addr + 1] * (255 - a) + 255 / 2) / 255;
456  pix[image_addr + 2] = b + (pix[image_addr + 2] * (255 - a) + 255 / 2) / 255;
457  }
458  }
459  }
460 
461  XFree(xcim);
462  xcim = NULL;
463 }
464 
465 /**
466  * Read new data in the image structure.
467  *
468  * @param dpy X11 display to grab from
469  * @param d
470  * @param image Image where the grab will be put
471  * @param x Top-Left grabbing rectangle horizontal coordinate
472  * @param y Top-Left grabbing rectangle vertical coordinate
473  * @return 0 if error, !0 if successful
474  */
475 static int xget_zpixmap(Display *dpy, Drawable d, XImage *image, int x, int y)
476 {
477  xGetImageReply rep;
478  xGetImageReq *req;
479  long nbytes;
480 
481  if (!image)
482  return 0;
483 
484  LockDisplay(dpy);
485  GetReq(GetImage, req);
486 
487  /* First set up the standard stuff in the request */
488  req->drawable = d;
489  req->x = x;
490  req->y = y;
491  req->width = image->width;
492  req->height = image->height;
493  req->planeMask = (unsigned int)AllPlanes;
494  req->format = ZPixmap;
495 
496  if (!_XReply(dpy, (xReply *)&rep, 0, xFalse) || !rep.length) {
497  UnlockDisplay(dpy);
498  SyncHandle();
499  return 0;
500  }
501 
502  nbytes = (long)rep.length << 2;
503  _XReadPad(dpy, image->data, nbytes);
504 
505  UnlockDisplay(dpy);
506  SyncHandle();
507  return 1;
508 }
509 
510 /**
511  * Grab a frame from x11 (public device demuxer API).
512  *
513  * @param s1 Context from avformat core
514  * @param pkt Packet holding the brabbed frame
515  * @return frame size in bytes
516  */
518 {
519  X11GrabContext *s = s1->priv_data;
520  Display *dpy = s->dpy;
521  XImage *image = s->image;
522  int x_off = s->x_off;
523  int y_off = s->y_off;
524  int follow_mouse = s->follow_mouse;
525  int screen, pointer_x, pointer_y, _, same_screen = 1;
526  Window w, root;
527  int64_t curtime, delay;
528  struct timespec ts;
529 
530  /* wait based on the frame rate */
531  for (;;) {
532  curtime = av_gettime();
533  delay = s->time_frame * av_q2d(s->time_base) - curtime;
534  if (delay <= 0) {
535  break;
536  }
537  ts.tv_sec = delay / 1000000;
538  ts.tv_nsec = (delay % 1000000) * 1000;
539  nanosleep(&ts, NULL);
540  }
541 
542  /* Calculate the time of the next frame */
543  do {
544  s->time_frame += INT64_C(1000000);
545  } while ((s->time_frame * av_q2d(s->time_base) - curtime) <= 0);
546 
547  av_init_packet(pkt);
548  pkt->data = image->data;
549  pkt->size = s->frame_size;
550  pkt->pts = curtime;
551  if (s->palette_changed) {
554  if (!pal) {
555  av_log(s, AV_LOG_ERROR, "Cannot append palette to packet\n");
556  } else {
557  memcpy(pal, s->palette, AVPALETTE_SIZE);
558  s->palette_changed = 0;
559  }
560  }
561 
562  screen = DefaultScreen(dpy);
563  root = RootWindow(dpy, screen);
564 
565  if (follow_mouse || s->draw_mouse)
566  same_screen = XQueryPointer(dpy, root, &w, &w,
567  &pointer_x, &pointer_y, &_, &_, &_);
568 
569  if (follow_mouse && same_screen) {
570  int screen_w, screen_h;
571 
572  screen_w = DisplayWidth(dpy, screen);
573  screen_h = DisplayHeight(dpy, screen);
574  if (follow_mouse == -1) {
575  // follow the mouse, put it at center of grabbing region
576  x_off += pointer_x - s->width / 2 - x_off;
577  y_off += pointer_y - s->height / 2 - y_off;
578  } else {
579  // follow the mouse, but only move the grabbing region when mouse
580  // reaches within certain pixels to the edge.
581  if (pointer_x > x_off + s->width - follow_mouse)
582  x_off += pointer_x - (x_off + s->width - follow_mouse);
583  else if (pointer_x < x_off + follow_mouse)
584  x_off -= (x_off + follow_mouse) - pointer_x;
585  if (pointer_y > y_off + s->height - follow_mouse)
586  y_off += pointer_y - (y_off + s->height - follow_mouse);
587  else if (pointer_y < y_off + follow_mouse)
588  y_off -= (y_off + follow_mouse) - pointer_y;
589  }
590  // adjust grabbing region position if it goes out of screen.
591  s->x_off = x_off = av_clip(x_off, 0, screen_w - s->width);
592  s->y_off = y_off = av_clip(y_off, 0, screen_h - s->height);
593 
594  if (s->show_region && s->region_win)
595  XMoveWindow(dpy, s->region_win,
597  s->y_off - REGION_WIN_BORDER);
598  }
599 
600  if (s->show_region && same_screen) {
601  if (s->region_win) {
602  XEvent evt = { .type = NoEventMask };
603  // Clean up the events, and do the initial draw or redraw.
604  while (XCheckMaskEvent(dpy, ExposureMask | StructureNotifyMask,
605  &evt))
606  ;
607  if (evt.type)
609  } else {
611  }
612  }
613 
614  if (s->use_shm) {
615  if (!XShmGetImage(dpy, root, image, x_off, y_off, AllPlanes))
616  av_log(s1, AV_LOG_INFO, "XShmGetImage() failed\n");
617  } else {
618  if (!xget_zpixmap(dpy, root, image, x_off, y_off))
619  av_log(s1, AV_LOG_INFO, "XGetZPixmap() failed\n");
620  }
621 
622  if (s->draw_mouse && same_screen)
623  paint_mouse_pointer(image, s1);
624 
625  return s->frame_size;
626 }
627 
628 /**
629  * Close x11 frame grabber (public device demuxer API).
630  *
631  * @param s1 Context from avformat core
632  * @return 0 success, !0 failure
633  */
635 {
636  X11GrabContext *x11grab = s1->priv_data;
637 
638  /* Detach cleanly from shared mem */
639  if (x11grab->use_shm) {
640  XShmDetach(x11grab->dpy, &x11grab->shminfo);
641  shmdt(x11grab->shminfo.shmaddr);
642  shmctl(x11grab->shminfo.shmid, IPC_RMID, NULL);
643  }
644 
645  /* Destroy X11 image */
646  if (x11grab->image) {
647  XDestroyImage(x11grab->image);
648  x11grab->image = NULL;
649  }
650 
651  if (x11grab->region_win)
652  XDestroyWindow(x11grab->dpy, x11grab->region_win);
653 
654  /* Free X11 display */
655  XCloseDisplay(x11grab->dpy);
656  return 0;
657 }
658 
659 #define OFFSET(x) offsetof(X11GrabContext, x)
660 #define DEC AV_OPT_FLAG_DECODING_PARAM
661 static const AVOption options[] = {
662  { "grab_x", "Initial x coordinate.", OFFSET(x_off), AV_OPT_TYPE_INT, { .i64 = 0 }, 0, INT_MAX, DEC },
663  { "grab_y", "Initial y coordinate.", OFFSET(y_off), AV_OPT_TYPE_INT, { .i64 = 0 }, 0, INT_MAX, DEC },
664  { "draw_mouse", "draw the mouse pointer", OFFSET(draw_mouse), AV_OPT_TYPE_INT, {.i64 = 1}, 0, 1, DEC },
665 
666  { "follow_mouse", "move the grabbing region when the mouse pointer reaches within specified amount of pixels to the edge of region",
667  OFFSET(follow_mouse), AV_OPT_TYPE_INT, {.i64 = 0}, -1, INT_MAX, DEC, "follow_mouse" },
668  { "centered", "keep the mouse pointer at the center of grabbing region when following",
669  0, AV_OPT_TYPE_CONST, {.i64 = -1}, INT_MIN, INT_MAX, DEC, "follow_mouse" },
670 
671  { "framerate", "set video frame rate", OFFSET(framerate), AV_OPT_TYPE_VIDEO_RATE, {.str = "ntsc"}, 0, 0, DEC },
672  { "show_region", "show the grabbing region", OFFSET(show_region), AV_OPT_TYPE_INT, {.i64 = 0}, 0, 1, DEC },
673  { "video_size", "set video frame size", OFFSET(width), AV_OPT_TYPE_IMAGE_SIZE, {.str = "vga"}, 0, 0, DEC },
674  { "use_shm", "use MIT-SHM extension", OFFSET(use_shm), AV_OPT_TYPE_INT, {.i64 = 1}, 0, 1, DEC },
675  { NULL },
676 };
677 
678 static const AVClass x11_class = {
679  .class_name = "X11grab indev",
680  .item_name = av_default_item_name,
681  .option = options,
682  .version = LIBAVUTIL_VERSION_INT,
684 };
685 
686 /** x11 grabber device demuxer declaration */
688  .name = "x11grab",
689  .long_name = NULL_IF_CONFIG_SMALL("X11grab"),
690  .priv_data_size = sizeof(X11GrabContext),
694  .flags = AVFMT_NOFILE,
695  .priv_class = &x11_class,
696 };
static int x11grab_read_header(AVFormatContext *s1)
Initialize the x11 grab device demuxer (public device demuxer API).
Definition: x11grab.c:259
#define NULL
Definition: coverity.c:32
const char * s
Definition: avisynth_c.h:631
static enum AVPixelFormat pix_fmt
static int setup_shm(AVFormatContext *s, Display *dpy, XImage **image)
Definition: x11grab.c:147
AVOption.
Definition: opt.h:245
AVInputFormat ff_x11grab_demuxer
x11 grabber device demuxer declaration
Definition: x11grab.c:687
#define AV_LOG_WARNING
Something somehow does not look correct.
Definition: log.h:182
#define LIBAVUTIL_VERSION_INT
Definition: version.h:70
packed RGB 8:8:8, 24bpp, RGBRGB...
Definition: pixfmt.h:64
void avpriv_set_pts_info(AVStream *s, int pts_wrap_bits, unsigned int pts_num, unsigned int pts_den)
Set the time base and wrapping info for a given stream.
Definition: utils.c:4404
const char * g
Definition: vf_curves.c:108
#define REGION_WIN_BORDER
Definition: x11grab.c:92
Cursor c
Definition: x11grab.c:88
enum AVCodecID codec_id
Specific type of the encoded data (the codec used).
Definition: avcodec.h:3915
int x_off
Horizontal top-left corner coordinate.
Definition: x11grab.c:74
int size
Definition: avcodec.h:1578
const char * b
Definition: vf_curves.c:109
int show_region
set by a private option.
Definition: x11grab.c:83
static AVPacket pkt
Window region_win
This is used by show_region option.
Definition: x11grab.c:89
static int x11grab_read_packet(AVFormatContext *s1, AVPacket *pkt)
Grab a frame from x11 (public device demuxer API).
Definition: x11grab.c:517
uint32_t palette[256]
Definition: x11grab.c:86
#define _(x)
Format I/O context.
Definition: avformat.h:1319
#define img
const char * class_name
The name of the class; usually it is the same name as the context structure type to which the AVClass...
Definition: log.h:72
static void x11grab_draw_region_win(X11GrabContext *s)
Draw grabbing region window.
Definition: x11grab.c:99
uint8_t
int width
Video only.
Definition: avcodec.h:3981
8 bit with AV_PIX_FMT_RGB32 palette
Definition: pixfmt.h:73
AVOptions.
#define AVPALETTE_SIZE
Definition: pixfmt.h:32
#define OFFSET(x)
Definition: x11grab.c:659
AVStream * avformat_new_stream(AVFormatContext *s, const AVCodec *c)
Add a new stream to a media file.
Definition: utils.c:4042
static double av_q2d(AVRational a)
Convert rational to double.
Definition: rational.h:80
uint8_t * data
Definition: avcodec.h:1577
#define DEC
Definition: x11grab.c:660
static av_cold int read_close(AVFormatContext *ctx)
Definition: libcdio.c:145
static void x11grab_region_win_init(X11GrabContext *s)
Initialize grabbing region window.
Definition: x11grab.c:120
#define av_log(a,...)
int64_t bit_rate
The average bitrate of the encoded data (in bits per second).
Definition: avcodec.h:3944
Main libavdevice API header.
static const AVOption options[]
Definition: x11grab.c:661
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition: log.h:176
static int pixfmt_from_image(AVFormatContext *s, XImage *image, int *pix_fmt)
Definition: x11grab.c:189
av_default_item_name
#define AVERROR(e)
Definition: error.h:43
#define NULL_IF_CONFIG_SMALL(x)
Return NULL if CONFIG_SMALL is true, otherwise the argument without modification. ...
Definition: internal.h:176
const char * r
Definition: vf_curves.c:107
#define AV_LOG_DEBUG
Stuff which is only useful for libav* developers.
Definition: log.h:197
enum AVMediaType codec_type
General type of the encoded data.
Definition: avcodec.h:3911
Definition: graph2dot.c:48
static const AVClass x11_class
Definition: x11grab.c:678
static const uint8_t offset[127][2]
Definition: vf_spp.c:92
static int x11grab_read_close(AVFormatContext *s1)
Close x11 frame grabber (public device demuxer API).
Definition: x11grab.c:634
AVRational avg_frame_rate
Average framerate.
Definition: avformat.h:954
#define FFMAX(a, b)
Definition: common.h:94
AVRational time_base
Time base.
Definition: x11grab.c:69
int64_t time_frame
Current time.
Definition: x11grab.c:70
common internal API header
static SDL_Surface * screen
Definition: ffplay.c:364
static int xget_zpixmap(Display *dpy, Drawable d, XImage *image, int x, int y)
Read new data in the image structure.
Definition: x11grab.c:475
char filename[1024]
input or output filename
Definition: avformat.h:1395
#define FFMIN(a, b)
Definition: common.h:96
int height
Height of the grab frame.
Definition: x11grab.c:73
#define width
int use_shm
!0 when using XShm extension
Definition: x11grab.c:79
packed RGB 8:8:8, 24bpp, BGRBGR...
Definition: pixfmt.h:65
static void paint_mouse_pointer(XImage *image, AVFormatContext *s1)
Paint a mouse pointer in an X11 image.
Definition: x11grab.c:395
static int read_header(FFV1Context *f)
Definition: ffv1dec.c:638
FILE * out
Definition: movenc-test.c:54
int64_t av_gettime(void)
Get the current time in microseconds.
Definition: time.c:39
Stream structure.
Definition: avformat.h:876
#define AVERROR_PATCHWELCOME
Not yet implemented in FFmpeg, patches welcome.
Definition: error.h:62
static int read_packet(void *opaque, uint8_t *buf, int buf_size)
Definition: avio_reading.c:42
X11 device demuxer context.
Definition: x11grab.c:66
#define AV_LOG_INFO
Standard information.
Definition: log.h:187
int palette_changed
Definition: x11grab.c:85
XShmSegmentInfo shminfo
When using XShm, keeps track of XShm infos.
Definition: x11grab.c:80
char * av_strdup(const char *s)
Duplicate the string s.
Definition: mem.c:267
static int setup_mouse(Display *dpy, int screen)
Definition: x11grab.c:176
int frame_size
Size in bytes of a grabbed frame.
Definition: x11grab.c:68
Describe the class of an AVClass context structure.
Definition: log.h:67
Definition: f_ebur128.c:91
rational number numerator/denominator
Definition: rational.h:43
XImage * image
X11 image holding the grab.
Definition: x11grab.c:78
offset must point to AVRational
Definition: opt.h:235
int width
Width of the grab frame.
Definition: x11grab.c:72
#define s1
Definition: regdef.h:38
offset must point to two consecutive integers
Definition: opt.h:232
misc parsing utilities
uint8_t pixel
Definition: tiny_ssim.c:42
int follow_mouse
Set by a private option.
Definition: x11grab.c:82
static av_always_inline AVRational av_inv_q(AVRational q)
Invert a rational.
Definition: rational.h:133
static int flags
Definition: cpu.c:47
#define AVFMT_NOFILE
Demuxer will use avio_open, no opened file should be provided by the caller.
Definition: avformat.h:476
#define AV_PIX_FMT_RGB555
Definition: pixfmt.h:326
void av_init_packet(AVPacket *pkt)
Initialize optional fields of a packet with default values.
Definition: avpacket.c:33
int draw_mouse
Set by a private option.
Definition: x11grab.c:81
int y_off
Vertical top-left corner coordinate.
Definition: x11grab.c:75
#define av_free(p)
Display * dpy
X11 display from which x11grab grabs frames.
Definition: x11grab.c:77
void * priv_data
Format private data.
Definition: avformat.h:1347
#define AV_PIX_FMT_RGB565
Definition: pixfmt.h:325
#define av_freep(p)
const char * name
A comma separated list of short names for the format.
Definition: avformat.h:660
AVCodecParameters * codecpar
Definition: avformat.h:1006
#define height
uint8_t * av_packet_new_side_data(AVPacket *pkt, enum AVPacketSideDataType type, int size)
Allocate new information of a packet.
Definition: avpacket.c:299
AVRational framerate
Set by a private option.
Definition: x11grab.c:84
This structure stores compressed data.
Definition: avcodec.h:1554
int64_t pts
Presentation timestamp in AVStream->time_base units; the time at which the decompressed packet will b...
Definition: avcodec.h:1570
#define AV_PIX_FMT_0RGB32
Definition: pixfmt.h:319