📄 coder.cpp
字号:
////////////////////////////////////////////////////////////////////////////
//
//
// Project : VideoNet version 1.1.
// Description : Peer to Peer Video Conferencing over the LAN.
// Author : Nagareshwar Y Talekar ( nsry2002@yahoo.co.in)
// Date : 15-6-2004.
//
// I have converted origional fast h.263 encoder library from C to C++
// so that it can be integrated into any windows application easily.
// I have removed some of unnecessary codes/files from the
// fast h263 library.Also moved definitions and declarations
// in their proper .h and .cpp files.
//
// File description :
// Name : coder.cpp
//
//
/////////////////////////////////////////////////////////////////////////////
/************************************************* * 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 "stdafx.h"
#include "coder.h"
#include "countbit.h"
#include "pred.h"
#include "quant.h"
#include "mot_est.h"
#include "dct.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
*
***********************************************************************///__inline__ void Clip(MB_Structure *data) 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++; }}/* Encodes one frame intra using params */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); Global::pic->QUANT = params->Q_intra; Global::pic->picture_coding_type = PCT_INTRA; bits->header += CountBitsPicture(Global::pic); for ( j = 0; j < Global::mbr; j++) { /* insert sync in *every* slice if use_gobsync is chosen */ if (Global::pic->use_gobsync && j != 0) bits->header += CountBitsSlice(j,params->Q_intra); for ( i = 0; i < Global::mbc; i++) { Global::pic->MB = i + j * Global::mbc; bits->no_intra++; FillLumBlock(i*MB_SIZE, j*MB_SIZE, params->data, data); FillChromBlock(i*MB_SIZE, j*MB_SIZE, params->data, data); qcoeff = MB_EncodeAndFindCBP(data, params->Q_intra, Mode, &CBP); /* Do standard VLC encoding */ /* COD = 0 ,Every block is coded as Intra frame */ CountBitsMB(Mode,0,CBP,0,Global::pic,bits);
CountBitsCoeff(qcoeff, Mode, CBP,bits,64);
MB_Decode(qcoeff, data, params->Q_intra, Mode);
Clip(data);
ReconImage(i,j,data,new_recon);
free(qcoeff); } } Global::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) */int *MB_EncodeAndFindCBP(MB_Structure *mb_orig, int QP, int I, int *CBP){ int i, j, k, l, row, col; int fblock[64]; int coeff[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; 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]); }#endif } Dct(fblock,coeff_ind); *CBP |= QuantAndFindCBP(coeff_ind,qcoeff_ind,QP,I,CBP_Mask); coeff_ind += 64; qcoeff_ind += 64; CBP_Mask = CBP_Mask>>1; } /* end l */ } /* End k */ 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; 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); }#endif
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -