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 |
|
|
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); |
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 |
243 |
matrix, |
matrix, |
244 |
63, |
63, |
245 |
sum, |
sum, |
246 |
pMB->lambda[i]); |
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 |
|
|
768 |
|
|
769 |
#define TRELLIS_MIN_EFFORT 3 |
#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, |
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) |
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 |
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 |
|
|
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) |