x11grab.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440
  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. * Initializes 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 *param, *offset;
  89. param = av_strdup(s1->filename);
  90. offset = strchr(param, '+');
  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, param, x_off, y_off, ap->width, ap->height);
  97. dpy = XOpenDisplay(param);
  98. if(!dpy) {
  99. av_log(s1, AV_LOG_ERROR, "Could not open X display.\n");
  100. return AVERROR(EIO);
  101. }
  102. if (ap->width <= 0 || ap->height <= 0 || ap->time_base.den <= 0) {
  103. av_log(s1, AV_LOG_ERROR, "AVParameters don't have video size and/or rate. Use -s and -r.\n");
  104. return AVERROR(EIO);
  105. }
  106. st = av_new_stream(s1, 0);
  107. if (!st) {
  108. return AVERROR(ENOMEM);
  109. }
  110. av_set_pts_info(st, 64, 1, 1000000); /* 64 bits pts in us */
  111. use_shm = XShmQueryExtension(dpy);
  112. av_log(s1, AV_LOG_INFO, "shared memory extension %s found\n", use_shm ? "" : "not");
  113. if(use_shm) {
  114. int scr = XDefaultScreen(dpy);
  115. image = XShmCreateImage(dpy,
  116. DefaultVisual(dpy, scr),
  117. DefaultDepth(dpy, scr),
  118. ZPixmap,
  119. NULL,
  120. &x11grab->shminfo,
  121. ap->width, ap->height);
  122. x11grab->shminfo.shmid = shmget(IPC_PRIVATE,
  123. image->bytes_per_line * image->height,
  124. IPC_CREAT|0777);
  125. if (x11grab->shminfo.shmid == -1) {
  126. av_log(s1, AV_LOG_ERROR, "Fatal: Can't get shared memory!\n");
  127. return AVERROR(ENOMEM);
  128. }
  129. x11grab->shminfo.shmaddr = image->data = shmat(x11grab->shminfo.shmid, 0, 0);
  130. x11grab->shminfo.readOnly = False;
  131. if (!XShmAttach(dpy, &x11grab->shminfo)) {
  132. av_log(s1, AV_LOG_ERROR, "Fatal: Failed to attach shared memory!\n");
  133. /* needs some better error subroutine :) */
  134. return AVERROR(EIO);
  135. }
  136. } else {
  137. image = XGetImage(dpy, RootWindow(dpy, DefaultScreen(dpy)),
  138. x_off,y_off,
  139. ap->width,ap->height,
  140. AllPlanes, ZPixmap);
  141. }
  142. switch (image->bits_per_pixel) {
  143. case 8:
  144. av_log (s1, AV_LOG_DEBUG, "8 bit palette\n");
  145. input_pixfmt = PIX_FMT_PAL8;
  146. break;
  147. case 16:
  148. if ( image->red_mask == 0xf800 &&
  149. image->green_mask == 0x07e0 &&
  150. image->blue_mask == 0x001f ) {
  151. av_log (s1, AV_LOG_DEBUG, "16 bit RGB565\n");
  152. input_pixfmt = PIX_FMT_RGB565;
  153. } else if (image->red_mask == 0x7c00 &&
  154. image->green_mask == 0x03e0 &&
  155. image->blue_mask == 0x001f ) {
  156. av_log(s1, AV_LOG_DEBUG, "16 bit RGB555\n");
  157. input_pixfmt = PIX_FMT_RGB555;
  158. } else {
  159. av_log(s1, AV_LOG_ERROR, "RGB ordering at image depth %i not supported ... aborting\n", image->bits_per_pixel);
  160. 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);
  161. return AVERROR(EIO);
  162. }
  163. break;
  164. case 24:
  165. if ( image->red_mask == 0xff0000 &&
  166. image->green_mask == 0x00ff00 &&
  167. image->blue_mask == 0x0000ff ) {
  168. input_pixfmt = PIX_FMT_BGR24;
  169. } else if ( image->red_mask == 0x0000ff &&
  170. image->green_mask == 0x00ff00 &&
  171. image->blue_mask == 0xff0000 ) {
  172. input_pixfmt = PIX_FMT_RGB24;
  173. } else {
  174. av_log(s1, AV_LOG_ERROR,"rgb ordering at image depth %i not supported ... aborting\n", image->bits_per_pixel);
  175. 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);
  176. return AVERROR(EIO);
  177. }
  178. break;
  179. case 32:
  180. #if 0
  181. GetColorInfo (image, &c_info);
  182. if ( c_info.alpha_mask == 0xff000000 && image->green_mask == 0x0000ff00) {
  183. /* byte order is relevant here, not endianness
  184. * endianness is handled by avcodec, but atm no such thing
  185. * as having ABGR, instead of ARGB in a word. Since we
  186. * need this for Solaris/SPARC, but need to do the conversion
  187. * for every frame we do it outside of this loop, cf. below
  188. * this matches both ARGB32 and ABGR32 */
  189. input_pixfmt = PIX_FMT_ARGB32;
  190. } else {
  191. av_log(s1, AV_LOG_ERROR,"image depth %i not supported ... aborting\n", image->bits_per_pixel);
  192. return AVERROR(EIO);
  193. }
  194. #endif
  195. input_pixfmt = PIX_FMT_RGB32;
  196. break;
  197. default:
  198. av_log(s1, AV_LOG_ERROR, "image depth %i not supported ... aborting\n", image->bits_per_pixel);
  199. return -1;
  200. }
  201. x11grab->frame_size = ap->width * ap->height * image->bits_per_pixel/8;
  202. x11grab->dpy = dpy;
  203. x11grab->width = ap->width;
  204. x11grab->height = ap->height;
  205. x11grab->time_base = ap->time_base;
  206. x11grab->time_frame = av_gettime() / av_q2d(ap->time_base);
  207. x11grab->x_off = x_off;
  208. x11grab->y_off = y_off;
  209. x11grab->image = image;
  210. x11grab->use_shm = use_shm;
  211. st->codec->codec_type = AVMEDIA_TYPE_VIDEO;
  212. st->codec->codec_id = CODEC_ID_RAWVIDEO;
  213. st->codec->width = ap->width;
  214. st->codec->height = ap->height;
  215. st->codec->pix_fmt = input_pixfmt;
  216. st->codec->time_base = ap->time_base;
  217. st->codec->bit_rate = x11grab->frame_size * 1/av_q2d(ap->time_base) * 8;
  218. return 0;
  219. }
  220. /**
  221. * Paints a mouse pointer in an X11 image.
  222. *
  223. * @param image image to paint the mouse pointer to
  224. * @param s context used to retrieve original grabbing rectangle
  225. * coordinates
  226. * @param x Mouse pointer coordinate
  227. * @param y Mouse pointer coordinate
  228. */
  229. static void
  230. paint_mouse_pointer(XImage *image, struct x11_grab *s)
  231. {
  232. int x_off = s->x_off;
  233. int y_off = s->y_off;
  234. int width = s->width;
  235. int height = s->height;
  236. Display *dpy = s->dpy;
  237. XFixesCursorImage *xcim;
  238. int x, y;
  239. int line, column;
  240. int to_line, to_column;
  241. int image_addr, xcim_addr;
  242. xcim = XFixesGetCursorImage(dpy);;
  243. x = xcim->x - xcim->xhot;
  244. y = xcim->y - xcim->yhot;
  245. to_line = FFMIN((y + xcim->height), (height + y_off));
  246. to_column = FFMIN((x + xcim->width), (width + x_off));
  247. for (line = FFMAX(y, y_off); line < to_line; line++) {
  248. for (column = FFMAX(x, x_off); column < to_column; column++) {
  249. xcim_addr = (line - y) * xcim->width + column - x;
  250. if ((unsigned char)(xcim->pixels[xcim_addr] >> 24) != 0) { // skip fully transparent pixel
  251. image_addr = ((line - y_off) * width + column - x_off) * 4;
  252. image->data[image_addr] = (unsigned char)(xcim->pixels[xcim_addr] >> 0);
  253. image->data[image_addr+1] = (unsigned char)(xcim->pixels[xcim_addr] >> 8);
  254. image->data[image_addr+2] = (unsigned char)(xcim->pixels[xcim_addr] >> 16);
  255. }
  256. }
  257. }
  258. XFree(xcim);
  259. xcim = NULL;
  260. }
  261. /**
  262. * Reads new data in the image structure.
  263. *
  264. * @param dpy X11 display to grab from
  265. * @param d
  266. * @param image Image where the grab will be put
  267. * @param x Top-Left grabbing rectangle horizontal coordinate
  268. * @param y Top-Left grabbing rectangle vertical coordinate
  269. * @return 0 if error, !0 if successful
  270. */
  271. static int
  272. xget_zpixmap(Display *dpy, Drawable d, XImage *image, int x, int y)
  273. {
  274. xGetImageReply rep;
  275. xGetImageReq *req;
  276. long nbytes;
  277. if (!image) {
  278. return 0;
  279. }
  280. LockDisplay(dpy);
  281. GetReq(GetImage, req);
  282. /* First set up the standard stuff in the request */
  283. req->drawable = d;
  284. req->x = x;
  285. req->y = y;
  286. req->width = image->width;
  287. req->height = image->height;
  288. req->planeMask = (unsigned int)AllPlanes;
  289. req->format = ZPixmap;
  290. if (!_XReply(dpy, (xReply *)&rep, 0, xFalse) || !rep.length) {
  291. UnlockDisplay(dpy);
  292. SyncHandle();
  293. return 0;
  294. }
  295. nbytes = (long)rep.length << 2;
  296. _XReadPad(dpy, image->data, nbytes);
  297. UnlockDisplay(dpy);
  298. SyncHandle();
  299. return 1;
  300. }
  301. /**
  302. * Grabs a frame from x11 (public device demuxer API).
  303. *
  304. * @param s1 Context from avformat core
  305. * @param pkt Packet holding the brabbed frame
  306. * @return frame size in bytes
  307. */
  308. static int
  309. x11grab_read_packet(AVFormatContext *s1, AVPacket *pkt)
  310. {
  311. struct x11_grab *s = s1->priv_data;
  312. Display *dpy = s->dpy;
  313. XImage *image = s->image;
  314. int x_off = s->x_off;
  315. int y_off = s->y_off;
  316. int64_t curtime, delay;
  317. struct timespec ts;
  318. /* Calculate the time of the next frame */
  319. s->time_frame += INT64_C(1000000);
  320. /* wait based on the frame rate */
  321. for(;;) {
  322. curtime = av_gettime();
  323. delay = s->time_frame * av_q2d(s->time_base) - curtime;
  324. if (delay <= 0) {
  325. if (delay < INT64_C(-1000000) * av_q2d(s->time_base)) {
  326. s->time_frame += INT64_C(1000000);
  327. }
  328. break;
  329. }
  330. ts.tv_sec = delay / 1000000;
  331. ts.tv_nsec = (delay % 1000000) * 1000;
  332. nanosleep(&ts, NULL);
  333. }
  334. if (av_new_packet(pkt, s->frame_size) < 0) {
  335. return AVERROR(EIO);
  336. }
  337. pkt->pts = curtime;
  338. if(s->use_shm) {
  339. if (!XShmGetImage(dpy, RootWindow(dpy, DefaultScreen(dpy)), image, x_off, y_off, AllPlanes)) {
  340. av_log (s1, AV_LOG_INFO, "XShmGetImage() failed\n");
  341. }
  342. } else {
  343. if (!xget_zpixmap(dpy, RootWindow(dpy, DefaultScreen(dpy)), image, x_off, y_off)) {
  344. av_log (s1, AV_LOG_INFO, "XGetZPixmap() failed\n");
  345. }
  346. }
  347. if(!s->nomouse){
  348. paint_mouse_pointer(image, s);
  349. }
  350. /* XXX: avoid memcpy */
  351. memcpy(pkt->data, image->data, s->frame_size);
  352. return s->frame_size;
  353. }
  354. /**
  355. * Closes x11 frame grabber (public device demuxer API).
  356. *
  357. * @param s1 Context from avformat core
  358. * @return 0 success, !0 failure
  359. */
  360. static int
  361. x11grab_read_close(AVFormatContext *s1)
  362. {
  363. struct x11_grab *x11grab = s1->priv_data;
  364. /* Detach cleanly from shared mem */
  365. if (x11grab->use_shm) {
  366. XShmDetach(x11grab->dpy, &x11grab->shminfo);
  367. shmdt(x11grab->shminfo.shmaddr);
  368. shmctl(x11grab->shminfo.shmid, IPC_RMID, NULL);
  369. }
  370. /* Destroy X11 image */
  371. if (x11grab->image) {
  372. XDestroyImage(x11grab->image);
  373. x11grab->image = NULL;
  374. }
  375. /* Free X11 display */
  376. XCloseDisplay(x11grab->dpy);
  377. return 0;
  378. }
  379. /** x11 grabber device demuxer declaration */
  380. AVInputFormat x11_grab_device_demuxer =
  381. {
  382. "x11grab",
  383. NULL_IF_CONFIG_SMALL("X11grab"),
  384. sizeof(struct x11_grab),
  385. NULL,
  386. x11grab_read_header,
  387. x11grab_read_packet,
  388. x11grab_read_close,
  389. .flags = AVFMT_NOFILE,
  390. };