Fix vf_tcdump's compilation
[mplayer/kovensky.git] / libvo / vo_kva.c
blob605d3730cbd0ca92378c02f0ab42ab1b14a78e3b
1 /*
2 * OS/2 video output driver
4 * Copyright (c) 2007-2009 by KO Myung-Hun (komh@chollian.net)
6 * This file is part of MPlayer.
8 * MPlayer is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
13 * MPlayer is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
18 * You should have received a copy of the GNU General Public License along
19 * with MPlayer; if not, write to the Free Software Foundation, Inc.,
20 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
23 #define INCL_WIN
24 #define INCL_GPI
25 #define INCL_DOS
26 #include <os2.h>
28 #include <mmioos2.h>
29 #include <fourcc.h>
31 #include <stdio.h>
32 #include <stdlib.h>
33 #include <string.h>
34 #include <float.h>
36 #include <kva.h>
38 #include "config.h"
39 #include "mp_msg.h"
40 #include "video_out.h"
41 #include "video_out_internal.h"
42 #include "aspect.h"
44 #include "fastmemcpy.h"
45 #include "mp_fifo.h"
46 #include "osdep/keycodes.h"
47 #include "input/input.h"
48 #include "input/mouse.h"
49 #include "subopt-helper.h"
50 #include "sub.h"
52 #include "cpudetect.h"
53 #include "libswscale/swscale.h"
54 #include "libmpcodecs/vf_scale.h"
56 static const vo_info_t info = {
57 "SNAP/WarpOverlay!/DIVE video output",
58 "kva",
59 "KO Myung-Hun <komh@chollian.net>",
63 LIBVO_EXTERN(kva)
65 #define WC_MPLAYER "WC_MPLAYER"
67 #define SRC_WIDTH m_int.kvas.szlSrcSize.cx
68 #define SRC_HEIGHT m_int.kvas.szlSrcSize.cy
70 #define HWNDFROMWINID(wid) ((wid) + 0x80000000UL)
72 static const struct keymap m_vk_map[] = {
73 {VK_NEWLINE, KEY_ENTER}, {VK_TAB, KEY_TAB}, {VK_SPACE, ' '},
75 // control keys
76 {VK_CTRL, KEY_CTRL}, {VK_BACKSPACE, KEY_BS},
77 {VK_DELETE, KEY_DELETE}, {VK_INSERT, KEY_INSERT},
78 {VK_HOME, KEY_HOME}, {VK_END, KEY_END},
79 {VK_PAGEUP, KEY_PAGE_UP}, {VK_PAGEDOWN, KEY_PAGE_DOWN},
80 {VK_ESC, KEY_ESC},
82 // cursor keys
83 {VK_RIGHT, KEY_RIGHT}, {VK_LEFT, KEY_LEFT},
84 {VK_DOWN, KEY_DOWN}, {VK_UP, KEY_UP},
86 // function keys
87 {VK_F1, KEY_F+1}, {VK_F2, KEY_F+2}, {VK_F3, KEY_F+3}, {VK_F4, KEY_F+4},
88 {VK_F5, KEY_F+5}, {VK_F6, KEY_F+6}, {VK_F7, KEY_F+7}, {VK_F8, KEY_F+8},
89 {VK_F9, KEY_F+9}, {VK_F10, KEY_F+10}, {VK_F11, KEY_F+11}, {VK_F12, KEY_F+12},
91 {0, 0}
94 static const struct keymap m_keypad_map[] = {
95 // keypad keys
96 {0x52, KEY_KP0}, {0x4F, KEY_KP1}, {0x50, KEY_KP2}, {0x51, KEY_KP3},
97 {0x4B, KEY_KP4}, {0x4C, KEY_KP5}, {0x4D, KEY_KP6}, {0x47, KEY_KP7},
98 {0x48, KEY_KP8}, {0x49, KEY_KP9}, {0x53, KEY_KPDEC}, {0x5A, KEY_KPENTER},
100 {0, 0}
103 static const struct keymap m_mouse_map[] = {
104 {WM_BUTTON1DOWN, MOUSE_BTN0},
105 {WM_BUTTON3DOWN, MOUSE_BTN1},
106 {WM_BUTTON2DOWN, MOUSE_BTN2},
107 {WM_BUTTON1DBLCLK, MOUSE_BTN0_DBL},
108 {WM_BUTTON3DBLCLK, MOUSE_BTN1_DBL},
109 {WM_BUTTON2DBLCLK, MOUSE_BTN2_DBL},
111 {0, 0}
114 struct {
115 HAB hab;
116 HMQ hmq;
117 HWND hwndFrame;
118 HWND hwndClient;
119 HWND hwndSysMenu;
120 HWND hwndTitleBar;
121 HWND hwndMinMax;
122 FOURCC fcc;
123 int iImageFormat;
124 int nChromaShift;
125 KVASETUP kvas;
126 KVACAPS kvac;
127 RECTL rclDst;
128 int bpp;
129 LONG lStride;
130 PBYTE pbImage;
131 BOOL fFixT23;
132 PFNWP pfnwpOldFrame;
133 uint8_t *planes[MP_MAX_PLANES]; // y = 0, u = 1, v = 2
134 int stride[MP_MAX_PLANES];
135 BOOL fHWAccel;
136 RECTL rclParent;
137 struct SwsContext *sws;
138 } m_int;
140 static inline void setAspectRatio(ULONG ulRatio)
142 m_int.kvas.ulRatio = ulRatio;
143 kvaSetup(&m_int.kvas);
146 static int query_format_info(int format, PBOOL pfHWAccel, PFOURCC pfcc,
147 int *pbpp, int *pnChromaShift)
149 BOOL fHWAccel;
150 FOURCC fcc;
151 INT bpp;
152 INT nChromaShift;
154 switch (format) {
155 case IMGFMT_YV12:
156 fHWAccel = m_int.kvac.ulInputFormatFlags & KVAF_YV12;
157 fcc = FOURCC_YV12;
158 bpp = 1;
159 nChromaShift = 1;
160 break;
162 case IMGFMT_YUY2:
163 fHWAccel = m_int.kvac.ulInputFormatFlags & KVAF_YUY2;
164 fcc = FOURCC_Y422;
165 bpp = 2;
166 nChromaShift = 0;
167 break;
169 case IMGFMT_YVU9:
170 fHWAccel = m_int.kvac.ulInputFormatFlags & KVAF_YVU9;
171 fcc = FOURCC_YVU9;
172 bpp = 1;
173 nChromaShift = 2;
174 break;
176 case IMGFMT_BGR24:
177 fHWAccel = m_int.kvac.ulInputFormatFlags & KVAF_BGR24;
178 fcc = FOURCC_BGR3;
179 bpp = 3;
180 nChromaShift = 0;
181 break;
183 case IMGFMT_BGR16:
184 fHWAccel = m_int.kvac.ulInputFormatFlags & KVAF_BGR16;
185 fcc = FOURCC_R565;
186 bpp = 2;
187 nChromaShift = 0;
188 break;
190 case IMGFMT_BGR15:
191 fHWAccel = m_int.kvac.ulInputFormatFlags & KVAF_BGR15;
192 fcc = FOURCC_R555;
193 bpp = 2;
194 nChromaShift = 0;
195 break;
197 default:
198 return 1;
201 if (pfHWAccel)
202 *pfHWAccel = fHWAccel;
204 if (pfcc)
205 *pfcc = fcc;
207 if (pbpp)
208 *pbpp = bpp;
210 if (pnChromaShift)
211 *pnChromaShift = nChromaShift;
213 return 0;
216 static void imgCreate(void)
218 int size = SRC_HEIGHT * m_int.lStride;;
220 switch (m_int.iImageFormat) {
221 case IMGFMT_YV12:
222 size += size / 2;
223 break;
225 case IMGFMT_YVU9:
226 size += size / 8;
227 break;
230 m_int.pbImage = malloc(size);
232 memset(m_int.planes, 0, sizeof(m_int.planes));
233 memset(m_int.stride, 0, sizeof(m_int.stride));
234 m_int.planes[0] = m_int.pbImage;
235 m_int.stride[0] = m_int.lStride;
237 // YV12 or YVU9 ?
238 if (m_int.nChromaShift) {
239 m_int.planes[1] = m_int.planes[0] + SRC_HEIGHT * m_int.stride[0];
240 m_int.stride[1] = m_int.stride[0] >> m_int.nChromaShift;
242 m_int.planes[2] = m_int.planes[1] +
243 (SRC_HEIGHT >> m_int.nChromaShift) * m_int.stride[1];
244 m_int.stride[2] = m_int.stride[1];
248 static void imgFree(void)
250 free(m_int.pbImage);
252 m_int.pbImage = NULL;
255 static void imgDisplay(void)
257 PVOID pBuffer;
258 ULONG ulBPL;
260 if (!kvaLockBuffer(&pBuffer, &ulBPL)) {
261 uint8_t *dst[MP_MAX_PLANES] = {NULL};
262 int dstStride[MP_MAX_PLANES] = {0};
264 // Get packed or Y
265 dst[0] = pBuffer;
266 dstStride[0] = ulBPL;
268 // YV12 or YVU9 ?
269 if (m_int.nChromaShift) {
270 // Get V
271 dst[2] = dst[0] + SRC_HEIGHT * dstStride[0];
272 dstStride[2] = dstStride[0] >> m_int.nChromaShift;
274 // Get U
275 dst[1] = dst[2] +
276 (SRC_HEIGHT >> m_int.nChromaShift ) * dstStride[2];
277 dstStride[1] = dstStride[2];
280 if (m_int.fHWAccel) {
281 int w, h;
283 w = m_int.stride[0];
284 h = SRC_HEIGHT;
286 // Copy packed or Y
287 mem2agpcpy_pic(dst[0], m_int.planes[0], w, h,
288 dstStride[0], m_int.stride[0]);
290 // YV12 or YVU9 ?
291 if (m_int.nChromaShift) {
292 w >>= m_int.nChromaShift; h >>= m_int.nChromaShift;
294 // Copy U
295 mem2agpcpy_pic(dst[1], m_int.planes[1], w, h,
296 dstStride[1], m_int.stride[1]);
298 // Copy V
299 mem2agpcpy_pic(dst[2], m_int.planes[2], w, h,
300 dstStride[2], m_int.stride[2]);
302 } else {
303 sws_scale(m_int.sws, m_int.planes, m_int.stride, 0, SRC_HEIGHT,
304 dst, dstStride);
307 kvaUnlockBuffer();
311 // Frame window procedure to work around T23 laptop with S3 video card,
312 // which supports upscaling only.
313 static MRESULT EXPENTRY NewFrameWndProc(HWND hwnd, ULONG msg, MPARAM mp1,
314 MPARAM mp2)
316 switch (msg) {
317 case WM_QUERYTRACKINFO:
319 PTRACKINFO pti = (PTRACKINFO)mp2;
320 RECTL rcl;
322 if (vo_fs)
323 break;
325 m_int.pfnwpOldFrame(hwnd, msg, mp1, mp2);
327 rcl.xLeft = 0;
328 rcl.yBottom = 0;
329 rcl.xRight = SRC_WIDTH + 1;
330 rcl.yTop = SRC_HEIGHT + 1;
332 WinCalcFrameRect(hwnd, &rcl, FALSE);
334 pti->ptlMinTrackSize.x = rcl.xRight - rcl.xLeft;
335 pti->ptlMinTrackSize.y = rcl.yTop - rcl.yBottom;
337 pti->ptlMaxTrackSize.x = vo_screenwidth;
338 pti->ptlMaxTrackSize.y = vo_screenheight;
340 return (MRESULT)TRUE;
343 case WM_ADJUSTWINDOWPOS:
345 PSWP pswp = (PSWP)mp1;
346 RECTL rcl;
348 if (vo_fs)
349 break;
351 if (pswp->fl & SWP_SIZE) {
352 rcl.xLeft = pswp->x;
353 rcl.yBottom = pswp->y;
354 rcl.xRight = rcl.xLeft + pswp->cx;
355 rcl.yTop = rcl.yBottom + pswp->cy;
357 WinCalcFrameRect(hwnd, &rcl, TRUE);
359 if (rcl.xRight - rcl.xLeft <= SRC_WIDTH)
360 rcl.xRight = rcl.xLeft + (SRC_WIDTH + 1);
362 if (rcl.yTop - rcl.yBottom <= SRC_HEIGHT)
363 rcl.yTop = rcl.yBottom + (SRC_HEIGHT + 1);
365 WinCalcFrameRect(hwnd, &rcl, FALSE);
367 if (rcl.xRight - rcl.xLeft > vo_screenwidth) {
368 rcl.xLeft = 0;
369 rcl.xRight = vo_screenwidth;
372 if (rcl.yTop - rcl.yBottom > vo_screenheight) {
373 rcl.yBottom = 0;
374 rcl.yTop = vo_screenheight;
377 pswp->fl |= SWP_MOVE;
378 pswp->x = rcl.xLeft;
379 pswp->y = rcl.yBottom;
380 pswp->cx = rcl.xRight - rcl.xLeft;
381 pswp->cy = rcl.yTop - rcl.yBottom;
383 break;
387 return m_int.pfnwpOldFrame(hwnd, msg, mp1, mp2);
390 static MRESULT EXPENTRY WndProc(HWND hwnd, ULONG msg, MPARAM mp1, MPARAM mp2)
392 // if slave mode, ignore mouse events and deliver them to a parent window
393 if (WinID != -1 &&
394 ((msg >= WM_MOUSEFIRST && msg <= WM_MOUSELAST) ||
395 (msg >= WM_EXTMOUSEFIRST && msg <= WM_EXTMOUSELAST))) {
396 WinPostMsg(HWNDFROMWINID(WinID), msg, mp1, mp2);
398 return (MRESULT)TRUE;
401 switch (msg) {
402 case WM_CLOSE:
403 mplayer_put_key(KEY_CLOSE_WIN);
405 return 0;
407 case WM_CHAR:
409 USHORT fsFlags = SHORT1FROMMP(mp1);
410 UCHAR uchScan = CHAR4FROMMP(mp1);
411 USHORT usCh = SHORT1FROMMP(mp2);
412 USHORT usVk = SHORT2FROMMP(mp2);
413 int mpkey;
415 if (fsFlags & KC_KEYUP)
416 break;
418 if (fsFlags & KC_SCANCODE) {
419 mpkey = lookup_keymap_table(m_keypad_map, uchScan);
420 if (mpkey) {
421 // distinguish KEY_KP0 and KEY_KPINS
422 if (mpkey == KEY_KP0 && usCh != '0')
423 mpkey = KEY_KPINS;
425 // distinguish KEY_KPDEC and KEY_KPDEL
426 if (mpkey == KEY_KPDEC && usCh != '.')
427 mpkey = KEY_KPDEL;
429 mplayer_put_key(mpkey);
431 return (MRESULT)TRUE;
435 if (fsFlags & KC_VIRTUALKEY) {
436 mpkey = lookup_keymap_table(m_vk_map, usVk);
437 if (mpkey) {
438 mplayer_put_key(mpkey);
440 return (MRESULT)TRUE;
444 if ((fsFlags & KC_CHAR) && !HIBYTE(usCh))
445 mplayer_put_key(usCh);
447 return (MRESULT)TRUE;
450 case WM_BUTTON1DOWN:
451 case WM_BUTTON3DOWN:
452 case WM_BUTTON2DOWN:
453 case WM_BUTTON1DBLCLK:
454 case WM_BUTTON3DBLCLK:
455 case WM_BUTTON2DBLCLK:
456 if (WinQueryFocus(HWND_DESKTOP) != hwnd)
457 WinSetFocus(HWND_DESKTOP, hwnd);
458 else if (!vo_nomouse_input)
459 mplayer_put_key(lookup_keymap_table(m_mouse_map, msg));
461 return (MRESULT)TRUE;
463 case WM_PAINT:
465 HPS hps;
466 RECTL rcl, rclDst;
467 PRECTL prcl = NULL;
468 HRGN hrgn, hrgnDst;
469 RGNRECT rgnCtl;
471 // get a current movie area
472 kvaAdjustDstRect(&m_int.kvas.rclSrcRect, &rclDst);
474 // get a current invalidated area
475 hps = WinBeginPaint(hwnd, NULLHANDLE, &rcl);
477 // create a region for an invalidated area
478 hrgn = GpiCreateRegion(hps, 1, &rcl);
479 // create a region for a movie area
480 hrgnDst = GpiCreateRegion(hps, 1, &rclDst);
482 // exclude a movie area from an invalidated area
483 GpiCombineRegion(hps, hrgn, hrgn, hrgnDst, CRGN_DIFF);
485 // get rectangles from the region
486 rgnCtl.ircStart = 1;
487 rgnCtl.ulDirection = RECTDIR_LFRT_TOPBOT;
488 GpiQueryRegionRects(hps, hrgn, NULL, &rgnCtl, NULL);
490 if (rgnCtl.crcReturned > 0) {
491 rgnCtl.crc = rgnCtl.crcReturned;
492 prcl = malloc(sizeof(RECTL) * rgnCtl.crcReturned);
495 // draw black bar if needed
496 if (prcl && GpiQueryRegionRects(hps, hrgn, NULL, &rgnCtl, prcl)) {
497 int i;
499 for (i = 0; i < rgnCtl.crcReturned; i++)
500 WinFillRect(hps, &prcl[i], CLR_BLACK);
503 free(prcl);
505 GpiDestroyRegion(hps, hrgnDst);
506 GpiDestroyRegion(hps, hrgn);
508 WinEndPaint(hps);
510 return 0;
514 return WinDefWindowProc(hwnd, msg, mp1, mp2);
517 // Change process type from VIO to PM to use PM APIs.
518 static void morphToPM(void)
520 PPIB pib;
522 DosGetInfoBlocks(NULL, &pib);
524 // Change flag from VIO to PM:
525 if (pib->pib_ultype == 2)
526 pib->pib_ultype = 3;
529 static int preinit(const char *arg)
531 HWND hwndParent;
532 ULONG flFrameFlags;
533 ULONG kvaMode = 0;
535 int fUseSnap = 0;
536 int fUseWO = 0;
537 int fUseDive = 0;
538 int fFixT23 = 0;
540 const opt_t subopts[] = {
541 {"snap", OPT_ARG_BOOL, &fUseSnap, NULL},
542 {"wo", OPT_ARG_BOOL, &fUseWO, NULL},
543 {"dive", OPT_ARG_BOOL, &fUseDive, NULL},
544 {"t23", OPT_ARG_BOOL, &fFixT23, NULL},
545 {NULL, 0, NULL, NULL}
548 PCSZ pcszVideoModeStr[3] = {"DIVE", "WarpOverlay!", "SNAP"};
550 if (subopt_parse(arg, subopts) != 0)
551 return -1;
553 morphToPM();
555 memset(&m_int, 0, sizeof(m_int));
557 m_int.hab = WinInitialize(0);
558 m_int.hmq = WinCreateMsgQueue(m_int.hab, 0);
560 WinRegisterClass(m_int.hab,
561 WC_MPLAYER,
562 WndProc,
563 CS_SIZEREDRAW | CS_MOVENOTIFY,
564 sizeof(PVOID));
566 if (WinID == -1) {
567 hwndParent = HWND_DESKTOP;
568 flFrameFlags = FCF_SYSMENU | FCF_TITLEBAR | FCF_MINMAX |
569 FCF_SIZEBORDER | FCF_TASKLIST;
570 } else {
571 hwndParent = HWNDFROMWINID(WinID);
572 flFrameFlags = 0;
575 m_int.hwndFrame =
576 WinCreateStdWindow(hwndParent, // parent window handle
577 WS_VISIBLE, // frame window style
578 &flFrameFlags, // window style
579 WC_MPLAYER, // class name
580 "", // window title
581 0L, // default client style
582 NULLHANDLE, // resource in exe file
583 1, // frame window id
584 &m_int.hwndClient); // client window handle
586 if (m_int.hwndFrame == NULLHANDLE)
587 return -1;
589 m_int.hwndSysMenu = WinWindowFromID(m_int.hwndFrame, FID_SYSMENU);
590 m_int.hwndTitleBar = WinWindowFromID(m_int.hwndFrame, FID_TITLEBAR);
591 m_int.hwndMinMax = WinWindowFromID(m_int.hwndFrame, FID_MINMAX);
593 m_int.fFixT23 = fFixT23;
595 if (m_int.fFixT23)
596 m_int.pfnwpOldFrame = WinSubclassWindow(m_int.hwndFrame,
597 NewFrameWndProc);
599 if (!!fUseSnap + !!fUseWO + !!fUseDive > 1)
600 mp_msg(MSGT_VO, MSGL_WARN,"KVA: Multiple mode specified!!!\n");
602 if (fUseSnap)
603 kvaMode = KVAM_SNAP;
604 else if (fUseWO)
605 kvaMode = KVAM_WO;
606 else if (fUseDive)
607 kvaMode = KVAM_DIVE;
608 else
609 kvaMode = KVAM_AUTO;
611 if (kvaInit(kvaMode, m_int.hwndClient, vo_colorkey)) {
612 mp_msg(MSGT_VO, MSGL_ERR, "KVA: Init failed!!!\n");
614 return -1;
617 kvaCaps(&m_int.kvac);
619 mp_msg(MSGT_VO, MSGL_V, "KVA: Selected video mode = %s\n",
620 pcszVideoModeStr[m_int.kvac.ulMode - 1]);
622 kvaDisableScreenSaver();
624 // Might cause PM DLLs to be loaded which incorrectly enable SIG_FPE,
625 // so mask off all floating-point exceptions.
626 _control87(MCW_EM, MCW_EM);
628 return 0;
631 static void uninit(void)
633 kvaEnableScreenSaver();
635 imgFree();
637 sws_freeContext(m_int.sws);
639 if (m_int.hwndFrame != NULLHANDLE) {
640 kvaResetAttr();
641 kvaDone();
643 if (m_int.fFixT23)
644 WinSubclassWindow(m_int.hwndFrame, m_int.pfnwpOldFrame);
646 WinDestroyWindow(m_int.hwndFrame);
649 WinDestroyMsgQueue(m_int.hmq);
650 WinTerminate(m_int.hab);
653 static int config(uint32_t width, uint32_t height,
654 uint32_t d_width, uint32_t d_height,
655 uint32_t flags, char *title, uint32_t format)
657 RECTL rcl;
659 mp_msg(MSGT_VO, MSGL_V,
660 "KVA: Using 0x%X (%s) image format, vo_config_count = %d\n",
661 format, vo_format_name(format), vo_config_count);
663 imgFree();
665 if (query_format_info(format, &m_int.fHWAccel, &m_int.fcc, &m_int.bpp,
666 &m_int.nChromaShift))
667 return 1;
669 m_int.iImageFormat = format;
671 // if there is no hw accel for given format,
672 // try any format supported by hw accel
673 if (!m_int.fHWAccel) {
674 int dstFormat = 0;
676 sws_freeContext(m_int.sws);
678 if (m_int.kvac.ulInputFormatFlags & KVAF_YV12)
679 dstFormat = IMGFMT_YV12;
680 else if (m_int.kvac.ulInputFormatFlags & KVAF_YUY2)
681 dstFormat = IMGFMT_YUY2;
682 else if (m_int.kvac.ulInputFormatFlags & KVAF_YVU9)
683 dstFormat = IMGFMT_YVU9;
684 else if (m_int.kvac.ulInputFormatFlags & KVAF_BGR24)
685 dstFormat = IMGFMT_BGR24;
686 else if (m_int.kvac.ulInputFormatFlags & KVAF_BGR16)
687 dstFormat = IMGFMT_BGR16;
688 else if (m_int.kvac.ulInputFormatFlags & KVAF_BGR15)
689 dstFormat = IMGFMT_BGR15;
691 if (query_format_info(dstFormat, NULL, &m_int.fcc, NULL, NULL))
692 return 1;
694 m_int.sws = sws_getContextFromCmdLine(width, height, format,
695 width, height, dstFormat);
698 mp_msg(MSGT_VO, MSGL_V, "KVA: Selected FOURCC = %.4s\n", (char *)&m_int.fcc);
700 m_int.kvas.ulLength = sizeof(KVASETUP);
701 m_int.kvas.szlSrcSize.cx = width;
702 m_int.kvas.szlSrcSize.cy = height;
703 m_int.kvas.rclSrcRect.xLeft = 0;
704 m_int.kvas.rclSrcRect.yTop = 0;
705 m_int.kvas.rclSrcRect.xRight = width;
706 m_int.kvas.rclSrcRect.yBottom = height;
707 m_int.kvas.ulRatio = vo_keepaspect ? KVAR_FORCEANY : KVAR_NONE;
708 m_int.kvas.ulAspectWidth = d_width;
709 m_int.kvas.ulAspectHeight = d_height;
710 m_int.kvas.fccSrcColor = m_int.fcc;
711 m_int.kvas.fDither = TRUE;
713 if (kvaSetup(&m_int.kvas)) {
714 mp_msg(MSGT_VO, MSGL_ERR, "KVA: Setup failed!!!\n");
716 return 1;
719 m_int.lStride = width * m_int.bpp;
721 imgCreate();
723 if (WinID == -1) {
724 WinSetWindowText(m_int.hwndFrame, title);
726 // initialize 'vo_fs' only once at first config() call
727 if (vo_config_count == 0)
728 vo_fs = flags & VOFLAG_FULLSCREEN;
730 // workaround for T23 laptop with S3 Video by Franz Bakan
731 if (!vo_fs && m_int.fFixT23) {
732 d_width++;
733 d_height++;
736 m_int.rclDst.xLeft = ((LONG)vo_screenwidth - (LONG)d_width) / 2;
737 m_int.rclDst.yBottom = ((LONG)vo_screenheight - (LONG)d_height) / 2;
738 m_int.rclDst.xRight = m_int.rclDst.xLeft + d_width;
739 m_int.rclDst.yTop = m_int.rclDst.yBottom + d_height;
741 if (vo_fs) {
742 d_width = vo_screenwidth;
743 d_height = vo_screenheight;
745 // when -fs option is used without this, title bar is not highlighted
746 WinSetActiveWindow(HWND_DESKTOP, m_int.hwndFrame);
748 WinSetParent(m_int.hwndSysMenu, HWND_OBJECT, FALSE);
749 WinSetParent(m_int.hwndTitleBar, HWND_OBJECT, FALSE);
750 WinSetParent(m_int.hwndMinMax, HWND_OBJECT, FALSE);
752 setAspectRatio(KVAR_FORCEANY);
755 rcl.xLeft = ((LONG)vo_screenwidth - (LONG)d_width) / 2;
756 rcl.yBottom = ((LONG)vo_screenheight - (LONG)d_height) /2 ;
757 rcl.xRight = rcl.xLeft + d_width;
758 rcl.yTop = rcl.yBottom + d_height;
759 } else {
760 vo_fs = 0;
762 WinQueryWindowRect(HWNDFROMWINID(WinID), &m_int.rclDst);
763 rcl = m_int.rclDst;
766 WinCalcFrameRect(m_int.hwndFrame, &rcl, FALSE);
768 WinSetWindowPos(m_int.hwndFrame, HWND_TOP,
769 rcl.xLeft, rcl.yBottom,
770 rcl.xRight - rcl.xLeft, rcl.yTop - rcl.yBottom,
771 SWP_SIZE | SWP_MOVE | SWP_ZORDER | SWP_SHOW |
772 (WinID == -1 ? SWP_ACTIVATE : 0));
774 WinInvalidateRect(m_int.hwndFrame, NULL, TRUE);
776 return 0;
779 static uint32_t get_image(mp_image_t *mpi)
781 if (m_int.iImageFormat != mpi->imgfmt)
782 return VO_FALSE;
784 if (mpi->type == MP_IMGTYPE_STATIC || mpi->type == MP_IMGTYPE_TEMP) {
785 if (mpi->flags & MP_IMGFLAG_PLANAR) {
786 mpi->planes[1] = m_int.planes[1];
787 mpi->planes[2] = m_int.planes[2];
789 mpi->stride[1] = m_int.stride[1];
790 mpi->stride[2] = m_int.stride[2];
793 mpi->planes[0] = m_int.planes[0];
794 mpi->stride[0] = m_int.stride[0];
795 mpi->flags |= MP_IMGFLAG_DIRECT;
797 return VO_TRUE;
800 return VO_FALSE;
803 static uint32_t draw_image(mp_image_t *mpi)
805 // if -dr or -slices then do nothing:
806 if (mpi->flags & (MP_IMGFLAG_DIRECT | MP_IMGFLAG_DRAW_CALLBACK))
807 return VO_TRUE;
809 draw_slice(mpi->planes, mpi->stride, mpi->w, mpi->h, mpi->x, mpi->y);
811 return VO_TRUE;
814 static int query_format(uint32_t format)
816 BOOL fHWAccel;
817 int res;
819 if (query_format_info(format, &fHWAccel, NULL, NULL, NULL))
820 return 0;
822 res = VFCAP_CSP_SUPPORTED | VFCAP_OSD;
823 if (fHWAccel) {
824 res |= VFCAP_CSP_SUPPORTED_BY_HW | VFCAP_HWSCALE_UP;
826 if (!m_int.fFixT23)
827 res |= VFCAP_HWSCALE_DOWN;
830 return res;
833 static int fs_toggle(void)
835 RECTL rcl;
837 vo_fs = !vo_fs;
839 if (vo_fs) {
840 SWP swp;
842 WinQueryWindowPos(m_int.hwndFrame, &swp);
843 m_int.rclDst.xLeft = swp.x;
844 m_int.rclDst.yBottom = swp.y;
845 m_int.rclDst.xRight = m_int.rclDst.xLeft + swp.cx;
846 m_int.rclDst.yTop = m_int.rclDst.yBottom + swp.cy;
847 WinCalcFrameRect(m_int.hwndFrame, &m_int.rclDst, TRUE);
849 if (WinID != -1)
850 WinSetParent(m_int.hwndFrame, HWND_DESKTOP, FALSE);
852 WinSetParent(m_int.hwndSysMenu, HWND_OBJECT, FALSE);
853 WinSetParent(m_int.hwndTitleBar, HWND_OBJECT, FALSE);
854 WinSetParent(m_int.hwndMinMax, HWND_OBJECT, FALSE);
856 rcl.xLeft = 0;
857 rcl.yBottom = 0;
858 rcl.xRight = vo_screenwidth;
859 rcl.yTop = vo_screenheight;
861 setAspectRatio(KVAR_FORCEANY);
862 } else {
863 if (WinID != -1)
864 WinSetParent(m_int.hwndFrame, HWNDFROMWINID(WinID), TRUE);
866 WinSetParent(m_int.hwndSysMenu, m_int.hwndFrame, FALSE);
867 WinSetParent(m_int.hwndTitleBar, m_int.hwndFrame, FALSE);
868 WinSetParent(m_int.hwndMinMax, m_int.hwndFrame, FALSE);
870 rcl = m_int.rclDst;
872 setAspectRatio(vo_keepaspect ? KVAR_FORCEANY : KVAR_NONE);
875 WinCalcFrameRect(m_int.hwndFrame, &rcl, FALSE);
877 WinSetWindowPos(m_int.hwndFrame, HWND_TOP,
878 rcl.xLeft, rcl.yBottom,
879 rcl.xRight - rcl.xLeft, rcl.yTop - rcl.yBottom,
880 SWP_SIZE | SWP_MOVE | SWP_ZORDER | SWP_SHOW |
881 (WinID == -1 ? SWP_ACTIVATE : 0));
883 return VO_TRUE;
886 static int color_ctrl_set(char *what, int value)
888 ULONG ulAttr;
889 ULONG ulValue;
891 if (!strcmp(what, "brightness"))
892 ulAttr = KVAA_BRIGHTNESS;
893 else if (!strcmp(what, "contrast"))
894 ulAttr = KVAA_CONTRAST;
895 else if (!strcmp(what, "hue"))
896 ulAttr = KVAA_HUE;
897 else if (!strcmp(what, "saturation"))
898 ulAttr = KVAA_SATURATION;
899 else
900 return VO_NOTIMPL;
902 ulValue = (value + 100) * 255 / 200;
904 if (kvaSetAttr(ulAttr, &ulValue))
905 return VO_NOTIMPL;
907 return VO_TRUE;
910 static int color_ctrl_get(char *what, int *value)
912 ULONG ulAttr;
913 ULONG ulValue;
915 if (!strcmp(what, "brightness"))
916 ulAttr = KVAA_BRIGHTNESS;
917 else if (!strcmp(what, "contrast"))
918 ulAttr = KVAA_CONTRAST;
919 else if (!strcmp(what, "hue"))
920 ulAttr = KVAA_HUE;
921 else if (!strcmp(what, "saturation"))
922 ulAttr = KVAA_SATURATION;
923 else
924 return VO_NOTIMPL;
926 if (kvaQueryAttr(ulAttr, &ulValue))
927 return VO_NOTIMPL;
929 // add 1 to adjust range
930 *value = ((ulValue + 1) * 200 / 255) - 100;
932 return VO_TRUE;
935 static int control(uint32_t request, void *data)
937 switch (request) {
938 case VOCTRL_GET_IMAGE:
939 return get_image(data);
941 case VOCTRL_DRAW_IMAGE:
942 return draw_image(data);
944 case VOCTRL_QUERY_FORMAT:
945 return query_format(*(uint32_t *)data);
947 case VOCTRL_FULLSCREEN:
948 return fs_toggle();
950 case VOCTRL_SET_EQUALIZER:
952 struct voctrl_set_equalizer_args *args = data;
953 color_ctrl_set(args->name, args->value);
956 case VOCTRL_GET_EQUALIZER:
958 struct voctrl_get_equalizer_args *args = data;
959 return color_ctrl_get(args->name, args->valueptr);
962 case VOCTRL_UPDATE_SCREENINFO:
963 vo_screenwidth = m_int.kvac.cxScreen;
964 vo_screenheight = m_int.kvac.cyScreen;
966 aspect_save_screenres(vo_screenwidth, vo_screenheight);
968 return VO_TRUE;
971 return VO_NOTIMPL;
974 static int draw_frame(uint8_t *src[])
976 return VO_ERROR;
979 static int draw_slice(uint8_t *src[], int stride[], int w, int h, int x, int y)
981 uint8_t *s;
982 uint8_t *d;
984 // copy packed or Y
985 d = m_int.planes[0] + m_int.stride[0] * y + x;
986 s = src[0];
987 mem2agpcpy_pic(d, s, w * m_int.bpp, h, m_int.stride[0], stride[0]);
989 // YV12 or YVU9
990 if (m_int.nChromaShift) {
991 w >>= m_int.nChromaShift; h >>= m_int.nChromaShift;
992 x >>= m_int.nChromaShift; y >>= m_int.nChromaShift;
994 // copy U
995 d = m_int.planes[1] + m_int.stride[1] * y + x;
996 s = src[1];
997 mem2agpcpy_pic(d, s, w, h, m_int.stride[1], stride[1]);
999 // copy V
1000 d = m_int.planes[2] + m_int.stride[2] * y + x;
1001 s = src[2];
1002 mem2agpcpy_pic(d, s, w, h, m_int.stride[2], stride[2]);
1005 return 0;
1008 #define vo_draw_alpha(imgfmt) \
1009 vo_draw_alpha_##imgfmt(w, h, src, srca, stride, \
1010 m_int.planes[0] + m_int.stride[0] * y0 + m_int.bpp * x0, \
1011 m_int.stride[0])
1013 static void draw_alpha(int x0, int y0, int w, int h,
1014 unsigned char *src, unsigned char *srca, int stride)
1016 switch (m_int.iImageFormat) {
1017 case IMGFMT_YV12:
1018 case IMGFMT_YVU9:
1019 vo_draw_alpha(yv12);
1020 break;
1022 case IMGFMT_YUY2:
1023 vo_draw_alpha(yuy2);
1024 break;
1026 case IMGFMT_BGR24:
1027 vo_draw_alpha(rgb24);
1028 break;
1030 case IMGFMT_BGR16:
1031 vo_draw_alpha(rgb16);
1032 break;
1034 case IMGFMT_BGR15:
1035 vo_draw_alpha(rgb15);
1036 break;
1040 static void draw_osd(void)
1042 vo_draw_text(SRC_WIDTH, SRC_HEIGHT, draw_alpha);
1045 static void flip_page(void)
1047 imgDisplay();
1050 static void check_events(void)
1052 QMSG qm;
1054 // On slave mode, we need to change our window size according to a
1055 // parent window size
1056 if (WinID != -1) {
1057 RECTL rcl;
1059 WinQueryWindowRect(HWNDFROMWINID(WinID), &rcl);
1061 if (rcl.xLeft != m_int.rclParent.xLeft ||
1062 rcl.yBottom != m_int.rclParent.yBottom ||
1063 rcl.xRight != m_int.rclParent.xRight ||
1064 rcl.yTop != m_int.rclParent.yTop) {
1065 WinSetWindowPos(m_int.hwndFrame, NULLHANDLE,
1066 rcl.xLeft, rcl.yBottom,
1067 rcl.xRight - rcl.xLeft, rcl.yTop - rcl.yBottom,
1068 SWP_SIZE | SWP_MOVE);
1070 m_int.rclParent = rcl;
1074 while (WinPeekMsg(m_int.hab, &qm, NULLHANDLE, 0, 0, PM_REMOVE))
1075 WinDispatchMsg(m_int.hab, &qm);