home *** CD-ROM | disk | FTP | other *** search
/ Computer Shopper 275 / DPCS0111DVD.ISO / Toolkit / Audio-Visual / VirtualDub / Source / VirtualDub-1.9.10-src.7z / src / Riza / source / displaydrv.cpp < prev    next >
Encoding:
C/C++ Source or Header  |  2009-09-14  |  12.3 KB  |  389 lines

  1. //    VirtualDub - Video processing and capture application
  2. //    A/V interface library
  3. //    Copyright (C) 1998-2005 Avery Lee
  4. //
  5. //    This program is free software; you can redistribute it and/or modify
  6. //    it under the terms of the GNU General Public License as published by
  7. //    the Free Software Foundation; either version 2 of the License, or
  8. //    (at your option) any later version.
  9. //
  10. //    This program is distributed in the hope that it will be useful,
  11. //    but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. //    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  13. //    GNU General Public License for more details.
  14. //
  15. //    You should have received a copy of the GNU General Public License
  16. //    along with this program; if not, write to the Free Software
  17. //    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  18.  
  19. #define DIRECTDRAW_VERSION 0x0300
  20. #define INITGUID
  21. #include <vector>
  22. #include <ddraw.h>
  23. #include <mmsystem.h>
  24. #include <vd2/system/vdalloc.h>
  25. #include <vd2/system/memory.h>
  26. #include <vd2/system/log.h>
  27. #include <vd2/system/memory.h>
  28. #include <vd2/system/math.h>
  29. #include <vd2/system/vdstl.h>
  30. #include <vd2/system/w32assist.h>
  31. #include <vd2/Kasumi/pixmaputils.h>
  32. #include <vd2/Kasumi/pixmapops.h>
  33. #include <vd2/Riza/display.h>
  34. #include "displaydrv.h"
  35.  
  36. #define VDDEBUG_DISP (void)sizeof printf
  37. //#define VDDEBUG_DISP VDDEBUG
  38.  
  39. #if 0
  40.     #define DEBUG_LOG(x) VDLog(kVDLogInfo, VDStringW(L##x))
  41. #else
  42.     #define DEBUG_LOG(x)
  43. #endif
  44.  
  45. using namespace nsVDPixmap;
  46.  
  47. ///////////////////////////////////////////////////////////////////////////
  48.  
  49. namespace {
  50.  
  51.     #define TABROW(x)    TABENT(x+0),TABENT(x+1),TABENT(x+2),TABENT(x+3),TABENT(x+4),TABENT(x+5),TABENT(x+6),TABENT(x+7),TABENT(x+8),TABENT(x+9),TABENT(x+10),TABENT(x+11),TABENT(x+12),TABENT(x+13),TABENT(x+14),TABENT(x+15)
  52.     #define TABLE        TABROW(0x00),TABROW(0x10),TABROW(0x20),TABROW(0x30),TABROW(0x40),TABROW(0x50),TABROW(0x60),TABROW(0x70),TABROW(0x80),TABROW(0x90),TABROW(0xA0),TABROW(0xB0),TABROW(0xC0),TABROW(0xD0),TABROW(0xE0),TABROW(0xF0),TABROW(0x100),TABROW(0x110),TABROW(0x120)
  53.  
  54.     // d     = spacing between shades
  55.     // n     = number of shades
  56.     //
  57.     // require: dn = 255
  58.  
  59.     const uint8 rdithertab8[256+48]={
  60.     #define    TABENT(x)    ((x) > 255 ? 5*36 : (((x)*5) / 255)*36)
  61.         TABLE
  62.     #undef TABENT
  63.     };
  64.     const uint8 gdithertab8[256+48]={
  65.     #define    TABENT(x)    ((x) > 255 ? 5* 6 : (((x)*5) / 255)*6)
  66.         TABLE
  67.     #undef TABENT
  68.     };
  69.     const uint8 bdithertab8[256+48]={
  70.     #define    TABENT(x)    ((x) > 255 ? 5* 1 : (((x)*5) / 255)*1)
  71.         TABLE
  72.     #undef TABENT
  73.     };
  74.     #undef TABROW
  75.     #undef TABLE
  76. }
  77.  
  78. // 0 8 2 A
  79. // C 4 E 6
  80. // 3 B 1 9
  81. // F 7 D 5
  82.  
  83. template<int d0, int d1, int d2, int d3>
  84. struct VDDitherUtils {
  85.     enum {
  86.         rb0 = d0*51/16,
  87.         rb1 = d1*51/16,
  88.         rb2 = d2*51/16,
  89.         rb3 = d3*51/16,
  90.         g0 = d0*51/16,
  91.         g1 = d1*51/16,
  92.         g2 = d2*51/16,
  93.         g3 = d3*51/16,
  94.     };
  95.  
  96.     static void DoSpan8To8(uint8 *dstp, const uint8 *srcp, int w2, const uint8 *pLogPal, const uint8 *palette) {
  97.         const uint8 *p;
  98.  
  99.         switch(w2 & 3) {
  100.             do {
  101.         case 0:    p = &palette[4*srcp[0]]; dstp[w2  ] = pLogPal[rdithertab8[rb0+p[2]] + gdithertab8[g0+p[1]] + bdithertab8[rb0+p[0]]];
  102.         case 1:    p = &palette[4*srcp[1]]; dstp[w2+1] = pLogPal[rdithertab8[rb1+p[2]] + gdithertab8[g1+p[1]] + bdithertab8[rb1+p[0]]];
  103.         case 2:    p = &palette[4*srcp[2]]; dstp[w2+2] = pLogPal[rdithertab8[rb2+p[2]] + gdithertab8[g2+p[1]] + bdithertab8[rb2+p[0]]];
  104.         case 3:    p = &palette[4*srcp[3]]; dstp[w2+3] = pLogPal[rdithertab8[rb3+p[2]] + gdithertab8[g3+p[1]] + bdithertab8[rb3+p[0]]];
  105.  
  106.                 srcp += 16;
  107.             } while((w2 += 4) < 0);
  108.         }
  109.     }
  110.  
  111.     static void DoSpan15To8(uint8 *dstp, const uint16 *srcp, int w2, const uint8 *pLogPal) {
  112.         uint32 px;
  113.  
  114.         switch(w2 & 3) {
  115.             do {
  116.         case 0:    px = srcp[0];
  117.                 dstp[w2  ] = pLogPal[rdithertab8[rb0 + ((px&0x7c00) >> 7)] + gdithertab8[g0 + ((px&0x03e0) >> 2)] + bdithertab8[rb0 + ((px&0x001f) << 3)]];
  118.         case 1:    px = srcp[1];
  119.                 dstp[w2+1] = pLogPal[rdithertab8[rb1 + ((px&0x7c00) >> 7)] + gdithertab8[g1 + ((px&0x03e0) >> 2)] + bdithertab8[rb1 + ((px&0x001f) << 3)]];
  120.         case 2:    px = srcp[2];
  121.                 dstp[w2+2] = pLogPal[rdithertab8[rb2 + ((px&0x7c00) >> 7)] + gdithertab8[g2 + ((px&0x03e0) >> 2)] + bdithertab8[rb2 + ((px&0x001f) << 3)]];
  122.         case 3:    px = srcp[3];
  123.                 dstp[w2+3] = pLogPal[rdithertab8[rb3 + ((px&0x7c00) >> 7)] + gdithertab8[g3 + ((px&0x03e0) >> 2)] + bdithertab8[rb3 + ((px&0x001f) << 3)]];
  124.  
  125.                 srcp += 4;
  126.             } while((w2 += 4) < 0);
  127.         }
  128.     }
  129.  
  130.     static void DoSpan16To8(uint8 *dstp, const uint16 *srcp, int w2, const uint8 *pLogPal) {
  131.         uint32 px;
  132.  
  133.         switch(w2 & 3) {
  134.             do {
  135.         case 0:    px = srcp[0];
  136.                 dstp[w2  ] = pLogPal[rdithertab8[rb0 + ((px&0xf800) >> 8)] + gdithertab8[g0 + ((px&0x07e0) >> 3)] + bdithertab8[rb0 + ((px&0x001f) << 3)]];
  137.         case 1:    px = srcp[1];
  138.                 dstp[w2+1] = pLogPal[rdithertab8[rb1 + ((px&0xf800) >> 8)] + gdithertab8[g1 + ((px&0x07e0) >> 3)] + bdithertab8[rb1 + ((px&0x001f) << 3)]];
  139.         case 2:    px = srcp[2];
  140.                 dstp[w2+2] = pLogPal[rdithertab8[rb2 + ((px&0xf800) >> 8)] + gdithertab8[g2 + ((px&0x07e0) >> 3)] + bdithertab8[rb2 + ((px&0x001f) << 3)]];
  141.         case 3:    px = srcp[3];
  142.                 dstp[w2+3] = pLogPal[rdithertab8[rb3 + ((px&0xf800) >> 8)] + gdithertab8[g3 + ((px&0x07e0) >> 3)] + bdithertab8[rb3 + ((px&0x001f) << 3)]];
  143.  
  144.                 srcp += 4;
  145.             } while((w2 += 4) < 0);
  146.         }
  147.     }
  148.  
  149.     static void DoSpan24To8(uint8 *dstp, const uint8 *srcp, int w2, const uint8 *pLogPal) {
  150.         switch(w2 & 3) {
  151.             do {
  152.         case 0:    dstp[w2  ] = pLogPal[rdithertab8[rb0+srcp[ 2]] + gdithertab8[g0+srcp[ 1]] + bdithertab8[rb0+srcp[ 0]]];
  153.         case 1:    dstp[w2+1] = pLogPal[rdithertab8[rb1+srcp[ 5]] + gdithertab8[g1+srcp[ 4]] + bdithertab8[rb1+srcp[ 3]]];
  154.         case 2:    dstp[w2+2] = pLogPal[rdithertab8[rb2+srcp[ 8]] + gdithertab8[g2+srcp[ 7]] + bdithertab8[rb2+srcp[ 6]]];
  155.         case 3:    dstp[w2+3] = pLogPal[rdithertab8[rb3+srcp[11]] + gdithertab8[g3+srcp[10]] + bdithertab8[rb3+srcp[ 9]]];
  156.  
  157.                 srcp += 12;
  158.             } while((w2 += 4) < 0);
  159.         }
  160.     }
  161.  
  162.     static void DoSpan32To8(uint8 *dstp, const uint8 *srcp, int w2, const uint8 *pLogPal) {
  163.         switch(w2 & 3) {
  164.             do {
  165.         case 0:    dstp[w2  ] = pLogPal[rdithertab8[rb0+srcp[ 2]] + gdithertab8[g0+srcp[ 1]] + bdithertab8[rb0+srcp[ 0]]];
  166.         case 1:    dstp[w2+1] = pLogPal[rdithertab8[rb1+srcp[ 6]] + gdithertab8[g1+srcp[ 5]] + bdithertab8[rb1+srcp[ 4]]];
  167.         case 2:    dstp[w2+2] = pLogPal[rdithertab8[rb2+srcp[10]] + gdithertab8[g2+srcp[ 9]] + bdithertab8[rb2+srcp[ 8]]];
  168.         case 3:    dstp[w2+3] = pLogPal[rdithertab8[rb3+srcp[14]] + gdithertab8[g3+srcp[13]] + bdithertab8[rb3+srcp[12]]];
  169.  
  170.                 srcp += 16;
  171.             } while((w2 += 4) < 0);
  172.         }
  173.     }
  174. };
  175.  
  176. void VDDitherImage8To8(VDPixmap& dst, const VDPixmap& src, const uint8 *pLogPal, const uint8 *palette) {
  177.     int h = dst.h;
  178.     int w = dst.w;
  179.  
  180.     uint8 *dstp0 = (uint8 *)dst.data;
  181.     const uint8 *srcp0 = (const uint8 *)src.data;
  182.  
  183.     do {
  184.         int w2 = -w;
  185.  
  186.         uint8 *dstp = dstp0 + w - (w2&3);
  187.         const uint8 *srcp = srcp0;
  188.  
  189.         switch(h & 3) {
  190.             case 0: VDDitherUtils< 0, 8, 2,10>::DoSpan8To8(dstp, srcp, w2, pLogPal, palette); break;
  191.             case 1: VDDitherUtils<12, 4,14, 6>::DoSpan8To8(dstp, srcp, w2, pLogPal, palette); break;
  192.             case 2: VDDitherUtils< 3,11, 1, 9>::DoSpan8To8(dstp, srcp, w2, pLogPal, palette); break;
  193.             case 3: VDDitherUtils<15, 7,13, 5>::DoSpan8To8(dstp, srcp, w2, pLogPal, palette); break;
  194.         }
  195.  
  196.         dstp0 += dst.pitch;
  197.         srcp0 = (const uint8 *)((const char *)srcp0 + src.pitch);
  198.     } while(--h);
  199. }
  200.  
  201. void VDDitherImage15To8(VDPixmap& dst, const VDPixmap& src, const uint8 *pLogPal) {
  202.     int h = dst.h;
  203.     int w = dst.w;
  204.  
  205.     uint8 *dstp0 = (uint8 *)dst.data;
  206.     const uint16 *srcp0 = (const uint16 *)src.data;
  207.  
  208.     do {
  209.         int w2 = -w;
  210.  
  211.         uint8 *dstp = dstp0 + w - (w2&3);
  212.         const uint16 *srcp = srcp0;
  213.  
  214.         switch(h & 3) {
  215.             case 0: VDDitherUtils< 0, 8, 2,10>::DoSpan15To8(dstp, srcp, w2, pLogPal); break;
  216.             case 1: VDDitherUtils<12, 4,14, 6>::DoSpan15To8(dstp, srcp, w2, pLogPal); break;
  217.             case 2: VDDitherUtils< 3,11, 1, 9>::DoSpan15To8(dstp, srcp, w2, pLogPal); break;
  218.             case 3: VDDitherUtils<15, 7,13, 5>::DoSpan15To8(dstp, srcp, w2, pLogPal); break;
  219.         }
  220.  
  221.         dstp0 += dst.pitch;
  222.         srcp0 = (const uint16 *)((const char *)srcp0 + src.pitch);
  223.     } while(--h);
  224. }
  225.  
  226. void VDDitherImage16To8(VDPixmap& dst, const VDPixmap& src, const uint8 *pLogPal) {
  227.     int h = dst.h;
  228.     int w = dst.w;
  229.  
  230.     uint8 *dstp0 = (uint8 *)dst.data;
  231.     const uint16 *srcp0 = (const uint16 *)src.data;
  232.  
  233.     do {
  234.         int w2 = -w;
  235.  
  236.         uint8 *dstp = dstp0 + w - (w2&3);
  237.         const uint16 *srcp = srcp0;
  238.  
  239.         switch(h & 3) {
  240.             case 0: VDDitherUtils< 0, 8, 2,10>::DoSpan16To8(dstp, srcp, w2, pLogPal); break;
  241.             case 1: VDDitherUtils<12, 4,14, 6>::DoSpan16To8(dstp, srcp, w2, pLogPal); break;
  242.             case 2: VDDitherUtils< 3,11, 1, 9>::DoSpan16To8(dstp, srcp, w2, pLogPal); break;
  243.             case 3: VDDitherUtils<15, 7,13, 5>::DoSpan16To8(dstp, srcp, w2, pLogPal); break;
  244.         }
  245.  
  246.         dstp0 += dst.pitch;
  247.         srcp0 = (const uint16 *)((const char *)srcp0 + src.pitch);
  248.     } while(--h);
  249. }
  250.  
  251. void VDDitherImage24To8(VDPixmap& dst, const VDPixmap& src, const uint8 *pLogPal) {
  252.     int h = dst.h;
  253.     int w = dst.w;
  254.  
  255.     uint8 *dstp0 = (uint8 *)dst.data;
  256.     const uint8 *srcp0 = (const uint8 *)src.data;
  257.  
  258.     do {
  259.         int w2 = -w;
  260.  
  261.         uint8 *dstp = dstp0 + w - (w2&3);
  262.         const uint8 *srcp = srcp0;
  263.  
  264.         switch(h & 3) {
  265.             case 0: VDDitherUtils< 0, 8, 2,10>::DoSpan24To8(dstp, srcp, w2, pLogPal); break;
  266.             case 1: VDDitherUtils<12, 4,14, 6>::DoSpan24To8(dstp, srcp, w2, pLogPal); break;
  267.             case 2: VDDitherUtils< 3,11, 1, 9>::DoSpan24To8(dstp, srcp, w2, pLogPal); break;
  268.             case 3: VDDitherUtils<15, 7,13, 5>::DoSpan24To8(dstp, srcp, w2, pLogPal); break;
  269.         }
  270.  
  271.         dstp0 += dst.pitch;
  272.         srcp0 += src.pitch;
  273.     } while(--h);
  274. }
  275.  
  276. void VDDitherImage32To8(VDPixmap& dst, const VDPixmap& src, const uint8 *pLogPal) {
  277.     int h = dst.h;
  278.     int w = dst.w;
  279.  
  280.     uint8 *dstp0 = (uint8 *)dst.data;
  281.     const uint8 *srcp0 = (const uint8 *)src.data;
  282.  
  283.     do {
  284.         int w2 = -w;
  285.  
  286.         uint8 *dstp = dstp0 + w - (w2&3);
  287.         const uint8 *srcp = srcp0;
  288.  
  289.         switch(h & 3) {
  290.             case 0: VDDitherUtils< 0, 8, 2,10>::DoSpan32To8(dstp, srcp, w2, pLogPal); break;
  291.             case 1: VDDitherUtils<12, 4,14, 6>::DoSpan32To8(dstp, srcp, w2, pLogPal); break;
  292.             case 2: VDDitherUtils< 3,11, 1, 9>::DoSpan32To8(dstp, srcp, w2, pLogPal); break;
  293.             case 3: VDDitherUtils<15, 7,13, 5>::DoSpan32To8(dstp, srcp, w2, pLogPal); break;
  294.         }
  295.  
  296.         dstp0 += dst.pitch;
  297.         srcp0 += src.pitch;
  298.     } while(--h);
  299. }
  300.  
  301. void VDDitherImage(VDPixmap& dst, const VDPixmap& src, const uint8 *pLogPal) {
  302.     VDASSERT(dst.w == src.w && dst.h == src.h);
  303.  
  304.     if (dst.w<=0 || dst.h<=0)
  305.         return;
  306.  
  307.     if (dst.format == kPixFormat_Pal8) {
  308.         switch(src.format) {
  309.         case kPixFormat_Pal8:
  310.             VDDitherImage8To8(dst, src, pLogPal, (const uint8 *)src.palette);
  311.             break;
  312.         case kPixFormat_XRGB1555:
  313.             VDDitherImage15To8(dst, src, pLogPal);
  314.             break;
  315.         case kPixFormat_RGB565:
  316.             VDDitherImage16To8(dst, src, pLogPal);
  317.             break;
  318.         case kPixFormat_RGB888:
  319.             VDDitherImage24To8(dst, src, pLogPal);
  320.             break;
  321.         case kPixFormat_XRGB8888:
  322.             VDDitherImage32To8(dst, src, pLogPal);
  323.             break;
  324.         }
  325.     }
  326. }
  327.  
  328. ///////////////////////////////////////////////////////////////////////////////
  329.  
  330. VDVideoDisplayMinidriver::VDVideoDisplayMinidriver()
  331.     : mbDisplayDebugInfo(false)
  332.     , mbHighPrecision(false)
  333.     , mColorOverride(0)
  334. {
  335. }
  336.  
  337. bool VDVideoDisplayMinidriver::IsFramePending() {
  338.     return false;
  339. }
  340.  
  341. void VDVideoDisplayMinidriver::SetFilterMode(FilterMode mode) {
  342. }
  343.  
  344. void VDVideoDisplayMinidriver::SetFullScreen(bool fullscreen) {
  345. }
  346.  
  347. void VDVideoDisplayMinidriver::SetDisplayDebugInfo(bool enable) {
  348.     mbDisplayDebugInfo = enable;
  349. }
  350.  
  351. void VDVideoDisplayMinidriver::SetColorOverride(uint32 color) {
  352.     mColorOverride = color;
  353. }
  354.  
  355. void VDVideoDisplayMinidriver::SetHighPrecision(bool enable) {
  356.     mbHighPrecision = enable;
  357. }
  358.  
  359. bool VDVideoDisplayMinidriver::Tick(int id) {
  360.     return true;
  361. }
  362.  
  363. void VDVideoDisplayMinidriver::Poll() {
  364. }
  365.  
  366. bool VDVideoDisplayMinidriver::Resize() {
  367.     return true;
  368. }
  369.  
  370. bool VDVideoDisplayMinidriver::SetSubrect(const vdrect32 *r) {
  371.     return false;
  372. }
  373.  
  374. void VDVideoDisplayMinidriver::SetLogicalPalette(const uint8 *pLogicalPalette) {
  375. }
  376.  
  377. float VDVideoDisplayMinidriver::GetSyncDelta() const {
  378.     return 0.0f;
  379. }
  380.  
  381. void VDVideoDisplayMinidriver::GetFormatString(const VDVideoDisplaySourceInfo& info, VDStringA& s) {
  382.     s.sprintf("%dx%d (%s)%s"
  383.         , info.pixmap.w
  384.         , info.pixmap.h
  385.         , VDPixmapGetInfo(info.pixmap.format).name
  386.         , info.bInterlaced ? " (interlaced)" : ""
  387.         );
  388. }
  389.