/* global.h, global variables                                               */

/* Copyright (C) 1996, MPEG Software Simulation Group. All Rights Reserved. */

/*
 * Disclaimer of Warranty
 *
 * These software programs are available to the user without any license fee or
 * royalty on an "as is" basis.  The MPEG Software Simulation Group disclaims
 * any and all warranties, whether express, implied, or statuary, including any
 * implied warranties or merchantability or of fitness for a particular
 * purpose.  In no event shall the copyright-holder be liable for any
 * incidental, punitive, or consequential damages of any kind whatsoever
 * arising from the use of these programs.
 *
 * This disclaimer of warranty extends to the user of these programs and user's
 * customers, employees, agents, transferees, successors, and assigns.
 *
 * The MPEG Software Simulation Group does not represent or warrant that the
 * programs furnished hereunder are free of infringement of any third-party
 * patents.
 *
 * Commercial implementations of MPEG-1 and MPEG-2 video, including shareware,
 * are subject to royalty fees to patent holders.  Many of these patents are
 * general enough such that they are unavoidable regardless of implementation
 * design.
 *
 * MPEG2AVI
 * --------
 * v0.16B34 introduced g_scan[2][64], which is set during decoder 
 *           initialization, this allows MPEG2AVI to efficiently handle
 *           transposing and non-transposing iDCT routines
 *          multi-threaded read_ahead() mechanism allows for larger buffer,
 *          MPV_BSBUFSIZE set to 64KB
 * v0.16B33 reordered struct layer_data, prefixed all bitstream vars with
 *          "bs_...", added "bs_Buflevel" 
 *         removed demux_queue from MPEG2AVI, eliminates redundant data copy
 *         changed MPV_BSBUFSIZE to 32768
 *         renamed the int32/fpu64 IDCT functions to be consistent with
 *         the rest of the project's naming conventions.
 * v0.16B32 renamed functions in vtitler.c, added parameter for dwXpos
 * v0.16B31 added struct for mpeg_picture_info header (mpv_pinfo), and two
 *    global variables for (g_mpvpcur, g_mpvplast) to describe the current 
 *    and previous displayed frames (displayed, not coded!)
 *    retired g_dpictref, g_dpictype (replaced by struct mpv_pinfo )
 * v0.16B3  added global variables for g_dwSystemPackLBA, g_dpictref, 
 *          g_dpictype, g_dtimecode.  They are domained in "display-order" 
 *          (not coded bitstream order!)  gethdr.c sets these vars, and
 *          store.c reads these vars.
 * v0.16B23 inlined some functions, increased getbits.c's buffering to 8192
 *          bytes (now set as a #define MPV_BSBUFSIZE), moved color-space
 *          conversion to file [ yuv2rgb.c ]
 *
 * v0.16B22 introduction of MMX optimizations...MPEG2V12 now *REQUIRES* MMX
 *          added idct_mmx_aan() , the AAN-MMX IDCT algorithm requires
 *          a transpose operation for correct orientation.  The necessary
 *          transpose is integrated with the zigzag scan-order in getblk.c
 *          (MPEG2AVI must choose the transposed-scan order if AAN-IDCT is in
 *          use)
 *          store.c now calls y444rgb24() to perform YUV->RGB conversion
 *          idct_mmx_chen1() [ idctchen.c]
 * v0.16B21 fixed stupid, stupid bug in BBDMUX v1.2 [ exit() caused problems ]
 * v0.16B2 added frame-code print in upper left corner, UserOptPrintFrameCode,
 *          struct tgoptimecode (for recording timecode in GOP header)
 *          vtitler.c,
 *          UserOptVOBStartLBA (VOB only)
 *			UserOptVOBStreamID (VOB only)
 * v0.16B1 store.c converted to C++,
 *         retired g_AVIrate, replaced by UserOptAVIrate,
 *         added UserOptAVISplitLength for auto AVI-file splitting
 *         added UserOptAVISkipEvery for dropping 1 frame every N frames
 *         avi_fileopen(), avi_fileclose(), and store_one_avi() are now
 *         directed to aviinit.cpp, a wrapper to access cAVIwriter
 * v0.15B51 added UserOptFRangeStart * UserOptFRangeEnd, for user-selected 
 *          frame encoding range, g_cmd_buttonStop to support this function
 * v0.15B5 added new file icm_emul.c (emulates ICSeqCompress...),
 *         added shrink422()
 *         T_AVI_YUY2 for YUY2 mode
 * v0.15B4 shrink420() in vresize.c, hid shrink888x, 888y, 888xy from public.
 *        The public-interface is just one function "shrink888()",
 *        added functions/constants for YV12 AVI output
 * v0.15  X and Y resizing : shrink888...() located in vresize.c
 *        added some #define for keyboard control during output mode -o6 (GDI)
 *        pausing/playing, IVTC on/off and IVTC +/-, toggling fast/FIR filter
 *        g_cmd_ivtc, g_cmd_filter
 *        created new useropt.h -(struct tUserOption )for capturing 
 *        & managing user-options.  Reduces # global variables in source-code.
 *        modified conv420to422, conv422to444 (simul two plane conversion )
 *
 * v0.14  added functions for resolution - halving
 */

#include "mpeg2dec.h"
#include "useropt.h"
#include "yuv2yuv.h"
#include <windows.h>
#include <vfw.h>
#include <time.h> // added px3

/* choose between declaration (GLOBAL undefined)
 * and definition (GLOBAL defined)
 * GLOBAL is defined in exactly one file mpeg2dec.c)
 */

#ifndef GLOBAL
#define EXTERN extern
#else
#define EXTERN
#endif

#define MPV_BSBUFSIZE (65536) // buffer size for getbits.c package

/* prototypes of global functions */
/* readpic.c */
void Substitute_Frame_Buffer _ANSI_ARGS_ ((int bitstream_framenum, 
  int sequence_framenum));

/* Get_Bits.c */
void Initialize_Buffer _ANSI_ARGS_((void));
void Fill_Buffer _ANSI_ARGS_((void));
/* v0.16B33 macro-ized functions for faster performance */
#define Show_Bits(N) ( ld->bs_Bfr >> (32-N) )
#define Get_Bits1() (Get_Bits(1))
//unsigned int Show_Bits _ANSI_ARGS_((int n));
//unsigned int Get_Bits1 _ANSI_ARGS_((void));
void Flush_Buffer _ANSI_ARGS_((int n));
unsigned int Get_Bits _ANSI_ARGS_((int n));
int Get_Byte _ANSI_ARGS_((void));
int Get_Word _ANSI_ARGS_((void));

/* systems.c */
void Next_Packet _ANSI_ARGS_((void));
int Get_Long _ANSI_ARGS_((void));
void Flush_Buffer32 _ANSI_ARGS_((void));
unsigned int Get_Bits32 _ANSI_ARGS_((void));


/* getblk.c */
void Decode_MPEG1_Intra_Block _ANSI_ARGS_((int comp, int dc_dct_pred[]));
void Decode_MPEG1_Non_Intra_Block _ANSI_ARGS_((int comp));
void Decode_MPEG2_Intra_Block _ANSI_ARGS_((int comp, int dc_dct_pred[]));
void Decode_MPEG2_Non_Intra_Block _ANSI_ARGS_((int comp));
// 0.16B22 mmx versions prepare IDCT-data for AAN MMX IDCT transform.
// see getblk.c for details on the differences between MMX and non-MMX
// Decode_Block functions

// 0.16B33 the AAN-IDCT now has transpose-operation built-in. these 
// functions are removed
//void Decode_MPEG1_Intra_Block_mmx _ANSI_ARGS_((int comp, int dc_dct_pred[]));
//void Decode_MPEG1_Non_Intra_Block_mmx _ANSI_ARGS_((int comp));
//void Decode_MPEG2_Intra_Block_mmx _ANSI_ARGS_((int comp,int dc_dct_pred[]));
//void Decode_MPEG2_Non_Intra_Block_mmx _ANSI_ARGS_((int comp));

/* gethdr.c */
int Get_Hdr _ANSI_ARGS_((void));
void next_start_code _ANSI_ARGS_((void));
int slice_header _ANSI_ARGS_((void));
void marker_bit _ANSI_ARGS_((char *text));

/* getpic.c */
void Decode_Picture _ANSI_ARGS_((int bitstream_framenum, 
  int sequence_framenum));
void Output_Last_Frame_of_Sequence _ANSI_ARGS_((int framenum));

/* getvlc.c */
int Get_macroblock_type _ANSI_ARGS_((void));
int Get_motion_code _ANSI_ARGS_((void));
int Get_dmvector _ANSI_ARGS_((void));
int Get_coded_block_pattern _ANSI_ARGS_((void));
int Get_macroblock_address_increment _ANSI_ARGS_((void));
int Get_Luma_DC_dct_diff _ANSI_ARGS_((void));
int Get_Chroma_DC_dct_diff _ANSI_ARGS_((void));

/* idct.c */
//void Fast_IDCT _ANSI_ARGS_((short *block)); // renamed to idct_int32()
void idct_int32 _ANSI_ARGS_((short *block)); 
void init_int32_idct _ANSI_ARGS_((void));

/* idct_aan.c */
void idct_mmx_aan _ANSI_ARGS_(( short *src_result ));  //v0.16B22

/* idctchen.c */
void idct_mmx_chen1 _ANSI_ARGS_(( short *src_result ));  //v0.16B22

/* idctmm32.c */
void idct_mmx32 _ANSI_ARGS_(( short *src_result ));  //v0.16B33

/* Reference_IDCT.c */
void init_idct_ref64 _ANSI_ARGS_((void));
void idct_ref64 _ANSI_ARGS_((short *block));

/* motion.c */
void motion_vectors _ANSI_ARGS_((int PMV[2][2][2], int dmvector[2],
  int motion_vertical_field_select[2][2], int s, int motion_vector_count, 
  int mv_format, int h_r_size, int v_r_size, int dmv, int mvscale));
void motion_vector _ANSI_ARGS_((int *PMV, int *dmvector,
  int h_r_size, int v_r_size, int dmv, int mvscale, int full_pel_vector));
void Dual_Prime_Arithmetic _ANSI_ARGS_((int DMV[][2], int *dmvector, int mvx, int mvy));

/* mpeg2dec.c */
void Error _ANSI_ARGS_((char *text));
void Warning _ANSI_ARGS_((char *text));
void Print_Bits _ANSI_ARGS_((int code, int bits, int len));

/* recon.c */
void form_predictions _ANSI_ARGS_((int bx, int by, int macroblock_type, 
  int motion_type, int PMV[2][2][2], int motion_vertical_field_select[2][2], 
  int dmvector[2], int stwtype));

/* spatscal.c */
//void Spatial_Prediction _ANSI_ARGS_((void));

/* store.cpp */
//void Write_Frame _ANSI_ARGS_((unsigned char *src[], int frame));
void Write_Frame (unsigned char *src[], int frame);

int rstrcmpi( const char a[], const char b[] );

#ifdef DISPLAY
/* display.c */
void Initialize_Display_Process _ANSI_ARGS_((char *name));
void Terminate_Display_Process _ANSI_ARGS_((void));
void Display_Second_Field _ANSI_ARGS_((void));
void dither _ANSI_ARGS_((unsigned char *src[]));
void Initialize_Dither_Matrix _ANSI_ARGS_((void));
#endif

/* rsl - aviinit.c */
/* due to some reorganization, these functions are now in a file which
   converts the calls to a C++ object cAVIwriter */
#ifdef _AVIINIT_CPP
#define HEHE extern "C"
#else
#define HEHE 
#endif


//SVE 20.05.2001 Neuer Parameter: Erzwungener Keyframe
//HEHE DWORD store_avi_one( const char aviFilename[], unsigned char *source_frame,
//	LPRECT CropRect, long FrameNumber );
HEHE DWORD store_avi_one( const char aviFilename[], unsigned char *source_frame,
	LPRECT CropRect, long FrameNumber, BOOL bKeyFrame );
//SVE ENDE
HEHE DWORD avi_fileopen( const char aviFilename[], const char szName[], LPRECT
	CropRect, BITMAPINFO bmi, tUserOption UserOptFPS, tUserOption 
	UserOptAVISplitLength, BOOL fCodecSelect );
	// 0.15B51 opened to public
HEHE DWORD avi_fileclose( void );

/* icm_emul.c */
BOOL _ICSeqCompressFrameStart( PCOMPVARS pc, LPBITMAPINFO lpbiIn );
LPVOID _ICSeqCompressFrame( COMPVARS *lpcompvars, UINT uiFlags, 
	LPVOID lpBits,BOOL *pfKey, LONG *plSize );

/* rsl - telecine.c */

  /* inverse_telecine() has been retired */

  /* ivtc 8bpp frame, perform downsample 1/2 resolution */
unsigned char * ivtc_half_8bpp( unsigned char y_src[], long FrameNum );
  /* ivtc 8bpp frame, full resolution */
unsigned char * ivtc_full_8bpp( unsigned char y_src[], long FrameNum );

/* rsl - vidfilt.c */
/*
void conv422to444 _ANSI_ARGS_((unsigned char *src, unsigned char *dst));
void conv420to422 _ANSI_ARGS_((unsigned char *src, unsigned char *dst));
void fast420to444(unsigned char *src,unsigned char *dst );
*/
void conv422to444 _ANSI_ARGS_((unsigned char *src1, unsigned char *src2, 
							  unsigned char *dst1, unsigned char *dst2,
							  long dstW, long dstH ););
void conv420to422 _ANSI_ARGS_((unsigned char *src1, unsigned char *src2, 
							  unsigned char *dst1, unsigned char *dst2,
							  long dstW, long dstH ) );
void fast420to444(unsigned char *src1,unsigned char *src2,unsigned char *dst1,
				  unsigned char *dst2, long dstW, long dstH );

    /* these two filters were pulled from MPEG2V12 encoder's readpic.c */
void conv444to422 _ANSI_ARGS_((unsigned char *src1,unsigned char *src2,
							unsigned char *dst1,unsigned char *dst2,
							long srcW, long srcH ));
void conv422to420 _ANSI_ARGS_((unsigned char *src1,unsigned char *src2,
							unsigned char *dst1,unsigned char *dst2,
							long srcW, long srcH ));

/* rsl - vresize.c */
void shrink_frame( unsigned char dest1bmp[], unsigned char dest2bmp[],
	unsigned char dest3bmp[], const unsigned char src1bmp[], 
	const unsigned char src2bmp[], const unsigned char src3bmp[],
	LPRECT lpsrcRect, LPRECT lpdstRect, DWORD format );
void half420to420( unsigned char y_dest[], unsigned char u_dest[], 
	unsigned char v_dest[], unsigned const char y_src[], 
	unsigned const char u_src[],unsigned const char v_src[], 
	LPRECT lpsrcRect );
void downsample_half( unsigned char dest1[], unsigned char dest2[],
	unsigned const char src1[], unsigned const char src2[], 
	long srcW, long srcH );
BOOL process_resize_cmd( void );

/* rsl - vtitler.c */
void vt_printstr( DWORD pbmp[], char szText[], DWORD dwXpos, DWORD dwYpos,
	DWORD dwStride );
void vt_printnum( DWORD pbmp[], DWORD number, DWORD dwXpos, DWORD dwYpos, 
	DWORD dwStride );

/* rsl - windisp.c */
void display_dib( unsigned char *source_frame, LPRECT lpCropRect, long FrameNum );

/* rsl - yuv2yuv.c */
void yuv2yuv( tImageOpBlock *convBlock );	// convert (YUVplanar -> YUVplanar)
void yuv420toyv12( tImageOpBlock *convBlock ); // convert planar -> contiguous
void yuv422toyuy2( tImageOpBlock *convBlock ); // convert planar -> contiguous

/* rsl - yuv2rgb.c */
void yuv444torgb24( tImageOpBlock *pConvBlock );
//void y444rgb24( unsigned char *fbout, unsigned char *y_src, 
//	unsigned char *u_src, unsigned char *v_src, long srcW, long srcH,
//	LPRECT RCrop ); // v0.16B23
void y420rgb24( unsigned char fb[], LPRECT RCrop ); 
	/* nonstandard YUV420->RGB*/


/* global variables */

/* the following two vars exchange data between main() and avistore() */
//#define _ASK_USER -1

//EXTERN int g_AVIrate	/* AVI file frame-rate (fps) */
//#ifdef GLOBAL
//  = _ASK_USER
//#endif
;



EXTERN char Version[]
#ifdef GLOBAL
  ="MPEG2AVI 0.16B35"
#endif
;

EXTERN char Author[]
#ifdef GLOBAL
  ="(C) 1996, MPEG Software Simulation Group"
#endif
;


/* v0.16B34 non-transpose scan patterns for zig-zag and alternate */
EXTERN const unsigned char scan_no[2][64]
#ifdef GLOBAL
=
{
  { /* Zig-Zag scan pattern  */
    0,1,8,16,9,2,3,10,17,24,32,25,18,11,4,5,
    12,19,26,33,40,48,41,34,27,20,13,6,7,14,21,28,
    35,42,49,56,57,50,43,36,29,22,15,23,30,37,44,51,
    58,59,52,45,38,31,39,46,53,60,61,54,47,55,62,63
  },
  { /* Alternate scan pattern */
    0,8,16,24,1,9,2,10,17,25,32,40,48,56,57,49,
    41,33,26,18,3,11,4,12,19,27,34,42,50,58,35,43,
    51,59,20,28,5,13,6,14,21,29,36,44,52,60,37,45,
    53,61,22,30,7,15,23,31,38,46,54,62,39,47,55,63
  }
}
#endif
;

/* v0.16B34 transpose scan order for MMX IDCT */
EXTERN const unsigned char scan_tr[2][64]
#ifdef GLOBAL
=
{
	{ /* Zig-Zag scan pattern  */
		 0,  8,  1,  2,  9, 16, 24, 17,
		10,  3,  4, 11, 18, 25, 32, 40,
		33, 26, 19, 12,  5,  6, 13, 20,
		27, 34, 41, 48, 56, 49, 42, 35,
		28, 21, 14,  7, 15, 22, 29, 36,
		43, 50, 57, 58, 51, 44, 37, 30,
		23, 31, 38, 45, 52, 59, 60, 53,
		46, 39, 47, 54, 61, 62, 55, 63
	},
	{ /* Alternate scan pattern */
		0,  1,  2,  3,  8,  9, 16, 17,
		10, 11,  4,  5,  6,  7, 15, 14,
		13, 12, 19, 18, 24, 25, 32, 33,
		26, 27, 20, 21, 22, 23, 28, 29,
		30, 31, 34, 35, 40, 41, 48, 49,
		42, 43, 36, 37, 38, 39, 44, 45,
		46, 47, 50, 51, 56, 57, 58, 59,
		52, 53, 54, 55, 60, 61, 62, 63,
	}

}
#endif
;

// v0.16B34 new scan-order variable, initialized during run-time
EXTERN unsigned char g_scan[2][64];	

/* default intra quantization matrix */
EXTERN unsigned char default_intra_quantizer_matrix[64]
#ifdef GLOBAL
=
{
  8, 16, 19, 22, 26, 27, 29, 34,
  16, 16, 22, 24, 27, 29, 34, 37,
  19, 22, 26, 27, 29, 34, 34, 38,
  22, 22, 26, 27, 29, 34, 37, 40,
  22, 26, 27, 29, 32, 35, 40, 48,
  26, 27, 29, 32, 35, 40, 48, 58,
  26, 27, 29, 34, 38, 46, 56, 69,
  27, 29, 35, 38, 46, 56, 69, 83
}
#endif
;

/* non-linear quantization coefficient table */
EXTERN unsigned char Non_Linear_quantizer_scale[32]
#ifdef GLOBAL
=
{
   0, 1, 2, 3, 4, 5, 6, 7,
   8,10,12,14,16,18,20,22,
  24,28,32,36,40,44,48,52,
  56,64,72,80,88,96,104,112
}
#endif
;

/* color space conversion coefficients
 * for YCbCr -> RGB mapping
 *
 * entries are {crv,cbu,cgu,cgv}
 *
 * crv=(255/224)*65536*(1-cr)/0.5
 * cbu=(255/224)*65536*(1-cb)/0.5
 * cgu=(255/224)*65536*(cb/cg)*(1-cb)/0.5
 * cgv=(255/224)*65536*(cr/cg)*(1-cr)/0.5
 *
 * where Y=cr*R+cg*G+cb*B (cr+cg+cb=1)
 */

/* ISO/IEC 13818-2 section 6.3.6 sequence_display_extension() */

EXTERN int Inverse_Table_6_9[8][4]
#ifdef GLOBAL
=
{
  {117504, 138453, 13954, 34903}, /* no sequence_display_extension */
  {117504, 138453, 13954, 34903}, /* ITU-R Rec. 709 (1990) */
  {104597, 132201, 25675, 53279}, /* unspecified */
  {104597, 132201, 25675, 53279}, /* reserved */
  {104448, 132798, 24759, 53109}, /* FCC */
  {104597, 132201, 25675, 53279}, /* ITU-R Rec. 624-4 System B, G */
  {104597, 132201, 25675, 53279}, /* SMPTE 170M */
  {117579, 136230, 16907, 35559}  /* SMPTE 240M (1987) */
}
#endif
;





/* output types (Output_Type) */
#define T_YUV   0
#define T_SIF   1
#define T_TGA   2
#define T_PPM   3
#define T_X11   4
#define T_X11HIQ 5
/* rsl */
#define T_GDI   6		// GDI RGB24
#define T_AVI   7		// AVI RGB24
#define T_AVI_YV12   8
#define T_AVI_YUY2   9

/* telecine.c */
#define _DISABLE_IVTC -1
#define _IVTC_AUTODISABLE 1 /* automatic shutdown for IVTC, if IVTC is currently on */

/* decoder operation control variables */
EXTERN int Output_Type;
EXTERN int hiQdither;

/* decoder operation control flags */
EXTERN int Quiet_Flag;
EXTERN int Trace_Flag;
EXTERN int Fault_Flag;
EXTERN int Verbose_Flag;
EXTERN int Two_Streams;
EXTERN int Spatial_Flag;
EXTERN int Reference_IDCT_Flag;
EXTERN int Frame_Store_Flag;
EXTERN int System_Stream_Flag;
EXTERN int Display_Progressive_Flag;
EXTERN int Ersatz_Flag;
EXTERN int Big_Picture_Flag;
EXTERN int Verify_Flag;
EXTERN int Stats_Flag;
EXTERN int User_Data_Flag;
EXTERN int Main_Bitstream_Flag;

//SVE 20.05.2001 Neue Globale Variablen
EXTERN int m_iDeinterlace;
EXTERN int m_iSharpen;
EXTERN BOOL m_bProcessFilterFile;
EXTERN char* m_lpProcessFilterFile;
EXTERN double m_dSigma;

//SVE ENDE

#define _DISABLE_CROP -1

EXTERN int g_vobflag;/* indicates whether input-file is a VOB file */
EXTERN int g_vHalfResFlag; /* output pictures are X/Y downsampled 1/2 res*/
/* EXTERN int g_AVIcropX;  for AVI output, cropX & cropY are the target 
   EXTERN int g_AVIcropY;  size of the encoded AVI frames */
/* replaced by UserOptCropX, UserOptCropY */

EXTERN int Resized_Picture_Width;  /* used by store.c to set output res */
EXTERN int Resized_Picture_Height; /* used by store.c to set output res */

EXTERN int g_fastfilter; /* 1 = fast/minimal filtering on output */


/* filenames */
EXTERN char Output_Directory[1000];
EXTERN char Output_Picture_Filename[1000];
EXTERN char *Substitute_Picture_Filename;
EXTERN char *Main_Bitstream_Filename; 
EXTERN char *Enhancement_Layer_Bitstream_Filename; 


/* buffers for multiuse purposes */
EXTERN char Error_Text[256];
EXTERN unsigned char *Clip;

/* pointers to generic picture buffers */
EXTERN unsigned char *backward_reference_frame[3];
EXTERN unsigned char *forward_reference_frame[3];

EXTERN unsigned char *auxframe[3];
EXTERN unsigned char *current_frame[3];
EXTERN unsigned char *substitute_frame[3];


/* pointers to scalability picture buffers */
EXTERN unsigned char *llframe0[3];
EXTERN unsigned char *llframe1[3];

EXTERN short *lltmp;
EXTERN char *Lower_Layer_Picture_Filename;




/* non-normative variables derived from normative elements */
EXTERN int Coded_Picture_Width;
EXTERN int Coded_Picture_Height;
EXTERN int Chroma_Width;
EXTERN int Chroma_Height;
EXTERN int block_count;
EXTERN int Second_Field;
EXTERN int profile, level;

/* normative derived variables (as per ISO/IEC 13818-2) */
EXTERN int horizontal_size;
EXTERN int vertical_size;
EXTERN int mb_width;
EXTERN int mb_height;
EXTERN double bit_rate;
EXTERN double frame_rate; 



/* headers */

/* ISO/IEC 13818-2 section 6.2.2.1:  sequence_header() */
EXTERN int aspect_ratio_information;
EXTERN int frame_rate_code; 
EXTERN int bit_rate_value; 
EXTERN int vbv_buffer_size;
EXTERN int constrained_parameters_flag;

/* ISO/IEC 13818-2 section 6.2.2.3:  sequence_extension() */
EXTERN int profile_and_level_indication;
EXTERN int progressive_sequence;
EXTERN int chroma_format;
EXTERN int low_delay;
EXTERN int frame_rate_extension_n;
EXTERN int frame_rate_extension_d;

/* ISO/IEC 13818-2 section 6.2.2.4:  sequence_display_extension() */
EXTERN int video_format;  
EXTERN int color_description;
EXTERN int color_primaries;
EXTERN int transfer_characteristics;
EXTERN int matrix_coefficients;
EXTERN int display_horizontal_size;
EXTERN int display_vertical_size;

/* ISO/IEC 13818-2 section 6.2.3: picture_header() */
EXTERN int temporal_reference;
EXTERN int picture_coding_type;
EXTERN int vbv_delay;
EXTERN int full_pel_forward_vector;
EXTERN int forward_f_code;
EXTERN int full_pel_backward_vector;
EXTERN int backward_f_code;


/* ISO/IEC 13818-2 section 6.2.3.1: picture_coding_extension() header */
EXTERN int f_code[2][2];
EXTERN int intra_dc_precision;
EXTERN int picture_structure;
EXTERN int top_field_first;
EXTERN int frame_pred_frame_dct;
EXTERN int concealment_motion_vectors;

EXTERN int intra_vlc_format;

EXTERN int repeat_first_field;

EXTERN int chroma_420_type;
EXTERN int progressive_frame;
EXTERN int composite_display_flag;
EXTERN int v_axis;
EXTERN int field_sequence;
EXTERN int sub_carrier;
EXTERN int burst_amplitude;
EXTERN int sub_carrier_phase;



/* ISO/IEC 13818-2 section 6.2.3.3: picture_display_extension() header */
EXTERN int frame_center_horizontal_offset[3];
EXTERN int frame_center_vertical_offset[3];



/* ISO/IEC 13818-2 section 6.2.2.5: sequence_scalable_extension() header */
EXTERN int layer_id;
EXTERN int lower_layer_prediction_horizontal_size;
EXTERN int lower_layer_prediction_vertical_size;
EXTERN int horizontal_subsampling_factor_m;
EXTERN int horizontal_subsampling_factor_n;
EXTERN int vertical_subsampling_factor_m;
EXTERN int vertical_subsampling_factor_n;


/* ISO/IEC 13818-2 section 6.2.3.5: picture_spatial_scalable_extension() header */
EXTERN int lower_layer_temporal_reference;
EXTERN int lower_layer_horizontal_offset;
EXTERN int lower_layer_vertical_offset;
EXTERN int spatial_temporal_weight_code_table_index;
EXTERN int lower_layer_progressive_frame;
EXTERN int lower_layer_deinterlaced_field_select;






/* ISO/IEC 13818-2 section 6.2.3.6: copyright_extension() header */
EXTERN int copyright_flag;
EXTERN int copyright_identifier;
EXTERN int original_or_copy;
EXTERN int copyright_number_1;
EXTERN int copyright_number_2;
EXTERN int copyright_number_3;

/* ISO/IEC 13818-2 section 6.2.2.6: group_of_pictures_header()  */
EXTERN int drop_flag;
EXTERN int hour;
EXTERN int minute;
EXTERN int sec;
EXTERN int frame;
EXTERN int closed_gop;
EXTERN int broken_link;



/* layer specific variables (needed for SNR and DP scalability) */
// v0.16b33 members re-ordered for better alignment,
// all bitstream variables prefixed with "bs_..."
EXTERN struct layer_data {
  /* sequence header and quant_matrix_extension() */
  int intra_quantizer_matrix[64];
  int non_intra_quantizer_matrix[64];
  int chroma_intra_quantizer_matrix[64];
  int chroma_non_intra_quantizer_matrix[64];
  short block[12][64];
  unsigned char bs_Rdbfr[MPV_BSBUFSIZE];  /* bit input */

  unsigned char bs_Inbfr[16];
  unsigned char *bs_Rdptr;
  int bs_Buflevel;	// buffer level
  /* from mpeg2play */
  unsigned char *bs_Rdmax;
  unsigned int bs_Bfr;
  int bs_Infile;
  int bs_Incnt;
  int bs_Bitcnt;
 
  int load_intra_quantizer_matrix;
  int load_non_intra_quantizer_matrix;
  int load_chroma_intra_quantizer_matrix;
  int load_chroma_non_intra_quantizer_matrix;

  int MPEG2_Flag;
  /* sequence scalable extension */
  int scalable_mode;
  /* picture coding extension */
  int q_scale_type;
  int alternate_scan;
  /* picture spatial scalable extension */
  int pict_scal;
  /* slice/macroblock */
  int priority_breakpoint;
  int quantizer_scale;
  int intra_slice;
} base, enhan, *ld;




#ifdef VERIFY
EXTERN int verify_sequence_header;
EXTERN int verify_group_of_pictures_header;
EXTERN int verify_picture_header;
EXTERN int verify_slice_header;
EXTERN int verify_sequence_extension;
EXTERN int verify_sequence_display_extension;
EXTERN int verify_quant_matrix_extension;
EXTERN int verify_sequence_scalable_extension;
EXTERN int verify_picture_display_extension;
EXTERN int verify_picture_coding_extension;
EXTERN int verify_picture_spatial_scalable_extension;
EXTERN int verify_picture_temporal_scalable_extension;
EXTERN int verify_copyright_extension;
#endif /* VERIFY */


EXTERN int Decode_Layer;

/* verify.c */
#ifdef VERIFY
void Check_Headers _ANSI_ARGS_((int Bitstream_Framenum, int Sequence_Framenum));
void Clear_Verify_Headers _ANSI_ARGS_((void));
#endif /* VERIFY */


EXTERN int global_MBA;
EXTERN int global_pic;
EXTERN int True_Framenum;

EXTERN long g_cbMPEGPicture;

/* rsl - variable for inverse-telecine algorithm
   this variable is declared in windisp.c, used by windisp.c and mpeg2dec.c
*/

/*EXTERN int g_ivtc_start_state;  frame-offset for IVTC algorithm 
  g_ivtc_start_state converted to UserOptIVTC...moved to */
EXTERN int g_cmd_ivtc; /* command-variable for on-the-fly IVTC adjustment */
EXTERN int g_cmd_filter; /* command-variable for on-the-fly filter adjustment */
EXTERN int g_cmd_resize; /* command-variable for on-the-fly downsize adjustment */
EXTERN int g_cmd_buttonStop;   /* toggle for stopping the decoder immediately */

/* .data = frame-offset,  .data2 = auto-disable (BOOL) */
EXTERN tUserOption UserOptIVTC; 

/* User Options to re-size X/Y output to aribtrary dimensions */
EXTERN tUserOption UserOptResizeX ;
EXTERN tUserOption UserOptResizeY ;

/* User Options to crop X/Y output to arbitrary dimensions */
EXTERN tUserOption UserOptCropX ;
EXTERN tUserOption UserOptCropY ;

/* User Options to restrict frame range */
EXTERN tUserOption UserOptFRangeStart;
EXTERN tUserOption UserOptFRangeEnd;

/* User option to preset AVI-frame rate, value is looked up against table */
EXTERN tUserOption UserOptAVIrate;

/* User option to enable auto-segmentation of AVI-file size */
EXTERN tUserOption UserOptAVISplitLength;

/* User option to drop 1 frame every N frames, for A/V sync on NTSC DVD-content */
EXTERN tUserOption UserOptAVISkipEvery;

/* User option to print MPEG-frame# in upper left corner of output frame */
EXTERN tUserOption UserOptPrintFrameCode;

/* User option to start DEMUXER @ logicalblockaddress (VOB only) */
EXTERN tUserOption UserOptVOBStartLBA;

/* User selection for Video Stream ID (VOB only) */
EXTERN tUserOption UserOptVOBStreamID;

EXTERN tUserOption UserOptIDCT;

EXTERN tUserOption UserOptNoAudioDecoding;

/* for windisp.c */
#define KEY_IVTC_DOWN '-'           /* - IVTC frame offset */
#define KEY_IVTC_UP   '+'           /* + IVTC frame offset */
#define KEY_IVTC_DISABLE 'I'        /* shut off IVTC */
#define KEY_IVTC_ENABLE 'I'         /* Turn on IVTC */
#define KEY_IVTC_TG_AUTODISABLE 'A' /* autodisable IVTC */
#define KEY_FILTER_TOGGLE 'F'       /* toggle between Hi-Q and FAST mode */
#define KEY_QUIT 'Q'
#define KEYEX 0xE0				/* sepcial prefix keyboard code */
#define KEYEX_RESIZE_XINC 0x4D  /* RIGHT ARROW */
#define KEYEX_RESIZE_XDEC 0x4B  /* LEFT  ARROW */
#define KEYEX_RESIZE_YINC 0x48  /* UP ARROW */
#define KEYEX_RESIZE_YDEC 0x50  /* DOWN ARROW */
#define KEY_RESIZE_RESET 'R'    /* reset resize */


/* 0.16B2, structure for passing timecode from gethdr.c -> store.c */
struct goptimecode_struct
{
	unsigned char hour;
	unsigned char minute;
	unsigned char second;
	unsigned char frame;
};

/* v0.16B31, structure for passing MPEG-picutre info header data 
   from gethdr.c -> store.c */
struct mpv_pinfo_struct	// MPEG-video picture info header
{
	BOOL brff;	// flag repeat_first_field
	BOOL btff;	// flag top_first_field
	BOOL bpf;	// flag pregressive_frame
	BOOL bfs;	// flag field_sequence
	int tref;	// field temporal_reference
	int pty;	// field picture_coding_type (I/P/B)
};

typedef struct mpv_pinfo_struct mpv_pinfo;
typedef struct goptimecode_struct tgoptimecode;

/* v0.16B3 the following variables are *not* == bitstream parameters of same 
   name!  These are processed values with the proper DISPLAY order restored.
   In other words, they are in the correct display order for use by the output
   pipeline. */
EXTERN tgoptimecode	g_dtimecode; /* GOP timecode (reference = display order ) */
//EXTERN unsigned short g_dpictref; /* Picture-header temporal reference */
//EXTERN int g_dpictype;	/* Picture-header type (I/P/B) (reference = display order) */
EXTERN DWORD g_dwLBApack; /* LBA address (VOB) of last MPEG-system header */
EXTERN mpv_pinfo g_mpvpcur; /* MPEG-picture info, current Displayed frame */
EXTERN mpv_pinfo g_mpvplast; /* MPEG-picture info, previous Displayed frame */


// this var is set by the demuxer, it is referenced to bitstream-order
EXTERN DWORD g_dwSystemPackLBA; /* LBA address (VOB) of last MPEG-SYSTEM HEADER*/


/* v0.16B22, function pointer to chosen IDCT function */
typedef void (*idctFunctionType)( short *);
EXTERN idctFunctionType p_idctFunction;
#define _IDCT_FPU64 0  
//#define _IDCT_INT32 1	// default - v0.16B32 and earlier
#define _IDCT_INT32 4	// default - v0.16B32 and earlier
#define _IDCT_MMX32 1	// default - v0.16B33 and later
#define _IDCT_MMX_CHEN1 2
#define _IDCT_MMX_AAN 3	


//#define ENHANCED_ERROR_LOGGING

//SVE Hinweis: Diese Nachrichten und die runstate-Struktur sind auch in der 
//SVE          mpv_read.cpp definiert. Jede Änderung muß auch dort vorgenommen werden

//SVE 03.06.2001
// Die Datenstruktur runstate wird jetzt extern definiert
#include "runstate.h"
HEHE runstate st_RunState;

/*
HEHE struct runstate {
	short	fileCount;
	char	curFile[256];
//SVE 03.06.2001 Typen geändert auf DWORD
	DWORD	fileSize;
	DWORD	filePos;
//SVE 03.06.2001 double	fileSize;
//SVE 03.06.2001 double  filePos;
//SVE 03.06.2001 time_t  startTime;
	long    encFrames;
//SVE 03.06.2001 Typ geändert auf DWORD
	DWORD  kbWritten;
//SVE 03.06.2001 double  kbWritten;
	long    keyFrames;
//SVE 03.06.2001 double  FPS;
//SVE 03.06.2001 double  lowestFPS;
//SVE 03.06.2001 double  highestFPS;
//SVE 03.06.2001 char    RemainTime[9];
//SVE 03.06.2001 char    ElapsedTime[9];

//SVE 03.06.2001
	HWND	hWndStatusDlg;
	DWORD	fileSizes[10];
} st_RunState;
*/
