00001 #pragma once
00002
00003 #include <windows.h>
00004 #include <winreg.h>
00005 #include <stdio.h>
00006 #include <io.h>
00007 #include <fcntl.h>
00008
00009
00010 #define PICTURE_START_CODE 0x100
00011 #define SLICE_START_CODE_MIN 0x101
00012 #define SLICE_START_CODE_MAX 0x1AF
00013 #define USER_DATA_START_CODE 0x1B2
00014 #define SEQUENCE_HEADER_CODE 0x1B3
00015 #define EXTENSION_START_CODE 0x1B5
00016 #define SEQUENCE_END_CODE 0x1B7
00017 #define GROUP_START_CODE 0x1B8
00018
00019 #define SYSTEM_END_CODE 0x1B9
00020 #define PACK_START_CODE 0x1BA
00021 #define SYSTEM_START_CODE 0x1BB
00022 #define PRIVATE_STREAM_1 0x1BD
00023 #define VIDEO_ELEMENTARY_STREAM 0x1E0
00024
00025
00026 #define SEQUENCE_EXTENSION_ID 1
00027 #define SEQUENCE_DISPLAY_EXTENSION_ID 2
00028 #define QUANT_MATRIX_EXTENSION_ID 3
00029 #define COPYRIGHT_EXTENSION_ID 4
00030 #define PICTURE_DISPLAY_EXTENSION_ID 7
00031 #define PICTURE_CODING_EXTENSION_ID 8
00032
00033 #define ZIG_ZAG 0
00034 #define MB_WEIGHT 32
00035 #define MB_CLASS4 64
00036
00037 #define I_TYPE 1
00038 #define P_TYPE 2
00039 #define B_TYPE 3
00040
00041 #define TOP_FIELD 1
00042 #define BOTTOM_FIELD 2
00043 #define FRAME_PICTURE 3
00044
00045 #define MACROBLOCK_INTRA 1
00046 #define MACROBLOCK_PATTERN 2
00047 #define MACROBLOCK_MOTION_BACKWARD 4
00048 #define MACROBLOCK_MOTION_FORWARD 8
00049 #define MACROBLOCK_QUANT 16
00050
00051 #define MC_FIELD 1
00052 #define MC_FRAME 2
00053 #define MC_16X8 2
00054 #define MC_DMV 3
00055
00056 #define MV_FIELD 0
00057 #define MV_FRAME 1
00058
00059 #define CHROMA420 1
00060 #define CHROMA422 2
00061 #define CHROMA444 3
00062
00063 #define BUFFER_SIZE 2048
00064 #define MAX_FILE_NUMBER 256
00065
00066 #define IDCT_MMX 1
00067 #define IDCT_SSEMMX 2
00068 #define IDCT_FPU 3
00069 #define IDCT_REF 4
00070
00071 #define FO_NONE 0
00072 #define FO_FILM 1
00073 #define FO_SWAP 2
00074
00075
00076 typedef void (WINAPI *PBufferOp) (unsigned char*, int, int);
00077
00078 #define MAX_FRAME_NUMBER 1000000
00079 #define MAX_GOP_SIZE 1024
00080
00081
00082 class CMPEG2Dec
00083 {
00084 protected:
00085
00086
00087 void Initialize_Buffer();
00088 void Fill_Buffer();
00089 void Next_Packet();
00090 void Flush_Buffer_All(unsigned int N);
00091 unsigned int Get_Bits_All(unsigned int N);
00092 void Next_File();
00093
00094 unsigned int Show_Bits(unsigned int N);
00095 unsigned int Get_Bits(unsigned int N);
00096 void Flush_Buffer(unsigned int N);
00097 void Fill_Next();
00098 unsigned int Get_Byte();
00099 unsigned int Get_Short();
00100 void next_start_code();
00101
00102 unsigned char Rdbfr[BUFFER_SIZE], *Rdptr, *Rdmax;
00103 unsigned int CurrentBfr, NextBfr, BitsLeft, Val, Read;
00104
00105
00106 int Get_Hdr();
00107 void sequence_header();
00108 int slice_header();
00109 private:
00110 void group_of_pictures_header();
00111 void picture_header();
00112 void sequence_extension();
00113 void sequence_display_extension();
00114 void quant_matrix_extension();
00115 void picture_display_extension();
00116 void picture_coding_extension();
00117 void copyright_extension();
00118 int extra_bit_information();
00119 void extension_and_user_data();
00120
00121 protected:
00122
00123 void Decode_Picture(int ref, unsigned char *dst, int pitch);
00124 private:
00125 void Update_Picture_Buffers();
00126 void picture_data();
00127 int slice(int MBAmax);
00128 void macroblock_modes(int *pmacroblock_type, int *pmotion_type,
00129 int *pmotion_vector_count, int *pmv_format, int *pdmv, int *pmvscale, int *pdct_type);
00130 void Clear_Block(int count);
00131 void Add_Block(int count, int bx, int by, int dct_type, int addflag);
00132 void motion_compensation(int MBA, int macroblock_type, int motion_type,
00133 int PMV[2][2][2], int motion_vertical_field_select[2][2], int dmvector[2], int dct_type);
00134 void skipped_macroblock(int dc_dct_pred[3], int PMV[2][2][2],
00135 int *motion_type, int motion_vertical_field_select[2][2], int *macroblock_type);
00136 int start_of_slice(int *MBA, int *MBAinc, int dc_dct_pred[3], int PMV[2][2][2]);
00137 int decode_macroblock(int *macroblock_type, int *motion_type, int *dct_type,
00138 int PMV[2][2][2], int dc_dct_pred[3], int motion_vertical_field_select[2][2], int dmvector[2]);
00139 void Decode_MPEG2_Intra_Block(int comp, int dc_dct_pred[]);
00140 void Decode_MPEG2_Non_Intra_Block(int comp);
00141
00142 int Get_macroblock_type();
00143 int Get_I_macroblock_type();
00144 int Get_P_macroblock_type();
00145 int Get_B_macroblock_type();
00146 int Get_D_macroblock_type();
00147 int Get_coded_block_pattern();
00148 int Get_macroblock_address_increment();
00149 int Get_Luma_DC_dct_diff();
00150 int Get_Chroma_DC_dct_diff();
00151
00152 void form_predictions(int bx, int by, int macroblock_type, int motion_type,
00153 int PMV[2][2][2], int motion_vertical_field_select[2][2], int dmvector[2]);
00154 void form_prediction(unsigned char *src[], int sfield, unsigned char *dst[], int dfield,
00155 int lx, int lx2, int w, int h, int x, int y, int dx, int dy, int average_flag);
00156 void form_component_prediction(unsigned char *src, unsigned char *dst,
00157 int lx, int lx2, int w, int h, int x, int y, int dx, int dy, int average_flag);
00158
00159
00160 void motion_vectors(int PMV[2][2][2], int dmvector[2], int motion_vertical_field_select[2][2],
00161 int s, int motion_vector_count, int mv_format,
00162 int h_r_size, int v_r_size, int dmv, int mvscale);
00163 void Dual_Prime_Arithmetic(int DMV[][2], int *dmvector, int mvx, int mvy);
00164 private:
00165 void motion_vector(int *PMV, int *dmvector, int h_r_size, int v_r_size,
00166 int dmv, int mvscale, int full_pel_vector);
00167 void decode_motion_vector(int *pred, int r_size, int motion_code,
00168 int motion_residualesidual, int full_pel_vector);
00169 int Get_motion_code();
00170 int Get_dmvector();
00171
00172 protected:
00173
00174 void assembleFrame(unsigned char *src[], int pf, unsigned char *dst, int pitch);
00175 private:
00176 void Luminance_Filter(unsigned char *src, unsigned char *dst);
00177 void conv420to422(unsigned char *src, unsigned char *dst, int frame_type);
00178 void conv422to444(unsigned char *src, unsigned char *dst);
00179 void conv444toRGB24(unsigned char *py, unsigned char *pu, unsigned char *pv, unsigned char *dst, int pitch);
00180 void conv422toYUY2(unsigned char *py, unsigned char *pu, unsigned char *pv, unsigned char *dst, int pitch);
00181
00182 protected:
00183
00184 int Fault_Flag;
00185 int File_Flag;
00186 int File_Limit;
00187 int FO_Flag;
00188 int IDCT_Flag;
00189 int SystemStream_Flag;
00190
00191 int Luminance_Flag;
00192 int Resize_Flag;
00193
00194 int KeyOp_Flag;
00195 int lfsr0, lfsr1;
00196 PBufferOp BufferOp;
00197
00198 int Infile[MAX_FILE_NUMBER];
00199 char *Infilename[MAX_FILE_NUMBER];
00200
00201 int intra_quantizer_matrix[64];
00202 int non_intra_quantizer_matrix[64];
00203 int chroma_intra_quantizer_matrix[64];
00204 int chroma_non_intra_quantizer_matrix[64];
00205
00206 int load_intra_quantizer_matrix;
00207 int load_non_intra_quantizer_matrix;
00208 int load_chroma_intra_quantizer_matrix;
00209 int load_chroma_non_intra_quantizer_matrix;
00210
00211 int q_scale_type;
00212 int alternate_scan;
00213 int quantizer_scale;
00214
00215 void *fTempArray, *p_fTempArray;
00216 short *block[8], *p_block[8];
00217 int pf_backward, pf_forward, pf_current;
00218
00219
00220 unsigned char *backward_reference_frame[3], *forward_reference_frame[3];
00221 unsigned char *auxframe[3], *current_frame[3];
00222 unsigned char *u422, *v422, *u444, *v444, *lum;
00223 unsigned char *dstFrame;
00224 __int64 RGB_Scale, RGB_Offset, RGB_CRV, RGB_CBU, RGB_CGX, LumOffsetMask, LumGainMask;
00225
00226 int HALF_WIDTH, PROGRESSIVE_HEIGHT, INTERLACED_HEIGHT, DOUBLE_WIDTH;
00227 int HALF_WIDTH_D8, LUM_AREA, CLIP_AREA, HALF_CLIP_AREA, CLIP_STEP;
00228 int DSTBYTES, DSTBYTES2;
00229 public:
00230 int Clip_Width, Clip_Height, Resize_Width, Resize_Height;
00231 protected:
00232
00233 int Coded_Picture_Width, Coded_Picture_Height, Chroma_Width, Chroma_Height;
00234 int block_count, Second_Field;
00235 int horizontal_size, vertical_size, mb_width, mb_height;
00236
00237
00238 int progressive_sequence;
00239 int chroma_format;
00240
00241
00242 int picture_coding_type;
00243 int temporal_reference;
00244
00245
00246 int f_code[2][2];
00247 int picture_structure;
00248 int frame_pred_frame_dct;
00249 int progressive_frame;
00250 int concealment_motion_vectors;
00251 int intra_dc_precision;
00252 int top_field_first;
00253 int repeat_first_field;
00254 int intra_vlc_format;
00255
00256
00257 typedef struct {
00258 DWORD number;
00259 int file;
00260 __int64 position;
00261 } GOPLIST;
00262 GOPLIST *GOPList[MAX_FRAME_NUMBER];
00263
00264 typedef struct {
00265 DWORD top;
00266 DWORD bottom;
00267 char forward;
00268 char backward;
00269 } FRAMELIST;
00270 FRAMELIST *FrameList[MAX_FRAME_NUMBER];
00271
00272 unsigned char *GOPBuffer[MAX_GOP_SIZE];
00273 public:
00274 BOOL Field_Order, Full_Frame;
00275 protected:
00276 HINSTANCE hLibrary;
00277
00278 void Copyodd(unsigned char *src, unsigned char *dst, int pitch, int forward);
00279 void Copyeven(unsigned char *src, unsigned char *dst, int pitch, int forward);
00280 public:
00281 FILE *VF_File;
00282 int VF_FrameRate;
00283 DWORD VF_FrameLimit;
00284 DWORD VF_FrameBound;
00285 DWORD VF_GOPLimit;
00286 DWORD VF_GOPNow;
00287 DWORD VF_GOPSize;
00288 int VF_FrameSize;
00289 DWORD VF_OldFrame;
00290 DWORD VF_OldRef;
00291
00292 enum DstFormat {
00293 RGB24, YUY2
00294 };
00295 DstFormat m_dstFormat;
00296
00297 CMPEG2Dec();
00298 ~CMPEG2Dec() {Close();}
00299 int Open(const char *path, DstFormat);
00300 void Close();
00301 void Decode(unsigned char *dst, DWORD frame, int pitch);
00302 bool dstRGB24() const { return m_dstFormat == RGB24; }
00303 bool dstYUY2() const { return m_dstFormat == YUY2; }
00304 };