?? coder.c
字號:
/************************************************* * libr263: fast H.263 encoder library * * Copyright (C) 1996, Roalt Aalmoes, Twente University * SPA multimedia group * * Based on Telenor TMN 1.6 encoder (Copyright (C) 1995, Telenor R&D) * created by Karl Lillevold * * Author encoder: Roalt Aalmoes, <aalmoes@huygens.nl> * * Date: 31-07-96 **************************************************/ /* Modyfied by Roalt Aalmoes with better algorithms and performance * objectives * * Warning: Although I tried to remove all advanced options code, * there might still be some artifacts in the code. Please do not * blame me for not removing all of it. * Removing all advanced and alternating quantization code was done * for performance reasons. I'm sorry these options are not * implemented here, but see the tmn1.7 code for a slow functioning * advanced H.263 compression algorithm. *****************************************************************//* Notes for clear code: *//* var. j is used for row indexing of MB in frame *//* var. i is used for column indexing of MB in frame *//* pic is declared global *//* MV is changed: it is now a real array of MV instead of array of pointers to MV. Its range is also equal to MVs encoded, without border MVs Advantages: No more mallocs and frees in the code, disadvantages: ?? *//* PictImage structure is replaced by int pointer. Drawback: not flexible for other format. */#include"sim.h"/********************************************************************** * * Name: Clip * Description: clips recontructed data 0-255 * * Input: pointer to recon. data structure * Side effects: data structure clipped * * Date: 960715 Author: Roalt Aalmoes * ***********************************************************************/ void Clip(MB_Structure *data) { int n; int *mb_ptr = (int *) data; for (n = 0; n < 256 + 64 + 64; n++) { *mb_ptr = mmin(255,mmax(0,*mb_ptr)); mb_ptr++; }}/* 編碼I幀圖像 */void CodeIntraH263(CParam *params, Bits *bits){ unsigned int *new_recon; MB_Structure *data = (MB_Structure *)malloc(sizeof(MB_Structure)); int *qcoeff; int Mode = MODE_INTRA; int CBP; int i,j; new_recon = params->recon; ZeroBits(bits); pic->QUANT = params->Q_intra; pic->picture_coding_type = PCT_INTRA; bits->header += CountBitsPicture(pic); // 50 for ( j = 0; j < mbr; j++) { // 列 /* insert sync in *every* slice if use_gobsync is chosen */
// use_gobsync 0 不需加入slice的頭 if (pic->use_gobsync && j != 0) bits->header += CountBitsSlice(j,params->Q_intra); for ( i = 0; i < mbc; i++) { //行 pic->MB = i + j * mbc; bits->no_intra++; FillLumBlock(i*MB_SIZE, j*MB_SIZE, params->data, data); //MB_size 16 FillChromBlock(i*MB_SIZE, j*MB_SIZE, params->data, data);
//dct變換和量化,返回量化后系數的值qcoeff, 和CBP值(每個小宏塊是否全0) qcoeff = MB_EncodeAndFindCBP(data, params->Q_intra, Mode, &CBP); /* Do standard VLC encoding */ // 變長編碼 /* COD = 0 ,Every block is coded as Intra frame */ // COD=0 宏塊為幀內
//計算宏塊信息需要的bit數,編碼cod, 對宏塊的COD,CBPCM,CBPY 編碼 CountBitsMB(Mode,0,CBP,0,pic,bits);//intra中沒有skip宏塊
//宏塊, 對qcoeff編碼(16*16宏塊),寫入bit流,并將寫入bit數加入到 bits->Y,和 bits->U中 CountBitsCoeff(qcoeff, Mode, CBP,bits,64);
//重建 MB_Decode(qcoeff, data, params->Q_intra, Mode);
//限定到0和255間 Clip(data);
//重建圖像,把data(16*16)的數據放回到new recon(width*height) 中 ReconImage(i,j,data,new_recon);
free(qcoeff); } } pic->QP_mean = params->Q_intra; params->recon = new_recon; AddBitsPicture(bits); free(data); return;}/********************************************************************** * * Name: MB_Encode * Description: DCT and quantization of Macroblocks * * Input: MB data struct, mquant (1-31, 0 = no quant), * MB info struct * Returns: Pointer to quantized coefficients * Side effects: * * Date: 930128 Author: Robert.Danielsen@nta.no * **********************************************************************//* If you compare original quant with FindCBP, you see they both act on the same range of coefficients in the cases INTRA (1..63) or INTER (0..63) */
// dct變換后,系數量化,得到量化系數.返回地址.// mb_orig 原圖像yuv數值, QP 量化參數值, I 幀內或幀間模式, CBP值
//CBP從32+16+8+4+2+1,共6個8*8塊的情況,qcoeff系數中有非零值,則加上相對應的值
//對mb_orig中數值,dct變換,并量化得到量化系數
//inter狀態下,為圖像宏塊和預測宏塊的差值int *MB_EncodeAndFindCBP(MB_Structure *mb_orig, int QP, int I, int *CBP){ int i, j, k, l, row, col; int fblock[64]; //暫存8*8小塊的值 int coeff[384]; // 16*16的宏塊yuv值dct變換后系數共384個 int *coeff_ind; int *qcoeff; int *qcoeff_ind; int CBP_Mask = 32; *CBP = 0; /* CBP gives bit pattern of lowest 6 bits that specify which coordinates are not zero. Bits 6 (32) to 2 (4) repr. four 8x8 Y parts of macroblock, while bits 1 (2) and 0 (1) repr. resp. the U and V component */ if ((qcoeff=(int *)malloc(sizeof(int)*384)) == 0) { fprintf(stderr,"mb_encode(): Couldn't allocate qcoeff.\n"); exit(0); } coeff_ind = coeff; qcoeff_ind = qcoeff;
//Y值 for (k=0;k<16;k+=8) { for (l=0;l<16;l+=8) { for (i=k,row=0;row<64;i++,row+=8)
{ #if LONGISDOUBLEINT for (j=l,col=0;col<8;j += 2,col +=2 ) { *(long *) (fblock+row+col) = * (long *) &(mb_orig->lum[i][j]); } #else for (j=l,col=0;col<8;j++ , col++ ) { *(int *) (fblock+row+col) = * (int *) &(mb_orig->lum[i][j]); // 8*8的塊 } #endif }
//dct變換得到系數,在coeff_ind地址處, coeff_ind 值為DC值 Dct(fblock,coeff_ind);
//量化放入qcoeff_ind,并找到CBP值(63個AC系數 qcoeff是否全0) *CBP |= QuantAndFindCBP(coeff_ind,qcoeff_ind,QP,I,CBP_Mask);//CBP_Mask 32,16,8,4,2,1分別對應6個8*8小塊 coeff_ind += 64; qcoeff_ind += 64; CBP_Mask = CBP_Mask>>1; // 除以2 } /* end l */ } /* End k */
//U值 for (i=0;i<8;i++) {#ifdef LONGISDOUBLEINT for (j=0;j<8;j += 2) { *(long *) (fblock+i*8+j) = *(long *) &(mb_orig->Cb[i][j]); }#else for (j=0;j<8;j++) { *(int *) (fblock+i*8+j) = *(int *) &(mb_orig->Cb[i][j]); } #endif } Dct(fblock,coeff_ind); *CBP |= QuantAndFindCBP(coeff_ind,qcoeff_ind,QP,I,CBP_Mask /* i == 4 */); coeff_ind += 64; qcoeff_ind += 64; CBP_Mask = CBP_Mask>>1;
//V值 for (i=0;i<8;i++) {#ifdef LONGISDOUBLEINT for (j=0;j<8;j += 2) { * (long *) (fblock+i*8+j) = *(long *) &(mb_orig->Cr[i][j]); }#else for (j=0;j<8;j ++) { * (int *) (fblock+i*8+j) = *(int *) &(mb_orig->Cr[i][j]); }#endif } Dct(fblock,coeff_ind); *CBP |= QuantAndFindCBP(coeff_ind,qcoeff_ind,QP,I, CBP_Mask /* i == 5 */);
return qcoeff;}/********************************************************************** * * Name: MB_Decode * Description: Reconstruction of quantized DCT-coded Macroblocks * * Input: Quantized coefficients, MB data * QP (1-31, 0 = no quant), MB info block * Returns: int (just 0) * Side effects: * * Date: 930128 Author: Robert.Danielsen@nta.no * **********************************************************************/ int MB_Decode(int *qcoeff, MB_Structure *mb_recon, int QP, int I){ int i, j, k, l, row, col; int *iblock; int *qcoeff_ind; int *rcoeff, *rcoeff_ind; if ((iblock = (int *)malloc(sizeof(int)*64)) == NULL) { fprintf(stderr,"MB_Coder: Could not allocate space for iblock\n"); exit(-1); } if ((rcoeff = (int *)malloc(sizeof(int)*384)) == NULL) { fprintf(stderr,"MB_Coder: Could not allocate space for rcoeff\n"); exit(-1); } /* For control purposes */ /* Zero data */ for (i = 0; i < 16; i++)#ifdef LONGISDOUBLEINT for (j = 0; j < 8; j+=2) *(long *) &(mb_recon->lum[i][j]) = 0L;#else for (j = 0; j < 8; j++) *(int *) &(mb_recon->lum[i][j]) = 0;#endif for (i = 0; i < 8; i++) #ifdef LONGISDOUBLEINT for (j = 0; j < 8; j += 2) { *(long *) &(mb_recon->Cb[i][j]) = 0L; *(long *) &(mb_recon->Cr[i][j]) = 0L; }#else for (j = 0; j < 8; j ++) { *(int *) &(mb_recon->Cb[i][j]) = 0; *(int *) &(mb_recon->Cr[i][j]) = 0; }#endif qcoeff_ind = qcoeff; rcoeff_ind = rcoeff; for (k=0;k<16;k+=8) { for (l=0;l<16;l+=8) { Dequant(qcoeff_ind,rcoeff_ind,QP,I);#ifdef STANDARDIDCT idctref(rcoeff_ind,iblock); #else idct(rcoeff_ind,iblock); #endif qcoeff_ind += 64; rcoeff_ind += 64; for (i=k,row=0;row<64;i++,row+=8) {#ifdef LONGISDOUBLEINT for (j=l,col=0;col<8;j += 2,col += 2) { *(long *) &(mb_recon->lum[i][j]) = * (long *) (iblock+row+col); }#else for (j=l,col=0;col<8; j++, col++) { *(int *) &(mb_recon->lum[i][j]) = * (int *) (iblock+row+col); }#endif } } } Dequant(qcoeff_ind,rcoeff_ind,QP,I);#ifdef STANDARDIDCT idctref(rcoeff_ind,iblock); #else idct(rcoeff_ind,iblock); #endif qcoeff_ind += 64; rcoeff_ind += 64; for (i=0;i<8;i++) {#ifdef LONGISDOUBLEINT for (j=0;j<8;j +=2 ) { *(long *) &(mb_recon->Cb[i][j]) = *(long *) (iblock+i*8+j); }#else for (j=0;j<8;j++ ) { *(int *) &(mb_recon->Cb[i][j]) = *(int *) (iblock+i*8+j); }#endif } Dequant(qcoeff_ind,rcoeff_ind,QP,I);
#ifdef STANDARDIDCT idctref(rcoeff_ind,iblock); #else idct(rcoeff_ind,iblock); #endif for (i=0;i<8;i++) {#ifdef LONGISDOUBLEINT for (j=0;j<8;j += 2) { *(long *) &(mb_recon->Cr[i][j]) = *(long *) (iblock+i*8+j); }#else for (j=0;j<8;j++) { *(int *) &(mb_recon->Cr[i][j]) = *(int *) (iblock+i*8+j);
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -