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

vid_countbit.c

Go to the documentation of this file.
00001 /************************************************************************
00002  *
00003  *  vid_countbit.c, bitstream generation for tmn (TMN encoder)
00004  *  Copyright (C) 1995, 1996  Telenor R&D, Norway
00005  *        Karl Olav Lillevold <Karl.Lillevold@nta.no>
00006  *  
00007  *  Contacts: 
00008  *  Karl Olav Lillevold               <Karl.Lillevold@nta.no>, or
00009  *  Robert Danielsen                  <Robert.Danielsen@nta.no>
00010  *
00011  *  Telenor Research and Development  http://www.nta.no/brukere/DVC/
00012  *  P.O.Box 83                        tel.:   +47 63 84 84 00
00013  *  N-2007 Kjeller, Norway            fax.:   +47 63 81 00 76
00014  *  
00015  ************************************************************************/
00016 
00017 /*
00018  * Disclaimer of Warranty
00019  *
00020  * These software programs are available to the user without any
00021  * license fee or royalty on an "as is" basis.  Telenor Research and
00022  * Development disclaims any and all warranties, whether express,
00023  * implied, or statuary, including any implied warranties or
00024  * merchantability or of fitness for a particular purpose.  In no
00025  * event shall the copyright-holder be liable for any incidental,
00026  * punitive, or consequential damages of any kind whatsoever arising
00027  * from the use of these programs.
00028  *
00029  * This disclaimer of warranty extends to the user of these programs
00030  * and user's customers, employees, agents, transferees, successors,
00031  * and assigns.
00032  *
00033  * Telenor Research and Development does not represent or warrant that
00034  * the programs furnished hereunder are free of infringement of any
00035  * third-party patents.
00036  *
00037  * Commercial implementations of H.263, including shareware, are
00038  * subject to royalty fees to patent holders.  Many of these patents
00039  * are general enough such that they are unavoidable regardless of
00040  * implementation design.
00041  * */
00042 
00043 
00044 #include"vid_sim.h"
00045 #include"vid_sactbls.h"
00046 #include"vid_indices.h"
00047 #include "vid_putvlc.h"
00048 #include"video_codec.h"
00049 
00050 extern video_codec *VidSt;
00051 
00052 /**********************************************************************
00053  *
00054  *      Name:        CountBitsMB
00055  *      Description:    counts bits used for MB info
00056  *      
00057  *      Input:          Mode, COD, CBP, Picture and Bits structures
00058  *      Returns:       
00059  *      Side effects:
00060  *
00061  *      Date: 941129    Author: Karl.Lillevold@nta.no
00062  *
00063  ***********************************************************************/
00064 
00065 void CountBitsMB(int Mode, int COD, int CBP, int CBPB, Pict *pic, Bits *bits)
00066 {
00067   int cbpy, cbpcm, length;
00068 
00069   /* COD */
00070   if ((VidSt->trace)) {
00071     fprintf((VidSt->tf),"MB-nr: %d",pic->MB);
00072     if (pic->picture_coding_type == PCT_INTER)
00073       fprintf((VidSt->tf),"  COD: %d\n",COD);
00074   }
00075   if (pic->picture_coding_type == PCT_INTER) {
00076     putbits(1,COD);
00077     bits->COD++;
00078   }
00079 
00080   if (COD) 
00081     return;    /* not coded */
00082 
00083   /* CBPCM */
00084   cbpcm = Mode | ((CBP&3)<<4);
00085   if ((VidSt->trace)) {
00086     fprintf((VidSt->tf),"CBPCM (CBP=%d) (cbpcm=%d): ",CBP,cbpcm);
00087   }
00088   if (pic->picture_coding_type == PCT_INTRA)
00089     length = put_cbpcm_intra (CBP, Mode);
00090   else
00091     length = put_cbpcm_inter (CBP, Mode);
00092   bits->CBPCM += length;
00093 
00094     /* MODB & CBPB */
00095   if (pic->PB) {
00096     switch (pic->MODB) {
00097     case PBMODE_NORMAL:
00098       putbits(1,0);
00099       bits->MODB += 1;
00100       break;
00101     case PBMODE_MVDB:
00102       putbits(2,2);
00103       bits->MODB += 2;
00104       break;
00105     case PBMODE_CBPB_MVDB:
00106       putbits(2,3);
00107       bits->MODB += 2;
00108       /* CBPB */
00109       putbits(6,CBPB);
00110       bits->CBPB += 6;
00111       break;
00112     }
00113     if ((VidSt->trace)) 
00114       fprintf((VidSt->tf),"MODB: %d, CBPB: %d\n", pic->MODB, CBPB);
00115   }
00116     
00117   /* CBPY */
00118   cbpy = CBP>>2;
00119   if (Mode == MODE_INTRA || Mode == MODE_INTRA_Q) /* Intra */
00120     cbpy = cbpy^15;
00121   if ((VidSt->trace)) {
00122     fprintf((VidSt->tf),"CBPY (CBP=%d) (cbpy=%d): ",CBP,cbpy);
00123   }
00124   length = put_cbpy (CBP, Mode);
00125 
00126   bits->CBPY += length;
00127   
00128   /* DQUANT */
00129   if ((Mode == MODE_INTER_Q) || (Mode == MODE_INTRA_Q)) {
00130     if ((VidSt->trace)) {
00131       fprintf((VidSt->tf),"DQUANT: ");
00132     }
00133     switch (pic->DQUANT) {
00134     case -1:
00135       putbits(2,0);
00136       break;
00137     case -2:
00138       putbits(2,1);
00139       break;
00140     case 1:
00141       putbits(2,2);
00142       break;
00143     case 2:
00144       putbits(2,3);
00145       break;
00146     default:
00147       fprintf(stderr,"Invalid DQUANT\n");
00148       exit(-1);
00149     }
00150     bits->DQUANT += 2;
00151   }
00152   return;
00153 }
00154 
00155 /**********************************************************************
00156  *
00157  *      Name:           Count_sac_BitsMB
00158  *      Description:    counts bits used for MB info using SAC models
00159  *                      modified from CountBitsMB
00160  *
00161  *      Input:          Mode, COD, CBP, Picture and Bits structures
00162  *      Returns:        none
00163  *      Side effects:   Updates Bits structure.
00164  *
00165  *      Author:        pmulroy@visual.bt.co.uk
00166  *
00167  ***********************************************************************/
00168  
00169 void Count_sac_BitsMB(int Mode,int COD,int CBP,int CBPB,Pict *pic,Bits *bits)
00170 {
00171   int cbpy, cbpcm, length, i;
00172  
00173   (VidSt->arith_used) = 1;
00174  
00175   /* COD */
00176  
00177   if ((VidSt->trace)) {
00178     fprintf((VidSt->tf),"MB-nr: %d",pic->MB);
00179     if (pic->picture_coding_type == PCT_INTER)
00180       fprintf((VidSt->tf),"  COD: %d ",COD);
00181   }
00182  
00183   if (pic->picture_coding_type == PCT_INTER)
00184     bits->COD+=AR_Encode(COD, cumf_COD);
00185  
00186   if (COD)
00187     return;    /* not coded */
00188  
00189   /* CBPCM */
00190  
00191   cbpcm = Mode | ((CBP&3)<<4);
00192   if ((VidSt->trace)) {
00193     fprintf((VidSt->tf),"CBPCM (CBP=%d) (cbpcm=%d): ",CBP,cbpcm);
00194   }
00195   if (pic->picture_coding_type == PCT_INTRA)
00196     length = AR_Encode(indexfn(cbpcm,mcbpc_intratab,9),cumf_MCBPC_intra);
00197   else
00198     length = AR_Encode(indexfn(cbpcm,mcbpctab,21),cumf_MCBPC);
00199  
00200   bits->CBPCM += length;
00201  
00202   /* MODB & CBPB */
00203    if (pic->PB) {
00204      switch (pic->MODB) {
00205      case PBMODE_NORMAL:
00206        bits->MODB += AR_Encode(0, cumf_MODB);
00207        break;
00208      case PBMODE_MVDB:
00209        bits->MODB += AR_Encode(1, cumf_MODB);
00210        break;
00211      case PBMODE_CBPB_MVDB:
00212        bits->MODB += AR_Encode(2, cumf_MODB);
00213        /* CBPB */
00214        for(i=5; i>1; i--)
00215          bits->CBPB += AR_Encode(((CBPB & 1<<i)>>i), cumf_YCBPB);
00216        for(i=1; i>-1; i--)
00217          bits->CBPB += AR_Encode(((CBPB & 1<<i)>>i), cumf_UVCBPB);
00218        break;
00219      }
00220      if ((VidSt->trace)) 
00221        fprintf((VidSt->tf),"MODB: %d, CBPB: %d\n", pic->MODB, CBPB);
00222    }
00223 
00224   /* CBPY */
00225  
00226   cbpy = CBP>>2;
00227   if (Mode == MODE_INTRA || Mode == MODE_INTRA_Q) { /* Intra */
00228     length = AR_Encode(indexfn(cbpy,cbpy_intratab,16),cumf_CBPY_intra);
00229   } else {
00230     length = AR_Encode(indexfn(cbpy,cbpytab,16),cumf_CBPY);
00231   }
00232   if ((VidSt->trace)) {
00233     fprintf((VidSt->tf),"CBPY (CBP=%d) (cbpy=%d): ",CBP,cbpy);
00234   }
00235   bits->CBPY += length;
00236  
00237   /* DQUANT */
00238  
00239   if ((Mode == MODE_INTER_Q) || (Mode == MODE_INTRA_Q)) {
00240     if ((VidSt->trace)) {
00241       fprintf((VidSt->tf),"DQUANT: ");
00242     }
00243     bits->DQUANT += AR_Encode(indexfn(pic->DQUANT+2,dquanttab,4), cumf_DQUANT);
00244   }
00245   return;
00246 }
00247 
00248 
00249 /**********************************************************************
00250  *
00251  *      Name:        CountBitsSlice
00252  *      Description:    couonts bits used for slice (GOB) info
00253  *      
00254  *      Input:          slice no., quantizer
00255  *
00256  *      Date: 94????    Author: Karl.Lillevold@nta.no
00257  *
00258  ***********************************************************************/
00259 
00260 int CountBitsSlice(int slice, int quant)
00261 {
00262   int bits = 0;
00263 
00264   if ((VidSt->arith_used)) {
00265     bits+=encoder_flush(); /* Need to call before fixed length string output */
00266     (VidSt->arith_used) = 0;
00267   }
00268 
00269   /* Picture Start Code */
00270   if ((VidSt->trace))
00271     fprintf((VidSt->tf),"GOB sync (GBSC): ");
00272   putbits(PSC_LENGTH,PSC); /* PSC */
00273   bits += PSC_LENGTH;
00274 
00275   /* Group Number */
00276   if ((VidSt->trace))
00277     fprintf((VidSt->tf),"GN: ");
00278   putbits(5,slice);
00279   bits += 5;
00280 
00281   /* GOB Sub Bitstream Indicator */
00282   /* if CPM == 1: read 2 bits GSBI */
00283   /* not supported in this version */
00284 
00285   /* GOB Frame ID */
00286   if ((VidSt->trace))
00287     fprintf((VidSt->tf),"GFID: ");
00288   putbits(2, 0);  
00289   /* NB: in error-prone environments this value should change if 
00290      PTYPE in picture header changes. In this version of the encoder
00291      PTYPE only changes when PB-frames are used in the following cases:
00292      (i) after the first intra frame
00293      (ii) if the distance between two P-frames is very large 
00294      Therefore I haven't implemented this GFID change */
00295   /* GFID is not allowed to change unless PTYPE changes */
00296   bits += 2;
00297 
00298   /* Gquant */
00299   if ((VidSt->trace))
00300     fprintf((VidSt->tf),"GQUANT: ");
00301   putbits(5,quant);
00302   bits += 5;
00303 
00304   return bits;
00305 }
00306 
00307 
00308 /**********************************************************************
00309  *
00310  *      Name:        CountBitsCoeff
00311  *      Description:    counts bits used for coeffs
00312  *      
00313  *      Input:        qcoeff, coding mode CBP, bits structure, no. of 
00314  *                      coeffs
00315  *        
00316  *      Returns:        struct with no. of bits used
00317  *      Side effects:   
00318  *
00319  *      Date: 940111    Author: Karl.Lillevold@nta.no
00320  *
00321  ***********************************************************************/
00322 
00323 void CountBitsCoeff(int *qcoeff, int Mode, int CBP, Bits *bits, int ncoeffs)
00324 {
00325   
00326   int i;
00327 
00328   if (Mode == MODE_INTRA || Mode == MODE_INTRA_Q) {
00329     for (i = 0; i < 4; i++) {
00330       bits->Y += CodeCoeff(Mode, qcoeff,i,ncoeffs);
00331     }
00332     for (i = 4; i < 6; i++) {
00333       bits->C += CodeCoeff(Mode, qcoeff,i,ncoeffs);
00334     }
00335   }
00336   else {
00337     for (i = 0; i < 4; i++) {
00338       if ((i==0 && CBP&32) || 
00339           (i==1 && CBP&16) ||
00340           (i==2 && CBP&8) || 
00341           (i==3 && CBP&4) || 
00342           (i==4 && CBP&2) ||
00343           (i==5 && CBP&1)) {
00344         bits->Y += CodeCoeff(Mode, qcoeff, i, ncoeffs);
00345       }
00346     }
00347     for (i = 4; i < 6; i++) {
00348       if ((i==0 && CBP&32) || 
00349           (i==1 && CBP&16) ||
00350           (i==2 && CBP&8) || 
00351           (i==3 && CBP&4) || 
00352           (i==4 && CBP&2) ||
00353           (i==5 && CBP&1)) {
00354         bits->C += CodeCoeff(Mode, qcoeff, i, ncoeffs);
00355       }
00356     }
00357   }
00358   return;
00359 }
00360   
00361 int CodeCoeff(int Mode, int *qcoeff, int block, int ncoeffs)
00362 {
00363   int j, bits;
00364   int prev_run, run, prev_level, level, first;
00365   int prev_s, s, length;
00366 
00367   run = bits = 0;
00368   first = 1;
00369   prev_run = prev_level = level = s = prev_s = 0;
00370   
00371   if ((VidSt->trace)) {
00372     fprintf((VidSt->tf),"Coeffs block %d:\n",block);
00373   }
00374 
00375   for (j = block*ncoeffs; j< (block + 1)*ncoeffs; j++) {
00376     /* Do this block's DC-coefficient first */
00377     if (!(j%ncoeffs) && (Mode == MODE_INTRA || Mode == MODE_INTRA_Q)) {
00378       /* DC coeff */
00379       if ((VidSt->trace)) {
00380         fprintf((VidSt->tf),"DC: ");
00381       }
00382       if (qcoeff[block*ncoeffs] != 128)
00383         putbits(8,qcoeff[block*ncoeffs]);
00384       else
00385         putbits(8,255);
00386       bits += 8;
00387     }
00388     else {
00389       /* AC coeff */
00390       s = 0;
00391       /* Increment run if coeff is zero */
00392       if ((level = qcoeff[j]) == 0) {
00393         run++;
00394       }
00395       else {
00396         /* code run & level and count bits */
00397         if (level < 0) {
00398           s = 1;
00399           level = -level;
00400         }
00401 
00402         if (!first) {
00403           /* Encode the previous coefficient */
00404           if (prev_level  < 13 && prev_run < 64)
00405             length = put_coeff (prev_run, prev_level, 0);  
00406           else
00407             length = 0;
00408           if (length == 0) {  /* Escape coding */
00409             if ((VidSt->trace)) {
00410               fprintf((VidSt->tf),"Escape code: ");
00411             }
00412             if (prev_s == 1) {prev_level = (prev_level^0xff)+1;}
00413             putbits(7,3);       /* Escape code */
00414             if ((VidSt->trace))
00415               fprintf((VidSt->tf),"last: ");
00416             putbits(1,0);
00417             if ((VidSt->trace))
00418               fprintf((VidSt->tf),"run: ");
00419             putbits(6,prev_run);
00420             if ((VidSt->trace))
00421               fprintf((VidSt->tf),"level: ");
00422             putbits(8,prev_level);
00423             bits += 22;
00424           }
00425           else {
00426             putbits(1,prev_s);
00427             bits += length + 1;
00428           }
00429         }
00430         prev_run = run; prev_s = s;
00431         prev_level = level; 
00432 
00433         run = first = 0;
00434       }
00435     }
00436   }
00437   /* Encode the last coeff */
00438   if (!first) {
00439     if ((VidSt->trace)) {
00440       fprintf((VidSt->tf),"Last coeff: ");
00441     }
00442     if (prev_level  < 13 && prev_run < 64) 
00443       length = put_coeff (prev_run, prev_level, 1);   
00444     else
00445       length = 0;
00446     if (length == 0) {  /* Escape coding */
00447       if ((VidSt->trace)) {
00448         fprintf((VidSt->tf),"Escape code: ");
00449       }
00450       if (prev_s == 1) {prev_level = (prev_level^0xff)+1;}
00451       putbits (7,3);    /* Escape code */
00452       if ((VidSt->trace))
00453         fprintf((VidSt->tf),"last: ");
00454       putbits(1,1);
00455       if ((VidSt->trace))
00456         fprintf((VidSt->tf),"run: ");
00457       putbits(6,prev_run);
00458       if ((VidSt->trace))
00459         fprintf((VidSt->tf),"level: ");
00460       putbits(8,prev_level);
00461       bits += 22;
00462     }
00463     else {
00464       putbits(1,prev_s);
00465       bits += length + 1;
00466     }
00467   }
00468   return bits;
00469 }
00470 
00471 /**********************************************************************
00472  *
00473  *      Name:           Count_sac_BitsCoeff
00474  *                      counts bits using SAC models
00475  *
00476  *      Input:          qcoeff, coding mode CBP, bits structure, no. of
00477  *                      coeffs
00478  *
00479  *      Returns:        struct with no. of bits used
00480  *      Side effects:
00481  *
00482  *      Author:        pmulroy@visual.bt.co.uk
00483  *
00484  ***********************************************************************/
00485  
00486 void Count_sac_BitsCoeff(int *qcoeff,int Mode,int CBP,Bits *bits,int ncoeffs)
00487 {
00488  
00489   int i;
00490  
00491   (VidSt->arith_used) = 1;
00492  
00493   if (Mode == MODE_INTRA || Mode == MODE_INTRA_Q) {
00494     for (i = 0; i < 4; i++) {
00495       bits->Y += Code_sac_Coeff(Mode, qcoeff,i,ncoeffs);
00496     }
00497     for (i = 4; i < 6; i++) {
00498       bits->C += Code_sac_Coeff(Mode, qcoeff,i,ncoeffs);
00499     }
00500   }
00501   else {
00502     for (i = 0; i < 4; i++) {
00503       if ((i==0 && CBP&32) ||
00504           (i==1 && CBP&16) ||
00505           (i==2 && CBP&8) ||
00506           (i==3 && CBP&4) ||
00507           (i==4 && CBP&2) ||
00508           (i==5 && CBP&1)) {
00509         bits->Y += Code_sac_Coeff(Mode, qcoeff, i, ncoeffs);
00510       }
00511     }
00512     for (i = 4; i < 6; i++) {
00513       if ((i==0 && CBP&32) ||
00514           (i==1 && CBP&16) ||
00515           (i==2 && CBP&8) ||
00516           (i==3 && CBP&4) ||
00517           (i==4 && CBP&2) ||
00518           (i==5 && CBP&1)) {
00519         bits->C += Code_sac_Coeff(Mode, qcoeff, i, ncoeffs);
00520       }
00521     }
00522   }
00523   return;
00524 }
00525  
00526 int Code_sac_Coeff(int Mode, int *qcoeff, int block, int ncoeffs)
00527 {
00528   int j, bits, mod_index, intra;
00529   int prev_run, run, prev_level, level, first, prev_position, position;
00530   int prev_ind, ind, prev_s, s, length;
00531  
00532   run = bits = 0;
00533   first = 1; position = 0; intra = 0;
00534  
00535   level = s = ind = 0;
00536   prev_run = prev_level = prev_ind = prev_s = prev_position = 0;
00537  
00538   intra = (Mode == MODE_INTRA || Mode == MODE_INTRA_Q);
00539  
00540   for (j = block*ncoeffs; j< (block + 1)*ncoeffs; j++) {
00541  
00542     if (!(j%ncoeffs) && intra) {
00543       if ((VidSt->trace)) {
00544         fprintf((VidSt->tf),"DC: ");
00545       }
00546       if (qcoeff[block*ncoeffs]!=128)
00547         mod_index = indexfn(qcoeff[block*ncoeffs],intradctab,254);
00548       else
00549         mod_index = indexfn(255,intradctab,254);
00550       bits += AR_Encode(mod_index, cumf_INTRADC);
00551     }
00552     else {
00553  
00554       s = 0;
00555       /* Increment run if coeff is zero */
00556       if ((level = qcoeff[j]) == 0) {
00557         run++;
00558       }
00559       else {
00560         /* code run & level and count bits */
00561         if (level < 0) {
00562           s = 1;
00563           level = -level;
00564         }
00565         ind = level | run<<4;
00566         ind = ind | 0<<12; /* Not last coeff */
00567         position++;
00568  
00569         if (!first) {
00570           mod_index = indexfn(prev_ind, tcoeftab, 103);
00571           if (prev_level < 13 && prev_run < 64)
00572             length = CodeTCoef(mod_index, prev_position, intra);
00573           else
00574             length = -1;
00575  
00576           if (length == -1) {  /* Escape coding */
00577             if ((VidSt->trace)) {
00578               fprintf((VidSt->tf),"Escape coding:\n");
00579             }
00580  
00581             if (prev_s == 1) {prev_level = (prev_level^0xff)+1;}
00582  
00583             mod_index = indexfn(ESCAPE, tcoeftab, 103);
00584             bits += CodeTCoef(mod_index, prev_position, intra);
00585  
00586             if (intra)
00587               bits += AR_Encode(indexfn(0, lasttab, 2), cumf_LAST_intra);
00588             else
00589               bits += AR_Encode(indexfn(0, lasttab, 2), cumf_LAST);
00590  
00591             if (intra)
00592               bits += AR_Encode(indexfn(prev_run, runtab, 64), cumf_RUN_intra);
00593             else
00594               bits += AR_Encode(indexfn(prev_run, runtab, 64), cumf_RUN);
00595  
00596             if (intra)
00597               bits += AR_Encode(indexfn(prev_level, leveltab, 254), 
00598         cumf_LEVEL_intra);
00599             else
00600               bits += AR_Encode(indexfn(prev_level, leveltab, 254), 
00601         cumf_LEVEL);
00602  
00603           }
00604           else {
00605             bits += AR_Encode(indexfn(prev_s, signtab, 2), cumf_SIGN);
00606             bits += length;
00607           }
00608         }
00609  
00610         prev_run = run; prev_s = s;
00611         prev_level = level; prev_ind = ind;
00612         prev_position = position;
00613  
00614         run = first = 0;
00615  
00616       }
00617     }
00618   }
00619  
00620   /* Encode Last Coefficient */
00621  
00622   if (!first) {
00623     if ((VidSt->trace)) {
00624       fprintf((VidSt->tf),"Last coeff: ");
00625     }
00626     prev_ind = prev_ind | 1<<12;   /* last coeff */
00627     mod_index = indexfn(prev_ind, tcoeftab, 103);
00628  
00629     if (prev_level  < 13 && prev_run < 64)
00630       length = CodeTCoef(mod_index, prev_position, intra);
00631     else
00632       length = -1;
00633  
00634     if (length == -1) {  /* Escape coding */
00635       if ((VidSt->trace)) {
00636         fprintf((VidSt->tf),"Escape coding:\n");
00637       }
00638 
00639       if (prev_s == 1) {prev_level = (prev_level^0xff)+1;}
00640  
00641       mod_index = indexfn(ESCAPE, tcoeftab, 103);
00642       bits += CodeTCoef(mod_index, prev_position, intra);
00643  
00644       if (intra)
00645         bits += AR_Encode(indexfn(1, lasttab, 2), cumf_LAST_intra);
00646       else
00647         bits += AR_Encode(indexfn(1, lasttab, 2), cumf_LAST);
00648  
00649       if (intra)
00650         bits += AR_Encode(indexfn(prev_run, runtab, 64), cumf_RUN_intra);
00651       else
00652         bits += AR_Encode(indexfn(prev_run, runtab, 64), cumf_RUN);
00653  
00654       if (intra)
00655         bits += AR_Encode(indexfn(prev_level, leveltab, 254), cumf_LEVEL_intra);
00656       else
00657         bits += AR_Encode(indexfn(prev_level, leveltab, 254), cumf_LEVEL);
00658     }
00659     else {
00660       bits += AR_Encode(indexfn(prev_s, signtab, 2), cumf_SIGN);
00661       bits += length;
00662     }
00663   } /* last coeff */
00664  
00665   return bits;
00666 }
00667  
00668 /*********************************************************************
00669  *
00670  *      Name:           CodeTCoef
00671  *
00672  *      Description:    Encodes an AC Coefficient using the
00673  *                      relevant SAC model.
00674  *
00675  *      Input:          Model index, position in DCT block and intra/
00676  *        inter flag.
00677  *
00678  *      Returns:        Number of bits used.
00679  *
00680  *      Side Effects:   None
00681  *
00682  *      Author:         pmulroy@visual.bt.co.uk
00683  *
00684  *********************************************************************/
00685 
00686 int CodeTCoef(int mod_index, int position, int intra)
00687 {
00688   int length;
00689  
00690   switch (position) {
00691     case 1:
00692     {
00693         if (intra)
00694           length = AR_Encode(mod_index, cumf_TCOEF1_intra);
00695         else
00696           length = AR_Encode(mod_index, cumf_TCOEF1);
00697         break;
00698     }
00699     case 2:
00700     {
00701         if (intra)
00702           length = AR_Encode(mod_index, cumf_TCOEF2_intra);
00703         else
00704           length = AR_Encode(mod_index, cumf_TCOEF2);
00705         break;
00706     }
00707     case 3:
00708     {
00709         if (intra)
00710           length = AR_Encode(mod_index, cumf_TCOEF3_intra);
00711         else
00712           length = AR_Encode(mod_index, cumf_TCOEF3);
00713         break;
00714     }
00715     default:
00716     {
00717         if (intra)
00718           length = AR_Encode(mod_index, cumf_TCOEFr_intra);
00719         else
00720           length = AR_Encode(mod_index, cumf_TCOEFr);
00721         break;
00722     }
00723   }
00724  
00725   return length;
00726 }
00727 
00728 /**********************************************************************
00729  *
00730  *      Name:        FindCBP
00731  *      Description:    Finds the CBP for a macroblock
00732  *      
00733  *      Input:        qcoeff and mode
00734  *        
00735  *      Returns:        CBP
00736  *      Side effects:   
00737  *
00738  *      Date: 940829    Author: Karl.Lillevold@nta.no
00739  *
00740  ***********************************************************************/
00741 
00742 
00743 int FindCBP(int *qcoeff, int Mode, int ncoeffs)
00744 {
00745   
00746   int i,j;
00747   int CBP = 0;
00748   int intra = (Mode == MODE_INTRA || Mode == MODE_INTRA_Q);
00749 
00750   /* Set CBP for this Macroblock */
00751   for (i = 0; i < 6; i++) {
00752     for (j = i*ncoeffs + intra; j < (i+1)*ncoeffs; j++) {
00753       if (qcoeff[j]) {
00754         if (i == 0) {CBP |= 32;}
00755         else if (i == 1) {CBP |= 16;}
00756         else if (i == 2) {CBP |= 8;}
00757         else if (i == 3) {CBP |= 4;}
00758         else if (i == 4) {CBP |= 2;}
00759         else if (i == 5) {CBP |= 1;}
00760         else {
00761           fprintf(stderr,"Error in CBP assignment\n");
00762           exit(-1);
00763         }
00764         break;
00765       }
00766     }
00767   }
00768 
00769   return CBP;
00770 }
00771 
00772 
00773 void CountBitsVectors(MotionVector *MV[6][MBR+1][MBC+2], Bits *bits, 
00774               int x, int y, int Mode, int newgob, Pict *pic)
00775 {
00776   int y_vec, x_vec;
00777   int pmv0, pmv1;
00778   int start,stop,block;
00779 
00780   x++;y++;
00781 
00782   if (Mode == MODE_INTER4V) {
00783     start = 1; stop = 4;
00784   }
00785   else {
00786     start = 0; stop = 0;
00787   }
00788 
00789   for (block = start; block <= stop;  block++) {
00790 
00791     FindPMV(MV,x,y,&pmv0,&pmv1, block, newgob, 1);
00792 
00793     x_vec = (2*MV[block][y][x]->x + MV[block][y][x]->x_half) - pmv0;
00794     y_vec = (2*MV[block][y][x]->y + MV[block][y][x]->y_half) - pmv1;
00795 
00796     if (!(VidSt->long_vectors)) {
00797       if (x_vec < -32) x_vec += 64;
00798       else if (x_vec > 31) x_vec -= 64;
00799 
00800       if (y_vec < -32) y_vec += 64;
00801       else if (y_vec > 31) y_vec -= 64;
00802     }
00803     else {
00804       if (pmv0 < -31 && x_vec < -63) x_vec += 64;
00805       else if (pmv0 > 32 && x_vec > 63) x_vec -= 64;
00806 
00807       if (pmv1 < -31 && y_vec < -63) y_vec += 64;
00808       else if (pmv1 > 32 && y_vec > 63) y_vec -= 64;
00809     }
00810     
00811     if ((VidSt->trace)) {
00812       fprintf((VidSt->tf),"Vectors:\n");
00813     }
00814 
00815     if (x_vec < 0) x_vec += 64;
00816     if (y_vec < 0) y_vec += 64;
00817 
00818     bits->vec += put_mv (x_vec);
00819     bits->vec += put_mv (y_vec);
00820 
00821     if ((VidSt->trace)) {
00822       if (x_vec > 31) x_vec -= 64;
00823       if (y_vec > 31) y_vec -= 64;
00824       fprintf((VidSt->tf),"(x,y) = (%d,%d) - ",
00825               (2*MV[block][y][x]->x + MV[block][y][x]->x_half),
00826               (2*MV[block][y][x]->y + MV[block][y][x]->y_half));
00827       fprintf((VidSt->tf),"(Px,Py) = (%d,%d)\n", pmv0,pmv1);
00828       fprintf((VidSt->tf),"(x_diff,y_diff) = (%d,%d)\n",x_vec,y_vec);
00829     }
00830   }
00831 
00832   /* PB-frames delta vectors */
00833   if (pic->PB)
00834     if (pic->MODB == PBMODE_MVDB || pic->MODB == PBMODE_CBPB_MVDB) {
00835 
00836       x_vec = MV[5][y][x]->x;
00837       y_vec = MV[5][y][x]->y;
00838 
00839       /* x_vec and y_vec are the PB-delta vectors */
00840     
00841       if (x_vec < 0) x_vec += 64;
00842       if (y_vec < 0) y_vec += 64;
00843 
00844       if ((VidSt->trace)) {
00845         fprintf((VidSt->tf),"PB delta vectors:\n");
00846       }
00847 
00848       bits->vec += put_mv (x_vec);
00849       bits->vec += put_mv (y_vec);
00850 
00851       if ((VidSt->trace)) {
00852         if (x_vec > 31) x_vec -= 64;
00853         if (y_vec > 31) y_vec -= 64;
00854         fprintf((VidSt->tf),"MVDB (x,y) = (%d,%d)\n",x_vec,y_vec);
00855       }
00856     }
00857 
00858 
00859   return;
00860 }
00861 
00862 void Count_sac_BitsVectors(MotionVector *MV[6][MBR+1][MBC+2], Bits *bits,
00863                       int x, int y, int Mode, int newgob, Pict *pic)
00864 {
00865   int y_vec, x_vec;
00866   int pmv0, pmv1;
00867   int start,stop,block;
00868  
00869   (VidSt->arith_used) = 1;
00870   x++;y++;
00871  
00872   if (Mode == MODE_INTER4V) {
00873     start = 1; stop = 4;
00874   }
00875   else {
00876     start = 0; stop = 0;
00877   }
00878  
00879   for (block = start; block <= stop;  block++) {
00880  
00881     FindPMV(MV,x,y,&pmv0,&pmv1, block, newgob, 1);
00882  
00883     x_vec = (2*MV[block][y][x]->x + MV[block][y][x]->x_half) - pmv0;
00884     y_vec = (2*MV[block][y][x]->y + MV[block][y][x]->y_half) - pmv1;
00885  
00886     if (!(VidSt->long_vectors)) {
00887       if (x_vec < -32) x_vec += 64;
00888       else if (x_vec > 31) x_vec -= 64;
00889 
00890       if (y_vec < -32) y_vec += 64;
00891       else if (y_vec > 31) y_vec -= 64;
00892     }
00893     else {
00894       if (pmv0 < -31 && x_vec < -63) x_vec += 64;
00895       else if (pmv0 > 32 && x_vec > 63) x_vec -= 64;
00896 
00897       if (pmv1 < -31 && y_vec < -63) y_vec += 64;
00898       else if (pmv1 > 32 && y_vec > 63) y_vec -= 64;
00899     }
00900 
00901     if (x_vec < 0) x_vec += 64;
00902     if (y_vec < 0) y_vec += 64;
00903  
00904     if ((VidSt->trace)) {
00905       fprintf((VidSt->tf),"Vectors:\n");
00906     }
00907  
00908     bits->vec += AR_Encode(indexfn(x_vec,mvdtab,64),cumf_MVD);
00909     bits->vec += AR_Encode(indexfn(y_vec,mvdtab,64),cumf_MVD);
00910  
00911     if ((VidSt->trace)) {
00912       if (x_vec > 31) x_vec -= 64;
00913       if (y_vec > 31) y_vec -= 64;
00914       fprintf((VidSt->tf),"(x,y) = (%d,%d) - ",
00915               (2*MV[block][y][x]->x + MV[block][y][x]->x_half),
00916               (2*MV[block][y][x]->y + MV[block][y][x]->y_half));
00917       fprintf((VidSt->tf),"(Px,Py) = (%d,%d)\n", pmv0,pmv1);
00918       fprintf((VidSt->tf),"(x_diff,y_diff) = (%d,%d)\n",x_vec,y_vec);
00919     }
00920   }
00921 
00922    /* PB-frames delta vectors */
00923   if (pic->PB)
00924     if (pic->MODB == PBMODE_MVDB || pic->MODB == PBMODE_CBPB_MVDB) {
00925  
00926       x_vec = MV[5][y][x]->x;
00927       y_vec = MV[5][y][x]->y;
00928  
00929       if (x_vec < -32)
00930         x_vec += 64;
00931       else if (x_vec > 31)
00932         x_vec -= 64;
00933       if (y_vec < -32)
00934         y_vec += 64;
00935       else if (y_vec > 31)
00936         y_vec -= 64;
00937       
00938       if (x_vec < 0) x_vec += 64;
00939       if (y_vec < 0) y_vec += 64;
00940       
00941       if ((VidSt->trace)) {
00942         fprintf((VidSt->tf),"PB delta vectors:\n");
00943       }
00944       
00945       bits->vec += AR_Encode(indexfn(x_vec,mvdtab,64),cumf_MVD);
00946       bits->vec += AR_Encode(indexfn(y_vec,mvdtab,64),cumf_MVD);
00947       
00948       if ((VidSt->trace)) {
00949         if (x_vec > 31) x_vec -= 64;
00950         if (y_vec > 31) y_vec -= 64;
00951         fprintf((VidSt->tf),"MVDB (x,y) = (%d,%d)\n",x_vec,y_vec);
00952       }
00953     }
00954   
00955   return;
00956 }
00957 
00958 void FindPMV(MotionVector *MV[6][MBR+1][MBC+2], int x, int y, 
00959              int *pmv0, int *pmv1, int block, int newgob, int half_pel)
00960 {
00961   int p1,p2,p3;
00962   int xin1,xin2,xin3;
00963   int yin1,yin2,yin3;
00964   int vec1,vec2,vec3;
00965   int l8,o8,or8;
00966 
00967 
00968   l8 = o8 = or8 = 0;
00969   if (MV[0][y][x-1]->Mode == MODE_INTER4V)
00970     l8 = 1;
00971   if (MV[0][y-1][x]->Mode == MODE_INTER4V)
00972     o8 = 1;
00973   if (MV[0][y-1][x+1]->Mode == MODE_INTER4V)
00974     or8 = 1;
00975 
00976   switch (block) {
00977   case 0: 
00978     vec1 = (l8 ? 2 : 0) ; yin1 = y  ; xin1 = x-1;
00979     vec2 = (o8 ? 3 : 0) ; yin2 = y-1; xin2 = x;
00980     vec3 = (or8? 3 : 0) ; yin3 = y-1; xin3 = x+1;
00981     break;
00982   case 1:
00983     vec1 = (l8 ? 2 : 0) ; yin1 = y  ; xin1 = x-1;
00984     vec2 = (o8 ? 3 : 0) ; yin2 = y-1; xin2 = x;
00985     vec3 = (or8? 3 : 0) ; yin3 = y-1; xin3 = x+1;
00986     break;
00987   case 2:
00988     vec1 = 1            ; yin1 = y  ; xin1 = x;
00989     vec2 = (o8 ? 4 : 0) ; yin2 = y-1; xin2 = x;
00990     vec3 = (or8? 3 : 0) ; yin3 = y-1; xin3 = x+1;
00991     break;
00992   case 3:
00993     vec1 = (l8 ? 4 : 0) ; yin1 = y  ; xin1 = x-1;
00994     vec2 = 1            ; yin2 = y  ; xin2 = x;
00995     vec3 = 2            ; yin3 = y  ; xin3 = x;
00996     break;
00997   case 4:
00998     vec1 = 3            ; yin1 = y  ; xin1 = x;
00999     vec2 = 1            ; yin2 = y  ; xin2 = x;
01000     vec3 = 2            ; yin3 = y  ; xin3 = x;
01001     break;
01002   default:
01003     fprintf(stderr,"Illegal block number in FindPMV (vid_countbit.c)\n");
01004     exit(-1);
01005     break;
01006   }
01007   if (half_pel) {
01008     p1 = 2*MV[vec1][yin1][xin1]->x + MV[vec1][yin1][xin1]->x_half;
01009     p2 = 2*MV[vec2][yin2][xin2]->x + MV[vec2][yin2][xin2]->x_half;
01010     p3 = 2*MV[vec3][yin3][xin3]->x + MV[vec3][yin3][xin3]->x_half;
01011   }
01012   else {
01013     p1 = 2*MV[vec1][yin1][xin1]->x;
01014     p2 = 2*MV[vec2][yin2][xin2]->x;
01015     p3 = 2*MV[vec3][yin3][xin3]->x;
01016   }
01017   if (newgob && (block == 0 || block == 1 || block == 2))
01018     p2 = 2*NO_VEC;
01019 
01020   if (p2 == 2*NO_VEC) { p2 = p3 = p1; }
01021 
01022   *pmv0 = p1+p2+p3 - mmax(p1,mmax(p2,p3)) - mmin(p1,mmin(p2,p3));
01023     
01024   if (half_pel) {
01025     p1 = 2*MV[vec1][yin1][xin1]->y + MV[vec1][yin1][xin1]->y_half;
01026     p2 = 2*MV[vec2][yin2][xin2]->y + MV[vec2][yin2][xin2]->y_half;
01027     p3 = 2*MV[vec3][yin3][xin3]->y + MV[vec3][yin3][xin3]->y_half;
01028   }
01029   else {
01030     p1 = 2*MV[vec1][yin1][xin1]->y;
01031     p2 = 2*MV[vec2][yin2][xin2]->y;
01032     p3 = 2*MV[vec3][yin3][xin3]->y;
01033   }    
01034   if (newgob && (block == 0 || block == 1 || block == 2))
01035     p2 = 2*NO_VEC;
01036 
01037   if (p2 == 2*NO_VEC) { p2 = p3 = p1; }
01038 
01039   *pmv1 = p1+p2+p3 - mmax(p1,mmax(p2,p3)) - mmin(p1,mmin(p2,p3));
01040   
01041   return;
01042 }
01043 
01044 void ZeroBits(Bits *bits)
01045 {
01046   bits->Y = 0;
01047   bits->C = 0;
01048   bits->vec = 0;
01049   bits->CBPY = 0;
01050   bits->CBPCM = 0;
01051   bits->MODB = 0;
01052   bits->CBPB = 0;
01053   bits->COD = 0;
01054   bits->DQUANT = 0;
01055   bits->header = 0;
01056   bits->total = 0;
01057   bits->no_inter = 0;
01058   bits->no_inter4v = 0;
01059   bits->no_intra = 0;
01060   return;
01061 }
01062 void ZeroRes(Results *res)
01063 {
01064   res->SNR_l = (float)0;
01065   res->SNR_Cr = (float)0;
01066   res->SNR_Cb = (float)0;
01067   res->QP_mean = (float)0;
01068 }
01069 void AddBits(Bits *total, Bits *bits)
01070 {
01071   total->Y += bits->Y;
01072   total->C += bits->C;
01073   total->vec += bits->vec;
01074   total->CBPY += bits->CBPY;
01075   total->CBPCM += bits->CBPCM;
01076   total->MODB += bits->MODB;
01077   total->CBPB += bits->CBPB;
01078   total->COD += bits->COD;
01079   total->DQUANT += bits->DQUANT;
01080   total->header += bits->header;
01081   total->total += bits->total;
01082   total->no_inter += bits->no_inter;
01083   total->no_inter4v += bits->no_inter4v;
01084   total->no_intra += bits->no_intra;
01085   return;
01086 }
01087 void AddRes(Results *total, Results *res, Pict *pic)
01088 {
01089   total->SNR_l += res->SNR_l;
01090   total->SNR_Cr += res->SNR_Cr;
01091   total->SNR_Cb += res->SNR_Cb;
01092   total->QP_mean += pic->QP_mean;
01093   return;
01094 }
01095 
01096 void AddBitsPicture(Bits *bits)
01097 {
01098   bits->total = 
01099     bits->Y + 
01100     bits->C + 
01101     bits->vec +  
01102     bits->CBPY + 
01103     bits->CBPCM + 
01104     bits->MODB +
01105     bits->CBPB +
01106     bits->COD + 
01107     bits->DQUANT +
01108     bits->header ;
01109 } 
01110 void ZeroVec(MotionVector *MV)
01111 {
01112   MV->x = 0;
01113   MV->y = 0;
01114   MV->x_half = 0;
01115   MV->y_half = 0;
01116   return;
01117 }
01118 void MarkVec(MotionVector *MV)
01119 {
01120   MV->x = NO_VEC;
01121   MV->y = NO_VEC;
01122   MV->x_half = 0;
01123   MV->y_half = 0;
01124   return;
01125 }
01126 
01127 void CopyVec(MotionVector *MV2, MotionVector *MV1)
01128 {
01129   MV2->x = MV1->x;
01130   MV2->x_half = MV1->x_half;
01131   MV2->y = MV1->y;
01132   MV2->y_half = MV1->y_half;
01133   return;
01134 }
01135 
01136 int EqualVec(MotionVector *MV2, MotionVector *MV1)
01137 {
01138   if (MV1->x != MV2->x)
01139     return 0;
01140   if (MV1->y != MV2->y)
01141     return 0;
01142   if (MV1->x_half != MV2->x_half)
01143     return 0;
01144   if (MV1->y_half != MV2->y_half)
01145     return 0;
01146   return 1;
01147 }
01148 
01149 /**********************************************************************
01150  *
01151  *      Name:        CountBitsPicture(Pict *pic)
01152  *      Description:    counts the number of bits needed for picture
01153  *                      header
01154  *      
01155  *      Input:          pointer to picture structure
01156  *      Returns:        number of bits
01157  *      Side effects:
01158  *
01159  *      Date: 941128    Author:Karl.Lillevold@nta.no
01160  *
01161  ***********************************************************************/
01162 
01163 int CountBitsPicture(Pict *pic)
01164 {
01165   int bits = 0;
01166 
01167   /* in case of arithmetic coding, encoder_flush() has been called before
01168      zeroflush() in vid_main.c */
01169 
01170   /* Picture start code */
01171   if ((VidSt->trace)) {
01172     fprintf((VidSt->tf),"picture_start_code: ");
01173   }
01174   putbits(PSC_LENGTH,PSC);
01175   bits += PSC_LENGTH;
01176 
01177   /* Group number */
01178   if ((VidSt->trace)) {
01179     fprintf((VidSt->tf),"Group number in picture header: ");
01180   }
01181   putbits(5,0); 
01182   bits += 5;
01183   
01184   /* Time reference */
01185   if ((VidSt->trace)) {
01186     fprintf((VidSt->tf),"Time reference: ");
01187   }
01188   putbits(8,pic->TR);
01189   bits += 8;
01190 
01191  /* bit 1 */
01192   if ((VidSt->trace)) {
01193     fprintf((VidSt->tf),"spare: ");
01194   }
01195   pic->spare = 1; /* always 1 to avoid start code emulation */
01196   putbits(1,pic->spare);
01197   bits += 1;
01198 
01199   /* bit 2 */
01200   if ((VidSt->trace)) {
01201     fprintf((VidSt->tf),"always zero for distinction with H.261\n");
01202   }
01203   putbits(1,0);
01204   bits += 1;
01205   
01206   /* bit 3 */
01207   if ((VidSt->trace)) {
01208     fprintf((VidSt->tf),"split_screen_indicator: ");
01209   }
01210   putbits(1,0);     /* no support for split-screen in this software */
01211   bits += 1;
01212 
01213   /* bit 4 */
01214   if ((VidSt->trace)) {
01215     fprintf((VidSt->tf),"document_camera_indicator: ");
01216   }
01217   putbits(1,0);
01218   bits += 1;
01219 
01220   /* bit 5 */
01221   if ((VidSt->trace)) {
01222     fprintf((VidSt->tf),"freeze_picture_release: ");
01223   }
01224   putbits(1,0);
01225   bits += 1;
01226 
01227   /* bit 6-8 */
01228   if ((VidSt->trace)) {
01229     fprintf((VidSt->tf),"source_format: ");
01230   }
01231   putbits(3,pic->source_format);
01232   bits += 3;
01233 
01234   /* bit 9 */
01235   if ((VidSt->trace)) {
01236     fprintf((VidSt->tf),"picture_coding_type: ");
01237   }
01238   putbits(1,pic->picture_coding_type);
01239   bits += 1;
01240 
01241   /* bit 10 */
01242   if ((VidSt->trace)) {
01243     fprintf((VidSt->tf),"mv_outside_frame: ");
01244   }
01245   putbits(1,pic->unrestricted_mv_mode);  /* Unrestricted Motion Vector mode */
01246   bits += 1;
01247 
01248   /* bit 11 */
01249   if ((VidSt->trace)) {
01250     fprintf((VidSt->tf),"sac_coding: ");
01251   }
01252   putbits(1,(VidSt->syntax_arith_coding)); /* Syntax-based Arithmetic Coding mode */
01253   bits += 1;
01254 
01255   /* bit 12 */
01256   if ((VidSt->trace)) {
01257     fprintf((VidSt->tf),"adv_pred_mode: ");
01258   }
01259   putbits(1,(VidSt->advanced)); /* Advanced Prediction mode */
01260   bits += 1;
01261 
01262   /* bit 13 */
01263   if ((VidSt->trace)) {
01264     fprintf((VidSt->tf),"PB-coded: "); /* PB-frames mode */
01265   }
01266   putbits(1,pic->PB);
01267   bits += 1;
01268 
01269 
01270   /* QUANT */
01271   if ((VidSt->trace)) {
01272     fprintf((VidSt->tf),"QUANT: ");
01273   }
01274   putbits(5,pic->QUANT);
01275   bits += 5;
01276 
01277   /* Continuous Presence Multipoint (CPM) */
01278   putbits(1,0); /* CPM is not supported in this software */
01279   bits += 1;
01280 
01281   /* Picture Sub Bitstream Indicator (PSBI) */
01282   /* if CPM == 1: 2 bits PSBI */
01283   /* not supported */
01284 
01285   /* extra information for PB-frames */
01286   if (pic->PB) {
01287     if ((VidSt->trace)) {
01288       fprintf((VidSt->tf),"TRB: ");
01289     }
01290     putbits(3,pic->TRB);
01291     bits += 3;
01292 
01293     if ((VidSt->trace)) {
01294       fprintf((VidSt->tf),"BQUANT: ");
01295     }
01296     putbits(2,pic->BQUANT);
01297     bits += 2;
01298     
01299   }
01300 
01301   /* PEI (extra information) */
01302   if ((VidSt->trace)) {
01303     fprintf((VidSt->tf),"PEI: ");
01304   }
01305   /* "Encoders shall not insert PSPARE until specified by the ITU" */
01306   putbits(1,0); 
01307   bits += 1;
01308 
01309   /* PSPARE */
01310   /* if PEI == 1: 8 bits PSPARE + another PEI bit */
01311   /* not supported */
01312 
01313   return bits;
01314 }
01315 

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