]> git.sesse.net Git - ffmpeg/blobdiff - libavdevice/x11grab.c
Process picture aspect ratio changes in H.264.
[ffmpeg] / libavdevice / x11grab.c
index f1375f6deb7814aa029285ec20f7fdd1b2536743..60993276f5aac2ed73cde5c2ce6940d2a07a0a2e 100644 (file)
  */
 
 /**
- * @file x11grab.c
+ * @file libavdevice/x11grab.c
  * X11 frame device demuxer by Clemens Fruhwirth <clemens@endorphin.org>
  * and Edouard Gomez <ed.gomez@free.fr>.
  */
 
+#define _XOPEN_SOURCE 600
+
 #include "config.h"
 #include "libavformat/avformat.h"
-#include <unistd.h>
-#include <fcntl.h>
-#include <sys/ioctl.h>
-#include <sys/time.h>
-#define _LINUX_TIME_H 1
 #include <time.h>
 #include <X11/X.h>
 #include <X11/Xlib.h>
 #include <X11/Xlibint.h>
 #include <X11/Xproto.h>
 #include <X11/Xutil.h>
-#include <sys/ipc.h>
 #include <sys/shm.h>
 #include <X11/extensions/XShm.h>
+#include <X11/extensions/Xfixes.h>
 
 /**
  * X11 Device Demuxer context
  */
-typedef struct x11_grab_s
+struct x11_grab
 {
     int frame_size;          /**< Size in bytes of a grabbed frame */
     AVRational time_base;    /**< Time base */
@@ -70,8 +67,8 @@ typedef struct x11_grab_s
     XImage *image;           /**< X11 image holding the grab */
     int use_shm;             /**< !0 when using XShm extension */
     XShmSegmentInfo shminfo; /**< When using XShm, keeps track of XShm infos */
-    int mouse_warning_shown;
-} x11_grab_t;
+    int nomouse;
+};
 
 /**
  * Initializes the x11 grab device demuxer (public device demuxer API).
@@ -87,10 +84,10 @@ typedef struct x11_grab_s
 static int
 x11grab_read_header(AVFormatContext *s1, AVFormatParameters *ap)
 {
-    x11_grab_t *x11grab = s1->priv_data;
+    struct x11_grab *x11grab = s1->priv_data;
     Display *dpy;
     AVStream *st = NULL;
-    int input_pixfmt;
+    enum PixelFormat input_pixfmt;
     XImage *image;
     int x_off = 0;
     int y_off = 0;
@@ -101,6 +98,7 @@ x11grab_read_header(AVFormatContext *s1, AVFormatParameters *ap)
     offset = strchr(param, '+');
     if (offset) {
         sscanf(offset, "%d,%d", &x_off, &y_off);
+        x11grab->nomouse= strstr(offset, "nomouse");
         *offset= 0;
     }
 
@@ -227,7 +225,6 @@ x11grab_read_header(AVFormatContext *s1, AVFormatParameters *ap)
     x11grab->y_off = y_off;
     x11grab->image = image;
     x11grab->use_shm = use_shm;
-    x11grab->mouse_warning_shown = 0;
 
     st->codec->codec_type = CODEC_TYPE_VIDEO;
     st->codec->codec_id = CODEC_ID_RAWVIDEO;
@@ -240,62 +237,6 @@ x11grab_read_header(AVFormatContext *s1, AVFormatParameters *ap)
     return 0;
 }
 
-/**
- * Get pointer coordinates from X11.
- *
- * @param x Integer where horizontal coordinate will be returned
- * @param y Integer where vertical coordinate will be returned
- * @param dpy X11 display from where pointer coordinates are retrieved
- * @param s1 Context used for logging errors if necessary
- */
-static void
-get_pointer_coordinates(int *x, int *y, Display *dpy, AVFormatContext *s1)
-{
-    Window mrootwindow, childwindow;
-    int dummy;
-
-    mrootwindow = DefaultRootWindow(dpy);
-
-    if (XQueryPointer(dpy, mrootwindow, &mrootwindow, &childwindow,
-                      x, y, &dummy, &dummy, (unsigned int*)&dummy)) {
-    } else {
-        x11_grab_t *s = s1->priv_data;
-        if (!s->mouse_warning_shown) {
-            av_log(s1, AV_LOG_INFO, "couldn't find mouse pointer\n");
-            s->mouse_warning_shown = 1;
-        }
-        *x = -1;
-        *y = -1;
-    }
-}
-
-/**
- * Mouse painting helper function that applies an 'and' and 'or' mask pair to
- * '*dst' pixel. It actually draws a mouse pointer pixel to grabbed frame.
- *
- * @param dst Destination pixel
- * @param and Part of the mask that must be applied using a bitwise 'and'
- *            operator
- * @param or  Part of the mask that must be applied using a bitwise 'or'
- *            operator
- * @param bits_per_pixel Bits per pixel used in the grabbed image
- */
-static void inline
-apply_masks(uint8_t *dst, int and, int or, int bits_per_pixel)
-{
-    switch (bits_per_pixel) {
-    case 32:
-        *(uint32_t*)dst = (*(uint32_t*)dst & and) | or;
-        break;
-    case 16:
-        *(uint16_t*)dst = (*(uint16_t*)dst & and) | or;
-        break;
-    case 8:
-        *dst = !!or;
-        break;
-    }
-}
-
 /**
  * Paints a mouse pointer in an X11 image.
  *
@@ -306,71 +247,43 @@ apply_masks(uint8_t *dst, int and, int or, int bits_per_pixel)
  * @param y Mouse pointer coordinate
  */
 static void
-paint_mouse_pointer(XImage *image, x11_grab_t *s, int x, int y)
+paint_mouse_pointer(XImage *image, struct x11_grab *s)
 {
-    /* 16x20x1bpp bitmap for the black channel of the mouse pointer */
-    static const uint16_t const mousePointerBlack[] =
-        {
-            0x0000, 0x0003, 0x0005, 0x0009, 0x0011,
-            0x0021, 0x0041, 0x0081, 0x0101, 0x0201,
-            0x03c1, 0x0049, 0x0095, 0x0093, 0x0120,
-            0x0120, 0x0240, 0x0240, 0x0380, 0x0000
-        };
-
-    /* 16x20x1bpp bitmap for the white channel of the mouse pointer */
-    static const uint16_t const mousePointerWhite[] =
-        {
-            0x0000, 0x0000, 0x0002, 0x0006, 0x000e,
-            0x001e, 0x003e, 0x007e, 0x00fe, 0x01fe,
-            0x003e, 0x0036, 0x0062, 0x0060, 0x00c0,
-            0x00c0, 0x0180, 0x0180, 0x0000, 0x0000
-        };
-
     int x_off = s->x_off;
     int y_off = s->y_off;
     int width = s->width;
     int height = s->height;
+    Display *dpy = s->dpy;
+    XFixesCursorImage *xcim;
+    int x, y;
+    int line, column;
+    int to_line, to_column;
+    int image_addr, xcim_addr;
 
-    if (   x - x_off >= 0 && x < width + x_off
-        && y - y_off >= 0 && y < height + y_off) {
-        uint8_t *im_data = (uint8_t*)image->data;
-        int bytes_per_pixel;
-        int line;
-        int masks;
+    xcim = XFixesGetCursorImage(dpy);;
 
-        /* Select correct masks and pixel size */
-        if (image->bits_per_pixel == 8) {
-            masks = 1;
-        } else {
-            masks = (image->red_mask|image->green_mask|image->blue_mask);
-        }
-        bytes_per_pixel = image->bits_per_pixel>>3;
-
-        /* Shift to right line */
-        im_data += image->bytes_per_line * (y - y_off);
-        /* Shift to right pixel in the line */
-        im_data += bytes_per_pixel * (x - x_off);
-
-        /* Draw the cursor - proper loop */
-        for (line = 0; line < FFMIN(20, (y_off + height) - y); line++) {
-            uint8_t *cursor = im_data;
-            int column;
-            uint16_t bm_b;
-            uint16_t bm_w;
-
-            bm_b = mousePointerBlack[line];
-            bm_w = mousePointerWhite[line];
-
-            for (column = 0; column < FFMIN(16, (x_off + width) - x); column++) {
-                apply_masks(cursor, ~(masks*(bm_b&1)), masks*(bm_w&1),
-                            image->bits_per_pixel);
-                cursor += bytes_per_pixel;
-                bm_b >>= 1;
-                bm_w >>= 1;
+    x = xcim->x - xcim->xhot;
+    y = xcim->y - xcim->yhot;
+
+    to_line = FFMIN((y + xcim->height), (height + y_off));
+    to_column = FFMIN((x + xcim->width), (width + x_off));
+
+    for (line = FFMAX(y, y_off); line < to_line; line++) {
+        for (column = FFMAX(x, x_off); column < to_column; column++) {
+            xcim_addr = (line - y) * xcim->width + column - x;
+
+            if ((unsigned char)(xcim->pixels[xcim_addr] >> 24) != 0) { // skip fully transparent pixel
+                image_addr = ((line - y_off) * width + column - x_off) * 4;
+
+                image->data[image_addr] = (unsigned char)(xcim->pixels[xcim_addr] >> 0);
+                image->data[image_addr+1] = (unsigned char)(xcim->pixels[xcim_addr] >> 8);
+                image->data[image_addr+2] = (unsigned char)(xcim->pixels[xcim_addr] >> 16);
             }
-            im_data += image->bytes_per_line;
         }
     }
+
+    XFree(xcim);
+    xcim = NULL;
 }
 
 
@@ -431,7 +344,7 @@ xget_zpixmap(Display *dpy, Drawable d, XImage *image, int x, int y)
 static int
 x11grab_read_packet(AVFormatContext *s1, AVPacket *pkt)
 {
-    x11_grab_t *s = s1->priv_data;
+    struct x11_grab *s = s1->priv_data;
     Display *dpy = s->dpy;
     XImage *image = s->image;
     int x_off = s->x_off;
@@ -474,10 +387,8 @@ x11grab_read_packet(AVFormatContext *s1, AVPacket *pkt)
         }
     }
 
-    {
-        int pointer_x, pointer_y;
-        get_pointer_coordinates(&pointer_x, &pointer_y, dpy, s1);
-        paint_mouse_pointer(image, s, pointer_x, pointer_y);
+    if(!s->nomouse){
+        paint_mouse_pointer(image, s);
     }
 
 
@@ -495,7 +406,7 @@ x11grab_read_packet(AVFormatContext *s1, AVPacket *pkt)
 static int
 x11grab_read_close(AVFormatContext *s1)
 {
-    x11_grab_t *x11grab = s1->priv_data;
+    struct x11_grab *x11grab = s1->priv_data;
 
     /* Detach cleanly from shared mem */
     if (x11grab->use_shm) {
@@ -519,8 +430,8 @@ x11grab_read_close(AVFormatContext *s1)
 AVInputFormat x11_grab_device_demuxer =
 {
     "x11grab",
-    "X11grab",
-    sizeof(x11_grab_t),
+    NULL_IF_CONFIG_SMALL("X11grab"),
+    sizeof(struct x11_grab),
     NULL,
     x11grab_read_header,
     x11grab_read_packet,