tools/sofa2wavs: fix build on Windows
[ffmpeg.git] / libavdevice / gdigrab.c
1 /*
2 * GDI video grab interface
3 *
4 * This file is part of FFmpeg.
5 *
6 * Copyright (C) 2013 Calvin Walton <calvin.walton@kepstin.ca>
7 * Copyright (C) 2007-2010 Christophe Gisquet <word1.word2@gmail.com>
8 *
9 * FFmpeg is free software; you can redistribute it and/or
10 * modify it under the terms of the GNU Lesser General Public License
11 * as published by the Free Software Foundation; either version 2.1
12 * of the License, or (at your option) any later version.
13 *
14 * FFmpeg is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU Lesser General Public License for more details.
18 *
19 * You should have received a copy of the GNU Lesser General Public
20 * License along with FFmpeg; if not, write to the Free Software
21 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
22 */
23
24 /**
25 * @file
26 * GDI frame device demuxer
27 * @author Calvin Walton <calvin.walton@kepstin.ca>
28 * @author Christophe Gisquet <word1.word2@gmail.com>
29 */
30
31 #include "config.h"
32 #include "libavformat/demux.h"
33 #include "libavformat/internal.h"
34 #include "libavutil/mem.h"
35 #include "libavutil/opt.h"
36 #include "libavutil/time.h"
37 #include "libavutil/wchar_filename.h"
38 #include <windows.h>
39
40 /**
41 * GDI Device Demuxer context
42 */
43 struct gdigrab {
44 const AVClass *class; /**< Class for private options */
45
46 int frame_size; /**< Size in bytes of the frame pixel data */
47 int header_size; /**< Size in bytes of the DIB header */
48 AVRational time_base; /**< Time base */
49 int64_t time_frame; /**< Current time */
50
51 int draw_mouse; /**< Draw mouse cursor (private option) */
52 int show_region; /**< Draw border (private option) */
53 AVRational framerate; /**< Capture framerate (private option) */
54 int width; /**< Width of the grab frame (private option) */
55 int height; /**< Height of the grab frame (private option) */
56 int offset_x; /**< Capture x offset (private option) */
57 int offset_y; /**< Capture y offset (private option) */
58
59 HWND hwnd; /**< Handle of the window for the grab */
60 HDC source_hdc; /**< Source device context */
61 HDC dest_hdc; /**< Destination, source-compatible DC */
62 BITMAPINFO bmi; /**< Information describing DIB format */
63 HBITMAP hbmp; /**< Information on the bitmap captured */
64 void *buffer; /**< The buffer containing the bitmap image data */
65 RECT clip_rect; /**< The subarea of the screen or window to clip */
66
67 HWND region_hwnd; /**< Handle of the region border window */
68
69 int cursor_error_printed;
70 };
71
72 #define WIN32_API_ERROR(str) \
73 av_log(s1, AV_LOG_ERROR, str " (error %li)\n", GetLastError())
74
75 #define REGION_WND_BORDER 3
76
77 /**
78 * Callback to handle Windows messages for the region outline window.
79 *
80 * In particular, this handles painting the frame rectangle.
81 *
82 * @param hwnd The region outline window handle.
83 * @param msg The Windows message.
84 * @param wparam First Windows message parameter.
85 * @param lparam Second Windows message parameter.
86 * @return 0 success, !0 failure
87 */
88 static LRESULT CALLBACK
89 gdigrab_region_wnd_proc(HWND hwnd, UINT msg, WPARAM wparam, LPARAM lparam)
90 {
91 PAINTSTRUCT ps;
92 HDC hdc;
93 RECT rect;
94
95 switch (msg) {
96 case WM_PAINT:
97 hdc = BeginPaint(hwnd, &ps);
98
99 GetClientRect(hwnd, &rect);
100 FrameRect(hdc, &rect, GetStockObject(BLACK_BRUSH));
101
102 rect.left++; rect.top++; rect.right--; rect.bottom--;
103 FrameRect(hdc, &rect, GetStockObject(WHITE_BRUSH));
104
105 rect.left++; rect.top++; rect.right--; rect.bottom--;
106 FrameRect(hdc, &rect, GetStockObject(BLACK_BRUSH));
107
108 EndPaint(hwnd, &ps);
109 break;
110 default:
111 return DefWindowProc(hwnd, msg, wparam, lparam);
112 }
113 return 0;
114 }
115
116 /**
117 * Initialize the region outline window.
118 *
119 * @param s1 The format context.
120 * @param gdigrab gdigrab context.
121 * @return 0 success, !0 failure
122 */
123 static int
124 gdigrab_region_wnd_init(AVFormatContext *s1, struct gdigrab *gdigrab)
125 {
126 HWND hwnd;
127 RECT rect = gdigrab->clip_rect;
128 HRGN region = NULL;
129 HRGN region_interior = NULL;
130
131 DWORD style = WS_POPUP | WS_VISIBLE;
132 DWORD ex = WS_EX_TOOLWINDOW | WS_EX_TOPMOST | WS_EX_TRANSPARENT;
133
134 rect.left -= REGION_WND_BORDER; rect.top -= REGION_WND_BORDER;
135 rect.right += REGION_WND_BORDER; rect.bottom += REGION_WND_BORDER;
136
137 AdjustWindowRectEx(&rect, style, FALSE, ex);
138
139 // Create a window with no owner; use WC_DIALOG instead of writing a custom
140 // window class
141 hwnd = CreateWindowEx(ex, WC_DIALOG, NULL, style, rect.left, rect.top,
142 rect.right - rect.left, rect.bottom - rect.top,
143 NULL, NULL, NULL, NULL);
144 if (!hwnd) {
145 WIN32_API_ERROR("Could not create region display window");
146 goto error;
147 }
148
149 // Set the window shape to only include the border area
150 GetClientRect(hwnd, &rect);
151 region = CreateRectRgn(0, 0,
152 rect.right - rect.left, rect.bottom - rect.top);
153 region_interior = CreateRectRgn(REGION_WND_BORDER, REGION_WND_BORDER,
154 rect.right - rect.left - REGION_WND_BORDER,
155 rect.bottom - rect.top - REGION_WND_BORDER);
156 CombineRgn(region, region, region_interior, RGN_DIFF);
157 if (!SetWindowRgn(hwnd, region, FALSE)) {
158 WIN32_API_ERROR("Could not set window region");
159 goto error;
160 }
161 // The "region" memory is now owned by the window
162 region = NULL;
163 DeleteObject(region_interior);
164
165 SetWindowLongPtr(hwnd, GWLP_WNDPROC, (LONG_PTR) gdigrab_region_wnd_proc);
166
167 ShowWindow(hwnd, SW_SHOW);
168
169 gdigrab->region_hwnd = hwnd;
170
171 return 0;
172
173 error:
174 if (region)
175 DeleteObject(region);
176 if (region_interior)
177 DeleteObject(region_interior);
178 if (hwnd)
179 DestroyWindow(hwnd);
180 return 1;
181 }
182
183 /**
184 * Cleanup/free the region outline window.
185 *
186 * @param s1 The format context.
187 * @param gdigrab gdigrab context.
188 */
189 static void
190 gdigrab_region_wnd_destroy(AVFormatContext *s1, struct gdigrab *gdigrab)
191 {
192 if (gdigrab->region_hwnd)
193 DestroyWindow(gdigrab->region_hwnd);
194 gdigrab->region_hwnd = NULL;
195 }
196
197 /**
198 * Process the Windows message queue.
199 *
200 * This is important to prevent Windows from thinking the window has become
201 * unresponsive. As well, things like WM_PAINT (to actually draw the window
202 * contents) are handled from the message queue context.
203 *
204 * @param s1 The format context.
205 * @param gdigrab gdigrab context.
206 */
207 static void
208 gdigrab_region_wnd_update(AVFormatContext *s1, struct gdigrab *gdigrab)
209 {
210 HWND hwnd = gdigrab->region_hwnd;
211 MSG msg;
212
213 while (PeekMessage(&msg, hwnd, 0, 0, PM_REMOVE)) {
214 DispatchMessage(&msg);
215 }
216 }
217
218 /**
219 * Initializes the gdi grab device demuxer (public device demuxer API).
220 *
221 * @param s1 Context from avformat core
222 * @return AVERROR_IO error, 0 success
223 */
224 static int
225 gdigrab_read_header(AVFormatContext *s1)
226 {
227 struct gdigrab *gdigrab = s1->priv_data;
228
229 HWND hwnd;
230 HDC source_hdc = NULL;
231 HDC dest_hdc = NULL;
232 BITMAPINFO bmi;
233 HBITMAP hbmp = NULL;
234 void *buffer = NULL;
235
236 const char *filename = s1->url;
237 const char *name = NULL;
238 AVStream *st = NULL;
239
240 int bpp;
241 int horzres;
242 int vertres;
243 int desktophorzres;
244 int desktopvertres;
245 RECT virtual_rect;
246 RECT clip_rect;
247 BITMAP bmp;
248 int ret;
249
250 if (!strncmp(filename, "title=", 6)) {
251 wchar_t *name_w = NULL;
252 name = filename + 6;
253
254 if(utf8towchar(name, &name_w)) {
255 ret = AVERROR(errno);
256 goto error;
257 }
258 if(!name_w) {
259 ret = AVERROR(EINVAL);
260 goto error;
261 }
262
263 hwnd = FindWindowW(NULL, name_w);
264 av_freep(&name_w);
265 if (!hwnd) {
266 av_log(s1, AV_LOG_ERROR,
267 "Can't find window '%s', aborting.\n", name);
268 ret = AVERROR(EIO);
269 goto error;
270 }
271 if (gdigrab->show_region) {
272 av_log(s1, AV_LOG_WARNING,
273 "Can't show region when grabbing a window.\n");
274 gdigrab->show_region = 0;
275 }
276 } else if (!strcmp(filename, "desktop")) {
277 hwnd = NULL;
278 } else if (!strncmp(filename, "hwnd=", 5)) {
279 char *p;
280 name = filename + 5;
281
282 hwnd = (HWND) strtoull(name, &p, 0);
283
284 if (p == NULL || p == name || p[0] != '\0')
285 {
286 av_log(s1, AV_LOG_ERROR,
287 "Invalid window handle '%s', must be a valid integer.\n", name);
288 ret = AVERROR(EINVAL);
289 goto error;
290 }
291 } else {
292 av_log(s1, AV_LOG_ERROR,
293 "Please use \"desktop\", \"title=<windowname>\" or \"hwnd=<hwnd>\" to specify your target.\n");
294 ret = AVERROR(EIO);
295 goto error;
296 }
297
298 /* This will get the device context for the selected window, or if
299 * none, the primary screen */
300 source_hdc = GetDC(hwnd);
301 if (!source_hdc) {
302 WIN32_API_ERROR("Couldn't get window device context");
303 ret = AVERROR(EIO);
304 goto error;
305 }
306 bpp = GetDeviceCaps(source_hdc, BITSPIXEL);
307
308 horzres = GetDeviceCaps(source_hdc, HORZRES);
309 vertres = GetDeviceCaps(source_hdc, VERTRES);
310 desktophorzres = GetDeviceCaps(source_hdc, DESKTOPHORZRES);
311 desktopvertres = GetDeviceCaps(source_hdc, DESKTOPVERTRES);
312
313 if (hwnd) {
314 GetClientRect(hwnd, &virtual_rect);
315 /* window -- get the right height and width for scaling DPI */
316 virtual_rect.left = virtual_rect.left * desktophorzres / horzres;
317 virtual_rect.right = virtual_rect.right * desktophorzres / horzres;
318 virtual_rect.top = virtual_rect.top * desktopvertres / vertres;
319 virtual_rect.bottom = virtual_rect.bottom * desktopvertres / vertres;
320 } else {
321 /* desktop -- get the right height and width for scaling DPI */
322 virtual_rect.left = GetSystemMetrics(SM_XVIRTUALSCREEN);
323 virtual_rect.top = GetSystemMetrics(SM_YVIRTUALSCREEN);
324 virtual_rect.right = (virtual_rect.left + GetSystemMetrics(SM_CXVIRTUALSCREEN)) * desktophorzres / horzres;
325 virtual_rect.bottom = (virtual_rect.top + GetSystemMetrics(SM_CYVIRTUALSCREEN)) * desktopvertres / vertres;
326 }
327
328 /* If no width or height set, use full screen/window area */
329 if (!gdigrab->width || !gdigrab->height) {
330 clip_rect.left = virtual_rect.left;
331 clip_rect.top = virtual_rect.top;
332 clip_rect.right = virtual_rect.right;
333 clip_rect.bottom = virtual_rect.bottom;
334 } else {
335 clip_rect.left = gdigrab->offset_x;
336 clip_rect.top = gdigrab->offset_y;
337 clip_rect.right = gdigrab->width + gdigrab->offset_x;
338 clip_rect.bottom = gdigrab->height + gdigrab->offset_y;
339 }
340
341 if (clip_rect.left < virtual_rect.left ||
342 clip_rect.top < virtual_rect.top ||
343 clip_rect.right > virtual_rect.right ||
344 clip_rect.bottom > virtual_rect.bottom) {
345 av_log(s1, AV_LOG_ERROR,
346 "Capture area (%li,%li),(%li,%li) extends outside window area (%li,%li),(%li,%li)",
347 clip_rect.left, clip_rect.top,
348 clip_rect.right, clip_rect.bottom,
349 virtual_rect.left, virtual_rect.top,
350 virtual_rect.right, virtual_rect.bottom);
351 ret = AVERROR(EIO);
352 goto error;
353 }
354
355
356 if (name) {
357 av_log(s1, AV_LOG_INFO,
358 "Found window %s, capturing %lix%lix%i at (%li,%li)\n",
359 name,
360 clip_rect.right - clip_rect.left,
361 clip_rect.bottom - clip_rect.top,
362 bpp, clip_rect.left, clip_rect.top);
363 } else {
364 av_log(s1, AV_LOG_INFO,
365 "Capturing whole desktop as %lix%lix%i at (%li,%li)\n",
366 clip_rect.right - clip_rect.left,
367 clip_rect.bottom - clip_rect.top,
368 bpp, clip_rect.left, clip_rect.top);
369 }
370
371 if (clip_rect.right - clip_rect.left <= 0 ||
372 clip_rect.bottom - clip_rect.top <= 0 || bpp%8) {
373 av_log(s1, AV_LOG_ERROR, "Invalid properties, aborting\n");
374 ret = AVERROR(EIO);
375 goto error;
376 }
377
378 dest_hdc = CreateCompatibleDC(source_hdc);
379 if (!dest_hdc) {
380 WIN32_API_ERROR("Screen DC CreateCompatibleDC");
381 ret = AVERROR(EIO);
382 goto error;
383 }
384
385 /* Create a DIB and select it into the dest_hdc */
386 bmi.bmiHeader.biSize = sizeof(BITMAPINFOHEADER);
387 bmi.bmiHeader.biWidth = clip_rect.right - clip_rect.left;
388 bmi.bmiHeader.biHeight = -(clip_rect.bottom - clip_rect.top);
389 bmi.bmiHeader.biPlanes = 1;
390 bmi.bmiHeader.biBitCount = bpp;
391 bmi.bmiHeader.biCompression = BI_RGB;
392 bmi.bmiHeader.biSizeImage = 0;
393 bmi.bmiHeader.biXPelsPerMeter = 0;
394 bmi.bmiHeader.biYPelsPerMeter = 0;
395 bmi.bmiHeader.biClrUsed = 0;
396 bmi.bmiHeader.biClrImportant = 0;
397 hbmp = CreateDIBSection(dest_hdc, &bmi, DIB_RGB_COLORS,
398 &buffer, NULL, 0);
399 if (!hbmp) {
400 WIN32_API_ERROR("Creating DIB Section");
401 ret = AVERROR(EIO);
402 goto error;
403 }
404
405 if (!SelectObject(dest_hdc, hbmp)) {
406 WIN32_API_ERROR("SelectObject");
407 ret = AVERROR(EIO);
408 goto error;
409 }
410
411 /* Get info from the bitmap */
412 GetObject(hbmp, sizeof(BITMAP), &bmp);
413
414 st = avformat_new_stream(s1, NULL);
415 if (!st) {
416 ret = AVERROR(ENOMEM);
417 goto error;
418 }
419 avpriv_set_pts_info(st, 64, 1, 1000000); /* 64 bits pts in us */
420
421 gdigrab->frame_size = bmp.bmWidthBytes * bmp.bmHeight * bmp.bmPlanes;
422 gdigrab->header_size = sizeof(BITMAPFILEHEADER) + sizeof(BITMAPINFOHEADER) +
423 (bpp <= 8 ? (1 << bpp) : 0) * sizeof(RGBQUAD) /* palette size */;
424 gdigrab->time_base = av_inv_q(gdigrab->framerate);
425 gdigrab->time_frame = av_gettime_relative() / av_q2d(gdigrab->time_base);
426
427 gdigrab->hwnd = hwnd;
428 gdigrab->source_hdc = source_hdc;
429 gdigrab->dest_hdc = dest_hdc;
430 gdigrab->hbmp = hbmp;
431 gdigrab->bmi = bmi;
432 gdigrab->buffer = buffer;
433 gdigrab->clip_rect = clip_rect;
434
435 gdigrab->cursor_error_printed = 0;
436
437 if (gdigrab->show_region) {
438 if (gdigrab_region_wnd_init(s1, gdigrab)) {
439 ret = AVERROR(EIO);
440 goto error;
441 }
442 }
443
444 st->avg_frame_rate = av_inv_q(gdigrab->time_base);
445
446 st->codecpar->codec_type = AVMEDIA_TYPE_VIDEO;
447 st->codecpar->codec_id = AV_CODEC_ID_BMP;
448 st->codecpar->bit_rate = (gdigrab->header_size + gdigrab->frame_size) * 1/av_q2d(gdigrab->time_base) * 8;
449
450 return 0;
451
452 error:
453 if (source_hdc)
454 ReleaseDC(hwnd, source_hdc);
455 if (dest_hdc)
456 DeleteDC(dest_hdc);
457 if (hbmp)
458 DeleteObject(hbmp);
459 if (source_hdc)
460 DeleteDC(source_hdc);
461 return ret;
462 }
463
464 /**
465 * Paints a mouse pointer in a Win32 image.
466 *
467 * @param s1 Context of the log information
468 * @param s Current grad structure
469 */
470 static void paint_mouse_pointer(AVFormatContext *s1, struct gdigrab *gdigrab)
471 {
472 CURSORINFO ci = {0};
473
474 #define CURSOR_ERROR(str) \
475 if (!gdigrab->cursor_error_printed) { \
476 WIN32_API_ERROR(str); \
477 gdigrab->cursor_error_printed = 1; \
478 }
479
480 ci.cbSize = sizeof(ci);
481
482 if (GetCursorInfo(&ci)) {
483 HCURSOR icon = CopyCursor(ci.hCursor);
484 ICONINFO info;
485 POINT pos;
486 RECT clip_rect = gdigrab->clip_rect;
487 HWND hwnd = gdigrab->hwnd;
488 int horzres = GetDeviceCaps(gdigrab->source_hdc, HORZRES);
489 int vertres = GetDeviceCaps(gdigrab->source_hdc, VERTRES);
490 int desktophorzres = GetDeviceCaps(gdigrab->source_hdc, DESKTOPHORZRES);
491 int desktopvertres = GetDeviceCaps(gdigrab->source_hdc, DESKTOPVERTRES);
492 info.hbmMask = NULL;
493 info.hbmColor = NULL;
494
495 if (ci.flags != CURSOR_SHOWING)
496 return;
497
498 if (!icon) {
499 /* Use the standard arrow cursor as a fallback.
500 * You'll probably only hit this in Wine, which can't fetch
501 * the current system cursor. */
502 icon = CopyCursor(LoadCursor(NULL, IDC_ARROW));
503 }
504
505 if (!GetIconInfo(icon, &info)) {
506 CURSOR_ERROR("Could not get icon info");
507 goto icon_error;
508 }
509
510 if (hwnd) {
511 RECT rect;
512
513 if (GetWindowRect(hwnd, &rect)) {
514 pos.x = ci.ptScreenPos.x - clip_rect.left - info.xHotspot - rect.left;
515 pos.y = ci.ptScreenPos.y - clip_rect.top - info.yHotspot - rect.top;
516
517 //that would keep the correct location of mouse with hidpi screens
518 pos.x = pos.x * desktophorzres / horzres;
519 pos.y = pos.y * desktopvertres / vertres;
520 } else {
521 CURSOR_ERROR("Couldn't get window rectangle");
522 goto icon_error;
523 }
524 } else {
525 //that would keep the correct location of mouse with hidpi screens
526 pos.x = ci.ptScreenPos.x * desktophorzres / horzres - clip_rect.left - info.xHotspot;
527 pos.y = ci.ptScreenPos.y * desktopvertres / vertres - clip_rect.top - info.yHotspot;
528 }
529
530 av_log(s1, AV_LOG_DEBUG, "Cursor pos (%li,%li) -> (%li,%li)\n",
531 ci.ptScreenPos.x, ci.ptScreenPos.y, pos.x, pos.y);
532
533 if (pos.x >= 0 && pos.x <= clip_rect.right - clip_rect.left &&
534 pos.y >= 0 && pos.y <= clip_rect.bottom - clip_rect.top) {
535 if (!DrawIcon(gdigrab->dest_hdc, pos.x, pos.y, icon))
536 CURSOR_ERROR("Couldn't draw icon");
537 }
538
539 icon_error:
540 if (info.hbmMask)
541 DeleteObject(info.hbmMask);
542 if (info.hbmColor)
543 DeleteObject(info.hbmColor);
544 if (icon)
545 DestroyCursor(icon);
546 } else {
547 CURSOR_ERROR("Couldn't get cursor info");
548 }
549 }
550
551 /**
552 * Grabs a frame from gdi (public device demuxer API).
553 *
554 * @param s1 Context from avformat core
555 * @param pkt Packet holding the grabbed frame
556 * @return frame size in bytes
557 */
558 static int gdigrab_read_packet(AVFormatContext *s1, AVPacket *pkt)
559 {
560 struct gdigrab *gdigrab = s1->priv_data;
561
562 HDC dest_hdc = gdigrab->dest_hdc;
563 HDC source_hdc = gdigrab->source_hdc;
564 RECT clip_rect = gdigrab->clip_rect;
565 AVRational time_base = gdigrab->time_base;
566 int64_t time_frame = gdigrab->time_frame;
567
568 BITMAPFILEHEADER bfh;
569 int file_size = gdigrab->header_size + gdigrab->frame_size;
570
571 int64_t curtime, delay;
572
573 /* Calculate the time of the next frame */
574 time_frame += INT64_C(1000000);
575
576 /* Run Window message processing queue */
577 if (gdigrab->show_region)
578 gdigrab_region_wnd_update(s1, gdigrab);
579
580 /* wait based on the frame rate */
581 for (;;) {
582 curtime = av_gettime_relative();
583 delay = time_frame * av_q2d(time_base) - curtime;
584 if (delay <= 0) {
585 if (delay < INT64_C(-1000000) * av_q2d(time_base)) {
586 time_frame += INT64_C(1000000);
587 }
588 break;
589 }
590 if (s1->flags & AVFMT_FLAG_NONBLOCK) {
591 return AVERROR(EAGAIN);
592 } else {
593 av_usleep(delay);
594 }
595 }
596
597 if (av_new_packet(pkt, file_size) < 0)
598 return AVERROR(ENOMEM);
599 pkt->pts = av_gettime();
600
601 /* Blit screen grab */
602 if (!BitBlt(dest_hdc, 0, 0,
603 clip_rect.right - clip_rect.left,
604 clip_rect.bottom - clip_rect.top,
605 source_hdc,
606 clip_rect.left, clip_rect.top, SRCCOPY | CAPTUREBLT)) {
607 WIN32_API_ERROR("Failed to capture image");
608 return AVERROR(EIO);
609 }
610 if (gdigrab->draw_mouse)
611 paint_mouse_pointer(s1, gdigrab);
612
613 /* Copy bits to packet data */
614
615 bfh.bfType = 0x4d42; /* "BM" in little-endian */
616 bfh.bfSize = file_size;
617 bfh.bfReserved1 = 0;
618 bfh.bfReserved2 = 0;
619 bfh.bfOffBits = gdigrab->header_size;
620
621 memcpy(pkt->data, &bfh, sizeof(bfh));
622
623 memcpy(pkt->data + sizeof(bfh), &gdigrab->bmi.bmiHeader, sizeof(gdigrab->bmi.bmiHeader));
624
625 if (gdigrab->bmi.bmiHeader.biBitCount <= 8)
626 GetDIBColorTable(dest_hdc, 0, 1 << gdigrab->bmi.bmiHeader.biBitCount,
627 (RGBQUAD *) (pkt->data + sizeof(bfh) + sizeof(gdigrab->bmi.bmiHeader)));
628
629 memcpy(pkt->data + gdigrab->header_size, gdigrab->buffer, gdigrab->frame_size);
630
631 gdigrab->time_frame = time_frame;
632
633 return gdigrab->header_size + gdigrab->frame_size;
634 }
635
636 /**
637 * Closes gdi frame grabber (public device demuxer API).
638 *
639 * @param s1 Context from avformat core
640 * @return 0 success, !0 failure
641 */
642 static int gdigrab_read_close(AVFormatContext *s1)
643 {
644 struct gdigrab *s = s1->priv_data;
645
646 if (s->show_region)
647 gdigrab_region_wnd_destroy(s1, s);
648
649 if (s->source_hdc)
650 ReleaseDC(s->hwnd, s->source_hdc);
651 if (s->dest_hdc)
652 DeleteDC(s->dest_hdc);
653 if (s->hbmp)
654 DeleteObject(s->hbmp);
655 if (s->source_hdc)
656 DeleteDC(s->source_hdc);
657
658 return 0;
659 }
660
661 #define OFFSET(x) offsetof(struct gdigrab, x)
662 #define DEC AV_OPT_FLAG_DECODING_PARAM
663 static const AVOption options[] = {
664 { "draw_mouse", "draw the mouse pointer", OFFSET(draw_mouse), AV_OPT_TYPE_INT, {.i64 = 1}, 0, 1, DEC },
665 { "show_region", "draw border around capture area", OFFSET(show_region), AV_OPT_TYPE_INT, {.i64 = 0}, 0, 1, DEC },
666 { "framerate", "set video frame rate", OFFSET(framerate), AV_OPT_TYPE_VIDEO_RATE, {.str = "ntsc"}, 0, INT_MAX, DEC },
667 { "video_size", "set video frame size", OFFSET(width), AV_OPT_TYPE_IMAGE_SIZE, {.str = NULL}, 0, 0, DEC },
668 { "offset_x", "capture area x offset", OFFSET(offset_x), AV_OPT_TYPE_INT, {.i64 = 0}, INT_MIN, INT_MAX, DEC },
669 { "offset_y", "capture area y offset", OFFSET(offset_y), AV_OPT_TYPE_INT, {.i64 = 0}, INT_MIN, INT_MAX, DEC },
670 { NULL },
671 };
672
673 static const AVClass gdigrab_class = {
674 .class_name = "GDIgrab indev",
675 .item_name = av_default_item_name,
676 .option = options,
677 .version = LIBAVUTIL_VERSION_INT,
678 .category = AV_CLASS_CATEGORY_DEVICE_VIDEO_INPUT,
679 };
680
681 /** gdi grabber device demuxer declaration */
682 const FFInputFormat ff_gdigrab_demuxer = {
683 .p.name = "gdigrab",
684 .p.long_name = NULL_IF_CONFIG_SMALL("GDI API Windows frame grabber"),
685 .p.flags = AVFMT_NOFILE,
686 .p.priv_class = &gdigrab_class,
687 .priv_data_size = sizeof(struct gdigrab),
688 .read_header = gdigrab_read_header,
689 .read_packet = gdigrab_read_packet,
690 .read_close = gdigrab_read_close,
691 };