Add const where appropriate, also gets rid of a compiler warning.
[mplayer/glamo.git] / libvo / vo_kva.c
blob4805d51fbde782b245c48121a427d5bb9363baa6
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 "help_mp.h"
41 #include "video_out.h"
42 #include "video_out_internal.h"
43 #include "aspect.h"
45 #include "fastmemcpy.h"
46 #include "mp_fifo.h"
47 #include "osdep/keycodes.h"
48 #include "input/input.h"
49 #include "input/mouse.h"
50 #include "subopt-helper.h"
51 #include "sub.h"
53 #include "cpudetect.h"
54 #include "libswscale/swscale.h"
55 #include "libmpcodecs/vf_scale.h"
57 static const vo_info_t info = {
58 "SNAP/WarpOverlay!/DIVE video output",
59 "kva",
60 "KO Myung-Hun <komh@chollian.net>",
64 LIBVO_EXTERN(kva)
66 #define WC_MPLAYER "WC_MPLAYER"
68 #define SRC_WIDTH m_int.kvas.szlSrcSize.cx
69 #define SRC_HEIGHT m_int.kvas.szlSrcSize.cy
71 #define HWNDFROMWINID(wid) ((wid) + 0x80000000UL)
73 static const struct keymap m_vk_map[] = {
74 {VK_NEWLINE, KEY_ENTER}, {VK_TAB, KEY_TAB}, {VK_SPACE, ' '},
76 // control keys
77 {VK_CTRL, KEY_CTRL}, {VK_BACKSPACE, KEY_BS},
78 {VK_DELETE, KEY_DELETE}, {VK_INSERT, KEY_INSERT},
79 {VK_HOME, KEY_HOME}, {VK_END, KEY_END},
80 {VK_PAGEUP, KEY_PAGE_UP}, {VK_PAGEDOWN, KEY_PAGE_DOWN},
81 {VK_ESC, KEY_ESC},
83 // cursor keys
84 {VK_RIGHT, KEY_RIGHT}, {VK_LEFT, KEY_LEFT},
85 {VK_DOWN, KEY_DOWN}, {VK_UP, KEY_UP},
87 // function keys
88 {VK_F1, KEY_F+1}, {VK_F2, KEY_F+2}, {VK_F3, KEY_F+3}, {VK_F4, KEY_F+4},
89 {VK_F5, KEY_F+5}, {VK_F6, KEY_F+6}, {VK_F7, KEY_F+7}, {VK_F8, KEY_F+8},
90 {VK_F9, KEY_F+9}, {VK_F10, KEY_F+10}, {VK_F11, KEY_F+11}, {VK_F12, KEY_F+12},
92 {0, 0}
95 static const struct keymap m_keypad_map[] = {
96 // keypad keys
97 {0x52, KEY_KP0}, {0x4F, KEY_KP1}, {0x50, KEY_KP2}, {0x51, KEY_KP3},
98 {0x4B, KEY_KP4}, {0x4C, KEY_KP5}, {0x4D, KEY_KP6}, {0x47, KEY_KP7},
99 {0x48, KEY_KP8}, {0x49, KEY_KP9}, {0x53, KEY_KPDEC}, {0x5A, KEY_KPENTER},
101 {0, 0}
104 static const struct keymap m_mouse_map[] = {
105 {WM_BUTTON1DOWN, MOUSE_BTN0},
106 {WM_BUTTON3DOWN, MOUSE_BTN1},
107 {WM_BUTTON2DOWN, MOUSE_BTN2},
108 {WM_BUTTON1DBLCLK, MOUSE_BTN0_DBL},
109 {WM_BUTTON3DBLCLK, MOUSE_BTN1_DBL},
110 {WM_BUTTON2DBLCLK, MOUSE_BTN2_DBL},
112 {0, 0}
115 struct {
116 HAB hab;
117 HMQ hmq;
118 HWND hwndFrame;
119 HWND hwndClient;
120 HWND hwndSysMenu;
121 HWND hwndTitleBar;
122 HWND hwndMinMax;
123 FOURCC fcc;
124 int iImageFormat;
125 int nChromaShift;
126 KVASETUP kvas;
127 KVACAPS kvac;
128 RECTL rclDst;
129 int bpp;
130 LONG lStride;
131 PBYTE pbImage;
132 BOOL fFixT23;
133 PFNWP pfnwpOldFrame;
134 uint8_t *planes[MP_MAX_PLANES]; // y = 0, u = 1, v = 2
135 int stride[MP_MAX_PLANES];
136 BOOL fHWAccel;
137 RECTL rclParent;
138 struct SwsContext *sws;
139 } m_int;
141 static inline void setAspectRatio(ULONG ulRatio)
143 m_int.kvas.ulRatio = ulRatio;
144 kvaSetup(&m_int.kvas);
147 static int query_format_info(int format, PBOOL pfHWAccel, PFOURCC pfcc,
148 int *pbpp, int *pnChromaShift)
150 BOOL fHWAccel;
151 FOURCC fcc;
152 INT bpp;
153 INT nChromaShift;
155 switch (format) {
156 case IMGFMT_YV12:
157 fHWAccel = m_int.kvac.ulInputFormatFlags & KVAF_YV12;
158 fcc = FOURCC_YV12;
159 bpp = 1;
160 nChromaShift = 1;
161 break;
163 case IMGFMT_YUY2:
164 fHWAccel = m_int.kvac.ulInputFormatFlags & KVAF_YUY2;
165 fcc = FOURCC_Y422;
166 bpp = 2;
167 nChromaShift = 0;
168 break;
170 case IMGFMT_YVU9:
171 fHWAccel = m_int.kvac.ulInputFormatFlags & KVAF_YVU9;
172 fcc = FOURCC_YVU9;
173 bpp = 1;
174 nChromaShift = 2;
175 break;
177 case IMGFMT_BGR24:
178 fHWAccel = m_int.kvac.ulInputFormatFlags & KVAF_BGR24;
179 fcc = FOURCC_BGR3;
180 bpp = 3;
181 nChromaShift = 0;
182 break;
184 case IMGFMT_BGR16:
185 fHWAccel = m_int.kvac.ulInputFormatFlags & KVAF_BGR16;
186 fcc = FOURCC_R565;
187 bpp = 2;
188 nChromaShift = 0;
189 break;
191 case IMGFMT_BGR15:
192 fHWAccel = m_int.kvac.ulInputFormatFlags & KVAF_BGR15;
193 fcc = FOURCC_R555;
194 bpp = 2;
195 nChromaShift = 0;
196 break;
198 default:
199 return 1;
202 if (pfHWAccel)
203 *pfHWAccel = fHWAccel;
205 if (pfcc)
206 *pfcc = fcc;
208 if (pbpp)
209 *pbpp = bpp;
211 if (pnChromaShift)
212 *pnChromaShift = nChromaShift;
214 return 0;
217 static void imgCreate(void)
219 int size = SRC_HEIGHT * m_int.lStride;;
221 switch (m_int.iImageFormat) {
222 case IMGFMT_YV12:
223 size += size / 2;
224 break;
226 case IMGFMT_YVU9:
227 size += size / 8;
228 break;
231 m_int.pbImage = malloc(size);
233 memset(m_int.planes, 0, sizeof(m_int.planes));
234 memset(m_int.stride, 0, sizeof(m_int.stride));
235 m_int.planes[0] = m_int.pbImage;
236 m_int.stride[0] = m_int.lStride;
238 // YV12 or YVU9 ?
239 if (m_int.nChromaShift) {
240 m_int.planes[1] = m_int.planes[0] + SRC_HEIGHT * m_int.stride[0];
241 m_int.stride[1] = m_int.stride[0] >> m_int.nChromaShift;
243 m_int.planes[2] = m_int.planes[1] +
244 (SRC_HEIGHT >> m_int.nChromaShift) * m_int.stride[1];
245 m_int.stride[2] = m_int.stride[1];
249 static void imgFree(void)
251 free(m_int.pbImage);
253 m_int.pbImage = NULL;
256 static void imgDisplay(void)
258 PVOID pBuffer;
259 ULONG ulBPL;
261 if (!kvaLockBuffer(&pBuffer, &ulBPL)) {
262 uint8_t *dst[MP_MAX_PLANES] = {NULL};
263 int dstStride[MP_MAX_PLANES] = {0};
265 // Get packed or Y
266 dst[0] = pBuffer;
267 dstStride[0] = ulBPL;
269 // YV12 or YVU9 ?
270 if (m_int.nChromaShift) {
271 // Get V
272 dst[2] = dst[0] + SRC_HEIGHT * dstStride[0];
273 dstStride[2] = dstStride[0] >> m_int.nChromaShift;
275 // Get U
276 dst[1] = dst[2] +
277 (SRC_HEIGHT >> m_int.nChromaShift ) * dstStride[2];
278 dstStride[1] = dstStride[2];
281 if (m_int.fHWAccel) {
282 int w, h;
284 w = m_int.stride[0];
285 h = SRC_HEIGHT;
287 // Copy packed or Y
288 mem2agpcpy_pic(dst[0], m_int.planes[0], w, h,
289 dstStride[0], m_int.stride[0]);
291 // YV12 or YVU9 ?
292 if (m_int.nChromaShift) {
293 w >>= m_int.nChromaShift; h >>= m_int.nChromaShift;
295 // Copy U
296 mem2agpcpy_pic(dst[1], m_int.planes[1], w, h,
297 dstStride[1], m_int.stride[1]);
299 // Copy V
300 mem2agpcpy_pic(dst[2], m_int.planes[2], w, h,
301 dstStride[2], m_int.stride[2]);
303 } else {
304 sws_scale(m_int.sws, m_int.planes, m_int.stride, 0, SRC_HEIGHT,
305 dst, dstStride);
308 kvaUnlockBuffer();
312 // Frame window procedure to work around T23 laptop with S3 video card,
313 // which supports upscaling only.
314 static MRESULT EXPENTRY NewFrameWndProc(HWND hwnd, ULONG msg, MPARAM mp1,
315 MPARAM mp2)
317 switch (msg) {
318 case WM_QUERYTRACKINFO:
320 PTRACKINFO pti = (PTRACKINFO)mp2;
321 RECTL rcl;
323 if (vo_fs)
324 break;
326 m_int.pfnwpOldFrame(hwnd, msg, mp1, mp2);
328 rcl.xLeft = 0;
329 rcl.yBottom = 0;
330 rcl.xRight = SRC_WIDTH + 1;
331 rcl.yTop = SRC_HEIGHT + 1;
333 WinCalcFrameRect(hwnd, &rcl, FALSE);
335 pti->ptlMinTrackSize.x = rcl.xRight - rcl.xLeft;
336 pti->ptlMinTrackSize.y = rcl.yTop - rcl.yBottom;
338 pti->ptlMaxTrackSize.x = vo_screenwidth;
339 pti->ptlMaxTrackSize.y = vo_screenheight;
341 return (MRESULT)TRUE;
344 case WM_ADJUSTWINDOWPOS:
346 PSWP pswp = (PSWP)mp1;
347 RECTL rcl;
349 if (vo_fs)
350 break;
352 if (pswp->fl & SWP_SIZE) {
353 rcl.xLeft = pswp->x;
354 rcl.yBottom = pswp->y;
355 rcl.xRight = rcl.xLeft + pswp->cx;
356 rcl.yTop = rcl.yBottom + pswp->cy;
358 WinCalcFrameRect(hwnd, &rcl, TRUE);
360 if (rcl.xRight - rcl.xLeft <= SRC_WIDTH)
361 rcl.xRight = rcl.xLeft + (SRC_WIDTH + 1);
363 if (rcl.yTop - rcl.yBottom <= SRC_HEIGHT)
364 rcl.yTop = rcl.yBottom + (SRC_HEIGHT + 1);
366 WinCalcFrameRect(hwnd, &rcl, FALSE);
368 if (rcl.xRight - rcl.xLeft > vo_screenwidth) {
369 rcl.xLeft = 0;
370 rcl.xRight = vo_screenwidth;
373 if (rcl.yTop - rcl.yBottom > vo_screenheight) {
374 rcl.yBottom = 0;
375 rcl.yTop = vo_screenheight;
378 pswp->fl |= SWP_MOVE;
379 pswp->x = rcl.xLeft;
380 pswp->y = rcl.yBottom;
381 pswp->cx = rcl.xRight - rcl.xLeft;
382 pswp->cy = rcl.yTop - rcl.yBottom;
384 break;
388 return m_int.pfnwpOldFrame(hwnd, msg, mp1, mp2);
391 static MRESULT EXPENTRY WndProc(HWND hwnd, ULONG msg, MPARAM mp1, MPARAM mp2)
393 // if slave mode, ignore mouse events and deliver them to a parent window
394 if (WinID != -1 &&
395 ((msg >= WM_MOUSEFIRST && msg <= WM_MOUSELAST) ||
396 (msg >= WM_EXTMOUSEFIRST && msg <= WM_EXTMOUSELAST))) {
397 WinPostMsg(HWNDFROMWINID(WinID), msg, mp1, mp2);
399 return (MRESULT)TRUE;
402 switch (msg) {
403 case WM_CLOSE:
404 mplayer_put_key(KEY_CLOSE_WIN);
406 return 0;
408 case WM_CHAR:
410 USHORT fsFlags = SHORT1FROMMP(mp1);
411 UCHAR uchScan = CHAR4FROMMP(mp1);
412 USHORT usCh = SHORT1FROMMP(mp2);
413 USHORT usVk = SHORT2FROMMP(mp2);
414 int mpkey;
416 if (fsFlags & KC_KEYUP)
417 break;
419 if (fsFlags & KC_SCANCODE) {
420 mpkey = lookup_keymap_table(m_keypad_map, uchScan);
421 if (mpkey) {
422 // distinguish KEY_KP0 and KEY_KPINS
423 if (mpkey == KEY_KP0 && usCh != '0')
424 mpkey = KEY_KPINS;
426 // distinguish KEY_KPDEC and KEY_KPDEL
427 if (mpkey == KEY_KPDEC && usCh != '.')
428 mpkey = KEY_KPDEL;
430 mplayer_put_key(mpkey);
432 return (MRESULT)TRUE;
436 if (fsFlags & KC_VIRTUALKEY) {
437 mpkey = lookup_keymap_table(m_vk_map, usVk);
438 if (mpkey) {
439 mplayer_put_key(mpkey);
441 return (MRESULT)TRUE;
445 if ((fsFlags & KC_CHAR) && !HIBYTE(usCh))
446 mplayer_put_key(usCh);
448 return (MRESULT)TRUE;
451 case WM_BUTTON1DOWN:
452 case WM_BUTTON3DOWN:
453 case WM_BUTTON2DOWN:
454 case WM_BUTTON1DBLCLK:
455 case WM_BUTTON3DBLCLK:
456 case WM_BUTTON2DBLCLK:
457 if (WinQueryFocus(HWND_DESKTOP) != hwnd)
458 WinSetFocus(HWND_DESKTOP, hwnd);
459 else if (!vo_nomouse_input)
460 mplayer_put_key(lookup_keymap_table(m_mouse_map, msg));
462 return (MRESULT)TRUE;
464 case WM_PAINT:
466 HPS hps;
467 RECTL rcl, rclDst;
468 PRECTL prcl = NULL;
469 HRGN hrgn, hrgnDst;
470 RGNRECT rgnCtl;
472 // get a current movie area
473 kvaAdjustDstRect(&m_int.kvas.rclSrcRect, &rclDst);
475 // get a current invalidated area
476 hps = WinBeginPaint(hwnd, NULLHANDLE, &rcl);
478 // create a region for an invalidated area
479 hrgn = GpiCreateRegion(hps, 1, &rcl);
480 // create a region for a movie area
481 hrgnDst = GpiCreateRegion(hps, 1, &rclDst);
483 // exclude a movie area from an invalidated area
484 GpiCombineRegion(hps, hrgn, hrgn, hrgnDst, CRGN_DIFF);
486 // get rectangles from the region
487 rgnCtl.ircStart = 1;
488 rgnCtl.ulDirection = RECTDIR_LFRT_TOPBOT;
489 GpiQueryRegionRects(hps, hrgn, NULL, &rgnCtl, NULL);
491 if (rgnCtl.crcReturned > 0) {
492 rgnCtl.crc = rgnCtl.crcReturned;
493 prcl = malloc(sizeof(RECTL) * rgnCtl.crcReturned);
496 // draw black bar if needed
497 if (prcl && GpiQueryRegionRects(hps, hrgn, NULL, &rgnCtl, prcl)) {
498 int i;
500 for (i = 0; i < rgnCtl.crcReturned; i++)
501 WinFillRect(hps, &prcl[i], CLR_BLACK);
504 free(prcl);
506 GpiDestroyRegion(hps, hrgnDst);
507 GpiDestroyRegion(hps, hrgn);
509 WinEndPaint(hps);
511 return 0;
515 return WinDefWindowProc(hwnd, msg, mp1, mp2);
518 // Change process type from VIO to PM to use PM APIs.
519 static void morphToPM(void)
521 PPIB pib;
523 DosGetInfoBlocks(NULL, &pib);
525 // Change flag from VIO to PM:
526 if (pib->pib_ultype == 2)
527 pib->pib_ultype = 3;
530 static int preinit(const char *arg)
532 HWND hwndParent;
533 ULONG flFrameFlags;
534 ULONG kvaMode = 0;
536 int fUseSnap = 0;
537 int fUseWO = 0;
538 int fUseDive = 0;
539 int fFixT23 = 0;
541 const opt_t subopts[] = {
542 {"snap", OPT_ARG_BOOL, &fUseSnap, NULL},
543 {"wo", OPT_ARG_BOOL, &fUseWO, NULL},
544 {"dive", OPT_ARG_BOOL, &fUseDive, NULL},
545 {"t23", OPT_ARG_BOOL, &fFixT23, NULL},
546 {NULL, 0, NULL, NULL}
549 PCSZ pcszVideoModeStr[3] = {"DIVE", "WarpOverlay!", "SNAP"};
551 if (subopt_parse(arg, subopts) != 0)
552 return -1;
554 morphToPM();
556 memset(&m_int, 0, sizeof(m_int));
558 m_int.hab = WinInitialize(0);
559 m_int.hmq = WinCreateMsgQueue(m_int.hab, 0);
561 WinRegisterClass(m_int.hab,
562 WC_MPLAYER,
563 WndProc,
564 CS_SIZEREDRAW | CS_MOVENOTIFY,
565 sizeof(PVOID));
567 if (WinID == -1) {
568 hwndParent = HWND_DESKTOP;
569 flFrameFlags = FCF_SYSMENU | FCF_TITLEBAR | FCF_MINMAX |
570 FCF_SIZEBORDER | FCF_TASKLIST;
571 } else {
572 hwndParent = HWNDFROMWINID(WinID);
573 flFrameFlags = 0;
576 m_int.hwndFrame =
577 WinCreateStdWindow(hwndParent, // parent window handle
578 WS_VISIBLE, // frame window style
579 &flFrameFlags, // window style
580 WC_MPLAYER, // class name
581 "", // window title
582 0L, // default client style
583 NULLHANDLE, // resource in exe file
584 1, // frame window id
585 &m_int.hwndClient); // client window handle
587 if (m_int.hwndFrame == NULLHANDLE)
588 return -1;
590 m_int.hwndSysMenu = WinWindowFromID(m_int.hwndFrame, FID_SYSMENU);
591 m_int.hwndTitleBar = WinWindowFromID(m_int.hwndFrame, FID_TITLEBAR);
592 m_int.hwndMinMax = WinWindowFromID(m_int.hwndFrame, FID_MINMAX);
594 m_int.fFixT23 = fFixT23;
596 if (m_int.fFixT23)
597 m_int.pfnwpOldFrame = WinSubclassWindow(m_int.hwndFrame,
598 NewFrameWndProc);
600 if (!!fUseSnap + !!fUseWO + !!fUseDive > 1)
601 mp_msg(MSGT_VO, MSGL_WARN,"KVA: Multiple mode specified!!!\n");
603 if (fUseSnap)
604 kvaMode = KVAM_SNAP;
605 else if (fUseWO)
606 kvaMode = KVAM_WO;
607 else if (fUseDive)
608 kvaMode = KVAM_DIVE;
609 else
610 kvaMode = KVAM_AUTO;
612 if (kvaInit(kvaMode, m_int.hwndClient, vo_colorkey)) {
613 mp_msg(MSGT_VO, MSGL_ERR, "KVA: Init failed!!!\n");
615 return -1;
618 kvaCaps(&m_int.kvac);
620 mp_msg(MSGT_VO, MSGL_V, "KVA: Selected video mode = %s\n",
621 pcszVideoModeStr[m_int.kvac.ulMode - 1]);
623 kvaDisableScreenSaver();
625 // Might cause PM DLLs to be loaded which incorrectly enable SIG_FPE,
626 // so mask off all floating-point exceptions.
627 _control87(MCW_EM, MCW_EM);
629 return 0;
632 static void uninit(void)
634 kvaEnableScreenSaver();
636 imgFree();
638 sws_freeContext(m_int.sws);
640 if (m_int.hwndFrame != NULLHANDLE) {
641 kvaResetAttr();
642 kvaDone();
644 if (m_int.fFixT23)
645 WinSubclassWindow(m_int.hwndFrame, m_int.pfnwpOldFrame);
647 WinDestroyWindow(m_int.hwndFrame);
650 WinDestroyMsgQueue(m_int.hmq);
651 WinTerminate(m_int.hab);
654 static int config(uint32_t width, uint32_t height,
655 uint32_t d_width, uint32_t d_height,
656 uint32_t flags, char *title, uint32_t format)
658 RECTL rcl;
660 mp_msg(MSGT_VO, MSGL_V,
661 "KVA: Using 0x%X (%s) image format, vo_config_count = %d\n",
662 format, vo_format_name(format), vo_config_count);
664 imgFree();
666 if (query_format_info(format, &m_int.fHWAccel, &m_int.fcc, &m_int.bpp,
667 &m_int.nChromaShift))
668 return 1;
670 m_int.iImageFormat = format;
672 // if there is no hw accel for given format,
673 // try any format supported by hw accel
674 if (!m_int.fHWAccel) {
675 int dstFormat = 0;
677 sws_freeContext(m_int.sws);
679 if (m_int.kvac.ulInputFormatFlags & KVAF_YV12)
680 dstFormat = IMGFMT_YV12;
681 else if (m_int.kvac.ulInputFormatFlags & KVAF_YUY2)
682 dstFormat = IMGFMT_YUY2;
683 else if (m_int.kvac.ulInputFormatFlags & KVAF_YVU9)
684 dstFormat = IMGFMT_YVU9;
685 else if (m_int.kvac.ulInputFormatFlags & KVAF_BGR24)
686 dstFormat = IMGFMT_BGR24;
687 else if (m_int.kvac.ulInputFormatFlags & KVAF_BGR16)
688 dstFormat = IMGFMT_BGR16;
689 else if (m_int.kvac.ulInputFormatFlags & KVAF_BGR15)
690 dstFormat = IMGFMT_BGR15;
692 if (query_format_info(dstFormat, NULL, &m_int.fcc, NULL, NULL))
693 return 1;
695 m_int.sws = sws_getContextFromCmdLine(width, height, format,
696 width, height, dstFormat);
699 mp_msg(MSGT_VO, MSGL_V, "KVA: Selected FOURCC = %.4s\n", (char *)&m_int.fcc);
701 m_int.kvas.ulLength = sizeof(KVASETUP);
702 m_int.kvas.szlSrcSize.cx = width;
703 m_int.kvas.szlSrcSize.cy = height;
704 m_int.kvas.rclSrcRect.xLeft = 0;
705 m_int.kvas.rclSrcRect.yTop = 0;
706 m_int.kvas.rclSrcRect.xRight = width;
707 m_int.kvas.rclSrcRect.yBottom = height;
708 m_int.kvas.ulRatio = vo_keepaspect ? KVAR_FORCEANY : KVAR_NONE;
709 m_int.kvas.ulAspectWidth = d_width;
710 m_int.kvas.ulAspectHeight = d_height;
711 m_int.kvas.fccSrcColor = m_int.fcc;
712 m_int.kvas.fDither = TRUE;
714 if (kvaSetup(&m_int.kvas)) {
715 mp_msg(MSGT_VO, MSGL_ERR, "KVA: Setup failed!!!\n");
717 return 1;
720 m_int.lStride = width * m_int.bpp;
722 imgCreate();
724 if (WinID == -1) {
725 WinSetWindowText(m_int.hwndFrame, title);
727 // initialize 'vo_fs' only once at first config() call
728 if (vo_config_count == 0)
729 vo_fs = flags & VOFLAG_FULLSCREEN;
731 // workaround for T23 laptop with S3 Video by Franz Bakan
732 if (!vo_fs && m_int.fFixT23) {
733 d_width++;
734 d_height++;
737 m_int.rclDst.xLeft = ((LONG)vo_screenwidth - (LONG)d_width) / 2;
738 m_int.rclDst.yBottom = ((LONG)vo_screenheight - (LONG)d_height) / 2;
739 m_int.rclDst.xRight = m_int.rclDst.xLeft + d_width;
740 m_int.rclDst.yTop = m_int.rclDst.yBottom + d_height;
742 if (vo_fs) {
743 d_width = vo_screenwidth;
744 d_height = vo_screenheight;
746 // when -fs option is used without this, title bar is not highlighted
747 WinSetActiveWindow(HWND_DESKTOP, m_int.hwndFrame);
749 WinSetParent(m_int.hwndSysMenu, HWND_OBJECT, FALSE);
750 WinSetParent(m_int.hwndTitleBar, HWND_OBJECT, FALSE);
751 WinSetParent(m_int.hwndMinMax, HWND_OBJECT, FALSE);
753 setAspectRatio(KVAR_FORCEANY);
756 rcl.xLeft = ((LONG)vo_screenwidth - (LONG)d_width) / 2;
757 rcl.yBottom = ((LONG)vo_screenheight - (LONG)d_height) /2 ;
758 rcl.xRight = rcl.xLeft + d_width;
759 rcl.yTop = rcl.yBottom + d_height;
760 } else {
761 vo_fs = 0;
763 WinQueryWindowRect(HWNDFROMWINID(WinID), &m_int.rclDst);
764 rcl = m_int.rclDst;
767 WinCalcFrameRect(m_int.hwndFrame, &rcl, FALSE);
769 WinSetWindowPos(m_int.hwndFrame, HWND_TOP,
770 rcl.xLeft, rcl.yBottom,
771 rcl.xRight - rcl.xLeft, rcl.yTop - rcl.yBottom,
772 SWP_SIZE | SWP_MOVE | SWP_ZORDER | SWP_SHOW |
773 (WinID == -1 ? SWP_ACTIVATE : 0));
775 WinInvalidateRect(m_int.hwndFrame, NULL, TRUE);
777 return 0;
780 static uint32_t get_image(mp_image_t *mpi)
782 if (m_int.iImageFormat != mpi->imgfmt)
783 return VO_FALSE;
785 if (mpi->type == MP_IMGTYPE_STATIC || mpi->type == MP_IMGTYPE_TEMP) {
786 if (mpi->flags & MP_IMGFLAG_PLANAR) {
787 mpi->planes[1] = m_int.planes[1];
788 mpi->planes[2] = m_int.planes[2];
790 mpi->stride[1] = m_int.stride[1];
791 mpi->stride[2] = m_int.stride[2];
794 mpi->planes[0] = m_int.planes[0];
795 mpi->stride[0] = m_int.stride[0];
796 mpi->flags |= MP_IMGFLAG_DIRECT;
798 return VO_TRUE;
801 return VO_FALSE;
804 static uint32_t draw_image(mp_image_t *mpi)
806 // if -dr or -slices then do nothing:
807 if (mpi->flags & (MP_IMGFLAG_DIRECT | MP_IMGFLAG_DRAW_CALLBACK))
808 return VO_TRUE;
810 draw_slice(mpi->planes, mpi->stride, mpi->w, mpi->h, mpi->x, mpi->y);
812 return VO_TRUE;
815 static int query_format(uint32_t format)
817 BOOL fHWAccel;
818 int res;
820 if (query_format_info(format, &fHWAccel, NULL, NULL, NULL))
821 return 0;
823 res = VFCAP_CSP_SUPPORTED | VFCAP_OSD;
824 if (fHWAccel) {
825 res |= VFCAP_CSP_SUPPORTED_BY_HW | VFCAP_HWSCALE_UP;
827 if (!m_int.fFixT23)
828 res |= VFCAP_HWSCALE_DOWN;
831 return res;
834 static int fs_toggle(void)
836 RECTL rcl;
838 vo_fs = !vo_fs;
840 if (vo_fs) {
841 SWP swp;
843 WinQueryWindowPos(m_int.hwndFrame, &swp);
844 m_int.rclDst.xLeft = swp.x;
845 m_int.rclDst.yBottom = swp.y;
846 m_int.rclDst.xRight = m_int.rclDst.xLeft + swp.cx;
847 m_int.rclDst.yTop = m_int.rclDst.yBottom + swp.cy;
848 WinCalcFrameRect(m_int.hwndFrame, &m_int.rclDst, TRUE);
850 if (WinID != -1)
851 WinSetParent(m_int.hwndFrame, HWND_DESKTOP, FALSE);
853 WinSetParent(m_int.hwndSysMenu, HWND_OBJECT, FALSE);
854 WinSetParent(m_int.hwndTitleBar, HWND_OBJECT, FALSE);
855 WinSetParent(m_int.hwndMinMax, HWND_OBJECT, FALSE);
857 rcl.xLeft = 0;
858 rcl.yBottom = 0;
859 rcl.xRight = vo_screenwidth;
860 rcl.yTop = vo_screenheight;
862 setAspectRatio(KVAR_FORCEANY);
863 } else {
864 if (WinID != -1)
865 WinSetParent(m_int.hwndFrame, HWNDFROMWINID(WinID), TRUE);
867 WinSetParent(m_int.hwndSysMenu, m_int.hwndFrame, FALSE);
868 WinSetParent(m_int.hwndTitleBar, m_int.hwndFrame, FALSE);
869 WinSetParent(m_int.hwndMinMax, m_int.hwndFrame, FALSE);
871 rcl = m_int.rclDst;
873 setAspectRatio(vo_keepaspect ? KVAR_FORCEANY : KVAR_NONE);
876 WinCalcFrameRect(m_int.hwndFrame, &rcl, FALSE);
878 WinSetWindowPos(m_int.hwndFrame, HWND_TOP,
879 rcl.xLeft, rcl.yBottom,
880 rcl.xRight - rcl.xLeft, rcl.yTop - rcl.yBottom,
881 SWP_SIZE | SWP_MOVE | SWP_ZORDER | SWP_SHOW |
882 (WinID == -1 ? SWP_ACTIVATE : 0));
884 return VO_TRUE;
887 static int color_ctrl_set(char *what, int value)
889 ULONG ulAttr;
890 ULONG ulValue;
892 if (!strcmp(what, "brightness"))
893 ulAttr = KVAA_BRIGHTNESS;
894 else if (!strcmp(what, "contrast"))
895 ulAttr = KVAA_CONTRAST;
896 else if (!strcmp(what, "hue"))
897 ulAttr = KVAA_HUE;
898 else if (!strcmp(what, "saturation"))
899 ulAttr = KVAA_SATURATION;
900 else
901 return VO_NOTIMPL;
903 ulValue = (value + 100) * 255 / 200;
905 if (kvaSetAttr(ulAttr, &ulValue))
906 return VO_NOTIMPL;
908 return VO_TRUE;
911 static int color_ctrl_get(char *what, int *value)
913 ULONG ulAttr;
914 ULONG ulValue;
916 if (!strcmp(what, "brightness"))
917 ulAttr = KVAA_BRIGHTNESS;
918 else if (!strcmp(what, "contrast"))
919 ulAttr = KVAA_CONTRAST;
920 else if (!strcmp(what, "hue"))
921 ulAttr = KVAA_HUE;
922 else if (!strcmp(what, "saturation"))
923 ulAttr = KVAA_SATURATION;
924 else
925 return VO_NOTIMPL;
927 if (kvaQueryAttr(ulAttr, &ulValue))
928 return VO_NOTIMPL;
930 // add 1 to adjust range
931 *value = ((ulValue + 1) * 200 / 255) - 100;
933 return VO_TRUE;
936 static int control(uint32_t request, void *data, ...)
938 switch (request) {
939 case VOCTRL_GET_IMAGE:
940 return get_image(data);
942 case VOCTRL_DRAW_IMAGE:
943 return draw_image(data);
945 case VOCTRL_QUERY_FORMAT:
946 return query_format(*(uint32_t *)data);
948 case VOCTRL_FULLSCREEN:
949 return fs_toggle();
951 case VOCTRL_SET_EQUALIZER:
953 va_list ap;
954 int value;
956 va_start(ap, data);
957 value = va_arg(ap, int);
958 va_end(ap);
960 return color_ctrl_set(data, value);
963 case VOCTRL_GET_EQUALIZER:
965 va_list ap;
966 int *value;
968 va_start(ap, data);
969 value = va_arg(ap, int *);
970 va_end(ap);
972 return color_ctrl_get(data, value);
975 case VOCTRL_UPDATE_SCREENINFO:
976 vo_screenwidth = m_int.kvac.cxScreen;
977 vo_screenheight = m_int.kvac.cyScreen;
979 aspect_save_screenres(vo_screenwidth, vo_screenheight);
981 return VO_TRUE;
984 return VO_NOTIMPL;
987 static int draw_frame(uint8_t *src[])
989 return VO_ERROR;
992 static int draw_slice(uint8_t *src[], int stride[], int w, int h, int x, int y)
994 uint8_t *s;
995 uint8_t *d;
997 // copy packed or Y
998 d = m_int.planes[0] + m_int.stride[0] * y + x;
999 s = src[0];
1000 mem2agpcpy_pic(d, s, w * m_int.bpp, h, m_int.stride[0], stride[0]);
1002 // YV12 or YVU9
1003 if (m_int.nChromaShift) {
1004 w >>= m_int.nChromaShift; h >>= m_int.nChromaShift;
1005 x >>= m_int.nChromaShift; y >>= m_int.nChromaShift;
1007 // copy U
1008 d = m_int.planes[1] + m_int.stride[1] * y + x;
1009 s = src[1];
1010 mem2agpcpy_pic(d, s, w, h, m_int.stride[1], stride[1]);
1012 // copy V
1013 d = m_int.planes[2] + m_int.stride[2] * y + x;
1014 s = src[2];
1015 mem2agpcpy_pic(d, s, w, h, m_int.stride[2], stride[2]);
1018 return 0;
1021 #define vo_draw_alpha(imgfmt) \
1022 vo_draw_alpha_##imgfmt(w, h, src, srca, stride, \
1023 m_int.planes[0] + m_int.stride[0] * y0 + m_int.bpp * x0, \
1024 m_int.stride[0])
1026 static void draw_alpha(int x0, int y0, int w, int h,
1027 unsigned char *src, unsigned char *srca, int stride)
1029 switch (m_int.iImageFormat) {
1030 case IMGFMT_YV12:
1031 case IMGFMT_YVU9:
1032 vo_draw_alpha(yv12);
1033 break;
1035 case IMGFMT_YUY2:
1036 vo_draw_alpha(yuy2);
1037 break;
1039 case IMGFMT_BGR24:
1040 vo_draw_alpha(rgb24);
1041 break;
1043 case IMGFMT_BGR16:
1044 vo_draw_alpha(rgb16);
1045 break;
1047 case IMGFMT_BGR15:
1048 vo_draw_alpha(rgb15);
1049 break;
1053 static void draw_osd(void)
1055 vo_draw_text(SRC_WIDTH, SRC_HEIGHT, draw_alpha);
1058 static void flip_page(void)
1060 imgDisplay();
1063 static void check_events(void)
1065 QMSG qm;
1067 // On slave mode, we need to change our window size according to a
1068 // parent window size
1069 if (WinID != -1) {
1070 RECTL rcl;
1072 WinQueryWindowRect(HWNDFROMWINID(WinID), &rcl);
1074 if (rcl.xLeft != m_int.rclParent.xLeft ||
1075 rcl.yBottom != m_int.rclParent.yBottom ||
1076 rcl.xRight != m_int.rclParent.xRight ||
1077 rcl.yTop != m_int.rclParent.yTop) {
1078 WinSetWindowPos(m_int.hwndFrame, NULLHANDLE,
1079 rcl.xLeft, rcl.yBottom,
1080 rcl.xRight - rcl.xLeft, rcl.yTop - rcl.yBottom,
1081 SWP_SIZE | SWP_MOVE);
1083 m_int.rclParent = rcl;
1087 while (WinPeekMsg(m_int.hab, &qm, NULLHANDLE, 0, 0, PM_REMOVE))
1088 WinDispatchMsg(m_int.hab, &qm);