CodeOneIntra.cpp
上传用户:szklck
上传日期:2007-01-22
资源大小:925k
文件大小:7k
源码类别:

图形图像处理

开发平台:

Visual C++

  1. #include"stdafx.h"
  2. #include"Global.h"
  3. //帧内编码
  4. PictImage *CodeOneIntra(PictImage *curr, int QP, Bits *bits, Pict *pic)
  5. {
  6.   PictImage *recon;
  7.   MB_Structure *data = (MB_Structure *)malloc(sizeof(MB_Structure));
  8.   int * qcoeff;
  9.   int Mode =3;//  MODE_INTRA;
  10.   int CBP,COD;
  11.   int i,j;
  12.   recon = InitImage(pels*lines);
  13.   ZeroBits(bits);
  14.   
  15.   pic->QUANT = QP;
  16.   bits->header += CountBitsPicture(pic);
  17.   COD = 0; 
  18.   for ( j = 0; j < lines/MB_SIZE; j++) {
  19.     /* 如果允许GOB同步,则输出GOB同步头  */
  20.     if (pic->use_gobsync && j != 0)
  21.       bits->header += CountBitsSlice(j,QP);
  22.     for ( i = 0; i < pels/MB_SIZE; i++) {
  23.       pic->MB = i + j * (pels/MB_SIZE);
  24.       bits->no_intra++;
  25.       FillLumBlock(i*MB_SIZE, j*MB_SIZE, curr, data);//写亮度图像16*16 curr:图像数据 data:宏块树组
  26.       FillChromBlock(i*MB_SIZE, j*MB_SIZE, curr, data);//写色度图像2*8*8 
  27.       qcoeff = MB_Encode(data, QP, Mode); //变换并量化后数据到qcoeff=16*16+2*8*8个单元
  28.       CBP = FindCBP(qcoeff,Mode,64);
  29.       if (!syntax_arith_coding) {
  30.         CountBitsMB(Mode,COD,CBP,0,pic,bits);//输出宏块层信息
  31.         CountBitsCoeff(qcoeff, Mode, CBP,bits,64);
  32.       } 
  33.       MB_Decode(qcoeff, data, QP, Mode);  //反变换
  34.       Clip(data);                       //使 0<=data<=255 
  35.       ReconImage(i,j,data,recon);     //将重建宏块data输出到整个图像recon中
  36.       free(qcoeff);
  37.     }
  38.   }
  39.   pic->QP_mean = (float)QP;
  40.   free(data);
  41.   return recon;
  42. }
  43. void Clip(MB_Structure *data)
  44. {
  45.   int m,n;
  46.   for (n = 0; n < 16; n++) 
  47.     for (m = 0; m < 16; m++) 
  48.       data->lum[n][m] = mmin(255,mmax(0,data->lum[n][m]));
  49.   for (n = 0; n < 8; n++) 
  50.     for (m = 0; m < 8; m++) {
  51.       data->Cr[n][m] = mmin(255,mmax(0,data->Cr[n][m]));
  52.       data->Cb[n][m] = mmin(255,mmax(0,data->Cb[n][m]));
  53.     }
  54.   
  55. }
  56. /**********************************************************************/
  57. void ReconImage (int i, int j, MB_Structure *data, PictImage *recon)
  58. {
  59.   int n;
  60.   register int m;
  61.   int x_curr, y_curr;
  62.   x_curr = i * MB_SIZE;
  63.   y_curr = j * MB_SIZE;
  64.   /* Fill in luminance data */
  65.   for (n = 0; n < MB_SIZE; n++)
  66.     for (m= 0; m < MB_SIZE; m++) {
  67.       *(recon->lum + x_curr+m + (y_curr+n)*pels) = (unsigned char)data->lum[n][m]; 
  68.     }
  69.   /* Fill in chrominance data */
  70.   for (n = 0; n < (MB_SIZE>>1); n++)
  71.     for (m = 0; m < (MB_SIZE>>1); m++) {
  72.       *(recon->Cr + (x_curr>>1)+m + ((y_curr>>1)+n)*cpels) = (unsigned char)data->Cr[n][m];
  73.       *(recon->Cb + (x_curr>>1)+m + ((y_curr>>1)+n)*cpels) = (unsigned char)data->Cb[n][m];
  74.     }
  75.   return;
  76. }
  77. /***********************************************************************/
  78. void Quant(int *coeff, int *qcoeff, int QP, int Mode)
  79. {
  80.   int i;
  81.   for (i = 0; i < 64; i++) 
  82.     qcoeff[i] = coeff[i]/(2*QP);
  83.     
  84. qcoeff[0]=qcoeff[0]*2*QP/8;
  85.  
  86.   return;
  87. }
  88. /***********************************************************************/
  89. void Dequant(int *qcoeff, int *rcoeff, int QP, int Mode)
  90. {
  91.   int i;
  92.     for (i = 0; i < 64; i++)
  93.       rcoeff[i] = qcoeff[i]*2*QP;
  94.   rcoeff[0]=rcoeff[0]*8/(2*QP);
  95.   return;
  96. }
  97. /***********************************************************************/
  98. void FillLumBlock( int x, int y, PictImage *image, MB_Structure *data)
  99. {
  100.   int n;
  101.   register int m;
  102.   for (n = 0; n < MB_SIZE; n++)
  103.     for (m = 0; m < MB_SIZE; m++)
  104.       data->lum[n][m] = 
  105.         (int)(*(image->lum + x+m + (y+n)*pels));
  106.   return;
  107. }
  108. /***********************************************************************/
  109. void FillChromBlock(int x_curr, int y_curr, PictImage *image,
  110.             MB_Structure *data)
  111. {
  112.   int n;
  113.   register int m;
  114.   int x, y;
  115.   x = x_curr>>1;
  116.   y = y_curr>>1;
  117.   for (n = 0; n < (MB_SIZE>>1); n++)
  118.     for (m = 0; m < (MB_SIZE>>1); m++) {
  119.       data->Cr[n][m] = 
  120.         (int)(*(image->Cr +x+m + (y+n)*cpels));
  121.       data->Cb[n][m] = 
  122.         (int)(*(image->Cb +x+m + (y+n)*cpels));
  123.     }
  124.   return;
  125. }
  126. /***********************************************************************/
  127. int *MB_Encode(MB_Structure *mb_orig, int QP, int I)
  128. {
  129.   int        i, j, k, l, row, col;
  130.   int        fblock[64];
  131.   int        coeff[384];
  132.   int        *coeff_ind;
  133.   int        *qcoeff;
  134.   int        *qcoeff_ind;
  135.   if ((qcoeff=(int *)malloc(sizeof(int)*384)) == 0) {
  136.     fprintf(stderr,"mb_encode(): Couldn't allocate qcoeff.n");
  137.     exit(-1);
  138.   }
  139.   coeff_ind = coeff;
  140.   qcoeff_ind = qcoeff;
  141.   for (k=0;k<16;k+=8) {
  142.     for (l=0;l<16;l+=8) {
  143.       for (i=k,row=0;row<64;i++,row+=8) {
  144.         for (j=l,col=0;col<8;j++,col++) {
  145.           fblock[row+col] = mb_orig->lum[i][j];
  146.         }
  147.       }
  148.       Dct(fblock,coeff_ind);
  149.       Quant(coeff_ind,qcoeff_ind,QP,I);
  150.       coeff_ind += 64;
  151.       qcoeff_ind += 64;
  152.     }
  153.   }
  154.   for (i=0;i<8;i++) {
  155.     for (j=0;j<8;j++) {
  156.       *(fblock+i*8+j) = mb_orig->Cb[i][j];
  157.     }
  158.   }
  159.   Dct(fblock,coeff_ind);
  160.   Quant(coeff_ind,qcoeff_ind,QP,I); 
  161.   coeff_ind += 64;
  162.   qcoeff_ind += 64;
  163.   for (i=0;i<8;i++) {
  164.     for (j=0;j<8;j++) {
  165.       *(fblock+i*8+j) = mb_orig->Cr[i][j];
  166.     }
  167.   }
  168.   Dct(fblock,coeff_ind);
  169.   Quant(coeff_ind,qcoeff_ind,QP,I); 
  170.   return qcoeff;
  171. }
  172. /************************************************************************/
  173.      
  174. int MB_Decode(int *qcoeff, MB_Structure *mb_recon, int QP, int I)
  175. {
  176.   int i, j, k, l, row, col;
  177.   int *iblock;
  178.   int *qcoeff_ind;
  179.   int *rcoeff, *rcoeff_ind;
  180.   if ((iblock = (int *)malloc(sizeof(int)*64)) == NULL) {
  181.     fprintf(stderr,"MB_Coder: Could not allocate space for iblockn");
  182.     exit(-1);
  183.   }
  184.   if ((rcoeff = (int *)malloc(sizeof(int)*384)) == NULL) {
  185.     fprintf(stderr,"MB_Coder: Could not allocate space for rcoeffn");
  186.     exit(-1);
  187.   }  
  188.   for (i = 0; i < 16; i++)
  189.     for (j = 0; j < 16; j++)
  190.       mb_recon->lum[j][i] = 0;
  191.   for (i = 0; i < 8; i++) 
  192.     for (j = 0; j < 8; j++) {
  193.       mb_recon->Cb[j][i] = 0;
  194.       mb_recon->Cr[j][i] = 0;
  195.     }
  196.   qcoeff_ind = qcoeff;
  197.   rcoeff_ind = rcoeff;
  198.   for (k=0;k<16;k+=8) {
  199.     for (l=0;l<16;l+=8) {
  200.       Dequant(qcoeff_ind,rcoeff_ind,QP,I);
  201.       idct(rcoeff_ind,iblock); 
  202.       qcoeff_ind += 64;
  203.       rcoeff_ind += 64;
  204.       for (i=k,row=0;row<64;i++,row+=8) {
  205.         for (j=l,col=0;col<8;j++,col++) {
  206.           mb_recon->lum[i][j] = *(iblock+row+col);
  207.         }
  208.       }
  209.     }
  210.   }
  211.   Dequant(qcoeff_ind,rcoeff_ind,QP,I);
  212.   idct(rcoeff_ind,iblock); 
  213.   qcoeff_ind += 64;
  214.   rcoeff_ind += 64;
  215.   for (i=0;i<8;i++) {
  216.     for (j=0;j<8;j++) {
  217.       mb_recon->Cb[i][j] = *(iblock+i*8+j);
  218.     }
  219.   }
  220.   Dequant(qcoeff_ind,rcoeff_ind,QP,I);
  221.   idct(rcoeff_ind,iblock); 
  222.   for (i=0;i<8;i++) {
  223.     for (j=0;j<8;j++) {
  224.       mb_recon->Cr[i][j] = *(iblock+i*8+j);
  225.     }
  226.   }
  227.   free(iblock);
  228.   free(rcoeff);
  229.   return 0;
  230. }