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

📄 countbit.c

📁 H.263的压缩算法
💻 C
📖 第 1 页 / 共 5 页
字号:
int CodeTCoef (int mod_index, int position, int intra){  int length;  switch (position)  {    case 1:      {        if (intra)          length = AR_Encode (mod_index, cumf_TCOEF1_intra);        else          length = AR_Encode (mod_index, cumf_TCOEF1);        break;      }    case 2:      {        if (intra)          length = AR_Encode (mod_index, cumf_TCOEF2_intra);        else          length = AR_Encode (mod_index, cumf_TCOEF2);        break;      }    case 3:      {        if (intra)          length = AR_Encode (mod_index, cumf_TCOEF3_intra);        else          length = AR_Encode (mod_index, cumf_TCOEF3);        break;      }    default:      {        if (intra)          length = AR_Encode (mod_index, cumf_TCOEFr_intra);        else          length = AR_Encode (mod_index, cumf_TCOEFr);        break;      }  }  return length;}/********************************************************************** * *	Name:        FindCBP *	Description:	Finds the CBP for a macroblock * *	Input:        qcoeff and mode * *	Returns:	CBP *	Side effects: * *	Date: 940829	Author:	Karl.Lillevold@nta.no * ***********************************************************************/int FindCBP (int *qcoeff, int Mode, int ncoeffs){  int i, j;  int CBP = 0;  int intra = ((Mode == MODE_INTRA || Mode == MODE_INTRA_Q) && (!advanced_intra_coding));  /* Set CBP for this Macroblock */  for (i = 0; i < 6; i++)  {    for (j = i * ncoeffs + intra; j < (i + 1) * ncoeffs; j++)    {      if (qcoeff[j])      {        if (i == 0)        {          CBP |= 32;        } else if (i == 1)        {          CBP |= 16;        } else if (i == 2)        {          CBP |= 8;        } else if (i == 3)        {          CBP |= 4;        } else if (i == 4)        {          CBP |= 2;        } else if (i == 5)        {          CBP |= 1;        } else        {          fprintf (stderr, "Error in CBP assignment\n");          exit (-1);        }        break;      }    }  }  return CBP;}/********************************************************************** * *	Name:           CountBitsVectors *	Description:	counts bits used for MVs and writes to bitstream * *	Input:          MV struct, bits struct, picture struct, *                      position, mode, annex O prediction type, * *	Returns: *	Side effects: * *	Date: 940111	Author:	??? *            970831    Added support for annex O, true b pictures, *                      modified by mikeg@ee.ubc.ca * ***********************************************************************/void CountBitsVectors (MotionVector * MV[7][MBR + 1][MBC + 2], Bits * bits,                        int x, int y, int Mode, int newgob, Pict * pic,                        int scalability_prediction_type){  int y_vec, x_vec;  int pmv0, pmv1;  int start, stop, block, blockb;  int y_vecb, x_vecb;  int pmv0b, pmv1b;  x++;  y++;  if (PCT_B != pic->picture_coding_type)  {    if (Mode == MODE_INTER4V || Mode == MODE_INTER4V_Q)    {      start = 1;      stop = 4;    } else    {      start = 0;      stop = 0;    }    for (block = start; block <= stop; block++)    {      FindPMV (MV, x, y, &pmv0, &pmv1, block, newgob, 1);      x_vec = (2 * MV[block][y][x]->x + MV[block][y][x]->x_half) - pmv0;      y_vec = (2 * MV[block][y][x]->y + MV[block][y][x]->y_half) - pmv1;      /* When PLUSPTYPE is signalled and Annex D is used, *       * Reversible motion vector codes are used          */      if (EPTYPE && long_vectors)      {        if (trace)        {          fprintf (tf, "Vectors:\n");        }        bits->vec += put_rvlc(x_vec);        bits->vec += put_rvlc(y_vec);        if (trace)        {          fprintf (tf, "(x,y) = (%d,%d) - ",                   (2 * MV[block][y][x]->x + MV[block][y][x]->x_half),                   (2 * MV[block][y][x]->y + MV[block][y][x]->y_half));          fprintf (tf, "(Px,Py) = (%d,%d)\n", pmv0, pmv1);          fprintf (tf, "(x_diff,y_diff) = (%d,%d)\n", x_vec, y_vec);        }        /* prevent start code emulation */        if (x_vec == 1 && y_vec == 1)        {          if (trace)          {            fprintf (tf, "Prevent Start Code Emulation bit:\n");          }          putbits (1,1);        }      }      else      {        if (!long_vectors)        {          if (x_vec < -32)            x_vec += 64;          else if (x_vec > 31)            x_vec -= 64;          if (y_vec < -32)            y_vec += 64;          else if (y_vec > 31)            y_vec -= 64;        } else        {          if (pmv0 < -31 && x_vec < -63)            x_vec += 64;          else if (pmv0 > 32 && x_vec > 63)            x_vec -= 64;            if (pmv1 < -31 && y_vec < -63)            y_vec += 64;          else if (pmv1 > 32 && y_vec > 63)            y_vec -= 64;        }        if (trace)        {          fprintf (tf, "Vectors:\n");        }        if (x_vec < 0)          x_vec += 64;        if (y_vec < 0)          y_vec += 64;        bits->vec += put_mv (x_vec);        bits->vec += put_mv (y_vec);        if (trace)        {          if (x_vec > 31)            x_vec -= 64;          if (y_vec > 31)            y_vec -= 64;          fprintf (tf, "(x,y) = (%d,%d) - ",                   (2 * MV[block][y][x]->x + MV[block][y][x]->x_half),                   (2 * MV[block][y][x]->y + MV[block][y][x]->y_half));          fprintf (tf, "(Px,Py) = (%d,%d)\n", pmv0, pmv1);          fprintf (tf, "(x_diff,y_diff) = (%d,%d)\n", x_vec, y_vec);        }      }    }  }   else if ((PCT_B == pic->picture_coding_type) && (MODE_INTER == Mode))  {    switch (scalability_prediction_type)    {      case B_DIRECT_PREDICTION:        break;      case B_FORWARD_PREDICTION:        block = 0;        FindPMV (MV, x, y, &pmv0, &pmv1, block, newgob, 1);        x_vec = (2 * MV[block][y][x]->x + MV[block][y][x]->x_half) - pmv0;        y_vec = (2 * MV[block][y][x]->y + MV[block][y][x]->y_half) - pmv1;       /* When PLUSPTYPE is signalled and Annex D is used, *         * Reversible motion vector codes are used          */        if (EPTYPE && long_vectors)        {          if (trace)          {            fprintf (tf, "Vectors:\n");          }          bits->vec += put_rvlc(x_vec);          bits->vec += put_rvlc(y_vec);          if (trace)          {             fprintf (tf, "(x,y) = (%d,%d) - ",                     (2 * MV[block][y][x]->x + MV[block][y][x]->x_half),                     (2 * MV[block][y][x]->y + MV[block][y][x]->y_half));            fprintf (tf, "(Px,Py) = (%d,%d)\n", pmv0, pmv1);            fprintf (tf, "(x_diff,y_diff) = (%d,%d)\n", x_vec, y_vec);          }          /* prevent start code emulation */          if (x_vec == 1 && y_vec == 1)          {            if (trace)            {              fprintf (tf, "Prevent Start Code Emulation bit:\n");            }            putbits (1,1);          }        }        else        {                  if (!long_vectors)          {            if (x_vec < -32)              x_vec += 64;            else if (x_vec > 31)              x_vec -= 64;            if (y_vec < -32)              y_vec += 64;            else if (y_vec > 31)              y_vec -= 64;          } else          {            if (pmv0 < -31 && x_vec < -63)              x_vec += 64;            else if (pmv0 > 32 && x_vec > 63)              x_vec -= 64;             if (pmv1 < -31 && y_vec < -63)              y_vec += 64;            else if (pmv1 > 32 && y_vec > 63)              y_vec -= 64;          }          if (trace)          {            fprintf (tf, "Vectors:\n");          }          if (x_vec < 0)            x_vec += 64;          if (y_vec < 0)            y_vec += 64;          bits->vec += put_mv (x_vec);          bits->vec += put_mv (y_vec);          if (trace)          {            if (x_vec > 31)              x_vec -= 64;            if (y_vec > 31)              y_vec -= 64;            fprintf (tf, "Forward (x,y) = (%d,%d) - ",                     (2 * MV[block][y][x]->x + MV[block][y][x]->x_half),                     (2 * MV[block][y][x]->y + MV[block][y][x]->y_half));            fprintf (tf, "(Px,Py) = (%d,%d)\n", pmv0, pmv1);            fprintf (tf, "(x_diff,y_diff) = (%d,%d)\n", x_vec, y_vec);          }        }        break;      case B_BACKWARD_PREDICTION:        blockb = 5;        FindPMV (MV, x, y, &pmv0b, &pmv1b, blockb, newgob, 1);        x_vecb = (2 * MV[blockb][y][x]->x + MV[blockb][y][x]->x_half) - pmv0b;        y_vecb = (2 * MV[blockb][y][x]->y + MV[blockb][y][x]->y_half) - pmv1b;        /* When PLUSPTYPE is signalled and Annex D is used, *         * Reversible motion vector codes are used          */        if (EPTYPE && long_vectors)        {          if (trace)          {            fprintf (tf, "Vectors:\n");          }          bits->vec += put_rvlc(x_vecb);          bits->vec += put_rvlc(y_vecb);          if (trace)          {            fprintf (tf, "(x,y) = (%d,%d) - ",                     (2 * MV[blockb][y][x]->x + MV[blockb][y][x]->x_half),                     (2 * MV[blockb][y][x]->y + MV[blockb][y][x]->y_half));            fprintf (tf, "(Px,Py) = (%d,%d)\n", pmv0b, pmv1b);            fprintf (tf, "(x_diff,y_diff) = (%d,%d)\n", x_vecb, y_vecb);          }          /* prevent start code emulation */          if (x_vecb == 1 && y_vecb == 1)          {            if (trace)            {              fprintf (tf, "Prevent Start Code Emulation bit:\n");            }            putbits (1,1);          }        }        else        {          if (!long_vectors)          {            if (x_vecb < -32)              x_vecb += 64;            else if (x_vecb > 31)              x_vecb -= 64;            if (y_vecb < -32)              y_vecb += 64;            else if (y_vecb > 31)              y_vecb -= 64;          } else          {            if (pmv0b < -31 && x_vecb < -63)              x_vecb += 64;            else if (pmv0b > 32 && x_vecb > 63)              x_vecb -= 64;            if (pmv1b < -31 && y_vecb < -63)              y_vecb += 64;            else if (pmv1b > 32 && y_vecb > 63)              y_vecb -= 64;          }           if (trace)          {            fprintf (tf, "Vectors:\n");          }          if (x_vecb < 0)            x_vecb += 64;          if (y_vecb < 0)            y_vecb += 64;          bits->vec += put_mv (x_vecb);          bits->vec += put_mv (y_vecb);            if (trace)          {            if (x_vecb > 31)              x_vecb -= 64;            if (y_vecb > 31)              y_vecb -= 64;            fprintf (tf, "Backward (x,y) = (%d,%d) - ",                     (2 * MV[blockb][y][x]->x + MV[blockb][y][x]->x_half),                     (2 * MV[blockb][y][x]->y + MV[blockb][y][x]->y_half));            fprintf (tf, "(Px,Py) = (%d,%d)\n", pmv0b, pmv1b);            fprintf (tf, "(x_diff,y_diff) = (%d,%d)\n", x_vecb, y_vecb);          }        }        break;      case B_BIDIRECTIONAL_PREDICTION:        block = 0;        FindPMV (MV, x, y, &pmv0, &pmv1, block, newgob, 1);        x_vec = (2 * MV[block][y][x]->x + MV[block][y][x]->x_half) - pmv0;        y_vec = (2 * MV[block][y][x]->y + MV[block][y][x]->y_half) - pmv1;        blockb = 5;        FindPMV (MV, x, y, &pmv0b, &pmv1b, blockb, newgob, 1);        x_vecb = (2 * MV[blockb][y][x]->x + MV[blockb][y][x]->x_half) - pmv0b;        y_vecb = (2 * MV[blockb][y][x]->y + MV[blockb][y][x]->y_half) - pmv1b;        /* When PLUSPTYPE is signalled and Annex D is used, *         * Reversible motion vector codes are used          */        if (EPTYPE && long_vectors)        {          if (trace)          {            fprintf (tf, "Vectors:\n");          }          bits->vec += put_rvlc(x_vec);          bits->vec += put_rvlc(y_vec);          /* prevent start code emulation */          if (x_vec == 1 && y_vec == 1)          {            if (trace)            {              fprintf (tf, "Prevent Start Code Emulation bit:\n");            }            putbits (1,1);          }          /* Backward Vectors */          bits->vec += put_rvlc(x_vecb);          bits->vec += put_rvlc(y_vecb);          /* prevent start code emulation */          if (x_vecb == 1 && y_vecb == 1)          {            if (trace)            {              fprintf (tf, "Prevent Start Code Emulation bit:\n");            }            putbits (1,1);          }          if (trace)          {            fprintf (tf, "Forward (x,y) = (%d,%d) - ",                     (2 * MV[block][y][x]->x + MV[block][y][x]->x_half),                     (2 * MV[block][y][x]->y + MV[block][y][x]->y_half));            fprintf (tf, "(Px,Py) = (%d,%d)\n", pmv0, pmv1);            fprintf (tf, "(x_diff,y_diff) = (%d,%d)\n", x_vec, y_vec);            fprintf (tf, "Backward (x,y) = (%d,%d) - ",                     (2 * MV[blockb][y][x]->x + MV[blockb][y][x]->x_half),                     (2 * MV[blockb][y][x]->y + MV[blockb][y][x]->y_half));            fprintf (tf, "(Px,Py) = (%d,%d)\n", pmv0b, pmv1b);            fprintf (tf, "(x_diff,y_diff) = (%d,%d)\n", x_vecb, y_vecb);          }        }        else        {          if (!long_vectors)          {

⌨️ 快捷键说明

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