MPEG2Dec.cpp

00001 #include "stdafx.h"
00002 #include "MPEG2Dec.h"
00003 
00004 int testint;
00005 
00006 struct CPU {
00007         BOOL                                    mmx;
00008         BOOL                                    _3dnow;
00009         BOOL                                    ssemmx;
00010         BOOL                                    ssefpu;
00011 } cpu;
00012 
00013 void CheckCPU()
00014 {
00015         __asm
00016         {
00017                 mov                     eax, 1
00018                 cpuid
00019                 test            edx, 0x00800000         // STD MMX
00020                 jz                      TEST_SSE
00021                 mov                     [cpu.mmx], 1
00022 TEST_SSE:
00023                 test            edx, 0x02000000         // STD SSE
00024                 jz                      TEST_3DNOW
00025                 mov                     [cpu.ssemmx], 1
00026                 mov                     [cpu.ssefpu], 1
00027 TEST_3DNOW:
00028                 mov                     eax, 0x80000001
00029                 cpuid
00030                 test            edx, 0x80000000         // 3D NOW
00031                 jz                      TEST_SSEMMX
00032                 mov                     [cpu._3dnow], 1
00033 TEST_SSEMMX:
00034                 test            edx, 0x00400000         // SSE MMX
00035                 jz                      TEST_END
00036                 mov                     [cpu.ssemmx], 1
00037 TEST_END:
00038         }
00039 }
00040 
00041 
00042 // idct
00043 extern "C" void __fastcall MMX_IDCT(short *block);
00044 extern "C" void __fastcall SSEMMX_IDCT(short *block);
00045 extern void Initialize_FPU_IDCT(void);
00046 extern void FPU_IDCT(short *block);
00047 extern void Initialize_REF_IDCT(void);
00048 extern void REF_IDCT(short *block);
00049 
00050 /* default intra quantization matrix */
00051 static unsigned char default_intra_quantizer_matrix[64] =
00052 {
00053         8, 16, 19, 22, 26, 27, 29, 34,
00054         16, 16, 22, 24, 27, 29, 34, 37,
00055         19, 22, 26, 27, 29, 34, 34, 38,
00056         22, 22, 26, 27, 29, 34, 37, 40,
00057         22, 26, 27, 29, 32, 35, 40, 48,
00058         26, 27, 29, 32, 35, 40, 48, 58,
00059         26, 27, 29, 34, 38, 46, 56, 69,
00060         27, 29, 35, 38, 46, 56, 69, 83
00061 };
00062 
00063 /* zig-zag and alternate scan patterns */
00064 static unsigned char scan[2][64] =
00065 {
00066         { /* Zig-Zag scan pattern  */
00067                 0,  1,  8, 16,  9,  2,  3, 10,
00068            17, 24, 32, 25, 18, 11,  4,  5,
00069            12, 19, 26, 33, 40, 48, 41, 34,
00070            27, 20, 13,  6,  7, 14, 21, 28,
00071            35, 42, 49, 56, 57, 50, 43, 36,
00072            29, 22, 15, 23, 30, 37, 44, 51,
00073            58, 59, 52, 45, 38, 31, 39, 46,
00074            53, 60, 61, 54, 47, 55, 62, 63
00075         }
00076         ,
00077         { /* Alternate scan pattern */
00078                 0,  8, 16, 24,  1,  9,  2, 10,
00079            17, 25, 32, 40, 48, 56, 57, 49,
00080            41, 33, 26, 18,  3, 11, 4,  12,
00081            19, 27, 34, 42, 50, 58, 35, 43,
00082            51, 59, 20, 28,  5, 13,  6, 14,
00083            21, 29, 36, 44, 52, 60, 37, 45,
00084            53, 61, 22, 30,  7, 15, 23, 31,
00085            38, 46, 54, 62, 39, 47, 55, 63
00086         }
00087 };
00088 
00089 /* non-linear quantization coefficient table */
00090 static unsigned char Non_Linear_quantizer_scale[32] =
00091 {
00092         0, 1, 2, 3, 4, 5, 6, 7,
00093         8, 10, 12, 14, 16, 18, 20, 22,
00094         24, 28, 32, 36, 40, 44, 48, 52,
00095         56, 64, 72, 80, 88, 96, 104, 112
00096 };
00097 
00098 #define ERROR_VALUE     (-1)
00099 
00100 typedef struct {
00101         char run, level, len;
00102 } DCTtab;
00103 
00104 typedef struct {
00105         char val, len;
00106 } VLCtab;
00107 
00108 /* Table B-10, motion_code, codes 0001 ... 01xx */
00109 static VLCtab MVtab0[8] =
00110 {
00111         {ERROR_VALUE,0}, {3,3}, {2,2}, {2,2}, {1,1}, {1,1}, {1,1}, {1,1}
00112 };
00113 
00114 /* Table B-10, motion_code, codes 0000011 ... 000011x */
00115 static VLCtab MVtab1[8] =
00116 {
00117         {ERROR_VALUE,0}, {ERROR_VALUE,0}, {ERROR_VALUE,0}, {7,6}, {6,6}, {5,6}, {4,5}, {4,5}
00118 };
00119 
00120 /* Table B-10, motion_code, codes 0000001100 ... 000001011x */
00121 static VLCtab MVtab2[12] =
00122 {
00123         {16,9}, {15,9}, {14,9}, {13,9},
00124         {12,9}, {11,9}, {10,8}, {10,8},
00125         {9,8},  {9,8},  {8,8},  {8,8}
00126 };
00127 
00128 /* Table B-9, coded_block_pattern, codes 01000 ... 111xx */
00129 static VLCtab CBPtab0[32] =
00130 {
00131         {ERROR_VALUE,0}, {ERROR_VALUE,0}, {ERROR_VALUE,0}, {ERROR_VALUE,0},
00132         {ERROR_VALUE,0}, {ERROR_VALUE,0}, {ERROR_VALUE,0}, {ERROR_VALUE,0},
00133         {62,5}, {2,5},  {61,5}, {1,5},  {56,5}, {52,5}, {44,5}, {28,5},
00134         {40,5}, {20,5}, {48,5}, {12,5}, {32,4}, {32,4}, {16,4}, {16,4},
00135         {8,4},  {8,4},  {4,4},  {4,4},  {60,3}, {60,3}, {60,3}, {60,3}
00136 };
00137 
00138 /* Table B-9, coded_block_pattern, codes 00000100 ... 001111xx */
00139 static VLCtab CBPtab1[64] =
00140 {
00141         {ERROR_VALUE,0}, {ERROR_VALUE,0}, {ERROR_VALUE,0}, {ERROR_VALUE,0},
00142         {58,8}, {54,8}, {46,8}, {30,8},
00143         {57,8}, {53,8}, {45,8}, {29,8}, {38,8}, {26,8}, {37,8}, {25,8},
00144         {43,8}, {23,8}, {51,8}, {15,8}, {42,8}, {22,8}, {50,8}, {14,8},
00145         {41,8}, {21,8}, {49,8}, {13,8}, {35,8}, {19,8}, {11,8}, {7,8},
00146         {34,7}, {34,7}, {18,7}, {18,7}, {10,7}, {10,7}, {6,7},  {6,7},
00147         {33,7}, {33,7}, {17,7}, {17,7}, {9,7},  {9,7},  {5,7},  {5,7},
00148         {63,6}, {63,6}, {63,6}, {63,6}, {3,6},  {3,6},  {3,6},  {3,6},
00149         {36,6}, {36,6}, {36,6}, {36,6}, {24,6}, {24,6}, {24,6}, {24,6}
00150 };
00151 
00152 /* Table B-9, coded_block_pattern, codes 000000001 ... 000000111 */
00153 static VLCtab CBPtab2[8] =
00154 {
00155         {ERROR_VALUE,0}, {0,9}, {39,9}, {27,9}, {59,9}, {55,9}, {47,9}, {31,9}
00156 };
00157 
00158 /* Table B-1, macroblock_address_increment, codes 00010 ... 011xx */
00159 static VLCtab MBAtab1[16] =
00160 {
00161         {ERROR_VALUE,0}, {ERROR_VALUE,0}, {7,5}, {6,5}, {5,4}, {5,4}, {4,4},
00162         {4,4}, {3,3}, {3,3}, {3,3}, {3,3}, {2,3}, {2,3}, {2,3}, {2,3}
00163 };
00164 
00165 /* Table B-1, macroblock_address_increment, codes 00000011000 ... 0000111xxxx */
00166 static VLCtab MBAtab2[104] =
00167 {
00168         {33,11}, {32,11}, {31,11}, {30,11}, {29,11}, {28,11}, {27,11}, {26,11},
00169         {25,11}, {24,11}, {23,11}, {22,11}, {21,10}, {21,10}, {20,10}, {20,10},
00170         {19,10}, {19,10}, {18,10}, {18,10}, {17,10}, {17,10}, {16,10}, {16,10},
00171         {15,8},  {15,8},  {15,8},  {15,8},  {15,8},  {15,8},  {15,8},  {15,8},
00172         {14,8},  {14,8},  {14,8},  {14,8},  {14,8},  {14,8},  {14,8},  {14,8},
00173         {13,8},  {13,8},  {13,8},  {13,8},  {13,8},  {13,8},  {13,8},  {13,8},
00174         {12,8},  {12,8},  {12,8},  {12,8},  {12,8},  {12,8},  {12,8},  {12,8},
00175         {11,8},  {11,8},  {11,8},  {11,8},  {11,8},  {11,8},  {11,8},  {11,8},
00176         {10,8},  {10,8},  {10,8},  {10,8},  {10,8},  {10,8},  {10,8},  {10,8},
00177         {9,7},   {9,7},   {9,7},   {9,7},   {9,7},   {9,7},   {9,7},   {9,7},
00178         {9,7},   {9,7},   {9,7},   {9,7},   {9,7},   {9,7},   {9,7},   {9,7},
00179         {8,7},   {8,7},   {8,7},   {8,7},   {8,7},   {8,7},   {8,7},   {8,7},
00180         {8,7},   {8,7},   {8,7},   {8,7},   {8,7},   {8,7},   {8,7},   {8,7}
00181 };
00182 
00183 /* Table B-12, dct_dc_size_luminance, codes 00xxx ... 11110 */
00184 static VLCtab DClumtab0[32] =
00185 {
00186         {1, 2}, {1, 2}, {1, 2}, {1, 2}, {1, 2}, {1, 2}, {1, 2}, {1, 2},
00187         {2, 2}, {2, 2}, {2, 2}, {2, 2}, {2, 2}, {2, 2}, {2, 2}, {2, 2},
00188         {0, 3}, {0, 3}, {0, 3}, {0, 3}, {3, 3}, {3, 3}, {3, 3}, {3, 3},
00189         {4, 3}, {4, 3}, {4, 3}, {4, 3}, {5, 4}, {5, 4}, {6, 5}, {ERROR_VALUE, 0}
00190 };
00191 
00192 /* Table B-12, dct_dc_size_luminance, codes 111110xxx ... 111111111 */
00193 static VLCtab DClumtab1[16] =
00194 {
00195         {7, 6}, {7, 6}, {7, 6}, {7, 6}, {7, 6}, {7, 6}, {7, 6}, {7, 6},
00196         {8, 7}, {8, 7}, {8, 7}, {8, 7}, {9, 8}, {9, 8}, {10,9}, {11,9}
00197 };
00198 
00199 /* Table B-13, dct_dc_size_chrominance, codes 00xxx ... 11110 */
00200 static VLCtab DCchromtab0[32] =
00201 {
00202         {0, 2}, {0, 2}, {0, 2}, {0, 2}, {0, 2}, {0, 2}, {0, 2}, {0, 2},
00203         {1, 2}, {1, 2}, {1, 2}, {1, 2}, {1, 2}, {1, 2}, {1, 2}, {1, 2},
00204         {2, 2}, {2, 2}, {2, 2}, {2, 2}, {2, 2}, {2, 2}, {2, 2}, {2, 2},
00205         {3, 3}, {3, 3}, {3, 3}, {3, 3}, {4, 4}, {4, 4}, {5, 5}, {ERROR_VALUE, 0}
00206 };
00207 
00208 /* Table B-13, dct_dc_size_chrominance, codes 111110xxxx ... 1111111111 */
00209 static VLCtab DCchromtab1[32] =
00210 {
00211         {6, 6}, {6, 6}, {6, 6}, {6, 6}, {6, 6}, {6, 6}, {6, 6}, {6, 6},
00212         {6, 6}, {6, 6}, {6, 6}, {6, 6}, {6, 6}, {6, 6}, {6, 6}, {6, 6},
00213         {7, 7}, {7, 7}, {7, 7}, {7, 7}, {7, 7}, {7, 7}, {7, 7}, {7, 7},
00214         {8, 8}, {8, 8}, {8, 8}, {8, 8}, {9, 9}, {9, 9}, {10,10}, {11,10}
00215 };
00216 
00217 /* Table B-14, DCT coefficients table zero,
00218  * codes 0100 ... 1xxx (used for first (DC) coefficient)
00219  */
00220 static DCTtab DCTtabfirst[12] =
00221 {
00222         {0,2,4}, {2,1,4}, {1,1,3}, {1,1,3},
00223         {0,1,1}, {0,1,1}, {0,1,1}, {0,1,1},
00224         {0,1,1}, {0,1,1}, {0,1,1}, {0,1,1}
00225 };
00226 
00227 /* Table B-14, DCT coefficients table zero,
00228  * codes 0100 ... 1xxx (used for all other coefficients)
00229  */
00230 static DCTtab DCTtabnext[12] =
00231 {
00232         {0,2,4},  {2,1,4},  {1,1,3},  {1,1,3},
00233         {64,0,2}, {64,0,2}, {64,0,2}, {64,0,2}, /* EOB */
00234         {0,1,2},  {0,1,2},  {0,1,2},  {0,1,2}
00235 };
00236 
00237 /* Table B-14, DCT coefficients table zero,
00238  * codes 000001xx ... 00111xxx
00239  */
00240 static DCTtab DCTtab0[60] =
00241 {
00242         {65,0,6}, {65,0,6}, {65,0,6}, {65,0,6}, /* Escape */
00243         {2,2,7}, {2,2,7}, {9,1,7}, {9,1,7},
00244         {0,4,7}, {0,4,7}, {8,1,7}, {8,1,7},
00245         {7,1,6}, {7,1,6}, {7,1,6}, {7,1,6},
00246         {6,1,6}, {6,1,6}, {6,1,6}, {6,1,6},
00247         {1,2,6}, {1,2,6}, {1,2,6}, {1,2,6},
00248         {5,1,6}, {5,1,6}, {5,1,6}, {5,1,6},
00249         {13,1,8}, {0,6,8}, {12,1,8}, {11,1,8},
00250         {3,2,8}, {1,3,8}, {0,5,8}, {10,1,8},
00251         {0,3,5}, {0,3,5}, {0,3,5}, {0,3,5},
00252         {0,3,5}, {0,3,5}, {0,3,5}, {0,3,5},
00253         {4,1,5}, {4,1,5}, {4,1,5}, {4,1,5},
00254         {4,1,5}, {4,1,5}, {4,1,5}, {4,1,5},
00255         {3,1,5}, {3,1,5}, {3,1,5}, {3,1,5},
00256         {3,1,5}, {3,1,5}, {3,1,5}, {3,1,5}
00257 };
00258 
00259 /* Table B-15, DCT coefficients table one,
00260  * codes 000001xx ... 11111111
00261 */
00262 static DCTtab DCTtab0a[252] =
00263 {
00264         {65,0,6}, {65,0,6}, {65,0,6}, {65,0,6}, /* Escape */
00265         {7,1,7}, {7,1,7}, {8,1,7}, {8,1,7},
00266         {6,1,7}, {6,1,7}, {2,2,7}, {2,2,7},
00267         {0,7,6}, {0,7,6}, {0,7,6}, {0,7,6},
00268         {0,6,6}, {0,6,6}, {0,6,6}, {0,6,6},
00269         {4,1,6}, {4,1,6}, {4,1,6}, {4,1,6},
00270         {5,1,6}, {5,1,6}, {5,1,6}, {5,1,6},
00271         {1,5,8}, {11,1,8}, {0,11,8}, {0,10,8},
00272         {13,1,8}, {12,1,8}, {3,2,8}, {1,4,8},
00273         {2,1,5}, {2,1,5}, {2,1,5}, {2,1,5},
00274         {2,1,5}, {2,1,5}, {2,1,5}, {2,1,5},
00275         {1,2,5}, {1,2,5}, {1,2,5}, {1,2,5},
00276         {1,2,5}, {1,2,5}, {1,2,5}, {1,2,5},
00277         {3,1,5}, {3,1,5}, {3,1,5}, {3,1,5},
00278         {3,1,5}, {3,1,5}, {3,1,5}, {3,1,5},
00279         {1,1,3}, {1,1,3}, {1,1,3}, {1,1,3},
00280         {1,1,3}, {1,1,3}, {1,1,3}, {1,1,3},
00281         {1,1,3}, {1,1,3}, {1,1,3}, {1,1,3},
00282         {1,1,3}, {1,1,3}, {1,1,3}, {1,1,3},
00283         {1,1,3}, {1,1,3}, {1,1,3}, {1,1,3},
00284         {1,1,3}, {1,1,3}, {1,1,3}, {1,1,3},
00285         {1,1,3}, {1,1,3}, {1,1,3}, {1,1,3},
00286         {1,1,3}, {1,1,3}, {1,1,3}, {1,1,3},
00287         {64,0,4}, {64,0,4}, {64,0,4}, {64,0,4}, /* EOB */
00288         {64,0,4}, {64,0,4}, {64,0,4}, {64,0,4},
00289         {64,0,4}, {64,0,4}, {64,0,4}, {64,0,4},
00290         {64,0,4}, {64,0,4}, {64,0,4}, {64,0,4},
00291         {0,3,4}, {0,3,4}, {0,3,4}, {0,3,4},
00292         {0,3,4}, {0,3,4}, {0,3,4}, {0,3,4},
00293         {0,3,4}, {0,3,4}, {0,3,4}, {0,3,4},
00294         {0,3,4}, {0,3,4}, {0,3,4}, {0,3,4},
00295         {0,1,2}, {0,1,2}, {0,1,2}, {0,1,2},
00296         {0,1,2}, {0,1,2}, {0,1,2}, {0,1,2},
00297         {0,1,2}, {0,1,2}, {0,1,2}, {0,1,2},
00298         {0,1,2}, {0,1,2}, {0,1,2}, {0,1,2},
00299         {0,1,2}, {0,1,2}, {0,1,2}, {0,1,2},
00300         {0,1,2}, {0,1,2}, {0,1,2}, {0,1,2},
00301         {0,1,2}, {0,1,2}, {0,1,2}, {0,1,2},
00302         {0,1,2}, {0,1,2}, {0,1,2}, {0,1,2},
00303         {0,1,2}, {0,1,2}, {0,1,2}, {0,1,2},
00304         {0,1,2}, {0,1,2}, {0,1,2}, {0,1,2},
00305         {0,1,2}, {0,1,2}, {0,1,2}, {0,1,2},
00306         {0,1,2}, {0,1,2}, {0,1,2}, {0,1,2},
00307         {0,1,2}, {0,1,2}, {0,1,2}, {0,1,2},
00308         {0,1,2}, {0,1,2}, {0,1,2}, {0,1,2},
00309         {0,1,2}, {0,1,2}, {0,1,2}, {0,1,2},
00310         {0,1,2}, {0,1,2}, {0,1,2}, {0,1,2},
00311         {0,2,3}, {0,2,3}, {0,2,3}, {0,2,3},
00312         {0,2,3}, {0,2,3}, {0,2,3}, {0,2,3},
00313         {0,2,3}, {0,2,3}, {0,2,3}, {0,2,3},
00314         {0,2,3}, {0,2,3}, {0,2,3}, {0,2,3},
00315         {0,2,3}, {0,2,3}, {0,2,3}, {0,2,3},
00316         {0,2,3}, {0,2,3}, {0,2,3}, {0,2,3},
00317         {0,2,3}, {0,2,3}, {0,2,3}, {0,2,3},
00318         {0,2,3}, {0,2,3}, {0,2,3}, {0,2,3},
00319         {0,4,5}, {0,4,5}, {0,4,5}, {0,4,5},
00320         {0,4,5}, {0,4,5}, {0,4,5}, {0,4,5},
00321         {0,5,5}, {0,5,5}, {0,5,5}, {0,5,5},
00322         {0,5,5}, {0,5,5}, {0,5,5}, {0,5,5},
00323         {9,1,7}, {9,1,7}, {1,3,7}, {1,3,7},
00324         {10,1,7}, {10,1,7}, {0,8,7}, {0,8,7},
00325         {0,9,7}, {0,9,7}, {0,12,8}, {0,13,8},
00326         {2,3,8}, {4,2,8}, {0,14,8}, {0,15,8}
00327 };
00328 
00329 /* Table B-14, DCT coefficients table zero,
00330  * codes 0000001000 ... 0000001111
00331  */
00332 static DCTtab DCTtab1[8] =
00333 {
00334         {16,1,10}, {5,2,10}, {0,7,10}, {2,3,10},
00335         {1,4,10}, {15,1,10}, {14,1,10}, {4,2,10}
00336 };
00337 
00338 /* Table B-15, DCT coefficients table one,
00339  * codes 000000100x ... 000000111x
00340  */
00341 static DCTtab DCTtab1a[8] =
00342 {
00343         {5,2,9}, {5,2,9}, {14,1,9}, {14,1,9},
00344         {2,4,10}, {16,1,10}, {15,1,9}, {15,1,9}
00345 };
00346 
00347 /* Table B-14/15, DCT coefficients table zero / one,
00348  * codes 000000010000 ... 000000011111
00349  */
00350 static DCTtab DCTtab2[16] =
00351 {
00352         {0,11,12}, {8,2,12}, {4,3,12}, {0,10,12},
00353         {2,4,12}, {7,2,12}, {21,1,12}, {20,1,12},
00354         {0,9,12}, {19,1,12}, {18,1,12}, {1,5,12},
00355         {3,3,12}, {0,8,12}, {6,2,12}, {17,1,12}
00356 };
00357 
00358 /* Table B-14/15, DCT coefficients table zero / one,
00359  * codes 0000000010000 ... 0000000011111
00360  */
00361 static DCTtab DCTtab3[16] =
00362 {
00363         {10,2,13}, {9,2,13}, {5,3,13}, {3,4,13},
00364         {2,5,13}, {1,7,13}, {1,6,13}, {0,15,13},
00365         {0,14,13}, {0,13,13}, {0,12,13}, {26,1,13},
00366         {25,1,13}, {24,1,13}, {23,1,13}, {22,1,13}
00367 };
00368 
00369 /* Table B-14/15, DCT coefficients table zero / one,
00370  * codes 00000000010000 ... 00000000011111
00371  */
00372 static DCTtab DCTtab4[16] =
00373 {
00374         {0,31,14}, {0,30,14}, {0,29,14}, {0,28,14},
00375         {0,27,14}, {0,26,14}, {0,25,14}, {0,24,14},
00376         {0,23,14}, {0,22,14}, {0,21,14}, {0,20,14},
00377         {0,19,14}, {0,18,14}, {0,17,14}, {0,16,14}
00378 };
00379 
00380 /* Table B-14/15, DCT coefficients table zero / one,
00381  * codes 000000000010000 ... 000000000011111
00382  */
00383 static DCTtab DCTtab5[16] =
00384 {
00385         {0,40,15}, {0,39,15}, {0,38,15}, {0,37,15},
00386         {0,36,15}, {0,35,15}, {0,34,15}, {0,33,15},
00387         {0,32,15}, {1,14,15}, {1,13,15}, {1,12,15},
00388         {1,11,15}, {1,10,15}, {1,9,15}, {1,8,15}
00389 };
00390 
00391 /* Table B-14/15, DCT coefficients table zero / one,
00392  * codes 0000000000010000 ... 0000000000011111
00393  */
00394 static DCTtab DCTtab6[16] =
00395 {
00396         {1,18,16}, {1,17,16}, {1,16,16}, {1,15,16},
00397         {6,3,16}, {16,2,16}, {15,2,16}, {14,2,16},
00398         {13,2,16}, {12,2,16}, {11,2,16}, {31,1,16},
00399         {30,1,16}, {29,1,16}, {28,1,16}, {27,1,16}
00400 };
00401 
00402 /* Table B-3, macroblock_type in P-pictures, codes 001..1xx */
00403 static VLCtab PMBtab0[8] =
00404 {
00405         {ERROR_VALUE,0},
00406         {MACROBLOCK_MOTION_FORWARD,3},
00407         {MACROBLOCK_PATTERN,2}, {MACROBLOCK_PATTERN,2},
00408         {MACROBLOCK_MOTION_FORWARD|MACROBLOCK_PATTERN,1}, 
00409         {MACROBLOCK_MOTION_FORWARD|MACROBLOCK_PATTERN,1},
00410         {MACROBLOCK_MOTION_FORWARD|MACROBLOCK_PATTERN,1}, 
00411         {MACROBLOCK_MOTION_FORWARD|MACROBLOCK_PATTERN,1}
00412 };
00413 
00414 /* Table B-3, macroblock_type in P-pictures, codes 000001..00011x */
00415 static VLCtab PMBtab1[8] =
00416 {
00417         {ERROR_VALUE,0},
00418         {MACROBLOCK_QUANT|MACROBLOCK_INTRA,6},
00419         {MACROBLOCK_QUANT|MACROBLOCK_PATTERN,5}, {MACROBLOCK_QUANT|MACROBLOCK_PATTERN,5},
00420         {MACROBLOCK_QUANT|MACROBLOCK_MOTION_FORWARD|MACROBLOCK_PATTERN,5}, {MACROBLOCK_QUANT|MACROBLOCK_MOTION_FORWARD|MACROBLOCK_PATTERN,5},
00421         {MACROBLOCK_INTRA,5}, {MACROBLOCK_INTRA,5}
00422 };
00423 
00424 /* Table B-4, macroblock_type in B-pictures, codes 0010..11xx */
00425 static VLCtab BMBtab0[16] =
00426 {
00427         {ERROR_VALUE,0}, 
00428         {ERROR_VALUE,0},
00429         {MACROBLOCK_MOTION_FORWARD,4},
00430         {MACROBLOCK_MOTION_FORWARD|MACROBLOCK_PATTERN,4},
00431         {MACROBLOCK_MOTION_BACKWARD,3}, 
00432         {MACROBLOCK_MOTION_BACKWARD,3},
00433         {MACROBLOCK_MOTION_BACKWARD|MACROBLOCK_PATTERN,3}, 
00434         {MACROBLOCK_MOTION_BACKWARD|MACROBLOCK_PATTERN,3},
00435         {MACROBLOCK_MOTION_FORWARD|MACROBLOCK_MOTION_BACKWARD,2}, 
00436         {MACROBLOCK_MOTION_FORWARD|MACROBLOCK_MOTION_BACKWARD,2},
00437         {MACROBLOCK_MOTION_FORWARD|MACROBLOCK_MOTION_BACKWARD,2}, 
00438         {MACROBLOCK_MOTION_FORWARD|MACROBLOCK_MOTION_BACKWARD,2},
00439         {MACROBLOCK_MOTION_FORWARD|MACROBLOCK_MOTION_BACKWARD|MACROBLOCK_PATTERN,2},
00440         {MACROBLOCK_MOTION_FORWARD|MACROBLOCK_MOTION_BACKWARD|MACROBLOCK_PATTERN,2},
00441         {MACROBLOCK_MOTION_FORWARD|MACROBLOCK_MOTION_BACKWARD|MACROBLOCK_PATTERN,2},
00442         {MACROBLOCK_MOTION_FORWARD|MACROBLOCK_MOTION_BACKWARD|MACROBLOCK_PATTERN,2}
00443 };
00444 
00445 /* Table B-4, macroblock_type in B-pictures, codes 000001..00011x */
00446 static VLCtab BMBtab1[8] =
00447 {
00448         {ERROR_VALUE,0},
00449         {MACROBLOCK_QUANT|MACROBLOCK_INTRA,6},
00450         {MACROBLOCK_QUANT|MACROBLOCK_MOTION_BACKWARD|MACROBLOCK_PATTERN,6},
00451         {MACROBLOCK_QUANT|MACROBLOCK_MOTION_FORWARD|MACROBLOCK_PATTERN,6},
00452         {MACROBLOCK_QUANT|MACROBLOCK_MOTION_FORWARD|MACROBLOCK_MOTION_BACKWARD|MACROBLOCK_PATTERN,5},
00453         {MACROBLOCK_QUANT|MACROBLOCK_MOTION_FORWARD|MACROBLOCK_MOTION_BACKWARD|MACROBLOCK_PATTERN,5},
00454         {MACROBLOCK_INTRA,5}, 
00455         {MACROBLOCK_INTRA,5}
00456 };
00457 
00458 //
00459 // getbit
00460 //
00461 
00462 void CMPEG2Dec::Initialize_Buffer()
00463 {
00464         Rdptr = Rdbfr + BUFFER_SIZE;
00465         Rdmax = Rdptr;
00466 
00467         if (SystemStream_Flag)
00468         {
00469                 if (Rdptr >= Rdmax)
00470                         Next_Packet();
00471                 CurrentBfr = *Rdptr++ << 24;
00472 
00473                 if (Rdptr >= Rdmax)
00474                         Next_Packet();
00475                 CurrentBfr += *Rdptr++ << 16;
00476 
00477                 if (Rdptr >= Rdmax)
00478                         Next_Packet();
00479                 CurrentBfr += *Rdptr++ << 8;
00480 
00481                 if (Rdptr >= Rdmax)
00482                         Next_Packet();
00483                 CurrentBfr += *Rdptr++;
00484 
00485                 Fill_Next();
00486         }
00487         else
00488         {
00489                 Fill_Buffer();
00490 
00491                 CurrentBfr = (*Rdptr << 24) + (*(Rdptr+1) << 16) + (*(Rdptr+2) << 8) + *(Rdptr+3);
00492                 Rdptr += 4;
00493 
00494                 Fill_Next();
00495         }
00496 
00497         BitsLeft = 32;
00498 }
00499 
00500 unsigned int CMPEG2Dec::Get_Bits_All(unsigned int N)
00501 {
00502         N -= BitsLeft;
00503         Val = (CurrentBfr << (32 - BitsLeft)) >> (32 - BitsLeft);
00504 
00505         if (N != 0)
00506                 Val = (Val << N) + (NextBfr >> (32 - N));
00507 
00508         CurrentBfr = NextBfr;
00509         BitsLeft = 32 - N;
00510         Fill_Next();
00511 
00512         return Val;
00513 }
00514 
00515 void CMPEG2Dec::Flush_Buffer_All(unsigned int N)
00516 {
00517         CurrentBfr = NextBfr;
00518         BitsLeft = BitsLeft + 32 - N;
00519         Fill_Next();
00520 }
00521 
00522 void CMPEG2Dec::Next_Packet()
00523 {
00524         unsigned int code, Packet_Length, Packet_Header_Length;
00525 
00526         for (;;)
00527         {
00528                 code = Get_Short();
00529                 code = (code<<16) + Get_Short();
00530 
00531                 // remove system layer byte stuffing
00532                 while ((code & 0xffffff00) != 0x00000100)
00533                         code = (code<<8) + Get_Byte();
00534 
00535                 switch (code)
00536                 {
00537                         case PACK_START_CODE:
00538                                 Rdptr += 8;
00539                                 break;
00540 
00541                         case VIDEO_ELEMENTARY_STREAM:   
00542                                 Packet_Length = Get_Short();
00543                                 Rdmax = Rdptr + Packet_Length;
00544 
00545                                 code = Get_Byte();
00546 
00547                                 if ((code & 0xc0)==0x80)
00548                                 {
00549                                         code = Get_Byte();
00550                                         Packet_Header_Length = Get_Byte();
00551 
00552                                         Rdptr += Packet_Header_Length;
00553                                         return;
00554                                 }
00555                                 else
00556                                         Rdptr += Packet_Length-1;
00557                                 break;
00558 
00559                         default:
00560                                 if (code>=SYSTEM_START_CODE)
00561                                 {
00562                                         code = Get_Short();
00563                                         Rdptr += code;
00564                                 }
00565                                 break;
00566                 }
00567         }
00568 }
00569 
00570 void CMPEG2Dec::Fill_Buffer()
00571 {
00572         Read = _read(Infile[File_Flag], Rdbfr, BUFFER_SIZE);
00573 
00574         if (Read < BUFFER_SIZE)
00575                 Next_File();
00576 
00577         if (KeyOp_Flag && (Rdbfr[20] & 0x10))
00578         {
00579                 BufferOp(Rdbfr, lfsr0, lfsr1);
00580                 Rdbfr[20] &= ~0x10;
00581         }
00582 
00583         Rdptr = Rdbfr;
00584 
00585         if (SystemStream_Flag)
00586                 Rdmax -= BUFFER_SIZE;
00587 }
00588 
00589 void CMPEG2Dec::Next_File()
00590 {
00591         if (File_Flag < File_Limit-1)
00592                 File_Flag ++;
00593 
00594         _lseeki64(Infile[File_Flag], 0, SEEK_SET);
00595         _read(Infile[File_Flag], Rdbfr + Read, BUFFER_SIZE - Read);
00596 }
00597 
00598 
00599 unsigned int CMPEG2Dec::Show_Bits(unsigned int N)
00600 {
00601         if (N <= BitsLeft)
00602                 return (CurrentBfr << (32 - BitsLeft)) >> (32 - N);
00603         else
00604         {
00605                 N -= BitsLeft;
00606                 return (((CurrentBfr << (32 - BitsLeft)) >> (32 - BitsLeft)) << N) + (NextBfr >> (32 - N));
00607         }
00608 }
00609 
00610 unsigned int CMPEG2Dec::Get_Bits(unsigned int N)
00611 {
00612         if (N < BitsLeft)
00613         {
00614                 Val = (CurrentBfr << (32 - BitsLeft)) >> (32 - N);
00615                 BitsLeft -= N;
00616                 return Val;
00617         }
00618         else
00619                 return Get_Bits_All(N);
00620 }
00621 
00622 void CMPEG2Dec::Flush_Buffer(unsigned int N)
00623 {
00624         if (N < BitsLeft)
00625                 BitsLeft -= N;
00626         else
00627                 Flush_Buffer_All(N);    
00628 }
00629 
00630 void CMPEG2Dec::Fill_Next()
00631 {
00632         if (SystemStream_Flag && Rdptr>=Rdmax-4)
00633         {
00634                 if (Rdptr >= Rdmax)
00635                         Next_Packet();
00636                 NextBfr = Get_Byte() << 24;
00637 
00638                 if (Rdptr >= Rdmax)
00639                         Next_Packet();
00640                 NextBfr += Get_Byte() << 16;
00641 
00642                 if (Rdptr >= Rdmax)
00643                         Next_Packet();
00644                 NextBfr += Get_Byte() << 8;
00645 
00646                 if (Rdptr >= Rdmax)
00647                         Next_Packet();
00648                 NextBfr += Get_Byte();
00649         }
00650         else if (Rdptr < Rdbfr+BUFFER_SIZE-4)
00651         {
00652                 NextBfr = (*Rdptr << 24) + (*(Rdptr+1) << 16) + (*(Rdptr+2) << 8) + *(Rdptr+3);
00653                 Rdptr += 4;
00654         }
00655         else
00656         {
00657                 if (Rdptr >= Rdbfr+BUFFER_SIZE)
00658                         Fill_Buffer();
00659                 NextBfr = *Rdptr++ << 24;
00660 
00661                 if (Rdptr >= Rdbfr+BUFFER_SIZE)
00662                         Fill_Buffer();
00663                 NextBfr += *Rdptr++ << 16;
00664 
00665                 if (Rdptr >= Rdbfr+BUFFER_SIZE)
00666                         Fill_Buffer();
00667                 NextBfr += *Rdptr++ << 8;
00668 
00669                 if (Rdptr >= Rdbfr+BUFFER_SIZE)
00670                         Fill_Buffer();
00671                 NextBfr += *Rdptr++;
00672         }
00673 }
00674 
00675 unsigned int CMPEG2Dec::Get_Byte()
00676 {
00677         while (Rdptr >= (Rdbfr + BUFFER_SIZE))
00678         {
00679                 Read = _read(Infile[File_Flag], Rdbfr, BUFFER_SIZE);
00680 
00681                 if (Read < BUFFER_SIZE)
00682                         Next_File();
00683 
00684                 if (KeyOp_Flag && (Rdbfr[20] & 0x10))
00685                 {
00686                         BufferOp(Rdbfr, lfsr0, lfsr1);
00687                         Rdbfr[20] &= ~0x10;
00688                 }
00689 
00690                 Rdptr -= BUFFER_SIZE;
00691                 Rdmax -= BUFFER_SIZE;
00692         }
00693 
00694         return *Rdptr++;
00695 }
00696 
00697 unsigned int CMPEG2Dec::Get_Short()
00698 {
00699         unsigned int i = Get_Byte();
00700         return (i<<8) + Get_Byte();
00701 }
00702 
00703 void CMPEG2Dec::next_start_code()
00704 {
00705         Flush_Buffer(BitsLeft & 7);
00706 
00707         while (Show_Bits(24) != 1)
00708                 Flush_Buffer(8);
00709 }
00710 
00711 //
00712 // gethdr
00713 //
00714 
00715 int CMPEG2Dec::Get_Hdr()
00716 {
00717         for (;;)
00718         {
00719                 /* look for next_start_code */
00720                 next_start_code();
00721 
00722                 switch (Get_Bits(32))
00723                 {
00724                         case SEQUENCE_HEADER_CODE:
00725                                 sequence_header();
00726                                 break;
00727 
00728                         case GROUP_START_CODE:
00729                                 group_of_pictures_header();
00730                                 break;
00731 
00732                         case PICTURE_START_CODE:
00733                                 picture_header();
00734                                 return 1;
00735                 }
00736         }
00737 }
00738 
00739 /* decode group of pictures header */
00740 /* ISO/IEC 13818-2 section 6.2.2.6 */
00741 void CMPEG2Dec::group_of_pictures_header()
00742 {
00743         int gop_hour;
00744         int gop_minute;
00745         int gop_sec;
00746         int gop_frame;
00747 
00748         int drop_flag;
00749         int closed_gop;
00750         int broken_link;
00751 
00752         drop_flag   = Get_Bits(1);
00753         gop_hour    = Get_Bits(5);
00754         gop_minute  = Get_Bits(6);
00755         Flush_Buffer(1);        // marker bit
00756         gop_sec     = Get_Bits(6);
00757         gop_frame       = Get_Bits(6);
00758         closed_gop  = Get_Bits(1);
00759         broken_link = Get_Bits(1);
00760 
00761         extension_and_user_data();
00762 }
00763 
00764 /* decode picture header */
00765 /* ISO/IEC 13818-2 section 6.2.3 */
00766 void CMPEG2Dec::picture_header()
00767 {
00768         int vbv_delay;
00769         int full_pel_forward_vector;
00770         int forward_f_code;
00771         int full_pel_backward_vector;
00772         int backward_f_code;
00773         int Extra_Information_Byte_Count;
00774 
00775         temporal_reference  = Get_Bits(10);
00776         picture_coding_type = Get_Bits(3);
00777         vbv_delay           = Get_Bits(16);
00778 
00779         if (picture_coding_type==P_TYPE || picture_coding_type==B_TYPE)
00780         {
00781                 full_pel_forward_vector = Get_Bits(1);
00782                 forward_f_code = Get_Bits(3);
00783         }
00784 
00785         if (picture_coding_type==B_TYPE)
00786         {
00787                 full_pel_backward_vector = Get_Bits(1);
00788                 backward_f_code = Get_Bits(3);
00789         }
00790 
00791         Extra_Information_Byte_Count = extra_bit_information();
00792         extension_and_user_data();
00793 }
00794 
00795 /* decode sequence header */
00796 void CMPEG2Dec::sequence_header()
00797 {
00798         int frame_rate_code;
00799         int vbv_buffer_size;
00800         int aspect_ratio_information;
00801         int bit_rate_value;
00802 
00803         int constrained_parameters_flag;
00804         int i;
00805 
00806         horizontal_size             = Get_Bits(12);
00807         vertical_size               = Get_Bits(12);
00808         aspect_ratio_information    = Get_Bits(4);
00809         frame_rate_code             = Get_Bits(4);
00810         bit_rate_value              = Get_Bits(18);
00811         Flush_Buffer(1);        // marker bit
00812         vbv_buffer_size             = Get_Bits(10);
00813         constrained_parameters_flag = Get_Bits(1);
00814 
00815         if (load_intra_quantizer_matrix = Get_Bits(1))
00816         {
00817                 for (i=0; i<64; i++)
00818                         intra_quantizer_matrix[scan[ZIG_ZAG][i]] = Get_Bits(8);
00819         }
00820         else
00821         {
00822                 for (i=0; i<64; i++)
00823                         intra_quantizer_matrix[i] = default_intra_quantizer_matrix[i];
00824         }
00825 
00826         if (load_non_intra_quantizer_matrix = Get_Bits(1))
00827         {
00828                 for (i=0; i<64; i++)
00829                         non_intra_quantizer_matrix[scan[ZIG_ZAG][i]] = Get_Bits(8);
00830         }
00831         else
00832         {
00833                 for (i=0; i<64; i++)
00834                         non_intra_quantizer_matrix[i] = 16;
00835         }
00836 
00837         /* copy luminance to chrominance matrices */
00838         for (i=0; i<64; i++)
00839         {
00840                 chroma_intra_quantizer_matrix[i] = intra_quantizer_matrix[i];
00841                 chroma_non_intra_quantizer_matrix[i] = non_intra_quantizer_matrix[i];
00842         }
00843         extension_and_user_data();
00844 }
00845 
00846 /* decode slice header */
00847 /* ISO/IEC 13818-2 section 6.2.4 */
00848 int CMPEG2Dec::slice_header()
00849 {
00850         int slice_vertical_position_extension;
00851         int quantizer_scale_code;
00852         int slice_picture_id_enable = 0;
00853         int slice_picture_id = 0;
00854         int extra_information_slice = 0;
00855 
00856         slice_vertical_position_extension = vertical_size>2800 ? Get_Bits(3) : 0;
00857 
00858         quantizer_scale_code = Get_Bits(5);
00859         quantizer_scale = q_scale_type ? Non_Linear_quantizer_scale[quantizer_scale_code] : quantizer_scale_code<<1;
00860 
00861         /* slice_id introduced in March 1995 as part of the video corridendum
00862            (after the IS was drafted in November 1994) */
00863         if (Get_Bits(1))
00864         {
00865                 Get_Bits(1);    // intra slice
00866 
00867                 slice_picture_id_enable = Get_Bits(1);
00868                 slice_picture_id = Get_Bits(6);
00869 
00870                 extra_information_slice = extra_bit_information();
00871         }
00872 
00873         return slice_vertical_position_extension;
00874 }
00875 
00876 /* decode extension and user data */
00877 /* ISO/IEC 13818-2 section 6.2.2.2 */
00878 void CMPEG2Dec::extension_and_user_data()
00879 {
00880         int code, ext_ID;
00881 
00882         next_start_code();
00883 
00884         while ((code = Show_Bits(32))==EXTENSION_START_CODE || code==USER_DATA_START_CODE)
00885         {
00886                 if (code==EXTENSION_START_CODE)
00887                 {
00888                         Flush_Buffer(32);
00889                         ext_ID = Get_Bits(4);
00890 
00891                         switch (ext_ID)
00892                         {
00893                                 case SEQUENCE_EXTENSION_ID:
00894                                         sequence_extension();
00895                                         break;
00896 
00897                                 case SEQUENCE_DISPLAY_EXTENSION_ID:
00898                                         sequence_display_extension();
00899                                         break;
00900 
00901                                 case QUANT_MATRIX_EXTENSION_ID:
00902                                         quant_matrix_extension();
00903                                         break;
00904 
00905                                 case PICTURE_DISPLAY_EXTENSION_ID:
00906                                         picture_display_extension();
00907                                         break;
00908 
00909                                 case PICTURE_CODING_EXTENSION_ID:
00910                                         picture_coding_extension();
00911                                         break;
00912 
00913                                 case COPYRIGHT_EXTENSION_ID:
00914                                         copyright_extension();
00915                                         break;
00916                         }
00917                         next_start_code();
00918                 }
00919                 else
00920                 {
00921                         Flush_Buffer(32);
00922                         next_start_code();
00923                 }
00924         }
00925 }
00926 
00927 /* decode sequence extension */
00928 /* ISO/IEC 13818-2 section 6.2.2.3 */
00929 void CMPEG2Dec::sequence_extension()
00930 {
00931         int profile_and_level_indication;
00932         int low_delay;
00933         int frame_rate_extension_n;
00934         int frame_rate_extension_d;
00935 
00936         int horizontal_size_extension;
00937         int vertical_size_extension;
00938         int bit_rate_extension;
00939         int vbv_buffer_size_extension;
00940 
00941         profile_and_level_indication = Get_Bits(8);
00942         progressive_sequence         = Get_Bits(1);
00943         chroma_format                = Get_Bits(2);
00944         horizontal_size_extension    = Get_Bits(2);
00945         vertical_size_extension      = Get_Bits(2);
00946         bit_rate_extension           = Get_Bits(12);
00947         Flush_Buffer(1);        // marker bit
00948         vbv_buffer_size_extension    = Get_Bits(8);
00949         low_delay                    = Get_Bits(1);
00950  
00951         frame_rate_extension_n       = Get_Bits(2);
00952         frame_rate_extension_d       = Get_Bits(5);
00953 
00954         horizontal_size = (horizontal_size_extension<<12) | (horizontal_size&0x0fff);
00955         vertical_size = (vertical_size_extension<<12) | (vertical_size&0x0fff);
00956 }
00957 
00958 /* decode sequence display extension */
00959 void CMPEG2Dec::sequence_display_extension()
00960 {
00961         int video_format;  
00962         int color_description;
00963         int color_primaries;
00964         int transfer_characteristics;
00965         int matrix_coefficients;
00966         int display_horizontal_size;
00967         int display_vertical_size;
00968 
00969         video_format      = Get_Bits(3);
00970         color_description = Get_Bits(1);
00971 
00972         if (color_description)
00973         {
00974                 color_primaries          = Get_Bits(8);
00975                 transfer_characteristics = Get_Bits(8);
00976                 matrix_coefficients      = Get_Bits(8);
00977         }
00978 
00979         display_horizontal_size = Get_Bits(14);
00980         Flush_Buffer(1);        // marker bit
00981         display_vertical_size   = Get_Bits(14);
00982 }
00983 
00984 /* decode quant matrix entension */
00985 /* ISO/IEC 13818-2 section 6.2.3.2 */
00986 void CMPEG2Dec::quant_matrix_extension()
00987 {
00988         int i;
00989 
00990         if (load_intra_quantizer_matrix = Get_Bits(1))
00991                 for (i=0; i<64; i++)
00992                         chroma_intra_quantizer_matrix[scan[ZIG_ZAG][i]]
00993                                 = intra_quantizer_matrix[scan[ZIG_ZAG][i]] = Get_Bits(8);
00994 
00995         if (load_non_intra_quantizer_matrix = Get_Bits(1))
00996                 for (i=0; i<64; i++)
00997                         chroma_non_intra_quantizer_matrix[scan[ZIG_ZAG][i]]
00998                                 = non_intra_quantizer_matrix[scan[ZIG_ZAG][i]] = Get_Bits(8);
00999 
01000         if (load_chroma_intra_quantizer_matrix = Get_Bits(1))
01001                 for (i=0; i<64; i++)
01002                         chroma_intra_quantizer_matrix[scan[ZIG_ZAG][i]] = Get_Bits(8);
01003 
01004         if (load_chroma_non_intra_quantizer_matrix = Get_Bits(1))
01005                 for (i=0; i<64; i++)
01006                         chroma_non_intra_quantizer_matrix[scan[ZIG_ZAG][i]] = Get_Bits(8);
01007 }
01008 
01009 /* decode picture display extension */
01010 /* ISO/IEC 13818-2 section 6.2.3.3. */
01011 void CMPEG2Dec::picture_display_extension()
01012 {
01013         int frame_center_horizontal_offset[3];
01014         int frame_center_vertical_offset[3];
01015 
01016         int i;
01017         int number_of_frame_center_offsets;
01018 
01019         /* based on ISO/IEC 13818-2 section 6.3.12 
01020            (November 1994) Picture display extensions */
01021 
01022         /* derive number_of_frame_center_offsets */
01023         if (progressive_sequence)
01024         {
01025                 if (repeat_first_field)
01026                 {
01027                         if (top_field_first)
01028                                 number_of_frame_center_offsets = 3;
01029                         else
01030                                 number_of_frame_center_offsets = 2;
01031                 }
01032                 else
01033                         number_of_frame_center_offsets = 1;
01034         }
01035         else
01036         {
01037                 if (picture_structure!=FRAME_PICTURE)
01038                         number_of_frame_center_offsets = 1;
01039                 else
01040                 {
01041                         if (repeat_first_field)
01042                                 number_of_frame_center_offsets = 3;
01043                         else
01044                                 number_of_frame_center_offsets = 2;
01045                 }
01046         }
01047 
01048         /* now parse */
01049         for (i=0; i<number_of_frame_center_offsets; i++)
01050         {
01051                 frame_center_horizontal_offset[i] = Get_Bits(16);
01052                 Flush_Buffer(1);        // marker bit
01053 
01054                 frame_center_vertical_offset[i] = Get_Bits(16);
01055                 Flush_Buffer(1);        // marker bit
01056         }
01057 }
01058 
01059 /* decode picture coding extension */
01060 void CMPEG2Dec::picture_coding_extension()
01061 {
01062         int chroma_420_type;
01063         int composite_display_flag;
01064         int v_axis;
01065         int field_sequence;
01066         int sub_carrier;
01067         int burst_amplitude;
01068         int sub_carrier_phase;
01069 
01070         f_code[0][0] = Get_Bits(4);
01071         f_code[0][1] = Get_Bits(4);
01072         f_code[1][0] = Get_Bits(4);
01073         f_code[1][1] = Get_Bits(4);
01074 
01075         intra_dc_precision         = Get_Bits(2);
01076         picture_structure          = Get_Bits(2);
01077         top_field_first            = Get_Bits(1);
01078         frame_pred_frame_dct       = Get_Bits(1);
01079         concealment_motion_vectors = Get_Bits(1);
01080         q_scale_type                       = Get_Bits(1);
01081         intra_vlc_format           = Get_Bits(1);
01082         alternate_scan                     = Get_Bits(1);
01083         repeat_first_field         = Get_Bits(1);
01084         chroma_420_type            = Get_Bits(1);
01085         progressive_frame          = Get_Bits(1);
01086         composite_display_flag     = Get_Bits(1);
01087 
01088         pf_current = progressive_frame;
01089 
01090         if (composite_display_flag)
01091         {
01092                 v_axis            = Get_Bits(1);
01093                 field_sequence    = Get_Bits(3);
01094                 sub_carrier       = Get_Bits(1);
01095                 burst_amplitude   = Get_Bits(7);
01096                 sub_carrier_phase = Get_Bits(8);
01097         }
01098 }
01099 
01100 /* decode extra bit information */
01101 /* ISO/IEC 13818-2 section 6.2.3.4. */
01102 int CMPEG2Dec::extra_bit_information()
01103 {
01104         int Byte_Count = 0;
01105 
01106         while (Get_Bits(1))
01107         {
01108                 Flush_Buffer(8);
01109                 Byte_Count ++;
01110         }
01111 
01112         return(Byte_Count);
01113 }
01114 
01115 /* Copyright extension */
01116 /* ISO/IEC 13818-2 section 6.2.3.6. */
01117 /* (header added in November, 1994 to the IS document) */
01118 void CMPEG2Dec::copyright_extension()
01119 {
01120         int copyright_flag;
01121         int copyright_identifier;
01122         int original_or_copy;
01123         int copyright_number_1;
01124         int copyright_number_2;
01125         int copyright_number_3;
01126 
01127         int reserved_data;
01128 
01129         copyright_flag =       Get_Bits(1); 
01130         copyright_identifier = Get_Bits(8);
01131         original_or_copy =     Get_Bits(1);
01132   
01133         /* reserved */
01134         reserved_data = Get_Bits(7);
01135 
01136         Flush_Buffer(1); // marker bit
01137         copyright_number_1 =   Get_Bits(20);
01138         Flush_Buffer(1); // marker bit
01139         copyright_number_2 =   Get_Bits(22);
01140         Flush_Buffer(1); // marker bit
01141         copyright_number_3 =   Get_Bits(22);
01142 }
01143 
01144 //
01145 // getpic
01146 //
01147 
01148 static const unsigned char cc_table[12] = {
01149         0, 0, 0, 0, 1, 2, 1, 2, 1, 2, 1, 2
01150 };
01151 
01152 void CMPEG2Dec::Decode_Picture(int ref, unsigned char *dst, int pitch)
01153 {
01154         if (picture_structure==FRAME_PICTURE && Second_Field)
01155                 Second_Field = 0;
01156 
01157         if (picture_coding_type!=B_TYPE)
01158         {
01159                 pf_forward = pf_backward;
01160                 pf_backward = pf_current;
01161         }
01162 
01163         Update_Picture_Buffers();
01164 
01165         picture_data();
01166 
01167         if (ref && (picture_structure==FRAME_PICTURE || Second_Field))
01168         {
01169                 if (picture_coding_type==B_TYPE)
01170                         assembleFrame(auxframe, pf_current, dst, pitch);
01171                 else
01172                         assembleFrame(forward_reference_frame, pf_forward, dst, pitch);
01173         }
01174 
01175         if (picture_structure!=FRAME_PICTURE)
01176                 Second_Field = !Second_Field;
01177 }
01178 
01179 /* reuse old picture buffers as soon as they are no longer needed */
01180 void CMPEG2Dec::Update_Picture_Buffers()
01181 {                           
01182         int cc;              /* color component index */
01183         unsigned char *tmp;  /* temporary swap pointer */
01184 
01185         for (cc=0; cc<3; cc++)
01186         {
01187                 /* B pictures  do not need to be save for future reference */
01188                 if (picture_coding_type==B_TYPE)
01189                         current_frame[cc] = auxframe[cc];
01190                 else
01191                 {
01192                         if (!Second_Field)
01193                         {
01194                                 /* only update at the beginning of the coded frame */
01195                                 tmp = forward_reference_frame[cc];
01196 
01197                                 /* the previously decoded reference frame is stored coincident with the 
01198                                    location where the backward reference frame is stored (backwards 
01199                                    prediction is not needed in P pictures) */
01200                                 forward_reference_frame[cc] = backward_reference_frame[cc];
01201 
01202                                 /* update pointer for potential future B pictures */
01203                                 backward_reference_frame[cc] = tmp;
01204                         }
01205 
01206                         /* can erase over old backward reference frame since it is not used
01207                            in a P picture, and since any subsequent B pictures will use the 
01208                            previously decoded I or P frame as the backward_reference_frame */
01209                         current_frame[cc] = backward_reference_frame[cc];
01210                 }
01211 
01212             if (picture_structure==BOTTOM_FIELD)
01213                         current_frame[cc] += (cc==0) ? Coded_Picture_Width : Chroma_Width;
01214         }
01215 }
01216 
01217 /* decode all macroblocks of the current picture */
01218 /* stages described in ISO/IEC 13818-2 section 7 */
01219 void CMPEG2Dec::picture_data()
01220 {
01221         int MBAmax;
01222 
01223         /* number of macroblocks per picture */
01224         MBAmax = mb_width*mb_height;
01225 
01226         if (picture_structure!=FRAME_PICTURE)
01227                 MBAmax>>=1;
01228 
01229         for (;;)
01230                 if (slice(MBAmax)<0)
01231                         return;
01232 }
01233 
01234 /* decode all macroblocks of the current picture */
01235 /* ISO/IEC 13818-2 section 6.3.16 */
01236 /* return 0 : go to next slice */
01237 /* return -1: go to next picture */
01238 int CMPEG2Dec::slice(int MBAmax)
01239 {
01240         int MBA = 0, MBAinc =0, macroblock_type, motion_type, dct_type, ret;
01241         int dc_dct_pred[3], PMV[2][2][2], motion_vertical_field_select[2][2], dmvector[2];
01242 
01243         if ((ret=start_of_slice(&MBA, &MBAinc, dc_dct_pred, PMV))!=1)
01244                 return ret;
01245 
01246         for (;;)
01247         {
01248                 /* this is how we properly exit out of picture */
01249                 if (MBA>=MBAmax) return -1;             // all macroblocks decoded
01250 
01251                 if (MBAinc==0)
01252                 {
01253                         if (!Show_Bits(23) || Fault_Flag)       // next_start_code or fault
01254                         {
01255 resync:
01256                                 Fault_Flag = 0;
01257                                 return 0;       // trigger: go to next slice
01258                         }
01259                         else /* neither next_start_code nor Fault_Flag */
01260                         {
01261                                 /* decode macroblock address increment */
01262                                 MBAinc = Get_macroblock_address_increment();
01263                                 if (Fault_Flag) goto resync;
01264                         }
01265                 }
01266 
01267                 if (MBAinc==1) /* not skipped */
01268                 {
01269                         if (!decode_macroblock(&macroblock_type, &motion_type, &dct_type, PMV,
01270                                 dc_dct_pred, motion_vertical_field_select, dmvector))
01271                                 goto resync;
01272                 }
01273                 else /* MBAinc!=1: skipped macroblock */
01274                         /* ISO/IEC 13818-2 section 7.6.6 */
01275                         skipped_macroblock(dc_dct_pred, PMV, &motion_type, motion_vertical_field_select, &macroblock_type);
01276 
01277                 /* ISO/IEC 13818-2 section 7.6 */
01278                 motion_compensation(MBA, macroblock_type, motion_type, PMV,
01279                                                         motion_vertical_field_select, dmvector, dct_type);
01280 
01281                 /* advance to next macroblock */
01282                 MBA++; MBAinc--;
01283 
01284                 if (MBA>=MBAmax) return -1;             // all macroblocks decoded
01285         }
01286 }
01287 
01288 /* ISO/IEC 13818-2 section 6.3.17.1: Macroblock modes */
01289 void CMPEG2Dec::macroblock_modes(int *pmacroblock_type, int *pmotion_type,
01290                                                          int *pmotion_vector_count, int *pmv_format,
01291                                                          int *pdmv, int *pmvscale, int *pdct_type)
01292 {
01293         int macroblock_type, motion_type = 0, motion_vector_count;
01294         int mv_format, dmv, mvscale, dct_type;
01295 
01296         /* get macroblock_type */
01297         macroblock_type = Get_macroblock_type();
01298         if (Fault_Flag) return;
01299 
01300         /* get frame/field motion type */
01301         if (macroblock_type & (MACROBLOCK_MOTION_FORWARD|MACROBLOCK_MOTION_BACKWARD))
01302         {
01303                 if (picture_structure==FRAME_PICTURE)
01304                         motion_type = frame_pred_frame_dct ? MC_FRAME : Get_Bits(2);
01305                 else
01306                         motion_type = Get_Bits(2);
01307     }
01308         else if ((macroblock_type & MACROBLOCK_INTRA) && concealment_motion_vectors)
01309                 motion_type = (picture_structure==FRAME_PICTURE) ? MC_FRAME : MC_FIELD;
01310 
01311         /* derive motion_vector_count, mv_format and dmv, (table 6-17, 6-18) */
01312         if (picture_structure==FRAME_PICTURE)
01313         {
01314                 motion_vector_count = (motion_type==MC_FIELD) ? 2 : 1;
01315                 mv_format = (motion_type==MC_FRAME) ? MV_FRAME : MV_FIELD;
01316         }
01317         else
01318         {
01319                 motion_vector_count = (motion_type==MC_16X8) ? 2 : 1;
01320                 mv_format = MV_FIELD;
01321         }
01322         
01323         dmv = (motion_type==MC_DMV); /* dual prime */
01324 
01325         /*
01326            field mv predictions in frame pictures have to be scaled
01327            ISO/IEC 13818-2 section 7.6.3.1 Decoding the motion vectors
01328         */
01329         mvscale = (mv_format==MV_FIELD && picture_structure==FRAME_PICTURE);
01330 
01331         /* get dct_type (frame DCT / field DCT) */
01332         dct_type = (picture_structure==FRAME_PICTURE) && (!frame_pred_frame_dct)
01333                                 && (macroblock_type & (MACROBLOCK_PATTERN|MACROBLOCK_INTRA)) ? Get_Bits(1) : 0;
01334 
01335         /* return values */
01336         *pmacroblock_type = macroblock_type;
01337         *pmotion_type = motion_type;
01338         *pmotion_vector_count = motion_vector_count;
01339         *pmv_format = mv_format;
01340         *pdmv = dmv;
01341         *pmvscale = mvscale;
01342         *pdct_type = dct_type;
01343 }
01344 
01345 /* move/add 8x8-Block from block[comp] to backward_reference_frame */
01346 /* copy reconstructed 8x8 block from block[comp] to current_frame[]
01347    ISO/IEC 13818-2 section 7.6.8: Adding prediction and coefficient data
01348    This stage also embodies some of the operations implied by:
01349    - ISO/IEC 13818-2 section 7.6.7: Combining predictions
01350    - ISO/IEC 13818-2 section 6.1.3: Macroblock
01351 */
01352 void CMPEG2Dec::Add_Block(int count, int bx, int by, int dct_type, int addflag)
01353 {
01354         static const __int64 mmmask_128 = 0x0080008000800080;
01355 
01356         int comp, cc, iincr, bxh, byh;
01357         unsigned char *rfp;
01358         short *Block_Ptr;
01359 
01360         for (comp=0; comp<count; comp++)
01361         {
01362                 Block_Ptr = block[comp];
01363                 cc = cc_table[comp];
01364 
01365                 bxh = bx; byh = by;
01366 
01367                 if (cc==0)
01368                 {
01369                         if (picture_structure==FRAME_PICTURE)
01370                         {
01371                                 if (dct_type)
01372                                 {
01373                                         rfp = current_frame[0] + Coded_Picture_Width*(by+((comp&2)>>1)) + bx + ((comp&1)<<3);
01374                                         iincr = Coded_Picture_Width<<1;
01375                                 }
01376                                 else
01377                                 {
01378                                         rfp = current_frame[0] + Coded_Picture_Width*(by+((comp&2)<<2)) + bx + ((comp&1)<<3);
01379                                         iincr = Coded_Picture_Width;
01380                                 }
01381                         }
01382                         else
01383                         {
01384                                 rfp = current_frame[0] + (Coded_Picture_Width<<1)*(by+((comp&2)<<2)) + bx + ((comp&1)<<3);
01385                                 iincr = Coded_Picture_Width<<1;
01386                         }
01387                 }
01388                 else
01389                 {
01390                         if (chroma_format!=CHROMA444)
01391                                 bxh >>= 1;
01392                         if (chroma_format==CHROMA420)
01393                                 byh >>= 1;
01394 
01395                         if (picture_structure==FRAME_PICTURE)
01396                         {
01397                                 if (dct_type && chroma_format!=CHROMA420)
01398                                 {
01399                                         /* field DCT coding */
01400                                         rfp = current_frame[cc] + Chroma_Width*(byh+((comp&2)>>1)) + bxh + (comp&8);
01401                                         iincr = Chroma_Width<<1;
01402                                 }
01403                                 else
01404                                 {
01405                                         /* frame DCT coding */
01406                                         rfp = current_frame[cc] + Chroma_Width*(byh+((comp&2)<<2)) + bxh + (comp&8);
01407                                         iincr = Chroma_Width;
01408                                 }
01409                         }
01410                         else
01411                         {
01412                                 /* field picture */
01413                                 rfp = current_frame[cc] + (Chroma_Width<<1)*(byh+((comp&2)<<2)) + bxh + (comp&8);
01414                                 iincr = Chroma_Width<<1;
01415                         }
01416                 }
01417 
01418                 if (addflag)
01419                 {
01420                         __asm
01421                         {
01422                                 pxor            mm0, mm0
01423                                 mov                     eax, [rfp]
01424                                 mov                     ebx, [Block_Ptr]
01425                                 mov                     edi, 8
01426 addon:
01427                                 movq            mm2, [ebx+8]
01428 
01429                                 movq            mm3, [eax]
01430                                 movq            mm4, mm3
01431 
01432                                 movq            mm1, [ebx]
01433                                 punpckhbw       mm3, mm0
01434 
01435                                 paddsw          mm3, mm2
01436                                 packuswb        mm3, mm0
01437 
01438                                 punpcklbw       mm4, mm0
01439                                 psllq           mm3, 32
01440 
01441                                 paddsw          mm4, mm1
01442                                 packuswb        mm4, mm0
01443 
01444                                 por                     mm3, mm4                        
01445                                 add                     ebx, 16
01446 
01447                                 dec                     edi
01448                                 movq            [eax], mm3
01449 
01450                                 add                     eax, [iincr]
01451                                 cmp                     edi, 0x00
01452                                 jg                      addon
01453                         }
01454                 }
01455                 else
01456                 {
01457                         __asm
01458                         {
01459                                 mov                     eax, [rfp]
01460                                 mov                     ebx, [Block_Ptr]
01461                                 mov                     edi, 8
01462 
01463                                 pxor            mm0, mm0
01464                                 movq            mm7, [mmmask_128]
01465 addoff:
01466                                 movq            mm3, [ebx+8]
01467                                 movq            mm4, [ebx]
01468 
01469                                 paddsw          mm3, mm7
01470                                 paddsw          mm4, mm7
01471 
01472                                 packuswb        mm3, mm0
01473                                 packuswb        mm4, mm0
01474 
01475                                 psllq           mm3, 32
01476                                 por                     mm3, mm4
01477                         
01478                                 add                     ebx, 16
01479                                 dec                     edi
01480 
01481                                 movq            [eax], mm3
01482 
01483                                 add                     eax, [iincr]
01484                                 cmp                     edi, 0x00
01485                                 jg                      addoff
01486                         }
01487                 }
01488         }
01489 }
01490 
01491 /* set scratch pad macroblock to zero */
01492 void CMPEG2Dec::Clear_Block(int count)
01493 {
01494         int comp;
01495         short *Block_Ptr;
01496 
01497         for (comp=0; comp<count; comp++)
01498         {
01499                 Block_Ptr = block[comp];
01500 
01501                 __asm
01502                 {
01503                         mov                     eax, [Block_Ptr];
01504                         pxor            mm0, mm0;
01505                         movq            [eax+0 ], mm0;
01506                         movq            [eax+8 ], mm0;
01507                         movq            [eax+16], mm0;
01508                         movq            [eax+24], mm0;
01509                         movq            [eax+32], mm0;
01510                         movq            [eax+40], mm0;
01511                         movq            [eax+48], mm0;
01512                         movq            [eax+56], mm0;
01513                         movq            [eax+64], mm0;
01514                         movq            [eax+72], mm0;
01515                         movq            [eax+80], mm0;
01516                         movq            [eax+88], mm0;
01517                         movq            [eax+96], mm0;
01518                         movq            [eax+104],mm0;
01519                         movq            [eax+112],mm0;
01520                         movq            [eax+120],mm0;
01521                 }
01522         }
01523 }
01524 
01525 /* ISO/IEC 13818-2 section 7.6 */
01526 void CMPEG2Dec::motion_compensation(int MBA, int macroblock_type, int motion_type, 
01527                                                                 int PMV[2][2][2], int motion_vertical_field_select[2][2],
01528                                                                 int dmvector[2], int dct_type)
01529 {
01530         int bx, by;
01531         int comp;
01532 
01533         /* derive current macroblock position within picture */
01534         /* ISO/IEC 13818-2 section 6.3.1.6 and 6.3.1.7 */
01535         bx = 16*(MBA%mb_width);
01536         by = 16*(MBA/mb_width);
01537 
01538         /* motion compensation */
01539         if (!(macroblock_type & MACROBLOCK_INTRA))
01540                 form_predictions(bx, by, macroblock_type, motion_type, PMV, 
01541                         motion_vertical_field_select, dmvector);
01542 
01543         switch (IDCT_Flag)
01544         {
01545                 case IDCT_MMX:
01546                         for (comp=0; comp<block_count; comp++)
01547                                 MMX_IDCT(block[comp]);
01548                         break;
01549 
01550                 case IDCT_SSEMMX:
01551                         for (comp=0; comp<block_count; comp++)
01552                                 SSEMMX_IDCT(block[comp]);
01553                         break;
01554 
01555                 case IDCT_FPU:
01556                         __asm emms;
01557                         for (comp=0; comp<block_count; comp++)
01558                                 FPU_IDCT(block[comp]);
01559                         break;
01560 
01561                 case IDCT_REF:
01562                         __asm emms;
01563                         for (comp=0; comp<block_count; comp++)
01564                                 REF_IDCT(block[comp]);
01565                         break;
01566         }
01567 
01568         Add_Block(block_count, bx, by, dct_type, (macroblock_type & MACROBLOCK_INTRA)==0);
01569 }
01570 
01571 /* ISO/IEC 13818-2 section 7.6.6 */
01572 void CMPEG2Dec::skipped_macroblock(int dc_dct_pred[3], int PMV[2][2][2], int *motion_type, 
01573                                                            int motion_vertical_field_select[2][2], int *macroblock_type)
01574 {
01575         Clear_Block(block_count);
01576 
01577         /* reset intra_dc predictors */
01578         /* ISO/IEC 13818-2 section 7.2.1: DC coefficients in intra blocks */
01579         dc_dct_pred[0]=dc_dct_pred[1]=dc_dct_pred[2]=0;
01580 
01581         /* reset motion vector predictors */
01582         /* ISO/IEC 13818-2 section 7.6.3.4: Resetting motion vector predictors */
01583         if (picture_coding_type==P_TYPE)
01584                 PMV[0][0][0]=PMV[0][0][1]=PMV[1][0][0]=PMV[1][0][1]=0;
01585 
01586         /* derive motion_type */
01587         if (picture_structure==FRAME_PICTURE)
01588                 *motion_type = MC_FRAME;
01589         else
01590         {
01591                 *motion_type = MC_FIELD;
01592                 motion_vertical_field_select[0][0] = motion_vertical_field_select[0][1] = 
01593                         (picture_structure==BOTTOM_FIELD);
01594         }
01595 
01596         /* clear MACROBLOCK_INTRA */
01597         *macroblock_type&= ~MACROBLOCK_INTRA;
01598 }
01599 
01600 /* return==-1 means go to next picture */
01601 /* the expression "start of slice" is used throughout the normative
01602    body of the MPEG specification */
01603 int CMPEG2Dec::start_of_slice(int *MBA, int *MBAinc,
01604                                                   int dc_dct_pred[3], int PMV[2][2][2])
01605 {
01606         unsigned int code;
01607         int slice_vert_pos_ext;
01608 
01609         next_start_code();
01610         code = Get_Bits(32);
01611 
01612         if (code<SLICE_START_CODE_MIN || code>SLICE_START_CODE_MAX)
01613         {
01614                 // only slice headers are allowed in picture_data
01615                 Fault_Flag = 10;
01616                 return -1;
01617         }
01618 
01619         /* decode slice header (may change quantizer_scale) */
01620         slice_vert_pos_ext = slice_header();
01621 
01622         /* decode macroblock address increment */
01623         *MBAinc = Get_macroblock_address_increment();
01624         if (Fault_Flag) return -1;
01625 
01626         /* set current location */
01627         /* NOTE: the arithmetic used to derive macroblock_address below is
01628            equivalent to ISO/IEC 13818-2 section 6.3.17: Macroblock */
01629         *MBA = ((slice_vert_pos_ext<<7) + (code&255) - 1)*mb_width + *MBAinc - 1;
01630         *MBAinc = 1;    // first macroblock in slice: not skipped
01631 
01632         /* reset all DC coefficient and motion vector predictors */
01633         /* ISO/IEC 13818-2 section 7.2.1: DC coefficients in intra blocks */
01634         dc_dct_pred[0]=dc_dct_pred[1]=dc_dct_pred[2]=0;
01635   
01636         /* ISO/IEC 13818-2 section 7.6.3.4: Resetting motion vector predictors */
01637         PMV[0][0][0]=PMV[0][0][1]=PMV[1][0][0]=PMV[1][0][1]=0;
01638         PMV[0][1][0]=PMV[0][1][1]=PMV[1][1][0]=PMV[1][1][1]=0;
01639 
01640         /* successfull: trigger decode macroblocks in slice */
01641         return 1;
01642 }
01643 
01644 /* ISO/IEC 13818-2 sections 7.2 through 7.5 */
01645 int CMPEG2Dec::decode_macroblock(int *macroblock_type, int *motion_type, int *dct_type,
01646                                                          int PMV[2][2][2], int dc_dct_pred[3], 
01647                                                          int motion_vertical_field_select[2][2], int dmvector[2])
01648 {
01649         int quantizer_scale_code, comp, motion_vector_count, mv_format; 
01650         int dmv, mvscale, coded_block_pattern;
01651 
01652         /* ISO/IEC 13818-2 section 6.3.17.1: Macroblock modes */
01653         macroblock_modes(macroblock_type, motion_type, &motion_vector_count, &mv_format,
01654                                          &dmv, &mvscale, dct_type);
01655         if (Fault_Flag) return 0;       // trigger: go to next slice
01656 
01657         if (*macroblock_type & MACROBLOCK_QUANT)
01658         {
01659                 quantizer_scale_code = Get_Bits(5);
01660 
01661                 /* ISO/IEC 13818-2 section 7.4.2.2: Quantizer scale factor */
01662                 quantizer_scale = q_scale_type ?
01663                 Non_Linear_quantizer_scale[quantizer_scale_code] : (quantizer_scale_code << 1);
01664         }
01665 
01666         /* ISO/IEC 13818-2 section 6.3.17.2: Motion vectors */
01667         /* decode forward motion vectors */
01668         if ((*macroblock_type & MACROBLOCK_MOTION_FORWARD) 
01669                 || ((*macroblock_type & MACROBLOCK_INTRA) && concealment_motion_vectors))
01670                 motion_vectors(PMV, dmvector, motion_vertical_field_select, 0,
01671                 motion_vector_count, mv_format, f_code[0][0]-1, f_code[0][1]-1, dmv, mvscale);
01672         if (Fault_Flag) return 0;       // trigger: go to next slice
01673 
01674         /* decode backward motion vectors */
01675         if (*macroblock_type & MACROBLOCK_MOTION_BACKWARD)
01676                 motion_vectors(PMV, dmvector, motion_vertical_field_select, 1,
01677                 motion_vector_count,mv_format, f_code[1][0]-1, f_code[1][1]-1, 0, mvscale);
01678         if (Fault_Flag) return 0;  // trigger: go to next slice
01679 
01680         if ((*macroblock_type & MACROBLOCK_INTRA) && concealment_motion_vectors)
01681                 Flush_Buffer(1);        // marker bit
01682 
01683         /* macroblock_pattern */
01684         /* ISO/IEC 13818-2 section 6.3.17.4: Coded block pattern */
01685         if (*macroblock_type & MACROBLOCK_PATTERN)
01686         {
01687                 coded_block_pattern = Get_coded_block_pattern();
01688 
01689                 if (chroma_format==CHROMA422)
01690                         coded_block_pattern = (coded_block_pattern<<2) | Get_Bits(2);
01691                 else if (chroma_format==CHROMA444)
01692                         coded_block_pattern = (coded_block_pattern<<6) | Get_Bits(6);
01693         }
01694         else
01695             coded_block_pattern = (*macroblock_type & MACROBLOCK_INTRA) ? (1<<block_count)-1 : 0;
01696 
01697         if (Fault_Flag) return 0;       // trigger: go to next slice
01698 
01699         Clear_Block(block_count);
01700 
01701         /* decode blocks */
01702         for (comp=0; comp<block_count; comp++)
01703         {
01704                 if (coded_block_pattern & (1<<(block_count-1-comp)))
01705                 {
01706                         if (*macroblock_type & MACROBLOCK_INTRA)
01707                                 Decode_MPEG2_Intra_Block(comp, dc_dct_pred);
01708                         else
01709                                 Decode_MPEG2_Non_Intra_Block(comp);
01710                         if (Fault_Flag) return 0;       // trigger: go to next slice
01711                 }
01712         }
01713 
01714         /* reset intra_dc predictors */
01715         /* ISO/IEC 13818-2 section 7.2.1: DC coefficients in intra blocks */
01716         if (!(*macroblock_type & MACROBLOCK_INTRA))
01717                 dc_dct_pred[0]=dc_dct_pred[1]=dc_dct_pred[2]=0;
01718 
01719         /* reset motion vector predictors */
01720         if ((*macroblock_type & MACROBLOCK_INTRA) && !concealment_motion_vectors)
01721         {
01722                 /* intra mb without concealment motion vectors */
01723                 /* ISO/IEC 13818-2 section 7.6.3.4: Resetting motion vector predictors */
01724                 PMV[0][0][0]=PMV[0][0][1]=PMV[1][0][0]=PMV[1][0][1]=0;
01725                 PMV[0][1][0]=PMV[0][1][1]=PMV[1][1][0]=PMV[1][1][1]=0;
01726         }
01727 
01728         /* special "No_MC" macroblock_type case */
01729         /* ISO/IEC 13818-2 section 7.6.3.5: Prediction in P pictures */
01730         if ((picture_coding_type==P_TYPE) 
01731                 && !(*macroblock_type & (MACROBLOCK_MOTION_FORWARD|MACROBLOCK_INTRA)))
01732         {
01733                 /* non-intra mb without forward mv in a P picture */
01734                 /* ISO/IEC 13818-2 section 7.6.3.4: Resetting motion vector predictors */
01735                 PMV[0][0][0]=PMV[0][0][1]=PMV[1][0][0]=PMV[1][0][1]=0;
01736 
01737                 /* derive motion_type */
01738                 /* ISO/IEC 13818-2 section 6.3.17.1: Macroblock modes, frame_motion_type */
01739                 if (picture_structure==FRAME_PICTURE)
01740                         *motion_type = MC_FRAME;
01741                 else
01742                 {
01743                         *motion_type = MC_FIELD;
01744                         motion_vertical_field_select[0][0] = (picture_structure==BOTTOM_FIELD);
01745                 }
01746         }
01747         /* successfully decoded macroblock */
01748         return 1 ;
01749 }
01750 
01751 /* decode one intra coded MPEG-2 block */
01752 void CMPEG2Dec::Decode_MPEG2_Intra_Block(int comp, int dc_dct_pred[])
01753 {
01754         int val, i, j, sign, *qmat;
01755         unsigned int code;
01756         DCTtab *tab;
01757         short *bp;
01758 
01759         bp = block[comp];
01760         qmat = (comp<4 || chroma_format==CHROMA420) 
01761                 ? intra_quantizer_matrix : chroma_intra_quantizer_matrix;
01762 
01763         /* ISO/IEC 13818-2 section 7.2.1: decode DC coefficients */
01764         switch (cc_table[comp])
01765         {
01766                 case 0:
01767                         val = (dc_dct_pred[0]+= Get_Luma_DC_dct_diff());
01768                         break;
01769 
01770                 case 1:
01771                         val = (dc_dct_pred[1]+= Get_Chroma_DC_dct_diff());
01772                         break;
01773 
01774                 case 2:
01775                         val = (dc_dct_pred[2]+= Get_Chroma_DC_dct_diff());
01776                         break;
01777         }
01778 
01779         bp[0] = val << (3-intra_dc_precision);
01780 
01781         /* decode AC coefficients */
01782         for (i=1; ; i++)
01783         {
01784                 code = Show_Bits(16);
01785 
01786                 if (code>=16384 && !intra_vlc_format)
01787                         tab = &DCTtabnext[(code>>12)-4];
01788                 else if (code>=1024)
01789                 {
01790                         if (intra_vlc_format)
01791                                 tab = &DCTtab0a[(code>>8)-4];
01792                         else
01793                                 tab = &DCTtab0[(code>>8)-4];
01794                 }
01795                 else if (code>=512)
01796                 {
01797                         if (intra_vlc_format)
01798                                 tab = &DCTtab1a[(code>>6)-8];
01799                         else
01800                                 tab = &DCTtab1[(code>>6)-8];
01801                 }
01802                 else if (code>=256)
01803                         tab = &DCTtab2[(code>>4)-16];
01804                 else if (code>=128)
01805                         tab = &DCTtab3[(code>>3)-16];
01806                 else if (code>=64)
01807                         tab = &DCTtab4[(code>>2)-16];
01808                 else if (code>=32)
01809                         tab = &DCTtab5[(code>>1)-16];
01810                 else if (code>=16)
01811                         tab = &DCTtab6[code-16];
01812                 else
01813                 {
01814                         Fault_Flag = 1;
01815                         return;
01816                 }
01817 
01818                 Flush_Buffer(tab->len);
01819 
01820                 if (tab->run<64)
01821                 {
01822                         i+= tab->run;
01823                         val = tab->level;
01824                         sign = Get_Bits(1);
01825                 }
01826                 else if (tab->run==64) /* end_of_block */
01827                         return;
01828                 else /* escape */
01829                 {
01830                         i+= Get_Bits(6);
01831                         val = Get_Bits(12);
01832 
01833                         if (sign = (val>=2048))
01834                                 val = 4096 - val;
01835                 }
01836 
01837                 j = scan[alternate_scan][i];
01838 
01839                 val = (val * quantizer_scale * qmat[j]) >> 4;
01840                 bp[j] = sign ? -val : val;
01841         }
01842 }
01843 
01844 /* decode one non-intra coded MPEG-2 block */
01845 void CMPEG2Dec::Decode_MPEG2_Non_Intra_Block(int comp)
01846 {
01847         int val, i, j, sign, *qmat;
01848         unsigned int code;
01849         DCTtab *tab;
01850         short *bp;
01851 
01852         bp = block[comp];
01853         qmat = (comp<4 || chroma_format==CHROMA420) 
01854                 ? non_intra_quantizer_matrix : chroma_non_intra_quantizer_matrix;
01855 
01856         /* decode AC coefficients */
01857         for (i=0; ; i++)
01858         {
01859                 code = Show_Bits(16);
01860 
01861                 if (code>=16384)
01862                 {
01863                         if (i==0)
01864                                 tab = &DCTtabfirst[(code>>12)-4];
01865                         else
01866                                 tab = &DCTtabnext[(code>>12)-4];
01867                 }
01868                 else if (code>=1024)
01869                         tab = &DCTtab0[(code>>8)-4];
01870                 else if (code>=512)
01871                         tab = &DCTtab1[(code>>6)-8];
01872                 else if (code>=256)
01873                         tab = &DCTtab2[(code>>4)-16];
01874                 else if (code>=128)
01875                         tab = &DCTtab3[(code>>3)-16];
01876                 else if (code>=64)
01877                         tab = &DCTtab4[(code>>2)-16];
01878                 else if (code>=32)
01879                         tab = &DCTtab5[(code>>1)-16];
01880                 else if (code>=16)
01881                         tab = &DCTtab6[code-16];
01882                 else
01883                 {
01884                         Fault_Flag = 1;
01885                         return;
01886                 }
01887 
01888                 Flush_Buffer(tab->len);
01889 
01890                 if (tab->run<64)
01891                 {
01892                         i+= tab->run;
01893                         val = tab->level;
01894                         sign = Get_Bits(1);
01895                 }
01896                 else if (tab->run==64) /* end_of_block */
01897                         return;
01898                 else /* escape */
01899                 {
01900                         i+= Get_Bits(6);
01901                         val = Get_Bits(12);
01902 
01903                         if (sign = (val>=2048))
01904                                 val = 4096 - val;
01905                 }
01906 
01907                 j = scan[alternate_scan][i];
01908 
01909                 val = (((val<<1)+1) * quantizer_scale * qmat[j]) >> 5;
01910                 bp[j] = sign ? -val : val;
01911         }
01912 }
01913 
01914 int CMPEG2Dec::Get_macroblock_type()
01915 {
01916         int macroblock_type;
01917 
01918         switch (picture_coding_type)
01919         {
01920                 case I_TYPE:
01921                         macroblock_type = Get_I_macroblock_type();
01922                         break;
01923 
01924                 case P_TYPE:
01925                         macroblock_type = Get_P_macroblock_type();
01926                         break;
01927 
01928                 case B_TYPE:
01929                         macroblock_type = Get_B_macroblock_type();
01930                         break;
01931         }
01932 
01933         return macroblock_type;
01934 }
01935 
01936 int CMPEG2Dec::Get_I_macroblock_type()
01937 {
01938         if (Get_Bits(1))
01939                 return 1;
01940 
01941         if (!Get_Bits(1))
01942                 Fault_Flag = 2;
01943 
01944         return 17;
01945 }
01946 
01947 int CMPEG2Dec::Get_P_macroblock_type()
01948 {
01949         int code;
01950 
01951         if ((code = Show_Bits(6))>=8)
01952         {
01953                 code >>= 3;
01954                 Flush_Buffer(PMBtab0[code].len);
01955 
01956                 return PMBtab0[code].val;
01957         }
01958 
01959         if (code==0)
01960         {
01961                 Fault_Flag = 2;
01962                 return 0;
01963         }
01964 
01965         Flush_Buffer(PMBtab1[code].len);
01966 
01967         return PMBtab1[code].val;
01968 }
01969 
01970 int CMPEG2Dec::Get_B_macroblock_type()
01971 {
01972         int code;
01973 
01974         if ((code = Show_Bits(6))>=8)
01975         {
01976                 code >>= 2;
01977                 Flush_Buffer(BMBtab0[code].len);
01978 
01979                 return BMBtab0[code].val;
01980         }
01981 
01982         if (code==0)
01983         {
01984                 Fault_Flag = 2;
01985                 return 0;
01986         }
01987 
01988         Flush_Buffer(BMBtab1[code].len);
01989 
01990         return BMBtab1[code].val;
01991 }
01992 
01993 int CMPEG2Dec::Get_coded_block_pattern()
01994 {
01995         int code;
01996 
01997         if ((code = Show_Bits(9))>=128)
01998         {
01999                 code >>= 4;
02000                 Flush_Buffer(CBPtab0[code].len);
02001 
02002                 return CBPtab0[code].val;
02003         }
02004 
02005         if (code>=8)
02006         {
02007                 code >>= 1;
02008                 Flush_Buffer(CBPtab1[code].len);
02009 
02010                 return CBPtab1[code].val;
02011         }
02012 
02013         if (code<1)
02014         {
02015                 Fault_Flag = 3;
02016                 return 0;
02017         }
02018 
02019         Flush_Buffer(CBPtab2[code].len);
02020 
02021         return CBPtab2[code].val;
02022 }
02023 
02024 int CMPEG2Dec::Get_macroblock_address_increment()
02025 {
02026         int code, val;
02027 
02028         val = 0;
02029 
02030         while ((code = Show_Bits(11))<24)
02031         {
02032                 if (code!=15) /* if not macroblock_stuffing */
02033                 {
02034                         if (code==8) /* if macroblock_escape */
02035                                 val+= 33;
02036                         else
02037                         {
02038                                 Fault_Flag = 4;
02039                                 return 1;
02040                         }
02041                 }
02042                 Flush_Buffer(11);
02043         }
02044 
02045         /* macroblock_address_increment == 1 */
02046         /* ('1' is in the MSB position of the lookahead) */
02047         if (code>=1024)
02048         {
02049                 Flush_Buffer(1);
02050                 return val + 1;
02051         }
02052 
02053         /* codes 00010 ... 011xx */
02054         if (code>=128)
02055         {
02056                 /* remove leading zeros */
02057                 code >>= 6;
02058                 Flush_Buffer(MBAtab1[code].len);
02059     
02060                 return val + MBAtab1[code].val;
02061         }
02062   
02063         /* codes 00000011000 ... 0000111xxxx */
02064         code-= 24; /* remove common base */
02065         Flush_Buffer(MBAtab2[code].len);
02066 
02067         return val + MBAtab2[code].val;
02068 }
02069 
02070 /*
02071    parse VLC and perform dct_diff arithmetic.
02072    MPEG-2:  ISO/IEC 13818-2 section 7.2.1 
02073 
02074    Note: the arithmetic here is presented more elegantly than
02075    the spec, yet the results, dct_diff, are the same.
02076 */
02077 int CMPEG2Dec::Get_Luma_DC_dct_diff()
02078 {
02079         int code, size, dct_diff;
02080 
02081         /* decode length */
02082         code = Show_Bits(5);
02083 
02084         if (code<31)
02085         {
02086                 size = DClumtab0[code].val;
02087                 Flush_Buffer(DClumtab0[code].len);
02088         }
02089         else
02090         {
02091                 code = Show_Bits(9) - 0x1f0;
02092                 size = DClumtab1[code].val;
02093                 Flush_Buffer(DClumtab1[code].len);
02094         }
02095 
02096         if (size==0)
02097                 dct_diff = 0;
02098         else
02099         {
02100                 dct_diff = Get_Bits(size);
02101 
02102                 if ((dct_diff & (1<<(size-1)))==0)
02103                         dct_diff-= (1<<size) - 1;
02104         }
02105 
02106         return dct_diff;
02107 }
02108 
02109 int CMPEG2Dec::Get_Chroma_DC_dct_diff()
02110 {
02111         int code, size, dct_diff;
02112 
02113         /* decode length */
02114         code = Show_Bits(5);
02115 
02116         if (code<31)
02117         {
02118                 size = DCchromtab0[code].val;
02119                 Flush_Buffer(DCchromtab0[code].len);
02120         }
02121         else
02122         {
02123                 code = Show_Bits(10) - 0x3e0;
02124                 size = DCchromtab1[code].val;
02125                 Flush_Buffer(DCchromtab1[code].len);
02126         }
02127 
02128         if (size==0)
02129                 dct_diff = 0;
02130         else
02131         {
02132                 dct_diff = Get_Bits(size);
02133 
02134                 if ((dct_diff & (1<<(size-1)))==0)
02135                         dct_diff-= (1<<size) - 1;
02136         }
02137 
02138         return dct_diff;
02139 }
02140 
02141 /*
02142 static int currentfield;
02143 static unsigned char **predframe;
02144 static int DMV[2][2];
02145 static int stw;
02146 */
02147 
02148 void CMPEG2Dec::form_predictions(int bx, int by, int macroblock_type, int motion_type,
02149                                           int PMV[2][2][2], int motion_vertical_field_select[2][2],
02150                                           int dmvector[2])
02151 {
02152         int currentfield;
02153         unsigned char **predframe;
02154         int DMV[2][2];
02155         int stw;
02156         
02157         stw = 0;
02158 
02159         if ((macroblock_type & MACROBLOCK_MOTION_FORWARD) || (picture_coding_type==P_TYPE))
02160         {
02161                 if (picture_structure==FRAME_PICTURE)
02162                 {
02163                         if ((motion_type==MC_FRAME) || !(macroblock_type & MACROBLOCK_MOTION_FORWARD))
02164                         {
02165                                 /* frame-based prediction (broken into top and bottom halves
02166                                    for spatial scalability prediction purposes) */
02167                                 form_prediction(forward_reference_frame, 0, current_frame, 0, Coded_Picture_Width, 
02168                                         Coded_Picture_Width<<1, 16, 8, bx, by, PMV[0][0][0], PMV[0][0][1], stw);
02169 
02170                                 form_prediction(forward_reference_frame, 1, current_frame, 1, Coded_Picture_Width, 
02171                                         Coded_Picture_Width<<1, 16, 8, bx, by, PMV[0][0][0], PMV[0][0][1], stw);
02172                         }
02173                         else if (motion_type==MC_FIELD) /* field-based prediction */
02174                         {
02175                                 /* top field prediction */
02176                                 form_prediction(forward_reference_frame, motion_vertical_field_select[0][0], 
02177                                         current_frame, 0, Coded_Picture_Width<<1, Coded_Picture_Width<<1, 16, 8,
02178                                         bx, by>>1, PMV[0][0][0], PMV[0][0][1]>>1, stw);
02179 
02180                                 /* bottom field prediction */
02181                                 form_prediction(forward_reference_frame, motion_vertical_field_select[1][0], 
02182                                         current_frame, 1, Coded_Picture_Width<<1, Coded_Picture_Width<<1, 16, 8,
02183                                         bx, by>>1, PMV[1][0][0], PMV[1][0][1]>>1, stw);
02184                         }
02185                         else if (motion_type==MC_DMV) /* dual prime prediction */
02186                         {
02187                                 /* calculate derived motion vectors */
02188                                 Dual_Prime_Arithmetic(DMV, dmvector, PMV[0][0][0], PMV[0][0][1]>>1);
02189 
02190                                 /* predict top field from top field */
02191                                 form_prediction(forward_reference_frame, 0, current_frame, 0, 
02192                                         Coded_Picture_Width<<1, Coded_Picture_Width<<1, 16, 8, bx, by>>1,
02193                                         PMV[0][0][0], PMV[0][0][1]>>1, 0);
02194 
02195                                 /* predict and add to top field from bottom field */
02196                                 form_prediction(forward_reference_frame, 1, current_frame, 0,
02197                                         Coded_Picture_Width<<1, Coded_Picture_Width<<1, 16, 8, bx, by>>1,
02198                                         DMV[0][0], DMV[0][1], 1);
02199 
02200                                 /* predict bottom field from bottom field */
02201                                 form_prediction(forward_reference_frame, 1, current_frame, 1,
02202                                         Coded_Picture_Width<<1, Coded_Picture_Width<<1, 16, 8, bx, by>>1,
02203                                         PMV[0][0][0], PMV[0][0][1]>>1, 0);
02204 
02205                                 /* predict and add to bottom field from top field */
02206                                 form_prediction(forward_reference_frame, 0, current_frame, 1,
02207                                         Coded_Picture_Width<<1, Coded_Picture_Width<<1, 16, 8, bx, by>>1,
02208                                         DMV[1][0], DMV[1][1], 1);
02209                         }
02210                         else
02211                                 Fault_Flag = 5;
02212                 }
02213                 else
02214                 {
02215                         /* field picture */
02216                         currentfield = (picture_structure==BOTTOM_FIELD);
02217 
02218                         /* determine which frame to use for prediction */
02219                         if (picture_coding_type==P_TYPE && Second_Field && currentfield!=motion_vertical_field_select[0][0])
02220                                 predframe = backward_reference_frame;
02221                         else
02222                                 predframe = forward_reference_frame;
02223 
02224                         if ((motion_type==MC_FIELD) || !(macroblock_type & MACROBLOCK_MOTION_FORWARD))
02225                         {
02226                                 form_prediction(predframe, motion_vertical_field_select[0][0], current_frame, 0, 
02227                                         Coded_Picture_Width<<1, Coded_Picture_Width<<1, 16, 16, bx, by,
02228                                         PMV[0][0][0], PMV[0][0][1], stw);
02229                         }
02230                         else if (motion_type==MC_16X8)
02231                         {
02232                                 form_prediction(predframe, motion_vertical_field_select[0][0], current_frame, 0, 
02233                                         Coded_Picture_Width<<1, Coded_Picture_Width<<1, 16, 8, bx, by,
02234                                         PMV[0][0][0], PMV[0][0][1], stw);
02235 
02236                                 if (picture_coding_type==P_TYPE && Second_Field && currentfield!=motion_vertical_field_select[1][0])
02237                                         predframe = backward_reference_frame;
02238                                 else
02239                                         predframe = forward_reference_frame;
02240 
02241                                 form_prediction(predframe, motion_vertical_field_select[1][0], current_frame, 
02242                                         0, Coded_Picture_Width<<1, Coded_Picture_Width<<1, 16, 8, bx, by+8,
02243                                         PMV[1][0][0], PMV[1][0][1], stw);
02244                         }
02245                         else if (motion_type==MC_DMV)
02246                         {
02247                                 if (Second_Field)
02248                                         predframe = backward_reference_frame;
02249                                 else
02250                                         predframe = forward_reference_frame;
02251 
02252                                 /* calculate derived motion vectors */
02253                                 Dual_Prime_Arithmetic(DMV, dmvector, PMV[0][0][0], PMV[0][0][1]);
02254 
02255                                 /* predict from field of same parity */
02256                                 form_prediction(forward_reference_frame, currentfield, current_frame, 0, 
02257                                         Coded_Picture_Width<<1, Coded_Picture_Width<<1, 16, 16, bx, by,
02258                                         PMV[0][0][0], PMV[0][0][1], 0);
02259 
02260                                 /* predict from field of opposite parity */
02261                                 form_prediction(predframe, !currentfield, current_frame, 0,
02262                                         Coded_Picture_Width<<1, Coded_Picture_Width<<1, 16, 16, bx, by,
02263                                         DMV[0][0], DMV[0][1], 1);
02264                         }
02265                         else
02266                                 Fault_Flag = 5;
02267                 }
02268 
02269                 stw = 1;
02270         }
02271 
02272         if (macroblock_type & MACROBLOCK_MOTION_BACKWARD)
02273         {
02274                 if (picture_structure==FRAME_PICTURE)
02275                 {
02276                         if (motion_type==MC_FRAME)
02277                         {
02278                                 /* frame-based prediction */
02279                                 form_prediction(backward_reference_frame, 0, current_frame, 0,
02280                                         Coded_Picture_Width, Coded_Picture_Width<<1, 16, 8, bx, by,
02281                                         PMV[0][1][0], PMV[0][1][1], stw);
02282 
02283                                 form_prediction(backward_reference_frame, 1, current_frame, 1,
02284                                         Coded_Picture_Width, Coded_Picture_Width<<1, 16, 8, bx, by,
02285                                         PMV[0][1][0], PMV[0][1][1], stw);
02286                         }
02287                         else /* field-based prediction */
02288                         {
02289                                 /* top field prediction */
02290                                 form_prediction(backward_reference_frame, motion_vertical_field_select[0][1], 
02291                                         current_frame, 0, Coded_Picture_Width<<1, Coded_Picture_Width<<1, 16, 8,
02292                                         bx, by>>1, PMV[0][1][0], PMV[0][1][1]>>1, stw);
02293 
02294                                 /* bottom field prediction */
02295                                 form_prediction(backward_reference_frame, motion_vertical_field_select[1][1], 
02296                                         current_frame, 1, Coded_Picture_Width<<1, Coded_Picture_Width<<1, 16, 8,
02297                                         bx, by>>1, PMV[1][1][0], PMV[1][1][1]>>1, stw);
02298                         }
02299                 }
02300                 else
02301                 {
02302                         /* field picture */
02303                         if (motion_type==MC_FIELD)
02304                         {
02305                                 /* field-based prediction */
02306                                 form_prediction(backward_reference_frame, motion_vertical_field_select[0][1], 
02307                                         current_frame, 0, Coded_Picture_Width<<1, Coded_Picture_Width<<1, 16, 16,
02308                                         bx, by, PMV[0][1][0], PMV[0][1][1], stw);
02309                         }
02310                         else if (motion_type==MC_16X8)
02311                         {
02312                                 form_prediction(backward_reference_frame, motion_vertical_field_select[0][1],
02313                                         current_frame, 0, Coded_Picture_Width<<1, Coded_Picture_Width<<1, 16, 8,
02314                                         bx, by, PMV[0][1][0], PMV[0][1][1], stw);
02315 
02316                                 form_prediction(backward_reference_frame, motion_vertical_field_select[1][1],
02317                                         current_frame, 0, Coded_Picture_Width<<1, Coded_Picture_Width<<1, 16, 8,
02318                                         bx, by+8, PMV[1][1][0], PMV[1][1][1], stw);
02319                         }
02320                         else
02321                                 Fault_Flag = 5;
02322                 }
02323         }
02324 }
02325 
02326 void CMPEG2Dec::form_prediction(unsigned char *src[], int sfield, unsigned char *dst[],
02327                                                         int dfield, int lx, int lx2, int w, int h, int x, int y,
02328                                                         int dx, int dy, int average_flag)
02329 {
02330         form_component_prediction(src[0]+(sfield?lx2>>1:0), dst[0]+(dfield?lx2>>1:0),
02331                 lx, lx2, w, h, x, y, dx, dy, average_flag);
02332 
02333         if (chroma_format!=CHROMA444)
02334         {
02335                 lx>>=1; lx2>>=1; w>>=1; x>>=1; dx/=2;
02336         }
02337 
02338         if (chroma_format==CHROMA420)
02339         {
02340                 h>>=1; y>>=1; dy/=2;
02341         }
02342 
02343         /* Cb */
02344         form_component_prediction(src[1]+(sfield?lx2>>1:0), dst[1]+(dfield?lx2>>1:0),
02345                 lx, lx2, w, h, x, y, dx, dy, average_flag);
02346 
02347         /* Cr */
02348         form_component_prediction(src[2]+(sfield?lx2>>1:0), dst[2]+(dfield?lx2>>1:0),
02349                 lx, lx2, w, h, x, y, dx, dy, average_flag);
02350 }
02351 
02352 /* ISO/IEC 13818-2 section 7.6.4: Forming predictions */
02353 void CMPEG2Dec::form_component_prediction(unsigned char *src, unsigned char *dst,
02354                                                                                   int lx, int lx2, int w, int h, int x, int y,
02355                                                                                   int dx, int dy, int average_flag)
02356 {
02357         static const __int64 mmmask_0001 = 0x0001000100010001;
02358         static const __int64 mmmask_0002 = 0x0002000200020002;
02359         static const __int64 mmmask_0003 = 0x0003000300030003;
02360         static const __int64 mmmask_0006 = 0x0006000600060006;
02361 
02362         unsigned char *s = src + lx * (y + (dy>>1)) + x + (dx>>1);
02363         unsigned char *d = dst + lx * y + x;
02364         int flag = (average_flag<<2) + ((dx & 1)<<1) + (dy & 1);
02365 
02366         switch (flag)
02367         {
02368                 case 0:
02369                         // d[i] = s[i];
02370                         __asm
02371                         {
02372                                 mov                     eax, [s]
02373                                 mov                     ebx, [d]
02374                                 mov                     esi, 0x00
02375                                 mov                     edi, [h]
02376 mc0:
02377                                 movq            mm1, [eax+esi]
02378                                 add                     esi, 0x08
02379                                 cmp                     esi, [w]
02380                                 movq            [ebx+esi-8], mm1
02381                                 jl                      mc0
02382 
02383                                 add                     eax, [lx2]
02384                                 add                     ebx, [lx2]
02385                                 dec                     edi
02386                                 mov                     esi, 0x00
02387                                 cmp                     edi, 0x00
02388                                 jg                      mc0
02389                         }
02390                         break;
02391 
02392                 case 1:
02393                         // d[i] = (s[i]+s[i+lx]+1)>>1;
02394                         __asm
02395                         {
02396                                 pxor            mm0, mm0
02397                                 movq            mm7, [mmmask_0001]
02398                                 mov                     eax, [s]
02399                                 mov                     ebx, [d]
02400                                 mov                     ecx, eax
02401                                 add                     ecx, [lx]
02402                                 mov                     esi, 0x00
02403                                 mov                     edi, [h]
02404 mc1:
02405                                 movq            mm1, [eax+esi]
02406                                 movq            mm2, [ecx+esi]
02407 
02408                                 movq            mm3, mm1
02409                                 movq            mm4, mm2
02410 
02411                                 punpcklbw       mm1, mm0
02412                                 punpckhbw       mm3, mm0
02413                                 punpcklbw       mm2, mm0
02414                                 punpckhbw       mm4, mm0
02415 
02416                                 paddsw          mm1, mm2
02417                                 paddsw          mm3, mm4
02418 
02419                                 paddsw          mm1, mm7
02420                                 paddsw          mm3, mm7
02421 
02422                                 psrlw           mm1, 1
02423                                 psrlw           mm3, 1
02424 
02425                                 packuswb        mm1, mm0
02426                                 packuswb        mm3, mm0
02427 
02428                                 psllq           mm3, 32
02429                                 por                     mm1, mm3
02430 
02431                                 add                     esi, 0x08
02432                                 cmp                     esi, [w]
02433                                 movq            [ebx+esi-8], mm1
02434                                 jl                      mc1
02435 
02436                                 add                     eax, [lx2]
02437                                 add                     ebx, [lx2]
02438                                 add                     ecx, [lx2]
02439                                 dec                     edi
02440                                 mov                     esi, 0x00
02441                                 cmp                     edi, 0x00
02442                                 jg                      mc1
02443                         }
02444                         break;
02445 
02446                 case 2:
02447                         // d[i] = (s[i]+s[i+1]+1)>>1;
02448                         __asm
02449                         {
02450                                 pxor            mm0, mm0
02451                                 movq            mm7, [mmmask_0001]
02452                                 mov                     eax, [s]
02453                                 mov                     ebx, [d]
02454                                 mov                     esi, 0x00
02455                                 mov                     edi, [h]
02456 mc2:
02457                                 movq            mm1, [eax+esi]
02458                                 movq            mm2, [eax+esi+1]
02459 
02460                                 movq            mm3, mm1
02461                                 movq            mm4, mm2
02462 
02463                                 punpcklbw       mm1, mm0
02464                                 punpckhbw       mm3, mm0
02465 
02466                                 punpcklbw       mm2, mm0
02467                                 punpckhbw       mm4, mm0
02468 
02469                                 paddsw          mm1, mm2
02470                                 paddsw          mm3, mm4
02471 
02472                                 paddsw          mm1, mm7
02473                                 paddsw          mm3, mm7
02474 
02475                                 psrlw           mm1, 1
02476                                 psrlw           mm3, 1
02477 
02478                                 packuswb        mm1, mm0
02479                                 packuswb        mm3, mm0
02480 
02481                                 psllq           mm3, 32
02482                                 por                     mm1, mm3
02483 
02484                                 add                     esi, 0x08
02485                                 cmp                     esi, [w]
02486                                 movq            [ebx+esi-8], mm1
02487                                 jl                      mc2
02488 
02489                                 add                     eax, [lx2]
02490                                 add                     ebx, [lx2]
02491                                 dec                     edi
02492                                 mov                     esi, 0x00
02493                                 cmp                     edi, 0x00
02494                                 jg                      mc2
02495                         }
02496                         break;
02497 
02498                 case 3:
02499                         // d[i] = (s[i]+s[i+1]+s[i+lx]+s[i+lx+1]+2)>>2;
02500                         __asm
02501                         {
02502                                 pxor            mm0, mm0
02503                                 movq            mm7, [mmmask_0002]
02504                                 mov                     eax, [s]
02505                                 mov                     ebx, [d]
02506                                 mov                     ecx, eax
02507                                 add                     ecx, [lx]
02508                                 mov                     esi, 0x00
02509                                 mov                     edi, [h]
02510 mc3:
02511                                 movq            mm1, [eax+esi]
02512                                 movq            mm2, [eax+esi+1]
02513                                 movq            mm3, mm1
02514                                 movq            mm4, mm2
02515 
02516                                 punpcklbw       mm1, mm0
02517                                 punpckhbw       mm3, mm0
02518 
02519                                 punpcklbw       mm2, mm0
02520                                 punpckhbw       mm4, mm0
02521 
02522                                 paddsw          mm1, mm2
02523                                 paddsw          mm3, mm4
02524 
02525                                 movq            mm5, [ecx+esi]
02526                                 paddsw          mm1, mm7
02527 
02528                                 movq            mm6, [ecx+esi+1]
02529                                 paddsw          mm3, mm7
02530 
02531                                 movq            mm2, mm5
02532                                 movq            mm4, mm6
02533 
02534                                 punpcklbw       mm2, mm0
02535                                 punpckhbw       mm5, mm0
02536 
02537                                 punpcklbw       mm4, mm0
02538                                 punpckhbw       mm6, mm0
02539                                 
02540                                 paddsw          mm2, mm4
02541                                 paddsw          mm5, mm6
02542 
02543                                 paddsw          mm1, mm2
02544                                 paddsw          mm3, mm5
02545 
02546                                 psrlw           mm1, 2
02547                                 psrlw           mm3, 2
02548 
02549                                 packuswb        mm1, mm0
02550                                 packuswb        mm3, mm0
02551 
02552                                 psllq           mm3, 32
02553                                 por                     mm1, mm3
02554 
02555                                 add                     esi, 0x08
02556                                 cmp                     esi, [w]
02557                                 movq            [ebx+esi-8], mm1
02558                                 jl                      mc3
02559 
02560                                 add                     eax, [lx2]
02561                                 add                     ebx, [lx2]
02562                                 add                     ecx, [lx2]
02563                                 dec                     edi
02564                                 mov                     esi, 0x00
02565                                 cmp                     edi, 0x00
02566                                 jg                      mc3
02567                         }
02568                         break;
02569 
02570                 case 4:
02571                         // d[i] = (s[i]+d[i]+1)>>1;
02572                         __asm
02573                         {
02574                                 pxor            mm0, mm0
02575                                 movq            mm7, [mmmask_0001]
02576                                 mov                     eax, [s]
02577                                 mov                     ebx, [d]
02578                                 mov                     esi, 0x00
02579                                 mov                     edi, [h]
02580 mc4:
02581                                 movq            mm1, [eax+esi]
02582                                 movq            mm2, [ebx+esi]
02583                                 movq            mm3, mm1
02584                                 movq            mm4, mm2
02585 
02586                                 punpcklbw       mm1, mm0
02587                                 punpckhbw       mm3, mm0
02588 
02589                                 punpcklbw       mm2, mm0
02590                                 punpckhbw       mm4, mm0
02591 
02592                                 paddsw          mm1, mm2
02593                                 paddsw          mm3, mm4
02594 
02595                                 paddsw          mm1, mm7
02596                                 paddsw          mm3, mm7
02597 
02598                                 psrlw           mm1, 1
02599                                 psrlw           mm3, 1
02600 
02601                                 packuswb        mm1, mm0
02602                                 packuswb        mm3, mm0
02603 
02604                                 psllq           mm3, 32
02605                                 por                     mm1, mm3
02606 
02607                                 add                     esi, 0x08
02608                                 cmp                     esi, [w]
02609                                 movq            [ebx+esi-8], mm1
02610                                 jl                      mc4
02611 
02612                                 add                     eax, [lx2]
02613                                 add                     ebx, [lx2]
02614                                 dec                     edi
02615                                 mov                     esi, 0x00
02616                                 cmp                     edi, 0x00
02617                                 jg                      mc4
02618                         }
02619                         break;
02620 
02621                 case 5:
02622                         // d[i] = ((d[i]<<1) + s[i]+s[i+lx] + 3)>>2;
02623                         __asm
02624                         {
02625                                 pxor            mm0, mm0
02626                                 movq            mm7, [mmmask_0003]
02627                                 mov                     eax, [s]
02628                                 mov                     ebx, [d]
02629                                 mov                     ecx, eax
02630                                 add                     ecx, [lx]
02631                                 mov                     esi, 0x00
02632                                 mov                     edi, [h]
02633 mc5:
02634                                 movq            mm1, [eax+esi]
02635                                 movq            mm2, [ecx+esi]
02636                                 movq            mm3, mm1
02637                                 movq            mm4, mm2
02638 
02639                                 punpcklbw       mm1, mm0
02640                                 punpckhbw       mm3, mm0
02641 
02642                                 punpcklbw       mm2, mm0
02643                                 punpckhbw       mm4, mm0
02644 
02645                                 paddsw          mm1, mm2
02646                                 paddsw          mm3, mm4
02647 
02648                                 movq            mm5, [ebx+esi]
02649 
02650                                 paddsw          mm1, mm7
02651                                 paddsw          mm3, mm7
02652 
02653                                 movq            mm6, mm5
02654                                 punpcklbw       mm5, mm0
02655                                 punpckhbw       mm6, mm0
02656 
02657                                 psllw           mm5, 1
02658                                 psllw           mm6, 1
02659 
02660                                 paddsw          mm1, mm5
02661                                 paddsw          mm3, mm6
02662 
02663                                 psrlw           mm1, 2
02664                                 psrlw           mm3, 2
02665 
02666                                 packuswb        mm1, mm0
02667                                 packuswb        mm3, mm0
02668 
02669                                 psllq           mm3, 32
02670                                 por                     mm1, mm3
02671 
02672                                 add                     esi, 0x08
02673                                 cmp                     esi, [w]
02674                                 movq            [ebx+esi-8], mm1
02675                                 jl                      mc5
02676 
02677                                 add                     eax, [lx2]
02678                                 add                     ebx, [lx2]
02679                                 add                     ecx, [lx2]
02680                                 dec                     edi
02681                                 mov                     esi, 0x00
02682                                 cmp                     edi, 0x00
02683                                 jg                      mc5
02684                         }
02685                         break;
02686 
02687                 case 6:
02688                         // d[i] = ((d[i]<<1) + s[i]+s[i+1] + 3) >> 2;
02689                         __asm
02690                         {
02691                                 pxor            mm0, mm0
02692                                 movq            mm7, [mmmask_0003]
02693                                 mov                     eax, [s]
02694                                 mov                     ebx, [d]
02695                                 mov                     esi, 0x00
02696                                 mov                     edi, [h]
02697 mc6:
02698                                 movq            mm1, [eax+esi]
02699                                 movq            mm2, [eax+esi+1]
02700                                 movq            mm3, mm1
02701                                 movq            mm4, mm2
02702 
02703                                 punpcklbw       mm1, mm0
02704                                 punpckhbw       mm3, mm0
02705 
02706                                 punpcklbw       mm2, mm0
02707                                 punpckhbw       mm4, mm0
02708 
02709                                 paddsw          mm1, mm2
02710                                 paddsw          mm3, mm4
02711 
02712                                 movq            mm5, [ebx+esi]
02713 
02714                                 paddsw          mm1, mm7
02715                                 paddsw          mm3, mm7
02716 
02717                                 movq            mm6, mm5
02718                                 punpcklbw       mm5, mm0
02719                                 punpckhbw       mm6, mm0
02720 
02721                                 psllw           mm5, 1
02722                                 psllw           mm6, 1
02723 
02724                                 paddsw          mm1, mm5
02725                                 paddsw          mm3, mm6
02726 
02727                                 psrlw           mm1, 2
02728                                 psrlw           mm3, 2
02729 
02730                                 packuswb        mm1, mm0
02731                                 packuswb        mm3, mm0
02732 
02733                                 psllq           mm3, 32
02734                                 por                     mm1, mm3
02735 
02736                                 add                     esi, 0x08
02737                                 cmp                     esi, [w]
02738                                 movq            [ebx+esi-8], mm1
02739                                 jl                      mc6
02740 
02741                                 add                     eax, [lx2]
02742                                 add                     ebx, [lx2]
02743                                 dec                     edi
02744                                 mov                     esi, 0x00
02745                                 cmp                     edi, 0x00
02746                                 jg                      mc6
02747                         }
02748                         break;
02749 
02750                 case 7:
02751                         // d[i] = ((d[i]<<2) + s[i]+s[i+1]+s[i+lx]+s[i+lx+1] + 6)>>3;
02752                         __asm
02753                         {
02754                                 pxor            mm0, mm0
02755                                 movq            mm7, [mmmask_0006]
02756                                 mov                     eax, [s]
02757                                 mov                     ebx, [d]
02758                                 mov                     ecx, eax
02759                                 add                     ecx, [lx]
02760                                 mov                     esi, 0x00
02761                                 mov                     edi, [h]
02762 mc7:
02763                                 movq            mm1, [eax+esi]
02764                                 movq            mm2, [eax+esi+1]
02765                                 movq            mm3, mm1
02766                                 movq            mm4, mm2
02767 
02768                                 punpcklbw       mm1, mm0
02769                                 punpckhbw       mm3, mm0
02770 
02771                                 punpcklbw       mm2, mm0
02772                                 punpckhbw       mm4, mm0
02773 
02774                                 paddsw          mm1, mm2
02775                                 paddsw          mm3, mm4
02776 
02777                                 movq            mm5, [ecx+esi]
02778                                 paddsw          mm1, mm7
02779 
02780                                 movq            mm6, [ecx+esi+1]
02781                                 paddsw          mm3, mm7
02782 
02783                                 movq            mm2, mm5
02784                                 movq            mm4, mm6
02785 
02786                                 punpcklbw       mm2, mm0
02787                                 punpckhbw       mm5, mm0
02788 
02789                                 punpcklbw       mm4, mm0
02790                                 punpckhbw       mm6, mm0
02791                                 
02792                                 paddsw          mm2, mm4
02793                                 paddsw          mm5, mm6
02794 
02795                                 paddsw          mm1, mm2
02796                                 paddsw          mm3, mm5
02797 
02798                                 movq            mm6, [ebx+esi]
02799 
02800                                 movq            mm4, mm6
02801                                 punpcklbw       mm4, mm0
02802                                 punpckhbw       mm6, mm0
02803 
02804                                 psllw           mm4, 2
02805                                 psllw           mm6, 2
02806 
02807                                 paddsw          mm1, mm4
02808                                 paddsw          mm3, mm6
02809 
02810                                 psrlw           mm1, 3
02811                                 psrlw           mm3, 3
02812 
02813                                 packuswb        mm1, mm0
02814                                 packuswb        mm3, mm0
02815 
02816                                 psllq           mm3, 32
02817                                 por                     mm1, mm3
02818 
02819                                 add                     esi, 0x08
02820                                 cmp                     esi, [w]
02821                                 movq            [ebx+esi-8], mm1
02822                                 jl                      mc7
02823 
02824                                 add                     eax, [lx2]
02825                                 add                     ebx, [lx2]
02826                                 add                     ecx, [lx2]
02827                                 dec                     edi
02828                                 mov                     esi, 0x00
02829                                 cmp                     edi, 0x00
02830                                 jg                      mc7
02831                         }
02832                         break;
02833         }
02834 }
02835 
02836 //
02837 // motion
02838 //
02839 
02840 /* ISO/IEC 13818-2 sections 6.2.5.2, 6.3.17.2, and 7.6.3: Motion vectors */
02841 void CMPEG2Dec::motion_vectors(int PMV[2][2][2],int dmvector[2],
02842                                         int motion_vertical_field_select[2][2], int s,
02843                                         int motion_vector_count, int mv_format, int h_r_size,
02844                                         int v_r_size, int dmv, int mvscale)
02845 {
02846         if (motion_vector_count==1)
02847         {
02848                 if (mv_format==MV_FIELD && !dmv)
02849                         motion_vertical_field_select[1][s] =
02850                         motion_vertical_field_select[0][s] = Get_Bits(1);
02851 
02852                 motion_vector(PMV[0][s],dmvector,h_r_size,v_r_size,dmv,mvscale,0);
02853 
02854                 /* update other motion vector predictors */
02855                 PMV[1][s][0] = PMV[0][s][0];
02856                 PMV[1][s][1] = PMV[0][s][1];
02857         }
02858         else
02859         {
02860                 motion_vertical_field_select[0][s] = Get_Bits(1);
02861                 motion_vector(PMV[0][s],dmvector,h_r_size,v_r_size,dmv,mvscale,0);
02862                 motion_vertical_field_select[1][s] = Get_Bits(1);
02863                 motion_vector(PMV[1][s],dmvector,h_r_size,v_r_size,dmv,mvscale,0);
02864         }
02865 }
02866 
02867 
02868 /* ISO/IEC 13818-2 section 7.6.3.6: Dual prime additional arithmetic */
02869 void CMPEG2Dec::Dual_Prime_Arithmetic(int DMV[][2],int *dmvector, int mvx,int mvy)
02870 {
02871         if (picture_structure==FRAME_PICTURE)
02872         {
02873                 if (top_field_first)
02874                 {
02875                         /* vector for prediction of top field from bottom field */
02876                         DMV[0][0] = ((mvx  +(mvx>0))>>1) + dmvector[0];
02877                         DMV[0][1] = ((mvy  +(mvy>0))>>1) + dmvector[1] - 1;
02878 
02879                         /* vector for prediction of bottom field from top field */
02880                         DMV[1][0] = ((3*mvx+(mvx>0))>>1) + dmvector[0];
02881                         DMV[1][1] = ((3*mvy+(mvy>0))>>1) + dmvector[1] + 1;
02882                 }
02883                 else
02884                 {
02885                         /* vector for prediction of top field from bottom field */
02886                         DMV[0][0] = ((3*mvx+(mvx>0))>>1) + dmvector[0];
02887                         DMV[0][1] = ((3*mvy+(mvy>0))>>1) + dmvector[1] - 1;
02888 
02889                         /* vector for prediction of bottom field from top field */
02890                         DMV[1][0] = ((mvx  +(mvx>0))>>1) + dmvector[0];
02891                         DMV[1][1] = ((mvy  +(mvy>0))>>1) + dmvector[1] + 1;
02892                 }
02893         }
02894         else
02895         {
02896                 /* vector for prediction from field of opposite 'parity' */
02897                 DMV[0][0] = ((mvx+(mvx>0))>>1) + dmvector[0];
02898                 DMV[0][1] = ((mvy+(mvy>0))>>1) + dmvector[1];
02899 
02900                 /* correct for vertical field shift */
02901                 if (picture_structure==TOP_FIELD)
02902                         DMV[0][1]--;
02903                 else
02904                         DMV[0][1]++;
02905         }
02906 }
02907 
02908 /* get and decode motion vector and differential motion vector for one prediction */
02909 void CMPEG2Dec::motion_vector(int *PMV, int *dmvector, int h_r_size, int v_r_size,
02910                                    int dmv, int mvscale, int full_pel_vector)
02911 {
02912         int motion_code, motion_residual;
02913 
02914         /* horizontal component */
02915         /* ISO/IEC 13818-2 Table B-10 */
02916         motion_code = Get_motion_code();
02917 
02918         motion_residual = (h_r_size!=0 && motion_code!=0) ? Get_Bits(h_r_size) : 0;
02919 
02920         decode_motion_vector(&PMV[0],h_r_size,motion_code,motion_residual,full_pel_vector);
02921 
02922         if (dmv)
02923                 dmvector[0] = Get_dmvector();
02924 
02925         /* vertical component */
02926         motion_code     = Get_motion_code();
02927         motion_residual = (v_r_size!=0 && motion_code!=0) ? Get_Bits(v_r_size) : 0;
02928 
02929         if (mvscale)
02930                 PMV[1] >>= 1; /* DIV 2 */
02931 
02932         decode_motion_vector(&PMV[1],v_r_size,motion_code,motion_residual,full_pel_vector);
02933 
02934         if (mvscale)
02935                 PMV[1] <<= 1;
02936 
02937         if (dmv)
02938                 dmvector[1] = Get_dmvector();
02939 }
02940 
02941 /* calculate motion vector component */
02942 /* ISO/IEC 13818-2 section 7.6.3.1: Decoding the motion vectors */
02943 /* Note: the arithmetic here is more elegant than that which is shown 
02944    in 7.6.3.1.  The end results (PMV[][][]) should, however, be the same.  */
02945 void CMPEG2Dec::decode_motion_vector(int *pred, int r_size, int motion_code,
02946                                                                  int motion_residual, int full_pel_vector)
02947 {
02948         int lim, vec;
02949 
02950         lim = 16<<r_size;
02951         vec = full_pel_vector ? (*pred >> 1) : (*pred);
02952 
02953         if (motion_code>0)
02954         {
02955                 vec+= ((motion_code-1)<<r_size) + motion_residual + 1;
02956                 if (vec>=lim)
02957                         vec-= lim + lim;
02958         }
02959         else if (motion_code<0)
02960         {
02961                 vec-= ((-motion_code-1)<<r_size) + motion_residual + 1;
02962                 if (vec<-lim)
02963                         vec+= lim + lim;
02964         }
02965 
02966         *pred = full_pel_vector ? (vec<<1) : vec;
02967 }
02968 
02969 int CMPEG2Dec::Get_motion_code()
02970 {
02971         int code;
02972 
02973         if (Get_Bits(1))
02974                 return 0;
02975 
02976         if ((code = Show_Bits(9))>=64)
02977         {
02978                 code >>= 6;
02979                 Flush_Buffer(MVtab0[code].len);
02980 
02981                 return Get_Bits(1)?-MVtab0[code].val:MVtab0[code].val;
02982         }
02983 
02984         if (code>=24)
02985         {
02986                 code >>= 3;
02987                 Flush_Buffer(MVtab1[code].len);
02988 
02989                 return Get_Bits(1)?-MVtab1[code].val:MVtab1[code].val;
02990         }
02991 
02992         if ((code-=12)<0)
02993         {
02994                 Fault_Flag = 10;
02995                 return 0;
02996         }
02997 
02998         Flush_Buffer(MVtab2[code].len);
02999 
03000         return Get_Bits(1) ? -MVtab2[code].val : MVtab2[code].val;
03001 }
03002 
03003 /* get differential motion vector (for dual prime prediction) */
03004 int CMPEG2Dec::Get_dmvector()
03005 {
03006         if (Get_Bits(1))
03007                 return Get_Bits(1) ? -1 : 1;
03008         else
03009                 return 0;
03010 }
03011 
03012 //
03013 // store
03014 //
03015 
03016 static const __int64 mmmask_0001 = 0x0001000100010001;
03017 static const __int64 mmmask_0002 = 0x0002000200020002;
03018 static const __int64 mmmask_0003 = 0x0003000300030003;
03019 static const __int64 mmmask_0004 = 0x0004000400040004;
03020 static const __int64 mmmask_0005 = 0x0005000500050005;
03021 static const __int64 mmmask_0007 = 0x0007000700070007;
03022 static const __int64 mmmask_0016 = 0x0010001000100010;
03023 static const __int64 mmmask_0040 = 0x0040004000400040;
03024 static const __int64 mmmask_0128 = 0x0080008000800080;
03025 
03026 void CMPEG2Dec::assembleFrame(unsigned char *src[], int pf, unsigned char *dst, int pitch)
03027 {
03028         unsigned char *y444;
03029 
03030         if (Fault_Flag)
03031                 Fault_Flag = 0;
03032 
03033         if (Luminance_Flag)
03034         {
03035                 Luminance_Filter(src[0], lum);
03036                 y444 = lum;
03037         }
03038         else
03039                 y444 = src[0];
03040 
03041         if (chroma_format==CHROMA420)
03042         {
03043                 conv420to422(src[1], u422, pf);
03044                 conv420to422(src[2], v422, pf);
03045 
03046                 if (!dstYUY2())
03047                 {
03048                         conv422to444(u422, u444);
03049                         conv422to444(v422, v444);
03050                 }
03051         }
03052         else if (!dstYUY2())
03053         {
03054                 conv422to444(src[1], u444);
03055                 conv422to444(src[2], v444);
03056         }
03057 
03058         if (dstYUY2())
03059                 conv422toYUY2(y444, u422, v422, dst, pitch);
03060         else
03061                 conv444toRGB24(y444, u444, v444, dst, pitch);
03062 }
03063 
03064 void CMPEG2Dec::Luminance_Filter(unsigned char *src, unsigned char *dst)
03065 {
03066         src += CLIP_AREA;
03067         dst += CLIP_AREA;
03068 
03069         __asm
03070         {
03071                 mov                     edx, this
03072                 mov                     eax, [src]
03073                 mov                     ebx, [dst]
03074                 mov                     esi, 0x00
03075                 mov                     edi, [edx].LUM_AREA
03076                 pxor            mm0, mm0
03077                 movq            mm5, [edx].LumOffsetMask
03078                 movq            mm6, [edx].LumGainMask
03079                 movq            mm7, mmmask_0040
03080 
03081 lumconv:
03082                 movq            mm1, [eax+esi]
03083                 movq            mm2, mm1
03084 
03085                 punpcklbw       mm1, mm0
03086                 punpckhbw       mm2, mm0
03087 
03088                 pmullw          mm1, mm6
03089                 pmullw          mm2, mm6
03090 
03091                 paddw           mm1, mm7
03092                 paddw           mm2, mm7
03093 
03094                 psrlw           mm1, 7
03095                 psrlw           mm2, 7
03096 
03097                 paddw           mm1, mm5
03098                 paddw           mm2, mm5
03099 
03100                 packuswb        mm1, mm0
03101                 packuswb        mm2, mm0
03102 
03103                 add                     esi, 0x08
03104                 cmp                     esi, edi
03105                 movq            [ebx+esi-8], mm1
03106                 movq            [ebx+esi-4], mm2
03107                 jl                      lumconv
03108         }
03109 }
03110 
03111 void CMPEG2Dec::conv422to444(unsigned char *src, unsigned char *dst)
03112 {
03113         src += HALF_CLIP_AREA;
03114         dst += CLIP_AREA;
03115 
03116         __asm
03117         {
03118                 mov                     edx, this
03119                 mov                     eax, [src]
03120                 mov                     ebx, [dst]
03121                 mov                     edi, [edx].Clip_Height
03122 
03123                 movq            mm1, [mmmask_0001]
03124                 pxor            mm0, mm0
03125 
03126 convyuv444init:
03127                 movq            mm7, [eax]
03128                 mov                     esi, 0x00
03129 
03130 convyuv444:
03131                 movq            mm2, mm7
03132                 movq            mm7, [eax+esi+8]
03133                 movq            mm3, mm2
03134                 movq            mm4, mm7
03135 
03136                 psrlq           mm3, 8
03137                 psllq           mm4, 56
03138                 por                     mm3, mm4
03139 
03140                 movq            mm4, mm2
03141                 movq            mm5, mm3
03142 
03143                 punpcklbw       mm4, mm0
03144                 punpcklbw       mm5, mm0
03145 
03146                 movq            mm6, mm4
03147                 paddusw         mm4, mm1
03148                 paddusw         mm4, mm5
03149                 psrlw           mm4, 1
03150                 psllq           mm4, 8
03151                 por                     mm4, mm6
03152 
03153                 punpckhbw       mm2, mm0
03154                 punpckhbw       mm3, mm0
03155 
03156                 movq            mm6, mm2
03157                 paddusw         mm2, mm1
03158                 paddusw         mm2, mm3
03159 
03160                 movq            [ebx+esi*2], mm4
03161 
03162                 psrlw           mm2, 1
03163                 psllq           mm2, 8
03164                 por                     mm2, mm6
03165 
03166                 add                     esi, 0x08
03167                 cmp                     esi, [edx].HALF_WIDTH_D8
03168                 movq            [ebx+esi*2-8], mm2
03169                 jl                      convyuv444
03170 
03171                 movq            mm2, mm7
03172                 punpcklbw       mm2, mm0
03173                 movq            mm3, mm2
03174 
03175                 psllq           mm2, 8
03176                 por                     mm2, mm3
03177 
03178                 movq            [ebx+esi*2], mm2
03179 
03180                 punpckhbw       mm7, mm0
03181                 movq            mm6, mm7
03182 
03183                 psllq           mm6, 8
03184                 por                     mm6, mm7
03185 
03186                 movq            [ebx+esi*2+8], mm6
03187 
03188                 add                     eax, [edx].HALF_WIDTH
03189                 add                     ebx, [edx].Coded_Picture_Width
03190                 dec                     edi
03191                 cmp                     edi, 0x00
03192                 jg                      convyuv444init
03193         }
03194 }
03195 
03196 void CMPEG2Dec::conv420to422(unsigned char *src, unsigned char *dst, int frame_type)
03197 {
03198         if (frame_type)
03199         {
03200                 __asm
03201                 {
03202                         push            ebp
03203                         mov                     eax, [src]
03204                         mov                     ebx, [dst]
03205                         mov                     ebp, this
03206                         mov                     ecx, ebx
03207                         add                     ecx, ds:[ebp].HALF_WIDTH
03208                         mov                     esi, 0x00
03209                         movq            mm3, [mmmask_0003]
03210                         pxor            mm0, mm0
03211                         movq            mm4, [mmmask_0002]
03212 
03213                         mov                     edx, eax
03214                         add                     edx, ds:[ebp].HALF_WIDTH
03215 convyuv422topp:
03216                         movd            mm1, [eax+esi]
03217                         movd            mm2, [edx+esi]
03218                         movd            [ebx+esi], mm1
03219                         punpcklbw       mm1, mm0
03220                         pmullw          mm1, mm3
03221                         paddusw         mm1, mm4
03222                         punpcklbw       mm2, mm0
03223                         paddusw         mm2, mm1
03224                         psrlw           mm2, 0x02
03225                         packuswb        mm2, mm0
03226 
03227                         add                     esi, 0x04
03228                         cmp                     esi, ds:[ebp].HALF_WIDTH
03229                         movd            [ecx+esi-4], mm2
03230                         jl                      convyuv422topp
03231 
03232                         add                     eax, ds:[ebp].HALF_WIDTH
03233                         add                     ebx, ds:[ebp].Coded_Picture_Width
03234                         add                     ecx, ds:[ebp].Coded_Picture_Width
03235                         mov                     esi, 0x00
03236 
03237                         mov                     edi, ds:[ebp].PROGRESSIVE_HEIGHT
03238 convyuv422p:
03239                         movd            mm1, [eax+esi]
03240 
03241                         punpcklbw       mm1, mm0
03242                         mov                     edx, eax
03243 
03244                         pmullw          mm1, mm3
03245                         sub                     edx, ds:[ebp].HALF_WIDTH
03246 
03247                         movd            mm5, [edx+esi]
03248                         movd            mm2, [edx+esi]
03249 
03250                         punpcklbw       mm5, mm0
03251                         punpcklbw       mm2, mm0
03252                         paddusw         mm5, mm1
03253                         paddusw         mm2, mm1
03254                         paddusw         mm5, mm4
03255                         paddusw         mm2, mm4
03256                         psrlw           mm5, 0x02
03257                         psrlw           mm2, 0x02
03258                         packuswb        mm5, mm0
03259                         packuswb        mm2, mm0
03260 
03261                         mov                     edx, eax
03262                         add                     edx, ds:[ebp].HALF_WIDTH
03263                         add                     esi, 0x04
03264                         cmp                     esi, ds:[ebp].HALF_WIDTH
03265                         movd            [ebx+esi-4], mm5
03266                         movd            [ecx+esi-4], mm2
03267 
03268                         jl                      convyuv422p
03269 
03270                         add                     eax, ds:[ebp].HALF_WIDTH
03271                         add                     ebx, ds:[ebp].Coded_Picture_Width
03272                         add                     ecx, ds:[ebp].Coded_Picture_Width
03273                         mov                     esi, 0x00
03274                         dec                     edi
03275                         cmp                     edi, 0x00
03276                         jg                      convyuv422p
03277 
03278                         mov                     edx, eax
03279                         sub                     edx, ds:[ebp].HALF_WIDTH
03280 convyuv422bottomp:
03281                         movd            mm1, [eax+esi]
03282                         movd            mm5, [edx+esi]
03283                         punpcklbw       mm5, mm0
03284                         movd            [ecx+esi], mm1
03285 
03286                         punpcklbw       mm1, mm0
03287                         pmullw          mm1, mm3
03288                         paddusw         mm5, mm1
03289                         paddusw         mm5, mm4
03290                         psrlw           mm5, 0x02
03291                         packuswb        mm5, mm0
03292 
03293                         add                     esi, 0x04
03294                         cmp                     esi, ds:[ebp].HALF_WIDTH
03295                         movd            [ebx+esi-4], mm5
03296                         jl                      convyuv422bottomp
03297                         pop                     ebp
03298                 }
03299         }
03300         else
03301         {
03302                 __asm
03303                 {
03304                         push            ebp
03305                         mov                     eax, [src]
03306                         mov                     ecx, [dst]
03307                         mov                     ebp, this
03308                         mov                     esi, 0x00
03309                         pxor            mm0, mm0
03310                         movq            mm3, [mmmask_0003]
03311                         movq            mm4, [mmmask_0004]
03312                         movq            mm5, [mmmask_0005]
03313 
03314 convyuv422topi:
03315                         movd            mm1, [eax+esi]
03316                         mov                     ebx, eax
03317                         add                     ebx, ds:[ebp].HALF_WIDTH
03318                         movd            mm2, [ebx+esi]
03319                         movd            [ecx+esi], mm1
03320                         punpcklbw       mm1, mm0
03321                         movq            mm6, mm1
03322                         pmullw          mm1, mm3
03323 
03324                         punpcklbw       mm2, mm0
03325                         movq            mm7, mm2
03326                         pmullw          mm2, mm5
03327                         paddusw         mm2, mm1
03328                         paddusw         mm2, mm4
03329                         psrlw           mm2, 0x03
03330                         packuswb        mm2, mm0
03331 
03332                         mov                     edx, ecx
03333                         add                     edx, ds:[ebp].HALF_WIDTH
03334                         pmullw          mm6, mm5
03335                         movd            [edx+esi], mm2
03336 
03337                         add                     ebx, ds:[ebp].HALF_WIDTH
03338                         movd            mm2, [ebx+esi]
03339                         punpcklbw       mm2, mm0
03340                         pmullw          mm2, mm3
03341                         paddusw         mm2, mm6
03342                         paddusw         mm2, mm4
03343                         psrlw           mm2, 0x03
03344                         packuswb        mm2, mm0
03345 
03346                         add                     edx, ds:[ebp].HALF_WIDTH
03347                         add                     ebx, ds:[ebp].HALF_WIDTH
03348                         pmullw          mm7, [mmmask_0007]
03349                         movd            [edx+esi], mm2
03350 
03351                         movd            mm2, [ebx+esi]
03352                         punpcklbw       mm2, mm0
03353                         paddusw         mm2, mm7
03354                         paddusw         mm2, mm4
03355                         psrlw           mm2, 0x03
03356                         packuswb        mm2, mm0
03357 
03358                         add                     edx, ds:[ebp].HALF_WIDTH
03359                         add                     esi, 0x04
03360                         cmp                     esi, ds:[ebp].HALF_WIDTH
03361                         movd            [edx+esi-4], mm2
03362 
03363                         jl                      convyuv422topi
03364 
03365                         add                     eax, ds:[ebp].Coded_Picture_Width
03366                         add                     ecx, ds:[ebp].DOUBLE_WIDTH
03367                         mov                     esi, 0x00
03368 
03369                         mov                     edi, ds:[ebp].INTERLACED_HEIGHT
03370 convyuv422i:
03371                         movd            mm1, [eax+esi]
03372                         punpcklbw       mm1, mm0
03373                         movq            mm6, mm1
03374                         mov                     ebx, eax
03375                         sub                     ebx, ds:[ebp].Coded_Picture_Width
03376                         movd            mm3, [ebx+esi]
03377                         pmullw          mm1, [mmmask_0007]
03378                         punpcklbw       mm3, mm0
03379                         paddusw         mm3, mm1
03380                         paddusw         mm3, mm4
03381                         psrlw           mm3, 0x03
03382                         packuswb        mm3, mm0
03383 
03384                         add                     ebx, ds:[ebp].HALF_WIDTH
03385                         movq            mm1, [ebx+esi]
03386                         add                     ebx, ds:[ebp].Coded_Picture_Width
03387                         movd            [ecx+esi], mm3
03388 
03389                         movq            mm3, [mmmask_0003]
03390                         movd            mm2, [ebx+esi]
03391 
03392                         punpcklbw       mm1, mm0
03393                         pmullw          mm1, mm3
03394                         punpcklbw       mm2, mm0
03395                         movq            mm7, mm2
03396                         pmullw          mm2, mm5
03397                         paddusw         mm2, mm1
03398                         paddusw         mm2, mm4
03399                         psrlw           mm2, 0x03
03400                         packuswb        mm2, mm0
03401 
03402                         pmullw          mm6, mm5
03403                         mov                     edx, ecx
03404                         add                     edx, ds:[ebp].HALF_WIDTH
03405                         movd            [edx+esi], mm2
03406 
03407                         add                     ebx, ds:[ebp].HALF_WIDTH
03408                         movd            mm2, [ebx+esi]
03409                         punpcklbw       mm2, mm0
03410                         pmullw          mm2, mm3
03411                         paddusw         mm2, mm6
03412                         paddusw         mm2, mm4
03413                         psrlw           mm2, 0x03
03414                         packuswb        mm2, mm0
03415 
03416                         pmullw          mm7, [mmmask_0007]
03417                         add                     edx, ds:[ebp].HALF_WIDTH
03418                         add                     ebx, ds:[ebp].HALF_WIDTH
03419                         movd            [edx+esi], mm2
03420 
03421                         movd            mm2, [ebx+esi]
03422                         punpcklbw       mm2, mm0
03423                         paddusw         mm2, mm7
03424                         paddusw         mm2, mm4
03425                         psrlw           mm2, 0x03
03426                         packuswb        mm2, mm0
03427 
03428                         add                     edx, ds:[ebp].HALF_WIDTH
03429                         add                     esi, 0x04
03430                         cmp                     esi, ds:[ebp].HALF_WIDTH
03431                         movd            [edx+esi-4], mm2
03432 
03433                         jl                      convyuv422i
03434                         add                     eax, ds:[ebp].Coded_Picture_Width
03435                         add                     ecx, ds:[ebp].DOUBLE_WIDTH
03436                         mov                     esi, 0x00
03437                         dec                     edi
03438                         cmp                     edi, 0x00
03439                         jg                      convyuv422i
03440 
03441 convyuv422bottomi:
03442                         movd            mm1, [eax+esi]
03443                         movq            mm6, mm1
03444                         punpcklbw       mm1, mm0
03445                         mov                     ebx, eax
03446                         sub                     ebx, ds:[ebp].Coded_Picture_Width
03447                         movd            mm3, [ebx+esi]
03448                         punpcklbw       mm3, mm0
03449                         pmullw          mm1, [mmmask_0007]
03450                         paddusw         mm3, mm1
03451                         paddusw         mm3, mm4
03452                         psrlw           mm3, 0x03
03453                         packuswb        mm3, mm0
03454 
03455                         add                     ebx, ds:[ebp].HALF_WIDTH
03456                         movq            mm1, [ebx+esi]
03457                         punpcklbw       mm1, mm0
03458                         movd            [ecx+esi], mm3
03459 
03460                         pmullw          mm1, [mmmask_0003]
03461                         add                     ebx, ds:[ebp].Coded_Picture_Width
03462                         movd            mm2, [ebx+esi]
03463                         punpcklbw       mm2, mm0
03464                         movq            mm7, mm2
03465                         pmullw          mm2, mm5
03466                         paddusw         mm2, mm1
03467                         paddusw         mm2, mm4
03468                         psrlw           mm2, 0x03
03469                         packuswb        mm2, mm0
03470 
03471                         mov                     edx, ecx
03472                         add                     edx, ds:[ebp].HALF_WIDTH
03473                         pmullw          mm7, [mmmask_0007]
03474                         movd            [edx+esi], mm2
03475 
03476                         add                     edx, ds:[ebp].HALF_WIDTH
03477                         movd            [edx+esi], mm6
03478 
03479                         punpcklbw       mm6, mm0
03480                         paddusw         mm6, mm7
03481                         paddusw         mm6, mm4
03482                         psrlw           mm6, 0x03
03483                         packuswb        mm6, mm0
03484 
03485                         add                     edx, ds:[ebp].HALF_WIDTH
03486                         add                     esi, 0x04
03487                         cmp                     esi, ds:[ebp].HALF_WIDTH
03488                         movd            [edx+esi-4], mm6
03489 
03490                         jl                      convyuv422bottomi
03491                         pop                     ebp
03492                 }
03493         }
03494 }
03495 
03496 void CMPEG2Dec::conv444toRGB24(unsigned char *py, unsigned char *pu, unsigned char *pv, unsigned char *dst, int pitch)
03497 {
03498         int PWIDTH = pitch - DSTBYTES;
03499 
03500         py += CLIP_STEP;
03501         pu += CLIP_STEP;
03502         pv += CLIP_STEP;
03503 
03504         int Clip_Height = this->Clip_Height;
03505         __int64 RGB_Offset = this->RGB_Offset;
03506         __int64 RGB_Scale = this->RGB_Scale;
03507         __int64 RGB_CBU = this->RGB_CBU;
03508         __int64 RGB_CRV = this->RGB_CRV;
03509         __int64 RGB_CGX = this->RGB_CGX;
03510         int Clip_Width = this->Clip_Width;
03511         int Coded_Picture_Width = this->Coded_Picture_Width;
03512 
03513         __asm
03514         {
03515                 mov                     eax, [py]
03516                 mov                     ebx, [pu]
03517                 mov                     ecx, [pv]
03518                 mov                     edx, [dst]
03519                 mov                     edi, Clip_Height
03520                 mov                     esi, 0x00
03521                 pxor            mm0, mm0
03522 
03523 convRGB24:
03524                 movd            mm1, [eax+esi]
03525                 movd            mm3, [ebx+esi]
03526                 punpcklbw       mm1, mm0
03527                 punpcklbw       mm3, mm0
03528                 movd            mm5, [ecx+esi]
03529                 punpcklbw       mm5, mm0
03530                 movq            mm7, [mmmask_0128]
03531                 psubw           mm3, mm7
03532                 psubw           mm5, mm7
03533 
03534                 psubw           mm1, RGB_Offset
03535                 movq            mm2, mm1
03536                 movq            mm7, [mmmask_0001]
03537                 punpcklwd       mm1, mm7
03538                 punpckhwd       mm2, mm7
03539                 movq            mm7, RGB_Scale
03540                 pmaddwd         mm1, mm7
03541                 pmaddwd         mm2, mm7
03542 
03543                 movq            mm4, mm3
03544                 punpcklwd       mm3, mm0
03545                 punpckhwd       mm4, mm0
03546                 movq            mm7, RGB_CBU
03547                 pmaddwd         mm3, mm7
03548                 pmaddwd         mm4, mm7
03549                 paddd           mm3, mm1
03550                 paddd           mm4, mm2
03551                 psrld           mm3, 13
03552                 psrld           mm4, 13
03553                 packuswb        mm3, mm0
03554                 packuswb        mm4, mm0
03555 
03556                 movq            mm6, mm5
03557                 punpcklwd       mm5, mm0
03558                 punpckhwd       mm6, mm0
03559                 movq            mm7, RGB_CRV
03560                 pmaddwd         mm5, mm7
03561                 pmaddwd         mm6, mm7
03562                 paddd           mm5, mm1
03563                 paddd           mm6, mm2
03564                 psrld           mm5, 13
03565                 psrld           mm6, 13
03566                 packuswb        mm5, mm0
03567                 packuswb        mm6, mm0
03568 
03569                 punpcklbw       mm3, mm5
03570                 punpcklbw       mm4, mm6
03571                 movq            mm5, mm3
03572                 movq            mm6, mm4
03573                 psrlq           mm5, 16
03574                 psrlq           mm6, 16
03575                 por                     mm3, mm5
03576                 por                     mm4, mm6
03577 
03578                 movd            mm5, [ebx+esi]
03579                 movd            mm6, [ecx+esi]
03580                 punpcklbw       mm5, mm0
03581                 punpcklbw       mm6, mm0
03582                 movq            mm7, [mmmask_0128]
03583                 psubw           mm5, mm7
03584                 psubw           mm6, mm7
03585 
03586                 movq            mm7, mm6
03587                 punpcklwd       mm6, mm5
03588                 punpckhwd       mm7, mm5                
03589                 movq            mm5, RGB_CGX
03590                 pmaddwd         mm6, mm5
03591                 pmaddwd         mm7, mm5
03592                 paddd           mm6, mm1
03593                 paddd           mm7, mm2
03594 
03595                 psrld           mm6, 13
03596                 psrld           mm7, 13
03597                 packuswb        mm6, mm0
03598                 packuswb        mm7, mm0
03599 
03600                 punpcklbw       mm3, mm6
03601                 punpcklbw       mm4, mm7
03602 
03603                 movq            mm1, mm3
03604                 movq            mm5, mm4
03605                 movq            mm6, mm4
03606 
03607                 psrlq           mm1, 32
03608                 psllq           mm1, 24
03609                 por                     mm1, mm3
03610 
03611                 psrlq           mm3, 40
03612                 psllq           mm6, 16
03613                 por                     mm3, mm6
03614                 movd            [edx], mm1
03615 
03616                 psrld           mm4, 16
03617                 psrlq           mm5, 24
03618                 por                     mm5, mm4
03619                 movd            [edx+4], mm3
03620 
03621                 add                     edx, 0x0c
03622                 add                     esi, 0x04
03623                 cmp                     esi, Clip_Width
03624                 movd            [edx-4], mm5
03625 
03626                 jl                      convRGB24
03627 
03628                 add                     eax, Coded_Picture_Width
03629                 add                     ebx, Coded_Picture_Width
03630                 add                     ecx, Coded_Picture_Width
03631                 add                     edx, PWIDTH
03632                 mov                     esi, 0x00
03633                 dec                     edi
03634                 cmp                     edi, 0x00
03635                 jg                      convRGB24
03636 
03637                 emms
03638         }
03639 }
03640 
03641 // YUV 4:2:2 Format:
03642 // YUYV YUYV ...
03643 void CMPEG2Dec::conv422toYUY2(unsigned char *py, unsigned char *pu, unsigned char *pv, unsigned char *dst, int pitch)
03644 {
03645         py += CLIP_STEP;
03646         pu += CLIP_STEP;
03647         pv += CLIP_STEP;
03648 
03649         int y = this->Clip_Height;
03650         int Clip_Width_2 = this->Clip_Width / 2;
03651         int Coded_Picture_Width = this->Coded_Picture_Width;
03652         int Coded_Picture_Width_2 = this->Coded_Picture_Width / 2;
03653 
03654         __asm
03655         {
03656                 emms
03657                 mov                     eax, [py]
03658                 mov                     ebx, [pu]
03659                 mov                     ecx, [pv]
03660                 mov                     edx, [dst]
03661                 mov                     edi, Clip_Width_2
03662         yloop:
03663                 xor                     esi, esi
03664         xloop:
03665                 movd            mm1, [eax+esi*2]                ;0000YYYY
03666                 movd            mm2, [ebx+esi]                  ;0000UUUU
03667                 movd            mm3, [ecx+esi]                  ;0000VVVV
03668                 ;interleave this to VYUYVYUY
03669                 punpcklbw       mm2, mm3                                ;VUVUVUVU
03670                 punpcklbw       mm1, mm2                                ;VYUYVYUY
03671                 movq            [edx+esi*4], mm1
03672                 movd            mm1, [eax+esi*2+4]              ;0000YYYY
03673                 punpckhdq       mm2, mm2                                ;xxxxVUVU
03674                 punpcklbw       mm1, mm2                                ;VYUYVYUY
03675                 movq            [edx+esi*4+8], mm1
03676                 add                     esi, 4
03677                 cmp                     esi, edi
03678                 jb                      xloop
03679                 add                     edx, pitch
03680                 add                     eax, Coded_Picture_Width
03681                 add                     ebx, Coded_Picture_Width_2
03682                 add                     ecx, Coded_Picture_Width_2
03683                 dec                     y
03684                 jnz                     yloop
03685                 emms
03686         }
03687 }
03688 
03689 //
03690 // codec
03691 //
03692 
03693 static const int ChromaFormat[4] = {
03694         0, 6, 8, 12
03695 };
03696 
03697 CMPEG2Dec::CMPEG2Dec()
03698 {
03699   VF_File = 0;
03700   VF_FrameLimit = VF_FrameBound = VF_GOPLimit = VF_GOPNow = VF_GOPSize =
03701         VF_OldFrame = VF_OldRef = 0;
03702   VF_FrameSize = VF_FrameRate = 0;
03703   memset(Rdbfr, 0, sizeof(Rdbfr));
03704   Rdptr = Rdmax = 0;
03705   CurrentBfr = NextBfr = BitsLeft = Val = Read = 0;
03706   Fault_Flag = File_Flag = File_Limit = FO_Flag = IDCT_Flag = SystemStream_Flag = 0;
03707   Luminance_Flag = Resize_Flag = KeyOp_Flag = lfsr0 = lfsr1 = 0;
03708   BufferOp = 0;
03709   memset(intra_quantizer_matrix, 0, sizeof(intra_quantizer_matrix));
03710   memset(non_intra_quantizer_matrix, 0, sizeof(non_intra_quantizer_matrix));
03711   memset(chroma_intra_quantizer_matrix, 0, sizeof(chroma_intra_quantizer_matrix));
03712   memset(chroma_non_intra_quantizer_matrix, 0, sizeof(chroma_non_intra_quantizer_matrix));
03713   load_intra_quantizer_matrix =
03714   load_non_intra_quantizer_matrix =
03715   load_chroma_intra_quantizer_matrix =
03716   load_chroma_non_intra_quantizer_matrix = 0;
03717   q_scale_type =
03718   alternate_scan =
03719   quantizer_scale = 0;
03720 
03721         int i;
03722         for (i=0; i<MAX_FILE_NUMBER; i++) Infilename[i] = NULL;
03723         for (i=0; i<8; i++) p_block[i] = block[i] = NULL;
03724         p_fTempArray = fTempArray = NULL;
03725         for (i=0; i<3; i++) backward_reference_frame[i] = forward_reference_frame[i] =  auxframe[i] = NULL;
03726         lum = NULL;
03727         u422 = v422 = u444 = v444 = dstFrame = NULL;
03728         hLibrary = NULL;
03729 
03730   CheckCPU();
03731 }
03732 
03733 static char* myfgets(char* buff, int len, FILE* file)
03734 {
03735         char* ret = buff;
03736 
03737         ret[0] = 0;
03738         
03739         while(ret = fgets(buff, len, file))
03740         {
03741                 while(isspace(*ret)) ret++;
03742                 if(*ret) break;
03743         }
03744 
03745         return(ret);
03746 }
03747 
03748 int CMPEG2Dec::Open(const char *path, DstFormat dstFormat)
03749 {
03750         m_dstFormat = dstFormat;
03751         char ID[19], PASS[19] = "DVD2AVIProjectFile";
03752         DWORD i, j, size, code, type, tff, rff, film, ntsc, gop, top, bottom, mapping;
03753         int repeat_on, repeat_off, repeat_init;
03754         int Clip_Top, Clip_Bottom, Clip_Left, Clip_Right, Squeeze_Width, Squeeze_Height;
03755 
03756         HKEY key; DWORD value = REG_SZ; DWORD length = 256;
03757         char *ext, buffer[256];
03758 
03759         CMPEG2Dec* out = this;
03760 
03761         out->VF_File = fopen(path, "r");
03762         if (out->VF_File==NULL)
03763                 return 0;
03764         if (fgets(ID, 19, out->VF_File)==NULL)
03765                 return 0;
03766         if (strcmp(ID, PASS))
03767                 return 0;
03768 
03769         // load DLL
03770         if (RegOpenKeyExA(HKEY_CURRENT_USER, "Software\\VFPlugin", 0, KEY_ALL_ACCESS, &key)==ERROR_SUCCESS)
03771         {
03772                 RegQueryValueExA(key, "DVD2AVI", NULL, &value, (unsigned char*)buffer, &length);
03773 
03774                 ext = strrchr(buffer, '\\');
03775                 sprintf(buffer + (int)(ext-buffer) + 1, "OpenDVD.dll");
03776                 RegCloseKey(key);
03777         }
03778 
03779         if ((hLibrary = LoadLibraryA(buffer)) != NULL)
03780                 BufferOp = (PBufferOp) GetProcAddress(hLibrary, "BufferOp");
03781 
03782         for (i=0; i<MAX_FILE_NUMBER; i++)
03783                 Infilename[i] = new char[_MAX_PATH];
03784 
03785         if(1 != fscanf(out->VF_File, "%d", &File_Limit))
03786                 return 0;
03787 
03788         i = File_Limit;
03789         while (i)
03790         {
03791                 if(1 != fscanf(out->VF_File, "%d ", &j))
03792                         return 0;
03793                 fgets(Infilename[File_Limit-i], j+1, out->VF_File);
03794                 if ((Infile[File_Limit-i] = _open(Infilename[File_Limit-i], _O_RDONLY | _O_BINARY))==-1)
03795                         return 0;
03796                 i--;
03797         }
03798 
03799         if(3 != sscanf(myfgets(buffer, sizeof(buffer), out->VF_File), "\nStream_Type=%d,%X,%X\n", &SystemStream_Flag, &lfsr0, &lfsr1))
03800                 return 0;
03801         if (lfsr0 || lfsr1)
03802                 KeyOp_Flag = 1;
03803         else
03804                 KeyOp_Flag = 0;
03805 
03806         if(1 != sscanf(myfgets(buffer, sizeof(buffer), out->VF_File), "iDCT_Algorithm=%d\n", &IDCT_Flag))
03807                 return 0;
03808 
03809         switch (IDCT_Flag)
03810         {
03811                 case IDCT_SSEMMX:
03812                         if (!cpu.ssemmx)
03813                                 IDCT_Flag = IDCT_MMX;
03814                         break;
03815 
03816                 case IDCT_FPU:
03817                         Initialize_FPU_IDCT();
03818                         break;
03819 
03820                 case IDCT_REF:
03821                         Initialize_REF_IDCT();
03822                         break;
03823         }
03824 
03825         File_Flag = 0;
03826         _lseeki64(Infile[0], 0, SEEK_SET);
03827         Initialize_Buffer();
03828 
03829         do
03830         {
03831                 next_start_code();
03832                 code = Get_Bits(32);
03833         }
03834         while (code!=SEQUENCE_HEADER_CODE);
03835 
03836         sequence_header();
03837 
03838         mb_width = (horizontal_size+15)/16;
03839         mb_height = progressive_sequence ? (vertical_size+15)/16 : 2*((vertical_size+31)/32);
03840 
03841         Coded_Picture_Width = 16 * mb_width;
03842         Coded_Picture_Height = 16 * mb_height;
03843 
03844         Chroma_Width = (chroma_format==CHROMA444) ? Coded_Picture_Width : Coded_Picture_Width>>1;
03845         Chroma_Height = (chroma_format!=CHROMA420) ? Coded_Picture_Height : Coded_Picture_Height>>1;
03846 
03847         block_count = ChromaFormat[chroma_format];
03848 
03849         for (i=0; i<8; i++)
03850         {
03851                 p_block[i] = (short *)new BYTE[sizeof(short)*64 + 64];
03852                 block[i]   = (short *)((long)p_block[i] + 64 - (long)p_block[i]%64);
03853         }
03854 
03855         p_fTempArray = (void *)new BYTE[sizeof(float)*128 + 64];
03856         fTempArray = (void *)((long)p_fTempArray + 64 - (long)p_fTempArray%64);
03857 
03858         for (i=0; i<3; i++)
03859         {
03860                 if (i==0)
03861                         size = Coded_Picture_Width * Coded_Picture_Height;
03862                 else
03863                         size = Chroma_Width * Chroma_Height;
03864 
03865                 backward_reference_frame[i] = new unsigned char[size];
03866                 forward_reference_frame[i] = new unsigned char[size];
03867                 auxframe[i] = new unsigned char[size];
03868         }
03869 
03870         if(1 != sscanf(myfgets(buffer, sizeof(buffer), out->VF_File), "YUVRGB_Scale=%d\n", &i))
03871                 return 0;
03872 
03873         if (i)
03874         {
03875                 RGB_Scale = 0x1000254310002543;
03876                 RGB_Offset = 0x0010001000100010;
03877                 RGB_CBU = 0x0000408D0000408D;
03878                 RGB_CGX = 0xF377E5FCF377E5FC;
03879                 RGB_CRV = 0x0000331300003313;
03880         }
03881         else
03882         {
03883                 RGB_Scale = 0x1000200010002000;
03884                 RGB_Offset = 0x0000000000000000;
03885                 RGB_CBU = 0x000038B4000038B4;
03886                 RGB_CGX = 0xF4FDE926F4FDE926;
03887                 RGB_CRV = 0x00002CDD00002CDD;
03888         }
03889 
03890         char* tmp = myfgets(buffer, sizeof(buffer), out->VF_File);
03891         if(2 != sscanf(tmp, "Luminance=%d,%d\n", &i, &j))
03892         {
03893                 if(2 != sscanf(tmp, "Luminance_Filter=%d,%d\n", &i, &j))
03894                         return 0;
03895                 i=128; j=0;
03896         }
03897 
03898         if (i==128 && j==0)
03899                 Luminance_Flag = 0;
03900         else
03901         {
03902                 Luminance_Flag = 1;
03903                 LumGainMask = ((__int64)i<<48) + ((__int64)i<<32) + ((__int64)i<<16) + (__int64)i;
03904                 LumOffsetMask = ((__int64)j<<48) + ((__int64)j<<32) + ((__int64)j<<16) + (__int64)j;
03905 
03906                 lum = new unsigned char[Coded_Picture_Width * Coded_Picture_Height];
03907         }
03908 
03909         if(6 != sscanf(myfgets(buffer, sizeof(buffer), out->VF_File), "Picture_Size=%d,%d,%d,%d,%d,%d\n", 
03910                 &Clip_Top, &Clip_Bottom, &Clip_Left, &Clip_Right, &Squeeze_Width, &Squeeze_Height))
03911                 return 0;
03912 
03913         Resize_Flag = 0;
03914         Resize_Width = Clip_Width = Coded_Picture_Width;
03915         Resize_Height = Clip_Height = Coded_Picture_Height;
03916         CLIP_AREA = HALF_CLIP_AREA = CLIP_STEP = 0;
03917 
03918         if (Clip_Top || Clip_Bottom || Clip_Left || Clip_Right)
03919         {
03920                 Clip_Width -= Clip_Left+Clip_Right;
03921                 Clip_Height -= Clip_Top+Clip_Bottom;
03922                 Resize_Width = Clip_Width;
03923                 Resize_Height = Clip_Height;
03924 
03925                 CLIP_AREA = Coded_Picture_Width * Clip_Top;
03926                 HALF_CLIP_AREA = (Coded_Picture_Width>>1) * Clip_Top;
03927                 CLIP_STEP = Coded_Picture_Width * Clip_Top + Clip_Left;
03928         }
03929 
03930         if (Squeeze_Width || Squeeze_Height)
03931         {
03932                 Resize_Flag = 1;
03933                 Resize_Width -= Squeeze_Width;
03934                 Resize_Height -= Squeeze_Height;
03935         }
03936 
03937         DSTBYTES = Clip_Width * (dstRGB24() ? 3 : 2);
03938         DSTBYTES2 = DSTBYTES * 2;
03939         LUM_AREA = Coded_Picture_Width * Clip_Height;
03940         PROGRESSIVE_HEIGHT = (Coded_Picture_Height>>1) - 2;
03941         INTERLACED_HEIGHT = (Coded_Picture_Height>>2) - 2;
03942         HALF_WIDTH = Coded_Picture_Width>>1;
03943         HALF_WIDTH_D8 = (Coded_Picture_Width>>1) - 8;
03944         DOUBLE_WIDTH = Coded_Picture_Width<<1;
03945 
03946         u422 = new unsigned char[Coded_Picture_Width * Coded_Picture_Height / 2];
03947         v422 = new unsigned char[Coded_Picture_Width * Coded_Picture_Height / 2];
03948         u444 = new unsigned char[Coded_Picture_Width * Coded_Picture_Height];
03949         v444 = new unsigned char[Coded_Picture_Width * Coded_Picture_Height];
03950         dstFrame = new unsigned char[Clip_Width * Clip_Height * 4];  // max value (super set)
03951 
03952         if(1 != sscanf(myfgets(buffer, sizeof(buffer), out->VF_File), "Field_Operation=%d\n", &FO_Flag))
03953                 return 0;
03954         if(1 != sscanf(myfgets(buffer, sizeof(buffer), out->VF_File), "Frame_Rate=%d\n", &(out->VF_FrameRate)))
03955                 return 0;
03956         if(4 != sscanf(myfgets(buffer, sizeof(buffer), out->VF_File), "Location=%d,%X,%d,%X\n", &i, &j, &i, &j))
03957                 return 0;
03958 
03959         ntsc = film = top = bottom = gop = mapping = repeat_on = repeat_off = repeat_init = 0;
03960 
03961         while (1 == fscanf(out->VF_File, "%d", &type) && type<9)
03962         {
03963                 if (type==7)    // I frame
03964                 {
03965                         GOPList[gop] = reinterpret_cast<GOPLIST*>(calloc(1, sizeof(GOPLIST)));
03966                         GOPList[gop]->number = film;
03967                         if(2 != fscanf(out->VF_File, "%d %X", &(GOPList[gop]->file), &j))
03968                                 break;
03969 
03970                         GOPList[gop]->position = (__int64)j*BUFFER_SIZE;
03971                         gop ++;
03972 
03973                         if(1 != fscanf(out->VF_File, "%d", &j))
03974                                 break;
03975 
03976                         tff = j>>1;
03977                         rff = j & 1;
03978                 }
03979                 else    // P, B frame
03980                 {
03981                         tff = type>>1;
03982                         rff = type & 1;
03983                 }
03984 
03985                 if (!film)
03986                 {
03987                         if (tff)
03988                                 Field_Order = 1;
03989                         else
03990                                 Field_Order = 0;
03991                 }
03992 
03993                 if (FO_Flag==FO_FILM)
03994                 {
03995                         if (rff)
03996                                 repeat_on++;
03997                         else
03998                                 repeat_off++;
03999 
04000                         if (repeat_init)
04001                         {
04002                                 if (repeat_off-repeat_on == 5)
04003                                 {
04004                                         repeat_on = repeat_off = 0;
04005                                 }
04006                                 else
04007                                 {
04008                                         FrameList[mapping] = reinterpret_cast<FRAMELIST*>(calloc(1, sizeof(FRAMELIST)));
04009                                         FrameList[mapping]->top = FrameList[mapping]->bottom = film;
04010                                         mapping ++;
04011                                 }
04012 
04013                                 if (repeat_on-repeat_off == 5)
04014                                 {
04015                                         repeat_on = repeat_off = 0;
04016                                         FrameList[mapping] = reinterpret_cast<FRAMELIST*>(calloc(1, sizeof(FRAMELIST)));
04017                                         FrameList[mapping]->top = FrameList[mapping]->bottom = film;
04018                                         mapping ++;
04019                                 }
04020                         }
04021                         else
04022                         {
04023                                 if (repeat_off-repeat_on == 3)
04024                                 {
04025                                         repeat_on = repeat_off = 0;
04026                                         repeat_init = 1;
04027                                 }
04028                                 else
04029                                 {
04030                                         FrameList[mapping] = reinterpret_cast<FRAMELIST*>(calloc(1, sizeof(FRAMELIST)));
04031                                         FrameList[mapping]->top = FrameList[mapping]->bottom = film;
04032                                         mapping ++;
04033                                 }
04034 
04035                                 if (repeat_on-repeat_off == 3)
04036                                 {
04037                                         repeat_on = repeat_off = 0;
04038                                         repeat_init = 1;
04039 
04040                                         FrameList[mapping] = reinterpret_cast<FRAMELIST*>(calloc(1, sizeof(FRAMELIST)));
04041                                         FrameList[mapping]->top = FrameList[mapping]->bottom = film;
04042                                         mapping ++;
04043                                 }
04044                         }
04045                 }
04046                 else
04047                 {
04048                         if (top)
04049                         {
04050                                 FrameList[ntsc]->bottom = film;
04051                                 ntsc ++;
04052                                 FrameList[ntsc] = reinterpret_cast<FRAMELIST*>(calloc(1, sizeof(FRAMELIST)));
04053                                 FrameList[ntsc]->top = film;
04054                         }
04055                         else if (bottom)
04056                         {
04057                                 FrameList[ntsc]->top = film;
04058                                 ntsc ++;
04059                                 FrameList[ntsc] = reinterpret_cast<FRAMELIST*>(calloc(1, sizeof(FRAMELIST)));
04060                                 FrameList[ntsc]->bottom = film;
04061                         }
04062                         else
04063                         {
04064                                 FrameList[ntsc] = reinterpret_cast<FRAMELIST*>(calloc(1, sizeof(FRAMELIST)));
04065                                 FrameList[ntsc]->top = film;
04066                                 FrameList[ntsc]->bottom = film;
04067                                 ntsc ++;
04068                         }
04069 
04070                         if (rff)
04071                         {
04072                                 if (!top && !bottom)
04073                                         FrameList[ntsc] = reinterpret_cast<FRAMELIST*>(calloc(1, sizeof(FRAMELIST)));
04074 
04075                                 if (tff)
04076                                 {
04077                                         FrameList[ntsc]->top = film;
04078                                         top = 1;
04079                                 }
04080                                 else
04081                                 {
04082                                         FrameList[ntsc]->bottom = film;
04083                                         bottom = 1;
04084                                 }
04085 
04086                                 if (top && bottom)
04087                                 {
04088                                         top = bottom = 0;
04089                                         ntsc ++;
04090                                 }
04091                         }
04092                 }
04093 
04094                 film ++;
04095         }
04096 
04097         out->VF_FrameBound = film;
04098         film -= 2;
04099 
04100         if (FO_Flag==FO_FILM)
04101         {
04102                 while (FrameList[mapping-1]->top >= film)
04103                         mapping --;
04104 
04105                 out->VF_FrameLimit = mapping;
04106         }
04107         else
04108         {
04109                 if (FO_Flag==FO_SWAP)
04110                 {
04111                         Field_Order = !Field_Order;
04112 
04113                         if (Field_Order)
04114                                 for (i=0; i<ntsc-1; i++)
04115                                         FrameList[i]->bottom = FrameList[i+1]->bottom;
04116                         else
04117                                 for (i=0; i<ntsc-1; i++)
04118                                         FrameList[i]->top = FrameList[i+1]->top;
04119                 }
04120 
04121                 while ((FrameList[ntsc-1]->top >= film) || (FrameList[ntsc-1]->bottom >= film))
04122                         ntsc --;
04123 
04124                 out->VF_FrameLimit = ntsc;
04125 
04126                 for (i=0; i<out->VF_FrameLimit-1; i++)
04127                         if (FrameList[i]->top==FrameList[i+1]->top || FrameList[i]->top==FrameList[i+1]->bottom ||
04128                                 FrameList[i]->bottom==FrameList[i+1]->top || FrameList[i]->bottom==FrameList[i+1]->bottom)
04129                         {
04130                                 FrameList[i]->forward = 1;
04131                                 FrameList[i+1]->backward = 1;
04132                         }
04133         }
04134 
04135         Full_Frame = 1;
04136         for (i=0; i<out->VF_FrameLimit; i++)
04137                 if (FrameList[i]->top!=FrameList[i]->bottom)
04138                 {
04139                         Full_Frame = 0;
04140                         break;
04141                 }
04142 
04143         out->VF_GOPNow = out->VF_GOPLimit = gop;
04144         out->VF_OldFrame = out->VF_FrameLimit;
04145         out->VF_FrameSize = Clip_Width * Clip_Height * 3;
04146 
04147         return 1;
04148 }
04149 
04150 void CMPEG2Dec::Decode(unsigned char *dst, DWORD frame, int pitch)
04151 {
04152         DWORD i, now, size, origin, ref, fo;
04153         int remain;
04154 
04155         CMPEG2Dec* in = this;
04156 
04157         if (FO_Flag==FO_FILM)
04158         {
04159                 fo = 0;
04160                 frame = FrameList[frame]->top;
04161         }
04162 
04163         origin = frame;
04164 
04165         if (FO_Flag!=FO_FILM)
04166         {
04167                 if (FrameList[frame]->top == FrameList[frame]->bottom)
04168                 {
04169                         fo = 0;
04170                         frame = FrameList[frame]->top;
04171                 }
04172                 else if (FrameList[frame]->top < FrameList[frame]->bottom)
04173                 {
04174                         fo = 1;
04175                         frame = FrameList[frame]->top;
04176                 }
04177                 else
04178                 {
04179                         fo = 2;
04180                         frame = FrameList[frame]->bottom;
04181                 }
04182         }
04183 
04184         ref = frame;
04185 
04186         if (frame >= GOPList[in->VF_GOPLimit-1]->number)
04187         {
04188                 now = in->VF_GOPLimit-1;
04189                 ref -= GOPList[in->VF_GOPLimit-1]->number;
04190                 size = in->VF_FrameBound - GOPList[in->VF_GOPLimit-1]->number + 1;
04191         }
04192         else
04193                 for (now = 0; now < (in->VF_GOPLimit-1); now++)
04194                 {
04195                         if (frame>=GOPList[now]->number && frame<GOPList[now+1]->number)
04196                         {
04197                                 ref -= GOPList[now]->number;
04198                                 size = GOPList[now+1]->number - GOPList[now]->number + 1;
04199                                 break;
04200                         }
04201                 }
04202 
04203         if (fo)
04204                 ref ++;
04205 
04206         if (now != in->VF_GOPNow)
04207         {
04208                 if ((in->VF_OldFrame + 1)==origin)
04209                 {
04210                         if (Full_Frame)
04211                         {
04212                                 Get_Hdr();
04213                                 Decode_Picture(1, dst, pitch);
04214 
04215                                 if (picture_structure!=FRAME_PICTURE)
04216                                 {
04217                                         Get_Hdr();
04218                                         Decode_Picture(1, dst, pitch);
04219                                 }
04220                         }
04221                         else
04222                                 switch (fo)
04223                                 {
04224                                         case 0:
04225                                                 if (!FrameList[origin]->backward)
04226                                                 {
04227                                                         Get_Hdr();
04228                                                         Decode_Picture(1, dst, pitch);
04229 
04230                                                         if (picture_structure!=FRAME_PICTURE)
04231                                                         {
04232                                                                 Get_Hdr();
04233                                                                 Decode_Picture(1, dst, pitch);
04234                                                         }
04235 
04236                                                         if (FrameList[origin]->forward)
04237                                                         {
04238                                                                 if (Field_Order)
04239                                                                         Copyodd(dst, dstFrame, pitch, 1);
04240                                                                 else
04241                                                                         Copyeven(dst, dstFrame, pitch, 1);
04242                                                         }
04243                                                 }
04244                                                 else
04245                                                 {
04246                                                         Copyodd(dstFrame, dst, pitch, 0);
04247                                                         Copyeven(dstFrame, dst, pitch, 0);
04248                                                 }
04249                                                 break;
04250 
04251                                         case 1:
04252                                                 Copyodd(dstFrame, dst, pitch, 0);
04253 
04254                                                 Get_Hdr();
04255                                                 Decode_Picture(1, dstFrame, DSTBYTES);
04256 
04257                                                 if (picture_structure!=FRAME_PICTURE)
04258                                                 {
04259                                                         Get_Hdr();
04260                                                         Decode_Picture(1, dstFrame, DSTBYTES);
04261                                                 }
04262 
04263                                                 Copyeven(dstFrame, dst, pitch, 0);
04264                                                 break;
04265 
04266                                         case 2: 
04267                                                 Copyeven(dstFrame, dst, pitch, 0);
04268 
04269                                                 Get_Hdr();
04270                                                 Decode_Picture(1, dstFrame, DSTBYTES);
04271 
04272                                                 if (picture_structure!=FRAME_PICTURE)
04273                                                 {
04274                                                         Get_Hdr();
04275                                                         Decode_Picture(1, dstFrame, DSTBYTES);
04276                                                 }
04277 
04278                                                 Copyodd(dstFrame, dst, pitch, 0);
04279                                                 break;
04280                                 }
04281 
04282                         if (in->VF_GOPSize)
04283                         {
04284                                 for (i=0; i < in->VF_GOPSize; i++)
04285                                         free(GOPBuffer[i]);
04286 
04287                                 in->VF_GOPSize = 0;
04288                         }
04289 
04290                         in->VF_GOPNow = in->VF_GOPLimit;
04291                         in->VF_OldFrame = origin;
04292                         return;
04293                 }
04294 
04295                 remain = ref;
04296                 in->VF_OldRef = ref;
04297                 in->VF_GOPNow = now;
04298                 Second_Field = 0;
04299 
04300                 if (size < in->VF_GOPSize)
04301                         for (i=0; i < (in->VF_GOPSize - size); i++)
04302                                 free(GOPBuffer[size+i]);
04303                 else if (size > in->VF_GOPSize)
04304                         for (i=0; i < (size - in->VF_GOPSize); i++)
04305                                 GOPBuffer[in->VF_GOPSize+i] = reinterpret_cast<unsigned char*>(malloc(in->VF_FrameSize));
04306 
04307                 in->VF_GOPSize = size;
04308 
04309                 File_Flag = GOPList[now]->file;
04310                 _lseeki64(Infile[GOPList[now]->file], GOPList[now]->position, SEEK_SET);
04311                 Initialize_Buffer();
04312 
04313                 while (Get_Hdr() && picture_coding_type!=I_TYPE);
04314 
04315                 Decode_Picture(0, dst, pitch);
04316 
04317                 while (Get_Hdr() && picture_coding_type==B_TYPE);
04318 
04319                 if (picture_structure!=FRAME_PICTURE)
04320                 {
04321                         Decode_Picture(0, dst, pitch);
04322                         Get_Hdr();
04323                 }
04324 
04325                 Decode_Picture(1, dst, pitch);
04326 
04327                 if (picture_structure!=FRAME_PICTURE)
04328                 {
04329                         Get_Hdr();
04330                         Decode_Picture(1, dst, pitch);
04331                 }
04332 
04333                 Copyodd(dst, GOPBuffer[0], pitch, 1);
04334                 Copyeven(dst, GOPBuffer[0], pitch, 1);
04335 
04336                 while (remain && Get_Hdr())
04337                 {
04338                         Decode_Picture(1, dst, pitch);
04339 
04340                         if (picture_structure!=FRAME_PICTURE)
04341                         {
04342                                 Get_Hdr();
04343                                 Decode_Picture(1, dst, pitch);
04344                         }
04345 
04346                         Copyodd(dst, GOPBuffer[ref - remain + 1], pitch, 1);
04347                         Copyeven(dst, GOPBuffer[ref - remain + 1], pitch, 1);
04348 
04349                         remain--;
04350                 }
04351 
04352                 if (!Full_Frame && ref>=(size-2))
04353                 {
04354                         Copyodd(dst, dstFrame, pitch, 1);
04355                         Copyeven(dst, dstFrame, pitch, 1);
04356                 }
04357         }
04358         else
04359         {
04360                 remain = ref - in->VF_OldRef;
04361 
04362                 if (remain > 0)
04363                 {
04364                         in->VF_OldRef = ref;
04365 
04366                         while (remain && Get_Hdr())
04367                         {
04368                                 Decode_Picture(1, dst, pitch);
04369 
04370                                 if (picture_structure!=FRAME_PICTURE)
04371                                 {
04372                                         Get_Hdr();
04373                                         Decode_Picture(1, dst, pitch);
04374                                 }
04375 
04376                                 Copyodd(dst, GOPBuffer[ref - remain + 1], pitch, 1);
04377                                 Copyeven(dst, GOPBuffer[ref - remain + 1], pitch, 1);
04378 
04379                                 remain--;
04380                         }
04381 
04382                         if (!Full_Frame && ref>=(size-2))
04383                         {
04384                                 Copyodd(dst, dstFrame, pitch, 1);
04385                                 Copyeven(dst, dstFrame, pitch, 1);
04386                         }
04387                 }
04388         }
04389 
04390         switch (fo)
04391         {
04392                 case 0:
04393                         Copyodd(GOPBuffer[ref], dst, pitch, 0);
04394                         Copyeven(GOPBuffer[ref], dst, pitch, 0);
04395                         break;
04396 
04397                 case 1:
04398                         Copyodd(GOPBuffer[ref-1], dst, pitch, 0);
04399                         Copyeven(GOPBuffer[ref], dst, pitch, 0);
04400                         break;
04401 
04402                 case 2:
04403                         Copyodd(GOPBuffer[ref], dst, pitch, 0);
04404                         Copyeven(GOPBuffer[ref-1], dst, pitch, 0);
04405                         break;
04406         }
04407 
04408         in->VF_OldFrame = origin;
04409 }
04410 
04411 void CMPEG2Dec::Close()
04412 {
04413         int i;
04414 
04415         for(i = 0; i < VF_GOPLimit; i++) free(GOPList[i]);
04416         for(i = 0; i < VF_FrameLimit; i++) free(FrameList[i]);
04417 
04418         if (VF_File != NULL)
04419                 fclose(VF_File);
04420 
04421         while (VF_GOPSize)
04422         {
04423                 VF_GOPSize--;
04424                 free(GOPBuffer[VF_GOPSize]);
04425         }
04426 
04427         while (File_Limit)
04428         {
04429                 File_Limit--;
04430                 _close(Infile[File_Limit]);
04431         }
04432 
04433         for (i=0; i<MAX_FILE_NUMBER; i++)
04434                 delete [] Infilename[i];
04435 
04436         for (i=0; i<3; i++)
04437         {
04438                 delete [] backward_reference_frame[i];
04439                 delete [] forward_reference_frame[i];
04440                 delete [] auxframe[i];
04441         }
04442 
04443         delete [] u422;
04444         delete [] v422;
04445         delete [] u444;
04446         delete [] v444;
04447         delete [] dstFrame;
04448 
04449         if(Luminance_Flag)
04450                 delete [] lum;
04451 
04452         for (i=0; i<8; i++)
04453                 delete [] p_block[i];
04454 
04455         delete [] p_fTempArray;
04456 
04457         if (hLibrary)
04458                 FreeLibrary(hLibrary);
04459 }
04460 
04461 void CMPEG2Dec::Copyodd(unsigned char *src, unsigned char *dst, int pitch, int forward)
04462 {
04463         int i;
04464         int PWIDTH = forward ? (pitch<<1) : DSTBYTES2;
04465         int QWIDTH = forward ? DSTBYTES2 : (pitch<<1);
04466 
04467         for (i=0; i<(Clip_Height>>1); i++)
04468         {
04469                 memcpy (dst, src, DSTBYTES);
04470                 src += PWIDTH;
04471                 dst += QWIDTH;
04472         }
04473 }
04474 
04475 void CMPEG2Dec::Copyeven(unsigned char *src, unsigned char *dst, int pitch, int forward)
04476 {
04477         int i;
04478         int PWIDTH = forward ? (pitch<<1) : DSTBYTES2;
04479         int QWIDTH = forward ? DSTBYTES2 : (pitch<<1);
04480         src += forward ? pitch : DSTBYTES;
04481         dst += forward ? DSTBYTES : pitch;
04482 
04483         for (i=0; i<(Clip_Height>>1); i++)
04484         {
04485                 memcpy (dst, src, DSTBYTES);
04486                 src += PWIDTH;
04487                 dst += QWIDTH;
04488         }
04489 }

Generated on Tue Dec 13 14:47:25 2005 for guliverkli by  doxygen 1.4.5