Mercurial > libavformat.hg
annotate x11grab.c @ 1586:56d6828ab647 libavformat
RFC 6: Overall cleanups again.
- License should be fixed now (finally)
- Added doxygen comments
- Cleaned up the X11Grab structure
- Renamed X11Grab structure to x11_grab_s
- Changed some Java stylish named functions to more C'stylish ones
- Changed order of some parameters so they are more libc alike with
destination as first parameter
- Use AVRational where relevant
- Fixed time calculation
- Cleaned up a few little tiny things around the sources
+ removed a goto
+ removed unused vars
+ etc...
(Patch by Edouard Gomez)
author | gpoirier |
---|---|
date | Wed, 13 Dec 2006 08:50:30 +0000 |
parents | 14c185883eab |
children | d977c962f64b |
rev | line source |
---|---|
1580
32bfc91cb541
Original X11 device demuxer patch from Clemens Fruhwirth
gpoirier
parents:
1579
diff
changeset
|
1 /* |
32bfc91cb541
Original X11 device demuxer patch from Clemens Fruhwirth
gpoirier
parents:
1579
diff
changeset
|
2 * X11 video grab interface |
1581 | 3 * |
1583 | 4 * This file is part of FFmpeg. |
1580
32bfc91cb541
Original X11 device demuxer patch from Clemens Fruhwirth
gpoirier
parents:
1579
diff
changeset
|
5 * |
1583 | 6 * FFmpeg integration: |
7 * Copyright (C) 2006 Clemens Fruhwirth <clemens@endorphin.org> | |
8 * Edouard Gomez <ed.gomez@free.fr> | |
1580
32bfc91cb541
Original X11 device demuxer patch from Clemens Fruhwirth
gpoirier
parents:
1579
diff
changeset
|
9 * |
1583 | 10 * This file contains code from grab.c: |
1584 | 11 * Copyright (c) 2000-2001 Fabrice Bellard |
1580
32bfc91cb541
Original X11 device demuxer patch from Clemens Fruhwirth
gpoirier
parents:
1579
diff
changeset
|
12 * |
32bfc91cb541
Original X11 device demuxer patch from Clemens Fruhwirth
gpoirier
parents:
1579
diff
changeset
|
13 * This file contains code from the xvidcap project: |
1583 | 14 * Copyright (C) 1997-1998 Rasca, Berlin |
15 * 2003-2004 Karl H. Beckers, Frankfurt | |
1580
32bfc91cb541
Original X11 device demuxer patch from Clemens Fruhwirth
gpoirier
parents:
1579
diff
changeset
|
16 * |
1583 | 17 * FFmpeg is free software; you can redistribute it and/or modify |
1580
32bfc91cb541
Original X11 device demuxer patch from Clemens Fruhwirth
gpoirier
parents:
1579
diff
changeset
|
18 * it under the terms of the GNU General Public License as published by |
32bfc91cb541
Original X11 device demuxer patch from Clemens Fruhwirth
gpoirier
parents:
1579
diff
changeset
|
19 * the Free Software Foundation; either version 2 of the License, or |
32bfc91cb541
Original X11 device demuxer patch from Clemens Fruhwirth
gpoirier
parents:
1579
diff
changeset
|
20 * (at your option) any later version. |
32bfc91cb541
Original X11 device demuxer patch from Clemens Fruhwirth
gpoirier
parents:
1579
diff
changeset
|
21 * |
1583 | 22 * FFmpeg is distributed in the hope that it will be useful, |
1580
32bfc91cb541
Original X11 device demuxer patch from Clemens Fruhwirth
gpoirier
parents:
1579
diff
changeset
|
23 * but WITHOUT ANY WARRANTY; without even the implied warranty of |
32bfc91cb541
Original X11 device demuxer patch from Clemens Fruhwirth
gpoirier
parents:
1579
diff
changeset
|
24 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
32bfc91cb541
Original X11 device demuxer patch from Clemens Fruhwirth
gpoirier
parents:
1579
diff
changeset
|
25 * GNU General Public License for more details. |
32bfc91cb541
Original X11 device demuxer patch from Clemens Fruhwirth
gpoirier
parents:
1579
diff
changeset
|
26 * |
1585
14c185883eab
RFC 5: Mouse cursor painting reduc and more cleanups
gpoirier
parents:
1584
diff
changeset
|
27 * You should have received a copy of the GNU General Public License along |
1586 | 28 * with FFmpeg; if not, write to the Free Software Foundation, Inc., |
1585
14c185883eab
RFC 5: Mouse cursor painting reduc and more cleanups
gpoirier
parents:
1584
diff
changeset
|
29 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. |
1580
32bfc91cb541
Original X11 device demuxer patch from Clemens Fruhwirth
gpoirier
parents:
1579
diff
changeset
|
30 */ |
1581 | 31 |
1586 | 32 /** |
33 * @file x11grab.c | |
34 * X11 frame device demuxer by Clemens Fruhwirth <clemens@endorphin.org> | |
35 * and Edouard Gomez <ed.gomez@free.fr>. | |
36 */ | |
37 | |
1580
32bfc91cb541
Original X11 device demuxer patch from Clemens Fruhwirth
gpoirier
parents:
1579
diff
changeset
|
38 #include "avformat.h" |
32bfc91cb541
Original X11 device demuxer patch from Clemens Fruhwirth
gpoirier
parents:
1579
diff
changeset
|
39 #include <unistd.h> |
32bfc91cb541
Original X11 device demuxer patch from Clemens Fruhwirth
gpoirier
parents:
1579
diff
changeset
|
40 #include <fcntl.h> |
32bfc91cb541
Original X11 device demuxer patch from Clemens Fruhwirth
gpoirier
parents:
1579
diff
changeset
|
41 #include <sys/ioctl.h> |
32bfc91cb541
Original X11 device demuxer patch from Clemens Fruhwirth
gpoirier
parents:
1579
diff
changeset
|
42 #include <sys/mman.h> |
32bfc91cb541
Original X11 device demuxer patch from Clemens Fruhwirth
gpoirier
parents:
1579
diff
changeset
|
43 #include <sys/time.h> |
32bfc91cb541
Original X11 device demuxer patch from Clemens Fruhwirth
gpoirier
parents:
1579
diff
changeset
|
44 #define _LINUX_TIME_H 1 |
32bfc91cb541
Original X11 device demuxer patch from Clemens Fruhwirth
gpoirier
parents:
1579
diff
changeset
|
45 #include <time.h> |
32bfc91cb541
Original X11 device demuxer patch from Clemens Fruhwirth
gpoirier
parents:
1579
diff
changeset
|
46 #include <X11/X.h> |
32bfc91cb541
Original X11 device demuxer patch from Clemens Fruhwirth
gpoirier
parents:
1579
diff
changeset
|
47 #include <X11/Xlib.h> |
32bfc91cb541
Original X11 device demuxer patch from Clemens Fruhwirth
gpoirier
parents:
1579
diff
changeset
|
48 #include <X11/Xlibint.h> |
32bfc91cb541
Original X11 device demuxer patch from Clemens Fruhwirth
gpoirier
parents:
1579
diff
changeset
|
49 #include <X11/Xproto.h> |
1583 | 50 #include <X11/Xutil.h> |
1580
32bfc91cb541
Original X11 device demuxer patch from Clemens Fruhwirth
gpoirier
parents:
1579
diff
changeset
|
51 #include <sys/ipc.h> |
32bfc91cb541
Original X11 device demuxer patch from Clemens Fruhwirth
gpoirier
parents:
1579
diff
changeset
|
52 #include <sys/shm.h> |
32bfc91cb541
Original X11 device demuxer patch from Clemens Fruhwirth
gpoirier
parents:
1579
diff
changeset
|
53 #include <X11/extensions/XShm.h> |
32bfc91cb541
Original X11 device demuxer patch from Clemens Fruhwirth
gpoirier
parents:
1579
diff
changeset
|
54 |
1586 | 55 /** |
56 * X11 Device Demuxer context | |
57 */ | |
58 typedef struct x11_grab_s | |
1581 | 59 { |
1586 | 60 int frame_size; /**< Size in bytes of a grabbed frame */ |
61 AVRational time_base; /**< Time base */ | |
62 int64_t time_frame; /**< Current time */ | |
63 | |
64 int height; /**< Height of the grab frame */ | |
65 int width; /**< Width of the grab frame */ | |
66 int x_off; /**< Horizontal top-left corner coordinate */ | |
67 int y_off; /**< Vertical top-left corner coordinate */ | |
1580
32bfc91cb541
Original X11 device demuxer patch from Clemens Fruhwirth
gpoirier
parents:
1579
diff
changeset
|
68 |
1586 | 69 Display *dpy; /**< X11 display from which x11grab grabs frames */ |
70 XImage *image; /**< X11 image holding the grab */ | |
71 int use_shm; /**< !0 when using XShm extension */ | |
72 XShmSegmentInfo shminfo; /**< When using XShm, keeps track of XShm infos */ | |
73 } x11_grab_t; | |
1580
32bfc91cb541
Original X11 device demuxer patch from Clemens Fruhwirth
gpoirier
parents:
1579
diff
changeset
|
74 |
1586 | 75 /** |
76 * Initializes the x11 grab device demuxer (public device demuxer API). | |
77 * | |
78 * @param s1 Context from avformat core | |
79 * @param ap Parameters from avformat core | |
80 * @return <ul> | |
81 * <li>ENOMEM no memory left</li> | |
82 * <li>AVERROR_IO other failure case</li> | |
83 * <li>0 success</li> | |
84 * </ul> | |
85 */ | |
1581 | 86 static int |
87 x11grab_read_header(AVFormatContext *s1, AVFormatParameters *ap) | |
1580
32bfc91cb541
Original X11 device demuxer patch from Clemens Fruhwirth
gpoirier
parents:
1579
diff
changeset
|
88 { |
1586 | 89 x11_grab_t *x11grab = s1->priv_data; |
1584 | 90 Display *dpy; |
91 AVStream *st = NULL; | |
92 int input_pixfmt; | |
93 XImage *image; | |
1586 | 94 int x_off = 0; |
95 int y_off = 0; | |
1584 | 96 int use_shm; |
97 | |
98 dpy = XOpenDisplay(NULL); | |
99 if(!dpy) { | |
1586 | 100 av_free(st); |
101 return AVERROR_IO; | |
1584 | 102 } |
1580
32bfc91cb541
Original X11 device demuxer patch from Clemens Fruhwirth
gpoirier
parents:
1579
diff
changeset
|
103 |
1584 | 104 sscanf(ap->device, "x11:%d,%d", &x_off, &y_off); |
105 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); | |
106 | |
107 if (!ap || ap->width <= 0 || ap->height <= 0 || ap->time_base.den <= 0) { | |
108 av_log(s1, AV_LOG_ERROR, "AVParameters don't have any video size. Use -s.\n"); | |
109 return AVERROR_IO; | |
110 } | |
111 | |
112 st = av_new_stream(s1, 0); | |
113 if (!st) { | |
114 return -ENOMEM; | |
115 } | |
116 av_set_pts_info(st, 64, 1, 1000000); /* 64 bits pts in us */ | |
1580
32bfc91cb541
Original X11 device demuxer patch from Clemens Fruhwirth
gpoirier
parents:
1579
diff
changeset
|
117 |
1584 | 118 use_shm = XShmQueryExtension(dpy); |
1586 | 119 av_log(s1, AV_LOG_INFO, "shared memory extension %s found\n", use_shm ? "" : "not"); |
1581 | 120 |
1584 | 121 if(use_shm) { |
122 int scr = XDefaultScreen(dpy); | |
123 image = XShmCreateImage(dpy, | |
124 DefaultVisual(dpy, scr), | |
125 DefaultDepth(dpy, scr), | |
126 ZPixmap, | |
127 NULL, | |
128 &x11grab->shminfo, | |
129 ap->width, ap->height); | |
130 x11grab->shminfo.shmid = shmget(IPC_PRIVATE, | |
131 image->bytes_per_line * image->height, | |
132 IPC_CREAT|0777); | |
133 if (x11grab->shminfo.shmid == -1) { | |
134 av_log(s1, AV_LOG_ERROR, "Fatal: Can't get shared memory!\n"); | |
135 return -ENOMEM; | |
136 } | |
137 x11grab->shminfo.shmaddr = image->data = shmat(x11grab->shminfo.shmid, 0, 0); | |
138 x11grab->shminfo.readOnly = False; | |
1580
32bfc91cb541
Original X11 device demuxer patch from Clemens Fruhwirth
gpoirier
parents:
1579
diff
changeset
|
139 |
1584 | 140 if (!XShmAttach(dpy, &x11grab->shminfo)) { |
141 av_log(s1, AV_LOG_ERROR, "Fatal: Failed to attach shared memory!\n"); | |
142 /* needs some better error subroutine :) */ | |
143 return AVERROR_IO; | |
144 } | |
145 } else { | |
146 image = XGetImage(dpy, RootWindow(dpy, DefaultScreen(dpy)), | |
147 x_off,y_off, | |
148 ap->width,ap->height, | |
149 AllPlanes, ZPixmap); | |
150 } | |
1580
32bfc91cb541
Original X11 device demuxer patch from Clemens Fruhwirth
gpoirier
parents:
1579
diff
changeset
|
151 |
1584 | 152 switch (image->bits_per_pixel) { |
153 case 8: | |
154 av_log (s1, AV_LOG_DEBUG, "8 bit pallete\n"); | |
155 input_pixfmt = PIX_FMT_PAL8; | |
156 break; | |
157 case 16: | |
1586 | 158 if ( image->red_mask == 0xf800 && |
159 image->green_mask == 0x07e0 && | |
160 image->blue_mask == 0x001f ) { | |
1584 | 161 av_log (s1, AV_LOG_DEBUG, "16 bit RGB565\n"); |
162 input_pixfmt = PIX_FMT_RGB565; | |
1586 | 163 } else if (image->red_mask == 0x7c00 && |
164 image->green_mask == 0x03e0 && | |
165 image->blue_mask == 0x001f ) { | |
1584 | 166 av_log(s1, AV_LOG_DEBUG, "16 bit RGB555\n"); |
167 input_pixfmt = PIX_FMT_RGB555; | |
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 24: | |
1586 | 175 if ( image->red_mask == 0xff0000 && |
176 image->green_mask == 0x00ff00 && | |
177 image->blue_mask == 0x0000ff ) { | |
1584 | 178 input_pixfmt = PIX_FMT_BGR24; |
1586 | 179 } else if ( image->red_mask == 0x0000ff && |
180 image->green_mask == 0x00ff00 && | |
181 image->blue_mask == 0xff0000 ) { | |
1584 | 182 input_pixfmt = PIX_FMT_RGB24; |
183 } else { | |
184 av_log(s1, AV_LOG_ERROR,"rgb ordering at image depth %i not supported ... aborting\n", image->bits_per_pixel); | |
185 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); | |
186 return AVERROR_IO; | |
187 } | |
188 break; | |
189 case 32: | |
1580
32bfc91cb541
Original X11 device demuxer patch from Clemens Fruhwirth
gpoirier
parents:
1579
diff
changeset
|
190 #if 0 |
1584 | 191 GetColorInfo (image, &c_info); |
1586 | 192 if ( c_info.alpha_mask == 0xff000000 && image->green_mask == 0x0000ff00) { |
1584 | 193 /* byte order is relevant here, not endianness |
194 * endianness is handled by avcodec, but atm no such thing | |
195 * as having ABGR, instead of ARGB in a word. Since we | |
196 * need this for Solaris/SPARC, but need to do the conversion | |
197 * for every frame we do it outside of this loop, cf. below | |
198 * this matches both ARGB32 and ABGR32 */ | |
199 input_pixfmt = PIX_FMT_ARGB32; | |
200 } else { | |
201 av_log(s1, AV_LOG_ERROR,"image depth %i not supported ... aborting\n", image->bits_per_pixel); | |
202 return AVERROR_IO; | |
203 } | |
1580
32bfc91cb541
Original X11 device demuxer patch from Clemens Fruhwirth
gpoirier
parents:
1579
diff
changeset
|
204 #endif |
1584 | 205 input_pixfmt = PIX_FMT_RGBA32; |
206 break; | |
207 default: | |
208 av_log(s1, AV_LOG_ERROR, "image depth %i not supported ... aborting\n", image->bits_per_pixel); | |
209 return -1; | |
210 } | |
1580
32bfc91cb541
Original X11 device demuxer patch from Clemens Fruhwirth
gpoirier
parents:
1579
diff
changeset
|
211 |
1586 | 212 x11grab->frame_size = ap->width * ap->height * image->bits_per_pixel/8; |
1584 | 213 x11grab->dpy = dpy; |
214 x11grab->width = ap->width; | |
215 x11grab->height = ap->height; | |
1586 | 216 x11grab->time_base = ap->time_base; |
217 x11grab->time_frame = av_gettime() / av_q2d(ap->time_base); | |
1584 | 218 x11grab->x_off = x_off; |
219 x11grab->y_off = y_off; | |
220 x11grab->image = image; | |
221 x11grab->use_shm = use_shm; | |
1580
32bfc91cb541
Original X11 device demuxer patch from Clemens Fruhwirth
gpoirier
parents:
1579
diff
changeset
|
222 |
1584 | 223 st->codec->codec_type = CODEC_TYPE_VIDEO; |
224 st->codec->codec_id = CODEC_ID_RAWVIDEO; | |
1586 | 225 st->codec->width = ap->width; |
226 st->codec->height = ap->height; | |
1584 | 227 st->codec->pix_fmt = input_pixfmt; |
1586 | 228 st->codec->time_base = ap->time_base; |
229 st->codec->bit_rate = x11grab->frame_size * 1/av_q2d(ap->time_base) * 8; | |
1584 | 230 |
231 return 0; | |
1580
32bfc91cb541
Original X11 device demuxer patch from Clemens Fruhwirth
gpoirier
parents:
1579
diff
changeset
|
232 } |
32bfc91cb541
Original X11 device demuxer patch from Clemens Fruhwirth
gpoirier
parents:
1579
diff
changeset
|
233 |
1586 | 234 /** |
235 * Get pointer coordinates from X11. | |
236 * | |
237 * @param x Integer where horizontal coordinate will be returned | |
238 * @param y Integer where vertical coordinate will be returned | |
239 * @param dpy X11 display from where pointer coordinates are retrieved | |
240 * @param s1 Context used for logging errors if necessary | |
241 */ | |
1581 | 242 static void |
1586 | 243 get_pointer_coordinates(int *x, int *y, Display *dpy, AVFormatContext *s1) |
1581 | 244 { |
1584 | 245 Window mrootwindow, childwindow; |
246 int dummy; | |
247 | |
248 mrootwindow = DefaultRootWindow(dpy); | |
249 | |
250 if (XQueryPointer(dpy, mrootwindow, &mrootwindow, &childwindow, | |
251 x, y, &dummy, &dummy, (unsigned int*)&dummy)) { | |
252 } else { | |
253 av_log(s1, AV_LOG_INFO, "couldn't find mouse pointer\n"); | |
254 *x = -1; | |
255 *y = -1; | |
256 } | |
1580
32bfc91cb541
Original X11 device demuxer patch from Clemens Fruhwirth
gpoirier
parents:
1579
diff
changeset
|
257 } |
32bfc91cb541
Original X11 device demuxer patch from Clemens Fruhwirth
gpoirier
parents:
1579
diff
changeset
|
258 |
1586 | 259 /** |
260 * Mouse painting helper function that applies an 'and' and 'or' mask pair to | |
261 * '*dst' pixel. It actually draws a mouse pointer pixel to grabbed frame. | |
262 * | |
263 * @param dst Destination pixel | |
264 * @param and Part of the mask that must be applied using a bitwise 'and' | |
265 * operator | |
266 * @param or Part of the mask that must be applied using a bitwise 'or' | |
267 * operator | |
268 * @param bits_per_pixel Bits per pixel used in the grabbed image | |
269 */ | |
1585
14c185883eab
RFC 5: Mouse cursor painting reduc and more cleanups
gpoirier
parents:
1584
diff
changeset
|
270 static void inline |
14c185883eab
RFC 5: Mouse cursor painting reduc and more cleanups
gpoirier
parents:
1584
diff
changeset
|
271 apply_masks(uint8_t *dst, int and, int or, int bits_per_pixel) |
14c185883eab
RFC 5: Mouse cursor painting reduc and more cleanups
gpoirier
parents:
1584
diff
changeset
|
272 { |
14c185883eab
RFC 5: Mouse cursor painting reduc and more cleanups
gpoirier
parents:
1584
diff
changeset
|
273 switch (bits_per_pixel) { |
14c185883eab
RFC 5: Mouse cursor painting reduc and more cleanups
gpoirier
parents:
1584
diff
changeset
|
274 case 32: |
14c185883eab
RFC 5: Mouse cursor painting reduc and more cleanups
gpoirier
parents:
1584
diff
changeset
|
275 *(uint32_t*)dst = (*(uint32_t*)dst & and) | or; |
14c185883eab
RFC 5: Mouse cursor painting reduc and more cleanups
gpoirier
parents:
1584
diff
changeset
|
276 break; |
14c185883eab
RFC 5: Mouse cursor painting reduc and more cleanups
gpoirier
parents:
1584
diff
changeset
|
277 case 16: |
14c185883eab
RFC 5: Mouse cursor painting reduc and more cleanups
gpoirier
parents:
1584
diff
changeset
|
278 *(uint16_t*)dst = (*(uint16_t*)dst & and) | or; |
14c185883eab
RFC 5: Mouse cursor painting reduc and more cleanups
gpoirier
parents:
1584
diff
changeset
|
279 break; |
14c185883eab
RFC 5: Mouse cursor painting reduc and more cleanups
gpoirier
parents:
1584
diff
changeset
|
280 case 8: |
1586 | 281 *dst = !!or; |
1585
14c185883eab
RFC 5: Mouse cursor painting reduc and more cleanups
gpoirier
parents:
1584
diff
changeset
|
282 break; |
14c185883eab
RFC 5: Mouse cursor painting reduc and more cleanups
gpoirier
parents:
1584
diff
changeset
|
283 } |
14c185883eab
RFC 5: Mouse cursor painting reduc and more cleanups
gpoirier
parents:
1584
diff
changeset
|
284 } |
1582
754781df8033
RFC 1 stage: Improve maintainability of the cursoir painting code
gpoirier
parents:
1581
diff
changeset
|
285 |
1586 | 286 /** |
287 * Paints a mouse pointer in an X11 image. | |
288 * | |
289 * @param image Image where to paint the mouse pointer | |
290 * @param s context used to retrieve original grabbing rectangle | |
291 * coordinates | |
292 * @param x Mouse pointer coordinate | |
293 * @param y Mouse pointer coordinate | |
294 */ | |
1581 | 295 static void |
1586 | 296 paint_mouse_pointer(XImage *image, x11_grab_t *s, int x, int y) |
1581 | 297 { |
1586 | 298 /* 16x20x1bpp bitmap for the black channel of the mouse pointer */ |
1584 | 299 static const uint16_t const mousePointerBlack[] = |
300 { | |
1586 | 301 0x0000, 0xc000, 0xa000, 0x9000, 0x8800, |
302 0x8400, 0x8200, 0x8100, 0x8080, 0x8040, | |
303 0x83c0, 0x9200, 0xa900, 0xc900, 0x0480, | |
304 0x0480, 0x0240, 0x0240, 0x01c0, 0x0000 | |
1584 | 305 }; |
1583 | 306 |
1586 | 307 /* 16x20x1bpp bitmap for the white channel of the mouse pointer */ |
1584 | 308 static const uint16_t const mousePointerWhite[] = |
309 { | |
1586 | 310 0x0000, 0x0000, 0x4000, 0x6000, 0x7000, |
311 0x7800, 0x7c00, 0x7e00, 0x7f00, 0x7f80, | |
312 0x7c00, 0x6c00, 0x4600, 0x0600, 0x0300, | |
313 0x0300, 0x0180, 0x0180, 0x0000, 0x0000 | |
1584 | 314 }; |
1583 | 315 |
1584 | 316 int x_off = s->x_off; |
317 int y_off = s->y_off; | |
318 int width = s->width; | |
319 int height = s->height; | |
1580
32bfc91cb541
Original X11 device demuxer patch from Clemens Fruhwirth
gpoirier
parents:
1579
diff
changeset
|
320 |
1586 | 321 if ( x - x_off >= 0 && x < width + x_off |
322 && y - y_off >= 0 && y < height + y_off) { | |
323 uint8_t *im_data = (uint8_t*)image->data; | |
324 int bytes_per_pixel; | |
1584 | 325 int line; |
1585
14c185883eab
RFC 5: Mouse cursor painting reduc and more cleanups
gpoirier
parents:
1584
diff
changeset
|
326 int masks; |
14c185883eab
RFC 5: Mouse cursor painting reduc and more cleanups
gpoirier
parents:
1584
diff
changeset
|
327 |
14c185883eab
RFC 5: Mouse cursor painting reduc and more cleanups
gpoirier
parents:
1584
diff
changeset
|
328 /* Select correct masks and pixel size */ |
1586 | 329 if (image->bits_per_pixel == 8) { |
330 masks = 1; | |
331 } else { | |
1585
14c185883eab
RFC 5: Mouse cursor painting reduc and more cleanups
gpoirier
parents:
1584
diff
changeset
|
332 masks = (image->red_mask|image->green_mask|image->blue_mask); |
14c185883eab
RFC 5: Mouse cursor painting reduc and more cleanups
gpoirier
parents:
1584
diff
changeset
|
333 } |
1586 | 334 bytes_per_pixel = image->bits_per_pixel>>3; |
1585
14c185883eab
RFC 5: Mouse cursor painting reduc and more cleanups
gpoirier
parents:
1584
diff
changeset
|
335 |
14c185883eab
RFC 5: Mouse cursor painting reduc and more cleanups
gpoirier
parents:
1584
diff
changeset
|
336 /* Shift to right line */ |
1586 | 337 im_data += image->bytes_per_line * (y - y_off); |
338 /* Shift to right pixel in the line */ | |
339 im_data += bytes_per_pixel * (x - x_off); | |
1585
14c185883eab
RFC 5: Mouse cursor painting reduc and more cleanups
gpoirier
parents:
1584
diff
changeset
|
340 |
14c185883eab
RFC 5: Mouse cursor painting reduc and more cleanups
gpoirier
parents:
1584
diff
changeset
|
341 /* Draw the cursor - proper loop */ |
1586 | 342 for (line = 0; line < FFMIN(20, (y_off + height) - y); line++) { |
1585
14c185883eab
RFC 5: Mouse cursor painting reduc and more cleanups
gpoirier
parents:
1584
diff
changeset
|
343 uint8_t *cursor = im_data; |
1586 | 344 int column; |
1585
14c185883eab
RFC 5: Mouse cursor painting reduc and more cleanups
gpoirier
parents:
1584
diff
changeset
|
345 uint16_t bm_b; |
14c185883eab
RFC 5: Mouse cursor painting reduc and more cleanups
gpoirier
parents:
1584
diff
changeset
|
346 uint16_t bm_w; |
14c185883eab
RFC 5: Mouse cursor painting reduc and more cleanups
gpoirier
parents:
1584
diff
changeset
|
347 |
1586 | 348 bm_b = mousePointerBlack[line]; |
349 bm_w = mousePointerWhite[line]; | |
1585
14c185883eab
RFC 5: Mouse cursor painting reduc and more cleanups
gpoirier
parents:
1584
diff
changeset
|
350 |
1586 | 351 for (column = 0; column < FFMIN(16, (x_off + width) - x); column++) { |
352 apply_masks(cursor, ~(masks*(bm_b&1)), masks*(bm_w&1), | |
1585
14c185883eab
RFC 5: Mouse cursor painting reduc and more cleanups
gpoirier
parents:
1584
diff
changeset
|
353 image->bits_per_pixel); |
1586 | 354 cursor += bytes_per_pixel; |
1585
14c185883eab
RFC 5: Mouse cursor painting reduc and more cleanups
gpoirier
parents:
1584
diff
changeset
|
355 bm_b >>= 1; |
14c185883eab
RFC 5: Mouse cursor painting reduc and more cleanups
gpoirier
parents:
1584
diff
changeset
|
356 bm_w >>= 1; |
14c185883eab
RFC 5: Mouse cursor painting reduc and more cleanups
gpoirier
parents:
1584
diff
changeset
|
357 } |
14c185883eab
RFC 5: Mouse cursor painting reduc and more cleanups
gpoirier
parents:
1584
diff
changeset
|
358 im_data += image->bytes_per_line; |
1584 | 359 } |
360 } | |
1580
32bfc91cb541
Original X11 device demuxer patch from Clemens Fruhwirth
gpoirier
parents:
1579
diff
changeset
|
361 } |
32bfc91cb541
Original X11 device demuxer patch from Clemens Fruhwirth
gpoirier
parents:
1579
diff
changeset
|
362 |
32bfc91cb541
Original X11 device demuxer patch from Clemens Fruhwirth
gpoirier
parents:
1579
diff
changeset
|
363 |
1586 | 364 /** |
365 * Reads new data in the image structure. | |
366 * | |
367 * @param dpy X11 display to grab from | |
368 * @param d | |
369 * @param image Image where the grab will be put | |
370 * @param x Top-Left grabbing rectangle horizontal coordinate | |
371 * @param y Top-Left grabbing rectangle vertical coordinate | |
372 * @return 0 if error, !0 if successful | |
1580
32bfc91cb541
Original X11 device demuxer patch from Clemens Fruhwirth
gpoirier
parents:
1579
diff
changeset
|
373 */ |
32bfc91cb541
Original X11 device demuxer patch from Clemens Fruhwirth
gpoirier
parents:
1579
diff
changeset
|
374 static int |
1586 | 375 xget_zpixmap(Display *dpy, Drawable d, XImage *image, int x, int y) |
1580
32bfc91cb541
Original X11 device demuxer patch from Clemens Fruhwirth
gpoirier
parents:
1579
diff
changeset
|
376 { |
1584 | 377 xGetImageReply rep; |
378 xGetImageReq *req; | |
379 long nbytes; | |
1581 | 380 |
1584 | 381 if (!image) { |
1586 | 382 return 0; |
1584 | 383 } |
384 | |
385 LockDisplay(dpy); | |
386 GetReq(GetImage, req); | |
1581 | 387 |
1584 | 388 /* First set up the standard stuff in the request */ |
389 req->drawable = d; | |
390 req->x = x; | |
391 req->y = y; | |
392 req->width = image->width; | |
393 req->height = image->height; | |
394 req->planeMask = (unsigned int)AllPlanes; | |
395 req->format = ZPixmap; | |
396 | |
1586 | 397 if (!_XReply(dpy, (xReply *)&rep, 0, xFalse) || !rep.length) { |
1584 | 398 UnlockDisplay(dpy); |
399 SyncHandle(); | |
1586 | 400 return 0; |
1584 | 401 } |
402 | |
403 nbytes = (long)rep.length << 2; | |
404 _XReadPad(dpy, image->data, nbytes); | |
405 | |
406 UnlockDisplay(dpy); | |
407 SyncHandle(); | |
1586 | 408 return 1; |
1580
32bfc91cb541
Original X11 device demuxer patch from Clemens Fruhwirth
gpoirier
parents:
1579
diff
changeset
|
409 } |
32bfc91cb541
Original X11 device demuxer patch from Clemens Fruhwirth
gpoirier
parents:
1579
diff
changeset
|
410 |
1586 | 411 /** |
412 * Grabs a frame from x11 (public device demuxer API). | |
413 * | |
414 * @param s1 Context from avformat core | |
415 * @param pkt Packet holding the brabbed frame | |
416 * @return frame size in bytes | |
417 */ | |
1585
14c185883eab
RFC 5: Mouse cursor painting reduc and more cleanups
gpoirier
parents:
1584
diff
changeset
|
418 static int |
14c185883eab
RFC 5: Mouse cursor painting reduc and more cleanups
gpoirier
parents:
1584
diff
changeset
|
419 x11grab_read_packet(AVFormatContext *s1, AVPacket *pkt) |
1580
32bfc91cb541
Original X11 device demuxer patch from Clemens Fruhwirth
gpoirier
parents:
1579
diff
changeset
|
420 { |
1586 | 421 x11_grab_t *s = s1->priv_data; |
1584 | 422 Display *dpy = s->dpy; |
423 XImage *image = s->image; | |
424 int x_off = s->x_off; | |
425 int y_off = s->y_off; | |
426 | |
427 int64_t curtime, delay; | |
428 struct timespec ts; | |
429 | |
430 /* Calculate the time of the next frame */ | |
431 s->time_frame += int64_t_C(1000000); | |
1580
32bfc91cb541
Original X11 device demuxer patch from Clemens Fruhwirth
gpoirier
parents:
1579
diff
changeset
|
432 |
1584 | 433 /* wait based on the frame rate */ |
434 for(;;) { | |
435 curtime = av_gettime(); | |
1586 | 436 delay = s->time_frame * av_q2d(s->time_base) - curtime; |
1584 | 437 if (delay <= 0) { |
1586 | 438 if (delay < int64_t_C(-1000000) * av_q2d(s->time_base)) { |
1584 | 439 s->time_frame += int64_t_C(1000000); |
440 } | |
441 break; | |
442 } | |
443 ts.tv_sec = delay / 1000000; | |
444 ts.tv_nsec = (delay % 1000000) * 1000; | |
445 nanosleep(&ts, NULL); | |
446 } | |
1581 | 447 |
1584 | 448 if (av_new_packet(pkt, s->frame_size) < 0) { |
449 return AVERROR_IO; | |
450 } | |
451 | |
1586 | 452 pkt->pts = curtime; |
1584 | 453 |
454 if(s->use_shm) { | |
455 if (!XShmGetImage(dpy, RootWindow(dpy, DefaultScreen(dpy)), image, x_off, y_off, AllPlanes)) { | |
456 av_log (s1, AV_LOG_INFO, "XShmGetImage() failed\n"); | |
457 } | |
458 } else { | |
1586 | 459 if (!xget_zpixmap(dpy, RootWindow(dpy, DefaultScreen(dpy)), image, x_off, y_off)) { |
1584 | 460 av_log (s1, AV_LOG_INFO, "XGetZPixmap() failed\n"); |
461 } | |
462 } | |
463 | |
464 { | |
465 int pointer_x, pointer_y; | |
1586 | 466 get_pointer_coordinates(&pointer_x, &pointer_y, dpy, s1); |
467 paint_mouse_pointer(image, s, pointer_x, pointer_y); | |
1584 | 468 } |
1580
32bfc91cb541
Original X11 device demuxer patch from Clemens Fruhwirth
gpoirier
parents:
1579
diff
changeset
|
469 |
1582
754781df8033
RFC 1 stage: Improve maintainability of the cursoir painting code
gpoirier
parents:
1581
diff
changeset
|
470 |
1584 | 471 /* XXX: avoid memcpy */ |
472 memcpy(pkt->data, image->data, s->frame_size); | |
473 return s->frame_size; | |
1580
32bfc91cb541
Original X11 device demuxer patch from Clemens Fruhwirth
gpoirier
parents:
1579
diff
changeset
|
474 } |
32bfc91cb541
Original X11 device demuxer patch from Clemens Fruhwirth
gpoirier
parents:
1579
diff
changeset
|
475 |
1586 | 476 /** |
477 * Closes x11 frame grabber (public device demuxer API). | |
478 * | |
479 * @param s1 Context from avformat core | |
480 * @return 0 success, !0 failure | |
481 */ | |
1585
14c185883eab
RFC 5: Mouse cursor painting reduc and more cleanups
gpoirier
parents:
1584
diff
changeset
|
482 static int |
14c185883eab
RFC 5: Mouse cursor painting reduc and more cleanups
gpoirier
parents:
1584
diff
changeset
|
483 x11grab_read_close(AVFormatContext *s1) |
1580
32bfc91cb541
Original X11 device demuxer patch from Clemens Fruhwirth
gpoirier
parents:
1579
diff
changeset
|
484 { |
1586 | 485 x11_grab_t *x11grab = s1->priv_data; |
1582
754781df8033
RFC 1 stage: Improve maintainability of the cursoir painting code
gpoirier
parents:
1581
diff
changeset
|
486 |
1584 | 487 /* Detach cleanly from shared mem */ |
488 if (x11grab->use_shm) { | |
489 XShmDetach(x11grab->dpy, &x11grab->shminfo); | |
490 shmdt(x11grab->shminfo.shmaddr); | |
491 shmctl(x11grab->shminfo.shmid, IPC_RMID, NULL); | |
492 } | |
1582
754781df8033
RFC 1 stage: Improve maintainability of the cursoir painting code
gpoirier
parents:
1581
diff
changeset
|
493 |
1584 | 494 /* Destroy X11 image */ |
495 if (x11grab->image) { | |
496 XDestroyImage(x11grab->image); | |
497 x11grab->image = NULL; | |
498 } | |
1583 | 499 |
1584 | 500 /* Free X11 display */ |
501 XCloseDisplay(x11grab->dpy); | |
502 return 0; | |
1580
32bfc91cb541
Original X11 device demuxer patch from Clemens Fruhwirth
gpoirier
parents:
1579
diff
changeset
|
503 } |
32bfc91cb541
Original X11 device demuxer patch from Clemens Fruhwirth
gpoirier
parents:
1579
diff
changeset
|
504 |
1586 | 505 /** x11 grabber device demuxer declaration */ |
1581 | 506 AVInputFormat x11_grab_device_demuxer = |
507 { | |
1584 | 508 "x11grab", |
509 "X11grab", | |
1586 | 510 sizeof(x11_grab_t), |
1584 | 511 NULL, |
512 x11grab_read_header, | |
513 x11grab_read_packet, | |
514 x11grab_read_close, | |
515 .flags = AVFMT_NOFILE, | |
1580
32bfc91cb541
Original X11 device demuxer patch from Clemens Fruhwirth
gpoirier
parents:
1579
diff
changeset
|
516 }; |