00001 #ifndef _VID_SIM_H_
00002 #define _VID_SIM_H_
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036
00037
00038
00039
00040
00041
00042
00043
00044
00045
00046
00047
00048
00049
00050
00051
00052
00053
00054
00055
00056
00057
00058
00059
00060
00061
00062
00063
00064
00065
00066
00067
00068
00069 #include<stdio.h>
00070 #include<stdlib.h>
00071 #include<string.h>
00072 #include<limits.h>
00073 #include "vid_config.h"
00074 #include"vid_macros.h"
00075
00076
00077 #define MB_SIZE 16
00078
00079
00080
00081
00082
00083
00084
00085
00086
00087
00088
00089
00090
00091
00092
00093
00094
00095
00096
00097
00098
00099
00100
00101
00102
00103
00104
00105
00106
00107
00108 #define MBC 88
00109 #define MBR 72
00110
00111
00112 #define PREF_NULL_VEC 100
00113 #define PREF_16_VEC 200
00114 #define PREF_PBDELTA_NULL_VEC 50
00115
00116
00117
00118 #if(0)
00119 int headerlength;
00120 int pels;
00121 int cpels;
00122 int lines;
00123 int trace;
00124 int advanced;
00125 int syntax_arith_coding;
00126 int pb_frames;
00127 int mv_outside_frame;
00128 int long_vectors;
00129 float target_framerate;
00130 FILE *tf;
00131 #endif
00132
00133
00134
00135
00136 #define PSC 1
00137 #define PSC_LENGTH 17
00138
00139 #define ESCAPE 7167
00140
00141 #define PCT_INTER 1
00142 #define PCT_INTRA 0
00143 #define ON 1
00144 #define OFF 0
00145
00146 #define SF_SQCIF 1
00147 #define SF_QCIF 2
00148 #define SF_CIF 3
00149 #define SF_4CIF 4
00150 #define SF_16CIF 5
00151
00152 #define MODE_INTER 0
00153 #define MODE_INTER_Q 1
00154 #define MODE_INTER4V 2
00155 #define MODE_INTRA 3
00156 #define MODE_INTRA_Q 4
00157
00158 #define PBMODE_NORMAL 0
00159 #define PBMODE_MVDB 1
00160 #define PBMODE_CBPB_MVDB 2
00161
00162 #define NO_VEC 999
00163
00164
00165
00166 typedef struct motionvector {
00167 int x;
00168 int y;
00169 int x_half;
00170 int y_half;
00171 int min_error;
00172 int Mode;
00173 } MotionVector;
00174
00175
00176
00177 typedef struct point {
00178 int x;
00179 int y;
00180 } Point;
00181
00182
00183
00184 typedef struct pict_image {
00185 unsigned char *lum;
00186 unsigned char *Cr;
00187 unsigned char *Cb;
00188 } PictImage;
00189
00190
00191
00192
00193
00194 typedef struct pict {
00195 int prev;
00196 int curr;
00197 int TR;
00198 int bit_rate;
00199 int src_frame_rate;
00200 float target_frame_rate;
00201 int source_format;
00202 int picture_coding_type;
00203 int spare;
00204 int unrestricted_mv_mode;
00205 int PB;
00206 int QUANT;
00207 int DQUANT;
00208 int MB;
00209 int seek_dist;
00210 int use_gobsync;
00211 int MODB;
00212 int BQUANT;
00213 int TRB;
00214 float QP_mean;
00215 } Pict;
00216
00217
00218
00219 typedef struct slice {
00220 unsigned int vert_pos;
00221 unsigned int quant_scale;
00222 } Slice;
00223
00224
00225
00226 typedef struct macroblock {
00227 int mb_address;
00228 int macroblock_type;
00229 int skipped;
00230 MotionVector motion;
00231 } Macroblock;
00232
00233
00234
00235 typedef struct mb_structure {
00236 int lum[16][16];
00237 int Cr[8][8];
00238 int Cb[8][8];
00239 } MB_Structure;
00240
00241
00242
00243
00244 typedef struct bits_counted {
00245 int Y;
00246 int C;
00247 int vec;
00248 int CBPY;
00249 int CBPCM;
00250 int MODB;
00251 int CBPB;
00252 int COD;
00253 int header;
00254 int DQUANT;
00255 int total;
00256 int no_inter;
00257 int no_inter4v;
00258 int no_intra;
00259
00260
00261 } Bits;
00262
00263
00264
00265
00266
00267
00268 typedef struct results {
00269 float SNR_l;
00270 float SNR_Cr;
00271 float SNR_Cb;
00272 float QP_mean;
00273 } Results;
00274
00275 void Help();
00276 void AdvancedHelp();
00277 int NextTwoPB(PictImage *p2, PictImage *bim, PictImage *p1,
00278 int bskip, int pskip, int seek_dist);
00279 void PrintSNR(Results *res, int num);
00280 void PrintResult(Bits *bits, int num_units, int num);
00281 unsigned char *ReadImage(char *filename, int frame_no, int headerlength);
00282 PictImage *FillImage(unsigned char *in);
00283 void WriteImage(PictImage *image, char *filename);
00284 PictImage *InitImage(int size);
00285 void FreeImage(PictImage *image);
00286 char *StripName(char *s);
00287 int *MB_Encode(MB_Structure *mb_orig, int QP, int I);
00288 int MB_Decode(int *qcoeff, MB_Structure *mb_recon, int QP, int I);
00289 int Dct( int *block, int *coeff);
00290 int idct(int *coeff,int *block);
00291 void FillLumBlock( int x, int y, PictImage *image, MB_Structure *data);
00292 void FillChromBlock(int x_curr, int y_curr, PictImage *image,
00293 MB_Structure *data);
00294 void ReconImage (int i, int j, MB_Structure *data, PictImage *recon);
00295 void CodeOneOrTwo(PictImage *curr, PictImage *B_image, PictImage *prev,
00296 PictImage *prev_recon, int QP, int frameskip, Bits *bits,
00297 Pict *pic, PictImage *B_recon, PictImage *recon);
00298 PictImage *CodeOneIntra(PictImage *curr, int QP, Bits *bits, Pict *pic);
00299 void Dequant(int *qcoeff, int *rcoeff, int QP, int I);
00300 void Quant(int *coeff, int *qcoeff, int QP, int I);
00301 void CountBitsCoeff(int *qcoeff, int I, int CBP, Bits *bits, int ncoeffs);
00302 int CodeCoeff(int Mode, int *qcoeff, int block, int ncoeffs);
00303 int FindCBP(int *qcoeff, int Mode, int ncoeffs);
00304 void CountBitsVectors(MotionVector *MV[5][MBR+1][MBC+2], Bits *bits,
00305 int i, int j, int Mode, int newgob, Pict *pic);
00306 void FindPMV(MotionVector *MV[5][MBR+1][MBC+2], int x, int y,
00307 int *p0, int *p1, int block, int newgob, int half_pel);
00308 void ZeroBits(Bits *bits);
00309 void ZeroRes(Results *res);
00310 void ZeroVec(MotionVector *MV);
00311 void MarkVec(MotionVector *MV);
00312 void CopyVec(MotionVector *MV1, MotionVector *MV2);
00313 int EqualVec(MotionVector *MV2, MotionVector *MV1);
00314 void AddBits(Bits *total, Bits *bits);
00315 void AddRes(Results *total, Results *res, Pict *pic);
00316 void AddBitsPicture(Bits *bits);
00317 void FindMB(int x, int y, unsigned char *image, int MB[16][16]);
00318 MB_Structure *MB_Recon(PictImage *prev_recon, MB_Structure *diff,
00319 int x_curr, int y_curr, MotionVector *MV);
00320 MB_Structure *Predict(PictImage *curr, PictImage *prev_recon,
00321 int x_curr, int y_curr, MotionVector *MV);
00322 unsigned char *InterpolateImage(unsigned char *image, int w, int h);
00323 void MotionEstimatePicture(unsigned char *curr, unsigned char *prev,
00324 unsigned char *prev_ipol, int seek_dist,
00325 MotionVector *MV[5][MBR+1][MBC+2], int gobsync);
00326 void MotionEstimation(unsigned char *curr, unsigned char *prev, int x_curr,
00327 int y_curr, int xoff, int yoff, int seek_dist,
00328 MotionVector *MV[5][MBR+1][MBC+2], int *sad_0);
00329 unsigned char *LoadArea(unsigned char *im, int x, int y,
00330 int x_size, int y_size, int lx);
00331 int SAD_Macroblock(unsigned char *ii, unsigned char *act_block,
00332 int h_length, int Min_FRAME);
00333 int SAD_MB_Bidir(unsigned char *ii, unsigned char *aa, unsigned char *bb,
00334 int width, int min_sofar);
00335 int SAD_Block(unsigned char *ii, unsigned char *act_block,
00336 int h_length, int min_sofar);
00337 int SAD_MB_integer(int *ii, int *act_block, int h_length, int min_sofar);
00338 MB_Structure *Predict_P(PictImage *curr_image, PictImage *prev_image,
00339 unsigned char *prev_ipol,int x_curr, int y_curr,
00340 MotionVector *fr[5][MBR+1][MBC+2], int PB);
00341 MB_Structure *Predict_B(PictImage *curr_image, PictImage *prev_image,
00342 unsigned char *prev_ipol,int x_curr, int y_curr,
00343 MotionVector *fr[5][MBR+1][MBC+2],
00344 MB_Structure *recon_P, int TR,int TRB);
00345 void Clip(MB_Structure *data);
00346 void FindForwLumPredPB(unsigned char *prev_ipol, int x_curr, int y_curr,
00347 MotionVector *fr, int *pred, int TRD, int TRB,
00348 int bdx, int bdy, int bs, int comp);
00349 void FindBiDirLumPredPB(int *recon_P, MotionVector *fr, int *pred, int TRD,
00350 int TRB, int bdx, int bdy, int nh, int nv);
00351 void FindBiDirChrPredPB(MB_Structure *recon_P, int dx, int dy,
00352 MB_Structure *pred);
00353 void FindBiDirLimits(int vec, int *start, int *stop, int nhv);
00354 void FindBiDirChromaLimits(int vec, int *start, int *stop);
00355 void BiDirPredBlock(int xstart, int xstop, int ystart, int ystop,
00356 int xvec, int yvec, int *recon, int *pred, int bl);
00357 void DoPredChrom_P(int x_curr, int y_curr, int dx, int dy,
00358 PictImage *curr, PictImage *prev,
00359 MB_Structure *pred_error);
00360 void FindHalfPel(int x, int y, MotionVector *MV, unsigned char *prev,
00361 int *curr, int bs, int comp);
00362 void FindPred(int x, int y, MotionVector *fr, unsigned char *prev,
00363 int *pred, int bs, int comp);
00364 void FindPredOBMC(int x, int y, MotionVector *MV[5][MBR+1][MBC+2],
00365 unsigned char *prev, int *pred, int comp, int PB);
00366 MB_Structure *MB_Recon_P(PictImage *prev_image, unsigned char *prev_ipol,
00367 MB_Structure *diff, int x_curr, int y_curr,
00368 MotionVector *MV[5][MBR+1][MBC+2], int PB);
00369 MB_Structure *MB_Recon_B(PictImage *prev, MB_Structure *diff,
00370 unsigned char *prev_ipol,int x_curr, int y_curr,
00371 MotionVector *MV[5][MBR+1][MBC+2],
00372 MB_Structure *recon_P,int TR, int TRB);
00373 void ReconLumBlock_P(int x, int y, MotionVector *fr,
00374 unsigned char *prev, int *data,int bs,int comp);
00375 void ReconChromBlock_P(int x_curr, int y_curr, int dx, int dy,
00376 PictImage *prev, MB_Structure *data);
00377 void FindChromBlock_P(int x_curr, int y_curr, int dx, int dy,
00378 PictImage *prev, MB_Structure *data);
00379 void ComputeSNR(PictImage *im1, PictImage *im2, Results *res, int write);
00380 void ZeroMBlock(MB_Structure *data);
00381 int CountBitsPicture(Pict *pic);
00382 void CountBitsMB(int Mode, int COD, int CBP, int CBPB, Pict *pic, Bits *bits);
00383 int CountBitsSlice(int slice, int quant);
00384 int ChooseMode(unsigned char *curr, int x_pos, int y_pos, int min_SAD);
00385 int ModifyMode(int Mode, int dquant);
00386 int *InterleaveCoeff(int *qp, int *qb, int bs);
00387 void MakeEdgeImage(unsigned char *src, unsigned char *dst, int width,
00388 int height, int edge);
00389 void BitPrint(int length, int val, char *bit);
00390
00391
00392 #ifdef OFFLINE_RATE_CONTROL
00393 int FrameUpdateQP(int buf, int bits, int frames_left, int QP, int B,
00394 float seconds);
00395 #else
00396 int UpdateQuantizer(int mb, float QP_mean, int pict_type, float bit_rate,
00397 int mb_width, int mb_height, int bitcount) ;
00398
00399 int InitializeQuantizer(int pict_type, float bit_rate,
00400 float target_frame_rate, float QP_mean);
00401 void InitializeRateControl();
00402 void UpdateRateControl(int bits);
00403 #endif
00404
00405 #ifndef FASTIDCT
00406
00407 void init_idctref (void);
00408 void idctref (int *coeff, int *block);
00409 #endif
00410
00411
00412
00413 void Count_sac_BitsCoeff(int *qcoeff, int I, int CBP, Bits *bits, int ncoeffs);
00414 int Code_sac_Coeff(int Mode, int *qcoeff, int block, int ncoeffs);
00415 int CodeTCoef(int mod_index, int position, int intra);
00416 void Count_sac_BitsVectors(MotionVector *MV[5][MBR+1][MBC+2], Bits *bits,
00417 int i, int j, int Mode, int newgob, Pict *pic);
00418 void Count_sac_BitsMB(int Mode,int COD,int CBP,int CBPB,Pict *pic,Bits *bits);
00419 int AR_Encode(int index, int cumul_freq[]);
00420 int indexfn(int value, int table[], int max);
00421 int bit_opp_bits(int);
00422 int bit_in_psc_layer(int);
00423 int encoder_flush();
00424
00425
00426
00427 void initbits();
00428 void putbits (int, int);
00429 int alignbits ();
00430 int bitcount();
00431
00432
00433
00434 #ifdef BROKEN_SUN_HEADERS
00435 extern int printf();
00436 extern int fprintf();
00437 extern int time();
00438 extern int fclose();
00439 extern int rewind();
00440 extern int fseek();
00441 extern int fread();
00442 extern int fwrite();
00443 extern int fflush();
00444 extern int fscanf();
00445 extern int _flsbuf();
00446 extern int _filbuf();
00447 #endif
00448
00449 #endif