GetBlk.cpp
上传用户:panstart
上传日期:2022-04-12
资源大小:199k
文件大小:10k
源码类别:

IP电话/视频会议

开发平台:

C++ Builder

  1. ////////////////////////////////////////////////////////////////////////////
  2. //
  3. //
  4. //    Project     : VideoNet version 1.1.
  5. //    Description : Peer to Peer Video Conferencing over the LAN.
  6. //   Author      : Nagareshwar Y Talekar ( nsry2002@yahoo.co.in)
  7. //    Date        : 15-6-2004.
  8. //
  9. //    This is the modified version of tmndecode (H.263 decoder) 
  10. //    written by Karl & Robert.It was in ANSI C. I have converted into C++
  11. //    so that it can be integrated into any windows application. I have 
  12. //    removed some of the files which had display and file storing 
  13. //    functions.I have removed the unnecessary code and also added some
  14. //    new files..
  15. //   Original library dealt with files. Input & Output , both were files.
  16. //    I have done some major changes so that it can be used for real time 
  17. //    decoding process. Now one can use this library for decoding H263 frames. 
  18. //
  19. //
  20. //    File description : 
  21. //    Name    : GetBlk.cpp
  22. //
  23. /////////////////////////////////////////////////////////////////////////////
  24. /************************************************************************
  25.  *
  26.  *  getblk.c, DCT block decoding for tmndecode (H.263 decoder)
  27.  *  Copyright (C) 1996  Telenor R&D, Norway
  28.  *        Karl Olav Lillevold <Karl.Lillevold@nta.no>
  29.  *
  30.  *  This program is free software; you can redistribute it and/or modify
  31.  *  it under the terms of the GNU General Public License as published by
  32.  *  the Free Software Foundation; either version 2 of the License, or
  33.  *  (at your option) any later version.
  34.  *
  35.  *  This program is distributed in the hope that it will be useful,
  36.  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
  37.  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  38.  *  GNU General Public License for more details.
  39.  *
  40.  *  You should have received a copy of the GNU General Public License
  41.  *  along with this program; if not, write to the Free Software
  42.  *  Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  43.  *
  44.  *  Karl Olav Lillevold               <Karl.Lillevold@nta.no>
  45.  *  Telenor Research and Development
  46.  *  P.O.Box 83                        tel.:   +47 63 84 84 00
  47.  *  N-2007 Kjeller, Norway            fax.:   +47 63 81 00 76
  48.  *
  49.  *  Robert Danielsen                  e-mail: Robert.Danielsen@nta.no
  50.  *  Telenor Research and Development  www:    http://www.nta.no/brukere/DVC/
  51.  *  P.O.Box 83                        tel.:   +47 63 84 84 00
  52.  *  N-2007 Kjeller, Norway            fax.:   +47 63 81 00 76
  53.  *  
  54.  ************************************************************************/
  55. /*
  56.  * based on mpeg2decode, (C) 1994, MPEG Software Simulation Group
  57.  * and mpeg2play, (C) 1994 Stefan Eckart
  58.  *                         <stefan@lis.e-technik.tu-muenchen.de>
  59.  *
  60.  */
  61. #include "GetBlk.h"
  62. void getblock(int comp,int mode)
  63. {
  64.   int val, i, j, sign;
  65.   unsigned int code;
  66.   VLCtabI *tab;
  67.   short *bp;
  68.   int run, last, level, QP;
  69.   short *qval;
  70.   bp = ld->block[comp];   
  71.   /* decode AC coefficients */
  72.   for (i=(mode==0); ; i++) {
  73.     code = showbits(12);
  74.     if (code>=512)
  75.       tab = &DCT3Dtab0[(code>>5)-16];
  76.     else if (code>=128)
  77.       tab = &DCT3Dtab1[(code>>2)-32];
  78.     else if (code>=8)
  79.       tab = &DCT3Dtab2[(code>>0)-8];
  80.     else {
  81.       
  82.   if (!quiet)
  83.         fputs("invalid Huffman code in getblock()n",dlog);
  84.       fault = 1;
  85.       return;
  86.     }
  87.     flushbits(tab->len);
  88.     run = (tab->val >> 4) & 255;
  89.     level = tab->val & 15;
  90.     last = (tab->val >> 12) & 1;
  91.     if (trace) {
  92.       fputs(" (",dlog);
  93.       printbits(code,12,tab->len);
  94.     }
  95.    if (tab->val==ESCAPE) { /* escape */
  96.       if (trace) {
  97.         putchar(' ');
  98.         printbits(showbits(1),1,1);
  99.       }
  100.       last = getbits1();
  101.       if (trace) {
  102.         putchar(' ');
  103.         printbits(showbits(6),6,6);
  104.       }
  105.       i += run = getbits(6);
  106.       if (trace) {
  107.         putchar(' ');
  108.         printbits(showbits(8),8,8);
  109.       }
  110.       level = getbits(8);
  111.       if ((sign = (level>=128)))
  112.         val = 256 - level;
  113.       else 
  114.         val = level;
  115.     }
  116.     else {
  117.       i+= run;
  118.       val = level;
  119.       sign = getbits(1);
  120.       //if (trace)
  121.       //  printf("%d",sign);
  122.     }
  123.     if (i >= 64)
  124.     {
  125.       if (!quiet)
  126.         fputs("DCT coeff index (i) out of boundsn",dlog);
  127.       fault = 1;
  128.       return;
  129.     }
  130.     //if (trace)
  131.      // fputs("): %d/%dn",run,sign ? -val : val);
  132.     j = zig_zag_scan[i];
  133.     qval = &bp[j];
  134.     if (comp >= 6)
  135.       QP = mmax (1, mmin( 31, ( bquant_tab[bquant] * quant ) >> 2 ));
  136.     else 
  137.       QP = quant;
  138.       
  139.     /* TMN3 dequantization */
  140.     if ((QP % 2) == 1)
  141.       *qval = ( sign ? -(QP * (2* val+1))  : QP * (2* val+1) );
  142.     else
  143.       *qval = ( sign ? -(QP * (2* val+1)-1): QP * (2* val+1)-1 );
  144.     if (last) { /* That's it */
  145.       if (trace)
  146.         fputs("lastn",dlog);
  147.       return;
  148.     }
  149.   }
  150. }
  151. /*********************************************************************
  152.  *
  153.  *        Name:        get_sac_block
  154.  *
  155.  * Description: Decodes blocks of Arithmetic Encoded DCT Coeffs.
  156.  *        and performs Run Length Decoding and Coefficient        
  157.  *        Dequantisation.
  158.  *
  159.  * Input:        Picture block type and number.
  160.  *
  161.  * Returns:    Nothing.
  162.  *
  163.  * Side Effects:
  164.  *
  165.  * Author:        Wayne Ellis <ellis_w_wayne@bt-web.bt.co.uk>
  166.  *
  167.  *********************************************************************/
  168. void get_sac_block(int comp, int ptype)
  169. {
  170.   int position=0;
  171.   int TCOEF_index, symbol_word;
  172.   int last=0, QP, i, j;
  173.   short *qval, *bp;
  174.   RunCoef DCTcoef;
  175.   bp = ld->block[comp];
  176.   i = (ptype==0);
  177.   while (!last) { /* while there are DCT coefficients remaining */
  178.     position++; /* coefficient counter relates to Coeff. model */
  179.     TCOEF_index = DecodeTCoef(position, !ptype);
  180.     if (TCOEF_index == ESCAPE_INDEX) {        /* ESCAPE code encountered */
  181.       DCTcoef = Decode_Escape_Char(!ptype, &last);
  182.       if (trace)
  183.         fputs("ESC: ",dlog);
  184.     }
  185.     else {
  186.       symbol_word = tcoeftab[TCOEF_index];
  187.       DCTcoef = vlc_word_decode(symbol_word,&last);
  188.     }
  189.     /*if (trace) {
  190.       printf("val: %d, run: %d, sign: %d, last: %dn", 
  191.              DCTcoef.val, DCTcoef.run, DCTcoef.sign, last);
  192.     }
  193. */
  194.     i += DCTcoef.run;
  195.     j = zig_zag_scan[i];
  196.     qval = &bp[j];
  197.     i++;
  198.     if (comp >= 6)
  199.       QP = mmax (1, mmin( 31, ( bquant_tab[bquant] * quant ) >> 2 ));
  200.     else 
  201.       QP = quant;
  202.     if ((QP % 2) == 1)
  203.       *qval = ( (DCTcoef.sign) ? -(QP * (2* (DCTcoef.val)+1))  : 
  204.         QP * (2* (DCTcoef.val)+1) );
  205.     else
  206.       *qval = ( (DCTcoef.sign) ? -(QP * (2* (DCTcoef.val)+1)-1): 
  207.         QP * (2* (DCTcoef.val)+1)-1 );
  208.         
  209.   }
  210.   return;
  211. }
  212. /*********************************************************************
  213.  *
  214.  *        Name:        vlc_word_decode
  215.  *
  216.  * Description: Fills Decoder FIFO after a fixed word length
  217.  *        string has been detected.
  218.  *
  219.  * Input:        Symbol to be decoded, last data flag.
  220.  *
  221.  * Returns: Decoded Symbol via the structure DCTcoeff.
  222.  *
  223.  * Side Effects: Updates last flag.
  224.  *
  225.  * Author:        Wayne Ellis <ellis_w_wayne@bt-web.bt.co.uk>
  226.  *
  227.  *********************************************************************/
  228. RunCoef vlc_word_decode(int symbol_word, int *last)
  229. {
  230.   int sign_index;
  231.   RunCoef DCTcoef;
  232.   *last = (symbol_word >> 12) & 01;
  233.  
  234.   DCTcoef.run = (symbol_word >> 4) & 255; 
  235.   DCTcoef.val = (symbol_word) & 15;
  236.   sign_index = decode_a_symbol(cumf_SIGN);
  237.   DCTcoef.sign = signtab[sign_index];
  238.         
  239.   return (DCTcoef);
  240. /*********************************************************************
  241.  *
  242.  *        Name:        Decode_Escape_Char
  243.  *
  244.  * Description: Decodes all components for a Symbol when an 
  245.  *        ESCAPE character has been detected.
  246.  *
  247.  * Input:        Picture Type and last data flag.
  248.  *
  249.  * Returns: Decoded Symbol via the structure DCTcoeff.
  250.  *
  251.  * Side Effects: Modifies last data flag.
  252.  *
  253.  * Author:        Wayne Ellis <ellis_w_wayne@bt-web.bt.co.uk>
  254.  *
  255.  *********************************************************************/
  256. RunCoef Decode_Escape_Char(int intra, int *last)
  257. {
  258.   int last_index, run, run_index, level, level_index;
  259.   RunCoef DCTcoef;
  260.   if (intra) {
  261.     last_index = decode_a_symbol(cumf_LAST_intra);
  262.     *last = last_intratab[last_index];
  263.   }
  264.   else {
  265.     last_index = decode_a_symbol(cumf_LAST);
  266.     *last = lasttab[last_index];
  267.   }
  268.   if (intra) 
  269.     run_index = decode_a_symbol(cumf_RUN_intra);
  270.   else
  271.     run_index = decode_a_symbol(cumf_RUN);
  272.   run = runtab[run_index];
  273.   /*$if (mrun) run|=64;$*/
  274.   DCTcoef.run = run;
  275.   if (intra)
  276.     level_index = decode_a_symbol(cumf_LEVEL_intra);
  277.   else
  278.     level_index = decode_a_symbol(cumf_LEVEL);
  279.   //if (trace)
  280.   //  printf("level_idx: %d ",level_index);
  281.   level = leveltab[level_index];
  282.   if (level >128) 
  283.     level -=256;
  284.   if (level < 0) {
  285.     DCTcoef.sign = 1;
  286.     DCTcoef.val = abs(level);
  287.   }
  288.   else {
  289.     DCTcoef.sign = 0;
  290.     DCTcoef.val = level;
  291.   }
  292.   return (DCTcoef);
  293.         
  294. }
  295. /*********************************************************************
  296.  *
  297.  *        Name:        DecodeTCoef
  298.  *
  299.  * Description: Decodes a.c DCT Coefficients using the        
  300.  *        relevant arithmetic decoding model.
  301.  *
  302.  * Input:        DCT Coeff count and Picture Type.
  303.  *
  304.  * Returns: Index to LUT
  305.  *
  306.  * Side Effects: None
  307.  *
  308.  * Author:        Wayne Ellis <ellis_w_wayne@bt-web.bt.co.uk>
  309.  *
  310.  *********************************************************************/
  311. int DecodeTCoef(int position, int intra)
  312. {
  313.   int index;
  314.   switch (position) {
  315.   case 1:
  316.     {
  317.       if (intra) 
  318.         index = decode_a_symbol(cumf_TCOEF1_intra);
  319.       else 
  320.         index = decode_a_symbol(cumf_TCOEF1); 
  321.       break; 
  322.     }
  323.   case 2:
  324.     {
  325.       if (intra) 
  326.         index = decode_a_symbol(cumf_TCOEF2_intra);
  327.       else
  328.         index = decode_a_symbol(cumf_TCOEF2);
  329.       break; 
  330.     }
  331.   case 3:
  332.     {
  333.       if (intra) 
  334.         index = decode_a_symbol(cumf_TCOEF3_intra);
  335.       else
  336.         index = decode_a_symbol(cumf_TCOEF3);
  337.       break; 
  338.     }
  339.   default: 
  340.     {
  341.       if (intra) 
  342.         index = decode_a_symbol(cumf_TCOEFr_intra);
  343.       else
  344.         index = decode_a_symbol(cumf_TCOEFr);
  345.       break; 
  346.     }
  347.   }
  348.   return (index);
  349. }