]> git.sesse.net Git - ffmpeg/blob - libavdevice/x11grab.c
Merge commit '84f226a3bcd8b39801a4c9051c033ab7d61aaf76'
[ffmpeg] / libavdevice / x11grab.c
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 "avdevice.h"
56
57 #include "libavutil/log.h"
58 #include "libavutil/opt.h"
59 #include "libavutil/parseutils.h"
60 #include "libavutil/time.h"
61
62 #include "libavformat/internal.h"
63
64 /** X11 device demuxer context */
65 typedef struct X11GrabContext {
66     const AVClass *class;    /**< Class for private options. */
67     int frame_size;          /**< Size in bytes of a grabbed frame */
68     AVRational time_base;    /**< Time base */
69     int64_t time_frame;      /**< Current time */
70
71     int width;               /**< Width of the grab frame */
72     int height;              /**< Height of the grab frame */
73     int x_off;               /**< Horizontal top-left corner coordinate */
74     int y_off;               /**< Vertical top-left corner coordinate */
75
76     Display *dpy;            /**< X11 display from which x11grab grabs frames */
77     XImage *image;           /**< X11 image holding the grab */
78     int use_shm;             /**< !0 when using XShm extension */
79     XShmSegmentInfo shminfo; /**< When using XShm, keeps track of XShm infos */
80     int draw_mouse;          /**< Set by a private option. */
81     int follow_mouse;        /**< Set by a private option. */
82     int show_region;         /**< set by a private option. */
83     AVRational framerate;    /**< Set by a private option. */
84     int palette_changed;
85     uint32_t palette[256];
86
87     Cursor c;
88     Window region_win;       /**< This is used by show_region option. */
89 } X11GrabContext;
90
91 #define REGION_WIN_BORDER 3
92
93 /**
94  * Draw grabbing region window
95  *
96  * @param s x11grab context
97  */
98 static void x11grab_draw_region_win(X11GrabContext *s)
99 {
100     Display *dpy = s->dpy;
101     Window win   = s->region_win;
102     int screen = DefaultScreen(dpy);
103     GC gc = XCreateGC(dpy, win, 0, 0);
104
105     XSetForeground(dpy, gc, WhitePixel(dpy, screen));
106     XSetBackground(dpy, gc, BlackPixel(dpy, screen));
107     XSetLineAttributes(dpy, gc, REGION_WIN_BORDER, LineDoubleDash, 0, 0);
108     XDrawRectangle(dpy, win, gc, 1, 1,
109                    (s->width  + REGION_WIN_BORDER * 2) - 1 * 2 - 1,
110                    (s->height + REGION_WIN_BORDER * 2) - 1 * 2 - 1);
111     XFreeGC(dpy, gc);
112 }
113
114 /**
115  * Initialize grabbing region window
116  *
117  * @param s x11grab context
118  */
119 static void x11grab_region_win_init(X11GrabContext *s)
120 {
121     Display *dpy = s->dpy;
122     XRectangle rect;
123     XSetWindowAttributes attribs = { .override_redirect = True };
124     int screen = DefaultScreen(dpy);
125
126     s->region_win = XCreateWindow(dpy, RootWindow(dpy, screen),
127                                   s->x_off  - REGION_WIN_BORDER,
128                                   s->y_off  - REGION_WIN_BORDER,
129                                   s->width  + REGION_WIN_BORDER * 2,
130                                   s->height + REGION_WIN_BORDER * 2,
131                                   0, CopyFromParent,
132                                   InputOutput, CopyFromParent,
133                                   CWOverrideRedirect, &attribs);
134     rect.x      = 0;
135     rect.y      = 0;
136     rect.width  = s->width;
137     rect.height = s->height;
138     XShapeCombineRectangles(dpy, s->region_win,
139                             ShapeBounding, REGION_WIN_BORDER, REGION_WIN_BORDER,
140                             &rect, 1, ShapeSubtract, 0);
141     XMapWindow(dpy, s->region_win);
142     XSelectInput(dpy, s->region_win, ExposureMask | StructureNotifyMask);
143     x11grab_draw_region_win(s);
144 }
145
146 static int setup_shm(AVFormatContext *s, Display *dpy, XImage **image)
147 {
148     X11GrabContext *g = s->priv_data;
149     int scr           = XDefaultScreen(dpy);
150     XImage *img       = XShmCreateImage(dpy, DefaultVisual(dpy, scr),
151                                         DefaultDepth(dpy, scr), ZPixmap, NULL,
152                                         &g->shminfo, g->width, g->height);
153
154     g->shminfo.shmid = shmget(IPC_PRIVATE, img->bytes_per_line * img->height,
155                               IPC_CREAT | 0777);
156
157     if (g->shminfo.shmid == -1) {
158         av_log(s, AV_LOG_ERROR, "Cannot get shared memory!\n");
159         return AVERROR(ENOMEM);
160     }
161
162     g->shminfo.shmaddr  = img->data = shmat(g->shminfo.shmid, 0, 0);
163     g->shminfo.readOnly = False;
164
165     if (!XShmAttach(dpy, &g->shminfo)) {
166         av_log(s, AV_LOG_ERROR, "Failed to attach shared memory!\n");
167         /* needs some better error subroutine :) */
168         return AVERROR(EIO);
169     }
170
171     *image = img;
172     return 0;
173 }
174
175 static int setup_mouse(Display *dpy, int screen)
176 {
177     int ev_ret, ev_err;
178
179     if (XFixesQueryExtension(dpy, &ev_ret, &ev_err)) {
180         Window root = RootWindow(dpy, screen);
181         XFixesSelectCursorInput(dpy, root, XFixesDisplayCursorNotifyMask);
182         return 0;
183     }
184
185     return AVERROR(ENOSYS);
186 }
187
188 static int pixfmt_from_image(AVFormatContext *s, XImage *image, int *pix_fmt)
189 {
190     av_log(s, AV_LOG_DEBUG,
191            "Image r 0x%.6lx g 0x%.6lx b 0x%.6lx and depth %i\n",
192            image->red_mask,
193            image->green_mask,
194            image->blue_mask,
195            image->bits_per_pixel);
196
197     *pix_fmt = AV_PIX_FMT_NONE;
198
199     switch (image->bits_per_pixel) {
200     case 8:
201         *pix_fmt =  AV_PIX_FMT_PAL8;
202         break;
203     case 16:
204         if (image->red_mask   == 0xf800 &&
205             image->green_mask == 0x07e0 &&
206             image->blue_mask  == 0x001f) {
207             *pix_fmt = AV_PIX_FMT_RGB565;
208         } else if (image->red_mask   == 0x7c00 &&
209                    image->green_mask == 0x03e0 &&
210                    image->blue_mask  == 0x001f) {
211             *pix_fmt = AV_PIX_FMT_RGB555;
212         }
213         break;
214     case 24:
215         if (image->red_mask   == 0xff0000 &&
216             image->green_mask == 0x00ff00 &&
217             image->blue_mask  == 0x0000ff) {
218             *pix_fmt = AV_PIX_FMT_BGR24;
219         } else if (image->red_mask   == 0x0000ff &&
220                    image->green_mask == 0x00ff00 &&
221                    image->blue_mask  == 0xff0000) {
222             *pix_fmt = AV_PIX_FMT_RGB24;
223         }
224         break;
225     case 32:
226         if (image->red_mask   == 0xff0000 &&
227             image->green_mask == 0x00ff00 &&
228             image->blue_mask  == 0x0000ff ) {
229             *pix_fmt = AV_PIX_FMT_0RGB32;
230         }
231         break;
232     }
233     if (*pix_fmt == AV_PIX_FMT_NONE) {
234         av_log(s, AV_LOG_ERROR,
235                "XImages with RGB mask 0x%.6lx 0x%.6lx 0x%.6lx and depth %i "
236                "are currently not supported.\n",
237                image->red_mask,
238                image->green_mask,
239                image->blue_mask,
240                image->bits_per_pixel);
241
242         return AVERROR_PATCHWELCOME;
243     }
244
245     return 0;
246 }
247
248 /**
249  * Initialize the x11 grab device demuxer (public device demuxer API).
250  *
251  * @param s1 Context from avformat core
252  * @return <ul>
253  *          <li>AVERROR(ENOMEM) no memory left</li>
254  *          <li>AVERROR(EIO) other failure case</li>
255  *          <li>0 success</li>
256  *         </ul>
257  */
258 static int x11grab_read_header(AVFormatContext *s1)
259 {
260     X11GrabContext *x11grab = s1->priv_data;
261     Display *dpy;
262     AVStream *st = NULL;
263     XImage *image;
264     int x_off = 0, y_off = 0, ret = 0, screen, use_shm = 0;
265     char *dpyname, *offset;
266     Colormap color_map;
267     XColor color[256];
268     int i;
269
270     dpyname = av_strdup(s1->filename);
271     if (!dpyname)
272         goto out;
273
274     offset = strchr(dpyname, '+');
275     if (offset) {
276         sscanf(offset, "%d,%d", &x_off, &y_off);
277         if (strstr(offset, "nomouse")) {
278             av_log(s1, AV_LOG_WARNING,
279                    "'nomouse' specification in argument is deprecated: "
280                    "use 'draw_mouse' option with value 0 instead\n");
281             x11grab->draw_mouse = 0;
282         }
283         *offset = 0;
284     }
285
286     av_log(s1, AV_LOG_INFO,
287            "device: %s -> display: %s x: %d y: %d width: %d height: %d\n",
288            s1->filename, dpyname, x_off, y_off, x11grab->width, x11grab->height);
289
290     dpy = XOpenDisplay(dpyname);
291     av_freep(&dpyname);
292     if (!dpy) {
293         av_log(s1, AV_LOG_ERROR, "Could not open X display.\n");
294         ret = AVERROR(EIO);
295         goto out;
296     }
297
298     st = avformat_new_stream(s1, NULL);
299     if (!st) {
300         ret = AVERROR(ENOMEM);
301         goto out;
302     }
303     avpriv_set_pts_info(st, 64, 1, 1000000); /* 64 bits pts in us */
304
305     screen = DefaultScreen(dpy);
306
307     if (x11grab->follow_mouse) {
308         int screen_w, screen_h;
309         Window w;
310
311         screen_w = DisplayWidth(dpy, screen);
312         screen_h = DisplayHeight(dpy, screen);
313         XQueryPointer(dpy, RootWindow(dpy, screen), &w, &w, &x_off, &y_off,
314                       &ret, &ret, &ret);
315         x_off -= x11grab->width / 2;
316         y_off -= x11grab->height / 2;
317         x_off  = FFMIN(FFMAX(x_off, 0), screen_w - x11grab->width);
318         y_off  = FFMIN(FFMAX(y_off, 0), screen_h - x11grab->height);
319         av_log(s1, AV_LOG_INFO,
320                "followmouse is enabled, resetting grabbing region to x: %d y: %d\n",
321                x_off, y_off);
322     }
323
324     if (x11grab->use_shm) {
325         use_shm = XShmQueryExtension(dpy);
326         av_log(s1, AV_LOG_INFO,
327                "shared memory extension %sfound\n", use_shm ? "" : "not ");
328     }
329
330     if (use_shm && setup_shm(s1, dpy, &image) < 0) {
331         av_log(s1, AV_LOG_WARNING, "Falling back to XGetImage\n");
332         use_shm = 0;
333     }
334
335     if (!use_shm) {
336         image = XGetImage(dpy, RootWindow(dpy, screen),
337                           x_off, y_off,
338                           x11grab->width, x11grab->height,
339                           AllPlanes, ZPixmap);
340     }
341
342     if (x11grab->draw_mouse && setup_mouse(dpy, screen) < 0) {
343         av_log(s1, AV_LOG_WARNING,
344                "XFixes not available, cannot draw the mouse cursor\n");
345         x11grab->draw_mouse = 0;
346     }
347
348     x11grab->frame_size = x11grab->width * x11grab->height * image->bits_per_pixel / 8;
349     x11grab->dpy        = dpy;
350     x11grab->time_base  = av_inv_q(x11grab->framerate);
351     x11grab->time_frame = av_gettime() / av_q2d(x11grab->time_base);
352     x11grab->x_off      = x_off;
353     x11grab->y_off      = y_off;
354     x11grab->image      = image;
355     x11grab->use_shm    = use_shm;
356
357     ret = pixfmt_from_image(s1, image, &st->codec->pix_fmt);
358     if (ret < 0)
359         goto out;
360
361     if (st->codec->pix_fmt == AV_PIX_FMT_PAL8) {
362         color_map = DefaultColormap(dpy, screen);
363         for (i = 0; i < 256; ++i)
364             color[i].pixel = i;
365         XQueryColors(dpy, color_map, color, 256);
366         for (i = 0; i < 256; ++i)
367             x11grab->palette[i] = (color[i].red   & 0xFF00) << 8 |
368                                   (color[i].green & 0xFF00)      |
369                                   (color[i].blue  & 0xFF00) >> 8;
370         x11grab->palette_changed = 1;
371     }
372
373
374     st->codec->codec_type = AVMEDIA_TYPE_VIDEO;
375     st->codec->codec_id   = AV_CODEC_ID_RAWVIDEO;
376     st->codec->width      = x11grab->width;
377     st->codec->height     = x11grab->height;
378     st->codec->time_base  = x11grab->time_base;
379     st->codec->bit_rate   = x11grab->frame_size * 1 / av_q2d(x11grab->time_base) * 8;
380
381 out:
382     av_free(dpyname);
383     return ret;
384 }
385
386 /**
387  * Paint a mouse pointer in an X11 image.
388  *
389  * @param image image to paint the mouse pointer to
390  * @param s context used to retrieve original grabbing rectangle
391  *          coordinates
392  */
393 static void paint_mouse_pointer(XImage *image, AVFormatContext *s1)
394 {
395     X11GrabContext *s = s1->priv_data;
396     int x_off    = s->x_off;
397     int y_off    = s->y_off;
398     int width    = s->width;
399     int height   = s->height;
400     Display *dpy = s->dpy;
401     XFixesCursorImage *xcim;
402     int x, y;
403     int line, column;
404     int to_line, to_column;
405     int pixstride = image->bits_per_pixel >> 3;
406     /* Warning: in its insanity, xlib provides unsigned image data through a
407      * char* pointer, so we have to make it uint8_t to make things not break.
408      * Anyone who performs further investigation of the xlib API likely risks
409      * permanent brain damage. */
410     uint8_t *pix = image->data;
411     Window root;
412     XSetWindowAttributes attr;
413
414     /* Code doesn't currently support 16-bit or PAL8 */
415     if (image->bits_per_pixel != 24 && image->bits_per_pixel != 32)
416         return;
417
418     if (!s->c)
419         s->c = XCreateFontCursor(dpy, XC_left_ptr);
420     root = DefaultRootWindow(dpy);
421     attr.cursor = s->c;
422     XChangeWindowAttributes(dpy, root, CWCursor, &attr);
423
424     xcim = XFixesGetCursorImage(dpy);
425     if (!xcim) {
426         av_log(s1, AV_LOG_WARNING,
427                "XFixesGetCursorImage failed\n");
428         return;
429     }
430
431     x = xcim->x - xcim->xhot;
432     y = xcim->y - xcim->yhot;
433
434     to_line   = FFMIN((y + xcim->height), (height + y_off));
435     to_column = FFMIN((x + xcim->width),  (width  + x_off));
436
437     for (line = FFMAX(y, y_off); line < to_line; line++) {
438         for (column = FFMAX(x, x_off); column < to_column; column++) {
439             int xcim_addr  = (line  - y)     * xcim->width + column - x;
440             int image_addr = ((line - y_off) * width       + column - x_off) * pixstride;
441             int r          = (uint8_t)(xcim->pixels[xcim_addr] >>  0);
442             int g          = (uint8_t)(xcim->pixels[xcim_addr] >>  8);
443             int b          = (uint8_t)(xcim->pixels[xcim_addr] >> 16);
444             int a          = (uint8_t)(xcim->pixels[xcim_addr] >> 24);
445
446             if (a == 255) {
447                 pix[image_addr + 0] = r;
448                 pix[image_addr + 1] = g;
449                 pix[image_addr + 2] = b;
450             } else if (a) {
451                 /* pixel values from XFixesGetCursorImage come premultiplied by alpha */
452                 pix[image_addr + 0] = r + (pix[image_addr + 0] * (255 - a) + 255 / 2) / 255;
453                 pix[image_addr + 1] = g + (pix[image_addr + 1] * (255 - a) + 255 / 2) / 255;
454                 pix[image_addr + 2] = b + (pix[image_addr + 2] * (255 - a) + 255 / 2) / 255;
455             }
456         }
457     }
458
459     XFree(xcim);
460     xcim = NULL;
461 }
462
463 /**
464  * Read new data in the image structure.
465  *
466  * @param dpy X11 display to grab from
467  * @param d
468  * @param image Image where the grab will be put
469  * @param x Top-Left grabbing rectangle horizontal coordinate
470  * @param y Top-Left grabbing rectangle vertical coordinate
471  * @return 0 if error, !0 if successful
472  */
473 static int xget_zpixmap(Display *dpy, Drawable d, XImage *image, int x, int y)
474 {
475     xGetImageReply rep;
476     xGetImageReq *req;
477     long nbytes;
478
479     if (!image)
480         return 0;
481
482     LockDisplay(dpy);
483     GetReq(GetImage, req);
484
485     /* First set up the standard stuff in the request */
486     req->drawable  = d;
487     req->x         = x;
488     req->y         = y;
489     req->width     = image->width;
490     req->height    = image->height;
491     req->planeMask = (unsigned int)AllPlanes;
492     req->format    = ZPixmap;
493
494     if (!_XReply(dpy, (xReply *)&rep, 0, xFalse) || !rep.length) {
495         UnlockDisplay(dpy);
496         SyncHandle();
497         return 0;
498     }
499
500     nbytes = (long)rep.length << 2;
501     _XReadPad(dpy, image->data, nbytes);
502
503     UnlockDisplay(dpy);
504     SyncHandle();
505     return 1;
506 }
507
508 /**
509  * Grab a frame from x11 (public device demuxer API).
510  *
511  * @param s1 Context from avformat core
512  * @param pkt Packet holding the brabbed frame
513  * @return frame size in bytes
514  */
515 static int x11grab_read_packet(AVFormatContext *s1, AVPacket *pkt)
516 {
517     X11GrabContext *s = s1->priv_data;
518     Display *dpy      = s->dpy;
519     XImage *image     = s->image;
520     int x_off         = s->x_off;
521     int y_off         = s->y_off;
522     int follow_mouse  = s->follow_mouse;
523     int screen, pointer_x, pointer_y, _, same_screen = 1;
524     Window w, root;
525     int64_t curtime, delay;
526     struct timespec ts;
527
528     /* Calculate the time of the next frame */
529     s->time_frame += INT64_C(1000000);
530
531     /* wait based on the frame rate */
532     for (;;) {
533         curtime = av_gettime();
534         delay   = s->time_frame * av_q2d(s->time_base) - curtime;
535         if (delay <= 0) {
536             if (delay < INT64_C(-1000000) * av_q2d(s->time_base))
537                 s->time_frame += INT64_C(1000000);
538             break;
539         }
540         ts.tv_sec  = delay / 1000000;
541         ts.tv_nsec = (delay % 1000000) * 1000;
542         nanosleep(&ts, NULL);
543     }
544
545     av_init_packet(pkt);
546     pkt->data = image->data;
547     pkt->size = s->frame_size;
548     pkt->pts  = curtime;
549     if (s->palette_changed) {
550         uint8_t *pal = av_packet_new_side_data(pkt, AV_PKT_DATA_PALETTE,
551                                                AVPALETTE_SIZE);
552         if (!pal) {
553             av_log(s, AV_LOG_ERROR, "Cannot append palette to packet\n");
554         } else {
555             memcpy(pal, s->palette, AVPALETTE_SIZE);
556             s->palette_changed = 0;
557         }
558     }
559
560     screen = DefaultScreen(dpy);
561     root   = RootWindow(dpy, screen);
562
563     if (follow_mouse || s->draw_mouse)
564         same_screen = XQueryPointer(dpy, root, &w, &w,
565                                     &pointer_x, &pointer_y, &_, &_, &_);
566
567     if (follow_mouse && same_screen) {
568         int screen_w, screen_h;
569
570         screen_w = DisplayWidth(dpy, screen);
571         screen_h = DisplayHeight(dpy, screen);
572         if (follow_mouse == -1) {
573             // follow the mouse, put it at center of grabbing region
574             x_off += pointer_x - s->width / 2 - x_off;
575             y_off += pointer_y - s->height / 2 - y_off;
576         } else {
577             // follow the mouse, but only move the grabbing region when mouse
578             // reaches within certain pixels to the edge.
579             if (pointer_x > x_off + s->width - follow_mouse)
580                 x_off += pointer_x - (x_off + s->width - follow_mouse);
581             else if (pointer_x < x_off + follow_mouse)
582                 x_off -= (x_off + follow_mouse) - pointer_x;
583             if (pointer_y > y_off + s->height - follow_mouse)
584                 y_off += pointer_y - (y_off + s->height - follow_mouse);
585             else if (pointer_y < y_off + follow_mouse)
586                 y_off -= (y_off + follow_mouse) - pointer_y;
587         }
588         // adjust grabbing region position if it goes out of screen.
589         s->x_off = x_off = FFMIN(FFMAX(x_off, 0), screen_w - s->width);
590         s->y_off = y_off = FFMIN(FFMAX(y_off, 0), screen_h - s->height);
591
592         if (s->show_region && s->region_win)
593             XMoveWindow(dpy, s->region_win,
594                         s->x_off - REGION_WIN_BORDER,
595                         s->y_off - REGION_WIN_BORDER);
596     }
597
598     if (s->show_region && same_screen) {
599         if (s->region_win) {
600             XEvent evt = { .type = NoEventMask };
601             // Clean up the events, and do the initial draw or redraw.
602             while (XCheckMaskEvent(dpy, ExposureMask | StructureNotifyMask,
603                                    &evt))
604                 ;
605             if (evt.type)
606                 x11grab_draw_region_win(s);
607         } else {
608             x11grab_region_win_init(s);
609         }
610     }
611
612     if (s->use_shm) {
613         if (!XShmGetImage(dpy, root, image, x_off, y_off, AllPlanes))
614             av_log(s1, AV_LOG_INFO, "XShmGetImage() failed\n");
615     } else {
616         if (!xget_zpixmap(dpy, root, image, x_off, y_off))
617             av_log(s1, AV_LOG_INFO, "XGetZPixmap() failed\n");
618     }
619
620     if (s->draw_mouse && same_screen)
621         paint_mouse_pointer(image, s1);
622
623     return s->frame_size;
624 }
625
626 /**
627  * Close x11 frame grabber (public device demuxer API).
628  *
629  * @param s1 Context from avformat core
630  * @return 0 success, !0 failure
631  */
632 static int x11grab_read_close(AVFormatContext *s1)
633 {
634     X11GrabContext *x11grab = s1->priv_data;
635
636     /* Detach cleanly from shared mem */
637     if (x11grab->use_shm) {
638         XShmDetach(x11grab->dpy, &x11grab->shminfo);
639         shmdt(x11grab->shminfo.shmaddr);
640         shmctl(x11grab->shminfo.shmid, IPC_RMID, NULL);
641     }
642
643     /* Destroy X11 image */
644     if (x11grab->image) {
645         XDestroyImage(x11grab->image);
646         x11grab->image = NULL;
647     }
648
649     if (x11grab->region_win)
650         XDestroyWindow(x11grab->dpy, x11grab->region_win);
651
652     /* Free X11 display */
653     XCloseDisplay(x11grab->dpy);
654     return 0;
655 }
656
657 #define OFFSET(x) offsetof(X11GrabContext, x)
658 #define DEC AV_OPT_FLAG_DECODING_PARAM
659 static const AVOption options[] = {
660     { "grab_x", "Initial x coordinate.", OFFSET(x_off), AV_OPT_TYPE_INT, { .i64 = 0 }, 0, INT_MAX, DEC },
661     { "grab_y", "Initial y coordinate.", OFFSET(y_off), AV_OPT_TYPE_INT, { .i64 = 0 }, 0, INT_MAX, DEC },
662     { "draw_mouse", "draw the mouse pointer", OFFSET(draw_mouse), AV_OPT_TYPE_INT, {.i64 = 1}, 0, 1, DEC },
663
664     { "follow_mouse", "move the grabbing region when the mouse pointer reaches within specified amount of pixels to the edge of region",
665       OFFSET(follow_mouse), AV_OPT_TYPE_INT, {.i64 = 0}, -1, INT_MAX, DEC, "follow_mouse" },
666     { "centered",     "keep the mouse pointer at the center of grabbing region when following",
667       0, AV_OPT_TYPE_CONST, {.i64 = -1}, INT_MIN, INT_MAX, DEC, "follow_mouse" },
668
669     { "framerate",  "set video frame rate",      OFFSET(framerate),   AV_OPT_TYPE_VIDEO_RATE, {.str = "ntsc"}, 0, 0, DEC },
670     { "show_region", "show the grabbing region", OFFSET(show_region), AV_OPT_TYPE_INT,        {.i64 = 0}, 0, 1, DEC },
671     { "video_size",  "set video frame size",     OFFSET(width),       AV_OPT_TYPE_IMAGE_SIZE, {.str = "vga"}, 0, 0, DEC },
672     { "use_shm",     "use MIT-SHM extension",    OFFSET(use_shm),     AV_OPT_TYPE_INT,        {.i64 = 1}, 0, 1, DEC },
673     { NULL },
674 };
675
676 static const AVClass x11_class = {
677     .class_name = "X11grab indev",
678     .item_name  = av_default_item_name,
679     .option     = options,
680     .version    = LIBAVUTIL_VERSION_INT,
681     .category   = AV_CLASS_CATEGORY_DEVICE_VIDEO_INPUT,
682 };
683
684 /** x11 grabber device demuxer declaration */
685 AVInputFormat ff_x11grab_demuxer = {
686     .name           = "x11grab",
687     .long_name      = NULL_IF_CONFIG_SMALL("X11grab"),
688     .priv_data_size = sizeof(X11GrabContext),
689     .read_header    = x11grab_read_header,
690     .read_packet    = x11grab_read_packet,
691     .read_close     = x11grab_read_close,
692     .flags          = AVFMT_NOFILE,
693     .priv_class     = &x11_class,
694 };