41 #include "libavutil/log.h"
42 #include "libavutil/opt.h"
43 #include "libavutil/parseutils.h"
44 #include "libavutil/time.h"
46 #include <X11/cursorfont.h>
49 #include <X11/Xlibint.h>
50 #include <X11/Xproto.h>
51 #include <X11/Xutil.h>
53 #include <X11/extensions/shape.h>
54 #include <X11/extensions/XShm.h>
55 #include <X11/extensions/Xfixes.h>
87 #define REGION_WIN_BORDER 3
96 Display *dpy = s->
dpy;
101 screen = DefaultScreen(dpy);
102 gc = XCreateGC(dpy, win, 0, 0);
103 XSetForeground(dpy, gc, WhitePixel(dpy, screen));
104 XSetBackground(dpy, gc, BlackPixel(dpy, screen));
106 XDrawRectangle(dpy, win, gc,
121 Display *dpy = s->
dpy;
123 XSetWindowAttributes attribs;
126 screen = DefaultScreen(dpy);
127 attribs.override_redirect = True;
128 s->
region_win = XCreateWindow(dpy, RootWindow(dpy, screen),
134 InputOutput, CopyFromParent,
135 CWOverrideRedirect, &attribs);
138 rect.width = s->
width;
142 &rect, 1, ShapeSubtract, 0);
144 XSelectInput(dpy, s->
region_win, ExposureMask | StructureNotifyMask);
180 offset = strchr(dpyname,
'+');
182 sscanf(offset,
"%d,%d", &x_off, &y_off);
183 if (strstr(offset,
"nomouse")) {
185 "'nomouse' specification in argument is deprecated: "
186 "use 'draw_mouse' option with value 0 instead\n");
192 av_log(s1,
AV_LOG_INFO,
"device: %s -> display: %s x: %d y: %d width: %d height: %d\n",
195 dpy = XOpenDisplay(dpyname);
210 screen = DefaultScreen(dpy);
213 int screen_w, screen_h;
216 screen_w = DisplayWidth(dpy, screen);
217 screen_h = DisplayHeight(dpy, screen);
218 XQueryPointer(dpy, RootWindow(dpy, screen), &w, &w, &x_off, &y_off, &ret, &ret, &ret);
219 x_off -= x11grab->
width / 2;
220 y_off -= x11grab->
height / 2;
223 av_log(s1,
AV_LOG_INFO,
"followmouse is enabled, resetting grabbing region to x: %d y: %d\n", x_off, y_off);
226 use_shm = XShmQueryExtension(dpy);
227 av_log(s1,
AV_LOG_INFO,
"shared memory extension%s found\n", use_shm ?
"" :
" not");
230 int scr = XDefaultScreen(dpy);
231 image = XShmCreateImage(dpy,
232 DefaultVisual(dpy, scr),
233 DefaultDepth(dpy, scr),
238 x11grab->
shminfo.shmid = shmget(IPC_PRIVATE,
239 image->bytes_per_line * image->height,
241 if (x11grab->
shminfo.shmid == -1) {
246 x11grab->
shminfo.shmaddr = image->data = shmat(x11grab->
shminfo.shmid, 0, 0);
247 x11grab->
shminfo.readOnly = False;
249 if (!XShmAttach(dpy, &x11grab->
shminfo)) {
256 image = XGetImage(dpy, RootWindow(dpy, screen),
262 switch (image->bits_per_pixel) {
266 color_map = DefaultColormap(dpy, screen);
267 for (i = 0; i < 256; ++i)
269 XQueryColors(dpy, color_map, color, 256);
270 for (i = 0; i < 256; ++i)
271 x11grab->
palette[i] = (color[i].red & 0xFF00) << 8 |
272 (color[i].green & 0xFF00) |
273 (color[i].blue & 0xFF00) >> 8;
277 if ( image->red_mask == 0xf800 &&
278 image->green_mask == 0x07e0 &&
279 image->blue_mask == 0x001f ) {
282 }
else if (image->red_mask == 0x7c00 &&
283 image->green_mask == 0x03e0 &&
284 image->blue_mask == 0x001f ) {
288 av_log(s1,
AV_LOG_ERROR,
"RGB ordering at image depth %i not supported ... aborting\n", image->bits_per_pixel);
289 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);
295 if ( image->red_mask == 0xff0000 &&
296 image->green_mask == 0x00ff00 &&
297 image->blue_mask == 0x0000ff ) {
299 }
else if ( image->red_mask == 0x0000ff &&
300 image->green_mask == 0x00ff00 &&
301 image->blue_mask == 0xff0000 ) {
304 av_log(s1,
AV_LOG_ERROR,
"rgb ordering at image depth %i not supported ... aborting\n", image->bits_per_pixel);
305 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);
314 av_log(s1,
AV_LOG_ERROR,
"image depth %i not supported ... aborting\n", image->bits_per_pixel);
356 XFixesCursorImage *xcim;
359 int to_line, to_column;
360 int pixstride = image->bits_per_pixel >> 3;
367 XSetWindowAttributes attr;
370 if (image->bits_per_pixel != 24 && image->bits_per_pixel != 32)
374 s->
c = XCreateFontCursor(dpy, XC_left_ptr);
375 w = DefaultRootWindow(dpy);
377 XChangeWindowAttributes(dpy, w, CWCursor, &attr);
379 xcim = XFixesGetCursorImage(dpy);
381 x = xcim->x - xcim->xhot;
382 y = xcim->y - xcim->yhot;
384 to_line =
FFMIN((y + xcim->height), (height + y_off));
385 to_column =
FFMIN((x + xcim->width), (width + x_off));
387 for (line =
FFMAX(y, y_off); line < to_line; line++) {
388 for (column =
FFMAX(x, x_off); column < to_column; column++) {
389 int xcim_addr = (line -
y) * xcim->width + column - x;
390 int image_addr = ((line - y_off) * width + column -
x_off) * pixstride;
391 int r = (
uint8_t)(xcim->pixels[xcim_addr] >> 0);
392 int g = (
uint8_t)(xcim->pixels[xcim_addr] >> 8);
393 int b = (
uint8_t)(xcim->pixels[xcim_addr] >> 16);
394 int a = (
uint8_t)(xcim->pixels[xcim_addr] >> 24);
397 pix[image_addr+0] =
r;
398 pix[image_addr+1] =
g;
399 pix[image_addr+2] =
b;
402 pix[image_addr+0] = r + (pix[image_addr+0]*(255-
a) + 255/2) / 255;
403 pix[image_addr+1] = g + (pix[image_addr+1]*(255-
a) + 255/2) / 255;
404 pix[image_addr+2] = b + (pix[image_addr+2]*(255-
a) + 255/2) / 255;
436 GetReq(GetImage, req);
442 req->width = image->width;
443 req->height = image->height;
444 req->planeMask = (
unsigned int)AllPlanes;
445 req->format = ZPixmap;
447 if (!_XReply(dpy, (xReply *)&rep, 0, xFalse) || !rep.length) {
453 nbytes = (long)rep.length << 2;
454 _XReadPad(dpy, image->data, nbytes);
481 int64_t curtime, delay;
497 ts.tv_sec = delay / 1000000;
498 ts.tv_nsec = (delay % 1000000) * 1000;
499 nanosleep(&ts, NULL);
503 pkt->
data = image->data;
517 screen = DefaultScreen(dpy);
518 root = RootWindow(dpy, screen);
520 int screen_w, screen_h;
521 int pointer_x, pointer_y,
_;
524 screen_w = DisplayWidth(dpy, screen);
525 screen_h = DisplayHeight(dpy, screen);
526 XQueryPointer(dpy, root, &w, &w, &pointer_x, &pointer_y, &_, &_, &_);
527 if (follow_mouse == -1) {
529 x_off += pointer_x - s->
width / 2 - x_off;
530 y_off += pointer_y - s->
height / 2 - y_off;
534 if (pointer_x > x_off + s->
width - follow_mouse) {
535 x_off += pointer_x - (x_off + s->
width - follow_mouse);
536 }
else if (pointer_x < x_off + follow_mouse)
537 x_off -= (x_off + follow_mouse) - pointer_x;
538 if (pointer_y > y_off + s->
height - follow_mouse) {
539 y_off += pointer_y - (y_off + s->
height - follow_mouse);
540 }
else if (pointer_y < y_off + follow_mouse)
541 y_off -= (y_off + follow_mouse) - pointer_y;
557 for (evt.type = NoEventMask; XCheckMaskEvent(dpy, ExposureMask | StructureNotifyMask, &evt); );
566 if (!XShmGetImage(dpy, root, image, x_off, y_off, AllPlanes)) {
596 shmdt(x11grab->
shminfo.shmaddr);
597 shmctl(x11grab->
shminfo.shmid, IPC_RMID, NULL);
601 if (x11grab->
image) {
602 XDestroyImage(x11grab->
image);
603 x11grab->
image = NULL;
611 XCloseDisplay(x11grab->
dpy);
615 #define OFFSET(x) offsetof(struct x11grab, x)
616 #define DEC AV_OPT_FLAG_DECODING_PARAM
620 {
"follow_mouse",
"move the grabbing region when the mouse pointer reaches within specified amount of pixels to the edge of region",
622 {
"centered",
"keep the mouse pointer at the center of grabbing region when following",
642 .priv_data_size =
sizeof(
struct x11grab),
static int x11grab_read_header(AVFormatContext *s1)
Initialize the x11 grab device demuxer (public device demuxer API).
int y_off
Vertical top-left corner coordinate.
AVInputFormat ff_x11grab_demuxer
x11 grabber device demuxer declaration
#define AV_LOG_WARNING
Something somehow does not look correct.
#define LIBAVUTIL_VERSION_INT
char * av_strdup(const char *s) av_malloc_attrib
Duplicate the string s.
#define REGION_WIN_BORDER
AVRational framerate
Set by a private option.
packed RGB 8:8:8, 24bpp, RGBRGB...
AVCodecContext * codec
Codec context associated with this stream.
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.
int show_region
set by a private option.
static int x11grab_read_packet(AVFormatContext *s1, AVPacket *pkt)
Grab a frame from x11 (public device demuxer API).
int height
Height of the grab frame.
AVRational time_base
This is the fundamental unit of time (in seconds) in terms of which frame timestamps are represented...
void av_freep(void *ptr)
Free a memory block which has been allocated with av_malloc(z)() or av_realloc() and set the pointer ...
packed RGB 8:8:8, 24bpp, BGRBGR...
const char * av_default_item_name(void *ctx)
Return the context name.
#define AV_PIX_FMT_RGB555
XShmSegmentInfo shminfo
When using XShm, keeps track of XShm infos.
static void x11grab_region_win_init(struct x11grab *s)
Initialize grabbing region window.
const char * class_name
The name of the class; usually it is the same name as the context structure type to which the AVClass...
static const uint32_t color[16+AV_CLASS_CATEGORY_NB]
static const uint8_t offset[511][2]
static double av_q2d(AVRational a)
Convert rational to double.
AVStream * avformat_new_stream(AVFormatContext *s, const AVCodec *c)
Add a new stream to a media file.
const OptionDef options[]
AVRational time_base
Time base.
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
void av_free(void *ptr)
Free a memory block which has been allocated with av_malloc(z)() or av_realloc(). ...
void * priv_data
Format private data.
char filename[1024]
input or output filename
#define NULL_IF_CONFIG_SMALL(x)
Return NULL if CONFIG_SMALL is true, otherwise the argument without modification. ...
#define AV_LOG_DEBUG
Stuff which is only useful for libav* developers.
static const AVClass x11_class
int draw_mouse
Set by a private option.
static int x11grab_read_close(AVFormatContext *s1)
Close x11 frame grabber (public device demuxer API).
AVPixelFormat
Pixel format.
uint8_t * av_packet_new_side_data(AVPacket *pkt, enum AVPacketSideDataType type, int size)
Allocate new information of a packet.
XImage * image
X11 image holding the grab.
int follow_mouse
Set by a private option.
static SDL_Surface * screen
int64_t av_gettime(void)
Get the current time in microseconds.
static int xget_zpixmap(Display *dpy, Drawable d, XImage *image, int x, int y)
Read new data in the image structure.
int bit_rate
the average bitrate
int width
picture width / height.
static void paint_mouse_pointer(XImage *image, struct x11grab *s)
Paint a mouse pointer in an X11 image.
int use_shm
!0 when using XShm extension
#define AV_LOG_INFO
Standard information.
int frame_size
Size in bytes of a grabbed frame.
enum AVMediaType codec_type
#define AV_PIX_FMT_0RGB32
static void x11grab_draw_region_win(struct x11grab *s)
Draw grabbing region window.
#define AV_PIX_FMT_RGB565
Main libavdevice API header.
BYTE int const BYTE int int int height
Describe the class of an AVClass context structure.
rational number numerator/denominator
int x_off
Horizontal top-left corner coordinate.
offset must point to AVRational
offset must point to two consecutive integers
Window region_win
This is used by show_region option.
static av_always_inline AVRational av_inv_q(AVRational q)
Invert a rational.
int64_t time_frame
Current time.
X11 Device Demuxer context.
void av_init_packet(AVPacket *pkt)
Initialize optional fields of a packet with default values.
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){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));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=av_sample_fmt_is_planar(in_fmt);out_planar=av_sample_fmt_is_planar(out_fmt);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);ff_audio_convert_init_arm(ac);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
int width
Width of the grab frame.
8 bit with PIX_FMT_RGB32 palette
This structure stores compressed data.
int64_t pts
Presentation timestamp in AVStream->time_base units; the time at which the decompressed packet will b...
Display * dpy
X11 display from which x11grab grabs frames.