📄 block_functions.c
字号:
/************************************************************************ * * coder.c, main coding engine of 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. **//***************************************************************** * * Modified by Pat Mulroy, BT Labs to run syntax based arithmetic * coding. SAC option, H.263 (Annex E). * *****************************************************************/#include"sim.h"/********************************************************************** * * Name: MB_Encode * Description: DCT of Macroblocks * * Input: MB data struct * Returns: Pointer to coefficients * Side effects: * * Date: 930128 Author: Robert.Danielsen@nta.no * Date: 970915 Author: guyc@ee.ubc.ca * Quantization done separatly * **********************************************************************/int *MB_Encode(MB_Structure *mb_orig){ int i, j, k, l, row, col; int fblock[64]; int *coeff; int *coeff_ind; if ((coeff = (int *)malloc(sizeof(int)*384)) == NULL) { fprintf(stderr,"MB_Encode: Could not allocate space for coeff\n"); exit(-1); } coeff_ind = coeff; for (k=0;k<16;k+=8) { for (l=0;l<16;l+=8) { for (i=k,row=0;row<64;i++,row+=8) { for (j=l,col=0;col<8;j++,col++) { *(fblock+row+col) = mb_orig->lum[i][j]; } } Dct(fblock,coeff_ind); coeff_ind += 64; } } for (i=0;i<8;i++) { for (j=0;j<8;j++) { *(fblock+i*8+j) = mb_orig->Cb[i][j]; } } Dct(fblock,coeff_ind); coeff_ind += 64; for (i=0;i<8;i++) { for (j=0;j<8;j++) { *(fblock+i*8+j) = mb_orig->Cr[i][j]; } } Dct(fblock,coeff_ind); return coeff;}/********************************************************************** * * 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 * Date: 970915 Author: guyc@ee.ubc.ca * Deqantization done separatly **********************************************************************/int MB_Decode(int *rcoeff, MB_Structure *mb_recon){ int i, j, k, l, row, col; int *iblock; int *rcoeff_ind; if ((iblock = (int *)malloc(sizeof(int)*64)) == NULL) { fprintf(stderr,"MB_Coder: Could not allocate space for iblock\n"); exit(-1); } /* For control purposes */ /* Zero data */ for (i = 0; i < 16; i++) for (j = 0; j < 16; j++) mb_recon->lum[j][i] = 0; for (i = 0; i < 8; i++) for (j = 0; j < 8; j++) { mb_recon->Cb[j][i] = 0; mb_recon->Cr[j][i] = 0; } rcoeff_ind = rcoeff; for (k=0;k<16;k+=8) { for (l=0;l<16;l+=8) {#ifndef FASTIDCT idctref(rcoeff_ind,iblock); #else idct(rcoeff_ind,iblock); #endif rcoeff_ind += 64; for (i=k,row=0;row<64;i++,row+=8) { for (j=l,col=0;col<8;j++,col++) { mb_recon->lum[i][j] = *(iblock+row+col); } } } }#ifndef FASTIDCT idctref(rcoeff_ind,iblock); #else idct(rcoeff_ind,iblock); #endif rcoeff_ind += 64; for (i=0;i<8;i++) { for (j=0;j<8;j++) { mb_recon->Cb[i][j] = *(iblock+i*8+j); } }/* printf("Cb.\n"); for (i=0;i<8;i++) { printf("%d %d %d %d %d %d %d %d\n", mb_recon->Cb[i][0], mb_recon->Cb[i][1], mb_recon->Cb[i][2], mb_recon->Cb[i][3], mb_recon->Cb[i][4], mb_recon->Cb[i][5], mb_recon->Cb[i][6], mb_recon->Cb[i][7]); } printf("\n");*/#ifndef FASTIDCT idctref(rcoeff_ind,iblock); #else idct(rcoeff_ind,iblock); #endif for (i=0;i<8;i++) { for (j=0;j<8;j++) { mb_recon->Cr[i][j] = *(iblock+i*8+j); } } free(iblock); return 0;}/********************************************************************** * * Name: FillLumBlock * Description: Fills the luminance of one block of PictImage * * Input: Position, pointer to PictImage, array to fill * Returns: * Side effects: fills array * * Date: 930129 Author: Karl.Lillevold@nta.no * ***********************************************************************/void FillLumBlock( int x, int y, PictImage *image, MB_Structure *data){ int n; register int m; for (n = 0; n < MB_SIZE; n++) for (m = 0; m < MB_SIZE; m++) data->lum[n][m] = (int)(*(image->lum + x+m + (y+n)*pels)); return;}/********************************************************************** * * Name: FillChromBlock * Description: Fills the chrominance of one block of PictImage * * Input: Position, pointer to PictImage, array to fill * Returns: * Side effects: fills array * 128 subtracted from each * * Date: 930129 Author: Karl.Lillevold@nta.no * ***********************************************************************/void FillChromBlock(int x_curr, int y_curr, PictImage *image, MB_Structure *data){ int n; register int m; int x, y; x = x_curr>>1; y = y_curr>>1; for (n = 0; n < (MB_SIZE>>1); n++) for (m = 0; m < (MB_SIZE>>1); m++) { data->Cr[n][m] = (int)(*(image->Cr +x+m + (y+n)*cpels)); data->Cb[n][m] = (int)(*(image->Cb +x+m + (y+n)*cpels)); } return;}/********************************************************************** * * Name: ZeroMBlock * Description: Fills one MB with Zeros * * Input: MB_Structure to zero out * Returns: * Side effects: * * Date: 940829 Author: Karl.Lillevold@nta.no * ***********************************************************************/void ZeroMBlock(MB_Structure *data){ int n; register int m; for (n = 0; n < MB_SIZE; n++) for (m = 0; m < MB_SIZE; m++) data->lum[n][m] = 0; for (n = 0; n < (MB_SIZE>>1); n++) for (m = 0; m < (MB_SIZE>>1); m++) { data->Cr[n][m] = 0; data->Cb[n][m] = 0; } return;}/********************************************************************** * * Name: Clip * Description: clips recontructed data 0-255 * * Input: pointer to recon. data structure * Side effects: data structure clipped * * Date: 950718 Author: Karl.Lillevold@nta.no * ***********************************************************************/void Clip(MB_Structure *data){ int m,n; for (n = 0; n < 16; n++) { for (m = 0; m < 16; m++) { data->lum[n][m] = mmin(255,mmax(0,data->lum[n][m])); } } for (n = 0; n < 8; n++) { for (m = 0; m < 8; m++) { data->Cr[n][m] = mmin(255,mmax(0,data->Cr[n][m])); data->Cb[n][m] = mmin(255,mmax(0,data->Cb[n][m])); } }}/********************************************************************** * * Name: LoadArea * Description: fills array with a square of image-data * * Input: pointer to image and position, x and y size * Returns: pointer to area * Side effects: memory allocated to array * * Date: 940203 Author: PGB * Mod: KOL * ***********************************************************************/unsigned char *LoadArea (unsigned char *im, int x, int y, int x_size, int y_size, int lx){ unsigned char *res = (unsigned char *) malloc (sizeof (char) * x_size * y_size); unsigned char *in; unsigned char *out; int i = x_size; int j = y_size; in = im + (y * lx) + x; out = res; while (j--) { while (i--) *out++ = *in++; i = x_size; in += lx - x_size; }; return res;}/********************************************************************** * * Name: SAD_Macroblock * Description: fast way to find the SAD of one vector * * Input: pointers to search_area and current block, * Min_F1/F2/FR * Returns: sad_f1/f2 * Side effects: * * Date: 940203 Author: PGB * Mod: KOL * ***********************************************************************/int SAD_Macroblock (unsigned char *ii, unsigned char *act_block, int h_length, int Min_FRAME){ int i; int sad = 0; unsigned char *kk; kk = act_block; i = 16; while (i--) { sad += (abs (*ii - *kk) + abs (*(ii + 1) - *(kk + 1)) + abs (*(ii + 2) - *(kk + 2)) + abs (*(ii + 3) - *(kk + 3)) + abs (*(ii + 4) - *(kk + 4)) + abs (*(ii + 5) - *(kk + 5)) + abs (*(ii + 6) - *(kk + 6)) + abs (*(ii + 7) - *(kk + 7)) + abs (*(ii + 8) - *(kk + 8)) + abs (*(ii + 9) - *(kk + 9)) + abs (*(ii + 10) - *(kk + 10)) + abs (*(ii + 11) - *(kk + 11)) + abs (*(ii + 12) - *(kk + 12)) + abs (*(ii + 13) - *(kk + 13)) + abs (*(ii + 14) - *(kk + 14)) + abs (*(ii + 15) - *(kk + 15))); ii += h_length; kk += 16; if (sad > Min_FRAME) return INT_MAX; } return sad;}int SAD_Block (unsigned char *ii, unsigned char *act_block, int h_length, int min_sofar){ int i; int sad = 0; unsigned char *kk; kk = act_block; i = 8; while (i--) { sad += (abs (*ii - *kk) + abs (*(ii + 1) - *(kk + 1)) + abs (*(ii + 2) - *(kk + 2)) + abs (*(ii + 3) - *(kk + 3)) + abs (*(ii + 4) - *(kk + 4)) + abs (*(ii + 5) - *(kk + 5)) + abs (*(ii + 6) - *(kk + 6)) + abs (*(ii + 7) - *(kk + 7))); ii += h_length; kk += 16; if (sad > min_sofar) return INT_MAX; } return sad;}int SAD_MB_Bidir (unsigned char *ii, unsigned char *aa, unsigned char *bb,
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -