*/
#include "config.h"
- #include "libavformat/internal.h"
- #include "libavutil/log.h"
- #include "libavutil/opt.h"
- #include "libavutil/parseutils.h"
- #include "libavutil/time.h"
+
#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/Xproto.h>
#include <X11/Xutil.h>
- #include <sys/shm.h>
+
#include <X11/extensions/shape.h>
- #include <X11/extensions/XShm.h>
#include <X11/extensions/Xfixes.h>
- /**
- * X11 Device Demuxer context
- */
+ #include <X11/extensions/XShm.h>
+
+#include "avdevice.h"
+
-#include "libavformat/avformat.h"
+ #include "libavutil/log.h"
+ #include "libavutil/opt.h"
+ #include "libavutil/parseutils.h"
+ #include "libavutil/time.h"
+
+ #include "libavformat/internal.h"
+
+ /** X11 device demuxer context */
struct x11grab {
const AVClass *class; /**< Class for private options. */
int frame_size; /**< Size in bytes of a grabbed frame */
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 draw_mouse; /**< Set by a private option. */
- int follow_mouse; /**< Set by a private option. */
- int show_region; /**< set by a private option. */
- AVRational framerate; /**< Set by a private option. */
+ 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. */
};
int x_off = 0;
int y_off = 0;
int screen;
- int use_shm;
- char *param, *offset;
+ int use_shm = 0;
+ char *dpyname, *offset;
int ret = 0;
- AVRational framerate;
+ Colormap color_map;
+ XColor color[256];
+ int i;
- param = av_strdup(s1->filename);
- if (!param)
+ 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;
+ *offset = 0;
}
- av_log(s1, AV_LOG_INFO, "device: %s -> display: %s x: %d y: %d width: %d height: %d\n",
- if ((ret = av_parse_video_size(&x11grab->width, &x11grab->height,
- x11grab->video_size)) < 0) {
- av_log(s1, AV_LOG_ERROR, "Couldn't parse video size.\n");
- goto out;
- }
- if ((ret = av_parse_video_rate(&framerate, x11grab->framerate)) < 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) {
+ if (!dpy) {
av_log(s1, AV_LOG_ERROR, "Could not open X display.\n");
ret = AVERROR(EIO);
goto out;
screen_w = DisplayWidth(dpy, screen);
screen_h = DisplayHeight(dpy, screen);
- XQueryPointer(dpy, RootWindow(dpy, screen), &w, &w, &x_off, &y_off, &ret, &ret, &ret);
+ XQueryPointer(dpy, RootWindow(dpy, screen), &w, &w, &x_off, &y_off,
+ &ret, &ret, &ret);
x_off -= x11grab->width / 2;
y_off -= x11grab->height / 2;
- x_off = FFMIN(FFMAX(x_off, 0), screen_w - x11grab->width);
- y_off = FFMIN(FFMAX(y_off, 0), screen_h - x11grab->height);
- av_log(s1, AV_LOG_INFO, "followmouse is enabled, resetting grabbing region to x: %d y: %d\n", x_off, y_off);
+ x_off = FFMIN(FFMAX(x_off, 0), screen_w - x11grab->width);
+ y_off = FFMIN(FFMAX(y_off, 0), screen_h - x11grab->height);
+ av_log(s1, AV_LOG_INFO,
+ "followmouse is enabled, resetting grabbing region to x: %d y: %d\n",
+ x_off, y_off);
}
- use_shm = XShmQueryExtension(dpy);
- av_log(s1, AV_LOG_INFO,
- "shared memory extension %s found\n", use_shm ? "" : "not");
+ if (x11grab->use_shm) {
+ use_shm = XShmQueryExtension(dpy);
- av_log(s1, AV_LOG_INFO, "shared memory extension%s found\n", use_shm ? "" : " not");
++ av_log(s1, AV_LOG_INFO,
++ "shared memory extension%s found\n", use_shm ? "" : " not");
+ }
- if(use_shm) {
+ if (use_shm) {
int scr = XDefaultScreen(dpy);
image = XShmCreateImage(dpy,
DefaultVisual(dpy, scr),
switch (image->bits_per_pixel) {
case 8:
- av_log (s1, AV_LOG_DEBUG, "8 bit palette\n");
+ av_log(s1, AV_LOG_DEBUG, "8 bit palette\n");
input_pixfmt = 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;
break;
case 16:
- if ( image->red_mask == 0xf800 &&
- image->green_mask == 0x07e0 &&
- image->blue_mask == 0x001f ) {
- av_log (s1, AV_LOG_DEBUG, "16 bit RGB565\n");
+ if (image->red_mask == 0xf800 &&
+ image->green_mask == 0x07e0 &&
+ image->blue_mask == 0x001f) {
+ av_log(s1, AV_LOG_DEBUG, "16 bit RGB565\n");
input_pixfmt = AV_PIX_FMT_RGB565;
} else if (image->red_mask == 0x7c00 &&
image->green_mask == 0x03e0 &&
av_log(s1, AV_LOG_DEBUG, "16 bit RGB555\n");
input_pixfmt = AV_PIX_FMT_RGB555;
} else {
- av_log(s1, AV_LOG_ERROR, "RGB ordering at image depth %i not supported ... aborting\n", image->bits_per_pixel);
- 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);
+ av_log(s1, AV_LOG_ERROR,
+ "RGB ordering at image depth %i not supported ... aborting\n",
+ image->bits_per_pixel);
+ 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);
- ret = AVERROR(EIO);
+ ret = AVERROR_PATCHWELCOME;
goto out;
}
break;
case 24:
- if ( image->red_mask == 0xff0000 &&
- image->green_mask == 0x00ff00 &&
- image->blue_mask == 0x0000ff ) {
+ if (image->red_mask == 0xff0000 &&
+ image->green_mask == 0x00ff00 &&
+ image->blue_mask == 0x0000ff) {
input_pixfmt = AV_PIX_FMT_BGR24;
- } else if ( image->red_mask == 0x0000ff &&
- image->green_mask == 0x00ff00 &&
- image->blue_mask == 0xff0000 ) {
+ } else if (image->red_mask == 0x0000ff &&
+ image->green_mask == 0x00ff00 &&
+ image->blue_mask == 0xff0000) {
input_pixfmt = AV_PIX_FMT_RGB24;
} else {
- av_log(s1, AV_LOG_ERROR,"rgb ordering at image depth %i not supported ... aborting\n", image->bits_per_pixel);
- 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);
+ av_log(s1, AV_LOG_ERROR,
+ "rgb ordering at image depth %i not supported ... aborting\n",
+ image->bits_per_pixel);
+ 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);
- ret = AVERROR(EIO);
+ ret = AVERROR_PATCHWELCOME;
goto out;
}
break;
case 32:
- input_pixfmt = AV_PIX_FMT_RGB32;
+ if ( image->red_mask == 0xff0000 &&
+ image->green_mask == 0x00ff00 &&
+ image->blue_mask == 0x0000ff ) {
+ input_pixfmt = AV_PIX_FMT_0RGB32;
+ } else {
+ av_log(s1, AV_LOG_ERROR,"rgb ordering at image depth %i not supported ... aborting\n", image->bits_per_pixel);
+ 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);
+ ret = AVERROR_PATCHWELCOME;
+ goto out;
+ }
break;
default:
- av_log(s1, AV_LOG_ERROR, "image depth %i not supported ... aborting\n", image->bits_per_pixel);
+ av_log(s1, AV_LOG_ERROR,
+ "image depth %i not supported ... aborting\n",
+ image->bits_per_pixel);
- ret = AVERROR(EINVAL);
+ ret = AVERROR_PATCHWELCOME;
goto out;
}
- x11grab->frame_size = x11grab->width * x11grab->height * image->bits_per_pixel/8;
- x11grab->dpy = dpy;
+ 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;
- x11grab->image = image;
- x11grab->use_shm = use_shm;
+ x11grab->x_off = x_off;
+ x11grab->y_off = y_off;
+ x11grab->image = image;
+ x11grab->use_shm = use_shm;
st->codec->codec_type = AVMEDIA_TYPE_VIDEO;
- st->codec->codec_id = AV_CODEC_ID_RAWVIDEO;
- st->codec->width = x11grab->width;
- st->codec->height = x11grab->height;
- st->codec->pix_fmt = input_pixfmt;
- st->codec->time_base = x11grab->time_base;
- st->codec->bit_rate = x11grab->frame_size * 1/av_q2d(x11grab->time_base) * 8;
+ st->codec->codec_id = AV_CODEC_ID_RAWVIDEO;
+ st->codec->width = x11grab->width;
+ st->codec->height = x11grab->height;
+ st->codec->pix_fmt = input_pixfmt;
+ st->codec->time_base = x11grab->time_base;
+ st->codec->bit_rate = x11grab->frame_size * 1 / av_q2d(x11grab->time_base) * 8;
out:
- av_free(param);
+ av_free(dpyname);
return ret;
}
* @param s context used to retrieve original grabbing rectangle
* coordinates
*/
- static void
- paint_mouse_pointer(XImage *image, AVFormatContext *s1)
-static void paint_mouse_pointer(XImage *image, struct x11grab *s)
++static void paint_mouse_pointer(XImage *image, AVFormatContext *s1)
{
- int x_off = s->x_off;
- int y_off = s->y_off;
- int width = s->width;
- int height = s->height;
+ struct x11grab *s = s1->priv_data;
+ 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;
if (image->bits_per_pixel != 24 && image->bits_per_pixel != 32)
return;
- if(!s->c)
++ 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;
av_init_packet(pkt);
pkt->data = image->data;
pkt->size = s->frame_size;
- pkt->pts = curtime;
+ 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);
+ root = RootWindow(dpy, screen);
if (follow_mouse) {
int screen_w, screen_h;
int pointer_x, pointer_y, _;
}
}
- if(s->use_shm) {
- if (!XShmGetImage(dpy, root, image, x_off, y_off, AllPlanes)) {
- av_log (s1, AV_LOG_INFO, "XShmGetImage() failed\n");
- }
+ if (s->use_shm) {
+ if (!XShmGetImage(dpy, root, image, x_off, y_off, AllPlanes))
+ av_log(s1, AV_LOG_INFO, "XShmGetImage() failed\n");
} else {
- if (!xget_zpixmap(dpy, root, image, x_off, y_off)) {
- av_log (s1, AV_LOG_INFO, "XGetZPixmap() failed\n");
- }
+ if (!xget_zpixmap(dpy, root, image, x_off, y_off))
+ av_log(s1, AV_LOG_INFO, "XGetZPixmap() failed\n");
}
- if (s->draw_mouse) {
+ if (s->draw_mouse)
- paint_mouse_pointer(image, s);
+ paint_mouse_pointer(image, s1);
- }
return s->frame_size;
}