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.

453 lines
14KB

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