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

📄 countbit.c

📁 H.263的压缩算法
💻 C
📖 第 1 页 / 共 5 页
字号:
/************************************************************************ * *  countbit.c, bitstream generation for tmn (TMN encoder) * *  Copyright (C) 1997  University of BC, Canada * *  Contacts:  *  Michael Gallant                   <mikeg@ee.ubc.ca> *  Guy Cote                          <guyc@ee.ubc.ca> *  Berna Erol                        <bernae@ee.ubc.ca> * *  UBC Image Processing Laboratory   http://www.ee.ubc.ca/image *  2356 Main Mall                    tel.: +1 604 822 4051 *  Vancouver BC Canada V6T1Z4        fax.: +1 604 822 5949 * *  Copyright (C) 1995, 1996  Telenor R&D, Norway * *  Contacts:  *  Robert Danielsen                  <Robert.Danielsen@nta.no> * *  Telenor Research and Development  http://www.nta.no/brukere/DVC/ *  P.O.Box 83                        tel.:   +47 63 84 84 00 *  N-2007 Kjeller, Norway            fax.:   +47 63 81 00 76 *   ************************************************************************//* Disclaimer of Warranty *  * These software programs are available to the user without any license fee * or royalty on an "as is" basis. The University of British Columbia * disclaims any and all warranties, whether express, implied, or * statuary, including any implied warranties or merchantability or of * fitness for a particular purpose.  In no event shall the * copyright-holder be liable for any incidental, punitive, or * consequential damages of any kind whatsoever arising from the use of * these programs. *  * This disclaimer of warranty extends to the user of these programs and * user's customers, employees, agents, transferees, successors, and * assigns. *  * The University of British Columbia does not represent or warrant that the * programs furnished hereunder are free of infringement of any * third-party patents. *  * Commercial implementations of H.263, including shareware, are subject to * royalty fees to patent holders.  Many of these patents are general * enough such that they are unavoidable regardless of implementation * design. *  */#include"sim.h"#include"sactbls.h"#include"indices.h"#include "putvlc.h"int arith_used = 0;int true_b_length[5][3] = { {0,2,4}, {3,3,5}, {3,3,5}, {5,5,5}, {6,7,9} };int true_b_code[5][3] = { {1,3,1}, {4,5,6}, {2,3,7}, {4,5,1}, {1,1,1} };int ei_length[4][4] = { {1,3,3,3}, {4,7,7,7}, {8,8,8,8}, {8,8,8,8} };int ei_code[4][4] = { {1,1,2,3}, {1,1,2,3}, {1,9,10,11}, {12,13,14,15} };int ep_length[4][3] = { {0,1,3}, {3,3,5}, {5,5,6}, {7,8,9} };int ep_code[4][3] = { {1,1,1}, {2,3,1}, {2,3,1}, {1,1,1} };/********************************************************************** * *	Name:        CountBitsMB *	Description:    counts bits used for MB info * *	Input:	        Mode, COD, CBP, Picture and Bits structures *	Returns: *	Side effects: * *	Date: 941129	Author: <klillevo@mailbox.jf.intel.com> * ***********************************************************************/void CountBitsMB (int Mode, int COD, int CBP, int CBPB, Pict * pic,                   Bits * bits, int scalability_prediction_type){  int cbpy, cbpcm, length;  int true_B_cbp_present = 0;  /* COD */  if (trace)  {    fprintf (tf, "MB-nr: %d\n", pic->MB);    if (pic->picture_coding_type == PCT_INTER ||        pic->picture_coding_type == PCT_IPB   ||        pic->picture_coding_type == PCT_PB)      fprintf (tf, "  COD: %d\n", COD);  }  if (pic->picture_coding_type == PCT_INTER ||      pic->picture_coding_type == PCT_IPB   ||      pic->picture_coding_type == PCT_PB )  {    putbits (1, COD);    bits->COD++;  }  if (COD)    return;                     /* not coded */  /* CBPCM */  cbpcm = Mode | ((CBP & 3) << 4);  if (trace)  {    fprintf (tf, "CBPCM (CBP=%d) (cbpcm=%d): ", CBP, cbpcm);  }  if (pic->picture_coding_type == PCT_INTRA)    length = put_cbpcm_intra (CBP, Mode);  else     length = put_cbpcm_inter (CBP, Mode);  bits->CBPCM += length;  /* INTRA_MODE when advanced intra coding mode is used */  if ((advanced_intra_coding) && (Mode == MODE_INTRA || Mode == MODE_INTRA_Q))  {    if (trace)      fprintf (tf, "INTRA_MODE: ");    if (pic->Intra_Mode)    {      /* length is two bits */      putbits (2, pic->Intra_Mode);      bits->INTRA_MODE += 2;    } else    {      /* length is one bit */      putbits (1, pic->Intra_Mode);      bits->INTRA_MODE++;    }  }  /* MODB & CBPB */  if (pic->PB == PB_FRAMES)  {    switch (pic->MODB)    {      case PBMODE_NORMAL:        putbits (1, 0);        bits->MODB += 1;        break;      case PBMODE_MVDB:        putbits (2, 2);        bits->MODB += 2;        break;      case PBMODE_CBPB_MVDB:        putbits (2, 3);        bits->MODB += 2;        /* CBPB */        putbits (6, CBPB);        bits->CBPB += 6;        break;    }    if (trace)    {      fprintf (tf, "MODB: %d, CBPB: %d\n", pic->MODB, CBPB);    }  }  if (pic->PB == IM_PB_FRAMES)  {    switch (pic->MODB)    {      case PBMODE_BIDIR_PRED:        putbits (1, 0);        bits->MODB += 1;        break;      case PBMODE_CBPB_BIDIR_PRED:        putbits (2, 2);        bits->MODB += 2;        /* CBPB */        putbits (6, CBPB);        bits->CBPB += 6;        break;      case PBMODE_FRW_PRED:        putbits (3, 6);        bits->MODB += 3;        break;      case PBMODE_CBPB_FRW_PRED:        putbits (4, 14);        bits->MODB += 4;        /* CBPB */        putbits (6, CBPB);        bits->CBPB += 6;        break;      case PBMODE_BCKW_PRED:        putbits (5, 30);        bits->MODB += 5;        break;      case PBMODE_CBPB_BCKW_PRED:        putbits (5, 31);        bits->MODB += 5;        /* CBPB */        putbits (6, CBPB);        bits->CBPB += 6;        break;    }    if (trace)    {      fprintf (tf, "MODB: %d, CBPB: %d\n", pic->MODB, CBPB);    }  }  /* CBPY */  cbpy = CBP >> 2;  /* Intra. */  if (Mode == MODE_INTRA || Mode == MODE_INTRA_Q)    cbpy = cbpy ^ 15;  else if (alternative_inter_vlc && ((CBP & 3) == 3))     cbpy = cbpy ^ 15;  if (trace)  {    fprintf (tf, "CBPY (CBP=%d) (cbpy=%d): ", CBP, cbpy);  }  length = put_cbpy (CBP, Mode);  bits->CBPY += length;  /* DQUANT */  if ((Mode == MODE_INTER_Q) || (Mode == MODE_INTRA_Q)     || (Mode == MODE_INTER4V_Q))  {    if (trace)    {      fprintf (tf, "DQUANT: %d\n ",pic->DQUANT);    }    if (!modified_quantization)    {      switch (pic->DQUANT)      {        case -1:          putbits (2, 0);          break;        case -2:          putbits (2, 1);          break;        case 1:          putbits (2, 2);          break;        case 2:          putbits (2, 3);          break;        default:          fprintf (stderr, "Invalid DQUANT\n");          exit (-1);      }      bits->DQUANT += 2;    }    else    {      /* modified quantization mode, dquant take any value between 0 and       * 31 */      if (pic->dquant_size == 2)      {        putbits (2, pic->DQUANT);        bits->DQUANT += 2;      } else      {        /* DQUANT will be coded into 6 bits */        putbits (1, 0);        putbits (5, pic->DQUANT);        bits->DQUANT += 6;      }    }  }  return;}/********************************************************************** * *	Name:         CountBitsScalMB *	Description:  counts bits used for EI, EP, and B MB info * *	Input:	      Mode, COD, CBP, Picture and Bits structures *	Returns: *	Side effects: * *	Date: 970831  Author: Michael Gallant <mikeg@ee.ubc.ca> * ***********************************************************************/void CountBitsScalMB (int Mode, int COD, int CBP, int CBPB, Pict * pic,                      Bits * bits, int scalability_prediction_type,                      int MV_present){  int cbpy, cbpcm, length, code;  int cbp_present = 0;  int param = 0;  /* COD */  if (trace)  {    fprintf (tf, "MB-nr: %d", pic->MB);    fprintf (tf, "  COD: %d\n", COD);  }  putbits (1, COD);  bits->COD++;  if (COD)    return;                     /* not coded */  /* MBTYPE */  if (trace)  {    fprintf (tf, "MBTYPE: ");  }  switch (pic->picture_coding_type)  {    case PCT_B:      if (B_INTRA_PREDICTION != scalability_prediction_type)      {        if (pic->DQUANT)          param = 2;        else          param = ((CBP) ? 1 : 0);      }      else      {        if (pic->DQUANT)          param = 1;        else          param = 0;      }      length = true_b_length[scalability_prediction_type][param];      code = true_b_code[scalability_prediction_type][param];      break;    case PCT_EI:      length = ei_length[scalability_prediction_type<<1+((pic->DQUANT) ? 1:0)][CBP&3];      code = ei_code[scalability_prediction_type<<1+((pic->DQUANT) ? 1:0)][CBP&3];      break;    case PCT_EP:      if (EP_INTRA_PREDICTION != scalability_prediction_type)      {        param += (CBP || MV_present) ? 1 : 0;      }      param += (pic->DQUANT) ? 1 : 0;      if (MV_present)      {        cbp_present = 1;      }      length = ep_length[scalability_prediction_type][param];      code = ep_code[scalability_prediction_type][param];      break;    default:      break;  }  putbits(length, code);  if ( (PCT_EI != pic->picture_coding_type) &&        (MODE_INTRA == Mode || MODE_INTRA_Q == Mode || 0 != CBP ||          ( (PCT_B == pic->picture_coding_type) && (pic->DQUANT) ) ) )  {    cbp_present = 1;  }   /* INTRA_MODE when advanced intra coding mode is used */  if ((advanced_intra_coding) && (Mode == MODE_INTRA || Mode == MODE_INTRA_Q))  {    if (trace)      fprintf (tf, "INTRA_MODE: ");    if (pic->Intra_Mode)    {      /* length is two bits */      putbits (2, pic->Intra_Mode);      bits->INTRA_MODE += 2;    }     else    {      /* length is one bit */      putbits (1, pic->Intra_Mode);      bits->INTRA_MODE++;    }  }  /* Coded Block Patterns. */  if (cbp_present)  {    cbpcm = (CBP & 3);    if (trace)    {      fprintf (tf, "CBPC: ");    }    if (0 == cbpcm)    {      putbits (1, 0);      bits->CBPCM += 1;    }     else if (1 == cbpcm)    {      putbits (2, 2);      bits->CBPCM += 2;    }     else if (2 == cbpcm)    {      putbits (3, 7);      bits->CBPCM += 3;    }     else    {      putbits (3, 6);      bits->CBPCM += 3;    }  }  if (cbp_present || (PCT_EI == pic->picture_coding_type) )  {    cbpy = CBP >> 2;    if (trace)    {      fprintf (tf, "CBPY: ");    }    if ( (MODE_INTRA == Mode || MODE_INTRA_Q == Mode) ||         (PCT_EI == pic->picture_coding_type && EI_UPWARD_PREDICTION == scalability_prediction_type) ||         (PCT_EP == pic->picture_coding_type &&           (EP_UPWARD_PREDICTION == scalability_prediction_type ||            EP_BIDIRECTIONAL_PREDICTION == scalability_prediction_type) ) )    {      length = put_cbpy (CBP, MODE_INTRA);      bits->CBPY += length;    }    else if (alternative_inter_vlc && (3 == cbpcm) )    {      length = put_cbpy (CBP, MODE_INTRA);      bits->CBPY += length;    }    else    {      length = put_cbpy (CBP, MODE_INTER);      bits->CBPY += length;    }  }  /* DQUANT */  if ((Mode == MODE_INTER_Q) || (Mode == MODE_INTRA_Q) || (Mode == MODE_INTER4V_Q))  {    if (trace)    {      fprintf (tf, "DQUANT: %d\n ",pic->DQUANT);    }    if (!modified_quantization)    {      switch (pic->DQUANT)      {        case -1:          putbits (2, 0);          break;        case -2:          putbits (2, 1);          break;        case 1:          putbits (2, 2);          break;        case 2:          putbits (2, 3);          break;        default:          fprintf (stderr, "Invalid DQUANT\n");          exit (-1);      }      bits->DQUANT += 2;    }     else    {      /* modified quantization mode, dquant take any value between 0 and       * 31 */      if (pic->dquant_size == 2)      {        putbits (2, pic->DQUANT);        bits->DQUANT += 2;      }       else      {        /* DQUANT will be coded into 6 bits */        putbits (1, 0);        putbits (5, pic->DQUANT);        bits->DQUANT += 6;      }

⌨️ 快捷键说明

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