Main Page | File List

GETBLK.C

00001 /************************************************************************
00002  *
00003  *  getblk.c, DCT block 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 /* based on mpeg2decode, (C) 1994, MPEG Software Simulation Group and
00055  * mpeg2play, (C) 1994 Stefan Eckart <stefan@lis.e-technik.tu-muenchen.de>
00056  * 
00057  */
00058 
00059 
00060 #include <stdio.h>
00061 #include <stdlib.h>
00062 
00063 #include "config.h"
00064 #include "tmndec.h"
00065 #include "global.h"
00066 
00067 #define INDICES
00068 #include "indices.h"
00069 
00070 #define SACTABLES
00071 #include "sactbls.h"
00072 
00073 
00074 typedef struct
00075 {
00076   char run, level, len;
00077 } DCTtab;
00078 typedef struct
00079 {
00080   int val, len;
00081 } VLCtabI;
00082 typedef struct
00083 {
00084   int val, run, sign;
00085 } RunCoef;
00086 
00087 typedef struct
00088 {
00089   int code, level, last, run, sign, extended_level;
00090 } CodeCoeff;
00091 
00092 
00093 
00094 /* local prototypes */
00095 RunCoef vlc_word_decode _ANSI_ARGS_ ((int symbol_word, int *last));
00096 RunCoef Decode_Escape_Char _ANSI_ARGS_ ((int intra, int *last));
00097 int DecodeTCoef _ANSI_ARGS_ ((int position, int intra));
00098 
00099 
00100 
00101 extern VLCtabI DCT3Dtab0[], DCT3Dtab1[], DCT3Dtab2[];
00102 
00103 /* Tables used in advanced intra coding mode */
00104 extern VLCtabI INTRA_DCT3Dtab0[], INTRA_DCT3Dtab1[], INTRA_DCT3Dtab2[];
00105 
00106 extern MQ_chroma_QP_table[];
00107 
00108 void getblock (int comp, int mode, int INTRA_AC_DC, int Mode)
00109 {
00110   int val, i, j, sign;
00111   unsigned int code;
00112   VLCtabI *tab;
00113   short *bp;
00114   int run, last, level, QP;
00115   short *qval;
00116 
00117 
00118   /* store_code used in alternative inter VLC mode */
00119   int use_intra = 0;
00120   CodeCoeff store_code[64];
00121   int coeff_ind;
00122 
00123   bp = ld->block[comp];
00124 
00125   /* decode AC coefficients (or all coefficients in advanced intra coding
00126    * mode) */
00127 
00128   if (alternative_inter_VLC_mode && !(Mode == MODE_INTRA || Mode == MODE_INTRA_Q))
00129   {
00130     coeff_ind = 0;
00131     use_intra = 0;
00132     for (i = (mode == 0);; i++)
00133     {
00134       code = showbits (12);
00135       store_code[coeff_ind].code = code;
00136       if (code >= 512)
00137         tab = &DCT3Dtab0[(code >> 5) - 16];
00138       else if (code >= 128)
00139         tab = &DCT3Dtab1[(code >> 2) - 32];
00140       else if (code >= 8)
00141         tab = &DCT3Dtab2[(code >> 0) - 8];
00142       else
00143       {
00144         fault = 1;
00145         return;
00146       }
00147 
00148       run = (tab->val >> 4) & 255;
00149       last = (tab->val >> 12) & 1;
00150       flushbits (tab->len);
00151       if (tab->val == ESCAPE)
00152       {
00153         last = getbits1 ();
00154         store_code[coeff_ind].last = last;
00155 
00156         i += run = getbits (6);
00157         store_code[coeff_ind].run = run;
00158 
00159         level = getbits (8);
00160         store_code[coeff_ind].level = level;
00161 
00162 
00163         if (level == 128 && modified_quantization_mode)
00164         {
00165           level = getbits (11);
00166           store_code[coeff_ind].extended_level = level;
00167         }
00168       } else
00169       {
00170         store_code[coeff_ind].sign = getbits (1);
00171         i += run;
00172       }
00173       coeff_ind += 1;
00174 
00175       if (i >= 64)
00176         use_intra = 1;
00177       if (last)
00178         break;
00179     }
00180   }
00181   coeff_ind = 0;
00182 
00183   for (i = (mode == 0);; i++)
00184   {
00185     if ((alternative_inter_VLC_mode) && !(Mode == MODE_INTRA || Mode == MODE_INTRA_Q))
00186     {
00187       if (use_intra)
00188       {
00189         if ((store_code[coeff_ind].code) >= 512)
00190           tab = &INTRA_DCT3Dtab0[((store_code[coeff_ind].code) >> 5) - 16];
00191         else if ((store_code[coeff_ind].code) >= 128)
00192           tab = &INTRA_DCT3Dtab1[((store_code[coeff_ind].code) >> 2) - 32];
00193         else if ((store_code[coeff_ind].code) >= 8)
00194           tab = &INTRA_DCT3Dtab2[((store_code[coeff_ind].code) >> 0) - 8];
00195         else
00196         {
00197           fault = 1;
00198           return;
00199         }
00200         run = (tab->val >> 6) & 63;
00201         level = tab->val & 63;
00202         last = (tab->val >> 12) & 1;
00203       } else
00204       {
00205         if (store_code[coeff_ind].code >= 512)
00206           tab = &DCT3Dtab0[(store_code[coeff_ind].code >> 5) - 16];
00207         else if (store_code[coeff_ind].code >= 128)
00208           tab = &DCT3Dtab1[(store_code[coeff_ind].code >> 2) - 32];
00209         else if (store_code[coeff_ind].code >= 8)
00210           tab = &DCT3Dtab2[(store_code[coeff_ind].code >> 0) - 8];
00211         else
00212         {
00213           fault = 1;
00214           return;
00215         }
00216 
00217         run = (tab->val >> 4) & 255;
00218         level = tab->val & 15;
00219         last = (tab->val >> 12) & 1;
00220       }
00221 
00222       if (tab->val == ESCAPE)
00223       {                         /* escape */
00224         last = store_code[coeff_ind].last;
00225         i += run = store_code[coeff_ind].run;
00226         level = store_code[coeff_ind].level;
00227 
00228         if ((sign = (level >= 128)))
00229           val = 256 - level;
00230         else
00231           val = level;
00232 
00233         if (level == 128 && modified_quantization_mode)
00234         {
00235           level = store_code[coeff_ind].extended_level;
00236           level = (level >> 6 & 0x001F) | level << 5;
00237           /* correctly set the sign */
00238           level = (level << (sizeof (int) * 8 - 11)) >> (sizeof (int) * 8 - 11);
00239           if (level >= 0)
00240             sign = 0;
00241           else
00242             sign = 1;
00243           val = abs (level);
00244         }
00245       } else
00246       {
00247         sign = store_code[coeff_ind].sign;
00248         i += run;
00249         val = level;
00250       }
00251       coeff_ind += 1;
00252 
00253     } else
00254     {
00255       code = showbits (12);
00256       if ((advanced_intra_coding) && (Mode == MODE_INTRA || Mode == MODE_INTRA_Q))
00257       {
00258         if (code >= 512)
00259           tab = &INTRA_DCT3Dtab0[(code >> 5) - 16];
00260         else if (code >= 128)
00261           tab = &INTRA_DCT3Dtab1[(code >> 2) - 32];
00262         else if (code >= 8)
00263           tab = &INTRA_DCT3Dtab2[(code >> 0) - 8];
00264         else
00265         {
00266           fault = 1;
00267           return;
00268         }
00269         run = (tab->val >> 6) & 63;
00270         level = tab->val & 63;
00271         last = (tab->val >> 12) & 1;
00272       } else
00273       {
00274 
00275         if (code >= 512)
00276           tab = &DCT3Dtab0[(code >> 5) - 16];
00277         else if (code >= 128)
00278           tab = &DCT3Dtab1[(code >> 2) - 32];
00279         else if (code >= 8)
00280           tab = &DCT3Dtab2[(code >> 0) - 8];
00281         else
00282         {
00283           fault = 1;
00284           return;
00285         }
00286 
00287         run = (tab->val >> 4) & 255;
00288         level = tab->val & 15;
00289         last = (tab->val >> 12) & 1;
00290       }
00291       flushbits (tab->len);
00292       if (tab->val == ESCAPE)
00293       {                         /* escape */
00294         last = getbits1 ();
00295         i += run = getbits (6);
00296         level = getbits (8);
00297 
00298         if ((sign = (level >= 128)))
00299           val = 256 - level;
00300         else
00301           val = level;
00302 
00303         if (level == 128 && modified_quantization_mode)
00304         {
00305           /* extended escape code was received */
00306           level = getbits (11);
00307           level = (level >> 6 & 0x001F) | level << 5;
00308           /* correctly set the sign */
00309           level = (level << (sizeof (int) * 8 - 11)) >> (sizeof (int) * 8 - 11);
00310           if (level >= 0)
00311             sign = 0;
00312           else
00313             sign = 1;
00314           val = abs (level);
00315         }
00316       } else
00317       {
00318         i += run;
00319         val = level;
00320         sign = getbits (1);
00321       }
00322     }
00323 
00324     if (i >= 64)
00325     {
00326       fault = 1;
00327       return;
00328     }
00329 
00330     /* Descan in the proper order in advanced intra coding mode */
00331 
00332     if (advanced_intra_coding && (Mode == MODE_INTRA || Mode == MODE_INTRA_Q))
00333     {
00334       switch (INTRA_AC_DC)
00335       {
00336         case INTRA_MODE_DC:
00337           j = zig_zag_scan[i];
00338           break;
00339         case INTRA_MODE_VERT_AC:
00340           j = alternate_horizontal_scan[i];
00341           break;
00342         case INTRA_MODE_HORI_AC:
00343           j = alternate_vertical_scan[i];
00344           break;
00345         default:
00346           exit (-1);
00347           break;
00348       }
00349     } else
00350     {
00351       j = zig_zag_scan[i];
00352     }
00353     qval = &bp[j];
00354     if (comp >= 6)
00355       QP = mmax (1, mmin (31, (bquant_tab[bquant] * quant) >> 2));
00356     else
00357       QP = quant;
00358 
00359     if (modified_quantization_mode && (comp == 4 || comp == 5 || comp == 10 || comp == 11))
00360     {
00361       /* when modified quantization mode is on, use modified quantizer for
00362        * chorominance coefficients */
00363 
00364       QP = MQ_chroma_QP_table[QP];
00365     }
00366     if ((advanced_intra_coding) && (Mode == MODE_INTRA || Mode == MODE_INTRA_Q))
00367     {
00368       /* Advanced intra coding dequantization */
00369       *qval = (sign ? -(QP * (2 * val)) : QP * (2 * val));
00370     } else
00371     {
00372       /* TMN3 dequantization */
00373       if ((QP % 2) == 1)
00374         *qval = (sign ? -(QP * (2 * val + 1)) : QP * (2 * val + 1));
00375       else
00376         *qval = (sign ? -(QP * (2 * val + 1) - 1) : QP * (2 * val + 1) - 1);
00377     }
00378     if (last)
00379     {                           /* That's it */
00380       return;
00381     }
00382   }
00383 }
00384 
00385 
00386 
00387 /*********************************************************************
00388  *
00389  *        Name:        get_sac_block
00390  *
00391  *      Description:    Decodes blocks of Arithmetic Encoded DCT Coeffs.
00392  *        and performs Run Length Decoding and Coefficient
00393  *        Dequantisation.
00394  *
00395  *      Input:        Picture block type and number.
00396  *
00397  *      Returns:        Nothing.
00398  *
00399  *      Side Effects:
00400  *
00401  *      Author:        Wayne Ellis <ellis_w_wayne@bt-web.bt.co.uk>
00402  *
00403  *********************************************************************/
00404 
00405 
00406 void get_sac_block (int comp, int ptype, int INTRA_AC_DC, int Mode)
00407 {
00408   int position = 0;
00409   int TCOEF_index, symbol_word;
00410   int last = 0, QP, i, j;
00411   short *qval;
00412   short *bp;
00413   RunCoef DCTcoef;
00414   int intra;
00415 
00416 
00417   bp = ld->block[comp];
00418 
00419   i = (ptype == 0);
00420   intra = (Mode == MODE_INTRA || Mode == MODE_INTRA_Q);
00421 
00422   while (!last)
00423   {                             /* while there are DCT coefficients
00424                                  * remaining */
00425     position++;                 /* coefficient counter relates to Coeff.
00426                                  * model */
00427     TCOEF_index = DecodeTCoef (position, intra);
00428 
00429     if (TCOEF_index == ESCAPE_INDEX)
00430     {                           /* ESCAPE code encountered */
00431       DCTcoef = Decode_Escape_Char (intra, &last);
00432     } else
00433     {
00434       symbol_word = tcoeftab[TCOEF_index];
00435 
00436       DCTcoef = vlc_word_decode (symbol_word, &last);
00437     }
00438 
00439     i += DCTcoef.run;
00440     if (advanced_intra_coding && (Mode == MODE_INTRA || Mode == MODE_INTRA_Q))
00441     {
00442       switch (INTRA_AC_DC)
00443       {
00444         case INTRA_MODE_DC:
00445           j = zig_zag_scan[i];
00446           break;
00447         case INTRA_MODE_VERT_AC:
00448           j = alternate_horizontal_scan[i];
00449           break;
00450         case INTRA_MODE_HORI_AC:
00451           j = alternate_vertical_scan[i];
00452           break;
00453         default:
00454           exit (-1);
00455           break;
00456       }
00457     } else
00458     {
00459       j = zig_zag_scan[i];
00460     }
00461 
00462     qval = &bp[j];
00463 
00464     i++;
00465 
00466     if (comp >= 6)
00467       QP = mmax (1, mmin (31, (bquant_tab[bquant] * quant) >> 2));
00468     else
00469       QP = quant;
00470     if ((advanced_intra_coding) && (Mode == MODE_INTRA || Mode == MODE_INTRA_Q))
00471     {
00472       /* Advanced intra coding dequantization */
00473       *qval = (DCTcoef.sign ? -(QP * (2 * DCTcoef.val)) : QP * (2 * DCTcoef.val));
00474     } else
00475     {
00476 
00477       if ((QP % 2) == 1)
00478         *qval = ((DCTcoef.sign) ? -(QP * (2 * (DCTcoef.val) + 1)) :
00479                  QP * (2 * (DCTcoef.val) + 1));
00480       else
00481         *qval = ((DCTcoef.sign) ? -(QP * (2 * (DCTcoef.val) + 1) - 1) :
00482                  QP * (2 * (DCTcoef.val) + 1) - 1);
00483     }
00484   }
00485   return;
00486 }
00487 
00488 /*********************************************************************
00489  *
00490  *        Name:        vlc_word_decode
00491  *
00492  *      Description:    Fills Decoder FIFO after a fixed word length
00493  *        string has been detected.
00494  *
00495  *      Input:        Symbol to be decoded, last data flag.
00496  *
00497  *      Returns:        Decoded Symbol via the structure DCTcoeff.
00498  *
00499  *      Side Effects:   Updates last flag.
00500  *
00501  *      Author:        Wayne Ellis <ellis_w_wayne@bt-web.bt.co.uk>
00502  *
00503  *********************************************************************/
00504 
00505 RunCoef vlc_word_decode (int symbol_word, int *last)
00506 {
00507   int sign_index;
00508   RunCoef DCTcoef;
00509 
00510   *last = (symbol_word >> 12) & 01;
00511 
00512   DCTcoef.run = (symbol_word >> 4) & 255;
00513 
00514   DCTcoef.val = (symbol_word) & 15;
00515 
00516   sign_index = decode_a_symbol (cumf_SIGN);
00517 
00518   DCTcoef.sign = signtab[sign_index];
00519 
00520   return (DCTcoef);
00521 }
00522 
00523 /*********************************************************************
00524  *
00525  *        Name:        Decode_Escape_Char
00526  *
00527  *      Description:    Decodes all components for a Symbol when an
00528  *        ESCAPE character has been detected.
00529  *
00530  *      Input:        Picture Type and last data flag.
00531  *
00532  *      Returns:        Decoded Symbol via the structure DCTcoeff.
00533  *
00534  *      Side Effects:   Modifies last data flag.
00535  *
00536  *      Author:        Wayne Ellis <ellis_w_wayne@bt-web.bt.co.uk>
00537  *
00538  *********************************************************************/
00539 
00540 RunCoef Decode_Escape_Char (int intra, int *last)
00541 {
00542   int last_index, run, run_index, level, level_index;
00543   RunCoef DCTcoef;
00544 
00545   if (intra)
00546   {
00547     last_index = decode_a_symbol (cumf_LAST_intra);
00548     *last = last_intratab[last_index];
00549   } else
00550   {
00551     last_index = decode_a_symbol (cumf_LAST);
00552     *last = lasttab[last_index];
00553   }
00554 
00555   if (intra)
00556     run_index = decode_a_symbol (cumf_RUN_intra);
00557   else
00558     run_index = decode_a_symbol (cumf_RUN);
00559 
00560   run = runtab[run_index];
00561 
00562   /* $if (mrun) run|=64;$ */
00563 
00564   DCTcoef.run = run;
00565 
00566   if (intra)
00567     level_index = decode_a_symbol (cumf_LEVEL_intra);
00568   else
00569     level_index = decode_a_symbol (cumf_LEVEL);
00570 
00571   level = leveltab[level_index];
00572 
00573   if (level > 128)
00574     level -= 256;
00575 
00576   if (level < 0)
00577   {
00578     DCTcoef.sign = 1;
00579     DCTcoef.val = abs (level);
00580   } else
00581   {
00582     DCTcoef.sign = 0;
00583     DCTcoef.val = level;
00584   }
00585 
00586   return (DCTcoef);
00587 
00588 }
00589 /*********************************************************************
00590  *
00591  *        Name:        DecodeTCoef
00592  *
00593  *      Description:    Decodes a.c DCT Coefficients using the
00594  *        relevant arithmetic decoding model.
00595  *
00596  *      Input:        DCT Coeff count and Picture Type.
00597  *
00598  *      Returns:        Index to LUT
00599  *
00600  *      Side Effects:   None
00601  *
00602  *      Author:        Wayne Ellis <ellis_w_wayne@bt-web.bt.co.uk>
00603  *
00604  *********************************************************************/
00605 
00606 int DecodeTCoef (int position, int intra)
00607 {
00608   int index;
00609 
00610   switch (position)
00611   {
00612     case 1:
00613       {
00614         if (intra)
00615           index = decode_a_symbol (cumf_TCOEF1_intra);
00616         else
00617           index = decode_a_symbol (cumf_TCOEF1);
00618         break;
00619       }
00620     case 2:
00621       {
00622         if (intra)
00623           index = decode_a_symbol (cumf_TCOEF2_intra);
00624         else
00625           index = decode_a_symbol (cumf_TCOEF2);
00626         break;
00627       }
00628     case 3:
00629       {
00630         if (intra)
00631           index = decode_a_symbol (cumf_TCOEF3_intra);
00632         else
00633           index = decode_a_symbol (cumf_TCOEF3);
00634         break;
00635       }
00636     default:
00637       {
00638         if (intra)
00639           index = decode_a_symbol (cumf_TCOEFr_intra);
00640         else
00641           index = decode_a_symbol (cumf_TCOEFr);
00642         break;
00643       }
00644   }
00645 
00646   return (index);
00647 }

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