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

  1. //    VirtualDub - Video processing and capture application
  2. //    Copyright (C) 1998-2003 Avery Lee
  3. //
  4. //    This program is free software; you can redistribute it and/or modify
  5. //    it under the terms of the GNU General Public License as published by
  6. //    the Free Software Foundation; either version 2 of the License, or
  7. //    (at your option) any later version.
  8. //
  9. //    This program is distributed in the hope that it will be useful,
  10. //    but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. //    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  12. //    GNU General Public License for more details.
  13. //
  14. //    You should have received a copy of the GNU General Public License
  15. //    along with this program; if not, write to the Free Software
  16. //    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  17.  
  18. #include <string.h>
  19. #include <stdio.h>
  20. #include <stddef.h>
  21. #include <math.h>
  22.  
  23. #ifdef _WIN32
  24. #include <windows.h>
  25. #endif
  26.  
  27. #include <vd2/system/atomic.h>
  28. #include <vd2/system/memory.h>
  29. #include <vd2/Meia/MPEGDecoder.h>
  30. #include <vd2/Meia/MPEGPredict.h>
  31. #include <vd2/Meia/MPEGConvert.h>
  32. #include <vd2/Meia/MPEGIDCT.h>
  33.  
  34. ///////////////////////////////////////////////////////////////////////////
  35.  
  36. #pragma optimize("agty",on)
  37. #pragma inline_recursion(on)
  38. #pragma inline_depth(32)
  39.  
  40. #pragma warning(push)
  41. #pragma warning(disable:4035)
  42.  
  43. #if _MSC_VER >= 1300
  44.     extern unsigned long _byteswap_ulong(unsigned long v);
  45.     #pragma intrinsic(_byteswap_ulong)
  46.     static inline unsigned long bswap(unsigned long v) {
  47.         return _byteswap_ulong(v);
  48.     }
  49. #else
  50.     static inline unsigned long bswap(unsigned long v) {
  51.         __asm {
  52.             mov eax,v
  53.             bswap eax
  54.         }
  55.     }
  56. #endif
  57.  
  58. #pragma warning(pop)
  59.  
  60. ///////////////////////////////////////////////////////////////////////////
  61.  
  62. static const int zigzag_std[64]={
  63.      0,  1,  8, 16,  9,  2,  3, 10,
  64.     17, 24, 32, 25, 18, 11,  4,  5,
  65.     12, 19, 26, 33, 40, 48, 41, 34,
  66.     27, 20, 13,  6,  7, 14, 21, 28,
  67.     35, 42, 49, 56, 57, 50, 43, 36,
  68.     29, 22, 15, 23, 30, 37, 44, 51,
  69.     58, 59, 52, 45, 38, 31, 39, 46,
  70.     53, 60, 61, 54, 47, 55, 62, 63,
  71. };
  72.  
  73. #define TAB4_REP1(run,level,bits) {run,(level<0?- level:level),bits,(level<0?-1:0)}
  74. #define TAB4_REP2(run,level,bits) TAB4_REP1(run,level,bits),TAB4_REP1(run,level,bits)
  75. #define TAB4_REP4(run,level,bits) TAB4_REP2(run,level,bits),TAB4_REP2(run,level,bits)
  76. #define TAB4_REP8(run,level,bits) TAB4_REP4(run,level,bits),TAB4_REP4(run,level,bits)
  77.  
  78. //static const signed char dct_vshort_tab[22][4]={
  79. static const signed char dct_vshort_tab[54][4]={
  80.     TAB4_REP1(0,+3,6),    // 001010
  81.     TAB4_REP1(0,-3,6),    // 001011
  82.     TAB4_REP1(4,+1,6),    // 001100
  83.     TAB4_REP1(4,-1,6),    // 001101
  84.     TAB4_REP1(3,+1,6),    // 001110
  85.     TAB4_REP1(3,-1,6),    // 001111
  86.     TAB4_REP2(0,+2,5),    // 01000
  87.     TAB4_REP2(0,-2,5),    // 01001
  88.     TAB4_REP2(2,+1,5),    // 01010
  89.     TAB4_REP2(2,-1,5),    // 01011
  90.     TAB4_REP4(1,+1,4),    // 0110
  91.     TAB4_REP4(1,-1,4),    // 0111
  92.  
  93.     TAB4_REP8(0, 0,0),    // 100 (escape)
  94.     TAB4_REP8(0, 0,0),    // 101 (escape)
  95.     TAB4_REP8(0,+1,3),    // 110
  96.     TAB4_REP8(0,-1,3),    // 111
  97. };
  98.  
  99. static const signed char dct_short_tab[64][4]={
  100.     TAB4_REP2( 2,+2,8),    // 00001000
  101.     TAB4_REP2( 2,-2,8),    // 00001001
  102.     TAB4_REP2( 9,+1,8),    // 00001010
  103.     TAB4_REP2( 9,-1,8),    // 00001011
  104.     TAB4_REP2( 0,+4,8),    // 00001100
  105.     TAB4_REP2( 0,-4,8),    // 00001101
  106.     TAB4_REP2( 8,+1,8),    // 00001110
  107.     TAB4_REP2( 8,-1,8),    // 00001111
  108.     TAB4_REP4( 7,+1,7),    // 0001000
  109.     TAB4_REP4( 7,-1,7),    // 0001001
  110.     TAB4_REP4( 6,+1,7),    // 0001010
  111.     TAB4_REP4( 6,-1,7),    // 0001011
  112.     TAB4_REP4( 1,+2,7),    // 0001100
  113.     TAB4_REP4( 1,-2,7),    // 0001101
  114.     TAB4_REP4( 5,+1,7),    // 0001110
  115.     TAB4_REP4( 5,-1,7),    // 0001111
  116.     TAB4_REP1(13,+1,9),    // 001000000
  117.     TAB4_REP1(13,-1,9),    // 001000001
  118.     TAB4_REP1( 0,+6,9),    // 001000010
  119.     TAB4_REP1( 0,-6,9),    // 001000011
  120.     TAB4_REP1(12,+1,9),    // 001000100
  121.     TAB4_REP1(12,-1,9),    // 001000101
  122.     TAB4_REP1(11,+1,9),    // 001000110
  123.     TAB4_REP1(11,-1,9),    // 001000111
  124.     TAB4_REP1( 3,+2,9),    // 001001000
  125.     TAB4_REP1( 3,-2,9),    // 001001001
  126.     TAB4_REP1( 1,+3,9),    // 001001010
  127.     TAB4_REP1( 1,-3,9),    // 001001011
  128.     TAB4_REP1( 0,+5,9),    // 001001100
  129.     TAB4_REP1( 0,-5,9),    // 001001101
  130.     TAB4_REP1(10,+1,9),    // 001001110
  131.     TAB4_REP1(10,-1,9),    // 001001111
  132. };
  133.  
  134. static const unsigned char dct_long_tab[112][4]={
  135.     TAB4_REP1(10, 2,7),        // 000000 0010000
  136.     TAB4_REP1( 9, 2,7),        // 000000 0010001
  137.     TAB4_REP1( 5, 3,7),        // 000000 0010010
  138.     TAB4_REP1( 3, 4,7),        // 000000 0010011
  139.     TAB4_REP1( 2, 5,7),        // 000000 0010100
  140.     TAB4_REP1( 1, 7,7),        // 000000 0010101
  141.     TAB4_REP1( 1, 6,7),        // 000000 0010110
  142.     TAB4_REP1( 0,15,7),        // 000000 0010111
  143.     TAB4_REP1( 0,14,7),        // 000000 0011000
  144.     TAB4_REP1( 0,13,7),        // 000000 0011001
  145.     TAB4_REP1( 0,12,7),        // 000000 0011010
  146.     TAB4_REP1(26, 1,7),        // 000000 0011011
  147.     TAB4_REP1(25, 1,7),        // 000000 0011100
  148.     TAB4_REP1(24, 1,7),        // 000000 0011101
  149.     TAB4_REP1(23, 1,7),        // 000000 0011110
  150.     TAB4_REP1(22, 1,7),        // 000000 0011111
  151.     TAB4_REP2( 0,11,6),        // 000000 010000
  152.     TAB4_REP2( 8, 2,6),        // 000000 010001
  153.     TAB4_REP2( 4, 3,6),        // 000000 010010
  154.     TAB4_REP2( 0,10,6),        // 000000 010011
  155.     TAB4_REP2( 2, 4,6),        // 000000 010100
  156.     TAB4_REP2( 7, 2,6),        // 000000 010101
  157.     TAB4_REP2(21, 1,6),        // 000000 010110
  158.     TAB4_REP2(20, 1,6),        // 000000 010111
  159.     TAB4_REP2( 0, 9,6),        // 000000 011000
  160.     TAB4_REP2(19, 1,6),        // 000000 011001
  161.     TAB4_REP2(18, 1,6),        // 000000 011010
  162.     TAB4_REP2( 1, 5,6),        // 000000 011011
  163.     TAB4_REP2( 3, 3,6),        // 000000 011100
  164.     TAB4_REP2( 0, 8,6),        // 000000 011101
  165.     TAB4_REP2( 6, 2,6),        // 000000 011110
  166.     TAB4_REP2(17, 1,6),        // 000000 011111
  167.     TAB4_REP8(16, 1,4),        // 000000 1000
  168.     TAB4_REP8( 5, 2,4),        // 000000 1001
  169.     TAB4_REP8( 0, 7,4),        // 000000 1010
  170.     TAB4_REP8( 2, 3,4),        // 000000 1011
  171.     TAB4_REP8( 1, 4,4),        // 000000 1100
  172.     TAB4_REP8(15, 1,4),        // 000000 1101
  173.     TAB4_REP8(14, 1,4),        // 000000 1110
  174.     TAB4_REP8( 4, 2,4),        // 000000 1111
  175. };
  176.  
  177. static const unsigned char dct_vlong_tab[112][4]={
  178.     TAB4_REP1( 1,18,10),    // 000000 0000010000
  179.     TAB4_REP1( 1,17,10),    // 000000 0000010001
  180.     TAB4_REP1( 1,16,10),    // 000000 0000010010
  181.     TAB4_REP1( 1,15,10),    // 000000 0000010011
  182.     TAB4_REP1( 6, 3,10),    // 000000 0000010100
  183.     TAB4_REP1(16, 2,10),    // 000000 0000010101
  184.     TAB4_REP1(15, 2,10),    // 000000 0000010110
  185.     TAB4_REP1(14, 2,10),    // 000000 0000010111
  186.     TAB4_REP1(13, 2,10),    // 000000 0000011000
  187.     TAB4_REP1(12, 2,10),    // 000000 0000011001
  188.     TAB4_REP1(11, 2,10),    // 000000 0000011010
  189.     TAB4_REP1(31, 1,10),    // 000000 0000011011
  190.     TAB4_REP1(30, 1,10),    // 000000 0000011100
  191.     TAB4_REP1(29, 1,10),    // 000000 0000011101
  192.     TAB4_REP1(28, 1,10),    // 000000 0000011110
  193.     TAB4_REP1(27, 1,10),    // 000000 0000011111
  194.     TAB4_REP2( 0,40, 9),    // 000000 000010000
  195.     TAB4_REP2( 0,39, 9),    // 000000 000010001
  196.     TAB4_REP2( 0,38, 9),    // 000000 000010010
  197.     TAB4_REP2( 0,37, 9),    // 000000 000010011
  198.     TAB4_REP2( 0,36, 9),    // 000000 000010100
  199.     TAB4_REP2( 0,35, 9),    // 000000 000010101
  200.     TAB4_REP2( 0,34, 9),    // 000000 000010110
  201.     TAB4_REP2( 0,33, 9),    // 000000 000010111
  202.     TAB4_REP2( 0,32, 9),    // 000000 000011000
  203.     TAB4_REP2( 1,14, 9),    // 000000 000011001
  204.     TAB4_REP2( 1,13, 9),    // 000000 000011010
  205.     TAB4_REP2( 1,12, 9),    // 000000 000011011
  206.     TAB4_REP2( 1,11, 9),    // 000000 000011100
  207.     TAB4_REP2( 1,10, 9),    // 000000 000011101
  208.     TAB4_REP2( 1, 9, 9),    // 000000 000011110
  209.     TAB4_REP2( 1, 8, 9),    // 000000 000011111
  210.     TAB4_REP4( 0,31, 8),    // 000000 00010000
  211.     TAB4_REP4( 0,30, 8),    // 000000 00010001
  212.     TAB4_REP4( 0,29, 8),    // 000000 00010010
  213.     TAB4_REP4( 0,28, 8),    // 000000 00010011
  214.     TAB4_REP4( 0,27, 8),    // 000000 00010100
  215.     TAB4_REP4( 0,26, 8),    // 000000 00010101
  216.     TAB4_REP4( 0,25, 8),    // 000000 00010110
  217.     TAB4_REP4( 0,24, 8),    // 000000 00010111
  218.     TAB4_REP4( 0,23, 8),    // 000000 00011000
  219.     TAB4_REP4( 0,22, 8),    // 000000 00011001
  220.     TAB4_REP4( 0,21, 8),    // 000000 00011010
  221.     TAB4_REP4( 0,20, 8),    // 000000 00011011
  222.     TAB4_REP4( 0,19, 8),    // 000000 00011100
  223.     TAB4_REP4( 0,18, 8),    // 000000 00011101
  224.     TAB4_REP4( 0,17, 8),    // 000000 00011110
  225.     TAB4_REP4( 0,16, 8),    // 000000 00011111
  226. };
  227.  
  228. #undef TAB4_REP1
  229. #undef TAB4_REP2
  230. #undef TAB4_REP4
  231. #undef TAB4_REP8
  232.  
  233. ///////////////////////////////////////////////////////////////////////////
  234.  
  235. class VDMPEGDecoder : public IVDMPEGDecoder {
  236. private:
  237.     typedef unsigned char YCCSample;
  238.     
  239.     // critical stuff goes here -- we want short indices.
  240.  
  241.     // bit decoding -- this needs to be VERY FAST.
  242.  
  243.     unsigned long bitheap;
  244.     const unsigned char *bitsrc;
  245.     int bitcnt;        // 24-bits in heap
  246.  
  247. #if 0
  248.     __forceinline void bitheap_reset(const unsigned char *src) {
  249.         bitsrc = src+4;
  250.         bitheap = bswap(*(const unsigned long *)src);
  251.         bitcnt = -8;
  252.     }
  253.  
  254.     __forceinline unsigned long bitheap_peekbits(unsigned int bits) {
  255.         return bitheap >> (32-bits);
  256.     }
  257.  
  258.     __forceinline bool bitheap_checkzerobits(unsigned int bits) {
  259. //        return bitheap < (1UL<<(32-bits));
  260.         return !bitheap_peekbits(bits);
  261.     }
  262.  
  263.     __forceinline unsigned long bitheap_getbits(unsigned int bits) {
  264.         unsigned long rv = bitheap >> (32-bits);
  265.  
  266.         bitcnt += bits;
  267.  
  268.         bitheap <<= bits;
  269.  
  270.         if (bits >= 16) {
  271.             bitheap += (unsigned long)*bitsrc++ << bitcnt;
  272.             bitcnt -= 8;
  273.         }
  274.  
  275.         if (bits >= 8) {
  276.             bitheap += (unsigned long)*bitsrc++ << bitcnt;
  277.             bitcnt -= 8;
  278.         }
  279.  
  280.         if (bitcnt >= 0) {
  281.             bitheap += (unsigned long)*bitsrc++ << bitcnt;
  282.             bitcnt -= 8;
  283.         }
  284.         return rv;
  285.     }
  286.  
  287.     __forceinline long bitheap_getbitssigned(unsigned int bits) {
  288.         long rv = (long)bitheap >> (32-bits);
  289.  
  290.         bitcnt += bits;
  291.         bitheap <<= bits;
  292.  
  293.         if (bits >= 16) {
  294.             bitheap += (unsigned long)*bitsrc++ << bitcnt;
  295.             bitcnt -= 8;
  296.         }
  297.  
  298.         if (bits >= 8) {
  299.             bitheap += (unsigned long)*bitsrc++ << bitcnt;
  300.             bitcnt -= 8;
  301.         }
  302.  
  303.         if (bitcnt >= 0) {
  304.             bitheap += (unsigned long)*bitsrc++ << bitcnt;
  305.             bitcnt -= 8;
  306.         }
  307.  
  308.         return rv;
  309.     }
  310.  
  311.     __forceinline void bitheap_skipbits(unsigned int bits) {
  312.         bitcnt += bits;
  313.         bitheap <<= bits;
  314.  
  315.         if (bits >= 16) {
  316.             bitheap += (unsigned long)*bitsrc++ << bitcnt;
  317.             bitcnt -= 8;
  318.         }
  319.  
  320.         if (bits >= 8) {
  321.             bitheap += (unsigned long)*bitsrc++ << bitcnt;
  322.             bitcnt -= 8;
  323.         }
  324.  
  325.         if (bitcnt >= 0) {
  326.             bitheap += (unsigned long)*bitsrc++ << bitcnt;
  327.             bitcnt -= 8;
  328.         }
  329.     }
  330. #else
  331.     __forceinline void bitheap_reset(const unsigned char *src) {
  332.         bitsrc = src+4;
  333.         bitheap = bswap(*(const unsigned long *)src);
  334.         bitcnt = -8;
  335.     }
  336.  
  337.     __forceinline unsigned long bitheap_peekbits(unsigned int bits) {
  338.         return bitheap >> (32-bits);
  339.     }
  340.  
  341.     __forceinline bool bitheap_checkzerobits(unsigned int bits) {
  342. //        return bitheap < (1UL<<(32-bits));
  343.         return !bitheap_peekbits(bits);
  344.     }
  345.  
  346.     __forceinline long bitheap_getflag() {
  347.         long rv = bitheap & 0x80000000;
  348.  
  349.         bitheap += bitheap;
  350.  
  351.         if (++bitcnt >= 0) {
  352.             bitheap += (unsigned long)*bitsrc++ << bitcnt;
  353.             bitcnt -= 8;
  354.         }
  355.  
  356.         return rv;
  357.     }
  358.  
  359.     __forceinline unsigned long bitheap_getbitsconst(unsigned int bits) {
  360.         unsigned long rv = bitheap >> (32-bits);
  361.  
  362.         bitcnt += bits;
  363.  
  364.         bitheap <<= bits;
  365.  
  366.         if (bits >= 16) {
  367.             bitheap += (unsigned long)*bitsrc++ << bitcnt;
  368.             bitcnt -= 8;
  369.         }
  370.  
  371.         if (bits >= 8) {
  372.             bitheap += (unsigned long)*bitsrc++ << bitcnt;
  373.             bitcnt -= 8;
  374.         }
  375.  
  376.         if ((bits&7) && bitcnt >= 0) {
  377.             bitheap += (unsigned long)*bitsrc++ << bitcnt;
  378.             bitcnt -= 8;
  379.         }
  380.         return rv;
  381.     }
  382.  
  383.     __forceinline unsigned long bitheap_getbits(unsigned int bits) {
  384.         unsigned long rv = bitheap >> (32-bits);
  385.  
  386.         bitcnt += bits;
  387.  
  388.         bitheap <<= bits;
  389.  
  390.         while(bitcnt >= 0) {
  391.             bitheap += (unsigned long)*bitsrc++ << bitcnt;
  392.             bitcnt -= 8;
  393.         }
  394.         return rv;
  395.     }
  396.  
  397.     __forceinline long bitheap_getbitssigned(unsigned int bits) {
  398.         long rv = (long)bitheap >> (32-bits);
  399.  
  400.         bitcnt += bits;
  401.         bitheap <<= bits;
  402.  
  403.         while(bitcnt >= 0) {
  404.             bitheap += (unsigned long)*bitsrc++ << bitcnt;
  405.             bitcnt -= 8;
  406.         }
  407.  
  408.         return rv;
  409.     }
  410.  
  411.     __forceinline long bitheap_getbitssignedconst(unsigned int bits) {
  412.         long rv = (long)bitheap >> (32-bits);
  413.  
  414.         bitcnt += bits;
  415.         bitheap <<= bits;
  416.  
  417.         if (bits >= 16) {
  418.             bitheap += (unsigned long)*bitsrc++ << bitcnt;
  419.             bitcnt -= 8;
  420.         }
  421.  
  422.         if (bits >= 8) {
  423.             bitheap += (unsigned long)*bitsrc++ << bitcnt;
  424.             bitcnt -= 8;
  425.         }
  426.  
  427.         if ((bits&7) && bitcnt >= 0) {
  428.             bitheap += (unsigned long)*bitsrc++ << bitcnt;
  429.             bitcnt -= 8;
  430.         }
  431.  
  432.         return rv;
  433.     }
  434.  
  435.     __forceinline void bitheap_skipbitsconst(unsigned int bits) {
  436.         bitcnt += bits;
  437.         bitheap <<= bits;
  438.  
  439.         if (bits >= 16) {
  440.             bitheap += (unsigned long)*bitsrc++ << bitcnt;
  441.             bitcnt -= 8;
  442.         }
  443.  
  444.         if (bits >= 8) {
  445.             bitheap += (unsigned long)*bitsrc++ << bitcnt;
  446.             bitcnt -= 8;
  447.         }
  448.  
  449.         if ((bits&7) && bitcnt >= 0) {
  450.             bitheap += (unsigned long)*bitsrc++ << bitcnt;
  451.             bitcnt -= 8;
  452.         }
  453.     }
  454.  
  455.     __forceinline void bitheap_skipbits(unsigned int bits) {
  456.         bitcnt += bits;
  457.         bitheap <<= bits;
  458.  
  459.         while(bitcnt >= 0) {
  460.             bitheap += (unsigned long)*bitsrc++ << bitcnt;
  461.             bitcnt -= 8;
  462.         }
  463.     }
  464.  
  465.     __forceinline void bitheap_skipbits8(unsigned int bits) {
  466.         bitcnt += bits;
  467.         bitheap <<= bits;
  468.  
  469.         if (bitcnt >= 0) {
  470.             bitheap += (unsigned long)*bitsrc++ << bitcnt;
  471.             bitcnt -= 8;
  472.         }
  473.     }
  474. #endif
  475.  
  476.     ////////////////////////////////////
  477.  
  478.     enum {
  479.         kMBF_NewQuant    = 16,
  480.         kMBF_Both        = 12,
  481.         kMBF_Forward    = 8,
  482.         kMBF_Backward    = 4,
  483.         kMBF_Pattern    = 2,
  484.         kMBF_Intra        = 1
  485.     };
  486.  
  487.     long        mnYPitch;
  488.     long        mnYPitch8;
  489.     long        mnCPitch;
  490.     long        mnCPitch8;
  491.     unsigned    mnBlockW, mnBlockH;
  492.     int            mnLastY_DC;
  493.     int            mnLastCr_DC;
  494.     int            mnLastCb_DC;
  495.  
  496.     YCCSample *mpY, *mpCr, *mpCb;
  497.     YCCSample *mpFwdY, *mpFwdCr, *mpFwdCb;
  498.     YCCSample *mpBackY, *mpBackCr, *mpBackCb;
  499.  
  500.     int mnQuantValue;
  501.     const int *mpZigzagOrder;
  502.  
  503.     void (VDMPEGDecoder::*mpBlockDecoder)(YCCSample *dst, long pitch, bool intra, int dc) throw();
  504.  
  505.     int        mnForwardRSize;
  506.     int        mnForwardMask;
  507.     int        mnForwardSignExtend;
  508.     int        mnBackwardRSize;
  509.     int        mnBackwardMask;
  510.     int        mnBackwardSignExtend;
  511.  
  512.     bool    mbForwardFullPel;
  513.     bool    mbBackwardFullPel;
  514.  
  515.     int mIntraQ[32][64], mNonintraQ[32][64];
  516.  
  517.     const int *mpCurrentIntraQ, *mpCurrentNonintraQ;
  518.  
  519.     // non-critical stuff here.
  520.  
  521.     VDAtomicInt mRefCount;
  522.     
  523.     struct MPEGBuffer {
  524.         YCCSample *pY, *pCr, *pCb;
  525.         YCCSample *pYBuffer, *pCBuffer;
  526.         long frame;
  527.     } *mpBuffers;
  528.     int mnBuffers;
  529.     unsigned    mnHeight;
  530.  
  531.     long    mErrorState;
  532.  
  533.     void (VDMPEGDecoder::*mpSliceDecoder)(int) throw();
  534.  
  535.     const VDMPEGPredictorSet    *mpPredictors;
  536.     const VDMPEGConverterSet    *mpConverters;
  537.     const VDMPEGIDCTSet            *mpIDCTs;
  538.  
  539.     int        mUnscaledIntraQ[64];
  540.     int        mUnscaledNonintraQ[64];
  541.     bool    mbQuantizersDirty;
  542.  
  543.     //////
  544.  
  545.     void SetError(long err) { mErrorState |= err; }
  546.  
  547.     void UpdateQuantizers();
  548.  
  549.     void DecodeBlockPrescaled(YCCSample *dst, long pitch, bool intra, int dc) throw();
  550.     void DecodeBlockNonPrescaled(YCCSample *dst, long pitch, bool intra, int dc) throw();
  551.  
  552.     void DecodeBlock_Y(YCCSample *dst, bool intra) throw();
  553.     void DecodeBlock_C(YCCSample *dst, int& dc, bool intra) throw();
  554.  
  555.     int DecodeMotionVector(int rsize) throw();
  556.     int DecodeCodedBlockPattern() throw();
  557.  
  558.     void DecodeSlice_I(int) throw();
  559.     void DecodeSlice_P(int) throw();
  560.     void DecodeSlice_B(int) throw();
  561.  
  562.     void CopyPredictionForward(int posx, int posy, int dx, int dy);
  563.     void CopyPredictionBackward(int posx, int posy, int dx, int dy);
  564.     void AddPredictionBackward(int posx, int posy, int dx, int dy);
  565.  
  566.     void CopyPredictionY(YCCSample *dst, YCCSample *src, bool halfpelX, bool halfpelY);
  567.     void CopyPredictionC(YCCSample *dst, YCCSample *src, bool halfpelX, bool halfpelY);
  568.     void AddPredictionY(YCCSample *dst, YCCSample *src, bool halfpelX, bool halfpelY);
  569.     void AddPredictionC(YCCSample *dst, YCCSample *src, bool halfpelX, bool halfpelY);
  570.  
  571. public:
  572.     VDMPEGDecoder();
  573.     ~VDMPEGDecoder() throw();
  574.     
  575.     int AddRef() { return mRefCount.inc(); }
  576.     int Release() { int rc = mRefCount.dec(); if (!rc) delete this; return rc; }
  577.     
  578.     // decoding
  579.     
  580.     bool Init(int width, int height);
  581.     void Shutdown();
  582.     void SetIntraQuantizers(const unsigned char *pMatrix);
  583.     void SetNonintraQuantizers(const unsigned char *pMatrix);
  584.     void SetPredictors(const VDMPEGPredictorSet *pPredictors);
  585.     void SetConverters(const VDMPEGConverterSet *pConverters);
  586.     void SetIDCTs(const VDMPEGIDCTSet *pIDCTs);
  587.     
  588.     int DecodeFrame(const void *src, long len, long frame, int dst, int fwd, int rev);
  589.     long GetErrorState();
  590.     
  591.     // framebuffer access
  592.     
  593.     int GetFrameBuffer(long frame);
  594.     long GetFrameNumber(int buffer);
  595.     void CopyFrameBuffer(int dst, int src, long frameno);
  596.     void SwapFrameBuffers(int dst, int src);
  597.     void ClearFrameBuffers();
  598.     const void *GetYBuffer(int buffer, ptrdiff_t& pitch);
  599.     const void *GetCrBuffer(int buffer, ptrdiff_t& pitch);
  600.     const void *GetCbBuffer(int buffer, ptrdiff_t& pitch);
  601.     
  602.     // framebuffer conversion
  603.  
  604.     inline bool Decode(void *dst, long pitch, int buffer, tVDMPEGConverter pConv) {
  605.         if (buffer < 0 || buffer >= mnBuffers)
  606.             return false;
  607.  
  608.         if (!pConv)
  609.             return false;
  610.  
  611.         pConv((char *)dst, pitch,
  612.                 mpBuffers[buffer].pY, mnYPitch,
  613.                 mpBuffers[buffer].pCr,
  614.                 mpBuffers[buffer].pCb, mnCPitch, (int)mnBlockW, (int)mnHeight);
  615.  
  616.         return true;
  617.     }
  618.  
  619.     bool DecodeUYVY(void *dst, ptrdiff_t pitch, int buffer);
  620.     bool DecodeYUYV(void *dst, ptrdiff_t pitch, int buffer);
  621.     bool DecodeYVYU(void *dst, ptrdiff_t pitch, int buffer);
  622.     bool DecodeY41P(void *dst, ptrdiff_t pitch, int buffer);
  623.     bool DecodeRGB15(void *dst, ptrdiff_t pitch, int buffer);
  624.     bool DecodeRGB16(void *dst, ptrdiff_t pitch, int buffer);
  625.     bool DecodeRGB24(void *dst, ptrdiff_t pitch, int buffer);
  626.     bool DecodeRGB32(void *dst, ptrdiff_t pitch, int buffer);
  627.  
  628. private:
  629.     template<class T>
  630.     __forceinline void DecodeBlock(YCCSample *dst, ptrdiff_t pitch, bool intra, int dc, bool bPrescaled, T) {
  631.         unsigned long v = bitheap_peekbits(32);
  632.         T dct_coeff0[67];
  633.         T *dct_coeff = (T *)(((uintptr)dct_coeff0 + 7) & ~7);
  634.         const int *quant = mpCurrentIntraQ;
  635.         int idx = 0;
  636.         long level_addend = 0;
  637.  
  638.         memset(dct_coeff, 0, sizeof(*dct_coeff)*64);
  639.  
  640.         if (bPrescaled)
  641.             dct_coeff[0] = (dc * quant[0] + 128)>>8;
  642.         else
  643.             dct_coeff[0] = dc<<3;
  644.  
  645.         if (!intra) {
  646.             quant = mpCurrentNonintraQ;
  647.  
  648.             idx = -1;
  649.  
  650.             if (v >= 0x80000000) {
  651.                 int level;
  652.  
  653.                 if (bPrescaled)
  654.                     level = ((3 * quant[0] + 128)>>12);
  655.                 else
  656.                     level = ((((3 * quant[0])>>4)-1)|1);
  657.  
  658.                 if (v & 0x40000000)
  659.                     level = -level;
  660.  
  661.                 dct_coeff[0] = level;
  662.  
  663.                 idx = 0;
  664.                 bitheap_skipbits(2);
  665.                 v = bitheap_peekbits(32);
  666.             }
  667.  
  668.             level_addend = 1;
  669.         }
  670.  
  671.         for(;;) {
  672.             int level;
  673.             int level_sign;
  674.             int run;
  675.  
  676.     /*        {
  677.                 printf("%08lx | ", v);
  678.                 for(int i=11; i>=0; --i)
  679.                     putchar('0' + ((v>>i)&1));
  680.  
  681.             }*/
  682.  
  683. #if 1
  684.             if (v >= 0x80000000) {        // 1...
  685.                 if (v < 0xc0000000) {
  686.                     bitheap_skipbitsconst(2);
  687.     //                printf("EOB\n");
  688.                     break;
  689.                 }
  690.  
  691.                 level = 1;
  692.                 run = 0;
  693.                 level_sign = 6 - (v>>29);
  694.                 bitheap_skipbitsconst(3);
  695.             } else if (v >= 0x28000000) {        // 0010 10xx xxxx
  696.                 v >>= 26;
  697.  
  698.                 run = dct_vshort_tab[v-10][0];
  699.                 level = dct_vshort_tab[v-10][1];
  700.                 bitheap_skipbits8(dct_vshort_tab[v-10][2]);
  701.                 level_sign = dct_vshort_tab[v-10][3];
  702.             } else if (v >= 0x08000000) {        // 000 0100 00xx
  703.                 v >>= 23;
  704.  
  705.                 run = dct_short_tab[v-16][0];
  706.                 level = dct_short_tab[v-16][1];
  707.                 bitheap_skipbits8(dct_short_tab[v-16][2]);
  708.                 level_sign = dct_short_tab[v-16][3];
  709.             } else if (v >= 0x04000000) {    // 000001 (escape)
  710. #else
  711.  
  712.             if ((v&0xc0000000) == 0x80000000) {
  713.                 bitheap_skipbits(2);
  714.                 break;
  715.             }
  716.  
  717.             if (v >= 0x08000000) {
  718.                 typedef const signed char *tpTableEntry;
  719.  
  720.                 const unsigned long ptr1 = (unsigned long)dct_vshort_tab[(v>>26)-10];
  721.                 const unsigned long ptr2 = (unsigned long)dct_short_tab[(v>>23)-16];
  722.  
  723.                 signed long switch1 = (signed long)((v>>1) - 0x14000000) >> 31;
  724.  
  725.                 //    range                switch1        switch2
  726.                 //    80000000-FFFFFFFF    true        ?
  727.                 //    28000000-7FFFFFFF    false        false
  728.                 //    08000000-27FFFFFF    false        true
  729.  
  730.                 tpTableEntry ptr = (tpTableEntry)((ptr1 & ~switch1) + (ptr2 & switch1));
  731.                 
  732.                 run = ptr[0];
  733.                 level = ptr[1];
  734.                 bitheap_skipbits8(ptr[2]);
  735.                 level_sign = ptr[3];
  736.             } else if (v >= 0x04000000) {    // 000001 (escape)            
  737.  
  738. #endif
  739.                 run = (v>>20) - 64;
  740.  
  741.                 bitheap_skipbits(12);
  742.  
  743.                 level = bitheap_getbitssignedconst(8);
  744.  
  745.                 if (!(level & 0x7f))
  746.                     level = level*2 + bitheap_getbitsconst(8);
  747.  
  748.                 level_sign = 0;
  749.  
  750.                 if (level<0) {
  751.                     level_sign = -1;
  752.                     level = -level;
  753.                 }
  754.             } else {                    // 000000...
  755.  
  756.                 // The longest code is 16 bits, but given the six zeroes we already
  757.                 // know about, that still leaves a 1K table.  We split the remaining
  758.                 // codespace into a 7-bit long table and a 7-bit very-long table.
  759.  
  760.                 bitheap_skipbits(6);
  761.  
  762. #if 0
  763.                 v = bitheap_peekbits(10);
  764.  
  765.                 if (v >= 0x080) {
  766.                     v >>= 3;
  767.  
  768.                     run = dct_long_tab[v-16][0];
  769.                     level = dct_long_tab[v-16][1];
  770.                     bitheap_skipbits(dct_long_tab[v-16][2]);
  771.                 } else {
  772.                     // codes below 000000 0000010000 are illegal
  773.  
  774.                     if (v < 16) {
  775.                         SetError(kError | kErrorBadValue);
  776.                         break;
  777.                     }
  778.  
  779.     //                printf("%d\n", v);
  780.  
  781.                     run = dct_vlong_tab[v-16][0];
  782.                     level = dct_vlong_tab[v-16][1];
  783.                     bitheap_skipbits(dct_vlong_tab[v-16][2]);
  784.                 }
  785. #else
  786.                 typedef const signed char *tpTableEntry;
  787.  
  788.                 v = bitheap_peekbits(10);
  789.  
  790.                 if (v < 16) {
  791.                     SetError(kError | kErrorBadValue);
  792.                     break;
  793.                 }
  794.  
  795.                 const uintptr ptr1 = (uintptr)dct_long_tab[(v>>3)-16];
  796.                 const uintptr ptr2 = (uintptr)dct_vlong_tab[v-16];
  797.  
  798.                 sintptr switch1 = (sintptr)(sint32)(v - 0x080) >> (sizeof(void *) * 8 - 1);
  799.  
  800.                 //    range                switch1        switch2
  801.                 //    80000000-FFFFFFFF    true        ?
  802.                 //    28000000-7FFFFFFF    false        false
  803.                 //    08000000-27FFFFFF    false        true
  804.  
  805.                 tpTableEntry ptr = (tpTableEntry)((ptr1 & ~switch1) + (ptr2 & switch1));
  806.                 
  807.                 run = ptr[0];
  808.                 level = ptr[1];
  809.                 bitheap_skipbits(ptr[2]);
  810. #endif
  811.  
  812.                 level_sign = bitheap_getbitssignedconst(1);
  813.             }
  814.  
  815.             VDASSERT(!((level_sign+1)&~1));
  816.             VDASSERT(level>0 && level<=256);
  817.  
  818.     //        printf("  run(%2d), level(%2d) | ", run, level);
  819.  
  820.             idx += run+1;
  821.  
  822.             // forbidden stuff
  823.  
  824.             if (idx >= 64 || !level) {
  825.                 idx = 63;
  826.                 SetError(kError | kErrorTooManyCoefficients);
  827.                 break;
  828.             }
  829.  
  830.             // decode coefficient and oddify toward zero
  831.  
  832.             const int pos = mpZigzagOrder[idx];
  833.  
  834.     //        printf("%2d : %2d -> %2d\n", pos, (pos&56) + (pos&3)*2 + ((pos&4)>>2), level);
  835.  
  836.             if (bPrescaled)
  837.                 level = ((2*level + level_addend) * quant[idx] + 128) >> 12;
  838.             else
  839.                 level = ((((2*level + level_addend) * quant[idx]) >> 4) - 1) | 1;
  840.  
  841.             // apply sign and store coefficient
  842.  
  843.             dct_coeff[pos] = (level ^ level_sign) - level_sign;
  844.  
  845.             // prepare for next round
  846.  
  847.             v = bitheap_peekbits(32);
  848.         }
  849.  
  850.         // DCT coefficients are decoded -- now apply IDCT.
  851.  
  852. #if 0
  853.         static int count[4]={0};
  854.         static int total = 0;
  855.         ++count[(idx>0) + (idx>9) + (idx > 2)];
  856.         if (!(++total & 65535)) {
  857.             VDDEBUG2("MPEG: %d DC, %d <3, %d <10, %d full\n", count[0], count[1], count[2], count[3]);
  858.             memset(count, 0, sizeof count);
  859.         }
  860. #endif
  861.  
  862.         if (intra)
  863.             mpIDCTs->pIntra(dst, pitch, dct_coeff, idx);
  864.         else
  865.             mpIDCTs->pNonintra(dst, pitch, dct_coeff, idx);
  866.     }
  867. };
  868.  
  869. ///////////////////////////////////////////////////////////////////////////
  870.  
  871. VDMPEGDecoder::VDMPEGDecoder()
  872.     : mRefCount(0)
  873.     , mpBuffers(NULL)
  874.     , mbQuantizersDirty(true)
  875. {
  876. }
  877.  
  878. VDMPEGDecoder::~VDMPEGDecoder() {
  879.     Shutdown();
  880. }
  881.  
  882. IVDMPEGDecoder *CreateVDMPEGDecoder() {
  883.     return new VDMPEGDecoder();
  884. }
  885.  
  886. ///////////////////////////////////////////////////////////////////////////
  887. //
  888. //    control
  889. //
  890. ///////////////////////////////////////////////////////////////////////////
  891.  
  892. bool VDMPEGDecoder::Init(int width, int height) {
  893.     int i;
  894.  
  895.     Shutdown();
  896.  
  897.     mnBlockW    = (width+15) >> 4;
  898.     mnBlockH    = (height+15) >> 4;
  899.     mnHeight    = height;
  900.     mnYPitch    = (mnBlockW * 24 + 15) & ~15;        // all scanlines must be aligned to 16 for SSE/SSE2
  901.     mnYPitch8    = mnYPitch * 8;
  902.     mnCPitch    = mnYPitch * 2;
  903.     mnCPitch8    = mnCPitch * 8;
  904.  
  905.     // Attempt to allocate all buffers.
  906.  
  907.     mnBuffers = 3;
  908.  
  909.     if (!(mpBuffers = new MPEGBuffer[mnBuffers])) {
  910.         Shutdown();
  911.         return false;
  912.     }
  913.  
  914.     for(i=0; i<mnBuffers; ++i) {
  915.         mpBuffers[i].pYBuffer = mpBuffers[i].pCBuffer = NULL;
  916.         mpBuffers[i].frame = -1;
  917.     }
  918.  
  919.     for(i=0; i<mnBuffers; ++i) {
  920.         if (!(mpBuffers[i].pYBuffer = new_nothrow YCCSample[mnYPitch * mnBlockH * 16 + 127])) {
  921.             Shutdown();
  922.             return false;
  923.         }
  924.  
  925.         // Align Y to a 128-byte boundary (P4 L2 cache line) and set up Cb/Cr
  926.         mpBuffers[i].pY  = (YCCSample *)(((uintptr)mpBuffers[i].pYBuffer + 127) & ~127);
  927.         mpBuffers[i].pCr = mpBuffers[i].pY + mnBlockW * 16;
  928.         mpBuffers[i].pCb = mpBuffers[i].pCr + mnYPitch;
  929.     }
  930.  
  931.     // All good to go.
  932.  
  933.     return true;
  934. }
  935.  
  936. void VDMPEGDecoder::Shutdown() {
  937.     int i;
  938.  
  939.     if (mpBuffers) {
  940.         for(i=0; i<mnBuffers; ++i)
  941.             delete[] mpBuffers[i].pYBuffer;
  942.  
  943.         delete[] mpBuffers;
  944.         mpBuffers = NULL;
  945.     }
  946. }
  947.  
  948. void VDMPEGDecoder::SetIntraQuantizers(const unsigned char *pMatrix) {
  949.     static const unsigned char intramatrix_default[64]={
  950.          8, 16, 19, 22, 26, 27, 29, 34,
  951.         16, 16, 22, 24, 27, 29, 34, 37,
  952.         19, 22, 26, 27, 29, 34, 34, 38,
  953.         22, 22, 26, 27, 29, 34, 37, 40,
  954.         22, 26, 27, 29, 32, 35, 40, 48,
  955.         26, 27, 29, 32, 35, 40, 48, 58, 
  956.         26, 27, 29, 34, 38, 46, 56, 69,
  957.         27, 29, 35, 38, 46, 56, 69, 83
  958.     };
  959.  
  960.     // The matrix comes in stored in zigzag order.
  961.  
  962.     if (pMatrix) {
  963.         for(int i=0; i<64; ++i)
  964.             mUnscaledIntraQ[i] = pMatrix[i];
  965.     } else {
  966.         for(int i=0; i<64; ++i)
  967.             mUnscaledIntraQ[i] = intramatrix_default[zigzag_std[i]];
  968.     }
  969.  
  970.     mbQuantizersDirty = true;
  971. }
  972.  
  973. void VDMPEGDecoder::SetNonintraQuantizers(const unsigned char *pMatrix) {
  974.     if (pMatrix) {
  975.         for(int i=0; i<64; ++i)
  976.             mUnscaledNonintraQ[i] = pMatrix[i];
  977.     } else {
  978.         for(int i=0; i<64; ++i)
  979.             mUnscaledNonintraQ[i] = 16;
  980.     }
  981.  
  982.     mbQuantizersDirty = true;
  983. }
  984.  
  985. void VDMPEGDecoder::SetPredictors(const VDMPEGPredictorSet *pPredictors) {
  986.     mpPredictors = pPredictors;
  987.     mbQuantizersDirty = true;
  988. }
  989.  
  990. void VDMPEGDecoder::SetConverters(const VDMPEGConverterSet *pConverters) {
  991.     mpConverters = pConverters;
  992. }
  993.  
  994. void VDMPEGDecoder::SetIDCTs(const VDMPEGIDCTSet *pIDCTs) {
  995. #if 0
  996.     VDIDCTComplianceResult result;
  997.     VDASSERT(VDTestVideoIDCTCompliance(*pIDCTs, result));
  998. #endif
  999.     mpIDCTs = pIDCTs;
  1000.     mpZigzagOrder = pIDCTs->pAltScan ? pIDCTs->pAltScan : zigzag_std;
  1001.     mpBlockDecoder = pIDCTs->pPrescaler ? &VDMPEGDecoder::DecodeBlockPrescaled : &VDMPEGDecoder::DecodeBlockNonPrescaled;
  1002.     mbQuantizersDirty = true;
  1003. }
  1004.  
  1005. void VDMPEGDecoder::UpdateQuantizers() {
  1006.     int i, j;
  1007.  
  1008.     if (mpIDCTs->pPrescaler)
  1009.         for(i=0; i<64; ++i)
  1010.             mIntraQ[1][i] = mUnscaledIntraQ[i] * mpIDCTs->pPrescaler[mpZigzagOrder[i]];
  1011.     else
  1012.         for(i=0; i<64; ++i)
  1013.             mIntraQ[1][i] = mUnscaledIntraQ[i];
  1014.  
  1015.     for(j=0; j<32; ++j) {
  1016.         // Do not scale the DC coefficient -- it is always 8 in all cases.
  1017.  
  1018.         mIntraQ[j][0] = mIntraQ[1][0];
  1019.  
  1020.         for(i=1; i<64; ++i)
  1021.             mIntraQ[j][i] = mIntraQ[1][i]*j;
  1022.     }
  1023.  
  1024.     if (mpIDCTs->pPrescaler)
  1025.         for(i=0; i<64; ++i)
  1026.             mNonintraQ[1][i] = mUnscaledNonintraQ[i] * mpIDCTs->pPrescaler[mpZigzagOrder[i]];
  1027.     else
  1028.         for(i=0; i<64; ++i)
  1029.             mNonintraQ[1][i] = mUnscaledNonintraQ[i];
  1030.  
  1031.     for(j=0; j<32; ++j)
  1032.         for(i=0; i<64; ++i)
  1033.             mNonintraQ[j][i] = mNonintraQ[1][i]*j;
  1034. }
  1035.  
  1036. ///////////////////////////////////////////////////////////////////////////
  1037. //
  1038. //    decoding
  1039. //
  1040. ///////////////////////////////////////////////////////////////////////////
  1041.  
  1042. long VDMPEGDecoder::GetErrorState() {
  1043.     long rv = mErrorState;
  1044.  
  1045.     mErrorState = 0;
  1046.  
  1047.     return rv;
  1048. }
  1049.  
  1050. ///////////////////////////////////////////////////////////////////////////
  1051. //
  1052. //    framebuffer access
  1053. //
  1054. ///////////////////////////////////////////////////////////////////////////
  1055.  
  1056. int VDMPEGDecoder::GetFrameBuffer(long frame) {
  1057.     int i;
  1058.  
  1059.     for(i=0; i<mnBuffers; ++i)
  1060.         if (mpBuffers[i].frame == frame)
  1061.             return i;
  1062.  
  1063.     return -1;
  1064. }
  1065.  
  1066. long VDMPEGDecoder::GetFrameNumber(int buffer) {
  1067.     if (buffer<0 || buffer >= mnBuffers)
  1068.         return -1;
  1069.  
  1070.     return mpBuffers[buffer].frame;
  1071. }
  1072.  
  1073. void VDMPEGDecoder::CopyFrameBuffer(int dst, int src, long frameno) {
  1074.     if ((unsigned)src >= (unsigned)mnBuffers || (unsigned)dst >= (unsigned)mnBuffers)
  1075.         return;
  1076.  
  1077.     VDMemcpyRect(mpBuffers[dst].pY, mnYPitch, mpBuffers[src].pY, mnYPitch, mnBlockW * 16, mnBlockH * 16);
  1078.     VDMemcpyRect(mpBuffers[dst].pCr, mnCPitch, mpBuffers[src].pCr, mnCPitch, mnBlockW * 8, mnBlockH * 8);
  1079.     VDMemcpyRect(mpBuffers[dst].pCb, mnCPitch, mpBuffers[src].pCb, mnCPitch, mnBlockW * 8, mnBlockH * 8);
  1080.  
  1081.     mpBuffers[dst].frame = frameno;
  1082. }
  1083.  
  1084. void VDMPEGDecoder::SwapFrameBuffers(int dst, int src) {
  1085.     if ((src|dst)<0 || src >= mnBuffers)
  1086.         return;
  1087.  
  1088.     MPEGBuffer t;
  1089.  
  1090.     t = mpBuffers[dst];
  1091.     mpBuffers[dst] = mpBuffers[src];
  1092.     mpBuffers[src] = t;
  1093. }
  1094.  
  1095. void VDMPEGDecoder::ClearFrameBuffers() {
  1096.     for(int i=0; i<mnBuffers; ++i) {
  1097.         mpBuffers[i].frame = -1;
  1098.     }
  1099. }
  1100.  
  1101. const void *VDMPEGDecoder::GetYBuffer(int buffer, ptrdiff_t& pitch) {
  1102.     if (buffer < 0 || buffer >= mnBuffers)
  1103.         return NULL;
  1104.  
  1105.     pitch = mnYPitch;
  1106.  
  1107.     return mpBuffers[buffer].pY;
  1108. }
  1109.  
  1110. const void *VDMPEGDecoder::GetCrBuffer(int buffer, ptrdiff_t& pitch) {
  1111.     if (buffer < 0 || buffer >= mnBuffers)
  1112.         return NULL;
  1113.  
  1114.     pitch = mnCPitch;
  1115.  
  1116.     return mpBuffers[buffer].pCr;
  1117. }
  1118.  
  1119. const void *VDMPEGDecoder::GetCbBuffer(int buffer, ptrdiff_t& pitch) {
  1120.     if (buffer < 0 || buffer >= mnBuffers)
  1121.         return NULL;
  1122.  
  1123.     pitch = mnCPitch;
  1124.  
  1125.     return mpBuffers[buffer].pCb;
  1126. }
  1127.  
  1128. ///////////////////////////////////////////////////////////////////////////
  1129. //
  1130. //    colorspace conversion
  1131. //
  1132. ///////////////////////////////////////////////////////////////////////////
  1133.  
  1134. bool VDMPEGDecoder::DecodeUYVY(void *dst, ptrdiff_t pitch, int buffer) {
  1135.     return Decode(dst, pitch, buffer, mpConverters->DecodeUYVY);
  1136. }
  1137.  
  1138. bool VDMPEGDecoder::DecodeYUYV(void *dst, ptrdiff_t pitch, int buffer) {
  1139.     return Decode(dst, pitch, buffer, mpConverters->DecodeYUYV);
  1140. }
  1141.  
  1142. bool VDMPEGDecoder::DecodeYVYU(void *dst, ptrdiff_t pitch, int buffer) {
  1143.     return Decode(dst, pitch, buffer, mpConverters->DecodeYVYU);
  1144. }
  1145.  
  1146. bool VDMPEGDecoder::DecodeY41P(void *dst, ptrdiff_t pitch, int buffer) {
  1147.     return Decode(dst, pitch, buffer, mpConverters->DecodeY41P);
  1148. }
  1149.  
  1150. bool VDMPEGDecoder::DecodeRGB15(void *dst, ptrdiff_t pitch, int buffer) {
  1151.     return Decode(dst, pitch, buffer, mpConverters->DecodeRGB15);
  1152. }
  1153.  
  1154. bool VDMPEGDecoder::DecodeRGB16(void *dst, ptrdiff_t pitch, int buffer) {
  1155.     return Decode(dst, pitch, buffer, mpConverters->DecodeRGB16);
  1156. }
  1157.  
  1158. bool VDMPEGDecoder::DecodeRGB24(void *dst, ptrdiff_t pitch, int buffer) {
  1159.     return Decode(dst, pitch, buffer, mpConverters->DecodeRGB24);
  1160. }
  1161.  
  1162. bool VDMPEGDecoder::DecodeRGB32(void *dst, ptrdiff_t pitch, int buffer) {
  1163.     return Decode(dst, pitch, buffer, mpConverters->DecodeRGB32);
  1164. }
  1165.  
  1166. ///////////////////////////////////////////////////////////////////////////
  1167. //
  1168. //    decoding (yeeeaaah!!!!)
  1169. //
  1170. ///////////////////////////////////////////////////////////////////////////
  1171.  
  1172. // Decoding should start at the first byte after the picture start code.
  1173. //
  1174. // +-------------------------------+
  1175. // |     temporal reference 2-9    | buf[0]
  1176. // +-------+-----------+-----------+
  1177. // | tr0-1 |   type    |delay 13-15| buf[1]
  1178. // +-------------------+-----------+
  1179. // |         VBV delay 5-12        | buf[2]
  1180. // +-------------------+---+-------+
  1181. // |     VBV delay 0-4 |FPF|FFC 1-2| buf[3] (full_pel_forward_vector, forward_f_code)
  1182. // +---+---+-----------+---+-------+
  1183. // |FFC|FPB|back f code|xxxxxxxxxxx| buf[4] (full_pel_backward_vector)
  1184. // +---+---+-----------+-----------+
  1185.  
  1186. int VDMPEGDecoder::DecodeFrame(const void *_src, long len, long frame, int dst, int fwd, int rev) {
  1187.     const unsigned char *src = (const unsigned char *)_src;
  1188.     const unsigned char *limit = src + len - 4;
  1189.     int type;
  1190.  
  1191.     if (mbQuantizersDirty) {
  1192.         UpdateQuantizers();
  1193.         mbQuantizersDirty = false;
  1194.     }
  1195.  
  1196.     // Extract type and check.
  1197.  
  1198.     type = (src[1]&0x38)>>3;
  1199.  
  1200.     switch(type) {
  1201.     case 1:        // I-frame
  1202.         mpSliceDecoder = &VDMPEGDecoder::DecodeSlice_I;
  1203.         break;
  1204.     case 2:        // P-frame
  1205.         mpSliceDecoder = &VDMPEGDecoder::DecodeSlice_P;
  1206.         break;
  1207.     case 3:        // B-frame
  1208.         mpSliceDecoder = &VDMPEGDecoder::DecodeSlice_B;
  1209.  
  1210.         // extract backward prediction info
  1211.  
  1212.         mbBackwardFullPel = (src[4]>>6)&1;
  1213.         mnBackwardRSize = ((src[4]>>3)&7)-1;
  1214.         mnBackwardMask = (32<<mnBackwardRSize)-1;
  1215.         mnBackwardSignExtend = ~((16<<mnBackwardRSize)-1);
  1216.  
  1217.         break;
  1218.     default:
  1219.         SetError(kError | kErrorBadFrameType);
  1220.         return -1;
  1221.     }
  1222.  
  1223.     // extract forward prediction info
  1224.  
  1225.     if ((type&0xfe)==2) {
  1226.         mbForwardFullPel = (src[3]>>2)&1;
  1227.         mnForwardRSize = (((src[3]&3)<<1) + (src[4]>>7))-1;
  1228.         mnForwardMask = (32<<mnForwardRSize)-1;
  1229.         mnForwardSignExtend = ~((16<<mnForwardRSize)-1);
  1230.     }
  1231.  
  1232.     mpY = mpBuffers[dst].pY;
  1233.     mpCr = mpBuffers[dst].pCr;
  1234.     mpCb = mpBuffers[dst].pCb;
  1235.  
  1236.     // Hook up referencing frames.
  1237.  
  1238.     if (fwd>=0) {
  1239.         mpFwdY    = mpBuffers[fwd].pY;
  1240.         mpFwdCr = mpBuffers[fwd].pCr;
  1241.         mpFwdCb = mpBuffers[fwd].pCb;
  1242.     } else {
  1243.         mpFwdY = mpFwdCr = mpFwdCb = NULL;
  1244.     }
  1245.     
  1246.     if (rev>=0) {
  1247.         mpBackY    = mpBuffers[rev].pY;
  1248.         mpBackCr = mpBuffers[rev].pCr;
  1249.         mpBackCb = mpBuffers[rev].pCb;
  1250.     } else {
  1251.         mpBackY = mpBackCr = mpBackCb = NULL;
  1252.     }
  1253.  
  1254.     // Wipe destination frame.
  1255.  
  1256. #ifdef _DEBUG
  1257.     for(uint32 y1=0; y1<mnBlockH*16; ++y1)
  1258.         memset(mpY + mnYPitch*y1, 0x80, mnBlockW*16);
  1259.     for(uint32 y2=0; y2<mnBlockH*8; ++y2)
  1260.         memset(mpCr + mnCPitch*y2, 0x80, mnBlockW*8);
  1261.     for(uint32 y3=0; y3<mnBlockH*8; ++y3)
  1262.         memset(mpCb + mnCPitch*y3, 0x80, mnBlockW*8);
  1263. #endif
  1264.  
  1265.     // Search for start codes beginning at buf+4 (I) or buf+5 (P/B).
  1266.  
  1267.     if (type == 1)
  1268.         src += 4;
  1269.     else
  1270.         src += 5;
  1271.  
  1272.     long error = mErrorState;
  1273.  
  1274. #ifdef _WIN32
  1275.     __try {
  1276. #endif
  1277.         while(src < limit) {
  1278.             if (*src) {
  1279.                 while(src<limit && *src)
  1280.                     ++src;
  1281.                 continue;
  1282.             } else if (!src[1] && src[2]==1 && src[3]>0 && src[3]<0xb0) {
  1283.                 src += 3;
  1284.  
  1285.                 bitheap_reset(src+1);
  1286.  
  1287.                 mnQuantValue = bitheap_getbitsconst(5);
  1288.  
  1289.                 mpCurrentIntraQ = mIntraQ[mnQuantValue];
  1290.                 mpCurrentNonintraQ = mNonintraQ[mnQuantValue];
  1291.  
  1292.                 while(bitheap_getflag())
  1293.                     bitheap_skipbitsconst(8);
  1294.  
  1295.                 mErrorState = 0;
  1296.  
  1297.                 (this->*mpSliceDecoder)(src[0]);
  1298.  
  1299.                 // Attempt slice resynchronization if an error occurred.
  1300.  
  1301.                 if (!mErrorState) {
  1302.                     bitheap_skipbits(bitcnt & 7);
  1303.  
  1304.                     src = bitsrc - ((24-bitcnt)>>3) - 2;
  1305.                 }
  1306.  
  1307.                 if (!error && mErrorState)
  1308.                     error = mErrorState;
  1309.  
  1310.             }
  1311.  
  1312.             ++src;
  1313.         }
  1314. #ifdef _WIN32
  1315.     } __except(_exception_code() == EXCEPTION_ACCESS_VIOLATION) {
  1316.         error |= kError | kErrorSourceOverrun;
  1317.     }
  1318. #endif
  1319.  
  1320.     mErrorState = error;
  1321.  
  1322.     // set frame
  1323.  
  1324.     mpBuffers[dst].frame = frame;
  1325.  
  1326.     return dst;
  1327. }
  1328.  
  1329. ///////////////////////////////////////////////////////////////////////////
  1330. //
  1331. //    Macroblock decoder
  1332. //
  1333. ///////////////////////////////////////////////////////////////////////////
  1334.  
  1335. void VDMPEGDecoder::DecodeBlockPrescaled(YCCSample *dst, long pitch, bool intra, int dc) {
  1336.     DecodeBlock(dst, pitch, intra, dc, true, (int)0);
  1337. }
  1338.  
  1339. void VDMPEGDecoder::DecodeBlockNonPrescaled(YCCSample *dst, long pitch, bool intra, int dc) {
  1340.     DecodeBlock(dst, pitch, intra, dc, false, (short)0);
  1341. }
  1342.  
  1343. void VDMPEGDecoder::DecodeBlock_Y(YCCSample *dst, bool intra) {
  1344.  
  1345.     // decode DCT size
  1346.  
  1347.     if (intra) {
  1348.         int dc_coeff = 0;
  1349.         unsigned int v = bitheap_peekbits(3);
  1350.         unsigned int size;
  1351.  
  1352. //        printf("%08lx\n", bitheap_peekbits(32));
  1353.  
  1354.         if (v < 7) {
  1355.             static const unsigned char dc_short_table[][2]={
  1356.                 { 1, 2 }, { 1, 2 },        // 00  -> 1
  1357.                 { 2, 2 }, { 2, 2 },        // 01  -> 2
  1358.                 { 0, 3 },                // 100 -> 0
  1359.                 { 3, 3 },                // 101 -> 3
  1360.                 { 4, 3 },                // 110 -> 4
  1361.             };
  1362.  
  1363.             size = dc_short_table[v][0];
  1364.             bitheap_skipbits(dc_short_table[v][1]);
  1365.         } else {
  1366.             bitheap_skipbits(3);
  1367.  
  1368.             size = 5;
  1369.  
  1370.             while(bitheap_getflag())
  1371.                 ++size;
  1372.         }
  1373.  
  1374. //        printf("DC size: %d\n", size);
  1375.  
  1376.         // read DC coefficient delta
  1377.  
  1378.         if (size) {
  1379.             long extend = -1L<<size;
  1380.  
  1381.             dc_coeff = bitheap_getbitssignedconst(size);
  1382.             dc_coeff ^= extend;
  1383.             dc_coeff -= (dc_coeff>>31);
  1384.  
  1385.             mnLastY_DC += dc_coeff;
  1386.  
  1387.             // dc must be in [0,255]
  1388.  
  1389.             if (mnLastY_DC & ~0xff) {
  1390.                 SetError(kError | kErrorBadValue);
  1391.  
  1392.                 if (mnLastY_DC < 0)
  1393.                     mnLastY_DC = 0;
  1394.                 else
  1395.                     mnLastY_DC = 255;
  1396.             }
  1397.         }
  1398.  
  1399.         (this->*mpBlockDecoder)(dst, mnYPitch, true, mnLastY_DC);
  1400.     } else {
  1401.         (this->*mpBlockDecoder)(dst, mnYPitch, false, 0);
  1402.     }
  1403. }
  1404.  
  1405. void VDMPEGDecoder::DecodeBlock_C(YCCSample *dst, int& dc, bool intra) {
  1406.  
  1407.     // decode DCT size
  1408.  
  1409.     if (intra) {
  1410.         unsigned int v = bitheap_getbitsconst(2);
  1411.         unsigned int size;
  1412.         int dc_coeff = 0;
  1413.  
  1414.         if (v < 3) {
  1415.             size = v;
  1416.         } else {
  1417.             size = 3;
  1418.  
  1419.             while(bitheap_getflag())
  1420.                 ++size;
  1421.         }
  1422.  
  1423.         // read DC coefficient delta
  1424.  
  1425.         if (size) {
  1426.             long extend = -1L<<size;
  1427.  
  1428.             dc_coeff = bitheap_getbitssignedconst(size);
  1429.             dc_coeff ^= extend;
  1430.             dc_coeff -= (dc_coeff>>31);
  1431.  
  1432.             dc += dc_coeff;
  1433.  
  1434.             // dc_coeff must be in [0,255]
  1435.  
  1436.             if (dc & ~0xff) {
  1437.                 SetError(kError | kErrorBadValue);
  1438.  
  1439.                 if (dc < 0)
  1440.                     dc = 0;
  1441.                 else
  1442.                     dc = 255;
  1443.             }
  1444.         }
  1445.  
  1446.         (this->*mpBlockDecoder)(dst, mnCPitch, true, dc);
  1447.     } else {
  1448.         (this->*mpBlockDecoder)(dst, mnCPitch, false, 0);
  1449.     }
  1450. }
  1451.  
  1452. ///////////////////////////////////////////////////////////////////////////
  1453. //
  1454. //    I-frame decoder
  1455. //
  1456. ///////////////////////////////////////////////////////////////////////////
  1457.  
  1458. static const unsigned char mb_short_lookup[14][2]={
  1459.     { 6, 4 },                                    // 000 10xx xxxx
  1460.     { 5, 4 },                                    // 000 11xx xxxx
  1461.     { 4, 3 }, { 4, 3 },                            // 001 0xxx xxxx
  1462.     { 3, 3 }, { 3, 3 },                            // 001 1xxx xxxx
  1463.     { 2, 2 }, { 2, 2 }, { 2, 2 }, { 2, 2 },        // 010 xxxx xxxx
  1464.     { 1, 2 }, { 1, 2 }, { 1, 2 }, { 1, 2 },        // 011 xxxx xxxx
  1465. };
  1466.  
  1467. void VDMPEGDecoder::DecodeSlice_I(int slice) {
  1468.     unsigned int pos_x = (unsigned int)-1;
  1469.     unsigned int pos_y = slice-1;
  1470.  
  1471.     mnLastY_DC = 128;
  1472.     mnLastCr_DC = 128;
  1473.     mnLastCb_DC = 128;
  1474.  
  1475.     do {
  1476.         int inc = 1;
  1477.         unsigned long v = bitheap_peekbits(11);
  1478.  
  1479.         // 000 0000 1111 is stuffing.
  1480.  
  1481.         while(v == 15)  {
  1482.             bitheap_skipbits(11);
  1483.             v = bitheap_peekbits(11);
  1484.         }
  1485.  
  1486.         // 000 0000 1000 is the escape.
  1487.  
  1488.         while(v == 8) {
  1489.             bitheap_skipbits(11);
  1490.             v = bitheap_peekbits(11);
  1491.             inc += 33;
  1492.         }
  1493.  
  1494.         // whatever is left must be the VLC for the macroblock increment
  1495.         //
  1496.         // 1xx xxxx xxxx -> 1
  1497.  
  1498.         bitheap_skipbits(1);
  1499.  
  1500.         if (v < 0x400) {
  1501.             if (v >= 0x80) {
  1502.                 inc += mb_short_lookup[(v>>6)-2][0];
  1503.                 bitheap_skipbits(mb_short_lookup[(v>>6)-2][1]);
  1504.             } else if (v >= 0x60) {            // 000 011? xxxx
  1505.                 inc += 8 - ((v>>4)&1);
  1506.                 bitheap_skipbits(6);
  1507.             } else if (v >= 0x30) {            // 000 0011 0000
  1508.                 inc += 20 - (v>>3);
  1509.                 bitheap_skipbits(7);
  1510.             } else if (v >= 0x24) {            // 000 0010 0100
  1511.                 inc += 38 - (v>>1);
  1512.                 bitheap_skipbits(9);
  1513.             } else {
  1514.                 inc += 56 - v;
  1515.                 bitheap_skipbits(10);
  1516.             }
  1517.         }
  1518.  
  1519.         // process macroblock skip
  1520.  
  1521.         pos_x += inc;
  1522.  
  1523.         if (pos_x >= mnBlockW) {
  1524.             pos_y += pos_x / mnBlockW;
  1525.             pos_x %= mnBlockW;
  1526.         }
  1527.  
  1528.         // no decoding out of bounds!!
  1529.  
  1530.         if (pos_y >= mnBlockH) {
  1531.             SetError(kError | kErrorTooManyMacroblocks);
  1532.             return;
  1533.         }
  1534.  
  1535.         // decode macroblock type:
  1536.         //
  1537.         //    1    keep quantizer
  1538.         //    01    change quantizer
  1539.  
  1540.         if (!bitheap_getflag()) {
  1541.             if (!bitheap_getflag()) {
  1542.                 SetError(kError | kErrorBadValue);
  1543.             }
  1544.  
  1545.             mnQuantValue = bitheap_getbitsconst(5);
  1546.  
  1547.             mpCurrentIntraQ = mIntraQ[mnQuantValue];
  1548.         }
  1549.  
  1550.         DecodeBlock_Y(mpY + mnYPitch8 * (2*pos_y+0) + 16 * pos_x, true);
  1551.         DecodeBlock_Y(mpY + mnYPitch8 * (2*pos_y+0) + 16 * pos_x + 8, true);
  1552.         DecodeBlock_Y(mpY + mnYPitch8 * (2*pos_y+1) + 16 * pos_x, true);
  1553.         DecodeBlock_Y(mpY + mnYPitch8 * (2*pos_y+1) + 16 * pos_x + 8, true);
  1554.         DecodeBlock_C(mpCb + mnCPitch8*pos_y + 8*pos_x, mnLastCb_DC, true);
  1555.         DecodeBlock_C(mpCr + mnCPitch8*pos_y + 8*pos_x, mnLastCr_DC, true);
  1556.  
  1557.     } while(!bitheap_checkzerobits(23));
  1558. }
  1559.  
  1560. ///////////////////////////////////////////////////////////////////////////
  1561. //
  1562. //    P/B-frame helpers
  1563. //
  1564. ///////////////////////////////////////////////////////////////////////////
  1565.  
  1566. #define REPEAT1(v,b) {v,b}
  1567. #define REPEAT2(v,b) {v,b},{v,b}
  1568. #define REPEAT4(v,b) {v,b},{v,b},{v,b},{v,b}
  1569. #define REPEAT8(v,b) {v,b},{v,b},{v,b},{v,b},{v,b},{v,b},{v,b},{v,b}
  1570. #define REPEAT16(v,b) REPEAT8(v,b),REPEAT8(v,b)
  1571.  
  1572. int VDMPEGDecoder::DecodeMotionVector(int rsize) {
  1573.     if (bitheap_getflag())
  1574.         return 0;
  1575.  
  1576.     unsigned int v = bitheap_peekbits(9);
  1577.  
  1578.     static const unsigned char mv_decode_short[7]={
  1579.         3,            // 0001
  1580.         2,2,        // 001
  1581.         1,1,1,1,    // 01
  1582.     };
  1583.  
  1584.     static const unsigned char mv_decode_long[52][2]={
  1585.         REPEAT1(16, 9),        // 0000 0011 00
  1586.         REPEAT1(15, 9),        // 0000 0011 01
  1587.         REPEAT1(14, 9),        // 0000 0011 10
  1588.         REPEAT1(13, 9),        // 0000 0011 11
  1589.         REPEAT1(12, 9),        // 0000 0100 00
  1590.         REPEAT1(11, 9),        // 0000 0100 01
  1591.         REPEAT2(10, 8),        // 0000 0100 1
  1592.         REPEAT2( 9, 8),        // 0000 0101 0
  1593.         REPEAT2( 8, 8),        // 0000 0101 1
  1594.         REPEAT8( 7, 6),        // 0000 011
  1595.         REPEAT8( 6, 6),        // 0000 100
  1596.         REPEAT8( 5, 6),        // 0000 101
  1597.         REPEAT16( 4, 5),    // 0000 11
  1598.     };
  1599.  
  1600.     if (v >= 64) {
  1601.         v = mv_decode_short[(v>>6)-1];
  1602.  
  1603.         bitheap_skipbits(v);
  1604.     } else {
  1605.         if (v < 12) {
  1606.             SetError(kError | kErrorBadValue);
  1607.             return 0;
  1608.         }
  1609.  
  1610.         bitheap_skipbits(mv_decode_long[v-12][1]);
  1611.         v = mv_decode_long[v-12][0];
  1612.     }
  1613.  
  1614.     int inverter = bitheap_getbitssignedconst(1);
  1615.  
  1616.     if (rsize)
  1617.         v = ((v-1)<<rsize) + bitheap_getbits(rsize) + 1;
  1618.  
  1619.     return (v ^ inverter) - inverter;
  1620. }
  1621.  
  1622. int VDMPEGDecoder::DecodeCodedBlockPattern() {
  1623.     static const unsigned char cbp_short[52][2]={
  1624.         REPEAT1(63,6),        // 001100
  1625.         REPEAT1( 3,6),        // 001101
  1626.         REPEAT1(36,6),        // 001110
  1627.         REPEAT1(24,6),        // 001111
  1628.         REPEAT2(62,5),        // 01000
  1629.         REPEAT2( 2,5),        // 01001
  1630.         REPEAT2(61,5),        // 01010
  1631.         REPEAT2( 1,5),        // 01011
  1632.         REPEAT2(56,5),        // 01100
  1633.         REPEAT2(52,5),        // 01101
  1634.         REPEAT2(44,5),        // 01110
  1635.         REPEAT2(28,5),        // 01111
  1636.         REPEAT2(40,5),        // 10000
  1637.         REPEAT2(20,5),        // 10001
  1638.         REPEAT2(48,5),        // 10010
  1639.         REPEAT2(12,5),        // 10011
  1640.         REPEAT4(32,4),        // 1010
  1641.         REPEAT4(16,4),        // 1011
  1642.         REPEAT4( 8,4),        // 1100
  1643.         REPEAT4( 4,4),        // 1101
  1644.         REPEAT8(60,3),        // 111
  1645.     };
  1646.  
  1647.     static const unsigned char cbp_long[94][2]={
  1648.         REPEAT1(39,9),        // 0000 0001 0
  1649.         REPEAT1(27,9),        // 0000 0001 1
  1650.         REPEAT1(59,9),        // 0000 0010 0
  1651.         REPEAT1(55,9),        // 0000 0010 1
  1652.         REPEAT1(47,9),        // 0000 0011 0
  1653.         REPEAT1(31,9),        // 0000 0011 1
  1654.         REPEAT2(58,8),        // 0000 0100
  1655.         REPEAT2(54,8),        // 0000 0101
  1656.         REPEAT2(46,8),        // 0000 0110
  1657.         REPEAT2(30,8),        // 0000 0111
  1658.         REPEAT2(57,8),        // 0000 1000
  1659.         REPEAT2(53,8),        // 0000 1001
  1660.         REPEAT2(45,8),        // 0000 1010
  1661.         REPEAT2(29,8),        // 0000 1011
  1662.         REPEAT2(38,8),        // 0000 1100
  1663.         REPEAT2(26,8),        // 0000 1101
  1664.         REPEAT2(37,8),        // 0000 1110
  1665.         REPEAT2(25,8),        // 0000 1111
  1666.         REPEAT2(43,8),        // 0001 0000
  1667.         REPEAT2(23,8),        // 0001 0001
  1668.         REPEAT2(51,8),        // 0001 0010
  1669.         REPEAT2(15,8),        // 0001 0011
  1670.         REPEAT2(42,8),        // 0001 0100
  1671.         REPEAT2(22,8),        // 0001 0101
  1672.         REPEAT2(50,8),        // 0001 0110
  1673.         REPEAT2(14,8),        // 0001 0111
  1674.         REPEAT2(41,8),        // 0001 1000
  1675.         REPEAT2(21,8),        // 0001 1001
  1676.         REPEAT2(49,8),        // 0001 1010
  1677.         REPEAT2(13,8),        // 0001 1011
  1678.         REPEAT2(35,8),        // 0001 1100
  1679.         REPEAT2(19,8),        // 0001 1101
  1680.         REPEAT2(11,8),        // 0001 1110
  1681.         REPEAT2( 7,8),        // 0001 1111
  1682.         REPEAT4(34,7),        // 0010 000
  1683.         REPEAT4(18,7),        // 0010 001
  1684.         REPEAT4(10,7),        // 0010 010
  1685.         REPEAT4( 6,7),        // 0010 011
  1686.         REPEAT4(33,7),        // 0010 100
  1687.         REPEAT4(17,7),        // 0010 101
  1688.         REPEAT4( 9,7),        // 0010 110
  1689.         REPEAT4( 5,7),        // 0010 111
  1690.     };
  1691.  
  1692.     unsigned long v = bitheap_peekbits(9);
  1693.  
  1694.     if (v < 96) {
  1695.         if (v < 2) {
  1696.             SetError(kError | kErrorBadValue);
  1697.             return 0;
  1698.         }
  1699.  
  1700.         bitheap_skipbits(cbp_long[v-2][1]);
  1701.         return cbp_long[v-2][0];
  1702.     } else {
  1703.         v>>=3;
  1704.  
  1705.         bitheap_skipbits(cbp_short[v-12][1]);
  1706.         return cbp_short[v-12][0];
  1707.     }
  1708. }
  1709.  
  1710. ///////////////////////////////////////////////////////////////////////////
  1711. //
  1712. //    P-frame decoder
  1713. //
  1714. ///////////////////////////////////////////////////////////////////////////
  1715.  
  1716. void VDMPEGDecoder::DecodeSlice_P(int slice) {
  1717.     unsigned int pos_x = (unsigned int)-1;
  1718.     unsigned int pos_y = slice-1;
  1719.     int forw_x = 0, forw_y = 0;
  1720.  
  1721.     mnLastY_DC = 128;
  1722.     mnLastCr_DC = 128;
  1723.     mnLastCb_DC = 128;
  1724.  
  1725.     do {
  1726.         int inc = 1;
  1727.         unsigned long v = bitheap_peekbits(11);
  1728.  
  1729.         // 000 0000 1111 is stuffing.
  1730.  
  1731.         while(v == 15)  {
  1732.             bitheap_skipbits(11);
  1733.             v = bitheap_peekbits(11);
  1734.         }
  1735.  
  1736.         // 000 0000 1000 is the escape.
  1737.  
  1738.         while(v == 8) {
  1739.             bitheap_skipbits(11);
  1740.             v = bitheap_peekbits(11);
  1741.             inc += 33;
  1742.         }
  1743.  
  1744.         // whatever is left must be the VLC for the macroblock increment
  1745.         //
  1746.         // 1xx xxxx xxxx -> 1
  1747.  
  1748.         bitheap_skipbits(1);
  1749.  
  1750.         if (v < 0x400) {
  1751.             if (v >= 0x80) {
  1752.                 inc += mb_short_lookup[(v>>6)-2][0];
  1753.                 bitheap_skipbits(mb_short_lookup[(v>>6)-2][1]);
  1754.             } else if (v >= 0x60) {            // 000 011? xxxx
  1755.                 inc += 8 - ((v>>4)&1);
  1756.                 bitheap_skipbits(6);
  1757.             } else if (v >= 0x30) {            // 000 0011 0000
  1758.                 inc += 20 - (v>>3);
  1759.                 bitheap_skipbits(7);
  1760.             } else if (v >= 0x24) {            // 000 0010 0100
  1761.                 inc += 38 - (v>>1);
  1762.                 bitheap_skipbits(9);
  1763.             } else {
  1764.                 inc += 56 - v;
  1765.                 bitheap_skipbits(10);
  1766.             }
  1767.         }
  1768.  
  1769.         // process macroblock skip
  1770.  
  1771.         if (pos_x == (unsigned int)-1) {
  1772.             pos_x += inc;
  1773.  
  1774.             if (pos_x >= mnBlockW) {
  1775.                 pos_y += pos_x / mnBlockW;
  1776.                 pos_x %= mnBlockW;
  1777.             }
  1778.         } else {
  1779.             if (inc > 1) {
  1780.                 mnLastY_DC = 128;
  1781.                 mnLastCr_DC = 128;
  1782.                 mnLastCb_DC = 128;
  1783.                 forw_x = forw_y = 0;
  1784.  
  1785.                 while(--inc>0) {
  1786.                     if (++pos_x >= mnBlockW) {
  1787.                         pos_x = 0;
  1788.                         if (++pos_y >= mnBlockH) {
  1789.                             SetError(kError | kErrorTooManyMacroblocks);
  1790.                             return;
  1791.                         }
  1792.                     }
  1793.  
  1794.                     CopyPredictionForward(pos_x, pos_y, 0,0);
  1795.                 }
  1796.             }
  1797.  
  1798.             if (++pos_x >= mnBlockW) {
  1799.                 pos_x = 0;
  1800.                 if (++pos_y >= mnBlockH) {
  1801.                     SetError(kError | kErrorTooManyMacroblocks);
  1802.                     return;
  1803.                 }
  1804.             }
  1805.  
  1806.         }
  1807.  
  1808.         // no decoding out of bounds!!
  1809.  
  1810.         if (pos_y >= mnBlockH) {
  1811.             SetError(kError | kErrorTooManyMacroblocks);
  1812.             return;
  1813.         }
  1814.  
  1815. //        printf("Decoding (%d,%d): %08lx\n", pos_x, pos_y, bitheap_peekbits(32));
  1816.  
  1817.         // decode macroblock type
  1818.  
  1819.         enum {
  1820.             kMBF_NewQuant    = 8,
  1821.             kMBF_Forward    = 4,
  1822.             kMBF_Pattern    = 2,
  1823.             kMBF_Intra        = 1
  1824.         };
  1825.  
  1826.         static const unsigned char mb_type_P[32][2]={
  1827.             REPEAT1(0,0),
  1828.             REPEAT1(kMBF_NewQuant+kMBF_Intra, 5),
  1829.             REPEAT2(kMBF_NewQuant+kMBF_Pattern, 4),
  1830.             REPEAT2(kMBF_NewQuant+kMBF_Forward+kMBF_Pattern, 4),
  1831.             REPEAT2(kMBF_Intra, 4),
  1832.             REPEAT8(kMBF_Forward, 2),
  1833.             REPEAT16(kMBF_Pattern, 1),
  1834.         };
  1835.  
  1836.         int mb_flags;
  1837.  
  1838.         if (bitheap_getflag()) {
  1839.             mb_flags = kMBF_Forward + kMBF_Pattern;
  1840.         } else {
  1841.             v = bitheap_peekbits(5);
  1842.  
  1843.             mb_flags = mb_type_P[v][0];
  1844.             bitheap_skipbits(mb_type_P[v][1]);
  1845.         }
  1846.  
  1847.         // read in quantizer
  1848.  
  1849.         if (mb_flags & kMBF_NewQuant) {
  1850.             mnQuantValue = bitheap_getbitsconst(5);
  1851.  
  1852.             mpCurrentIntraQ = mIntraQ[mnQuantValue];
  1853.             mpCurrentNonintraQ = mNonintraQ[mnQuantValue];
  1854.         }
  1855.  
  1856.         // read in motion vector and predict
  1857.  
  1858.         if (mb_flags & kMBF_Forward) {
  1859.             int recon_x = DecodeMotionVector(mnForwardRSize);
  1860.             int recon_y = DecodeMotionVector(mnForwardRSize);
  1861.  
  1862.             forw_x += recon_x;
  1863.             forw_y += recon_y;
  1864.  
  1865.             forw_x = ((forw_x & mnForwardMask) + mnForwardSignExtend) ^ mnForwardSignExtend;
  1866.             forw_y = ((forw_y & mnForwardMask) + mnForwardSignExtend) ^ mnForwardSignExtend;
  1867.  
  1868.             CopyPredictionForward(pos_x, pos_y, forw_x, forw_y);
  1869.         } else {
  1870.             forw_x = forw_y = 0;
  1871.  
  1872.             if (!(mb_flags & kMBF_Intra))
  1873.                 CopyPredictionForward(pos_x, pos_y, 0, 0);
  1874.         }
  1875.  
  1876.         // read in coded block pattern
  1877.  
  1878.         int cbp = (mb_flags & kMBF_Intra) ? 63 : 0;
  1879.  
  1880.         if (mb_flags & kMBF_Pattern) {
  1881.             cbp = DecodeCodedBlockPattern();
  1882.         }
  1883.  
  1884.         // decode blocks
  1885.  
  1886.         bool intraflag = (mb_flags & kMBF_Intra);
  1887.  
  1888.         if (!intraflag) {
  1889.             mnLastY_DC = 128;
  1890.             mnLastCr_DC = 128;
  1891.             mnLastCb_DC = 128;
  1892.         }
  1893.  
  1894.         if (cbp&32) DecodeBlock_Y(mpY + mnYPitch8 * (2*pos_y+0) + 16 * pos_x, intraflag);
  1895.         if (cbp&16) DecodeBlock_Y(mpY + mnYPitch8 * (2*pos_y+0) + 16 * pos_x + 8, intraflag);
  1896.         if (cbp& 8) DecodeBlock_Y(mpY + mnYPitch8 * (2*pos_y+1) + 16 * pos_x, intraflag);
  1897.         if (cbp& 4) DecodeBlock_Y(mpY + mnYPitch8 * (2*pos_y+1) + 16 * pos_x + 8, intraflag);
  1898.         if (cbp& 2) DecodeBlock_C(mpCb + mnCPitch8*pos_y + 8*pos_x, mnLastCb_DC, intraflag);
  1899.         if (cbp& 1) DecodeBlock_C(mpCr + mnCPitch8*pos_y + 8*pos_x, mnLastCr_DC, intraflag);
  1900.  
  1901.     } while(!bitheap_checkzerobits(23));
  1902. }
  1903.  
  1904. ///////////////////////////////////////////////////////////////////////////
  1905. //
  1906. //    B-frame decoder
  1907. //
  1908. ///////////////////////////////////////////////////////////////////////////
  1909.  
  1910. void VDMPEGDecoder::DecodeSlice_B(int slice) {
  1911.     unsigned int pos_x = (unsigned int)-1;
  1912.     unsigned int pos_y = slice-1;
  1913.     int forw_x = 0, forw_y = 0;
  1914.     int back_x = 0, back_y = 0;
  1915.     int mb_flags = 0;
  1916.  
  1917.     mnLastY_DC = 128;
  1918.     mnLastCr_DC = 128;
  1919.     mnLastCb_DC = 128;
  1920.  
  1921.     do {
  1922.         int inc = 1;
  1923.         unsigned long v = bitheap_peekbits(11);
  1924.  
  1925.         // 000 0000 1111 is stuffing.
  1926.  
  1927.         while(v == 15)  {
  1928.             bitheap_skipbits(11);
  1929.             v = bitheap_peekbits(11);
  1930.         }
  1931.  
  1932.         // 000 0000 1000 is the escape.
  1933.  
  1934.         while(v == 8) {
  1935.             bitheap_skipbits(11);
  1936.             v = bitheap_peekbits(11);
  1937.             inc += 33;
  1938.         }
  1939.  
  1940.         // whatever is left must be the VLC for the macroblock increment
  1941.         //
  1942.         // 1xx xxxx xxxx -> 1
  1943.  
  1944.         bitheap_skipbits(1);
  1945.  
  1946.         if (v < 0x400) {
  1947.             if (v >= 0x80) {
  1948.                 inc += mb_short_lookup[(v>>6)-2][0];
  1949.                 bitheap_skipbits(mb_short_lookup[(v>>6)-2][1]);
  1950.             } else if (v >= 0x60) {            // 000 011? xxxx
  1951.                 inc += 8 - ((v>>4)&1);
  1952.                 bitheap_skipbits(6);
  1953.             } else if (v >= 0x30) {            // 000 0011 0000
  1954.                 inc += 20 - (v>>3);
  1955.                 bitheap_skipbits(7);
  1956.             } else if (v >= 0x24) {            // 000 0010 0100
  1957.                 inc += 38 - (v>>1);
  1958.                 bitheap_skipbits(9);
  1959.             } else {
  1960.                 inc += 56 - v;
  1961.                 bitheap_skipbits(10);
  1962.             }
  1963.         }
  1964.  
  1965.         // process macroblock skip
  1966.  
  1967.         if (pos_x == (unsigned int)-1) {
  1968.             pos_x += inc;
  1969.  
  1970.             if (pos_x >= mnBlockW) {
  1971.                 pos_y += pos_x / mnBlockW;
  1972.                 pos_x %= mnBlockW;
  1973.             }
  1974.         } else {
  1975.             if (inc > 1) {
  1976.                 mnLastY_DC = 128;
  1977.                 mnLastCr_DC = 128;
  1978.                 mnLastCb_DC = 128;
  1979.  
  1980.                 // We will have neither the forward nor backward flags set whenever
  1981.                 // we encounter a first-block skip -- in this case we shouldn't draw
  1982.                 // anything.
  1983.  
  1984.                 if (!(mb_flags & (kMBF_Forward | kMBF_Backward))) {
  1985.                     // Non-initial skip after intra block is illegal.  In this case,
  1986.                     // set the error flag, then reset motion vectors to a relatively
  1987.                     // safe configuration.
  1988.  
  1989.                     SetError(kError);
  1990.                     mb_flags = kMBF_Forward | kMBF_Backward;
  1991.                     forw_x = forw_y = back_x = back_y = 0;
  1992.                 }
  1993.  
  1994.                 while(--inc>0) {
  1995.                     if (++pos_x >= mnBlockW) {
  1996.                         pos_x = 0;
  1997.                         if (++pos_y >= mnBlockH) {
  1998.                             SetError(kError | kErrorTooManyMacroblocks);
  1999.                             return;
  2000.                         }
  2001.                     }
  2002.  
  2003.                     if (mb_flags & kMBF_Forward) {
  2004.                         CopyPredictionForward(pos_x, pos_y, forw_x, forw_y);
  2005.  
  2006.                         if (mb_flags & kMBF_Backward)
  2007.                             AddPredictionBackward(pos_x, pos_y, back_x, back_y);
  2008.                     } else
  2009.                         CopyPredictionBackward(pos_x, pos_y, back_x, back_y);
  2010.                 }
  2011.             }
  2012.  
  2013.             if (++pos_x >= mnBlockW) {
  2014.                 pos_x = 0;
  2015.                 if (++pos_y >= mnBlockH) {
  2016.                     SetError(kError | kErrorTooManyMacroblocks);
  2017.                     return;
  2018.                 }
  2019.             }
  2020.  
  2021.         }
  2022.  
  2023.         // no decoding out of bounds!!
  2024.  
  2025.         if (pos_y >= mnBlockH) {
  2026.             SetError(kError | kErrorTooManyMacroblocks);
  2027.             return;
  2028.         }
  2029.  
  2030. //        printf("Decoding (%d,%d): %08lx\n", pos_x, pos_y, bitheap_peekbits(32));
  2031.  
  2032.         // decode macroblock type
  2033.  
  2034.         static const unsigned char mb_type_B[64][2]={
  2035.             REPEAT1(0,0),                                                // umm...
  2036.             REPEAT1(kMBF_NewQuant + kMBF_Intra, 6),                        // 000001
  2037.             REPEAT1(kMBF_NewQuant + kMBF_Backward + kMBF_Pattern, 6),    // 000010
  2038.             REPEAT1(kMBF_NewQuant + kMBF_Forward + kMBF_Pattern, 6),    // 000011
  2039.             REPEAT2(kMBF_NewQuant + kMBF_Both + kMBF_Pattern, 5),        // 00010
  2040.             REPEAT2(kMBF_Intra, 5),                                        // 00011
  2041.             REPEAT4(kMBF_Forward, 4),                                    // 0010
  2042.             REPEAT4(kMBF_Forward + kMBF_Pattern, 4),                    // 0011
  2043.             REPEAT8(kMBF_Backward, 3),                                    // 010
  2044.             REPEAT8(kMBF_Backward + kMBF_Pattern, 3),                    // 011
  2045.             REPEAT16(kMBF_Forward + kMBF_Backward, 2),                    // 10
  2046.             REPEAT16(kMBF_Forward + kMBF_Backward + kMBF_Pattern, 2),    // 11
  2047.         };
  2048.  
  2049.         v = bitheap_peekbits(6);
  2050.  
  2051.         mb_flags = mb_type_B[v][0];
  2052.         bitheap_skipbits(mb_type_B[v][1]);
  2053.  
  2054.         // read in quantizer
  2055.  
  2056.         if (mb_flags & kMBF_NewQuant) {
  2057.             mnQuantValue = bitheap_getbitsconst(5);
  2058.  
  2059.             mpCurrentIntraQ = mIntraQ[mnQuantValue];
  2060.             mpCurrentNonintraQ = mNonintraQ[mnQuantValue];
  2061.         }
  2062.  
  2063.         // read in motion vector and predict
  2064.  
  2065.         if (mb_flags & kMBF_Forward) {
  2066.             int recon_x = DecodeMotionVector(mnForwardRSize);
  2067.             int recon_y = DecodeMotionVector(mnForwardRSize);
  2068.  
  2069.             forw_x += recon_x;
  2070.             forw_y += recon_y;
  2071.  
  2072.             forw_x = ((forw_x & mnForwardMask) + mnForwardSignExtend) ^ mnForwardSignExtend;
  2073.             forw_y = ((forw_y & mnForwardMask) + mnForwardSignExtend) ^ mnForwardSignExtend;
  2074.  
  2075. //            printf("at(%2d,%2d) forward vector %+3d,%+3d\n", pos_x, pos_y, forw_x, forw_y);
  2076.  
  2077.             CopyPredictionForward(pos_x, pos_y, forw_x, forw_y);
  2078.         }
  2079.  
  2080.         if (mb_flags & kMBF_Backward) {
  2081.             int recon_x = DecodeMotionVector(mnBackwardRSize);
  2082.             int recon_y = DecodeMotionVector(mnBackwardRSize);
  2083.  
  2084.             back_x += recon_x;
  2085.             back_y += recon_y;
  2086.  
  2087.             back_x = ((back_x & mnBackwardMask) + mnBackwardSignExtend) ^ mnBackwardSignExtend;
  2088.             back_y = ((back_y & mnBackwardMask) + mnBackwardSignExtend) ^ mnBackwardSignExtend;
  2089.  
  2090. //            printf("at(%2d,%2d) backward vector %+3d,%+3d\n", pos_x, pos_y, back_x, back_y);
  2091.  
  2092.             if (mb_flags & kMBF_Forward)
  2093.                 AddPredictionBackward(pos_x, pos_y, back_x, back_y);
  2094.             else
  2095.                 CopyPredictionBackward(pos_x, pos_y, back_x, back_y);
  2096.         }
  2097.  
  2098.         // read in coded block pattern
  2099.  
  2100.         int cbp = 0;
  2101.  
  2102.         if (mb_flags & kMBF_Intra) {
  2103.             cbp = 63;
  2104.             forw_x = forw_y = back_x = back_y = 0;
  2105.         } else {
  2106.             mnLastY_DC = 128;
  2107.             mnLastCr_DC = 128;
  2108.             mnLastCb_DC = 128;
  2109.         }
  2110.  
  2111.         if (mb_flags & kMBF_Pattern) {
  2112.             cbp = DecodeCodedBlockPattern();
  2113.         }
  2114.  
  2115.         // decode blocks
  2116.  
  2117.         bool intraflag = (mb_flags & kMBF_Intra);
  2118.  
  2119.         if (cbp&32) DecodeBlock_Y(mpY + mnYPitch8 * (2*pos_y+0) + 16 * pos_x, intraflag);
  2120.         if (cbp&16) DecodeBlock_Y(mpY + mnYPitch8 * (2*pos_y+0) + 16 * pos_x + 8, intraflag);
  2121.         if (cbp& 8) DecodeBlock_Y(mpY + mnYPitch8 * (2*pos_y+1) + 16 * pos_x, intraflag);
  2122.         if (cbp& 4) DecodeBlock_Y(mpY + mnYPitch8 * (2*pos_y+1) + 16 * pos_x + 8, intraflag);
  2123.         if (cbp& 2) DecodeBlock_C(mpCb + mnCPitch8*pos_y + 8*pos_x, mnLastCb_DC, intraflag);
  2124.         if (cbp& 1) DecodeBlock_C(mpCr + mnCPitch8*pos_y + 8*pos_x, mnLastCr_DC, intraflag);
  2125.  
  2126.     } while(!bitheap_checkzerobits(23));
  2127. }
  2128.  
  2129. ///////////////////////////////////////////////////////////////////////////
  2130. //
  2131. //    prediction
  2132. //
  2133. ///////////////////////////////////////////////////////////////////////////
  2134.  
  2135. void VDMPEGDecoder::CopyPredictionForward(int posx, int posy, int dx, int dy) {
  2136.     if (mbForwardFullPel) {
  2137.         dx *= 2;
  2138.         dy *= 2;
  2139.     }
  2140.  
  2141.     int dxY        = posx*32 + dx;
  2142.     int dyY        = posy*32 + dy;
  2143.     int dxC        = posx*16 + dx/2;
  2144.     int dyC        = posy*16 + dy/2;
  2145.  
  2146.     // check vectors
  2147.  
  2148.     if ((unsigned)dxY > (unsigned)(mnBlockW*32-32) || (unsigned)dyY > (unsigned)(mnBlockH*32-32)) {
  2149.         dxY = posx*32;
  2150.         dyY = posy*32;
  2151.         dxC = posx*16;
  2152.         dyC = posy*16;
  2153.         SetError(kError | kErrorBadMotionVector);
  2154.     }
  2155.  
  2156.     (mpPredictors->Y_predictors[dyY&1][dxY&1])(mpY + posy*mnYPitch8*2 + posx*16, mpFwdY + mnYPitch*(dyY>>1) + (dxY>>1), mnYPitch);
  2157.     (mpPredictors->C_predictors[dyC&1][dxC&1])(mpCr + posy*mnCPitch8 + posx*8, mpFwdCr + mnCPitch*(dyC>>1) + (dxC>>1), mnCPitch);
  2158.     (mpPredictors->C_predictors[dyC&1][dxC&1])(mpCb + posy*mnCPitch8 + posx*8, mpFwdCb + mnCPitch*(dyC>>1) + (dxC>>1), mnCPitch);
  2159.  
  2160. //    __asm emms
  2161. }
  2162.  
  2163. void VDMPEGDecoder::CopyPredictionBackward(int posx, int posy, int dx, int dy) {
  2164.     if (mbBackwardFullPel) {
  2165.         dx *= 2;
  2166.         dy *= 2;
  2167.     }
  2168.  
  2169.     int dxY        = posx*32 + dx;
  2170.     int dyY        = posy*32 + dy;
  2171.     int dxC        = posx*16 + dx/2;
  2172.     int dyC        = posy*16 + dy/2;
  2173.  
  2174.     // check vectors
  2175.  
  2176.     if ((unsigned)dxY > (unsigned)(mnBlockW*32-32) || (unsigned)dyY > (unsigned)(mnBlockH*32-32)) {
  2177.         dxY = posx*32;
  2178.         dyY = posy*32;
  2179.         dxC = posx*16;
  2180.         dyC = posy*16;
  2181.         SetError(kError | kErrorBadMotionVector);
  2182.     }
  2183.  
  2184.     (mpPredictors->Y_predictors[dyY&1][dxY&1])(mpY + posy*mnYPitch8*2 + posx*16, mpBackY + mnYPitch*(dyY>>1) + (dxY>>1), mnYPitch);
  2185.     (mpPredictors->C_predictors[dyC&1][dxC&1])(mpCr + posy*mnCPitch8 + posx*8, mpBackCr + mnCPitch*(dyC>>1) + (dxC>>1), mnCPitch);
  2186.     (mpPredictors->C_predictors[dyC&1][dxC&1])(mpCb + posy*mnCPitch8 + posx*8, mpBackCb + mnCPitch*(dyC>>1) + (dxC>>1), mnCPitch);
  2187.  
  2188. //    __asm emms
  2189. }
  2190.  
  2191. void VDMPEGDecoder::AddPredictionBackward(int posx, int posy, int dx, int dy) {
  2192.     if (mbBackwardFullPel) {
  2193.         dx *= 2;
  2194.         dy *= 2;
  2195.     }
  2196.  
  2197.     int dxY        = posx*32 + dx;
  2198.     int dyY        = posy*32 + dy;
  2199.     int dxC        = posx*16 + dx/2;
  2200.     int dyC        = posy*16 + dy/2;
  2201.  
  2202.     // check vectors
  2203.  
  2204.     if ((unsigned)dxY > (unsigned)(mnBlockW*32-32) || (unsigned)dyY > (unsigned)(mnBlockH*32-32)) {
  2205.         dxY = posx*32;
  2206.         dyY = posy*32;
  2207.         dxC = posx*16;
  2208.         dyC = posy*16;
  2209.         SetError(kError | kErrorBadMotionVector);
  2210.     }
  2211.  
  2212.     (mpPredictors->Y_adders[dyY&1][dxY&1])(mpY + posy*mnYPitch8*2 + posx*16, mpBackY + mnYPitch*(dyY>>1) + (dxY>>1), mnYPitch);
  2213.     (mpPredictors->C_adders[dyC&1][dxC&1])(mpCr + posy*mnCPitch8 + posx*8, mpBackCr + mnCPitch*(dyC>>1) + (dxC>>1), mnCPitch);
  2214.     (mpPredictors->C_adders[dyC&1][dxC&1])(mpCb + posy*mnCPitch8 + posx*8, mpBackCb + mnCPitch*(dyC>>1) + (dxC>>1), mnCPitch);
  2215.  
  2216. //    __asm emms
  2217. }
  2218.