You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

454 lines
14KB

  1. /*
  2. * X11 video grab interface
  3. *
  4. * This file is part of Libav.
  5. *
  6. * Libav integration:
  7. * Copyright (C) 2006 Clemens Fruhwirth <clemens@endorphin.org>
  8. * Edouard Gomez <ed.gomez@free.fr>
  9. *
  10. * This file contains code from grab.c:
  11. * Copyright (c) 2000-2001 Fabrice Bellard
  12. *
  13. * This file contains code from the xvidcap project:
  14. * Copyright (C) 1997-1998 Rasca, Berlin
  15. * 2003-2004 Karl H. Beckers, Frankfurt
  16. *
  17. * Libav is free software; you can redistribute it and/or modify
  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. *
  22. * Libav is distributed in the hope that it will be useful,
  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. *
  27. * You should have received a copy of the GNU General Public License
  28. * along with Libav; if not, write to the Free Software
  29. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  30. */
  31. /**
  32. * @file
  33. * X11 frame device demuxer by Clemens Fruhwirth <clemens@endorphin.org>
  34. * and Edouard Gomez <ed.gomez@free.fr>.
  35. */
  36. #include "config.h"
  37. #include "libavformat/avformat.h"
  38. #include <time.h>
  39. #include <X11/X.h>
  40. #include <X11/Xlib.h>
  41. #include <X11/Xlibint.h>
  42. #include <X11/Xproto.h>
  43. #include <X11/Xutil.h>
  44. #include <sys/shm.h>
  45. #include <X11/extensions/XShm.h>
  46. #include <X11/extensions/Xfixes.h>
  47. /**
  48. * X11 Device Demuxer context
  49. */
  50. struct x11_grab
  51. {
  52. int frame_size; /**< Size in bytes of a grabbed frame */
  53. AVRational time_base; /**< Time base */
  54. int64_t time_frame; /**< Current time */
  55. int height; /**< Height of the grab frame */
  56. int width; /**< Width of the grab frame */
  57. int x_off; /**< Horizontal top-left corner coordinate */
  58. int y_off; /**< Vertical top-left corner coordinate */
  59. Display *dpy; /**< X11 display from which x11grab grabs frames */
  60. XImage *image; /**< X11 image holding the grab */
  61. int use_shm; /**< !0 when using XShm extension */
  62. XShmSegmentInfo shminfo; /**< When using XShm, keeps track of XShm infos */
  63. int nomouse;
  64. };
  65. /**
  66. * Initialize the x11 grab device demuxer (public device demuxer API).
  67. *
  68. * @param s1 Context from avformat core
  69. * @param ap Parameters from avformat core
  70. * @return <ul>
  71. * <li>AVERROR(ENOMEM) no memory left</li>
  72. * <li>AVERROR(EIO) other failure case</li>
  73. * <li>0 success</li>
  74. * </ul>
  75. */
  76. static int
  77. x11grab_read_header(AVFormatContext *s1, AVFormatParameters *ap)
  78. {
  79. struct x11_grab *x11grab = s1->priv_data;
  80. Display *dpy;
  81. AVStream *st = NULL;
  82. enum PixelFormat input_pixfmt;
  83. XImage *image;
  84. int x_off = 0;
  85. int y_off = 0;
  86. int use_shm;
  87. char *param, *offset;
  88. param = av_strdup(s1->filename);
  89. offset = strchr(param, '+');
  90. if (offset) {
  91. sscanf(offset, "%d,%d", &x_off, &y_off);
  92. x11grab->nomouse= strstr(offset, "nomouse");
  93. *offset= 0;
  94. }
  95. 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, ap->width, ap->height);
  96. dpy = XOpenDisplay(param);
  97. if(!dpy) {
  98. av_log(s1, AV_LOG_ERROR, "Could not open X display.\n");
  99. return AVERROR(EIO);
  100. }
  101. if (ap->width <= 0 || ap->height <= 0 || ap->time_base.den <= 0) {
  102. av_log(s1, AV_LOG_ERROR, "AVParameters don't have video size and/or rate. Use -s and -r.\n");
  103. return AVERROR(EIO);
  104. }
  105. st = av_new_stream(s1, 0);
  106. if (!st) {
  107. return AVERROR(ENOMEM);
  108. }
  109. av_set_pts_info(st, 64, 1, 1000000); /* 64 bits pts in us */
  110. use_shm = XShmQueryExtension(dpy);
  111. av_log(s1, AV_LOG_INFO, "shared memory extension %s found\n", use_shm ? "" : "not");
  112. if(use_shm) {
  113. int scr = XDefaultScreen(dpy);
  114. image = XShmCreateImage(dpy,
  115. DefaultVisual(dpy, scr),
  116. DefaultDepth(dpy, scr),
  117. ZPixmap,
  118. NULL,
  119. &x11grab->shminfo,
  120. ap->width, ap->height);
  121. x11grab->shminfo.shmid = shmget(IPC_PRIVATE,
  122. image->bytes_per_line * image->height,
  123. IPC_CREAT|0777);
  124. if (x11grab->shminfo.shmid == -1) {
  125. av_log(s1, AV_LOG_ERROR, "Fatal: Can't get shared memory!\n");
  126. return AVERROR(ENOMEM);
  127. }
  128. x11grab->shminfo.shmaddr = image->data = shmat(x11grab->shminfo.shmid, 0, 0);
  129. x11grab->shminfo.readOnly = False;
  130. if (!XShmAttach(dpy, &x11grab->shminfo)) {
  131. av_log(s1, AV_LOG_ERROR, "Fatal: Failed to attach shared memory!\n");
  132. /* needs some better error subroutine :) */
  133. return AVERROR(EIO);
  134. }
  135. } else {
  136. image = XGetImage(dpy, RootWindow(dpy, DefaultScreen(dpy)),
  137. x_off,y_off,
  138. ap->width,ap->height,
  139. AllPlanes, ZPixmap);
  140. }
  141. switch (image->bits_per_pixel) {
  142. case 8:
  143. av_log (s1, AV_LOG_DEBUG, "8 bit palette\n");
  144. input_pixfmt = PIX_FMT_PAL8;
  145. break;
  146. case 16:
  147. if ( image->red_mask == 0xf800 &&
  148. image->green_mask == 0x07e0 &&
  149. image->blue_mask == 0x001f ) {
  150. av_log (s1, AV_LOG_DEBUG, "16 bit RGB565\n");
  151. input_pixfmt = PIX_FMT_RGB565;
  152. } else if (image->red_mask == 0x7c00 &&
  153. image->green_mask == 0x03e0 &&
  154. image->blue_mask == 0x001f ) {
  155. av_log(s1, AV_LOG_DEBUG, "16 bit RGB555\n");
  156. input_pixfmt = PIX_FMT_RGB555;
  157. } else {
  158. av_log(s1, AV_LOG_ERROR, "RGB ordering at image depth %i not supported ... aborting\n", image->bits_per_pixel);
  159. 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);
  160. return AVERROR(EIO);
  161. }
  162. break;
  163. case 24:
  164. if ( image->red_mask == 0xff0000 &&
  165. image->green_mask == 0x00ff00 &&
  166. image->blue_mask == 0x0000ff ) {
  167. input_pixfmt = PIX_FMT_BGR24;
  168. } else if ( image->red_mask == 0x0000ff &&
  169. image->green_mask == 0x00ff00 &&
  170. image->blue_mask == 0xff0000 ) {
  171. input_pixfmt = PIX_FMT_RGB24;
  172. } else {
  173. av_log(s1, AV_LOG_ERROR,"rgb ordering at image depth %i not supported ... aborting\n", image->bits_per_pixel);
  174. 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);
  175. return AVERROR(EIO);
  176. }
  177. break;
  178. case 32:
  179. #if 0
  180. GetColorInfo (image, &c_info);
  181. if ( c_info.alpha_mask == 0xff000000 && image->green_mask == 0x0000ff00) {
  182. /* byte order is relevant here, not endianness
  183. * endianness is handled by avcodec, but atm no such thing
  184. * as having ABGR, instead of ARGB in a word. Since we
  185. * need this for Solaris/SPARC, but need to do the conversion
  186. * for every frame we do it outside of this loop, cf. below
  187. * this matches both ARGB32 and ABGR32 */
  188. input_pixfmt = PIX_FMT_ARGB32;
  189. } else {
  190. av_log(s1, AV_LOG_ERROR,"image depth %i not supported ... aborting\n", image->bits_per_pixel);
  191. return AVERROR(EIO);
  192. }
  193. #endif
  194. input_pixfmt = PIX_FMT_RGB32;
  195. break;
  196. default:
  197. av_log(s1, AV_LOG_ERROR, "image depth %i not supported ... aborting\n", image->bits_per_pixel);
  198. return -1;
  199. }
  200. x11grab->frame_size = ap->width * ap->height * image->bits_per_pixel/8;
  201. x11grab->dpy = dpy;
  202. x11grab->width = ap->width;
  203. x11grab->height = ap->height;
  204. x11grab->time_base = ap->time_base;
  205. x11grab->time_frame = av_gettime() / av_q2d(ap->time_base);
  206. x11grab->x_off = x_off;
  207. x11grab->y_off = y_off;
  208. x11grab->image = image;
  209. x11grab->use_shm = use_shm;
  210. st->codec->codec_type = AVMEDIA_TYPE_VIDEO;
  211. st->codec->codec_id = CODEC_ID_RAWVIDEO;
  212. st->codec->width = ap->width;
  213. st->codec->height = ap->height;
  214. st->codec->pix_fmt = input_pixfmt;
  215. st->codec->time_base = ap->time_base;
  216. st->codec->bit_rate = x11grab->frame_size * 1/av_q2d(ap->time_base) * 8;
  217. return 0;
  218. }
  219. /**
  220. * Paint a mouse pointer in an X11 image.
  221. *
  222. * @param image image to paint the mouse pointer to
  223. * @param s context used to retrieve original grabbing rectangle
  224. * coordinates
  225. */
  226. static void
  227. paint_mouse_pointer(XImage *image, struct x11_grab *s)
  228. {
  229. int x_off = s->x_off;
  230. int y_off = s->y_off;
  231. int width = s->width;
  232. int height = s->height;
  233. Display *dpy = s->dpy;
  234. XFixesCursorImage *xcim;
  235. int x, y;
  236. int line, column;
  237. int to_line, to_column;
  238. int pixstride = image->bits_per_pixel >> 3;
  239. /* Warning: in its insanity, xlib provides unsigned image data through a
  240. * char* pointer, so we have to make it uint8_t to make things not break.
  241. * Anyone who performs further investigation of the xlib API likely risks
  242. * permanent brain damage. */
  243. uint8_t *pix = image->data;
  244. /* Code doesn't currently support 16-bit or PAL8 */
  245. if (image->bits_per_pixel != 24 && image->bits_per_pixel != 32)
  246. return;
  247. xcim = XFixesGetCursorImage(dpy);
  248. x = xcim->x - xcim->xhot;
  249. y = xcim->y - xcim->yhot;
  250. to_line = FFMIN((y + xcim->height), (height + y_off));
  251. to_column = FFMIN((x + xcim->width), (width + x_off));
  252. for (line = FFMAX(y, y_off); line < to_line; line++) {
  253. for (column = FFMAX(x, x_off); column < to_column; column++) {
  254. int xcim_addr = (line - y) * xcim->width + column - x;
  255. int image_addr = ((line - y_off) * width + column - x_off) * pixstride;
  256. int r = (uint8_t)(xcim->pixels[xcim_addr] >> 0);
  257. int g = (uint8_t)(xcim->pixels[xcim_addr] >> 8);
  258. int b = (uint8_t)(xcim->pixels[xcim_addr] >> 16);
  259. int a = (uint8_t)(xcim->pixels[xcim_addr] >> 24);
  260. if (a == 255) {
  261. pix[image_addr+0] = r;
  262. pix[image_addr+1] = g;
  263. pix[image_addr+2] = b;
  264. } else if (a) {
  265. /* pixel values from XFixesGetCursorImage come premultiplied by alpha */
  266. pix[image_addr+0] = r + (pix[image_addr+0]*(255-a) + 255/2) / 255;
  267. pix[image_addr+1] = g + (pix[image_addr+1]*(255-a) + 255/2) / 255;
  268. pix[image_addr+2] = b + (pix[image_addr+2]*(255-a) + 255/2) / 255;
  269. }
  270. }
  271. }
  272. XFree(xcim);
  273. xcim = NULL;
  274. }
  275. /**
  276. * Read new data in the image structure.
  277. *
  278. * @param dpy X11 display to grab from
  279. * @param d
  280. * @param image Image where the grab will be put
  281. * @param x Top-Left grabbing rectangle horizontal coordinate
  282. * @param y Top-Left grabbing rectangle vertical coordinate
  283. * @return 0 if error, !0 if successful
  284. */
  285. static int
  286. xget_zpixmap(Display *dpy, Drawable d, XImage *image, int x, int y)
  287. {
  288. xGetImageReply rep;
  289. xGetImageReq *req;
  290. long nbytes;
  291. if (!image) {
  292. return 0;
  293. }
  294. LockDisplay(dpy);
  295. GetReq(GetImage, req);
  296. /* First set up the standard stuff in the request */
  297. req->drawable = d;
  298. req->x = x;
  299. req->y = y;
  300. req->width = image->width;
  301. req->height = image->height;
  302. req->planeMask = (unsigned int)AllPlanes;
  303. req->format = ZPixmap;
  304. if (!_XReply(dpy, (xReply *)&rep, 0, xFalse) || !rep.length) {
  305. UnlockDisplay(dpy);
  306. SyncHandle();
  307. return 0;
  308. }
  309. nbytes = (long)rep.length << 2;
  310. _XReadPad(dpy, image->data, nbytes);
  311. UnlockDisplay(dpy);
  312. SyncHandle();
  313. return 1;
  314. }
  315. /**
  316. * Grab a frame from x11 (public device demuxer API).
  317. *
  318. * @param s1 Context from avformat core
  319. * @param pkt Packet holding the brabbed frame
  320. * @return frame size in bytes
  321. */
  322. static int
  323. x11grab_read_packet(AVFormatContext *s1, AVPacket *pkt)
  324. {
  325. struct x11_grab *s = s1->priv_data;
  326. Display *dpy = s->dpy;
  327. XImage *image = s->image;
  328. int x_off = s->x_off;
  329. int y_off = s->y_off;
  330. int64_t curtime, delay;
  331. struct timespec ts;
  332. /* Calculate the time of the next frame */
  333. s->time_frame += INT64_C(1000000);
  334. /* wait based on the frame rate */
  335. for(;;) {
  336. curtime = av_gettime();
  337. delay = s->time_frame * av_q2d(s->time_base) - curtime;
  338. if (delay <= 0) {
  339. if (delay < INT64_C(-1000000) * av_q2d(s->time_base)) {
  340. s->time_frame += INT64_C(1000000);
  341. }
  342. break;
  343. }
  344. ts.tv_sec = delay / 1000000;
  345. ts.tv_nsec = (delay % 1000000) * 1000;
  346. nanosleep(&ts, NULL);
  347. }
  348. if (av_new_packet(pkt, s->frame_size) < 0) {
  349. return AVERROR(EIO);
  350. }
  351. pkt->pts = curtime;
  352. if(s->use_shm) {
  353. if (!XShmGetImage(dpy, RootWindow(dpy, DefaultScreen(dpy)), image, x_off, y_off, AllPlanes)) {
  354. av_log (s1, AV_LOG_INFO, "XShmGetImage() failed\n");
  355. }
  356. } else {
  357. if (!xget_zpixmap(dpy, RootWindow(dpy, DefaultScreen(dpy)), image, x_off, y_off)) {
  358. av_log (s1, AV_LOG_INFO, "XGetZPixmap() failed\n");
  359. }
  360. }
  361. if(!s->nomouse){
  362. paint_mouse_pointer(image, s);
  363. }
  364. /* XXX: avoid memcpy */
  365. memcpy(pkt->data, image->data, s->frame_size);
  366. return s->frame_size;
  367. }
  368. /**
  369. * Close x11 frame grabber (public device demuxer API).
  370. *
  371. * @param s1 Context from avformat core
  372. * @return 0 success, !0 failure
  373. */
  374. static int
  375. x11grab_read_close(AVFormatContext *s1)
  376. {
  377. struct x11_grab *x11grab = s1->priv_data;
  378. /* Detach cleanly from shared mem */
  379. if (x11grab->use_shm) {
  380. XShmDetach(x11grab->dpy, &x11grab->shminfo);
  381. shmdt(x11grab->shminfo.shmaddr);
  382. shmctl(x11grab->shminfo.shmid, IPC_RMID, NULL);
  383. }
  384. /* Destroy X11 image */
  385. if (x11grab->image) {
  386. XDestroyImage(x11grab->image);
  387. x11grab->image = NULL;
  388. }
  389. /* Free X11 display */
  390. XCloseDisplay(x11grab->dpy);
  391. return 0;
  392. }
  393. /** x11 grabber device demuxer declaration */
  394. AVInputFormat ff_x11_grab_device_demuxer =
  395. {
  396. "x11grab",
  397. NULL_IF_CONFIG_SMALL("X11grab"),
  398. sizeof(struct x11_grab),
  399. NULL,
  400. x11grab_read_header,
  401. x11grab_read_packet,
  402. x11grab_read_close,
  403. .flags = AVFMT_NOFILE,
  404. };