Libav
x11grab.c
Go to the documentation of this file.
1 /*
2  * X11 video grab interface
3  *
4  * This file is part of Libav.
5  *
6  * Libav 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  * Libav 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  * Libav 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 Libav; if not, write to the Free Software
29  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
30  */
31 
39 #include "config.h"
40 #include "libavformat/avformat.h"
41 #include "libavformat/internal.h"
42 #include "libavutil/log.h"
43 #include "libavutil/opt.h"
44 #include "libavutil/parseutils.h"
45 #include "libavutil/time.h"
46 #include <time.h>
47 #include <X11/X.h>
48 #include <X11/Xlib.h>
49 #include <X11/Xlibint.h>
50 #include <X11/Xproto.h>
51 #include <X11/Xutil.h>
52 #include <sys/shm.h>
53 #include <X11/extensions/shape.h>
54 #include <X11/extensions/XShm.h>
55 #include <X11/extensions/Xfixes.h>
56 
60 struct x11grab {
61  const AVClass *class;
62  int frame_size;
64  int64_t time_frame;
66  char *video_size;
67  int height;
68  int width;
69  int x_off;
70  int y_off;
72  Display *dpy;
73  XImage *image;
74  int use_shm;
75  XShmSegmentInfo shminfo;
76  int draw_mouse;
79  char *framerate;
81  Window region_win;
82 };
83 
84 #define REGION_WIN_BORDER 3
85 
90 static void
92 {
93  Display *dpy = s->dpy;
94  int screen;
95  Window win = s->region_win;
96  GC gc;
97 
98  screen = DefaultScreen(dpy);
99  gc = XCreateGC(dpy, win, 0, 0);
100  XSetForeground(dpy, gc, WhitePixel(dpy, screen));
101  XSetBackground(dpy, gc, BlackPixel(dpy, screen));
102  XSetLineAttributes(dpy, gc, REGION_WIN_BORDER, LineDoubleDash, 0, 0);
103  XDrawRectangle(dpy, win, gc,
104  1, 1,
105  (s->width + REGION_WIN_BORDER * 2) - 1 * 2 - 1,
106  (s->height + REGION_WIN_BORDER * 2) - 1 * 2 - 1);
107  XFreeGC(dpy, gc);
108 }
109 
115 static void
117 {
118  Display *dpy = s->dpy;
119  int screen;
120  XSetWindowAttributes attribs;
121  XRectangle rect;
122 
123  screen = DefaultScreen(dpy);
124  attribs.override_redirect = True;
125  s->region_win = XCreateWindow(dpy, RootWindow(dpy, screen),
128  s->width + REGION_WIN_BORDER * 2,
129  s->height + REGION_WIN_BORDER * 2,
130  0, CopyFromParent,
131  InputOutput, CopyFromParent,
132  CWOverrideRedirect, &attribs);
133  rect.x = 0;
134  rect.y = 0;
135  rect.width = s->width;
136  rect.height = s->height;
137  XShapeCombineRectangles(dpy, s->region_win,
138  ShapeBounding, REGION_WIN_BORDER, REGION_WIN_BORDER,
139  &rect, 1, ShapeSubtract, 0);
140  XMapWindow(dpy, s->region_win);
141  XSelectInput(dpy, s->region_win, ExposureMask | StructureNotifyMask);
143 }
144 
155 static int
157 {
158  struct x11grab *x11grab = s1->priv_data;
159  Display *dpy;
160  AVStream *st = NULL;
161  enum AVPixelFormat input_pixfmt;
162  XImage *image;
163  int x_off = 0;
164  int y_off = 0;
165  int screen;
166  int use_shm;
167  char *param, *offset;
168  int ret = 0;
170 
171  param = av_strdup(s1->filename);
172  if (!param)
173  goto out;
174 
175  offset = strchr(param, '+');
176  if (offset) {
177  sscanf(offset, "%d,%d", &x_off, &y_off);
178  x11grab->draw_mouse = !strstr(offset, "nomouse");
179  *offset= 0;
180  }
181 
182  if ((ret = av_parse_video_size(&x11grab->width, &x11grab->height, x11grab->video_size)) < 0) {
183  av_log(s1, AV_LOG_ERROR, "Couldn't parse video size.\n");
184  goto out;
185  }
186  if ((ret = av_parse_video_rate(&framerate, x11grab->framerate)) < 0) {
187  av_log(s1, AV_LOG_ERROR, "Could not parse framerate: %s.\n", x11grab->framerate);
188  goto out;
189  }
190  av_log(s1, AV_LOG_INFO, "device: %s -> display: %s x: %d y: %d width: %d height: %d\n",
191  s1->filename, param, x_off, y_off, x11grab->width, x11grab->height);
192 
193  dpy = XOpenDisplay(param);
194  if(!dpy) {
195  av_log(s1, AV_LOG_ERROR, "Could not open X display.\n");
196  ret = AVERROR(EIO);
197  goto out;
198  }
199 
200  st = avformat_new_stream(s1, NULL);
201  if (!st) {
202  ret = AVERROR(ENOMEM);
203  goto out;
204  }
205  avpriv_set_pts_info(st, 64, 1, 1000000); /* 64 bits pts in us */
206 
207  screen = DefaultScreen(dpy);
208 
209  if (x11grab->follow_mouse) {
210  int screen_w, screen_h;
211  Window w;
212 
213  screen_w = DisplayWidth(dpy, screen);
214  screen_h = DisplayHeight(dpy, screen);
215  XQueryPointer(dpy, RootWindow(dpy, screen), &w, &w, &x_off, &y_off, &ret, &ret, &ret);
216  x_off -= x11grab->width / 2;
217  y_off -= x11grab->height / 2;
218  x_off = FFMIN(FFMAX(x_off, 0), screen_w - x11grab->width);
219  y_off = FFMIN(FFMAX(y_off, 0), screen_h - x11grab->height);
220  av_log(s1, AV_LOG_INFO, "followmouse is enabled, resetting grabbing region to x: %d y: %d\n", x_off, y_off);
221  }
222 
223  use_shm = XShmQueryExtension(dpy);
224  av_log(s1, AV_LOG_INFO, "shared memory extension %s found\n", use_shm ? "" : "not");
225 
226  if(use_shm) {
227  int scr = XDefaultScreen(dpy);
228  image = XShmCreateImage(dpy,
229  DefaultVisual(dpy, scr),
230  DefaultDepth(dpy, scr),
231  ZPixmap,
232  NULL,
233  &x11grab->shminfo,
234  x11grab->width, x11grab->height);
235  x11grab->shminfo.shmid = shmget(IPC_PRIVATE,
236  image->bytes_per_line * image->height,
237  IPC_CREAT|0777);
238  if (x11grab->shminfo.shmid == -1) {
239  av_log(s1, AV_LOG_ERROR, "Fatal: Can't get shared memory!\n");
240  ret = AVERROR(ENOMEM);
241  goto out;
242  }
243  x11grab->shminfo.shmaddr = image->data = shmat(x11grab->shminfo.shmid, 0, 0);
244  x11grab->shminfo.readOnly = False;
245 
246  if (!XShmAttach(dpy, &x11grab->shminfo)) {
247  av_log(s1, AV_LOG_ERROR, "Fatal: Failed to attach shared memory!\n");
248  /* needs some better error subroutine :) */
249  ret = AVERROR(EIO);
250  goto out;
251  }
252  } else {
253  image = XGetImage(dpy, RootWindow(dpy, screen),
254  x_off,y_off,
255  x11grab->width, x11grab->height,
256  AllPlanes, ZPixmap);
257  }
258 
259  switch (image->bits_per_pixel) {
260  case 8:
261  av_log (s1, AV_LOG_DEBUG, "8 bit palette\n");
262  input_pixfmt = AV_PIX_FMT_PAL8;
263  break;
264  case 16:
265  if ( image->red_mask == 0xf800 &&
266  image->green_mask == 0x07e0 &&
267  image->blue_mask == 0x001f ) {
268  av_log (s1, AV_LOG_DEBUG, "16 bit RGB565\n");
269  input_pixfmt = AV_PIX_FMT_RGB565;
270  } else if (image->red_mask == 0x7c00 &&
271  image->green_mask == 0x03e0 &&
272  image->blue_mask == 0x001f ) {
273  av_log(s1, AV_LOG_DEBUG, "16 bit RGB555\n");
274  input_pixfmt = AV_PIX_FMT_RGB555;
275  } else {
276  av_log(s1, AV_LOG_ERROR, "RGB ordering at image depth %i not supported ... aborting\n", image->bits_per_pixel);
277  av_log(s1, AV_LOG_ERROR, "color masks: r 0x%.6lx g 0x%.6lx b 0x%.6lx\n", image->red_mask, image->green_mask, image->blue_mask);
278  ret = AVERROR(EIO);
279  goto out;
280  }
281  break;
282  case 24:
283  if ( image->red_mask == 0xff0000 &&
284  image->green_mask == 0x00ff00 &&
285  image->blue_mask == 0x0000ff ) {
286  input_pixfmt = AV_PIX_FMT_BGR24;
287  } else if ( image->red_mask == 0x0000ff &&
288  image->green_mask == 0x00ff00 &&
289  image->blue_mask == 0xff0000 ) {
290  input_pixfmt = AV_PIX_FMT_RGB24;
291  } else {
292  av_log(s1, AV_LOG_ERROR,"rgb ordering at image depth %i not supported ... aborting\n", image->bits_per_pixel);
293  av_log(s1, AV_LOG_ERROR, "color masks: r 0x%.6lx g 0x%.6lx b 0x%.6lx\n", image->red_mask, image->green_mask, image->blue_mask);
294  ret = AVERROR(EIO);
295  goto out;
296  }
297  break;
298  case 32:
299  input_pixfmt = AV_PIX_FMT_RGB32;
300  break;
301  default:
302  av_log(s1, AV_LOG_ERROR, "image depth %i not supported ... aborting\n", image->bits_per_pixel);
303  ret = AVERROR(EINVAL);
304  goto out;
305  }
306 
307  x11grab->frame_size = x11grab->width * x11grab->height * image->bits_per_pixel/8;
308  x11grab->dpy = dpy;
309  x11grab->time_base = (AVRational){framerate.den, framerate.num};
310  x11grab->time_frame = av_gettime() / av_q2d(x11grab->time_base);
311  x11grab->x_off = x_off;
312  x11grab->y_off = y_off;
313  x11grab->image = image;
314  x11grab->use_shm = use_shm;
315 
318  st->codec->width = x11grab->width;
319  st->codec->height = x11grab->height;
320  st->codec->pix_fmt = input_pixfmt;
321  st->codec->time_base = x11grab->time_base;
322  st->codec->bit_rate = x11grab->frame_size * 1/av_q2d(x11grab->time_base) * 8;
323 
324 out:
325  av_free(param);
326  return ret;
327 }
328 
336 static void
337 paint_mouse_pointer(XImage *image, struct x11grab *s)
338 {
339  int x_off = s->x_off;
340  int y_off = s->y_off;
341  int width = s->width;
342  int height = s->height;
343  Display *dpy = s->dpy;
344  XFixesCursorImage *xcim;
345  int x, y;
346  int line, column;
347  int to_line, to_column;
348  int pixstride = image->bits_per_pixel >> 3;
349  /* Warning: in its insanity, xlib provides unsigned image data through a
350  * char* pointer, so we have to make it uint8_t to make things not break.
351  * Anyone who performs further investigation of the xlib API likely risks
352  * permanent brain damage. */
353  uint8_t *pix = image->data;
354 
355  /* Code doesn't currently support 16-bit or PAL8 */
356  if (image->bits_per_pixel != 24 && image->bits_per_pixel != 32)
357  return;
358 
359  xcim = XFixesGetCursorImage(dpy);
360 
361  x = xcim->x - xcim->xhot;
362  y = xcim->y - xcim->yhot;
363 
364  to_line = FFMIN((y + xcim->height), (height + y_off));
365  to_column = FFMIN((x + xcim->width), (width + x_off));
366 
367  for (line = FFMAX(y, y_off); line < to_line; line++) {
368  for (column = FFMAX(x, x_off); column < to_column; column++) {
369  int xcim_addr = (line - y) * xcim->width + column - x;
370  int image_addr = ((line - y_off) * width + column - x_off) * pixstride;
371  int r = (uint8_t)(xcim->pixels[xcim_addr] >> 0);
372  int g = (uint8_t)(xcim->pixels[xcim_addr] >> 8);
373  int b = (uint8_t)(xcim->pixels[xcim_addr] >> 16);
374  int a = (uint8_t)(xcim->pixels[xcim_addr] >> 24);
375 
376  if (a == 255) {
377  pix[image_addr+0] = r;
378  pix[image_addr+1] = g;
379  pix[image_addr+2] = b;
380  } else if (a) {
381  /* pixel values from XFixesGetCursorImage come premultiplied by alpha */
382  pix[image_addr+0] = r + (pix[image_addr+0]*(255-a) + 255/2) / 255;
383  pix[image_addr+1] = g + (pix[image_addr+1]*(255-a) + 255/2) / 255;
384  pix[image_addr+2] = b + (pix[image_addr+2]*(255-a) + 255/2) / 255;
385  }
386  }
387  }
388 
389  XFree(xcim);
390  xcim = NULL;
391 }
392 
393 
404 static int
405 xget_zpixmap(Display *dpy, Drawable d, XImage *image, int x, int y)
406 {
407  xGetImageReply rep;
408  xGetImageReq *req;
409  long nbytes;
410 
411  if (!image) {
412  return 0;
413  }
414 
415  LockDisplay(dpy);
416  GetReq(GetImage, req);
417 
418  /* First set up the standard stuff in the request */
419  req->drawable = d;
420  req->x = x;
421  req->y = y;
422  req->width = image->width;
423  req->height = image->height;
424  req->planeMask = (unsigned int)AllPlanes;
425  req->format = ZPixmap;
426 
427  if (!_XReply(dpy, (xReply *)&rep, 0, xFalse) || !rep.length) {
428  UnlockDisplay(dpy);
429  SyncHandle();
430  return 0;
431  }
432 
433  nbytes = (long)rep.length << 2;
434  _XReadPad(dpy, image->data, nbytes);
435 
436  UnlockDisplay(dpy);
437  SyncHandle();
438  return 1;
439 }
440 
448 static int
450 {
451  struct x11grab *s = s1->priv_data;
452  Display *dpy = s->dpy;
453  XImage *image = s->image;
454  int x_off = s->x_off;
455  int y_off = s->y_off;
456 
457  int screen;
458  Window root;
459  int follow_mouse = s->follow_mouse;
460 
461  int64_t curtime, delay;
462  struct timespec ts;
463 
464  /* Calculate the time of the next frame */
465  s->time_frame += INT64_C(1000000);
466 
467  /* wait based on the frame rate */
468  for(;;) {
469  curtime = av_gettime();
470  delay = s->time_frame * av_q2d(s->time_base) - curtime;
471  if (delay <= 0) {
472  if (delay < INT64_C(-1000000) * av_q2d(s->time_base)) {
473  s->time_frame += INT64_C(1000000);
474  }
475  break;
476  }
477  ts.tv_sec = delay / 1000000;
478  ts.tv_nsec = (delay % 1000000) * 1000;
479  nanosleep(&ts, NULL);
480  }
481 
482  av_init_packet(pkt);
483  pkt->data = image->data;
484  pkt->size = s->frame_size;
485  pkt->pts = curtime;
486 
487  screen = DefaultScreen(dpy);
488  root = RootWindow(dpy, screen);
489  if (follow_mouse) {
490  int screen_w, screen_h;
491  int pointer_x, pointer_y, _;
492  Window w;
493 
494  screen_w = DisplayWidth(dpy, screen);
495  screen_h = DisplayHeight(dpy, screen);
496  XQueryPointer(dpy, root, &w, &w, &pointer_x, &pointer_y, &_, &_, &_);
497  if (follow_mouse == -1) {
498  // follow the mouse, put it at center of grabbing region
499  x_off += pointer_x - s->width / 2 - x_off;
500  y_off += pointer_y - s->height / 2 - y_off;
501  } else {
502  // follow the mouse, but only move the grabbing region when mouse
503  // reaches within certain pixels to the edge.
504  if (pointer_x > x_off + s->width - follow_mouse) {
505  x_off += pointer_x - (x_off + s->width - follow_mouse);
506  } else if (pointer_x < x_off + follow_mouse)
507  x_off -= (x_off + follow_mouse) - pointer_x;
508  if (pointer_y > y_off + s->height - follow_mouse) {
509  y_off += pointer_y - (y_off + s->height - follow_mouse);
510  } else if (pointer_y < y_off + follow_mouse)
511  y_off -= (y_off + follow_mouse) - pointer_y;
512  }
513  // adjust grabbing region position if it goes out of screen.
514  s->x_off = x_off = FFMIN(FFMAX(x_off, 0), screen_w - s->width);
515  s->y_off = y_off = FFMIN(FFMAX(y_off, 0), screen_h - s->height);
516 
517  if (s->show_region && s->region_win)
518  XMoveWindow(dpy, s->region_win,
520  s->y_off - REGION_WIN_BORDER);
521  }
522 
523  if (s->show_region) {
524  if (s->region_win) {
525  XEvent evt;
526  // clean up the events, and do the initinal draw or redraw.
527  for (evt.type = NoEventMask; XCheckMaskEvent(dpy, ExposureMask | StructureNotifyMask, &evt); );
528  if (evt.type)
530  } else {
532  }
533  }
534 
535  if(s->use_shm) {
536  if (!XShmGetImage(dpy, root, image, x_off, y_off, AllPlanes)) {
537  av_log (s1, AV_LOG_INFO, "XShmGetImage() failed\n");
538  }
539  } else {
540  if (!xget_zpixmap(dpy, root, image, x_off, y_off)) {
541  av_log (s1, AV_LOG_INFO, "XGetZPixmap() failed\n");
542  }
543  }
544 
545  if (s->draw_mouse) {
546  paint_mouse_pointer(image, s);
547  }
548 
549  return s->frame_size;
550 }
551 
558 static int
560 {
561  struct x11grab *x11grab = s1->priv_data;
562 
563  /* Detach cleanly from shared mem */
564  if (x11grab->use_shm) {
565  XShmDetach(x11grab->dpy, &x11grab->shminfo);
566  shmdt(x11grab->shminfo.shmaddr);
567  shmctl(x11grab->shminfo.shmid, IPC_RMID, NULL);
568  }
569 
570  /* Destroy X11 image */
571  if (x11grab->image) {
572  XDestroyImage(x11grab->image);
573  x11grab->image = NULL;
574  }
575 
576  if (x11grab->region_win) {
577  XDestroyWindow(x11grab->dpy, x11grab->region_win);
578  }
579 
580  /* Free X11 display */
581  XCloseDisplay(x11grab->dpy);
582  return 0;
583 }
584 
585 #define OFFSET(x) offsetof(struct x11grab, x)
586 #define DEC AV_OPT_FLAG_DECODING_PARAM
587 static const AVOption options[] = {
588  { "video_size", "A string describing frame size, such as 640x480 or hd720.", OFFSET(video_size), AV_OPT_TYPE_STRING, {.str = "vga"}, 0, 0, DEC },
589  { "framerate", "", OFFSET(framerate), AV_OPT_TYPE_STRING, {.str = "ntsc"}, 0, 0, DEC },
590  { "draw_mouse", "Draw the mouse pointer.", OFFSET(draw_mouse), AV_OPT_TYPE_INT, { .i64 = 1 }, 0, 1, DEC },
591  { "follow_mouse", "Move the grabbing region when the mouse pointer reaches within specified amount of pixels to the edge of region.",
592  OFFSET(follow_mouse), AV_OPT_TYPE_INT, { .i64 = 0 }, -1, INT_MAX, DEC, "follow_mouse" },
593  { "centered", "Keep the mouse pointer at the center of grabbing region when following.", 0, AV_OPT_TYPE_CONST, { .i64 = -1 }, INT_MIN, INT_MAX, DEC, "follow_mouse" },
594  { "show_region", "Show the grabbing region.", OFFSET(show_region), AV_OPT_TYPE_INT, { .i64 = 0 }, 0, 1, DEC },
595  { NULL },
596 };
597 
598 static const AVClass x11_class = {
599  .class_name = "X11grab indev",
600  .item_name = av_default_item_name,
601  .option = options,
602  .version = LIBAVUTIL_VERSION_INT,
603 };
604 
607  .name = "x11grab",
608  .long_name = NULL_IF_CONFIG_SMALL("X11grab"),
609  .priv_data_size = sizeof(struct x11grab),
610  .read_header = x11grab_read_header,
611  .read_packet = x11grab_read_packet,
612  .read_close = x11grab_read_close,
613  .flags = AVFMT_NOFILE,
614  .priv_class = &x11_class,
615 };
#define FFMAX(a, b)
Definition: common.h:55
static int x11grab_read_header(AVFormatContext *s1)
Initialize the x11 grab device demuxer (public device demuxer API).
Definition: x11grab.c:156
AVOption.
Definition: opt.h:234
int y_off
Vertical top-left corner coordinate.
Definition: x11grab.c:70
AVInputFormat ff_x11grab_demuxer
x11 grabber device demuxer declaration
Definition: x11grab.c:606
const char * name
A comma separated list of short names for the format.
Definition: avformat.h:525
packed RGB 8:8:8, 24bpp, RGBRGB...
Definition: pixfmt.h:67
char * av_strdup(const char *s) av_malloc_attrib
Duplicate the string s.
Definition: mem.c:213
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:2821
#define REGION_WIN_BORDER
Definition: x11grab.c:84
AVCodecContext * codec
Codec context associated with this stream.
Definition: avformat.h:718
int num
numerator
Definition: rational.h:44
int size
Definition: avcodec.h:974
void av_log(void *avcl, int level, const char *fmt,...) av_printf_format(3
Send the specified message to the log if the level is less than or equal to the current av_log_level...
enum AVPixelFormat pix_fmt
Pixel format, see AV_PIX_FMT_xxx.
Definition: avcodec.h:1254
int show_region
set by a private option.
Definition: x11grab.c:78
static int x11grab_read_packet(AVFormatContext *s1, AVPacket *pkt)
Grab a frame from x11 (public device demuxer API).
Definition: x11grab.c:449
#define _(x)
int height
Height of the grab frame.
Definition: x11grab.c:67
AVRational time_base
This is the fundamental unit of time (in seconds) in terms of which frame timestamps are represented...
Definition: avcodec.h:1175
Format I/O context.
Definition: avformat.h:922
uint8_t
XShmSegmentInfo shminfo
When using XShm, keeps track of XShm infos.
Definition: x11grab.c:75
static void x11grab_region_win_init(struct x11grab *s)
Initialize grabbing region window.
Definition: x11grab.c:116
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:38
#define b
Definition: input.c:52
#define OFFSET(x)
Definition: x11grab.c:585
static double av_q2d(AVRational a)
Convert rational to double.
Definition: rational.h:69
#define DEC
Definition: x11grab.c:586
#define AVFMT_NOFILE
Demuxer will use avio_open, no opened file should be provided by the caller.
Definition: avformat.h:409
AVStream * avformat_new_stream(AVFormatContext *s, const AVCodec *c)
Add a new stream to a media file.
Definition: utils.c:2518
AVRational time_base
Time base.
Definition: x11grab.c:63
#define r
Definition: input.c:51
static const AVOption options[]
Definition: x11grab.c:587
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition: log.h:123
void av_free(void *ptr)
Free a memory block which has been allocated with av_malloc(z)() or av_realloc(). ...
Definition: mem.c:186
void * priv_data
Format private data.
Definition: avformat.h:950
char filename[1024]
input or output filename
Definition: avformat.h:998
#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:150
g
Definition: yuv2rgb.c:535
#define AV_LOG_DEBUG
Stuff which is only useful for libav* developers.
Definition: log.h:144
Definition: graph2dot.c:49
static const AVClass x11_class
Definition: x11grab.c:598
int draw_mouse
Set by a private option.
Definition: x11grab.c:76
static int x11grab_read_close(AVFormatContext *s1)
Close x11 frame grabber (public device demuxer API).
Definition: x11grab.c:559
AVPixelFormat
Pixel format.
Definition: pixfmt.h:63
int av_parse_video_rate(AVRational *rate, const char *str)
Parse str and store the detected values in *rate.
Definition: parseutils.c:122
XImage * image
X11 image holding the grab.
Definition: x11grab.c:73
char * video_size
String describing video size, set by a private option.
Definition: x11grab.c:66
int follow_mouse
Set by a private option.
Definition: x11grab.c:77
int64_t av_gettime(void)
Get the current time in microseconds.
Definition: time.c:37
static int xget_zpixmap(Display *dpy, Drawable d, XImage *image, int x, int y)
Read new data in the image structure.
Definition: x11grab.c:405
int bit_rate
the average bitrate
Definition: avcodec.h:1114
int width
picture width / height.
Definition: avcodec.h:1224
static void paint_mouse_pointer(XImage *image, struct x11grab *s)
Paint a mouse pointer in an X11 image.
Definition: x11grab.c:337
int use_shm
!0 when using XShm extension
Definition: x11grab.c:74
LIBAVUTIL_VERSION_INT
Definition: eval.c:55
int av_parse_video_size(int *width_ptr, int *height_ptr, const char *str)
Parse str and put in width_ptr and height_ptr the detected values.
Definition: parseutils.c:95
8 bit with PIX_FMT_RGB32 palette
Definition: pixfmt.h:76
Stream structure.
Definition: avformat.h:699
NULL
Definition: eval.c:55
static SDL_Surface * screen
Definition: avplay.c:280
static int width
Definition: utils.c:156
#define AV_LOG_INFO
Standard information.
Definition: log.h:134
int frame_size
Size in bytes of a grabbed frame.
Definition: x11grab.c:62
enum AVMediaType codec_type
Definition: avcodec.h:1058
enum AVCodecID codec_id
Definition: avcodec.h:1067
static void x11grab_draw_region_win(struct x11grab *s)
Draw grabbing region window.
Definition: x11grab.c:91
#define AV_PIX_FMT_RGB555
Definition: pixfmt.h:231
char * framerate
Set by a private option.
Definition: x11grab.c:79
packed RGB 8:8:8, 24bpp, BGRBGR...
Definition: pixfmt.h:68
Describe the class of an AVClass context structure.
Definition: log.h:33
rational number numerator/denominator
Definition: rational.h:43
int x_off
Horizontal top-left corner coordinate.
Definition: x11grab.c:69
uint8_t * data
Definition: avcodec.h:973
Window region_win
This is used by show_region option.
Definition: x11grab.c:81
int height
Definition: gxfenc.c:72
uint8_t pi<< 24) CONV_FUNC_GROUP(AV_SAMPLE_FMT_FLT, float, AV_SAMPLE_FMT_U8, uint8_t,(*(constuint8_t *) pi-0x80)*(1.0f/(1<< 7))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_DBL, double, AV_SAMPLE_FMT_U8, uint8_t,(*(constuint8_t *) pi-0x80)*(1.0/(1<< 7))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_U8, uint8_t, AV_SAMPLE_FMT_S16, int16_t,(*(constint16_t *) pi >>8)+0x80) CONV_FUNC_GROUP(AV_SAMPLE_FMT_FLT, float, AV_SAMPLE_FMT_S16, int16_t,*(constint16_t *) pi *(1.0f/(1<< 15))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_DBL, double, AV_SAMPLE_FMT_S16, int16_t,*(constint16_t *) pi *(1.0/(1<< 15))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_U8, uint8_t, AV_SAMPLE_FMT_S32, int32_t,(*(constint32_t *) pi >>24)+0x80) CONV_FUNC_GROUP(AV_SAMPLE_FMT_FLT, float, AV_SAMPLE_FMT_S32, int32_t,*(constint32_t *) pi *(1.0f/(1U<< 31))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_DBL, double, AV_SAMPLE_FMT_S32, int32_t,*(constint32_t *) pi *(1.0/(1U<< 31))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_U8, uint8_t, AV_SAMPLE_FMT_FLT, float, av_clip_uint8(lrintf(*(constfloat *) pi *(1<< 7))+0x80)) CONV_FUNC_GROUP(AV_SAMPLE_FMT_S16, int16_t, AV_SAMPLE_FMT_FLT, float, av_clip_int16(lrintf(*(constfloat *) pi *(1<< 15)))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_S32, int32_t, AV_SAMPLE_FMT_FLT, float, av_clipl_int32(llrintf(*(constfloat *) pi *(1U<< 31)))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_U8, uint8_t, AV_SAMPLE_FMT_DBL, double, av_clip_uint8(lrint(*(constdouble *) pi *(1<< 7))+0x80)) CONV_FUNC_GROUP(AV_SAMPLE_FMT_S16, int16_t, AV_SAMPLE_FMT_DBL, double, av_clip_int16(lrint(*(constdouble *) pi *(1<< 15)))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_S32, int32_t, AV_SAMPLE_FMT_DBL, double, av_clipl_int32(llrint(*(constdouble *) pi *(1U<< 31))))#defineSET_CONV_FUNC_GROUP(ofmt, ifmt) staticvoidset_generic_function(AudioConvert *ac){}voidff_audio_convert_free(AudioConvert **ac){if(!*ac) return;ff_dither_free(&(*ac) ->dc);av_freep(ac);}AudioConvert *ff_audio_convert_alloc(AVAudioResampleContext *avr, enumAVSampleFormatout_fmt, enumAVSampleFormatin_fmt, intchannels, intsample_rate, intapply_map){AudioConvert *ac;intin_planar, out_planar;ac=av_mallocz(sizeof(*ac));if(!ac) returnNULL;ac->avr=avr;ac->out_fmt=out_fmt;ac->in_fmt=in_fmt;ac->channels=channels;ac->apply_map=apply_map;if(avr->dither_method!=AV_RESAMPLE_DITHER_NONE &&av_get_packed_sample_fmt(out_fmt)==AV_SAMPLE_FMT_S16 &&av_get_bytes_per_sample(in_fmt)>2){ac->dc=ff_dither_alloc(avr, out_fmt, in_fmt, channels, sample_rate, apply_map);if(!ac->dc){av_free(ac);returnNULL;}returnac;}in_planar=ff_sample_fmt_is_planar(in_fmt, channels);out_planar=ff_sample_fmt_is_planar(out_fmt, channels);if(in_planar==out_planar){ac->func_type=CONV_FUNC_TYPE_FLAT;ac->planes=in_planar?ac->channels:1;}elseif(in_planar) ac->func_type=CONV_FUNC_TYPE_INTERLEAVE;elseac->func_type=CONV_FUNC_TYPE_DEINTERLEAVE;set_generic_function(ac);if(ARCH_AARCH64) ff_audio_convert_init_aarch64(ac);if(ARCH_ARM) ff_audio_convert_init_arm(ac);if(ARCH_X86) ff_audio_convert_init_x86(ac);returnac;}intff_audio_convert(AudioConvert *ac, AudioData *out, AudioData *in){intuse_generic=1;intlen=in->nb_samples;intp;if(ac->dc){av_dlog(ac->avr,"%dsamples-audio_convert:%sto%s(dithered)\n", len, av_get_sample_fmt_name(ac->in_fmt), av_get_sample_fmt_name(ac->out_fmt));returnff_convert_dither(ac-> out
#define AV_PIX_FMT_RGB565
Definition: pixfmt.h:230
int64_t time_frame
Current time.
Definition: x11grab.c:64
X11 Device Demuxer context.
Definition: x11grab.c:60
void av_init_packet(AVPacket *pkt)
Initialize optional fields of a packet with default values.
Definition: avpacket.c:47
int den
denominator
Definition: rational.h:45
#define AV_PIX_FMT_RGB32
Definition: pixfmt.h:222
int width
Width of the grab frame.
Definition: x11grab.c:68
av_default_item_name
Return the context name.
Definition: dnxhdenc.c:52
This structure stores compressed data.
Definition: avcodec.h:950
int64_t pts
Presentation timestamp in AVStream->time_base units; the time at which the decompressed packet will b...
Definition: avcodec.h:966
#define FFMIN(a, b)
Definition: common.h:57
Display * dpy
X11 display from which x11grab grabs frames.
Definition: x11grab.c:72