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