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
13KB

  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. typedef struct
  61. {
  62. Display *dpy;
  63. int frame_format;
  64. int frame_size;
  65. int frame_rate;
  66. int frame_rate_base;
  67. int64_t time_frame;
  68. int height;
  69. int width;
  70. int x_off;
  71. int y_off;
  72. XImage *image;
  73. int use_shm;
  74. XShmSegmentInfo shminfo;
  75. int mouse_wanted;
  76. } X11Grab;
  77. static int
  78. x11grab_read_header(AVFormatContext *s1, AVFormatParameters *ap)
  79. {
  80. X11Grab *x11grab = s1->priv_data;
  81. Display *dpy;
  82. AVStream *st = NULL;
  83. int width, height;
  84. int frame_rate, frame_rate_base, frame_size;
  85. int input_pixfmt;
  86. XImage *image;
  87. int x_off=0; int y_off = 0;
  88. int use_shm;
  89. dpy = XOpenDisplay(NULL);
  90. if(!dpy) {
  91. goto fail;
  92. }
  93. sscanf(ap->device, "x11:%d,%d", &x_off, &y_off);
  94. 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);
  95. if (!ap || ap->width <= 0 || ap->height <= 0 || ap->time_base.den <= 0) {
  96. av_log(s1, AV_LOG_ERROR, "AVParameters don't have any video size. Use -s.\n");
  97. return AVERROR_IO;
  98. }
  99. width = ap->width;
  100. height = ap->height;
  101. frame_rate = ap->time_base.den;
  102. frame_rate_base = ap->time_base.num;
  103. st = av_new_stream(s1, 0);
  104. if (!st) {
  105. return -ENOMEM;
  106. }
  107. av_set_pts_info(st, 48, 1, 1000000); /* 48 bits pts in us */
  108. use_shm = XShmQueryExtension(dpy);
  109. av_log(s1, AV_LOG_INFO, "shared memory extension %s\n", use_shm ? "found" : "not found");
  110. if(use_shm) {
  111. int scr = XDefaultScreen(dpy);
  112. image = XShmCreateImage(dpy,
  113. DefaultVisual(dpy,scr),
  114. DefaultDepth(dpy,scr),
  115. ZPixmap,
  116. NULL,
  117. &x11grab->shminfo,
  118. ap->width, ap->height);
  119. x11grab->shminfo.shmid = shmget(IPC_PRIVATE,
  120. image->bytes_per_line * image->height,
  121. IPC_CREAT|0777);
  122. if (x11grab->shminfo.shmid == -1) {
  123. av_log(s1, AV_LOG_ERROR, "Fatal: Can't get shared memory!\n");
  124. return -ENOMEM;
  125. }
  126. x11grab->shminfo.shmaddr = image->data = shmat(x11grab->shminfo.shmid, 0, 0);
  127. x11grab->shminfo.readOnly = False;
  128. if (!XShmAttach(dpy, &x11grab->shminfo)) {
  129. av_log(s1, AV_LOG_ERROR, "Fatal: Failed to attach shared memory!\n");
  130. /* needs some better error subroutine :) */
  131. return AVERROR_IO;
  132. }
  133. } else {
  134. image = XGetImage(dpy, RootWindow(dpy, DefaultScreen(dpy)),
  135. x_off,y_off,
  136. ap->width,ap->height,
  137. AllPlanes, ZPixmap);
  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:
  175. #if 0
  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. }
  189. #endif
  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. frame_size = width * height * image->bits_per_pixel/8;
  197. x11grab->frame_size = frame_size;
  198. x11grab->dpy = dpy;
  199. x11grab->width = ap->width;
  200. x11grab->height = ap->height;
  201. x11grab->frame_rate = frame_rate;
  202. x11grab->frame_rate_base = frame_rate_base;
  203. x11grab->time_frame = av_gettime() * frame_rate / frame_rate_base;
  204. x11grab->x_off = x_off;
  205. x11grab->y_off = y_off;
  206. x11grab->image = image;
  207. x11grab->use_shm = use_shm;
  208. x11grab->mouse_wanted = 1;
  209. st->codec->codec_type = CODEC_TYPE_VIDEO;
  210. st->codec->codec_id = CODEC_ID_RAWVIDEO;
  211. st->codec->width = width;
  212. st->codec->height = height;
  213. st->codec->pix_fmt = input_pixfmt;
  214. st->codec->time_base.den = frame_rate;
  215. st->codec->time_base.num = frame_rate_base;
  216. st->codec->bit_rate = frame_size * 1/av_q2d(st->codec->time_base) * 8;
  217. return 0;
  218. fail:
  219. av_free(st);
  220. return AVERROR_IO;
  221. }
  222. static uint16_t mousePointerBlack[] =
  223. {
  224. 0, 49152, 40960, 36864, 34816, 33792, 33280, 33024, 32896, 32832,
  225. 33728, 37376, 43264, 51456, 1152, 1152, 576, 576, 448, 0
  226. };
  227. static uint16_t mousePointerWhite[] =
  228. {
  229. 0, 0, 16384, 24576, 28672, 30720, 31744, 32256, 32512, 32640, 31744,
  230. 27648, 17920, 1536, 768, 768, 384, 384, 0, 0
  231. };
  232. static void
  233. getCurrentPointer(AVFormatContext *s1, X11Grab *s, int *x, int *y)
  234. {
  235. Window mrootwindow, childwindow;
  236. int dummy;
  237. Display *dpy = s->dpy;
  238. mrootwindow = DefaultRootWindow(dpy);
  239. if (XQueryPointer(dpy, mrootwindow, &mrootwindow, &childwindow,
  240. x, y, &dummy, &dummy, (unsigned int*)&dummy)) {
  241. } else {
  242. av_log(s1, AV_LOG_INFO, "couldn't find mouse pointer\n");
  243. *x = -1;
  244. *y = -1;
  245. }
  246. }
  247. #define DRAW_CURSOR_TEMPLATE(type_t) \
  248. do { \
  249. type_t *cursor; \
  250. int width_cursor; \
  251. uint16_t bm_b, bm_w, mask; \
  252. \
  253. for (line = 0; line < min(20, (y_off + height) - *y); line++ ) { \
  254. if (s->mouse_wanted == 1) { \
  255. bm_b = mousePointerBlack[line]; \
  256. bm_w = mousePointerWhite[line]; \
  257. } else { \
  258. bm_b = mousePointerWhite[line]; \
  259. bm_w = mousePointerBlack[line]; \
  260. } \
  261. mask = ( 0x0001 << 15 ); \
  262. \
  263. for (cursor = (type_t*) im_data, width_cursor = 0; \
  264. ((width_cursor + *x) < (width + x_off) && width_cursor < 16); \
  265. cursor++, width_cursor++) { \
  266. if ( ( bm_b & mask ) > 0 ) { \
  267. *cursor &= ((~ image->red_mask) & (~ image->green_mask) & (~image->blue_mask )); \
  268. } else if ( ( bm_w & mask ) > 0 ) { \
  269. *cursor |= (image->red_mask | image->green_mask | image->blue_mask ); \
  270. } \
  271. mask >>= 1; \
  272. } \
  273. im_data += image->bytes_per_line; \
  274. } \
  275. } while (0)
  276. static void
  277. paintMousePointer(AVFormatContext *s1, X11Grab *s, int *x, int *y, XImage *image)
  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. if ( (*x - x_off) >= 0 && *x < (width + x_off)
  284. && (*y - y_off) >= 0 && *y < (height + y_off) ) {
  285. int line;
  286. uint8_t *im_data = (uint8_t*)image->data;
  287. im_data += (image->bytes_per_line * (*y - y_off)); // shift to right line
  288. im_data += (image->bits_per_pixel / 8 * (*x - x_off)); // shift to right pixel
  289. switch(image->bits_per_pixel) {
  290. case 32:
  291. DRAW_CURSOR_TEMPLATE(uint32_t);
  292. break;
  293. case 16:
  294. DRAW_CURSOR_TEMPLATE(uint16_t);
  295. break;
  296. default:
  297. /* XXX: How do we deal with 24bit and 8bit modes ? */
  298. break;
  299. }
  300. }
  301. }
  302. /*
  303. * just read new data in the image structure, the image
  304. * structure inclusive the data area must be allocated before
  305. */
  306. static int
  307. XGetZPixmap(Display *dpy, Drawable d, XImage *image, int x, int y)
  308. {
  309. xGetImageReply rep;
  310. xGetImageReq *req;
  311. long nbytes;
  312. if (!image) {
  313. return False;
  314. }
  315. LockDisplay(dpy);
  316. GetReq(GetImage, req);
  317. /* First set up the standard stuff in the request */
  318. req->drawable = d;
  319. req->x = x;
  320. req->y = y;
  321. req->width = image->width;
  322. req->height = image->height;
  323. req->planeMask = (unsigned int)AllPlanes;
  324. req->format = ZPixmap;
  325. if (!_XReply(dpy, (xReply *) &rep, 0, xFalse) || !rep.length) {
  326. UnlockDisplay(dpy);
  327. SyncHandle();
  328. return False;
  329. }
  330. nbytes = (long)rep.length << 2;
  331. _XReadPad(dpy, image->data, nbytes);
  332. UnlockDisplay(dpy);
  333. SyncHandle();
  334. return True;
  335. }
  336. static int x11grab_read_packet(AVFormatContext *s1, AVPacket *pkt)
  337. {
  338. X11Grab *s = s1->priv_data;
  339. Display *dpy = s->dpy;
  340. XImage *image = s->image;
  341. int x_off = s->x_off;
  342. int y_off = s->y_off;
  343. int64_t curtime, delay;
  344. struct timespec ts;
  345. /* Calculate the time of the next frame */
  346. s->time_frame += int64_t_C(1000000);
  347. /* wait based on the frame rate */
  348. for(;;) {
  349. curtime = av_gettime();
  350. delay = s->time_frame * s->frame_rate_base / s->frame_rate - curtime;
  351. if (delay <= 0) {
  352. if (delay < int64_t_C(-1000000) * s->frame_rate_base / s->frame_rate) {
  353. s->time_frame += int64_t_C(1000000);
  354. }
  355. break;
  356. }
  357. ts.tv_sec = delay / 1000000;
  358. ts.tv_nsec = (delay % 1000000) * 1000;
  359. nanosleep(&ts, NULL);
  360. }
  361. if (av_new_packet(pkt, s->frame_size) < 0) {
  362. return AVERROR_IO;
  363. }
  364. pkt->pts = curtime & ((1LL << 48) - 1);
  365. if(s->use_shm) {
  366. if (!XShmGetImage(dpy, RootWindow(dpy, DefaultScreen(dpy)), image, x_off, y_off, AllPlanes)) {
  367. av_log (s1, AV_LOG_INFO, "XShmGetImage() failed\n");
  368. }
  369. } else {
  370. if (!XGetZPixmap(dpy, RootWindow(dpy, DefaultScreen(dpy)), image, x_off, y_off)) {
  371. av_log (s1, AV_LOG_INFO, "XGetZPixmap() failed\n");
  372. }
  373. }
  374. {
  375. int pointer_x, pointer_y;
  376. getCurrentPointer(s1, s, &pointer_x, &pointer_y);
  377. paintMousePointer(s1, s, &pointer_x, &pointer_y, image);
  378. }
  379. /* XXX: avoid memcpy */
  380. memcpy(pkt->data, image->data, s->frame_size);
  381. return s->frame_size;
  382. }
  383. static int x11grab_read_close(AVFormatContext *s1)
  384. {
  385. X11Grab *x11grab = s1->priv_data;
  386. /* Free shared mem if necessary */
  387. if (x11grab->use_shm) {
  388. shmdt(x11grab->shminfo.shmaddr);
  389. shmctl(x11grab->shminfo.shmid, IPC_RMID, NULL);
  390. }
  391. /* Free X11 display */
  392. XCloseDisplay(x11grab->dpy);
  393. return 0;
  394. }
  395. AVInputFormat x11_grab_device_demuxer =
  396. {
  397. "x11grab",
  398. "X11grab",
  399. sizeof(X11Grab),
  400. NULL,
  401. x11grab_read_header,
  402. x11grab_read_packet,
  403. x11grab_read_close,
  404. .flags = AVFMT_NOFILE,
  405. };