Main Page | Class List | File List | Class Members | File Members

vid_sim.h

Go to the documentation of this file.
00001 #ifndef _VID_SIM_H_
00002 #define _VID_SIM_H_
00003 
00004 /************************************************************************
00005  *
00006  *  vid_sim.h, part of (TMN encoder)
00007  *  Copyright (C) 1995, 1996  Telenor R&D, Norway
00008  *        Karl Olav Lillevold <Karl.Lillevold@nta.no>
00009  *  
00010  *  Contacts: 
00011  *  Karl Olav Lillevold               <Karl.Lillevold@nta.no>, or
00012  *  Robert Danielsen                  <Robert.Danielsen@nta.no>
00013  *
00014  *  Telenor Research and Development  http://www.nta.no/brukere/DVC/
00015  *  P.O.Box 83                        tel.:   +47 63 84 84 00
00016  *  N-2007 Kjeller, Norway            fax.:   +47 63 81 00 76
00017  *  
00018  ************************************************************************/
00019 
00020 /*
00021  * Disclaimer of Warranty
00022  *
00023  * These software programs are available to the user without any
00024  * license fee or royalty on an "as is" basis.  Telenor Research and
00025  * Development disclaims any and all warranties, whether express,
00026  * implied, or statuary, including any implied warranties or
00027  * merchantability or of fitness for a particular purpose.  In no
00028  * event shall the copyright-holder be liable for any incidental,
00029  * punitive, or consequential damages of any kind whatsoever arising
00030  * from the use of these programs.
00031  *
00032  * This disclaimer of warranty extends to the user of these programs
00033  * and user's customers, employees, agents, transferees, successors,
00034  * and assigns.
00035  *
00036  * Telenor Research and Development does not represent or warrant that
00037  * the programs furnished hereunder are free of infringement of any
00038  * third-party patents.
00039  *
00040  * Commercial implementations of H.263, including shareware, are
00041  * subject to royalty fees to patent holders.  Many of these patents
00042  * are general enough such that they are unavoidable regardless of
00043  * implementation design.
00044  * */
00045 
00046 /**********************************************************************
00047  *
00048  * Headerfile for TMN coder
00049  * Type definitions and declaration of functions 
00050  * Date last modified: every now and then
00051  *
00052  **********************************************************************/
00053 
00054 /* If you are not using the included Makefile, or want to override
00055    the Makefile, you can uncomment one or more of the defines 
00056    below instead */
00057 /* #define PRINTMV */
00058 /* to print MVs to stdout while coding. */
00059 /* #define PRINTQ */
00060 /* to print the quantizer used during coding */
00061 /* #define FASTIDCT */
00062 /* for a fast single precision IDCT. */
00063 /* #define OFFLINE_RATE_CONTROL */
00064 /* for the rate control optimized for offline encoding. */
00065 /* #define QCIF */
00066 /* to change the coding format uncommment the above line and change to
00067    SQCIF, QCIF, CIF, CIF4, or CIF16 */
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 /* This should not be changed */
00077 #define MB_SIZE 16
00078 
00079 /* If someone forgets to define a coding format, QCIF is default */
00080 /*#if (!defined SQCIF && !defined QCIF && !defined CIF && !defined CIF4 && !defined CIF16)
00081 #define QCIF
00082 #endif*/
00083 
00084 /* The coding format is decided here. */
00085 /*$#ifdef SQCIF
00086 #define DEF_PELS 128
00087 #define DEF_LINES 96
00088 #endif
00089 #ifdef QCIF
00090 #define DEF_PELS 176
00091 #define DEF_LINES 144
00092 #endif
00093 #ifdef CIF
00094 #define DEF_PELS 352
00095 #define DEF_LINES 288
00096 #endif
00097 #ifdef CIF4
00098 #define DEF_PELS 704
00099 #define DEF_LINES 576
00100 #endif
00101 #ifdef CIF16
00102 #define DEF_PELS 1408
00103 #define DEF_LINES 1152
00104 #endif$*/
00105 
00106 /*$#define MBC   DEF_PELS/MB_SIZE
00107 #define MBR   DEF_LINES/MB_SIZE$*/
00108 #define MBC   88
00109 #define MBR   72
00110 
00111 /* Parameters from TMN */
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  /* 001 */
00147 #define SF_QCIF                         2  /* 010 */
00148 #define SF_CIF                          3  /* 011 */
00149 #define SF_4CIF                         4  /* 100 */
00150 #define SF_16CIF                        5  /* 101 */
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 /* Motionvector structure */
00165 
00166 typedef struct motionvector {
00167   int x;        /* Horizontal comp. of mv         */
00168   int y;        /* Vertical comp. of mv         */
00169   int x_half;        /* Horizontal half-pel acc.         */
00170   int y_half;        /* Vertical half-pel acc.   */
00171   int min_error;        /* Min error for this vector     */
00172   int Mode;                     /* Necessary for adv. pred. mode */
00173 } MotionVector;
00174 
00175 /* Point structure */
00176 
00177 typedef struct point {
00178   int x;
00179   int y;
00180 } Point;
00181 
00182 /* Structure with image data */
00183 
00184 typedef struct pict_image {
00185   unsigned char *lum;        /* Luminance plane        */
00186   unsigned char *Cr;        /* Cr plane        */
00187   unsigned char *Cb;        /* Cb plane        */
00188 } PictImage;
00189 
00190 /* Group of pictures structure. */
00191 
00192 /* Picture structure */
00193 
00194 typedef struct pict {
00195   int prev; 
00196   int curr;
00197   int TR;             /* Time reference */
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;        /* Motion vector search window */
00210   int use_gobsync;      /* flag for gob_sync */
00211   int MODB;             /* B-frame mode */
00212   int BQUANT;           /* which quantizer to use for B-MBs in PB-frame */
00213   int TRB;              /* Time reference for B-picture */
00214   float QP_mean;        /* mean quantizer */
00215 } Pict;
00216 
00217 /* Slice structure */
00218 
00219 typedef struct slice {
00220   unsigned int vert_pos;        /* Vertical position of slice        */
00221   unsigned int quant_scale;     /* Quantization scale        */
00222 } Slice;
00223 
00224 /* Macroblock structure */
00225 
00226 typedef struct macroblock {
00227   int mb_address;        /* Macroblock address        */
00228   int macroblock_type;        /* Macroblock type        */
00229   int skipped;        /* 1 if skipped        */
00230   MotionVector motion;          /* Motion Vector        */
00231 } Macroblock;
00232 
00233 /* Structure for macroblock data */
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 /* Structure for counted bits */
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 /* NB: Remember to change AddBits(), ZeroBits() and AddBitsPicture() 
00260    when entries are added here */
00261 } Bits;
00262 
00263 
00264 /* Structure for data for data from previous macroblock */
00265 
00266 /* Structure for average results and virtal buffer data */
00267 
00268 typedef struct results {
00269   float SNR_l;        /* SNR for luminance */
00270   float SNR_Cr;        /* SNR for chrominance */
00271   float SNR_Cb;
00272   float QP_mean;                /* Mean quantizer */
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 /* global declarations for idctref */
00407 void init_idctref (void);
00408 void idctref (int *coeff, int *block);
00409 #endif
00410 
00411 /* Syntax based arithmetic coding routines */
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 /* bitstream-functions */
00426 
00427 void initbits();
00428 void putbits (int, int);
00429 int alignbits ();
00430 int bitcount();
00431 
00432 /* Fix broken header-files on suns to avoid compiler warnings */
00433 /* #define BROKEN_SUN_HEADERS here or in Makefile */
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 /* #ifndef _VID_SIM_H_ */

Generated on Sun Jul 16 16:27:45 2006 by  doxygen 1.3.9.1