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