⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 getblk.c

📁 H.263+(VC++商业源代码)
💻 C
字号:
#include <stdio.h>

#include "config.h"
#include "global.h"


#define INDICES
#include "indices.h"

#define SACTABLES
#include "sactbls.h"


typedef struct {
  char run, level, len;
} DCTtab;
typedef struct {
  int val, len;
} VLCtabI;
typedef struct {
  int val, run, sign;
} RunCoef;

/* local prototypes */
RunCoef vlc_word_decode _ANSI_ARGS_((int symbol_word, int *last)); 
RunCoef Decode_Escape_Char _ANSI_ARGS_((int intra, int *last));
int DecodeTCoef _ANSI_ARGS_((int position, int intra));



extern VLCtabI DCT3Dtab0[],DCT3Dtab1[],DCT3Dtab2[];



void getblock(comp,mode)
int comp;
int mode;
{
  int val, i, j, sign;
  unsigned int code;
  VLCtabI *tab;
  short *bp;
  int off = 0 ;
  int run, last, level, QP;
  short *qval;
  
  if(comp < 0) comp = 0 ;
  if(comp >= 12 ) comp = 11 ;

  bp = ld->block[comp];   

  /* decode AC coefficients */
  for (i=(mode==0); ; i++) {
    code = showbits(12);
    if (code>=512)
	{
	  off = (code>>5)-16 ;
	  if( off < 0 ) off = 0 ;
	  if( off >= 112 ) off=111 ;
      tab = &DCT3Dtab0[off];
	}
    else if (code>=128)
	{
      off = (code>>2)-32 ;
	  if( off < 0 ) off = 0 ;
	  if( off >= 112 ) off=111 ;
      tab = &DCT3Dtab1[off];
	}
    else if (code>=8)
	{
   	  off = (code>>0)-8 ;
	  if( off < 0 ) off = 0 ;
	  if( off >= 112 ) off=111 ;
	  tab = &DCT3Dtab2[off];
	}
   else {
      tab = &DCT3Dtab2[0];
    }

    flushbits(tab->len);

    run = (tab->val >> 4) & 255;
    level = tab->val & 15;
    last = (tab->val >> 12) & 1;


   if (tab->val==ESCAPE) { /* escape */
      last = getbits1();
      i += run = getbits(6);
      level = getbits(8);

      if ((sign = (level>=128)))
        val = 256 - level;
      else 
	val = level;
    }
    else {
      i+= run;
      val = level;
      sign = getbits(1);
    }

    if (i >= 64)
    {
		i = 63 ;
    }

 

    j = zig_zag_scan[i];
    qval = &bp[j];
	if( bquant <0 ) bquant = 0 ;
	if( bquant >3 ) bquant = 3 ;

    if (comp >= 6)
      QP = mmax (1, mmin( 31, ( bquant_tab[bquant] * quant ) >> 2 ));
    else 
      QP = quant;
      
    /* TMN3 dequantization */
    if ((QP % 2) == 1)
      *qval = ( sign ? -(QP * (2* val+1))  : QP * (2* val+1) );
    else
      *qval = ( sign ? -(QP * (2* val+1)-1): QP * (2* val+1)-1 );

    if (last) { /* That's it */
      return;
    }
  }
}



/*********************************************************************
 *
 * 	Name:		get_sac_block
 *
 *	Description:	Decodes blocks of Arithmetic Encoded DCT Coeffs.
 *			and performs Run Length Decoding and Coefficient       	
 *			Dequantisation.
 *
 *	Input:		Picture block type and number.
 *
 *	Returns:	Nothing.
 *
 *	Side Effects:	
 *
 *********************************************************************/


void get_sac_block(int comp, int ptype)
{
  int position=0;
  int count = 0 ;
  int TCOEF_index, symbol_word;
  int last=0, QP, i, j;
  short *qval, *bp;
  RunCoef DCTcoef;

  if(comp < 0) comp = 0 ;
  if(comp >= 12 ) comp = 11 ;
  bp = ld->block[comp];

  i = (ptype==0);

  while (!last) {
					  /* while there are DCT coefficients remaining */
    count ++ ;
	if( count > 50000 ) 
		break ;
    position++;	/* coefficient counter relates to Coeff. model */
    TCOEF_index = DecodeTCoef(position, !ptype);
	if( TCOEF_index < 0 ) TCOEF_index = 0 ;
	if( TCOEF_index >= 103 ) TCOEF_index = 102 ;


    if (TCOEF_index == ESCAPE_INDEX) { 	/* ESCAPE code encountered */
      DCTcoef = Decode_Escape_Char(!ptype, &last);
    }
    else {
      symbol_word = tcoeftab[TCOEF_index];

      DCTcoef = vlc_word_decode(symbol_word,&last);
    }


    i += DCTcoef.run;
	if( i< 0 ) i = 0 ;
	if( i>=64) i = 63 ;
    j = zig_zag_scan[i];

    qval = &bp[j];

    i++;
	if( bquant <0 ) bquant = 0 ;
	if( bquant >3 ) bquant = 3 ;

    if (comp >= 6)
      QP = mmax (1, mmin( 31, ( bquant_tab[bquant] * quant ) >> 2 ));
    else 
      QP = quant;

    if ((QP % 2) == 1)
      *qval = ( (DCTcoef.sign) ? -(QP * (2* (DCTcoef.val)+1))  : 
		QP * (2* (DCTcoef.val)+1) );
    else
      *qval = ( (DCTcoef.sign) ? -(QP * (2* (DCTcoef.val)+1)-1): 
		QP * (2* (DCTcoef.val)+1)-1 );
	
  }	
  return;
}

/*********************************************************************
 *
 * 	Name:		vlc_word_decode
 *
 *	Description:	Fills Decoder FIFO after a fixed word length
 *			string has been detected.
 *
 *	Input:		Symbol to be decoded, last data flag.
 *
 *	Returns:	Decoded Symbol via the structure DCTcoeff.
 *
 *	Side Effects:	Updates last flag.
 *
 *********************************************************************/

RunCoef vlc_word_decode(int symbol_word, int *last)
{
  int sign_index;
  RunCoef DCTcoef;

  *last = (symbol_word >> 12) & 01;
 
  DCTcoef.run = (symbol_word >> 4) & 255; 

  DCTcoef.val = (symbol_word) & 15;

  sign_index = decode_a_symbol(cumf_SIGN);	
  if(sign_index <0 )sign_index=0;
  if(sign_index>1)sign_index=1;

  DCTcoef.sign = signtab[sign_index];
	
  return (DCTcoef);
} 

/*********************************************************************
 *
 * 	Name:		Decode_Escape_Char
 *
 *	Description:	Decodes all components for a Symbol when an 
 *			ESCAPE character has been detected.
 *
 *	Input:		Picture Type and last data flag.
 *
 *	Returns:	Decoded Symbol via the structure DCTcoeff.
 *
 *	Side Effects:	Modifies last data flag.
 *
 *********************************************************************/

RunCoef Decode_Escape_Char(int intra, int *last)
{
  int last_index, run, run_index, level, level_index;
  RunCoef DCTcoef;

  if (intra) {
    last_index = decode_a_symbol(cumf_LAST_intra);
	if(last_index <0)last_index =0;
	if(last_index >1)last_index =1;
    *last = last_intratab[last_index];
  }
  else {
    last_index = decode_a_symbol(cumf_LAST);
	if(last_index <0)last_index =0;
	if(last_index >1)last_index =1;
    *last = lasttab[last_index];
  }

  if (intra) 
    run_index = decode_a_symbol(cumf_RUN_intra);
  else
    run_index = decode_a_symbol(cumf_RUN);
  if(run_index<0)run_index=0;
  if(run_index>=64)run_index=63;
  run = runtab[run_index];

  /*$if (mrun) run|=64;$*/

  DCTcoef.run = run;

  if (intra)
    level_index = decode_a_symbol(cumf_LEVEL_intra);
  else
    level_index = decode_a_symbol(cumf_LEVEL);
	if(level_index<0)level_index=0;
	if(level_index>=254)level_index=253 ;

  level = leveltab[level_index];

  if (level >128) 
    level -=256;

  if (level < 0) {
    DCTcoef.sign = 1;
    DCTcoef.val = abs(level);
  }

  else {	
    DCTcoef.sign = 0;
    DCTcoef.val = level;
  }

  return (DCTcoef);
	
}
/*********************************************************************
 *
 * 	Name:		DecodeTCoef
 *
 *	Description:	Decodes a.c DCT Coefficients using the 	
 *			relevant arithmetic decoding model.
 *
 *	Input:		DCT Coeff count and Picture Type.	
 *
 *	Returns:	Index to LUT
 *
 *	Side Effects:	None
 *
 *********************************************************************/

int DecodeTCoef(int position, int intra)
{
  int index;

  switch (position) {
  case 1:
    {
      if (intra) 
	index = decode_a_symbol(cumf_TCOEF1_intra);
      else 
	index = decode_a_symbol(cumf_TCOEF1); 
      break; 
    }
  case 2:
    {
      if (intra) 
	index = decode_a_symbol(cumf_TCOEF2_intra);
      else
	index = decode_a_symbol(cumf_TCOEF2);
      break; 
    }
  case 3:
    {
      if (intra) 
	index = decode_a_symbol(cumf_TCOEF3_intra);
      else
	index = decode_a_symbol(cumf_TCOEF3);
      break; 
    }
  default: 
    {
      if (intra) 
	index = decode_a_symbol(cumf_TCOEFr_intra);
      else
	index = decode_a_symbol(cumf_TCOEFr);
      break; 
    }
  }

  return (index);
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -