1 |
/***************************************************************************** |
/***************************************************************************** |
2 |
* |
* |
3 |
* XVID MPEG-4 VIDEO CODEC |
* XVID MPEG-4 VIDEO CODEC |
4 |
* - MB Transfert/Quantization functions - |
* - MB Transfer/Quantization functions - |
5 |
* |
* |
6 |
* Copyright(C) 2001-2003 Peter Ross <pross@xvid.org> |
* Copyright(C) 2001-2010 Peter Ross <pross@xvid.org> |
7 |
* 2001-2003 Michael Militzer <isibaar@xvid.org> |
* 2001-2010 Michael Militzer <michael@xvid.org> |
8 |
* 2003 Edouard Gomez <ed.gomez@free.fr> |
* 2003 Edouard Gomez <ed.gomez@free.fr> |
9 |
* |
* |
10 |
* This program is free software ; you can redistribute it and/or modify |
* This program is free software ; you can redistribute it and/or modify |
40 |
#include "../dct/fdct.h" |
#include "../dct/fdct.h" |
41 |
#include "../dct/idct.h" |
#include "../dct/idct.h" |
42 |
#include "../quant/quant.h" |
#include "../quant/quant.h" |
43 |
|
#include "../motion/sad.h" |
44 |
#include "../encoder.h" |
#include "../encoder.h" |
45 |
|
|
46 |
#include "../quant/quant_matrix.h" |
#include "../quant/quant_matrix.h" |
123 |
int16_t qcoeff[6 * 64], |
int16_t qcoeff[6 * 64], |
124 |
int16_t data[6*64]) |
int16_t data[6*64]) |
125 |
{ |
{ |
|
int mpeg; |
|
126 |
int scaler_lum, scaler_chr; |
int scaler_lum, scaler_chr; |
127 |
|
quant_intraFuncPtr quant; |
128 |
|
|
129 |
quant_intraFuncPtr const quant[2] = |
/* check if quant matrices need to be re-initialized with new quant */ |
130 |
{ |
if (pParam->vol_flags & XVID_VOL_MPEGQUANT) { |
131 |
quant_h263_intra, |
if (pParam->last_quant_initialized_intra != pMB->quant) { |
132 |
quant_mpeg_intra |
init_intra_matrix(pParam->mpeg_quant_matrices, pMB->quant); |
133 |
}; |
} |
134 |
|
quant = quant_mpeg_intra; |
135 |
|
} else { |
136 |
|
quant = quant_h263_intra; |
137 |
|
} |
138 |
|
|
|
mpeg = !!(pParam->vol_flags & XVID_VOL_MPEGQUANT); |
|
139 |
scaler_lum = get_dc_scaler(pMB->quant, 1); |
scaler_lum = get_dc_scaler(pMB->quant, 1); |
140 |
scaler_chr = get_dc_scaler(pMB->quant, 0); |
scaler_chr = get_dc_scaler(pMB->quant, 0); |
141 |
|
|
142 |
/* Quantize the block */ |
/* Quantize the block */ |
143 |
start_timer(); |
start_timer(); |
144 |
quant[mpeg](&data[0 * 64], &qcoeff[0 * 64], pMB->quant, scaler_lum, pParam->mpeg_quant_matrices); |
quant(&data[0 * 64], &qcoeff[0 * 64], pMB->quant, scaler_lum, pParam->mpeg_quant_matrices); |
145 |
quant[mpeg](&data[1 * 64], &qcoeff[1 * 64], pMB->quant, scaler_lum, pParam->mpeg_quant_matrices); |
quant(&data[1 * 64], &qcoeff[1 * 64], pMB->quant, scaler_lum, pParam->mpeg_quant_matrices); |
146 |
quant[mpeg](&data[2 * 64], &qcoeff[2 * 64], pMB->quant, scaler_lum, pParam->mpeg_quant_matrices); |
quant(&data[2 * 64], &qcoeff[2 * 64], pMB->quant, scaler_lum, pParam->mpeg_quant_matrices); |
147 |
quant[mpeg](&data[3 * 64], &qcoeff[3 * 64], pMB->quant, scaler_lum, pParam->mpeg_quant_matrices); |
quant(&data[3 * 64], &qcoeff[3 * 64], pMB->quant, scaler_lum, pParam->mpeg_quant_matrices); |
148 |
quant[mpeg](&data[4 * 64], &qcoeff[4 * 64], pMB->quant, scaler_chr, pParam->mpeg_quant_matrices); |
quant(&data[4 * 64], &qcoeff[4 * 64], pMB->quant, scaler_chr, pParam->mpeg_quant_matrices); |
149 |
quant[mpeg](&data[5 * 64], &qcoeff[5 * 64], pMB->quant, scaler_chr, pParam->mpeg_quant_matrices); |
quant(&data[5 * 64], &qcoeff[5 * 64], pMB->quant, scaler_chr, pParam->mpeg_quant_matrices); |
150 |
stop_quant_timer(); |
stop_quant_timer(); |
151 |
} |
} |
152 |
|
|
187 |
const uint16_t * const Zigzag, |
const uint16_t * const Zigzag, |
188 |
const uint16_t * const QuantMatrix, |
const uint16_t * const QuantMatrix, |
189 |
int Non_Zero, |
int Non_Zero, |
190 |
int Sum); |
int Sum, |
191 |
|
int Lambda_Mod, |
192 |
|
const uint32_t rel_var8, |
193 |
|
const int Metric); |
194 |
|
|
195 |
/* Quantize all blocks -- Inter mode */ |
/* Quantize all blocks -- Inter mode */ |
196 |
static __inline uint8_t |
static __inline uint8_t |
242 |
pMB->quant, &scan_tables[0][0], |
pMB->quant, &scan_tables[0][0], |
243 |
matrix, |
matrix, |
244 |
63, |
63, |
245 |
sum); |
sum, |
246 |
|
pMB->lambda[i], |
247 |
|
pMB->rel_var8[i], |
248 |
|
!!(frame->vop_flags & XVID_VOP_RD_PSNRHVSM)); |
249 |
} |
} |
250 |
stop_quant_timer(); |
stop_quant_timer(); |
251 |
|
|
766 |
return -1; |
return -1; |
767 |
} |
} |
768 |
|
|
769 |
|
#define TRELLIS_MIN_EFFORT 3 |
770 |
|
|
771 |
|
static __inline uint32_t calc_mseh(int16_t dQ, uint16_t mask, |
772 |
|
const int index, const int Lambda) |
773 |
|
{ |
774 |
|
uint32_t t = (mask * Inv_iMask_Coeff[index] + 32) >> 7; |
775 |
|
uint16_t u = abs(dQ) << 4; |
776 |
|
uint16_t thresh = (t < 65536) ? t : 65535; |
777 |
|
|
778 |
|
if (u <= thresh) |
779 |
|
u = 0; /* The error is not perceivable */ |
780 |
|
else |
781 |
|
u -= thresh; |
782 |
|
|
783 |
|
u = ((u + iCSF_Round[index]) * iCSF_Coeff[index]) >> 16; |
784 |
|
|
785 |
|
return (((Lambda*u*u)>>4) + 4*Lambda*dQ*dQ) / 5; |
786 |
|
} |
787 |
|
|
788 |
/* this routine has been strippen of all debug code */ |
/* this routine has been strippen of all debug code */ |
789 |
static int |
static int |
790 |
dct_quantize_trellis_c(int16_t *const Out, |
dct_quantize_trellis_c(int16_t *const Out, |
793 |
const uint16_t * const Zigzag, |
const uint16_t * const Zigzag, |
794 |
const uint16_t * const QuantMatrix, |
const uint16_t * const QuantMatrix, |
795 |
int Non_Zero, |
int Non_Zero, |
796 |
int Sum) |
int Sum, |
797 |
|
int Lambda_Mod, |
798 |
|
const uint32_t rel_var8, |
799 |
|
const int Metric) |
800 |
{ |
{ |
801 |
|
|
802 |
/* Note: We should search last non-zero coeffs on *real* DCT input coeffs |
/* Note: We should search last non-zero coeffs on *real* DCT input coeffs |
811 |
uint32_t * const Run_Costs = Run_Costs0 + 1; |
uint32_t * const Run_Costs = Run_Costs0 + 1; |
812 |
|
|
813 |
/* it's 1/lambda, actually */ |
/* it's 1/lambda, actually */ |
814 |
const int Lambda = Trellis_Lambda_Tabs[Q-1]; |
const int Lambda = (Lambda_Mod*Trellis_Lambda_Tabs[Q-1])>>LAMBDA_EXP; |
815 |
|
|
816 |
int Run_Start = -1; |
int Run_Start = -1; |
817 |
uint32_t Min_Cost = 2<<TL_SHIFT; |
uint32_t Min_Cost = 2<<TL_SHIFT; |
821 |
|
|
822 |
int i, j; |
int i, j; |
823 |
|
|
824 |
|
uint32_t mask = (Metric) ? ((isqrt(2*coeff8_energy(In)*rel_var8) + 48) >> 6) : 0; |
825 |
|
|
826 |
/* source (w/ CBP penalty) */ |
/* source (w/ CBP penalty) */ |
827 |
Run_Costs[-1] = 2<<TL_SHIFT; |
Run_Costs[-1] = 2<<TL_SHIFT; |
828 |
|
|
829 |
Non_Zero = Find_Last(Out, Zigzag, Non_Zero); |
Non_Zero = Find_Last(Out, Zigzag, Non_Zero); |
830 |
if (Non_Zero<0) |
if (Non_Zero < TRELLIS_MIN_EFFORT) |
831 |
return 0; /* Sum is zero if there are only zero coeffs */ |
Non_Zero = TRELLIS_MIN_EFFORT; |
832 |
|
|
833 |
for(i=0; i<=Non_Zero; i++) { |
for(i=0; i<=Non_Zero; i++) { |
834 |
const int q = ((Q*QuantMatrix[Zigzag[i]])>>4); |
const int q = ((Q*QuantMatrix[Zigzag[i]])>>4); |
838 |
|
|
839 |
const int AC = In[Zigzag[i]]; |
const int AC = In[Zigzag[i]]; |
840 |
const int Level1 = Out[Zigzag[i]]; |
const int Level1 = Out[Zigzag[i]]; |
841 |
const unsigned int Dist0 = Lambda* AC*AC; |
const unsigned int Dist0 = (Metric) ? (calc_mseh(AC, mask, Zigzag[i], Lambda)) : (Lambda* AC*AC); |
842 |
uint32_t Best_Cost = 0xf0000000; |
uint32_t Best_Cost = 0xf0000000; |
843 |
Last_Cost += Dist0; |
Last_Cost += Dist0; |
844 |
|
|
855 |
Nodes[i].Level = 1; |
Nodes[i].Level = 1; |
856 |
dQ = Lev0 - AC; |
dQ = Lev0 - AC; |
857 |
} |
} |
858 |
Cost0 = Lambda*dQ*dQ; |
Cost0 = (Metric) ? (calc_mseh(dQ, mask, Zigzag[i], Lambda)) : (Lambda*dQ*dQ); |
859 |
|
|
860 |
Nodes[i].Run = 1; |
Nodes[i].Run = 1; |
861 |
Best_Cost = (Code_Len20[0]<<TL_SHIFT) + Run_Costs[i-1]+Cost0; |
Best_Cost = (Code_Len20[0]<<TL_SHIFT) + Run_Costs[i-1]+Cost0; |
910 |
Tbl_L2_Last = (Level2>=- 6) ? B16_17_Code_Len_Last[Level2^-1] : Code_Len0; |
Tbl_L2_Last = (Level2>=- 6) ? B16_17_Code_Len_Last[Level2^-1] : Code_Len0; |
911 |
} |
} |
912 |
|
|
913 |
|
if (Metric) { |
914 |
|
Dist1 = calc_mseh(dQ1, mask, Zigzag[i], Lambda); |
915 |
|
Dist2 = calc_mseh(dQ2, mask, Zigzag[i], Lambda); |
916 |
|
} |
917 |
|
else { |
918 |
Dist1 = Lambda*dQ1*dQ1; |
Dist1 = Lambda*dQ1*dQ1; |
919 |
Dist2 = Lambda*dQ2*dQ2; |
Dist2 = Lambda*dQ2*dQ2; |
920 |
|
} |
921 |
dDist21 = Dist2-Dist1; |
dDist21 = Dist2-Dist1; |
922 |
|
|
923 |
for(Run=i-Run_Start; Run>0; --Run) |
for(Run=i-Run_Start; Run>0; --Run) |