]> git.sesse.net Git - ffmpeg/blobdiff - libavdevice/x11grab.c
Merge commit '65e78a2e4b111627c0ebdf2c9baec95e5e21560d'
[ffmpeg] / libavdevice / x11grab.c
index 03fd567db89c8f82c9118fa623653c9e770ac24b..120f754b1089632cffdfa10c934180515dae24b0 100644 (file)
@@ -1,9 +1,9 @@
 /*
  * X11 video grab interface
  *
- * This file is part of Libav.
+ * This file is part of FFmpeg.
  *
- * Libav integration:
+ * FFmpeg integration:
  * Copyright (C) 2006 Clemens Fruhwirth <clemens@endorphin.org>
  *                    Edouard Gomez <ed.gomez@free.fr>
  *
  * Copyright (C) 1997-1998 Rasca, Berlin
  *               2003-2004 Karl H. Beckers, Frankfurt
  *
- * Libav is free software; you can redistribute it and/or modify
+ * FFmpeg is free software; you can redistribute it and/or modify
  * it under the terms of the GNU General Public License as published by
  * the Free Software Foundation; either version 2 of the License, or
  * (at your option) any later version.
  *
- * Libav is distributed in the hope that it will be useful,
+ * FFmpeg is distributed in the hope that it will be useful,
  * but WITHOUT ANY WARRANTY; without even the implied warranty of
  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  * GNU General Public License for more details.
  *
  * You should have received a copy of the GNU General Public License
- * along with Libav; if not, write to the Free Software
+ * along with FFmpeg; if not, write to the Free Software
  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  */
 
@@ -41,6 +41,7 @@
 #include <time.h>
 #include <sys/shm.h>
 
+#include <X11/cursorfont.h>
 #include <X11/X.h>
 #include <X11/Xlib.h>
 #include <X11/Xlibint.h>
 #include <X11/extensions/Xfixes.h>
 #include <X11/extensions/XShm.h>
 
+#include "avdevice.h"
+
 #include "libavutil/log.h"
 #include "libavutil/opt.h"
 #include "libavutil/parseutils.h"
 #include "libavutil/time.h"
 
-#include "libavformat/avformat.h"
 #include "libavformat/internal.h"
 
 /** X11 device demuxer context */
@@ -66,9 +68,8 @@ typedef struct X11GrabContext {
     AVRational time_base;    /**< Time base */
     int64_t time_frame;      /**< Current time */
 
-    char *video_size;        /**< String describing video size, set by a private option. */
-    int height;              /**< Height of the grab frame */
     int width;               /**< Width of the grab frame */
+    int height;              /**< Height of the grab frame */
     int x_off;               /**< Horizontal top-left corner coordinate */
     int y_off;               /**< Vertical top-left corner coordinate */
 
@@ -79,8 +80,11 @@ typedef struct X11GrabContext {
     int draw_mouse;          /**< Set by a private option. */
     int follow_mouse;        /**< Set by a private option. */
     int show_region;         /**< set by a private option. */
-    char *framerate;         /**< Set by a private option. */
+    AVRational framerate;    /**< Set by a private option. */
+    int palette_changed;
+    uint32_t palette[256];
 
+    Cursor c;
     Window region_win;       /**< This is used by show_region option. */
 } X11GrabContext;
 
@@ -204,7 +208,11 @@ static int pixfmt_from_image(AVFormatContext *s, XImage *image, int *pix_fmt)
         }
         break;
     case 32:
-        *pix_fmt = AV_PIX_FMT_RGB32;
+        if (image->red_mask   == 0xff0000 &&
+            image->green_mask == 0x00ff00 &&
+            image->blue_mask  == 0x0000ff ) {
+            *pix_fmt = AV_PIX_FMT_0RGB32;
+        }
         break;
     default:
         av_log(s, AV_LOG_ERROR,
@@ -237,39 +245,34 @@ static int x11grab_read_header(AVFormatContext *s1)
     Display *dpy;
     AVStream *st = NULL;
     XImage *image;
-    int x_off = 0, y_off = 0, ret = 0, screen, use_shm;
-    char *param, *offset;
-    AVRational framerate;
-
-    param = av_strdup(s1->filename);
-    if (!param)
+    int x_off = 0, y_off = 0, ret = 0, screen, use_shm = 0;
+    char *dpyname, *offset;
+    Colormap color_map;
+    XColor color[256];
+    int i;
+
+    dpyname = av_strdup(s1->filename);
+    if (!dpyname)
         goto out;
 
-    offset = strchr(param, '+');
+    offset = strchr(dpyname, '+');
     if (offset) {
         sscanf(offset, "%d,%d", &x_off, &y_off);
-        x11grab->draw_mouse = !strstr(offset, "nomouse");
+        if (strstr(offset, "nomouse")) {
+            av_log(s1, AV_LOG_WARNING,
+                   "'nomouse' specification in argument is deprecated: "
+                   "use 'draw_mouse' option with value 0 instead\n");
+            x11grab->draw_mouse = 0;
+        }
         *offset = 0;
     }
 
-    ret = av_parse_video_size(&x11grab->width, &x11grab->height,
-                              x11grab->video_size);
-    if (ret < 0) {
-        av_log(s1, AV_LOG_ERROR, "Couldn't parse video size.\n");
-        goto out;
-    }
-
-    ret = av_parse_video_rate(&framerate, x11grab->framerate);
-    if (ret < 0) {
-        av_log(s1, AV_LOG_ERROR, "Could not parse framerate: %s.\n",
-               x11grab->framerate);
-        goto out;
-    }
     av_log(s1, AV_LOG_INFO,
            "device: %s -> display: %s x: %d y: %d width: %d height: %d\n",
-           s1->filename, param, x_off, y_off, x11grab->width, x11grab->height);
+           s1->filename, dpyname, x_off, y_off, x11grab->width, x11grab->height);
 
-    dpy = XOpenDisplay(param);
+    dpy = XOpenDisplay(dpyname);
+    av_freep(&dpyname);
     if (!dpy) {
         av_log(s1, AV_LOG_ERROR, "Could not open X display.\n");
         ret = AVERROR(EIO);
@@ -302,9 +305,11 @@ static int x11grab_read_header(AVFormatContext *s1)
                x_off, y_off);
     }
 
-    use_shm = XShmQueryExtension(dpy);
-    av_log(s1, AV_LOG_INFO,
-           "shared memory extension %sfound\n", use_shm ? "" : "not ");
+    if (x11grab->use_shm) {
+        use_shm = XShmQueryExtension(dpy);
+        av_log(s1, AV_LOG_INFO,
+               "shared memory extension %sfound\n", use_shm ? "" : "not ");
+    }
 
     if (use_shm && setup_shm(s1, dpy, &image) < 0) {
         av_log(s1, AV_LOG_WARNING, "Falling back to XGetImage\n");
@@ -320,7 +325,7 @@ static int x11grab_read_header(AVFormatContext *s1)
 
     x11grab->frame_size = x11grab->width * x11grab->height * image->bits_per_pixel / 8;
     x11grab->dpy        = dpy;
-    x11grab->time_base  = (AVRational) { framerate.den, framerate.num };
+    x11grab->time_base  = av_inv_q(x11grab->framerate);
     x11grab->time_frame = av_gettime() / av_q2d(x11grab->time_base);
     x11grab->x_off      = x_off;
     x11grab->y_off      = y_off;
@@ -331,6 +336,19 @@ static int x11grab_read_header(AVFormatContext *s1)
     if (ret < 0)
         goto out;
 
+    if (st->codec->pix_fmt == AV_PIX_FMT_PAL8) {
+        color_map = DefaultColormap(dpy, screen);
+        for (i = 0; i < 256; ++i)
+            color[i].pixel = i;
+        XQueryColors(dpy, color_map, color, 256);
+        for (i = 0; i < 256; ++i)
+            x11grab->palette[i] = (color[i].red   & 0xFF00) << 8 |
+                                  (color[i].green & 0xFF00)      |
+                                  (color[i].blue  & 0xFF00) >> 8;
+        x11grab->palette_changed = 1;
+    }
+
+
     st->codec->codec_type = AVMEDIA_TYPE_VIDEO;
     st->codec->codec_id   = AV_CODEC_ID_RAWVIDEO;
     st->codec->width      = x11grab->width;
@@ -339,7 +357,7 @@ static int x11grab_read_header(AVFormatContext *s1)
     st->codec->bit_rate   = x11grab->frame_size * 1 / av_q2d(x11grab->time_base) * 8;
 
 out:
-    av_free(param);
+    av_free(dpyname);
     return ret;
 }
 
@@ -350,8 +368,9 @@ out:
  * @param s context used to retrieve original grabbing rectangle
  *          coordinates
  */
-static void paint_mouse_pointer(XImage *image, X11GrabContext *s)
+static void paint_mouse_pointer(XImage *image, AVFormatContext *s1)
 {
+    X11GrabContext *s = s1->priv_data;
     int x_off    = s->x_off;
     int y_off    = s->y_off;
     int width    = s->width;
@@ -367,12 +386,26 @@ static void paint_mouse_pointer(XImage *image, X11GrabContext *s)
      * Anyone who performs further investigation of the xlib API likely risks
      * permanent brain damage. */
     uint8_t *pix = image->data;
+    Window w;
+    XSetWindowAttributes attr;
 
     /* Code doesn't currently support 16-bit or PAL8 */
     if (image->bits_per_pixel != 24 && image->bits_per_pixel != 32)
         return;
 
+    if (!s->c)
+        s->c = XCreateFontCursor(dpy, XC_left_ptr);
+    w = DefaultRootWindow(dpy);
+    attr.cursor = s->c;
+    XChangeWindowAttributes(dpy, w, CWCursor, &attr);
+
     xcim = XFixesGetCursorImage(dpy);
+    if (!xcim) {
+        av_log(s1, AV_LOG_WARNING,
+               "XFixes extension not available, impossible to draw cursor\n");
+        s->draw_mouse = 0;
+        return;
+    }
 
     x = xcim->x - xcim->xhot;
     y = xcim->y - xcim->yhot;
@@ -492,6 +525,16 @@ static int x11grab_read_packet(AVFormatContext *s1, AVPacket *pkt)
     pkt->data = image->data;
     pkt->size = s->frame_size;
     pkt->pts  = curtime;
+    if (s->palette_changed) {
+        uint8_t *pal = av_packet_new_side_data(pkt, AV_PKT_DATA_PALETTE,
+                                               AVPALETTE_SIZE);
+        if (!pal) {
+            av_log(s, AV_LOG_ERROR, "Cannot append palette to packet\n");
+        } else {
+            memcpy(pal, s->palette, AVPALETTE_SIZE);
+            s->palette_changed = 0;
+        }
+    }
 
     screen = DefaultScreen(dpy);
     root   = RootWindow(dpy, screen);
@@ -552,7 +595,7 @@ static int x11grab_read_packet(AVFormatContext *s1, AVPacket *pkt)
     }
 
     if (s->draw_mouse)
-        paint_mouse_pointer(image, s);
+        paint_mouse_pointer(image, s1);
 
     return s->frame_size;
 }
@@ -591,13 +634,17 @@ static int x11grab_read_close(AVFormatContext *s1)
 #define OFFSET(x) offsetof(X11GrabContext, x)
 #define DEC AV_OPT_FLAG_DECODING_PARAM
 static const AVOption options[] = {
-    { "video_size", "A string describing frame size, such as 640x480 or hd720.", OFFSET(video_size), AV_OPT_TYPE_STRING, {.str = "vga"}, 0, 0, DEC },
-    { "framerate", "", OFFSET(framerate), AV_OPT_TYPE_STRING, {.str = "ntsc"}, 0, 0, DEC },
-    { "draw_mouse", "Draw the mouse pointer.", OFFSET(draw_mouse), AV_OPT_TYPE_INT, { .i64 = 1 }, 0, 1, DEC },
-    { "follow_mouse", "Move the grabbing region when the mouse pointer reaches within specified amount of pixels to the edge of region.",
-      OFFSET(follow_mouse), AV_OPT_TYPE_INT, { .i64 = 0 }, -1, INT_MAX, DEC, "follow_mouse" },
-    { "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" },
-    { "show_region", "Show the grabbing region.", OFFSET(show_region), AV_OPT_TYPE_INT, { .i64 = 0 }, 0, 1, DEC },
+    { "draw_mouse", "draw the mouse pointer", OFFSET(draw_mouse), AV_OPT_TYPE_INT, {.i64 = 1}, 0, 1, DEC },
+
+    { "follow_mouse", "move the grabbing region when the mouse pointer reaches within specified amount of pixels to the edge of region",
+      OFFSET(follow_mouse), AV_OPT_TYPE_INT, {.i64 = 0}, -1, INT_MAX, DEC, "follow_mouse" },
+    { "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" },
+
+    { "framerate",  "set video frame rate",      OFFSET(framerate),   AV_OPT_TYPE_VIDEO_RATE, {.str = "ntsc"}, 0, 0, DEC },
+    { "show_region", "show the grabbing region", OFFSET(show_region), AV_OPT_TYPE_INT,        {.i64 = 0}, 0, 1, DEC },
+    { "video_size",  "set video frame size",     OFFSET(width),       AV_OPT_TYPE_IMAGE_SIZE, {.str = "vga"}, 0, 0, DEC },
+    { "use_shm",     "use MIT-SHM extension",    OFFSET(use_shm),     AV_OPT_TYPE_INT,        {.i64 = 1}, 0, 1, DEC },
     { NULL },
 };
 
@@ -606,6 +653,7 @@ static const AVClass x11_class = {
     .item_name  = av_default_item_name,
     .option     = options,
     .version    = LIBAVUTIL_VERSION_INT,
+    .category   = AV_CLASS_CATEGORY_DEVICE_VIDEO_INPUT,
 };
 
 /** x11 grabber device demuxer declaration */