Main Page | File List

GETVLC.C

00001 /************************************************************************
00002  *
00003  *  getvlc.c, variable length decoding for tmndecode (H.263 decoder)
00004  *  Copyright (C) 1995, 1996  Telenor R&D, Norway
00005  *
00006  *  Contacts:
00007  *  Robert Danielsen                  <Robert.Danielsen@nta.no>
00008  *
00009  *  Telenor Research and Development  http://www.nta.no/brukere/DVC/
00010  *  P.O.Box 83                        tel.:   +47 63 84 84 00
00011  *  N-2007 Kjeller, Norway            fax.:   +47 63 81 00 76
00012  *
00013  *  Copyright (C) 1997  University of BC, Canada
00014  *  Modified by: Michael Gallant <mikeg@ee.ubc.ca>
00015  *               Guy Cote <guyc@ee.ubc.ca>
00016  *               Berna Erol <bernae@ee.ubc.ca>
00017  *
00018  *  Contacts:
00019  *  Michael Gallant                   <mikeg@ee.ubc.ca>
00020  *
00021  *  UBC Image Processing Laboratory   http://www.ee.ubc.ca/image
00022  *  2356 Main Mall                    tel.: +1 604 822 4051
00023  *  Vancouver BC Canada V6T1Z4        fax.: +1 604 822 5949
00024  *
00025  ************************************************************************/
00026 
00027 /* Disclaimer of Warranty
00028  * 
00029  * These software programs are available to the user without any license fee
00030  * or royalty on an "as is" basis. The University of British Columbia
00031  * disclaims any and all warranties, whether express, implied, or
00032  * statuary, including any implied warranties or merchantability or of
00033  * fitness for a particular purpose.  In no event shall the
00034  * copyright-holder be liable for any incidental, punitive, or
00035  * consequential damages of any kind whatsoever arising from the use of
00036  * these programs.
00037  * 
00038  * This disclaimer of warranty extends to the user of these programs and
00039  * user's customers, employees, agents, transferees, successors, and
00040  * assigns.
00041  * 
00042  * The University of British Columbia does not represent or warrant that the
00043  * programs furnished hereunder are free of infringement of any
00044  * third-party patents.
00045  * 
00046  * Commercial implementations of H.263, including shareware, are subject to
00047  * royalty fees to patent holders.  Many of these patents are general
00048  * enough such that they are unavoidable regardless of implementation
00049  * design.
00050  * 
00051  */
00052 
00053 
00054 
00055 /* based on mpeg2decode, (C) 1994, MPEG Software Simulation Group and
00056  * mpeg2play, (C) 1994 Stefan Eckart <stefan@lis.e-technik.tu-muenchen.de>
00057  * 
00058  */
00059 
00060 
00061 #include <stdio.h>
00062 
00063 #include "config.h"
00064 #include "tmndec.h"
00065 #include "global.h"
00066 #include "getvlc.h"
00067 
00068 int getTMNMV ()
00069 {
00070   int code;
00071 
00072  
00073   if (getbits1 ())
00074   {
00075     return 0;
00076   }
00077   if ((code = showbits (12)) >= 512)
00078   {
00079     code = (code >> 8) - 2;
00080     flushbits (TMNMVtab0[code].len);
00081 
00082     return TMNMVtab0[code].val;
00083   }
00084   if (code >= 128)
00085   {
00086     code = (code >> 2) - 32;
00087     flushbits (TMNMVtab1[code].len);
00088 
00089     return TMNMVtab1[code].val;
00090   }
00091   if ((code -= 5) < 0)
00092   {
00093     fault = 1;
00094     return 0;
00095   }
00096   flushbits (TMNMVtab2[code].len);
00097 
00098   return TMNMVtab2[code].val;
00099 }
00100 
00101 /**********************************************************************
00102  *
00103  *      Name:        getRVLC
00104  *      Description: extracts RVLC for motion vectors in Annex D
00105  *
00106  *      Input:
00107  *
00108  *      Returns:     motion vector component
00109  *      Side effects:
00110  *
00111  *      Date: 971026 Author: Guy Cote -- guyc@ee.ubc.ca
00112  *
00113  ***********************************************************************/
00114  
00115 int getRVLC ()
00116 {
00117  
00118   int code = 0, sign;
00119  
00120  
00121   if (getbits1 ())
00122   {
00123     return 0;
00124   }
00125   code = 2 + getbits(1);
00126   while (getbits(1))
00127   {
00128     code <<= 1;
00129     code += getbits(1);
00130   }
00131   sign = code & 1;
00132   code >>= 1;
00133   return (sign) ? -code : code;
00134 }
00135 
00136 int getMCBPC ()
00137 {
00138   int code;
00139 
00140   code = showbits (13);
00141 
00142   if (code >> 4 == 1)
00143   {
00144     /* macroblock stuffing */
00145     flushbits (9);
00146     return 255;
00147   }
00148   if (code == 0)
00149   {
00150     fault = 1;
00151     return 0;
00152   }
00153   if (code >= 4096)
00154   {
00155     flushbits (1);
00156     return 0;
00157   }
00158   if (code >= 16)
00159   {
00160     flushbits (MCBPCtab0[code >> 4].len);
00161     return MCBPCtab0[code >> 4].val;
00162   } else
00163   {
00164     flushbits (MCBPCtab1[code - 8].len);
00165     return MCBPCtab1[code - 8].val;
00166   }
00167 }
00168 
00169 /**********************************************************************
00170  *
00171  *      Name:        getMBTYPE
00172  *      Description: extracts annex O MBTYPE information from bitstream
00173  *               and whether or not CBP or Quant fields 
00174  *               follow
00175  *
00176  *      Input:       pointers for cbp and dquant (to be filled in from
00177  *               MBTYPE table in annex O).
00178  *
00179  *      Returns:
00180  *      Side effects:
00181  *
00182  *      Date: 971102 Author: Michael Gallant --- mikeg@ee.ubc.ca
00183  *
00184  ***********************************************************************/
00185 int getMBTYPE (int *cbp_present, int *quant_present)
00186 { 
00187   int code;
00188 
00189   code = showbits (9);
00190   
00191   if (code == 1)
00192   {
00193     /* macroblock stuffing */
00194     flushbits (9);
00195     *cbp_present = *quant_present = NO;
00196     return B_EI_EP_STUFFING;
00197 
00198   }
00199 
00200   switch (pict_type)
00201   {
00202     case PCT_B:
00203 
00204       if (code < 4)
00205       {
00206         fault = 1;
00207         *cbp_present = *quant_present = -1;
00208         return INVALID_MBTYPE;
00209       }
00210 
00211       code >>= 2;
00212 
00213       if (code >= 96)
00214       {
00215         flushbits (2);
00216         *cbp_present = YES;
00217         *quant_present = NO;
00218         return B_DIRECT_PREDICTION;
00219       }
00220       else
00221       {
00222         flushbits (MBTYPEtabB[code].len);
00223         *cbp_present = CBP_present_B[MBTYPEtabB[code].val];
00224         *quant_present = QUANT_present_B[MBTYPEtabB[code].val];
00225         return PRED_type_B[MBTYPEtabB[code].val];
00226       }
00227 
00228       break;
00229 
00230     case PCT_EP:
00231 
00232       if (code < 2)
00233       {
00234         fault = 1;
00235         *cbp_present = *quant_present = -1;
00236         return INVALID_MBTYPE;
00237       }
00238 
00239       code >>= 1;
00240 
00241       if (code >= 128)
00242       {
00243         flushbits (1);
00244         *cbp_present = YES;
00245         *quant_present = NO;
00246         return EP_FORWARD_PREDICTION;
00247       }
00248       else
00249       {
00250         flushbits (MBTYPEtabEP[code].len);
00251         *cbp_present = CBP_present_EP[MBTYPEtabEP[code].val];
00252         *quant_present = QUANT_present_EP[MBTYPEtabEP[code].val];
00253         return PRED_type_EP[MBTYPEtabEP[code].val];
00254       }
00255 
00256       break;
00257 
00258     case PCT_EI:
00259 
00260       /* In the case of EI pictures, we use cbp_present to return the chrominance 
00261        * coded block pattern */
00262       if (code < 2)
00263       {
00264         fault = 1;
00265         *cbp_present = *quant_present = -1;
00266         return INVALID_MBTYPE;
00267       }
00268 
00269       code >>= 1;
00270   
00271       if (8 == code)
00272       {
00273         fault = 1;
00274         *cbp_present = *quant_present = -1;
00275         return INVALID_MBTYPE;
00276       }
00277   
00278       if (code >= 128)
00279       {
00280         flushbits (1);
00281         *cbp_present = 0;
00282         *quant_present = NO;
00283         return EI_EP_UPWARD_PREDICTION;
00284       }
00285       else
00286       {
00287         flushbits (MBTYPEtabEI[code].len);
00288         *cbp_present = CBPC_pattern_EI[MBTYPEtabEI[code].val];
00289         *quant_present = QUANT_present_EI[MBTYPEtabEI[code].val];
00290         return PRED_type_EI[MBTYPEtabEI[code].val];
00291       }
00292 
00293       break;
00294       
00295     default:
00296 
00297       break;
00298 
00299   }    
00300 }
00301 
00302 int getMODB ()
00303 {
00304   int code;
00305   int MODB;
00306 
00307   
00308   if (pict_type == PCT_IPB)
00309   {
00310     code = getbits (1);
00311     if (code == 0)
00312     {
00313       MODB = PBMODE_BIDIR_PRED;
00314     }
00315     else
00316     {
00317       code=getbits (1);
00318       if (code==0)
00319       {
00320         MODB = PBMODE_CBPB_BIDIR_PRED;
00321       }
00322       else
00323       {
00324         code=getbits (1);
00325         if (code==0) 
00326         {
00327           MODB = PBMODE_FRW_PRED;
00328         }
00329         else
00330         {
00331           code=getbits (1);
00332           if (code==0) 
00333           {
00334             MODB = PBMODE_CBPB_FRW_PRED;
00335           }
00336           else
00337           {
00338             code=getbits (1);
00339             if (code==0) 
00340             {
00341               MODB = PBMODE_BCKW_PRED;
00342             }
00343             else
00344             {
00345               MODB = PBMODE_CBPB_BCKW_PRED;
00346             }
00347           }
00348         }
00349       }
00350     }
00351   } else
00352   {
00353     code = showbits (2);
00354 
00355     if (code < 2)
00356     {
00357       MODB = 0;
00358       flushbits (1);
00359     } else if (code == 2)
00360     {
00361       MODB = 1;
00362       flushbits (2);
00363     } else
00364     {
00365       /* code == 3 */
00366       MODB = 2;
00367       flushbits (2);
00368     }
00369   }
00370 
00371   return MODB;
00372 
00373 }
00374 
00375 
00376 int getMCBPCintra ()
00377 {
00378   int code;
00379 
00380   
00381   code = showbits (9);
00382 
00383   if (code == 1)
00384   {
00385     /* macroblock stuffing */
00386     flushbits (9);
00387     return 255;
00388 
00389   }
00390   if (code < 8)
00391   {
00392     fault = 1;
00393     return 0;
00394   }
00395   code >>= 3;
00396 
00397   if (code >= 32)
00398   {
00399     flushbits (1);
00400     return 3;
00401   }
00402   flushbits (MCBPCtabintra[code].len);
00403 
00404   return MCBPCtabintra[code].val;
00405 }
00406 
00407 /* extract vlc representing true B CBPC --- mikeg@ee.ubc.ca */
00408 int getscalabilityCBPC ()
00409 {
00410   int code;
00411 
00412   
00413   code = showbits (1);
00414 
00415   if (0 == code)
00416   {
00417     flushbits (1);
00418     return 0;
00419   } else
00420   {
00421     code = showbits (2);
00422 
00423     if (2 == code)
00424     {
00425       flushbits (2);
00426       return 1;
00427     } else
00428     {
00429       code = showbits (3);
00430 
00431       if (6 == code)
00432       {
00433         flushbits (3);
00434         return 3;
00435       } else if (7 == code)
00436       {
00437         flushbits (3);
00438         return 2;
00439       } else
00440       {
00441         fault = 1;
00442         return 0;
00443       }
00444     }
00445   }
00446 }
00447 
00448 int getCBPY ()
00449 {
00450   int code;
00451 
00452   
00453   code = showbits (6);
00454   if (code < 2)
00455   {
00456     fault = 1;
00457     return -1;
00458   }
00459   if (code >= 48)
00460   {
00461     flushbits (2);
00462     return 0;
00463   }
00464   flushbits (CBPYtab[code].len);
00465 
00466   
00467   return CBPYtab[code].val;
00468 }

Generated on Mon May 8 22:27:08 2006 by  doxygen 1.3.9.1