--- mbtransquant.c 2003/12/01 10:46:40 1.21.2.22 +++ mbtransquant.c 2010/11/28 15:18:21 1.33 @@ -1,10 +1,10 @@ /***************************************************************************** * * XVID MPEG-4 VIDEO CODEC - * - MB Transfert/Quantization functions - + * - MB Transfer/Quantization functions - * - * Copyright(C) 2001-2003 Peter Ross - * 2001-2003 Michael Militzer + * Copyright(C) 2001-2010 Peter Ross + * 2001-2010 Michael Militzer * 2003 Edouard Gomez * * This program is free software ; you can redistribute it and/or modify @@ -21,7 +21,7 @@ * along with this program ; if not, write to the Free Software * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA * - * $Id: mbtransquant.c,v 1.21.2.22 2003/12/01 10:46:40 syskin Exp $ + * $Id: mbtransquant.c,v 1.33 2010/11/28 15:18:21 Isibaar Exp $ * ****************************************************************************/ @@ -40,9 +40,9 @@ #include "../dct/fdct.h" #include "../dct/idct.h" #include "../quant/quant.h" +#include "../motion/sad.h" #include "../encoder.h" -#include "../image/reduced.h" #include "../quant/quant_matrix.h" MBFIELDTEST_PTR MBFieldTest; @@ -91,12 +91,12 @@ /* Perform DCT */ start_timer(); - fdct(&data[0 * 64]); - fdct(&data[1 * 64]); - fdct(&data[2 * 64]); - fdct(&data[3 * 64]); - fdct(&data[4 * 64]); - fdct(&data[5 * 64]); + fdct((short * const)&data[0 * 64]); + fdct((short * const)&data[1 * 64]); + fdct((short * const)&data[2 * 64]); + fdct((short * const)&data[3 * 64]); + fdct((short * const)&data[4 * 64]); + fdct((short * const)&data[5 * 64]); stop_dct_timer(); } @@ -106,12 +106,12 @@ const uint8_t cbp) { start_timer(); - if(cbp & (1 << (5 - 0))) idct(&data[0 * 64]); - if(cbp & (1 << (5 - 1))) idct(&data[1 * 64]); - if(cbp & (1 << (5 - 2))) idct(&data[2 * 64]); - if(cbp & (1 << (5 - 3))) idct(&data[3 * 64]); - if(cbp & (1 << (5 - 4))) idct(&data[4 * 64]); - if(cbp & (1 << (5 - 5))) idct(&data[5 * 64]); + if(cbp & (1 << (5 - 0))) idct((short * const)&data[0 * 64]); + if(cbp & (1 << (5 - 1))) idct((short * const)&data[1 * 64]); + if(cbp & (1 << (5 - 2))) idct((short * const)&data[2 * 64]); + if(cbp & (1 << (5 - 3))) idct((short * const)&data[3 * 64]); + if(cbp & (1 << (5 - 4))) idct((short * const)&data[4 * 64]); + if(cbp & (1 << (5 - 5))) idct((short * const)&data[5 * 64]); stop_idct_timer(); } @@ -123,27 +123,30 @@ int16_t qcoeff[6 * 64], int16_t data[6*64]) { - int mpeg; int scaler_lum, scaler_chr; + quant_intraFuncPtr quant; - quant_intraFuncPtr const quant[2] = - { - quant_h263_intra, - quant_mpeg_intra - }; + /* check if quant matrices need to be re-initialized with new quant */ + if (pParam->vol_flags & XVID_VOL_MPEGQUANT) { + if (pParam->last_quant_initialized_intra != pMB->quant) { + init_intra_matrix(pParam->mpeg_quant_matrices, pMB->quant); + } + quant = quant_mpeg_intra; + } else { + quant = quant_h263_intra; + } - mpeg = !!(pParam->vol_flags & XVID_VOL_MPEGQUANT); scaler_lum = get_dc_scaler(pMB->quant, 1); scaler_chr = get_dc_scaler(pMB->quant, 0); /* Quantize the block */ start_timer(); - quant[mpeg](&data[0 * 64], &qcoeff[0 * 64], pMB->quant, scaler_lum, pParam->mpeg_quant_matrices); - quant[mpeg](&data[1 * 64], &qcoeff[1 * 64], pMB->quant, scaler_lum, pParam->mpeg_quant_matrices); - quant[mpeg](&data[2 * 64], &qcoeff[2 * 64], pMB->quant, scaler_lum, pParam->mpeg_quant_matrices); - quant[mpeg](&data[3 * 64], &qcoeff[3 * 64], pMB->quant, scaler_lum, pParam->mpeg_quant_matrices); - quant[mpeg](&data[4 * 64], &qcoeff[4 * 64], pMB->quant, scaler_chr, pParam->mpeg_quant_matrices); - quant[mpeg](&data[5 * 64], &qcoeff[5 * 64], pMB->quant, scaler_chr, pParam->mpeg_quant_matrices); + quant(&data[0 * 64], &qcoeff[0 * 64], pMB->quant, scaler_lum, pParam->mpeg_quant_matrices); + quant(&data[1 * 64], &qcoeff[1 * 64], pMB->quant, scaler_lum, pParam->mpeg_quant_matrices); + quant(&data[2 * 64], &qcoeff[2 * 64], pMB->quant, scaler_lum, pParam->mpeg_quant_matrices); + quant(&data[3 * 64], &qcoeff[3 * 64], pMB->quant, scaler_lum, pParam->mpeg_quant_matrices); + quant(&data[4 * 64], &qcoeff[4 * 64], pMB->quant, scaler_chr, pParam->mpeg_quant_matrices); + quant(&data[5 * 64], &qcoeff[5 * 64], pMB->quant, scaler_chr, pParam->mpeg_quant_matrices); stop_quant_timer(); } @@ -183,7 +186,11 @@ int Q, const uint16_t * const Zigzag, const uint16_t * const QuantMatrix, - int Non_Zero); + int Non_Zero, + int Sum, + int Lambda_Mod, + const uint32_t rel_var8, + const int Metric); /* Quantize all blocks -- Inter mode */ static __inline uint8_t @@ -216,7 +223,7 @@ sum = quant[mpeg](&qcoeff[i*64], &data[i*64], pMB->quant, pParam->mpeg_quant_matrices); - if(sum && (frame->vop_flags & XVID_VOP_TRELLISQUANT)) { + if(sum && (pMB->quant > 2) && (frame->vop_flags & XVID_VOP_TRELLISQUANT)) { const uint16_t *matrix; const static uint16_t h263matrix[] = { @@ -234,7 +241,11 @@ sum = dct_quantize_trellis_c(&qcoeff[i*64], &data[i*64], pMB->quant, &scan_tables[0][0], matrix, - 63); + 63, + sum, + pMB->lambda[i], + pMB->rel_var8[i], + !!(frame->vop_flags & XVID_VOP_RD_PSNRHVSM)); } stop_quant_timer(); @@ -308,38 +319,22 @@ uint32_t stride = pParam->edged_width; uint32_t stride2 = stride / 2; uint32_t next_block = stride * 8; - int32_t cst; - int vop_reduced; uint8_t *pY_Cur, *pU_Cur, *pV_Cur; const IMAGE * const pCurrent = &frame->image; - transfer_operation_8to16_t * const functions[2] = - { - (transfer_operation_8to16_t *)transfer_8to16copy, - (transfer_operation_8to16_t *)filter_18x18_to_8x8 - }; - transfer_operation_8to16_t *transfer_op = NULL; - - vop_reduced = !!(frame->vop_flags & XVID_VOP_REDUCED); /* Image pointers */ - pY_Cur = pCurrent->y + (y_pos << (4+vop_reduced)) * stride + (x_pos << (4+vop_reduced)); - pU_Cur = pCurrent->u + (y_pos << (3+vop_reduced)) * stride2 + (x_pos << (3+vop_reduced)); - pV_Cur = pCurrent->v + (y_pos << (3+vop_reduced)) * stride2 + (x_pos << (3+vop_reduced)); - - /* Block size */ - cst = 8<y + (y_pos << 4) * stride + (x_pos << 4); + pU_Cur = pCurrent->u + (y_pos << 3) * stride2 + (x_pos << 3); + pV_Cur = pCurrent->v + (y_pos << 3) * stride2 + (x_pos << 3); /* Do the transfer */ start_timer(); - transfer_op(&data[0 * 64], pY_Cur, stride); - transfer_op(&data[1 * 64], pY_Cur + cst, stride); - transfer_op(&data[2 * 64], pY_Cur + next_block, stride); - transfer_op(&data[3 * 64], pY_Cur + next_block + cst, stride); - transfer_op(&data[4 * 64], pU_Cur, stride2); - transfer_op(&data[5 * 64], pV_Cur, stride2); + transfer_8to16copy(&data[0 * 64], pY_Cur, stride); + transfer_8to16copy(&data[1 * 64], pY_Cur + 8, stride); + transfer_8to16copy(&data[2 * 64], pY_Cur + next_block, stride); + transfer_8to16copy(&data[3 * 64], pY_Cur + next_block + 8, stride); + transfer_8to16copy(&data[4 * 64], pU_Cur, stride2); + transfer_8to16copy(&data[5 * 64], pV_Cur, stride2); stop_transfer_timer(); } @@ -357,48 +352,38 @@ uint32_t stride = pParam->edged_width; uint32_t stride2 = stride / 2; uint32_t next_block = stride * 8; - uint32_t cst; - int vop_reduced; const IMAGE * const pCurrent = &frame->image; - /* Array of function pointers, indexed by [vop_reduced<<1+add] */ - transfer_operation_16to8_t * const functions[4] = + /* Array of function pointers, indexed by [add] */ + transfer_operation_16to8_t * const functions[2] = { (transfer_operation_16to8_t*)transfer_16to8copy, (transfer_operation_16to8_t*)transfer_16to8add, - (transfer_operation_16to8_t*)copy_upsampled_8x8_16to8, - (transfer_operation_16to8_t*)add_upsampled_8x8_16to8 }; transfer_operation_16to8_t *transfer_op = NULL; - /* Makes this vars booleans */ - vop_reduced = !!(frame->vop_flags & XVID_VOP_REDUCED); - /* Image pointers */ - pY_Cur = pCurrent->y + (y_pos << (4+vop_reduced)) * stride + (x_pos << (4+vop_reduced)); - pU_Cur = pCurrent->u + (y_pos << (3+vop_reduced)) * stride2 + (x_pos << (3+vop_reduced)); - pV_Cur = pCurrent->v + (y_pos << (3+vop_reduced)) * stride2 + (x_pos << (3+vop_reduced)); + pY_Cur = pCurrent->y + (y_pos << 4) * stride + (x_pos << 4); + pU_Cur = pCurrent->u + (y_pos << 3) * stride2 + (x_pos << 3); + pV_Cur = pCurrent->v + (y_pos << 3) * stride2 + (x_pos << 3); if (pMB->field_dct) { next_block = stride; stride *= 2; } - /* Block size */ - cst = 8<> 7; + uint16_t u = abs(dQ) << 4; + uint16_t thresh = (t < 65536) ? t : 65535; + + if (u <= thresh) + u = 0; /* The error is not perceivable */ + else + u -= thresh; - while(last--) - sum += abs(C[last]); + u = ((u + iCSF_Round[index]) * iCSF_Coeff[index]) >> 16; - return(sum); + return (((Lambda*u*u)>>4) + 4*Lambda*dQ*dQ) / 5; } /* this routine has been strippen of all debug code */ @@ -799,7 +792,11 @@ int Q, const uint16_t * const Zigzag, const uint16_t * const QuantMatrix, - int Non_Zero) + int Non_Zero, + int Sum, + int Lambda_Mod, + const uint32_t rel_var8, + const int Metric) { /* Note: We should search last non-zero coeffs on *real* DCT input coeffs @@ -809,12 +806,12 @@ * helps. */ typedef struct { int16_t Run, Level; } NODE; - NODE Nodes[65], Last; + NODE Nodes[65], Last = { 0, 0}; uint32_t Run_Costs0[64+1]; uint32_t * const Run_Costs = Run_Costs0 + 1; /* it's 1/lambda, actually */ - const int Lambda = Trellis_Lambda_Tabs[Q-1]; + const int Lambda = (Lambda_Mod*Trellis_Lambda_Tabs[Q-1])>>LAMBDA_EXP; int Run_Start = -1; uint32_t Min_Cost = 2<> 6) : 0; /* source (w/ CBP penalty) */ Run_Costs[-1] = 2<>4); @@ -839,7 +838,7 @@ const int AC = In[Zigzag[i]]; const int Level1 = Out[Zigzag[i]]; - const unsigned int Dist0 = Lambda* AC*AC; + const unsigned int Dist0 = (Metric) ? (calc_mseh(AC, mask, Zigzag[i], Lambda)) : (Lambda* AC*AC); uint32_t Best_Cost = 0xf0000000; Last_Cost += Dist0; @@ -856,7 +855,7 @@ Nodes[i].Level = 1; dQ = Lev0 - AC; } - Cost0 = Lambda*dQ*dQ; + Cost0 = (Metric) ? (calc_mseh(dQ, mask, Zigzag[i], Lambda)) : (Lambda*dQ*dQ); Nodes[i].Run = 1; Best_Cost = (Code_Len20[0]<=- 6) ? B16_17_Code_Len_Last[Level2^-1] : Code_Len0; } - Dist1 = Lambda*dQ1*dQ1; - Dist2 = Lambda*dQ2*dQ2; + if (Metric) { + Dist1 = calc_mseh(dQ1, mask, Zigzag[i], Lambda); + Dist2 = calc_mseh(dQ2, mask, Zigzag[i], Lambda); + } + else { + Dist1 = Lambda*dQ1*dQ1; + Dist2 = Lambda*dQ2*dQ2; + } dDist21 = Dist2-Dist1; for(Run=i-Run_Start; Run>0; --Run) @@ -1002,23 +1007,23 @@ } } - /* It seems trellis doesn't give good results... just compute the Out sum - * and quit */ + /* It seems trellis doesn't give good results... just leave the block untouched + * and return the original sum value */ if (Last_Node<0) - return Compute_Sum(Out, Non_Zero); + return Sum; /* reconstruct optimal sequence backward with surviving paths */ memset(Out, 0x00, 64*sizeof(*Out)); Out[Zigzag[Last_Node]] = Last.Level; i = Last_Node - Last.Run; - sum = 0; + Sum = abs(Last.Level); while(i>=0) { Out[Zigzag[i]] = Nodes[i].Level; - sum += abs(Nodes[i].Level); + Sum += abs(Nodes[i].Level); i -= Nodes[i].Run; } - return sum; + return Sum; } /* original version including heavy debugging info */