text_code.c
来自「实现在linux下的mpeg4编解码」· C语言 代码 · 共 981 行 · 第 1/2 页
C
981 行
/************************************************************************** * * * This code is developed by Adam Li. This software is an * * implementation of a part of one or more MPEG-4 Video tools as * * specified in ISO/IEC 14496-2 standard. Those intending to use this * * software module in hardware or software products are advised that its * * use may infringe existing patents or copyrights, and any such use * * would be at such party's own risk. The original developer of this * * software module and his/her company, and subsequent editors and their * * companies (including Project Mayo), will have no liability for use of * * this software or modifications or derivatives thereof. * * * * Project Mayo gives users of the Codec a license to this software * * module or modifications thereof for use in hardware or software * * products claiming conformance to the MPEG-4 Video Standard as * * described in the Open DivX license. * * * * The complete Open DivX license can be found at * * http://www.projectmayo.com/opendivx/license.php . * * * **************************************************************************//************************************************************************** * * text_code.c * * Copyright (C) 2001 Project Mayo * * Adam Li * * DivX Advance Research Center <darc@projectmayo.com> * **************************************************************************//* This file contains some functions for text coding of image. *//* Some codes of this project come from MoMuSys MPEG-4 implementation. *//* Please see seperate acknowledgement file for a list of contributors. */#include "text_defs.h"#include "mot_code.h"#include "bitstream.h"#include "putvlc.h"#include "mot_util.h"#include "text_code_mb.h"#include "text_code.h"#define SKIPP 6extern FILE *ftrace;Void Bits_CountMB_combined ( Int DQUANT, Int Mode, Int COD, Int ACpred_flag, Int CBP, Int vop_type, Bits *bits, Image *mottext_bitstream, Int *MB_transp_pattern );Int doDCACpred ( Int *qcoeff, Int *CBP, Int ncoeffs, Int x_pos, Int y_pos, Int ***DC_store, Int QP, Int MB_width, Int direction[], Int mid_grey );Void nullfill(Int pred[], Int mid_grey);Int Idir_c(Int val, Int QP);Int IntraDCSwitch_Decision _P_(( Int Mode, Int intra_dc_vlc_thr, Int Qp ));Int FindCBP _P_(( Int *qcoeff, Int Mode, Int ncoeffs ));/***********************************************************CommentBegin****** * * -- VopCodeShapeTextIntraCom --Intra texture encoding of one vop, * Combined shape/(motion)/texture mode * * Purpose : * Intra texture encoding of one vop (combined shape/(mot)/text mode) * * Arguments in : * Vop curr : the current vop to be coded * Int intra_dcpred_disable : disable intradc prediction * Image* AB_SizeConversionDecisions: * Image* AB_first_MMR_values * VolConfig *vol_config : configuration information * Int rc_type : rate control type: * * Arguments out : * Vop *rec_curr : the reconstructed current vop * Image *texture_bitstream : the output bitstream * Bits : statistics information * * Description : * This function performs Intra texture encoding of one vop. * ***********************************************************CommentEnd********/Void VopCodeShapeTextIntraCom(Vop *curr,Vop *rec_curr, Image *mottext_bitstream){ Int QP = GetVopIntraQuantizer(curr); Int Mode = MODE_INTRA; Int* qcoeff; Int i, j; Int CBP, COD; Int CBPY, CBPC; Int num_pixels = GetImageSizeX(GetVopY(curr)); Int num_lines = GetImageSizeY(GetVopY(curr)); Int vop_type; Int ***DC_store; Int MB_width = num_pixels / MB_SIZE; Int MB_height = num_lines / MB_SIZE; Int m; Int ACpred_flag=-1; Int direction[6]; Int switched=0; Int DQUANT =0; Bits nbits, *bits; bits = &nbits; qcoeff = (Int *) malloc (sizeof (Int) * 384); #ifdef _RC_DEBUG_ fprintf(ftrace, "RC - VopCodeShapeTextIntraCom(): ---> CODING WITH: %d \n",QP); #endif for (i = 0; i < 6; i++) direction[i] = 0; /* allocate space for 3D matrix to keep track of prediction values for DC/AC prediction */ DC_store = (Int ***)calloc(MB_width*MB_height, sizeof(Int **)); for (i = 0; i < MB_width*MB_height; i++) { DC_store[i] = (Int **)calloc(6, sizeof(Int *)); for (j = 0; j < 6; j++) DC_store[i][j] = (Int *)calloc(15, sizeof(Int)); } Bits_Reset (bits); vop_type = PCT_INTRA; for (j = 0; j < num_lines/MB_SIZE; j++) /* Macro Block loop */ { for (i = 0; i < num_pixels/MB_SIZE; i++) { DQUANT = 0; COD = 0; bits->no_intra++; CodeMB (curr, rec_curr, NULL, i*MB_SIZE, j*MB_SIZE, num_pixels, QP+DQUANT, MODE_INTRA, qcoeff); m =0; DC_store[j*MB_width+i][0][m] = qcoeff[m]*cal_dc_scaler(QP+DQUANT,1); DC_store[j*MB_width+i][1][m] = qcoeff[m+64]*cal_dc_scaler(QP+DQUANT,1); DC_store[j*MB_width+i][2][m] = qcoeff[m+128]*cal_dc_scaler(QP+DQUANT,1); DC_store[j*MB_width+i][3][m] = qcoeff[m+192]*cal_dc_scaler(QP+DQUANT,1); DC_store[j*MB_width+i][4][m] = qcoeff[m+256]*cal_dc_scaler(QP+DQUANT,2); DC_store[j*MB_width+i][5][m] = qcoeff[m+320]*cal_dc_scaler(QP+DQUANT,2); for (m = 1; m < 8; m++) { DC_store[j*MB_width+i][0][m] = qcoeff[m]; DC_store[j*MB_width+i][1][m] = qcoeff[m+64]; DC_store[j*MB_width+i][2][m] = qcoeff[m+128]; DC_store[j*MB_width+i][3][m] = qcoeff[m+192]; DC_store[j*MB_width+i][4][m] = qcoeff[m+256]; DC_store[j*MB_width+i][5][m] = qcoeff[m+320]; } for (m = 0; m < 7; m++) { DC_store[j*MB_width+i][0][m+8] = qcoeff[(m+1)*8]; DC_store[j*MB_width+i][1][m+8] = qcoeff[(m+1)*8+64]; DC_store[j*MB_width+i][2][m+8] = qcoeff[(m+1)*8+128]; DC_store[j*MB_width+i][3][m+8] = qcoeff[(m+1)*8+192]; DC_store[j*MB_width+i][4][m+8] = qcoeff[(m+1)*8+256]; DC_store[j*MB_width+i][5][m+8] = qcoeff[(m+1)*8+320]; } CBP = FindCBP(qcoeff,Mode,64); /* Do the DC/AC prediction, changing the qcoeff values as appropriate */ if (GetVopIntraACDCPredDisable(curr) == 0) { ACpred_flag = doDCACpred(qcoeff, &CBP, 64, i, j, DC_store, QP+DQUANT, MB_width, direction,GetVopMidGrey(curr)); } else ACpred_flag = -1; switched = IntraDCSwitch_Decision(Mode, GetVopIntraDCVlcThr(curr), QP); if (switched) CBP = FindCBP(qcoeff,MODE_INTER,64); if (DQUANT) Mode=MODE_INTRA_Q;else Mode=MODE_INTRA; QP+=DQUANT; CBPY = CBP >> 2; CBPY = CBPY & 15; /* last 4 bits */ CBPC = CBP & 3; /* last 2 bits */ Bits_CountMB_combined (DQUANT, Mode, COD, ACpred_flag, CBP, vop_type, bits, mottext_bitstream,/*MB_transp_pattern*/NULL); /* added the variable intra_dcpred_diable */ MB_CodeCoeff(bits, qcoeff, Mode, CBP, 64, GetVopIntraACDCPredDisable(curr), NULL, mottext_bitstream, /*MB_transp_pattern*/NULL, direction, 1 /*GetVopErrorResDisable(curr)*/, 0 /*GetVopReverseVlc(curr)*/, switched, 0 /*curr->alternate_scan*/); } } /* Free allocated memory for 3D matrix */ for (i = 0; i < MB_width*MB_height; i++) { for (j = 0; j < 6; j++) free(DC_store[i][j]); free(DC_store[i]); } free(DC_store); free ((Char*)qcoeff);}/***********************************************************CommentBegin****** * * -- VopShapeMotText -- Combined Inter encoding of shape motion and texture * * Purpose : * Combined Inter encoding of texture and motion. * Used by VopCodeMotTextInter * * Arguments in : * Vop *curr : the current vop to be encoded * Vop *rec_prev: the previous reconstructed vop * Image *mot_x : the x-coordinates of the motion vectors * Image *mot_y : the y-coordinates of the motion vectors * Image *MB_decisions: Contains for each macroblock the encoding mode * Int f_code_for: MV search range 1/2 pel: 1=32,2=64,...,7=2048 * Image* AB_SizeConversionDecisions: * Image* AB_first_MMR_values : * Int intra_dcpred_disable : disable the intra dc prediction * VolConfig *vol_config : configuration information * Int rc_type : rate control type * * Arguments out : * Vop *rec_curr : the reconstructed current vop * Image *mottext_bitstream : the output texture/motion bitstream * Bits *bits : Coding statistics * ***********************************************************CommentEnd********/Void VopShapeMotText (Vop *curr, Vop *comp, Image *MB_decisions, Image *mot_x, Image *mot_y,Int f_code_for, Int intra_acdc_pred_disable,Vop *rec_curr,Image *mottext_bitstream){ Int Mode=0; Int QP = GetVopQuantizer(curr); Int* qcoeff=NULL; Int i, j; Int CBP; Int COD; Int CBPY, CBPC; Int MB_in_width, MB_in_height, B_in_width, mbnum, boff; SInt p; SInt *ptr=NULL; Float *motx_ptr=NULL, *moty_ptr=NULL; Int num_pixels; Int num_lines; Int vop_type=PCT_INTER; Int ***DC_store=NULL; Int m, n; Int ACpred_flag=-1; Int direction[6]; Int switched=0; Int DQUANT=0; Bits nbits, *bits; bits = &nbits; qcoeff = (Int *) malloc (sizeof (Int) * 384); num_pixels = GetImageSizeX(GetVopY(curr)); num_lines = GetImageSizeY(GetVopY(curr)); MB_in_width = num_pixels / MB_SIZE; MB_in_height = num_lines / MB_SIZE; B_in_width = 2 * MB_in_width; for (i = 0; i < 6; i++) direction[i] = 0; #ifdef _RC_DEBUG_ printf("RC - VopShapeMotText(): ---> CODING WITH: %d \n",QP); #endif /* allocate space for 3D matrix to keep track of prediction values for DC/AC prediction */ DC_store = (Int ***)calloc(MB_in_width*MB_in_height, sizeof(Int **)); for (i = 0; i < MB_in_width*MB_in_height; i++) { DC_store[i] = (Int **)calloc(6, sizeof(Int *)); for (j = 0; j < 6; j++) DC_store[i][j] = (Int *)calloc(15, sizeof(Int)); } Bits_Reset (bits); vop_type = PCT_INTER; ptr = (SInt *) GetImageData(MB_decisions); motx_ptr = (Float *) GetImageData(mot_x); moty_ptr = (Float *) GetImageData(mot_y); for (j = 0; j < num_lines/MB_SIZE; j++) { for (i = 0; i < MB_in_width; i++) { switched=0; p = *ptr; DQUANT = 0; /* Fill DC_store with default coeff values */ for (m = 0; m < 6; m++) { DC_store[j*MB_in_width+i][m][0] = GetVopMidGrey(curr)*8; for (n = 1; n < 15; n++) DC_store[j*MB_in_width+i][m][n] = 0; } switch (p) { case MBM_INTRA: Mode = (DQUANT == 0) ? MODE_INTRA : MODE_INTRA_Q; bits->no_intra++; break; case MBM_INTER16: Mode = (DQUANT == 0) ? MODE_INTER : MODE_INTER_Q; bits->no_inter++; break; case MBM_INTER8: Mode = MODE_INTER4V; bits->no_inter4v++; DQUANT = 0; /* Can't change QP for 8x8 mode */ break; default: printf("invalid MB_decision value :%d\n", p); exit(0); } CodeMB (curr, rec_curr, comp, i*MB_SIZE, j*MB_SIZE, num_pixels, QP + DQUANT, Mode, qcoeff); mbnum = j*MB_in_width + i; boff = (2 * (mbnum / MB_in_width) * B_in_width + 2 * (mbnum % MB_in_width)); CBP = FindCBP(qcoeff,Mode,64); if ((CBP == 0) && (p == 1) && (*(motx_ptr +boff) == 0.0) && (*(moty_ptr +boff) == 0.0)) { COD = 1; /* skipped macroblock */ BitstreamPutBits(mottext_bitstream, (long) (COD), 1L); bits->COD ++; *ptr = SKIPP; Mode = MODE_INTER; } else { COD = 0; /* coded macroblock */ if ((Mode == MODE_INTRA) || (Mode == MODE_INTRA_Q)) { /* Store the qcoeff-values needed later for prediction */ m =0; DC_store[j*MB_in_width+i][0][m] = qcoeff[m]*cal_dc_scaler(QP+DQUANT,1); DC_store[j*MB_in_width+i][1][m] = qcoeff[m+64]*cal_dc_scaler(QP+DQUANT,1); DC_store[j*MB_in_width+i][2][m] = qcoeff[m+128]*cal_dc_scaler(QP+DQUANT,1); DC_store[j*MB_in_width+i][3][m] = qcoeff[m+192]*cal_dc_scaler(QP+DQUANT,1); DC_store[j*MB_in_width+i][4][m] = qcoeff[m+256]*cal_dc_scaler(QP+DQUANT,2); DC_store[j*MB_in_width+i][5][m] = qcoeff[m+320]*cal_dc_scaler(QP+DQUANT,2); for (m = 1; m < 8; m++) { DC_store[j*MB_in_width+i][0][m] = qcoeff[m]; DC_store[j*MB_in_width+i][1][m] = qcoeff[m+64]; DC_store[j*MB_in_width+i][2][m] = qcoeff[m+128]; DC_store[j*MB_in_width+i][3][m] = qcoeff[m+192]; DC_store[j*MB_in_width+i][4][m] = qcoeff[m+256]; DC_store[j*MB_in_width+i][5][m] = qcoeff[m+320]; } for (m = 0; m < 7; m++) { DC_store[j*MB_in_width+i][0][m+8] = qcoeff[(m+1)*8]; DC_store[j*MB_in_width+i][1][m+8] = qcoeff[(m+1)*8+64]; DC_store[j*MB_in_width+i][2][m+8] = qcoeff[(m+1)*8+128]; DC_store[j*MB_in_width+i][3][m+8] = qcoeff[(m+1)*8+192]; DC_store[j*MB_in_width+i][4][m+8] = qcoeff[(m+1)*8+256]; DC_store[j*MB_in_width+i][5][m+8] = qcoeff[(m+1)*8+320]; } if (intra_acdc_pred_disable == 0) ACpred_flag = doDCACpred(qcoeff, &CBP, 64, i, j, DC_store, QP+DQUANT, MB_in_width, direction,GetVopMidGrey(curr)); else ACpred_flag = -1; /* Not to go into bitstream */ } switched = IntraDCSwitch_Decision(Mode, GetVopIntraDCVlcThr(curr), QP); if (switched) CBP = FindCBP(qcoeff,MODE_INTER,64); CBPY = CBP >> 2; CBPY = CBPY & 15; /* last 4 bits */ CBPC = CBP & 3; /* last 2 bits */ Bits_CountMB_combined (DQUANT, Mode, COD, ACpred_flag, CBP, vop_type, bits, mottext_bitstream,/*MB_transp_pattern*/NULL); Bits_CountMB_Motion( mot_x, mot_y, NULL, MB_decisions, i, j, f_code_for, 0 /*quarter_pel*/, mottext_bitstream, 1 /*GetVopErrorResDisable(curr)*/, 0, (Int **)NULL, 0 /*GetVopShape(curr)*/); MB_CodeCoeff(bits, qcoeff, Mode, CBP, 64, intra_acdc_pred_disable, NULL, mottext_bitstream, /*MB_transp_pattern*/NULL, direction, 1/*GetVopErrorResDisable(curr)*/, 0/*GetVopReverseVlc(curr)*/, switched, 0 /*curr->alternate_scan*/); } ptr++; } /* for i loop */ } /* for j loop */ /* Free allocated memory for 3D matrix */ for (i = 0; i < MB_in_width*MB_in_height; i++) { for (j = 0; j < 6; j++) free(DC_store[i][j]); free(DC_store[i]); }
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?