RFC 5: Mouse cursor painting reduc and more cleanups
[libav.git] / libavformat / x11grab.c
CommitLineData
f58f4ce1
GP
1/*
2 * X11 video grab interface
2909f177 3 *
ef84bf04 4 * This file is part of FFmpeg.
f58f4ce1 5 *
ef84bf04
EG
6 * FFmpeg integration:
7 * Copyright (C) 2006 Clemens Fruhwirth <clemens@endorphin.org>
8 * Edouard Gomez <ed.gomez@free.fr>
f58f4ce1 9 *
ef84bf04 10 * This file contains code from grab.c:
aac105fc 11 * Copyright (c) 2000-2001 Fabrice Bellard
f58f4ce1
GP
12 *
13 * This file contains code from the xvidcap project:
ef84bf04
EG
14 * Copyright (C) 1997-1998 Rasca, Berlin
15 * 2003-2004 Karl H. Beckers, Frankfurt
f58f4ce1 16 *
ef84bf04 17 * FFmpeg is free software; you can redistribute it and/or modify
f58f4ce1
GP
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 *
ef84bf04 22 * FFmpeg is distributed in the hope that it will be useful,
f58f4ce1
GP
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 *
0235a311
EG
27 * You should have received a copy of the GNU General Public License along
28 * with this program; if not, write to the Free Software Foundation, Inc.,
29 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
f58f4ce1 30 */
2909f177 31
f58f4ce1
GP
32#include "avformat.h"
33#include <unistd.h>
34#include <fcntl.h>
35#include <sys/ioctl.h>
36#include <sys/mman.h>
37#include <sys/time.h>
38#define _LINUX_TIME_H 1
39#include <time.h>
40#include <X11/X.h>
41#include <X11/Xlib.h>
42#include <X11/Xlibint.h>
43#include <X11/Xproto.h>
ef84bf04 44#include <X11/Xutil.h>
f58f4ce1
GP
45#include <sys/ipc.h>
46#include <sys/shm.h>
47#include <X11/extensions/XShm.h>
48
2909f177
EG
49typedef struct
50{
aac105fc
EG
51 Display *dpy;
52 int frame_format;
53 int frame_size;
54 int frame_rate;
55 int frame_rate_base;
56 int64_t time_frame;
57
58 int height;
59 int width;
60 int x_off;
61 int y_off;
62 XImage *image;
63 int use_shm;
64 XShmSegmentInfo shminfo;
65 int mouse_wanted;
f58f4ce1
GP
66} X11Grab;
67
2909f177
EG
68static int
69x11grab_read_header(AVFormatContext *s1, AVFormatParameters *ap)
f58f4ce1 70{
aac105fc
EG
71 X11Grab *x11grab = s1->priv_data;
72 Display *dpy;
73 AVStream *st = NULL;
74 int width, height;
75 int frame_rate, frame_rate_base, frame_size;
76 int input_pixfmt;
77 XImage *image;
78 int x_off=0; int y_off = 0;
79 int use_shm;
80
81 dpy = XOpenDisplay(NULL);
82 if(!dpy) {
83 goto fail;
84 }
85
86 sscanf(ap->device, "x11:%d,%d", &x_off, &y_off);
87 av_log(s1, AV_LOG_INFO, "device: %s -> x: %d y: %d width: %d height: %d\n", ap->device, x_off, y_off, ap->width, ap->height);
88
89 if (!ap || ap->width <= 0 || ap->height <= 0 || ap->time_base.den <= 0) {
90 av_log(s1, AV_LOG_ERROR, "AVParameters don't have any video size. Use -s.\n");
91 return AVERROR_IO;
92 }
93
94 width = ap->width;
95 height = ap->height;
96 frame_rate = ap->time_base.den;
97 frame_rate_base = ap->time_base.num;
98
99 st = av_new_stream(s1, 0);
100 if (!st) {
101 return -ENOMEM;
102 }
103 av_set_pts_info(st, 64, 1, 1000000); /* 64 bits pts in us */
104
105 use_shm = XShmQueryExtension(dpy);
106 av_log(s1, AV_LOG_INFO, "shared memory extension %s\n", use_shm ? "found" : "not found");
107
108 if(use_shm) {
109 int scr = XDefaultScreen(dpy);
110 image = XShmCreateImage(dpy,
111 DefaultVisual(dpy, scr),
112 DefaultDepth(dpy, scr),
113 ZPixmap,
114 NULL,
115 &x11grab->shminfo,
116 ap->width, ap->height);
117 x11grab->shminfo.shmid = shmget(IPC_PRIVATE,
118 image->bytes_per_line * image->height,
119 IPC_CREAT|0777);
120 if (x11grab->shminfo.shmid == -1) {
121 av_log(s1, AV_LOG_ERROR, "Fatal: Can't get shared memory!\n");
122 return -ENOMEM;
123 }
124 x11grab->shminfo.shmaddr = image->data = shmat(x11grab->shminfo.shmid, 0, 0);
125 x11grab->shminfo.readOnly = False;
126
127 if (!XShmAttach(dpy, &x11grab->shminfo)) {
128 av_log(s1, AV_LOG_ERROR, "Fatal: Failed to attach shared memory!\n");
129 /* needs some better error subroutine :) */
130 return AVERROR_IO;
131 }
132 } else {
133 image = XGetImage(dpy, RootWindow(dpy, DefaultScreen(dpy)),
134 x_off,y_off,
135 ap->width,ap->height,
136 AllPlanes, ZPixmap);
137 }
138
139 switch (image->bits_per_pixel) {
140 case 8:
141 av_log (s1, AV_LOG_DEBUG, "8 bit pallete\n");
142 input_pixfmt = PIX_FMT_PAL8;
143 break;
144 case 16:
145 if ( image->red_mask == 0xF800 && image->green_mask == 0x07E0
146 && image->blue_mask == 0x1F ) {
147 av_log (s1, AV_LOG_DEBUG, "16 bit RGB565\n");
148 input_pixfmt = PIX_FMT_RGB565;
149 } else if ( image->red_mask == 0x7C00 &&
150 image->green_mask == 0x03E0 &&
151 image->blue_mask == 0x1F ) {
152 av_log(s1, AV_LOG_DEBUG, "16 bit RGB555\n");
153 input_pixfmt = PIX_FMT_RGB555;
154 } else {
155 av_log(s1, AV_LOG_ERROR, "RGB ordering at image depth %i not supported ... aborting\n", image->bits_per_pixel);
156 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);
157 return AVERROR_IO;
158 }
159 break;
160 case 24:
161 if ( image->red_mask == 0xFF0000 &&
162 image->green_mask == 0xFF00
163 && image->blue_mask == 0xFF ) {
164 input_pixfmt = PIX_FMT_BGR24;
165 } else if ( image->red_mask == 0xFF && image->green_mask == 0xFF00
166 && image->blue_mask == 0xFF0000 ) {
167 input_pixfmt = PIX_FMT_RGB24;
168 } else {
169 av_log(s1, AV_LOG_ERROR,"rgb ordering at image depth %i not supported ... aborting\n", image->bits_per_pixel);
170 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);
171 return AVERROR_IO;
172 }
173 break;
174 case 32:
f58f4ce1 175#if 0
aac105fc
EG
176 GetColorInfo (image, &c_info);
177 if ( c_info.alpha_mask == 0xFF000000 && image->green_mask == 0xFF00 ) {
178 /* byte order is relevant here, not endianness
179 * endianness is handled by avcodec, but atm no such thing
180 * as having ABGR, instead of ARGB in a word. Since we
181 * need this for Solaris/SPARC, but need to do the conversion
182 * for every frame we do it outside of this loop, cf. below
183 * this matches both ARGB32 and ABGR32 */
184 input_pixfmt = PIX_FMT_ARGB32;
185 } else {
186 av_log(s1, AV_LOG_ERROR,"image depth %i not supported ... aborting\n", image->bits_per_pixel);
187 return AVERROR_IO;
188 }
f58f4ce1 189#endif
aac105fc
EG
190 input_pixfmt = PIX_FMT_RGBA32;
191 break;
192 default:
193 av_log(s1, AV_LOG_ERROR, "image depth %i not supported ... aborting\n", image->bits_per_pixel);
194 return -1;
195 }
196
197 frame_size = width * height * image->bits_per_pixel/8;
198 x11grab->frame_size = frame_size;
199 x11grab->dpy = dpy;
200 x11grab->width = ap->width;
201 x11grab->height = ap->height;
202 x11grab->frame_rate = frame_rate;
203 x11grab->frame_rate_base = frame_rate_base;
204 x11grab->time_frame = av_gettime() * frame_rate / frame_rate_base;
205 x11grab->x_off = x_off;
206 x11grab->y_off = y_off;
207 x11grab->image = image;
208 x11grab->use_shm = use_shm;
209 x11grab->mouse_wanted = 1;
210
211 st->codec->codec_type = CODEC_TYPE_VIDEO;
212 st->codec->codec_id = CODEC_ID_RAWVIDEO;
213 st->codec->width = width;
214 st->codec->height = height;
215 st->codec->pix_fmt = input_pixfmt;
216 st->codec->time_base.den = frame_rate;
217 st->codec->time_base.num = frame_rate_base;
218 st->codec->bit_rate = frame_size * 1/av_q2d(st->codec->time_base) * 8;
219
220 return 0;
2909f177 221fail:
aac105fc
EG
222 av_free(st);
223 return AVERROR_IO;
f58f4ce1
GP
224}
225
2909f177
EG
226static void
227getCurrentPointer(AVFormatContext *s1, X11Grab *s, int *x, int *y)
228{
aac105fc
EG
229 Window mrootwindow, childwindow;
230 int dummy;
231 Display *dpy = s->dpy;
232
233 mrootwindow = DefaultRootWindow(dpy);
234
235 if (XQueryPointer(dpy, mrootwindow, &mrootwindow, &childwindow,
236 x, y, &dummy, &dummy, (unsigned int*)&dummy)) {
237 } else {
238 av_log(s1, AV_LOG_INFO, "couldn't find mouse pointer\n");
239 *x = -1;
240 *y = -1;
241 }
f58f4ce1
GP
242}
243
0235a311
EG
244static void inline
245apply_masks(uint8_t *dst, int and, int or, int bits_per_pixel)
246{
247 switch (bits_per_pixel) {
248 case 32:
249 *(uint32_t*)dst = (*(uint32_t*)dst & and) | or;
250 break;
251 case 16:
252 *(uint16_t*)dst = (*(uint16_t*)dst & and) | or;
253 break;
254 case 8:
255 *dst = (or) ? 1 : 0;
256 break;
257 }
258}
65a1c656 259
2909f177
EG
260static void
261paintMousePointer(AVFormatContext *s1, X11Grab *s, int *x, int *y, XImage *image)
262{
aac105fc
EG
263 static const uint16_t const mousePointerBlack[] =
264 {
265 0, 49152, 40960, 36864, 34816,
266 33792, 33280, 33024, 32896, 32832,
267 33728, 37376, 43264, 51456, 1152,
268 1152, 576, 576, 448, 0
269 };
270
271 static const uint16_t const mousePointerWhite[] =
272 {
273 0, 0, 16384, 24576, 28672,
274 30720, 31744, 32256, 32512, 32640,
275 31744, 27648, 17920, 1536, 768,
276 768, 384, 384, 0, 0
277 };
278
279 int x_off = s->x_off;
280 int y_off = s->y_off;
281 int width = s->width;
282 int height = s->height;
283
284 if ( (*x - x_off) >= 0 && *x < (width + x_off)
285 && (*y - y_off) >= 0 && *y < (height + y_off) ) {
286 int line;
287 uint8_t *im_data = (uint8_t*)image->data;
0235a311
EG
288 const uint16_t *black;
289 const uint16_t *white;
290 int masks;
291 int onepixel;
292
293 /* Select correct pointer pixels */
294 if (s->mouse_wanted == 1) {
295 /* Normal pointer */
296 black = mousePointerBlack;
297 white = mousePointerWhite;
298 } else {
299 /* Inverted pointer */
300 black = mousePointerWhite;
301 white = mousePointerBlack;
302 }
aac105fc 303
0235a311
EG
304 /* Select correct masks and pixel size */
305 switch (image->bits_per_pixel) {
aac105fc 306 case 32:
0235a311
EG
307 masks = (image->red_mask|image->green_mask|image->blue_mask);
308 onepixel = 4;
309 break;
310 case 24:
311 /* XXX: Though the code seems to support 24bit images, the
312 * apply_masks lacks support for 24bit */
313 masks = (image->red_mask|image->green_mask|image->blue_mask);
314 onepixel = 3;
aac105fc
EG
315 break;
316 case 16:
0235a311
EG
317 masks = (image->red_mask|image->green_mask|image->blue_mask);
318 onepixel = 2;
aac105fc 319 break;
0235a311
EG
320 case 8:
321 masks = 1;
322 onepixel = 1;
aac105fc 323 break;
0235a311
EG
324 default:
325 /* Shut up gcc */
326 masks = 0;
327 onepixel = 0;
328 }
329
330 /* Shift to right line */
331 im_data += (image->bytes_per_line * (*y - y_off));
332 /* Shift to right pixel */
333 im_data += (image->bits_per_pixel / 8 * (*x - x_off));
334
335 /* Draw the cursor - proper loop */
336 for (line = 0; line < min(20, (y_off + height) - *y); line++) {
337 uint8_t *cursor = im_data;
338 int width_cursor;
339 uint16_t bm_b;
340 uint16_t bm_w;
341
342 bm_b = black[line];
343 bm_w = white[line];
344
345 for (width_cursor=0;
346 width_cursor < 16 && (width_cursor + *x) < (width + x_off);
347 width_cursor++) {
348 apply_masks(cursor,
349 ~(masks*(bm_b&1)), masks*(bm_w&1),
350 image->bits_per_pixel);
351 cursor += onepixel;
352 bm_b >>= 1;
353 bm_w >>= 1;
354 }
355 im_data += image->bytes_per_line;
aac105fc
EG
356 }
357 }
f58f4ce1
GP
358}
359
360
361/*
362 * just read new data in the image structure, the image
363 * structure inclusive the data area must be allocated before
364 */
365static int
366XGetZPixmap(Display *dpy, Drawable d, XImage *image, int x, int y)
367{
aac105fc
EG
368 xGetImageReply rep;
369 xGetImageReq *req;
370 long nbytes;
371
372 if (!image) {
373 return False;
374 }
375
376 LockDisplay(dpy);
377 GetReq(GetImage, req);
378
379 /* First set up the standard stuff in the request */
380 req->drawable = d;
381 req->x = x;
382 req->y = y;
383 req->width = image->width;
384 req->height = image->height;
385 req->planeMask = (unsigned int)AllPlanes;
386 req->format = ZPixmap;
387
388 if (!_XReply(dpy, (xReply *) &rep, 0, xFalse) || !rep.length) {
389 UnlockDisplay(dpy);
390 SyncHandle();
391 return False;
392 }
393
394 nbytes = (long)rep.length << 2;
395 _XReadPad(dpy, image->data, nbytes);
396
397 UnlockDisplay(dpy);
398 SyncHandle();
399 return True;
f58f4ce1
GP
400}
401
0235a311
EG
402static int
403x11grab_read_packet(AVFormatContext *s1, AVPacket *pkt)
f58f4ce1 404{
aac105fc
EG
405 X11Grab *s = s1->priv_data;
406 Display *dpy = s->dpy;
407 XImage *image = s->image;
408 int x_off = s->x_off;
409 int y_off = s->y_off;
410
411 int64_t curtime, delay;
412 struct timespec ts;
413
414 /* Calculate the time of the next frame */
415 s->time_frame += int64_t_C(1000000);
416
417 /* wait based on the frame rate */
418 for(;;) {
419 curtime = av_gettime();
420 delay = s->time_frame * s->frame_rate_base / s->frame_rate - curtime;
421 if (delay <= 0) {
422 if (delay < int64_t_C(-1000000) * s->frame_rate_base / s->frame_rate) {
423 s->time_frame += int64_t_C(1000000);
424 }
425 break;
426 }
427 ts.tv_sec = delay / 1000000;
428 ts.tv_nsec = (delay % 1000000) * 1000;
429 nanosleep(&ts, NULL);
430 }
431
432 if (av_new_packet(pkt, s->frame_size) < 0) {
433 return AVERROR_IO;
434 }
435
436 pkt->pts = curtime & ((1LL << 48) - 1);
437
438 if(s->use_shm) {
439 if (!XShmGetImage(dpy, RootWindow(dpy, DefaultScreen(dpy)), image, x_off, y_off, AllPlanes)) {
440 av_log (s1, AV_LOG_INFO, "XShmGetImage() failed\n");
441 }
442 } else {
443 if (!XGetZPixmap(dpy, RootWindow(dpy, DefaultScreen(dpy)), image, x_off, y_off)) {
444 av_log (s1, AV_LOG_INFO, "XGetZPixmap() failed\n");
445 }
446 }
447
448 {
449 int pointer_x, pointer_y;
450 getCurrentPointer(s1, s, &pointer_x, &pointer_y);
451 paintMousePointer(s1, s, &pointer_x, &pointer_y, image);
452 }
453
454
455 /* XXX: avoid memcpy */
456 memcpy(pkt->data, image->data, s->frame_size);
457 return s->frame_size;
f58f4ce1
GP
458}
459
0235a311
EG
460static int
461x11grab_read_close(AVFormatContext *s1)
f58f4ce1 462{
aac105fc
EG
463 X11Grab *x11grab = s1->priv_data;
464
465 /* Detach cleanly from shared mem */
466 if (x11grab->use_shm) {
467 XShmDetach(x11grab->dpy, &x11grab->shminfo);
468 shmdt(x11grab->shminfo.shmaddr);
469 shmctl(x11grab->shminfo.shmid, IPC_RMID, NULL);
470 }
471
472 /* Destroy X11 image */
473 if (x11grab->image) {
474 XDestroyImage(x11grab->image);
475 x11grab->image = NULL;
476 }
477
478 /* Free X11 display */
479 XCloseDisplay(x11grab->dpy);
480 return 0;
f58f4ce1
GP
481}
482
2909f177
EG
483AVInputFormat x11_grab_device_demuxer =
484{
aac105fc
EG
485 "x11grab",
486 "X11grab",
487 sizeof(X11Grab),
488 NULL,
489 x11grab_read_header,
490 x11grab_read_packet,
491 x11grab_read_close,
492 .flags = AVFMT_NOFILE,
f58f4ce1 493};